*** empty log message ***
[gnus] / lisp / gnus.el
1 ;;; (ding) Gnus --- a newsreader for GNU Emacs
2 ;; Copyright (C) 1987,88,89,90,93,94,95 Free Software Foundation, Inc.
3
4 ;; Author: Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
5 ;;      Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23
24 ;;; Commentary:
25
26 ;; Although (ding) Gnus looks suspiciously like GNUS, it isn't quite
27 ;; the same beast. Most internal structures have been changed. If you
28 ;; have written packages that depend on any of the hash tables,
29 ;; `gnus-newsrc-alist', `gnus-killed-assoc', marked lists, the .newsrc
30 ;; buffer, or internal knowledge of the `nntp-header-' macros, or
31 ;; dependence on the buffers having a certain format, your code will
32 ;; fail.
33
34 ;;; Code:
35
36 (require 'mail-utils)
37 (require 'timezone)
38 (require 'nnheader)
39
40 ;; Site dependent variables. These variables should be defined in
41 ;; paths.el.
42
43 (defvar gnus-default-nntp-server nil
44   "Specify a default NNTP server.
45 This variable should be defined in paths.el, and should never be set
46 by the user.
47 If you want to change servers, you should use `gnus-select-method'.
48 See the documentation to that variable.")
49
50 (defconst gnus-backup-default-subscribed-newsgroups 
51   '("news.announce.newusers" "news.groups.questions" "gnu.emacs.gnus")
52   "Default default new newsgroups the first time Gnus is run.
53 Should be set in paths.el, and shouldn't be touched by the user.")
54
55 (defvar gnus-local-domain nil
56   "Local domain name without a host name.
57 The DOMAINNAME environment variable is used instead if it is defined.
58 If the `system-name' function returns the full Internet name, there is
59 no need to set this variable.")
60
61 (defvar gnus-local-organization nil
62   "String with a description of what organization (if any) the user belongs to.
63 The ORGANIZATION environment variable is used instead if it is defined.
64 If this variable contains a function, this function will be called
65 with the current newsgroup name as the argument. The function should
66 return a string.
67 In any case, if the string (either in the variable, in the environment
68 variable, or returned by the function) is a file name, the contents of
69 this file will be used as the organization.")
70
71 (defvar gnus-use-generic-from nil
72   "If nil, the full host name will be the system name prepended to the domain name.
73 If this is a string, the full host name will be this string.
74 If this is non-nil, non-string, the domain name will be used as the
75 full host name.")
76
77 (defvar gnus-use-generic-path nil
78   "If nil, use the NNTP server name in the Path header.
79 If stringp, use this; if non-nil, use no host name (user name only).")
80
81
82 ;; Customization variables
83
84 (defvar gnus-select-method 
85   (list 'nntp (or (getenv "NNTPSERVER") 
86                   (if (and gnus-default-nntp-server
87                            (not (string= gnus-default-nntp-server "")))
88                       gnus-default-nntp-server)
89                   (system-name)))
90   "*Default method for selecting a newsgroup.
91 This variable should be a list, where the first element is how the
92 news is to be fetched, the second is the address. 
93
94 For instance, if you want to get your news via NNTP from
95 \"flab.flab.edu\", you could say:
96
97 (setq gnus-select-method '(nntp \"flab.flab.edu\"))
98
99 If you want to use your local spool, say:
100
101 (setq gnus-select-method (list 'nnspool (system-name)))
102
103 If you use this variable, you must set `gnus-nntp-server' to nil.
104
105 There is a lot more to know about select methods and virtual servers -
106 see the manual for details.")
107
108 ;; Added by Sudish Joseph <joseph@cis.ohio-state.edu>.
109 (defvar gnus-post-method nil
110   "*Preferred method for posting USENET news.
111 If this variable is nil, Gnus will use the current method to decide
112 which method to use when posting.  If it is non-nil, it will override
113 the current method. This method will not be used in mail groups and
114 the like, only in \"real\" newsgroups.
115
116 The value must be a valid method as discussed in the documentation of
117 `gnus-select-method'.")
118
119 (defvar gnus-refer-article-method nil
120   "*Preferred method for fetching an article by Message-ID.
121 If you are reading news from the local spool (with nnspool), fetching
122 articles by Message-ID is painfully slow. By setting this method to an
123 nntp method, you might get acceptable results.
124
125 The value of this variable must be a valid select method as discussed
126 in the documentation of `gnus-select-method'")
127
128 (defvar gnus-secondary-select-methods nil
129   "*A list of secondary methods that will be used for reading news.
130 This is a list where each element is a complete select methdod (see
131 `gnus-select-method').  
132
133 If, for instance, you want to read your mail with the nnml backend,
134 you could set this variable:
135
136 (setq gnus-secondary-select-methods '((nnml \"\"))")
137
138 (defvar gnus-secondary-servers nil
139   "*List of NNTP servers that the user can choose between interactively.
140 To make Gnus query you for a server, you have to give `gnus' a
141 non-numeric prefix - `C-u M-x gnus', in short.")
142
143 (defvar gnus-nntp-server nil
144   "*The name of the host running the NNTP server.
145 This variable is semi-obsolete. Use the `gnus-select-method'
146 variable instead.")
147
148 (defvar gnus-nntp-service "nntp"
149   "*NNTP service name (\"nntp\" or 119).
150 This is an obsolete variable, which is scarcely used. If you use an
151 nntp server for your newsgroup and want to change the port number
152 used to 899, you would say something along these lines:
153
154  (setq gnus-select-method '(nntp \"my.nntp.server\" (nntp-port-number 899)))")
155
156 (defvar gnus-startup-file "~/.newsrc"
157   "*Your `.newsrc' file.
158 `.newsrc-SERVER' will be used instead if that exists.")
159
160 (defvar gnus-signature-file "~/.signature"
161   "*Your signature file.
162 If the variable is a string that doesn't correspond to a file, the
163 string itself is inserted.")
164
165 (defvar gnus-signature-function nil
166   "*A function that should return a signature file name.
167 The function will be called with the name of the newsgroup being
168 posted to.
169 If the function returns a string that doesn't correspond to a file, the
170 string itself is inserted.
171 If the function returns nil, the `gnus-signature-file' variable will
172 be used instead.")
173
174 (defvar gnus-init-file "~/.gnus"
175   "*Your Gnus elisp startup file.
176 If a file with the .el or .elc suffixes exist, it will be read
177 instead.") 
178
179 (defvar gnus-group-faq-directory
180   "/anonymous@rtfm.mit.edu:/pub/usenet-by-group/"
181   "*Directory where the group FAQs are stored.
182 This will most commonly be on a remote machine, and the file will be
183 fetched by ange-ftp.")
184
185 (defvar gnus-group-archive-directory
186   "/ftp@sina.tcamc.uh.edu:/pub/emacs/ding-list/" 
187   "*The address of the (ding) archives.")
188
189 (defvar gnus-default-subscribed-newsgroups nil
190   "*This variable lists what newsgroups should be subscribed the first time Gnus is used.
191 It should be a list of strings.
192 If it is `t', Gnus will not do anything special the first time it is
193 started; it'll just use the normal newsgroups subscription methods.")
194
195 (defvar gnus-post-prepare-function nil
196   "*Function that is run after a post buffer has been prepared.
197 It is called with the name of the newsgroup that is posted to. It
198 might be used, for instance, for inserting signatures based on the
199 newsgroup name. (In that case, `gnus-signature-file' and
200 `mail-signature' should both be set to nil).")
201
202 (defvar gnus-use-cross-reference t
203   "*Non-nil means that cross referenced articles will be marked as read.
204 If nil, ignore cross references.  If t, mark articles as read in
205 subscribed newsgroups. If neither t nor nil, mark as read in all
206 newsgroups.") 
207
208 (defvar gnus-use-dribble-file t
209   "*Non-nil means that Gnus will use a dribble file to store user updates.
210 If Emacs should crash without saving the .newsrc files, complete
211 information can be restored from the dribble file.")
212
213 (defvar gnus-use-followup-to 'use
214   "*Specifies what to do with Followup-To header.
215 If nil, ignore the header. If it is t, use its value, but ignore 
216 `poster'. If it is neither nil nor t, which is the default, always use
217 the value.") 
218
219 (defvar gnus-followup-to-function nil
220   "*A variable that contains a function that returns a followup address.
221 The function will be called in the buffer of the article that is being
222 followed up. The buffer will be narrowed to the headers of the
223 article. To pick header headers, one might use `mail-fetch-field'.  The
224 function will be called with the name of the current newsgroup as the
225 argument.
226
227 Here's an example `gnus-followup-to-function':
228
229 (setq gnus-followup-to-function
230       (lambda (group)
231         (cond ((string= group \"mail.list\")
232                (or (mail-fetch-field \"sender\") 
233                    (mail-fetch-field \"from\")))
234               (t
235                (or (mail-fetch-field \"reply-to\") 
236                    (mail-fetch-field \"from\"))))))")
237
238 (defvar gnus-reply-to-function nil
239   "*A variable that contains a function that returns a reply address.
240 See the `gnus-followup-to-function' variable for an explanation of how
241 this variable is used.
242
243 This function should return a string that will be used to fill in the
244 header.  This function may also return a list.  In that case, every
245 list element should be a cons where the first car should be a string
246 with the header name, and the cdr should be a string with the header
247 value.")
248
249 (defvar gnus-asynchronous nil
250   "*If non-nil, Gnus will supply backends with data needed for async article fetching.")
251
252 (defvar gnus-asynchronous-article-function nil
253   "*Function for picking articles to pre-fetch, possibly.")
254
255 (defvar gnus-large-newsgroup 200
256   "*The number of articles which indicates a large newsgroup.
257 If the number of articles in a newsgroup is greater than this value,
258 confirmation is required for selecting the newsgroup.")
259
260 ;; Suggested by Andrew Eskilsson <pi92ae@lelle.pt.hk-r.se>.
261 (defvar gnus-no-groups-message "No news is horrible news"
262   "*Message displayed by Gnus when no groups are available.")
263
264 (defvar gnus-author-copy (getenv "AUTHORCOPY")
265   "*Save outgoing articles in this file.
266 Initialized from the AUTHORCOPY environment variable.
267
268 If this variable begins with the character \"|\", outgoing articles
269 will be piped to the named program. It is possible to save an article
270 in an MH folder as follows:
271
272 \(setq gnus-author-copy \"|/usr/local/lib/mh/rcvstore +Article\")
273
274 If the first character is not a pipe, articles are saved using the
275 function specified by the `gnus-author-copy-saver' variable.")
276
277 (defvar gnus-mail-self-blind nil
278   "*Non-nil means insert a BCC header in all outgoing articles.
279 This will result in having a copy of the article mailed to yourself.
280 The BCC header is inserted when the post buffer is initialized, so you
281 can remove or alter the BCC header to override the default.")
282
283 (defvar gnus-author-copy-saver (function rmail-output)
284   "*A function called to save outgoing articles.
285 This function will be called with the same of the file to store the
286 article in. The default function is `rmail-output' which saves in Unix
287 mailbox format.")
288
289 (defvar gnus-use-long-file-name (not (memq system-type '(usg-unix-v xenix)))
290   "*Non-nil means that the default name of a file to save articles in is the group name.
291 If it's nil, the directory form of the group name is used instead.
292
293 If this variable is a list, and the list contains the element
294 `not-score', long file names will not be used for score files; if it
295 contains the element `not-save', long file names will not be used for
296 saving; and if it contains the element `not-kill', long file names
297 will not be used for kill files.")
298
299 (defvar gnus-article-save-directory (or (getenv "SAVEDIR") "~/News/")
300   "*Name of the directory articles will be saved in (default \"~/News\").
301 Initialized from the SAVEDIR environment variable.")
302
303 (defvar gnus-kill-files-directory (or (getenv "SAVEDIR") "~/News/")
304   "*Name of the directory where kill files will be stored (default \"~/News\").
305 Initialized from the SAVEDIR environment variable.")
306
307 (defvar gnus-default-article-saver 'gnus-summary-save-in-rmail
308   "*A function to save articles in your favorite format.
309 The function must be interactively callable (in other words, it must
310 be an Emacs command).
311
312 Gnus provides the following functions:
313
314 * gnus-summary-save-in-rmail (Rmail format)
315 * gnus-summary-save-in-mail (Unix mail format)
316 * gnus-summary-save-in-folder (MH folder)
317 * gnus-summary-save-in-file (article format).")
318
319 (defvar gnus-rmail-save-name (function gnus-plain-save-name)
320   "*A function generating a file name to save articles in Rmail format.
321 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
322
323 (defvar gnus-mail-save-name (function gnus-plain-save-name)
324   "*A function generating a file name to save articles in Unix mail format.
325 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
326
327 (defvar gnus-folder-save-name (function gnus-folder-save-name)
328   "*A function generating a file name to save articles in MH folder.
329 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER.")
330
331 (defvar gnus-file-save-name (function gnus-numeric-save-name)
332   "*A function generating a file name to save articles in article format.
333 The function is called with NEWSGROUP, HEADERS, and optional
334 LAST-FILE.")
335
336 (defvar gnus-split-methods nil
337   "*Variable used to suggest where articles are to be saved.
338 The syntax of this variable is the same as `nnmail-split-methods'.  
339
340 For instance, if you would like to save articles related to Gnus in
341 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
342 you could set this variable to something like:
343
344  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
345    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))")
346
347 (defvar gnus-score-interactive-default-score 1000
348   "*Scoring commands will raise/lower the score with this number as the default.")
349
350 (defvar gnus-save-score nil
351   "*If non-nil, save group scoring info.")
352
353 (defvar gnus-score-file-suffix "SCORE"
354   "*Suffix of the score files.")
355
356 (defvar gnus-use-adaptive-scoring nil
357   "*If non-nil, use some adaptive scoring scheme.")
358
359 (defvar gnus-use-cache nil
360   "*If non-nil, Gnus will cache (some) articles locally.")
361
362 (defvar gnus-fetch-old-headers nil
363   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
364 If an unread article in the group refers to an older, already read (or
365 just marked as read) article, the old article will not normally be
366 displayed in the Summary buffer.  If this variable is non-nil, Gnus
367 will attempt to grab the headers to the old articles, and thereby
368 build complete threads.  If it has the value `some', only enough
369 headers to connect otherwise loose threads will be displayed.
370
371 The server has to support XOVER for any of this to work.")
372
373 (defvar gnus-visual t
374   "*If non-nil, will do various highlighting.
375 If nil, no mouse highlights (or any other highlights) will be
376 performed.  This might speed up Gnus some when generating large group
377 and summary buffers.")
378
379 (defvar gnus-novice-user t
380   "*Non-nil means that you are a usenet novice.
381 If non-nil, verbose messages may be displayed and confirmations may be
382 required.")
383
384 (defvar gnus-expert-user nil
385   "*Non-nil means that you will never be asked for confirmation about anything.
386 And that means *anything*.")
387
388 (defvar gnus-keep-same-level nil
389   "*Non-nil means that the next newsgroup after the current will be on the same level.
390 When you type, for instance, `n' after reading the last article in the
391 current newsgroup, you will go to the next newsgroup. If this variable
392 is nil, the next newsgroup will be the next from the group
393 buffer. 
394 If this variable is non-nil, Gnus will either put you in the
395 next newsgroup with the same level, or, if no such newsgroup is
396 available, the next newsgroup with the lowest possible level higher
397 than the current level.
398 If this variable is `best', Gnus will make the next newsgroup the one
399 with the best level.")
400
401 (defvar gnus-summary-make-false-root 'adopt
402   "*nil means that Gnus won't gather loose threads.
403 If the root of a thread has expired or been read in a previous
404 session, the information necessary to build a complete thread has been
405 lost. Instead of having many small sub-threads from this original thread
406 scattered all over the summary buffer, Gnus can gather them. 
407
408 If non-nil, Gnus will try to gather all loose sub-threads from an
409 original thread into one large thread.
410
411 If this variable is non-nil, it should be one of `none', `adopt',
412 `dummy' or `empty'.
413
414 If this variable is `none', Gnus will not make a false root, but just
415 present the sub-threads after another.
416 If this variable is `dummy', Gnus will create a dummy root that will
417 have all the sub-threads as children.
418 If this variable is `adopt', Gnus will make one of the \"children\"
419 the parent and mark all the step-children as such.
420 If this variable is `empty', the \"children\" are printed with empty
421 subject fields.  (Or rather, they will be printed with a string
422 given by the `gnus-summary-same-subject' variable.)")
423
424 (defvar gnus-summary-gather-subject-limit nil
425   "*Maximum length of subject comparisons when gathering loose threads.
426 Use nil to compare full subjects.  Setting this variable to a low
427 number will help gather threads that have been corrupted by
428 newsreaders chopping off subject lines, but it might also mean that
429 unrelated articles that have subject that happen to begin with the
430 same few characters will be incorrectly gathered.
431
432 If this variable is `fuzzy', Gnus will use a fuzzy algortihm when
433 comparing subjects.")
434
435 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
436 (defvar gnus-summary-same-subject ""
437   "*String indicating that the current article has the same subject as the previous.
438 This variable will only be used if the value of
439 `gnus-summary-make-false-root' is `empty'.")
440
441 (defvar gnus-summary-goto-unread nil
442   "*If non-nil, marking commands will go to the next unread article.")
443
444 (defvar gnus-check-new-newsgroups t
445   "*Non-nil means that Gnus will add new newsgroups at startup.
446 If this variable is `ask-server', Gnus will ask the server for new
447 groups since the last time it checked. This means that the killed list
448 is no longer necessary, so you could set `gnus-save-killed-list' to
449 nil. 
450
451 A variant is to have this variable be a list of select methods. Gnus
452 will then use the `ask-server' method on all these select methods to
453 query for new groups from all those servers.
454
455 Eg.
456   (setq gnus-check-new-newsgroups 
457         '((nntp \"some.server\") (nntp \"other.server\")))
458
459 If this variable is nil, then you have to tell Gnus explicitly to
460 check for new newsgroups with \\<gnus-group-mode-map>\\[gnus-find-new-newsgroups].")
461
462 (defvar gnus-check-bogus-newsgroups nil
463   "*Non-nil means that Gnus will check and remove bogus newsgroup at startup.
464 If this variable is nil, then you have to tell Gnus explicitly to
465 check for bogus newsgroups with \\<gnus-group-mode-map>\\[gnus-group-check-bogus-groups].")
466
467 (defvar gnus-read-active-file t
468   "*Non-nil means that Gnus will read the entire active file at startup.
469 If this variable is nil, Gnus will only know about the groups in your
470 `.newsrc' file.
471
472 If this variable is `some', Gnus will try to only read the relevant
473 parts of the active file from the server.  Not all servers support
474 this, and it might be quite slow with other servers, but this should
475 generally be faster than both the t and nil value.
476
477 If you set this variable to nil or `some', you probably still want to
478 be told about new newsgroups that arrive.  To do that, set
479 `gnus-check-new-newsgroups' to `ask-server'.  This may not work
480 properly with all servers.")
481
482 (defvar gnus-level-subscribed 5
483   "*Groups with levels less than or equal to this variable are subscribed.")
484
485 (defvar gnus-level-unsubscribed 7
486   "*Groups with levels less than or equal to this variable are unsubscribed.
487 Groups with levels less than `gnus-level-subscribed', which should be
488 less than thiss variable, are subscribed.")
489
490 (defvar gnus-level-zombie 8
491   "*Groups with this level are zombie groups.")
492
493 (defvar gnus-level-killed 9
494   "*Groups with this level are killed.")
495
496 (defvar gnus-level-default-subscribed 3
497   "*New subscribed groups will be subscribed at this level.")
498
499 (defvar gnus-level-default-unsubscribed 6
500   "*New unsubscribed groups will be unsubscribed at this level.")
501
502 (defvar gnus-activate-foreign-newsgroups nil
503   "*If nil, Gnus will not check foreign newsgroups at startup.
504 If it is non-nil, it should be a number between one and nine. Foreign
505 newsgroups that have a level lower or equal to this number will be
506 activated on startup. For instance, if you want to active all
507 subscribed newsgroups, but not the rest, you'd set this variable to 
508 `gnus-level-subscribed'.
509
510 If you subscribe to lots of newsgroups from different servers, startup
511 might take a while. By setting this variable to nil, you'll save time,
512 but you won't be told how many unread articles there are in the
513 groups.")
514
515 (defvar gnus-save-newsrc-file t
516   "*Non-nil means that Gnus will save the `.newsrc' file.
517 Gnus always saves its own startup file, which is called
518 \".newsrc.eld\".  The file called \".newsrc\" is in a format that can
519 be readily understood by other newsreaders.  If you don't plan on
520 using other newsreaders, set this variable to nil to save some time on
521 exit.")
522
523 (defvar gnus-save-killed-list t
524   "*If non-nil, save the list of killed groups to the startup file.
525 This will save both time (when starting and quitting) and space (both
526 memory and disk), but it will also mean that Gnus has no record of
527 which groups are new and which are old, so the automatic new
528 newsgroups subscription methods become meaningless. You should always
529 set `gnus-check-new-newsgroups' to `ask-server' or nil if you set this
530 variable to nil.")
531
532 (defvar gnus-interactive-catchup t
533   "*If non-nil, require your confirmation when catching up a group.")
534
535 (defvar gnus-interactive-post t
536   "*If non-nil, group name will be asked for when posting.")
537
538 (defvar gnus-interactive-exit t
539   "*If non-nil, require your confirmation when exiting Gnus.")
540
541 (defvar gnus-kill-killed nil
542   "*If non-nil, Gnus will apply kill files to already killed articles.
543 If it is nil, Gnus will never apply kill files to articles that have
544 already been through the scoring process, which might very well save lots
545 of time.")
546
547 (defvar gnus-extract-address-components 'gnus-extract-address-components
548   "*Function for extracting address components from a From header.
549 Two pre-defined function exist: `gnus-extract-address-components',
550 which is the default, quite fast, and too simplistic solution, and
551 `mail-extract-address-components', which works much better, but is
552 slower.")
553
554 (defvar gnus-summary-default-score 0
555   "*Default article score level.
556 If this variable is nil, scoring will be disabled.")
557
558 (defvar gnus-group-default-list-level gnus-level-subscribed
559   "*Default listing level.")
560
561 (defvar gnus-group-always-list-unread gnus-level-subscribed
562   "*Always list groups less than this variable with unread articles. 
563 If nil, use parameter to specify.")
564
565 (defvar gnus-user-login-name nil
566   "*The login name of the user.
567 Got from the function `user-login-name' if undefined.")
568
569 (defvar gnus-user-full-name nil
570   "*The full name of the user.
571 Got from the NAME environment variable if undefined.")
572
573 (defvar gnus-user-from-line nil
574   "*Your full, complete e-mail address.  
575 Overrides the other Gnus variables if it is non-nil.
576
577 Here are two example values of this variable:
578
579  \"Lars Magne Ingebrigtsen <larsi@ifi.uio.no>\"
580
581 and
582
583  \"larsi@ifi.uio.no (Lars Magne Ingebrigtsen)\"
584
585 The first version is recommended, but the name has to be quoted if it
586 contains non-alphanumerical characters.")
587
588 (defvar gnus-show-mime nil
589   "*If non-ni, do mime processing of articles.
590 The articles will simply be fed to the function given by
591 `gnus-show-mime-method'.")
592
593 (defvar gnus-show-mime-method (function metamail-buffer)
594   "*Function to process a MIME message.
595 The function is called from the article buffer.")
596
597 (defvar gnus-show-threads t
598   "*If non-nil, display threads in summary mode.")
599
600 (defvar gnus-thread-hide-subtree nil
601   "*If non-nil, hide all threads initially.
602 If threads are hidden, you have to run the command
603 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
604 to expose hidden threads.")
605
606 (defvar gnus-thread-hide-killed t
607   "*If non-nil, hide killed threads automatically.")
608
609 (defvar gnus-thread-ignore-subject nil
610   "*If non-nil, ignore subjects and do all threading based on the Reference header.
611 If nil, which is the default, articles that have different subjects
612 from their parents will start separate threads.")
613
614 (defvar gnus-thread-indent-level 4
615   "*Number that says how much each sub-thread should be indented.")
616
617 ;; jwz: nuke newsgroups whose name is all digits - that means that
618 ;; some loser has let articles get into the root of the news spool,
619 ;; which is toxic. Lines beginning with whitespace also tend to be
620 ;; toxic.
621 (defvar gnus-ignored-newsgroups
622   (purecopy (mapconcat 'identity
623                        '("^to\\."       ; not "real" groups
624                          "^[0-9. \t]+ " ; all digits in name
625                          "[][\"#'()     ;\\]"   ; bogus characters
626                          )
627                        "\\|"))
628   "*A regexp to match uninteresting newsgroups in the active file.
629 Any lines in the active file matching this regular expression are
630 removed from the newsgroup list before anything else is done to it,
631 thus making them effectively non-existant.")
632
633 (defvar gnus-ignored-headers
634   "^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:"
635   "*All headers that match this regexp will be hidden.
636 Also see `gnus-visible-headers'.")
637
638 (defvar gnus-visible-headers "^From:\\|^Newsgroups:\\|^Subject:\\|^Date:\\|^Followup-To:\\|^Reply-To:\\|^Organization:\\|^Summary:\\|^Keywords:\\|^To:\\|^Cc:"
639   "*All headers that do not match this regexp will be hidden.
640 Also see `gnus-ignored-headers'.")
641
642 (defvar gnus-sorted-header-list
643   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:" "^To:" 
644     "^Cc:" "^Date:" "^Organization:")
645   "*This variable is a list of regular expressions.
646 If it is non-nil, headers that match the regular expressions will
647 be placed first in the article buffer in the sequence specified by
648 this list.")
649
650 (defvar gnus-check-before-posting t
651   "In non-nil, Gnus will attempt to run some checks on outgoing posts.")
652
653 (defvar gnus-required-headers
654   '(From Date Newsgroups Subject Message-ID Organization Lines X-Newsreader)
655   "*Headers to be generated or prompted for when posting an article.
656 RFC977 and RFC1036 require From, Date, Newsgroups, Subject,
657 Message-ID.  Organization, Lines and X-Newsreader are optional.  If
658 you want Gnus not to insert some header, remove it from this list.")
659
660 (defvar gnus-show-all-headers nil
661   "*If non-nil, don't hide any headers.")
662
663 (defvar gnus-save-all-headers t
664   "*If non-nil, don't remove any headers before saving.")
665
666 (defvar gnus-inhibit-startup-message nil
667   "*If non-nil, the startup message will not be displayed.")
668
669 (defvar gnus-auto-extend-newsgroup t
670   "*If non-nil, extend newsgroup forward and backward when requested.")
671
672 (defvar gnus-auto-select-first t
673   "*If non-nil, select the first unread article when entering a group.
674 If you want to prevent automatic selection of the first unread article
675 in some newsgroups, set the variable to nil in
676 `gnus-select-group-hook'.") 
677
678 (defvar gnus-auto-select-next t
679   "*If non-nil, offer to go to the next group from the end of the previous.
680 If the value is t and the next newsgroup is empty, Gnus will exit
681 summary mode and go back to group mode.  If the value is neither nil
682 nor t, Gnus will select the following unread newsgroup.  In
683 particular, if the value is the symbol `quietly', the next unread
684 newsgroup will be selected without any confirmations.")
685
686 (defvar gnus-auto-select-same nil
687   "*If non-nil, select the next article with the same subject.")
688
689 (defvar gnus-summary-check-current nil
690   "*If non-nil, consider the current article when moving.
691 The \"unread\" movement commands will stay on the same line if the
692 current article is unread.")
693
694 (defvar gnus-auto-center-summary t
695   "*If non-nil, always center the current summary buffer.")
696
697 (defvar gnus-auto-mail-to-author nil
698   "*If non-nil, mail the authors of articles a copy of your follow-ups.
699 If this variable is `ask', the user will be prompted for whether to
700 mail a copy.  The string given by `gnus-mail-courtesy-message' will be
701 inserted at the beginning of the mail copy.
702
703 Mail is sent using the function specified by the
704 `gnus-mail-send-method' variable.")
705
706 ;; Added by Ethan Bradford <ethanb@ptolemy.astro.washington.edu>.
707 (defvar gnus-mail-courtesy-message
708   "The following message is a courtesy copy of an article\nthat has been posted as well.\n\n"
709   "*This is inserted at the start of a mailed copy of a posted message.
710 If this variable is nil, no such courtesy message will be added.")
711
712 (defvar gnus-break-pages t
713   "*If non-nil, do page breaking on articles.
714 The page delimiter is specified by the `gnus-page-delimiter'
715 variable.")
716
717 (defvar gnus-page-delimiter "^\^L"
718   "*Regexp describing what to use as article page delimiters.
719 The default value is \"^\^L\", which is a form linefeed at the
720 beginning of a line.")
721
722 (defvar gnus-use-full-window t
723   "*If non-nil, use the entire Emacs screen.")
724
725 ; for split windows. maybe a better way?
726 (defvar gnus-split-window nil
727   "*If non-nil, put the article buffer in left-hand side of the window .")
728
729 (defvar gnus-window-configuration
730   '((newsgroups (1 0 0))
731     (summary (0 1 0))
732     (article (0 3 10)))
733   "*Specify window configurations for each action.
734 The format of the variable is either a list of (ACTION (G S A)), where
735 G, S, and A are the relative height of group, summary, and article
736 windows, respectively, or a list of (ACTION FUNCTION), where FUNCTION
737 is a function that will be called with ACTION as an argument. ACTION
738 can be `summary', `newsgroups', or `article'.")
739
740 (defvar gnus-mail-reply-method (function gnus-mail-reply-using-mail)
741   "*Function to compose a reply.
742 Two pre-made functions are `gnus-mail-reply-using-mail' (sendmail) and
743 `gnus-mail-reply-using-mhe' (MH-E).")
744
745 (defvar gnus-mail-forward-method (function gnus-mail-forward-using-mail)
746   "*Function to forward the current message to another user.
747 Two pre-made functions are `gnus-mail-forward-using-mail' (sendmail)
748 and `gnus-mail-forward-using-mhe' (MH-E).") 
749
750 (defvar gnus-mail-other-window-method 'gnus-mail-other-window-using-mail
751   "*Function to compose mail in the other window.
752 Two pre-made functions are `gnus-mail-other-window-using-mail'
753 (sendmail) and `gnus-mail-other-window-using-mhe' (MH-E).")
754
755 (defvar gnus-mail-send-method send-mail-function
756   "*Function to mail a message which is also being posted as an article.
757 The message must have To or Cc header.  The default is copied from
758 the variable `send-mail-function'.")
759
760 (defvar gnus-subscribe-newsgroup-method 'gnus-subscribe-zombies
761   "*Function called with a group name when new group is detected.
762 A few pre-made functions are supplied: `gnus-subscribe-randomly'
763 inserts new groups at the beginning of the list of groups;
764 `gnus-subscribe-alphabetically' inserts new groups in strict
765 alphabetic order; `gnus-subscribe-hierarchically' inserts new groups
766 in hierarchical newsgroup order; `gnus-subscribe-interactively' asks
767 for your decision.")
768
769 ;; Suggested by a bug report by Hallvard B Furuseth.
770 ;; <h.b.furuseth@usit.uio.no>. 
771 (defvar gnus-subscribe-options-newsgroup-method
772   (function gnus-subscribe-alphabetically)
773   "*This function is called to subscribe newsgroups mentioned on \"options -n\" lines.
774 If, for instance, you want to subscribe to all newsgroups in the
775 \"no\" and \"alt\" hierarchies, you'd put the following in your
776 .newsrc file:
777
778 options -n no.all alt.all
779
780 Gnus will the subscribe all new newsgroups in these hierarchies with
781 the subscription method in this variable.")
782
783 (defvar gnus-subscribe-hierarchical-interactive nil
784   "*If non-nil, Gnus will offer to subscribe hierarchically.
785 When a new hierarchy appears, Gnus will ask the user:
786
787 'alt.binaries': Do you want to subscribe to this hierarchy? ([d]ys):
788
789 If the user pressed `d', Gnus will descend the hierarchy, `y' will
790 subscribe to all newsgroups in the hierarchy and `s' will skip this
791 hierarchy in its entirety.")
792
793 (defvar gnus-group-sort-function 'gnus-group-sort-by-alphabet
794   "*Function used for sorting the group buffer.
795 This function will be called with group info entries as the arguments
796 for the groups to be sorted.  Pre-made functions include
797 `gnus-sort-by-alphabet', `gnus-sort-by-unread' and
798 `gnus-sort-by-level'")
799
800 ;; Mark variables suggested by Thomas Michanek
801 ;; <Thomas.Michanek@telelogic.se>. 
802 (defvar gnus-unread-mark ? 
803   "*Mark used for unread articles.")
804 (defvar gnus-ticked-mark ?!
805   "*Mark used for ticked articles.")
806 (defvar gnus-dormant-mark ??
807   "*Mark used for dormant articles.")
808 (defvar gnus-del-mark ?D
809   "*Mark used for del'd articles.")
810 (defvar gnus-read-mark ?d
811   "*Mark used for read articles.")
812 (defvar gnus-expirable-mark ?E
813   "*Mark used for expirable articles.")
814 (defvar gnus-killed-mark ?K
815   "*Mark used for killed articles.")
816 (defvar gnus-kill-file-mark ?X
817   "*Mark used for articles killed by kill files.")
818 (defvar gnus-low-score-mark ?Y
819   "*Mark used for articles with a low score.")
820 (defvar gnus-catchup-mark ?C
821   "*Mark used for articles that are caught up.")
822 (defvar gnus-replied-mark ?R
823   "*Mark used for articles that have been replied to.")
824 (defvar gnus-process-mark ?# 
825   "*Process mark.")
826 (defvar gnus-ancient-mark ?A
827   "*Mark used for ancient articles.")
828 (defvar gnus-canceled-mark ?G
829   "*Mark used for cancelled articles.")
830 (defvar gnus-score-over-mark ?+
831   "*Score mark used for articles with high scores.")
832 (defvar gnus-score-below-mark ?-
833   "*Score mark used for articles with low scores.")
834 (defvar gnus-empty-thread-mark ? 
835   "*There is no thread under the article.")
836 (defvar gnus-not-empty-thread-mark ?=
837   "*There is a thread under the article.")
838
839 (defvar gnus-view-pseudo-asynchronously nil
840   "*If non-nil, Gnus will view pseudo-articles asynchronously.")
841
842 (defvar gnus-view-pseudos nil
843   "*If `automatic', pseudo-articles will be viewed automatically.
844 If `not-confirm', pseudos will be viewed automatically, and the user
845 will not be asked to confirm the command.")
846
847 (defvar gnus-view-pseudos-separately t
848   "*If non-nil, one pseudo-article will be created for each file to be viewed.
849 If nil, all files that use the same viewing command will be given as a
850 list of parameters to that command.")
851
852 (defvar gnus-group-line-format "%M%S%5y: %(%g%)\n"
853   "*Format of group lines.
854 It works along the same lines as a normal formatting string,
855 with some simple extensions.
856
857 %M    Only marked articles (character, \"*\" or \" \")
858 %S    Whether the group is subscribed (character, \"U\", \"K\", \"Z\" or \" \")
859 %L    Level of subscribedness (integer)
860 %N    Number of unread articles (integer)
861 %I    Number of dormant articles (integer)
862 %i    Number of ticked and dormant (integer)
863 %T    Number of ticked articles (integer)
864 %R    Number of read articles (integer)
865 %t    Total number of articles (integer)
866 %y    Number of unread, unticked articles (integer)
867 %G    Group name (string)
868 %g    Qualified group name (string)
869 %D    Group description (string)
870 %s    Select method (string)
871 %o    Moderated group (char, \"m\")
872 %O    Moderated group (string, \"(m)\" or \"\")
873 %n    Select from where (string)
874 %z    A string that look like `<%s:%n>' if a foreign select method is used
875 %u    User defined specifier. The next character in the format string should
876       be a letter.  Gnus will call the function gnus-user-format-function-X,
877       where X is the letter following %u. The function will be passed the
878       current header as argument. The function should return a string, which
879       will be inserted into the buffer just like information from any other
880       group specifier.
881
882 Text between %( and %) will be highlighted with `gnus-mouse-face' when
883 the mouse point move inside the area.  There can only be one such area.
884
885 Note that this format specification is not always respected. For
886 reasons of efficiency, when listing killed groups, this specification
887 is ignored altogether. If the spec is changed considerably, your
888 output may end up looking strange when listing both alive and killed
889 groups.
890
891 If you use %o or %O, reading the active file will be slower and quite
892 a bit of extra memory will be used. %D will also worsen performance.
893 Also note that if you change the format specification to include any
894 of these specs, you must probably re-start Gnus to see them go into
895 effect.") 
896
897 (defvar gnus-summary-line-format "%U%R%z%I%(%[%4L: %-20,20n%]%) %s\n"
898   "*The format specification of the lines in the summary buffer.
899
900 It works along the same lines as a normal formatting string,
901 with some simple extensions.
902
903 %N   Article number, left padded with spaces (string)
904 %S   Subject (string)
905 %s   Subject if it is at the root of a thread, and \"\" otherwise (string)
906 %n   Name of the poster (string)
907 %A   Address of the poster (string)
908 %F   Contents of the From: header (string)
909 %x   Contents of the Xref: header (string)
910 %D   Date of the article (string)
911 %d   Date of the article (string) in DD-MMM format
912 %M   Message-id of the article (string)
913 %r   References of the article (string)
914 %c   Number of characters in the article (integer)
915 %L   Number of lines in the article (integer)
916 %I   Indentation based on thread level (a string of spaces)
917 %T   A string with two possible values: 80 spaces if the article
918      is on thread level two or larger and 0 spaces on level one
919 %R   \"R\" if this article has been replied to, \" \" otherwise (character)
920 %U   Status of this article (character, \"D\", \"K\", \"-\" or \" \")
921 %[   Opening bracket (character, \"[\" or \"<\")
922 %]   Closing bracket (character, \"]\" or \">\")
923 %>   Spaces of length thread-level (string)
924 %<   Spaces of length (- 20 thread-level) (string)
925 %i   Article score (number)
926 %z   Article zcore (character)
927 %t   Number of articles under the current thread (number).
928 %e   Whether the thread is empty or not (character).
929 %u   User defined specifier. The next character in the format string should
930      be a letter.  Gnus will call the function gnus-user-format-function-X,
931      where X is the letter following %u. The function will be passed the
932      current header as argument. The function should return a string, which
933      will be inserted into the summary just like information from any other
934      summary specifier.
935
936 Text between %( and %) will be highlighted with `gnus-mouse-face'
937 when the mouse point is placed inside the area.  There can only be one
938 such area.
939
940 The %U (status), %R (replied) and %z (zcore) specs have to be handled
941 with care. For reasons of efficiency, Gnus will compute what column
942 these characters will end up in, and \"hard-code\" that. This means that
943 it is illegal to have these specs after a variable-length spec. Well,
944 you might not be arrested, but your summary buffer will look strange,
945 which is bad enough.
946
947 The smart choice is to have these specs as for to the left as
948 possible. 
949
950 This restriction may disappear in later versions of Gnus.")
951
952 (defvar gnus-summary-dummy-line-format "*   :                          : %S\n"
953   "*The format specification for the dummy roots in the summary buffer.
954 It works along the same lines as a normal formatting string,
955 with some simple extensions.
956
957 %S  The subject")
958
959 (defvar gnus-summary-mode-line-format "(ding) %G/%A %Z"
960   "*The format specification for the summary mode line.")
961
962 (defvar gnus-article-mode-line-format "(ding) %G/%A %S"
963   "*The format specification for the article mode line.")
964
965 (defvar gnus-group-mode-line-format "(ding) List of groups   {%M:%S}  "
966   "*The format specification for the group mode line.")
967
968 (defvar gnus-valid-select-methods
969   '(("nntp" post address prompt-address)
970     ("nnspool" post)
971     ("nnvirtual" none virtual prompt-address) 
972     ("nnmbox" mail respool) 
973     ("nnml" mail respool)
974     ("nnmh" mail respool) 
975     ("nndir" none prompt-address address)
976     ("nndigest" none) 
977     ("nndoc" none prompt-address) 
978     ("nnbabyl" mail respool) 
979     ("nnkiboze" none virtual) 
980     ("nnfolder" mail respool))
981   "An alist of valid select methods.
982 The first element of each list lists should be a string with the name
983 of the select method. The other elements may be be the category of
984 this method (ie. `post', `mail', `none' or whatever) or other
985 properties that this method has (like being respoolable).
986 If you implement a new select method, all you should have to change is
987 this variable. I think.")
988
989 (defvar gnus-updated-mode-lines '(group article summary)
990   "*List of buffers that should update their mode lines.
991 The list may contain the symbols `group', `article' and `summary'. If
992 the corresponding symbol is present, Gnus will keep that mode line
993 updated with information that may be pertinent. 
994 If this variable is nil, screen refresh may be quicker.")
995
996 ;; Added by Keinonen Kari <kk85613@cs.tut.fi>.
997 (defvar gnus-mode-non-string-length 21
998   "*Max length of mode-line non-string contents.
999 If this is nil, Gnus will take space as is needed, leaving the rest
1000 of the modeline intact.")
1001
1002 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
1003 (defvar gnus-mouse-face 'highlight
1004   "*Face used for mouse highlighting in Gnus.
1005 No mouse highlights will be done if `gnus-visual' is nil.")
1006
1007 (defvar gnus-summary-mark-below nil
1008   "*Mark all articles with a score below this variable as read.
1009 This variable is local to each summary buffer and usually set by the
1010 score file.")  
1011
1012 (defvar gnus-thread-sort-functions '(gnus-thread-sort-by-number)
1013   "*List of functions used for sorting threads in the summary buffer.
1014 By default, threads are sorted by article number.
1015
1016 Each function takes two threads and return non-nil if the first thread
1017 should be sorted before the other.  If you use more than one function,
1018 the primary sort function should be the last.
1019
1020 Ready-mady functions include `gnus-thread-sort-by-number',
1021 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
1022 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
1023 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').")
1024
1025 (defvar gnus-thread-score-function '+
1026   "*Function used for calculating the total score of a thread.
1027
1028 The function is called with the scores of the article and each
1029 subthread and should then return the score of the thread.
1030
1031 Some functions you can use are `+', `max', or `min'.")
1032
1033 (defvar gnus-score-find-score-files-function 'gnus-score-find-bnews
1034   "*Function used to find SCORE files.
1035 The function will be called with the group name as the argument, and
1036 should return a list of score files to apply to that group.  The score
1037 files do not actually have to exist.
1038
1039 Predefined values are:
1040
1041 gnus-score-find-single: Only apply the group's own SCORE file.
1042 gnus-score-find-hierarchical: Also apply SCORE files from parent groups.
1043 gnus-score-find-bnews: Apply SCORE files whose names matches.
1044
1045 See the documentation to these functions for more information.
1046
1047 This variable can also be a list of functions to be called.  Each
1048 function should either return a list of score files, or a list of
1049 score alists.")
1050
1051 (defvar gnus-options-subscribe nil
1052   "*All new groups matching this regexp will be subscribed unconditionally.
1053 Note that this variable deals only with new newsgroups.  This variable
1054 does not affect old newsgroups.")
1055
1056 (defvar gnus-options-not-subscribe nil
1057   "*All new groups matching this regexp will be ignored.
1058 Note that this variable deals only with new newsgroups.  This variable
1059 does not affect old (already subscribed) newsgroups.")
1060
1061 (defvar gnus-auto-expirable-newsgroups nil
1062   "*Groups in which to automatically mark read articles as expirable.
1063 If non-nil, this should be a regexp that should match all groups in
1064 which to perform auto-expiry.  This only makes sense for mail groups.")
1065
1066
1067 ;; Hooks.
1068
1069 (defvar gnus-group-mode-hook nil
1070   "*A hook for Gnus group mode.")
1071
1072 (defvar gnus-summary-mode-hook nil
1073   "*A hook for Gnus summary mode.")
1074
1075 (defvar gnus-article-mode-hook nil
1076   "*A hook for Gnus article mode.")
1077
1078 (defvar gnus-open-server-hook nil
1079   "*A hook called just before opening connection to the news server.")
1080
1081 (defvar gnus-startup-hook nil
1082   "*A hook called at startup.
1083 This hook is called after Gnus is connected to the NNTP server.")
1084
1085 (defvar gnus-get-new-news-hook nil
1086   "*A hook run just before Gnus checks for new news.")
1087
1088 (defvar gnus-group-prepare-function 'gnus-group-prepare-flat
1089   "*A function that is called to generate the group buffer.
1090 The function is called with three arguments: The first is a number;
1091 all group with a level less or equal to that number should be listed,
1092 if the second is non-nil, empty groups should also be displayed. If
1093 the third is non-nil, it is a number. No groups with a level lower
1094 than this number should be displayed.
1095
1096 The only current function implemented is `gnus-group-prepare-flat'.")
1097
1098 (defvar gnus-group-prepare-hook nil
1099   "*A hook called after the group buffer has been generated.
1100 If you want to modify the group buffer, you can use this hook.")
1101
1102 (defvar gnus-summary-prepare-hook nil
1103   "*A hook called after the summary buffer has been generated.
1104 If you want to modify the summary buffer, you can use this hook.")
1105
1106 (defvar gnus-article-prepare-hook nil
1107   "*A hook called after an article has been prepared in the article buffer.
1108 If you want to run a special decoding program like nkf, use this hook.")
1109
1110 (defvar gnus-article-display-hook nil
1111   "*A hook called after the article is displayed in the article buffer.
1112 The hook is designed to change the contents of the article
1113 buffer. Typical functions that this hook may contain are
1114 `gnus-article-hide-headers' (hide selected headers),
1115 `gnus-article-hide-signature' (hide signature) and
1116 `gnus-article-treat-overstrike' (turn \"^H_\" into bold characters).")
1117 (add-hook 'gnus-article-display-hook 'gnus-article-hide-headers-if-wanted)
1118 (add-hook 'gnus-article-display-hook 'gnus-article-treat-overstrike)
1119
1120 (defvar gnus-article-x-face-command
1121   "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | xv -"
1122   "String or function to be executed to display an X-Face header.
1123 If it is a string, the command will be executed in a sub-shell
1124 asynchronously. The compressed face will be piped to this command.") 
1125
1126 (defvar gnus-select-group-hook nil
1127   "*A hook called when a newsgroup is selected.
1128
1129 If you'd like to simplify subjects like the
1130 `gnus-summary-next-same-subject' command does, you can use the
1131 following hook:
1132
1133  (setq gnus-select-group-hook
1134       (list
1135         (lambda ()
1136           (mapcar (lambda (header)
1137                      (header-set-subject
1138                       header
1139                       (gnus-simplify-subject
1140                        (header-subject header) 're-only)))
1141                   gnus-newsgroup-headers))))")
1142
1143 (defvar gnus-select-article-hook
1144   '(gnus-summary-show-thread)
1145   "*A hook called when an article is selected.
1146 The default hook shows conversation thread subtrees of the selected
1147 article automatically using `gnus-summary-show-thread'.")
1148
1149 (defvar gnus-apply-kill-hook '(gnus-apply-kill-file)
1150   "*A hook called to apply kill files to a group.
1151 This hook is intended to apply a kill file to the selected newsgroup.
1152 The function `gnus-apply-kill-file' is called by default.
1153
1154 Since a general kill file is too heavy to use only for a few
1155 newsgroups, I recommend you to use a lighter hook function. For
1156 example, if you'd like to apply a kill file to articles which contains
1157 a string `rmgroup' in subject in newsgroup `control', you can use the
1158 following hook:
1159
1160 \(setq gnus-apply-kill-hook
1161       (list
1162         (lambda ()
1163           (cond ((string-match \"control\" gnus-newsgroup-name)
1164                  (gnus-kill \"Subject\" \"rmgroup\")
1165                  (gnus-expunge \"X\"))))))")
1166
1167 (defvar gnus-visual-mark-article-hook 
1168   (list 'gnus-visual-highlight-selected-summary)
1169   "*Hook run after selecting an article in the summary buffer.
1170 It is meant to be used for highlighting the article in some way.  It
1171 is not run if `gnus-visual' is nil.")
1172
1173 (defvar gnus-prepare-article-hook (list 'gnus-inews-insert-signature)
1174   "*A hook called after preparing body, but before preparing header headers.
1175 The default hook (`gnus-inews-insert-signature') inserts a signature
1176 file specified by the variable `gnus-signature-file'.")
1177
1178 (defvar gnus-inews-article-hook (list 'gnus-inews-do-fcc)
1179   "*A hook called before finally posting an article.
1180 The default hook (`gnus-inews-do-fcc') does FCC processing (ie. saves
1181 the article to a file).")
1182
1183 (defvar gnus-inews-article-header-hook nil
1184   "*A hook called after inserting the headers in an article to be posted.
1185 The hook is called from the *post-news* buffer, narrowed to the
1186 headers.")
1187
1188 (defvar gnus-exit-group-hook nil
1189   "*A hook called when exiting (not quitting) summary mode.")
1190
1191 (defvar gnus-suspend-gnus-hook nil
1192   "*A hook called when suspending (not exiting) Gnus.")
1193
1194 (defvar gnus-exit-gnus-hook nil
1195   "*A hook called when exiting Gnus.")
1196
1197 (defvar gnus-save-newsrc-hook nil
1198   "*A hook called when saving the newsrc file.")
1199
1200 (defvar gnus-visual-summary-update-hook 
1201   (list 'gnus-visual-summary-highlight-line)
1202   "*A hook called when a summary line is changed.
1203 The hook will not be called if `gnus-visual' is nil.
1204
1205 The default function `gnus-visual-summary-highlight-line' will
1206 highlight the line according to the `gnus-visual-summary-highlight'
1207 variable.")
1208
1209 (defvar gnus-mark-article-hook (list 'gnus-summary-mark-unread-as-read)
1210   "*A hook called when an article is selected for the first time.
1211 The hook is intended to mark an article as read (or unread)
1212 automatically when it is selected.")
1213
1214 \f
1215 ;; Internal variables
1216
1217 (defvar gnus-internal-global-score-files nil)
1218 (defvar gnus-score-file-list nil)
1219
1220 ;; Avoid highlighting in kill files.
1221 (defvar gnus-summary-inhibit-highlight nil)
1222 (defvar gnus-newsgroup-selected-overlay nil)
1223
1224 (defvar gnus-article-mode-map nil)
1225 (defvar caesar-translate-table nil)
1226 (defvar gnus-dribble-buffer nil)
1227 (defvar gnus-headers-retrieved-by nil)
1228 (defvar gnus-article-reply nil)
1229 (defvar gnus-override-method nil)
1230 (defvar gnus-article-check-size nil)
1231
1232 (defvar gnus-current-move-group nil)
1233
1234 (defvar gnus-newsgroup-dependencies nil)
1235 (defvar gnus-newsgroup-threads nil)
1236 (defvar gnus-newsgroup-async nil)
1237 (defconst gnus-group-edit-buffer "*Gnus edit newsgroup*")
1238
1239 (defconst gnus-group-line-format-alist
1240   (list (list ?M 'marked ?c)
1241         (list ?S 'subscribed ?c)
1242         (list ?L 'level ?d)
1243         (list ?N 'number ?s)
1244         (list ?I 'number-of-dormant ?d)
1245         (list ?T 'number-of-ticked ?d)
1246         (list ?R 'number-of-read ?s)
1247         (list ?t 'number-total ?d)
1248         (list ?y 'number-of-unread-unticked ?s)
1249         (list ?i 'number-of-ticked-and-dormant ?d)
1250         (list ?g 'group ?s)
1251         (list ?G 'qualified-group ?s)
1252         (list ?D 'newsgroup-description ?s)
1253         (list ?o 'moderated ?c)
1254         (list ?O 'moderated-string ?s)
1255         (list ?s 'news-server ?s)
1256         (list ?n 'news-method ?s)
1257         (list ?z 'news-method-string ?s)
1258         (list ?u 'user-defined ?s)))
1259
1260 (defconst gnus-summary-line-format-alist 
1261   (list (list ?N 'number ?d)
1262         (list ?S 'subject ?s)
1263         (list ?s 'subject-or-nil ?s)
1264         (list ?n 'name ?s)
1265         (list ?A 'address ?s)
1266         (list ?F 'from ?s)
1267         (list ?x (macroexpand '(header-xref header)) ?s)
1268         (list ?D (macroexpand '(header-date header)) ?s)
1269         (list ?d '(gnus-dd-mmm (header-date header)) ?s)
1270         (list ?M (macroexpand '(header-id header)) ?s)
1271         (list ?r (macroexpand '(header-references header)) ?s)
1272         (list ?c '(or (header-chars header) 0) ?d)
1273         (list ?L 'lines ?d)
1274         (list ?I 'indentation ?s)
1275         (list ?T '(if (= level 0) "" (make-string (frame-width) ? )) ?s)
1276         (list ?R 'replied ?c)
1277         (list ?\[ 'opening-bracket ?c)
1278         (list ?\] 'closing-bracket ?c)
1279         (list ?\> '(make-string level ? ) ?s)
1280         (list ?\< '(make-string (max 0 (- 20 level)) ? ) ?s)
1281         (list ?i 'score ?s)
1282         (list ?z 'score-char ?c)
1283         (list ?U 'unread ?c)
1284         (list ?t '(gnus-summary-number-of-articles-in-thread 
1285                    (if (boundp 'thread) (symbol-value 'thread)) nil) ?d)
1286         (list ?e '(gnus-summary-number-of-articles-in-thread 
1287                    (if (boundp 'thread) (symbol-value 'thread)) t) ?c)
1288         (list ?u 'user-defined ?s))
1289   "An alist of format specifications that can appear in summary lines,
1290 and what variables they correspond with, along with the type of the
1291 variable (string, integer, character, etc).")
1292
1293 (defconst gnus-summary-dummy-line-format-alist
1294   (list (list ?S 'subject ?s)
1295         (list ?N 'number ?d)
1296         (list ?u 'user-defined ?s)))
1297
1298 (defconst gnus-summary-mode-line-format-alist 
1299   (list (list ?G 'group-name ?s)
1300         (list ?g '(gnus-short-group-name group-name) ?s)
1301         (list ?A 'article-number ?d)
1302         (list ?Z 'unread-and-unselected ?s)
1303         (list ?V 'gnus-version ?s)
1304         (list ?U 'unread ?d)
1305         (list ?S 'subject ?s)
1306         (list ?e 'unselected ?d)
1307         (list ?u 'user-defined ?s)
1308         (list ?s '(gnus-current-score-file-nondirectory) ?s)))
1309
1310 (defconst gnus-group-mode-line-format-alist 
1311   (list (list ?S 'news-server ?s)
1312         (list ?M 'news-method ?s)
1313         (list ?u 'user-defined ?s)))
1314
1315 (defvar gnus-have-read-active-file nil)
1316
1317 (defconst gnus-maintainer "Lars Magne Ingebrigtsen <larsi@ifi.uio.no>"
1318   "The mail address of the Gnus maintainer.")
1319
1320 (defconst gnus-version "(ding) Gnus v0.63"
1321   "Version number for this version of Gnus.")
1322
1323 (defvar gnus-info-nodes
1324   '((gnus-group-mode            "(gnus)The Group Buffer")
1325     (gnus-summary-mode          "(gnus)The Summary Buffer")
1326     (gnus-article-mode          "(gnus)The Article Buffer"))
1327   "Assoc list of major modes and related Info nodes.")
1328
1329 (defvar gnus-documentation-group-file "~/dgnus/lisp/doc.txt"
1330   "The location of the (ding) Gnus documentation group.")
1331
1332 (defvar gnus-group-buffer "*Group*")
1333 (defvar gnus-summary-buffer "*Summary*")
1334 (defvar gnus-article-buffer "*Article*")
1335 (defvar gnus-server-buffer "*Server*")
1336
1337 (defvar gnus-work-buffer " *gnus work*")
1338
1339 (defvar gnus-buffer-list nil
1340   "Gnus buffers that should be killed on exit.")
1341
1342 (defvar gnus-server-alist nil
1343   "List of available servers.")
1344
1345 (defvar gnus-variable-list
1346   '(gnus-newsrc-options gnus-newsrc-options-n
1347     gnus-newsrc-last-checked-date 
1348     gnus-newsrc-alist gnus-server-alist
1349     gnus-killed-list gnus-zombie-list)
1350   "Gnus variables saved in the quick startup file.")
1351
1352 (defvar gnus-overload-functions
1353   '((news-inews gnus-inews-news "rnewspost")
1354     (caesar-region gnus-caesar-region "rnews"))
1355   "Functions overloaded by gnus.
1356 It is a list of `(original overload &optional file)'.")
1357
1358 (defvar gnus-newsrc-options nil
1359   "Options line in the .newsrc file.")
1360
1361 (defvar gnus-newsrc-options-n nil
1362   "List of regexps representing groups to be subscribed/ignored unconditionally.") 
1363
1364 (defvar gnus-newsrc-last-checked-date nil
1365   "Date Gnus last asked server for new newsgroups.")
1366
1367 (defvar gnus-newsrc-alist nil
1368   "Assoc list of read articles.
1369 gnus-newsrc-hashtb should be kept so that both hold the same information.")
1370
1371 (defvar gnus-newsrc-hashtb nil
1372   "Hashtable of gnus-newsrc-alist.")
1373
1374 (defvar gnus-killed-list nil
1375   "List of killed newsgroups.")
1376
1377 (defvar gnus-killed-hashtb nil
1378   "Hash table equivalent of gnus-killed-list.")
1379
1380 (defvar gnus-zombie-list nil
1381   "List of almost dead newsgroups.")
1382
1383 (defvar gnus-description-hashtb nil
1384   "Descriptions of newsgroups.")
1385
1386 (defvar gnus-list-of-killed-groups nil
1387   "List of newsgroups that have recently been killed by the user.")
1388
1389 (defvar gnus-active-hashtb nil
1390   "Hashtable of active articles.")
1391
1392 (defvar gnus-moderated-list nil
1393   "List of moderated newsgroups.")
1394
1395 (defvar gnus-current-startup-file nil
1396   "Startup file for the current host.")
1397
1398 (defvar gnus-last-search-regexp nil
1399   "Default regexp for article search command.")
1400
1401 (defvar gnus-last-shell-command nil
1402   "Default shell command on article.")
1403
1404 (defvar gnus-current-select-method nil
1405   "The current method for selecting a newsgroup.")
1406
1407 (defvar gnus-have-all-newsgroups nil)
1408
1409 (defvar gnus-article-internal-prepare-hook nil)
1410
1411 (defvar gnus-newsgroup-name nil)
1412 (defvar gnus-newsgroup-begin nil)
1413 (defvar gnus-newsgroup-end nil)
1414 (defvar gnus-newsgroup-last-rmail nil)
1415 (defvar gnus-newsgroup-last-mail nil)
1416 (defvar gnus-newsgroup-last-folder nil)
1417 (defvar gnus-newsgroup-last-file nil)
1418 (defvar gnus-newsgroup-auto-expire nil)
1419 (defvar gnus-newsgroup-active nil)
1420
1421 (defvar gnus-newsgroup-unreads nil
1422   "List of unread articles in the current newsgroup.")
1423
1424 (defvar gnus-newsgroup-unselected nil
1425   "List of unselected unread articles in the current newsgroup.")
1426
1427 (defvar gnus-newsgroup-marked nil
1428   "List of ticked articles in the current newsgroup (a subset of unread art).")
1429
1430 (defvar gnus-newsgroup-killed nil
1431   "List of ranges of articles that have been through the scoring process.")
1432
1433 (defvar gnus-newsgroup-kill-headers nil)
1434
1435 (defvar gnus-newsgroup-replied nil
1436   "List of articles that have been replied to in the current newsgroup.")
1437
1438 (defvar gnus-newsgroup-expirable nil
1439   "List of articles in the current newsgroup that can be expired.")
1440
1441 (defvar gnus-newsgroup-processable nil
1442   "List of articles in the current newsgroup that can be processed.")
1443
1444 (defvar gnus-newsgroup-bookmarks nil
1445   "List of articles in the current newsgroup that have bookmarks.")
1446
1447 (defvar gnus-newsgroup-dormant nil
1448   "List of dormant articles in the current newsgroup.")
1449
1450 (defvar gnus-newsgroup-scored nil
1451   "List of scored articles in the current newsgroup.")
1452
1453 (defvar gnus-newsgroup-headers nil
1454   "List of article headers in the current newsgroup.")
1455 (defvar gnus-newsgroup-headers-hashtb-by-number nil)
1456
1457 (defvar gnus-newsgroup-ancient nil
1458   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1459
1460 (defvar gnus-current-article nil)
1461 (defvar gnus-article-current nil)
1462 (defvar gnus-current-headers nil)
1463 (defvar gnus-have-all-headers nil)
1464 (defvar gnus-last-article nil)
1465 (defvar gnus-newsgroup-history nil)
1466 (defvar gnus-current-kill-article nil)
1467
1468 ;; Save window configuration.
1469 (defvar gnus-winconf-edit-group nil)
1470
1471 ;; Format specs
1472 (defvar gnus-summary-line-format-spec nil)
1473 (defvar gnus-summary-dummy-line-format-spec nil)
1474 (defvar gnus-group-line-format-spec nil)
1475 (defvar gnus-summary-mode-line-format-spec nil)
1476 (defvar gnus-article-mode-line-format-spec nil)
1477 (defvar gnus-group-mode-line-format-spec nil)
1478 (defvar gnus-summary-mark-positions nil)
1479
1480 (defvar gnus-summary-expunge-below nil)
1481 (defvar gnus-reffed-article-number nil)
1482
1483 (defvar rmail-default-file (expand-file-name "~/XMBOX"))
1484 (defvar rmail-default-rmail-file (expand-file-name "~/XNEWS"))
1485
1486 (defconst gnus-summary-local-variables 
1487   '(gnus-newsgroup-name 
1488     gnus-newsgroup-begin gnus-newsgroup-end 
1489     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail 
1490     gnus-newsgroup-last-folder gnus-newsgroup-last-file 
1491     gnus-newsgroup-auto-expire gnus-newsgroup-unreads 
1492     gnus-newsgroup-unselected gnus-newsgroup-marked
1493     gnus-newsgroup-replied gnus-newsgroup-expirable
1494     gnus-newsgroup-processable gnus-newsgroup-killed
1495     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1496     gnus-newsgroup-headers gnus-newsgroup-headers-hashtb-by-number
1497     gnus-current-article gnus-current-headers gnus-have-all-headers
1498     gnus-last-article gnus-article-internal-prepare-hook
1499     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1500     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1501     gnus-newsgroup-threads gnus-newsgroup-async
1502     gnus-score-alist gnus-current-score-file gnus-summary-expunge-below 
1503     gnus-summary-mark-below gnus-newsgroup-active gnus-scores-exclude-files
1504     gnus-newsgroup-history gnus-newsgroup-ancient)
1505   "Variables that are buffer-local to the summary buffers.")
1506
1507 ;;; End of variables.
1508
1509 ;; Define some autoload functions Gnus might use.
1510 (eval-and-compile
1511   (autoload 'metamail-buffer "metamail")
1512   (autoload 'Info-goto-node "info")
1513   (autoload 'hexl-hex-string-to-integer "hexl")
1514   
1515   (autoload 'timezone-make-date-arpa-standard "timezone")
1516   (autoload 'timezone-fix-time "timezone")
1517   (autoload 'timezone-make-sortable-date "timezone")
1518   (autoload 'timezone-make-time-string "timezone")
1519   
1520   (autoload 'rmail-output "rmailout")
1521   (autoload 'mail-position-on-field "sendmail")
1522   (autoload 'mail-setup "sendmail")
1523   (autoload 'news-mail-other-window "rnewspost")
1524   (autoload 'news-reply-yank-original "rnewspost")
1525   (autoload 'news-caesar-buffer-body "rnewspost")
1526
1527   (autoload 'rmail-insert-rmail-file-header "rmail")
1528   (autoload 'rmail-count-new-messages "rmail")
1529   (autoload 'rmail-show-message "rmail")
1530
1531   (autoload 'gnus-mail-reply-using-mhe "gnus-mh")
1532   (autoload 'gnus-mail-forward-using-mhe "gnus-mh")
1533   (autoload 'gnus-mail-other-window-using-mhe "gnus-mh")
1534   (autoload 'gnus-summary-save-in-folder "gnus-mh")
1535   (autoload 'gnus-Folder-save-name "gnus-mh")
1536   (autoload 'gnus-folder-save-name "gnus-mh")
1537   
1538   (autoload 'gnus-group-make-menu-bar "gnus-vis")
1539   (autoload 'gnus-summary-make-menu-bar "gnus-vis")
1540   (autoload 'gnus-article-make-menu-bar "gnus-vis")
1541   (autoload 'gnus-visual-highlight-selected-summary "gnus-vis")
1542   (autoload 'gnus-visual-summary-highlight-line "gnus-vis")
1543
1544   (autoload 'gnus-uu-mark-by-regexp "gnus-uu" nil t)
1545   (autoload 'gnus-uu-mark-region "gnus-uu" nil t)
1546   (autoload 'gnus-uu-mark-thread "gnus-uu" nil t)
1547   (autoload 'gnus-uu-mark-sparse "gnus-uu" nil t)
1548   (autoload 'gnus-uu-mark-series "gnus-uu" nil t)
1549   (autoload 'gnus-uu-mark-all "gnus-uu" nil t)
1550   (autoload 'gnus-uu-post-news "gnus-uu" nil t)
1551   (autoload 'gnus-uu-digest-mail-forward "gnus-uu" nil t)
1552   (autoload 'gnus-uu-digest-post-forward "gnus-uu" nil t)
1553
1554   (autoload 'gnus-uu-decode-uu "gnus-uu" nil t)
1555   (autoload 'gnus-uu-decode-uu-and-save "gnus-uu" nil t)
1556   (autoload 'gnus-uu-decode-unshar "gnus-uu" nil t)
1557   (autoload 'gnus-uu-decode-unshar-and-save "gnus-uu" nil t)
1558   (autoload 'gnus-uu-decode-save "gnus-uu" nil t)
1559   (autoload 'gnus-uu-decode-binhex "gnus-uu" nil t)
1560   (autoload 'gnus-uu-decode-uu-view "gnus-uu" nil t)
1561   (autoload 'gnus-uu-decode-uu-and-save-view "gnus-uu" nil t)
1562   (autoload 'gnus-uu-decode-unshar-view "gnus-uu" nil t)
1563   (autoload 'gnus-uu-decode-unshar-and-save-view "gnus-uu" nil t)
1564   (autoload 'gnus-uu-decode-save-view "gnus-uu" nil t)
1565   (autoload 'gnus-uu-decode-binhex-view "gnus-uu" nil t)
1566   (autoload 'gnus-uu-decode-postscript "gnus-uu" nil t)
1567   (autoload 'gnus-uu-decode-postscript-and-save "gnus-uu" nil t)
1568   (autoload 'gnus-uu-decode-postscript-view "gnus-uu" nil t)
1569   (autoload 'gnus-uu-decode-postscript-and-save-view "gnus-uu" nil t)
1570
1571   (autoload 'gnus-kill "gnus-kill")
1572   (autoload 'gnus-apply-kill-file-internal "gnus-kill")
1573   (autoload 'gnus-kill-file-edit-file "gnus-kill")
1574   (autoload 'gnus-kill-file-raise-followups-to-author "gnus-kill")
1575   (autoload 'gnus-execute "gnus-kill")
1576   (autoload 'gnus-expunge "gnus-kill")
1577
1578   (autoload 'gnus-cache-possibly-enter-article "gnus-cache")
1579   (autoload 'gnus-cache-save-buffers "gnus-cache")
1580   (autoload 'gnus-cache-possibly-remove-article "gnus-cache")
1581   (autoload 'gnus-cache-request-article "gnus-cache")
1582   (autoload 'gnus-cache-retrieve-headers "gnus-cache")
1583   (autoload 'gnus-cache-possibly-alter-active "gnus-cache")
1584   (autoload 'gnus-jog-cache "gnus-cache" nil t)
1585
1586   (autoload 'pp "pp")
1587   (autoload 'pp-to-string "pp")
1588   (autoload 'mail-extract-address-components "mail-extr")
1589
1590   (autoload 'gnus-summary-increase-map "gnus-score" nil nil 'keymap)
1591   (autoload 'gnus-summary-lower-map "gnus-score" nil nil 'keymap)
1592   (autoload 'gnus-summary-score-map "gnus-score" nil nil 'keymap)
1593   (autoload 'gnus-score-save "gnus-score")
1594   (autoload 'gnus-score-headers "gnus-score")
1595   (autoload 'gnus-current-score-file-nondirectory "gnus-score")
1596   (autoload 'gnus-score-adaptive "gnus-score")
1597   (autoload 'gnus-score-remove-lines-adaptive "gnus-score")
1598
1599   (autoload 'gnus-summary-send-map "gnus-msg" nil nil 'keymap)
1600   (autoload 'gnus-group-post-news "gnus-msg" nil t)
1601   (autoload 'gnus-summary-post-news "gnus-msg" nil t)
1602   (autoload 'gnus-summary-followup "gnus-msg" nil t)
1603   (autoload 'gnus-summary-followup-with-original "gnus-msg" nil t)
1604   (autoload 'gnus-summary-followup-and-reply "gnus-msg" nil t)
1605   (autoload 'gnus-summary-followup-and-reply-with-original "gnus-msg" nil t)
1606   (autoload 'gnus-summary-cancel-article "gnus-msg" nil t)
1607   (autoload 'gnus-summary-supersede-article "gnus-msg" nil t)
1608   (autoload 'gnus-post-news "gnus-msg" nil t)
1609   (autoload 'gnus-inews-news "gnus-msg" nil t)
1610   (autoload 'gnus-cancel-news "gnus-msg" nil t)
1611   (autoload 'gnus-summary-reply "gnus-msg" nil t)
1612   (autoload 'gnus-summary-reply-with-original "gnus-msg" nil t)
1613   (autoload 'gnus-summary-mail-forward "gnus-msg" nil t)
1614   (autoload 'gnus-summary-mail-other-window "gnus-msg" nil t)
1615   (autoload 'gnus-mail-reply-using-mail "gnus-msg")
1616   (autoload 'gnus-mail-yank-original "gnus-msg")
1617   (autoload 'gnus-mail-send-and-exit "gnus-msg")
1618   (autoload 'gnus-mail-forward-using-mail "gnus-msg")
1619   (autoload 'gnus-mail-other-window-using-mail "gnus-msg")
1620
1621   )
1622
1623 \f
1624
1625 ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
1626 ;; If you want the cursor to go somewhere else, set these two
1627 ;; functions in some startup hook to whatever you want.
1628 (defalias 'gnus-summary-position-cursor 'gnus-goto-colon)
1629 (defalias 'gnus-group-position-cursor 'gnus-goto-colon)
1630
1631 ;; Cruft to make Gnus work under GNU XEmacs.
1632 (defconst gnus-xemacs (not (not (string-match "XEmacs\\|Lucid" emacs-version)))
1633   "Non-nil if Gnus is running under GNU XEmacs.")
1634
1635 (if gnus-xemacs
1636     (progn
1637       (or (memq 'underline (funcall 'list-faces))
1638           (make-face 'underline))
1639       (or (face-differs-from-default-p 'underline)
1640           (set-face-underline-p 'underline t))
1641       (or (fboundp 'set-text-properties)
1642           (defun set-text-properties (start end props &optional buffer)
1643             (if props
1644                 (put-text-property start end (car props) 
1645                                    (funcall (intern "cadr") props) buffer)
1646               (remove-text-properties start end ()))))))
1647
1648 ;;; Various macros and substs.
1649
1650 (defmacro gnus-eval-in-buffer-window (buffer &rest forms)
1651   "Pop to BUFFER, evaluate FORMS, and then returns to original window."
1652   (` (let ((GnusStartBufferWindow (selected-window)))
1653        (unwind-protect
1654            (progn
1655              (pop-to-buffer (, buffer))
1656              (,@ forms))
1657          (select-window GnusStartBufferWindow)))))
1658
1659 (defmacro gnus-gethash (string hashtable)
1660   "Get hash value of STRING in HASHTABLE."
1661   ;;(` (symbol-value (abbrev-symbol (, string) (, hashtable))))
1662   ;;(` (abbrev-expansion (, string) (, hashtable)))
1663   (` (symbol-value (intern-soft (, string) (, hashtable)))))
1664
1665 (defmacro gnus-sethash (string value hashtable)
1666   "Set hash value. Arguments are STRING, VALUE, and HASHTABLE."
1667   ;; We cannot use define-abbrev since it only accepts string as value.
1668   ;; (set (intern string hashtable) value))
1669   (` (set (intern (, string) (, hashtable)) (, value))))
1670
1671 (defsubst gnus-buffer-substring (beg end)
1672   (buffer-substring (match-beginning beg) (match-end end)))
1673
1674 (defsubst gnus-simplify-subject-re (subject)
1675   "Remove \"Re:\" from subject lines."
1676   (let ((case-fold-search t))
1677     (if (string-match "^re: *" subject)
1678         (substring subject (match-end 0))
1679       subject)))
1680
1681 (defsubst gnus-goto-char (point)
1682   (and point (goto-char point)))
1683
1684 (defmacro gnus-buffer-exists-p (buffer)
1685   (` (and (, buffer)
1686           (funcall (if (stringp (, buffer)) 'get-buffer 'buffer-name)
1687                    (, buffer)))))
1688
1689 (defmacro gnus-kill-buffer (buffer)
1690   (` (if (gnus-buffer-exists-p (, buffer))
1691          (kill-buffer (, buffer)))))
1692
1693 (defsubst gnus-point-at-bol ()
1694   "Return point at the beginning of line."
1695   (let ((p (point)))
1696     (beginning-of-line)
1697     (prog1
1698         (point)
1699       (goto-char p))))
1700
1701 (defsubst gnus-point-at-eol ()
1702   "Return point at the beginning of line."
1703   (let ((p (point)))
1704     (end-of-line)
1705     (prog1
1706         (point)
1707       (goto-char p))))
1708
1709 \f
1710 ;;;
1711 ;;; Gnus Utility Functions
1712 ;;;
1713
1714 (defun gnus-extract-address-components (from)
1715   (let (name address)
1716     (if (string-match "\\b[^@ \t<>]+[!@][^@ \t<>]+\\b" from)
1717         (setq address (substring from (match-beginning 0) (match-end 0))))
1718     (and address
1719          (string-match (concat "<" (regexp-quote address) ">") from)
1720          (setq name (substring from 0 (1- (match-beginning 0)))))
1721     (or name
1722         (and (string-match "(.+)" from)
1723              (setq name (substring from (1+ (match-beginning 0)) 
1724                                    (1- (match-end 0))))))
1725     ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
1726     (list (or name from) (or address from))))
1727
1728 (defun gnus-fetch-field (field)
1729   "Return the value of the header FIELD of current article."
1730   (save-excursion
1731     (save-restriction
1732       (let ((case-fold-search t))
1733         (gnus-narrow-to-headers)
1734         (mail-fetch-field field)))))
1735
1736 (defun gnus-goto-colon ()
1737   (beginning-of-line)
1738   (search-forward ":" (gnus-point-at-eol) t))
1739
1740 (defun gnus-narrow-to-headers ()
1741   (widen)
1742   (save-excursion
1743     (goto-char 1)
1744     (if (search-forward "\n\n")
1745         (narrow-to-region 1 (1- (point))))))
1746
1747 (defun gnus-update-format-specifications ()
1748   (gnus-make-thread-indent-array)
1749   (setq gnus-summary-line-format-spec 
1750         (gnus-parse-format
1751          gnus-summary-line-format gnus-summary-line-format-alist))
1752   (gnus-update-summary-mark-positions)
1753   (setq gnus-summary-dummy-line-format-spec 
1754         (gnus-parse-format gnus-summary-dummy-line-format 
1755                            gnus-summary-dummy-line-format-alist))
1756   (setq gnus-group-line-format-spec
1757         (gnus-parse-format 
1758          gnus-group-line-format 
1759          gnus-group-line-format-alist))
1760   (if (and (string-match "%D" gnus-group-line-format)
1761            (not gnus-description-hashtb)
1762            gnus-read-active-file)
1763       (gnus-read-descriptions-file))
1764   (setq gnus-summary-mode-line-format-spec 
1765         (gnus-parse-format gnus-summary-mode-line-format 
1766                            gnus-summary-mode-line-format-alist))
1767   (setq gnus-article-mode-line-format-spec 
1768         (gnus-parse-format gnus-article-mode-line-format 
1769                            gnus-summary-mode-line-format-alist))
1770   (setq gnus-group-mode-line-format-spec 
1771         (gnus-parse-format gnus-group-mode-line-format 
1772                            gnus-group-mode-line-format-alist)))
1773
1774 (defun gnus-update-summary-mark-positions ()
1775   (save-excursion
1776     (let ((gnus-replied-mark 129)
1777           (gnus-score-below-mark 130)
1778           (gnus-score-over-mark 130)
1779           (thread nil)
1780           pos)
1781       (gnus-set-work-buffer)
1782       (gnus-summary-insert-line 
1783        nil [0 "" "" "" "" "" 0 0 ""]  0 nil 128 t nil "" nil 1)
1784       (goto-char (point-min))
1785       (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
1786                                          (- (point) 2)))))
1787       (goto-char (point-min))
1788       (setq pos (cons (cons 'replied (and (search-forward "\201" nil t)
1789                                           (- (point) 2))) pos))
1790       (goto-char (point-min))
1791       (setq pos (cons (cons 'score (and (search-forward "\202" nil t)
1792                                         (- (point) 2))) pos))
1793       (setq gnus-summary-mark-positions pos))))
1794
1795 (defun gnus-format-max-width (form length)
1796   (let ((val (eval form)))
1797     (if (> (length val) length)
1798         (substring val 0 length)
1799       val)))
1800
1801 (defun gnus-set-mouse-face (string)
1802   ;; Set mouse face property on STRING.
1803   (or gnus-xemacs
1804       (put-text-property 0 (length string) 'mouse-face gnus-mouse-face string))
1805   string)
1806
1807 (defun gnus-parse-format (format spec-alist)
1808   ;; This function parses the FORMAT string with the help of the
1809   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
1810   ;; string.  If the FORMAT string contains the specifiers %( and %)
1811   ;; the text between them will have the mouse-face text property.
1812   (if (string-match "\\`\\(.*\\)%(\\(.*\\)%)\\(.*\n?\\)\\'" format)
1813       (if (and gnus-visual gnus-mouse-face)
1814           (let ((pre (substring format (match-beginning 1) (match-end 1)))
1815                 (button (substring format (match-beginning 2) (match-end 2)))
1816                 (post (substring format (match-beginning 3) (match-end 3))))
1817             (list 'concat
1818                   (gnus-parse-simple-format pre spec-alist)
1819                   (list 'gnus-set-mouse-face
1820                         (gnus-parse-simple-format button spec-alist))
1821                   (gnus-parse-simple-format post spec-alist)))
1822         (gnus-parse-simple-format
1823          (concat (substring format (match-beginning 1) (match-end 1))
1824                  (substring format (match-beginning 2) (match-end 2))
1825                  (substring format (match-beginning 3) (match-end 3)))
1826          spec-alist))
1827     (gnus-parse-simple-format format spec-alist)))
1828
1829 (defun gnus-parse-simple-format (format spec-alist)
1830   ;; This function parses the FORMAT string with the help of the
1831   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
1832   ;; string. The list will consist of the symbol `format', a format
1833   ;; specification string, and a list of forms depending on the
1834   ;; SPEC-ALIST.
1835   (let ((max-width 0)
1836         spec flist fstring b newspec max-width elem beg)
1837     (save-excursion
1838       (gnus-set-work-buffer)
1839       (insert format)
1840       (goto-char 1)
1841       (while (re-search-forward "%[-0-9]*\\(,[0-9]*\\)*\\(.\\)\\(.\\)?" nil t)
1842         (setq spec (string-to-char (buffer-substring (match-beginning 2)
1843                                                      (match-end 2))))
1844         ;; First check if there are any specs that look anything like
1845         ;; "%12,12A", ie. with a "max width specification". These have
1846         ;; to be treated specially.
1847         (if (setq beg (match-beginning 1))
1848             (setq max-width 
1849                   (string-to-int 
1850                    (buffer-substring (1+ (match-beginning 1)) (match-end 1))))
1851           (setq max-width 0)
1852           (setq beg (match-beginning 2)))
1853         ;; Find the specification from `spec-alist'.
1854         (if (not (setq elem (cdr (assq spec spec-alist))))
1855             (setq elem '("*" ?s)))
1856         ;; Treat user defined format specifiers specially
1857         (and (eq (car elem) 'user-defined)
1858              (setq elem
1859                    (list 
1860                     (list (intern (concat "gnus-user-format-function-"
1861                                           (buffer-substring
1862                                            (match-beginning 3)
1863                                            (match-end 3))))
1864                           'header)
1865                     ?s))
1866              (delete-region (match-beginning 3) (match-end 3)))
1867         (if (not (zerop max-width))
1868             (let ((el (car elem)))
1869               (cond ((= (car (cdr elem)) ?c) 
1870                      (setq el (list 'char-to-string el)))
1871                     ((= (car (cdr elem)) ?d)
1872                      (numberp el) (setq el (list 'int-to-string el))))
1873               (setq flist (cons (list 'gnus-format-max-width el max-width) 
1874                                 flist))
1875               (setq newspec ?s))
1876           (setq flist (cons (car elem) flist))
1877           (setq newspec (car (cdr elem))))
1878         ;; Remove the old specification (and possibly a ",12" string).
1879         (delete-region beg (match-end 2))
1880         ;; Insert the new specification.
1881         (goto-char beg)
1882         (insert newspec))
1883       (setq fstring (buffer-substring 1 (point-max))))
1884     (cons 'format (cons fstring (nreverse flist)))))
1885
1886 ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
1887 (defun gnus-read-init-file ()
1888   (and gnus-init-file
1889        (or (and (file-exists-p gnus-init-file) 
1890                 ;; Don't try to load a directory.
1891                 (not (file-directory-p gnus-init-file)))
1892            (file-exists-p (concat gnus-init-file ".el"))
1893            (file-exists-p (concat gnus-init-file ".elc")))
1894        (load gnus-init-file nil t)))
1895
1896 (defun gnus-set-work-buffer ()
1897   (if (get-buffer gnus-work-buffer)
1898       (progn
1899         (set-buffer gnus-work-buffer)
1900         (gnus-add-current-to-buffer-list)
1901         (erase-buffer))
1902     (set-buffer (get-buffer-create gnus-work-buffer))
1903     (buffer-disable-undo (current-buffer))))
1904
1905 ;; Article file names when saving.
1906
1907 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
1908   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1909 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group/num.
1910 Otherwise, it is like ~/News/news/group/num."
1911   (let ((default
1912           (expand-file-name
1913            (concat (if (gnus-use-long-file-name 'not-save)
1914                        (gnus-capitalize-newsgroup newsgroup)
1915                      (gnus-newsgroup-directory-form newsgroup))
1916                    "/" (int-to-string (header-number headers)))
1917            (or gnus-article-save-directory "~/News"))))
1918     (if (and last-file
1919              (string-equal (file-name-directory default)
1920                            (file-name-directory last-file))
1921              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
1922         default
1923       (or last-file default))))
1924
1925 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
1926   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1927 If variable `gnus-use-long-file-name' is nil, it is ~/News/news.group/num.
1928 Otherwise, it is like ~/News/news/group/num."
1929   (let ((default
1930           (expand-file-name
1931            (concat (if (gnus-use-long-file-name 'not-save)
1932                        newsgroup
1933                      (gnus-newsgroup-directory-form newsgroup))
1934                    "/" (int-to-string (header-number headers)))
1935            (or gnus-article-save-directory "~/News"))))
1936     (if (and last-file
1937              (string-equal (file-name-directory default)
1938                            (file-name-directory last-file))
1939              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
1940         default
1941       (or last-file default))))
1942
1943 (defun gnus-Plain-save-name (newsgroup headers &optional last-file)
1944   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1945 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group.
1946 Otherwise, it is like ~/News/news/group/news."
1947   (or last-file
1948       (expand-file-name
1949        (if (gnus-use-long-file-name 'not-save)
1950            (gnus-capitalize-newsgroup newsgroup)
1951          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
1952        (or gnus-article-save-directory "~/News"))))
1953
1954 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
1955   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1956 If variable `gnus-use-long-file-name' is nil, it is ~/News/news.group.
1957 Otherwise, it is like ~/News/news/group/news."
1958   (or last-file
1959       (expand-file-name
1960        (if (gnus-use-long-file-name 'not-save)
1961            newsgroup
1962          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
1963        (or gnus-article-save-directory "~/News"))))
1964
1965 ;; For subscribing new newsgroup
1966
1967 (defun gnus-subscribe-hierarchical-interactive (groups)
1968   (let ((groups (sort groups 'string<))
1969         prefixes prefix start rest ans group starts)
1970     (while groups
1971       (setq prefixes (list "^"))
1972       (while (and groups prefixes)
1973         (while (not (string-match (car prefixes) (car groups)))
1974           (setq prefixes (cdr prefixes)))
1975         (setq prefix (car prefixes))
1976         (setq start (1- (length prefix)))
1977         (if (and (string-match "[^\\.]\\." (car groups) start)
1978                  (cdr groups)
1979                  (setq prefix 
1980                        (concat "^" (substring (car groups) 0 (match-end 0))))
1981                  (string-match prefix (car (cdr groups))))
1982             (progn
1983               (setq prefixes (cons prefix prefixes))
1984               (message "Descend hierarchy %s? ([y]nsq): " 
1985                        (substring prefix 1 (1- (length prefix))))
1986               (setq ans (read-char))
1987               (cond ((= ans ?n)
1988                      (while (and groups 
1989                                  (string-match prefix 
1990                                                (setq group (car groups))))
1991                        (setq gnus-killed-list 
1992                              (cons group gnus-killed-list))
1993                        (gnus-sethash group group gnus-killed-hashtb)
1994                        (setq groups (cdr groups)))
1995                      (setq starts (cdr starts)))
1996                     ((= ans ?s)
1997                      (while (and groups 
1998                                  (string-match prefix 
1999                                                (setq group (car groups))))
2000                        (gnus-sethash group group gnus-killed-hashtb)
2001                        (gnus-subscribe-alphabetically (car groups))
2002                        (setq groups (cdr groups)))
2003                      (setq starts (cdr starts)))
2004                     ((= ans ?q)
2005                      (while groups
2006                        (setq group (car groups))
2007                        (setq gnus-killed-list (cons group gnus-killed-list))
2008                        (gnus-sethash group group gnus-killed-hashtb)
2009                        (setq groups (cdr groups))))
2010                     (t nil)))
2011           (message "Subscribe %s? ([n]yq)" (car groups))
2012           (setq ans (read-char))
2013           (setq group (car groups))
2014           (cond ((= ans ?y)
2015                  (gnus-subscribe-alphabetically (car groups))
2016                  (gnus-sethash group group gnus-killed-hashtb))
2017                 ((= ans ?q)
2018                  (while groups
2019                    (setq group (car groups))
2020                    (setq gnus-killed-list (cons group gnus-killed-list))
2021                    (gnus-sethash group group gnus-killed-hashtb)
2022                    (setq groups (cdr groups))))
2023                 (t 
2024                  (setq gnus-killed-list (cons group gnus-killed-list))
2025                  (gnus-sethash group group gnus-killed-hashtb)))
2026           (setq groups (cdr groups)))))))
2027
2028 (defun gnus-subscribe-randomly (newsgroup)
2029   "Subscribe new NEWSGROUP by making it the first newsgroup."
2030   (gnus-subscribe-newsgroup newsgroup))
2031
2032 (defun gnus-subscribe-alphabetically (newgroup)
2033   "Subscribe new NEWSGROUP and insert it in alphabetical order."
2034   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2035   (let ((groups (cdr gnus-newsrc-alist))
2036         before)
2037     (while (and (not before) groups)
2038       (if (string< newgroup (car (car groups)))
2039           (setq before (car (car groups)))
2040         (setq groups (cdr groups))))
2041     (gnus-subscribe-newsgroup newgroup before)))
2042
2043 (defun gnus-subscribe-hierarchically (newgroup)
2044   "Subscribe new NEWSGROUP and insert it in hierarchical newsgroup order."
2045   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2046   (save-excursion
2047     (set-buffer (find-file-noselect gnus-current-startup-file))
2048     (let ((groupkey newgroup)
2049           before)
2050       (while (and (not before) groupkey)
2051         (goto-char (point-min))
2052         (let ((groupkey-re
2053                (concat "^\\(" (regexp-quote groupkey) ".*\\)[!:]")))
2054           (while (and (re-search-forward groupkey-re nil t)
2055                       (progn
2056                         (setq before (buffer-substring
2057                                       (match-beginning 1) (match-end 1)))
2058                         (string< before newgroup)))))
2059         ;; Remove tail of newsgroup name (eg. a.b.c -> a.b)
2060         (setq groupkey
2061               (if (string-match "^\\(.*\\)\\.[^.]+$" groupkey)
2062                   (substring groupkey (match-beginning 1) (match-end 1)))))
2063       (gnus-subscribe-newsgroup newgroup before))))
2064
2065 (defun gnus-subscribe-interactively (newsgroup)
2066   "Subscribe new NEWSGROUP interactively.
2067 It is inserted in hierarchical newsgroup order if subscribed. If not,
2068 it is killed."
2069   (if (gnus-y-or-n-p (format "Subscribe new newsgroup: %s " newsgroup))
2070       (gnus-subscribe-hierarchically newsgroup)
2071     (setq gnus-killed-list (cons newsgroup gnus-killed-list))))
2072
2073 (defun gnus-subscribe-zombies (newsgroup)
2074   "Make new NEWSGROUP a zombie group."
2075   (setq gnus-zombie-list (cons newsgroup gnus-zombie-list)))
2076
2077 (defun gnus-subscribe-newsgroup (newsgroup &optional next)
2078   "Subscribe new NEWSGROUP.
2079 If NEXT is non-nil, it is inserted before NEXT. Otherwise it is made
2080 the first newsgroup."
2081   ;; We subscribe the group by changing its level to `subscribed'.
2082   (gnus-group-change-level 
2083    newsgroup gnus-level-default-subscribed
2084    gnus-level-killed (gnus-gethash (or next "dummy.group") gnus-newsrc-hashtb))
2085   (message "Subscribe newsgroup: %s" newsgroup))
2086
2087 ;; For directories
2088
2089 (defun gnus-newsgroup-directory-form (newsgroup)
2090   "Make hierarchical directory name from NEWSGROUP name."
2091   (let ((newsgroup (substring newsgroup 0)) ;Copy string.
2092         (len (length newsgroup))
2093         (idx 0))
2094     ;; Replace all occurrences of `.' with `/'.
2095     (while (< idx len)
2096       (if (= (aref newsgroup idx) ?.)
2097           (aset newsgroup idx ?/))
2098       (setq idx (1+ idx)))
2099     newsgroup
2100     ))
2101
2102 (defun gnus-make-directory (dir)
2103   "Make DIRECTORY recursively."
2104   (let* ((dir (expand-file-name dir default-directory))
2105          dirs)
2106     (if (string-match "/$" dir)
2107         (setq dir (substring dir 0 (match-beginning 0))))
2108     (while (not (file-exists-p dir))
2109       (setq dirs (cons dir dirs))
2110       (string-match "/[^/]+$" dir)
2111       (setq dir (substring dir 0 (match-beginning 0))))
2112     (while dirs
2113       (make-directory (car dirs))
2114       (setq dirs (cdr dirs)))))
2115
2116 (defun gnus-capitalize-newsgroup (newsgroup)
2117   "Capitalize NEWSGROUP name."
2118   (and (not (zerop (length newsgroup)))
2119        (concat (char-to-string (upcase (aref newsgroup 0)))
2120                (substring newsgroup 1))))
2121
2122 ;; Var
2123
2124 (defun gnus-simplify-subject (subject &optional re-only)
2125   "Remove `Re:' and words in parentheses.
2126 If optional argument RE-ONLY is non-nil, strip `Re:' only."
2127   (let ((case-fold-search t))           ;Ignore case.
2128     ;; Remove `Re:' and `Re^N:'.
2129     (if (string-match "^re:[ \t]*" subject)
2130         (setq subject (substring subject (match-end 0))))
2131     ;; Remove words in parentheses from end.
2132     (or re-only
2133         (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
2134           (setq subject (substring subject 0 (match-beginning 0)))))
2135     ;; Return subject string.
2136     subject))
2137
2138 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
2139 ;; all whitespace.
2140 (defun gnus-simplify-subject-fuzzy (subject)
2141   (let ((case-fold-search t))
2142     (save-excursion
2143       (gnus-set-work-buffer)
2144       (insert subject)
2145       (goto-char (point-min))
2146       (and (looking-at "^[ \t]*re:[ \t]*")
2147            (replace-match "" t t))
2148       (while (re-search-forward "[ \t\n]*([^()]*)[ \t\n]*\\'" nil t)
2149         (replace-match "" t t)
2150         (goto-char (point-min)))
2151       (while (re-search-forward "[ \t]+" nil 'move)
2152         (replace-match " " t t))
2153       ;; the 'move above makes sure we are at (point-max)
2154       (and (= (preceding-char) ? )
2155            (delete-char -1))
2156       (buffer-string))))
2157
2158 ;; Add the current buffer to the list of buffers to be killed on exit. 
2159 (defun gnus-add-current-to-buffer-list ()
2160   (setq gnus-buffer-list (cons (current-buffer) gnus-buffer-list)))
2161
2162 (defun gnus-string> (s1 s2)
2163   (not (or (string< s1 s2)
2164            (string= s1 s2))))
2165
2166 ;; Functions accessing headers.
2167 ;; Functions are more convenient than macros in some cases.
2168
2169 (defun gnus-header-number (header)
2170   (header-number header))
2171
2172 (defun gnus-header-subject (header)
2173   (header-subject header))
2174
2175 (defun gnus-header-from (header)
2176   (header-from header))
2177
2178 (defun gnus-header-xref (header)
2179   (header-xref header))
2180
2181 (defun gnus-header-lines (header)
2182   (header-lines header))
2183
2184 (defun gnus-header-date (header)
2185   (header-date header))
2186
2187 (defun gnus-header-id (header)
2188   (header-id header))
2189
2190 (defun gnus-header-references (header)
2191   (header-references header))
2192
2193 ;;; General various misc type functions.
2194
2195 (defun gnus-clear-system ()
2196   "Clear all variables and buffers."
2197   ;; Clear Gnus variables.
2198   (let ((variables gnus-variable-list))
2199     (while variables
2200       (set (car variables) nil)
2201       (setq variables (cdr variables))))
2202   ;; Clear other internal variables.
2203   (setq gnus-list-of-killed-groups nil
2204         gnus-have-read-active-file nil
2205         gnus-newsrc-alist nil
2206         gnus-newsrc-hashtb nil
2207         gnus-killed-list nil
2208         gnus-zombie-list nil
2209         gnus-killed-hashtb nil
2210         gnus-active-hashtb nil
2211         gnus-moderated-list nil
2212         gnus-description-hashtb nil
2213         gnus-newsgroup-headers nil
2214         gnus-newsgroup-headers-hashtb-by-number nil
2215         gnus-newsgroup-name nil
2216         gnus-server-alist nil
2217         gnus-internal-global-score-files nil
2218         gnus-current-select-method nil)
2219   ;; Reset any score variables.
2220   (and (boundp 'gnus-score-cache)
2221        (set 'gnus-score-cache nil))
2222   ;; Kill the startup file.
2223   (and gnus-current-startup-file
2224        (get-file-buffer gnus-current-startup-file)
2225        (kill-buffer (get-file-buffer gnus-current-startup-file)))
2226   ;; Save any cache buffers.
2227   (and gnus-use-cache (gnus-cache-save-buffers))
2228   ;; Clear the dribble buffer.
2229   (gnus-dribble-clear)
2230   ;; Kill global KILL file buffer.
2231   (if (get-file-buffer (gnus-newsgroup-kill-file nil))
2232       (kill-buffer (get-file-buffer (gnus-newsgroup-kill-file nil))))
2233   (gnus-kill-buffer nntp-server-buffer)
2234   ;; Kill Gnus buffers.
2235   (while gnus-buffer-list
2236     (gnus-kill-buffer (car gnus-buffer-list))
2237     (setq gnus-buffer-list (cdr gnus-buffer-list))))
2238
2239 (defun gnus-configure-windows (action &optional force)
2240   "Configure Gnus windows according to the next ACTION.
2241 The ACTION is either a symbol, such as `summary', or a
2242 configuration list such as `(1 1 2)'.  If ACTION is not a list,
2243 configuration list is got from the variable gnus-window-configuration.
2244 If FORCE is non-nil, the updating will be done whether it is necessary
2245 or not."
2246   (let* ((windows
2247           (if (listp action) action 
2248             (if (listp gnus-window-configuration)
2249                 (car (cdr (assq action gnus-window-configuration)))
2250               gnus-window-configuration)))
2251          (grpwin (get-buffer-window gnus-group-buffer))
2252          (subwin (get-buffer-window gnus-summary-buffer))
2253          (artwin (get-buffer-window gnus-article-buffer))
2254          (winsum nil)
2255          (height nil)
2256          (grpheight 0)
2257          (subheight 0)
2258          (artheight 0)
2259          ;; Make split-window-vertically leave focus in upper window.
2260          (split-window-keep-point t))
2261     (if (and (symbolp windows) (fboundp windows))
2262         (funcall windows action)
2263       (if (and (not force)
2264                (or (null windows)               ;No configuration is specified.
2265                    (and (eq (null grpwin)
2266                             (zerop (nth 0 windows)))
2267                         (eq (null subwin)
2268                             (zerop (nth 1 windows)))
2269                         (eq (null artwin)
2270                             (zerop (nth 2 windows))))))
2271           ;; No need to change window configuration.
2272           nil
2273         (select-window (or grpwin subwin artwin (selected-window)))
2274         ;; First of all, compute the height of each window.
2275         (cond (gnus-use-full-window
2276                ;; Take up the entire screen.
2277                (delete-other-windows)
2278                (setq height (window-height (selected-window))))
2279               (t
2280                (setq height (+ (if grpwin (window-height grpwin) 0)
2281                                (if subwin (window-height subwin) 0)
2282                                (if artwin (window-height artwin) 0)))))
2283         ;; The group buffer exits always. So, use it to extend the
2284         ;; group window so as to get enough window space.
2285         (switch-to-buffer gnus-group-buffer 'norecord)
2286         (and (get-buffer gnus-summary-buffer)
2287              (delete-windows-on gnus-summary-buffer))
2288         (and (get-buffer gnus-article-buffer)
2289              (delete-windows-on gnus-article-buffer))
2290         ;; Compute expected window height.
2291         (setq winsum (apply (function +) windows))
2292         (if (not (zerop (nth 0 windows)))
2293             (setq grpheight (max window-min-height
2294                                  (/ (* height (nth 0 windows)) winsum))))
2295         (if (not (zerop (nth 1 windows)))
2296             (setq subheight (max window-min-height
2297                                  (/ (* height (nth 1 windows)) winsum))))
2298         (if (not (zerop (nth 2 windows)))
2299             (if gnus-split-window ;hack by erik
2300                 (setq artheight height)
2301               (setq artheight (max window-min-height
2302                                    (/ (* height (nth 2 windows)) winsum)))))
2303         (setq height (+ grpheight subheight artheight))
2304         (enlarge-window (max 0 (- height (window-height (selected-window)))))
2305         ;; Then split the window.
2306         (if (and (not (zerop artheight))
2307                  (or (not (zerop grpheight))
2308                      (not (zerop subheight))))
2309             (if gnus-split-window
2310                 (split-window-horizontally)
2311               (split-window-vertically (+ grpheight subheight))))
2312         (and (not (zerop grpheight))
2313              (not (zerop subheight))
2314              (split-window-vertically grpheight))
2315         ;; Then select buffers in each window.
2316         (or (zerop grpheight)
2317             (progn
2318               (switch-to-buffer gnus-group-buffer 'norecord)
2319               (other-window 1)))
2320         (or (zerop subheight)
2321             (progn
2322               (switch-to-buffer gnus-summary-buffer 'norecord)
2323               (other-window 1)))
2324         (or (zerop artheight)
2325             (progn
2326               ;; If article buffer does not exist, it will be created
2327               ;; and initialized.
2328               (gnus-article-setup-buffer)
2329               (switch-to-buffer gnus-article-buffer 'norecord)
2330               (setq buffer-read-only t) ; !!! Why!?! 
2331               (bury-buffer gnus-summary-buffer)
2332               (bury-buffer gnus-group-buffer)))
2333         (or (zerop subheight)
2334             (progn
2335               (pop-to-buffer gnus-summary-buffer)
2336               ;; It seems that some code in this function will set
2337               ;; buffer-read-only to nil. I have absolutely no idea
2338               ;; why. 
2339               (setq buffer-read-only t))))))) ; !!! Why!?! 
2340
2341 (defun gnus-window-configuration-split (action)
2342   (switch-to-buffer gnus-group-buffer t)
2343   (delete-other-windows)
2344   (split-window-horizontally)
2345   (cond ((memq action '(newsgoups summary))
2346          (if (gnus-buffer-exists-p gnus-summary-buffer)
2347              (switch-to-buffer-other-window gnus-summary-buffer)))
2348         ((eq action 'article)
2349          (switch-to-buffer gnus-summary-buffer t)
2350          (other-window 1)
2351          (gnus-article-setup-buffer)
2352          (switch-to-buffer gnus-article-buffer t))))
2353
2354 (defun gnus-version ()
2355   "Version numbers of this version of Gnus."
2356   (interactive)
2357   (let ((methods gnus-valid-select-methods)
2358         (mess gnus-version)
2359         meth)
2360     ;; Go through all the legal select methods and add their version
2361     ;; numbers to the total version string. Only the backends that are
2362     ;; currently in use will have their message numbers taken into
2363     ;; consideration. 
2364     (while methods
2365       (setq meth (intern (concat (car (car methods)) "-version")))
2366       (and (boundp meth)
2367            (stringp (symbol-value meth))
2368            (setq mess (concat mess "; " (symbol-value meth))))
2369       (setq methods (cdr methods)))
2370     (message mess)))
2371
2372 (defun gnus-info-find-node ()
2373   "Find Info documentation of Gnus."
2374   (interactive)
2375   ;; Enlarge info window if needed.
2376   (cond ((eq major-mode 'gnus-group-mode)
2377          (gnus-configure-windows '(1 0 0)) ;Take all windows.
2378          (pop-to-buffer gnus-group-buffer))
2379         ((eq major-mode 'gnus-summary-mode)
2380          (gnus-configure-windows '(0 1 0)) ;Take all windows.
2381          (pop-to-buffer gnus-summary-buffer)))
2382   (Info-goto-node (car (cdr (assq major-mode gnus-info-nodes)))))
2383
2384 (defun gnus-bug ()
2385   "Send a bug report to the Gnus maintainers."
2386   (interactive)
2387   (pop-to-buffer "*Gnus Bug*")
2388   (erase-buffer)
2389   (mail-mode)
2390   (mail-setup gnus-maintainer "[Gnus Bug Report] " nil nil nil nil)
2391   (goto-char (point-min))
2392   (re-search-forward (concat "^" (regexp-quote mail-header-separator) "$"))
2393   (forward-line 1)
2394   (insert (format "%s\n%s\n\n" (gnus-version) (emacs-version)))
2395   (gnus-debug)
2396   (message ""))
2397
2398 (defun gnus-debug ()
2399   "Attemps to go through the Gnus source file and report what variables have been changed.
2400 The source file has to be in the Emacs load path."
2401   (interactive)
2402   (let ((dirs load-path)
2403         file expr olist)
2404     (while dirs
2405       (if (file-exists-p (setq file (concat (car dirs) "/gnus.el")))
2406           (save-excursion
2407             (setq dirs nil)
2408             (set-buffer (get-buffer-create "*gnus bug info*"))
2409             (buffer-disable-undo (current-buffer))
2410             (erase-buffer)
2411             (insert-file-contents file)
2412             (goto-char (point-min))
2413             (or (search-forward "\n;; Internal variables" nil t)
2414                 (error "Malformed sources"))
2415             (narrow-to-region (point-min) (point))
2416             (goto-char (point-min))
2417             (while (setq expr (condition-case () 
2418                                   (read (current-buffer)) (error nil)))
2419               (and (eq (car expr) 'defvar)
2420                    (stringp (nth 3 expr))
2421                    (not (equal (eval (nth 2 expr))
2422                                (and (boundp (nth 1 expr))
2423                                     (symbol-value (nth 1 expr)))))
2424                    (setq olist (cons (nth 1 expr) olist))))
2425             (kill-buffer (current-buffer)))
2426         (setq dirs (cdr dirs))))
2427     (while olist
2428       (insert "(setq " (symbol-name (car olist)) " '" 
2429               (prin1-to-string (symbol-value (car olist))) ")\n")
2430       (setq olist (cdr olist)))
2431     (insert "\n\n")))
2432
2433 (defun gnus-overload-functions (&optional overloads)
2434   "Overload functions specified by optional argument OVERLOADS.
2435 If nothing is specified, use the variable gnus-overload-functions."
2436   (let ((defs nil)
2437         (overloads (or overloads gnus-overload-functions)))
2438     (while overloads
2439       (setq defs (car overloads))
2440       (setq overloads (cdr overloads))
2441       ;; Load file before overloading function if necessary.  Make
2442       ;; sure we cannot use `require' always.
2443       (and (not (fboundp (car defs)))
2444            (car (cdr (cdr defs)))
2445            (load (car (cdr (cdr defs))) nil 'nomessage))
2446       (fset (car defs) (car (cdr defs))))))
2447
2448 (defun gnus-replace-chars-in-string (string from to)
2449   "Replace characters in STRING from FROM to TO."
2450   (let ((string (substring string 0))   ;Copy string.
2451         (len (length string))
2452         (idx 0))
2453     ;; Replace all occurrences of FROM with TO.
2454     (while (< idx len)
2455       (if (= (aref string idx) from)
2456           (aset string idx to))
2457       (setq idx (1+ idx)))
2458     string))
2459
2460 (defun gnus-days-between (date1 date2)
2461   ;; Return the number of days between date1 and date2.
2462   (- (gnus-day-number date1) (gnus-day-number date2)))
2463
2464 (defun gnus-day-number (date)
2465   (let ((dat (mapcar (lambda (s) (and s (string-to-int s)) )
2466                      (timezone-parse-date date))))
2467     (timezone-absolute-from-gregorian 
2468      (nth 1 dat) (nth 2 dat) (car dat))))
2469
2470 ;; Returns a floating point number that says how many seconds have
2471 ;; lapsed between Jan 1 12:00:00 1970 and DATE.
2472 (defun gnus-seconds-since-epoch (date)
2473   (let* ((tdate (mapcar (lambda (ti) (and ti (string-to-int ti)))
2474                         (timezone-parse-date date)))
2475          (ttime (mapcar (lambda (ti) (and ti (string-to-int ti)))
2476                         (timezone-parse-time
2477                          (aref (timezone-parse-date date) 3))))
2478          (edate (mapcar (lambda (ti) (and ti (string-to-int ti)))
2479                         (timezone-parse-date "Jan 1 12:00:00 1970")))
2480          (tday (- (timezone-absolute-from-gregorian 
2481                    (nth 1 tdate) (nth 2 tdate) (nth 0 tdate))
2482                   (timezone-absolute-from-gregorian 
2483                    (nth 1 edate) (nth 2 edate) (nth 0 edate)))))
2484     (+ (nth 2 ttime)
2485        (* (nth 1 ttime) 60)
2486        (* 1.0 (nth 0 ttime) 60 60)
2487        (* 1.0 tday 60 60 24))))
2488
2489 (defun gnus-file-newer-than (file date)
2490   (let ((fdate (nth 5 (file-attributes file))))
2491     (or (> (car fdate) (car date))
2492         (and (= (car fdate) (car date))
2493              (> (nth 1 fdate) (nth 1 date))))))
2494
2495 ;; Two silly functions to ensure that all `y-or-n-p' questions clear
2496 ;; the echo area.
2497 (defun gnus-y-or-n-p (prompt)
2498   (prog1
2499       (y-or-n-p prompt)
2500     (message "")))
2501
2502 (defun gnus-yes-or-no-p (prompt)
2503   (prog1
2504       (yes-or-no-p prompt)
2505     (message "")))
2506
2507 ;; Check whether to use long file names.
2508 (defun gnus-use-long-file-name (symbol)
2509   ;; The variable has to be set...
2510   (and gnus-use-long-file-name
2511        ;; If it isn't a list, then we return t.
2512        (or (not (listp gnus-use-long-file-name))
2513            ;; If it is a list, and the list contains `symbol', we
2514            ;; return nil.  
2515            (not (memq symbol gnus-use-long-file-name)))))
2516
2517 ;; I suspect there's a better way, but I haven't taken the time to do
2518 ;; it yet. -erik selberg@cs.washington.edu
2519 (defun gnus-dd-mmm (messy-date)
2520   "Return a string like DD-MMM from a big messy string"
2521   (let ((datevec (timezone-parse-date messy-date)))
2522     (format "%2s-%s"
2523             (or (aref datevec 2) "??")
2524             (capitalize
2525              (or (car 
2526                   (nth (1- (string-to-number (aref datevec 1)))
2527                        timezone-months-assoc))
2528                  "???")))))
2529
2530 ;; Make a hash table (default and minimum size is 255).
2531 ;; Optional argument HASHSIZE specifies the table size.
2532 (defun gnus-make-hashtable (&optional hashsize)
2533   (make-vector (if hashsize (max (gnus-create-hash-size hashsize) 255) 255) 0))
2534
2535 ;; Make a number that is suitable for hashing; bigger than MIN and one
2536 ;; less than 2^x.
2537 (defun gnus-create-hash-size (min)
2538   (let ((i 1))
2539     (while (< i min)
2540       (setq i (* 2 i)))
2541     (1- i)))
2542
2543 ;;; List and range functions
2544
2545 (defun gnus-last-element (list)
2546   "Return last element of LIST."
2547   (while (cdr list)
2548     (setq list (cdr list)))
2549   (car list))
2550
2551 (defun gnus-copy-sequence (list)
2552   "Do a complete, total copy of a list."
2553   (if (and (consp list) (not (consp (cdr list))))
2554       (cons (car list) (cdr list))
2555     (mapcar (lambda (elem) (if (consp elem) 
2556                                (if (consp (cdr elem))
2557                                    (gnus-copy-sequence elem)
2558                                  (cons (car elem) (cdr elem)))
2559                              elem))
2560             list)))
2561
2562 (defun gnus-set-difference (list1 list2)
2563   "Return a list of elements of LIST1 that do not appear in LIST2."
2564   (let ((list1 (copy-sequence list1)))
2565     (while list2
2566       (setq list1 (delq (car list2) list1))
2567       (setq list2 (cdr list2)))
2568     list1))
2569
2570 (defun gnus-sorted-complement (list1 list2)
2571   "Return a list of elements of LIST1 that do not appear in LIST2.
2572 Both lists have to be sorted over <."
2573   (let (out)
2574     (if (or (null list1) (null list2))
2575         (or list1 list2)
2576       (while (and list1 list2)
2577         (cond ((= (car list1) (car list2))
2578                (setq list1 (cdr list1)
2579                      list2 (cdr list2)))
2580               ((< (car list1) (car list2))
2581                (setq out (cons (car list1) out))
2582                (setq list1 (cdr list1)))
2583               (t
2584                (setq out (cons (car list2) out))
2585                (setq list2 (cdr list2)))))
2586       (nconc (nreverse out) (or list1 list2)))))
2587
2588 (defun gnus-intersection (list1 list2)      
2589   (let ((result nil))
2590     (while list2
2591       (if (memq (car list2) list1)
2592           (setq result (cons (car list2) result)))
2593       (setq list2 (cdr list2)))
2594     result))
2595
2596 (defun gnus-sorted-intersection (list1 list2)
2597   ;; LIST1 and LIST2 have to be sorted over <.
2598   (let (out)
2599     (while (and list1 list2)
2600       (cond ((= (car list1) (car list2))
2601              (setq out (cons (car list1) out)
2602                    list1 (cdr list1)
2603                    list2 (cdr list2)))
2604             ((< (car list1) (car list2))
2605              (setq list1 (cdr list1)))
2606             (t
2607              (setq list2 (cdr list2)))))
2608     (nreverse out)))
2609
2610 (defun gnus-set-sorted-intersection (list1 list2)
2611   ;; LIST1 and LIST2 have to be sorted over <.
2612   ;; This function modifies LIST1.
2613   (let* ((top (cons nil list1))
2614          (prev top))
2615   (while (and list1 list2)
2616     (cond ((= (car list1) (car list2))
2617            (setq prev list1
2618                  list1 (cdr list1)
2619                  list2 (cdr list2)))
2620           ((< (car list1) (car list2))
2621            (setcdr prev (cdr list1))
2622            (setq list1 (cdr list1)))
2623           (t
2624            (setq list2 (cdr list2)))))
2625   (setcdr prev nil)
2626   (cdr top)))
2627
2628 (defun gnus-compress-sequence (numbers &optional always-list)
2629   "Convert list of numbers to a list of ranges or a single range.
2630 If ALWAYS-LIST is non-nil, this function will always release a list of
2631 ranges."
2632   (let* ((first (car numbers))
2633          (last (car numbers))
2634          result)
2635     (if (null numbers)
2636         nil
2637       (if (not (listp (cdr numbers)))
2638           numbers
2639         (while numbers
2640           (cond ((= last (car numbers)) nil) ;Omit duplicated number
2641                 ((= (1+ last) (car numbers)) ;Still in sequence
2642                  (setq last (car numbers)))
2643                 (t                      ;End of one sequence
2644                  (setq result 
2645                        (cons (if (= first last) first
2646                                (cons first last)) result))
2647                  (setq first (car numbers))
2648                  (setq last  (car numbers))))
2649           (setq numbers (cdr numbers)))
2650         (if (and (not always-list) (null result))
2651             (if (= first last) (list first) (cons first last))
2652           (nreverse (cons (if (= first last) first (cons first last))
2653                           result)))))))
2654
2655 (defalias 'gnus-uncompress-sequence 'gnus-uncompress-range)
2656 (defun gnus-uncompress-range (ranges)
2657   "Expand a list of ranges into a list of numbers.
2658 RANGES is either a single range on the form `(num . num)' or a list of
2659 these ranges."
2660   (let (first last result)
2661     (cond 
2662      ((null ranges)
2663       nil)
2664      ((not (listp (cdr ranges)))
2665       (setq first (car ranges))
2666       (setq last (cdr ranges))
2667       (while (<= first last)
2668         (setq result (cons first result))
2669         (setq first (1+ first)))
2670       (nreverse result))
2671      (t
2672       (while ranges
2673         (if (atom (car ranges))
2674             (if (numberp (car ranges))
2675                 (setq result (cons (car ranges) result)))
2676           (setq first (car (car ranges)))
2677           (setq last  (cdr (car ranges)))
2678           (while (<= first last)
2679             (setq result (cons first result))
2680             (setq first (1+ first))))
2681         (setq ranges (cdr ranges)))
2682       (nreverse result)))))
2683
2684 (defun gnus-add-to-range (ranges list)
2685   "Return a list of ranges that has all articles from both RANGES and LIST.
2686 Note: LIST has to be sorted over `<'."
2687   (if (not ranges)
2688       (gnus-compress-sequence list t)
2689     (setq list (copy-sequence list))
2690     (or (listp (cdr ranges))
2691         (setq ranges (list ranges)))
2692     (let ((out ranges)
2693           ilist lowest highest temp)
2694       (while (and ranges list)
2695         (setq ilist list)
2696         (setq lowest (or (and (atom (car ranges)) (car ranges))
2697                          (car (car ranges))))
2698         (while (and list (cdr list) (< (car (cdr list)) lowest))
2699           (setq list (cdr list)))
2700         (if (< (car ilist) lowest)
2701             (progn
2702               (setq temp list)
2703               (setq list (cdr list))
2704               (setcdr temp nil)
2705               (setq out (nconc (gnus-compress-sequence ilist t) out))))
2706         (setq highest (or (and (atom (car ranges)) (car ranges))
2707                           (cdr (car ranges))))
2708         (while (and list (<= (car list) highest))
2709           (setq list (cdr list)))
2710         (setq ranges (cdr ranges)))
2711       (if list
2712           (setq out (nconc (gnus-compress-sequence list t) out)))
2713       (setq out (sort out (lambda (r1 r2) 
2714                             (< (or (and (atom r1) r1) (car r1))
2715                                (or (and (atom r2) r2) (car r2))))))
2716       (setq ranges out)
2717       (while ranges
2718         (if (atom (car ranges))
2719             (if (cdr ranges)
2720                 (if (atom (car (cdr ranges)))
2721                     (if (= (1+ (car ranges)) (car (cdr ranges)))
2722                         (progn
2723                           (setcar ranges (cons (car ranges) 
2724                                                (car (cdr ranges))))
2725                           (setcdr ranges (cdr (cdr ranges)))))
2726                   (if (= (1+ (car ranges)) (car (car (cdr ranges))))
2727                       (progn
2728                         (setcar (car (cdr ranges)) (car ranges))
2729                         (setcar ranges (car (cdr ranges)))
2730                         (setcdr ranges (cdr (cdr ranges)))))))
2731           (if (cdr ranges)
2732               (if (atom (car (cdr ranges)))
2733                   (if (= (1+ (cdr (car ranges))) (car (cdr ranges)))
2734                       (progn
2735                         (setcdr (car ranges) (car (cdr ranges)))
2736                         (setcdr ranges (cdr (cdr ranges)))))
2737                 (if (= (1+ (cdr (car ranges))) (car (car (cdr ranges))))
2738                     (progn
2739                       (setcdr (car ranges) (cdr (car (cdr ranges))))
2740                       (setcdr ranges (cdr (cdr ranges))))))))
2741         (setq ranges (cdr ranges)))
2742       out)))
2743
2744 (defun gnus-remove-from-range (ranges list)
2745   "Return a list of ranges that has all articles from LIST removed from RANGES.
2746 Note: LIST has to be sorted over `<'."
2747   ;; !!! This function shouldn't look like this, but I've got a headache.
2748   (gnus-compress-sequence 
2749    (gnus-sorted-complement
2750     (gnus-uncompress-range ranges) list)))
2751
2752 (defun gnus-member-of-range (number ranges)
2753   (if (not (listp (cdr ranges)))
2754       (and (>= number (car ranges)) 
2755            (<= number (cdr ranges)))
2756     (let ((not-stop t))
2757       (while (and ranges 
2758                   (if (numberp (car ranges))
2759                       (>= number (car ranges))
2760                     (>= number (car (car ranges))))
2761                   not-stop)
2762         (if (if (numberp (car ranges))
2763                 (= number (car ranges))
2764               (and (>= number (car (car ranges)))
2765                    (<= number (cdr (car ranges)))))
2766             (setq not-stop nil))
2767         (setq ranges (cdr ranges)))
2768       (not not-stop))))
2769
2770 \f
2771 ;;;
2772 ;;; Gnus group mode
2773 ;;;
2774
2775 (defvar gnus-group-mode-map nil)
2776 (defvar gnus-group-make-map nil)
2777 (defvar gnus-group-list-map nil)
2778 (defvar gnus-group-sub-map nil)
2779 (put 'gnus-group-mode 'mode-class 'special)
2780
2781 (if gnus-group-mode-map
2782     nil
2783   (setq gnus-group-mode-map (make-keymap))
2784   (suppress-keymap gnus-group-mode-map)
2785   (define-key gnus-group-mode-map " " 'gnus-group-read-group)
2786   (define-key gnus-group-mode-map "=" 'gnus-group-select-group)
2787   (define-key gnus-group-mode-map "\r" 'gnus-group-select-group)
2788   (define-key gnus-group-mode-map "j" 'gnus-group-jump-to-group)
2789   (define-key gnus-group-mode-map "n" 'gnus-group-next-unread-group)
2790   (define-key gnus-group-mode-map "p" 'gnus-group-prev-unread-group)
2791   (define-key gnus-group-mode-map "\177" 'gnus-group-prev-unread-group)
2792   (define-key gnus-group-mode-map "N" 'gnus-group-next-group)
2793   (define-key gnus-group-mode-map "P" 'gnus-group-prev-group)
2794   (define-key gnus-group-mode-map "\M-n" 'gnus-group-next-unread-group-same-level)
2795   (define-key gnus-group-mode-map "\M-p" 'gnus-group-prev-unread-group-same-level)
2796   (define-key gnus-group-mode-map "," 'gnus-group-best-unread-group)
2797   (define-key gnus-group-mode-map "." 'gnus-group-first-unread-group)
2798   (define-key gnus-group-mode-map "u" 'gnus-group-unsubscribe-current-group)
2799   (define-key gnus-group-mode-map "U" 'gnus-group-unsubscribe-group)
2800   (define-key gnus-group-mode-map "c" 'gnus-group-catchup-current)
2801   (define-key gnus-group-mode-map "C" 'gnus-group-catchup-current-all)
2802   (define-key gnus-group-mode-map "l" 'gnus-group-list-groups)
2803   (define-key gnus-group-mode-map "L" 'gnus-group-list-all-groups)
2804   (define-key gnus-group-mode-map "m" 'gnus-group-mail)
2805   (define-key gnus-group-mode-map "g" 'gnus-group-get-new-news)
2806   (define-key gnus-group-mode-map "\M-g" 'gnus-group-get-new-news-this-group)
2807   (define-key gnus-group-mode-map "R" 'gnus-group-restart)
2808   (define-key gnus-group-mode-map "r" 'gnus-group-read-init-file)
2809   (define-key gnus-group-mode-map "B" 'gnus-group-browse-foreign-server)
2810   (define-key gnus-group-mode-map "b" 'gnus-group-check-bogus-groups)
2811   (define-key gnus-group-mode-map "F" 'gnus-find-new-newsgroups)
2812   (define-key gnus-group-mode-map "\C-c\C-d" 'gnus-group-describe-group)
2813   (define-key gnus-group-mode-map "\M-d" 'gnus-group-describe-all-groups)
2814   (define-key gnus-group-mode-map "\C-c\C-a" 'gnus-group-apropos)
2815   (define-key gnus-group-mode-map "\C-c\M-C-a" 'gnus-group-description-apropos)
2816   (define-key gnus-group-mode-map "a" 'gnus-group-post-news)
2817   (define-key gnus-group-mode-map "\ek" 'gnus-group-edit-local-kill)
2818   (define-key gnus-group-mode-map "\eK" 'gnus-group-edit-global-kill)
2819   (define-key gnus-group-mode-map "\C-k" 'gnus-group-kill-group)
2820   (define-key gnus-group-mode-map "\C-y" 'gnus-group-yank-group)
2821   (define-key gnus-group-mode-map "\C-w" 'gnus-group-kill-region)
2822   (define-key gnus-group-mode-map "\C-x\C-t" 'gnus-group-transpose-groups)
2823   (define-key gnus-group-mode-map "\C-c\C-l" 'gnus-group-list-killed)
2824   (define-key gnus-group-mode-map "\C-c\C-x" 'gnus-group-expire-articles)
2825   (define-key gnus-group-mode-map "\C-c\M-\C-x" 'gnus-group-expire-all-groups)
2826   (define-key gnus-group-mode-map "V" 'gnus-version)
2827   (define-key gnus-group-mode-map "s" 'gnus-group-save-newsrc)
2828   (define-key gnus-group-mode-map "z" 'gnus-group-suspend)
2829   (define-key gnus-group-mode-map "Z" 'gnus-group-clear-dribble)
2830   (define-key gnus-group-mode-map "q" 'gnus-group-exit)
2831   (define-key gnus-group-mode-map "Q" 'gnus-group-quit)
2832   (define-key gnus-group-mode-map "\M-f" 'gnus-group-fetch-faq)
2833   (define-key gnus-group-mode-map "?" 'gnus-group-describe-briefly)
2834   (define-key gnus-group-mode-map "\C-c\C-i" 'gnus-info-find-node)
2835   (define-key gnus-group-mode-map "\M-e" 'gnus-group-edit-group-method)
2836   (define-key gnus-group-mode-map "^" 'gnus-group-enter-server-mode)
2837   (define-key gnus-group-mode-map
2838     (if gnus-xemacs [button2] [mouse-2]) 'gnus-mouse-pick-group)
2839   (define-key gnus-group-mode-map "<" 'beginning-of-buffer)
2840   (define-key gnus-group-mode-map ">" 'end-of-buffer)
2841   (define-key gnus-group-mode-map "\C-c\C-b" 'gnus-bug)
2842
2843   (define-prefix-command 'gnus-group-make-map)
2844   (define-key gnus-group-mode-map "M" 'gnus-group-make-map)
2845   (define-key gnus-group-make-map "d" 'gnus-group-make-directory-group)
2846   (define-key gnus-group-make-map "h" 'gnus-group-make-help-group)
2847   (define-key gnus-group-make-map "a" 'gnus-group-make-archive-group)
2848   (define-key gnus-group-make-map "k" 'gnus-group-make-kiboze-group)
2849   (define-key gnus-group-make-map "m" 'gnus-group-make-group)
2850   (define-key gnus-group-make-map "E" 'gnus-group-edit-group)
2851   (define-key gnus-group-make-map "e" 'gnus-group-edit-group-method)
2852   (define-key gnus-group-make-map "p" 'gnus-group-edit-group-parameters)
2853
2854   (define-prefix-command 'gnus-group-list-map)
2855   (define-key gnus-group-mode-map "G" 'gnus-group-list-map)
2856   (define-key gnus-group-list-map "k" 'gnus-group-list-killed)
2857   (define-key gnus-group-list-map "z" 'gnus-group-list-zombies)
2858   (define-key gnus-group-list-map "s" 'gnus-group-list-groups)
2859   (define-key gnus-group-list-map "u" 'gnus-group-list-all-groups)
2860   (define-key gnus-group-list-map "a" 'gnus-group-apropos)
2861   (define-key gnus-group-list-map "d" 'gnus-group-description-apropos)
2862   (define-key gnus-group-list-map "m" 'gnus-group-list-matching)
2863   (define-key gnus-group-list-map "M" 'gnus-group-list-all-matching)
2864
2865   (define-prefix-command 'gnus-group-sub-map)
2866   (define-key gnus-group-mode-map "S" 'gnus-group-sub-map)
2867   (define-key gnus-group-sub-map "l" 'gnus-group-set-current-level)
2868   (define-key gnus-group-sub-map "t" 'gnus-group-unsubscribe-current-group)
2869   (define-key gnus-group-sub-map "s" 'gnus-group-unsubscribe-group)
2870   (define-key gnus-group-sub-map "k" 'gnus-group-kill-group)
2871   (define-key gnus-group-sub-map "y" 'gnus-group-yank-group)
2872   (define-key gnus-group-sub-map "w" 'gnus-group-kill-region)
2873   (define-key gnus-group-sub-map "z" 'gnus-group-kill-all-zombies))
2874
2875 (defun gnus-group-mode ()
2876   "Major mode for reading news.
2877
2878 All normal editing commands are switched off.
2879 \\<gnus-group-mode-map>
2880 The group buffer lists (some of) the groups available.  For instance,
2881 `\\[gnus-group-list-groups]' will list all subscribed groups with unread articles, while `\\[gnus-group-list-zombies]'
2882 lists all zombie groups. 
2883
2884 Groups that are displayed can be entered with `\\[gnus-group-read-group]'.  To subscribe 
2885 to a group not displayed, type `\\[gnus-group-unsubscribe-group]'. 
2886
2887 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]'). 
2888
2889 The following commands are available:
2890
2891 \\{gnus-group-mode-map}"
2892   (interactive)
2893   (if gnus-visual (gnus-group-make-menu-bar))
2894   (kill-all-local-variables)
2895   (setq mode-line-modified "-- ")
2896   (make-local-variable 'mode-line-format)
2897   (setq mode-line-format (copy-sequence mode-line-format))
2898   (and (equal (nth 3 mode-line-format) "   ")
2899        (setcar (nthcdr 3 mode-line-format) ""))
2900   (setq major-mode 'gnus-group-mode)
2901   (setq mode-name "Group")
2902   (gnus-group-set-mode-line)
2903   (setq mode-line-process nil)
2904   (use-local-map gnus-group-mode-map)
2905   (buffer-disable-undo (current-buffer))
2906   (setq truncate-lines t)
2907   (setq buffer-read-only t)
2908   (run-hooks 'gnus-group-mode-hook))
2909
2910 (defun gnus-mouse-pick-group (e)
2911   (interactive "e")
2912   (mouse-set-point e)
2913   (gnus-group-read-group nil))
2914
2915 ;;;###autoload
2916 (defun gnus-no-server (&optional arg)
2917   "Read network news.
2918 If ARG is a positive number, Gnus will use that as the
2919 startup level. If ARG is nil, Gnus will be started at level 2. 
2920 If ARG is non-nil and not a positive number, Gnus will
2921 prompt the user for the name of an NNTP server to use.
2922 As opposed to `gnus', this command will not connect to the local server."
2923   (interactive "P")
2924   (gnus (or arg (1- gnus-level-default-subscribed)) t))
2925
2926 (defalias '\(ding\) 'gnus)
2927
2928 ;;;###autoload
2929 (defun gnus (&optional arg dont-connect)
2930   "Read network news.
2931 If ARG is non-nil and a positive number, Gnus will use that as the
2932 startup level. If ARG is non-nil and not a positive number, Gnus will
2933 prompt the user for the name of an NNTP server to use."
2934   (interactive "P")
2935   (if (get-buffer gnus-group-buffer)
2936       (progn
2937         (switch-to-buffer gnus-group-buffer)
2938         (gnus-group-get-new-news))
2939     (gnus-clear-system)
2940     (gnus-read-init-file)
2941     (let ((level (and arg (numberp arg) (> arg 0) arg))
2942           did-connect)
2943       (unwind-protect
2944           (progn
2945             (switch-to-buffer gnus-group-buffer)
2946             (gnus-add-current-to-buffer-list)
2947             (gnus-group-mode)
2948             (or dont-connect 
2949                 (setq did-connect
2950                       (gnus-start-news-server (and arg (not level))))))
2951         (if (and (not dont-connect) 
2952                  (not did-connect))
2953             (gnus-group-quit)
2954           (run-hooks 'gnus-startup-hook)
2955           ;; NNTP server is successfully open. 
2956           (gnus-update-format-specifications)
2957           (let ((buffer-read-only nil))
2958             (erase-buffer)
2959             (if (not gnus-inhibit-startup-message)
2960                 (progn
2961                   (gnus-group-startup-message)
2962                   (sit-for 0))))
2963           (gnus-setup-news nil level)
2964           (and gnus-use-dribble-file (gnus-dribble-open))
2965           (or t (not gnus-novice-user)
2966               gnus-expert-user
2967               (gnus-group-describe-briefly)) ;Show brief help message.
2968           (gnus-group-list-groups level))))))
2969
2970 (defun gnus-group-startup-message (&optional x y)
2971   "Insert startup message in current buffer."
2972   ;; Insert the message.
2973   (erase-buffer)
2974   (insert
2975    (format "
2976     %s
2977            A newsreader 
2978       for GNU Emacs
2979
2980         Based on GNUS 
2981              written by 
2982      Masanobu UMEDA
2983
2984     Lars Magne 
2985          Ingebrigtsen 
2986       larsi@ifi.uio.no
2987
2988            gnus-version))
2989   ;; And then hack it.
2990   ;; 18 is the longest line.
2991   (indent-rigidly (point-min) (point-max) 
2992                   (/ (max (- (window-width) (or x 28)) 0) 2))
2993   (goto-char (point-min))
2994   ;; +4 is fuzzy factor.
2995   (insert-char ?\n (/ (max (- (window-height) (or y 12)) 0) 2)))
2996
2997 (defun gnus-group-setup-buffer ()
2998   (or (get-buffer gnus-group-buffer)
2999       (progn
3000         (switch-to-buffer gnus-group-buffer)
3001         (gnus-add-current-to-buffer-list)
3002         (gnus-group-mode))))
3003
3004 (defun gnus-group-list-groups (level &optional unread)
3005   "List newsgroups with level LEVEL or lower that have unread alticles.
3006 Default is all subscribed groups.
3007 If argument UNREAD is non-nil, groups with no unread articles are also listed."
3008   (interactive (list (and current-prefix-arg
3009                           (prefix-numeric-value current-prefix-arg))))
3010   (setq level (or level gnus-group-default-list-level gnus-level-subscribed))
3011   (gnus-group-setup-buffer)     ;May call from out of group buffer
3012   (let ((case-fold-search nil)
3013         (group (gnus-group-group-name)))
3014     (funcall gnus-group-prepare-function level unread nil)
3015     (if (zerop (buffer-size))
3016         (message gnus-no-groups-message)
3017       (goto-char (point-min))
3018       (if (not group)
3019           ;; Go to the first group with unread articles.
3020           (gnus-group-search-forward nil nil nil t)
3021         ;; Find the right group to put point on. If the current group
3022         ;; has disapeared in the new listing, try to find the next
3023         ;; one. If no next one can be found, just leave point at the
3024         ;; first newsgroup in the buffer.
3025         (if (not (gnus-goto-char
3026                   (text-property-any (point-min) (point-max) 
3027                                      'gnus-group (intern group))))
3028             (let ((newsrc (nthcdr 3 (gnus-gethash group gnus-newsrc-hashtb))))
3029               (while (and newsrc
3030                           (not (gnus-goto-char 
3031                                 (text-property-any 
3032                                  (point-min) (point-max) 'gnus-group 
3033                                  (intern (car (car newsrc)))))))
3034                 (setq newsrc (cdr newsrc)))
3035               (or newsrc (progn (goto-char (point-max))
3036                                 (forward-line -1))))))
3037       ;; Adjust cursor point.
3038       (gnus-group-position-cursor))))
3039
3040 (defun gnus-group-prepare-flat (level &optional all lowest regexp) 
3041   "List all newsgroups with unread articles of level LEVEL or lower.
3042 If ALL is non-nil, list groups that have no unread articles.
3043 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher.
3044 If REGEXP, only list groups matching REGEXP."
3045   (set-buffer gnus-group-buffer)
3046   (let ((buffer-read-only nil)
3047         (newsrc (cdr gnus-newsrc-alist))
3048         (lowest (or lowest 1))
3049         info clevel unread group)
3050     (erase-buffer)
3051     (if (< lowest gnus-level-zombie)
3052         ;; List living groups.
3053         (while newsrc
3054           (setq info (car newsrc)
3055                 group (car info)
3056                 newsrc (cdr newsrc)
3057                 unread (car (gnus-gethash group gnus-newsrc-hashtb)))
3058           (and unread ; This group might be bogus
3059                (or (not regexp)
3060                    (string-match regexp group))
3061                (<= (setq clevel (car (cdr info))) level) 
3062                (>= clevel lowest)
3063                (or all            ; We list all groups?
3064                    (eq unread t)  ; We list unactivated groups
3065                    (> unread 0)   ; We list groups with unread articles
3066                    (cdr (assq 'tick (nth 3 info)))) ; And groups with tickeds
3067                (gnus-group-insert-group-line 
3068                 nil group (car (cdr info)) (nth 3 info) unread (nth 4 info)))))
3069
3070     ;; List dead groups.
3071     (and (>= level gnus-level-zombie) (<= lowest gnus-level-zombie)
3072          (gnus-group-prepare-flat-list-dead 
3073           (setq gnus-zombie-list (sort gnus-zombie-list 'string<)) 
3074           gnus-level-zombie ?Z
3075           regexp))
3076     (and (>= level gnus-level-killed) (<= lowest gnus-level-killed)
3077          (gnus-group-prepare-flat-list-dead 
3078           (setq gnus-killed-list (sort gnus-killed-list 'string<)) 
3079           gnus-level-killed ?K regexp))
3080
3081     (gnus-group-set-mode-line)
3082     (setq gnus-have-all-newsgroups all)
3083     (run-hooks 'gnus-group-prepare-hook)))
3084
3085 (defun gnus-group-prepare-flat-list-dead (groups level mark regexp)
3086   ;; List zombies and killed lists somehwat faster, which was
3087   ;; suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>. It does
3088   ;; this by ignoring the group format specification altogether.
3089   (let (group beg)
3090     (while groups
3091       (setq group (car groups)
3092             groups (cdr groups))
3093       (if (or (not regexp)
3094               (string-match regexp group))
3095           (progn
3096             (setq beg (point))
3097             (insert (format " %c    *: %s\n" mark group))
3098             (add-text-properties 
3099              beg (1+ beg) 
3100              (list 'gnus-group (intern group)
3101                    'gnus-unread t
3102                    'gnus-level level)))))))
3103
3104 (defun gnus-group-real-name (group)
3105   "Find the real name of a foreign newsgroup."
3106   (if (string-match "^[^:]+:" group)
3107       (substring group (match-end 0))
3108     group))
3109
3110 (defun gnus-group-prefixed-name (group method)
3111   "Return the whole name from GROUP and METHOD."
3112   (and (stringp method) (setq method (gnus-server-to-method method)))
3113   (concat (format "%s" (car method))
3114           (if (and 
3115                (assoc (format "%s" (car method)) (gnus-methods-using 'address))
3116                (not (string= (nth 1 method) "")))
3117               (concat "+" (nth 1 method)))
3118           ":" group))
3119
3120 (defun gnus-group-real-prefix (group)
3121   "Return the prefix of the current group name."
3122   (if (string-match "^[^:]+:" group)
3123       (substring group 0 (match-end 0))
3124     ""))
3125
3126 (defun gnus-group-method-name (group)
3127   "Return the method used for selecting GROUP."
3128   (let ((prefix (gnus-group-real-prefix group)))
3129     (if (equal prefix "")
3130         gnus-select-method
3131       (if (string-match "^[^\\+]+\\+" prefix)
3132           (list (intern (substring prefix 0 (1- (match-end 0))))
3133                 (substring prefix (match-end 0) (1- (length prefix))))
3134         (list (intern (substring prefix 0 (1- (length prefix)))) "")))))
3135
3136 (defun gnus-group-foreign-p (group)
3137   "Return nil if GROUP is native, non-nil if it is foreign."
3138   (string-match ":" group))
3139
3140 (defun gnus-group-set-info (info &optional method-only-group part)
3141   (let* ((entry (gnus-gethash
3142                  (or method-only-group (car info)) gnus-newsrc-hashtb))
3143          (part-info info)
3144          (info (if method-only-group (nth 2 entry) info)))
3145     (if (not method-only-group)
3146         ()
3147       (or entry
3148           (error "Trying to change non-existant group %s" method-only-group))
3149       ;; We have recevied parts of the actual group info - either the
3150       ;; select method or the group parameters.  We first check
3151       ;; whether we have to extend the info, and if so, do that.
3152       (let ((len (length info))
3153             (total (if (eq part 'method) 5 6)))
3154         (and (< len total)
3155              (setcdr (nthcdr (1- len) info)
3156                      (make-list (- total len) nil)))
3157         ;; Then we enter the new info.
3158         (setcar (nthcdr (1- total) info) part-info)))
3159     ;; We uncompress some lists of marked articles.
3160     (let (marked)
3161       (if (not (setq marked (nth 3 info)))
3162           ()
3163         (while marked
3164           (or (eq 'score (car (car marked)))
3165               (eq 'bookmark (car (car marked)))
3166               (eq 'killed (car (car marked)))
3167               (setcdr (car marked) 
3168                       (gnus-uncompress-range (cdr (car marked)))))
3169           (setq marked (cdr marked)))))
3170     (if entry
3171         ()
3172       ;; This is a new group, so we just create it.
3173       (save-excursion
3174         (set-buffer gnus-group-buffer)
3175         (if (nth 4 info)
3176             ;; It's a foreign group...
3177             (gnus-group-make-group 
3178              (gnus-group-real-name (car info))
3179              (prin1-to-string (car (nth 4 info)))
3180              (nth 1 (nth 4 info)))
3181           ;; It's a native group.
3182           (gnus-group-make-group
3183            (car info)
3184            (prin1-to-string (car gnus-select-method))
3185            (nth 1 gnus-select-method)))
3186         (message "Note: New group created")
3187         (setq entry 
3188               (gnus-gethash (gnus-group-prefixed-name 
3189                              (gnus-group-real-name (car info))
3190                              (or (nth 4 info) gnus-select-method))
3191                             gnus-newsrc-hashtb))))
3192     ;; Whether it was a new group or not, we now have the entry, so we
3193     ;; can do the update.
3194     (if entry
3195         (progn
3196           (setcar (nthcdr 2 entry) info)
3197           (if (and (not (eq (car entry) t)) 
3198                    (gnus-gethash (car info) gnus-active-hashtb))
3199               (let ((marked (nth 3 info)))
3200                 (setcar entry 
3201                         (max 0 (- (length (gnus-list-of-unread-articles 
3202                                            (car info)))
3203                                   (length (cdr (assq 'tick marked)))
3204                                   (length (cdr (assq 'dormant marked)))))))))
3205       (error "No such group: %s" (car info)))))
3206
3207 (defun gnus-group-set-method-info (group select-method)
3208   (gnus-group-set-info select-method group 'method))
3209
3210 (defun gnus-group-set-params-info (group params)
3211   (gnus-group-set-info params group 'params))
3212
3213 (defun gnus-group-update-group-line ()
3214   "This function updates the current line in the newsgroup buffer and
3215 moves the point to the colon."
3216   (let* ((buffer-read-only nil)
3217          (group (gnus-group-group-name))
3218          (entry (and group (gnus-gethash group gnus-newsrc-hashtb))))
3219     (if entry
3220         (gnus-dribble-enter 
3221          (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
3222                  ")")))
3223     (beginning-of-line)
3224     (delete-region (point) (progn (forward-line 1) (point)))
3225     (gnus-group-insert-group-line-info group)
3226     (forward-line -1)
3227     (gnus-group-position-cursor)))
3228
3229 (defun gnus-group-insert-group-line-info (group)
3230   (let ((entry (gnus-gethash group gnus-newsrc-hashtb)) 
3231         active info)
3232     (if entry
3233         (progn
3234           (setq info (nth 2 entry))
3235           (gnus-group-insert-group-line 
3236            nil group (nth 1 info) (nth 3 info) (car entry) (nth 4 info)))
3237       (setq active (gnus-gethash group gnus-active-hashtb))
3238       (gnus-group-insert-group-line 
3239        nil group (if (member group gnus-zombie-list) gnus-level-zombie
3240                    gnus-level-killed)
3241        nil (if active (- (1+ (cdr active)) (car active)) 0) nil))))
3242
3243 (defun gnus-group-insert-group-line (gformat group level marked number method)
3244   (let* ((gformat (or gformat gnus-group-line-format-spec))
3245          (active (gnus-gethash group gnus-active-hashtb))
3246          (number-total (if active (1+ (- (cdr active) (car active))) 0))
3247          (number-of-dormant (length (cdr (assq 'dormant marked))))
3248          (number-of-ticked (length (cdr (assq 'tick marked))))
3249          (number-of-ticked-and-dormant
3250           (+ number-of-ticked number-of-dormant))
3251          (number-of-unread-unticked 
3252           (if (numberp number) (int-to-string (max 0 number))
3253             "*"))
3254          (number-of-read
3255           (if (numberp number)
3256               (max 0 (- number-total number))
3257             "*"))
3258          (subscribed (cond ((<= level gnus-level-subscribed) ? )
3259                            ((<= level gnus-level-unsubscribed) ?U)
3260                            ((= level gnus-level-zombie) ?Z)
3261                            (t ?K)))
3262          (qualified-group (gnus-group-real-name group))
3263          (newsgroup-description 
3264           (if gnus-description-hashtb
3265               (or (gnus-gethash group gnus-description-hashtb) "")
3266             ""))
3267          (moderated (if (member group gnus-moderated-list) ?m ? ))
3268          (moderated-string (if (eq moderated ?m) "(m)" ""))
3269          (method (gnus-server-get-method group method))
3270          (news-server (or (car (cdr method)) ""))
3271          (news-method (or (car method) ""))
3272          (news-method-string 
3273           (if method (format "(%s:%s)" (car method) (car (cdr method))) ""))
3274          (marked (if (and 
3275                       (numberp number) 
3276                       (zerop number)
3277                       (> number-of-ticked 0))
3278                      ?* ? ))
3279          (number (if (eq number t) "*" (+ number number-of-dormant 
3280                                           number-of-ticked)))
3281          (buffer-read-only nil)
3282          b)
3283     (beginning-of-line)
3284     (setq b (point))
3285     ;; Insert the text.
3286     (insert (eval gformat))
3287
3288     (add-text-properties 
3289      b (1+ b) (list 'gnus-group (intern group)
3290                     'gnus-unread (if (numberp number)
3291                                      (string-to-int number-of-unread-unticked)
3292                                    t)
3293                     'gnus-marked marked
3294                     'gnus-level level))))
3295
3296 (defun gnus-group-update-group (group &optional visible-only)
3297   "Update newsgroup info of GROUP.
3298 If VISIBLE-ONLY is non-nil, the group won't be displayed if it isn't already."
3299   (save-excursion
3300     (set-buffer gnus-group-buffer)
3301     (let ((buffer-read-only nil)
3302           visible)
3303       (let ((entry (gnus-gethash group gnus-newsrc-hashtb)))
3304         (if entry
3305             (gnus-dribble-enter 
3306              (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
3307                      ")"))))
3308       ;; Buffer may be narrowed.
3309       (save-restriction
3310         (widen)
3311         ;; Search a line to modify.  If the buffer is large, the search
3312         ;; takes long time.  In most cases, current point is on the line
3313         ;; we are looking for.  So, first of all, check current line. 
3314         (if (or (progn
3315                   (beginning-of-line)
3316                   (eq (get-text-property (point) 'gnus-group)
3317                       (intern group)))
3318                 (progn
3319                   (gnus-goto-char 
3320                    (text-property-any 
3321                     (point-min) (point-max) 'gnus-group (intern group)))))
3322             ;; GROUP is listed in current buffer. So, delete old line.
3323             (progn
3324               (setq visible t)
3325               (beginning-of-line)
3326               (delete-region (point) (progn (forward-line 1) (point))))
3327           ;; No such line in the buffer, find out where it's supposed to
3328           ;; go, and insert it there (or at the end of the buffer).
3329           ;; Fix by Per Abrahamsen <amanda@iesd.auc.dk>.
3330           (or visible-only
3331               (let ((entry (cdr (gnus-gethash group gnus-newsrc-hashtb))))
3332                 (while (and entry
3333                             (car entry)
3334                             (not
3335                              (gnus-goto-char
3336                               (text-property-any
3337                                (point-min) (point-max) 
3338                                'gnus-group (intern (car (car entry)))))))
3339                   (setq entry (cdr entry)))
3340                 (or entry (goto-char (point-max)))))))
3341       (if (or visible (not visible-only))
3342           (gnus-group-insert-group-line-info group))
3343       (gnus-group-set-mode-line))))
3344
3345 (defun gnus-group-set-mode-line ()
3346   (if (memq 'group gnus-updated-mode-lines)
3347       (let* ((gformat (or gnus-group-mode-line-format-spec
3348                           (setq gnus-group-mode-line-format-spec
3349                                 (gnus-parse-format 
3350                                  gnus-group-mode-line-format 
3351                                  gnus-group-mode-line-format-alist))))
3352              (news-server (car (cdr gnus-select-method)))
3353              (news-method (car gnus-select-method))
3354              (mode-string (eval gformat))
3355              (max-len 60))
3356         (if (> (length mode-string) max-len) 
3357             (setq mode-string (substring mode-string 0 (- max-len 4))))
3358         (setq mode-line-buffer-identification mode-string)
3359         (set-buffer-modified-p t))))
3360
3361 (defun gnus-group-group-name ()
3362   "Get the name of the newsgroup on the current line."
3363   (let ((group (get-text-property (gnus-point-at-bol) 'gnus-group)))
3364     (and group (symbol-name group))))
3365
3366 (defun gnus-group-group-level ()
3367   "Get the level of the newsgroup on the current line."
3368   (get-text-property (gnus-point-at-bol) 'gnus-level))
3369
3370 (defun gnus-group-search-forward (&optional backward all level first-too)
3371   "Find the next newsgroup with unread articles.
3372 If BACKWARD is non-nil, find the previous newsgroup instead.
3373 If ALL is non-nil, just find any newsgroup.
3374 If LEVEL is non-nil, find group with level LEVEL, or higher if no such
3375 group exists.
3376 If FIRST-TOO, the current line is also eligeble as a target."
3377   (let ((way (if backward -1 1))
3378         (low 10)
3379         (beg (point))
3380         pos found)
3381     (or first-too (forward-line way))
3382     (while (and 
3383             (not (eobp))
3384             (not (setq 
3385                   found 
3386                   (and (or all
3387                            (and
3388                             (let ((unread 
3389                                    (get-text-property (point) 'gnus-unread)))
3390                               (or (eq unread t) (and unread (> unread 0))))
3391                             (let ((lev (get-text-property
3392                                         (point) 'gnus-level)))
3393                               (and lev (<= (get-text-property 
3394                                            (point) 'gnus-level)
3395                                            gnus-level-subscribed)))))
3396                        (or (not level)
3397                            (let ((lev (get-text-property (point) 'gnus-level)))
3398                              (if (and lev (<= lev level))
3399                                  t
3400                                (if (< lev low)
3401                                    (progn
3402                                      (setq low lev)
3403                                      (setq pos (point))))
3404                                nil))))))
3405             (zerop (forward-line way))))
3406     (if found 
3407         (progn (gnus-group-position-cursor) t)
3408       (if pos (goto-char pos) (goto-char beg))
3409       nil)))
3410
3411 ;; Gnus group mode commands
3412
3413 (defun gnus-group-read-group (all &optional no-article group)
3414   "Read news in this newsgroup.
3415 If argument ALL is non-nil, already read articles become readable.
3416 If optional argument NO-ARTICLE is non-nil, no article body is displayed."
3417   (interactive "P")
3418   (let ((group (or group (gnus-group-group-name)))
3419         number active marked entry)
3420     (or group (error "No group on current line"))
3421     (setq marked 
3422           (nth 3 (nth 2 (setq entry (gnus-gethash group gnus-newsrc-hashtb)))))
3423     ;; This group might be a dead group. In that case we have to get
3424     ;; the number of unread articles from `gnus-active-hashtb'.
3425     (if entry
3426         (setq number (car entry))
3427       (if (setq active (gnus-gethash group gnus-active-hashtb))
3428           (setq number (- (1+ (cdr active)) (car active)))))
3429     (gnus-summary-read-group 
3430      group (or all (and (numberp number) 
3431                         (zerop (+ number (length (cdr (assq 'tick marked)))
3432                                   (length (cdr (assq 'dormant marked)))))))
3433      no-article)))
3434
3435 (defun gnus-group-select-group (all)
3436   "Select this newsgroup.
3437 No article is selected automatically.
3438 If argument ALL is non-nil, already read articles become readable."
3439   (interactive "P")
3440   (gnus-group-read-group all t))
3441
3442 (defun gnus-group-jump-to-group (group)
3443   "Jump to newsgroup GROUP."
3444   (interactive (list (completing-read "Group: " gnus-active-hashtb nil
3445                                       (not (not gnus-read-active-file)))))
3446
3447   (if (equal group "")
3448       (error "empty group name"))
3449
3450   (let ((b (text-property-any (point-min) (point-max) 
3451                               'gnus-group (intern group))))
3452     (if b
3453         ;; Either go to the line in the group buffer...
3454         (goto-char b)
3455       ;; ... or insert the line.
3456       (or
3457        (gnus-gethash group gnus-active-hashtb)
3458        (gnus-activate-newsgroup group)
3459        (error "%s error: %s" group (gnus-status-message group)))
3460
3461       (gnus-group-update-group group)
3462       (goto-char (text-property-any (point-min) (point-max) 
3463                                     'gnus-group (intern group)))))
3464   ;; Adjust cursor point.
3465   (gnus-group-position-cursor))
3466
3467 (defun gnus-group-next-group (n)
3468   "Go to next N'th newsgroup.
3469 If N is negative, search backward instead.
3470 Returns the difference between N and the number of skips actually
3471 done."
3472   (interactive "p")
3473   (gnus-group-next-unread-group n t))
3474
3475 (defun gnus-group-next-unread-group (n &optional all level)
3476   "Go to next N'th unread newsgroup.
3477 If N is negative, search backward instead.
3478 If ALL is non-nil, choose any newsgroup, unread or not.
3479 If LEVEL is non-nil, choose the next group with level LEVEL, or, if no
3480 such group can be found, the next group with a level higher than
3481 LEVEL.
3482 Returns the difference between N and the number of skips actually
3483 made."
3484   (interactive "p")
3485   (let ((backward (< n 0))
3486         (n (abs n)))
3487     (while (and (> n 0)
3488                 (gnus-group-search-forward backward all level))
3489       (setq n (1- n)))
3490     (if (/= 0 n) (message "No more%s newsgroups%s" (if all "" " unread")
3491                           (if level " on this level or higher" "")))
3492     n))
3493
3494 (defun gnus-group-prev-group (n)
3495   "Go to previous N'th newsgroup.
3496 Returns the difference between N and the number of skips actually
3497 done."
3498   (interactive "p")
3499   (gnus-group-next-unread-group (- n) t))
3500
3501 (defun gnus-group-prev-unread-group (n)
3502   "Go to previous N'th unread newsgroup.
3503 Returns the difference between N and the number of skips actually
3504 done."  
3505   (interactive "p")
3506   (gnus-group-next-unread-group (- n)))
3507
3508 (defun gnus-group-next-unread-group-same-level (n)
3509   "Go to next N'th unread newsgroup on the same level.
3510 If N is negative, search backward instead.
3511 Returns the difference between N and the number of skips actually
3512 done."
3513   (interactive "p")
3514   (gnus-group-next-unread-group n t (gnus-group-group-level))
3515   (gnus-group-position-cursor))
3516
3517 (defun gnus-group-prev-unread-group-same-level (n)
3518   "Go to next N'th unread newsgroup on the same level.
3519 Returns the difference between N and the number of skips actually
3520 done."
3521   (interactive "p")
3522   (gnus-group-next-unread-group (- n) t (gnus-group-group-level))
3523   (gnus-group-position-cursor))
3524
3525 (defun gnus-group-best-unread-group (&optional exclude-group)
3526   "Go to the group with the highest level.
3527 If EXCLUDE-GROUP, do not go to that group."
3528   (interactive)
3529   (goto-char (point-min))
3530   (let ((best 10)
3531         unread best-point)
3532     (while (setq unread (get-text-property (point) 'gnus-unread))
3533       (if (and (numberp unread) (> unread 0))
3534           (progn
3535             (or best-point (setq best-point (point)))
3536             (if (and (< (get-text-property (point) 'gnus-level) best)
3537                      (or (not exclude-group)
3538                          (not (equal exclude-group (gnus-group-group-name)))))
3539                 (progn 
3540                   (setq best (get-text-property (point) 'gnus-level))
3541                   (setq best-point (point))))))
3542       (forward-line 1))
3543     (if best-point (goto-char best-point))
3544     (gnus-summary-position-cursor)
3545     (and best-point (gnus-group-group-name))))
3546
3547 (defun gnus-group-first-unread-group ()
3548   "Go to the first group with unread articles."
3549   (interactive)
3550   (goto-char (point-min))
3551   (or (get-text-property (point) 'gnus-unread)
3552       (gnus-group-next-unread-group 1))
3553   (gnus-group-position-cursor))
3554
3555 (defun gnus-group-enter-server-mode ()
3556   "Jump to the server buffer."
3557   (interactive)
3558   (gnus-server-setup-buffer)
3559   (switch-to-buffer gnus-server-buffer)
3560   (gnus-server-prepare))
3561
3562 (defun gnus-group-make-group (name method address)
3563   "Add a new newsgroup.
3564 The user will be prompted for a NAME, for a select METHOD, and an
3565 ADDRESS."
3566   (interactive
3567    (cons 
3568     (read-string "Group name: ")
3569     (let ((method
3570            (completing-read 
3571             "Method: " (append gnus-valid-select-methods gnus-server-alist)
3572             nil t)))
3573       (if (assoc method gnus-valid-select-methods)
3574           (list method
3575                 (if (memq 'prompt-address
3576                           (assoc method gnus-valid-select-methods))
3577                     (read-string "Address: ")
3578                   ""))
3579         (list method nil)))))
3580   
3581   (let* ((meth (if address (list (intern method) address) method))
3582          (nname (gnus-group-prefixed-name name meth))
3583          info)
3584     (and (gnus-gethash nname gnus-newsrc-hashtb)
3585          (error "Group %s already exists" nname))
3586     (gnus-group-change-level 
3587      (setq info (list t nname gnus-level-default-subscribed nil nil meth))
3588      gnus-level-default-subscribed gnus-level-killed 
3589      (gnus-gethash (or (gnus-group-group-name) "dummy.group")
3590                        gnus-newsrc-hashtb) t)
3591     (gnus-sethash nname '(0 . 0) gnus-active-hashtb)
3592     (gnus-dribble-enter 
3593      (concat "(gnus-group-set-info '" (prin1-to-string (cdr info)) ")"))
3594     (gnus-group-insert-group-line-info nname)
3595
3596     (and (gnus-check-backend-function 'request-create-group nname)
3597          (gnus-request-create-group nname))))
3598
3599 (defun gnus-group-edit-group (group &optional part)
3600   "Edit the group on the current line."
3601   (interactive (list (gnus-group-group-name)))
3602   (let ((done-func '(lambda () 
3603                       "Exit editing mode and update the information."
3604                       (interactive)
3605                       (gnus-group-edit-group-done 'part 'group)))
3606         (part (or part 'info))
3607         info)
3608     (if group (setq info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
3609       (error "No group on current line"))
3610     (setq gnus-winconf-edit-group (current-window-configuration))
3611     (pop-to-buffer gnus-group-edit-buffer)
3612     (gnus-add-current-to-buffer-list)
3613     (emacs-lisp-mode)
3614     ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3615     (use-local-map (copy-keymap emacs-lisp-mode-map))
3616     (local-set-key "\C-c\C-c" done-func)
3617     ;; We modify the func to let it know what part it is editing.
3618     (setcar (cdr (nth 4 done-func)) (list 'quote part))
3619     (setcar (cdr (cdr (nth 4 done-func))) group)
3620     (erase-buffer)
3621     (insert
3622      (cond 
3623       ((eq part 'method)
3624        ";; Type `C-c C-c' after editing the select method.\n\n")
3625       ((eq part 'params)
3626        ";; Type `C-c C-c' after editing the group parameters.\n\n")
3627       ((eq part 'info)
3628        ";; Type `C-c C-c' after editing the group info.\n\n")))
3629     (let ((cinfo (gnus-copy-sequence info))
3630           marked)
3631       (if (not (setq marked (nth 3 cinfo)))
3632           ()
3633         (while marked
3634           (or (eq 'score (car (car marked)))
3635               (eq 'bookmark (car (car marked)))
3636               (eq 'killed (car (car marked)))
3637               (setcdr (car marked) 
3638                       (gnus-compress-sequence (sort (cdr (car marked)) '<) t)))
3639           (setq marked (cdr marked))))
3640       (insert 
3641        (pp-to-string
3642         (cond ((eq part 'method)
3643                (or (nth 4 info) "native"))
3644               ((eq part 'params)
3645                (nth 5 info))
3646               (t
3647                cinfo)))
3648        "\n"))))
3649
3650 (defun gnus-group-edit-group-method (group)
3651   "Edit the select method of GROUP."
3652   (interactive (list (gnus-group-group-name)))
3653   (gnus-group-edit-group group 'method))
3654
3655 (defun gnus-group-edit-group-parameters (group)
3656   "Edit the group parameters of GROUP."
3657   (interactive (list (gnus-group-group-name)))
3658   (gnus-group-edit-group group 'params))
3659
3660 (defun gnus-group-edit-group-done (part group)
3661   "Get info from buffer, update variables and jump to the group buffer."
3662   (set-buffer (get-buffer-create gnus-group-edit-buffer))
3663   (goto-char (point-min))
3664   (let ((form (read (current-buffer))))
3665     (if (eq part 'info) 
3666         (gnus-group-set-info form)
3667       (gnus-group-set-info form group part))
3668     (kill-buffer (current-buffer))
3669     (and gnus-winconf-edit-group
3670          (set-window-configuration gnus-winconf-edit-group))
3671     (setq gnus-winconf-edit-group nil)
3672     (set-buffer gnus-group-buffer)
3673     (gnus-group-update-group (gnus-group-group-name))
3674     (gnus-group-position-cursor)))
3675
3676 (defun gnus-group-make-help-group ()
3677   "Create the (ding) Gnus documentation group."
3678   (interactive)
3679   (and (gnus-gethash (gnus-group-prefixed-name "gnus-help" '(nndoc ""))
3680                      gnus-newsrc-hashtb)
3681        (error "Documentation group already exists"))
3682   (let ((path load-path))
3683     (while (and path
3684                 (not (file-exists-p (concat (file-name-as-directory (car path))
3685                                             "doc.txt"))))
3686       (setq path (cdr path)))
3687     (or path (error "Couldn't find doc group"))
3688     (gnus-group-make-group 
3689      "gnus-help" "nndoc" 
3690      (concat (file-name-as-directory (car path)) "doc.txt"))
3691     (gnus-group-position-cursor)))
3692
3693 (defun gnus-group-make-archive-group ()
3694   "Create the (ding) Gnus archive group."
3695   (interactive)
3696   (and (gnus-gethash (gnus-group-prefixed-name "ding.archives" '(nndir ""))
3697                      gnus-newsrc-hashtb)
3698        (error "Archive group already exists"))
3699   (gnus-group-make-group "ding.archives" "nndir" gnus-group-archive-directory)
3700   (gnus-group-position-cursor))
3701
3702 (defun gnus-group-make-directory-group (dir)
3703   "Create an nndir group.
3704 The user will be prompted for a directory. The contents of this
3705 directory will be used as a newsgroup. The directory should contain
3706 mail messages or news articles in files that have numeric names."
3707   (interactive
3708    (list (read-file-name "Create group from directory: ")))
3709   (or (file-exists-p dir) (error "No such directory"))
3710   (or (file-directory-p dir) (error "Not a directory"))
3711   (gnus-group-make-group dir "nndir" dir)
3712   (gnus-group-position-cursor))
3713
3714 (defun gnus-group-make-kiboze-group (group address scores)
3715   "Create an nnkiboze group.
3716 The user will be prompted for a name, a regexp to match groups, and
3717 score file entries for articles to include in the group."
3718   (interactive
3719    (list
3720     (read-string "nnkiboze group name: ")
3721     (read-string "Source groups (regexp): ")
3722     (let ((headers (mapcar (lambda (group) (list group))
3723                            '("subject" "from" "number" "date" "message-id"
3724                              "references" "chars" "lines" "xref")))
3725           scores header regexp regexps)
3726       (while (not (equal "" (setq header (completing-read 
3727                                           "Match on header: " headers nil t))))
3728         (setq regexps nil)
3729         (while (not (equal "" (setq regexp (read-string 
3730                                             (format "Match on %s (string): "
3731                                                     header)))))
3732           (setq regexps (cons (list regexp nil 1000 nil) regexps)))
3733         (setq scores (cons (cons header regexps) scores)))
3734       (car scores))))
3735   (gnus-group-make-group group "nnkiboze" address)
3736   (save-excursion
3737     (gnus-set-work-buffer)
3738     (let (emacs-lisp-mode-hook)
3739       (pp (list 'setq 'gnus-score-alist 
3740                 (list 'quote (list scores)))
3741           (current-buffer)))
3742     (write-region (point-min) (point-max) 
3743                   (concat (or gnus-kill-files-directory "~/News")
3744                           "nnkiboze:" group "." gnus-score-file-suffix)))
3745   (gnus-group-position-cursor))
3746
3747 ;; Group sorting commands
3748 ;; Suggested by Joe Hildebrand <hildjj@idaho.fuentez.com>.
3749
3750 (defun gnus-group-sort-groups ()
3751   "Sort the group buffer using `gnus-group-sort-function'."
3752   (interactive)
3753   (setq gnus-newsrc-alist 
3754         (sort (cdr gnus-newsrc-alist) gnus-group-sort-function))
3755   (gnus-make-hashtable-from-newsrc-alist)
3756   (gnus-get-unread-articles (1+ gnus-level-subscribed))
3757   (gnus-group-list-groups nil))
3758
3759 (defun gnus-group-sort-by-alphabet (info1 info2)
3760   (string< (car info1) (car info2)))
3761
3762 (defun gnus-group-sort-by-unread (info1 info2)
3763   (let ((n1 (car (gnus-gethash (car info1) gnus-newsrc-hashtb)))
3764         (n2 (car (gnus-gethash (car info2) gnus-newsrc-hashtb))))
3765     (< (or (and (numberp n1) n1) 0)
3766        (or (and (numberp n2) n2) 0))))
3767
3768 (defun gnus-group-sort-by-level (info1 info2)
3769   (< (nth 1 info1) (nth 1 info2)))
3770
3771 ;; Group catching up.
3772
3773 (defun gnus-group-catchup-current (n &optional all)
3774   "Mark all articles not marked as unread in current newsgroup as read.
3775 If prefix argument N is numeric, the ARG next newsgroups will be
3776 caught up. If ALL is non-nil, marked articles will also be marked as
3777 read. Cross references (Xref: header) of articles are ignored.
3778 The difference between N and actual number of newsgroups that were
3779 caught up is returned."
3780   (interactive "p")
3781   (if (or (not gnus-interactive-catchup) ;Without confirmation?
3782           gnus-expert-user
3783           (gnus-y-or-n-p
3784            (if all
3785                "Do you really want to mark all articles as read? "
3786              "Mark all unread articles as read? ")))
3787       (progn
3788         (while 
3789             (and (> n 0)
3790                  (progn
3791                    (setq n (1- n))
3792                    (gnus-group-catchup (gnus-group-group-name) all)
3793                    (gnus-group-update-group-line)
3794                    t)
3795                  (zerop (gnus-group-next-unread-group 1))))))
3796   n)
3797
3798 (defun gnus-group-catchup-current-all (n)
3799   "Mark all articles in current newsgroup as read.
3800 Cross references (Xref: header) of articles are ignored."
3801   (interactive "p")
3802   (gnus-group-catchup-current n 'all))
3803
3804 (defun gnus-group-catchup (group &optional all)
3805   "Mark all articles in GROUP as read.
3806 If ALL is non-nil, all articles are marked as read.
3807 The return value is the number of articles that were marked as read,
3808 or nil if no action could be taken."
3809   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
3810          (num (car entry))
3811          (marked (nth 3 (nth 2 entry)))
3812          ticked)
3813     (if (not (numberp (car entry)))
3814         (message "Can't catch up; non-active group")
3815       ;; Do the updating only if the newsgroup isn't killed
3816       (if entry
3817           (progn
3818             (setq ticked (if all nil (cdr (assq 'tick marked))))
3819             (gnus-update-read-articles group ticked nil ticked)
3820             (if (and all marked)
3821                 (setcar (nthcdr 3 (nth 2 entry)) 
3822                         (delq (assq 'dormant marked) marked))))))
3823     num))
3824
3825 (defun gnus-group-expire-articles (newsgroup)
3826   "Expire all expirable articles in the current newsgroup."
3827   (interactive (list (gnus-group-group-name)))
3828   (if (not newsgroup) (error "No current newsgroup"))
3829   (let ((expirable 
3830          (assq 'expire (nth 3 (nth 2 (gnus-gethash newsgroup 
3831                                                    gnus-newsrc-hashtb))))))
3832  (and expirable 
3833       (gnus-check-backend-function 'request-expire-articles newsgroup)
3834       (setcdr expirable
3835               (gnus-request-expire-articles (cdr expirable) newsgroup)))))
3836
3837 (defun gnus-group-expire-all-groups ()
3838   "Expire all expirable articles in all newsgroups."
3839   (interactive)
3840   (message "Expiring...")
3841   (let ((newsrc (cdr gnus-newsrc-alist)))
3842     (while newsrc
3843       (gnus-group-expire-articles (car (car newsrc)))
3844       (setq newsrc (cdr newsrc))))
3845   (message "Expiring...done"))
3846
3847 (defun gnus-group-set-current-level (n level)
3848   "Set the level of the next N groups to LEVEL."
3849   (interactive "p\nnLevel: ")
3850   (or (and (>= level 1) (<= level gnus-level-killed))
3851       (error "Illegal level: %d" level))
3852   (let (group)
3853     (while (and (> n 0)
3854                 (setq group (gnus-group-group-name)))
3855       (and (setq group (gnus-group-group-name))
3856            (message "Changed level of %s from %d to %d" 
3857                     group (gnus-group-group-level) level)
3858            (gnus-group-change-level group level
3859                                     (gnus-group-group-level))
3860            (gnus-group-update-group-line))
3861       (forward-line 1)
3862       (setq n (1- n))))
3863   (gnus-group-position-cursor)
3864   n)
3865
3866 (defun gnus-group-unsubscribe-current-group (arg)
3867   "Toggle subscribe from/to unsubscribe current group."
3868   (interactive "P")
3869   (let ((group (gnus-group-group-name)))
3870     (or group (error "No newsgroup on current line"))
3871     (or arg (setq arg (if (<= (gnus-group-group-level) gnus-level-subscribed)
3872                           gnus-level-default-unsubscribed
3873                         gnus-level-default-subscribed)))
3874     (gnus-group-unsubscribe-group group arg)
3875     (gnus-group-next-group 1)))
3876
3877 (defun gnus-group-unsubscribe-group (group &optional level)
3878   "Toggle subscribe from/to unsubscribe GROUP.
3879 New newsgroup is added to .newsrc automatically."
3880   (interactive
3881    (list (completing-read "Group: " gnus-active-hashtb nil 
3882                           gnus-have-read-active-file)))
3883   (let ((newsrc (gnus-gethash group gnus-newsrc-hashtb)))
3884     (cond (newsrc
3885            ;; Toggle subscription flag.
3886            (gnus-group-change-level 
3887             newsrc (if level level (if (<= (nth 1 (nth 2 newsrc)) 
3888                                            gnus-level-subscribed) 
3889                                        (1+ gnus-level-subscribed)
3890                                      gnus-level-default-subscribed)))
3891            (gnus-group-update-group group))
3892           ((and (stringp group)
3893                 (or (not gnus-have-read-active-file)
3894                     (gnus-gethash group gnus-active-hashtb)))
3895            ;; Add new newsgroup.
3896            (gnus-group-change-level 
3897             group 
3898             (if level level gnus-level-default-subscribed) 
3899             (or (and (member group gnus-zombie-list) 
3900                      gnus-level-zombie) 
3901                 gnus-level-killed)
3902             (or (and (gnus-group-group-name)
3903                      (gnus-gethash (gnus-group-group-name) gnus-newsrc-hashtb))
3904                 (gnus-gethash (car (car gnus-newsrc-alist)) 
3905                               gnus-newsrc-hashtb)))
3906            (gnus-group-update-group group))
3907           (t (error "No such newsgroup: %s" group)))
3908     (gnus-group-position-cursor)))
3909
3910 (defun gnus-group-transpose-groups (arg)
3911   "Exchange current newsgroup and previous newsgroup.
3912 With argument ARG, takes previous newsgroup and moves it past ARG newsgroup."
3913   (interactive "p")
3914   ;; BUG: last newsgroup and the last but one cannot be transposed
3915   ;; since gnus-group-search-forward does not move forward beyond the
3916   ;; last.  If we instead use forward-line, no problem, but I don't
3917   ;; want to use it for later extension.
3918   (while (> arg 0)
3919     (gnus-group-search-forward t t)
3920     (gnus-group-kill-group 1)
3921     (gnus-group-search-forward nil t)
3922     (gnus-group-yank-group)
3923     (gnus-group-search-forward nil t)
3924     (setq arg (1- arg))))
3925
3926 (defun gnus-group-kill-all-zombies ()
3927   "Kill all zombie newsgroups."
3928   (interactive)
3929   (setq gnus-killed-list (nconc gnus-zombie-list gnus-killed-list))
3930   (setq gnus-zombie-list nil)
3931   (funcall gnus-group-prepare-function gnus-level-subscribed nil nil)
3932   (goto-char (point-min))
3933   (gnus-group-position-cursor))
3934
3935 (defun gnus-group-kill-region (begin end)
3936   "Kill newsgroups in current region (excluding current point).
3937 The killed newsgroups can be yanked by using \\[gnus-group-yank-group]."
3938   (interactive "r")
3939   (let ((lines
3940          ;; Exclude a line where current point is on.
3941          (1-
3942           ;; Count lines.
3943           (save-excursion
3944             (count-lines
3945              (progn
3946                (goto-char begin)
3947                (beginning-of-line)
3948                (point))
3949              (progn
3950                (goto-char end)
3951                (end-of-line)
3952                (point)))))))
3953     (goto-char begin)
3954     (beginning-of-line)                 ;Important when LINES < 1
3955     (gnus-group-kill-group lines)))
3956
3957 (defun gnus-group-kill-group (n)
3958   "The the next N groups.
3959 The killed newsgroups can be yanked by using \\[gnus-group-yank-group].
3960 However, only groups that were alive can be yanked; already killed 
3961 groups or zombie groups can't be yanked.
3962 The return value is the name of the (last) newsgroup that was killed."
3963   (interactive "p")
3964   (let ((buffer-read-only nil)
3965         group entry level)
3966     (while (>= (setq n  (1- n)) 0)
3967       (setq group (gnus-group-group-name))
3968       (or group
3969           (signal 'end-of-buffer nil))
3970       (setq level (gnus-group-group-level))
3971       (beginning-of-line)
3972       (delete-region (point) (progn (forward-line 1) (point)))
3973       (if (setq entry (gnus-gethash group gnus-newsrc-hashtb))
3974           (setq gnus-list-of-killed-groups 
3975                 (cons (cons (car entry) (nth 2 entry)) 
3976                       gnus-list-of-killed-groups)))
3977       (gnus-group-change-level 
3978        (if entry entry group) gnus-level-killed (if entry nil level)))
3979     (if (eobp)
3980         (forward-line -1))
3981     (gnus-group-position-cursor)
3982     group))
3983
3984 (defun gnus-group-yank-group (&optional arg)
3985   "Yank the last newsgroups killed with \\[gnus-group-kill-group],
3986 inserting it before the current newsgroup.  The numeric ARG specifies
3987 how many newsgroups are to be yanked.  The name of the (last)
3988 newsgroup yanked is returned."
3989   (interactive "p")
3990   (if (not arg) (setq arg 1))
3991   (let (info group prev)
3992     (while (>= (setq arg (1- arg)) 0)
3993       (if (not (setq info (car gnus-list-of-killed-groups)))
3994           (error "No more newsgroups to yank"))
3995       (setq group (nth 2 info))
3996       ;; Find which newsgroup to insert this one before - search
3997       ;; backward until something suitable is found. If there are no
3998       ;; other newsgroups in this buffer, just make this newsgroup the
3999       ;; first newsgroup.
4000       (while (and (not (setq prev (gnus-group-group-name)))
4001                   (zerop (forward-line -1))))
4002       (if (not prev)
4003           (setq prev (car (car gnus-newsrc-alist))))
4004       (gnus-group-change-level 
4005        info (nth 2 info) gnus-level-killed 
4006        (gnus-gethash prev gnus-newsrc-hashtb)
4007        t)
4008       (gnus-group-insert-group-line-info (nth 1 info))
4009       (setq gnus-list-of-killed-groups 
4010             (cdr gnus-list-of-killed-groups)))
4011     (forward-line -1)
4012     (gnus-group-position-cursor)
4013     group))
4014       
4015 (defun gnus-group-list-all-groups (arg)
4016   "List all newsgroups with level ARG or lower.
4017 Default is 7, which lists all subscribed and most unsubscribed groups."
4018   (interactive "P")
4019   (setq arg (or arg 7))
4020   (gnus-group-list-groups arg t))
4021
4022 (defun gnus-group-list-killed ()
4023   "List all killed newsgroups in the group buffer."
4024   (interactive)
4025   (if (not gnus-killed-list)
4026       (message "No killed groups")
4027     (funcall gnus-group-prepare-function gnus-level-killed t gnus-level-killed)
4028     (goto-char (point-min)))
4029   (gnus-group-position-cursor))
4030
4031 (defun gnus-group-list-zombies ()
4032   "List all zombie newsgroups in the group buffer."
4033   (interactive)
4034   (if (not gnus-zombie-list)
4035       (message "No zombie groups")
4036     (funcall gnus-group-prepare-function gnus-level-zombie t gnus-level-zombie)
4037     (goto-char (point-min)))
4038   (gnus-group-position-cursor))
4039
4040 (defun gnus-group-get-new-news (&optional arg)
4041   "Get newly arrived articles.
4042 If ARG is non-nil, it should be a number between one and nine to
4043 specify which levels you are interested in re-scanning."
4044   (interactive "P")
4045   (run-hooks 'gnus-get-new-news-hook)
4046   (let ((level arg))
4047     (if (and gnus-read-active-file (not level))
4048         (progn
4049           (gnus-read-active-file)
4050           (gnus-get-unread-articles (or level (1+ gnus-level-subscribed))))
4051       (let ((gnus-read-active-file nil))
4052         (gnus-get-unread-articles (or level (1+ gnus-level-subscribed)))))
4053     (gnus-group-list-groups 
4054      (or gnus-group-always-list-unread level gnus-level-subscribed)
4055      gnus-have-all-newsgroups)))
4056
4057 (defun gnus-group-get-new-news-this-group (n)
4058   "Check for newly arrived news in the current group (and the N-1 next groups).
4059 The difference between N and the number of newsgroup checked is returned.
4060 If N is negative, this group and the N-1 previous groups will be checked."
4061   (interactive "p")
4062   (let ((way (if (< n 0) -1 1))
4063         (n (abs n))
4064         (w-p (window-start))
4065         group)
4066     (while (and (> n 0)
4067                 (progn
4068                   (or (gnus-get-new-news-in-group
4069                        (setq group (gnus-group-group-name)))
4070                       (progn 
4071                         (ding) 
4072                         (message "%s error: %s" 
4073                                  group (gnus-status-message group))))
4074                   t)
4075                 (zerop (gnus-group-next-group way)))
4076       (setq n (1- n)))
4077     (if (/= 0 n) (message "No more newsgroups"))
4078     ;; !!! I don't know why the buffer scrolls forward when updating
4079     ;; the first line in the group buffer, but it does. So we set the
4080     ;; window start forcibly.
4081     (set-window-start (get-buffer-window (current-buffer)) w-p)
4082     n))
4083
4084 (defun gnus-get-new-news-in-group (group)
4085   (and group 
4086        (gnus-activate-newsgroup group)
4087        (progn
4088          (gnus-get-unread-articles-in-group 
4089           (nth 2 (gnus-gethash group gnus-newsrc-hashtb))
4090           (gnus-gethash group gnus-active-hashtb))
4091          (gnus-group-update-group-line)
4092          t)))
4093
4094 (defun gnus-group-fetch-faq (group)
4095   "Fetch the FAQ for the current group."
4096   (interactive (list (gnus-group-group-name)))
4097   (or group (error "No group name given"))
4098   (let ((file (concat gnus-group-faq-directory group))) 
4099     (if (not (file-exists-p file))
4100         (error "No such file: %s" file)
4101       (find-file file))))
4102   
4103 (defun gnus-group-describe-group (force &optional group)
4104   "Display a description of the current newsgroup."
4105   (interactive "P")
4106   (and force (setq gnus-description-hashtb nil))
4107   (let ((group (or group (gnus-group-group-name)))
4108         desc)
4109     (or group (message "No group name given"))
4110     (and (or gnus-description-hashtb
4111              (setq desc (gnus-group-get-description group))
4112              (gnus-read-descriptions-file))
4113          (message
4114           (or desc (gnus-gethash group gnus-description-hashtb)
4115               "No description available")))))
4116
4117 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
4118 (defun gnus-group-describe-all-groups (force)
4119   "Pop up a buffer with descriptons of all newsgroups."
4120   (interactive "P")
4121   (and force (setq gnus-description-hashtb nil))
4122   (if (not (or gnus-description-hashtb
4123                (gnus-read-descriptions-file)))
4124       (error "Couldn't request descriptions file"))
4125   (let ((buffer-read-only nil)
4126         b)
4127     (erase-buffer)
4128     (mapatoms
4129      (lambda (group)
4130        (setq b (point))
4131        (insert (format "      *: %-20s %s\n" (symbol-name group)
4132                        (symbol-value group)))
4133        (add-text-properties 
4134         b (1+ b) (list 'gnus-group group
4135                        'gnus-unread t 'gnus-marked nil
4136                        'gnus-level (1+ gnus-level-subscribed))))
4137      gnus-description-hashtb)
4138     (goto-char (point-min))
4139     (gnus-group-position-cursor)))
4140
4141 ;; Suggested by by Daniel Quinlan <quinlan@best.com>.
4142 (defun gnus-group-apropos (regexp &optional search-description)
4143   "List all newsgroups that have names that match a regexp."
4144   (interactive "sGnus apropos (regexp): ")
4145   (let ((prev "")
4146         (obuf (current-buffer))
4147         groups des prev)
4148     ;; Go through all newsgroups that are known to Gnus.
4149     (mapatoms 
4150      (lambda (group)
4151        (and (string-match regexp (symbol-name group))
4152             (setq groups (cons (symbol-name group) groups))))
4153      gnus-active-hashtb)
4154     ;; Go through all descriptions that are known to Gnus. 
4155     (if search-description
4156         (mapatoms 
4157          (lambda (group)
4158            (and (string-match regexp (symbol-value group))
4159                 (gnus-gethash (symbol-name group) gnus-active-hashtb)
4160                 (setq groups (cons (symbol-name group) groups))))
4161          gnus-description-hashtb))
4162     (if (not groups)
4163         (message "No groups matched \"%s\"." regexp)
4164       ;; Print out all the groups.
4165       (save-excursion
4166         (pop-to-buffer "*Gnus Help*")
4167         (buffer-disable-undo (current-buffer))
4168         (erase-buffer)
4169         (setq groups (sort groups 'string<))
4170         (while groups
4171           ;; Groups may be entered twice into the list of groups.
4172           (if (not (string= (car groups) prev))
4173               (progn
4174                 (insert (setq prev (car groups)) "\n")
4175                 (if (and gnus-description-hashtb
4176                          (setq des (gnus-gethash (car groups) 
4177                                                  gnus-description-hashtb)))
4178                     (insert "  " des "\n"))))
4179           (setq groups (cdr groups)))
4180         (goto-char 1)))
4181     (pop-to-buffer obuf)))
4182
4183 (defun gnus-group-description-apropos (regexp)
4184   "List all newsgroups that have names or desccriptions that match a regexp."
4185   (interactive "sGnus description apropos (regexp): ")
4186   (if (not (or gnus-description-hashtb
4187                (gnus-read-descriptions-file)))
4188       (error "Couldn't request descriptions file"))
4189   (gnus-group-apropos regexp t))
4190
4191 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
4192 (defun gnus-group-list-matching (level regexp &optional all lowest) 
4193   "List all groups with unread articles that match REGEXP.
4194 If the prefix LEVEL is non-nil, it should be a number that says which
4195 level to cut off listing groups. 
4196 If ALL, also list groups with no unread articles.
4197 If LOWEST, don't list groups with level lower than LOWEST."
4198   (interactive "P\nsList newsgroups matching: ")
4199   (gnus-group-prepare-flat (or level gnus-level-subscribed)
4200                            all (or lowest 1) regexp)
4201   (goto-char (point-min))
4202   (gnus-group-position-cursor))
4203
4204 (defun gnus-group-list-all-matching (level regexp &optional lowest) 
4205   "List all groups that match REGEXP.
4206 If the prefix LEVEL is non-nil, it should be a number that says which
4207 level to cut off listing groups. 
4208 If LOWEST, don't list groups with level lower than LOWEST."
4209   (interactive "P\nsList newsgroups matching: ")
4210   (gnus-group-list-matching (or level gnus-level-killed) regexp t lowest))
4211
4212 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
4213 (defun gnus-group-save-newsrc ()
4214   "Save the Gnus startup files."
4215   (interactive)
4216   (gnus-save-newsrc-file))
4217
4218 (defun gnus-group-restart (&optional arg)
4219   "Force Gnus to read the .newsrc file."
4220   (interactive "P")
4221   (gnus-save-newsrc-file)
4222   (gnus-setup-news 'force)
4223   (gnus-group-list-groups arg gnus-have-all-newsgroups))
4224
4225 (defun gnus-group-read-init-file ()
4226   "Read the Gnus elisp init file."
4227   (interactive)
4228   (gnus-read-init-file))
4229
4230 (defun gnus-group-check-bogus-groups ()
4231   "Check bogus newsgroups."
4232   (interactive)
4233   (gnus-check-bogus-newsgroups (not gnus-expert-user))  ;Require confirmation.
4234   (gnus-group-list-groups nil gnus-have-all-newsgroups))
4235
4236 (defun gnus-group-mail ()
4237   "Start composing a mail."
4238   (interactive)
4239   (mail))
4240
4241 (defun gnus-group-edit-global-kill (article &optional group)
4242   "Edit the global kill file.
4243 If GROUP, edit that local kill file instead."
4244   (interactive "P")
4245   (setq gnus-current-kill-article article)
4246   (gnus-kill-file-edit-file group)
4247   (message
4248    (substitute-command-keys
4249     "Editing a global kill file (Type \\[gnus-kill-file-exit] to exit)")))
4250
4251 (defun gnus-group-edit-local-kill (article group)
4252   "Edit a local kill file."
4253   (interactive (list nil (gnus-group-group-name)))
4254   (gnus-group-edit-global-kill article group))
4255
4256 (defun gnus-group-force-update ()
4257   "Update `.newsrc' file."
4258   (interactive)
4259   (gnus-save-newsrc-file))
4260
4261 (defun gnus-group-suspend ()
4262   "Suspend the current Gnus session.
4263 In fact, cleanup buffers except for group mode buffer.
4264 The hook gnus-suspend-gnus-hook is called before actually suspending."
4265   (interactive)
4266   (run-hooks 'gnus-suspend-gnus-hook)
4267   ;; Kill Gnus buffers except for group mode buffer.
4268   (let ((group-buf (get-buffer gnus-group-buffer)))
4269     ;; Do this on a separate list in case the user does a ^G before we finish
4270     (let ((gnus-buffer-list
4271            (delq group-buf (delq gnus-dribble-buffer
4272                                  (append gnus-buffer-list nil)))))
4273       (while gnus-buffer-list
4274         (gnus-kill-buffer (car gnus-buffer-list))
4275         (setq gnus-buffer-list (cdr gnus-buffer-list))))
4276     (if group-buf
4277         (progn
4278           (setq gnus-buffer-list (list group-buf))
4279           (bury-buffer group-buf)
4280           (delete-windows-on group-buf t)))))
4281
4282 (defun gnus-group-clear-dribble ()
4283   "Clear all information from the dribble buffer."
4284   (interactive)
4285   (gnus-dribble-clear))
4286
4287 (defun gnus-group-exit ()
4288   "Quit reading news after updating .newsrc.eld and .newsrc.
4289 The hook `gnus-exit-gnus-hook' is called before actually exiting."
4290   (interactive)
4291   (if (or noninteractive                ;For gnus-batch-kill
4292           (zerop (buffer-size))         ;No news is good news.
4293           (not (gnus-server-opened gnus-select-method)) ;NNTP connection closed
4294           (not gnus-interactive-exit)   ;Without confirmation
4295           gnus-expert-user
4296           (gnus-y-or-n-p "Are you sure you want to quit reading news? "))
4297       (progn
4298         (run-hooks 'gnus-exit-gnus-hook)
4299         (gnus-save-newsrc-file)
4300         (gnus-close-backends)
4301         (gnus-clear-system))))
4302
4303 (defun gnus-close-backends ()
4304   ;; Send a close request to all backends that support such a request. 
4305   (let ((methods gnus-valid-select-methods)
4306         func)
4307     (while methods
4308       (if (fboundp (setq func (intern (concat (car (car methods))
4309                                               "-request-close"))))
4310           (funcall func))
4311       (setq methods (cdr methods)))))
4312
4313 (defun gnus-group-quit ()
4314   "Quit reading news without updating .newsrc.eld or .newsrc.
4315 The hook `gnus-exit-gnus-hook' is called before actually exiting."
4316   (interactive)
4317   (if (or noninteractive                ;For gnus-batch-kill
4318           (zerop (buffer-size))
4319           (not (gnus-server-opened gnus-select-method))
4320           gnus-expert-user
4321           (not gnus-current-startup-file)
4322           (gnus-yes-or-no-p
4323            (format "Quit reading news without saving %s? "
4324                    (file-name-nondirectory gnus-current-startup-file))))
4325       (progn
4326         (run-hooks 'gnus-exit-gnus-hook)
4327         (gnus-dribble-save)
4328         (gnus-close-backends)
4329         (gnus-clear-system))))
4330
4331 (defun gnus-group-describe-briefly ()
4332   "Give a one line description of the group mode commands."
4333   (interactive)
4334   (message
4335    (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")))
4336
4337 (defun gnus-group-browse-foreign-server (method)
4338   "Browse a foreign news server.
4339 If called interactively, this function will ask for a select method
4340  (nntp, nnspool, etc.) and a server address (eg. nntp.some.where). 
4341 If not, METHOD should be a list where the first element is the method
4342 and the second element is the address."
4343   (interactive
4344    (list (let ((how (completing-read 
4345                      "Which backend: "
4346                      (append gnus-valid-select-methods gnus-server-alist)
4347                      nil t "nntp")))
4348            ;; We either got a backend name or a virtual server name.
4349            ;; If the first, we also need an address.
4350            (if (assoc how gnus-valid-select-methods)
4351                (list (intern how)
4352                      ;; Suggested by mapjph@bath.ac.uk.
4353                      (completing-read 
4354                       "Address: " 
4355                       (mapcar (lambda (server) (list server))
4356                               gnus-secondary-servers)))
4357              ;; We got a server name, so we find the method.
4358              (gnus-server-to-method how)))))
4359   (gnus-browse-foreign-server method))
4360
4361 \f
4362 ;;;
4363 ;;; Browse Server Mode
4364 ;;;
4365
4366 (defvar gnus-browse-server-mode-hook nil)
4367 (defvar gnus-browse-server-mode-map nil)
4368 (put 'gnus-browse-server-mode 'mode-class 'special)
4369
4370 (if gnus-browse-server-mode-map
4371     nil
4372   (setq gnus-browse-server-mode-map (make-keymap))
4373   (suppress-keymap gnus-browse-server-mode-map)
4374   (define-key gnus-browse-server-mode-map " " 'gnus-browse-read-group)
4375   (define-key gnus-browse-server-mode-map "=" 'gnus-browse-read-group)
4376   (define-key gnus-browse-server-mode-map "n" 'gnus-browse-next-group)
4377   (define-key gnus-browse-server-mode-map "p" 'gnus-browse-prev-group)
4378   (define-key gnus-browse-server-mode-map "\177" 'gnus-browse-prev-group)
4379   (define-key gnus-browse-server-mode-map "N" 'gnus-browse-next-group)
4380   (define-key gnus-browse-server-mode-map "P" 'gnus-browse-prev-group)
4381   (define-key gnus-browse-server-mode-map "\M-n" 'gnus-browse-next-group)
4382   (define-key gnus-browse-server-mode-map "\M-p" 'gnus-browse-prev-group)
4383   (define-key gnus-browse-server-mode-map "\r" 'gnus-browse-read-group)
4384   (define-key gnus-browse-server-mode-map "u" 'gnus-browse-unsubscribe-current-group)
4385   (define-key gnus-browse-server-mode-map "l" 'gnus-browse-exit)
4386   (define-key gnus-browse-server-mode-map "L" 'gnus-browse-exit)
4387   (define-key gnus-browse-server-mode-map "q" 'gnus-browse-exit)
4388   (define-key gnus-browse-server-mode-map "Q" 'gnus-browse-exit)
4389   (define-key gnus-browse-server-mode-map "\C-c\C-c" 'gnus-browse-exit)
4390   (define-key gnus-browse-server-mode-map "?" 'gnus-browse-describe-briefly)
4391   (define-key gnus-browse-server-mode-map "\C-c\C-i" 'gnus-info-find-node)
4392   )
4393
4394 (defvar gnus-browse-current-method nil)
4395 (defvar gnus-browse-return-buffer nil)
4396
4397 (defun gnus-browse-foreign-server (method &optional return-buffer)
4398   (setq gnus-browse-current-method method)
4399   (setq gnus-browse-return-buffer return-buffer)
4400   (let ((gnus-select-method method)
4401         groups group)
4402     (message "Connecting to %s..." (nth 1 method))
4403     (or (gnus-server-opened method)
4404         (gnus-open-server method)
4405         (error "Unable to contact server: %s" (gnus-status-message method)))
4406     (or (gnus-request-list method)
4407         (error "Couldn't request list: %s" (gnus-status-message method)))
4408     (set-buffer (get-buffer-create "*Gnus Browse Server*"))
4409     (gnus-add-current-to-buffer-list)
4410     (buffer-disable-undo (current-buffer))
4411     (let ((buffer-read-only nil))
4412       (erase-buffer))
4413     (gnus-browse-server-mode)
4414     (setq mode-line-buffer-identification
4415           (format
4416            "(ding) Browse Server {%s:%s}" (car method) (car (cdr method))))
4417     (save-excursion
4418       (set-buffer nntp-server-buffer)
4419       (let ((cur (current-buffer)))
4420         (goto-char 1)
4421         (delete-matching-lines gnus-ignored-newsgroups)
4422         (while (re-search-forward 
4423                 "\\(^[^ \t]+\\)[ \t]+[0-9]+[ \t]+[0-9]+" nil t)
4424           (goto-char (match-end 1))
4425           (setq groups (cons (cons (buffer-substring (match-beginning 1)
4426                                                      (match-end 1))
4427                                    (max 0 (- (1+ (read cur)) (read cur))))
4428                              groups)))))
4429     (setq groups (sort groups 
4430                        (lambda (l1 l2)
4431                          (string< (car l1) (car l2)))))
4432     (let ((buffer-read-only nil))
4433       (while groups
4434         (setq group (car groups))
4435         (insert 
4436          (format "K%7d: %s\n" (cdr group) (car group)))
4437         (setq groups (cdr groups))))
4438     (switch-to-buffer (current-buffer))
4439     (goto-char 1)
4440     (gnus-group-position-cursor)))
4441
4442 (defun gnus-browse-server-mode ()
4443   "Major mode for browsing a foreign server."
4444   (interactive)
4445   (kill-all-local-variables)
4446   (setq mode-line-modified "-- ")
4447   (make-local-variable 'mode-line-format)
4448   (setq mode-line-format (copy-sequence mode-line-format))
4449   (and (equal (nth 3 mode-line-format) "   ")
4450        (setcar (nthcdr 3 mode-line-format) ""))
4451   (setq major-mode 'gnus-browse-server-mode)
4452   (setq mode-name "Browse Server")
4453   (setq mode-line-process nil)
4454   (use-local-map gnus-browse-server-mode-map)
4455   (buffer-disable-undo (current-buffer))
4456   (setq truncate-lines t)
4457   (setq buffer-read-only t)
4458   (run-hooks 'gnus-browse-server-mode-hook))
4459
4460 (defun gnus-browse-read-group ()
4461   "Not implemented, and will probably never be."
4462   (interactive)
4463   (error "You can't read while browsing"))
4464
4465 (defun gnus-browse-next-group (n)
4466   "Go to the next group."
4467   (interactive "p")
4468   (prog1
4469       (forward-line n)
4470     (gnus-group-position-cursor)))
4471
4472 (defun gnus-browse-prev-group (n)
4473   "Go to the next group."
4474   (interactive "p")
4475   (gnus-browse-next-group (- n)))
4476
4477 (defun gnus-browse-unsubscribe-current-group (arg)
4478   "(Un)subscribe to the next ARG groups."
4479   (interactive "p")
4480   (and (eobp)
4481        (error "No group at current line."))
4482   (let ((ward (if (< arg 0) -1 1))
4483         (arg (abs arg)))
4484     (while (and (> arg 0)
4485                 (not (eobp))
4486                 (gnus-browse-unsubscribe-group)
4487                 (zerop (gnus-browse-next-group ward)))
4488       (setq arg (1- arg)))
4489     (gnus-group-position-cursor)
4490     (if (/= 0 arg) (message "No more newsgroups"))
4491     arg))
4492   
4493 (defun gnus-browse-unsubscribe-group ()
4494   (let ((sub nil)
4495         (buffer-read-only nil)
4496         group)
4497     (save-excursion
4498       (beginning-of-line)
4499       (if (= (following-char) ?K) (setq sub t))
4500       (re-search-forward ": \\(.*\\)$" nil t)
4501       (setq group (gnus-group-prefixed-name 
4502                    (buffer-substring (match-beginning 1) (match-end 1))
4503                    gnus-browse-current-method))
4504       (beginning-of-line)
4505       (delete-char 1)
4506       (if sub
4507           (progn
4508             (gnus-group-change-level 
4509              (list t group gnus-level-default-subscribed
4510                    nil nil gnus-browse-current-method) 
4511              gnus-level-default-subscribed gnus-level-killed
4512              (gnus-gethash (car (nth 1 gnus-newsrc-alist)) gnus-newsrc-hashtb)
4513              t)
4514             (insert ? ))
4515         (gnus-group-change-level 
4516          group gnus-level-killed gnus-level-default-subscribed)
4517         (insert ?K)))
4518     t))
4519
4520 (defun gnus-browse-exit ()
4521   "Quit browsing and return to the group buffer."
4522   (interactive)
4523   (if (eq major-mode 'gnus-browse-server-mode)
4524       (kill-buffer (current-buffer)))
4525   (switch-to-buffer (or gnus-browse-return-buffer gnus-group-buffer)))
4526
4527 (defun gnus-browse-describe-briefly ()
4528   "Give a one line description of the group mode commands."
4529   (interactive)
4530   (message
4531    (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")))
4532       
4533 \f
4534 ;;;
4535 ;;; Gnus summary mode
4536 ;;;
4537
4538 (defvar gnus-summary-mode-map nil)
4539 (defvar gnus-summary-mark-map nil)
4540 (defvar gnus-summary-mscore-map nil)
4541 (defvar gnus-summary-extract-map nil)
4542 (defvar gnus-summary-extract-view-map nil)
4543 (defvar gnus-summary-article-map nil)
4544 (defvar gnus-summary-thread-map nil)
4545 (defvar gnus-summary-goto-map nil)
4546 (defvar gnus-summary-exit-map nil)
4547 (defvar gnus-summary-various-map nil)
4548 (defvar gnus-summary-interest-map nil)
4549 (defvar gnus-summary-process-map nil)
4550 (defvar gnus-summary-sort-map nil)
4551 (defvar gnus-summary-backend-map nil)
4552 (defvar gnus-summary-save-map nil)
4553 (defvar gnus-summary-wash-map nil)
4554 (defvar gnus-summary-help-map nil)
4555
4556 (put 'gnus-summary-mode 'mode-class 'special)
4557
4558 (if gnus-summary-mode-map
4559     nil
4560   (setq gnus-summary-mode-map (make-keymap))
4561   (suppress-keymap gnus-summary-mode-map)
4562
4563   ;; Non-orthogonal keys
4564
4565   (define-key gnus-summary-mode-map " " 'gnus-summary-next-page)
4566   (define-key gnus-summary-mode-map "\177" 'gnus-summary-prev-page)
4567   (define-key gnus-summary-mode-map "\r" 'gnus-summary-scroll-up)
4568   (define-key gnus-summary-mode-map "n" 'gnus-summary-next-unread-article)
4569   (define-key gnus-summary-mode-map "p" 'gnus-summary-prev-unread-article)
4570   (define-key gnus-summary-mode-map "N" 'gnus-summary-next-article)
4571   (define-key gnus-summary-mode-map "P" 'gnus-summary-prev-article)
4572   (define-key gnus-summary-mode-map "\M-\C-n" 'gnus-summary-next-same-subject)
4573   (define-key gnus-summary-mode-map "\M-\C-p" 'gnus-summary-prev-same-subject)
4574   (define-key gnus-summary-mode-map "\M-n" 'gnus-summary-next-unread-subject)
4575   (define-key gnus-summary-mode-map "\M-p" 'gnus-summary-prev-unread-subject)
4576   (define-key gnus-summary-mode-map "." 'gnus-summary-first-unread-article)
4577   (define-key gnus-summary-mode-map "," 'gnus-summary-best-unread-article)
4578   (define-key gnus-summary-mode-map "\M-s" 'gnus-summary-search-article-forward)
4579   (define-key gnus-summary-mode-map "\M-r" 'gnus-summary-search-article-backward)
4580   (define-key gnus-summary-mode-map "<" 'gnus-summary-beginning-of-article)
4581   (define-key gnus-summary-mode-map ">" 'gnus-summary-end-of-article)
4582   (define-key gnus-summary-mode-map "j" 'gnus-summary-goto-subject)
4583   (define-key gnus-summary-mode-map "^" 'gnus-summary-refer-parent-article)
4584   (define-key gnus-summary-mode-map "\M-^" 'gnus-summary-refer-article)
4585   (define-key gnus-summary-mode-map "u" 'gnus-summary-tick-article-forward)
4586   (define-key gnus-summary-mode-map "!" 'gnus-summary-tick-article-forward)
4587   (define-key gnus-summary-mode-map "U" 'gnus-summary-tick-article-backward)
4588   (define-key gnus-summary-mode-map "d" 'gnus-summary-mark-as-read-forward)
4589   (define-key gnus-summary-mode-map "D" 'gnus-summary-mark-as-read-backward)
4590   (define-key gnus-summary-mode-map "E" 'gnus-summary-mark-as-expirable)
4591   (define-key gnus-summary-mode-map "\M-u" 'gnus-summary-clear-mark-forward)
4592   (define-key gnus-summary-mode-map "\M-U" 'gnus-summary-clear-mark-backward)
4593   (define-key gnus-summary-mode-map "k" 'gnus-summary-kill-same-subject-and-select)
4594   (define-key gnus-summary-mode-map "\C-k" 'gnus-summary-kill-same-subject)
4595   (define-key gnus-summary-mode-map "\M-\C-k" 'gnus-summary-kill-thread)
4596   (define-key gnus-summary-mode-map "\M-\C-l" 'gnus-summary-lower-thread)
4597   (define-key gnus-summary-mode-map "e" 'gnus-summary-edit-article)
4598   (define-key gnus-summary-mode-map "#" 'gnus-summary-mark-as-processable)
4599   (define-key gnus-summary-mode-map "\M-#" 'gnus-summary-unmark-as-processable)
4600   (define-key gnus-summary-mode-map "\M-\C-t" 'gnus-summary-toggle-threads)
4601   (define-key gnus-summary-mode-map "\M-\C-s" 'gnus-summary-show-thread)
4602   (define-key gnus-summary-mode-map "\M-\C-h" 'gnus-summary-hide-thread)
4603   (define-key gnus-summary-mode-map "\M-\C-f" 'gnus-summary-next-thread)
4604   (define-key gnus-summary-mode-map "\M-\C-b" 'gnus-summary-prev-thread)
4605   (define-key gnus-summary-mode-map "\M-\C-u" 'gnus-summary-up-thread)
4606   (define-key gnus-summary-mode-map "\M-\C-d" 'gnus-summary-down-thread)
4607   (define-key gnus-summary-mode-map "&" 'gnus-summary-execute-command)
4608   (define-key gnus-summary-mode-map "c" 'gnus-summary-catchup-and-exit)
4609   (define-key gnus-summary-mode-map "\C-w" 'gnus-summary-mark-region-as-read)
4610   (define-key gnus-summary-mode-map "\C-t" 'gnus-summary-toggle-truncation)
4611   (define-key gnus-summary-mode-map "?" 'gnus-summary-mark-as-dormant)
4612   (define-key gnus-summary-mode-map "\C-c\M-\C-s" 'gnus-summary-show-all-expunged)
4613   (define-key gnus-summary-mode-map "\C-c\C-s\C-n" 'gnus-summary-sort-by-number)
4614   (define-key gnus-summary-mode-map "\C-c\C-s\C-a" 'gnus-summary-sort-by-author)
4615   (define-key gnus-summary-mode-map "\C-c\C-s\C-s" 'gnus-summary-sort-by-subject)
4616   (define-key gnus-summary-mode-map "\C-c\C-s\C-d" 'gnus-summary-sort-by-date)
4617   (define-key gnus-summary-mode-map "\C-c\C-s\C-i" 'gnus-summary-sort-by-score)
4618   (define-key gnus-summary-mode-map "=" 'gnus-summary-expand-window)
4619   (define-key gnus-summary-mode-map "\C-x\C-s" 'gnus-summary-reselect-current-group)
4620   (define-key gnus-summary-mode-map "\M-g" 'gnus-summary-rescan-group)
4621   (define-key gnus-summary-mode-map "w" 'gnus-summary-stop-page-breaking)
4622   (define-key gnus-summary-mode-map "\C-c\C-r" 'gnus-summary-caesar-message)
4623   (define-key gnus-summary-mode-map "\M-t" 'gnus-summary-toggle-mime)
4624   (define-key gnus-summary-mode-map "f" 'gnus-summary-followup)
4625   (define-key gnus-summary-mode-map "F" 'gnus-summary-followup-with-original)
4626   (define-key gnus-summary-mode-map "C" 'gnus-summary-cancel-article)
4627   (define-key gnus-summary-mode-map "r" 'gnus-summary-reply)
4628   (define-key gnus-summary-mode-map "R" 'gnus-summary-reply-with-original)
4629   (define-key gnus-summary-mode-map "\C-c\C-f" 'gnus-summary-mail-forward)
4630   (define-key gnus-summary-mode-map "o" 'gnus-summary-save-article)
4631   (define-key gnus-summary-mode-map "\C-o" 'gnus-summary-save-article-mail)
4632   (define-key gnus-summary-mode-map "|" 'gnus-summary-pipe-output)
4633   (define-key gnus-summary-mode-map "\M-k" 'gnus-summary-edit-local-kill)
4634   (define-key gnus-summary-mode-map "\M-K" 'gnus-summary-edit-global-kill)
4635   (define-key gnus-summary-mode-map "V" 'gnus-version)
4636   (define-key gnus-summary-mode-map "\C-c\C-d" 'gnus-summary-describe-group)
4637   (define-key gnus-summary-mode-map "q" 'gnus-summary-exit)
4638   (define-key gnus-summary-mode-map "Q" 'gnus-summary-exit-no-update)
4639   (define-key gnus-summary-mode-map "\C-c\C-i" 'gnus-info-find-node)
4640   (define-key gnus-summary-mode-map
4641     (if gnus-xemacs [button2] [mouse-2]) 'gnus-mouse-pick-article)
4642   (define-key gnus-summary-mode-map "m" 'gnus-summary-mail-other-window)
4643   (define-key gnus-summary-mode-map "a" 'gnus-summary-post-news)
4644   (define-key gnus-summary-mode-map "x" 'gnus-summary-remove-lines-marked-as-read)
4645 ; (define-key gnus-summary-mode-map "X" 'gnus-summary-remove-lines-marked-with)
4646   (define-key gnus-summary-mode-map "s" 'gnus-summary-isearch-article)
4647   (define-key gnus-summary-mode-map "t" 'gnus-summary-toggle-header)
4648   (define-key gnus-summary-mode-map "g" 'gnus-summary-show-article)
4649 ;  (define-key gnus-summary-mode-map "?" 'gnus-summary-describe-briefly)
4650   (define-key gnus-summary-mode-map "l" 'gnus-summary-goto-last-article)
4651   (define-key gnus-summary-mode-map "\C-c\C-v\C-v" 'gnus-uu-decode-uu-view)
4652   (define-key gnus-summary-mode-map "\C-d" 'gnus-summary-enter-digest-group)
4653
4654
4655   ;; Sort of orthogonal keymap
4656   (define-prefix-command 'gnus-summary-mark-map)
4657   (define-key gnus-summary-mode-map "M" 'gnus-summary-mark-map)
4658   (define-key gnus-summary-mark-map "t" 'gnus-summary-tick-article-forward)
4659   (define-key gnus-summary-mark-map "!" 'gnus-summary-tick-article-forward)
4660   (define-key gnus-summary-mark-map "d" 'gnus-summary-mark-as-read-forward)
4661   (define-key gnus-summary-mark-map "r" 'gnus-summary-mark-as-read-forward)
4662   (define-key gnus-summary-mark-map "c" 'gnus-summary-clear-mark-forward)
4663   (define-key gnus-summary-mark-map " " 'gnus-summary-clear-mark-forward)
4664   (define-key gnus-summary-mark-map "e" 'gnus-summary-mark-as-expirable)
4665   (define-key gnus-summary-mark-map "x" 'gnus-summary-mark-as-expirable)
4666   (define-key gnus-summary-mark-map "?" 'gnus-summary-mark-as-dormant)
4667   (define-key gnus-summary-mark-map "b" 'gnus-summary-set-bookmark)
4668   (define-key gnus-summary-mark-map "B" 'gnus-summary-remove-bookmark)
4669   (define-key gnus-summary-mark-map "#" 'gnus-summary-mark-as-processable)
4670   (define-key gnus-summary-mark-map "\M-#" 'gnus-summary-unmark-as-processable)
4671   (define-key gnus-summary-mark-map "\M-r" 'gnus-summary-remove-lines-marked-as-read)
4672   (define-key gnus-summary-mark-map "\M-\C-r" 'gnus-summary-remove-lines-marked-with)
4673   (define-key gnus-summary-mark-map "D" 'gnus-summary-show-all-dormant)
4674   (define-key gnus-summary-mark-map "\M-D" 'gnus-summary-hide-all-dormant)
4675   (define-key gnus-summary-mark-map "S" 'gnus-summary-show-all-expunged)
4676   (define-key gnus-summary-mark-map "C" 'gnus-summary-catchup)
4677   (define-key gnus-summary-mark-map "H" 'gnus-summary-catchup-to-here)
4678   (define-key gnus-summary-mark-map "\C-c" 'gnus-summary-catchup-all)
4679   (define-key gnus-summary-mark-map "k" 'gnus-summary-kill-same-subject-and-select)
4680   (define-key gnus-summary-mark-map "K" 'gnus-summary-kill-same-subject)
4681
4682   (define-prefix-command 'gnus-summary-mscore-map)
4683   (define-key gnus-summary-mark-map "s" 'gnus-summary-mscore-map)
4684   (define-key gnus-summary-mscore-map "c" 'gnus-summary-clear-above)
4685   (define-key gnus-summary-mscore-map "u" 'gnus-summary-tick-above)
4686   (define-key gnus-summary-mscore-map "m" 'gnus-summary-mark-above)
4687   (define-key gnus-summary-mscore-map "k" 'gnus-summary-kill-below)
4688
4689   (define-prefix-command 'gnus-summary-process-map)
4690   (define-key gnus-summary-mark-map "p" 'gnus-summary-process-map)
4691   (define-key gnus-summary-process-map "p" 'gnus-summary-mark-as-processable)
4692   (define-key gnus-summary-process-map "u" 'gnus-summary-unmark-as-processable)
4693   (define-key gnus-summary-process-map "U" 'gnus-summary-unmark-all-processable)
4694   (define-key gnus-summary-process-map "s" 'gnus-uu-mark-series)
4695   (define-key gnus-summary-process-map "r" 'gnus-uu-mark-region)
4696   (define-key gnus-summary-process-map "R" 'gnus-uu-mark-by-regexp)
4697   (define-key gnus-summary-process-map "t" 'gnus-uu-mark-thread)
4698   (define-key gnus-summary-process-map "a" 'gnus-uu-mark-all)
4699   (define-key gnus-summary-process-map "S" 'gnus-uu-mark-sparse)
4700   
4701   (define-key gnus-summary-mode-map "S" 'gnus-summary-send-map)
4702   
4703   (define-prefix-command 'gnus-summary-goto-map)
4704   (define-key gnus-summary-mode-map "G" 'gnus-summary-goto-map)
4705   (define-key gnus-summary-goto-map "n" 'gnus-summary-next-unread-article)
4706   (define-key gnus-summary-goto-map "p" 'gnus-summary-prev-unread-article)
4707   (define-key gnus-summary-goto-map "N" 'gnus-summary-next-article)
4708   (define-key gnus-summary-goto-map "P" 'gnus-summary-prev-article)
4709   (define-key gnus-summary-goto-map "\C-n" 'gnus-summary-next-same-subject)
4710   (define-key gnus-summary-goto-map "\C-p" 'gnus-summary-prev-same-subject)
4711   (define-key gnus-summary-goto-map "\M-n" 'gnus-summary-next-unread-subject)
4712   (define-key gnus-summary-goto-map "\M-p" 'gnus-summary-prev-unread-subject)
4713   (define-key gnus-summary-goto-map "f" 'gnus-summary-first-unread-article)
4714   (define-key gnus-summary-goto-map "b" 'gnus-summary-best-unread-article)
4715   (define-key gnus-summary-goto-map "g" 'gnus-summary-goto-subject)
4716   (define-key gnus-summary-goto-map "l" 'gnus-summary-goto-last-article)
4717   (define-key gnus-summary-goto-map "p" 'gnus-summary-pop-article)
4718
4719
4720   (define-prefix-command 'gnus-summary-thread-map)
4721   (define-key gnus-summary-mode-map "T" 'gnus-summary-thread-map)
4722   (define-key gnus-summary-thread-map "k" 'gnus-summary-kill-thread)
4723   (define-key gnus-summary-thread-map "l" 'gnus-summary-lower-thread)
4724   (define-key gnus-summary-thread-map "i" 'gnus-summary-raise-thread)
4725   (define-key gnus-summary-thread-map "T" 'gnus-summary-toggle-threads)
4726   (define-key gnus-summary-thread-map "s" 'gnus-summary-show-thread)
4727   (define-key gnus-summary-thread-map "S" 'gnus-summary-show-all-threads)
4728   (define-key gnus-summary-thread-map "h" 'gnus-summary-hide-thread)
4729   (define-key gnus-summary-thread-map "H" 'gnus-summary-hide-all-threads)
4730   (define-key gnus-summary-thread-map "n" 'gnus-summary-next-thread)
4731   (define-key gnus-summary-thread-map "p" 'gnus-summary-prev-thread)
4732   (define-key gnus-summary-thread-map "u" 'gnus-summary-up-thread)
4733   (define-key gnus-summary-thread-map "d" 'gnus-summary-down-thread)
4734   (define-key gnus-summary-thread-map "#" 'gnus-uu-mark-thread)
4735
4736   
4737   (define-prefix-command 'gnus-summary-exit-map)
4738   (define-key gnus-summary-mode-map "Z" 'gnus-summary-exit-map)
4739   (define-key gnus-summary-exit-map "c" 'gnus-summary-catchup-and-exit)
4740   (define-key gnus-summary-exit-map "C" 'gnus-summary-catchup-all-and-exit)
4741   (define-key gnus-summary-exit-map "E" 'gnus-summary-exit-no-update)
4742   (define-key gnus-summary-exit-map "Q" 'gnus-summary-exit)
4743   (define-key gnus-summary-exit-map "Z" 'gnus-summary-exit)
4744   (define-key gnus-summary-exit-map "n" 'gnus-summary-catchup-and-goto-next-group)
4745   (define-key gnus-summary-exit-map "R" 'gnus-summary-reselect-current-group)
4746   (define-key gnus-summary-exit-map "G" 'gnus-summary-rescan-group)
4747
4748
4749   (define-prefix-command 'gnus-summary-article-map)
4750   (define-key gnus-summary-mode-map "A" 'gnus-summary-article-map)
4751   (define-key gnus-summary-article-map " " 'gnus-summary-next-page)
4752   (define-key gnus-summary-article-map "n" 'gnus-summary-next-page)
4753   (define-key gnus-summary-article-map "\177" 'gnus-summary-prev-page)
4754   (define-key gnus-summary-article-map "p" 'gnus-summary-prev-page)
4755   (define-key gnus-summary-article-map "\r" 'gnus-summary-scroll-up)
4756   (define-key gnus-summary-article-map "<" 'gnus-summary-beginning-of-article)
4757   (define-key gnus-summary-article-map ">" 'gnus-summary-end-of-article)
4758   (define-key gnus-summary-article-map "b" 'gnus-summary-beginning-of-article)
4759   (define-key gnus-summary-article-map "e" 'gnus-summary-end-of-article)
4760   (define-key gnus-summary-article-map "^" 'gnus-summary-refer-parent-article)
4761   (define-key gnus-summary-article-map "r" 'gnus-summary-refer-parent-article)
4762   (define-key gnus-summary-article-map "w" 'gnus-summary-stop-page-breaking)
4763   (define-key gnus-summary-article-map "c" 'gnus-summary-caesar-message)
4764   (define-key gnus-summary-article-map "g" 'gnus-summary-show-article)
4765   (define-key gnus-summary-article-map "t" 'gnus-summary-toggle-header)
4766   (define-key gnus-summary-article-map "m" 'gnus-summary-toggle-mime)
4767   (define-key gnus-summary-article-map "s" 'gnus-summary-isearch-article)
4768
4769
4770   (define-prefix-command 'gnus-summary-wash-map)
4771   (define-key gnus-summary-mode-map "W" 'gnus-summary-wash-map)
4772   (define-key gnus-summary-wash-map "h" 'gnus-article-hide-headers)
4773   (define-key gnus-summary-wash-map "s" 'gnus-article-hide-signature)
4774   (define-key gnus-summary-wash-map "c" 'gnus-article-hide-citation)
4775   (define-key gnus-summary-wash-map "o" 'gnus-article-treat-overstrike)
4776   (define-key gnus-summary-wash-map "w" 'gnus-article-word-wrap)
4777   (define-key gnus-summary-wash-map "d" 'gnus-article-remove-cr)
4778   (define-key gnus-summary-wash-map "q" 'gnus-article-de-quoted-unreadable)
4779   (define-key gnus-summary-wash-map "f" 'gnus-article-display-x-face)
4780   (define-key gnus-summary-wash-map "t" 'gnus-article-date-ut)
4781   (define-key gnus-summary-wash-map "\C-t" 'gnus-article-date-local)
4782   (define-key gnus-summary-wash-map "T" 'gnus-article-date-lapsed)
4783
4784
4785   (define-prefix-command 'gnus-summary-help-map)
4786   (define-key gnus-summary-mode-map "H" 'gnus-summary-help-map)
4787   (define-key gnus-summary-help-map "v" 'gnus-version)
4788   (define-key gnus-summary-help-map "f" 'gnus-summary-fetch-faq)
4789   (define-key gnus-summary-help-map "d" 'gnus-summary-describe-group)
4790   (define-key gnus-summary-help-map "h" 'gnus-summary-describe-briefly)
4791   (define-key gnus-summary-help-map "i" 'gnus-info-find-node)
4792
4793
4794   (define-prefix-command 'gnus-summary-backend-map)
4795   (define-key gnus-summary-mode-map "B" 'gnus-summary-backend-map)
4796   (define-key gnus-summary-backend-map "e" 'gnus-summary-expire-articles)
4797   (define-key gnus-summary-backend-map "\177" 'gnus-summary-delete-article)
4798   (define-key gnus-summary-backend-map "m" 'gnus-summary-move-article)
4799   (define-key gnus-summary-backend-map "r" 'gnus-summary-respool-article)
4800   (define-key gnus-summary-backend-map "w" 'gnus-summary-edit-article)
4801   (define-key gnus-summary-backend-map "c" 'gnus-summary-copy-article)
4802
4803
4804   (define-prefix-command 'gnus-summary-save-map)
4805   (define-key gnus-summary-mode-map "O" 'gnus-summary-save-map)
4806   (define-key gnus-summary-save-map "o" 'gnus-summary-save-article)
4807   (define-key gnus-summary-save-map "m" 'gnus-summary-save-article-mail)
4808   (define-key gnus-summary-save-map "r" 'gnus-summary-save-article-rmail)
4809   (define-key gnus-summary-save-map "f" 'gnus-summary-save-article-file)
4810   (define-key gnus-summary-save-map "h" 'gnus-summary-save-article-folder)
4811   (define-key gnus-summary-save-map "p" 'gnus-summary-pipe-output)
4812
4813
4814   (define-prefix-command 'gnus-summary-extract-map)
4815   (define-key gnus-summary-mode-map "X" 'gnus-summary-extract-map)
4816 ;  (define-key gnus-summary-extract-map "x" 'gnus-summary-extract-any)
4817 ;  (define-key gnus-summary-extract-map "m" 'gnus-summary-extract-mime)
4818   (define-key gnus-summary-extract-map "u" 'gnus-uu-decode-uu)
4819   (define-key gnus-summary-extract-map "U" 'gnus-uu-decode-uu-and-save)
4820   (define-key gnus-summary-extract-map "s" 'gnus-uu-decode-unshar)
4821   (define-key gnus-summary-extract-map "S" 'gnus-uu-decode-unshar-and-save)
4822   (define-key gnus-summary-extract-map "o" 'gnus-uu-decode-save)
4823   (define-key gnus-summary-extract-map "O" 'gnus-uu-decode-save)
4824   (define-key gnus-summary-extract-map "b" 'gnus-uu-decode-binhex)
4825   (define-key gnus-summary-extract-map "B" 'gnus-uu-decode-binhex)
4826   (define-key gnus-summary-extract-map "p" 'gnus-uu-decode-postscript)
4827   (define-key gnus-summary-extract-map "P" 'gnus-uu-decode-postscript-and-save)
4828
4829
4830   (define-prefix-command 'gnus-summary-extract-view-map)
4831   (define-key gnus-summary-extract-map "v" 'gnus-summary-extract-view-map)
4832   (define-key gnus-summary-extract-view-map "u" 'gnus-uu-decode-uu-view)
4833   (define-key gnus-summary-extract-view-map "U" 'gnus-uu-decode-uu-and-save-view)
4834   (define-key gnus-summary-extract-view-map "s" 'gnus-uu-decode-unshar-view)
4835   (define-key gnus-summary-extract-view-map "S" 'gnus-uu-decode-unshar-and-save-view)
4836   (define-key gnus-summary-extract-view-map "o" 'gnus-uu-decode-save-view)
4837   (define-key gnus-summary-extract-view-map "O" 'gnus-uu-decode-save-view)
4838   (define-key gnus-summary-extract-view-map "b" 'gnus-uu-decode-binhex-view)
4839   (define-key gnus-summary-extract-view-map "B" 'gnus-uu-decode-binhex-view)
4840   (define-key gnus-summary-extract-view-map "p" 'gnus-uu-decode-postscript-view)
4841   (define-key gnus-summary-extract-view-map "P" 'gnus-uu-decode-postscript-and-save-view)
4842   
4843   
4844   (define-prefix-command 'gnus-summary-various-map)
4845   (define-key gnus-summary-mode-map "V" 'gnus-summary-various-map)
4846   (define-key gnus-summary-various-map "u" 'gnus-summary-universal-argument)
4847   (define-key gnus-summary-various-map "\C-s" 'gnus-summary-search-article-forward)
4848   (define-key gnus-summary-various-map "\C-r" 'gnus-summary-search-article-backward)
4849   (define-key gnus-summary-various-map "r" 'gnus-summary-refer-article)
4850   (define-key gnus-summary-various-map "&" 'gnus-summary-execute-command)
4851   (define-key gnus-summary-various-map "T" 'gnus-summary-toggle-truncation)
4852   (define-key gnus-summary-various-map "e" 'gnus-summary-expand-window)
4853   (define-key gnus-summary-various-map "D" 'gnus-summary-enter-digest-group)
4854   (define-key gnus-summary-various-map "k" 'gnus-summary-edit-local-kill)
4855   (define-key gnus-summary-various-map "K" 'gnus-summary-edit-global-kill)
4856
4857   (define-key gnus-summary-various-map "S" 'gnus-summary-score-map)
4858
4859   (define-prefix-command 'gnus-summary-sort-map)
4860   (define-key gnus-summary-various-map "s" 'gnus-summary-sort-map)
4861   (define-key gnus-summary-sort-map "n" 'gnus-summary-sort-by-number)
4862   (define-key gnus-summary-sort-map "a" 'gnus-summary-sort-by-author)
4863   (define-key gnus-summary-sort-map "s" 'gnus-summary-sort-by-subject)
4864   (define-key gnus-summary-sort-map "d" 'gnus-summary-sort-by-date)
4865   (define-key gnus-summary-sort-map "i" 'gnus-summary-sort-by-score)
4866
4867   (define-key gnus-summary-mode-map "I" 'gnus-summary-increase-map)
4868   (define-key gnus-summary-mode-map "L" 'gnus-summary-lower-map)
4869   )
4870
4871
4872 \f
4873
4874 (defun gnus-summary-mode ()
4875   "Major mode for reading articles.
4876
4877 All normal editing commands are switched off.
4878 \\<gnus-summary-mode-map>
4879 Each line in this buffer represents one article.  To read an
4880 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
4881 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]', 
4882 respectively.
4883
4884 You can also post articles and send mail from this buffer.  To 
4885 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author 
4886 of an article, type `\\[gnus-summary-reply]'.
4887
4888 There are approx. one gazillion commands you can execute in this 
4889 buffer; read the info pages for more information (`\\[gnus-info-find-node]'). 
4890
4891 The following commands are available:
4892
4893 \\{gnus-summary-mode-map}"
4894   (interactive)
4895   (if gnus-visual (gnus-summary-make-menu-bar))
4896   (kill-all-local-variables)
4897   (let ((locals gnus-summary-local-variables))
4898     (while locals
4899       (if (consp (car locals))
4900           (progn
4901             (make-local-variable (car (car locals)))
4902             (set (car (car locals)) (eval (cdr (car locals)))))
4903         (make-local-variable (car locals))
4904         (set (car locals) nil))
4905       (setq locals (cdr locals))))
4906   (gnus-make-thread-indent-array)
4907   (gnus-update-format-specifications)
4908   (setq mode-line-modified "-- ")
4909   (make-local-variable 'mode-line-format)
4910   (setq mode-line-format (copy-sequence mode-line-format))
4911   (and (equal (nth 3 mode-line-format) "   ")
4912        (setcar (nthcdr 3 mode-line-format) ""))
4913   (setq major-mode 'gnus-summary-mode)
4914   (setq mode-name "Summary")
4915   (make-local-variable 'minor-mode-alist)
4916   (gnus-set-mode-line 'summary)
4917   (use-local-map gnus-summary-mode-map)
4918   (buffer-disable-undo (current-buffer))
4919   (setq buffer-read-only t)             ;Disable modification
4920   (setq truncate-lines t)
4921   (setq selective-display t)
4922   (setq selective-display-ellipses t)   ;Display `...'
4923   (run-hooks 'gnus-summary-mode-hook))
4924
4925 (defun gnus-summary-clear-local-variables ()
4926   (let ((locals gnus-summary-local-variables))
4927     (while locals
4928       (if (consp (car locals))
4929           (set (car (car locals)) nil)
4930         (set (car locals) nil))
4931       (setq locals (cdr locals)))))
4932
4933 (defun gnus-mouse-pick-article (e)
4934   (interactive "e")
4935   (mouse-set-point e)
4936   (gnus-summary-next-page nil t))
4937
4938 (defun gnus-summary-setup-buffer (group)
4939   "Initialize summary buffer."
4940   (let ((buffer (concat "*Summary " group "*")))
4941     ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
4942     (setq gnus-summary-buffer (set-buffer (get-buffer-create buffer)))
4943     (gnus-add-current-to-buffer-list)
4944     (gnus-summary-mode)))
4945
4946 (defun gnus-set-global-variables ()
4947   ;; Set the global equivalents of the summary buffer-local variables
4948   ;; to the latest values they had. These reflect the summary buffer
4949   ;; that was in action when the last article was fetched.
4950   (if (eq major-mode 'gnus-summary-mode) 
4951       (progn
4952         (setq gnus-summary-buffer (current-buffer))
4953         (let ((name gnus-newsgroup-name)
4954               (marked gnus-newsgroup-marked)
4955               (unread gnus-newsgroup-unreads)
4956               (headers gnus-current-headers))
4957           (save-excursion
4958             (set-buffer gnus-group-buffer)
4959             (setq gnus-newsgroup-name name)
4960             (setq gnus-newsgroup-marked marked)
4961             (setq gnus-newsgroup-unreads unread)
4962             (setq gnus-current-headers headers))))))
4963
4964 (defun gnus-summary-insert-dummy-line (sformat subject number)
4965   (if (not sformat) 
4966       (setq sformat gnus-summary-dummy-line-format-spec))
4967   (let (b)
4968     (beginning-of-line)
4969     (setq b (point))
4970     (insert (eval sformat))
4971     (add-text-properties
4972      b (1+ b)
4973      (list 'gnus (list number ?Z 0)))))
4974
4975 (defvar gnus-thread-indent-array nil)
4976 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
4977 (defun gnus-make-thread-indent-array ()
4978   (let ((n 200))
4979     (if (and gnus-thread-indent-array
4980              (= gnus-thread-indent-level gnus-thread-indent-array-level))
4981         nil
4982       (setq gnus-thread-indent-array (make-vector 201 "")
4983             gnus-thread-indent-array-level gnus-thread-indent-level)
4984       (while (>= n 0)
4985         (aset gnus-thread-indent-array n
4986               (make-string (* n gnus-thread-indent-level) ? ))
4987         (setq n (1- n))))))
4988
4989 (defun gnus-summary-insert-line 
4990   (sformat header level current unread replied expirable subject-or-nil
4991            &optional dummy score)
4992   (or sformat (setq sformat gnus-summary-line-format-spec))
4993   (let* ((indentation (aref gnus-thread-indent-array level))
4994          (lines (header-lines header))
4995          (score (or score gnus-summary-default-score 0))
4996          (score-char (if (or (null gnus-summary-default-score)
4997                              (= score gnus-summary-default-score)) ? 
4998                        (if (< score gnus-summary-default-score) 
4999                            gnus-score-below-mark gnus-score-over-mark)))
5000          (replied (if replied gnus-replied-mark ? ))
5001          (from (header-from header))
5002          (name-address (funcall gnus-extract-address-components from))
5003          (address (car (cdr name-address)))
5004          (name (or (car name-address) (car (cdr name-address))))
5005          (subject (header-subject header))
5006          (number (header-number header))
5007          (opening-bracket (if dummy ?\< ?\[))
5008          (closing-bracket (if dummy ?\> ?\]))
5009          (buffer-read-only nil)
5010          (b (progn (beginning-of-line) (point))))
5011     (insert (eval sformat))
5012     (add-text-properties
5013      b (1+ b) (list 'gnus (list number (or unread gnus-unread-mark) level)))))
5014
5015 (defun gnus-summary-update-line (&optional dont-update)
5016   ;; Update summary line after change.
5017   (or (not gnus-summary-default-score)
5018       gnus-summary-inhibit-highlight
5019       (let ((gnus-summary-inhibit-highlight t)
5020             (article (gnus-summary-article-number)))
5021         (progn
5022           (or dont-update
5023               (if (and gnus-summary-mark-below
5024                        (< (gnus-summary-article-score)
5025                           gnus-summary-mark-below))
5026                   (and (not (memq article gnus-newsgroup-marked))
5027                        (not (memq article gnus-newsgroup-dormant))
5028                        (memq article gnus-newsgroup-unreads)
5029                        (gnus-summary-mark-article nil gnus-low-score-mark))
5030                 (and (eq (gnus-summary-article-mark) gnus-low-score-mark)
5031                      (gnus-summary-mark-article nil gnus-unread-mark))))
5032           (and gnus-visual
5033                (run-hooks 'gnus-visual-summary-update-hook))))))
5034
5035 (defun gnus-summary-update-lines ()
5036   ;; Rehighlight summary buffer according to `gnus-summary-highlight'.
5037   (and (save-excursion
5038          (set-buffer gnus-summary-buffer)
5039          (goto-char (point-min))
5040          (while (not (eobp))
5041            (gnus-summary-update-line)
5042            (forward-line 1)))))
5043
5044 (defun gnus-summary-number-of-articles-in-thread (thread &optional char)
5045   ;; Sum up all elements (and sub-elements) in a list.
5046   (let ((number 
5047          (if (listp thread) 
5048              (apply 
5049               '+ (mapcar 'gnus-summary-number-of-articles-in-thread thread))
5050            1)))
5051     (if char 
5052         (if (> number 1) gnus-not-empty-thread-mark
5053           gnus-empty-thread-mark)
5054       number)))
5055
5056 (defun gnus-summary-read-group (group &optional show-all no-article kill-buffer)
5057   "Start reading news in newsgroup GROUP.
5058 If SHOW-ALL is non-nil, already read articles are also listed.
5059 If NO-ARTICLE is non-nil, no article is selected initially."
5060   (message "Retrieving newsgroup: %s..." group)
5061   (gnus-summary-setup-buffer group)
5062   (let ((did-select (gnus-select-newsgroup group show-all))
5063         (method (car (gnus-find-method-for-group group))))
5064     (cond 
5065      ((null did-select) 
5066       (and (eq major-mode 'gnus-summary-mode)
5067            (not (equal (current-buffer) kill-buffer))
5068            (progn
5069              (kill-buffer (current-buffer))
5070              (switch-to-buffer gnus-group-buffer)
5071              (gnus-group-next-unread-group 1)))
5072       nil)
5073      ((eq did-select 'quit)
5074       (gnus-configure-windows 'summary)
5075       (gnus-configure-windows 'newsgroup)
5076       (and (eq major-mode 'gnus-summary-mode)
5077            (not (equal (current-buffer) kill-buffer))
5078            (kill-buffer (current-buffer)))
5079       (gnus-kill-buffer kill-buffer)
5080       (switch-to-buffer gnus-group-buffer)
5081       (gnus-group-next-unread-group 1)
5082       (signal 'quit nil))
5083      (t
5084       (gnus-set-global-variables)
5085       ;; Save the active value in effect when the group was entered.
5086       (setq gnus-newsgroup-active 
5087             (gnus-copy-sequence
5088              (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
5089       ;; You can change the subjects in this hook.
5090       (run-hooks 'gnus-select-group-hook)
5091       ;; Do score processing.
5092       (gnus-possibly-score-headers)
5093       ;; Update the format specifiers.
5094       (gnus-update-format-specifications)
5095       ;; Generate the summary buffer.
5096       (gnus-summary-prepare)
5097       (if (zerop (buffer-size))
5098           (cond (gnus-newsgroup-dormant
5099                  (gnus-summary-show-all-dormant))
5100                 ((and gnus-newsgroup-scored show-all)
5101                  (gnus-summary-show-all-expunged))))
5102       ;; Function `gnus-apply-kill-file' must be called in this hook.
5103       (run-hooks 'gnus-apply-kill-hook)
5104       (if (zerop (buffer-size))
5105           (progn
5106             ;; This newsgroup is empty.
5107             (gnus-summary-catchup-and-exit nil t) ;Without confirmations.
5108             (message "No unread news")
5109             (gnus-kill-buffer kill-buffer))
5110         (save-excursion
5111           (if kill-buffer
5112               (let ((gnus-summary-buffer kill-buffer))
5113                 (gnus-configure-windows 'newsgroups t))))
5114         ;; Hide conversation thread subtrees.  We cannot do this in
5115         ;; gnus-summary-prepare-hook since kill processing may not
5116         ;; work with hidden articles.
5117         (and gnus-show-threads
5118              gnus-thread-hide-subtree
5119              (gnus-summary-hide-all-threads))
5120         ;; Show first unread article if requested.
5121         (goto-char (point-min))
5122         (if (and (not no-article)
5123                  gnus-auto-select-first
5124                  (gnus-summary-first-unread-article))
5125             (gnus-configure-windows 'article)
5126           (gnus-configure-windows 'summary))
5127         (pop-to-buffer gnus-summary-buffer)
5128         (gnus-set-mode-line 'summary)
5129         (gnus-summary-position-cursor)
5130         ;; If in async mode, we send some info to the backend.
5131         (and gnus-newsgroup-async
5132              (setq gnus-newsgroup-threads (nreverse gnus-newsgroup-threads))
5133              (gnus-request-asynchronous 
5134               gnus-newsgroup-name
5135               (if (and gnus-asynchronous-article-function
5136                        (fboundp gnus-asynchronous-article-function))
5137                   (funcall gnus-asynchronous-article-function
5138                            gnus-newsgroup-threads)
5139                 gnus-newsgroup-threads)))
5140         (gnus-kill-buffer kill-buffer))))))
5141
5142 (defun gnus-summary-prepare ()
5143   ;; Generate the summary buffer.
5144   (let ((buffer-read-only nil))
5145     (erase-buffer)
5146     (gnus-summary-prepare-threads 
5147      (if gnus-show-threads
5148          (gnus-gather-threads 
5149           (gnus-sort-threads 
5150            (if gnus-summary-expunge-below
5151                (gnus-make-threads-and-expunge)
5152              (gnus-make-threads))))
5153        gnus-newsgroup-headers)
5154      0 nil nil t)
5155     ;; Erase header retrieval message.
5156     (gnus-summary-update-lines)
5157     (message "")
5158     ;; Call hooks for modifying summary buffer.
5159     ;; Suggested by sven@tde.LTH.Se (Sven Mattisson).
5160     (goto-char (point-min))
5161     (run-hooks 'gnus-summary-prepare-hook)))
5162
5163 (defun gnus-gather-threads (threads)
5164   "Gather threads that have lost their roots."
5165   (if (not gnus-summary-make-false-root)
5166       threads 
5167     (let ((hashtb (gnus-make-hashtable 1023))
5168           (prev threads)
5169           (result threads)
5170           thread subject hthread unre-subject whole-subject)
5171       (while threads
5172         (setq subject (header-subject (car (car threads)))
5173               whole-subject subject)
5174         (and gnus-summary-gather-subject-limit
5175              (or (and (numberp gnus-summary-gather-subject-limit)
5176                       (> (length subject) gnus-summary-gather-subject-limit)
5177                       (setq subject
5178                             (substring subject 0 
5179                                        gnus-summary-gather-subject-limit)))
5180                  (and (eq 'fuzzy gnus-summary-gather-subject-limit)
5181                       (setq subject (gnus-simplify-subject-fuzzy subject)))))
5182         (if (setq hthread 
5183                   (gnus-gethash 
5184                    (setq unre-subject (gnus-simplify-subject-re subject))
5185                    hashtb))
5186             (progn
5187               (or (stringp (car (car hthread)))
5188                   (setcar hthread (list whole-subject (car hthread))))
5189               (setcdr (car hthread) (nconc (cdr (car hthread)) 
5190                                            (list (car threads))))
5191               (setcdr prev (cdr threads))
5192               (setq threads prev))
5193           (gnus-sethash unre-subject threads hashtb))
5194         (setq prev threads)
5195         (setq threads (cdr threads)))
5196       result)))
5197
5198 (defun gnus-make-threads ()
5199   ;; This function takes the dependencies already made by 
5200   ;; `gnus-get-newsgroup-headers' and builds the trees. First we go
5201   ;; through the dependecies in the hash table and finds all the
5202   ;; roots. Roots do not refer back to any valid articles.
5203   (let (roots)
5204     (and gnus-fetch-old-headers (eq gnus-headers-retrieved-by 'nov)
5205          (gnus-build-old-threads))
5206     (mapatoms
5207      (lambda (refs)
5208        (if (not (car (symbol-value refs)))
5209            (setq roots (append (cdr (symbol-value refs)) roots))
5210          ;; Ok, these refer back to valid articles, but if
5211          ;; `gnus-thread-ignore-subject' is nil, we have to check that
5212          ;; the root has the same subject as its children. The children
5213          ;; that do not are made into roots and removed from the list
5214          ;; of children. 
5215          (or gnus-thread-ignore-subject
5216              (let* ((prev (symbol-value refs))
5217                     (subject (gnus-simplify-subject-re 
5218                               (header-subject (car prev))))
5219                     (headers (cdr prev)))
5220                (while headers
5221                  (if (not (string= subject
5222                                    (gnus-simplify-subject-re 
5223                                     (header-subject (car headers)))))
5224                      (progn
5225                        (setq roots (cons (car headers) roots))
5226                        (setcdr prev (cdr headers)))
5227                    (setq prev headers))
5228                  (setq headers (cdr headers)))))))
5229      gnus-newsgroup-dependencies)
5230     
5231     (mapcar 'gnus-trim-thread
5232             (apply 'append
5233                    (mapcar 'gnus-cut-thread
5234                            (mapcar 'gnus-make-sub-thread roots))))))
5235   
5236 (defun gnus-make-threads-and-expunge ()
5237   ;; This function takes the dependencies already made by 
5238   ;; `gnus-get-newsgroup-headers' and builds the trees. First we go
5239   ;; through the dependecies in the hash table and finds all the
5240   ;; roots. Roots do not refer back to any valid articles.
5241   (let (roots)
5242     (and gnus-fetch-old-headers (eq gnus-headers-retrieved-by 'nov)
5243          (gnus-build-old-threads))
5244     (mapatoms
5245      (lambda (refs)
5246        (if (not (car (symbol-value refs)))
5247            (if (and gnus-summary-expunge-below
5248                     (not gnus-fetch-old-headers))
5249                (let ((headers (cdr (symbol-value refs))))
5250                  (while headers
5251                   (if (not (< (or (cdr (assq (header-number (car headers))
5252                                              gnus-newsgroup-scored))
5253                                   gnus-summary-default-score 0)
5254                               gnus-summary-expunge-below))
5255                       (setq roots (cons (car headers) roots))
5256                     (setq gnus-newsgroup-unreads
5257                           (delq (header-number (car headers))
5258                                 gnus-newsgroup-unreads)))
5259                   (setq headers (cdr headers))))
5260              (setq roots (append (cdr (symbol-value refs)) roots)))
5261          ;; Ok, these refer back to valid articles, but if
5262          ;; `gnus-thread-ignore-subject' is nil, we have to check that
5263          ;; the root has the same subject as its children. The children
5264          ;; that do not are made into roots and removed from the list
5265          ;; of children. 
5266          (or gnus-thread-ignore-subject
5267              (let* ((prev (symbol-value refs))
5268                     (subject (gnus-simplify-subject-re 
5269                               (header-subject (car prev))))
5270                     (headers (cdr prev)))
5271                (while headers
5272                  (if (not (string= subject
5273                                    (gnus-simplify-subject-re 
5274                                     (header-subject (car headers)))))
5275                      (progn
5276                        (setq roots (cons (car headers) roots))
5277                        (setcdr prev (cdr headers)))
5278                    (setq prev headers))
5279                  (setq headers (cdr headers)))))
5280          (and gnus-summary-expunge-below
5281               (not gnus-fetch-old-headers)
5282               (let* ((prev (symbol-value refs))
5283                      (headers (cdr prev))
5284                      id)
5285                 (while headers
5286                   (if (not (< (or (cdr (assq (header-number (car headers))
5287                                              gnus-newsgroup-scored))
5288                                   gnus-summary-default-score 0)
5289                               gnus-summary-expunge-below))
5290                       (setq prev (cdr prev))
5291                     (setq gnus-newsgroup-unreads 
5292                           (delq (header-number (car headers))
5293                                 gnus-newsgroup-unreads))
5294                     (setcdr prev (cdr headers))
5295                     (setq id (gnus-gethash (header-id (car headers))
5296                                            gnus-newsgroup-dependencies))
5297                     (let ((h (cdr id)))
5298                       (while h
5299                         (if (not (< (or (cdr (assq (header-number (car h))
5300                                                    gnus-newsgroup-scored))
5301                                         gnus-summary-default-score 0)
5302                                     gnus-summary-expunge-below))
5303                             (setq roots (cons (car h) roots)))
5304                         (setq h (cdr h)))))
5305                   (setq headers (cdr headers)))))))
5306      gnus-newsgroup-dependencies)
5307     
5308     (mapcar 'gnus-trim-thread
5309             (apply 'append
5310                    (mapcar 'gnus-cut-thread
5311                            (mapcar 'gnus-make-sub-thread roots))))))
5312   
5313 (defun gnus-cut-thread (thread)
5314   ;; Remove leaf dormant or ancient articles from THREAD.
5315   (let ((head (car thread))
5316         (tail (apply 'append (mapcar 'gnus-cut-thread (cdr thread)))))
5317     (if (and (null tail)
5318              (let ((number (header-number head)))
5319                (or (memq number gnus-newsgroup-ancient)
5320                    (memq number gnus-newsgroup-dormant)
5321                    (and gnus-summary-expunge-below
5322                         (eq gnus-fetch-old-headers 'some)
5323                         (< (or (cdr (assq number gnus-newsgroup-scored))
5324                                gnus-summary-default-score 0)
5325                            gnus-summary-expunge-below)
5326                         (progn
5327                           (setq gnus-newsgroup-unreads
5328                                 (delq number gnus-newsgroup-unreads))
5329                           t)))))
5330         nil
5331       (list (cons head tail)))))
5332
5333 (defun gnus-trim-thread (thread)
5334   ;; Remove root ancient articles with only one child from THREAD.
5335   (if (and (eq gnus-fetch-old-headers 'some)
5336            (memq (header-number (car thread)) gnus-newsgroup-ancient)
5337            (= (length thread) 2))
5338       (gnus-trim-thread (nth 1 thread))
5339     thread))
5340
5341 (defun gnus-make-sub-thread (root)
5342   ;; This function makes a sub-tree for a node in the tree.
5343   (let ((children (reverse (cdr (gnus-gethash (downcase (header-id root))
5344                                               gnus-newsgroup-dependencies)))))
5345     (cons root (mapcar 'gnus-make-sub-thread children))))
5346
5347 (defun gnus-build-old-threads ()
5348   ;; Look at all the articles that refer back to old articles, and
5349   ;; fetch the headers for the articles that aren't there. This will
5350   ;; build complete threads - if the roots haven't been expired by the
5351   ;; server, that is.
5352   (let (id heads)
5353     (mapatoms
5354      (lambda (refs)
5355        (if (not (car (symbol-value refs)))
5356            (progn
5357              (setq heads (cdr (symbol-value refs)))
5358              (while heads
5359                (if (not (memq (header-number (car heads))
5360                               gnus-newsgroup-dormant))
5361                    (progn
5362                      (setq id (symbol-name refs))
5363                      (while (and (setq id (gnus-build-get-header id))
5364                                  (not (car (gnus-gethash 
5365                                             id gnus-newsgroup-dependencies)))))
5366                      (setq heads nil))
5367                  (setq heads (cdr heads)))))))
5368      gnus-newsgroup-dependencies)))
5369
5370 (defun gnus-build-get-header (id)
5371   ;; Look through the buffer of NOV lines and find the header to
5372   ;; ID. Enter this line into the dependencies hash table, and return
5373   ;; the id of the parent article (if any).
5374   (let ((deps gnus-newsgroup-dependencies)
5375         found header)
5376     (prog1
5377         (save-excursion
5378           (set-buffer nntp-server-buffer)
5379           (goto-char (point-min))
5380           (while (and (not found) (search-forward id nil t))
5381             (beginning-of-line)
5382             (setq found (looking-at (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
5383                                             (regexp-quote id))))
5384             (or found (beginning-of-line 2)))
5385           (if found
5386               (let (ref)
5387                 (beginning-of-line)
5388                 (and
5389                  (setq header (gnus-nov-parse-line 
5390                                (read (current-buffer)) deps))
5391                  (setq ref (header-references header))
5392                  (string-match "\\(<[^>]+>\\) *$" ref)
5393                  (substring ref (match-beginning 1) (match-end 1))))))
5394       (and header
5395            (setq gnus-newsgroup-headers (cons header gnus-newsgroup-headers)
5396                  gnus-newsgroup-ancient (cons (header-number header)
5397                                               gnus-newsgroup-ancient))))))
5398
5399 (defun gnus-sort-threads (threads)
5400   ;; Sort threads as specified in `gnus-thread-sort-functions'.
5401   (let ((fun gnus-thread-sort-functions))
5402     (while fun
5403       (setq threads (sort threads (car fun))
5404             fun (cdr fun))))
5405   threads)
5406
5407 (defun gnus-thread-header (thread)
5408   ;; Return header of first article in THREAD.
5409   (if (consp thread)
5410       (if (stringp (car thread))
5411           (car (car (cdr thread)))
5412         (car thread))
5413     thread))
5414
5415 (defun gnus-thread-sort-by-number (h1 h2)
5416   "Sort threads by root article number."
5417   (let ((h1 (gnus-thread-header h1))
5418         (h2 (gnus-thread-header h2)))
5419     (< (header-number h1) (header-number h2))))
5420
5421 (defun gnus-thread-sort-by-author (h1 h2)
5422   "Sort threads by root author."
5423   (let ((h1 (gnus-thread-header h1))
5424         (h2 (gnus-thread-header h2)))
5425     (string-lessp
5426      (let ((extract (funcall 
5427                      gnus-extract-address-components (header-from h1))))
5428        (or (car extract) (cdr extract)))
5429      (let ((extract (funcall
5430                      gnus-extract-address-components (header-from h2))))
5431        (or (car extract) (cdr extract))))))
5432
5433 (defun gnus-thread-sort-by-subject (h1 h2)
5434   "Sort threads by root subject."
5435   (let ((h1 (gnus-thread-header h1))
5436         (h2 (gnus-thread-header h2)))
5437     (string-lessp
5438      (downcase (gnus-simplify-subject (header-subject h1)))
5439      (downcase (gnus-simplify-subject (header-subject h2))))))
5440
5441 (defun gnus-thread-sort-by-date (h1 h2)
5442   "Sort threads by root article date."
5443   (let ((h1 (gnus-thread-header h1))
5444         (h2 (gnus-thread-header h2)))
5445     (string-lessp
5446      (gnus-sortable-date (header-date h1))
5447      (gnus-sortable-date (header-date h2)))))
5448
5449 (defun gnus-thread-sort-by-score (h1 h2)
5450   "Sort threads by root article score.
5451 Unscored articles will be counted as havin a score of zero."
5452   (let ((h1 (gnus-thread-header h1))
5453         (h2 (gnus-thread-header h2)))
5454     (let ((s1 (assq (header-number h1) gnus-newsgroup-scored))
5455           (s2 (assq (header-number h2) gnus-newsgroup-scored)))
5456       (> (or (cdr s1) gnus-summary-default-score 0)
5457          (or (cdr s2) gnus-summary-default-score 0)))))
5458
5459 (defun gnus-thread-sort-by-total-score (h1 h2)
5460   "Sort threads by the sum of all scores in the thread.
5461 Unscored articles will be counted as havin a score of zero."
5462   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
5463
5464 (defun gnus-thread-total-score (thread)
5465   ;;  This function find the total score of  THREAD.
5466   (if (consp thread)
5467       (if (stringp (car thread))
5468           (apply gnus-thread-score-function 0
5469                  (mapcar 'gnus-thread-total-score-1 (cdr thread)))
5470         (gnus-thread-total-score-1 thread))
5471     (gnus-thread-total-score-1 (list thread))))
5472
5473 (defun gnus-thread-total-score-1 (root)
5474   ;; This function find the total score of the thread below ROOT.
5475   (setq root (car root))
5476   (apply gnus-thread-score-function
5477          (or (cdr (assq (header-number root) gnus-newsgroup-scored))
5478              gnus-summary-default-score 0)
5479          (mapcar 'gnus-thread-total-score
5480                  (cdr (gnus-gethash (downcase (header-id root))
5481                                     gnus-newsgroup-dependencies)))))
5482
5483 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
5484 (defvar gnus-tmp-prev-subject "")
5485
5486 ;; Basic ideas by Paul Dworkin <paul@media-lab.media.mit.edu>.
5487 (defun gnus-summary-prepare-threads 
5488   (threads level &optional not-child no-subject cull)
5489   "Prepare summary buffer from THREADS and indentation LEVEL.  
5490 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'  
5491 or a straight list of headers."
5492   (let (thread header number subject clevel)
5493     (while threads
5494       (setq thread (car threads)
5495             threads (cdr threads))
5496       ;; If `thread' is a cons, hierarchical threads are used.  If not,
5497       ;; `thread' is the header.
5498       (if (consp thread)
5499           (setq header (car thread))
5500         (setq header thread)
5501         (and cull
5502              (or (memq (setq number (header-number header))
5503                        gnus-newsgroup-dormant)
5504                  (and gnus-summary-expunge-below
5505                       (< (or (cdr (assq number gnus-newsgroup-scored))
5506                              gnus-summary-default-score 0)
5507                          gnus-summary-expunge-below)))
5508              (progn
5509                (setq header nil)
5510                (setq gnus-newsgroup-unreads 
5511                      (delq number gnus-newsgroup-unreads)))))
5512       (cond 
5513        ((stringp header)
5514         ;; The header is a dummy root.
5515         (cond ((eq gnus-summary-make-false-root 'adopt)
5516                ;; We let the first article adopt the rest.
5517                (gnus-summary-prepare-threads (list (car (cdr thread))) 0)
5518                (setq thread (cdr (cdr thread)))
5519                (while thread
5520                  (gnus-summary-prepare-threads (list (car thread)) 1 t)
5521                  (setq thread (cdr thread))))
5522               ((eq gnus-summary-make-false-root 'dummy)
5523                ;; We output a dummy root.
5524                (gnus-summary-insert-dummy-line 
5525                 nil header (header-number (car (car (cdr thread)))))
5526                (setq clevel 1))
5527               ((eq gnus-summary-make-false-root 'empty)
5528                ;; We print the articles with empty subject fields. 
5529                (gnus-summary-prepare-threads (list (car (cdr thread))) 0)
5530                (setq thread (cdr (cdr thread)))
5531                (while thread
5532                  (gnus-summary-prepare-threads 
5533                   (list (car thread)) 0 nil
5534                   (not (and (eq gnus-summary-gather-subject-limit 'fuzzy)
5535                             (not (string=  
5536                                   (gnus-simplify-subject-re 
5537                                    (header-subject (car (car thread))))
5538                                   (gnus-simplify-subject-re header))))))
5539                  (setq thread (cdr thread))))
5540               (t
5541                ;; We do not make a root for the gathered
5542                ;; sub-threads at all.  
5543                (setq clevel 0)))
5544         ;; Print the sub-threads.
5545         (and (consp thread) (cdr thread)
5546              (gnus-summary-prepare-threads (cdr thread) clevel)))
5547        ;; The header is a real article.
5548        (header
5549         (setq number (header-number header)
5550               subject (header-subject header))
5551         (and gnus-newsgroup-async
5552              (setq gnus-newsgroup-threads
5553                    (cons (cons (header-number header)
5554                                (header-lines header)) gnus-newsgroup-threads)))
5555         (gnus-summary-insert-line
5556          nil header level nil 
5557          (cond ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
5558                ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
5559                ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
5560                ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
5561                (t gnus-ancient-mark))
5562          (memq number gnus-newsgroup-replied)
5563          (memq number gnus-newsgroup-expirable)
5564          (if no-subject 
5565              gnus-summary-same-subject
5566            (if (or (zerop level)
5567                    (and gnus-thread-ignore-subject
5568                         (not (string= 
5569                               (gnus-simplify-subject-re gnus-tmp-prev-subject)
5570                               (gnus-simplify-subject-re subject)))))
5571                subject
5572              gnus-summary-same-subject))
5573          not-child
5574          (cdr (assq number gnus-newsgroup-scored)))
5575         (setq gnus-tmp-prev-subject subject)
5576         ;; Recursively print subthreads.
5577         (and (consp thread) (cdr thread)
5578              (gnus-summary-prepare-threads (cdr thread) (1+ level))))))))
5579
5580 (defun gnus-select-newsgroup (group &optional read-all)
5581   "Select newsgroup GROUP.
5582 If READ-ALL is non-nil, all articles in the group are selected."
5583   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5584          (info (nth 2 entry))
5585          articles header-marks)
5586     (gnus-check-news-server
5587      (setq gnus-current-select-method (gnus-find-method-for-group group)))
5588
5589     (or (gnus-server-opened gnus-current-select-method)
5590         (gnus-open-server gnus-current-select-method)
5591         (error "Couldn't open server"))
5592     
5593     (or (and (eq (car entry) t)
5594              (gnus-activate-newsgroup (car info)))
5595         (gnus-request-group group t)
5596         (progn
5597           (kill-buffer (current-buffer))
5598           (error "Couldn't request group %s: %s" 
5599                  group (gnus-status-message group))))
5600
5601     (setq gnus-newsgroup-name group)
5602     (setq gnus-newsgroup-unselected nil)
5603     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5604
5605     (and gnus-asynchronous
5606          (gnus-check-backend-function 
5607           'request-asynchronous gnus-newsgroup-name)
5608          (setq gnus-newsgroup-async
5609                (gnus-request-asynchronous gnus-newsgroup-name)))
5610
5611     (setq articles (gnus-articles-to-read group read-all))
5612
5613     (cond 
5614      ((null articles) 
5615       (message "Couldn't select newsgroup")
5616       'quit)
5617      ((eq articles 0) nil)
5618      (t
5619       ;; Init the dependencies hash table.
5620       (setq gnus-newsgroup-dependencies 
5621             (gnus-make-hashtable (length articles)))
5622       ;; Retrieve the headers and read them in.
5623       (setq gnus-newsgroup-headers 
5624             (if (eq 'nov (setq gnus-headers-retrieved-by
5625                                (gnus-retrieve-headers 
5626                                 (if gnus-fetch-old-headers 
5627                                     (cons 1 articles) articles) 
5628                                 gnus-newsgroup-name)))
5629                 (progn
5630                   (gnus-get-newsgroup-headers-xover articles))
5631               (gnus-get-newsgroup-headers)))
5632       ;; If we were to fetch old headers, but the backend didn't
5633       ;; support XOVER, then it is possible we fetched one article
5634       ;; that we shouldn't have. If that's the case, we pop it off the
5635       ;; list of headers.
5636       (and (not (eq gnus-headers-retrieved-by 'nov))
5637            gnus-fetch-old-headers
5638            gnus-newsgroup-headers
5639            (/= (header-number (car gnus-newsgroup-headers)) (car articles))
5640            (setq gnus-newsgroup-headers (cdr gnus-newsgroup-headers)))
5641       ;; Remove cancelled articles from the list of unread articles.
5642       (setq gnus-newsgroup-unreads
5643             (gnus-set-sorted-intersection 
5644              gnus-newsgroup-unreads
5645              (mapcar (lambda (headers) (header-number headers))
5646                      gnus-newsgroup-headers)))
5647       ;; Adjust and set lists of article marks.
5648       (and info
5649            (let (marked)
5650              (gnus-adjust-marked-articles info)
5651              (setq gnus-newsgroup-marked 
5652                    (cdr (assq 'tick (setq marked (nth 3 info)))))
5653              (setq gnus-newsgroup-replied (cdr (assq 'reply marked)))
5654              (setq gnus-newsgroup-expirable (cdr (assq 'expire marked)))
5655              (setq gnus-newsgroup-killed (cdr (assq 'killed marked)))
5656              (setq gnus-newsgroup-bookmarks (cdr (assq 'bookmark marked)))
5657              (setq gnus-newsgroup-dormant (cdr (assq 'dormant marked)))
5658              (setq gnus-newsgroup-scored (cdr (assq 'score marked)))
5659              (setq gnus-newsgroup-processable nil)))
5660       ;; Check whether auto-expire is to be done in this group.
5661       (setq gnus-newsgroup-auto-expire
5662             (or (and (stringp gnus-auto-expirable-newsgroups)
5663                      (string-match gnus-auto-expirable-newsgroups group))
5664                 (memq 'auto-expire (nth 5 info))))
5665       ;; First and last article in this newsgroup.
5666       (and gnus-newsgroup-headers
5667            (setq gnus-newsgroup-begin 
5668                  (header-number (car gnus-newsgroup-headers)))
5669            (setq gnus-newsgroup-end
5670                  (header-number (gnus-last-element gnus-newsgroup-headers))))
5671       (setq gnus-reffed-article-number -1)
5672       ;; GROUP is successfully selected.
5673       (or gnus-newsgroup-headers t)))))
5674
5675 (defun gnus-articles-to-read (group read-all)
5676   ;; Find out what articles the user wants to read.
5677   (let* ((articles
5678           ;; Select all articles if `read-all' is non-nil, or if all the
5679           ;; unread articles are dormant articles.
5680           (if (or read-all
5681                   (= (length gnus-newsgroup-unreads) 
5682                      (length gnus-newsgroup-dormant)))
5683               (gnus-uncompress-range 
5684                (gnus-gethash group gnus-active-hashtb))
5685             gnus-newsgroup-unreads))
5686          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5687          (scored (length scored-list))
5688          (number (length articles))
5689          (marked (+ (length gnus-newsgroup-marked)
5690                     (length gnus-newsgroup-dormant)))
5691          (select
5692           (condition-case ()
5693               (cond ((and (or (<= scored marked)
5694                               (= scored number))
5695                           (numberp gnus-large-newsgroup)
5696                           (> number gnus-large-newsgroup))
5697                      (let ((input
5698                             (read-string
5699                              (format
5700                               "How many articles from %s (default %d): "
5701                               gnus-newsgroup-name number))))
5702                        (if (string-equal input "")
5703                            number input)))
5704                     ((and (> scored marked) (< scored number))
5705                      (let ((input
5706                             (read-string
5707                              (format 
5708                               "%s %s (%d scored, %d total): "
5709                               "How many articles from"
5710                               group scored number))))
5711                        (if (string-equal input "")
5712                            number input)))
5713                     (t number))
5714             (quit nil)))
5715          total-articles)
5716     (setq select (if (stringp select) (string-to-number select) select))
5717     (if (or (null select) (zerop select))
5718         select
5719       (if (and (not (zerop scored)) (<= (abs select) scored))
5720           (progn
5721             (setq articles (sort scored-list '<))
5722             (setq number (length articles)))
5723         (setq articles (copy-sequence articles)))
5724
5725       (setq total-articles articles)
5726       
5727       (if (< (abs select) number)
5728           (if (< select 0) 
5729               ;; Select the N oldest articles.
5730               (setcdr (nthcdr (1- (abs select)) articles) nil)
5731             ;; Select the N most recent articles.
5732             (setq articles (nthcdr (- number select) articles))))
5733       (setq gnus-newsgroup-unselected
5734             (gnus-sorted-intersection
5735              gnus-newsgroup-unreads
5736              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
5737       articles)))
5738
5739 (defun gnus-killed-articles (killed articles)
5740   (let (out)
5741     (while articles
5742       (if (inline (gnus-member-of-range (car articles) killed))
5743           (setq out (cons (car articles) out)))
5744       (setq articles (cdr articles)))
5745     out))
5746
5747 (defun gnus-adjust-marked-articles (info &optional active)
5748   "Remove all marked articles that are no longer legal."
5749   (let ((marked-lists (nth 3 info))
5750         (active (or active (gnus-gethash (car info) gnus-active-hashtb)))
5751         marked m prev)
5752     ;; There are many types of marked articles.
5753     (while marked-lists
5754       (setq m (cdr (setq prev (car marked-lists))))
5755       (cond ((or (eq 'tick (car prev)) (eq 'dormant (car prev)))
5756              ;; Make sure that all ticked articles are a subset of the
5757              ;; unread/unselected articles.
5758              (while m
5759                (if (or (memq (car m) gnus-newsgroup-unreads)
5760                        (memq (car m) gnus-newsgroup-unselected))
5761                    (setq prev m)
5762                  (setcdr prev (cdr m)))
5763                (setq m (cdr m))))
5764             ((eq 'score (car prev))
5765              ;; Scored articles should be a subset of
5766              ;; unread/unselected articles. 
5767              (while m
5768                (if (or (memq (car (car m)) gnus-newsgroup-unreads)
5769                        (memq (car (car m)) gnus-newsgroup-unreads))
5770                    (setq prev m)
5771                  (setcdr prev (cdr m)))
5772                (setq m (cdr m))))
5773             ((eq 'bookmark (car prev))
5774              ;; Bookmarks should be a subset of active articles.
5775              (while m
5776                (if (< (car (car m)) (car active))
5777                    (setcdr prev (cdr m))
5778                  (setq prev m))
5779                (setq m (cdr m))))
5780             ((eq 'killed (car prev))
5781              ;; Articles that have been through the kill process are
5782              ;; to be a subset of active articles.
5783              (while (and m (< (or (and (numberp (car m)) (car m))
5784                                   (cdr (car m)))
5785                               (car active)))
5786                (setcdr prev (cdr m))
5787                (setq m (cdr m)))
5788              (if (and m (< (or (and (numberp (car m)) (car m))
5789                                (car (car m)))
5790                            (car active))) 
5791                  (setcar (if (numberp (car m)) m (car m)) (car active))))
5792             ((or (eq 'reply (car prev)) (eq 'expire (car prev)))
5793              ;; The replied and expirable articles have to be articles
5794              ;; that are active. 
5795              (while m
5796                (if (< (car m) (car active))
5797                    (setcdr prev (cdr m))
5798                  (setq prev m))
5799                (setq m (cdr m)))))
5800       (setq marked-lists (cdr marked-lists)))
5801     ;; Remove all lists that are empty.
5802     (setq marked-lists (nth 3 info))
5803     (if marked-lists
5804         (progn
5805           (while (= 1 (length (car marked-lists)))
5806             (setq marked-lists (cdr marked-lists)))
5807           (setq m (cdr (setq prev marked-lists)))
5808           (while m
5809             (if (= 1 (length (car m)))
5810                 (setcdr prev (cdr m))
5811               (setq prev m))
5812             (setq m (cdr m)))
5813           (setcar (nthcdr 3 info) marked-lists)))
5814     ;; Finally, if there are no marked lists at all left, and if there
5815     ;; are no elements after the lists in the info list, we just chop
5816     ;; the info list off before the marked lists.
5817     (and (null marked-lists) 
5818          (not (nthcdr 4 info))
5819          (setcdr (nthcdr 2 info) nil)))
5820   info)
5821
5822 (defun gnus-set-marked-articles 
5823   (info ticked replied expirable killed dormant bookmark score) 
5824   "Enter the various lists of marked articles into the newsgroup info list."
5825   (let (newmarked)
5826     (and ticked (setq newmarked (cons (cons 'tick ticked) nil)))
5827     (and replied (setq newmarked (cons (cons 'reply replied) newmarked)))
5828     (and expirable (setq newmarked (cons (cons 'expire expirable) 
5829                                          newmarked)))
5830     (and killed (setq newmarked (cons (cons 'killed killed) newmarked)))
5831     (and dormant (setq newmarked (cons (cons 'dormant dormant) newmarked)))
5832     (and bookmark (setq newmarked (cons (cons 'bookmark bookmark) 
5833                                         newmarked)))
5834     (and score (setq newmarked (cons (cons 'score score) newmarked)))
5835     (if (nthcdr 3 info)
5836         (if newmarked
5837             (setcar (nthcdr 3 info) newmarked)
5838           (if (not (nthcdr 4 info))
5839               (setcdr (nthcdr 2 info) nil)
5840             (setcar (nthcdr 3 info) nil)))
5841       (if newmarked
5842           (setcdr (nthcdr 2 info) (cons newmarked nil))))))
5843
5844 (defun gnus-add-marked-articles (group type articles &optional info force)
5845   ;; Add ARTICLES of TYPE to the info of GROUP.
5846   ;; If INFO is non-nil, use that info. If FORCE is non-nil, don't
5847   ;; add, but replace marked articles of TYPE with ARTICLES.
5848   (let ((info (or info (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
5849         marked m)
5850     (or (not info)
5851         (and (not (setq marked (nthcdr 3 info)))
5852              (setcdr (nthcdr 2 info) (list (list (cons type articles)))))
5853         (and (not (setq m (assq type (car marked))))
5854              (setcar marked (cons (cons type articles) (car marked))))
5855         (if force
5856             (setcdr m articles)
5857           (nconc m articles)))))
5858          
5859 (defun gnus-set-mode-line (where)
5860   "This function sets the mode line of the article or summary buffers.
5861 If WHERE is `summary', the summary mode line format will be used."
5862   (if (memq where gnus-updated-mode-lines)
5863       (let (mode-string)
5864         (save-excursion
5865           (set-buffer gnus-summary-buffer)
5866           (let* ((mformat (if (eq where 'article) 
5867                               gnus-article-mode-line-format-spec
5868                             gnus-summary-mode-line-format-spec))
5869                  (group-name gnus-newsgroup-name)
5870                  (article-number (or gnus-current-article 0))
5871                  (unread (- (length gnus-newsgroup-unreads)
5872                             (length gnus-newsgroup-dormant)))
5873                  (unread-and-unticked 
5874                   (- unread (length gnus-newsgroup-marked)))
5875                  (unselected (length gnus-newsgroup-unselected))
5876                  (unread-and-unselected
5877                   (cond ((and (zerop unread-and-unticked)
5878                               (zerop unselected)) "")
5879                         ((zerop unselected) 
5880                          (format "{%d more}" unread-and-unticked))
5881                         (t (format "{%d(+%d) more}"
5882                                    unread-and-unticked unselected))))
5883                  (subject
5884                   (if gnus-current-headers
5885                       (header-subject gnus-current-headers) ""))
5886                  (max-len (and gnus-mode-non-string-length
5887                                (- (frame-width) gnus-mode-non-string-length)))
5888                  header) ;; passed as argument to any user-format-funcs
5889             (setq mode-string (eval mformat))
5890             (or (numberp max-len)
5891                 (setq max-len (length mode-string)))
5892             (if (< max-len 4) (setq max-len 4))
5893             (if (> (length mode-string) max-len)
5894                 (setq mode-string 
5895                       (concat (substring mode-string 0 (- max-len 3))
5896                               "...")))
5897             (setq mode-string (format (format "%%-%ds" max-len)
5898                                       mode-string))))
5899         (setq mode-line-buffer-identification mode-string)
5900         (set-buffer-modified-p t))))
5901
5902 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5903   "Go through the HEADERS list and add all Xrefs to a hash table.
5904 The resulting hash table is returned, or nil if no Xrefs were found."
5905   (let* ((from-method (gnus-find-method-for-group from-newsgroup))
5906          (prefix (if (and 
5907                       (gnus-group-foreign-p from-newsgroup)
5908                       (not (memq 'virtual 
5909                                  (assoc (symbol-name (car from-method))
5910                                         gnus-valid-select-methods))))
5911                      (gnus-group-real-prefix from-newsgroup)))
5912          (xref-hashtb (make-vector 63 0))
5913          start group entry number xrefs header)
5914     (while headers
5915       (setq header (car headers))
5916       (if (and (setq xrefs (header-xref header))
5917                (not (memq (header-number header) unreads)))
5918           (progn
5919             (setq start 0)
5920             (while (string-match "\\([^ ]+\\):\\([0-9]+\\)" xrefs start)
5921               (setq start (match-end 0))
5922               (setq group (concat prefix (substring xrefs (match-beginning 1) 
5923                                                     (match-end 1))))
5924               (setq number 
5925                     (string-to-int (substring xrefs (match-beginning 2) 
5926                                               (match-end 2))))
5927               (if (setq entry (gnus-gethash group xref-hashtb))
5928                   (setcdr entry (cons number (cdr entry)))
5929                 (gnus-sethash group (cons number nil) xref-hashtb)))))
5930       (setq headers (cdr headers)))
5931     (if start xref-hashtb nil)))
5932
5933 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads expirable)
5934   "Look through all the headers and mark the Xrefs as read."
5935   (let ((virtual (memq 'virtual 
5936                        (assoc (symbol-name (car (gnus-find-method-for-group 
5937                                                  from-newsgroup)))
5938                               gnus-valid-select-methods)))
5939         name entry read info xref-hashtb idlist active num range exps method
5940         nth4)
5941     (save-excursion
5942       (set-buffer gnus-group-buffer)
5943       (if (setq xref-hashtb 
5944                 (gnus-create-xref-hashtb from-newsgroup headers unreads))
5945           (mapatoms 
5946            (lambda (group)
5947              (if (string= from-newsgroup (setq name (symbol-name group)))
5948                  ()
5949                (setq idlist (symbol-value group))
5950                ;; Dead groups are not updated.
5951                (if (and (prog1 
5952                             (setq entry (gnus-gethash name gnus-newsrc-hashtb)
5953                                   info (nth 2 entry))
5954                           (if (stringp (setq nth4 (nth 4 info)))
5955                               (setq nth4 (gnus-server-to-method nth4))))
5956                         ;; Only do the xrefs if the group has the same
5957                         ;; select method as the group we have just read.
5958                         (or (gnus-methods-equal-p 
5959                              nth4 (gnus-find-method-for-group from-newsgroup))
5960                             virtual
5961                             (equal nth4 
5962                                    (setq method (gnus-find-method-for-group 
5963                                                  from-newsgroup)))
5964                             (and (equal (car nth4) (car method))
5965                                  (equal (nth 1 nth4) (nth 1 method))))
5966                         gnus-use-cross-reference
5967                         (or (not (eq gnus-use-cross-reference t))
5968                             virtual
5969                             ;; Only do cross-references on subscribed
5970                             ;; groups, if that is what is wanted.  
5971                             (<= (nth 1 info) gnus-level-subscribed)))
5972                    (progn
5973                      (setq num 0)
5974                      ;; Set the new list of read articles in this group.
5975                      (setq active (gnus-gethash name gnus-active-hashtb))
5976                      ;; First peel off all illegal article numbers.
5977                      (if active
5978                          (let ((ids idlist)
5979                                (ticked (cdr (assq 'tick (nth 3 info))))
5980                                (dormant (cdr (assq 'dormant (nth 3 info))))
5981                                id)
5982                            (setq exps nil)
5983                            (while ids
5984                              (setq id (car ids))
5985                              (if (or (> id (cdr active))
5986                                      (< id (car active))
5987                                      (memq id ticked)
5988                                      (memq id dormant))
5989                                  (setq idlist (delq id idlist)))
5990                              (and (memq id expirable)
5991                                   (setq exps (cons id exps)))
5992                              (setq ids (cdr ids)))))
5993                      ;; Update expirable articles.
5994                      (gnus-add-marked-articles nil 'expirable exps info)
5995                      (and (null (nth 2 info))
5996                           (> (car active) 1)
5997                           (setcar (nthcdr 2 info) (cons 1 (1- (car active)))))
5998                      (setcar (nthcdr 2 info)
5999                              (setq range
6000                                    (gnus-add-to-range 
6001                                     (nth 2 info) 
6002                                     (setq idlist (sort idlist '<)))))
6003                      ;; Then we have to re-compute how many unread
6004                      ;; articles there are in this group.
6005                      (if active
6006                          (progn
6007                            (cond 
6008                             ((not range)
6009                              (setq num (- (1+ (cdr active)) (car active))))
6010                             ((not (listp (cdr range)))
6011                              (setq num (- (cdr active) (- (1+ (cdr range)) 
6012                                                           (car range)))))
6013                             (t
6014                              (while range
6015                                (if (numberp (car range))
6016                                    (setq num (1+ num))
6017                                  (setq num (+ num (- (1+ (cdr (car range)))
6018                                                      (car (car range))))))
6019                                (setq range (cdr range)))
6020                              (setq num (- (cdr active) num))))
6021                            ;; Update the number of unread articles.
6022                            (setcar 
6023                             entry 
6024                             (max 0 (- num 
6025                                       (length (cdr (assq 'tick (nth 3 info))))
6026                                       (length 
6027                                        (cdr (assq 'dormant (nth 3 info)))))))
6028                            ;; Update the group buffer.
6029                            (gnus-group-update-group name t)))))))
6030            xref-hashtb)))))
6031
6032 (defun gnus-methods-equal-p (m1 m2)
6033   (let ((m1 (or m1 gnus-select-method))
6034         (m2 (or m2 gnus-select-method)))
6035     (or (equal m1 m2)
6036         (and (eq (car m1) (car m2))
6037              (or (not (memq 'address (assoc (symbol-name (car m1))
6038                                             gnus-valid-select-methods)))
6039                  (equal (nth 1 m1) (nth 1 m2)))))))
6040
6041 (defsubst gnus-header-value ()
6042   (buffer-substring (match-end 0) (gnus-point-at-eol)))
6043
6044 (defun gnus-get-newsgroup-headers ()
6045   (setq gnus-article-internal-prepare-hook nil)
6046   (let ((cur nntp-server-buffer)
6047         (dependencies gnus-newsgroup-dependencies)
6048         (none-id 0)
6049         headers char article id dep end)
6050     (save-excursion
6051       (set-buffer nntp-server-buffer)
6052       (goto-char 1)
6053       ;; Search to the beginning of the next header. Error messages
6054       ;; do not begin with 2 or 3.
6055       (while (re-search-forward "^[23][0-9]+ " nil t)
6056         (let ((header (make-vector 9 nil))
6057               (c (following-char))
6058               (case-fold-search t)
6059               (p (point))
6060               from subject in-reply-to references ref)
6061           (setq id nil
6062                 ref nil
6063                 references nil
6064                 subject nil
6065                 from nil)
6066           (header-set-number header (setq article (read cur)))
6067           ;; This implementation of this function, with nine
6068           ;; search-forwards instead of the one re-search-forward and
6069           ;; a case (which basically was the old function) is actually
6070           ;; about twice as fast, even though it looks messier. You
6071           ;; can't have everything, I guess. Speed and elegance
6072           ;; doesn't always come hand in hand.
6073           (save-restriction
6074             (narrow-to-region (point) (save-excursion 
6075                                         (search-forward "\n.\n" nil t)))
6076             (if (search-forward "\nfrom: " nil t)
6077                 (header-set-from header (gnus-header-value))
6078               (header-set-from header "(nobody)"))
6079             (goto-char p)
6080             (if (search-forward "\nsubject: " nil t)
6081                 (header-set-subject header (gnus-header-value))
6082               (header-set-subject header "(none)"))
6083             (goto-char p)
6084             (and (search-forward "\nxref: " nil t)
6085                  (header-set-xref header (gnus-header-value)))
6086             (goto-char p)
6087             (or (numberp (and (search-forward "\nlines: " nil t)
6088                               (header-set-lines header (read cur))))
6089                 (header-set-lines header 0))
6090             (goto-char p)
6091             (and (search-forward "\ndate: " nil t)
6092                  (header-set-date header (gnus-header-value)))
6093             (goto-char p)
6094             (if (search-forward "\nmessage-id: " nil t)
6095                 (header-set-id header (setq id (gnus-header-value)))
6096               ;; If there was no message-id, we just fake one to make
6097               ;; subsequent routines simpler.
6098               (header-set-id 
6099                header 
6100                (setq id (concat "none+" (int-to-string 
6101                                          (setq none-id (1+ none-id)))))))
6102             (goto-char p)
6103             (if (search-forward "\nreferences: " nil t)
6104                 (progn
6105                   (header-set-references header (gnus-header-value))
6106                   (setq end (match-end 0))
6107                   (save-excursion
6108                     (setq ref 
6109                           (downcase
6110                            (buffer-substring
6111                             (progn 
6112                               (end-of-line)
6113                               (search-backward ">" end t)
6114                               (1+ (point)))
6115                             (progn
6116                               (search-backward "<" end t)
6117                               (point)))))))
6118               ;; Get the references from the in-reply-to header if there
6119               ;; ware no references and the in-reply-to header looks
6120               ;; promising. 
6121               (if (and (search-forward "\nin-reply-to: " nil t)
6122                        (setq in-reply-to (gnus-header-value))
6123                        (string-match "<[^>]+>" in-reply-to))
6124                   (progn
6125                     (header-set-references 
6126                      header 
6127                      (setq ref (substring in-reply-to (match-beginning 0)
6128                                           (match-end 0))))
6129                     (setq ref (downcase ref)))
6130                 (setq ref "none")))
6131             ;; We do some threading while we read the headers. The
6132             ;; message-id and the last reference are both entered into
6133             ;; the same hash table. Some tippy-toeing around has to be
6134             ;; done in case an article has arrived before the article
6135             ;; which it refers to.
6136             (if (boundp (setq dep (intern (downcase id) dependencies)))
6137                 (if (car (symbol-value dep))
6138                     ;; An article with this Message-ID has already
6139                     ;; been seen, so we ignore this one, except we add
6140                     ;; any additional Xrefs (in case the two articles
6141                     ;; came from different servers.
6142                     (progn
6143                       (header-set-xref 
6144                        (car (symbol-value dep))
6145                        (concat (or (header-xref (car (symbol-value dep))) "")
6146                                (or (header-xref header) "")))
6147                       (setq header nil))
6148                   (setcar (symbol-value dep) header))
6149               (set dep (list header)))
6150             (if header
6151                 (progn
6152                   (if (boundp (setq dep (intern ref dependencies)))
6153                       (setcdr (symbol-value dep) 
6154                               (cons header (cdr (symbol-value dep))))
6155                     (set dep (list nil header)))
6156                   (setq headers (cons header headers))))
6157             (goto-char (point-max))))))
6158     (nreverse headers)))
6159
6160 ;; The following macros and functions were written by Felix Lee
6161 ;; <flee@cse.psu.edu>. 
6162
6163 ;; This is almost 4x faster than (string-to-int (buffer-substring ... ))
6164 ;; primarily because of garbage collection.  -jwz
6165 (defmacro gnus-read-integer (&optional point move-p)
6166   (` ((, (if move-p 'progn 'save-excursion))
6167       (,@ (if point (list (list 'goto-char point))))
6168       (if (and (<= (following-char) ?9)
6169                (>= (following-char) ?0))
6170           (read (current-buffer))
6171         0))))
6172
6173 (defmacro gnus-nov-skip-field ()
6174   '(search-forward "\t" eol 'move))
6175
6176 (defmacro gnus-nov-field ()
6177   '(buffer-substring (point) (if (gnus-nov-skip-field) (1- (point)) eol)))
6178
6179 ;; Goes through the xover lines and returns a list of vectors
6180 (defun gnus-get-newsgroup-headers-xover (sequence)
6181   "Parse the news overview data in the server buffer, and return a
6182 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
6183   ;; Get the Xref when the users reads the articles since most/some
6184   ;; NNTP servers do not include Xrefs when using XOVER.
6185   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
6186   (let ((cur nntp-server-buffer)
6187         (dependencies gnus-newsgroup-dependencies)
6188         (none 0)
6189         number headers header)
6190     (save-excursion
6191       (set-buffer nntp-server-buffer)
6192       (goto-char (point-min))
6193       (while (and sequence (not (eobp)))
6194         (setq number (read cur))
6195         (while (and sequence (< (car sequence) number))
6196           (setq sequence (cdr sequence)))
6197         (and sequence 
6198              (eq number (car sequence))
6199              (progn
6200                (setq sequence (cdr sequence))
6201                (if (setq header 
6202                          (inline (gnus-nov-parse-line number dependencies)))
6203                    (setq headers (cons header headers)))))
6204         (forward-line 1))
6205       (setq headers (nreverse headers)))
6206     headers))
6207
6208 (defun gnus-nov-parse-line (number dependencies)
6209   "Point has to be after the number on the beginning of the line."
6210   (let ((none 0)
6211         header (eol (gnus-point-at-eol)) ref id dep)
6212     (forward-char)
6213     ;; overview: [num subject from date id refs chars lines misc]
6214     (save-restriction
6215       (narrow-to-region (point) eol)
6216       (condition-case nil
6217           (setq header
6218                 (vector 
6219                  number                 ; number
6220                  (gnus-nov-field)       ; subject
6221                  (gnus-nov-field)       ; from
6222                  (gnus-nov-field)       ; date
6223                  (setq id (or (gnus-nov-field)
6224                               (concat "none+"
6225                                      (int-to-string 
6226                                       (setq none (1+ none)))))) ; id
6227                  (progn
6228                    (save-excursion
6229                      (let ((beg (point)))
6230                        (search-forward "\t" eol)
6231                        (if (search-backward ">" beg t)
6232                            (setq ref 
6233                                  (downcase 
6234                                   (buffer-substring 
6235                                    (1+ (point))
6236                                    (progn
6237                                      (search-backward "<" beg t)
6238                                      (point)))))
6239                          (setq ref nil))))
6240                    (gnus-nov-field))    ; refs
6241                  (read (current-buffer)) ; chars
6242                  (read (current-buffer)) ; lines
6243                  (if (/= (following-char) ?\t)
6244                      nil
6245                    (forward-char 1)
6246                    (gnus-nov-field))    ; misc
6247                  ))
6248         (error (progn 
6249                  (setq header nil)
6250                  (goto-char eol)))))
6251     ;; We build the thread tree.
6252     (and header
6253          (if (boundp (setq dep (intern (downcase id) dependencies)))
6254              (if (car (symbol-value dep))
6255                  ;; An article with this Message-ID has already been seen,
6256                  ;; so we ignore this one, except we add any additional
6257                  ;; Xrefs (in case the two articles came from different
6258                  ;; servers.
6259                  (progn
6260                    (header-set-xref 
6261                     (car (symbol-value dep))
6262                     (concat (or (header-xref (car (symbol-value dep))) "")
6263                             (or (header-xref header) "")))
6264                    (setq header nil))
6265                (setcar (symbol-value dep) header))
6266            (set dep (list header))))
6267     (if header
6268         (progn
6269           (if (boundp (setq dep (intern (or ref "none") 
6270                                         dependencies)))
6271               (setcdr (symbol-value dep) 
6272                       (cons header (cdr (symbol-value dep))))
6273             (set dep (list nil header)))))
6274     header))
6275
6276 (defun gnus-article-get-xrefs ()
6277   "Fill in the Xref value in `gnus-current-headers', if necessary.
6278 This is meant to be called in `gnus-article-internal-prepare-hook'."
6279   (or (not gnus-use-cross-reference)
6280       (header-xref gnus-current-headers)
6281       (let ((case-fold-search t)
6282             xref)
6283         (save-restriction
6284           (gnus-narrow-to-headers)
6285           (goto-char (point-min))
6286           (if (or (and (eq (downcase (following-char)) ?x)
6287                        (looking-at "Xref:"))
6288                   (search-forward "\nXref:" nil t))
6289               (progn
6290                 (goto-char (1+ (match-end 0)))
6291                 (setq xref (buffer-substring (point) 
6292                                              (progn (end-of-line) (point))))
6293                 (save-excursion
6294                   (set-buffer gnus-summary-buffer)
6295                   (header-set-xref gnus-current-headers xref))))))))
6296
6297 (defalias 'gnus-find-header-by-number 'gnus-get-header-by-number)
6298 (make-obsolete 'gnus-find-header-by-number 'gnus-get-header-by-number)
6299
6300 ;; Return a header specified by a NUMBER.
6301 (defun gnus-get-header-by-number (number)
6302   (save-excursion
6303     (set-buffer gnus-summary-buffer)
6304     (or gnus-newsgroup-headers-hashtb-by-number
6305         (gnus-make-headers-hashtable-by-number))
6306     (gnus-gethash (int-to-string number)
6307                   gnus-newsgroup-headers-hashtb-by-number)))
6308
6309 (defun gnus-make-headers-hashtable-by-number ()
6310   "Make hashtable for the variable gnus-newsgroup-headers by number."
6311   (save-excursion
6312     (set-buffer gnus-summary-buffer)
6313     (let ((headers gnus-newsgroup-headers)
6314           header)
6315       (setq gnus-newsgroup-headers-hashtb-by-number
6316             (gnus-make-hashtable (length headers)))
6317       (while headers
6318         (setq header (car headers))
6319         (gnus-sethash (int-to-string (header-number header))
6320                       header gnus-newsgroup-headers-hashtb-by-number)
6321         (setq headers (cdr headers))))))
6322
6323 (defun gnus-more-header-backward ()
6324   "Find new header backward."
6325   (let ((first (car (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
6326         (artnum gnus-newsgroup-begin)
6327         (header nil))
6328     (while (and (not header)
6329                 (> artnum first))
6330       (setq artnum (1- artnum))
6331       (setq header (gnus-read-header artnum)))
6332     header))
6333
6334 (defun gnus-more-header-forward (&optional backward)
6335   "Find new header forward.
6336 If BACKWARD, find new header backward instead."
6337   (if backward
6338       (gnus-more-header-backward)
6339     (let ((last (cdr (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
6340           (artnum gnus-newsgroup-end)
6341           (header nil))
6342       (while (and (not header)
6343                   (< artnum last))
6344         (setq artnum (1+ artnum))
6345         (setq header (gnus-read-header artnum)))
6346       header)))
6347
6348 (defun gnus-extend-newsgroup (header &optional backward)
6349   "Extend newsgroup selection with HEADER.
6350 Optional argument BACKWARD means extend toward backward."
6351   (if header
6352       (let ((artnum (header-number header)))
6353         (setq gnus-newsgroup-headers
6354               (if backward
6355                   (cons header gnus-newsgroup-headers)
6356                 (nconc gnus-newsgroup-headers (list header))))
6357         (setq gnus-newsgroup-unselected
6358               (delq artnum gnus-newsgroup-unselected))
6359         (setq gnus-newsgroup-begin (min gnus-newsgroup-begin artnum))
6360         (setq gnus-newsgroup-end (max gnus-newsgroup-end artnum)))))
6361
6362 (defun gnus-summary-work-articles (n)
6363   "Return a list of articles to be worked upon. The prefix argument,
6364 the list of process marked articles, and the current article will be
6365 taken into consideration."
6366   (let (articles)
6367     (if (and n (numberp n))
6368         (let ((backward (< n 0))
6369               (n (abs n)))
6370           (save-excursion
6371             (while (and (> n 0)
6372                         (setq articles (cons (gnus-summary-article-number) 
6373                                              articles))
6374                         (gnus-summary-search-forward nil nil backward))
6375               (setq n (1- n))))
6376           (sort articles (function <)))
6377       (or (reverse gnus-newsgroup-processable)
6378           (list (gnus-summary-article-number))))))
6379
6380 (defun gnus-summary-search-group (&optional backward use-level)
6381   "Search for next unread newsgroup.
6382 If optional argument BACKWARD is non-nil, search backward instead."
6383   (save-excursion
6384     (set-buffer gnus-group-buffer)
6385     (save-excursion
6386       ;; We don't want to alter current point of group mode buffer.
6387       (if (gnus-group-search-forward 
6388            backward nil
6389            (if use-level (gnus-group-group-level) nil))
6390           (gnus-group-group-name)))))
6391
6392 (defun gnus-summary-best-group (&optional exclude-group)
6393   "Find the name of the best unread group.
6394 If EXCLUDE-GROUP, do not go to this group."
6395   (save-excursion
6396     (set-buffer gnus-group-buffer)
6397     (save-excursion
6398       (gnus-group-best-unread-group exclude-group))))
6399
6400 (defun gnus-summary-search-subject (&optional backward unread subject)
6401   "Search for article forward.
6402 If BACKWARD is non-nil, search backward.
6403 If UNREAD is non-nil, only unread articles are selected.
6404 If SUBJECT is non-nil, the article which has the same subject will be
6405 searched for." 
6406   (let ((func (if backward 'previous-single-property-change
6407                 'next-single-property-change))
6408         (beg (point))
6409         (did t)
6410         pos)
6411     (beginning-of-line)
6412     (and gnus-summary-check-current unread
6413          (eq (nth 1 (get-text-property (point) 'gnus)) gnus-unread-mark)
6414          (setq did nil))
6415     (if (not did)
6416         ()
6417       (forward-char (if backward (if (bobp) 0 -1) (if (eobp) 0 1)))
6418       (while
6419           (and 
6420            (setq pos (funcall func (point) 'gnus))
6421            (goto-char (if backward (1- pos) pos))
6422            (setq did
6423                  (not (and
6424                        (or (not unread)
6425                            (eq (nth 1 (get-text-property (point) 'gnus))
6426                                gnus-unread-mark))
6427                        (or (not subject)
6428                            (equal (gnus-simplify-subject-re subject)
6429                                   (gnus-simplify-subject-re
6430                                    (gnus-summary-subject-string)))))))
6431            (if backward (if (bobp) nil (forward-char -1) t)
6432              (if (eobp) nil (forward-char 1) t)))))
6433     (if did
6434         (progn (goto-char beg) nil)
6435       (prog1
6436           (car (get-text-property (point) 'gnus))
6437         (gnus-summary-position-cursor)))))
6438
6439 (defun gnus-summary-search-forward (&optional unread subject backward)
6440   "Search for article forward.
6441 If UNREAD is non-nil, only unread articles are selected.
6442 If SUBJECT is non-nil, the article which has the same subject will be
6443 searched for. 
6444 If BACKWARD is non-nil, the search will be performed backwards instead."
6445   (gnus-summary-search-subject backward unread subject))
6446
6447 (defun gnus-summary-search-backward (&optional unread subject)
6448   "Search for article backward.
6449 If 1st optional argument UNREAD is non-nil, only unread article is selected.
6450 If 2nd optional argument SUBJECT is non-nil, the article which has
6451 the same subject will be searched for."
6452   (gnus-summary-search-forward unread subject t))
6453
6454 (defun gnus-summary-article-number (&optional number-or-nil)
6455   "The article number of the article on the current line.
6456 If there isn's an article number here, then we return the current
6457 article number."
6458   (let* ((p (point))
6459          (number (car (get-text-property 
6460                        (progn (beginning-of-line) 
6461                               (prog1 (point) (goto-char p)))
6462                        'gnus))))
6463     (if number-or-nil number (or number gnus-current-article))))
6464
6465 (defun gnus-summary-thread-level ()
6466   "The thread level of the article on the current line."
6467   (or (nth 2 (get-text-property (gnus-point-at-bol) 'gnus))
6468       0))
6469
6470 (defun gnus-summary-pseudo-article ()
6471   "The thread level of the article on the current line."
6472   (get-text-property (gnus-point-at-bol) 'gnus-pseudo))
6473
6474 (defun gnus-summary-article-mark ()
6475   "The mark on the current line."
6476   (nth 1 (get-text-property (gnus-point-at-bol) 'gnus)))
6477
6478 (defun gnus-summary-subject-string ()
6479   "Return current subject string or nil if nothing."
6480   (let ((article (gnus-summary-article-number))
6481         header)
6482     (and article 
6483          (setq header (gnus-get-header-by-number article))
6484          (header-subject header))))
6485
6486 (defalias 'gnus-summary-score 'gnus-summary-article-score)
6487 (make-obsolete 'gnus-summary-score 'gnus-summary-article-score)
6488 (defun gnus-summary-article-score ()
6489   "Return current article score."
6490   (or (cdr (assq (gnus-summary-article-number) gnus-newsgroup-scored))
6491       gnus-summary-default-score 0))
6492
6493 ;; Written by Sudish Joseph <joseph@cis.ohio-state.edu>.
6494 (defun gnus-summary-recenter-new ()
6495   "Center point in the summary window.
6496 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6497 displayed, no centering will be performed." 
6498   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6499   ;; Recenter only when requested. Suggested by popovich@park.cs.columbia.edu.
6500   (sit-for 0)
6501   (and gnus-auto-center-summary
6502        (get-buffer-window gnus-article-buffer)
6503        (let* ((height (window-height))
6504               (w-end (window-end))
6505               (top (cond ((< height 4) 0)
6506                          ((< height 6) 1)
6507                          (t 2))))
6508          (and (= w-end (point-max))
6509              ;; adjust for blank lines at the bottom of the window
6510              (setq top (+ (count-lines (window-start) (point))
6511                           (- height (count-lines (window-start) w-end) 2))))
6512          (recenter top))))
6513
6514 (defun gnus-summary-recenter ()
6515   "Center point in the summary window.
6516 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6517 displayed, no centering will be performed." 
6518   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6519   ;; Recenter only when requested. Suggested by popovich@park.cs.columbia.edu.
6520   (let ((top (cond ((< (window-height) 4) 0)
6521                    ((< (window-height) 6) 1)
6522                    (t 2))))
6523     (and 
6524      ;; The user has to want it,
6525      gnus-auto-center-summary 
6526      ;; the article buffer must be displayed,
6527      (get-buffer-window gnus-article-buffer)
6528      ;; there must be lines left to scroll forward,
6529      (zerop (save-excursion (forward-line (- (window-height) 1 top))))
6530      ;; so we recenter.
6531      (set-window-start 
6532       (get-buffer-window (current-buffer)) 
6533       (save-excursion (forward-line (- top)) (point))))))
6534
6535 ;; Function written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
6536 (defun gnus-short-group-name (newsgroup)
6537   "Convert a long group name to an initialized form.
6538 The last part of the name is left intact: \"rec.arts.anime\" becomes
6539 \"r.a.anime\"."
6540   (let ((name ""))
6541     (while newsgroup
6542       (if (string-match "\\." newsgroup)
6543           (progn
6544             (setq name (concat name (substring newsgroup 0 1))
6545                   newsgroup (substring newsgroup (match-end 0))
6546                   name (concat name ".")))
6547           (setq name (concat name newsgroup)
6548                 newsgroup nil)))
6549     name))
6550
6551
6552 (defun gnus-summary-jump-to-group (newsgroup)
6553   "Move point to NEWSGROUP in group mode buffer."
6554   ;; Keep update point of group mode buffer if visible.
6555   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6556       (save-window-excursion
6557         ;; Take care of tree window mode.
6558         (if (get-buffer-window gnus-group-buffer)
6559             (pop-to-buffer gnus-group-buffer))
6560         (gnus-group-jump-to-group newsgroup))
6561     (save-excursion
6562       ;; Take care of tree window mode.
6563       (if (get-buffer-window gnus-group-buffer)
6564           (pop-to-buffer gnus-group-buffer)
6565         (set-buffer gnus-group-buffer))
6566       (gnus-group-jump-to-group newsgroup))))
6567
6568 ;; This function returns a list of article numbers based on the
6569 ;; difference between the ranges of read articles in this group and
6570 ;; the range of active articles.
6571 (defun gnus-list-of-unread-articles (group)
6572   (let* ((read (nth 2 (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
6573          (active (gnus-gethash group gnus-active-hashtb))
6574          (last (cdr active))
6575          unread first nlast unread)
6576     ;; If none are read, then all are unread. 
6577     (if (not read)
6578         (setq first (car active))
6579       ;; If the range of read articles is a single range, then the
6580       ;; first unread article is the article after the last read
6581       ;; article. Sounds logical, doesn't it?
6582       (if (not (listp (cdr read)))
6583           (setq first (1+ (cdr read)))
6584         ;; `read' is a list of ranges.
6585         (if (/= (setq nlast (or (and (numberp (car read)) (car read)) 
6586                                 (car (car read)))) 1)
6587             (setq first 1))
6588         (while read
6589           (if first 
6590               (while (< first nlast)
6591                 (setq unread (cons first unread))
6592                 (setq first (1+ first))))
6593           (setq first (1+ (if (atom (car read)) (car read) (cdr (car read)))))
6594           (setq nlast (if (atom (car (cdr read))) 
6595                           (car (cdr read))
6596                         (car (car (cdr read)))))
6597           (setq read (cdr read)))))
6598     ;; And add the last unread articles.
6599     (while (<= first last)
6600       (setq unread (cons first unread))
6601       (setq first (1+ first)))
6602     ;; Return the list of unread articles.
6603     (nreverse unread)))
6604
6605
6606 ;; Various summary commands
6607
6608 (defun gnus-summary-universal-argument ()
6609   "Perform any operation on all articles marked with the process mark."
6610   (interactive)
6611   (gnus-set-global-variables)
6612   (let ((articles (reverse gnus-newsgroup-processable))
6613         key func)
6614     (or articles (error "No articles marked"))
6615     (or (setq func (key-binding (read-key-sequence "C-c C-u")))
6616         (error "Undefined key"))
6617     (while articles
6618       (gnus-summary-goto-subject (car articles))
6619       (command-execute func)
6620       (gnus-summary-remove-process-mark (car articles))
6621       (setq articles (cdr articles)))))
6622
6623 (defun gnus-summary-toggle-truncation (arg)
6624   "Toggle truncation of summary lines.
6625 With arg, turn line truncation on iff arg is positive."
6626   (interactive "P")
6627   (setq truncate-lines
6628         (if (null arg) (not truncate-lines)
6629           (> (prefix-numeric-value arg) 0)))
6630   (redraw-display))
6631
6632 (defun gnus-summary-reselect-current-group (all)
6633   "Once exit and then reselect the current newsgroup.
6634 The prefix argument ALL means to select all articles."
6635   (interactive "P")
6636   (gnus-set-global-variables)
6637   (let ((current-subject (gnus-summary-article-number)))
6638     (gnus-summary-exit t)
6639     ;; We have to adjust the point of group mode buffer because the
6640     ;; current point was moved to the next unread newsgroup by
6641     ;; exiting.
6642     (gnus-summary-jump-to-group gnus-newsgroup-name)
6643     (gnus-group-read-group all t)
6644     (gnus-summary-goto-subject current-subject)))
6645
6646 (defun gnus-summary-rescan-group (all)
6647   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6648   (interactive "P")
6649   (gnus-set-global-variables)
6650   ;; Fix by Ilja Weis <kult@uni-paderborn.de>.
6651   (let ((group gnus-newsgroup-name))
6652     (gnus-summary-exit t)
6653     (gnus-summary-jump-to-group group)
6654     (save-excursion
6655       (set-buffer gnus-group-buffer)
6656       (gnus-group-get-new-news-this-group 1))
6657     (gnus-summary-jump-to-group group)
6658     (gnus-group-read-group all)))
6659
6660 (defun gnus-summary-exit (&optional temporary)
6661   "Exit reading current newsgroup, and then return to group selection mode.
6662 gnus-exit-group-hook is called with no arguments if that value is non-nil."
6663   (interactive)
6664   (gnus-set-global-variables)
6665   (gnus-kill-save-kill-buffer)
6666   (let* ((group gnus-newsgroup-name)
6667          (quit-buffer (cdr (assoc 'quit-buffer (gnus-find-method-for-group
6668                                                 gnus-newsgroup-name))))
6669          (mode major-mode)
6670          (method (car (gnus-find-method-for-group group)))
6671          (buf (current-buffer)))
6672     (if gnus-newsgroup-kill-headers
6673         (setq gnus-newsgroup-killed
6674               (gnus-compress-sequence
6675                (nconc
6676                 (gnus-set-sorted-intersection
6677                  (gnus-uncompress-range gnus-newsgroup-killed)
6678                  (setq gnus-newsgroup-unselected
6679                        (sort gnus-newsgroup-unselected '<)))
6680                 (setq gnus-newsgroup-unreads
6681                       (sort gnus-newsgroup-unreads '<))) t)))
6682     (or (listp (cdr gnus-newsgroup-killed))
6683         (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6684     (let ((updated nil)
6685           (headers gnus-newsgroup-headers))
6686       (gnus-close-group group)
6687       (run-hooks 'gnus-exit-group-hook)
6688       (gnus-update-read-articles 
6689        group gnus-newsgroup-unreads gnus-newsgroup-unselected 
6690        gnus-newsgroup-marked
6691        t gnus-newsgroup-replied gnus-newsgroup-expirable
6692        gnus-newsgroup-killed gnus-newsgroup-dormant
6693        gnus-newsgroup-bookmarks 
6694        (and gnus-save-score gnus-newsgroup-scored))
6695       (and gnus-use-cross-reference
6696            (gnus-mark-xrefs-as-read 
6697             group headers gnus-newsgroup-unreads gnus-newsgroup-expirable))
6698       ;; Do adaptive scoring, and possibly save score files.
6699       (and gnus-use-adaptive-scoring
6700            (gnus-score-adaptive))
6701       (and (fboundp 'gnus-score-save)
6702            (funcall 'gnus-score-save))
6703       ;; Do not switch windows but change the buffer to work.
6704       (set-buffer gnus-group-buffer)
6705       (or (eq 'nndigest method)
6706           (gnus-group-update-group group)))
6707     ;; Make sure where I was, and go to next newsgroup.
6708     (if (eq method 'nndigest)
6709         ()
6710       (gnus-group-jump-to-group group)
6711       (gnus-group-next-unread-group 1))
6712     (if temporary
6713         ;; If exiting temporary, caller should adjust group mode
6714         ;; buffer point by itself.
6715         nil                             ;Nothing to do.
6716       ;; We set all buffer-local variables to nil. It is unclear why
6717       ;; this is needed, but if we don't, buffer-local variables are
6718       ;; not garbage-collected, it seems. This would the lead to en
6719       ;; ever-growing Emacs.
6720       (set-buffer buf)
6721       (gnus-summary-clear-local-variables)
6722       ;; We clear the global counterparts of the buffer-local
6723       ;; variables as well, just to be on the safe side.
6724       (set-buffer gnus-group-buffer)
6725       (gnus-summary-clear-local-variables)
6726       (gnus-configure-windows 'article)
6727       (gnus-configure-windows 'newsgroups)
6728       ;; Return to group mode buffer. 
6729       (if (eq mode 'gnus-summary-mode)
6730           (gnus-kill-buffer buf))
6731       (if (get-buffer gnus-article-buffer)
6732           (bury-buffer gnus-article-buffer))
6733       (setq gnus-current-select-method gnus-select-method)
6734       (pop-to-buffer gnus-group-buffer)
6735       (if (eq method 'nndigest)
6736           ()
6737         (gnus-group-jump-to-group group)
6738         (gnus-group-next-unread-group 1))
6739       (if (gnus-buffer-exists-p quit-buffer)
6740           (progn
6741             (switch-to-buffer quit-buffer)
6742             (gnus-set-global-variables)
6743             (gnus-configure-windows 'summary))))))
6744
6745 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6746 (defun gnus-summary-exit-no-update (&optional no-questions)
6747   "Quit reading current newsgroup without updating read article info."
6748   (interactive)
6749   (let* ((group gnus-newsgroup-name)
6750          (quit-buffer (cdr (assoc 'quit-buffer 
6751                                   (gnus-find-method-for-group group)))))
6752     (if (or no-questions
6753             gnus-expert-user
6754             (gnus-y-or-n-p "Do you really wanna quit reading this group? "))
6755         (progn
6756           (gnus-close-group group)
6757           (gnus-summary-clear-local-variables)
6758           (set-buffer gnus-group-buffer)
6759           (gnus-summary-clear-local-variables)
6760           ;; Return to group selection mode.
6761           (gnus-configure-windows 'newsgroups)
6762           (if (get-buffer gnus-summary-buffer)
6763               (kill-buffer gnus-summary-buffer))
6764           (if (get-buffer gnus-article-buffer)
6765               (bury-buffer gnus-article-buffer))
6766           (pop-to-buffer gnus-group-buffer)
6767           (if (equal (gnus-group-group-name) group)
6768               (gnus-group-next-unread-group 1))
6769           (if (gnus-buffer-exists-p quit-buffer)
6770               (progn
6771                 (switch-to-buffer quit-buffer)
6772                 (gnus-configure-windows 'summary)))))))
6773
6774 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6775 (defun gnus-summary-fetch-faq (group)
6776   "Fetch the FAQ for the current group."
6777   (interactive (list gnus-newsgroup-name))
6778   (gnus-configure-windows 'article)
6779   (pop-to-buffer gnus-article-buffer)
6780   (find-file (concat gnus-group-faq-directory group)))
6781
6782 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6783 (defun gnus-summary-describe-group (force)
6784   "Describe the current newsgroup."
6785   (interactive "P")
6786   (gnus-group-describe-group force gnus-newsgroup-name))
6787
6788 (defun gnus-summary-describe-briefly ()
6789   "Describe summary mode commands briefly."
6790   (interactive)
6791   (message
6792     (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")))
6793
6794 ;; Walking around group mode buffer from summary mode.
6795
6796 (defun gnus-summary-next-group (&optional no-article group backward)
6797   "Exit current newsgroup and then select next unread newsgroup.
6798 If prefix argument NO-ARTICLE is non-nil, no article is selected initially.
6799 If BACKWARD, go to previous group instead."
6800   (interactive "P")
6801   (gnus-set-global-variables)
6802   (let ((ingroup gnus-newsgroup-name)
6803         (sumbuf (current-buffer))
6804         num)
6805     (gnus-summary-exit t)               ;Update all information.
6806     (if (and group
6807              (or (and (numberp (setq num (car (gnus-gethash
6808                                                group gnus-newsrc-hashtb))))
6809                       (< num 1))
6810                  (null num)))
6811         (progn
6812           (gnus-group-jump-to-group group)
6813           (setq group nil))
6814       (gnus-group-jump-to-group ingroup))
6815     (gnus-summary-search-group backward)
6816     (let ((group (or group (gnus-summary-search-group backward)))
6817           (buf gnus-summary-buffer))
6818       (if (null group)
6819           (gnus-summary-exit-no-update t)
6820         (message "Selecting %s..." group)
6821         ;; We are now in group mode buffer.
6822         ;; Make sure group mode buffer point is on GROUP.
6823         (gnus-group-jump-to-group group)
6824         (if (not (eq gnus-auto-select-next 'quietly))
6825             (progn
6826               (gnus-summary-read-group group nil no-article buf)
6827               (and (string= gnus-newsgroup-name ingroup)
6828                    (bufferp sumbuf) (buffer-name sumbuf)
6829                    (progn
6830                      (set-buffer (setq gnus-summary-buffer sumbuf))
6831                      (gnus-summary-exit-no-update t))))
6832           (gnus-summary-read-group group nil no-article buf)
6833           (while (and (string= gnus-newsgroup-name ingroup)
6834                       (bufferp sumbuf) (buffer-name sumbuf))
6835             (set-buffer gnus-group-buffer)
6836             (gnus-group-next-unread-group 1)
6837             (recenter)
6838             (gnus-summary-read-group 
6839              (gnus-group-group-name) nil no-article buf)))))))
6840
6841
6842 (defun gnus-summary-prev-group (no-article)
6843   "Exit current newsgroup and then select previous unread newsgroup.
6844 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6845   (interactive "P")
6846   (gnus-summary-next-group no-article nil t))
6847
6848 ;; Walking around summary lines.
6849
6850 (defun gnus-summary-first-subject (unread)
6851   "Go to the first unread subject.
6852 If UNREAD is non-nil, go to the first unread article.
6853 Returns nil if there are no unread articles."
6854   (interactive "P")
6855   (let ((begin (point)))
6856     (goto-char (point-min))
6857     (if (not unread)
6858         t
6859       (while (and (not (eq (nth 1 (get-text-property (point) 'gnus))
6860                            gnus-unread-mark))
6861                   (zerop (forward-line 1))))
6862       (prog1
6863           (if (not (eobp))
6864               t
6865             ;; If there is no unread articles, stay where you are.
6866             (goto-char begin)
6867             (message "No more unread articles")
6868             nil)
6869         (gnus-summary-position-cursor)))))
6870
6871 (defun gnus-summary-next-subject (n &optional unread dont-display)
6872   "Go to next N'th summary line.
6873 If N is negative, go to the previous N'th subject line.
6874 If UNREAD is non-nil, only unread articles are selected.
6875 The difference between N and the actual number of steps taken is
6876 returned."
6877   (interactive "p")
6878   (let ((backward (< n 0))
6879         (n (abs n)))
6880     (while (and (> n 0)
6881                 (gnus-summary-search-forward unread nil backward))
6882       (setq n (1- n)))
6883     (if (/= 0 n) (message "No more%s articles" (if unread " unread" "")))
6884     (or dont-display
6885         (progn
6886           (gnus-summary-recenter)
6887           (gnus-summary-position-cursor)))
6888   n))
6889
6890 (defun gnus-summary-next-unread-subject (n)
6891   "Go to next N'th unread summary line."
6892   (interactive "p")
6893   (gnus-summary-next-subject n t))
6894
6895 (defun gnus-summary-prev-subject (n &optional unread)
6896   "Go to previous N'th summary line.
6897 If optional argument UNREAD is non-nil, only unread article is selected."
6898   (interactive "p")
6899   (gnus-summary-next-subject (- n) unread))
6900
6901 (defun gnus-summary-prev-unread-subject (n)
6902   "Go to previous N'th unread summary line."
6903   (interactive "p")
6904   (gnus-summary-next-subject (- n) t))
6905
6906 (defun gnus-summary-goto-subject (article)
6907   "Go the subject line of ARTICLE."
6908   (interactive
6909    (list
6910     (string-to-int
6911      (completing-read "Article number: "
6912                       (mapcar
6913                        (lambda (headers)
6914                          (list
6915                           (int-to-string (header-number headers))))
6916                        gnus-newsgroup-headers)
6917                       nil 'require-match))))
6918   (or article (error "No article number"))
6919   (let ((b (point)))
6920     (goto-char (point-min))
6921     (while (and (not (eq (car (get-text-property (point) 'gnus)) article))
6922                 (zerop (forward-line 1))))
6923     (prog1
6924         (if (not (eobp))
6925             article
6926           (goto-char b)
6927           nil)
6928       (gnus-summary-position-cursor))))
6929
6930 ;; Walking around summary lines with displaying articles.
6931
6932 (defun gnus-summary-expand-window ()
6933   "Expand summary window to show headers full window."
6934   (interactive)
6935   (gnus-set-global-variables)
6936   (gnus-configure-windows 'summary)
6937   (pop-to-buffer gnus-summary-buffer))
6938
6939 (defun gnus-summary-display-article (article &optional all-header)
6940   "Display ARTICLE in article buffer."
6941   (gnus-set-global-variables)
6942   (if (null article)
6943       nil
6944     (gnus-article-prepare article all-header)
6945     (if (eq (gnus-summary-article-mark) ?Z)
6946         (progn
6947           (forward-line 1)
6948           (gnus-summary-position-cursor)))
6949     (run-hooks 'gnus-select-article-hook)
6950     (gnus-summary-recenter)
6951 ;    (set-window-point (get-buffer-window (current-buffer)) (point-max))
6952 ;    (sit-for 0)
6953     (gnus-summary-goto-subject article)
6954     ;; Successfully display article.
6955     (gnus-summary-update-line)
6956     t))
6957
6958 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6959   "Select the current article.
6960 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6961 non-nil, the article will be re-fetched even if it already present in
6962 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6963 be displayed."
6964   (and (not pseudo) (gnus-summary-pseudo-article)
6965        (error "This is a pseudo-article."))
6966   (let ((article (or article (gnus-summary-article-number)))
6967         (all-headers (not (not all-headers)))) ;Must be T or NIL.
6968     (if (or (null gnus-current-article)
6969             (null gnus-article-current)
6970             (/= article (cdr gnus-article-current))
6971             (not (equal (car gnus-article-current) gnus-newsgroup-name))
6972             force)
6973         ;; The requested article is different from the current article.
6974         (progn
6975           (gnus-summary-display-article article all-headers)
6976           (gnus-configure-windows 'article)
6977           article)
6978       (if all-headers (gnus-article-show-all-headers))
6979       (gnus-configure-windows 'article)
6980       (pop-to-buffer gnus-summary-buffer)
6981       nil)))
6982
6983 (defun gnus-summary-set-current-mark (&optional current-mark)
6984   "Obsolete function."
6985   nil)
6986
6987 (defun gnus-summary-next-article (unread &optional subject backward)
6988   "Select the next article.
6989 If UNREAD, only unread articles are selected.
6990 If SUBJECT, only articles with SUBJECT are selected.
6991 If BACKWARD, the previous article is selected instead of the next."
6992   (interactive "P")
6993   (let ((opoint (point))
6994         (method (car (gnus-find-method-for-group gnus-newsgroup-name)))
6995         header)
6996     (cond
6997      ;; Is there such an article?
6998      ((gnus-summary-display-article 
6999        (gnus-summary-search-forward unread subject backward))
7000       (gnus-summary-position-cursor))
7001      ;; If not, we try the first unread, if that is wanted.
7002      ((and subject
7003            gnus-auto-select-same
7004            (gnus-summary-first-unread-article))
7005       (message "Wrapped"))
7006      ;; Try to get next/previous article not displayed in this group.
7007      ((and gnus-auto-extend-newsgroup
7008            (not unread) (not subject)
7009            (setq header (gnus-more-header-forward backward)))
7010       (gnus-extend-newsgroup header backward)
7011       (let ((buffer-read-only nil))
7012         (goto-char (if backward (point-min) (point-max)))
7013         (gnus-summary-prepare-threads (list header) 0))
7014       (gnus-summary-goto-article (if backward gnus-newsgroup-begin
7015                                    gnus-newsgroup-end)))
7016      ;; Go to next/previous group.
7017      (t
7018       (or (eq method 'nndigest)
7019           (gnus-summary-jump-to-group gnus-newsgroup-name))
7020       (let ((cmd (aref (this-command-keys) 0))
7021             (group 
7022              (if (eq gnus-keep-same-level 'best) 
7023                  (gnus-summary-best-group gnus-newsgroup-name)
7024                (gnus-summary-search-group backward gnus-keep-same-level))))
7025         ;; Keep just the event type of CMD.
7026         (and (listp cmd) (setq cmd (car cmd)))
7027         ;; Select next unread newsgroup automagically.
7028         (cond 
7029          ((not gnus-auto-select-next)
7030           (message "No more%s articles" (if unread " unread" "")))
7031          ((eq gnus-auto-select-next 'quietly)
7032           ;; Select quietly.
7033           (if (eq method 'nndigest)
7034               (gnus-summary-exit)
7035             (message "No more%s articles (%s)..."
7036                      (if unread " unread" "") 
7037                      (if group (concat "selecting " group)
7038                        "exiting"))
7039             (gnus-summary-next-group nil group backward)))
7040          (t
7041           (let ((keystrokes '(?\C-n ?\C-p))
7042                 key)
7043             (while (or (null key) (memq key keystrokes))
7044               (message 
7045                "No more%s articles%s" (if unread " unread" "")
7046                (if (and group (not (eq method 'nndigest)))
7047                    (format " (Type %s for %s [%s])"
7048                            (single-key-description cmd) group
7049                            (car (gnus-gethash group gnus-newsrc-hashtb)))
7050                  (format " (Type %s to exit %s)"
7051                          (single-key-description cmd)
7052                          gnus-newsgroup-name)))
7053               ;; Confirm auto selection.
7054               (let* ((event (read-event)))
7055                 (setq key (if (listp event) (car event) event))
7056                 (if (member key keystrokes)
7057                     (let ((obuf (current-buffer)))
7058                       (switch-to-buffer gnus-group-buffer)
7059                       (gnus-group-jump-to-group group)
7060                       (execute-kbd-macro (char-to-string key))
7061                       (setq group (gnus-group-group-name))
7062                       (switch-to-buffer obuf)))))
7063             (if (eq key cmd)
7064                 (if (or (not group) (eq method 'nndigest))
7065                     (gnus-summary-exit)
7066                   (gnus-summary-next-group nil group backward))
7067               (setq unread-command-events (list key)))))))))))
7068
7069 (defun gnus-summary-next-unread-article ()
7070   "Select unread article after current one."
7071   (interactive)
7072   (gnus-summary-next-article t (and gnus-auto-select-same
7073                                     (gnus-summary-subject-string))))
7074
7075 (defun gnus-summary-prev-article (unread &optional subject)
7076   "Select the article after the current one.
7077 If UNREAD is non-nil, only unread articles are selected."
7078   (interactive "P")
7079   (gnus-summary-next-article unread subject t))
7080
7081 (defun gnus-summary-prev-unread-article ()
7082   "Select unred article before current one."
7083   (interactive)
7084   (gnus-summary-prev-article t (and gnus-auto-select-same
7085                                     (gnus-summary-subject-string))))
7086
7087 (defun gnus-summary-next-page (lines &optional circular)
7088   "Show next page of selected article.
7089 If end of article, select next article.
7090 Argument LINES specifies lines to be scrolled up.
7091 If CIRCULAR is non-nil, go to the start of the article instead of 
7092 instead of selecting the next article when reaching the end of the
7093 current article." 
7094   (interactive "P")
7095   (setq gnus-summary-buffer (current-buffer))
7096   (gnus-set-global-variables)
7097   (let ((article (gnus-summary-article-number))
7098         (endp nil))
7099     (if (or (null gnus-current-article)
7100             (null gnus-article-current)
7101             (/= article (cdr gnus-article-current))
7102             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7103         ;; Selected subject is different from current article's.
7104         (gnus-summary-display-article article)
7105       (gnus-configure-windows 'article)
7106       (gnus-eval-in-buffer-window
7107        gnus-article-buffer
7108        (setq endp (gnus-article-next-page lines)))
7109       (if endp
7110           (cond (circular
7111                  (gnus-summary-beginning-of-article))
7112                 (lines
7113                  (message "End of message"))
7114                 ((null lines)
7115                  (gnus-summary-next-unread-article))))
7116       (and gnus-summary-buffer
7117            (buffer-name gnus-summary-buffer)
7118            (progn
7119              (pop-to-buffer gnus-summary-buffer)
7120              (gnus-summary-position-cursor))))))
7121
7122 (defun gnus-summary-prev-page (lines)
7123   "Show previous page of selected article.
7124 Argument LINES specifies lines to be scrolled down."
7125   (interactive "P")
7126   (let ((article (gnus-summary-article-number)))
7127     (if (or (null gnus-current-article)
7128             (null gnus-article-current)
7129             (/= article (cdr gnus-article-current))
7130             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7131         ;; Selected subject is different from current article's.
7132         (gnus-summary-display-article article)
7133       (gnus-configure-windows 'article)
7134       (pop-to-buffer gnus-summary-buffer)
7135       (gnus-eval-in-buffer-window gnus-article-buffer
7136         (gnus-article-prev-page lines))))
7137   (gnus-summary-position-cursor))
7138
7139 (defun gnus-summary-scroll-up (lines)
7140   "Scroll up (or down) one line current article.
7141 Argument LINES specifies lines to be scrolled up (or down if negative)."
7142   (interactive "p")
7143   (or (gnus-summary-select-article nil nil 'pseudo)
7144       (gnus-eval-in-buffer-window 
7145        gnus-article-buffer
7146        (cond ((> lines 0)
7147               (if (gnus-article-next-page lines)
7148                   (message "End of message")))
7149              ((< lines 0)
7150               (gnus-article-prev-page (- lines))))))
7151   (gnus-summary-position-cursor))
7152
7153 (defun gnus-summary-next-same-subject ()
7154   "Select next article which has the same subject as current one."
7155   (interactive)
7156   (gnus-summary-next-article nil (gnus-summary-subject-string)))
7157
7158 (defun gnus-summary-prev-same-subject ()
7159   "Select previous article which has the same subject as current one."
7160   (interactive)
7161   (gnus-summary-prev-article nil (gnus-summary-subject-string)))
7162
7163 (defun gnus-summary-next-unread-same-subject ()
7164   "Select next unread article which has the same subject as current one."
7165   (interactive)
7166   (gnus-summary-next-article t (gnus-summary-subject-string)))
7167
7168 (defun gnus-summary-prev-unread-same-subject ()
7169   "Select previous unread article which has the same subject as current one."
7170   (interactive)
7171   (gnus-summary-prev-article t (gnus-summary-subject-string)))
7172
7173 (defun gnus-summary-first-unread-article ()
7174   "Select the first unread article. 
7175 Return nil if there are no unread articles."
7176   (interactive)
7177   (prog1
7178       (if (gnus-summary-first-subject t)
7179           (gnus-summary-display-article (gnus-summary-article-number)))
7180     (gnus-summary-position-cursor)))
7181
7182 (defun gnus-summary-best-unread-article ()
7183   "Select the unread article with the highest score."
7184   (interactive)
7185   (gnus-set-global-variables)
7186   (let ((scored gnus-newsgroup-scored)
7187         (best -1000000)
7188         article art)
7189     (while scored
7190       (or (> best (cdr (car scored)))
7191           (and (memq (setq art (car (car scored))) gnus-newsgroup-unreads)
7192                (not (memq art gnus-newsgroup-marked))
7193                (not (memq art gnus-newsgroup-dormant))
7194                (if (= best (cdr (car scored)))
7195                    (setq article (min art article))
7196                  (setq article art)
7197                  (setq best (cdr (car scored))))))
7198       (setq scored (cdr scored)))
7199     (if article 
7200         (gnus-summary-goto-article article)
7201       (gnus-summary-first-unread-article))
7202     (gnus-summary-position-cursor)))
7203
7204 (defun gnus-summary-goto-article (article &optional all-headers)
7205   "Fetch ARTICLE and display it if it exists.
7206 If ALL-HEADERS is non-nil, no header lines are hidden."
7207   (interactive
7208    (list
7209     (string-to-int
7210      (completing-read 
7211       "Article number: "
7212       (mapcar (lambda (headers) (list (int-to-string (header-number headers))))
7213               gnus-newsgroup-headers) 
7214       nil 'require-match))))
7215   (prog1
7216       (and (gnus-summary-goto-subject article)
7217            (gnus-summary-display-article article all-headers))
7218     (gnus-summary-position-cursor)))
7219
7220 (defun gnus-summary-goto-last-article ()
7221   "Go to the last article."
7222   (interactive)
7223   (prog1
7224       (and gnus-last-article
7225            (gnus-summary-goto-article gnus-last-article))
7226     (gnus-summary-position-cursor)))
7227
7228 (defun gnus-summary-pop-article (number)
7229   "Pop one article off the history and go to the previous.
7230 NUMBER articles will be popped off."
7231   (interactive "p")
7232   (let (to)
7233     (setq gnus-newsgroup-history
7234           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7235     (if to
7236         (gnus-summary-goto-article (car to))
7237       (error "Article history empty")))
7238   (gnus-summary-position-cursor))
7239
7240 ;; Summary article oriented commands
7241
7242 (defun gnus-summary-refer-parent-article (n)
7243   "Refer parent article N times.
7244 The difference between N and the number of articles fetched is returned."
7245   (interactive "p")
7246   (gnus-set-global-variables)
7247   (while 
7248       (and 
7249        (> n 0)
7250        (let ((ref (header-references (gnus-get-header-by-number
7251                                       (gnus-summary-article-number)))))
7252          (if (and ref (not (equal ref ""))
7253                   (string-match "<[^<>]*>[ \t]*$" ref))
7254              (gnus-summary-refer-article 
7255               (substring ref (match-beginning 0) (match-end 0))))))
7256     (setq n (1- n)))
7257   (or (zerop n) (message "No references in article or expired article."))
7258   (gnus-summary-position-cursor)
7259   n)
7260     
7261 (defun gnus-summary-refer-article (message-id)
7262   "Refer article specified by MESSAGE-ID.
7263 NOTE: This command only works with newsgroup that use NNTP."
7264   (interactive "sMessage-ID: ")
7265   (if (or (not (stringp message-id))
7266           (zerop (length message-id)))
7267       ()
7268     ;; Construct the correct Message-ID if necessary.
7269     ;; Suggested by tale@pawl.rpi.edu.
7270     (or (string-match "^<" message-id)
7271         (setq message-id (concat "<" message-id)))
7272     (or (string-match ">$" message-id)
7273         (setq message-id (concat message-id ">")))
7274     (let ((header (car (gnus-gethash (downcase message-id)
7275                                      gnus-newsgroup-dependencies))))
7276       (if header
7277           (or (gnus-summary-goto-article (header-number header))
7278               ;; The header has been read, but the article had been
7279               ;; expunged, so we insert it again.
7280               (progn
7281                 (gnus-summary-insert-line
7282                  nil header 0 nil gnus-read-mark nil nil
7283                  (header-subject header))
7284                 (forward-line -1)
7285                 (header-number header)))
7286         (let ((gnus-override-method gnus-refer-article-method))
7287           (and gnus-refer-article-method
7288                (or (gnus-server-opened gnus-refer-article-method)
7289                    (gnus-open-server gnus-refer-article-method)))
7290           (if (gnus-article-prepare 
7291                message-id nil (gnus-read-header message-id))
7292               (progn
7293                 (gnus-summary-insert-line 
7294                  nil gnus-current-headers 0 nil gnus-read-mark nil nil 
7295                  (header-subject gnus-current-headers))
7296                 (forward-line -1)
7297                 (gnus-summary-position-cursor)
7298                 (gnus-summary-update-line)
7299                 message-id)
7300             (message "No such references")
7301             nil))))))
7302
7303 (defun gnus-summary-enter-digest-group ()
7304   "Enter a digest group based on the current article."
7305   (interactive)
7306   (gnus-set-global-variables)
7307   (gnus-summary-select-article)
7308   (let ((name (format "%s/%d" 
7309                       (gnus-group-prefixed-name 
7310                        gnus-newsgroup-name (list 'nndigest "")) 
7311                       gnus-current-article))
7312         (buf (current-buffer)))
7313     (set-buffer gnus-group-buffer)
7314     (gnus-sethash 
7315      name 
7316      (list t nil (list name gnus-level-default-subscribed nil nil 
7317                        (list 'nndigest gnus-article-buffer
7318                              (cons 'quit-buffer buf))))
7319      gnus-newsrc-hashtb)
7320     (condition-case ()
7321         (gnus-group-read-group t nil name)
7322       (error nil)
7323       (quit nil))
7324     (if (not (equal major-mode 'gnus-group-mode))
7325         ()
7326       (switch-to-buffer buf)
7327       (gnus-set-global-variables)
7328       (gnus-configure-windows 'summary)
7329       (message "Article not a digest?"))))
7330   
7331 (defun gnus-summary-isearch-article ()
7332   "Do incremental search forward on current article."
7333   (interactive)
7334   (gnus-summary-select-article)
7335   (gnus-eval-in-buffer-window gnus-article-buffer
7336                               (isearch-forward)))
7337
7338 (defun gnus-summary-search-article-forward (regexp)
7339   "Search for an article containing REGEXP forward.
7340 gnus-select-article-hook is not called during the search."
7341   (interactive
7342    (list (read-string
7343           (concat "Search forward (regexp): "
7344                   (if gnus-last-search-regexp
7345                       (concat "(default " gnus-last-search-regexp ") "))))))
7346   (if (string-equal regexp "")
7347       (setq regexp (or gnus-last-search-regexp ""))
7348     (setq gnus-last-search-regexp regexp))
7349   (if (gnus-summary-search-article regexp nil)
7350       (gnus-eval-in-buffer-window 
7351        gnus-article-buffer
7352        (recenter 0))
7353     (error "Search failed: \"%s\"" regexp)))
7354
7355 (defun gnus-summary-search-article-backward (regexp)
7356   "Search for an article containing REGEXP backward.
7357 gnus-select-article-hook is not called during the search."
7358   (interactive
7359    (list (read-string
7360           (concat "Search backward (regexp): "
7361                   (if gnus-last-search-regexp
7362                       (concat "(default " gnus-last-search-regexp ") "))))))
7363   (if (string-equal regexp "")
7364       (setq regexp (or gnus-last-search-regexp ""))
7365     (setq gnus-last-search-regexp regexp))
7366   (if (gnus-summary-search-article regexp t)
7367       (gnus-eval-in-buffer-window
7368        gnus-article-buffer
7369        (recenter 0))
7370     (error "Search failed: \"%s\"" regexp)))
7371
7372 (defun gnus-summary-search-article (regexp &optional backward)
7373   "Search for an article containing REGEXP.
7374 Optional argument BACKWARD means do search for backward.
7375 gnus-select-article-hook is not called during the search."
7376   (let ((gnus-select-article-hook nil)  ;Disable hook.
7377         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7378         (re-search
7379          (if backward
7380              (function re-search-backward) (function re-search-forward)))
7381         (found nil)
7382         (last nil))
7383     ;; Hidden thread subtrees must be searched for ,too.
7384     (gnus-summary-show-all-threads)
7385     (if (eobp) (forward-line -1))
7386     ;; First of all, search current article.
7387     ;; We don't want to read article again from NNTP server nor reset
7388     ;; current point.
7389     (gnus-summary-select-article)
7390     (message "Searching article: %d..." gnus-current-article)
7391     (setq last gnus-current-article)
7392     (gnus-eval-in-buffer-window gnus-article-buffer
7393       (save-restriction
7394         (widen)
7395         ;; Begin search from current point.
7396         (setq found (funcall re-search regexp nil t))))
7397     ;; Then search next articles.
7398     (while (and (not found)
7399                 (gnus-summary-display-article 
7400                  (gnus-summary-search-subject backward nil nil)))
7401       (message "Searching article: %d..." gnus-current-article)
7402       (gnus-eval-in-buffer-window gnus-article-buffer
7403         (save-restriction
7404           (widen)
7405           (goto-char (if backward (point-max) (point-min)))
7406           (setq found (funcall re-search regexp nil t)))))
7407     (message "")
7408     ;; Adjust article pointer.
7409     (or (eq last gnus-current-article)
7410         (setq gnus-last-article last))
7411     ;; Return T if found such article.
7412     found))
7413
7414 (defun gnus-summary-execute-command (header regexp command &optional backward)
7415   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7416 If HEADER is an empty string (or nil), the match is done on the entire
7417 article. If BACKWARD (the prefix) is non-nil, search backward instead."
7418   (interactive
7419    (list (let ((completion-ignore-case t))
7420            (completing-read 
7421             "Header name: "
7422             (mapcar (string) (list string)
7423                     '("Number" "Subject" "From" "Lines" "Date"
7424                       "Message-ID" "Xref" "References")
7425                     nil 'require-match)))
7426          (read-string "Regexp: ")
7427          (read-key-sequence "Command: ")
7428          current-prefix-arg))
7429   ;; Hidden thread subtrees must be searched as well.
7430   (gnus-summary-show-all-threads)
7431   ;; We don't want to change current point nor window configuration.
7432   (save-excursion
7433     (save-window-excursion
7434       (message "Executing %s..." (key-description command))
7435       ;; We'd like to execute COMMAND interactively so as to give arguments.
7436       (gnus-execute header regexp
7437                     (` (lambda ()
7438                          (call-interactively '(, (key-binding command)))))
7439                     backward)
7440       (message "Executing %s... done" (key-description command)))))
7441
7442 (defun gnus-summary-beginning-of-article ()
7443   "Scroll the article back to the beginning."
7444   (interactive)
7445   (gnus-summary-select-article)
7446   (gnus-eval-in-buffer-window
7447    gnus-article-buffer
7448    (widen)
7449    (goto-char (point-min))
7450    (and gnus-break-pages (gnus-narrow-to-page))))
7451
7452 (defun gnus-summary-end-of-article ()
7453   "Scroll to the end of the article."
7454   (interactive)
7455   (gnus-summary-select-article)
7456   (gnus-eval-in-buffer-window 
7457    gnus-article-buffer
7458    (widen)
7459    (goto-char (point-max))
7460    (and gnus-break-pages (gnus-narrow-to-page))))
7461
7462 (defun gnus-summary-show-article ()
7463   "Force re-fetching of the current article."
7464   (interactive)
7465   (gnus-summary-select-article gnus-have-all-headers t t))
7466
7467 (defun gnus-summary-toggle-header (arg)
7468   "Show the headers if they are hidden, or hide them if they are shown.
7469 If ARG is a positive number, show the entire header.
7470 If ARG is a negative number, hide the unwanted header lines."
7471   (interactive "P")
7472   (gnus-set-global-variables)
7473   (save-excursion
7474     (set-buffer gnus-article-buffer)
7475     (let ((buffer-read-only nil))
7476       (if (numberp arg) 
7477           (if (> arg 0) (remove-text-properties 1 (point-max) '(invisible t))
7478             (if (< arg 0) (run-hooks 'gnus-article-display-hook)))
7479         (if (text-property-any 1 (point-max) 'invisible t)
7480             (remove-text-properties 1 (point-max) '(invisible t))
7481           (let ((gnus-have-all-headers nil))
7482             (run-hooks 'gnus-article-display-hook))))
7483       (set-window-point (get-buffer-window (current-buffer)) 1))))
7484
7485 (defun gnus-summary-show-all-headers ()
7486   "Make all header lines visible."
7487   (interactive)
7488   (gnus-article-show-all-headers))
7489
7490 (defun gnus-summary-toggle-mime (arg)
7491   "Toggle MIME processing.
7492 If ARG is a positive number, turn MIME processing on."
7493   (interactive "P")
7494   (setq gnus-show-mime
7495         (if (null arg) (not gnus-show-mime)
7496           (> (prefix-numeric-value arg) 0)))
7497   (gnus-summary-select-article t 'force))
7498
7499 (defun gnus-summary-caesar-message (rotnum)
7500   "Caesar rotates all letters of current message by 13/47 places.
7501 With prefix arg, specifies the number of places to rotate each letter forward.
7502 Caesar rotates Japanese letters by 47 places in any case."
7503   (interactive "P")
7504   (gnus-summary-select-article)
7505   (let ((mail-header-separator "")) ; !!! Is this necessary?
7506     (gnus-overload-functions)
7507     (gnus-eval-in-buffer-window 
7508      gnus-article-buffer
7509      (save-restriction
7510        (widen)
7511        ;; We don't want to jump to the beginning of the message.
7512        ;; `save-excursion' does not do its job.
7513        (move-to-window-line 0)
7514        (let ((last (point)))
7515          (news-caesar-buffer-body rotnum)
7516          (goto-char last)
7517          (recenter 0))))))
7518
7519 (defun gnus-summary-stop-page-breaking ()
7520   "Stop page breaking in the current article."
7521   (interactive)
7522   (gnus-summary-select-article)
7523   (gnus-eval-in-buffer-window gnus-article-buffer (widen)))
7524
7525 ;; Suggested by Brian Edmonds <bedmonds@prodigy.bc.ca>.
7526
7527 (defun gnus-summary-move-article (n &optional to-newsgroup select-method)
7528   "Move the current article to a different newsgroup.
7529 If N is a positive number, move the N next articles.
7530 If N is a negative number, move the N previous articles.
7531 If N is nil and any articles have been marked with the process mark,
7532 move those articles instead.
7533 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to. 
7534 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
7535 re-spool using this method.
7536 For this function to work, both the current newsgroup and the
7537 newsgroup that you want to move to have to support the `request-move'
7538 and `request-accept' functions. (Ie. mail newsgroups at present.)"
7539   (interactive "P")
7540   (gnus-set-global-variables)
7541   (or (gnus-check-backend-function 'request-move-article gnus-newsgroup-name)
7542       (error "The current newsgroup does not support article moving"))
7543   (let ((articles (gnus-summary-work-articles n))
7544         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
7545         art-group)
7546     (if (and (not to-newsgroup) (not select-method))
7547         (setq to-newsgroup
7548               (completing-read 
7549                (format "Where do you want to move %s? %s"
7550                        (if (> (length articles) 1)
7551                            (format "these %d articles" (length articles))
7552                          "this article")
7553                        (if gnus-current-move-group
7554                            (format "(%s default) " gnus-current-move-group)
7555                          ""))
7556                gnus-active-hashtb nil nil prefix)))
7557     (if to-newsgroup
7558         (progn
7559           (if (or (string= to-newsgroup "") (string= to-newsgroup prefix))
7560               (setq to-newsgroup (or gnus-current-move-group "")))
7561           (or (gnus-gethash to-newsgroup gnus-active-hashtb)
7562               (gnus-activate-newsgroup to-newsgroup)
7563               (error "No such group: %s" to-newsgroup))
7564           (setq gnus-current-move-group to-newsgroup)))
7565     (or (gnus-check-backend-function 'request-accept-article 
7566                                      (or select-method to-newsgroup))
7567         (error "%s does not support article moving" to-newsgroup))
7568     (message "Moving to %s: %s..." (or select-method to-newsgroup) articles)
7569     (while articles
7570       (if (setq art-group
7571                 (gnus-request-move-article 
7572                  (car articles)                   ; Article to move
7573                  gnus-newsgroup-name              ; From newsgrouo
7574                  (nth 1 (gnus-find-method-for-group 
7575                          gnus-newsgroup-name))    ; Server
7576                  (list 'gnus-request-accept-article 
7577                        (if select-method
7578                            (list 'quote select-method)
7579                          to-newsgroup)
7580                        (not (cdr articles)))     ; Accept form
7581                  (not (cdr articles))))          ; Only save nov last time
7582           (let* ((buffer-read-only nil)
7583                  (entry 
7584                   (or
7585                    (gnus-gethash (car art-group) gnus-newsrc-hashtb)
7586                    (gnus-gethash 
7587                     (gnus-group-prefixed-name 
7588                      (car art-group) 
7589                      (if select-method (list select-method "")
7590                        (gnus-find-method-for-group to-newsgroup)))
7591                     gnus-newsrc-hashtb)))
7592                  (info (nth 2 entry))
7593                  (article (car articles))
7594                  (marked (nth 3 info)))
7595             (gnus-summary-goto-subject article)
7596             (beginning-of-line)
7597             (delete-region (point)
7598                            (progn (forward-line 1) (point)))
7599             (if (not (memq article gnus-newsgroup-unreads))
7600                 (setcar (cdr (cdr info))
7601                         (gnus-add-to-range (nth 2 info) 
7602                                            (list (cdr art-group)))))
7603             ;; Copy any marks over to the new group.
7604             (let ((marks '((tick . gnus-newsgroup-marked)
7605                            (dormant . gnus-newsgroup-dormant)
7606                            (expire . gnus-newsgroup-expirable)
7607                            (bookmark . gnus-newsgroup-bookmarks)
7608                         ;   (score . gnus-newsgroup-scored)
7609                            (reply . gnus-newsgroup-replied)))
7610                   (to-article (cdr art-group)))
7611               (while marks
7612                 (if (memq article (symbol-value (cdr (car marks))))
7613                     (gnus-add-marked-articles 
7614                      (car info) (car (car marks)) (list to-article) info))
7615                 (setq marks (cdr marks))))
7616             (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7617             (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
7618             (setq gnus-newsgroup-dormant
7619                   (delq article gnus-newsgroup-dormant)))
7620         (message "Couldn't move article %s" (car articles)))
7621       (gnus-summary-remove-process-mark (car articles))
7622       (setq articles (cdr articles)))))
7623
7624 (defun gnus-summary-respool-article (n &optional respool-method)
7625   "Respool the current article.
7626 The article will be squeezed through the mail spooling process again,
7627 which means that it will be put in some mail newsgroup or other
7628 depending on `nnmail-split-methods'.
7629 If N is a positive number, respool the N next articles.
7630 If N is a negative number, respool the N previous articles.
7631 If N is nil and any articles have been marked with the process mark,
7632 respool those articles instead.
7633
7634 Respooling can be done both from mail groups and \"real\" newsgroups.
7635 In the former case, the articles in question will be moved from the
7636 current group into whatever groups they are destined to.  In the
7637 latter case, they will be copied into the relevant groups."
7638   (interactive "P")
7639   (gnus-set-global-variables)
7640   (let ((respool-methods (gnus-methods-using 'respool))
7641         (methname 
7642          (symbol-name (car (gnus-find-method-for-group gnus-newsgroup-name)))))
7643     (or respool-method
7644         (setq respool-method
7645               (completing-read
7646                "What method do you want to use when respooling? "
7647                respool-methods nil t methname)))
7648     (or (string= respool-method "")
7649         (if (assoc (symbol-name
7650                     (car (gnus-find-method-for-group gnus-newsgroup-name)))
7651                    respool-methods)
7652             (gnus-summary-move-article n nil (intern respool-method))
7653           (gnus-summary-copy-article n nil (intern respool-method))))))
7654
7655 ;; Suggested by gregj@unidata.com (Gregory J. Grubbs).
7656 (defun gnus-summary-copy-article (n &optional to-newsgroup select-method)
7657   "Move the current article to a different newsgroup.
7658 If N is a positive number, move the N next articles.
7659 If N is a negative number, move the N previous articles.
7660 If N is nil and any articles have been marked with the process mark,
7661 move those articles instead.
7662 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to. 
7663 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
7664 re-spool using this method.
7665 For this function to work, the newsgroup that you want to move to have
7666 to support the `request-move' and `request-accept'
7667 functions. (Ie. mail newsgroups at present.)"
7668   (interactive "P")
7669   (gnus-set-global-variables)
7670   (let ((articles (gnus-summary-work-articles n))
7671         (copy-buf (get-buffer-create "*copy work*"))
7672         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
7673         art-group)
7674     (buffer-disable-undo copy-buf)
7675     (if (and (not to-newsgroup) (not select-method))
7676         (setq to-newsgroup
7677               (completing-read 
7678                (format "Where do you want to copy %s? %s"
7679                        (if (> (length articles) 1)
7680                            (format "these %d articles" (length articles))
7681                          "this article")
7682                        (if gnus-current-move-group
7683                            (format "(%s default) " gnus-current-move-group)
7684                          ""))
7685                gnus-active-hashtb nil nil prefix)))
7686     (if to-newsgroup
7687         (progn
7688           (if (or (string= to-newsgroup "") (string= to-newsgroup prefix))
7689               (setq to-newsgroup (or gnus-current-move-group "")))
7690           (or (gnus-gethash to-newsgroup gnus-active-hashtb)
7691               (gnus-activate-newsgroup to-newsgroup)
7692               (error "No such group: %s" to-newsgroup))
7693           (setq gnus-current-move-group to-newsgroup)))
7694     (or (gnus-check-backend-function 'request-accept-article 
7695                                      (or select-method to-newsgroup))
7696         (error "%s does not support article copying" to-newsgroup))
7697     (message "Copying to %s: %s..." (or select-method to-newsgroup) articles)
7698     (while articles
7699       (if (setq art-group
7700                 (save-excursion
7701                   (set-buffer copy-buf)
7702                   (gnus-request-article-this-buffer
7703                    (car articles) gnus-newsgroup-name)
7704                   (gnus-request-accept-article
7705                    (if select-method (quote select-method) to-newsgroup)
7706                    (not (cdr articles)))))
7707           (let* ((entry 
7708                   (or
7709                    (gnus-gethash (car art-group) gnus-newsrc-hashtb)
7710                    (gnus-gethash 
7711                     (gnus-group-prefixed-name 
7712                      (car art-group) 
7713                      (if select-method (list select-method "")
7714                        (gnus-find-method-for-group to-newsgroup)))
7715                     gnus-newsrc-hashtb)))
7716                  (info (nth 2 entry))
7717                  (article (car articles))
7718                  (marked (nth 3 info)))
7719             (if (not (memq article gnus-newsgroup-unreads))
7720                 (setcar (cdr (cdr info))
7721                         (gnus-add-to-range (nth 2 info) 
7722                                            (list (cdr art-group)))))
7723             ;; Copy any marks over to the new group.
7724             (let ((marks '((tick . gnus-newsgroup-marked)
7725                            (dormant . gnus-newsgroup-dormant)
7726                            (expire . gnus-newsgroup-expirable)
7727                            (bookmark . gnus-newsgroup-bookmarks)
7728                         ;   (score . gnus-newsgroup-scored)
7729                            (reply . gnus-newsgroup-replied)))
7730                   (to-article (cdr art-group)))
7731               (while marks
7732                 (if (memq article (symbol-value (cdr (car marks))))
7733                     (gnus-add-marked-articles 
7734                      (car info) (car (car marks)) (list to-article) info))
7735                 (setq marks (cdr marks)))))
7736         (message "Couldn't copy article %s" (car articles)))
7737       (gnus-summary-remove-process-mark (car articles))
7738       (setq articles (cdr articles)))
7739     (kill-buffer copy-buf)))
7740
7741 (defun gnus-summary-expire-articles ()
7742   "Expire all articles that are marked as expirable in the current group."
7743   (interactive)
7744   (if (and gnus-newsgroup-expirable
7745            (gnus-check-backend-function 
7746             'request-expire-articles gnus-newsgroup-name))
7747       (let ((expirable gnus-newsgroup-expirable))
7748         ;; The list of articles that weren't expired is returned.
7749         (setq gnus-newsgroup-expirable 
7750               (gnus-request-expire-articles gnus-newsgroup-expirable
7751                                             gnus-newsgroup-name))
7752         ;; We go through the old list of expirable, and mark all
7753         ;; really expired articles as non-existant.
7754         (while expirable
7755           (or (memq (car expirable) gnus-newsgroup-expirable)
7756               (gnus-summary-mark-as-read (car expirable) "%"))
7757           (setq expirable (cdr expirable))))))
7758
7759 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
7760 (defun gnus-summary-delete-article (n)
7761   "Delete the N next (mail) articles.
7762 This command actually deletes articles. This is not a marking
7763 command. The article will disappear forever from you life, never to
7764 return. 
7765 If N is negative, delete backwards.
7766 If N is nil and articles have been marked with the process mark,
7767 delete these instead."
7768   (interactive "P")
7769   (or (gnus-check-backend-function 'request-expire-articles 
7770                                    gnus-newsgroup-name)
7771       (error "The current newsgroup does not support article deletion."))
7772   ;; Compute the list of articles to delete.
7773   (let ((articles (gnus-summary-work-articles n)))
7774     (if (and gnus-novice-user
7775              (not (gnus-y-or-n-p 
7776                    (format "Do you really want to delete %s forever? "
7777                            (if (> (length articles) 1) "these articles"
7778                              "this article")))))
7779         ()
7780       ;; Delete the articles.
7781       (setq gnus-newsgroup-expirable 
7782             (gnus-request-expire-articles 
7783              articles gnus-newsgroup-name 'force))
7784       (while articles
7785         (gnus-summary-remove-process-mark (car articles))       
7786         ;; The backend might not have been able to delete the article
7787         ;; after all.  
7788         (or (memq (car articles) gnus-newsgroup-expirable)
7789             (gnus-summary-mark-as-read (car articles) gnus-canceled-mark))
7790         (setq articles (cdr articles)))))
7791   (gnus-summary-position-cursor))
7792
7793 (defun gnus-summary-edit-article ()
7794   "Enter into a buffer and edit the current article.
7795 This will have permanent effect only in mail groups."
7796   (interactive)
7797   (or (gnus-check-backend-function 
7798        'request-replace-article gnus-newsgroup-name)
7799       (error "The current newsgroup does not support article editing."))
7800   (gnus-summary-select-article t)
7801   (other-window 1)
7802   (message "C-c C-c to end edits")
7803   (setq buffer-read-only nil)
7804   (text-mode)
7805   (use-local-map (copy-keymap (current-local-map)))
7806   (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
7807   (goto-char 1)
7808   (search-forward "\n\n" nil t))
7809
7810 (defun gnus-summary-edit-article-done ()
7811   "Make edits to the current article permanent."
7812   (interactive)
7813   (if (not (gnus-request-replace-article 
7814             (cdr gnus-article-current) (car gnus-article-current) 
7815             (current-buffer)))
7816       (error "Couldn't replace article.")
7817     (gnus-article-mode)
7818     (use-local-map gnus-article-mode-map)
7819     (setq buffer-read-only t)
7820     (pop-to-buffer gnus-summary-buffer)))      
7821
7822 ;; Summary score commands.
7823
7824 ;; Suggested by boubaker@cenatls.cena.dgac.fr.
7825
7826 (defun gnus-summary-raise-score (n)
7827   "Raise the score of the current article by N."
7828   (interactive "p")
7829   (gnus-summary-set-score (+ (gnus-summary-article-score) n)))
7830
7831 (defun gnus-summary-lower-score (n)
7832   "Lower the score of the current article by N."
7833   (interactive "p")
7834   (gnus-summary-raise-score (- n)))
7835
7836 (defun gnus-summary-set-score (n)
7837   "Set the score of the current article to N."
7838   (interactive "p")
7839   ;; Skip dummy header line.
7840   (save-excursion
7841     (if (eq (gnus-summary-article-mark) ?Z)
7842         (forward-line 1))
7843     (let ((buffer-read-only nil))
7844       ;; Set score.
7845       (gnus-summary-update-mark
7846        (if (= n (or gnus-summary-default-score 0)) ? 
7847          (if (< n (or gnus-summary-default-score 0)) 
7848              gnus-score-below-mark gnus-score-over-mark)) 'score))
7849     (let* ((article (gnus-summary-article-number))
7850            (score (assq article gnus-newsgroup-scored)))
7851       (if score (setcdr score n)
7852         (setq gnus-newsgroup-scored 
7853               (cons (cons article n) gnus-newsgroup-scored))))
7854     (gnus-summary-update-line)))
7855
7856 ;; Summary marking commands.
7857
7858 (defun gnus-summary-raise-same-subject-and-select (score)
7859   "Raise articles which has the same subject with SCORE and select the next."
7860   (interactive "p")
7861   (let ((subject (gnus-summary-subject-string)))
7862     (gnus-summary-raise-score score)
7863     (while (gnus-summary-search-subject nil nil subject)
7864       (gnus-summary-raise-score score))
7865     (gnus-summary-next-article t)))
7866
7867 (defun gnus-summary-raise-same-subject (score)
7868   "Raise articles which has the same subject with SCORE."
7869   (interactive "p")
7870   (let ((subject (gnus-summary-subject-string)))
7871     (gnus-summary-raise-score score)
7872     (while (gnus-summary-search-subject nil nil subject)
7873       (gnus-summary-raise-score score))
7874     (gnus-summary-next-subject 1 t)))
7875
7876 (defun gnus-summary-raise-thread (score)
7877   "Raise articles under current thread with SCORE."
7878   (interactive "P")
7879   (setq score (1- (gnus-score-default score)))
7880   (let (e)
7881     (save-excursion
7882       (let ((level (gnus-summary-thread-level)))
7883         (gnus-summary-raise-score score)
7884         (while (and (zerop (gnus-summary-next-subject 1 nil t))
7885                     (> (gnus-summary-thread-level) level))
7886           (gnus-summary-raise-score score))
7887         (setq e (point))))
7888     (let ((gnus-summary-check-current t))
7889       (or (zerop (gnus-summary-next-subject 1 t))
7890           (goto-char e))))
7891   (gnus-summary-recenter)
7892   (gnus-summary-position-cursor)
7893   (gnus-set-mode-line 'summary))
7894
7895 (defun gnus-summary-lower-same-subject-and-select (score)
7896   "Raise articles which has the same subject with SCORE and select the next."
7897   (interactive "p")
7898   (gnus-summary-raise-same-subject-and-select (- score)))
7899
7900 (defun gnus-summary-lower-same-subject (score)
7901   "Raise articles which has the same subject with SCORE."
7902   (interactive "p")
7903   (gnus-summary-raise-same-subject (- score)))
7904
7905 (defun gnus-summary-lower-thread (score)
7906   "Raise articles under current thread with SCORE."
7907   (interactive "P")
7908   (gnus-summary-raise-thread (- (1- (gnus-score-default score)))))
7909
7910 (defun gnus-summary-kill-same-subject-and-select (unmark)
7911   "Mark articles which has the same subject as read, and then select the next.
7912 If UNMARK is positive, remove any kind of mark.
7913 If UNMARK is negative, tick articles."
7914   (interactive "P")
7915   (if unmark
7916       (setq unmark (prefix-numeric-value unmark)))
7917   (let ((count
7918          (gnus-summary-mark-same-subject
7919           (gnus-summary-subject-string) unmark)))
7920     ;; Select next unread article. If auto-select-same mode, should
7921     ;; select the first unread article.
7922     (gnus-summary-next-article t (and gnus-auto-select-same
7923                                       (gnus-summary-subject-string)))
7924     (message "%d articles are marked as %s"
7925              count (if unmark "unread" "read"))))
7926
7927 (defun gnus-summary-kill-same-subject (unmark)
7928   "Mark articles which has the same subject as read. 
7929 If UNMARK is positive, remove any kind of mark.
7930 If UNMARK is negative, tick articles."
7931   (interactive "P")
7932   (if unmark
7933       (setq unmark (prefix-numeric-value unmark)))
7934   (let ((count
7935          (gnus-summary-mark-same-subject
7936           (gnus-summary-subject-string) unmark)))
7937     ;; If marked as read, go to next unread subject.
7938     (if (null unmark)
7939         ;; Go to next unread subject.
7940         (gnus-summary-next-subject 1 t))
7941     (message "%d articles are marked as %s"
7942              count (if unmark "unread" "read"))))
7943
7944 (defun gnus-summary-mark-same-subject (subject &optional unmark)
7945   "Mark articles with same SUBJECT as read, and return marked number.
7946 If optional argument UNMARK is positive, remove any kinds of marks.
7947 If optional argument UNMARK is negative, mark articles as unread instead."
7948   (let ((count 1))
7949     (save-excursion
7950       (cond ((null unmark)
7951              (gnus-summary-mark-as-read nil gnus-killed-mark))
7952             ((> unmark 0)
7953              (gnus-summary-tick-article nil t))
7954             (t
7955              (gnus-summary-tick-article)))
7956       (while (and subject
7957                   (gnus-summary-search-forward nil subject))
7958         (cond ((null unmark)
7959                (gnus-summary-mark-as-read nil gnus-killed-mark))
7960               ((> unmark 0)
7961                (gnus-summary-tick-article nil t))
7962               (t
7963                (gnus-summary-tick-article)))
7964         (setq count (1+ count))))
7965     ;; Hide killed thread subtrees.  Does not work properly always.
7966     ;;(and (null unmark)
7967     ;;     gnus-thread-hide-killed
7968     ;;     (gnus-summary-hide-thread))
7969     ;; Return number of articles marked as read.
7970     count))
7971
7972 (defun gnus-summary-mark-as-processable (n &optional unmark)
7973   "Set the process mark on the next N articles.
7974 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
7975 the process mark instead.  The difference between N and the actual
7976 number of articles marked is returned."
7977   (interactive "p")
7978   (let ((backward (< n 0))
7979         (n (abs n)))
7980   (while (and 
7981           (> n 0)
7982           (if unmark
7983               (gnus-summary-remove-process-mark (gnus-summary-article-number))
7984             (gnus-summary-set-process-mark (gnus-summary-article-number)))
7985           (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
7986     (setq n (1- n)))
7987   (if (/= 0 n) (message "No more articles"))
7988   (gnus-summary-recenter)
7989   (gnus-summary-position-cursor)
7990   n))
7991
7992 (defun gnus-summary-unmark-as-processable (n)
7993   "Remove the process mark from the next N articles.
7994 If N is negative, mark backward instead.  The difference between N and
7995 the actual number of articles marked is returned."
7996   (interactive "p")
7997   (gnus-summary-mark-as-processable n t))
7998
7999 (defun gnus-summary-unmark-all-processable ()
8000   "Remove the process mark from all articles."
8001   (interactive)
8002   (save-excursion
8003     (while gnus-newsgroup-processable
8004       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8005   (gnus-summary-position-cursor))
8006
8007 (defun gnus-summary-mark-as-expirable (n)
8008   "Mark N articles forward as expirable.
8009 If N is negative, mark backward instead. The difference between N and
8010 the actual number of articles marked is returned."
8011   (interactive "p")
8012   (gnus-summary-mark-forward n gnus-expirable-mark))
8013
8014 (defun gnus-summary-mark-article-as-replied (article)
8015   "Mark ARTICLE replied and update the summary line."
8016   (setq gnus-newsgroup-replied (cons article gnus-newsgroup-replied))
8017   (let ((buffer-read-only nil))
8018     (if (gnus-summary-goto-subject article)
8019         (progn
8020           (gnus-summary-update-mark gnus-replied-mark 'replied)
8021           t))))
8022
8023 (defun gnus-summary-set-bookmark (article)
8024   "Set a bookmark in current article."
8025   (interactive (list (gnus-summary-article-number)))
8026   (if (or (not (get-buffer gnus-article-buffer))
8027           (not gnus-current-article)
8028           (not gnus-article-current)
8029           (not (equal gnus-newsgroup-name (car gnus-article-current))))
8030       (error "No current article selected"))
8031   ;; Remove old bookmark, if one exists.
8032   (let ((old (assq article gnus-newsgroup-bookmarks)))
8033     (if old (setq gnus-newsgroup-bookmarks 
8034                   (delq old gnus-newsgroup-bookmarks))))
8035   ;; Set the new bookmark, which is on the form 
8036   ;; (article-number . line-number-in-body).
8037   (setq gnus-newsgroup-bookmarks 
8038         (cons 
8039          (cons article 
8040                (save-excursion
8041                  (set-buffer gnus-article-buffer)
8042                  (count-lines
8043                   (min (point)
8044                        (save-excursion
8045                          (goto-char 1)
8046                          (search-forward "\n\n" nil t)
8047                          (point)))
8048                   (point))))
8049          gnus-newsgroup-bookmarks))
8050   (message "A bookmark has been added to the current article."))
8051
8052 (defun gnus-summary-remove-bookmark (article)
8053   "Remove the bookmark from the current article."
8054   (interactive (list (gnus-summary-article-number)))
8055   ;; Remove old bookmark, if one exists.
8056   (let ((old (assq article gnus-newsgroup-bookmarks)))
8057     (if old 
8058         (progn
8059           (setq gnus-newsgroup-bookmarks 
8060                 (delq old gnus-newsgroup-bookmarks))
8061           (message "Removed bookmark."))
8062       (message "No bookmark in current article."))))
8063
8064 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8065 (defun gnus-summary-mark-as-dormant (n)
8066   "Mark N articles forward as dormant.
8067 If N is negative, mark backward instead.  The difference between N and
8068 the actual number of articles marked is returned."
8069   (interactive "p")
8070   (gnus-summary-mark-forward n gnus-dormant-mark))
8071
8072 (defun gnus-summary-set-process-mark (article)
8073   "Set the process mark on ARTICLE and update the summary line."
8074   (setq gnus-newsgroup-processable (cons article gnus-newsgroup-processable))
8075   (let ((buffer-read-only nil))
8076     (if (gnus-summary-goto-subject article)
8077         (progn
8078           (and (eq (gnus-summary-article-mark) ?Z)
8079                (forward-line 1))
8080           (gnus-summary-update-mark gnus-process-mark 'replied)
8081           t))))
8082
8083 (defun gnus-summary-remove-process-mark (article)
8084   "Remove the process mark from ARTICLE and update the summary line."
8085   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8086   (let ((buffer-read-only nil))
8087     (if (gnus-summary-goto-subject article)
8088         (progn
8089           (and (eq (gnus-summary-article-mark) ?Z)
8090                (forward-line 1))
8091           (gnus-summary-update-mark ?  'replied)
8092           (if (memq article gnus-newsgroup-replied) 
8093               (gnus-summary-update-mark gnus-replied-mark 'replied))
8094           t))))
8095
8096 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8097   "Mark N articles as read forwards.
8098 If N is negative, mark backwards instead.
8099 Mark with MARK. If MARK is ? , ?! or ??, articles will be
8100 marked as unread. 
8101 The difference between N and the actual number of articles marked is
8102 returned."
8103   (interactive "p")
8104   (gnus-set-global-variables)
8105   (let ((backward (< n 0))
8106         (n (abs n))
8107         (mark (or mark gnus-del-mark)))
8108   (while (and (> n 0)
8109               (gnus-summary-mark-article nil mark no-expire)
8110               (zerop (gnus-summary-next-subject 
8111                       (if backward -1 1) gnus-summary-goto-unread t)))
8112     (setq n (1- n)))
8113   (if (/= 0 n) (message "No more %sarticles" (if mark "" "unread ")))
8114   (gnus-summary-recenter)
8115   (gnus-summary-position-cursor)
8116   (gnus-set-mode-line 'summary)
8117   n))
8118
8119 (defun gnus-summary-mark-article (&optional article mark no-expire)
8120   "Mark ARTICLE with MARK.
8121 MARK can be any character.
8122 Five MARK strings are reserved: ?  (unread), 
8123 ?! (ticked), ?? (dormant), ?D (read), ?E (expirable).
8124 If MARK is nil, then the default character ?D is used.
8125 If ARTICLE is nil, then the article on the current line will be
8126 marked." 
8127   ;; If no mark is given, then we check auto-expiring.
8128   (and (not no-expire)
8129        gnus-newsgroup-auto-expire 
8130        (or (not mark)
8131            (and (numberp mark) (or (= mark gnus-killed-mark)
8132                                    (= mark gnus-del-mark)
8133                                    (= mark gnus-catchup-mark)
8134                                    (= mark gnus-low-score-mark)
8135                                    (= mark gnus-read-mark))))
8136        (setq mark gnus-expirable-mark))
8137   (let* ((mark (or (and (stringp mark) (aref mark 0)) mark gnus-del-mark))
8138          (article (or article (gnus-summary-article-number))))
8139     (if (or (= mark gnus-unread-mark) 
8140             (= mark gnus-ticked-mark) 
8141             (= mark gnus-dormant-mark))
8142         (gnus-mark-article-as-unread article mark)
8143       (gnus-mark-article-as-read article mark))
8144
8145     ;; See whether the article is to be put in the cache.
8146     (and gnus-use-cache
8147          (save-excursion
8148            (gnus-summary-select-article)
8149            (gnus-cache-possibly-enter-article 
8150             gnus-newsgroup-name article 
8151             (gnus-get-header-by-number article)
8152             (= mark gnus-ticked-mark)
8153             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8154
8155     (if (gnus-summary-goto-subject article)
8156         (let ((buffer-read-only nil))
8157           (gnus-summary-show-thread)
8158           (beginning-of-line)
8159           (and (eq (gnus-summary-article-mark) ?Z)
8160                (forward-line 1))
8161           ;; Fix the mark.
8162           (gnus-summary-update-mark mark 'unread)
8163           t))))
8164
8165 (defun gnus-summary-update-mark (mark type)
8166   (beginning-of-line)
8167   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8168         plist)
8169     (if (not forward)
8170         ()
8171       (forward-char forward)
8172       (setq plist (text-properties-at (point)))
8173       (delete-char 1)
8174       (and (memq 'gnus plist) 
8175            (setcar (cdr (car (cdr (memq 'gnus plist)))) mark))
8176       (insert mark)
8177       (and plist (add-text-properties (1- (point)) (point) plist))
8178       (gnus-summary-update-line (eq mark gnus-unread-mark)))))
8179   
8180 (defun gnus-mark-article-as-read (article &optional mark)
8181   "Enter ARTICLE in the pertinent lists and remove it from others."
8182   ;; Make the article expirable.
8183   (let ((mark (or (and (stringp mark) (aref mark 0)) mark gnus-del-mark)))
8184     (if (= mark gnus-expirable-mark)
8185         (setq gnus-newsgroup-expirable (cons article gnus-newsgroup-expirable))
8186       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8187     ;; Remove from unread and marked lists.
8188     (setq gnus-newsgroup-unreads
8189           (delq article gnus-newsgroup-unreads))
8190     (setq gnus-newsgroup-marked
8191           (delq article gnus-newsgroup-marked))
8192     (setq gnus-newsgroup-dormant
8193           (delq article gnus-newsgroup-dormant))))
8194
8195 (defun gnus-mark-article-as-unread (article &optional mark)
8196   "Enter ARTICLE in the pertinent lists and remove it from others."
8197   (let ((mark (or (and (stringp mark) (aref mark 0)) mark gnus-ticked-mark)))
8198     ;; Add to unread list.
8199     (or (memq article gnus-newsgroup-unreads)
8200         (setq gnus-newsgroup-unreads
8201               (cons article gnus-newsgroup-unreads)))
8202     ;; If CLEAR-MARK is non-nil, the article must be removed from marked
8203     ;; list.  Otherwise, it must be added to the list.
8204     (setq gnus-newsgroup-marked
8205           (delq article gnus-newsgroup-marked))
8206     (setq gnus-newsgroup-dormant
8207           (delq article gnus-newsgroup-dormant))
8208     (setq gnus-newsgroup-expirable 
8209           (delq article gnus-newsgroup-expirable))
8210     (if (= mark gnus-ticked-mark)
8211         (setq gnus-newsgroup-marked 
8212               (cons article gnus-newsgroup-marked)))
8213     (if (= mark gnus-dormant-mark)
8214         (setq gnus-newsgroup-dormant 
8215               (cons article gnus-newsgroup-dormant)))))
8216
8217 (defalias 'gnus-summary-mark-as-unread-forward 
8218   'gnus-summary-tick-article-forward)
8219 (make-obsolete 'gnus-summary-mark-as-unread-forward 
8220                'gnus-summary-tick-article-forward)
8221 (defun gnus-summary-tick-article-forward (n)
8222   "Tick N articles forwards.
8223 If N is negative, tick backwards instead.
8224 The difference between N and the number of articles ticked is returned."
8225   (interactive "p")
8226   (gnus-summary-mark-forward n gnus-ticked-mark))
8227
8228 (defalias 'gnus-summary-mark-as-unread-backward 
8229   'gnus-summary-tick-article-backward)
8230 (make-obsolete 'gnus-summary-mark-as-unread-backward 
8231                'gnus-summary-tick-article-backward)
8232 (defun gnus-summary-tick-article-backward (n)
8233   "Tick N articles backwards.
8234 The difference between N and the number of articles ticked is returned."
8235   (interactive "p")
8236   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8237
8238 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8239 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8240 (defun gnus-summary-tick-article (&optional article clear-mark)
8241   "Mark current article as unread.
8242 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8243 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8244   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8245                                        gnus-ticked-mark)))
8246
8247 (defun gnus-summary-mark-as-read-forward (n)
8248   "Mark N articles as read forwards.
8249 If N is negative, mark backwards instead.
8250 The difference between N and the actual number of articles marked is
8251 returned."
8252   (interactive "p")
8253   (gnus-summary-mark-forward n gnus-del-mark t))
8254
8255 (defun gnus-summary-mark-as-read-backward (n)
8256   "Mark the N articles as read backwards.
8257 The difference between N and the actual number of articles marked is
8258 returned."
8259   (interactive "p")
8260   (gnus-summary-mark-forward (- n) gnus-del-mark t))
8261
8262 (defun gnus-summary-mark-as-read (&optional article mark)
8263   "Mark current article as read.
8264 ARTICLE specifies the article to be marked as read.
8265 MARK specifies a string to be inserted at the beginning of the line."
8266   (gnus-summary-mark-article article mark))
8267
8268 (defun gnus-summary-clear-mark-forward (n)
8269   "Clear marks from N articles forward.
8270 If N is negative, clear backward instead.
8271 The difference between N and the number of marks cleared is returned."
8272   (interactive "p")
8273   (gnus-summary-mark-forward n gnus-unread-mark))
8274
8275 (defun gnus-summary-clear-mark-backward (n)
8276   "Clear marks from N articles backward.
8277 The difference between N and the number of marks cleared is returned."
8278   (interactive "p")
8279   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8280
8281 (defun gnus-summary-mark-unread-as-read ()
8282   "Intended to be used by `gnus-summary-mark-article-hook'."
8283   (or (memq gnus-current-article gnus-newsgroup-marked)
8284       (memq gnus-current-article gnus-newsgroup-dormant)
8285       (memq gnus-current-article gnus-newsgroup-expirable)
8286       (gnus-summary-mark-as-read gnus-current-article gnus-read-mark)))
8287
8288 (defun gnus-summary-mark-region-as-read (point mark all)
8289   "Mark all unread articles between point and mark as read.
8290 If given a prefix, mark all articles between point and mark as read,
8291 even ticked and dormant ones."
8292   (interactive "r\nP")
8293   (save-excursion
8294     (goto-char point)
8295     (beginning-of-line)
8296     (while (and 
8297             (< (point) mark)
8298             (progn
8299               (and
8300                (or all
8301                    (and
8302                     (not (memq (gnus-summary-article-number)
8303                                gnus-newsgroup-marked))
8304                     (not (memq (gnus-summary-article-number)
8305                                gnus-newsgroup-dormant))))
8306                (gnus-summary-mark-article
8307                 (gnus-summary-article-number) gnus-del-mark))
8308               t)
8309             (zerop (forward-line 1))))))
8310
8311 ;; Fix by Per Abrahamsen <amanda@iesd.auc.dk>.
8312 (defalias 'gnus-summary-delete-marked-as-read 
8313   'gnus-summary-remove-lines-marked-as-read)
8314 (make-obsolete 'gnus-summary-delete-marked-as-read 
8315                'gnus-summary-remove-lines-marked-as-read)
8316 (defun gnus-summary-remove-lines-marked-as-read ()
8317   "Remove lines that are marked as read."
8318   (interactive)
8319   (gnus-summary-remove-lines-marked-with 
8320    (concat (mapconcat
8321             (lambda (char) (char-to-string (symbol-value char)))
8322             '(gnus-del-mark gnus-read-mark
8323               gnus-killed-mark gnus-kill-file-mark
8324               gnus-low-score-mark gnus-expirable-mark)
8325             ""))))
8326
8327 (defalias 'gnus-summary-delete-marked-with 
8328   'gnus-summary-remove-lines-marked-with)
8329 (make-obsolete 'gnus-summary-delete-marked-with 
8330                'gnus-summary-remove-lines-marked-with)
8331 ;; Rewrite by Daniel Quinlan <quinlan@best.com>.
8332 (defun gnus-summary-remove-lines-marked-with (marks)
8333   "Remove lines that are marked with MARKS (e.g. \"DK\")."
8334   (interactive "sMarks: ")
8335   ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
8336   (save-excursion
8337     (set-buffer gnus-summary-buffer)
8338     (let ((buffer-read-only nil)
8339           (marks (concat "^[" marks "]")))
8340       (goto-char (point-min))
8341       (if gnus-use-adaptive-scoring
8342           (gnus-score-remove-lines-adaptive marks)
8343         (while (re-search-forward marks (point-max) t)
8344           (beginning-of-line)
8345           (delete-region (point)
8346                          (progn (forward-line 1) (point))))))
8347     (or (zerop (buffer-size))
8348         (if (eobp)
8349             (gnus-summary-prev-subject 1)
8350           (gnus-summary-position-cursor)))))
8351
8352 (defun gnus-summary-expunge-below (score)
8353   "Remove articles with score less than SCORE."
8354   (interactive "P")
8355   (setq score (if score
8356                   (prefix-numeric-value score)
8357                 (or gnus-summary-default-score 0)))
8358   (save-excursion
8359     (set-buffer gnus-summary-buffer)
8360     (goto-char (point-min))
8361     (let ((buffer-read-only nil)
8362           beg)
8363       (while (not (eobp))
8364         (if (< (gnus-summary-article-score) score)
8365             (progn
8366               (setq beg (point))
8367               (forward-line 1)
8368               (delete-region beg (point)))
8369           (forward-line 1)))
8370       ;; Adjust point.
8371       (or (zerop (buffer-size))
8372           (if (eobp)
8373               (gnus-summary-prev-subject 1)
8374             (gnus-summary-position-cursor))))))
8375
8376 (defun gnus-summary-mark-below (score mark)
8377   "Mark articles with score less than SCORE with MARK."
8378   (interactive "P\ncMark: ")
8379   (setq score (if score
8380                   (prefix-numeric-value score)
8381                 (or gnus-summary-default-score 0)))
8382   (save-excursion
8383     (set-buffer gnus-summary-buffer)
8384     (goto-char (point-min))
8385     (while (not (eobp))
8386       (and (< (gnus-summary-article-score) score)
8387            (gnus-summary-mark-article nil mark))
8388       (forward-line 1))))
8389
8390 (defun gnus-summary-kill-below (score)
8391   "Mark articles with score below SCORE as read."
8392   (interactive "P")
8393   (gnus-summary-mark-below score gnus-killed-mark))
8394
8395 (defun gnus-summary-clear-above (score)
8396   "Clear all marks from articles with score above SCORE."
8397   (interactive "P")
8398   (gnus-summary-mark-above score gnus-unread-mark))
8399
8400 (defun gnus-summary-tick-above (score)
8401   "Tick all articles with score above SCORE."
8402   (interactive "P")
8403   (gnus-summary-mark-above score gnus-ticked-mark))
8404
8405 (defun gnus-summary-mark-above (score mark)
8406   "Mark articles with score over SCORE with MARK."
8407   (interactive "P\ncMark: ")
8408   (setq score (if score
8409                   (prefix-numeric-value score)
8410                 (or gnus-summary-default-score 0)))
8411   (save-excursion
8412     (set-buffer gnus-summary-buffer)
8413     (goto-char (point-min))
8414     (while (not (eobp))
8415       (if (> (gnus-summary-article-score) score)
8416           (progn
8417             (gnus-summary-mark-article nil mark)
8418             (forward-line 1))
8419         (forward-line 1)))))
8420
8421 ;; Suggested by Daniel Quinlan <quinlan@best.com>.  
8422 (defun gnus-summary-show-all-expunged ()
8423   "Display all the hidden articles that were expunged for low scores."
8424   (interactive)
8425   (let ((buffer-read-only nil))
8426     (let ((scored gnus-newsgroup-scored)
8427           headers h)
8428       (while scored
8429         (or (gnus-summary-goto-subject (car (car scored)))
8430             (and (setq h (gnus-get-header-by-number (car (car scored))))
8431                  (< (cdr (car scored)) gnus-summary-expunge-below)
8432                  (setq headers (cons h headers))))
8433         (setq scored (cdr scored)))
8434       (or headers (error "No expunged articles hidden."))
8435       (goto-char (point-min))
8436       (save-excursion 
8437         (gnus-summary-prepare-threads (nreverse headers) 0)))
8438     (goto-char (point-min))
8439     (gnus-summary-position-cursor)))
8440
8441 (defun gnus-summary-show-all-dormant ()
8442   "Display all the hidden articles that are marked as dormant."
8443   (interactive)
8444   (let ((buffer-read-only nil))
8445     (goto-char (point-min))
8446     (let ((dormant gnus-newsgroup-dormant)
8447           headers h)
8448       (while dormant
8449         (or (gnus-summary-goto-subject (car dormant))
8450             (and (setq h (gnus-get-header-by-number (car dormant)))
8451                  (setq headers (cons h headers))))
8452         (setq dormant (cdr dormant)))
8453       (or headers (error "No dormant articles hidden."))
8454       (save-excursion (gnus-summary-prepare-threads (nreverse headers) 0)))
8455     (goto-char (point-min))
8456     (gnus-summary-position-cursor)))
8457
8458 (defun gnus-summary-hide-all-dormant ()
8459   "Hide all dormant articles."
8460   (interactive)
8461   (gnus-summary-remove-lines-marked-with (char-to-string gnus-dormant-mark))
8462   (gnus-summary-position-cursor))
8463
8464 (defun gnus-summary-catchup (all &optional quietly to-here)
8465   "Mark all articles not marked as unread in this newsgroup as read.
8466 If prefix argument ALL is non-nil, all articles are marked as read.
8467 If QUIETLY is non-nil, no questions will be asked.
8468 If TO-HERE is non-nil, it should be a point in the buffer. All
8469 articles before this point will be marked as read.
8470 The number of articles marked as read is returned."
8471   (interactive "P")
8472   (prog1
8473       (if (or quietly
8474               (not gnus-interactive-catchup) ;Without confirmation?
8475               gnus-expert-user
8476               (gnus-y-or-n-p
8477                (if all
8478                    "Mark absolutely all articles as read? "
8479                  "Mark all unread articles as read? ")))
8480           (let ((unreads (length gnus-newsgroup-unreads)))
8481             (if (gnus-summary-first-subject (not all))
8482                 (while (and (gnus-summary-mark-as-read nil gnus-catchup-mark)
8483                             (if to-here (< (point) to-here) t)
8484                             (gnus-summary-search-subject nil (not all)))))
8485             (- unreads (length gnus-newsgroup-unreads))
8486             (or to-here
8487                 (setq gnus-newsgroup-unreads gnus-newsgroup-marked))))
8488     (gnus-summary-position-cursor)))
8489
8490 (defun gnus-summary-catchup-to-here (&optional all)
8491   "Mark all unticked articles before the current one as read.
8492 If ALL is non-nil, also mark ticked and dormant articles as read."
8493   (interactive)
8494   (beginning-of-line)
8495   (gnus-summary-catchup all t (point))
8496   (gnus-summary-position-cursor))
8497
8498 (defun gnus-summary-catchup-all (&optional quietly)
8499   "Mark all articles in this newsgroup as read."
8500   (interactive)
8501   (gnus-summary-catchup t quietly))
8502
8503 (defun gnus-summary-catchup-and-exit (all &optional quietly)
8504   "Mark all articles not marked as unread in this newsgroup as read, then exit.
8505 If prefix argument ALL is non-nil, all articles are marked as read."
8506   (interactive "P")
8507   (gnus-summary-catchup all quietly)
8508   ;; Select next newsgroup or exit.
8509   (if (eq gnus-auto-select-next 'quietly)
8510       (gnus-summary-next-group nil)
8511     (gnus-summary-exit)))
8512
8513 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8514   "Mark all articles in this newsgroup as read, and then exit."
8515   (interactive)
8516   (gnus-summary-catchup-and-exit t quietly))
8517
8518 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
8519 (defun gnus-summary-catchup-and-goto-next-group (all)
8520   "Mark all articles in this group as read and select the next group.
8521 If given a prefix, mark all articles, unread as well as ticked, as
8522 read." 
8523   (interactive "P")
8524   (gnus-summary-catchup all)
8525   (gnus-summary-next-group))
8526
8527 ;; Thread-based commands.
8528
8529 (defun gnus-summary-toggle-threads (arg)
8530   "Toggle showing conversation threads.
8531 If ARG is positive number, turn showing conversation threads on."
8532   (interactive "P")
8533   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
8534     (setq gnus-show-threads
8535           (if (null arg) (not gnus-show-threads)
8536             (> (prefix-numeric-value arg) 0)))
8537     (gnus-summary-prepare)
8538     (gnus-summary-goto-subject current)
8539     (gnus-summary-position-cursor)))
8540
8541 (defun gnus-summary-show-all-threads ()
8542   "Show all threads."
8543   (interactive)
8544   (save-excursion
8545     (let ((buffer-read-only nil))
8546       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
8547   (gnus-summary-position-cursor))
8548
8549 (defun gnus-summary-show-thread ()
8550   "Show thread subtrees.
8551 Returns nil if no thread was there to be shown."
8552   (interactive)
8553   (prog1
8554       (save-excursion
8555         (let ((buffer-read-only nil)
8556               ;; first goto end then to beg, to have point at beg after let
8557               (end (progn (end-of-line) (point)))
8558               (beg (progn (beginning-of-line) (point))))
8559           (prog1
8560               ;; Any hidden lines here?
8561               (search-forward "\r" end t)
8562             (subst-char-in-region beg end ?\^M ?\n t))))
8563     (gnus-summary-position-cursor)))
8564
8565 (defun gnus-summary-hide-all-threads ()
8566   "Hide all thread subtrees."
8567   (interactive)
8568   (save-excursion
8569     (goto-char (point-min))
8570     (gnus-summary-hide-thread)
8571     (while (and (not (eobp)) (zerop (forward-line 1)))
8572       (gnus-summary-hide-thread)))
8573   (gnus-summary-position-cursor))
8574
8575 (defun gnus-summary-hide-thread ()
8576   "Hide thread subtrees.
8577 Returns nil if no threads were there to be hidden."
8578   (interactive)
8579   (let ((buffer-read-only nil)
8580         (start (point))
8581         (level (gnus-summary-thread-level))
8582         (end (point)))
8583     ;; Go forward until either the buffer ends or the subthread
8584     ;; ends. 
8585     (if (eobp)
8586         ()
8587       (while (and (zerop (forward-line 1))
8588                   (> (gnus-summary-thread-level) level))
8589         (setq end (point)))
8590       (prog1
8591           (save-excursion
8592             (goto-char end)
8593             (search-backward "\n" start t))
8594         (subst-char-in-region start end ?\n ?\^M t)
8595         (forward-line -1)))))
8596
8597 (defun gnus-summary-go-to-next-thread (&optional previous)
8598   "Go to the same level (or less) next thread.
8599 If PREVIOUS is non-nil, go to previous thread instead.
8600 Return the article number moved to, or nil if moving was impossible."
8601   (let ((level (gnus-summary-thread-level))
8602         (article (gnus-summary-article-number)))
8603     (if previous 
8604         (while (and (zerop (gnus-summary-prev-subject 1))
8605                     (> (gnus-summary-thread-level) level)))
8606       (while (and (zerop (gnus-summary-next-subject 1))
8607                   (> (gnus-summary-thread-level) level))))
8608     (gnus-summary-recenter)
8609     (gnus-summary-position-cursor)
8610     (let ((oart (gnus-summary-article-number)))
8611       (and (/= oart article) oart))))
8612
8613 (defun gnus-summary-next-thread (n)
8614   "Go to the same level next N'th thread.
8615 If N is negative, search backward instead.
8616 Returns the difference between N and the number of skips actually
8617 done."
8618   (interactive "p")
8619   (let ((backward (< n 0))
8620         (n (abs n)))
8621   (while (and (> n 0)
8622               (gnus-summary-go-to-next-thread backward))
8623     (setq n (1- n)))
8624   (gnus-summary-position-cursor)
8625   (if (/= 0 n) (message "No more threads"))
8626   n))
8627
8628 (defun gnus-summary-prev-thread (n)
8629   "Go to the same level previous N'th thread.
8630 Returns the difference between N and the number of skips actually
8631 done."
8632   (interactive "p")
8633   (gnus-summary-next-thread (- n)))
8634
8635 (defun gnus-summary-go-down-thread (&optional same)
8636   "Go down one level in the current thread.
8637 If SAME is non-nil, also move to articles of the same level."
8638   (let ((level (gnus-summary-thread-level))
8639         (start (point)))
8640     (if (and (zerop (forward-line 1))
8641              (> (gnus-summary-thread-level) level))
8642         t
8643       (goto-char start)
8644       nil)))
8645
8646 (defun gnus-summary-go-up-thread ()
8647   "Go up one level in the current thread."
8648   (let ((level (gnus-summary-thread-level))
8649         (start (point)))
8650     (while (and (zerop (forward-line -1))
8651                 (>= (gnus-summary-thread-level) level)))
8652     (if (>= (gnus-summary-thread-level) level)
8653         (progn
8654           (goto-char start)
8655           nil)
8656       t)))
8657
8658 (defun gnus-summary-down-thread (n)
8659   "Go down thread N steps.
8660 If N is negative, go up instead.
8661 Returns the difference between N and how many steps down that were
8662 taken."
8663   (interactive "p")
8664   (let ((up (< n 0))
8665         (n (abs n)))
8666   (while (and (> n 0)
8667               (if up (gnus-summary-go-up-thread)
8668                 (gnus-summary-go-down-thread)))
8669     (setq n (1- n)))
8670   (gnus-summary-position-cursor)
8671   (if (/= 0 n) (message "Can't go further"))
8672   n))
8673
8674 (defun gnus-summary-up-thread (n)
8675   "Go up thread N steps.
8676 If N is negative, go up instead.
8677 Returns the difference between N and how many steps down that were
8678 taken."
8679   (interactive "p")
8680   (gnus-summary-down-thread (- n)))
8681
8682 (defun gnus-summary-kill-thread (unmark)
8683   "Mark articles under current thread as read.
8684 If the prefix argument is positive, remove any kinds of marks.
8685 If the prefix argument is negative, tick articles instead."
8686   (interactive "P")
8687   (if unmark
8688       (setq unmark (prefix-numeric-value unmark)))
8689   (let ((killing t)
8690         (level (gnus-summary-thread-level)))
8691     (save-excursion
8692       (while killing
8693         ;; Mark the article...
8694         (cond ((null unmark) (gnus-summary-mark-as-read nil gnus-killed-mark))
8695               ((> unmark 0) (gnus-summary-tick-article nil t))
8696               (t (gnus-summary-tick-article)))
8697         ;; ...and go forward until either the buffer ends or the subtree
8698         ;; ends. 
8699         (if (not (and (zerop (forward-line 1))
8700                       (> (gnus-summary-thread-level) level)))
8701             (setq killing nil))))
8702     ;; Hide killed subtrees.
8703     (and (null unmark)
8704          gnus-thread-hide-killed
8705          (gnus-summary-hide-thread))
8706     ;; If marked as read, go to next unread subject.
8707     (if (null unmark)
8708         ;; Go to next unread subject.
8709         (gnus-summary-next-subject 1 t)))
8710   (gnus-set-mode-line 'summary))
8711
8712 ;; Summary sorting commands
8713
8714 (defun gnus-summary-sort-by-number (reverse)
8715   "Sort summary buffer by article number.
8716 Argument REVERSE means reverse order."
8717   (interactive "P")
8718   (gnus-summary-sort 
8719    (cons 'gnus-summary-article-number 'gnus-thread-sort-by-number) reverse))
8720
8721 (defun gnus-summary-sort-by-author (reverse)
8722   "Sort summary buffer by author name alphabetically.
8723 If case-fold-search is non-nil, case of letters is ignored.
8724 Argument REVERSE means reverse order."
8725   (interactive "P")
8726   (gnus-summary-sort
8727    (cons
8728     (lambda ()
8729       (let ((extract (funcall
8730                       gnus-extract-address-components
8731                       (header-from (gnus-get-header-by-number
8732                                     (gnus-summary-article-number))))))
8733         (or (car extract) (cdr extract))))
8734     'gnus-thread-sort-by-author)
8735    reverse))
8736
8737 (defun gnus-summary-sort-by-subject (reverse)
8738   "Sort summary buffer by subject alphabetically. `Re:'s are ignored.
8739 If case-fold-search is non-nil, case of letters is ignored.
8740 Argument REVERSE means reverse order."
8741   (interactive "P")
8742   (gnus-summary-sort
8743    (cons
8744     (lambda ()
8745       (downcase (gnus-simplify-subject (gnus-summary-subject-string))))
8746     'gnus-thread-sort-by-subject)
8747    reverse))
8748
8749 (defun gnus-summary-sort-by-date (reverse)
8750   "Sort summary buffer by date.
8751 Argument REVERSE means reverse order."
8752   (interactive "P")
8753   (gnus-summary-sort
8754    (cons
8755     (lambda ()
8756       (gnus-sortable-date
8757        (header-date 
8758         (gnus-get-header-by-number (gnus-summary-article-number)))))
8759     'gnus-thread-sort-by-date)
8760    reverse))
8761
8762 (defun gnus-summary-sort-by-score (reverse)
8763   "Sort summary buffer by score.
8764 Argument REVERSE means reverse order."
8765   (interactive "P")
8766   (gnus-summary-sort 
8767    (cons 'gnus-summary-article-score 'gnus-thread-sort-by-score)
8768    (not reverse)))
8769
8770 (defvar gnus-summary-already-sorted nil)
8771 (defun gnus-summary-sort (predicate reverse)
8772   ;; Sort summary buffer by PREDICATE.  REVERSE means reverse order. 
8773   (if gnus-summary-already-sorted
8774       ()
8775     (let (buffer-read-only)
8776       (if (not gnus-show-threads)
8777           (progn
8778             (goto-char (point-min))
8779             (sort-subr reverse 'forward-line 'end-of-line (car predicate)))
8780         (let ((gnus-thread-sort-functions (list (cdr predicate)))
8781               (gnus-summary-already-sorted nil))
8782           (gnus-summary-prepare))))))
8783   
8784 (defun gnus-sortable-date (date)
8785   "Make sortable string by string-lessp from DATE.
8786 Timezone package is used."
8787   (let* ((date   (timezone-fix-time date nil nil)) ;[Y M D H M S]
8788          (year   (aref date 0))
8789          (month  (aref date 1))
8790          (day    (aref date 2)))
8791     (timezone-make-sortable-date 
8792      year month day 
8793      (timezone-make-time-string
8794       (aref date 3) (aref date 4) (aref date 5)))))
8795
8796
8797 ;; Summary saving commands.
8798
8799 (defun gnus-summary-save-article (n)
8800   "Save the current article using the default saver function.
8801 If N is a positive number, save the N next articles.
8802 If N is a negative number, save the N previous articles.
8803 If N is nil and any articles have been marked with the process mark,
8804 save those articles instead.
8805 The variable `gnus-default-article-saver' specifies the saver function."
8806   (interactive "P")
8807   (let (articles process)
8808     (if (and n (numberp n))
8809         (let ((backward (< n 0))
8810               (n (abs n)))
8811           (save-excursion
8812             (while (and (> n 0)
8813                         (setq articles (cons (gnus-summary-article-number) 
8814                                              articles))
8815                         (gnus-summary-search-forward nil nil backward))
8816               (setq n (1- n))))
8817           (setq articles (sort articles (function <))))
8818       (if gnus-newsgroup-processable
8819           (progn
8820             (setq articles (setq gnus-newsgroup-processable
8821                                  (nreverse gnus-newsgroup-processable)))
8822             (setq process t))
8823         (setq articles (list (gnus-summary-article-number)))))
8824     (while articles
8825       (let ((header (gnus-gethash (int-to-string (car articles))
8826                                   gnus-newsgroup-headers-hashtb-by-number)))
8827         (if (vectorp header)
8828             (progn
8829               (gnus-summary-select-article t nil nil (car articles))
8830               (or gnus-save-all-headers
8831                   (gnus-article-hide-headers t))
8832               ;; Remove any X-Gnus lines.
8833               (save-excursion
8834                 (save-restriction
8835                   (set-buffer gnus-article-buffer)
8836                   (let ((buffer-read-only nil))
8837                     (goto-char (point-min))
8838                     (narrow-to-region (point) (or (search-forward "\n\n" nil t)
8839                                                   (point-max)))
8840                     (while (re-search-forward "^X-Gnus" nil t)
8841                       (beginning-of-line)
8842                       (delete-region (point)
8843                                      (progn (forward-line 1) (point))))
8844                     (widen))))
8845               (if gnus-default-article-saver
8846                   (funcall gnus-default-article-saver)
8847                 (error "No default saver is defined.")))
8848           (if (assq 'name header)
8849               (gnus-copy-file (cdr (assq 'name header)))
8850             (message "Article %d is unsaveable" (car articles)))))
8851       (if process
8852           (gnus-summary-remove-process-mark (car articles)))
8853       (setq articles (cdr articles)))
8854     (if process (setq gnus-newsgroup-processable 
8855                       (nreverse gnus-newsgroup-processable)))
8856     (gnus-summary-position-cursor)
8857     n))
8858
8859 (defun gnus-summary-pipe-output (arg)
8860   "Pipe the current article to a subprocess.
8861 If N is a positive number, pipe the N next articles.
8862 If N is a negative number, pipe the N previous articles.
8863 If N is nil and any articles have been marked with the process mark,
8864 pipe those articles instead."
8865   (interactive "P")
8866   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
8867     (gnus-summary-save-article arg)))
8868
8869 (defun gnus-summary-save-article-mail (arg)
8870   "Append the current article to an mail file.
8871 If N is a positive number, save the N next articles.
8872 If N is a negative number, save the N previous articles.
8873 If N is nil and any articles have been marked with the process mark,
8874 save those articles instead."
8875   (interactive "P")
8876   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
8877     (gnus-summary-save-article arg)))
8878
8879 (defun gnus-summary-save-article-rmail (arg)
8880   "Append the current article to an rmail file.
8881 If N is a positive number, save the N next articles.
8882 If N is a negative number, save the N previous articles.
8883 If N is nil and any articles have been marked with the process mark,
8884 save those articles instead."
8885   (interactive "P")
8886   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
8887     (gnus-summary-save-article arg)))
8888
8889 (defun gnus-summary-save-article-file (arg)
8890   "Append the current article to a file.
8891 If N is a positive number, save the N next articles.
8892 If N is a negative number, save the N previous articles.
8893 If N is nil and any articles have been marked with the process mark,
8894 save those articles instead."
8895   (interactive "P")
8896   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
8897     (gnus-summary-save-article arg)))
8898
8899 (defun gnus-summary-save-article-folder (arg)
8900   "Append the current article to an mh folder.
8901 If N is a positive number, save the N next articles.
8902 If N is a negative number, save the N previous articles.
8903 If N is nil and any articles have been marked with the process mark,
8904 save those articles instead."
8905   (interactive "P")
8906   (let ((gnus-default-article-saver 'gnus-summary-save-in-folder))
8907     (gnus-summary-save-article arg)))
8908
8909 (defun gnus-read-save-file-name (prompt default-name)
8910   (let ((methods gnus-split-methods)
8911         split-name)
8912     (if (not gnus-split-methods)
8913         ()
8914       (save-excursion
8915         (set-buffer gnus-article-buffer)
8916         (gnus-narrow-to-headers)
8917         (while methods
8918           (goto-char (point-min))
8919           (and (condition-case () 
8920                    (re-search-forward (car (car methods)) nil t)
8921                  (error nil))
8922                (setq split-name (cons (nth 1 (car methods)) split-name)))
8923           (setq methods (cdr methods)))
8924         (widen)))
8925     (cond ((null split-name)
8926            (read-file-name
8927             (concat prompt " (default "
8928                     (file-name-nondirectory default-name) ") ")
8929             (file-name-directory default-name)
8930             default-name))
8931           ((= 1 (length split-name))
8932            (read-file-name
8933             (concat prompt " (default " (car split-name) ") ")
8934             gnus-article-save-directory
8935             (concat gnus-article-save-directory (car split-name))))
8936           (t
8937            (setq split-name (mapcar (lambda (el) (list el))
8938                                     (nreverse split-name)))
8939            (let ((result (completing-read 
8940                           (concat prompt " ")
8941                           split-name nil nil)))
8942              (concat gnus-article-save-directory
8943                      (if (string= result "")
8944                          (car (car split-name))
8945                        result)))))))
8946
8947 (defun gnus-summary-save-in-rmail (&optional filename)
8948   "Append this article to Rmail file.
8949 Optional argument FILENAME specifies file name.
8950 Directory to save to is default to `gnus-article-save-directory' which
8951 is initialized from the SAVEDIR environment variable."
8952   (interactive)
8953   (let ((default-name
8954           (funcall gnus-rmail-save-name gnus-newsgroup-name
8955                    gnus-current-headers gnus-newsgroup-last-rmail)))
8956     (or filename
8957         (setq filename (gnus-read-save-file-name 
8958                         "Save in rmail file:" default-name)))
8959     (gnus-make-directory (file-name-directory filename))
8960     (gnus-eval-in-buffer-window 
8961      gnus-article-buffer
8962      (save-excursion
8963        (save-restriction
8964          (widen)
8965          (gnus-output-to-rmail filename))))
8966     ;; Remember the directory name to save articles.
8967     (setq gnus-newsgroup-last-rmail filename)))
8968
8969 (defun gnus-summary-save-in-mail (&optional filename)
8970   "Append this article to Unix mail file.
8971 Optional argument FILENAME specifies file name.
8972 Directory to save to is default to `gnus-article-save-directory' which
8973 is initialized from the SAVEDIR environment variable."
8974   (interactive)
8975   (let ((default-name
8976           (funcall gnus-mail-save-name gnus-newsgroup-name
8977                    gnus-current-headers gnus-newsgroup-last-mail)))
8978     (or filename
8979         (setq filename (gnus-read-save-file-name 
8980                         "Save in Unix mail file:" default-name)))
8981     (setq filename
8982           (expand-file-name filename
8983                             (and default-name
8984                                  (file-name-directory default-name))))
8985     (gnus-make-directory (file-name-directory filename))
8986     (gnus-eval-in-buffer-window 
8987      gnus-article-buffer
8988      (save-excursion
8989        (save-restriction
8990          (widen)
8991          (if (and (file-readable-p filename) (rmail-file-p filename))
8992              (gnus-output-to-rmail filename)
8993            (rmail-output filename 1 t t)))))
8994     ;; Remember the directory name to save articles.
8995     (setq gnus-newsgroup-last-mail filename)))
8996
8997 (defun gnus-summary-save-in-file (&optional filename)
8998   "Append this article to file.
8999 Optional argument FILENAME specifies file name.
9000 Directory to save to is default to `gnus-article-save-directory' which
9001 is initialized from the SAVEDIR environment variable."
9002   (interactive)
9003   (let ((default-name
9004           (funcall gnus-file-save-name gnus-newsgroup-name
9005                    gnus-current-headers gnus-newsgroup-last-file)))
9006     (or filename
9007         (setq filename (gnus-read-save-file-name 
9008                         "Save in file:" default-name)))
9009     (gnus-make-directory (file-name-directory filename))
9010     (gnus-eval-in-buffer-window 
9011      gnus-article-buffer
9012      (save-excursion
9013        (save-restriction
9014          (widen)
9015          (gnus-output-to-file filename))))
9016     ;; Remember the directory name to save articles.
9017     (setq gnus-newsgroup-last-file filename)))
9018
9019 (defun gnus-summary-save-in-pipe (&optional command)
9020   "Pipe this article to subprocess."
9021   (interactive)
9022   (let ((command (read-string "Shell command on article: "
9023                               gnus-last-shell-command)))
9024     (if (string-equal command "")
9025         (setq command gnus-last-shell-command))
9026     (gnus-eval-in-buffer-window 
9027      gnus-article-buffer
9028      (save-restriction
9029        (widen)
9030        (shell-command-on-region (point-min) (point-max) command nil)))
9031     (setq gnus-last-shell-command command)))
9032
9033 ;; Summary extract commands
9034
9035 (defun gnus-summary-insert-pseudos (pslist)
9036   (let ((buffer-read-only nil)
9037         (article (gnus-summary-article-number))
9038         b)
9039     (or (gnus-summary-goto-subject article)
9040         (error (format "No such article: %d" article)))
9041     (or gnus-newsgroup-headers-hashtb-by-number
9042         (gnus-make-headers-hashtable-by-number))
9043     (gnus-summary-position-cursor)
9044     ;; If all commands are to be bunched up on one line, we collect
9045     ;; them here.  
9046     (if gnus-view-pseudos-separately
9047         ()
9048       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9049             files action)
9050         (while ps
9051           (setq action (cdr (assq 'action (car ps))))
9052           (setq files (list (cdr (assq 'name (car ps)))))
9053           (while (and ps (cdr ps)
9054                       (string= (or action "1")
9055                                (or (cdr (assq 'action (car (cdr ps)))) "2")))
9056             (setq files (cons (cdr (assq 'name (car (cdr ps)))) files))
9057             (setcdr ps (cdr (cdr ps))))
9058           (if (not files)
9059               ()
9060             (if (not (string-match "%s" action))
9061                 (setq files (cons " " files)))
9062             (setq files (cons " " files))
9063             (and (assq 'execute (car ps))
9064                  (setcdr (assq 'execute (car ps))
9065                          (funcall (if (string-match "%s" action)
9066                                       'format 'concat)
9067                                   action 
9068                                   (mapconcat (lambda (f) f) files " ")))))
9069           (setq ps (cdr ps)))))
9070     (if gnus-view-pseudos
9071         (while pslist
9072           (and (assq 'execute (car pslist))
9073                (gnus-execute-command (cdr (assq 'execute (car pslist)))
9074                                      (eq gnus-view-pseudos 'not-confirm)))
9075           (setq pslist (cdr pslist)))
9076       (save-excursion
9077         (while pslist
9078           (gnus-summary-goto-subject (or (cdr (assq 'article (car pslist)))
9079                                          (gnus-summary-article-number)))
9080           (forward-line 1)
9081           (setq b (point))
9082           (insert "          " (file-name-nondirectory 
9083                                 (cdr (assq 'name (car pslist))))
9084                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9085           (add-text-properties 
9086            b (1+ b) (list 'gnus (list gnus-reffed-article-number
9087                                       gnus-unread-mark 0)
9088                           'gnus-pseudo (car pslist)))
9089           (forward-line -1)
9090           (gnus-sethash (int-to-string gnus-reffed-article-number)
9091                         (car pslist) gnus-newsgroup-headers-hashtb-by-number)
9092           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9093           (setq pslist (cdr pslist)))))))
9094
9095 (defun gnus-pseudos< (p1 p2)
9096   (let ((c1 (cdr (assq 'action p1)))
9097         (c2 (cdr (assq 'action p2))))
9098     (and c1 c2 (string< c1 c2))))
9099
9100 (defun gnus-request-pseudo-article (props)
9101   (cond ((assq 'execute props)
9102          (gnus-execute-command (cdr (assq 'execute props)))))
9103   (let ((gnus-current-article (gnus-summary-article-number)))
9104     (run-hooks 'gnus-mark-article-hook)))
9105
9106 (defun gnus-execute-command (command &optional automatic)
9107   (save-excursion
9108     (gnus-article-setup-buffer)
9109     (set-buffer gnus-article-buffer)
9110     (let ((command (if automatic command (read-string "Command: " command)))
9111           (buffer-read-only nil))
9112       (erase-buffer)
9113       (insert "$ " command "\n\n")
9114       (if gnus-view-pseudo-asynchronously
9115           (start-process "gnus-execute" nil "sh" "-c" command)
9116         (call-process "sh" nil t nil "-c" command)))))
9117
9118 (defun gnus-copy-file (file &optional to)
9119   "Copy FILE to TO."
9120   (interactive
9121    (list (read-file-name "Copy file: " default-directory)
9122          (read-file-name "Copy file to: " default-directory)))
9123   (or to (setq to (read-file-name "Copy file to: " default-directory)))
9124   (and (file-directory-p to) 
9125        (setq to (concat (file-name-as-directory to)
9126                         (file-name-nondirectory file))))
9127   (copy-file file to))
9128
9129 ;; Summary kill commands.
9130
9131 (defun gnus-summary-edit-global-kill (article)
9132   "Edit the \"global\" kill file."
9133   (interactive (list (gnus-summary-article-number)))
9134   (gnus-group-edit-global-kill article))
9135
9136 (defun gnus-summary-edit-local-kill ()
9137   "Edit a local kill file applied to the current newsgroup."
9138   (interactive)
9139   (setq gnus-current-headers 
9140         (gnus-gethash 
9141          (int-to-string (gnus-summary-article-number))
9142          gnus-newsgroup-headers-hashtb-by-number))
9143   (gnus-set-global-variables)
9144   (gnus-group-edit-local-kill 
9145    (gnus-summary-article-number) gnus-newsgroup-name))
9146
9147 \f
9148 ;;;
9149 ;;; Gnus article mode
9150 ;;;
9151
9152 (put 'gnus-article-mode 'mode-class 'special)
9153
9154 (if gnus-article-mode-map
9155     nil
9156   (setq gnus-article-mode-map (make-keymap))
9157   (suppress-keymap gnus-article-mode-map)
9158   (define-key gnus-article-mode-map " " 'gnus-article-next-page)
9159   (define-key gnus-article-mode-map "\177" 'gnus-article-prev-page)
9160   (define-key gnus-article-mode-map "\C-c^" 'gnus-article-refer-article)
9161   (define-key gnus-article-mode-map "h" 'gnus-article-show-summary)
9162   (define-key gnus-article-mode-map "s" 'gnus-article-show-summary)
9163   (define-key gnus-article-mode-map "\C-c\C-m" 'gnus-article-mail)
9164   (define-key gnus-article-mode-map "\C-c\C-M" 'gnus-article-mail-with-original)
9165   (define-key gnus-article-mode-map "?" 'gnus-article-describe-briefly)
9166   
9167   ;; Duplicate almost all summary keystrokes in the article mode map.
9168   (let ((commands 
9169          (list "#" "\M-#" "\C-c\M-#" "\r" "n" "p"
9170                "N" "P" "\M-\C-n" "\M-\C-p" "." "\M-s" "\M-r"
9171                "<" ">" "l" "j" "^" "\M-^" "-" "u" "U" "d" "D"
9172                "\M-u" "\M-U" "k" "\C-k" "\M-\C-k" "c" "x" "X" 
9173                "\M-\C-x" "\M-\177" "b" "B" "$" "w" "\C-c\C-r"
9174                "t" "\M-t" "C" "S"
9175                "m" "o" "\C-o" "|" "\M-m" "\M-\C-m" "\M-k" "M"
9176                "V" "\C-c\C-d")))
9177     (while commands
9178       (define-key gnus-article-mode-map (car commands) 
9179         'gnus-article-summary-command)
9180       (setq commands (cdr commands))))
9181
9182   (let ((commands (list "q" "Q" "r" "R" "\C-c\C-f" "m"  "a" "f" "F")))
9183     (while commands
9184       (define-key gnus-article-mode-map (car commands) 
9185         'gnus-article-summary-command-nosave)
9186       (setq commands (cdr commands)))))
9187
9188
9189 (defun gnus-article-mode ()
9190   "Major mode for displaying an article.
9191
9192 All normal editing commands are switched off.
9193
9194 The following commands are available:
9195
9196 \\<gnus-article-mode-map>
9197 \\[gnus-article-next-page]\t Scroll the article one page forwards
9198 \\[gnus-article-prev-page]\t Scroll the article one page backwards
9199 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
9200 \\[gnus-article-show-summary]\t Display the summary buffer
9201 \\[gnus-article-mail]\t Send a reply to the address near point
9202 \\[gnus-article-mail-with-original]\t Send a reply to the address near point; include the original article
9203 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
9204 \\[gnus-info-find-node]\t Go to the Gnus info node"
9205   (interactive)
9206   (if gnus-visual (gnus-article-make-menu-bar))
9207   (kill-all-local-variables)
9208   (setq mode-line-modified "-- ")
9209   (make-local-variable 'mode-line-format)
9210   (setq mode-line-format (copy-sequence mode-line-format))
9211   (and (equal (nth 3 mode-line-format) "   ")
9212        (setcar (nthcdr 3 mode-line-format) ""))
9213   (setq mode-name "Article")
9214   (setq major-mode 'gnus-article-mode)
9215   (make-local-variable 'minor-mode-alist)
9216   (or (assq 'gnus-show-mime minor-mode-alist)
9217       (setq minor-mode-alist
9218             (cons (list 'gnus-show-mime " MIME") minor-mode-alist)))
9219   (use-local-map gnus-article-mode-map)
9220   (make-local-variable 'page-delimiter)
9221   (setq page-delimiter gnus-page-delimiter)
9222   (buffer-disable-undo (current-buffer))
9223   (setq buffer-read-only t)             ;Disable modification
9224   (run-hooks 'gnus-article-mode-hook))
9225
9226 (defun gnus-article-setup-buffer ()
9227   "Initialize article mode buffer."
9228   (or (get-buffer gnus-article-buffer)
9229       (save-excursion
9230         (set-buffer (get-buffer-create gnus-article-buffer))
9231         (gnus-add-current-to-buffer-list)
9232         (gnus-article-mode))))
9233
9234 (defun gnus-request-article-this-buffer (article group)
9235   "Get an article and insert it into this buffer."
9236   (setq group (or group gnus-newsgroup-name))
9237   ;; Using `gnus-request-article' directly will insert the article into
9238   ;; `nntp-server-buffer' - so we'll save some time by not having to
9239   ;; copy it from the server buffer into the article buffer.
9240
9241   ;; We only request an article by message-id when we do not have the
9242   ;; headers for it, so we'll have to get those.
9243   (and (stringp article) 
9244        (let ((gnus-override-method gnus-refer-article-method))
9245          (gnus-read-header article)))
9246
9247   ;; If the article number is negative, that means that this article
9248   ;; doesn't belong in this newsgroup (possibly), so we find its
9249   ;; message-id and request it by id instead of number.
9250   (if (and (numberp article) (< article 0))
9251       (save-excursion
9252         (set-buffer gnus-summary-buffer)
9253         (let ((header (gnus-gethash (int-to-string article)
9254                                     gnus-newsgroup-headers-hashtb-by-number)))
9255           (if (vectorp header)
9256               ;; It's a real article.
9257               (setq article (header-id header))
9258             ;; It is an extracted pseudo-article.
9259             (setq article nil)
9260             (gnus-request-pseudo-article header)))))
9261
9262   ;; Check the cache.
9263   (if (and gnus-use-cache
9264            (numberp article)
9265            (gnus-cache-request-article article group))
9266       'article
9267     ;; Get the article and into the article buffer.
9268     (if article
9269         (progn
9270           (erase-buffer)
9271           (let ((gnus-override-method 
9272                  (and (stringp article) gnus-refer-article-method)))
9273             (and (gnus-request-article article group (current-buffer))
9274                  'article)))
9275       'pseudo)))
9276
9277 (defun gnus-read-header (id)
9278   "Read the headers of article ID and enter them into the Gnus system."
9279   (or gnus-newsgroup-headers-hashtb-by-number
9280       (gnus-make-headers-hashtable-by-number))
9281   (let (header)
9282     (if (not (setq header 
9283                    (car (if (let ((gnus-nov-is-evil t))
9284                               (gnus-retrieve-headers 
9285                                (list id) gnus-newsgroup-name))
9286                             (gnus-get-newsgroup-headers)))))
9287         nil
9288       (if (stringp id)
9289           (header-set-number header gnus-reffed-article-number))
9290       (setq gnus-newsgroup-headers (cons header gnus-newsgroup-headers))
9291       (gnus-sethash (int-to-string (header-number header)) header
9292                     gnus-newsgroup-headers-hashtb-by-number)
9293       (if (stringp id)
9294           (setq gnus-reffed-article-number (1- gnus-reffed-article-number)))
9295       (setq gnus-current-headers header)
9296       header)))
9297
9298 (defun gnus-article-prepare (article &optional all-headers header)
9299   "Prepare ARTICLE in article mode buffer.
9300 ARTICLE should either be an article number or a Message-ID.
9301 If ARTICLE is an id, HEADER should be the article headers.
9302 If ALL-HEADERS is non-nil, no headers are hidden."
9303   (save-excursion
9304     ;; Make sure we start in a summary buffer.
9305     (or (eq major-mode 'gnus-summary-mode)
9306         (set-buffer gnus-summary-buffer))
9307     (setq gnus-summary-buffer (current-buffer))
9308     ;; Make sure the connection to the server is alive.
9309     (or (gnus-server-opened (gnus-find-method-for-group gnus-newsgroup-name))
9310         (progn
9311           (gnus-check-news-server 
9312            (gnus-find-method-for-group gnus-newsgroup-name))
9313           (gnus-request-group gnus-newsgroup-name t)))
9314     (or gnus-newsgroup-headers-hashtb-by-number
9315         (gnus-make-headers-hashtable-by-number))
9316     (let* ((article (if header (header-number header) article))
9317            (summary-buffer (current-buffer))
9318            (internal-hook gnus-article-internal-prepare-hook)
9319            (bookmark (cdr (assq article gnus-newsgroup-bookmarks)))
9320            (group gnus-newsgroup-name)
9321            result)
9322       (save-excursion
9323         (gnus-article-setup-buffer)
9324         (set-buffer gnus-article-buffer)
9325         (let ((buffer-read-only nil))
9326           (if (not (setq result (gnus-request-article-this-buffer 
9327                                  article group)))
9328               ;; There is no such article.
9329               (progn
9330                 (save-excursion
9331                   (set-buffer gnus-summary-buffer)
9332                   (setq gnus-current-article 0)
9333                   (and (numberp article) 
9334                        (gnus-summary-mark-as-read article gnus-canceled-mark))
9335                   (message "No such article (may be canceled)")
9336                   (and (numberp article)
9337                        (setq gnus-current-article article))
9338                   (ding))
9339                 (and (numberp article)
9340                      (setq gnus-article-current 
9341                            (cons gnus-newsgroup-name article)))
9342                 nil)
9343             (if (not (eq result 'article))
9344                 (progn
9345                   (save-excursion
9346                     (set-buffer summary-buffer)
9347                     (setq gnus-last-article gnus-current-article
9348                           gnus-newsgroup-history (cons gnus-current-article
9349                                                        gnus-newsgroup-history)
9350                           gnus-current-article 0
9351                           gnus-current-headers nil
9352                           gnus-article-current nil)
9353                     (gnus-configure-windows 'article)
9354                     (gnus-set-global-variables))
9355                   (gnus-set-mode-line 'article))
9356               ;; The result from the `request' was an actual article -
9357               ;; or at least some text that is now displayed in the
9358               ;; article buffer.
9359               (if (and (numberp article)
9360                        (not (eq article gnus-current-article)))
9361                   ;; Seems like a new article has been selected.
9362                   ;; `gnus-current-article' must be an article number.
9363                   (save-excursion
9364                     (set-buffer summary-buffer)
9365                     (setq gnus-last-article gnus-current-article
9366                           gnus-newsgroup-history (cons gnus-current-article
9367                                                        gnus-newsgroup-history)
9368                           gnus-current-article article
9369                           gnus-current-headers 
9370                           (gnus-get-header-by-number gnus-current-article)
9371                           gnus-article-current 
9372                           (cons gnus-newsgroup-name gnus-current-article))
9373                     (run-hooks 'gnus-mark-article-hook)
9374                     (gnus-set-mode-line 'summary)
9375                     (and gnus-visual 
9376                          (run-hooks 'gnus-visual-mark-article-hook))
9377                     ;; Set the global newsgroup variables here.
9378                     ;; Suggested by Jim Sisolak
9379                     ;; <sisolak@trans4.neep.wisc.edu>.
9380                     (gnus-set-global-variables)
9381                     (and gnus-use-cache 
9382                          (gnus-cache-possibly-enter-article
9383                           group article
9384                           (gnus-get-header-by-number article)
9385                           (memq article gnus-newsgroup-marked)
9386                           (memq article gnus-newsgroup-dormant)
9387                           (memq article gnus-newsgroup-unreads)))))
9388               ;; gnus-have-all-headers must be either T or NIL.
9389               (setq gnus-have-all-headers
9390                     (not (not (or all-headers gnus-show-all-headers))))
9391               ;; Hooks for getting information from the article.
9392               ;; This hook must be called before being narrowed.
9393               (run-hooks 'internal-hook)
9394               (run-hooks 'gnus-article-prepare-hook)
9395               ;; Decode MIME message.
9396               (if (and gnus-show-mime
9397                        (gnus-fetch-field "Mime-Version"))
9398                   (funcall gnus-show-mime-method))
9399               ;; Perform the article display hooks.
9400               (let ((buffer-read-only nil))
9401                 (run-hooks 'gnus-article-display-hook))
9402               ;; Do page break.
9403               (goto-char (point-min))
9404               (and gnus-break-pages (gnus-narrow-to-page))
9405               (gnus-set-mode-line 'article)
9406               (gnus-configure-windows 'article)
9407               (goto-char 1)
9408               (set-window-start 
9409                (get-buffer-window gnus-article-buffer)
9410                (cond (bookmark
9411                       (message "Moved to bookmark")
9412                       (search-forward "\n\n" nil t)
9413                       (forward-line bookmark)
9414                       (point))
9415                      (t
9416                       (point-min))))
9417               t)))))))
9418
9419 (defun gnus-article-show-all-headers ()
9420   "Show all article headers in article mode buffer."
9421   (save-excursion 
9422     (setq gnus-have-all-headers t)
9423     (gnus-article-setup-buffer)
9424     (set-buffer gnus-article-buffer)
9425     (let ((buffer-read-only nil))
9426       (remove-text-properties 1 (point-max) '(invisible t)))))
9427
9428 (defun gnus-article-hide-headers-if-wanted ()
9429   "Hide unwanted headers if `gnus-have-all-headers' is nil.
9430 Provided for backwards compatability."
9431   (or gnus-have-all-headers
9432       (gnus-article-hide-headers)))
9433
9434 (defun gnus-article-hide-headers (&optional delete)
9435   "Hide unwanted headers and possibly sort them as well."
9436   (interactive "P")
9437   (save-excursion
9438     (set-buffer gnus-article-buffer)
9439     (save-restriction
9440       (let ((sorted gnus-sorted-header-list)
9441             (buffer-read-only nil)
9442             want want-list beg want-l)
9443         ;; First we narrow to just the headers.
9444         (widen)
9445         (goto-char 1)
9446         ;; Hide any "From " lines at the beginning of (mail) articles. 
9447         (while (looking-at "From ")
9448           (forward-line 1))
9449         (if (/= (point) 1) 
9450             (add-text-properties 1 (point) '(invisible t)))
9451         ;; Then treat the rest of the header lines.
9452         (narrow-to-region 
9453          (point) 
9454          (progn (search-forward "\n\n" nil t) (forward-line -1) (point)))
9455         ;; Then we use the two regular expressions
9456         ;; `gnus-ignored-headers' and `gnus-visible-headers' to
9457         ;; select which header lines is to remain visible in the
9458         ;; article buffer.
9459         (goto-char 1)
9460         (while (re-search-forward "^[^ \t]*:" nil t)
9461           (beginning-of-line)
9462           ;; We add the headers we want to keep to a list and delete
9463           ;; them from the buffer.
9464           (if (or (and (stringp gnus-visible-headers)
9465                        (looking-at gnus-visible-headers))
9466                   (and (not (stringp gnus-visible-headers))
9467                        (stringp gnus-ignored-headers)
9468                        (not (looking-at gnus-ignored-headers))))
9469               (progn
9470                 (setq beg (point))
9471                 (forward-line 1)
9472                 ;; Be sure to get multi-line headers...
9473                 (re-search-forward "^[^ \t]*:" nil t)
9474                 (beginning-of-line)
9475                 (setq want-list 
9476                       (cons (buffer-substring beg (point)) want-list))
9477                 (delete-region beg (point))
9478                 (goto-char beg))
9479             (forward-line 1)))
9480         ;; Next we perform the sorting by looking at
9481         ;; `gnus-sorted-header-list'. 
9482         (goto-char 1)
9483         (while (and sorted want-list)
9484           (setq want-l want-list)
9485           (while (and want-l
9486                       (not (string-match (car sorted) (car want-l))))
9487             (setq want-l (cdr want-l)))
9488           (if want-l 
9489               (progn
9490                 (insert (car want-l))
9491                 (setq want-list (delq (car want-l) want-list))))
9492           (setq sorted (cdr sorted)))
9493         ;; Any headers that were not matched by the sorted list we
9494         ;; just tack on the end of the visible header list.
9495         (while want-list
9496           (insert (car want-list))
9497           (setq want-list (cdr want-list)))
9498         ;; And finally we make the unwanted headers invisible.
9499         (if delete
9500             (delete-region (point) (point-max))
9501           ;; Suggested by Sudish Joseph <joseph@cis.ohio-state.edu>.
9502           (add-text-properties (point) (point-max) '(invisible t)))))))
9503
9504 (defun gnus-article-hide-signature ()
9505   "Hides the signature in an article.
9506 It does this by hiding everyting after \"^-- *$\", which is what all
9507 signatures should be preceded by. Note that this may mean that parts
9508 of an article may disappear if the article has such a line in the
9509 middle of the text."
9510   (interactive)
9511   (save-excursion
9512     (set-buffer gnus-article-buffer)
9513     (let ((buffer-read-only nil))
9514       (goto-char (point-max))
9515       (if (re-search-backward "^-- *$" nil t)
9516           (progn
9517             (add-text-properties (point) (point-max) '(invisible t)))))))
9518
9519 (defun gnus-article-hide-citation ()
9520   "Hide all cited text.
9521 This function uses the famous, extremely intelligent \"shoot in foot\"
9522 algorithm - which is simply deleting all lines that start with
9523 \">\". Your mileage may vary. If you come up with anything better,
9524 please do mail it to me."
9525   (interactive)
9526   (save-excursion
9527     (set-buffer gnus-article-buffer)
9528     (let ((buffer-read-only nil))
9529       (goto-char 1)
9530       (search-forward "\n\n" nil t)
9531       (while (not (eobp))
9532         (if (looking-at ">")
9533             (add-text-properties 
9534              (point) (save-excursion (forward-line 1) (point))
9535              '(invisible t)))
9536         (forward-line 1)))))
9537
9538 ;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.
9539 (defun gnus-article-treat-overstrike ()
9540   "Translate overstrikes into bold text."
9541   (interactive)
9542   (save-excursion
9543     (set-buffer gnus-article-buffer)
9544     (let ((buffer-read-only nil))
9545       (while (search-forward "\b" nil t)
9546         (let ((next (following-char))
9547               (previous (char-after (- (point) 2))))
9548           (cond ((eq next previous)
9549                  (delete-region (- (point) 2) (point))
9550                  (put-text-property (point) (1+ (point))
9551                                     'face 'bold))
9552                 ((eq next ?_)
9553                  (delete-region (1- (point)) (1+ (point)))
9554                  (put-text-property (1- (point)) (point)
9555                                     'face 'underline))
9556                 ((eq previous ?_)
9557                  (delete-region (- (point) 2) (point))
9558                  (put-text-property (point) (1+ (point))
9559                                     'face 'underline))))))))
9560
9561 (defun gnus-article-word-wrap ()
9562   "Format too long lines."
9563   (interactive)
9564   (save-excursion
9565     (set-buffer gnus-article-buffer)
9566     (let ((buffer-read-only nil))
9567       (goto-char 1)
9568       (search-forward "\n\n" nil t)
9569       (end-of-line 1)
9570       (let ((paragraph-start "^\\W"))
9571         (while (not (eobp))
9572           (and (>= (current-column) (window-width))
9573                (/= (preceding-char) ?:)
9574                (fill-paragraph nil))
9575           (end-of-line 2))))))
9576
9577 (defun gnus-article-remove-cr ()
9578   "Remove carriage returns from an article."
9579   (interactive)
9580   (save-excursion
9581     (set-buffer gnus-article-buffer)
9582     (let ((buffer-read-only nil))
9583       (goto-char (point-min))
9584       (while (search-forward "\r" nil t)
9585         (replace-match "" t t)))))
9586
9587 (defun gnus-article-display-x-face ()
9588   "Look for an X-Face header and display it if present."
9589   (interactive)
9590   (save-excursion
9591     (set-buffer gnus-article-buffer)
9592     (goto-char (point-min))
9593     (if (or (not gnus-article-x-face-command)
9594             (and (re-search-forward "^\\($\\|X-Face: \\)" nil t)
9595                  (looking-at "^$")))
9596         ()
9597       (let ((face
9598              (buffer-substring
9599               (point) 
9600               (progn
9601                 (forward-line 1)
9602                 (while (and (looking-at "[ \t]") 
9603                             (zerop (forward-line 1))))
9604                 (point))))
9605             (command gnus-article-x-face-command))
9606         (if (symbolp command)
9607             (and (or (fboundp command) (error "%s is not a function" command))
9608                  (funcall command face))
9609           (let ((process (start-process "gnus-x-face" nil "sh" "-c" command)))
9610             (if process
9611                 (progn
9612                   (process-send-string process face)
9613                   (process-send-eof process))
9614               (error "Couldn't start process"))))))))
9615               
9616 (defun gnus-article-de-quoted-unreadable (&optional force)
9617   "Do a naïve translation of a quoted-printable-encoded article.
9618 This is in no way, shape or form meant as a replacement for real MIME
9619 processing, but is simply a stop-gap measure until MIME support is
9620 written.
9621 If FORCE, decode the article whether it is marked as quoted-printable
9622 or not." 
9623   (interactive (list 'force))
9624   (save-excursion
9625     (set-buffer gnus-article-buffer)
9626     (let ((case-fold-search t)
9627           (type (gnus-fetch-field "content-transfer-encoding")))
9628       (if (or force (and type (string-match "quoted-printable" type)))
9629           (progn
9630             (goto-char (point-min))
9631             (search-forward "\n\n" nil 'move)
9632             (gnus-mime-decode-quoted-printable (point) (point-max)))))))
9633
9634 (defun gnus-mime-decode-quoted-printable (from to)
9635   ;; Decode quoted-printable from region between FROM and TO.
9636   (save-excursion
9637     (goto-char from)
9638     (while (search-forward "=" to t)
9639       (cond ((eq (following-char) ?\n)
9640              (delete-char -1)
9641              (delete-char 1))
9642             ((looking-at "[0-9A-F][0-9A-F]")
9643              (delete-char -1)
9644              (insert (hexl-hex-string-to-integer
9645                       (buffer-substring (point) (+ 2 (point)))))
9646              (delete-char 2))
9647             ((message "Malformed MIME quoted-printable message"))))))
9648
9649
9650 (defun gnus-article-date-ut (&optional type)
9651   "Convert DATE date to universal time in the current article.
9652 If TYPE is `local', convert to local time; if it is `lapsed', output
9653 how much time has lapsed since DATE."
9654   (interactive (list 'ut))
9655   (let ((date (header-date (or gnus-current-headers 
9656                                (gnus-get-header-by-number
9657                                 (gnus-summary-article-number))))))
9658     (save-excursion
9659       (set-buffer gnus-article-buffer)
9660       (let ((buffer-read-only nil))
9661         (goto-char (point-min))
9662         (if (re-search-forward "^Date: \\|^X-Sent: " nil t)
9663             (delete-region (gnus-point-at-bol)
9664                            (progn (forward-line 1) (point)))
9665           (- (search-forward "\n\n") 2))
9666         (insert
9667          (cond 
9668           ((eq type 'local)
9669            (concat "Date: " (timezone-make-date-arpa-standard date) "\n"))
9670           ((eq type 'ut)
9671            (concat "Date: " (timezone-make-date-arpa-standard date nil "UT")
9672                    "\n"))
9673           ((eq type 'lapsed)
9674            (let* ((sec (- (gnus-seconds-since-epoch (current-time-string))
9675                           (gnus-seconds-since-epoch date)))
9676                   (units (list (cons 'year (* 365.25 24 60 60))
9677                                (cons 'week (* 7 24 60 60))
9678                                (cons 'day (* 24 60 60))
9679                                (cons 'hour (* 60 60))
9680                                (cons 'minute 60)
9681                                (cons 'second 1)))
9682                   num prev)
9683              (concat
9684               "X-Sent: "
9685               (mapconcat 
9686                (lambda (unit)
9687                  (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
9688                      ""
9689                    (setq sec (- sec (* num (cdr unit))))
9690                    (prog1
9691                        (concat (if prev ", " "") (int-to-string (floor num))
9692                                " " (symbol-name (car unit))
9693                                (if (> num 1) "s" ""))
9694                      (setq prev t))))
9695                units "")
9696               " ago\n")))
9697           (t
9698            (error "Unknown conversion type: %s" type))))))))
9699
9700 (defun gnus-article-date-local ()
9701   "Convert the current article date to the local timezone."
9702   (interactive)
9703   (gnus-article-date-ut 'local))
9704
9705 (defun gnus-article-date-lapsed ()
9706   "Convert the current article date to time lapsed since it was sent."
9707   (interactive)
9708   (gnus-article-date-ut 'lapsed))
9709
9710
9711 ;; Article savers.
9712
9713 (defun gnus-output-to-rmail (file-name)
9714   "Append the current article to an Rmail file named FILE-NAME."
9715   (require 'rmail)
9716   ;; Most of these codes are borrowed from rmailout.el.
9717   (setq file-name (expand-file-name file-name))
9718   (setq rmail-default-rmail-file file-name)
9719   (let ((artbuf (current-buffer))
9720         (tmpbuf (get-buffer-create " *Gnus-output*")))
9721     (save-excursion
9722       (or (get-file-buffer file-name)
9723           (file-exists-p file-name)
9724           (if (gnus-yes-or-no-p
9725                (concat "\"" file-name "\" does not exist, create it? "))
9726               (let ((file-buffer (create-file-buffer file-name)))
9727                 (save-excursion
9728                   (set-buffer file-buffer)
9729                   (rmail-insert-rmail-file-header)
9730                   (let ((require-final-newline nil))
9731                     (write-region (point-min) (point-max) file-name t 1)))
9732                 (kill-buffer file-buffer))
9733             (error "Output file does not exist")))
9734       (set-buffer tmpbuf)
9735       (buffer-disable-undo (current-buffer))
9736       (erase-buffer)
9737       (insert-buffer-substring artbuf)
9738       (gnus-convert-article-to-rmail)
9739       ;; Decide whether to append to a file or to an Emacs buffer.
9740       (let ((outbuf (get-file-buffer file-name)))
9741         (if (not outbuf)
9742             (append-to-file (point-min) (point-max) file-name)
9743           ;; File has been visited, in buffer OUTBUF.
9744           (set-buffer outbuf)
9745           (let ((buffer-read-only nil)
9746                 (msg (and (boundp 'rmail-current-message)
9747                           (symbol-value 'rmail-current-message))))
9748             ;; If MSG is non-nil, buffer is in RMAIL mode.
9749             (if msg
9750                 (progn (widen)
9751                        (narrow-to-region (point-max) (point-max))))
9752             (insert-buffer-substring tmpbuf)
9753             (if msg
9754                 (progn
9755                   (goto-char (point-min))
9756                   (widen)
9757                   (search-backward "\^_")
9758                   (narrow-to-region (point) (point-max))
9759                   (goto-char (1+ (point-min)))
9760                   (rmail-count-new-messages t)
9761                   (rmail-show-message msg)))))))
9762     (kill-buffer tmpbuf)))
9763
9764 (defun gnus-output-to-file (file-name)
9765   "Append the current article to a file named FILE-NAME."
9766   (setq file-name (expand-file-name file-name))
9767   (let ((artbuf (current-buffer))
9768         (tmpbuf (get-buffer-create " *Gnus-output*")))
9769     (save-excursion
9770       (set-buffer tmpbuf)
9771       (buffer-disable-undo (current-buffer))
9772       (erase-buffer)
9773       (insert-buffer-substring artbuf)
9774       ;; Append newline at end of the buffer as separator, and then
9775       ;; save it to file.
9776       (goto-char (point-max))
9777       (insert "\n")
9778       (append-to-file (point-min) (point-max) file-name))
9779     (kill-buffer tmpbuf)))
9780
9781 (defun gnus-convert-article-to-rmail ()
9782   "Convert article in current buffer to Rmail message format."
9783   (let ((buffer-read-only nil))
9784     ;; Convert article directly into Babyl format.
9785     ;; Suggested by Rob Austein <sra@lcs.mit.edu>
9786     (goto-char (point-min))
9787     (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
9788     (while (search-forward "\n\^_" nil t) ;single char
9789       (replace-match "\n^_" t t))               ;2 chars: "^" and "_"
9790     (goto-char (point-max))
9791     (insert "\^_")))
9792
9793 (defun gnus-narrow-to-page (&optional arg)
9794   "Make text outside current page invisible except for page delimiter.
9795 A numeric arg specifies to move forward or backward by that many pages,
9796 thus showing a page other than the one point was originally in."
9797   (interactive "P")
9798   (setq arg (if arg (prefix-numeric-value arg) 0))
9799   (save-excursion
9800     (forward-page -1)                   ;Beginning of current page.
9801     (widen)
9802     (if (> arg 0)
9803         (forward-page arg)
9804       (if (< arg 0)
9805           (forward-page (1- arg))))
9806     ;; Find the end of the page.
9807     (forward-page)
9808     ;; If we stopped due to end of buffer, stay there.
9809     ;; If we stopped after a page delimiter, put end of restriction
9810     ;; at the beginning of that line.
9811     ;; These are commented out.
9812     ;;    (if (save-excursion (beginning-of-line)
9813     ;;                  (looking-at page-delimiter))
9814     ;;  (beginning-of-line))
9815     (narrow-to-region (point)
9816                       (progn
9817                         ;; Find the top of the page.
9818                         (forward-page -1)
9819                         ;; If we found beginning of buffer, stay there.
9820                         ;; If extra text follows page delimiter on same line,
9821                         ;; include it.
9822                         ;; Otherwise, show text starting with following line.
9823                         (if (and (eolp) (not (bobp)))
9824                             (forward-line 1))
9825                         (point)))))
9826
9827 (defun gnus-gmt-to-local ()
9828   "Rewrite Date header described in GMT to local in current buffer.
9829 Intended to be used with gnus-article-prepare-hook."
9830   (save-excursion
9831     (save-restriction
9832       (widen)
9833       (goto-char (point-min))
9834       (narrow-to-region (point-min)
9835                         (progn (search-forward "\n\n" nil 'move) (point)))
9836       (goto-char (point-min))
9837       (if (re-search-forward "^Date:[ \t]\\(.*\\)$" nil t)
9838           (let ((buffer-read-only nil)
9839                 (date (buffer-substring (match-beginning 1) (match-end 1))))
9840             (delete-region (match-beginning 1) (match-end 1))
9841             (insert
9842              (timezone-make-date-arpa-standard 
9843               date nil (current-time-zone))))))))
9844
9845
9846 ;; Article mode commands
9847
9848 (defun gnus-article-next-page (lines)
9849   "Show next page of current article.
9850 If end of article, return non-nil. Otherwise return nil.
9851 Argument LINES specifies lines to be scrolled up."
9852   (interactive "P")
9853   (move-to-window-line -1)
9854   ;; Fixed by enami@ptgd.sony.co.jp (enami tsugutomo)
9855   (if (save-excursion
9856         (end-of-line)
9857         (and (pos-visible-in-window-p)  ;Not continuation line.
9858              (eobp)))
9859       ;; Nothing in this page.
9860       (if (or (not gnus-break-pages)
9861               (save-excursion
9862                 (save-restriction
9863                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
9864           t                             ;Nothing more.
9865         (gnus-narrow-to-page 1)         ;Go to next page.
9866         nil)
9867     ;; More in this page.
9868     (condition-case ()
9869         (scroll-up lines)
9870       (end-of-buffer
9871        ;; Long lines may cause an end-of-buffer error.
9872        (goto-char (point-max))))
9873     nil))
9874
9875 (defun gnus-article-prev-page (lines)
9876   "Show previous page of current article.
9877 Argument LINES specifies lines to be scrolled down."
9878   (interactive "P")
9879   (move-to-window-line 0)
9880   (if (and gnus-break-pages
9881            (bobp)
9882            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
9883       (progn
9884         (gnus-narrow-to-page -1) ;Go to previous page.
9885         (goto-char (point-max))
9886         (recenter -1))
9887     (scroll-down lines)))
9888
9889 (defun gnus-article-refer-article ()
9890   "Read article specified by message-id around point."
9891   (interactive)
9892   (search-forward ">" nil t)    ;Move point to end of "<....>".
9893   (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
9894       (let ((message-id
9895              (buffer-substring (match-beginning 1) (match-end 1))))
9896         (set-buffer gnus-summary-buffer)
9897         (gnus-summary-refer-article message-id))
9898     (error "No references around point")))
9899
9900 (defun gnus-article-mail (yank)
9901   "Send a reply to the address near point.
9902 If YANK is non-nil, include the original article."
9903   (interactive "P")
9904   (let ((address 
9905          (buffer-substring
9906           (save-excursion (re-search-backward "[ \t\n]" nil t) (1+ (point)))
9907           (save-excursion (re-search-forward "[ \t\n]" nil t) (1- (point))))))
9908     (and address
9909          (progn
9910            (switch-to-buffer gnus-summary-buffer)
9911            (funcall gnus-mail-reply-method yank address)))))
9912
9913 (defun gnus-article-mail-with-original ()
9914   "Send a reply to the address near point and include the original article."
9915   (interactive)
9916   (gnus-article-mail 'yank))
9917
9918 (defun gnus-article-show-summary ()
9919   "Reconfigure windows to show summary buffer."
9920   (interactive)
9921   (gnus-configure-windows 'article)
9922   (pop-to-buffer gnus-summary-buffer)
9923   (gnus-summary-goto-subject gnus-current-article))
9924
9925 (defun gnus-article-describe-briefly ()
9926   "Describe article mode commands briefly."
9927   (interactive)
9928   (message
9929    (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")))
9930
9931 (defun gnus-article-summary-command ()
9932   "Execute the last keystroke in the summary buffer."
9933   (interactive)
9934   (let ((obuf (current-buffer))
9935         (owin (current-window-configuration)))
9936     (switch-to-buffer gnus-summary-buffer 'norecord)
9937     (execute-kbd-macro (this-command-keys))
9938     (set-buffer obuf)
9939     (let ((npoint (point)))
9940       (set-window-configuration owin)
9941       (set-window-start (get-buffer-window (current-buffer)) (point)))))
9942
9943 (defun gnus-article-summary-command-nosave ()
9944   "Execute the last keystroke in the summary buffer."
9945   (interactive)
9946   (let ((obuf (current-buffer))
9947         (owin (current-window-configuration)))
9948     (switch-to-buffer gnus-summary-buffer 'norecord)
9949     (execute-kbd-macro (this-command-keys))))
9950
9951 ;; caesar-region written by phr@prep.ai.mit.edu  Nov 86
9952 ;; Modified by tower@prep Nov 86
9953 ;; Modified by umerin@flab.flab.Fujitsu.JUNET for ROT47.
9954
9955 (defun gnus-caesar-region (&optional n)
9956   "Caesar rotation of region by N, default 13, for decrypting netnews.
9957 ROT47 will be performed for Japanese text in any case."
9958   (interactive (if current-prefix-arg   ; Was there a prefix arg?
9959                    (list (prefix-numeric-value current-prefix-arg))
9960                  (list nil)))
9961   (cond ((not (numberp n)) (setq n 13))
9962         (t (setq n (mod n 26))))        ;canonicalize N
9963   (if (not (zerop n))           ; no action needed for a rot of 0
9964       (progn
9965         (if (or (not (boundp 'caesar-translate-table))
9966                 (not caesar-translate-table)
9967                 (/= (aref caesar-translate-table ?a) (+ ?a n)))
9968             (let ((i 0) 
9969                   (lower "abcdefghijklmnopqrstuvwxyz")
9970                   upper)
9971               (message "Building caesar-translate-table...")
9972               (setq caesar-translate-table (make-vector 256 0))
9973               (while (< i 256)
9974                 (aset caesar-translate-table i i)
9975                 (setq i (1+ i)))
9976               (setq lower (concat lower lower)
9977                     upper (upcase lower)
9978                     i 0)
9979               (while (< i 26)
9980                 (aset caesar-translate-table (+ ?a i) (aref lower (+ i n)))
9981                 (aset caesar-translate-table (+ ?A i) (aref upper (+ i n)))
9982                 (setq i (1+ i)))
9983               ;; ROT47 for Japanese text.
9984               ;; Thanks to ichikawa@flab.fujitsu.junet.
9985               (setq i 161)
9986               (let ((t1 (logior ?O 128))
9987                     (t2 (logior ?! 128))
9988                     (t3 (logior ?~ 128)))
9989                 (while (< i 256)
9990                   (aset caesar-translate-table i
9991                         (let ((v (aref caesar-translate-table i)))
9992                           (if (<= v t1) (if (< v t2) v (+ v 47))
9993                             (if (<= v t3) (- v 47) v))))
9994                   (setq i (1+ i))))
9995               (message "Building caesar-translate-table... done")))
9996         (let ((from (region-beginning))
9997               (to (region-end))
9998               (i 0) str len)
9999           (setq str (buffer-substring from to))
10000           (setq len (length str))
10001           (while (< i len)
10002             (aset str i (aref caesar-translate-table (aref str i)))
10003             (setq i (1+ i)))
10004           (goto-char from)
10005           (delete-region from to)
10006           (insert str)))))
10007
10008 \f
10009 ;; Basic ideas by emv@math.lsa.umich.edu (Edward Vielmetti)
10010
10011 ;;;###autoload
10012 (defalias 'gnus-batch-kill 'gnus-batch-score)
10013 ;;;###autoload
10014 (defun gnus-batch-score ()
10015   "Run batched scoring.
10016 Usage: emacs -batch -l gnus -f gnus-batch-score <newsgroups> ...
10017 Newsgroups is a list of strings in Bnews format.  If you want to score
10018 the comp hierarchy, you'd say \"comp.all\". If you would not like to
10019 score the alt hierarchy, you'd say \"!alt.all\"."
10020   (interactive)
10021   (let* ((yes-and-no
10022           (gnus-parse-n-options
10023            (apply (function concat)
10024                   (mapcar (lambda (g) (concat g " "))
10025                           command-line-args-left))))
10026          (gnus-expert-user t)
10027          (nnmail-spool-file nil)
10028          (gnus-use-dribble-file nil)
10029          (yes (car yes-and-no))
10030          (no (cdr yes-and-no))
10031          group subscribed newsrc entry
10032          ;; Disable verbose message.
10033          gnus-novice-user gnus-large-newsgroup)
10034     ;; Eat all arguments.
10035     (setq command-line-args-left nil)
10036     ;; Start Gnus.
10037     (gnus)
10038     ;; Apply kills to specified newsgroups in command line arguments.
10039     (setq newsrc (cdr gnus-newsrc-alist))
10040     (while newsrc
10041       (setq group (car (car newsrc)))
10042       (setq entry (gnus-gethash group gnus-newsrc-hashtb))
10043       (if (and (<= (nth 1 (car newsrc)) gnus-level-subscribed)
10044                (and (car entry)
10045                     (or (eq (car entry) t)
10046                         (not (zerop (car entry)))))
10047                (if yes (string-match yes group) t)
10048                (or (null no) (not (string-match no group))))
10049           (progn
10050             (gnus-summary-read-group group nil t)
10051             (and (eq (current-buffer) (get-buffer gnus-summary-buffer))
10052                  (gnus-summary-exit))))
10053       (setq newsrc (cdr newsrc)))
10054     ;; Exit Emacs.
10055     (switch-to-buffer gnus-group-buffer)
10056     (gnus-group-save-newsrc)))
10057
10058 (defun gnus-apply-kill-file ()
10059   "Apply a kill file to the current newsgroup.
10060 Returns the number of articles marked as read."
10061   (if (or (file-exists-p (gnus-newsgroup-kill-file nil))
10062           (file-exists-p (gnus-newsgroup-kill-file gnus-newsgroup-name)))
10063       (gnus-apply-kill-file-internal)
10064     0))
10065
10066 (defun gnus-kill-save-kill-buffer ()
10067   (save-excursion
10068     (let ((file (gnus-newsgroup-kill-file gnus-newsgroup-name)))
10069       (if (get-file-buffer file)
10070           (progn
10071             (set-buffer (get-file-buffer file))
10072             (and (buffer-modified-p) (save-buffer))
10073             (kill-buffer (current-buffer)))))))
10074
10075 (defvar gnus-kill-file-name "KILL"
10076   "Suffix of the kill files.")
10077
10078 (defun gnus-newsgroup-kill-file (newsgroup)
10079   "Return the name of a kill file name for NEWSGROUP.
10080 If NEWSGROUP is nil, return the global kill file name instead."
10081   (cond ((or (null newsgroup)
10082              (string-equal newsgroup ""))
10083          ;; The global KILL file is placed at top of the directory.
10084          (expand-file-name gnus-kill-file-name
10085                            (or gnus-kill-files-directory "~/News")))
10086         ((gnus-use-long-file-name 'not-kill)
10087          ;; Append ".KILL" to newsgroup name.
10088          (expand-file-name (concat newsgroup "." gnus-kill-file-name)
10089                            (or gnus-kill-files-directory "~/News")))
10090         (t
10091          ;; Place "KILL" under the hierarchical directory.
10092          (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
10093                                    "/" gnus-kill-file-name)
10094                            (or gnus-kill-files-directory "~/News")))))
10095
10096 \f
10097 ;;;
10098 ;;; Dribble file
10099 ;;;
10100
10101 (defvar gnus-dribble-ignore nil)
10102
10103 (defun gnus-dribble-file-name ()
10104   (concat gnus-startup-file "-dribble"))
10105
10106 (defun gnus-dribble-open ()
10107   (save-excursion 
10108     (set-buffer 
10109      (setq gnus-dribble-buffer (find-file-noselect (gnus-dribble-file-name))))
10110     (buffer-disable-undo (current-buffer))
10111     (bury-buffer gnus-dribble-buffer)
10112     (auto-save-mode t)
10113     (goto-char (point-max))))
10114
10115 (defun gnus-dribble-enter (string)
10116   (if (and (not gnus-dribble-ignore)
10117            gnus-dribble-buffer
10118            (buffer-name gnus-dribble-buffer))
10119       (let ((obuf (current-buffer)))
10120         (set-buffer gnus-dribble-buffer)
10121         (insert string "\n")
10122         (set-window-point (get-buffer-window (current-buffer)) (point-max))
10123         (set-buffer obuf))))
10124
10125 (defun gnus-dribble-read-file ()
10126   (let ((dribble-file (gnus-dribble-file-name)))
10127     (save-excursion 
10128       (set-buffer (setq gnus-dribble-buffer 
10129                         (get-buffer-create 
10130                          (file-name-nondirectory dribble-file))))
10131       (gnus-add-current-to-buffer-list)
10132       (erase-buffer)
10133       (set-visited-file-name dribble-file)
10134       (buffer-disable-undo (current-buffer))
10135       (bury-buffer (current-buffer))
10136       (set-buffer-modified-p nil)
10137       (let ((auto (make-auto-save-file-name))
10138             (gnus-dribble-ignore t))
10139         (if (or (file-exists-p auto) (file-exists-p dribble-file))
10140             (progn
10141               (if (file-newer-than-file-p auto dribble-file)
10142                   (setq dribble-file auto))
10143               (insert-file-contents dribble-file)
10144               (if (not (zerop (buffer-size)))
10145                   (set-buffer-modified-p t))
10146               (if (gnus-y-or-n-p 
10147                    "Auto-save file exists. Do you want to read it? ")
10148                   (progn
10149                     (message "Reading %s..." dribble-file) 
10150                     (eval-current-buffer)
10151                     (message "Reading %s...done" dribble-file)))))))))
10152
10153 (defun gnus-dribble-delete-file ()
10154   (if (file-exists-p (gnus-dribble-file-name))
10155       (delete-file (gnus-dribble-file-name)))
10156   (if gnus-dribble-buffer
10157       (save-excursion
10158         (set-buffer gnus-dribble-buffer)
10159         (let ((auto (make-auto-save-file-name)))
10160           (if (file-exists-p auto)
10161               (delete-file auto))
10162           (erase-buffer)
10163           (set-buffer-modified-p nil)))))
10164
10165 (defun gnus-dribble-save ()
10166   (if (and gnus-dribble-buffer
10167            (buffer-name gnus-dribble-buffer))
10168       (save-excursion
10169         (set-buffer gnus-dribble-buffer)
10170         (save-buffer))))
10171
10172 (defun gnus-dribble-clear ()
10173   (save-excursion
10174     (if (gnus-buffer-exists-p gnus-dribble-buffer)
10175         (progn
10176           (set-buffer gnus-dribble-buffer)
10177           (erase-buffer)
10178           (set-buffer-modified-p nil)
10179           (setq buffer-saved-size (buffer-size))))))
10180
10181 ;;;
10182 ;;; Server Communication
10183 ;;;
10184
10185 ;; All the Gnus backends have the same interface, and should return
10186 ;; data in a similar format. Below is an overview of what functions
10187 ;; these packages must supply and what results they should return.
10188 ;;
10189 ;; Variables:
10190 ;;
10191 ;; `nntp-server-buffer' - All data should be returned to Gnus in this
10192 ;; buffer. 
10193 ;;
10194 ;; Functions for the imaginary backend `choke':
10195 ;;
10196 ;; `choke-retrieve-headers ARTICLES &optional GROUP SERVER'
10197 ;; Should return all headers for all ARTICLES, or return NOV lines for
10198 ;; the same.
10199 ;;
10200 ;; `choke-request-group GROUP &optional SERVER DISCARD'
10201 ;; Switch to GROUP. If DISCARD is nil, active information on the group
10202 ;; must be returned.
10203 ;;
10204 ;; `choke-close-group GROUP &optional SERVER'
10205 ;; Close group. Most backends won't have to do anything with this
10206 ;; call, but it is an opportunity to clean up, if that is needed. It
10207 ;; is called when Gnus exits a group.
10208 ;;
10209 ;; `choke-request-article ARTICLE &optional GROUP SERVER'
10210 ;; Return ARTICLE, which is either an article number or
10211 ;; message-id. Note that not all backends can return articles based on
10212 ;; message-id. 
10213 ;;
10214 ;; `choke-request-list SERVER'
10215 ;; Return a list of all newsgroups on SERVER.
10216 ;;
10217 ;; `choke-request-list-newsgroups SERVER'
10218 ;; Return a list of descriptions of all newsgroups on SERVER.
10219 ;;
10220 ;; `choke-request-newgroups DATE &optional SERVER'
10221 ;; Return a list of all groups that have arrived after DATE on
10222 ;; SERVER. Note that the date doesn't have to be respected - Gnus will
10223 ;; always check whether the groups are old or not. Backends that do
10224 ;; not store date information may just return the entire list of
10225 ;; groups, although this might not be a good idea in general.
10226 ;;
10227 ;; `choke-request-post-buffer METHOD HEADER ARTICLE-BUFFER GROUP INFO'
10228 ;; Should return a buffer that is suitable for "posting". nnspool and
10229 ;; nntp return a `*post-buffer*', and nnmail return a `*mail*'
10230 ;; buffer. This function should fill out the appropriate headers. 
10231 ;;
10232 ;; `choke-request-post &optional SERVER'
10233 ;; Function that will be called from a buffer to be posted. 
10234 ;;
10235 ;; `choke-open-server SERVER &optional ARGUMENT'
10236 ;; Open a connection to SERVER.
10237 ;;
10238 ;; `choke-close-server &optional SERVER'
10239 ;; Close the connection to SERVER.
10240 ;;
10241 ;; `choke-server-opened &optional SERVER'
10242 ;; Whether the conenction to SERVER is opened or not.
10243 ;;
10244 ;; `choke-server-status &optional SERVER'
10245 ;; Should return a status string (not in the nntp buffer, but as the
10246 ;; result of the function).
10247 ;;
10248 ;; `choke-retrieve-groups GROUPS &optional SERVER'
10249 ;; Optional function for retrieving active file info on all groups in
10250 ;; GROUPS.  Two return formats are supported: The normal active file
10251 ;; format, and a list of GROUP lines.  This function should return (as
10252 ;; a function value) either `active' or `group', depending on what
10253 ;; format it returns.
10254 ;;
10255 ;; The following functions are optional and apply only to backends
10256 ;; that are able to control the contents of their groups totally
10257 ;; (ie. mail backends.)  Backends that aren't able to do that
10258 ;; shouldn't define these functions at all. Gnus will check for their
10259 ;; presence before attempting to call them.
10260 ;;
10261 ;; `choke-request-expire-articles ARTICLES &optional NEWSGROUP SERVER'
10262 ;; Should expire (according to some aging scheme) all ARTICLES. Most
10263 ;; backends will not be able to expire articles. Should return a list
10264 ;; of all articles that were not expired.
10265 ;;
10266 ;; `choke-request-move-article ARTICLE GROUP SERVER ACCEPT-FORM &optional LAST'
10267 ;; Should move ARTICLE from GROUP on SERVER by using ACCEPT-FORM.
10268 ;; Removes any information it has added to the article (extra headers,
10269 ;; whatever - make it as clean as possible), and then passes the
10270 ;; article on by evaling ACCEPT-FORM, which is normally a call to the
10271 ;; function described below. If the ACCEPT-FORM returns a non-nil
10272 ;; value, the article should then be deleted. If LAST is nil, that
10273 ;; means that there will be further calls to this function. This might
10274 ;; be taken as an advice not to save buffers/internal variables just
10275 ;; yet, but wait until the last call to speed things up.
10276 ;;
10277 ;; `choke-request-accept-article GROUP &optional LAST' 
10278 ;; The contents of the current buffer will be put into GROUP.  There
10279 ;; should, of course, be an article in the current buffer.  This
10280 ;; function is normally only called by the function described above,
10281 ;; and LAST works the same way as in that function.
10282 ;;
10283 ;; `choke-request-replace-article ARTICLE GROUP BUFFER'
10284 ;; Replace ARTICLE in GROUP with the contents of BUFFER.
10285 ;; This provides an easy interface for allowing editing of
10286 ;; articles. Note that even headers may be edited, so the backend has
10287 ;; to update any tables (nov buffers, etc) that it maintains after
10288 ;; replacing the article.
10289 ;;
10290 ;; `choke-request-create-group GROUP &optional SERVER'
10291 ;; Create GROUP on SERVER.  This might be a new, empty group, or it
10292 ;; might be a group that already exists, but hasn't been registered
10293 ;; yet. 
10294 ;;
10295 ;; All these functions must return nil if they couldn't service the
10296 ;; request. If the optional arguments are not supplied, some "current"
10297 ;; or "default" values should be used. In short, one should emulate an
10298 ;; NNTP server, in a way.
10299 ;;
10300 ;; If you want to write a new backend, you just have to supply the
10301 ;; functions listed above. In addition, you must enter the new backend
10302 ;; into the list of valid select methods:
10303 ;; (setq gnus-valid-select-methods 
10304 ;;       (cons '("choke" mail) gnus-valid-select-methods))
10305 ;; The first element in this list is the name of the backend. Other
10306 ;; elemnets may be `mail' (for mail groups),  `post' (for news
10307 ;; groups), `none' (neither), `respool' (for groups that can control
10308 ;; their contents). 
10309
10310 (defun gnus-start-news-server (&optional confirm)
10311   "Open a method for getting news.
10312 If CONFIRM is non-nil, the user will be asked for an NNTP server."
10313   (let (how where)
10314     (if gnus-current-select-method
10315         ;; Stream is already opened.
10316         nil
10317       ;; Open NNTP server.
10318       (if (null gnus-nntp-service) (setq gnus-nntp-server nil))
10319       (if confirm
10320           (progn
10321             ;; Read server name with completion.
10322             (setq gnus-nntp-server
10323                   (completing-read "NNTP server: "
10324                                    (mapcar (lambda (server) (list server))
10325                                            (cons (list gnus-nntp-server)
10326                                                  gnus-secondary-servers))
10327                                    nil nil gnus-nntp-server))))
10328
10329       (if (and gnus-nntp-server 
10330                (stringp gnus-nntp-server)
10331                (not (string= gnus-nntp-server "")))
10332           (setq gnus-select-method
10333                 (cond ((or (string= gnus-nntp-server "")
10334                            (string= gnus-nntp-server "::"))
10335                        (list 'nnspool (system-name)))
10336                       ((string-match "^:" gnus-nntp-server)
10337                        (list 'nnmh gnus-nntp-server 
10338                              (list 'nnmh-directory 
10339                                    (file-name-as-directory
10340                                     (expand-file-name
10341                                      (concat "~/" (substring
10342                                                    gnus-nntp-server 1)))))))
10343                       (t
10344                        (list 'nntp gnus-nntp-server)))))
10345
10346       (setq how (car gnus-select-method))
10347       (setq where (car (cdr gnus-select-method)))
10348       (cond ((eq how 'nnspool)
10349              (require 'nnspool)
10350              (message "Looking up local news spool..."))
10351             ((eq how 'nnmh)
10352              (require 'nnmh)
10353              (message "Looking up mh spool..."))
10354             (t
10355              (require 'nntp)))
10356       (setq gnus-current-select-method gnus-select-method)
10357       (run-hooks 'gnus-open-server-hook)
10358       (or 
10359        ;; gnus-open-server-hook might have opened it
10360        (gnus-server-opened gnus-select-method)  
10361        (gnus-open-server gnus-select-method)
10362        (gnus-y-or-n-p
10363         (format
10364          "%s server on %s can't be opened. Continue? "
10365          (car gnus-select-method) (nth 1 gnus-select-method)))
10366        (progn
10367          (message "Couldn't open server on %s" (nth 1 gnus-select-method))
10368          (ding)
10369          nil)))))
10370
10371 (defun gnus-check-news-server (method)
10372   "If the news server is down, start it up again."
10373   (let ((method (if method method gnus-select-method)))
10374     (and (stringp method)
10375          (setq method (gnus-server-to-method method)))
10376     (if (gnus-server-opened method)
10377         ;; Stream is already opened.
10378         t
10379       ;; Open server.
10380       (message "Opening server %s on %s..." (car method) (nth 1 method))
10381       (run-hooks 'gnus-open-server-hook)
10382       (or (gnus-server-opened method)
10383           (gnus-open-server method))
10384       (message ""))))
10385
10386 (defun gnus-nntp-message (&optional message)
10387   "Check the status of the NNTP server.
10388 If the status of the server is clear and MESSAGE is non-nil, MESSAGE
10389 is returned insted of the status string."
10390   (let ((status (gnus-status-message (gnus-find-method-for-group 
10391                                       gnus-newsgroup-name)))
10392         (message (or message "")))
10393     (if (and (stringp status) (> (length status) 0))
10394         status message)))
10395
10396 (defun gnus-get-function (method function)
10397   (and (stringp method)
10398        (setq method (gnus-server-to-method method)))
10399   (let ((func (intern (format "%s-%s" (car method) function))))
10400     (if (not (fboundp func)) 
10401         (progn
10402           (require (car method))
10403           (if (not (fboundp func)) 
10404               (error "No such function: %s" func))))
10405     func))
10406
10407 ;;; Interface functions to the backends.
10408
10409 (defun gnus-open-server (method)
10410   (funcall (gnus-get-function method 'open-server)
10411            (nth 1 method) (nthcdr 2 method)))
10412
10413 (defun gnus-close-server (method)
10414   (funcall (gnus-get-function method 'close-server) (nth 1 method)))
10415
10416 (defun gnus-request-list (method)
10417   (funcall (gnus-get-function method 'request-list) (nth 1 method)))
10418
10419 (defun gnus-request-list-newsgroups (method)
10420   (funcall (gnus-get-function method 'request-list-newsgroups) (nth 1 method)))
10421
10422 (defun gnus-request-newgroups (date method)
10423   (funcall (gnus-get-function method 'request-newgroups) 
10424            date (nth 1 method)))
10425
10426 (defun gnus-server-opened (method)
10427   (funcall (gnus-get-function method 'server-opened) (nth 1 method)))
10428
10429 (defun gnus-status-message (method)
10430   (let ((method (if (stringp method) (gnus-find-method-for-group method)
10431                   method)))
10432     (funcall (gnus-get-function method 'status-message) (nth 1 method))))
10433
10434 (defun gnus-request-group (group &optional dont-check)
10435   (let ((method (gnus-find-method-for-group group)))
10436     (funcall (gnus-get-function method 'request-group) 
10437              (gnus-group-real-name group) (nth 1 method) dont-check)))
10438
10439 (defun gnus-request-asynchronous (group &optional articles)
10440   (let ((method (gnus-find-method-for-group group)))
10441     (funcall (gnus-get-function method 'request-asynchronous) 
10442              (gnus-group-real-name group) (nth 1 method) articles)))
10443
10444 (defun gnus-list-active-group (group)
10445   (let ((method (gnus-find-method-for-group group))
10446         (func 'list-active-group))
10447     (and (gnus-check-backend-function func group)
10448          (funcall (gnus-get-function method func) 
10449                   (gnus-group-real-name group) (nth 1 method)))))
10450
10451 (defun gnus-request-group-description (group)
10452   (let ((method (gnus-find-method-for-group group))
10453         (func 'request-group-description))
10454     (and (gnus-check-backend-function func group)
10455          (funcall (gnus-get-function method func) 
10456                   (gnus-group-real-name group) (nth 1 method)))))
10457
10458 (defun gnus-close-group (group)
10459   (let ((method (gnus-find-method-for-group group)))
10460     (funcall (gnus-get-function method 'close-group) 
10461              (gnus-group-real-name group) (nth 1 method))))
10462
10463 (defun gnus-retrieve-headers (articles group)
10464   (let ((method (gnus-find-method-for-group group)))
10465     (if gnus-use-cache
10466         (gnus-cache-retrieve-headers articles group)
10467       (funcall (gnus-get-function method 'retrieve-headers) 
10468                articles (gnus-group-real-name group) (nth 1 method)))))
10469
10470 (defun gnus-retrieve-groups (groups method)
10471   (funcall (gnus-get-function method 'retrieve-groups) groups (nth 1 method)))
10472
10473 (defun gnus-request-article (article group &optional buffer)
10474   (let ((method (gnus-find-method-for-group group)))
10475     (funcall (gnus-get-function method 'request-article) 
10476              article (gnus-group-real-name group) (nth 1 method) buffer)))
10477
10478 (defun gnus-request-head (article group)
10479   (let ((method (gnus-find-method-for-group group)))
10480     (funcall (gnus-get-function method 'request-head) 
10481              article (gnus-group-real-name group) (nth 1 method))))
10482
10483 ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
10484 (defun gnus-request-post-buffer (post group subject header artbuf
10485                                       info follow-to respect-poster)
10486    (let* ((info (or info (and group (nth 2 (gnus-gethash 
10487                                             group gnus-newsrc-hashtb)))))
10488           (method
10489            (if (and gnus-post-method
10490                     ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
10491                     (memq 'post (assoc
10492                                  (format "%s" (car (gnus-find-method-for-group
10493                                                     gnus-newsgroup-name)))
10494                                         gnus-valid-select-methods)))
10495                gnus-post-method
10496              (gnus-find-method-for-group gnus-newsgroup-name))))
10497      (or (gnus-server-opened method)
10498          (gnus-open-server method)
10499          (error "Can't open server %s:%s" (car method) (nth 1 method)))
10500      (let ((mail-self-blind nil)
10501            (mail-archive-file-name nil))
10502        (funcall (gnus-get-function method 'request-post-buffer) 
10503                 post group subject header artbuf info follow-to
10504                 respect-poster))))
10505
10506 (defun gnus-request-post (method &optional force)
10507   (and (stringp method)
10508        (setq method (gnus-server-to-method method)))
10509   (and (not force) gnus-post-method
10510        (memq 'post (assoc (format "%s" (car method))
10511                           gnus-valid-select-methods))
10512        (setq method gnus-post-method))
10513   (funcall (gnus-get-function method 'request-post) 
10514            (nth 1 method)))
10515
10516 (defun gnus-request-expire-articles (articles group &optional force)
10517   (let ((method (gnus-find-method-for-group group)))
10518     (funcall (gnus-get-function method 'request-expire-articles) 
10519              articles (gnus-group-real-name group) (nth 1 method)
10520              force)))
10521
10522 (defun gnus-request-move-article 
10523   (article group server accept-function &optional last)
10524   (let ((method (gnus-find-method-for-group group)))
10525     (funcall (gnus-get-function method 'request-move-article) 
10526              article (gnus-group-real-name group) 
10527              (nth 1 method) accept-function last)))
10528
10529 (defun gnus-request-accept-article (group &optional last)
10530   (let ((func (if (symbolp group) group
10531                 (car (gnus-find-method-for-group group)))))
10532     (funcall (intern (format "%s-request-accept-article" func))
10533              (if (stringp group) (gnus-group-real-name group) group)
10534              last)))
10535
10536 (defun gnus-request-replace-article (article group buffer)
10537   (let ((func (car (gnus-find-method-for-group group))))
10538     (funcall (intern (format "%s-request-replace-article" func))
10539              article (gnus-group-real-name group) buffer)))
10540
10541 (defun gnus-request-create-group (group)
10542   (let ((method (gnus-find-method-for-group group)))
10543     (funcall (gnus-get-function method 'request-create-group) 
10544              (gnus-group-real-name group) (nth 1 method))))
10545
10546 (defun gnus-find-method-for-group (group &optional info)
10547   (or gnus-override-method
10548       (and (not group)
10549            gnus-select-method)
10550       (let ((info (or info (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
10551             method)
10552         (if (or (not info)
10553                 (not (setq method (nth 4 info))))
10554             (setq method gnus-select-method)
10555           (setq method
10556                 (cond ((stringp method)
10557                        (gnus-server-to-method method))
10558                       ((stringp (car method))
10559                        (gnus-server-extend-method group method))
10560                       (t
10561                        method))))
10562         (gnus-server-add-address method))))
10563
10564 (defun gnus-check-backend-function (func group)
10565   (let ((method (if (stringp group) (car (gnus-find-method-for-group group))
10566                  group)))
10567     (fboundp (intern (format "%s-%s" method func)))))
10568
10569 (defun gnus-methods-using (method)
10570   (let ((valids gnus-valid-select-methods)
10571         outs)
10572     (while valids
10573       (if (memq method (car valids)) 
10574           (setq outs (cons (car valids) outs)))
10575       (setq valids (cdr valids)))
10576     outs))
10577
10578 ;;; 
10579 ;;; Active & Newsrc File Handling
10580 ;;;
10581
10582 ;; Newsrc related functions.
10583 ;; Gnus internal format of gnus-newsrc-alist:
10584 ;; (("alt.general" 3 (1 . 1))
10585 ;;  ("alt.misc"    3 ((1 . 10) (12 . 15)))
10586 ;;  ("alt.test"    7 (1 . 99) (45 57 93)) ...)
10587 ;; The first item is the group name; the second is the subscription
10588 ;; level; the third is either a range of a list of ranges of read
10589 ;; articles, the optional fourth element is a list of marked articles,
10590 ;; the optional fifth element is the select method.
10591 ;;
10592 ;; Gnus internal format of gnus-newsrc-hashtb:
10593 ;; (95 ("alt.general" 3 (1 . 1)) ("alt.misc" 3 ((1 . 10) (12 . 15))) ...)
10594 ;; This is the entry for "alt.misc". The first element is the number
10595 ;; of unread articles in "alt.misc". The cdr of this entry is the
10596 ;; element *before* "alt.misc" in gnus-newsrc-alist, which makes is
10597 ;; trivial to remove or add new elements into gnus-newsrc-alist
10598 ;; without scanning the entire list. So, to get the actual information
10599 ;; of "alt.misc", you'd say something like 
10600 ;; (nth 2 (gnus-gethash "alt.misc" gnus-newsrc-hashtb))
10601 ;;
10602 ;; Gnus internal format of gnus-active-hashtb:
10603 ;; ((1 . 1))
10604 ;;  (5 . 10))
10605 ;;  (67 . 99)) ...)
10606 ;; The only element in each entry in this hash table is a range of
10607 ;; (possibly) available articles. (Articles in this range may have
10608 ;; been expired or cancelled.)
10609 ;;
10610 ;; Gnus internal format of gnus-killed-list and gnus-zombie-list:
10611 ;; ("alt.misc" "alt.test" "alt.general" ...)
10612
10613 (defun gnus-setup-news (&optional rawfile level)
10614   "Setup news information.
10615 If RAWFILE is non-nil, the .newsrc file will also be read.
10616 If LEVEL is non-nil, the news will be set up at level LEVEL."
10617   (let ((init (not (and gnus-newsrc-alist gnus-active-hashtb (not rawfile)))))
10618     ;; Clear some variables to re-initialize news information.
10619     (if init (setq gnus-newsrc-alist nil gnus-active-hashtb nil))
10620
10621     ;; Read the newsrc file and create `gnus-newsrc-hashtb'.
10622     (if init (gnus-read-newsrc-file rawfile))
10623
10624     ;; Read the active file and create `gnus-active-hashtb'.
10625     ;; If `gnus-read-active-file' is nil, then we just create an empty
10626     ;; hash table. The partial filling out of the hash table will be
10627     ;; done in `gnus-get-unread-articles'.
10628     (if (and gnus-read-active-file 
10629              (not level)
10630              (gnus-server-opened gnus-select-method))
10631         (gnus-read-active-file)
10632       (setq gnus-active-hashtb (make-vector 4095 0)))
10633
10634     (and init gnus-use-dribble-file (gnus-dribble-read-file))
10635
10636     ;; Find the number of unread articles in each non-dead group.
10637     (gnus-get-unread-articles (or level (1+ gnus-level-subscribed)))
10638     ;; Find new newsgroups and treat them.
10639     (if (and init gnus-check-new-newsgroups gnus-read-active-file (not level)
10640              (gnus-server-opened gnus-select-method))
10641         (gnus-find-new-newsgroups))
10642     (if (and init gnus-check-bogus-newsgroups 
10643              gnus-read-active-file (not level)
10644              (gnus-server-opened gnus-select-method))
10645         (gnus-check-bogus-newsgroups))))
10646
10647 (defun gnus-find-new-newsgroups ()
10648   "Search for new newsgroups and add them.
10649 Each new newsgroup will be treated with `gnus-subscribe-newsgroup-method.'
10650 The `-n' option line from .newsrc is respected."
10651   (interactive)
10652   (or (gnus-check-first-time-used)
10653       (if (or (consp gnus-check-new-newsgroups)
10654               (eq gnus-check-new-newsgroups 'ask-server))
10655           (gnus-ask-server-for-new-groups)
10656         (let ((groups 0)
10657               group new-newsgroups)
10658           (or gnus-have-read-active-file (gnus-read-active-file))
10659           (setq gnus-newsrc-last-checked-date (current-time-string))
10660           (if (not gnus-killed-hashtb) (gnus-make-hashtable-from-killed))
10661           ;; Go though every newsgroup in `gnus-active-hashtb' and compare
10662           ;; with `gnus-newsrc-hashtb' and `gnus-killed-hashtb'.
10663           (mapatoms
10664            (lambda (sym)
10665              (setq group (symbol-name sym))
10666              (if (or (gnus-gethash group gnus-killed-hashtb)
10667                      (gnus-gethash group gnus-newsrc-hashtb))
10668                  ()
10669                (let ((do-sub (gnus-matches-options-n group)))
10670                  (cond ((eq do-sub 'subscribe)
10671                         (setq groups (1+ groups))
10672                         (gnus-sethash group group gnus-killed-hashtb)
10673                         (funcall 
10674                          gnus-subscribe-options-newsgroup-method group))
10675                        ((eq do-sub 'ignore)
10676                         nil)
10677                        (t
10678                         (setq groups (1+ groups))
10679                         (gnus-sethash group group gnus-killed-hashtb)
10680                         (if gnus-subscribe-hierarchical-interactive
10681                             (setq new-newsgroups (cons group new-newsgroups))
10682                           (funcall gnus-subscribe-newsgroup-method group)))))))
10683            gnus-active-hashtb)
10684           (if new-newsgroups 
10685               (gnus-subscribe-hierarchical-interactive new-newsgroups))
10686           ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
10687           (if (> groups 0)
10688               (message "%d new newsgroup%s arrived." 
10689                        groups (if (> groups 1) "s have" " has")))))))
10690
10691 (defun gnus-matches-options-n (group)
10692   ;; Returns `subscribe' if the group is to be uncoditionally
10693   ;; subscribed, `ignore' if it is to be ignored, and nil if there is
10694   ;; no match for the group.
10695
10696   ;; First we check the two user variables.
10697   (cond
10698    ((and gnus-options-not-subscribe
10699          (string-match gnus-options-not-subscribe group))
10700     'subscribe)
10701    ((and gnus-options-subscribe
10702          (string-match gnus-options-subscribe group))
10703     'ignore)
10704    ;; Then we go through the list that was retrieved from the .newsrc
10705    ;; file.  This list has elements on the form 
10706    ;; `(REGEXP . {ignore,subscribe})'. The first match found (the list
10707    ;; is in the reverse order of the options line) is returned.
10708    (t
10709     (let ((regs gnus-newsrc-options-n))
10710       (while (and regs
10711                   (not (string-match (car (car gnus-newsrc-options-n)) group)))
10712         (setq regs (cdr regs)))
10713       (and regs (cdr (car regs)))))))
10714
10715 (defun gnus-ask-server-for-new-groups ()
10716   (let* ((date (or gnus-newsrc-last-checked-date (current-time-string)))
10717          (methods (cons gnus-select-method 
10718                         (append
10719                          (and (consp gnus-check-new-newsgroups)
10720                               gnus-check-new-newsgroups)
10721                          gnus-secondary-select-methods)))
10722          (groups 0)
10723          (new-date (current-time-string))
10724          hashtb group new-newsgroups got-new)
10725     ;; Go thorugh both primary and secondary select methods and
10726     ;; request new newsgroups.  
10727     (while methods
10728       (if (gnus-request-newgroups date (car methods))
10729           (save-excursion
10730             (setq got-new t)
10731             (or hashtb (setq hashtb (gnus-make-hashtable 
10732                                      (count-lines (point-min) (point-max)))))
10733             (set-buffer nntp-server-buffer)
10734             ;; Enter all the new groups in a hashtable.
10735             (gnus-active-to-gnus-format (car methods) hashtb)))
10736       (setq methods (cdr methods)))
10737     (and got-new (setq gnus-newsrc-last-checked-date new-date))
10738     ;; Now all new groups from all select methods are in `hashtb'.
10739     (mapatoms
10740      (lambda (group-sym)
10741        (setq group (symbol-name group-sym))
10742        (if (or (gnus-gethash group gnus-newsrc-hashtb)
10743                (member group gnus-zombie-list)
10744                (member group gnus-killed-list))
10745            ;; The group is already known.
10746            ()
10747          (gnus-sethash group (symbol-value group-sym) gnus-active-hashtb)
10748          (let ((do-sub (gnus-matches-options-n group)))
10749            (cond ((eq do-sub 'subscribe)
10750                   (setq groups (1+ groups))
10751                   (gnus-sethash group group gnus-killed-hashtb)
10752                   (funcall 
10753                    gnus-subscribe-options-newsgroup-method group))
10754                  ((eq do-sub 'ignore)
10755                   nil)
10756                  (t
10757                   (setq groups (1+ groups))
10758                   (gnus-sethash group group gnus-killed-hashtb)
10759                   (if gnus-subscribe-hierarchical-interactive
10760                       (setq new-newsgroups (cons group new-newsgroups))
10761                     (funcall gnus-subscribe-newsgroup-method group)))))))
10762      hashtb)
10763     (if new-newsgroups 
10764         (gnus-subscribe-hierarchical-interactive new-newsgroups))
10765     ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
10766     (if (> groups 0)
10767         (message "%d new newsgroup%s arrived." 
10768                  groups (if (> groups 1) "s have" " has")))
10769     got-new))
10770
10771 (defun gnus-check-first-time-used ()
10772   (if (or (> (length gnus-newsrc-alist) 1)
10773           (file-exists-p gnus-startup-file)
10774           (file-exists-p (concat gnus-startup-file ".el"))
10775           (file-exists-p (concat gnus-startup-file ".eld")))
10776       nil
10777     (message "First time user; subscribing you to default groups")
10778     (or gnus-have-read-active-file (gnus-read-active-file))
10779     (setq gnus-newsrc-last-checked-date (current-time-string))
10780     (let ((groups gnus-default-subscribed-newsgroups)
10781           group)
10782       (if (eq groups t)
10783           nil
10784         (setq groups (or groups gnus-backup-default-subscribed-newsgroups))
10785         (mapatoms
10786          (lambda (sym)
10787            (setq group (symbol-name sym))
10788            (let ((do-sub (gnus-matches-options-n group)))
10789              (cond ((eq do-sub 'subscribe)
10790                     (gnus-sethash group group gnus-killed-hashtb)
10791                     (funcall 
10792                      gnus-subscribe-options-newsgroup-method group))
10793                    ((eq do-sub 'ignore)
10794                     nil)
10795                    (t
10796                     (setq gnus-killed-list (cons group gnus-killed-list))))))
10797          gnus-active-hashtb)
10798         (while groups
10799           (if (gnus-gethash (car groups) gnus-active-hashtb)
10800               (gnus-group-change-level 
10801                (car groups) gnus-level-default-subscribed gnus-level-killed))
10802           (setq groups (cdr groups)))
10803         (gnus-group-make-help-group)
10804         (and gnus-novice-user
10805              (message "`G k' to list killed groups"))))))
10806
10807 ;; `gnus-group-change-level' is the fundamental function for changing
10808 ;; subscription levels of newsgroups. This might mean just changing
10809 ;; from level 1 to 2, which is pretty trivial, from 2 to 6 or back
10810 ;; again, which subscribes/unsubscribes a group, which is equally
10811 ;; trivial. Changing from 1-7 to 8-9 means that you kill a group, and
10812 ;; from 8-9 to 1-7 means that you remove the group from the list of
10813 ;; killed (or zombie) groups and add them to the (kinda) subscribed
10814 ;; groups. And last but not least, moving from 8 to 9 and 9 to 8,
10815 ;; which is trivial.
10816 ;; ENTRY can either be a string (newsgroup name) or a list (if
10817 ;; FROMKILLED is t, it's a list on the format (NUM INFO-LIST),
10818 ;; otherwise it's a list in the format of the `gnus-newsrc-hashtb'
10819 ;; entries. 
10820 ;; LEVEL is the new level of the group, OLDLEVEL is the old level and
10821 ;; PREVIOUS is the group (in hashtb entry format) to insert this group
10822 ;; after. 
10823 (defun gnus-group-change-level (entry level &optional oldlevel
10824                                       previous fromkilled)
10825   (let ((pinfo entry)
10826         group info active num)
10827     ;; Glean what info we can from the arguments
10828     (if (consp entry)
10829         (if fromkilled (setq group (nth 1 entry))
10830           (setq group (car (nth 2 entry))))
10831       (setq group entry))
10832     (if (and (stringp entry)
10833              oldlevel 
10834              (< oldlevel gnus-level-zombie))
10835         (setq entry (gnus-gethash entry gnus-newsrc-hashtb)))
10836     (if (and (not oldlevel)
10837              (listp entry))
10838         (setq oldlevel (car (cdr (nth 2 entry)))))
10839     (if (stringp previous)
10840         (setq previous (gnus-gethash previous gnus-newsrc-hashtb)))
10841
10842     (gnus-dribble-enter
10843      (format "(gnus-group-change-level %S %S %S %S %S)" 
10844              group level oldlevel (car (nth 2 previous)) fromkilled))
10845     
10846     ;; Then we remove the newgroup from any old structures, if needed.
10847     ;; If the group was killed, we remove it from the killed or zombie
10848     ;; list. If not, and it is in fact going to be killed, we remove
10849     ;; it from the newsrc hash table and assoc.
10850     (cond ((>= oldlevel gnus-level-zombie)
10851            (if (= oldlevel gnus-level-zombie)
10852                (setq gnus-zombie-list (delete group gnus-zombie-list))
10853              (setq gnus-killed-list (delete group gnus-killed-list))))
10854           (t
10855            (if (>= level gnus-level-zombie)
10856                (progn
10857                  (gnus-sethash (car (nth 2 entry))
10858                                nil gnus-newsrc-hashtb)
10859                  (if (nth 3 entry)
10860                      (setcdr (gnus-gethash (car (nth 3 entry))
10861                                            gnus-newsrc-hashtb)
10862                              (cdr entry)))
10863                  (setcdr (cdr entry) (cdr (cdr (cdr entry))))))))
10864
10865     ;; Finally we enter (if needed) the list where it is supposed to
10866     ;; go, and change the subscription level. If it is to be killed,
10867     ;; we enter it into the killed or zombie list.
10868     (cond ((>= level gnus-level-zombie)
10869            (and (string= group (gnus-group-real-name group))
10870                 (if (= level gnus-level-zombie)
10871                     (setq gnus-zombie-list (cons group gnus-zombie-list))
10872                   (setq gnus-killed-list (cons group gnus-killed-list)))))
10873           (t
10874            ;; If the list is to be entered into the newsrc assoc, and
10875            ;; it was killed, we have to create an entry in the newsrc
10876            ;; hashtb format and fix the pointers in the newsrc assoc.
10877            (if (>= oldlevel gnus-level-zombie)
10878                (progn
10879                  (if (listp entry)
10880                      (progn
10881                        (setq info (cdr entry))
10882                        (setq num (car entry)))
10883                    (setq active (gnus-gethash group gnus-active-hashtb))
10884                    (setq num (if active (- (1+ (cdr active)) (car active)) t))
10885                    ;; Check whether the group is foreign. If so, the
10886                    ;; foreign select method has to be entered into the
10887                    ;; info. 
10888                    (let ((method (gnus-group-method-name group)))
10889                      (if (eq method gnus-select-method)
10890                          (setq info (list group level nil))
10891                        (setq info (list group level nil nil method)))))
10892                  (setq entry (cons info (if previous (cdr (cdr previous))
10893                                           (cdr gnus-newsrc-alist))))
10894                  (setcdr (if previous (cdr previous) gnus-newsrc-alist)
10895                          entry)
10896                  (gnus-sethash group (cons num (if previous (cdr previous)
10897                                                  gnus-newsrc-alist))
10898                                gnus-newsrc-hashtb)
10899                  (if (cdr entry)
10900                      (setcdr (gnus-gethash (car (car (cdr entry)))
10901                                            gnus-newsrc-hashtb)
10902                              entry)))
10903              ;; It was alive, and it is going to stay alive, so we
10904              ;; just change the level and don't change any pointers or
10905              ;; hash table entries.
10906              (setcar (cdr (car (cdr (cdr entry)))) level))))))
10907
10908 (defun gnus-kill-newsgroup (newsgroup)
10909   "Obsolete function. Kills a newsgroup."
10910   (gnus-group-change-level
10911    (gnus-gethash newsgroup gnus-newsrc-hashtb) gnus-level-killed))
10912
10913 (defun gnus-check-bogus-newsgroups (&optional confirm)
10914   "Remove bogus newsgroups.
10915 If CONFIRM is non-nil, the user has to confirm the deletion of every
10916 newsgroup." 
10917   (let ((newsrc (cdr gnus-newsrc-alist))
10918         bogus group)
10919     (message "Checking bogus newsgroups...")
10920     (or gnus-have-read-active-file (gnus-read-active-file))
10921     ;; Find all bogus newsgroup that are subscribed.
10922     (while newsrc
10923       (setq group (car (car newsrc)))
10924       (if (or (gnus-gethash group gnus-active-hashtb)
10925               (nth 4 (car newsrc))
10926               (and confirm
10927                    (not (gnus-y-or-n-p
10928                          (format "Remove bogus newsgroup: %s " group)))))
10929           ;; Active newsgroup.
10930           ()
10931         ;; Found a bogus newsgroup.
10932         (setq bogus (cons group bogus)))
10933       (setq newsrc (cdr newsrc)))
10934     ;; Remove all bogus subscribed groups by first killing them, and
10935     ;; then removing them from the list of killed groups.
10936     (while bogus
10937       (gnus-group-change-level 
10938        (gnus-gethash (car bogus) gnus-newsrc-hashtb) gnus-level-killed)
10939       (setq gnus-killed-list (delete (car bogus) gnus-killed-list))
10940       (setq bogus (cdr bogus)))
10941     ;; Then we remove all bogus groups from the list of killed and
10942     ;; zombie groups. They are are removed without confirmation.
10943     (let ((dead-lists '(gnus-killed-list gnus-zombie-list))
10944           killed)
10945       (while dead-lists
10946         (setq killed (symbol-value (car dead-lists)))
10947         (while killed
10948           (setq group (car killed))
10949           (or (gnus-gethash group gnus-active-hashtb)
10950               ;; The group is bogus.
10951               (set (car dead-lists)
10952                    (delete group (symbol-value (car dead-lists)))))
10953           (setq killed (cdr killed)))
10954         (setq dead-lists (cdr dead-lists))))
10955     (message "Checking bogus newsgroups... done")))
10956
10957 (defun gnus-check-duplicate-killed-groups ()
10958   "Remove duplicates from the list of killed groups."
10959   (interactive)
10960   (let ((killed gnus-killed-list))
10961     (while killed
10962       (message "%d" (length killed))
10963       (setcdr killed (delete (car killed) (cdr killed)))
10964       (setq killed (cdr killed)))))
10965
10966 ;; Go though `gnus-newsrc-alist' and compare with `gnus-active-hashtb'
10967 ;; and compute how many unread articles there are in each group.
10968 (defun gnus-get-unread-articles (&optional level)
10969   (let ((newsrc (cdr gnus-newsrc-alist))
10970         (level (or level (1+ gnus-level-subscribed)))
10971         info group active virtuals method)
10972     (message "Checking new news...")
10973     (while newsrc
10974       (setq info (car newsrc))
10975       (setq group (car info))
10976       (setq active (gnus-gethash group gnus-active-hashtb))
10977
10978       ;; Check newsgroups. If the user doesn't want to check them, or
10979       ;; they can't be checked (for instance, if the news server can't
10980       ;; be reached) we just set the number of unread articles in this
10981       ;; newsgroup to t. This means that Gnus thinks that there are
10982       ;; unread articles, but it has no idea how many.
10983       (if (setq method (nth 4 info))
10984           (if (or (and gnus-activate-foreign-newsgroups 
10985                        (not (numberp gnus-activate-foreign-newsgroups)))
10986                   (and (numberp gnus-activate-foreign-newsgroups)
10987                        (<= (nth 1 info) gnus-activate-foreign-newsgroups)
10988                        (<= (nth 1 info) level)))
10989               (if (eq (car (if (stringp method) 
10990                                (gnus-server-to-method method)
10991                              (nth 4 info))) 'nnvirtual)
10992                   (setq virtuals (cons info virtuals))
10993                 (setq active (gnus-activate-newsgroup (car info)))))
10994         (if (and (not gnus-read-active-file)
10995                  (<= (nth 1 info) level))
10996             (progn
10997               (setq active (gnus-activate-newsgroup (car info))))))
10998       
10999       (or active (progn (gnus-sethash group nil gnus-active-hashtb)
11000                         (setcar (gnus-gethash group gnus-newsrc-hashtb) t)))
11001       (and active 
11002            (gnus-get-unread-articles-in-group info active)
11003            ;; Close the groups as we look at them!
11004            (gnus-close-group group))
11005       (setq newsrc (cdr newsrc)))
11006
11007     ;; Activate the virtual groups. This has to be done after all the
11008     ;; other groups. 
11009     ;; !!! If one virtual group contains another virtual group, even
11010     ;; doing it this way might cause problems.
11011    (while virtuals
11012       (and (setq active (gnus-activate-newsgroup (car (car virtuals))))
11013            (gnus-get-unread-articles-in-group (car virtuals) active))
11014       (setq virtuals (cdr virtuals)))
11015
11016     (message "Checking new news... done")))
11017
11018 ;; Create a hash table out of the newsrc alist. The `car's of the
11019 ;; alist elements are used as keys.
11020 (defun gnus-make-hashtable-from-newsrc-alist ()
11021   (let ((alist gnus-newsrc-alist)
11022          prev)
11023     (setq gnus-newsrc-hashtb (gnus-make-hashtable (length alist)))
11024     (setq alist 
11025           (setq prev (setq gnus-newsrc-alist 
11026                            (cons (list "dummy.group" 0 nil) alist))))
11027     (while alist
11028       (gnus-sethash (car (car alist)) (cons nil prev) gnus-newsrc-hashtb)
11029       (setq prev alist)
11030       (setq alist (cdr alist)))))
11031
11032 (defun gnus-make-hashtable-from-killed ()
11033   "Create a hash table from the killed and zombie lists."
11034   (let ((lists '(gnus-killed-list gnus-zombie-list))
11035         list)
11036     (setq gnus-killed-hashtb 
11037           (gnus-make-hashtable 
11038            (+ (length gnus-killed-list) (length gnus-zombie-list))))
11039     (while lists
11040       (setq list (symbol-value (car lists)))
11041       (setq lists (cdr lists))
11042       (while list
11043         (gnus-sethash (car list) (car list) gnus-killed-hashtb)
11044         (setq list (cdr list))))))
11045
11046 (defun gnus-get-unread-articles-in-group (info active)
11047   (let* ((range (nth 2 info))
11048          (num 0)
11049          (marked (nth 3 info))
11050          srange lowest group highest)
11051     ;; If a cache is present, we may have to alter the active info.
11052     (and gnus-use-cache
11053          (gnus-cache-possibly-alter-active (car info) active))
11054     ;; Modify the list of read articles according to what articles 
11055     ;; are available; then tally the unread articles and add the
11056     ;; number to the group hash table entry.
11057     (cond ((zerop (cdr active))
11058            (setq num 0))
11059           ((not range)
11060            (setq num (- (1+ (cdr active)) (car active))))
11061           ((not (listp (cdr range)))
11062            ;; Fix a single (num . num) range according to the
11063            ;; active hash table.
11064            ;; Fix by Carsten Bormann <cabo@Informatik.Uni-Bremen.DE>.
11065            (and (< (cdr range) (car active)) (setcdr range (1- (car active))))
11066            (and (> (cdr range) (cdr active)) (setcdr range (cdr active)))
11067            ;; Compute number of unread articles.
11068            (setq num (max 0 (- (cdr active) 
11069                                (- (1+ (cdr range)) (car range))))))
11070           (t
11071            ;; The read list is a list of ranges. Fix them according to
11072            ;; the active hash table.
11073            ;; First peel off any elements that are below the lower
11074            ;; active limit. 
11075            (while (and (cdr range) 
11076                        (>= (car active) 
11077                            (or (and (atom (car (cdr range))) (car (cdr range)))
11078                                (car (car (cdr range))))))
11079              (if (numberp (car range))
11080                  (setcar range 
11081                          (cons (car range) 
11082                                (or (and (numberp (car (cdr range)))
11083                                         (car (cdr range))) 
11084                                    (cdr (car (cdr range))))))
11085                (setcdr (car range) 
11086                        (or (and (numberp (nth 1 range)) (nth 1 range))
11087                            (cdr (car (cdr range))))))
11088              (setcdr range (cdr (cdr range))))
11089            ;; Adjust the first element to be the same as the lower limit. 
11090            (if (and (not (atom (car range))) 
11091                     (< (cdr (car range)) (car active)))
11092                (setcdr (car range) (1- (car active))))
11093            ;; Then we want to peel off any elements that are higher
11094            ;; than the upper active limit.  
11095            (let ((srange range))
11096              ;; Go past all legal elements.
11097              (while (and (cdr srange) 
11098                          (<= (or (and (atom (car (cdr srange)))
11099                                       (car (cdr srange)))
11100                                  (car (car (cdr srange)))) (cdr active)))
11101                (setq srange (cdr srange)))
11102              (if (cdr srange)
11103                  ;; Nuke all remaining illegal elements.
11104                  (setcdr srange nil))
11105
11106              ;; Adjust the final element.
11107              (if (and (not (atom (car srange)))
11108                       (> (cdr (car srange)) (cdr active)))
11109                  (setcdr (car srange) (cdr active))))
11110            ;; Compute the number of unread articles.
11111            (while range
11112              (setq num (+ num (- (1+ (or (and (atom (car range)) (car range))
11113                                          (cdr (car range))))
11114                                  (or (and (atom (car range)) (car range))
11115                                      (car (car range))))))
11116              (setq range (cdr range)))
11117            (setq num (max 0 (- (cdr active) num)))))
11118     (and info
11119          (progn
11120            (and (assq 'tick marked)
11121                 (inline (gnus-remove-illegal-marked-articles
11122                          (assq 'tick marked) (nth 2 info))))
11123            (and (assq 'dormant marked)
11124                 (inline (gnus-remove-illegal-marked-articles
11125                          (assq 'dormant marked) (nth 2 info))))
11126            (setcar
11127             (gnus-gethash (car info) gnus-newsrc-hashtb) 
11128             (setq num (max 0 (- num (length (cdr (assq 'tick marked)))
11129                                 (length (cdr (assq 'dormant marked)))))))))
11130     num))
11131
11132 (defun gnus-remove-illegal-marked-articles (marked ranges)
11133   (let ((m (cdr marked)))
11134     ;; Make sure that all ticked articles are a subset of the unread
11135     ;; articles. 
11136     (while m
11137       (if (gnus-member-of-range (car m) ranges)
11138           (setcdr marked (cdr m))
11139         (setq marked m))
11140       (setq m (cdr m)))))
11141
11142 (defun gnus-activate-newsgroup (group)
11143   (let ((method (gnus-find-method-for-group group))
11144         active)
11145     (and (or (gnus-server-opened method) (gnus-open-server method))
11146          (gnus-request-group group)
11147          (save-excursion
11148            (set-buffer nntp-server-buffer)
11149            (goto-char 1)
11150            (and (looking-at "[0-9]+ [0-9]+ \\([0-9]+\\) [0-9]+")
11151                 (progn
11152                   (goto-char (match-beginning 1))
11153                   (gnus-sethash 
11154                    group (setq active (cons (read (current-buffer))
11155                                             (read (current-buffer))))
11156                    gnus-active-hashtb))
11157                 active)))))
11158
11159 (defun gnus-update-read-articles 
11160   (group unread unselected ticked &optional domarks replied expirable killed
11161          dormant bookmark score)
11162   "Update the list of read and ticked articles in GROUP using the
11163 UNREAD and TICKED lists.
11164 Note: UNSELECTED has to be sorted over `<'.
11165 Returns whether the updating was successful."
11166   (let* ((active (or gnus-newsgroup-active 
11167                      (gnus-gethash group gnus-active-hashtb)))
11168          (entry (gnus-gethash group gnus-newsrc-hashtb))
11169          (number (car entry))
11170          (info (nth 2 entry))
11171          (marked (nth 3 info))
11172          (prev 1)
11173          (unread (sort (copy-sequence unread) (function <)))
11174          last read)
11175     (if (or (not info) (not active))
11176         ;; There is no info on this group if it was, in fact,
11177         ;; killed. Gnus stores no information on killed groups, so
11178         ;; there's nothing to be done. 
11179         ;; One could store the information somewhere temporarily,
11180         ;; perhaps... Hmmm... 
11181         ()
11182       ;; Remove any negative articles numbers.
11183       (while (and unread (< (car unread) 0))
11184         (setq unread (cdr unread)))
11185       (setq unread (sort (append unselected unread) '<))
11186       ;; Set the number of unread articles in gnus-newsrc-hashtb.
11187       (setcar entry (max 0 (- (length unread) (length ticked) 
11188                               (length dormant))))
11189       ;; Compute the ranges of read articles by looking at the list of
11190       ;; unread articles.  
11191       (while unread
11192         (if (/= (car unread) prev)
11193             (setq read (cons (if (= prev (1- (car unread))) prev
11194                                (cons prev (1- (car unread)))) read)))
11195         (setq prev (1+ (car unread)))
11196         (setq unread (cdr unread)))
11197       (if (<= prev (cdr active))
11198           (setq read (cons (cons prev (cdr active)) read)))
11199       ;; Enter this list into the group info.
11200       (setcar (cdr (cdr info)) 
11201               (if (> (length read) 1) (nreverse read) read))
11202       ;; Enter the list of ticked articles.
11203       (gnus-set-marked-articles 
11204        info ticked
11205        (if domarks replied (cdr (assq 'reply marked)))
11206        (if domarks expirable (cdr (assq 'expire marked)))
11207        (if domarks killed (cdr (assq 'killed marked)))
11208        (if domarks dormant (cdr (assq 'dormant marked)))
11209        (if domarks bookmark (cdr (assq 'bookmark marked)))
11210        (if domarks score (cdr (assq 'score marked))))
11211       t)))
11212
11213 (defun gnus-make-articles-unread (group articles)
11214   "Mark ARTICLES in GROUP as unread."
11215   (let ((info (nth 2 (or (gnus-gethash group gnus-newsrc-hashtb)
11216                          (gnus-gethash (gnus-group-real-name group)
11217                                        gnus-newsrc-hashtb)))))
11218     (setcar (nthcdr 2 info)
11219             (gnus-remove-from-range (nth 2 info) articles))
11220     (gnus-group-update-group group t)))
11221
11222 (defun gnus-read-active-file ()
11223   "Get active file from NNTP server."
11224   (gnus-group-set-mode-line)
11225   (let ((methods (cons gnus-select-method gnus-secondary-select-methods))
11226         list-type)
11227     (setq gnus-have-read-active-file nil)
11228     (save-excursion
11229       (set-buffer nntp-server-buffer)
11230       (while methods
11231         (let* ((where (nth 1 (car methods)))
11232                (mesg (format "Reading active file%s via %s..."
11233                              (if (and where (not (zerop (length where))))
11234                                  (concat " from " where) "")
11235                              (car (car methods)))))
11236           (message mesg)
11237           (gnus-check-news-server (car methods))
11238           (cond 
11239            ((and (eq gnus-read-active-file 'some)
11240                  (gnus-check-backend-function
11241                   'retrieve-groups (car (car methods))))
11242             (let ((newsrc (cdr gnus-newsrc-alist))
11243                   groups)
11244               (while newsrc
11245                 (and (gnus-server-equal 
11246                       (gnus-find-method-for-group
11247                        (car (car newsrc)) (car newsrc))
11248                       (gnus-server-get-method nil (car methods)))
11249                      (setq groups (cons (car (car newsrc)) groups)))
11250                 (setq newsrc (cdr newsrc)))
11251               (setq list-type (gnus-retrieve-groups groups (car methods)))
11252               (cond ((not list-type)
11253                      (message "Cannot read partial active file from %s server." 
11254                               (car (car methods)))
11255                      (ding)
11256                      (sit-for 2))
11257                     ((eq list-type 'active)
11258                      (gnus-active-to-gnus-format 
11259                       (and gnus-have-read-active-file (car methods)))
11260                      (setq gnus-have-read-active-file t))
11261                     (t
11262                      (gnus-groups-to-gnus-format
11263                       (and gnus-have-read-active-file (car methods)))
11264                      (setq gnus-have-read-active-file t)))))
11265            (t
11266             (if (not (gnus-request-list (car methods)))
11267                 (progn
11268                   (message "Cannot read active file from %s server." 
11269                            (car (car methods)))
11270                   (ding))
11271               (gnus-active-to-gnus-format 
11272                (and gnus-have-read-active-file (car methods)))
11273               (setq gnus-have-read-active-file t)
11274               (message "%s...done" mesg)))))
11275         (setq methods (cdr methods))))))
11276
11277 ;; rewritten by jwz based on ideas from Rick Sladkey <jrs@world.std.com>
11278 ;; Further rewrites by lmi.
11279 (defun gnus-active-to-gnus-format (method &optional hashtb)
11280   "Convert active file format to internal format.
11281 Lines matching `gnus-ignored-newsgroups' are ignored."
11282   (let ((cur (current-buffer))
11283         (hashtb (or hashtb 
11284                     (if method
11285                         gnus-active-hashtb
11286                       (setq gnus-active-hashtb
11287                             (gnus-make-hashtable 
11288                              (count-lines (point-min) (point-max))))))))
11289     ;; Delete unnecessary lines.
11290     (goto-char (point-min))
11291     (delete-matching-lines gnus-ignored-newsgroups)
11292     (and method (not (eq method gnus-select-method))
11293          (let ((prefix (gnus-group-prefixed-name "" method)))
11294            (goto-char (point-min))
11295            (while (and (not (eobp))
11296                        (null (insert prefix))
11297                        (zerop (forward-line 1))))))
11298     (goto-char (point-min))
11299     ;; Store active file in hashtable.
11300     (save-restriction
11301       (if (or (re-search-forward "\n.\r?$" nil t)
11302               (goto-char (point-max)))
11303           (progn
11304             (beginning-of-line)
11305             (narrow-to-region (point-min) (point))))
11306       (goto-char (point-min))
11307       (if (string-match "%[oO]" gnus-group-line-format)
11308           ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
11309           ;; If we want information on moderated groups, we use this
11310           ;; loop...   
11311           (condition-case ()
11312               (let ((mod-hashtb (make-vector 7 0))
11313                     group max mod)
11314                 (while (not (eobp))
11315                   (setq group (let ((obarray hashtb))
11316                                 (read cur)))
11317                   (setq max (read cur))
11318                   (set group (cons (read cur) max))
11319                   ;; Enter moderated groups into a list.
11320                   (if (string= 
11321                        (symbol-name  (let ((obarray mod-hashtb)) (read cur)))
11322                        "m")
11323                       (setq gnus-moderated-list 
11324                             (cons (symbol-name group) gnus-moderated-list)))
11325                   (forward-line 1)))
11326             (error 
11327              (progn (ding) (message "Possible error in active file."))))
11328         ;; And if we do not care about moderation, we use this loop,
11329         ;; which is faster.
11330         (condition-case ()
11331             (let (group max)
11332               (while (not (eobp))
11333                 ;; group gets set to a symbol interned in the hash table
11334                 ;; (what a hack!!)
11335                 (setq group (let ((obarray hashtb)) (read cur)))
11336                 (setq max (read cur))
11337                 (set group (cons (read cur) max))
11338                 (forward-line 1)))
11339           (error 
11340            (progn (ding) (message "Possible error in active file."))))))))
11341
11342 (defun gnus-groups-to-gnus-format (method &optional hashtb)
11343   ;; Parse a "groups" active file.
11344   (let ((cur (current-buffer))
11345         (hashtb (or hashtb 
11346                     (if method
11347                         gnus-active-hashtb
11348                       (setq gnus-active-hashtb
11349                             (gnus-make-hashtable 
11350                              (count-lines (point-min) (point-max)))))))
11351         (prefix (and method (not (eq method gnus-select-method))
11352                      (gnus-group-prefixed-name "" method))))
11353
11354     (goto-char (point-min))
11355     (condition-case ()
11356         ;; We split this into to separate loops, one with the prefix
11357         ;; and one without to speed the reading up somewhat.
11358         (if prefix
11359             (let (min max opoint)
11360               (while (not (eobp))
11361                 (read cur) (read cur)
11362                 (setq min (read cur)
11363                       max (read cur)
11364                       opoint (point))
11365                 (skip-chars-forward " \t")
11366                 (insert prefix)
11367                 (goto-char opoint)
11368                 (set (let ((obarray hashtb)) (read cur)) 
11369                      (cons min max))
11370                 (forward-line 1)))
11371           (let (min max opoint)
11372             (while (not (eobp))
11373               (if (= (following-char) ?2)
11374                   (progn
11375                     (read cur) (read cur)
11376                     (setq min (read cur)
11377                           max (read cur))
11378                     (set (let ((obarray hashtb)) (read cur)) 
11379                          (cons min max))))
11380               (forward-line 1))))
11381       (error 
11382        (progn (ding) (message "Possible error in active file."))))))
11383
11384 (defun gnus-read-newsrc-file (&optional force)
11385   "Read startup file.
11386 If FORCE is non-nil, the .newsrc file is read."
11387   (setq gnus-current-startup-file (gnus-make-newsrc-file gnus-startup-file))
11388   ;; Reset variables that might be defined in the .newsrc.eld file.
11389   (let ((variables gnus-variable-list))
11390     (while variables
11391       (set (car variables) nil)
11392       (setq variables (cdr variables))))
11393   (let* ((newsrc-file gnus-current-startup-file)
11394          (quick-file (concat newsrc-file ".el")))
11395     (save-excursion
11396       ;; We always load the .newsrc.eld file. If always contains
11397       ;; much information that can not be gotten from the .newsrc
11398       ;; file (ticked articles, killed groups, foreign methods, etc.)
11399       (gnus-read-newsrc-el-file quick-file)
11400  
11401       (if (or force
11402               (and (file-newer-than-file-p newsrc-file quick-file)
11403                    (file-newer-than-file-p newsrc-file 
11404                                            (concat quick-file "d")))
11405               (not gnus-newsrc-alist))
11406           ;; We read the .newsrc file. Note that if there if a
11407           ;; .newsrc.eld file exists, it has already been read, and
11408           ;; the `gnus-newsrc-hashtb' has been created. While reading
11409           ;; the .newsrc file, Gnus will only use the information it
11410           ;; can find there for changing the data already read -
11411           ;; ie. reading the .newsrc file will not trash the data
11412           ;; already read (except for read articles).
11413           (save-excursion
11414             (message "Reading %s..." newsrc-file)
11415             (set-buffer (find-file-noselect newsrc-file))
11416             (buffer-disable-undo (current-buffer))
11417             (gnus-newsrc-to-gnus-format)
11418             (kill-buffer (current-buffer))
11419             (message "Reading %s... done" newsrc-file))))))
11420
11421 (defun gnus-read-newsrc-el-file (file)
11422   (let ((ding-file (concat file "d")))
11423     ;; We always, always read the .eld file.
11424     (message "Reading %s..." ding-file)
11425     (let (gnus-newsrc-assoc)
11426       (condition-case nil
11427           (load ding-file t t t)
11428         (error nil))
11429       (and gnus-newsrc-assoc (setq gnus-newsrc-alist gnus-newsrc-assoc)))
11430     (let ((inhibit-quit t))
11431       (gnus-uncompress-newsrc-assoc))
11432     (gnus-make-hashtable-from-newsrc-alist)
11433     (if (not (file-newer-than-file-p file ding-file))
11434         ()
11435       ;; Old format quick file
11436       (message "Reading %s..." file)
11437       ;; The .el file is newer than the .eld file, so we read that one
11438       ;; as well. 
11439       (gnus-read-old-newsrc-el-file file))))
11440
11441 ;; Parse the old-style quick startup file
11442 (defun gnus-read-old-newsrc-el-file (file)
11443   (let (newsrc killed marked group g m len info)
11444     (prog1
11445         (let ((gnus-killed-assoc nil)
11446               gnus-marked-assoc gnus-newsrc-alist gnus-newsrc-assoc)
11447           (prog1
11448               (condition-case nil
11449                   (load file t t t)
11450                 (error nil))
11451             (setq newsrc gnus-newsrc-assoc
11452                   killed gnus-killed-assoc
11453                   marked gnus-marked-assoc)))
11454       (setq gnus-newsrc-alist nil)
11455       (while newsrc
11456         (setq group (car newsrc))
11457         (let ((info (nth 2 (gnus-gethash (car group) gnus-newsrc-hashtb))))
11458           (if info
11459               (progn
11460                 (setcar (nthcdr 2 info) (cdr (cdr group)))
11461                 (setcar (cdr info)
11462                         (if (nth 1 group) gnus-level-default-subscribed 
11463                           gnus-level-default-unsubscribed))
11464                 (setq gnus-newsrc-alist (cons info gnus-newsrc-alist)))
11465             (setq gnus-newsrc-alist
11466                   (cons 
11467                    (setq info
11468                          (list (car group)
11469                                (if (nth 1 group) gnus-level-default-subscribed
11470                                  gnus-level-default-unsubscribed) 
11471                                (cdr (cdr group))))
11472                    gnus-newsrc-alist)))
11473           (if (setq m (assoc (car group) marked))
11474             (setcdr (cdr (cdr info)) (cons (list (cons 'tick (cdr m))) nil))))
11475         (setq newsrc (cdr newsrc)))
11476       (setq newsrc killed)
11477       (while newsrc
11478         (setcar newsrc (car (car newsrc)))
11479         (setq newsrc (cdr newsrc)))
11480       (setq gnus-killed-list killed))
11481     (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist))
11482     (gnus-make-hashtable-from-newsrc-alist)))
11483       
11484 (defun gnus-make-newsrc-file (file)
11485   "Make server dependent file name by catenating FILE and server host name."
11486   (let* ((file (expand-file-name file nil))
11487          (real-file (concat file "-" (nth 1 gnus-select-method))))
11488     (if (file-exists-p real-file)
11489         real-file file)))
11490
11491 (defun gnus-uncompress-newsrc-assoc ()
11492   ;; Uncompress all lists of marked articles in the newsrc assoc.
11493   (let ((newsrc gnus-newsrc-alist)
11494         marked)
11495     (while newsrc
11496       (if (not (setq marked (nth 3 (car newsrc))))
11497           ()
11498         (while marked
11499           (or (eq 'score (car (car marked)))
11500               (eq 'bookmark (car (car marked)))
11501               (eq 'killed (car (car marked)))
11502               (setcdr (car marked) (gnus-uncompress-range (cdr (car marked)))))
11503           (setq marked (cdr marked))))
11504       (setq newsrc (cdr newsrc)))))
11505
11506 (defun gnus-compress-newsrc-assoc ()
11507   ;; Compress all lists of marked articles in the newsrc assoc.
11508   (let ((newsrc gnus-newsrc-alist)
11509         marked)
11510     (while newsrc
11511       (if (not (setq marked (nth 3 (car newsrc))))
11512           ()
11513         (while marked
11514           (or (eq 'score (car (car marked)))
11515               (eq 'bookmark (car (car marked)))
11516               (eq 'killed (car (car marked)))
11517               (setcdr (car marked) 
11518                       (gnus-compress-sequence (sort (cdr (car marked)) '<) t)))
11519           (setq marked (cdr marked))))
11520       (setq newsrc (cdr newsrc)))))
11521
11522 ;; jwz: rewrote this function to be much more efficient, and not be
11523 ;; subject to regexp overflow errors when it encounters very long
11524 ;; lines -- the old behavior was to blow off the rest of the *file*
11525 ;; when a line was encountered that was too long to match!!  Now it
11526 ;; uses only simple looking-at calls, and doesn't create as many
11527 ;; temporary strings.  It also now handles multiple consecutive
11528 ;; options lines (before it only handled the first.)
11529 ;; Tiny rewrite by lmi. 
11530 (defun gnus-newsrc-to-gnus-format ()
11531   "Parse current buffer as .newsrc file."
11532   ;; We have to re-initialize these variables (except for
11533   ;; gnus-killed-list) because the quick startup file may contain bogus
11534   ;; values.
11535   (setq gnus-newsrc-options nil)
11536   (setq gnus-newsrc-options-n nil)
11537   (gnus-parse-options-lines)
11538   (gnus-parse-newsrc-body))
11539
11540 (defun gnus-parse-options-lines ()
11541   ;; newsrc.5 seems to indicate that the options line can come anywhere
11542   ;; in the file, and that there can be any number of them:
11543   ;;
11544   ;;       An  options  line  starts  with  the  word  options (left-
11545   ;;       justified).  Then there are the list of  options  just  as
11546   ;;       they would be on the readnews command line.  For instance:
11547   ;;
11548   ;;       options -n all !net.sf-lovers !mod.human-nets -r
11549   ;;       options -c -r
11550   ;;
11551   ;;       A string of lines beginning with a space or tab after  the
11552   ;;       initial  options  line  will  be  considered  continuation
11553   ;;       lines.
11554   ;;
11555   ;; For now, we only accept it at the beginning of the file.
11556
11557   (goto-char (point-min))
11558   (skip-chars-forward " \t\n")
11559   (setq gnus-newsrc-options nil)
11560   (while (looking-at "^options[ \t]*\\(.*\\)\n")
11561     ;; handle consecutive options lines
11562     (setq gnus-newsrc-options (concat gnus-newsrc-options
11563                                       (if gnus-newsrc-options "\n\t")
11564                                       (buffer-substring (match-beginning 1)
11565                                                         (match-end 1))))
11566     (forward-line 1)
11567     (while (looking-at "[ \t]+\\(.*\\)\n")
11568       ;; handle subsequent continuation lines of this options line
11569       (setq gnus-newsrc-options (concat gnus-newsrc-options "\n\t"
11570                                         (buffer-substring (match-beginning 1)
11571                                                           (match-end 1))))
11572       (forward-line 1)))
11573   ;; Gather all "-n" options lines.
11574   (let ((start 0)
11575         (result nil))
11576     (if gnus-newsrc-options
11577         (while (and (string-match "^[ \t]*-n\\([^\n]*\\)$"
11578                                   gnus-newsrc-options
11579                                   start)
11580                     (setq start (match-end 0)))
11581           (setq result (concat result
11582                                (and result " ")
11583                                (substring gnus-newsrc-options
11584                                           (match-beginning 1)
11585                                           (match-end 1))))))
11586
11587     (and result (gnus-parse-n-options result))
11588     nil))
11589
11590 (defun gnus-parse-newsrc-body ()
11591   ;; Point has been positioned after the options lines.  We shouldn't
11592   ;; see any more in here.
11593
11594   (let ((subscribe nil)
11595         (read-list nil)
11596         (line (1+ (count-lines (point-min) (point))))
11597         (already-read (> (length gnus-newsrc-alist) 1))
11598         newsgroup
11599         p p2)
11600     (save-restriction
11601       (skip-chars-forward " \t")
11602       (while (not (eobp))
11603         (cond
11604          ((= (following-char) ?\n)
11605           ;; skip blank lines
11606           nil)
11607          (t
11608           (setq p (point))
11609           (skip-chars-forward "^:!\n")
11610           (if (= (following-char) ?\n)
11611               (error "line %d is unparsable in %s" line (buffer-name)))
11612           (setq p2 (point))
11613           (skip-chars-backward " \t")
11614
11615           ;; #### note: we could avoid consing a string here by binding obarray
11616           ;; and reading the newsgroup directly into the gnus-newsrc-hashtb,
11617           ;; then setq'ing newsgroup to symbol-name of that, like we do in
11618           ;; gnus-active-to-gnus-format.
11619           (setq newsgroup (buffer-substring p (point)))
11620           (goto-char p2)
11621
11622           (setq subscribe (= (following-char) ?:))
11623           (setq read-list nil)
11624
11625           (forward-char 1)              ; after : or !
11626           (skip-chars-forward " \t")
11627           (while (not (= (following-char) ?\n))
11628             (skip-chars-forward " \t")
11629             (or
11630              (and (cond
11631                    ((looking-at "\\([0-9]+\\)-\\([0-9]+\\)") ; a range
11632                     (setq read-list
11633                           (cons
11634                            (cons
11635                             (progn
11636                               ;; faster that buffer-substring/string-to-int
11637                               (narrow-to-region (point-min) (match-end 1))
11638                               (read (current-buffer)))
11639                             (progn
11640                               (narrow-to-region (point-min) (match-end 2))
11641                               (forward-char) ; skip over "-"
11642                               (prog1
11643                                   (read (current-buffer))
11644                                 (widen))))
11645                            read-list))
11646                     t)
11647                    ((looking-at "[0-9]+")
11648                     ;; faster that buffer-substring/string-to-int
11649                     (narrow-to-region (point-min) (match-end 0))
11650                     (setq p (read (current-buffer)))
11651                     (widen)
11652                     (setq read-list (cons (cons p p) read-list))
11653                     t)
11654                    (t
11655                     ;; bogus chars in ranges
11656                     nil))
11657                   (progn
11658                     (goto-char (match-end 0))
11659                     (skip-chars-forward " \t")
11660                     (cond ((= (following-char) ?,)
11661                            (forward-char 1)
11662                            t)
11663                           ((= (following-char) ?\n)
11664                            t)
11665                           (t
11666                            ;; bogus char after range
11667                            nil))))
11668              ;; if we get here, the parse failed
11669              (progn
11670                (end-of-line)            ; give up on this line
11671                (ding)
11672                (message "Ignoring bogus line %d for %s in %s"
11673                         line newsgroup (buffer-name))
11674                (sleep-for 1))))
11675           ;; We have already read .newsrc.eld, so we gently update the
11676           ;; data in the hash table with the information we have just
11677           ;; read. 
11678           (let ((info (nth 2 (gnus-gethash newsgroup gnus-newsrc-hashtb)))
11679                 level)
11680             (if info
11681                 (progn
11682                   (setcar (nthcdr 2 info) (nreverse read-list))
11683                   ;; We update the level very gently.  In fact, we
11684                   ;; only change it if there's been a status change
11685                   ;; from subscribed to unsubscribed, or vice versa.
11686                   (setq level (nth 1 info))
11687                   (cond ((and (<= level gnus-level-subscribed) (not subscribe))
11688                          (setq level (if read-list 
11689                                          gnus-level-default-unsubscribed 
11690                                        (1+ gnus-level-default-unsubscribed))))
11691                         ((and (> level gnus-level-subscribed) subscribe)
11692                          (setq level gnus-level-default-subscribed)))
11693                   (setcar (cdr info) level))
11694               (setq gnus-newsrc-alist
11695                     (cons (list newsgroup 
11696                                 (if subscribe
11697                                     gnus-level-default-subscribed 
11698                                   (if read-list 
11699                                       (1+ gnus-level-subscribed)
11700                                     gnus-level-default-unsubscribed))
11701                                 (nreverse read-list))
11702                           gnus-newsrc-alist))))))
11703         (setq line (1+ line))
11704         (forward-line 1)))
11705     (setq gnus-newsrc-alist (cdr gnus-newsrc-alist))
11706     (or already-read (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist)))
11707     (gnus-make-hashtable-from-newsrc-alist)
11708     nil))
11709
11710 (defun gnus-parse-n-options (options)
11711   "Parse -n NEWSGROUPS options and return a cons of YES and NO regexps."
11712   (let (yes no yes-or-no out newsgroup)
11713     ;; Parse each newsgroup description such as "comp.all".  Commas
11714     ;; and white spaces can be a newsgroup separator.
11715     (while
11716         (string-match "^[ \t\n,]*\\(!?\\)\\([^- \t\n,][^ \t\n,]*\\)" options)
11717       (setq yes-or-no
11718             (substring options (match-beginning 1) (match-end 1)))
11719       (setq newsgroup
11720             (regexp-quote
11721              (substring options
11722                         (match-beginning 2) (match-end 2))))
11723       (setq options (substring options (match-end 2)))
11724       ;; Rewrite "all" to ".+" not ".*".  ".+" requires at least one
11725       ;; character.
11726       (while (string-match "\\(^\\|\\\\[.]\\)all\\(\\\\[.]\\|$\\)" newsgroup)
11727         (setq newsgroup
11728               (concat (substring newsgroup 0 (match-end 1))
11729                       ".+"
11730                       (substring newsgroup (match-beginning 2)))))
11731       ;; It is yes or no.
11732       (setq out (cons (cons (if (string= yes-or-no "!") 'ignore 'subscribe)
11733                             (concat "^" newsgroup "$")) 
11734                       out)))
11735     (setq gnus-newsrc-options-n out)))
11736
11737 (defun gnus-save-newsrc-file ()
11738   "Save .newsrc file."
11739   ;; Note: We cannot save .newsrc file if all newsgroups are removed
11740   ;; from the variable gnus-newsrc-alist.
11741   (and (or gnus-newsrc-alist gnus-killed-list)
11742        gnus-current-startup-file
11743        (let ((make-backup-files t)
11744              (version-control nil)
11745              (require-final-newline t)) ;Don't ask even if requested.
11746          ;; You can stop or change version control of backup file.
11747          ;; Suggested by jason@violet.berkeley.edu.
11748          (run-hooks 'gnus-save-newsrc-hook)
11749          (save-excursion
11750            (if (or (not gnus-dribble-buffer)
11751                    (not (buffer-name gnus-dribble-buffer))
11752                    (zerop (save-excursion
11753                             (set-buffer gnus-dribble-buffer)
11754                             (buffer-size))))
11755                (message "(No changes need to be saved)")
11756              (if gnus-save-newsrc-file
11757                  (progn
11758                    (message "Saving %s..." gnus-current-startup-file)
11759                    ;; Make backup file of master newsrc.
11760                    (gnus-gnus-to-newsrc-format)
11761                    (message "Saving %s... done" gnus-current-startup-file)))
11762              ;; Quickly loadable .newsrc.
11763              (set-buffer (get-buffer-create " *Gnus-newsrc*"))
11764              (gnus-add-current-to-buffer-list)
11765              (buffer-disable-undo (current-buffer))
11766              (erase-buffer)
11767              (message "Saving %s.eld..." gnus-current-startup-file)
11768              (gnus-gnus-to-quick-newsrc-format)
11769              (write-region 1 (point-max) 
11770                            (concat gnus-current-startup-file ".eld") 
11771                            nil 'nomesg)
11772              (kill-buffer (current-buffer))
11773              (message "Saving %s.eld... done" gnus-current-startup-file)
11774              (gnus-dribble-delete-file))))))
11775
11776 (defun gnus-gnus-to-quick-newsrc-format ()
11777   "Insert Gnus variables such as gnus-newsrc-alist in lisp format."
11778   (insert ";; (ding) Gnus startup file.\n")
11779   (insert ";; Never delete this file - touch .newsrc instead to force Gnus\n")
11780   (insert ";; to read .newsrc.\n")
11781   (let ((variables gnus-variable-list)
11782         (inhibit-quit t)
11783         (gnus-newsrc-alist (cdr gnus-newsrc-alist))
11784         variable)
11785     ;; insert lisp expressions.
11786     (gnus-compress-newsrc-assoc)
11787     (while variables
11788       (setq variable (car variables))
11789       (and (boundp variable)
11790            (symbol-value variable)
11791            (or gnus-save-killed-list (not (eq variable 'gnus-killed-list)))
11792            (insert "(setq " (symbol-name variable) " '"
11793                    (prin1-to-string (symbol-value variable))
11794                    ")\n"))
11795       (setq variables (cdr variables)))
11796     (gnus-uncompress-newsrc-assoc)))
11797
11798
11799 (defun gnus-gnus-to-newsrc-format ()
11800   ;; Generate and save the .newsrc file.
11801   (let ((newsrc (cdr gnus-newsrc-alist))
11802         info ranges range)
11803     (save-excursion
11804       (set-buffer (create-file-buffer gnus-startup-file))
11805       (buffer-disable-undo (current-buffer))
11806       (erase-buffer)
11807       ;; Write options.
11808       (if gnus-newsrc-options (insert "options " gnus-newsrc-options "\n"))
11809       ;; Write subscribed and unsubscribed.
11810       (while newsrc
11811         (setq info (car newsrc))
11812         (if (not (nth 4 info))          ;Don't write foreign groups to .newsrc.
11813             (progn
11814               (insert (car info) (if (> (nth 1 info) gnus-level-subscribed)
11815                                      "!" ":"))
11816               (if (setq ranges (nth 2 info))
11817                   (progn
11818                     (insert " ")
11819                     (if (not (listp (cdr ranges)))
11820                         (if (= (car ranges) (cdr ranges))
11821                             (insert (int-to-string (car ranges)))
11822                           (insert (int-to-string (car ranges)) "-" 
11823                                   (int-to-string (cdr ranges))))
11824                       (while ranges
11825                         (setq range (car ranges)
11826                               ranges (cdr ranges))
11827                         (if (or (atom range) (= (car range) (cdr range)))
11828                             (insert (int-to-string 
11829                                      (or (and (atom range) range) 
11830                                          (car range))))
11831                           (insert (int-to-string (car range)) "-"
11832                                   (int-to-string (cdr range))))
11833                         (if ranges (insert ","))))))
11834               (insert "\n")))
11835         (setq newsrc (cdr newsrc)))
11836       (write-region 1 (point-max) gnus-current-startup-file nil 'nomesg)
11837       (kill-buffer (current-buffer)))))
11838
11839 (defun gnus-read-descriptions-file ()
11840   (message "Reading descriptions file...")
11841   (if (not (gnus-request-list-newsgroups gnus-select-method))
11842       (progn
11843         (message "Couldn't read newsgroups descriptions")
11844         nil)
11845     (let (group)
11846       (setq gnus-description-hashtb 
11847             (gnus-make-hashtable (length gnus-active-hashtb)))
11848       (save-excursion
11849         (save-restriction
11850           (set-buffer nntp-server-buffer)
11851           (goto-char (point-min))
11852           (delete-non-matching-lines "^[-\\._+A-Za-z0-9]+[ \t]")
11853           (goto-char (point-min))
11854           (if (or (search-forward "\n.\n" nil t)
11855                   (goto-char (point-max)))
11856               (progn
11857                 (beginning-of-line)
11858                 (narrow-to-region (point-min) (point))))
11859           (goto-char (point-min))
11860           (while (not (eobp))
11861             (setq group (let ((obarray gnus-description-hashtb))
11862                           (read (current-buffer))))
11863             (skip-chars-forward " \t")
11864             (set group (buffer-substring 
11865                         (point) (gnus-point-at-eol)))
11866             (forward-line 1))))
11867       (message "Reading descriptions file...done")
11868       t)))
11869
11870 (defun gnus-group-get-description (group)
11871   ;; Get the description of a group by sending XGTITLE to the server.
11872   (and (gnus-request-group-description group)
11873        (save-excursion
11874          (set-buffer nntp-server-buffer)
11875          (goto-char (point-min))
11876          (and (looking-at "[^ \t]+[ \t]+\\(.*\\)")
11877               (buffer-substring (match-beginning 1) (match-end 1))))))
11878
11879 ;;;
11880 ;;; Server
11881 ;;;
11882
11883 (defvar gnus-server-mode-hook nil
11884   "Hook run in `gnus-server-mode' buffers.")
11885
11886 (defconst gnus-server-line-format "     {%(%h:%w%)}\n"
11887   "Format of server lines.
11888 It works along the same lines as a normal formatting string,
11889 with some simple extensions.")
11890
11891 (defvar gnus-server-mode-line-format "(ding) List of servers"
11892   "The format specification for the server mode line.")
11893
11894 (defconst gnus-server-line-format-alist
11895   (list (list ?h 'how ?s)
11896         (list ?n 'name ?s)
11897         (list ?w 'where ?s)
11898         ))
11899
11900 (defconst gnus-server-mode-line-format-alist 
11901   (list (list ?S 'news-server ?s)
11902         (list ?M 'news-method ?s)
11903         (list ?u 'user-defined ?s)))
11904
11905 (defvar gnus-server-line-format-spec nil)
11906 (defvar gnus-server-mode-line-format-spec nil)
11907 (defvar gnus-server-killed-servers nil)
11908
11909 (defvar gnus-server-mode-map nil)
11910 (put 'gnus-server-mode 'mode-class 'special)
11911
11912 (if gnus-server-mode-map
11913     nil
11914   (setq gnus-server-mode-map (make-sparse-keymap))
11915   (suppress-keymap gnus-server-mode-map)
11916   (define-key gnus-server-mode-map " " 'gnus-server-read-server)
11917   (define-key gnus-server-mode-map "\r" 'gnus-server-read-server)
11918   (define-key gnus-server-mode-map "q" 'gnus-server-exit)
11919   (define-key gnus-server-mode-map "l" 'gnus-server-list-servers)
11920   (define-key gnus-server-mode-map "k" 'gnus-server-kill-server)
11921   (define-key gnus-server-mode-map "y" 'gnus-server-yank-server)
11922   (define-key gnus-server-mode-map "c" 'gnus-server-copy-server)
11923   (define-key gnus-server-mode-map "a" 'gnus-server-add-server)
11924   (define-key gnus-server-mode-map "e" 'gnus-server-edit-server))
11925
11926 (defun gnus-server-mode ()
11927   "Major mode for listing and editing servers.
11928
11929 All normal editing commands are switched off.
11930 \\<gnus-server-mode-map>
11931
11932 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]'). 
11933
11934 The following commands are available:
11935
11936 \\{gnus-server-mode-map}"
11937   (interactive)
11938 ;  (if gnus-visual (gnus-server-make-menu-bar))
11939   (kill-all-local-variables)
11940   (setq mode-line-modified "-- ")
11941   (make-local-variable 'mode-line-format)
11942   (setq mode-line-format (copy-sequence mode-line-format))
11943   (and (equal (nth 3 mode-line-format) "   ")
11944        (setcar (nthcdr 3 mode-line-format) ""))
11945   (setq major-mode 'gnus-server-mode)
11946   (setq mode-name "Server")
11947 ;  (gnus-group-set-mode-line)
11948   (setq mode-line-process nil)
11949   (use-local-map gnus-server-mode-map)
11950   (buffer-disable-undo (current-buffer))
11951   (setq truncate-lines t)
11952   (setq buffer-read-only t)
11953   (run-hooks 'gnus-server-mode-hook))
11954
11955 (defun gnus-server-insert-server-line (sformat name method)
11956   (let* ((sformat (or sformat gnus-server-line-format-spec))
11957          (how (car method))
11958          (where (nth 1 method))
11959          b)
11960     (beginning-of-line)
11961     (setq b (point))
11962     ;; Insert the text.
11963     (insert (eval sformat))
11964     (add-text-properties 
11965      b (1+ b) (list 'gnus-server (intern name)))))
11966
11967 (defun gnus-server-setup-buffer ()
11968   (if (get-buffer gnus-server-buffer)
11969       ()
11970     (save-excursion
11971       (set-buffer (get-buffer-create gnus-server-buffer))
11972       (gnus-server-mode))))
11973
11974 (defun gnus-server-prepare ()
11975   (setq gnus-server-mode-line-format-spec 
11976         (gnus-parse-format gnus-server-mode-line-format 
11977                            gnus-server-mode-line-format-alist))
11978   (setq gnus-server-line-format-spec 
11979         (gnus-parse-format gnus-server-line-format 
11980                            gnus-server-line-format-alist))
11981   (let ((alist gnus-server-alist)
11982         (buffer-read-only nil))
11983     (erase-buffer)
11984     (while alist
11985       (gnus-server-insert-server-line nil (car (car alist)) (cdr (car alist)))
11986       (setq alist (cdr alist))))
11987   (goto-char (point-min))
11988   (gnus-server-position-cursor))
11989
11990 (defun gnus-server-server-name ()
11991   (let ((server (get-text-property 
11992                  (save-excursion (beginning-of-line) (point)) 'gnus-server)))
11993     (and server (symbol-name server))))
11994
11995 (defalias 'gnus-server-position-cursor 'gnus-goto-colon)
11996
11997 (defvar gnus-winconf-edit-server nil)
11998 (defconst gnus-server-edit-buffer "*Gnus edit server*")
11999
12000 (defun gnus-server-update-server (server)
12001   (save-excursion
12002     (set-buffer gnus-server-buffer)
12003     (let ((buffer-read-only nil)
12004           (info (cdr (assoc server gnus-server-alist))))
12005       (gnus-dribble-enter 
12006        (concat "(gnus-server-set-info \"" server "\" '"
12007                (prin1-to-string info) ")"))
12008       ;; Buffer may be narrowed.
12009       (save-restriction
12010         (widen)
12011         (if (gnus-server-goto-server server)
12012             (delete-region (progn (beginning-of-line) (point))
12013                            (progn (forward-line 1) (point))))
12014         (let ((entry (assoc server gnus-server-alist)))
12015           (gnus-server-insert-server-line nil (car entry) (cdr entry))
12016           (gnus-server-position-cursor))))))
12017
12018 (defun gnus-server-set-info (server info)
12019   ;; Enter a select method into the virtual server alist.
12020   (gnus-dribble-enter 
12021    (concat "(gnus-server-set-info \"" server "\" '"
12022            (prin1-to-string info) ")"))
12023   (let* ((server (nth 1 info))
12024          (entry (assoc server gnus-server-alist)))
12025     (if entry (setcdr entry info)
12026       (setq gnus-server-alist
12027             (nconc gnus-server-alist (list (cons server info)))))))
12028
12029 (defun gnus-server-to-method (server)
12030   ;; Map virtual server names to select methods.
12031   (or (and (equal server "native") gnus-select-method)
12032       (cdr (assoc server gnus-server-alist))))
12033
12034 (defun gnus-server-extend-method (group method)
12035   ;; This function "extends" a virtual server.  If the server is
12036   ;; "hello", and the select method is ("hello" (my-var "something")) 
12037   ;; in the group "alt.alt", this will result in a new virtual server
12038   ;; called "helly+alt.alt".
12039   (let ((entry
12040          (gnus-copy-sequence 
12041           (if (equal (car method) "native") gnus-select-method
12042               (cdr (assoc (car method) gnus-server-alist))))))
12043     (setcar (cdr entry) (concat (nth 1 entry) "+" group))
12044     (nconc entry (cdr method))))
12045
12046 (defun gnus-server-get-method (group method)
12047   ;; Input either a server name, and extended server name, or a
12048   ;; select method, and return a select method. 
12049   (cond ((stringp method)
12050          (gnus-server-to-method method))
12051         ((stringp (car method))
12052          (gnus-server-extend-method group method))
12053         (t
12054          (gnus-server-add-address method))))
12055
12056 (defun gnus-server-add-address (method)
12057   (let ((method-name (symbol-name (car method))))
12058     (if (and (memq 'address (assoc method-name gnus-valid-select-methods))
12059              (not (assq (intern (concat method-name "-address")) method)))
12060         (append method (list (list (intern (concat method-name "-address"))
12061                                    (nth 1 method))))
12062       method)))
12063
12064 (defun gnus-server-equal (s1 s2)
12065   (or (equal s1 s2)
12066       (and (= (length s1) (length s2))
12067            (progn
12068              (while (and s1 (member (car s1) s2))
12069                (setq s1 (cdr s1)))
12070              (null s1)))))
12071
12072 ;;; Interactive server functions.
12073
12074 (defun gnus-server-kill-server (server)
12075   "Kill the server on the current line."
12076   (interactive (list (gnus-server-server-name)))
12077   (or (gnus-server-goto-server server)
12078       (if server (error "No such server: %s" server)
12079         (error "No server on the current line")))
12080   (let ((buffer-read-only nil))
12081     (delete-region (progn (beginning-of-line) (point))
12082                    (progn (forward-line 1) (point))))
12083   (setq gnus-server-killed-servers 
12084         (cons (assoc server gnus-server-alist) gnus-server-killed-servers))
12085   (setq gnus-server-alist (delq (car gnus-server-killed-servers)
12086                                 gnus-server-alist))
12087   (gnus-server-position-cursor))
12088
12089 (defun gnus-server-yank-server ()
12090   "Yank the previously killed server."
12091   (interactive)
12092   (or gnus-server-killed-servers
12093       (error "No killed servers to be yanked"))
12094   (let ((alist gnus-server-alist)
12095         (server (gnus-server-server-name))
12096         (killed (car gnus-server-killed-servers)))
12097     (if (not server) 
12098         (setq gnus-server-alist (nconc gnus-server-alist (list killed)))
12099       (if (string= server (car (car gnus-server-alist)))
12100           (setq gnus-server-alist (cons killed gnus-server-alist))
12101         (while (and (cdr alist)
12102                     (not (string= server (car (car (cdr alist))))))
12103           (setq alist (cdr alist)))
12104         (setcdr alist (cons killed (cdr alist)))))
12105     (gnus-server-update-server (car killed))
12106     (setq gnus-server-killed-servers (cdr gnus-server-killed-servers))
12107     (gnus-server-position-cursor)))
12108
12109 (defun gnus-server-exit ()
12110   "Return to the group buffer."
12111   (interactive)
12112   (kill-buffer (current-buffer))
12113   (switch-to-buffer gnus-group-buffer))
12114
12115 (defun gnus-server-list-servers ()
12116   "List all available servers."
12117   (interactive)
12118   (let ((cur (gnus-server-server-name)))
12119     (gnus-server-prepare)
12120     (if cur (gnus-server-goto-server cur)
12121       (goto-char (point-max))
12122       (forward-line -1))
12123     (gnus-server-position-cursor)))
12124
12125 (defun gnus-server-copy-server (from to)
12126   (interactive
12127    (list
12128     (or (gnus-server-server-name)
12129         (error "No server on the current line"))
12130     (read-string "Copy to: ")))
12131   (or from (error "No server on current line"))
12132   (or (and to (not (string= to ""))) (error "No name to copy to"))
12133   (and (assoc to gnus-server-alist) (error "%s already exists" to))
12134   (or (assoc from gnus-server-alist) 
12135       (error "%s: no such server" from))
12136   (let ((to-entry (gnus-copy-sequence (assoc from gnus-server-alist))))
12137     (setcar to-entry to)
12138     (setcar (nthcdr 2 to-entry) to)
12139     (setq gnus-server-killed-servers 
12140           (cons to-entry gnus-server-killed-servers))
12141     (gnus-server-yank-server)))
12142
12143 (defun gnus-server-add-server (how where)
12144   (interactive 
12145    (list (intern (completing-read "Server method: "
12146                                   gnus-valid-select-methods nil t))
12147          (read-string "Server name: ")))
12148   (setq gnus-server-killed-servers 
12149         (cons (list where how where) gnus-server-killed-servers))
12150   (gnus-server-yank-server))
12151
12152 (defun gnus-server-goto-server (server)
12153   "Jump to a server line."
12154   (interactive
12155    (list (completing-read "Goto server: " gnus-server-alist nil t)))
12156   (let ((to (text-property-any (point-min) (point-max) 
12157                                'gnus-server (intern server))))
12158     (and to
12159          (progn
12160            (goto-char to) 
12161            (gnus-server-position-cursor)))))
12162
12163 (defun gnus-server-edit-server (server)
12164   "Edit the server on the current line."
12165   (interactive (list (gnus-server-server-name)))
12166   (or server
12167       (error "No server on current line"))
12168   (setq gnus-winconf-edit-server (current-window-configuration))
12169   (pop-to-buffer gnus-server-edit-buffer)
12170   (gnus-add-current-to-buffer-list)
12171   (emacs-lisp-mode)
12172   (use-local-map (copy-keymap (current-local-map)))
12173   (let ((done-func '(lambda () 
12174                       "Exit editing mode and update the information."
12175                       (interactive)
12176                       (gnus-server-edit-server-done 'group))))
12177     (setcar (cdr (nth 4 done-func)) server)
12178     (local-set-key "\C-c\C-c" done-func))
12179   (erase-buffer)
12180   (insert ";; Type `C-c C-c' after you have edited the server.\n\n")
12181   (insert (pp-to-string (cdr (assoc server gnus-server-alist)))))
12182
12183 (defun gnus-server-edit-server-done (server)
12184   (interactive)
12185   (set-buffer (get-buffer-create gnus-server-edit-buffer))
12186   (goto-char (point-min))
12187   (let ((form (read (current-buffer))))
12188     (gnus-server-set-info server form))
12189   (kill-buffer (current-buffer))
12190   (and gnus-winconf-edit-server
12191        (set-window-configuration gnus-winconf-edit-server))
12192   (setq gnus-winconf-edit-server nil)
12193   (set-buffer gnus-server-buffer)
12194   (gnus-server-update-server (gnus-server-server-name))
12195   (gnus-server-position-cursor))
12196
12197 (defun gnus-server-read-server (server)
12198   "Browse a server."
12199   (interactive (list (gnus-server-server-name)))
12200   (gnus-browse-foreign-server (gnus-server-to-method server) (current-buffer)))
12201
12202 ;;; Gnus score functions.
12203
12204 (defvar gnus-global-score-files nil
12205   "*List of global score files and directories.
12206 Set this variable if you want to use people's score files.  One entry
12207 for each score file or each score file directory.  Gnus will decide
12208 by itself what score files are applicable to which group.
12209
12210 Say you want to use the single score file
12211 \"/ftp.ifi.uio.no@ftp:/pub/larsi/ding/score/soc.motss.SCORE\" and all
12212 score files in the \"/ftp.some-where:/pub/score\" directory.
12213
12214  (setq gnus-global-score-files
12215        '(\"/ftp.ifi.uio.no:/pub/larsi/ding/score/soc.motss.SCORE\"
12216          \"/ftp.some-where:/pub/score\"))")
12217
12218 (defun gnus-score-score-files (group)
12219   "Return a list of all possible score files."
12220   (and gnus-global-score-files 
12221        (or gnus-internal-global-score-files
12222            (gnus-score-search-global-directories gnus-global-score-files)))
12223   (setq gnus-kill-files-directory 
12224         (file-name-as-directory
12225          (or gnus-kill-files-directory "~/News/")))
12226   (if (not (file-readable-p gnus-kill-files-directory))
12227       (setq gnus-score-file-list nil)
12228     (if (gnus-use-long-file-name 'not-score)
12229         (if (or (not gnus-score-file-list)
12230                 (gnus-file-newer-than gnus-kill-files-directory
12231                                       (car gnus-score-file-list)))
12232               (setq gnus-score-file-list 
12233                     (cons (nth 5 (file-attributes gnus-kill-files-directory))
12234                           (nreverse 
12235                            (directory-files 
12236                             gnus-kill-files-directory t
12237                             (concat gnus-score-file-suffix "$"))))))
12238       (let ((dir (expand-file-name
12239                   (concat gnus-kill-files-directory
12240                           (gnus-replace-chars-in-string group ?. ?/))))
12241             (mdir (length (expand-file-name gnus-kill-files-directory)))
12242             files)
12243         (if (file-exists-p (concat dir "/" gnus-score-file-suffix))
12244             (setq files (list (concat dir "/" gnus-score-file-suffix))))
12245         (while (>= (1+ (length dir)) mdir)
12246           (and (file-exists-p (concat dir "/all/" gnus-score-file-suffix))
12247                (setq files (cons (concat dir "/all/" gnus-score-file-suffix)
12248                                  files)))
12249           (string-match "/[^/]*$" dir)
12250           (setq dir (substring dir 0 (match-beginning 0))))
12251         (setq gnus-score-file-list 
12252               (cons nil (nreverse files)))))
12253     (cdr gnus-score-file-list)))
12254         
12255 (defun gnus-score-find-bnews (group)
12256   "Return a list of score files for GROUP.
12257 The score files are those files in the ~/News directory which matches
12258 GROUP using BNews sys file syntax."
12259   (let* ((sfiles (append (gnus-score-score-files group)
12260                          gnus-internal-global-score-files))
12261          (kill-dir (file-name-as-directory 
12262                     (expand-file-name gnus-kill-files-directory)))
12263          (klen (length kill-dir))
12264          ofiles not-match regexp)
12265     (save-excursion
12266       (set-buffer (get-buffer-create "*gnus score files*"))
12267       (buffer-disable-undo (current-buffer))
12268       ;; Go through all score file names and create regexp with them
12269       ;; as the source.  
12270       (while sfiles
12271         (erase-buffer)
12272         (insert (car sfiles))
12273         (goto-char 1)
12274         ;; First remove the suffix itself.
12275         (re-search-forward (concat "." gnus-score-file-suffix "$"))
12276         (replace-match "" t t) 
12277         (goto-char 1)
12278         (if (looking-at (regexp-quote kill-dir))
12279             ;; If the file name was just "SCORE", `klen' is one character
12280             ;; too much.
12281             (delete-char (min (1- (point-max)) klen))
12282           (goto-char (point-max))
12283           (search-backward "/")
12284           (delete-region (1+ (point)) (point-min)))
12285         ;; Translate "all" to ".*".
12286         (while (search-forward "all" nil t)
12287           (replace-match ".*" t t))
12288         (goto-char 1)
12289         ;; Deal with "not."s.
12290         (if (looking-at "not.")
12291             (progn
12292               (setq not-match t)
12293               (setq regexp (buffer-substring 5 (point-max))))
12294           (setq regexp (buffer-substring 1 (point-max)))
12295           (setq not-match nil))
12296         ;; Finally - if this resulting regexp matches the group name,
12297         ;; we add this score file to the list of score files
12298         ;; applicable to this group.
12299         (if (or (and not-match
12300                      (not (string-match regexp group)))
12301                 (and (not not-match)
12302                      (string-match regexp group)))
12303             (setq ofiles (cons (car sfiles) ofiles)))
12304         (setq sfiles (cdr sfiles)))
12305       (kill-buffer (current-buffer))
12306       ;; Slight kludge here - the last score file returned should be
12307       ;; the local score file, whether it exists or not. This is so
12308       ;; that any score commands the user enters will go to the right
12309       ;; file, and not end up in some global score file.
12310       (let ((localscore
12311              (expand-file-name
12312               (if (gnus-use-long-file-name 'not-score)
12313                   (concat gnus-kill-files-directory group "." 
12314                           gnus-score-file-suffix)
12315                 (concat gnus-kill-files-directory
12316                         (gnus-replace-chars-in-string group ?. ?/)
12317                         "/" gnus-score-file-suffix)))))
12318         (and (member localscore ofiles)
12319              (delete localscore ofiles))
12320         (setq ofiles (cons localscore ofiles)))
12321       (nreverse ofiles))))
12322
12323 (defun gnus-score-find-single (group)
12324   "Return list containing the score file for GROUP."
12325   (list (gnus-score-file-name group)))
12326
12327 (defun gnus-score-find-hierarchical (group)
12328   "Return list of score files for GROUP.
12329 This includes the score file for the group and all its parents."
12330   (let ((all (copy-sequence '(nil)))
12331         (start 0))
12332     (while (string-match "\\." group (1+ start))
12333       (setq start (match-beginning 0))
12334       (setq all (cons (substring group 0 start) all)))
12335     (setq all (cons group all))
12336     (mapcar 'gnus-score-file-name (nreverse all))))
12337
12338 (defun gnus-possibly-score-headers ()
12339   (let ((func gnus-score-find-score-files-function)
12340         score-files scores)
12341     (and func (not (listp func))
12342          (setq func (list func)))
12343     ;; Go through all the functions for finding score files (or actual
12344     ;; scores) and add them to a list.
12345     (while func
12346       (and (symbolp (car func))
12347            (fboundp (car func))
12348            (setq score-files 
12349                  (nconc score-files (funcall (car func) gnus-newsgroup-name))))
12350       (setq func (cdr func)))
12351     (if score-files (gnus-score-headers score-files))))
12352
12353 (defun gnus-score-file-name (newsgroup)
12354   "Return the name of a score file for NEWSGROUP."
12355   (cond  ((or (null newsgroup)
12356               (string-equal newsgroup ""))
12357           ;; The global score file is placed at top of the directory.
12358           (expand-file-name gnus-score-file-suffix
12359                             (or gnus-kill-files-directory "~/News")))
12360          ((gnus-use-long-file-name 'not-score)
12361           ;; Append ".SCORE" to newsgroup name.
12362           (expand-file-name (concat newsgroup "." gnus-score-file-suffix)
12363                             (or gnus-kill-files-directory "~/News")))
12364          (t
12365           ;; Place "SCORE" under the hierarchical directory.
12366           (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
12367                                     "/" gnus-score-file-suffix)
12368                             (or gnus-kill-files-directory "~/News")))))
12369
12370 (defun gnus-score-search-global-directories (files)
12371   "Scan all global score directories for score files."
12372   ;; Set the variable `gnus-internal-global-score-files' to all
12373   ;; available global score files.
12374   (interactive (list gnus-global-score-files))
12375   (let (out)
12376     (while files
12377       (if (string-match "/$" (car files))
12378           (setq out (nconc (directory-files 
12379                             (car files) t
12380                             (concat gnus-score-file-suffix "$"))))
12381         (setq out (cons (car files) out)))
12382       (setq files (cdr files)))
12383     (setq gnus-internal-global-score-files out)))
12384
12385 (defun gnus-score-default (level)
12386   (if level (prefix-numeric-value level) 
12387     gnus-score-interactive-default-score))
12388
12389 (provide 'gnus)
12390
12391 ;;; gnus.el ends here