*** 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-fetch-old-headers nil
360   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
361 If an unread article in the group refers to an older, already read (or
362 just marked as read) article, the old article will not normally be
363 displayed in the Summary buffer.  If this variable is non-nil, Gnus
364 will attempt to grab the headers to the old articles, and thereby
365 build complete threads.  If it has the value `some', only enough
366 headers to connect otherwise loose threads will be displayed.
367
368 The server has to support XOVER for any of this to work.")
369
370 (defvar gnus-visual t
371   "*If non-nil, will do various highlighting.
372 If nil, no mouse highlights (or any other highlights) will be
373 performed.  This might speed up Gnus some when generating large group
374 and summary buffers.")
375
376 (defvar gnus-novice-user t
377   "*Non-nil means that you are a usenet novice.
378 If non-nil, verbose messages may be displayed and confirmations may be
379 required.")
380
381 (defvar gnus-expert-user nil
382   "*Non-nil means that you will never be asked for confirmation about anything.
383 And that means *anything*.")
384
385 (defvar gnus-keep-same-level nil
386   "*Non-nil means that the next newsgroup after the current will be on the same level.
387 When you type, for instance, `n' after reading the last article in the
388 current newsgroup, you will go to the next newsgroup. If this variable
389 is nil, the next newsgroup will be the next from the group
390 buffer. 
391 If this variable is non-nil, Gnus will either put you in the
392 next newsgroup with the same level, or, if no such newsgroup is
393 available, the next newsgroup with the lowest possible level higher
394 than the current level.
395 If this variable is `best', Gnus will make the next newsgroup the one
396 with the best level.")
397
398 (defvar gnus-summary-make-false-root 'adopt
399   "*nil means that Gnus won't gather loose threads.
400 If the root of a thread has expired or been read in a previous
401 session, the information necessary to build a complete thread has been
402 lost. Instead of having many small sub-threads from this original thread
403 scattered all over the summary buffer, Gnus can gather them. 
404
405 If non-nil, Gnus will try to gather all loose sub-threads from an
406 original thread into one large thread.
407
408 If this variable is non-nil, it should be one of `none', `adopt',
409 `dummy' or `empty'.
410
411 If this variable is `none', Gnus will not make a false root, but just
412 present the sub-threads after another.
413 If this variable is `dummy', Gnus will create a dummy root that will
414 have all the sub-threads as children.
415 If this variable is `adopt', Gnus will make one of the \"children\"
416 the parent and mark all the step-children as such.
417 If this variable is `empty', the \"children\" are printed with empty
418 subject fields.  (Or rather, they will be printed with a string
419 given by the `gnus-summary-same-subject' variable.)")
420
421 (defvar gnus-summary-gather-subject-limit nil
422   "*Maximum length of subject comparisons when gathering loose threads.
423 Use nil to compare full subjects.  Setting this variable to a low
424 number will help gather threads that have been corrupted by
425 newsreaders chopping off subject lines, but it might also mean that
426 unrelated articles that have subject that happen to begin with the
427 same few characters will be incorrectly gathered.
428
429 If this variable is `fuzzy', Gnus will use a fuzzy algortihm when
430 comparing subjects.")
431
432 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
433 (defvar gnus-summary-same-subject ""
434   "*String indicating that the current article has the same subject as the previous.
435 This variable will only be used if the value of
436 `gnus-summary-make-false-root' is `empty'.")
437
438 (defvar gnus-summary-goto-unread nil
439   "*If non-nil, marking commands will go to the next unread article.")
440
441 (defvar gnus-check-new-newsgroups t
442   "*Non-nil means that Gnus will add new newsgroups at startup.
443 If this variable is `ask-server', Gnus will ask the server for new
444 groups since the last time it checked. This means that the killed list
445 is no longer necessary, so you could set `gnus-save-killed-list' to
446 nil. 
447
448 A variant is to have this variable be a list of select methods. Gnus
449 will then use the `ask-server' method on all these select methods to
450 query for new groups from all those servers.
451
452 Eg.
453   (setq gnus-check-new-newsgroups 
454         '((nntp \"some.server\") (nntp \"other.server\")))
455
456 If this variable is nil, then you have to tell Gnus explicitly to
457 check for new newsgroups with \\<gnus-group-mode-map>\\[gnus-find-new-newsgroups].")
458
459 (defvar gnus-check-bogus-newsgroups nil
460   "*Non-nil means that Gnus will check and remove bogus newsgroup at startup.
461 If this variable is nil, then you have to tell Gnus explicitly to
462 check for bogus newsgroups with \\<gnus-group-mode-map>\\[gnus-group-check-bogus-groups].")
463
464 (defvar gnus-read-active-file t
465   "*Non-nil means that Gnus will read the entire active file at startup.
466 If this variable is nil, Gnus will only know about the groups in your
467 `.newsrc' file.
468
469 If this variable is `some', Gnus will try to only read the relevant
470 parts of the active file from the server.  Not all servers support
471 this, and it might be quite slow with other servers, but this should
472 generally be faster than both the t and nil value.
473
474 If you set this variable to nil or `some', you probably still want to
475 be told about new newsgroups that arrive.  To do that, set
476 `gnus-check-new-newsgroups' to `ask-server'.  This may not work
477 properly with all servers.")
478
479 (defvar gnus-level-subscribed 5
480   "*Groups with levels less than or equal to this variable are subscribed.")
481
482 (defvar gnus-level-unsubscribed 7
483   "*Groups with levels less than or equal to this variable are unsubscribed.
484 Groups with levels less than `gnus-level-subscribed', which should be
485 less than thiss variable, are subscribed.")
486
487 (defvar gnus-level-zombie 8
488   "*Groups with this level are zombie groups.")
489
490 (defvar gnus-level-killed 9
491   "*Groups with this level are killed.")
492
493 (defvar gnus-level-default-subscribed 3
494   "*New subscribed groups will be subscribed at this level.")
495
496 (defvar gnus-level-default-unsubscribed 6
497   "*New unsubscribed groups will be unsubscribed at this level.")
498
499 (defvar gnus-activate-foreign-newsgroups nil
500   "*If nil, Gnus will not check foreign newsgroups at startup.
501 If it is non-nil, it should be a number between one and nine. Foreign
502 newsgroups that have a level lower or equal to this number will be
503 activated on startup. For instance, if you want to active all
504 subscribed newsgroups, but not the rest, you'd set this variable to 
505 `gnus-level-subscribed'.
506
507 If you subscribe to lots of newsgroups from different servers, startup
508 might take a while. By setting this variable to nil, you'll save time,
509 but you won't be told how many unread articles there are in the
510 groups.")
511
512 (defvar gnus-save-newsrc-file t
513   "*Non-nil means that Gnus will save the `.newsrc' file.
514 Gnus always saves its own startup file, which is called
515 \".newsrc.eld\".  The file called \".newsrc\" is in a format that can
516 be readily understood by other newsreaders.  If you don't plan on
517 using other newsreaders, set this variable to nil to save some time on
518 exit.")
519
520 (defvar gnus-save-killed-list t
521   "*If non-nil, save the list of killed groups to the startup file.
522 This will save both time (when starting and quitting) and space (both
523 memory and disk), but it will also mean that Gnus has no record of
524 which groups are new and which are old, so the automatic new
525 newsgroups subscription methods become meaningless. You should always
526 set `gnus-check-new-newsgroups' to `ask-server' or nil if you set this
527 variable to nil.")
528
529 (defvar gnus-interactive-catchup t
530   "*If non-nil, require your confirmation when catching up a group.")
531
532 (defvar gnus-interactive-post t
533   "*If non-nil, group name will be asked for when posting.")
534
535 (defvar gnus-interactive-exit t
536   "*If non-nil, require your confirmation when exiting Gnus.")
537
538 (defvar gnus-kill-killed nil
539   "*If non-nil, Gnus will apply kill files to already killed articles.
540 If it is nil, Gnus will never apply kill files to articles that have
541 already been through the scoring process, which might very well save lots
542 of time.")
543
544 (defvar gnus-extract-address-components 'gnus-extract-address-components
545   "*Function for extracting address components from a From header.
546 Two pre-defined function exist: `gnus-extract-address-components',
547 which is the default, quite fast, and too simplistic solution, and
548 `mail-extract-address-components', which works much better, but is
549 slower.")
550
551 (defvar gnus-summary-default-score 0
552   "*Default article score level.
553 If this variable is nil, scoring will be disabled.")
554
555 (defvar gnus-group-default-list-level gnus-level-subscribed
556   "*Default listing level.")
557
558 (defvar gnus-group-always-list-unread gnus-level-subscribed
559   "*Always list groups less than this variable with unread articles. 
560 If nil, use parameter to specify.")
561
562 (defvar gnus-user-login-name nil
563   "*The login name of the user.
564 Got from the function `user-login-name' if undefined.")
565
566 (defvar gnus-user-full-name nil
567   "*The full name of the user.
568 Got from the NAME environment variable if undefined.")
569
570 (defvar gnus-user-from-line nil
571   "*Your full, complete e-mail address.  
572 Overrides the other Gnus variables if it is non-nil.
573
574 Here are two example values of this variable:
575
576  \"Lars Magne Ingebrigtsen <larsi@ifi.uio.no>\"
577
578 and
579
580  \"larsi@ifi.uio.no (Lars Magne Ingebrigtsen)\"
581
582 The first version is recommended, but the name has to be quoted if it
583 contains non-alphanumerical characters.")
584
585 (defvar gnus-show-mime nil
586   "*If non-ni, do mime processing of articles.
587 The articles will simply be fed to the function given by
588 `gnus-show-mime-method'.")
589
590 (defvar gnus-show-mime-method (function metamail-buffer)
591   "*Function to process a MIME message.
592 The function is called from the article buffer.")
593
594 (defvar gnus-show-threads t
595   "*If non-nil, display threads in summary mode.")
596
597 (defvar gnus-thread-hide-subtree nil
598   "*If non-nil, hide all threads initially.
599 If threads are hidden, you have to run the command
600 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
601 to expose hidden threads.")
602
603 (defvar gnus-thread-hide-killed t
604   "*If non-nil, hide killed threads automatically.")
605
606 (defvar gnus-thread-ignore-subject nil
607   "*If non-nil, ignore subjects and do all threading based on the Reference header.
608 If nil, which is the default, articles that have different subjects
609 from their parents will start separate threads.")
610
611 (defvar gnus-thread-indent-level 4
612   "*Number that says how much each sub-thread should be indented.")
613
614 ;; jwz: nuke newsgroups whose name is all digits - that means that
615 ;; some loser has let articles get into the root of the news spool,
616 ;; which is toxic. Lines beginning with whitespace also tend to be
617 ;; toxic.
618 (defvar gnus-ignored-newsgroups
619   (purecopy (mapconcat 'identity
620                        '("^to\\."       ; not "real" groups
621                          "^[0-9. \t]+ " ; all digits in name
622                          "[][\"#'()     ;\\]"   ; bogus characters
623                          )
624                        "\\|"))
625   "*A regexp to match uninteresting newsgroups in the active file.
626 Any lines in the active file matching this regular expression are
627 removed from the newsgroup list before anything else is done to it,
628 thus making them effectively non-existant.")
629
630 (defvar gnus-ignored-headers
631   "^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:"
632   "*All headers that match this regexp will be hidden.
633 Also see `gnus-visible-headers'.")
634
635 (defvar gnus-visible-headers "^From:\\|^Newsgroups:\\|^Subject:\\|^Date:\\|^Followup-To:\\|^Reply-To:\\|^Organization:\\|^Summary:\\|^Keywords:\\|^To:\\|^Cc:"
636   "*All headers that do not match this regexp will be hidden.
637 Also see `gnus-ignored-headers'.")
638
639 (defvar gnus-sorted-header-list
640   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:" "^To:" 
641     "^Cc:" "^Date:" "^Organization:")
642   "*This variable is a list of regular expressions.
643 If it is non-nil, headers that match the regular expressions will
644 be placed first in the article buffer in the sequence specified by
645 this list.")
646
647 (defvar gnus-check-before-posting t
648   "In non-nil, Gnus will attempt to run some checks on outgoing posts.")
649
650 (defvar gnus-required-headers
651   '(From Date Newsgroups Subject Message-ID Organization Lines X-Newsreader)
652   "*Headers to be generated or prompted for when posting an article.
653 RFC977 and RFC1036 require From, Date, Newsgroups, Subject,
654 Message-ID.  Organization, Lines and X-Newsreader are optional.  If
655 you want Gnus not to insert some header, remove it from this list.")
656
657 (defvar gnus-show-all-headers nil
658   "*If non-nil, don't hide any headers.")
659
660 (defvar gnus-save-all-headers t
661   "*If non-nil, don't remove any headers before saving.")
662
663 (defvar gnus-inhibit-startup-message nil
664   "*If non-nil, the startup message will not be displayed.")
665
666 (defvar gnus-auto-extend-newsgroup t
667   "*If non-nil, extend newsgroup forward and backward when requested.")
668
669 (defvar gnus-auto-select-first t
670   "*If non-nil, select the first unread article when entering a group.
671 If you want to prevent automatic selection of the first unread article
672 in some newsgroups, set the variable to nil in
673 `gnus-select-group-hook'.") 
674
675 (defvar gnus-auto-select-next t
676   "*If non-nil, offer to go to the next group from the end of the previous.
677 If the value is t and the next newsgroup is empty, Gnus will exit
678 summary mode and go back to group mode.  If the value is neither nil
679 nor t, Gnus will select the following unread newsgroup.  In
680 particular, if the value is the symbol `quietly', the next unread
681 newsgroup will be selected without any confirmations.")
682
683 (defvar gnus-auto-select-same nil
684   "*If non-nil, select the next article with the same subject.")
685
686 (defvar gnus-summary-check-current nil
687   "*If non-nil, consider the current article when moving.
688 The \"unread\" movement commands will stay on the same line if the
689 current article is unread.")
690
691 (defvar gnus-auto-center-summary t
692   "*If non-nil, always center the current summary buffer.")
693
694 (defvar gnus-auto-mail-to-author nil
695   "*If non-nil, mail the authors of articles a copy of your follow-ups.
696 If this variable is `ask', the user will be prompted for whether to
697 mail a copy.  The string given by `gnus-mail-courtesy-message' will be
698 inserted at the beginning of the mail copy.
699
700 Mail is sent using the function specified by the
701 `gnus-mail-send-method' variable.")
702
703 ;; Added by Ethan Bradford <ethanb@ptolemy.astro.washington.edu>.
704 (defvar gnus-mail-courtesy-message
705   "The following message is a courtesy copy of an article\nthat has been posted as well.\n\n"
706   "*This is inserted at the start of a mailed copy of a posted message.
707 If this variable is nil, no such courtesy message will be added.")
708
709 (defvar gnus-break-pages t
710   "*If non-nil, do page breaking on articles.
711 The page delimiter is specified by the `gnus-page-delimiter'
712 variable.")
713
714 (defvar gnus-page-delimiter "^\^L"
715   "*Regexp describing what to use as article page delimiters.
716 The default value is \"^\^L\", which is a form linefeed at the
717 beginning of a line.")
718
719 (defvar gnus-use-full-window t
720   "*If non-nil, use the entire Emacs screen.")
721
722 ; for split windows. maybe a better way?
723 (defvar gnus-split-window nil
724   "*If non-nil, put the article buffer in left-hand side of the window .")
725
726 (defvar gnus-window-configuration
727   '((newsgroups (1 0 0))
728     (summary (0 1 0))
729     (article (0 3 10)))
730   "*Specify window configurations for each action.
731 The format of the variable is either a list of (ACTION (G S A)), where
732 G, S, and A are the relative height of group, summary, and article
733 windows, respectively, or a list of (ACTION FUNCTION), where FUNCTION
734 is a function that will be called with ACTION as an argument. ACTION
735 can be `summary', `newsgroups', or `article'.")
736
737 (defvar gnus-mail-reply-method (function gnus-mail-reply-using-mail)
738   "*Function to compose a reply.
739 Two pre-made functions are `gnus-mail-reply-using-mail' (sendmail) and
740 `gnus-mail-reply-using-mhe' (MH-E).")
741
742 (defvar gnus-mail-forward-method (function gnus-mail-forward-using-mail)
743   "*Function to forward the current message to another user.
744 Two pre-made functions are `gnus-mail-forward-using-mail' (sendmail)
745 and `gnus-mail-forward-using-mhe' (MH-E).") 
746
747 (defvar gnus-mail-other-window-method 'gnus-mail-other-window-using-mail
748   "*Function to compose mail in the other window.
749 Two pre-made functions are `gnus-mail-other-window-using-mail'
750 (sendmail) and `gnus-mail-other-window-using-mhe' (MH-E).")
751
752 (defvar gnus-mail-send-method send-mail-function
753   "*Function to mail a message which is also being posted as an article.
754 The message must have To or Cc header.  The default is copied from
755 the variable `send-mail-function'.")
756
757 (defvar gnus-subscribe-newsgroup-method 'gnus-subscribe-zombies
758   "*Function called with a group name when new group is detected.
759 A few pre-made functions are supplied: `gnus-subscribe-randomly'
760 inserts new groups at the beginning of the list of groups;
761 `gnus-subscribe-alphabetically' inserts new groups in strict
762 alphabetic order; `gnus-subscribe-hierarchically' inserts new groups
763 in hierarchical newsgroup order; `gnus-subscribe-interactively' asks
764 for your decision.")
765
766 ;; Suggested by a bug report by Hallvard B Furuseth.
767 ;; <h.b.furuseth@usit.uio.no>. 
768 (defvar gnus-subscribe-options-newsgroup-method
769   (function gnus-subscribe-alphabetically)
770   "*This function is called to subscribe newsgroups mentioned on \"options -n\" lines.
771 If, for instance, you want to subscribe to all newsgroups in the
772 \"no\" and \"alt\" hierarchies, you'd put the following in your
773 .newsrc file:
774
775 options -n no.all alt.all
776
777 Gnus will the subscribe all new newsgroups in these hierarchies with
778 the subscription method in this variable.")
779
780 (defvar gnus-subscribe-hierarchical-interactive nil
781   "*If non-nil, Gnus will offer to subscribe hierarchically.
782 When a new hierarchy appears, Gnus will ask the user:
783
784 'alt.binaries': Do you want to subscribe to this hierarchy? ([d]ys):
785
786 If the user pressed `d', Gnus will descend the hierarchy, `y' will
787 subscribe to all newsgroups in the hierarchy and `s' will skip this
788 hierarchy in its entirety.")
789
790 (defvar gnus-group-sort-function 'gnus-group-sort-by-alphabet
791   "*Function used for sorting the group buffer.
792 This function will be called with group info entries as the arguments
793 for the groups to be sorted.  Pre-made functions include
794 `gnus-sort-by-alphabet', `gnus-sort-by-unread' and
795 `gnus-sort-by-level'")
796
797 ;; Mark variables suggested by Thomas Michanek
798 ;; <Thomas.Michanek@telelogic.se>. 
799 (defvar gnus-unread-mark ? 
800   "*Mark used for unread articles.")
801 (defvar gnus-ticked-mark ?!
802   "*Mark used for ticked articles.")
803 (defvar gnus-dormant-mark ??
804   "*Mark used for dormant articles.")
805 (defvar gnus-del-mark ?D
806   "*Mark used for del'd articles.")
807 (defvar gnus-read-mark ?d
808   "*Mark used for read articles.")
809 (defvar gnus-expirable-mark ?E
810   "*Mark used for expirable articles.")
811 (defvar gnus-killed-mark ?K
812   "*Mark used for killed articles.")
813 (defvar gnus-kill-file-mark ?X
814   "*Mark used for articles killed by kill files.")
815 (defvar gnus-low-score-mark ?Y
816   "*Mark used for articles with a low score.")
817 (defvar gnus-catchup-mark ?C
818   "*Mark used for articles that are caught up.")
819 (defvar gnus-replied-mark ?R
820   "*Mark used for articles that have been replied to.")
821 (defvar gnus-process-mark ?# 
822   "*Process mark.")
823 (defvar gnus-ancient-mark ?A
824   "*Mark used for ancient articles.")
825 (defvar gnus-canceled-mark ?G
826   "*Mark used for cancelled articles.")
827 (defvar gnus-score-over-mark ?+
828   "*Score mark used for articles with high scores.")
829 (defvar gnus-score-below-mark ?-
830   "*Score mark used for articles with low scores.")
831 (defvar gnus-empty-thread-mark ? 
832   "*There is no thread under the article.")
833 (defvar gnus-not-empty-thread-mark ?=
834   "*There is a thread under the article.")
835
836 (defvar gnus-view-pseudo-asynchronously nil
837   "*If non-nil, Gnus will view pseudo-articles asynchronously.")
838
839 (defvar gnus-view-pseudos nil
840   "*If `automatic', pseudo-articles will be viewed automatically.
841 If `not-confirm', pseudos will be viewed automatically, and the user
842 will not be asked to confirm the command.")
843
844 (defvar gnus-view-pseudos-separately t
845   "*If non-nil, one pseudo-article will be created for each file to be viewed.
846 If nil, all files that use the same viewing command will be given as a
847 list of parameters to that command.")
848
849 (defvar gnus-group-line-format "%M%S%5y: %(%g%)\n"
850   "*Format of group lines.
851 It works along the same lines as a normal formatting string,
852 with some simple extensions.
853
854 %M    Only marked articles (character, \"*\" or \" \")
855 %S    Whether the group is subscribed (character, \"U\", \"K\", \"Z\" or \" \")
856 %L    Level of subscribedness (integer)
857 %N    Number of unread articles (integer)
858 %I    Number of dormant articles (integer)
859 %i    Number of ticked and dormant (integer)
860 %T    Number of ticked articles (integer)
861 %R    Number of read articles (integer)
862 %t    Total number of articles (integer)
863 %y    Number of unread, unticked articles (integer)
864 %G    Group name (string)
865 %g    Qualified group name (string)
866 %D    Group description (string)
867 %s    Select method (string)
868 %o    Moderated group (char, \"m\")
869 %O    Moderated group (string, \"(m)\" or \"\")
870 %n    Select from where (string)
871 %z    A string that look like `<%s:%n>' if a foreign select method is used
872 %u    User defined specifier. The next character in the format string should
873       be a letter.  Gnus will call the function gnus-user-format-function-X,
874       where X is the letter following %u. The function will be passed the
875       current header as argument. The function should return a string, which
876       will be inserted into the buffer just like information from any other
877       group specifier.
878
879 Text between %( and %) will be highlighted with `gnus-mouse-face' when
880 the mouse point move inside the area.  There can only be one such area.
881
882 Note that this format specification is not always respected. For
883 reasons of efficiency, when listing killed groups, this specification
884 is ignored altogether. If the spec is changed considerably, your
885 output may end up looking strange when listing both alive and killed
886 groups.
887
888 If you use %o or %O, reading the active file will be slower and quite
889 a bit of extra memory will be used. %D will also worsen performance.
890 Also note that if you change the format specification to include any
891 of these specs, you must probably re-start Gnus to see them go into
892 effect.") 
893
894 (defvar gnus-summary-line-format "%U%R%z%I%(%[%4L: %-20,20n%]%) %s\n"
895   "*The format specification of the lines in the summary buffer.
896
897 It works along the same lines as a normal formatting string,
898 with some simple extensions.
899
900 %N   Article number, left padded with spaces (string)
901 %S   Subject (string)
902 %s   Subject if it is at the root of a thread, and \"\" otherwise (string)
903 %n   Name of the poster (string)
904 %A   Address of the poster (string)
905 %F   Contents of the From: header (string)
906 %x   Contents of the Xref: header (string)
907 %D   Date of the article (string)
908 %d   Date of the article (string) in DD-MMM format
909 %M   Message-id of the article (string)
910 %r   References of the article (string)
911 %c   Number of characters in the article (integer)
912 %L   Number of lines in the article (integer)
913 %I   Indentation based on thread level (a string of spaces)
914 %T   A string with two possible values: 80 spaces if the article
915      is on thread level two or larger and 0 spaces on level one
916 %R   \"R\" if this article has been replied to, \" \" otherwise (character)
917 %U   Status of this article (character, \"D\", \"K\", \"-\" or \" \")
918 %[   Opening bracket (character, \"[\" or \"<\")
919 %]   Closing bracket (character, \"]\" or \">\")
920 %>   Spaces of length thread-level (string)
921 %<   Spaces of length (- 20 thread-level) (string)
922 %i   Article score (number)
923 %z   Article zcore (character)
924 %t   Number of articles under the current thread (number).
925 %e   Whether the thread is empty or not (character).
926 %u   User defined specifier. The next character in the format string should
927      be a letter.  Gnus will call the function gnus-user-format-function-X,
928      where X is the letter following %u. The function will be passed the
929      current header as argument. The function should return a string, which
930      will be inserted into the summary just like information from any other
931      summary specifier.
932
933 Text between %( and %) will be highlighted with `gnus-mouse-face'
934 when the mouse point is placed inside the area.  There can only be one
935 such area.
936
937 The %U (status), %R (replied) and %z (zcore) specs have to be handled
938 with care. For reasons of efficiency, Gnus will compute what column
939 these characters will end up in, and \"hard-code\" that. This means that
940 it is illegal to have these specs after a variable-length spec. Well,
941 you might not be arrested, but your summary buffer will look strange,
942 which is bad enough.
943
944 The smart choice is to have these specs as for to the left as
945 possible. 
946
947 This restriction may disappear in later versions of Gnus.")
948
949 (defvar gnus-summary-dummy-line-format "*   :                          : %S\n"
950   "*The format specification for the dummy roots in the summary buffer.
951 It works along the same lines as a normal formatting string,
952 with some simple extensions.
953
954 %S  The subject")
955
956 (defvar gnus-summary-mode-line-format "(ding) %G/%A %Z"
957   "*The format specification for the summary mode line.")
958
959 (defvar gnus-article-mode-line-format "(ding) %G/%A %S"
960   "*The format specification for the article mode line.")
961
962 (defvar gnus-group-mode-line-format "(ding) List of groups   {%M:%S}  "
963   "*The format specification for the group mode line.")
964
965 (defvar gnus-valid-select-methods
966   '(("nntp" post address prompt-address)
967     ("nnspool" post)
968     ("nnvirtual" none virtual prompt-address) 
969     ("nnmbox" mail respool) 
970     ("nnml" mail respool)
971     ("nnmh" mail respool) 
972     ("nndir" none prompt-address address)
973     ("nndigest" none) 
974     ("nndoc" none prompt-address) 
975     ("nnbabyl" mail respool) 
976     ("nnkiboze" none virtual) 
977     ("nnfolder" mail respool))
978   "An alist of valid select methods.
979 The first element of each list lists should be a string with the name
980 of the select method. The other elements may be be the category of
981 this method (ie. `post', `mail', `none' or whatever) or other
982 properties that this method has (like being respoolable).
983 If you implement a new select method, all you should have to change is
984 this variable. I think.")
985
986 (defvar gnus-updated-mode-lines '(group article summary)
987   "*List of buffers that should update their mode lines.
988 The list may contain the symbols `group', `article' and `summary'. If
989 the corresponding symbol is present, Gnus will keep that mode line
990 updated with information that may be pertinent. 
991 If this variable is nil, screen refresh may be quicker.")
992
993 ;; Added by Keinonen Kari <kk85613@cs.tut.fi>.
994 (defvar gnus-mode-non-string-length 21
995   "*Max length of mode-line non-string contents.
996 If this is nil, Gnus will take space as is needed, leaving the rest
997 of the modeline intact.")
998
999 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
1000 (defvar gnus-mouse-face 'highlight
1001   "*Face used for mouse highlighting in Gnus.
1002 No mouse highlights will be done if `gnus-visual' is nil.")
1003
1004 (defvar gnus-summary-mark-below nil
1005   "*Mark all articles with a score below this variable as read.
1006 This variable is local to each summary buffer and usually set by the
1007 score file.")  
1008
1009 (defvar gnus-thread-sort-functions '(gnus-thread-sort-by-number)
1010   "*List of functions used for sorting threads in the summary buffer.
1011 By default, threads are sorted by article number.
1012
1013 Each function takes two threads and return non-nil if the first thread
1014 should be sorted before the other.  If you use more than one function,
1015 the primary sort function should be the last.
1016
1017 Ready-mady functions include `gnus-thread-sort-by-number',
1018 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
1019 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
1020 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').")
1021
1022 (defvar gnus-thread-score-function '+
1023   "*Function used for calculating the total score of a thread.
1024
1025 The function is called with the scores of the article and each
1026 subthread and should then return the score of the thread.
1027
1028 Some functions you can use are `+', `max', or `min'.")
1029
1030 (defvar gnus-score-find-score-files-function 'gnus-score-find-bnews
1031   "*Function used to find SCORE files.
1032 The function will be called with the group name as the argument, and
1033 should return a list of score files to apply to that group.  The score
1034 files do not actually have to exist.
1035
1036 Predefined values are:
1037
1038 gnus-score-find-single: Only apply the group's own SCORE file.
1039 gnus-score-find-hierarchical: Also apply SCORE files from parent groups.
1040 gnus-score-find-bnews: Apply SCORE files whose names matches.
1041
1042 See the documentation to these functions for more information.
1043
1044 This variable can also be a list of functions to be called.  Each
1045 function should either return a list of score files, or a list of
1046 score alists.")
1047
1048 (defvar gnus-options-subscribe nil
1049   "*All new groups matching this regexp will be subscribed unconditionally.
1050 Note that this variable deals only with new newsgroups.  This variable
1051 does not affect old newsgroups.")
1052
1053 (defvar gnus-options-not-subscribe nil
1054   "*All new groups matching this regexp will be ignored.
1055 Note that this variable deals only with new newsgroups.  This variable
1056 does not affect old (already subscribed) newsgroups.")
1057
1058 (defvar gnus-auto-expirable-newsgroups nil
1059   "*Groups in which to automatically mark read articles as expirable.
1060 If non-nil, this should be a regexp that should match all groups in
1061 which to perform auto-expiry.  This only makes sense for mail groups.")
1062
1063
1064 ;; Hooks.
1065
1066 (defvar gnus-group-mode-hook nil
1067   "*A hook for Gnus group mode.")
1068
1069 (defvar gnus-summary-mode-hook nil
1070   "*A hook for Gnus summary mode.")
1071
1072 (defvar gnus-article-mode-hook nil
1073   "*A hook for Gnus article mode.")
1074
1075 (defvar gnus-open-server-hook nil
1076   "*A hook called just before opening connection to the news server.")
1077
1078 (defvar gnus-startup-hook nil
1079   "*A hook called at startup.
1080 This hook is called after Gnus is connected to the NNTP server.")
1081
1082 (defvar gnus-get-new-news-hook nil
1083   "*A hook run just before Gnus checks for new news.")
1084
1085 (defvar gnus-group-prepare-function 'gnus-group-prepare-flat
1086   "*A function that is called to generate the group buffer.
1087 The function is called with three arguments: The first is a number;
1088 all group with a level less or equal to that number should be listed,
1089 if the second is non-nil, empty groups should also be displayed. If
1090 the third is non-nil, it is a number. No groups with a level lower
1091 than this number should be displayed.
1092
1093 The only current function implemented is `gnus-group-prepare-flat'.")
1094
1095 (defvar gnus-group-prepare-hook nil
1096   "*A hook called after the group buffer has been generated.
1097 If you want to modify the group buffer, you can use this hook.")
1098
1099 (defvar gnus-summary-prepare-hook nil
1100   "*A hook called after the summary buffer has been generated.
1101 If you want to modify the summary buffer, you can use this hook.")
1102
1103 (defvar gnus-article-prepare-hook nil
1104   "*A hook called after an article has been prepared in the article buffer.
1105 If you want to run a special decoding program like nkf, use this hook.")
1106
1107 (defvar gnus-article-display-hook nil
1108   "*A hook called after the article is displayed in the article buffer.
1109 The hook is designed to change the contents of the article
1110 buffer. Typical functions that this hook may contain are
1111 `gnus-article-hide-headers' (hide selected headers),
1112 `gnus-article-hide-signature' (hide signature) and
1113 `gnus-article-treat-overstrike' (turn \"^H_\" into bold characters).")
1114 (add-hook 'gnus-article-display-hook 'gnus-article-hide-headers-if-wanted)
1115 (add-hook 'gnus-article-display-hook 'gnus-article-treat-overstrike)
1116
1117 (defvar gnus-article-x-face-command
1118   "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | xv -"
1119   "String or function to be executed to display an X-Face header.
1120 If it is a string, the command will be executed in a sub-shell
1121 asynchronously. The compressed face will be piped to this command.") 
1122
1123 (defvar gnus-select-group-hook nil
1124   "*A hook called when a newsgroup is selected.
1125
1126 If you'd like to simplify subjects like the
1127 `gnus-summary-next-same-subject' command does, you can use the
1128 following hook:
1129
1130  (setq gnus-select-group-hook
1131       (list
1132         (lambda ()
1133           (mapcar (lambda (header)
1134                      (header-set-subject
1135                       header
1136                       (gnus-simplify-subject
1137                        (header-subject header) 're-only)))
1138                   gnus-newsgroup-headers))))")
1139
1140 (defvar gnus-select-article-hook
1141   '(gnus-summary-show-thread)
1142   "*A hook called when an article is selected.
1143 The default hook shows conversation thread subtrees of the selected
1144 article automatically using `gnus-summary-show-thread'.")
1145
1146 (defvar gnus-apply-kill-hook '(gnus-apply-kill-file)
1147   "*A hook called to apply kill files to a group.
1148 This hook is intended to apply a kill file to the selected newsgroup.
1149 The function `gnus-apply-kill-file' is called by default.
1150
1151 Since a general kill file is too heavy to use only for a few
1152 newsgroups, I recommend you to use a lighter hook function. For
1153 example, if you'd like to apply a kill file to articles which contains
1154 a string `rmgroup' in subject in newsgroup `control', you can use the
1155 following hook:
1156
1157 \(setq gnus-apply-kill-hook
1158       (list
1159         (lambda ()
1160           (cond ((string-match \"control\" gnus-newsgroup-name)
1161                  (gnus-kill \"Subject\" \"rmgroup\")
1162                  (gnus-expunge \"X\"))))))")
1163
1164 (defvar gnus-visual-mark-article-hook 
1165   (list 'gnus-visual-highlight-selected-summary)
1166   "*Hook run after selecting an article in the summary buffer.
1167 It is meant to be used for highlighting the article in some way.  It
1168 is not run if `gnus-visual' is nil.")
1169
1170 (defvar gnus-prepare-article-hook (list 'gnus-inews-insert-signature)
1171   "*A hook called after preparing body, but before preparing header headers.
1172 The default hook (`gnus-inews-insert-signature') inserts a signature
1173 file specified by the variable `gnus-signature-file'.")
1174
1175 (defvar gnus-inews-article-hook (list 'gnus-inews-do-fcc)
1176   "*A hook called before finally posting an article.
1177 The default hook (`gnus-inews-do-fcc') does FCC processing (ie. saves
1178 the article to a file).")
1179
1180 (defvar gnus-inews-article-header-hook nil
1181   "*A hook called after inserting the headers in an article to be posted.
1182 The hook is called from the *post-news* buffer, narrowed to the
1183 headers.")
1184
1185 (defvar gnus-exit-group-hook nil
1186   "*A hook called when exiting (not quitting) summary mode.")
1187
1188 (defvar gnus-suspend-gnus-hook nil
1189   "*A hook called when suspending (not exiting) Gnus.")
1190
1191 (defvar gnus-exit-gnus-hook nil
1192   "*A hook called when exiting Gnus.")
1193
1194 (defvar gnus-save-newsrc-hook nil
1195   "*A hook called when saving the newsrc file.")
1196
1197 (defvar gnus-visual-summary-update-hook 
1198   (list 'gnus-visual-summary-highlight-line)
1199   "*A hook called when a summary line is changed.
1200 The hook will not be called if `gnus-visual' is nil.
1201
1202 The default function `gnus-visual-summary-highlight-line' will
1203 highlight the line according to the `gnus-visual-summary-highlight'
1204 variable.")
1205
1206 (defvar gnus-mark-article-hook (list 'gnus-summary-mark-unread-as-read)
1207   "*A hook called when an article is selected for the first time.
1208 The hook is intended to mark an article as read (or unread)
1209 automatically when it is selected.")
1210
1211 \f
1212 ;; Internal variables
1213
1214 (defvar gnus-internal-global-score-files nil)
1215 (defvar gnus-score-file-list nil)
1216
1217 ;; Avoid highlighting in kill files.
1218 (defvar gnus-summary-inhibit-highlight nil)
1219 (defvar gnus-newsgroup-selected-overlay nil)
1220
1221 (defvar gnus-article-mode-map nil)
1222 (defvar caesar-translate-table nil)
1223 (defvar gnus-dribble-buffer nil)
1224 (defvar gnus-headers-retrieved-by nil)
1225 (defvar gnus-article-reply nil)
1226 (defvar gnus-override-method nil)
1227 (defvar gnus-article-check-size nil)
1228
1229 (defvar gnus-current-move-group nil)
1230
1231 (defvar gnus-newsgroup-dependencies nil)
1232 (defvar gnus-newsgroup-threads nil)
1233 (defvar gnus-newsgroup-async nil)
1234 (defconst gnus-group-edit-buffer "*Gnus edit newsgroup*")
1235
1236 (defconst gnus-group-line-format-alist
1237   (list (list ?M 'marked ?c)
1238         (list ?S 'subscribed ?c)
1239         (list ?L 'level ?d)
1240         (list ?N 'number ?s)
1241         (list ?I 'number-of-dormant ?d)
1242         (list ?T 'number-of-ticked ?d)
1243         (list ?R 'number-of-read ?s)
1244         (list ?t 'number-total ?d)
1245         (list ?y 'number-of-unread-unticked ?s)
1246         (list ?i 'number-of-ticked-and-dormant ?d)
1247         (list ?g 'group ?s)
1248         (list ?G 'qualified-group ?s)
1249         (list ?D 'newsgroup-description ?s)
1250         (list ?o 'moderated ?c)
1251         (list ?O 'moderated-string ?s)
1252         (list ?s 'news-server ?s)
1253         (list ?n 'news-method ?s)
1254         (list ?z 'news-method-string ?s)
1255         (list ?u 'user-defined ?s)))
1256
1257 (defconst gnus-summary-line-format-alist 
1258   (list (list ?N 'number ?s)
1259         (list ?S 'subject ?s)
1260         (list ?s 'subject-or-nil ?s)
1261         (list ?n 'name ?s)
1262         (list ?A 'address ?s)
1263         (list ?F 'from ?s)
1264         (list ?x (macroexpand '(header-xref header)) ?s)
1265         (list ?D (macroexpand '(header-date header)) ?s)
1266         (list ?d '(gnus-dd-mmm (header-date header)) ?s)
1267         (list ?M (macroexpand '(header-id header)) ?s)
1268         (list ?r (macroexpand '(header-references header)) ?s)
1269         (list ?c (macroexpand '(header-chars header)) ?d)
1270         (list ?L 'lines ?d)
1271         (list ?I 'indentation ?s)
1272         (list ?T '(if (< level 1) "" (make-string (frame-width) ? )) ?s)
1273         (list ?R 'replied ?c)
1274         (list ?\[ 'opening-bracket ?c)
1275         (list ?\] 'closing-bracket ?c)
1276         (list ?\> '(make-string level ? ) ?s)
1277         (list ?\< '(make-string (max 0 (- 20 level)) ? ) ?s)
1278         (list ?i 'score ?s)
1279         (list ?z 'score-char ?c)
1280         (list ?U 'unread ?c)
1281         (list ?t '(gnus-summary-number-of-articles-in-thread 
1282                    (if (boundp 'thread) (symbol-value 'thread)) nil) ?d)
1283         (list ?e '(gnus-summary-number-of-articles-in-thread 
1284                    (if (boundp 'thread) (symbol-value 'thread)) t) ?c)
1285         (list ?u 'user-defined ?s))
1286   "An alist of format specifications that can appear in summary lines,
1287 and what variables they correspond with, along with the type of the
1288 variable (string, integer, character, etc).")
1289
1290 (defconst gnus-summary-dummy-line-format-alist
1291   (list (list ?S 'subject ?s)
1292         (list ?N 'number ?d)
1293         (list ?u 'user-defined ?s)))
1294
1295 (defconst gnus-summary-mode-line-format-alist 
1296   (list (list ?G 'group-name ?s)
1297         (list ?g '(gnus-short-group-name group-name) ?s)
1298         (list ?A 'article-number ?d)
1299         (list ?Z 'unread-and-unselected ?s)
1300         (list ?V 'gnus-version ?s)
1301         (list ?U 'unread ?d)
1302         (list ?S 'subject ?s)
1303         (list ?e 'unselected ?d)
1304         (list ?u 'user-defined ?s)
1305         (list ?s '(gnus-current-score-file-nondirectory) ?s)))
1306
1307 (defconst gnus-group-mode-line-format-alist 
1308   (list (list ?S 'news-server ?s)
1309         (list ?M 'news-method ?s)
1310         (list ?u 'user-defined ?s)))
1311
1312 (defvar gnus-have-read-active-file nil)
1313
1314 (defconst gnus-maintainer "Lars Magne Ingebrigtsen <larsi@ifi.uio.no>"
1315   "The mail address of the Gnus maintainer.")
1316
1317 (defconst gnus-version "(ding) Gnus v0.61"
1318   "Version number for this version of Gnus.")
1319
1320 (defvar gnus-info-nodes
1321   '((gnus-group-mode            "(gnus)The Group Buffer")
1322     (gnus-summary-mode          "(gnus)The Summary Buffer")
1323     (gnus-article-mode          "(gnus)The Article Buffer"))
1324   "Assoc list of major modes and related Info nodes.")
1325
1326 (defvar gnus-documentation-group-file "~/dgnus/lisp/doc.txt"
1327   "The location of the (ding) Gnus documentation group.")
1328
1329 (defvar gnus-group-buffer "*Group*")
1330 (defvar gnus-summary-buffer "*Summary*")
1331 (defvar gnus-article-buffer "*Article*")
1332 (defvar gnus-server-buffer "*Server*")
1333
1334 (defvar gnus-work-buffer " *gnus work*")
1335
1336 (defvar gnus-buffer-list nil
1337   "Gnus buffers that should be killed on exit.")
1338
1339 (defvar gnus-server-alist nil
1340   "List of available servers.")
1341
1342 (defvar gnus-variable-list
1343   '(gnus-newsrc-options gnus-newsrc-options-n
1344     gnus-newsrc-last-checked-date 
1345     gnus-newsrc-alist gnus-server-alist
1346     gnus-killed-list gnus-zombie-list)
1347   "Gnus variables saved in the quick startup file.")
1348
1349 (defvar gnus-overload-functions
1350   '((news-inews gnus-inews-news "rnewspost")
1351     (caesar-region gnus-caesar-region "rnews"))
1352   "Functions overloaded by gnus.
1353 It is a list of `(original overload &optional file)'.")
1354
1355 (defvar gnus-newsrc-options nil
1356   "Options line in the .newsrc file.")
1357
1358 (defvar gnus-newsrc-options-n nil
1359   "List of regexps representing groups to be subscribed/ignored unconditionally.") 
1360
1361 (defvar gnus-newsrc-last-checked-date nil
1362   "Date Gnus last asked server for new newsgroups.")
1363
1364 (defvar gnus-newsrc-alist nil
1365   "Assoc list of read articles.
1366 gnus-newsrc-hashtb should be kept so that both hold the same information.")
1367
1368 (defvar gnus-newsrc-hashtb nil
1369   "Hashtable of gnus-newsrc-alist.")
1370
1371 (defvar gnus-killed-list nil
1372   "List of killed newsgroups.")
1373
1374 (defvar gnus-killed-hashtb nil
1375   "Hash table equivalent of gnus-killed-list.")
1376
1377 (defvar gnus-zombie-list nil
1378   "List of almost dead newsgroups.")
1379
1380 (defvar gnus-description-hashtb nil
1381   "Descriptions of newsgroups.")
1382
1383 (defvar gnus-list-of-killed-groups nil
1384   "List of newsgroups that have recently been killed by the user.")
1385
1386 (defvar gnus-active-hashtb nil
1387   "Hashtable of active articles.")
1388
1389 (defvar gnus-moderated-list nil
1390   "List of moderated newsgroups.")
1391
1392 (defvar gnus-current-startup-file nil
1393   "Startup file for the current host.")
1394
1395 (defvar gnus-last-search-regexp nil
1396   "Default regexp for article search command.")
1397
1398 (defvar gnus-last-shell-command nil
1399   "Default shell command on article.")
1400
1401 (defvar gnus-current-select-method nil
1402   "The current method for selecting a newsgroup.")
1403
1404 (defvar gnus-have-all-newsgroups nil)
1405
1406 (defvar gnus-article-internal-prepare-hook nil)
1407
1408 (defvar gnus-newsgroup-name nil)
1409 (defvar gnus-newsgroup-begin nil)
1410 (defvar gnus-newsgroup-end nil)
1411 (defvar gnus-newsgroup-last-rmail nil)
1412 (defvar gnus-newsgroup-last-mail nil)
1413 (defvar gnus-newsgroup-last-folder nil)
1414 (defvar gnus-newsgroup-last-file nil)
1415 (defvar gnus-newsgroup-auto-expire nil)
1416 (defvar gnus-newsgroup-active nil)
1417
1418 (defvar gnus-newsgroup-unreads nil
1419   "List of unread articles in the current newsgroup.")
1420
1421 (defvar gnus-newsgroup-unselected nil
1422   "List of unselected unread articles in the current newsgroup.")
1423
1424 (defvar gnus-newsgroup-marked nil
1425   "List of ticked articles in the current newsgroup (a subset of unread art).")
1426
1427 (defvar gnus-newsgroup-killed nil
1428   "List of ranges of articles that have been through the scoring process.")
1429
1430 (defvar gnus-newsgroup-kill-headers nil)
1431
1432 (defvar gnus-newsgroup-replied nil
1433   "List of articles that have been replied to in the current newsgroup.")
1434
1435 (defvar gnus-newsgroup-expirable nil
1436   "List of articles in the current newsgroup that can be expired.")
1437
1438 (defvar gnus-newsgroup-processable nil
1439   "List of articles in the current newsgroup that can be processed.")
1440
1441 (defvar gnus-newsgroup-bookmarks nil
1442   "List of articles in the current newsgroup that have bookmarks.")
1443
1444 (defvar gnus-newsgroup-dormant nil
1445   "List of dormant articles in the current newsgroup.")
1446
1447 (defvar gnus-newsgroup-scored nil
1448   "List of scored articles in the current newsgroup.")
1449
1450 (defvar gnus-newsgroup-headers nil
1451   "List of article headers in the current newsgroup.")
1452 (defvar gnus-newsgroup-headers-hashtb-by-number nil)
1453
1454 (defvar gnus-newsgroup-ancient nil
1455   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1456
1457 (defvar gnus-current-article nil)
1458 (defvar gnus-article-current nil)
1459 (defvar gnus-current-headers nil)
1460 (defvar gnus-have-all-headers nil)
1461 (defvar gnus-last-article nil)
1462 (defvar gnus-newsgroup-history nil)
1463 (defvar gnus-current-kill-article nil)
1464
1465 ;; Save window configuration.
1466 (defvar gnus-winconf-edit-group nil)
1467
1468 ;; Format specs
1469 (defvar gnus-summary-line-format-spec nil)
1470 (defvar gnus-summary-dummy-line-format-spec nil)
1471 (defvar gnus-group-line-format-spec nil)
1472 (defvar gnus-summary-mode-line-format-spec nil)
1473 (defvar gnus-article-mode-line-format-spec nil)
1474 (defvar gnus-group-mode-line-format-spec nil)
1475 (defvar gnus-summary-mark-positions nil)
1476
1477 (defvar gnus-summary-expunge-below nil)
1478 (defvar gnus-reffed-article-number nil)
1479
1480 (defvar rmail-default-file (expand-file-name "~/XMBOX"))
1481 (defvar rmail-default-rmail-file (expand-file-name "~/XNEWS"))
1482
1483 (defconst gnus-summary-local-variables 
1484   '(gnus-newsgroup-name 
1485     gnus-newsgroup-begin gnus-newsgroup-end 
1486     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail 
1487     gnus-newsgroup-last-folder gnus-newsgroup-last-file 
1488     gnus-newsgroup-auto-expire gnus-newsgroup-unreads 
1489     gnus-newsgroup-unselected gnus-newsgroup-marked
1490     gnus-newsgroup-replied gnus-newsgroup-expirable
1491     gnus-newsgroup-processable gnus-newsgroup-killed
1492     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1493     gnus-newsgroup-headers gnus-newsgroup-headers-hashtb-by-number
1494     gnus-current-article gnus-current-headers gnus-have-all-headers
1495     gnus-last-article gnus-article-internal-prepare-hook
1496     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1497     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1498     gnus-newsgroup-threads gnus-newsgroup-async
1499     gnus-score-alist gnus-current-score-file gnus-summary-expunge-below 
1500     gnus-summary-mark-below gnus-newsgroup-active gnus-scores-exclude-files
1501     gnus-newsgroup-history gnus-newsgroup-ancient)
1502   "Variables that are buffer-local to the summary buffers.")
1503
1504 ;;; End of variables.
1505
1506 ;; Define some autoload functions Gnus might use.
1507 (eval-and-compile
1508   (autoload 'metamail-buffer "metamail")
1509   (autoload 'Info-goto-node "info")
1510   (autoload 'hexl-hex-string-to-integer "hexl")
1511   
1512   (autoload 'timezone-make-date-arpa-standard "timezone")
1513   (autoload 'timezone-fix-time "timezone")
1514   (autoload 'timezone-make-sortable-date "timezone")
1515   (autoload 'timezone-make-time-string "timezone")
1516   
1517   (autoload 'rmail-output "rmailout")
1518   (autoload 'mail-position-on-field "sendmail")
1519   (autoload 'mail-setup "sendmail")
1520   (autoload 'news-mail-other-window "rnewspost")
1521   (autoload 'news-reply-yank-original "rnewspost")
1522   (autoload 'news-caesar-buffer-body "rnewspost")
1523
1524   (autoload 'rmail-insert-rmail-file-header "rmail")
1525   (autoload 'rmail-count-new-messages "rmail")
1526   (autoload 'rmail-show-message "rmail")
1527
1528   (autoload 'gnus-mail-reply-using-mhe "gnus-mh")
1529   (autoload 'gnus-mail-forward-using-mhe "gnus-mh")
1530   (autoload 'gnus-mail-other-window-using-mhe "gnus-mh")
1531   (autoload 'gnus-summary-save-in-folder "gnus-mh")
1532   (autoload 'gnus-Folder-save-name "gnus-mh")
1533   (autoload 'gnus-folder-save-name "gnus-mh")
1534   
1535   (autoload 'gnus-group-make-menu-bar "gnus-vis")
1536   (autoload 'gnus-summary-make-menu-bar "gnus-vis")
1537   (autoload 'gnus-article-make-menu-bar "gnus-vis")
1538   (autoload 'gnus-visual-highlight-selected-summary "gnus-vis")
1539   (autoload 'gnus-visual-summary-highlight-line "gnus-vis")
1540
1541   (autoload 'gnus-uu-mark-by-regexp "gnus-uu" nil t)
1542   (autoload 'gnus-uu-mark-region "gnus-uu" nil t)
1543   (autoload 'gnus-uu-mark-thread "gnus-uu" nil t)
1544   (autoload 'gnus-uu-mark-sparse "gnus-uu" nil t)
1545   (autoload 'gnus-uu-mark-series "gnus-uu" nil t)
1546   (autoload 'gnus-uu-mark-all "gnus-uu" nil t)
1547   (autoload 'gnus-uu-post-news "gnus-uu" nil t)
1548   (autoload 'gnus-uu-digest-mail-forward "gnus-uu" nil t)
1549   (autoload 'gnus-uu-digest-post-forward "gnus-uu" nil t)
1550
1551   (autoload 'gnus-uu-decode-uu "gnus-uu" nil t)
1552   (autoload 'gnus-uu-decode-uu-and-save "gnus-uu" nil t)
1553   (autoload 'gnus-uu-decode-unshar "gnus-uu" nil t)
1554   (autoload 'gnus-uu-decode-unshar-and-save "gnus-uu" nil t)
1555   (autoload 'gnus-uu-decode-save "gnus-uu" nil t)
1556   (autoload 'gnus-uu-decode-binhex "gnus-uu" nil t)
1557   (autoload 'gnus-uu-decode-uu-view "gnus-uu" nil t)
1558   (autoload 'gnus-uu-decode-uu-and-save-view "gnus-uu" nil t)
1559   (autoload 'gnus-uu-decode-unshar-view "gnus-uu" nil t)
1560   (autoload 'gnus-uu-decode-unshar-and-save-view "gnus-uu" nil t)
1561   (autoload 'gnus-uu-decode-save-view "gnus-uu" nil t)
1562   (autoload 'gnus-uu-decode-binhex-view "gnus-uu" nil t)
1563   (autoload 'gnus-uu-decode-postscript "gnus-uu" nil t)
1564   (autoload 'gnus-uu-decode-postscript-and-save "gnus-uu" nil t)
1565   (autoload 'gnus-uu-decode-postscript-view "gnus-uu" nil t)
1566   (autoload 'gnus-uu-decode-postscript-and-save-view "gnus-uu" nil t)
1567
1568   (autoload 'gnus-kill "gnus-kill")
1569   (autoload 'gnus-apply-kill-file-internal "gnus-kill")
1570   (autoload 'gnus-kill-file-edit-file "gnus-kill")
1571   (autoload 'gnus-kill-file-raise-followups-to-author "gnus-kill")
1572   (autoload 'gnus-execute "gnus-kill")
1573   (autoload 'gnus-expunge "gnus-kill")
1574
1575   (autoload 'pp "pp")
1576   (autoload 'pp-to-string "pp")
1577   (autoload 'mail-extract-address-components "mail-extr")
1578
1579   (autoload 'gnus-summary-increase-map "gnus-score" nil nil 'keymap)
1580   (autoload 'gnus-summary-lower-map "gnus-score" nil nil 'keymap)
1581   (autoload 'gnus-summary-score-map "gnus-score" nil nil 'keymap)
1582   (autoload 'gnus-score-save "gnus-score")
1583   (autoload 'gnus-score-headers "gnus-score")
1584   (autoload 'gnus-current-score-file-nondirectory "gnus-score")
1585   (autoload 'gnus-score-adaptive "gnus-score")
1586   (autoload 'gnus-score-remove-lines-adaptive "gnus-score")
1587
1588   (autoload 'gnus-summary-send-map "gnus-msg" nil nil 'keymap)
1589   (autoload 'gnus-group-post-news "gnus-msg" nil t)
1590   (autoload 'gnus-summary-post-news "gnus-msg" nil t)
1591   (autoload 'gnus-summary-followup "gnus-msg" nil t)
1592   (autoload 'gnus-summary-followup-with-original "gnus-msg" nil t)
1593   (autoload 'gnus-summary-followup-and-reply "gnus-msg" nil t)
1594   (autoload 'gnus-summary-followup-and-reply-with-original "gnus-msg" nil t)
1595   (autoload 'gnus-summary-cancel-article "gnus-msg" nil t)
1596   (autoload 'gnus-summary-supersede-article "gnus-msg" nil t)
1597   (autoload 'gnus-post-news "gnus-msg" nil t)
1598   (autoload 'gnus-inews-news "gnus-msg" nil t)
1599   (autoload 'gnus-cancel-news "gnus-msg" nil t)
1600   (autoload 'gnus-summary-reply "gnus-msg" nil t)
1601   (autoload 'gnus-summary-reply-with-original "gnus-msg" nil t)
1602   (autoload 'gnus-summary-mail-forward "gnus-msg" nil t)
1603   (autoload 'gnus-summary-mail-other-window "gnus-msg" nil t)
1604   (autoload 'gnus-mail-reply-using-mail "gnus-msg")
1605   (autoload 'gnus-mail-yank-original "gnus-msg")
1606   (autoload 'gnus-mail-send-and-exit "gnus-msg")
1607   (autoload 'gnus-mail-forward-using-mail "gnus-msg")
1608   (autoload 'gnus-mail-other-window-using-mail "gnus-msg")
1609
1610   )
1611
1612 \f
1613
1614 ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
1615 ;; If you want the cursor to go somewhere else, set these two
1616 ;; functions in some startup hook to whatever you want.
1617 (defalias 'gnus-summary-position-cursor 'gnus-goto-colon)
1618 (defalias 'gnus-group-position-cursor 'gnus-goto-colon)
1619
1620 ;; Cruft to make Gnus work under GNU XEmacs.
1621 (defconst gnus-xemacs (not (not (string-match "XEmacs\\|Lucid" emacs-version)))
1622   "Non-nil if Gnus is running under GNU XEmacs.")
1623
1624 (if gnus-xemacs
1625     (progn
1626       (or (memq 'underline (funcall 'list-faces))
1627           (make-face 'underline))
1628       (or (face-differs-from-default-p 'underline)
1629           (set-face-underline-p 'underline t))
1630       (or (fboundp 'set-text-properties)
1631           (defun set-text-properties (start end props &optional buffer)
1632             (if props
1633                 (put-text-property start end (car props) 
1634                                    (funcall (intern "cadr") props) buffer)
1635               (remove-text-properties start end ()))))))
1636
1637 ;;; Various macros and substs.
1638
1639 (defmacro gnus-eval-in-buffer-window (buffer &rest forms)
1640   "Pop to BUFFER, evaluate FORMS, and then returns to original window."
1641   (` (let ((GnusStartBufferWindow (selected-window)))
1642        (unwind-protect
1643            (progn
1644              (pop-to-buffer (, buffer))
1645              (,@ forms))
1646          (select-window GnusStartBufferWindow)))))
1647
1648 (defmacro gnus-gethash (string hashtable)
1649   "Get hash value of STRING in HASHTABLE."
1650   ;;(` (symbol-value (abbrev-symbol (, string) (, hashtable))))
1651   ;;(` (abbrev-expansion (, string) (, hashtable)))
1652   (` (symbol-value (intern-soft (, string) (, hashtable)))))
1653
1654 (defmacro gnus-sethash (string value hashtable)
1655   "Set hash value. Arguments are STRING, VALUE, and HASHTABLE."
1656   ;; We cannot use define-abbrev since it only accepts string as value.
1657   ;; (set (intern string hashtable) value))
1658   (` (set (intern (, string) (, hashtable)) (, value))))
1659
1660 (defsubst gnus-buffer-substring (beg end)
1661   (buffer-substring (match-beginning beg) (match-end end)))
1662
1663 (defsubst gnus-simplify-subject-re (subject)
1664   "Remove \"Re:\" from subject lines."
1665   (let ((case-fold-search t))
1666     (if (string-match "^re: *" subject)
1667         (substring subject (match-end 0))
1668       subject)))
1669
1670 (defsubst gnus-goto-char (point)
1671   (and point (goto-char point)))
1672
1673 (defmacro gnus-buffer-exists-p (buffer)
1674   (` (and (, buffer)
1675           (funcall (if (stringp (, buffer)) 'get-buffer 'buffer-name)
1676                    (, buffer)))))
1677
1678 (defmacro gnus-kill-buffer (buffer)
1679   (` (if (gnus-buffer-exists-p (, buffer))
1680          (kill-buffer (, buffer)))))
1681
1682 (defsubst gnus-point-at-bol ()
1683   "Return point at the beginning of line."
1684   (let ((p (point)))
1685     (beginning-of-line)
1686     (prog1
1687         (point)
1688       (goto-char p))))
1689
1690 (defsubst gnus-point-at-eol ()
1691   "Return point at the beginning of line."
1692   (let ((p (point)))
1693     (end-of-line)
1694     (prog1
1695         (point)
1696       (goto-char p))))
1697
1698 \f
1699 ;;;
1700 ;;; Gnus Utility Functions
1701 ;;;
1702
1703 (defun gnus-extract-address-components (from)
1704   (let (name address)
1705     (if (string-match "\\b[^@ \t<>]+[!@][^@ \t<>]+\\b" from)
1706         (setq address (substring from (match-beginning 0) (match-end 0))))
1707     (and address
1708          (string-match (concat "<" (regexp-quote address) ">") from)
1709          (setq name (substring from 0 (1- (match-beginning 0)))))
1710     (or name
1711         (and (string-match "(.+)" from)
1712              (setq name (substring from (1+ (match-beginning 0)) 
1713                                    (1- (match-end 0))))))
1714     ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
1715     (list (or name from) (or address from))))
1716
1717 (defun gnus-fetch-field (field)
1718   "Return the value of the header FIELD of current article."
1719   (save-excursion
1720     (save-restriction
1721       (gnus-narrow-to-headers)
1722       (mail-fetch-field field))))
1723
1724 (defun gnus-goto-colon ()
1725   (beginning-of-line)
1726   (search-forward ":" (gnus-point-at-eol) t))
1727
1728 (defun gnus-narrow-to-headers ()
1729   (widen)
1730   (save-excursion
1731     (goto-char 1)
1732     (if (search-forward "\n\n")
1733         (narrow-to-region 1 (1- (point))))))
1734
1735 (defun gnus-update-format-specifications ()
1736   (setq gnus-summary-line-format-spec 
1737         (gnus-parse-format
1738          gnus-summary-line-format gnus-summary-line-format-alist))
1739   (gnus-update-summary-mark-positions)
1740   (setq gnus-summary-dummy-line-format-spec 
1741         (gnus-parse-format gnus-summary-dummy-line-format 
1742                            gnus-summary-dummy-line-format-alist))
1743   (setq gnus-group-line-format-spec
1744         (gnus-parse-format 
1745          gnus-group-line-format 
1746          gnus-group-line-format-alist))
1747   (if (and (string-match "%D" gnus-group-line-format)
1748            (not gnus-description-hashtb)
1749            gnus-read-active-file)
1750       (gnus-read-descriptions-file))
1751   (setq gnus-summary-mode-line-format-spec 
1752         (gnus-parse-format gnus-summary-mode-line-format 
1753                            gnus-summary-mode-line-format-alist))
1754   (setq gnus-article-mode-line-format-spec 
1755         (gnus-parse-format gnus-article-mode-line-format 
1756                            gnus-summary-mode-line-format-alist))
1757   (setq gnus-group-mode-line-format-spec 
1758         (gnus-parse-format gnus-group-mode-line-format 
1759                            gnus-group-mode-line-format-alist)))
1760
1761 (defun gnus-update-summary-mark-positions ()
1762   (save-excursion
1763     (let ((gnus-replied-mark 129)
1764           (gnus-score-below-mark 130)
1765           (gnus-score-over-mark 130)
1766           (thread nil)
1767           pos)
1768       (gnus-set-work-buffer)
1769       (gnus-summary-insert-line 
1770        nil [0 "" "" "" "" "" 0 0 ""]  0 nil 128 t nil "" nil 1)
1771       (goto-char (point-min))
1772       (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
1773                                          (- (point) 2)))))
1774       (goto-char (point-min))
1775       (setq pos (cons (cons 'replied (and (search-forward "\201" nil t)
1776                                           (- (point) 2))) pos))
1777       (goto-char (point-min))
1778       (setq pos (cons (cons 'score (and (search-forward "\202" nil t)
1779                                         (- (point) 2))) pos))
1780       (setq gnus-summary-mark-positions pos))))
1781
1782 (defun gnus-format-max-width (var length)
1783   (let (result
1784         (val (eval var)))
1785     (if (> (length (setq result (if (numberp val) (int-to-string val) val))) 
1786            length)
1787         (format "%s" (substring result 0 length))
1788       (format "%s" result))))
1789
1790 (defun gnus-set-mouse-face (string)
1791   ;; Set mouse face property on STRING.
1792   (or gnus-xemacs
1793       (put-text-property 0 (length string) 'mouse-face gnus-mouse-face string))
1794   string)
1795
1796 (defun gnus-parse-format (format spec-alist)
1797   ;; This function parses the FORMAT string with the help of the
1798   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
1799   ;; string.  If the FORMAT string contains the specifiers %( and %)
1800   ;; the text between them will have the mouse-face text property.
1801   (if (string-match "\\`\\(.*\\)%(\\(.*\\)%)\\(.*\n?\\)\\'" format)
1802       (if (and gnus-visual gnus-mouse-face)
1803           (let ((pre (substring format (match-beginning 1) (match-end 1)))
1804                 (button (substring format (match-beginning 2) (match-end 2)))
1805                 (post (substring format (match-beginning 3) (match-end 3))))
1806             (list 'concat
1807                   (gnus-parse-simple-format pre spec-alist)
1808                   (list 'gnus-set-mouse-face
1809                         (gnus-parse-simple-format button spec-alist))
1810                   (gnus-parse-simple-format post spec-alist)))
1811         (gnus-parse-simple-format
1812          (concat (substring format (match-beginning 1) (match-end 1))
1813                  (substring format (match-beginning 2) (match-end 2))
1814                  (substring format (match-beginning 3) (match-end 3)))
1815          spec-alist))
1816     (gnus-parse-simple-format format spec-alist)))
1817
1818 (defun gnus-parse-simple-format (format spec-alist)
1819   ;; This function parses the FORMAT string with the help of the
1820   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
1821   ;; string. The list will consist of the symbol `format', a format
1822   ;; specification string, and a list of forms depending on the
1823   ;; SPEC-ALIST.
1824   (let ((max-width 0)
1825         spec flist fstring b newspec max-width elem beg)
1826     (save-excursion
1827       (gnus-set-work-buffer)
1828       (insert format)
1829       (goto-char 1)
1830       (while (re-search-forward "%[-0-9]*\\(,[0-9]*\\)*\\(.\\)\\(.\\)?" nil t)
1831         (setq spec (string-to-char (buffer-substring (match-beginning 2)
1832                                                      (match-end 2))))
1833         ;; First check if there are any specs that look anything like
1834         ;; "%12,12A", ie. with a "max width specification". These have
1835         ;; to be treated specially.
1836         (if (setq beg (match-beginning 1))
1837             (setq max-width 
1838                   (string-to-int 
1839                    (buffer-substring (1+ (match-beginning 1)) (match-end 1))))
1840           (setq max-width 0)
1841           (setq beg (match-beginning 2)))
1842         ;; Find the specification from `spec-alist'.
1843         (if (not (setq elem (cdr (assq spec spec-alist))))
1844             (setq elem '("*" ?s)))
1845         ;; Treat user defined format specifiers specially
1846         (and (eq (car elem) 'user-defined)
1847              (setq elem
1848                    (list 
1849                     (list (intern (concat "gnus-user-format-function-"
1850                                           (buffer-substring
1851                                            (match-beginning 3)
1852                                            (match-end 3))))
1853                           'header)
1854                     ?s))
1855              (delete-region (match-beginning 3) (match-end 3)))
1856         (if (not (zerop max-width))
1857             (let ((el (car elem)))
1858               (cond ((= (car (cdr elem)) ?c) 
1859                      (setq el (list 'char-to-string el)))
1860                     ((= (car (cdr elem)) ?d)
1861                      (numberp el) (setq el (list 'int-to-string el))))
1862               (setq flist (cons (list 'gnus-format-max-width 
1863                                       el max-width) 
1864                                 flist))
1865               (setq newspec ?s))
1866           (setq flist (cons (car elem) flist))
1867           (setq newspec (car (cdr elem))))
1868         ;; Remove the old specification (and possibly a ",12" string).
1869         (delete-region beg (match-end 2))
1870         ;; Insert the new specification.
1871         (goto-char beg)
1872         (insert newspec))
1873       (setq fstring (buffer-substring 1 (point-max))))
1874     (cons 'format (cons fstring (nreverse flist)))))
1875
1876 ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
1877 (defun gnus-read-init-file ()
1878   (and gnus-init-file
1879        (or (and (file-exists-p gnus-init-file) 
1880                 ;; Don't try to load a directory.
1881                 (not (file-directory-p gnus-init-file)))
1882            (file-exists-p (concat gnus-init-file ".el"))
1883            (file-exists-p (concat gnus-init-file ".elc")))
1884        (load gnus-init-file nil t)))
1885
1886 (defun gnus-set-work-buffer ()
1887   (if (get-buffer gnus-work-buffer)
1888       (progn
1889         (set-buffer gnus-work-buffer)
1890         (gnus-add-current-to-buffer-list)
1891         (erase-buffer))
1892     (set-buffer (get-buffer-create gnus-work-buffer))
1893     (buffer-disable-undo (current-buffer))))
1894
1895 ;; Article file names when saving.
1896
1897 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
1898   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1899 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group/num.
1900 Otherwise, it is like ~/News/news/group/num."
1901   (let ((default
1902           (expand-file-name
1903            (concat (if (gnus-use-long-file-name 'not-save)
1904                        (gnus-capitalize-newsgroup newsgroup)
1905                      (gnus-newsgroup-directory-form newsgroup))
1906                    "/" (int-to-string (header-number headers)))
1907            (or gnus-article-save-directory "~/News"))))
1908     (if (and last-file
1909              (string-equal (file-name-directory default)
1910                            (file-name-directory last-file))
1911              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
1912         default
1913       (or last-file default))))
1914
1915 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
1916   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1917 If variable `gnus-use-long-file-name' is nil, it is ~/News/news.group/num.
1918 Otherwise, it is like ~/News/news/group/num."
1919   (let ((default
1920           (expand-file-name
1921            (concat (if (gnus-use-long-file-name 'not-save)
1922                        newsgroup
1923                      (gnus-newsgroup-directory-form newsgroup))
1924                    "/" (int-to-string (header-number headers)))
1925            (or gnus-article-save-directory "~/News"))))
1926     (if (and last-file
1927              (string-equal (file-name-directory default)
1928                            (file-name-directory last-file))
1929              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
1930         default
1931       (or last-file default))))
1932
1933 (defun gnus-Plain-save-name (newsgroup headers &optional last-file)
1934   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1935 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group.
1936 Otherwise, it is like ~/News/news/group/news."
1937   (or last-file
1938       (expand-file-name
1939        (if (gnus-use-long-file-name 'not-save)
1940            (gnus-capitalize-newsgroup newsgroup)
1941          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
1942        (or gnus-article-save-directory "~/News"))))
1943
1944 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
1945   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1946 If variable `gnus-use-long-file-name' is nil, it is ~/News/news.group.
1947 Otherwise, it is like ~/News/news/group/news."
1948   (or last-file
1949       (expand-file-name
1950        (if (gnus-use-long-file-name 'not-save)
1951            newsgroup
1952          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
1953        (or gnus-article-save-directory "~/News"))))
1954
1955 ;; For subscribing new newsgroup
1956
1957 (defun gnus-subscribe-hierarchical-interactive (groups)
1958   (let ((groups (sort groups 'string<))
1959         prefixes prefix start rest ans group starts)
1960     (while groups
1961       (setq prefixes (list "^"))
1962       (while (and groups prefixes)
1963         (while (not (string-match (car prefixes) (car groups)))
1964           (setq prefixes (cdr prefixes)))
1965         (setq prefix (car prefixes))
1966         (setq start (1- (length prefix)))
1967         (if (and (string-match "[^\\.]\\." (car groups) start)
1968                  (cdr groups)
1969                  (setq prefix 
1970                        (concat "^" (substring (car groups) 0 (match-end 0))))
1971                  (string-match prefix (car (cdr groups))))
1972             (progn
1973               (setq prefixes (cons prefix prefixes))
1974               (message "Descend hierarchy %s? ([y]nsq): " 
1975                        (substring prefix 1 (1- (length prefix))))
1976               (setq ans (read-char))
1977               (cond ((= ans ?n)
1978                      (while (and groups 
1979                                  (string-match prefix 
1980                                                (setq group (car groups))))
1981                        (setq gnus-killed-list 
1982                              (cons group gnus-killed-list))
1983                        (gnus-sethash group group gnus-killed-hashtb)
1984                        (setq groups (cdr groups)))
1985                      (setq starts (cdr starts)))
1986                     ((= ans ?s)
1987                      (while (and groups 
1988                                  (string-match prefix 
1989                                                (setq group (car groups))))
1990                        (gnus-sethash group group gnus-killed-hashtb)
1991                        (gnus-subscribe-alphabetically (car groups))
1992                        (setq groups (cdr groups)))
1993                      (setq starts (cdr starts)))
1994                     ((= ans ?q)
1995                      (while groups
1996                        (setq group (car groups))
1997                        (setq gnus-killed-list (cons group gnus-killed-list))
1998                        (gnus-sethash group group gnus-killed-hashtb)
1999                        (setq groups (cdr groups))))
2000                     (t nil)))
2001           (message "Subscribe %s? ([n]yq)" (car groups))
2002           (setq ans (read-char))
2003           (setq group (car groups))
2004           (cond ((= ans ?y)
2005                  (gnus-subscribe-alphabetically (car groups))
2006                  (gnus-sethash group group gnus-killed-hashtb))
2007                 ((= ans ?q)
2008                  (while groups
2009                    (setq group (car groups))
2010                    (setq gnus-killed-list (cons group gnus-killed-list))
2011                    (gnus-sethash group group gnus-killed-hashtb)
2012                    (setq groups (cdr groups))))
2013                 (t 
2014                  (setq gnus-killed-list (cons group gnus-killed-list))
2015                  (gnus-sethash group group gnus-killed-hashtb)))
2016           (setq groups (cdr groups)))))))
2017
2018 (defun gnus-subscribe-randomly (newsgroup)
2019   "Subscribe new NEWSGROUP by making it the first newsgroup."
2020   (gnus-subscribe-newsgroup newsgroup))
2021
2022 (defun gnus-subscribe-alphabetically (newgroup)
2023   "Subscribe new NEWSGROUP and insert it in alphabetical order."
2024   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2025   (let ((groups (cdr gnus-newsrc-alist))
2026         before)
2027     (while (and (not before) groups)
2028       (if (string< newgroup (car (car groups)))
2029           (setq before (car (car groups)))
2030         (setq groups (cdr groups))))
2031     (gnus-subscribe-newsgroup newgroup before)))
2032
2033 (defun gnus-subscribe-hierarchically (newgroup)
2034   "Subscribe new NEWSGROUP and insert it in hierarchical newsgroup order."
2035   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2036   (save-excursion
2037     (set-buffer (find-file-noselect gnus-current-startup-file))
2038     (let ((groupkey newgroup)
2039           before)
2040       (while (and (not before) groupkey)
2041         (goto-char (point-min))
2042         (let ((groupkey-re
2043                (concat "^\\(" (regexp-quote groupkey) ".*\\)[!:]")))
2044           (while (and (re-search-forward groupkey-re nil t)
2045                       (progn
2046                         (setq before (buffer-substring
2047                                       (match-beginning 1) (match-end 1)))
2048                         (string< before newgroup)))))
2049         ;; Remove tail of newsgroup name (eg. a.b.c -> a.b)
2050         (setq groupkey
2051               (if (string-match "^\\(.*\\)\\.[^.]+$" groupkey)
2052                   (substring groupkey (match-beginning 1) (match-end 1)))))
2053       (gnus-subscribe-newsgroup newgroup before))))
2054
2055 (defun gnus-subscribe-interactively (newsgroup)
2056   "Subscribe new NEWSGROUP interactively.
2057 It is inserted in hierarchical newsgroup order if subscribed. If not,
2058 it is killed."
2059   (if (gnus-y-or-n-p (format "Subscribe new newsgroup: %s " newsgroup))
2060       (gnus-subscribe-hierarchically newsgroup)
2061     (setq gnus-killed-list (cons newsgroup gnus-killed-list))))
2062
2063 (defun gnus-subscribe-zombies (newsgroup)
2064   "Make new NEWSGROUP a zombie group."
2065   (setq gnus-zombie-list (cons newsgroup gnus-zombie-list)))
2066
2067 (defun gnus-subscribe-newsgroup (newsgroup &optional next)
2068   "Subscribe new NEWSGROUP.
2069 If NEXT is non-nil, it is inserted before NEXT. Otherwise it is made
2070 the first newsgroup."
2071   ;; We subscribe the group by changing its level to `subscribed'.
2072   (gnus-group-change-level 
2073    newsgroup gnus-level-default-subscribed
2074    gnus-level-killed (gnus-gethash (or next "dummy.group") gnus-newsrc-hashtb))
2075   (message "Subscribe newsgroup: %s" newsgroup))
2076
2077 ;; For directories
2078
2079 (defun gnus-newsgroup-directory-form (newsgroup)
2080   "Make hierarchical directory name from NEWSGROUP name."
2081   (let ((newsgroup (substring newsgroup 0)) ;Copy string.
2082         (len (length newsgroup))
2083         (idx 0))
2084     ;; Replace all occurrences of `.' with `/'.
2085     (while (< idx len)
2086       (if (= (aref newsgroup idx) ?.)
2087           (aset newsgroup idx ?/))
2088       (setq idx (1+ idx)))
2089     newsgroup
2090     ))
2091
2092 (defun gnus-make-directory (dir)
2093   "Make DIRECTORY recursively."
2094   (let* ((dir (expand-file-name dir default-directory))
2095          dirs)
2096     (if (string-match "/$" dir)
2097         (setq dir (substring dir 0 (match-beginning 0))))
2098     (while (not (file-exists-p dir))
2099       (setq dirs (cons dir dirs))
2100       (string-match "/[^/]+$" dir)
2101       (setq dir (substring dir 0 (match-beginning 0))))
2102     (while dirs
2103       (make-directory (car dirs))
2104       (setq dirs (cdr dirs)))))
2105
2106 (defun gnus-capitalize-newsgroup (newsgroup)
2107   "Capitalize NEWSGROUP name."
2108   (and (not (zerop (length newsgroup)))
2109        (concat (char-to-string (upcase (aref newsgroup 0)))
2110                (substring newsgroup 1))))
2111
2112 ;; Var
2113
2114 (defun gnus-simplify-subject (subject &optional re-only)
2115   "Remove `Re:' and words in parentheses.
2116 If optional argument RE-ONLY is non-nil, strip `Re:' only."
2117   (let ((case-fold-search t))           ;Ignore case.
2118     ;; Remove `Re:' and `Re^N:'.
2119     (if (string-match "^re:[ \t]*" subject)
2120         (setq subject (substring subject (match-end 0))))
2121     ;; Remove words in parentheses from end.
2122     (or re-only
2123         (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
2124           (setq subject (substring subject 0 (match-beginning 0)))))
2125     ;; Return subject string.
2126     subject))
2127
2128 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
2129 ;; all whitespace.
2130 (defun gnus-simplify-subject-fuzzy (subject)
2131   (let ((case-fold-search t))
2132     (save-excursion
2133       (gnus-set-work-buffer)
2134       (insert subject)
2135       (goto-char (point-min))
2136       (and (looking-at "^[ \t]*re:[ \t]*")
2137            (replace-match "" t t))
2138       (while (re-search-forward "[ \t\n]*([^()]*)[ \t\n]*\\'" nil t)
2139         (replace-match "" t t)
2140         (goto-char (point-min)))
2141       (while (re-search-forward "[ \t]+" nil 'move)
2142         (replace-match " " t t))
2143       ;; the 'move above makes sure we are at (point-max)
2144       (and (= (preceding-char) ? )
2145            (delete-char -1))
2146       (buffer-string))))
2147
2148 ;; Add the current buffer to the list of buffers to be killed on exit. 
2149 (defun gnus-add-current-to-buffer-list ()
2150   (setq gnus-buffer-list (cons (current-buffer) gnus-buffer-list)))
2151
2152 (defun gnus-string> (s1 s2)
2153   (not (or (string< s1 s2)
2154            (string= s1 s2))))
2155
2156 ;; Functions accessing headers.
2157 ;; Functions are more convenient than macros in some cases.
2158
2159 (defun gnus-header-number (header)
2160   (header-number header))
2161
2162 (defun gnus-header-subject (header)
2163   (header-subject header))
2164
2165 (defun gnus-header-from (header)
2166   (header-from header))
2167
2168 (defun gnus-header-xref (header)
2169   (header-xref header))
2170
2171 (defun gnus-header-lines (header)
2172   (header-lines header))
2173
2174 (defun gnus-header-date (header)
2175   (header-date header))
2176
2177 (defun gnus-header-id (header)
2178   (header-id header))
2179
2180 (defun gnus-header-references (header)
2181   (header-references header))
2182
2183 ;;; General various misc type functions.
2184
2185 (defun gnus-clear-system ()
2186   "Clear all variables and buffers."
2187   ;; Clear Gnus variables.
2188   (let ((variables gnus-variable-list))
2189     (while variables
2190       (set (car variables) nil)
2191       (setq variables (cdr variables))))
2192   ;; Clear other internal variables.
2193   (setq gnus-list-of-killed-groups nil
2194         gnus-have-read-active-file nil
2195         gnus-newsrc-alist nil
2196         gnus-newsrc-hashtb nil
2197         gnus-killed-list nil
2198         gnus-zombie-list nil
2199         gnus-killed-hashtb nil
2200         gnus-active-hashtb nil
2201         gnus-moderated-list nil
2202         gnus-description-hashtb nil
2203         gnus-newsgroup-headers nil
2204         gnus-newsgroup-headers-hashtb-by-number nil
2205         gnus-newsgroup-name nil
2206         gnus-server-alist nil
2207         gnus-internal-global-score-files nil
2208         gnus-current-select-method nil)
2209   ;; Reset any score variables.
2210   (and (boundp 'gnus-score-cache)
2211        (set 'gnus-score-cache nil))
2212   ;; Kill the startup file.
2213   (and gnus-current-startup-file
2214        (get-file-buffer gnus-current-startup-file)
2215        (kill-buffer (get-file-buffer gnus-current-startup-file)))
2216   ;; Clear the dribble buffer.
2217   (gnus-dribble-clear)
2218   ;; Kill global KILL file buffer.
2219   (if (get-file-buffer (gnus-newsgroup-kill-file nil))
2220       (kill-buffer (get-file-buffer (gnus-newsgroup-kill-file nil))))
2221   (gnus-kill-buffer nntp-server-buffer)
2222   ;; Kill Gnus buffers.
2223   (while gnus-buffer-list
2224     (gnus-kill-buffer (car gnus-buffer-list))
2225     (setq gnus-buffer-list (cdr gnus-buffer-list))))
2226
2227 (defun gnus-configure-windows (action &optional force)
2228   "Configure Gnus windows according to the next ACTION.
2229 The ACTION is either a symbol, such as `summary', or a
2230 configuration list such as `(1 1 2)'.  If ACTION is not a list,
2231 configuration list is got from the variable gnus-window-configuration.
2232 If FORCE is non-nil, the updating will be done whether it is necessary
2233 or not."
2234   (let* ((windows
2235           (if (listp action) action 
2236             (if (listp gnus-window-configuration)
2237                 (car (cdr (assq action gnus-window-configuration)))
2238               gnus-window-configuration)))
2239          (grpwin (get-buffer-window gnus-group-buffer))
2240          (subwin (get-buffer-window gnus-summary-buffer))
2241          (artwin (get-buffer-window gnus-article-buffer))
2242          (winsum nil)
2243          (height nil)
2244          (grpheight 0)
2245          (subheight 0)
2246          (artheight 0)
2247          ;; Make split-window-vertically leave focus in upper window.
2248          (split-window-keep-point t))
2249     (if (and (symbolp windows) (fboundp windows))
2250         (funcall windows action)
2251       (if (and (not force)
2252                (or (null windows)               ;No configuration is specified.
2253                    (and (eq (null grpwin)
2254                             (zerop (nth 0 windows)))
2255                         (eq (null subwin)
2256                             (zerop (nth 1 windows)))
2257                         (eq (null artwin)
2258                             (zerop (nth 2 windows))))))
2259           ;; No need to change window configuration.
2260           nil
2261         (select-window (or grpwin subwin artwin (selected-window)))
2262         ;; First of all, compute the height of each window.
2263         (cond (gnus-use-full-window
2264                ;; Take up the entire screen.
2265                (delete-other-windows)
2266                (setq height (window-height (selected-window))))
2267               (t
2268                (setq height (+ (if grpwin (window-height grpwin) 0)
2269                                (if subwin (window-height subwin) 0)
2270                                (if artwin (window-height artwin) 0)))))
2271         ;; The group buffer exits always. So, use it to extend the
2272         ;; group window so as to get enough window space.
2273         (switch-to-buffer gnus-group-buffer 'norecord)
2274         (and (get-buffer gnus-summary-buffer)
2275              (delete-windows-on gnus-summary-buffer))
2276         (and (get-buffer gnus-article-buffer)
2277              (delete-windows-on gnus-article-buffer))
2278         ;; Compute expected window height.
2279         (setq winsum (apply (function +) windows))
2280         (if (not (zerop (nth 0 windows)))
2281             (setq grpheight (max window-min-height
2282                                  (/ (* height (nth 0 windows)) winsum))))
2283         (if (not (zerop (nth 1 windows)))
2284             (setq subheight (max window-min-height
2285                                  (/ (* height (nth 1 windows)) winsum))))
2286         (if (not (zerop (nth 2 windows)))
2287             (if gnus-split-window ;hack by erik
2288                 (setq artheight height)
2289               (setq artheight (max window-min-height
2290                                    (/ (* height (nth 2 windows)) winsum)))))
2291         (setq height (+ grpheight subheight artheight))
2292         (enlarge-window (max 0 (- height (window-height (selected-window)))))
2293         ;; Then split the window.
2294         (if (and (not (zerop artheight))
2295                  (or (not (zerop grpheight))
2296                      (not (zerop subheight))))
2297             (if gnus-split-window
2298                 (split-window-horizontally)
2299               (split-window-vertically (+ grpheight subheight))))
2300         (and (not (zerop grpheight))
2301              (not (zerop subheight))
2302              (split-window-vertically grpheight))
2303         ;; Then select buffers in each window.
2304         (or (zerop grpheight)
2305             (progn
2306               (switch-to-buffer gnus-group-buffer 'norecord)
2307               (other-window 1)))
2308         (or (zerop subheight)
2309             (progn
2310               (switch-to-buffer gnus-summary-buffer 'norecord)
2311               (other-window 1)))
2312         (or (zerop artheight)
2313             (progn
2314               ;; If article buffer does not exist, it will be created
2315               ;; and initialized.
2316               (gnus-article-setup-buffer)
2317               (switch-to-buffer gnus-article-buffer 'norecord)
2318               (setq buffer-read-only t) ; !!! Why!?! 
2319               (bury-buffer gnus-summary-buffer)
2320               (bury-buffer gnus-group-buffer)))
2321         (or (zerop subheight)
2322             (progn
2323               (pop-to-buffer gnus-summary-buffer)
2324               ;; It seems that some code in this function will set
2325               ;; buffer-read-only to nil. I have absolutely no idea
2326               ;; why. 
2327               (setq buffer-read-only t))))))) ; !!! Why!?! 
2328
2329 (defun gnus-window-configuration-split (action)
2330   (switch-to-buffer gnus-group-buffer t)
2331   (delete-other-windows)
2332   (split-window-horizontally)
2333   (cond ((memq action '(newsgoups summary))
2334          (if (gnus-buffer-exists-p gnus-summary-buffer)
2335              (switch-to-buffer-other-window gnus-summary-buffer)))
2336         ((eq action 'article)
2337          (switch-to-buffer gnus-summary-buffer t)
2338          (other-window 1)
2339          (gnus-article-setup-buffer)
2340          (switch-to-buffer gnus-article-buffer t))))
2341
2342 (defun gnus-version ()
2343   "Version numbers of this version of Gnus."
2344   (interactive)
2345   (let ((methods gnus-valid-select-methods)
2346         (mess gnus-version)
2347         meth)
2348     ;; Go through all the legal select methods and add their version
2349     ;; numbers to the total version string. Only the backends that are
2350     ;; currently in use will have their message numbers taken into
2351     ;; consideration. 
2352     (while methods
2353       (setq meth (intern (concat (car (car methods)) "-version")))
2354       (and (boundp meth)
2355            (stringp (symbol-value meth))
2356            (setq mess (concat mess "; " (symbol-value meth))))
2357       (setq methods (cdr methods)))
2358     (message mess)))
2359
2360 (defun gnus-info-find-node ()
2361   "Find Info documentation of Gnus."
2362   (interactive)
2363   ;; Enlarge info window if needed.
2364   (cond ((eq major-mode 'gnus-group-mode)
2365          (gnus-configure-windows '(1 0 0)) ;Take all windows.
2366          (pop-to-buffer gnus-group-buffer))
2367         ((eq major-mode 'gnus-summary-mode)
2368          (gnus-configure-windows '(0 1 0)) ;Take all windows.
2369          (pop-to-buffer gnus-summary-buffer)))
2370   (Info-goto-node (car (cdr (assq major-mode gnus-info-nodes)))))
2371
2372 (defun gnus-bug ()
2373   "Send a bug report to the Gnus maintainers."
2374   (interactive)
2375   (pop-to-buffer "*Gnus Bug*")
2376   (erase-buffer)
2377   (mail-mode)
2378   (mail-setup gnus-maintainer "[Gnus Bug Report] " nil nil nil nil)
2379   (goto-char (point-min))
2380   (re-search-forward (concat "^" (regexp-quote mail-header-separator) "$"))
2381   (forward-line 1)
2382   (insert (format "%s\n%s\n\n" (gnus-version) (emacs-version)))
2383   (gnus-debug)
2384   (message ""))
2385
2386 (defun gnus-debug ()
2387   "Attemps to go through the Gnus source file and report what variables have been changed.
2388 The source file has to be in the Emacs load path."
2389   (interactive)
2390   (let ((dirs load-path)
2391         file expr olist)
2392     (while dirs
2393       (if (file-exists-p (setq file (concat (car dirs) "/gnus.el")))
2394           (save-excursion
2395             (setq dirs nil)
2396             (set-buffer (get-buffer-create "*gnus bug info*"))
2397             (buffer-disable-undo (current-buffer))
2398             (erase-buffer)
2399             (insert-file-contents file)
2400             (goto-char (point-min))
2401             (or (search-forward "\n;; Internal variables" nil t)
2402                 (error "Malformed sources"))
2403             (narrow-to-region (point-min) (point))
2404             (goto-char (point-min))
2405             (while (setq expr (condition-case () 
2406                                   (read (current-buffer)) (error nil)))
2407               (and (eq (car expr) 'defvar)
2408                    (stringp (nth 3 expr))
2409                    (not (equal (eval (nth 2 expr))
2410                                (and (boundp (nth 1 expr))
2411                                     (symbol-value (nth 1 expr)))))
2412                    (setq olist (cons (nth 1 expr) olist))))
2413             (kill-buffer (current-buffer)))
2414         (setq dirs (cdr dirs))))
2415     (while olist
2416       (insert "(setq " (symbol-name (car olist)) " '" 
2417               (prin1-to-string (symbol-value (car olist))) ")\n")
2418       (setq olist (cdr olist)))
2419     (insert "\n\n")))
2420
2421 (defun gnus-overload-functions (&optional overloads)
2422   "Overload functions specified by optional argument OVERLOADS.
2423 If nothing is specified, use the variable gnus-overload-functions."
2424   (let ((defs nil)
2425         (overloads (or overloads gnus-overload-functions)))
2426     (while overloads
2427       (setq defs (car overloads))
2428       (setq overloads (cdr overloads))
2429       ;; Load file before overloading function if necessary.  Make
2430       ;; sure we cannot use `require' always.
2431       (and (not (fboundp (car defs)))
2432            (car (cdr (cdr defs)))
2433            (load (car (cdr (cdr defs))) nil 'nomessage))
2434       (fset (car defs) (car (cdr defs))))))
2435
2436 (defun gnus-replace-chars-in-string (string from to)
2437   "Replace characters in STRING from FROM to TO."
2438   (let ((string (substring string 0))   ;Copy string.
2439         (len (length string))
2440         (idx 0))
2441     ;; Replace all occurrences of FROM with TO.
2442     (while (< idx len)
2443       (if (= (aref string idx) from)
2444           (aset string idx to))
2445       (setq idx (1+ idx)))
2446     string))
2447
2448 (defun gnus-days-between (date1 date2)
2449   ;; Return the number of days between date1 and date2.
2450   (- (gnus-day-number date1) (gnus-day-number date2)))
2451
2452 (defun gnus-day-number (date)
2453   (let ((dat (mapcar (lambda (s) (and s (string-to-int s)) )
2454                      (timezone-parse-date date))))
2455     (timezone-absolute-from-gregorian 
2456      (nth 1 dat) (nth 2 dat) (car dat))))
2457
2458 ;; Returns a floating point number that says how many seconds have
2459 ;; lapsed between Jan 1 12:00:00 1970 and DATE.
2460 (defun gnus-seconds-since-epoch (date)
2461   (let* ((tdate (mapcar (lambda (ti) (and ti (string-to-int ti)))
2462                         (timezone-parse-date date)))
2463          (ttime (mapcar (lambda (ti) (and ti (string-to-int ti)))
2464                         (timezone-parse-time
2465                          (aref (timezone-parse-date date) 3))))
2466          (edate (mapcar (lambda (ti) (and ti (string-to-int ti)))
2467                         (timezone-parse-date "Jan 1 12:00:00 1970")))
2468          (tday (- (timezone-absolute-from-gregorian 
2469                    (nth 1 tdate) (nth 2 tdate) (nth 0 tdate))
2470                   (timezone-absolute-from-gregorian 
2471                    (nth 1 edate) (nth 2 edate) (nth 0 edate)))))
2472     (+ (nth 2 ttime)
2473        (* (nth 1 ttime) 60)
2474        (* 1.0 (nth 0 ttime) 60 60)
2475        (* 1.0 tday 60 60 24))))
2476
2477 (defun gnus-file-newer-than (file date)
2478   (let ((fdate (nth 5 (file-attributes file))))
2479     (or (> (car fdate) (car date))
2480         (and (= (car fdate) (car date))
2481              (> (nth 1 fdate) (nth 1 date))))))
2482
2483 ;; Two silly functions to ensure that all `y-or-n-p' questions clear
2484 ;; the echo area.
2485 (defun gnus-y-or-n-p (prompt)
2486   (prog1
2487       (y-or-n-p prompt)
2488     (message "")))
2489
2490 (defun gnus-yes-or-no-p (prompt)
2491   (prog1
2492       (yes-or-no-p prompt)
2493     (message "")))
2494
2495 ;; Check whether to use long file names.
2496 (defun gnus-use-long-file-name (symbol)
2497   ;; The variable has to be set...
2498   (and gnus-use-long-file-name
2499        ;; If it isn't a list, then we return t.
2500        (or (not (listp gnus-use-long-file-name))
2501            ;; If it is a list, and the list contains `symbol', we
2502            ;; return nil.  
2503            (not (memq symbol gnus-use-long-file-name)))))
2504
2505 ;; I suspect there's a better way, but I haven't taken the time to do
2506 ;; it yet. -erik selberg@cs.washington.edu
2507 (defun gnus-dd-mmm (messy-date)
2508   "Return a string like DD-MMM from a big messy string"
2509   (let ((datevec (timezone-parse-date messy-date)))
2510     (format "%2s-%s"
2511             (or (aref datevec 2) "??")
2512             (capitalize
2513              (or (car 
2514                   (nth (1- (string-to-number (aref datevec 1)))
2515                        timezone-months-assoc))
2516                  "???")))))
2517
2518 ;; Make a hash table (default and minimum size is 255).
2519 ;; Optional argument HASHSIZE specifies the table size.
2520 (defun gnus-make-hashtable (&optional hashsize)
2521   (make-vector (if hashsize (max (gnus-create-hash-size hashsize) 255) 255) 0))
2522
2523 ;; Make a number that is suitable for hashing; bigger than MIN and one
2524 ;; less than 2^x.
2525 (defun gnus-create-hash-size (min)
2526   (let ((i 1))
2527     (while (< i min)
2528       (setq i (* 2 i)))
2529     (1- i)))
2530
2531 ;;; List and range functions
2532
2533 (defun gnus-last-element (list)
2534   "Return last element of LIST."
2535   (while (cdr list)
2536     (setq list (cdr list)))
2537   (car list))
2538
2539 (defun gnus-copy-sequence (list)
2540   "Do a complete, total copy of a list."
2541   (if (and (consp list) (not (consp (cdr list))))
2542       (cons (car list) (cdr list))
2543     (mapcar (lambda (elem) (if (consp elem) 
2544                                (if (consp (cdr elem))
2545                                    (gnus-copy-sequence elem)
2546                                  (cons (car elem) (cdr elem)))
2547                              elem))
2548             list)))
2549
2550 (defun gnus-set-difference (list1 list2)
2551   "Return a list of elements of LIST1 that do not appear in LIST2."
2552   (let ((list1 (copy-sequence list1)))
2553     (while list2
2554       (setq list1 (delq (car list2) list1))
2555       (setq list2 (cdr list2)))
2556     list1))
2557
2558 (defun gnus-sorted-complement (list1 list2)
2559   "Return a list of elements of LIST1 that do not appear in LIST2.
2560 Both lists have to be sorted over <."
2561   (let (out)
2562     (while (and list1 list2)
2563       (cond ((= (car list1) (car list2))
2564              (setq list1 (cdr list1)
2565                    list2 (cdr list2)))
2566             ((< (car list1) (car list2))
2567              (setq out (cons (car list1) out))
2568              (setq list1 (cdr list1)))
2569             (t
2570              (setq out (cons (car list2) out))
2571              (setq list2 (cdr list2)))))
2572     (nreverse (append (or list1 list2) out))))
2573
2574 (defun gnus-intersection (list1 list2)      
2575   (let ((result nil))
2576     (while list2
2577       (if (memq (car list2) list1)
2578           (setq result (cons (car list2) result)))
2579       (setq list2 (cdr list2)))
2580     result))
2581
2582 (defun gnus-sorted-intersection (list1 list2)
2583   ;; LIST1 and LIST2 have to be sorted over <.
2584   (let (out)
2585     (while (and list1 list2)
2586       (cond ((= (car list1) (car list2))
2587              (setq out (cons (car list1) out)
2588                    list1 (cdr list1)
2589                    list2 (cdr list2)))
2590             ((< (car list1) (car list2))
2591              (setq list1 (cdr list1)))
2592             (t
2593              (setq list2 (cdr list2)))))
2594     (nreverse out)))
2595
2596 (defun gnus-set-sorted-intersection (list1 list2)
2597   ;; LIST1 and LIST2 have to be sorted over <.
2598   ;; This function modifies LIST1.
2599   (let* ((top (cons nil list1))
2600          (prev top))
2601   (while (and list1 list2)
2602     (cond ((= (car list1) (car list2))
2603            (setq prev list1
2604                  list1 (cdr list1)
2605                  list2 (cdr list2)))
2606           ((< (car list1) (car list2))
2607            (setcdr prev (cdr list1))
2608            (setq list1 (cdr list1)))
2609           (t
2610            (setq list2 (cdr list2)))))
2611   (setcdr prev nil)
2612   (cdr top)))
2613
2614 (defun gnus-compress-sequence (numbers &optional always-list)
2615   "Convert list of numbers to a list of ranges or a single range.
2616 If ALWAYS-LIST is non-nil, this function will always release a list of
2617 ranges."
2618   (let* ((first (car numbers))
2619          (last (car numbers))
2620          result)
2621     (if (null numbers)
2622         nil
2623       (if (not (listp (cdr numbers)))
2624           numbers
2625         (while numbers
2626           (cond ((= last (car numbers)) nil) ;Omit duplicated number
2627                 ((= (1+ last) (car numbers)) ;Still in sequence
2628                  (setq last (car numbers)))
2629                 (t                      ;End of one sequence
2630                  (setq result 
2631                        (cons (if (= first last) first
2632                                (cons first last)) result))
2633                  (setq first (car numbers))
2634                  (setq last  (car numbers))))
2635           (setq numbers (cdr numbers)))
2636         (if (and (not always-list) (null result))
2637             (if (= first last) (list first) (cons first last))
2638           (nreverse (cons (if (= first last) first (cons first last))
2639                           result)))))))
2640
2641 (defalias 'gnus-uncompress-sequence 'gnus-uncompress-range)
2642 (defun gnus-uncompress-range (ranges)
2643   "Expand a list of ranges into a list of numbers.
2644 RANGES is either a single range on the form `(num . num)' or a list of
2645 these ranges."
2646   (let (first last result)
2647     (cond 
2648      ((null ranges)
2649       nil)
2650      ((not (listp (cdr ranges)))
2651       (setq first (car ranges))
2652       (setq last (cdr ranges))
2653       (while (<= first last)
2654         (setq result (cons first result))
2655         (setq first (1+ first)))
2656       (nreverse result))
2657      (t
2658       (while ranges
2659         (if (atom (car ranges))
2660             (if (numberp (car ranges))
2661                 (setq result (cons (car ranges) result)))
2662           (setq first (car (car ranges)))
2663           (setq last  (cdr (car ranges)))
2664           (while (<= first last)
2665             (setq result (cons first result))
2666             (setq first (1+ first))))
2667         (setq ranges (cdr ranges)))
2668       (nreverse result)))))
2669
2670 (defun gnus-add-to-range (ranges list)
2671   "Return a list of ranges that has all articles from both RANGES and LIST.
2672 Note: LIST has to be sorted over `<'."
2673   (if (not ranges)
2674       (gnus-compress-sequence list t)
2675     (setq list (copy-sequence list))
2676     (or (listp (cdr ranges))
2677         (setq ranges (list ranges)))
2678     (let ((out ranges)
2679           ilist lowest highest temp)
2680       (while (and ranges list)
2681         (setq ilist list)
2682         (setq lowest (or (and (atom (car ranges)) (car ranges))
2683                          (car (car ranges))))
2684         (while (and list (cdr list) (< (car (cdr list)) lowest))
2685           (setq list (cdr list)))
2686         (if (< (car ilist) lowest)
2687             (progn
2688               (setq temp list)
2689               (setq list (cdr list))
2690               (setcdr temp nil)
2691               (setq out (nconc (gnus-compress-sequence ilist t) out))))
2692         (setq highest (or (and (atom (car ranges)) (car ranges))
2693                           (cdr (car ranges))))
2694         (while (and list (<= (car list) highest))
2695           (setq list (cdr list)))
2696         (setq ranges (cdr ranges)))
2697       (if list
2698           (setq out (nconc (gnus-compress-sequence list t) out)))
2699       (setq out (sort out (lambda (r1 r2) 
2700                             (< (or (and (atom r1) r1) (car r1))
2701                                (or (and (atom r2) r2) (car r2))))))
2702       (setq ranges out)
2703       (while ranges
2704         (if (atom (car ranges))
2705             (if (cdr ranges)
2706                 (if (atom (car (cdr ranges)))
2707                     (if (= (1+ (car ranges)) (car (cdr ranges)))
2708                         (progn
2709                           (setcar ranges (cons (car ranges) 
2710                                                (car (cdr ranges))))
2711                           (setcdr ranges (cdr (cdr ranges)))))
2712                   (if (= (1+ (car ranges)) (car (car (cdr ranges))))
2713                       (progn
2714                         (setcar (car (cdr ranges)) (car ranges))
2715                         (setcar ranges (car (cdr ranges)))
2716                         (setcdr ranges (cdr (cdr ranges)))))))
2717           (if (cdr ranges)
2718               (if (atom (car (cdr ranges)))
2719                   (if (= (1+ (cdr (car ranges))) (car (cdr ranges)))
2720                       (progn
2721                         (setcdr (car ranges) (car (cdr ranges)))
2722                         (setcdr ranges (cdr (cdr ranges)))))
2723                 (if (= (1+ (cdr (car ranges))) (car (car (cdr ranges))))
2724                     (progn
2725                       (setcdr (car ranges) (cdr (car (cdr ranges))))
2726                       (setcdr ranges (cdr (cdr ranges))))))))
2727         (setq ranges (cdr ranges)))
2728       out)))
2729
2730 (defun gnus-remove-from-range (ranges list)
2731   "Return a list of ranges that has all articles from LIST removed from RANGES.
2732 Note: LIST has to be sorted over `<'."
2733   ;; !!! This function shouldn't look like this, but I've got a headache.
2734   (gnus-compress-sequence 
2735    (gnus-sorted-complement
2736     (gnus-uncompress-range ranges) list)))
2737
2738 (defun gnus-member-of-range (number ranges)
2739   (if (not (listp (cdr ranges)))
2740       (and (>= number (car ranges)) 
2741            (<= number (cdr ranges)))
2742     (let ((not-stop t))
2743       (while (and ranges 
2744                   (if (numberp (car ranges))
2745                       (>= number (car ranges))
2746                     (>= number (car (car ranges))))
2747                   not-stop)
2748         (if (if (numberp (car ranges))
2749                 (= number (car ranges))
2750               (and (>= number (car (car ranges)))
2751                    (<= number (cdr (car ranges)))))
2752             (setq not-stop nil))
2753         (setq ranges (cdr ranges)))
2754       (not not-stop))))
2755
2756 \f
2757 ;;;
2758 ;;; Gnus group mode
2759 ;;;
2760
2761 (defvar gnus-group-mode-map nil)
2762 (defvar gnus-group-make-map nil)
2763 (defvar gnus-group-list-map nil)
2764 (defvar gnus-group-sub-map nil)
2765 (put 'gnus-group-mode 'mode-class 'special)
2766
2767 (if gnus-group-mode-map
2768     nil
2769   (setq gnus-group-mode-map (make-keymap))
2770   (suppress-keymap gnus-group-mode-map)
2771   (define-key gnus-group-mode-map " " 'gnus-group-read-group)
2772   (define-key gnus-group-mode-map "=" 'gnus-group-select-group)
2773   (define-key gnus-group-mode-map "\r" 'gnus-group-select-group)
2774   (define-key gnus-group-mode-map "j" 'gnus-group-jump-to-group)
2775   (define-key gnus-group-mode-map "n" 'gnus-group-next-unread-group)
2776   (define-key gnus-group-mode-map "p" 'gnus-group-prev-unread-group)
2777   (define-key gnus-group-mode-map "\177" 'gnus-group-prev-unread-group)
2778   (define-key gnus-group-mode-map "N" 'gnus-group-next-group)
2779   (define-key gnus-group-mode-map "P" 'gnus-group-prev-group)
2780   (define-key gnus-group-mode-map "\M-n" 'gnus-group-next-unread-group-same-level)
2781   (define-key gnus-group-mode-map "\M-p" 'gnus-group-prev-unread-group-same-level)
2782   (define-key gnus-group-mode-map "," 'gnus-group-best-unread-group)
2783   (define-key gnus-group-mode-map "." 'gnus-group-first-unread-group)
2784   (define-key gnus-group-mode-map "u" 'gnus-group-unsubscribe-current-group)
2785   (define-key gnus-group-mode-map "U" 'gnus-group-unsubscribe-group)
2786   (define-key gnus-group-mode-map "c" 'gnus-group-catchup-current)
2787   (define-key gnus-group-mode-map "C" 'gnus-group-catchup-current-all)
2788   (define-key gnus-group-mode-map "l" 'gnus-group-list-groups)
2789   (define-key gnus-group-mode-map "L" 'gnus-group-list-all-groups)
2790   (define-key gnus-group-mode-map "m" 'gnus-group-mail)
2791   (define-key gnus-group-mode-map "g" 'gnus-group-get-new-news)
2792   (define-key gnus-group-mode-map "\M-g" 'gnus-group-get-new-news-this-group)
2793   (define-key gnus-group-mode-map "R" 'gnus-group-restart)
2794   (define-key gnus-group-mode-map "r" 'gnus-group-read-init-file)
2795   (define-key gnus-group-mode-map "B" 'gnus-group-browse-foreign-server)
2796   (define-key gnus-group-mode-map "b" 'gnus-group-check-bogus-groups)
2797   (define-key gnus-group-mode-map "F" 'gnus-find-new-newsgroups)
2798   (define-key gnus-group-mode-map "\C-c\C-d" 'gnus-group-describe-group)
2799   (define-key gnus-group-mode-map "\M-d" 'gnus-group-describe-all-groups)
2800   (define-key gnus-group-mode-map "\C-c\C-a" 'gnus-group-apropos)
2801   (define-key gnus-group-mode-map "\C-c\M-C-a" 'gnus-group-description-apropos)
2802   (define-key gnus-group-mode-map "a" 'gnus-group-post-news)
2803   (define-key gnus-group-mode-map "\ek" 'gnus-group-edit-local-kill)
2804   (define-key gnus-group-mode-map "\eK" 'gnus-group-edit-global-kill)
2805   (define-key gnus-group-mode-map "\C-k" 'gnus-group-kill-group)
2806   (define-key gnus-group-mode-map "\C-y" 'gnus-group-yank-group)
2807   (define-key gnus-group-mode-map "\C-w" 'gnus-group-kill-region)
2808   (define-key gnus-group-mode-map "\C-x\C-t" 'gnus-group-transpose-groups)
2809   (define-key gnus-group-mode-map "\C-c\C-l" 'gnus-group-list-killed)
2810   (define-key gnus-group-mode-map "\C-c\C-x" 'gnus-group-expire-articles)
2811   (define-key gnus-group-mode-map "\C-c\M-\C-x" 'gnus-group-expire-all-groups)
2812   (define-key gnus-group-mode-map "V" 'gnus-version)
2813   (define-key gnus-group-mode-map "s" 'gnus-group-save-newsrc)
2814   (define-key gnus-group-mode-map "z" 'gnus-group-suspend)
2815   (define-key gnus-group-mode-map "Z" 'gnus-group-clear-dribble)
2816   (define-key gnus-group-mode-map "q" 'gnus-group-exit)
2817   (define-key gnus-group-mode-map "Q" 'gnus-group-quit)
2818   (define-key gnus-group-mode-map "\M-f" 'gnus-group-fetch-faq)
2819   (define-key gnus-group-mode-map "?" 'gnus-group-describe-briefly)
2820   (define-key gnus-group-mode-map "\C-c\C-i" 'gnus-info-find-node)
2821   (define-key gnus-group-mode-map "\M-e" 'gnus-group-edit-group-method)
2822   (define-key gnus-group-mode-map "^" 'gnus-group-enter-server-mode)
2823   (define-key gnus-group-mode-map
2824     (if gnus-xemacs [button2] [mouse-2]) 'gnus-mouse-pick-group)
2825   (define-key gnus-group-mode-map "<" 'beginning-of-buffer)
2826   (define-key gnus-group-mode-map ">" 'end-of-buffer)
2827
2828   (define-prefix-command 'gnus-group-make-map)
2829   (define-key gnus-group-mode-map "M" 'gnus-group-make-map)
2830   (define-key gnus-group-make-map "d" 'gnus-group-make-directory-group)
2831   (define-key gnus-group-make-map "h" 'gnus-group-make-help-group)
2832   (define-key gnus-group-make-map "a" 'gnus-group-make-archive-group)
2833   (define-key gnus-group-make-map "k" 'gnus-group-make-kiboze-group)
2834   (define-key gnus-group-make-map "m" 'gnus-group-make-group)
2835   (define-key gnus-group-make-map "E" 'gnus-group-edit-group)
2836   (define-key gnus-group-make-map "e" 'gnus-group-edit-group-method)
2837   (define-key gnus-group-make-map "p" 'gnus-group-edit-group-parameters)
2838
2839   (define-prefix-command 'gnus-group-list-map)
2840   (define-key gnus-group-mode-map "G" 'gnus-group-list-map)
2841   (define-key gnus-group-list-map "k" 'gnus-group-list-killed)
2842   (define-key gnus-group-list-map "z" 'gnus-group-list-zombies)
2843   (define-key gnus-group-list-map "s" 'gnus-group-list-groups)
2844   (define-key gnus-group-list-map "u" 'gnus-group-list-all-groups)
2845   (define-key gnus-group-list-map "a" 'gnus-group-apropos)
2846   (define-key gnus-group-list-map "d" 'gnus-group-description-apropos)
2847   (define-key gnus-group-list-map "m" 'gnus-group-list-matching)
2848   (define-key gnus-group-list-map "M" 'gnus-group-list-all-matching)
2849
2850   (define-prefix-command 'gnus-group-sub-map)
2851   (define-key gnus-group-mode-map "S" 'gnus-group-sub-map)
2852   (define-key gnus-group-sub-map "l" 'gnus-group-set-current-level)
2853   (define-key gnus-group-sub-map "t" 'gnus-group-unsubscribe-current-group)
2854   (define-key gnus-group-sub-map "s" 'gnus-group-unsubscribe-group)
2855   (define-key gnus-group-sub-map "k" 'gnus-group-kill-group)
2856   (define-key gnus-group-sub-map "y" 'gnus-group-yank-group)
2857   (define-key gnus-group-sub-map "w" 'gnus-group-kill-region)
2858   (define-key gnus-group-sub-map "z" 'gnus-group-kill-all-zombies))
2859
2860 (defun gnus-group-mode ()
2861   "Major mode for reading news.
2862
2863 All normal editing commands are switched off.
2864 \\<gnus-group-mode-map>
2865 The group buffer lists (some of) the groups available.  For instance,
2866 `\\[gnus-group-list-groups]' will list all subscribed groups with unread articles, while `\\[gnus-group-list-zombies]'
2867 lists all zombie groups. 
2868
2869 Groups that are displayed can be entered with `\\[gnus-group-read-group]'.  To subscribe 
2870 to a group not displayed, type `\\[gnus-group-unsubscribe-group]'. 
2871
2872 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]'). 
2873
2874 The following commands are available:
2875
2876 \\{gnus-group-mode-map}"
2877   (interactive)
2878   (if gnus-visual (gnus-group-make-menu-bar))
2879   (kill-all-local-variables)
2880   (setq mode-line-modified "-- ")
2881   (make-local-variable 'mode-line-format)
2882   (setq mode-line-format (copy-sequence mode-line-format))
2883   (and (equal (nth 3 mode-line-format) "   ")
2884        (setcar (nthcdr 3 mode-line-format) ""))
2885   (setq major-mode 'gnus-group-mode)
2886   (setq mode-name "Group")
2887   (gnus-group-set-mode-line)
2888   (setq mode-line-process nil)
2889   (use-local-map gnus-group-mode-map)
2890   (buffer-disable-undo (current-buffer))
2891   (setq truncate-lines t)
2892   (setq buffer-read-only t)
2893   (run-hooks 'gnus-group-mode-hook))
2894
2895 (defun gnus-mouse-pick-group (e)
2896   (interactive "e")
2897   (mouse-set-point e)
2898   (gnus-group-read-group nil))
2899
2900 ;;;###autoload
2901 (defun gnus-no-server (&optional arg)
2902   "Read network news.
2903 If ARG is a positive number, Gnus will use that as the
2904 startup level. If ARG is nil, Gnus will be started at level 2. 
2905 If ARG is non-nil and not a positive number, Gnus will
2906 prompt the user for the name of an NNTP server to use.
2907 As opposed to `gnus', this command will not connect to the local server."
2908   (interactive "P")
2909   (gnus (or arg (1- gnus-level-default-subscribed)) t))
2910
2911 (defalias '\(ding\) 'gnus)
2912
2913 ;;;###autoload
2914 (defun gnus (&optional arg dont-connect)
2915   "Read network news.
2916 If ARG is non-nil and a positive number, Gnus will use that as the
2917 startup level. If ARG is non-nil and not a positive number, Gnus will
2918 prompt the user for the name of an NNTP server to use."
2919   (interactive "P")
2920   (if (get-buffer gnus-group-buffer)
2921       (progn
2922         (switch-to-buffer gnus-group-buffer)
2923         (gnus-group-get-new-news))
2924     (gnus-clear-system)
2925     (gnus-read-init-file)
2926     (let ((level (and arg (numberp arg) (> arg 0) arg))
2927           did-connect)
2928       (unwind-protect
2929           (progn
2930             (switch-to-buffer gnus-group-buffer)
2931             (gnus-add-current-to-buffer-list)
2932             (gnus-group-mode)
2933             (or dont-connect 
2934                 (setq did-connect
2935                       (gnus-start-news-server (and arg (not level))))))
2936         (if (and (not dont-connect) 
2937                  (not did-connect))
2938             (gnus-group-quit)
2939           (run-hooks 'gnus-startup-hook)
2940           ;; NNTP server is successfully open. 
2941           (gnus-update-format-specifications)
2942           (let ((buffer-read-only nil))
2943             (erase-buffer)
2944             (if (not gnus-inhibit-startup-message)
2945                 (progn
2946                   (gnus-group-startup-message)
2947                   (sit-for 0))))
2948           (gnus-setup-news nil level)
2949           (and gnus-use-dribble-file (gnus-dribble-open))
2950           (or t (not gnus-novice-user)
2951               gnus-expert-user
2952               (gnus-group-describe-briefly)) ;Show brief help message.
2953           (gnus-group-list-groups level))))))
2954
2955 (defun gnus-group-startup-message (&optional x y)
2956   "Insert startup message in current buffer."
2957   ;; Insert the message.
2958   (erase-buffer)
2959   (insert
2960    (format "
2961     %s
2962            A newsreader 
2963       for GNU Emacs
2964
2965         Based on GNUS 
2966              written by 
2967      Masanobu UMEDA
2968
2969     Lars Magne 
2970          Ingebrigtsen 
2971       larsi@ifi.uio.no
2972
2973            gnus-version))
2974   ;; And then hack it.
2975   ;; 18 is the longest line.
2976   (indent-rigidly (point-min) (point-max) 
2977                   (/ (max (- (window-width) (or x 28)) 0) 2))
2978   (goto-char (point-min))
2979   ;; +4 is fuzzy factor.
2980   (insert-char ?\n (/ (max (- (window-height) (or y 12)) 0) 2)))
2981
2982 (defun gnus-group-setup-buffer ()
2983   (or (get-buffer gnus-group-buffer)
2984       (progn
2985         (switch-to-buffer gnus-group-buffer)
2986         (gnus-add-current-to-buffer-list)
2987         (gnus-group-mode))))
2988
2989 (defun gnus-group-list-groups (level &optional unread)
2990   "List newsgroups with level LEVEL or lower that have unread alticles.
2991 Default is all subscribed groups.
2992 If argument UNREAD is non-nil, groups with no unread articles are also listed."
2993   (interactive (list (and current-prefix-arg
2994                           (prefix-numeric-value current-prefix-arg))))
2995   (setq level (or level gnus-group-default-list-level gnus-level-subscribed))
2996   (gnus-group-setup-buffer)     ;May call from out of group buffer
2997   (let ((case-fold-search nil)
2998         (group (gnus-group-group-name)))
2999     (funcall gnus-group-prepare-function level unread nil)
3000     (if (zerop (buffer-size))
3001         (message gnus-no-groups-message)
3002       (goto-char (point-min))
3003       (if (not group)
3004           ;; Go to the first group with unread articles.
3005           (gnus-group-search-forward nil nil nil t)
3006         ;; Find the right group to put point on. If the current group
3007         ;; has disapeared in the new listing, try to find the next
3008         ;; one. If no next one can be found, just leave point at the
3009         ;; first newsgroup in the buffer.
3010         (if (not (gnus-goto-char
3011                   (text-property-any (point-min) (point-max) 
3012                                      'gnus-group (intern group))))
3013             (let ((newsrc (nthcdr 3 (gnus-gethash group gnus-newsrc-hashtb))))
3014               (while (and newsrc
3015                           (not (gnus-goto-char 
3016                                 (text-property-any 
3017                                  (point-min) (point-max) 'gnus-group 
3018                                  (intern (car (car newsrc)))))))
3019                 (setq newsrc (cdr newsrc)))
3020               (or newsrc (progn (goto-char (point-max))
3021                                 (forward-line -1))))))
3022       ;; Adjust cursor point.
3023       (gnus-group-position-cursor))))
3024
3025 (defun gnus-group-prepare-flat (level &optional all lowest regexp) 
3026   "List all newsgroups with unread articles of level LEVEL or lower.
3027 If ALL is non-nil, list groups that have no unread articles.
3028 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher.
3029 If REGEXP, only list groups matching REGEXP."
3030   (set-buffer gnus-group-buffer)
3031   (let ((buffer-read-only nil)
3032         (newsrc (cdr gnus-newsrc-alist))
3033         (lowest (or lowest 1))
3034         info clevel unread group)
3035     (erase-buffer)
3036     (if (< lowest gnus-level-zombie)
3037         ;; List living groups.
3038         (while newsrc
3039           (setq info (car newsrc)
3040                 group (car info)
3041                 newsrc (cdr newsrc)
3042                 unread (car (gnus-gethash group gnus-newsrc-hashtb)))
3043           (and unread ; This group might be bogus
3044                (or (not regexp)
3045                    (string-match regexp group))
3046                (<= (setq clevel (car (cdr info))) level) 
3047                (>= clevel lowest)
3048                (or all            ; We list all groups?
3049                    (eq unread t)  ; We list unactivated groups
3050                    (> unread 0)   ; We list groups with unread articles
3051                    (cdr (assq 'tick (nth 3 info)))) ; And groups with tickeds
3052                (gnus-group-insert-group-line 
3053                 nil group (car (cdr info)) (nth 3 info) unread (nth 4 info)))))
3054
3055     ;; List dead groups.
3056     (and (>= level gnus-level-zombie) (<= lowest gnus-level-zombie)
3057          (gnus-group-prepare-flat-list-dead 
3058           (setq gnus-zombie-list (sort gnus-zombie-list 'string<)) 
3059           gnus-level-zombie ?Z
3060           regexp))
3061     (and (>= level gnus-level-killed) (<= lowest gnus-level-killed)
3062          (gnus-group-prepare-flat-list-dead 
3063           (setq gnus-killed-list (sort gnus-killed-list 'string<)) 
3064           gnus-level-killed ?K regexp))
3065
3066     (gnus-group-set-mode-line)
3067     (setq gnus-have-all-newsgroups all)
3068     (run-hooks 'gnus-group-prepare-hook)))
3069
3070 (defun gnus-group-prepare-flat-list-dead (groups level mark regexp)
3071   ;; List zombies and killed lists somehwat faster, which was
3072   ;; suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>. It does
3073   ;; this by ignoring the group format specification altogether.
3074   (let (group beg)
3075     (while groups
3076       (setq group (car groups)
3077             groups (cdr groups))
3078       (if (or (not regexp)
3079               (string-match regexp group))
3080           (progn
3081             (setq beg (point))
3082             (insert (format " %c    *: %s\n" mark group))
3083             (add-text-properties 
3084              beg (1+ beg) 
3085              (list 'gnus-group (intern group)
3086                    'gnus-unread t
3087                    'gnus-level level)))))))
3088
3089 (defun gnus-group-real-name (group)
3090   "Find the real name of a foreign newsgroup."
3091   (if (string-match "^[^:]+:" group)
3092       (substring group (match-end 0))
3093     group))
3094
3095 (defun gnus-group-prefixed-name (group method)
3096   "Return the whole name from GROUP and METHOD."
3097   (and (stringp method) (setq method (gnus-server-to-method method)))
3098   (concat (format "%s" (car method))
3099           (if (and 
3100                (assoc (format "%s" (car method)) (gnus-methods-using 'address))
3101                (not (string= (nth 1 method) "")))
3102               (concat "+" (nth 1 method)))
3103           ":" group))
3104
3105 (defun gnus-group-real-prefix (group)
3106   "Return the prefix of the current group name."
3107   (if (string-match "^[^:]+:" group)
3108       (substring group 0 (match-end 0))
3109     ""))
3110
3111 (defun gnus-group-method-name (group)
3112   "Return the method used for selecting GROUP."
3113   (let ((prefix (gnus-group-real-prefix group)))
3114     (if (equal prefix "")
3115         gnus-select-method
3116       (if (string-match "^[^\\+]+\\+" prefix)
3117           (list (intern (substring prefix 0 (1- (match-end 0))))
3118                 (substring prefix (match-end 0) (1- (length prefix))))
3119         (list (intern (substring prefix 0 (1- (length prefix)))) "")))))
3120
3121 (defun gnus-group-foreign-p (group)
3122   "Return nil if GROUP is native, non-nil if it is foreign."
3123   (string-match ":" group))
3124
3125 (defun gnus-group-set-info (info &optional method-only-group part)
3126   (let* ((entry (gnus-gethash
3127                  (or method-only-group (car info)) gnus-newsrc-hashtb))
3128          (part-info info)
3129          (info (if method-only-group (nth 2 entry) info)))
3130     (if (not method-only-group)
3131         ()
3132       (or entry
3133           (error "Trying to change non-existant group %s" method-only-group))
3134       ;; We have recevied parts of the actual group info - either the
3135       ;; select method or the group parameters.  We first check
3136       ;; whether we have to extend the info, and if so, do that.
3137       (let ((len (length info))
3138             (total (if (eq part 'method) 5 6)))
3139         (and (< len total)
3140              (setcdr (nthcdr (1- len) info)
3141                      (make-list (- total len) nil)))
3142         ;; Then we enter the new info.
3143         (setcar (nthcdr (1- total) info) part-info)))
3144     ;; We uncompress some lists of marked articles.
3145     (let (marked)
3146       (if (not (setq marked (nth 3 info)))
3147           ()
3148         (while marked
3149           (or (eq 'score (car (car marked)))
3150               (eq 'bookmark (car (car marked)))
3151               (eq 'killed (car (car marked)))
3152               (setcdr (car marked) 
3153                       (gnus-uncompress-range (cdr (car marked)))))
3154           (setq marked (cdr marked)))))
3155     (if entry
3156         ()
3157       ;; This is a new group, so we just create it.
3158       (save-excursion
3159         (set-buffer gnus-group-buffer)
3160         (if (nth 4 info)
3161             ;; It's a foreign group...
3162             (gnus-group-make-group 
3163              (gnus-group-real-name (car info))
3164              (prin1-to-string (car (nth 4 info)))
3165              (nth 1 (nth 4 info)))
3166           ;; It's a native group.
3167           (gnus-group-make-group
3168            (car info)
3169            (prin1-to-string (car gnus-select-method))
3170            (nth 1 gnus-select-method)))
3171         (message "Note: New group created")
3172         (setq entry 
3173               (gnus-gethash (gnus-group-prefixed-name 
3174                              (gnus-group-real-name (car info))
3175                              (or (nth 4 info) gnus-select-method))
3176                             gnus-newsrc-hashtb))))
3177     ;; Whether it was a new group or not, we now have the entry, so we
3178     ;; can do the update.
3179     (if entry
3180         (progn
3181           (setcar (nthcdr 2 entry) info)
3182           (if (and (not (eq (car entry) t)) 
3183                    (gnus-gethash (car info) gnus-active-hashtb))
3184               (let ((marked (nth 3 info)))
3185                 (setcar entry 
3186                         (max 0 (- (length (gnus-list-of-unread-articles 
3187                                            (car info)))
3188                                   (length (cdr (assq 'tick marked)))
3189                                   (length (cdr (assq 'dormant marked)))))))))
3190       (error "No such group: %s" (car info)))))
3191
3192 (defun gnus-group-set-method-info (group select-method)
3193   (gnus-group-set-info select-method group 'method))
3194
3195 (defun gnus-group-set-params-info (group params)
3196   (gnus-group-set-info params group 'params))
3197
3198 (defun gnus-group-update-group-line ()
3199   "This function updates the current line in the newsgroup buffer and
3200 moves the point to the colon."
3201   (let* ((buffer-read-only nil)
3202          (group (gnus-group-group-name))
3203          (entry (and group (gnus-gethash group gnus-newsrc-hashtb))))
3204     (if entry
3205         (gnus-dribble-enter 
3206          (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
3207                  ")")))
3208     (beginning-of-line)
3209     (delete-region (point) (progn (forward-line 1) (point)))
3210     (gnus-group-insert-group-line-info group)
3211     (forward-line -1)
3212     (gnus-group-position-cursor)))
3213
3214 (defun gnus-group-insert-group-line-info (group)
3215   (let ((entry (gnus-gethash group gnus-newsrc-hashtb)) 
3216         active info)
3217     (if entry
3218         (progn
3219           (setq info (nth 2 entry))
3220           (gnus-group-insert-group-line 
3221            nil group (nth 1 info) (nth 3 info) (car entry) (nth 4 info)))
3222       (setq active (gnus-gethash group gnus-active-hashtb))
3223       (gnus-group-insert-group-line 
3224        nil group (if (member group gnus-zombie-list) gnus-level-zombie
3225                    gnus-level-killed)
3226        nil (if active (- (1+ (cdr active)) (car active)) 0) nil))))
3227
3228 (defun gnus-group-insert-group-line (gformat group level marked number method)
3229   (let* ((gformat (or gformat gnus-group-line-format-spec))
3230          (active (gnus-gethash group gnus-active-hashtb))
3231          (number-total (if active (1+ (- (cdr active) (car active))) 0))
3232          (number-of-dormant (length (cdr (assq 'dormant marked))))
3233          (number-of-ticked (length (cdr (assq 'tick marked))))
3234          (number-of-ticked-and-dormant
3235           (+ number-of-ticked number-of-dormant))
3236          (number-of-unread-unticked 
3237           (if (numberp number) (int-to-string (max 0 number))
3238             "*"))
3239          (number-of-read
3240           (if (numberp number)
3241               (max 0 (- number-total number))
3242             "*"))
3243          (subscribed (cond ((<= level gnus-level-subscribed) ? )
3244                            ((<= level gnus-level-unsubscribed) ?U)
3245                            ((= level gnus-level-zombie) ?Z)
3246                            (t ?K)))
3247          (qualified-group (gnus-group-real-name group))
3248          (newsgroup-description 
3249           (if gnus-description-hashtb
3250               (or (gnus-gethash group gnus-description-hashtb) "")
3251             ""))
3252          (moderated (if (member group gnus-moderated-list) ?m ? ))
3253          (moderated-string (if (eq moderated ?m) "(m)" ""))
3254          (method (gnus-server-get-method group method))
3255          (news-server (or (car (cdr method)) ""))
3256          (news-method (or (car method) ""))
3257          (news-method-string 
3258           (if method (format "(%s:%s)" (car method) (car (cdr method))) ""))
3259          (marked (if (and 
3260                       (numberp number) 
3261                       (zerop number)
3262                       (> number-of-ticked 0))
3263                      ?* ? ))
3264          (number (if (eq number t) "*" (+ number number-of-dormant 
3265                                           number-of-ticked)))
3266          (buffer-read-only nil)
3267          b)
3268     (beginning-of-line)
3269     (setq b (point))
3270     ;; Insert the text.
3271     (insert (eval gformat))
3272
3273     (add-text-properties 
3274      b (1+ b) (list 'gnus-group (intern group)
3275                     'gnus-unread (if (numberp number)
3276                                      (string-to-int number-of-unread-unticked)
3277                                    t)
3278                     'gnus-marked marked
3279                     'gnus-level level))))
3280
3281 (defun gnus-group-update-group (group &optional visible-only)
3282   "Update newsgroup info of GROUP.
3283 If VISIBLE-ONLY is non-nil, the group won't be displayed if it isn't already."
3284   (save-excursion
3285     (set-buffer gnus-group-buffer)
3286     (let ((buffer-read-only nil)
3287           visible)
3288       (let ((entry (gnus-gethash group gnus-newsrc-hashtb)))
3289         (if entry
3290             (gnus-dribble-enter 
3291              (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
3292                      ")"))))
3293       ;; Buffer may be narrowed.
3294       (save-restriction
3295         (widen)
3296         ;; Search a line to modify.  If the buffer is large, the search
3297         ;; takes long time.  In most cases, current point is on the line
3298         ;; we are looking for.  So, first of all, check current line. 
3299         (if (or (progn
3300                   (beginning-of-line)
3301                   (eq (get-text-property (point) 'gnus-group)
3302                       (intern group)))
3303                 (progn
3304                   (gnus-goto-char 
3305                    (text-property-any 
3306                     (point-min) (point-max) 'gnus-group (intern group)))))
3307             ;; GROUP is listed in current buffer. So, delete old line.
3308             (progn
3309               (setq visible t)
3310               (beginning-of-line)
3311               (delete-region (point) (progn (forward-line 1) (point))))
3312           ;; No such line in the buffer, find out where it's supposed to
3313           ;; go, and insert it there (or at the end of the buffer).
3314           ;; Fix by Per Abrahamsen <amanda@iesd.auc.dk>.
3315           (or visible-only
3316               (let ((entry (cdr (gnus-gethash group gnus-newsrc-hashtb))))
3317                 (while (and entry
3318                             (car entry)
3319                             (not
3320                              (gnus-goto-char
3321                               (text-property-any
3322                                (point-min) (point-max) 
3323                                'gnus-group (intern (car (car entry)))))))
3324                   (setq entry (cdr entry)))
3325                 (or entry (goto-char (point-max)))))))
3326       (if (or visible (not visible-only))
3327           (gnus-group-insert-group-line-info group))
3328       (gnus-group-set-mode-line))))
3329
3330 (defun gnus-group-set-mode-line ()
3331   (if (memq 'group gnus-updated-mode-lines)
3332       (let* ((gformat (or gnus-group-mode-line-format-spec
3333                           (setq gnus-group-mode-line-format-spec
3334                                 (gnus-parse-format 
3335                                  gnus-group-mode-line-format 
3336                                  gnus-group-mode-line-format-alist))))
3337              (news-server (car (cdr gnus-select-method)))
3338              (news-method (car gnus-select-method))
3339              (mode-string (eval gformat))
3340              (max-len 60))
3341         (if (> (length mode-string) max-len) 
3342             (setq mode-string (substring mode-string 0 (- max-len 4))))
3343         (setq mode-line-buffer-identification mode-string)
3344         (set-buffer-modified-p t))))
3345
3346 (defun gnus-group-group-name ()
3347   "Get the name of the newsgroup on the current line."
3348   (let ((group (get-text-property (gnus-point-at-bol) 'gnus-group)))
3349     (and group (symbol-name group))))
3350
3351 (defun gnus-group-group-level ()
3352   "Get the level of the newsgroup on the current line."
3353   (get-text-property (gnus-point-at-bol) 'gnus-level))
3354
3355 (defun gnus-group-search-forward (&optional backward all level first-too)
3356   "Find the next newsgroup with unread articles.
3357 If BACKWARD is non-nil, find the previous newsgroup instead.
3358 If ALL is non-nil, just find any newsgroup.
3359 If LEVEL is non-nil, find group with level LEVEL, or higher if no such
3360 group exists.
3361 If FIRST-TOO, the current line is also eligeble as a target."
3362   (let ((way (if backward -1 1))
3363         (low 10)
3364         (beg (point))
3365         pos found)
3366     (or first-too (forward-line way))
3367     (while (and 
3368             (not (eobp))
3369             (not (setq 
3370                   found 
3371                   (and (or all
3372                            (and
3373                             (let ((unread 
3374                                    (get-text-property (point) 'gnus-unread)))
3375                               (or (eq unread t) (and unread (> unread 0))))
3376                             (let ((lev (get-text-property
3377                                         (point) 'gnus-level)))
3378                               (and lev (<= (get-text-property 
3379                                            (point) 'gnus-level)
3380                                            gnus-level-subscribed)))))
3381                        (or (not level)
3382                            (let ((lev (get-text-property (point) 'gnus-level)))
3383                              (if (and lev (<= lev level))
3384                                  t
3385                                (if (< lev low)
3386                                    (progn
3387                                      (setq low lev)
3388                                      (setq pos (point))))
3389                                nil))))))
3390             (zerop (forward-line way))))
3391     (if found 
3392         (progn (gnus-group-position-cursor) t)
3393       (if pos (goto-char pos) (goto-char beg))
3394       nil)))
3395
3396 ;; Gnus group mode commands
3397
3398 (defun gnus-group-read-group (all &optional no-article group)
3399   "Read news in this newsgroup.
3400 If argument ALL is non-nil, already read articles become readable.
3401 If optional argument NO-ARTICLE is non-nil, no article body is displayed."
3402   (interactive "P")
3403   (let ((group (or group (gnus-group-group-name)))
3404         number active marked entry)
3405     (or group (error "No group on current line"))
3406     (setq marked 
3407           (nth 3 (nth 2 (setq entry (gnus-gethash group gnus-newsrc-hashtb)))))
3408     ;; This group might be a dead group. In that case we have to get
3409     ;; the number of unread articles from `gnus-active-hashtb'.
3410     (if entry
3411         (setq number (car entry))
3412       (if (setq active (gnus-gethash group gnus-active-hashtb))
3413           (setq number (- (1+ (cdr active)) (car active)))))
3414     (gnus-summary-read-group 
3415      group (or all (and (numberp number) 
3416                         (zerop (+ number (length (cdr (assq 'tick marked)))
3417                                   (length (cdr (assq 'dormant marked)))))))
3418      no-article)))
3419
3420 (defun gnus-group-select-group (all)
3421   "Select this newsgroup.
3422 No article is selected automatically.
3423 If argument ALL is non-nil, already read articles become readable."
3424   (interactive "P")
3425   (gnus-group-read-group all t))
3426
3427 (defun gnus-group-jump-to-group (group)
3428   "Jump to newsgroup GROUP."
3429   (interactive (list (completing-read "Group: " gnus-active-hashtb nil
3430                                       (not (not gnus-read-active-file)))))
3431
3432   (if (equal group "")
3433       (error "empty group name"))
3434
3435   (let ((b (text-property-any (point-min) (point-max) 
3436                               'gnus-group (intern group))))
3437     (if b
3438         ;; Either go to the line in the group buffer...
3439         (goto-char b)
3440       ;; ... or insert the line.
3441       (or
3442        (gnus-gethash group gnus-active-hashtb)
3443        (gnus-activate-newsgroup group)
3444        (error "%s error: %s" group (gnus-status-message group)))
3445
3446       (gnus-group-update-group group)
3447       (goto-char (text-property-any (point-min) (point-max) 
3448                                     'gnus-group (intern group)))))
3449   ;; Adjust cursor point.
3450   (gnus-group-position-cursor))
3451
3452 (defun gnus-group-next-group (n)
3453   "Go to next N'th newsgroup.
3454 If N is negative, search backward instead.
3455 Returns the difference between N and the number of skips actually
3456 done."
3457   (interactive "p")
3458   (gnus-group-next-unread-group n t))
3459
3460 (defun gnus-group-next-unread-group (n &optional all level)
3461   "Go to next N'th unread newsgroup.
3462 If N is negative, search backward instead.
3463 If ALL is non-nil, choose any newsgroup, unread or not.
3464 If LEVEL is non-nil, choose the next group with level LEVEL, or, if no
3465 such group can be found, the next group with a level higher than
3466 LEVEL.
3467 Returns the difference between N and the number of skips actually
3468 made."
3469   (interactive "p")
3470   (let ((backward (< n 0))
3471         (n (abs n)))
3472     (while (and (> n 0)
3473                 (gnus-group-search-forward backward all level))
3474       (setq n (1- n)))
3475     (if (/= 0 n) (message "No more%s newsgroups%s" (if all "" " unread")
3476                           (if level " on this level or higher" "")))
3477     n))
3478
3479 (defun gnus-group-prev-group (n)
3480   "Go to previous N'th newsgroup.
3481 Returns the difference between N and the number of skips actually
3482 done."
3483   (interactive "p")
3484   (gnus-group-next-unread-group (- n) t))
3485
3486 (defun gnus-group-prev-unread-group (n)
3487   "Go to previous N'th unread newsgroup.
3488 Returns the difference between N and the number of skips actually
3489 done."  
3490   (interactive "p")
3491   (gnus-group-next-unread-group (- n)))
3492
3493 (defun gnus-group-next-unread-group-same-level (n)
3494   "Go to next N'th unread newsgroup on the same level.
3495 If N is negative, search backward instead.
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 (gnus-group-group-level))
3500   (gnus-group-position-cursor))
3501
3502 (defun gnus-group-prev-unread-group-same-level (n)
3503   "Go to next N'th unread newsgroup on the same level.
3504 Returns the difference between N and the number of skips actually
3505 done."
3506   (interactive "p")
3507   (gnus-group-next-unread-group (- n) t (gnus-group-group-level))
3508   (gnus-group-position-cursor))
3509
3510 (defun gnus-group-best-unread-group (&optional exclude-group)
3511   "Go to the group with the highest level.
3512 If EXCLUDE-GROUP, do not go to that group."
3513   (interactive)
3514   (goto-char (point-min))
3515   (let ((best 10)
3516         unread best-point)
3517     (while (setq unread (get-text-property (point) 'gnus-unread))
3518       (if (and (numberp unread) (> unread 0))
3519           (progn
3520             (or best-point (setq best-point (point)))
3521             (if (and (< (get-text-property (point) 'gnus-level) best)
3522                      (or (not exclude-group)
3523                          (not (equal exclude-group (gnus-group-group-name)))))
3524                 (progn 
3525                   (setq best (get-text-property (point) 'gnus-level))
3526                   (setq best-point (point))))))
3527       (forward-line 1))
3528     (if best-point (goto-char best-point))
3529     (gnus-summary-position-cursor)
3530     (and best-point (gnus-group-group-name))))
3531
3532 (defun gnus-group-first-unread-group ()
3533   "Go to the first group with unread articles."
3534   (interactive)
3535   (goto-char (point-min))
3536   (or (get-text-property (point) 'gnus-unread)
3537       (gnus-group-next-unread-group 1))
3538   (gnus-group-position-cursor))
3539
3540 (defun gnus-group-enter-server-mode ()
3541   "Jump to the server buffer."
3542   (interactive)
3543   (gnus-server-setup-buffer)
3544   (switch-to-buffer gnus-server-buffer)
3545   (gnus-server-prepare))
3546
3547 (defun gnus-group-make-group (name method address)
3548   "Add a new newsgroup.
3549 The user will be prompted for a NAME, for a select METHOD, and an
3550 ADDRESS."
3551   (interactive
3552    (cons 
3553     (read-string "Group name: ")
3554     (let ((method
3555            (completing-read 
3556             "Method: " (append gnus-valid-select-methods gnus-server-alist)
3557             nil t)))
3558       (if (assoc method gnus-valid-select-methods)
3559           (list method
3560                 (if (memq 'prompt-address
3561                           (assoc method gnus-valid-select-methods))
3562                     (read-string "Address: ")
3563                   ""))
3564         (list method nil)))))
3565   
3566   (let* ((meth (if address (list (intern method) address) method))
3567          (nname (gnus-group-prefixed-name name meth))
3568          info)
3569     (and (gnus-gethash nname gnus-newsrc-hashtb)
3570          (error "Group %s already exists" nname))
3571     (gnus-group-change-level 
3572      (setq info (list t nname gnus-level-default-subscribed nil nil meth))
3573      gnus-level-default-subscribed gnus-level-killed 
3574      (gnus-gethash (or (gnus-group-group-name) "dummy.group")
3575                        gnus-newsrc-hashtb) t)
3576     (gnus-sethash nname '(0 . 0) gnus-active-hashtb)
3577     (gnus-dribble-enter 
3578      (concat "(gnus-group-set-info '" (prin1-to-string (cdr info)) ")"))
3579     (gnus-group-insert-group-line-info nname)
3580
3581     (and (gnus-check-backend-function 'request-create-group nname)
3582          (gnus-request-create-group nname))))
3583
3584 (defun gnus-group-edit-group (group &optional part)
3585   "Edit the group on the current line."
3586   (interactive (list (gnus-group-group-name)))
3587   (let ((done-func '(lambda () 
3588                       "Exit editing mode and update the information."
3589                       (interactive)
3590                       (gnus-group-edit-group-done 'part 'group)))
3591         (part (or part 'info))
3592         info)
3593     (if group (setq info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
3594       (error "No group on current line"))
3595     (setq gnus-winconf-edit-group (current-window-configuration))
3596     (pop-to-buffer gnus-group-edit-buffer)
3597     (gnus-add-current-to-buffer-list)
3598     (emacs-lisp-mode)
3599     ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3600     (use-local-map (copy-keymap emacs-lisp-mode-map))
3601     (local-set-key "\C-c\C-c" done-func)
3602     ;; We modify the func to let it know what part it is editing.
3603     (setcar (cdr (nth 4 done-func)) (list 'quote part))
3604     (setcar (cdr (cdr (nth 4 done-func))) group)
3605     (erase-buffer)
3606     (insert
3607      (cond 
3608       ((eq part 'method)
3609        ";; Type `C-c C-c' after editing the select method.\n\n")
3610       ((eq part 'params)
3611        ";; Type `C-c C-c' after editing the group parameters.\n\n")
3612       ((eq part 'info)
3613        ";; Type `C-c C-c' after editing the group info.\n\n")))
3614     (let ((cinfo (gnus-copy-sequence info))
3615           marked)
3616       (if (not (setq marked (nth 3 cinfo)))
3617           ()
3618         (while marked
3619           (or (eq 'score (car (car marked)))
3620               (eq 'bookmark (car (car marked)))
3621               (eq 'killed (car (car marked)))
3622               (setcdr (car marked) 
3623                       (gnus-compress-sequence (sort (cdr (car marked)) '<) t)))
3624           (setq marked (cdr marked))))
3625       (insert 
3626        (pp-to-string
3627         (cond ((eq part 'method)
3628                (or (nth 4 info) "native"))
3629               ((eq part 'params)
3630                (nth 5 info))
3631               (t
3632                cinfo)))
3633        "\n"))))
3634
3635 (defun gnus-group-edit-group-method (group)
3636   "Edit the select method of GROUP."
3637   (interactive (list (gnus-group-group-name)))
3638   (gnus-group-edit-group group 'method))
3639
3640 (defun gnus-group-edit-group-parameters (group)
3641   "Edit the group parameters of GROUP."
3642   (interactive (list (gnus-group-group-name)))
3643   (gnus-group-edit-group group 'params))
3644
3645 (defun gnus-group-edit-group-done (part group)
3646   "Get info from buffer, update variables and jump to the group buffer."
3647   (set-buffer (get-buffer-create gnus-group-edit-buffer))
3648   (goto-char (point-min))
3649   (let ((form (read (current-buffer))))
3650     (if (eq part 'info) 
3651         (gnus-group-set-info form)
3652       (gnus-group-set-info form group part))
3653     (kill-buffer (current-buffer))
3654     (and gnus-winconf-edit-group
3655          (set-window-configuration gnus-winconf-edit-group))
3656     (setq gnus-winconf-edit-group nil)
3657     (set-buffer gnus-group-buffer)
3658     (gnus-group-update-group (gnus-group-group-name))
3659     (gnus-group-position-cursor)))
3660
3661 (defun gnus-group-make-help-group ()
3662   "Create the (ding) Gnus documentation group."
3663   (interactive)
3664   (and (gnus-gethash (gnus-group-prefixed-name "gnus-help" '(nndoc ""))
3665                      gnus-newsrc-hashtb)
3666        (error "Documentation group already exists"))
3667   (let ((path load-path))
3668     (while (and path
3669                 (not (file-exists-p (concat (file-name-as-directory (car path))
3670                                             "doc.txt"))))
3671       (setq path (cdr path)))
3672     (or path (error "Couldn't find doc group"))
3673     (gnus-group-make-group 
3674      "gnus-help" "nndoc" 
3675      (concat (file-name-as-directory (car path)) "doc.txt"))
3676     (gnus-group-position-cursor)))
3677
3678 (defun gnus-group-make-archive-group ()
3679   "Create the (ding) Gnus archive group."
3680   (interactive)
3681   (and (gnus-gethash (gnus-group-prefixed-name "ding.archives" '(nndir ""))
3682                      gnus-newsrc-hashtb)
3683        (error "Archive group already exists"))
3684   (gnus-group-make-group "ding.archives" "nndir" gnus-group-archive-directory)
3685   (gnus-group-position-cursor))
3686
3687 (defun gnus-group-make-directory-group (dir)
3688   "Create an nndir group.
3689 The user will be prompted for a directory. The contents of this
3690 directory will be used as a newsgroup. The directory should contain
3691 mail messages or news articles in files that have numeric names."
3692   (interactive
3693    (list (read-file-name "Create group from directory: ")))
3694   (or (file-exists-p dir) (error "No such directory"))
3695   (or (file-directory-p dir) (error "Not a directory"))
3696   (gnus-group-make-group dir "nndir" dir)
3697   (gnus-group-position-cursor))
3698
3699 (defun gnus-group-make-kiboze-group (group address scores)
3700   "Create an nnkiboze group.
3701 The user will be prompted for a name, a regexp to match groups, and
3702 score file entries for articles to include in the group."
3703   (interactive
3704    (list
3705     (read-string "nnkiboze group name: ")
3706     (read-string "Source groups (regexp): ")
3707     (let ((headers (mapcar (lambda (group) (list group))
3708                            '("subject" "from" "number" "date" "message-id"
3709                              "references" "chars" "lines" "xref")))
3710           scores header regexp regexps)
3711       (while (not (equal "" (setq header (completing-read 
3712                                           "Match on header: " headers nil t))))
3713         (setq regexps nil)
3714         (while (not (equal "" (setq regexp (read-string 
3715                                             (format "Match on %s (string): "
3716                                                     header)))))
3717           (setq regexps (cons (list regexp nil 1000 nil) regexps)))
3718         (setq scores (cons (cons header regexps) scores)))
3719       (car scores))))
3720   (gnus-group-make-group group "nnkiboze" address)
3721   (save-excursion
3722     (gnus-set-work-buffer)
3723     (let (emacs-lisp-mode-hook)
3724       (pp (list 'setq 'gnus-score-alist 
3725                 (list 'quote (list scores)))
3726           (current-buffer)))
3727     (write-region (point-min) (point-max) 
3728                   (concat (or gnus-kill-files-directory "~/News")
3729                           "nnkiboze:" group "." gnus-score-file-suffix)))
3730   (gnus-group-position-cursor))
3731
3732 ;; Group sorting commands
3733 ;; Suggested by Joe Hildebrand <hildjj@idaho.fuentez.com>.
3734
3735 (defun gnus-group-sort-groups ()
3736   "Sort the group buffer using `gnus-group-sort-function'."
3737   (interactive)
3738   (setq gnus-newsrc-alist 
3739         (sort (cdr gnus-newsrc-alist) gnus-group-sort-function))
3740   (gnus-make-hashtable-from-newsrc-alist)
3741   (gnus-get-unread-articles (1+ gnus-level-subscribed))
3742   (gnus-group-list-groups nil))
3743
3744 (defun gnus-group-sort-by-alphabet (info1 info2)
3745   (string< (car info1) (car info2)))
3746
3747 (defun gnus-group-sort-by-unread (info1 info2)
3748   (let ((n1 (car (gnus-gethash (car info1) gnus-newsrc-hashtb)))
3749         (n2 (car (gnus-gethash (car info2) gnus-newsrc-hashtb))))
3750     (< (or (and (numberp n1) n1) 0)
3751        (or (and (numberp n2) n2) 0))))
3752
3753 (defun gnus-group-sort-by-level (info1 info2)
3754   (< (nth 1 info1) (nth 1 info2)))
3755
3756 ;; Group catching up.
3757
3758 (defun gnus-group-catchup-current (n &optional all)
3759   "Mark all articles not marked as unread in current newsgroup as read.
3760 If prefix argument N is numeric, the ARG next newsgroups will be
3761 caught up. If ALL is non-nil, marked articles will also be marked as
3762 read. Cross references (Xref: header) of articles are ignored.
3763 The difference between N and actual number of newsgroups that were
3764 caught up is returned."
3765   (interactive "p")
3766   (if (or (not gnus-interactive-catchup) ;Without confirmation?
3767           gnus-expert-user
3768           (gnus-y-or-n-p
3769            (if all
3770                "Do you really want to mark all articles as read? "
3771              "Mark all unread articles as read? ")))
3772       (progn
3773         (while 
3774             (and (> n 0)
3775                  (progn
3776                    (setq n (1- n))
3777                    (gnus-group-catchup (gnus-group-group-name) all)
3778                    (gnus-group-update-group-line)
3779                    t)
3780                  (zerop (gnus-group-next-unread-group 1))))))
3781   n)
3782
3783 (defun gnus-group-catchup-current-all (n)
3784   "Mark all articles in current newsgroup as read.
3785 Cross references (Xref: header) of articles are ignored."
3786   (interactive "p")
3787   (gnus-group-catchup-current n 'all))
3788
3789 (defun gnus-group-catchup (group &optional all)
3790   "Mark all articles in GROUP as read.
3791 If ALL is non-nil, all articles are marked as read.
3792 The return value is the number of articles that were marked as read,
3793 or nil if no action could be taken."
3794   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
3795          (num (car entry))
3796          (marked (nth 3 (nth 2 entry)))
3797          ticked)
3798     (if (not (numberp (car entry)))
3799         (message "Can't catch up; non-active group")
3800       ;; Do the updating only if the newsgroup isn't killed
3801       (if entry
3802           (progn
3803             (setq ticked (if all nil (cdr (assq 'tick marked))))
3804             (gnus-update-read-articles group ticked nil ticked)
3805             (if (and all marked)
3806                 (setcar (nthcdr 3 (nth 2 entry)) 
3807                         (delq (assq 'dormant marked) marked))))))
3808     num))
3809
3810 (defun gnus-group-expire-articles (newsgroup)
3811   "Expire all expirable articles in the current newsgroup."
3812   (interactive (list (gnus-group-group-name)))
3813   (if (not newsgroup) (error "No current newsgroup"))
3814   (let ((expirable 
3815          (assq 'expire (nth 3 (nth 2 (gnus-gethash newsgroup 
3816                                                    gnus-newsrc-hashtb))))))
3817  (and expirable 
3818       (gnus-check-backend-function 'request-expire-articles newsgroup)
3819       (setcdr expirable
3820               (gnus-request-expire-articles (cdr expirable) newsgroup)))))
3821
3822 (defun gnus-group-expire-all-groups ()
3823   "Expire all expirable articles in all newsgroups."
3824   (interactive)
3825   (message "Expiring...")
3826   (let ((newsrc (cdr gnus-newsrc-alist)))
3827     (while newsrc
3828       (gnus-group-expire-articles (car (car newsrc)))
3829       (setq newsrc (cdr newsrc))))
3830   (message "Expiring...done"))
3831
3832 (defun gnus-group-set-current-level (n level)
3833   "Set the level of the next N groups to LEVEL."
3834   (interactive "p\nnLevel: ")
3835   (or (and (>= level 1) (<= level gnus-level-killed))
3836       (error "Illegal level: %d" level))
3837   (let (group)
3838     (while (and (> n 0)
3839                 (setq group (gnus-group-group-name)))
3840       (and (setq group (gnus-group-group-name))
3841            (message "Changed level of %s from %d to %d" 
3842                     group (gnus-group-group-level) level)
3843            (gnus-group-change-level group level
3844                                     (gnus-group-group-level))
3845            (gnus-group-update-group-line))
3846       (forward-line 1)
3847       (setq n (1- n))))
3848   (gnus-group-position-cursor)
3849   n)
3850
3851 (defun gnus-group-unsubscribe-current-group (arg)
3852   "Toggle subscribe from/to unsubscribe current group."
3853   (interactive "P")
3854   (let ((group (gnus-group-group-name)))
3855     (or group (error "No newsgroup on current line"))
3856     (or arg (setq arg (if (<= (gnus-group-group-level) gnus-level-subscribed)
3857                           gnus-level-default-unsubscribed
3858                         gnus-level-default-subscribed)))
3859     (gnus-group-unsubscribe-group group arg)
3860     (gnus-group-next-group 1)))
3861
3862 (defun gnus-group-unsubscribe-group (group &optional level)
3863   "Toggle subscribe from/to unsubscribe GROUP.
3864 New newsgroup is added to .newsrc automatically."
3865   (interactive
3866    (list (completing-read "Group: " gnus-active-hashtb nil 
3867                           gnus-have-read-active-file)))
3868   (let ((newsrc (gnus-gethash group gnus-newsrc-hashtb)))
3869     (cond (newsrc
3870            ;; Toggle subscription flag.
3871            (gnus-group-change-level 
3872             newsrc (if level level (if (<= (nth 1 (nth 2 newsrc)) 
3873                                            gnus-level-subscribed) 
3874                                        (1+ gnus-level-subscribed)
3875                                      gnus-level-default-subscribed)))
3876            (gnus-group-update-group group))
3877           ((and (stringp group)
3878                 (or (not gnus-have-read-active-file)
3879                     (gnus-gethash group gnus-active-hashtb)))
3880            ;; Add new newsgroup.
3881            (gnus-group-change-level 
3882             group 
3883             (if level level gnus-level-default-subscribed) 
3884             (or (and (member group gnus-zombie-list) 
3885                      gnus-level-zombie) 
3886                 gnus-level-killed)
3887             (or (and (gnus-group-group-name)
3888                      (gnus-gethash (gnus-group-group-name) gnus-newsrc-hashtb))
3889                 (gnus-gethash (car (car gnus-newsrc-alist)) 
3890                               gnus-newsrc-hashtb)))
3891            (gnus-group-update-group group))
3892           (t (error "No such newsgroup: %s" group)))
3893     (gnus-group-position-cursor)))
3894
3895 (defun gnus-group-transpose-groups (arg)
3896   "Exchange current newsgroup and previous newsgroup.
3897 With argument ARG, takes previous newsgroup and moves it past ARG newsgroup."
3898   (interactive "p")
3899   ;; BUG: last newsgroup and the last but one cannot be transposed
3900   ;; since gnus-group-search-forward does not move forward beyond the
3901   ;; last.  If we instead use forward-line, no problem, but I don't
3902   ;; want to use it for later extension.
3903   (while (> arg 0)
3904     (gnus-group-search-forward t t)
3905     (gnus-group-kill-group 1)
3906     (gnus-group-search-forward nil t)
3907     (gnus-group-yank-group)
3908     (gnus-group-search-forward nil t)
3909     (setq arg (1- arg))))
3910
3911 (defun gnus-group-kill-all-zombies ()
3912   "Kill all zombie newsgroups."
3913   (interactive)
3914   (setq gnus-killed-list (nconc gnus-zombie-list gnus-killed-list))
3915   (setq gnus-zombie-list nil)
3916   (funcall gnus-group-prepare-function gnus-level-subscribed nil nil)
3917   (goto-char (point-min))
3918   (gnus-group-position-cursor))
3919
3920 (defun gnus-group-kill-region (begin end)
3921   "Kill newsgroups in current region (excluding current point).
3922 The killed newsgroups can be yanked by using \\[gnus-group-yank-group]."
3923   (interactive "r")
3924   (let ((lines
3925          ;; Exclude a line where current point is on.
3926          (1-
3927           ;; Count lines.
3928           (save-excursion
3929             (count-lines
3930              (progn
3931                (goto-char begin)
3932                (beginning-of-line)
3933                (point))
3934              (progn
3935                (goto-char end)
3936                (end-of-line)
3937                (point)))))))
3938     (goto-char begin)
3939     (beginning-of-line)                 ;Important when LINES < 1
3940     (gnus-group-kill-group lines)))
3941
3942 (defun gnus-group-kill-group (n)
3943   "The the next N groups.
3944 The killed newsgroups can be yanked by using \\[gnus-group-yank-group].
3945 However, only groups that were alive can be yanked; already killed 
3946 groups or zombie groups can't be yanked.
3947 The return value is the name of the (last) newsgroup that was killed."
3948   (interactive "p")
3949   (let ((buffer-read-only nil)
3950         group entry level)
3951     (while (>= (setq n  (1- n)) 0)
3952       (setq group (gnus-group-group-name))
3953       (or group
3954           (signal 'end-of-buffer nil))
3955       (setq level (gnus-group-group-level))
3956       (beginning-of-line)
3957       (delete-region (point) (progn (forward-line 1) (point)))
3958       (if (setq entry (gnus-gethash group gnus-newsrc-hashtb))
3959           (setq gnus-list-of-killed-groups 
3960                 (cons (cons (car entry) (nth 2 entry)) 
3961                       gnus-list-of-killed-groups)))
3962       (gnus-group-change-level 
3963        (if entry entry group) gnus-level-killed (if entry nil level)))
3964     (if (eobp)
3965         (forward-line -1))
3966     (gnus-group-position-cursor)
3967     group))
3968
3969 (defun gnus-group-yank-group (&optional arg)
3970   "Yank the last newsgroups killed with \\[gnus-group-kill-group],
3971 inserting it before the current newsgroup.  The numeric ARG specifies
3972 how many newsgroups are to be yanked.  The name of the (last)
3973 newsgroup yanked is returned."
3974   (interactive "p")
3975   (if (not arg) (setq arg 1))
3976   (let (info group prev)
3977     (while (>= (setq arg (1- arg)) 0)
3978       (if (not (setq info (car gnus-list-of-killed-groups)))
3979           (error "No more newsgroups to yank"))
3980       (setq group (nth 2 info))
3981       ;; Find which newsgroup to insert this one before - search
3982       ;; backward until something suitable is found. If there are no
3983       ;; other newsgroups in this buffer, just make this newsgroup the
3984       ;; first newsgroup.
3985       (while (and (not (setq prev (gnus-group-group-name)))
3986                   (zerop (forward-line -1))))
3987       (if (not prev)
3988           (setq prev (car (car gnus-newsrc-alist))))
3989       (gnus-group-change-level 
3990        info (nth 2 info) gnus-level-killed 
3991        (gnus-gethash prev gnus-newsrc-hashtb)
3992        t)
3993       (gnus-group-insert-group-line-info (nth 1 info))
3994       (setq gnus-list-of-killed-groups 
3995             (cdr gnus-list-of-killed-groups)))
3996     (forward-line -1)
3997     (gnus-group-position-cursor)
3998     group))
3999       
4000 (defun gnus-group-list-all-groups (arg)
4001   "List all newsgroups with level ARG or lower.
4002 Default is 7, which lists all subscribed and most unsubscribed groups."
4003   (interactive "P")
4004   (setq arg (or arg 7))
4005   (gnus-group-list-groups arg t))
4006
4007 (defun gnus-group-list-killed ()
4008   "List all killed newsgroups in the group buffer."
4009   (interactive)
4010   (if (not gnus-killed-list)
4011       (message "No killed groups")
4012     (funcall gnus-group-prepare-function gnus-level-killed t gnus-level-killed)
4013     (goto-char (point-min)))
4014   (gnus-group-position-cursor))
4015
4016 (defun gnus-group-list-zombies ()
4017   "List all zombie newsgroups in the group buffer."
4018   (interactive)
4019   (if (not gnus-zombie-list)
4020       (message "No zombie groups")
4021     (funcall gnus-group-prepare-function gnus-level-zombie t gnus-level-zombie)
4022     (goto-char (point-min)))
4023   (gnus-group-position-cursor))
4024
4025 (defun gnus-group-get-new-news (&optional arg)
4026   "Get newly arrived articles.
4027 If ARG is non-nil, it should be a number between one and nine to
4028 specify which levels you are interested in re-scanning."
4029   (interactive "P")
4030   (run-hooks 'gnus-get-new-news-hook)
4031   (let ((level arg))
4032     (if (and gnus-read-active-file (not level))
4033         (progn
4034           (gnus-read-active-file)
4035           (gnus-get-unread-articles (or level (1+ gnus-level-subscribed))))
4036       (let ((gnus-read-active-file nil))
4037         (gnus-get-unread-articles (or level (1+ gnus-level-subscribed)))))
4038     (gnus-group-list-groups 
4039      (or gnus-group-always-list-unread level gnus-level-subscribed)
4040      gnus-have-all-newsgroups)))
4041
4042 (defun gnus-group-get-new-news-this-group (n)
4043   "Check for newly arrived news in the current group (and the N-1 next groups).
4044 The difference between N and the number of newsgroup checked is returned.
4045 If N is negative, this group and the N-1 previous groups will be checked."
4046   (interactive "p")
4047   (let ((way (if (< n 0) -1 1))
4048         (n (abs n))
4049         (w-p (window-start))
4050         group)
4051     (while (and (> n 0)
4052                 (progn
4053                   (or (gnus-get-new-news-in-group
4054                        (setq group (gnus-group-group-name)))
4055                       (progn 
4056                         (ding) 
4057                         (message "%s error: %s" 
4058                                  group (gnus-status-message group))))
4059                   t)
4060                 (zerop (gnus-group-next-group way)))
4061       (setq n (1- n)))
4062     (if (/= 0 n) (message "No more newsgroups"))
4063     ;; !!! I don't know why the buffer scrolls forward when updating
4064     ;; the first line in the group buffer, but it does. So we set the
4065     ;; window start forcibly.
4066     (set-window-start (get-buffer-window (current-buffer)) w-p)
4067     n))
4068
4069 (defun gnus-get-new-news-in-group (group)
4070   (and group 
4071        (gnus-activate-newsgroup group)
4072        (progn
4073          (gnus-get-unread-articles-in-group 
4074           (nth 2 (gnus-gethash group gnus-newsrc-hashtb))
4075           (gnus-gethash group gnus-active-hashtb))
4076          (gnus-group-update-group-line)
4077          t)))
4078
4079 (defun gnus-group-fetch-faq (group)
4080   "Fetch the FAQ for the current group."
4081   (interactive (list (gnus-group-group-name)))
4082   (or group (error "No group name given"))
4083   (let ((file (concat gnus-group-faq-directory group))) 
4084     (if (not (file-exists-p file))
4085         (error "No such file: %s" file)
4086       (find-file file))))
4087   
4088 (defun gnus-group-describe-group (force &optional group)
4089   "Display a description of the current newsgroup."
4090   (interactive "P")
4091   (and force (setq gnus-description-hashtb nil))
4092   (let ((group (or group (gnus-group-group-name)))
4093         desc)
4094     (or group (message "No group name given"))
4095     (and (or gnus-description-hashtb
4096              (setq desc (gnus-group-get-description group))
4097              (gnus-read-descriptions-file))
4098          (message
4099           (or desc (gnus-gethash group gnus-description-hashtb)
4100               "No description available")))))
4101
4102 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
4103 (defun gnus-group-describe-all-groups (force)
4104   "Pop up a buffer with descriptons of all newsgroups."
4105   (interactive "P")
4106   (and force (setq gnus-description-hashtb nil))
4107   (if (not (or gnus-description-hashtb
4108                (gnus-read-descriptions-file)))
4109       (error "Couldn't request descriptions file"))
4110   (let ((buffer-read-only nil)
4111         b)
4112     (erase-buffer)
4113     (mapatoms
4114      (lambda (group)
4115        (setq b (point))
4116        (insert (format "      *: %-20s %s\n" (symbol-name group)
4117                        (symbol-value group)))
4118        (add-text-properties 
4119         b (1+ b) (list 'gnus-group group
4120                        'gnus-unread t 'gnus-marked nil
4121                        'gnus-level (1+ gnus-level-subscribed))))
4122      gnus-description-hashtb)
4123     (goto-char (point-min))
4124     (gnus-group-position-cursor)))
4125
4126 ;; Suggested by by Daniel Quinlan <quinlan@best.com>.
4127 (defun gnus-group-apropos (regexp &optional search-description)
4128   "List all newsgroups that have names that match a regexp."
4129   (interactive "sGnus apropos (regexp): ")
4130   (let ((prev "")
4131         (obuf (current-buffer))
4132         groups des prev)
4133     ;; Go through all newsgroups that are known to Gnus.
4134     (mapatoms 
4135      (lambda (group)
4136        (and (string-match regexp (symbol-name group))
4137             (setq groups (cons (symbol-name group) groups))))
4138      gnus-active-hashtb)
4139     ;; Go through all descriptions that are known to Gnus. 
4140     (if search-description
4141         (mapatoms 
4142          (lambda (group)
4143            (and (string-match regexp (symbol-value group))
4144                 (gnus-gethash (symbol-name group) gnus-active-hashtb)
4145                 (setq groups (cons (symbol-name group) groups))))
4146          gnus-description-hashtb))
4147     (if (not groups)
4148         (message "No groups matched \"%s\"." regexp)
4149       ;; Print out all the groups.
4150       (save-excursion
4151         (pop-to-buffer "*Gnus Help*")
4152         (buffer-disable-undo (current-buffer))
4153         (erase-buffer)
4154         (setq groups (sort groups 'string<))
4155         (while groups
4156           ;; Groups may be entered twice into the list of groups.
4157           (if (not (string= (car groups) prev))
4158               (progn
4159                 (insert (setq prev (car groups)) "\n")
4160                 (if (and gnus-description-hashtb
4161                          (setq des (gnus-gethash (car groups) 
4162                                                  gnus-description-hashtb)))
4163                     (insert "  " des "\n"))))
4164           (setq groups (cdr groups)))
4165         (goto-char 1)))
4166     (pop-to-buffer obuf)))
4167
4168 (defun gnus-group-description-apropos (regexp)
4169   "List all newsgroups that have names or desccriptions that match a regexp."
4170   (interactive "sGnus description apropos (regexp): ")
4171   (if (not (or gnus-description-hashtb
4172                (gnus-read-descriptions-file)))
4173       (error "Couldn't request descriptions file"))
4174   (gnus-group-apropos regexp t))
4175
4176 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
4177 (defun gnus-group-list-matching (level regexp &optional all lowest) 
4178   "List all groups with unread articles that match REGEXP.
4179 If the prefix LEVEL is non-nil, it should be a number that says which
4180 level to cut off listing groups. 
4181 If ALL, also list groups with no unread articles.
4182 If LOWEST, don't list groups with level lower than LOWEST."
4183   (interactive "P\nsList newsgroups matching: ")
4184   (gnus-group-prepare-flat (or level gnus-level-subscribed)
4185                            all (or lowest 1) regexp)
4186   (goto-char (point-min))
4187   (gnus-group-position-cursor))
4188
4189 (defun gnus-group-list-all-matching (level regexp &optional lowest) 
4190   "List all groups that match REGEXP.
4191 If the prefix LEVEL is non-nil, it should be a number that says which
4192 level to cut off listing groups. 
4193 If LOWEST, don't list groups with level lower than LOWEST."
4194   (interactive "P\nsList newsgroups matching: ")
4195   (gnus-group-list-matching (or level gnus-level-killed) regexp t lowest))
4196
4197 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
4198 (defun gnus-group-save-newsrc ()
4199   "Save the Gnus startup files."
4200   (interactive)
4201   (gnus-save-newsrc-file))
4202
4203 (defun gnus-group-restart (&optional arg)
4204   "Force Gnus to read the .newsrc file."
4205   (interactive "P")
4206   (gnus-save-newsrc-file)
4207   (gnus-setup-news 'force)
4208   (gnus-group-list-groups arg gnus-have-all-newsgroups))
4209
4210 (defun gnus-group-read-init-file ()
4211   "Read the Gnus elisp init file."
4212   (interactive)
4213   (gnus-read-init-file))
4214
4215 (defun gnus-group-check-bogus-groups ()
4216   "Check bogus newsgroups."
4217   (interactive)
4218   (gnus-check-bogus-newsgroups (not gnus-expert-user))  ;Require confirmation.
4219   (gnus-group-list-groups nil gnus-have-all-newsgroups))
4220
4221 (defun gnus-group-mail ()
4222   "Start composing a mail."
4223   (interactive)
4224   (mail))
4225
4226 (defun gnus-group-edit-global-kill (article &optional group)
4227   "Edit the global kill file.
4228 If GROUP, edit that local kill file instead."
4229   (interactive "P")
4230   (setq gnus-current-kill-article article)
4231   (gnus-kill-file-edit-file group)
4232   (message
4233    (substitute-command-keys
4234     "Editing a global kill file (Type \\[gnus-kill-file-exit] to exit)")))
4235
4236 (defun gnus-group-edit-local-kill (article group)
4237   "Edit a local kill file."
4238   (interactive (list nil (gnus-group-group-name)))
4239   (gnus-group-edit-global-kill article group))
4240
4241 (defun gnus-group-force-update ()
4242   "Update `.newsrc' file."
4243   (interactive)
4244   (gnus-save-newsrc-file))
4245
4246 (defun gnus-group-suspend ()
4247   "Suspend the current Gnus session.
4248 In fact, cleanup buffers except for group mode buffer.
4249 The hook gnus-suspend-gnus-hook is called before actually suspending."
4250   (interactive)
4251   (run-hooks 'gnus-suspend-gnus-hook)
4252   ;; Kill Gnus buffers except for group mode buffer.
4253   (let ((group-buf (get-buffer gnus-group-buffer)))
4254     ;; Do this on a separate list in case the user does a ^G before we finish
4255     (let ((gnus-buffer-list
4256            (delq group-buf (delq gnus-dribble-buffer
4257                                  (append gnus-buffer-list nil)))))
4258       (while gnus-buffer-list
4259         (gnus-kill-buffer (car gnus-buffer-list))
4260         (setq gnus-buffer-list (cdr gnus-buffer-list))))
4261     (if group-buf
4262         (progn
4263           (setq gnus-buffer-list (list group-buf))
4264           (bury-buffer group-buf)
4265           (delete-windows-on group-buf t)))))
4266
4267 (defun gnus-group-clear-dribble ()
4268   "Clear all information from the dribble buffer."
4269   (interactive)
4270   (gnus-dribble-clear))
4271
4272 (defun gnus-group-exit ()
4273   "Quit reading news after updating .newsrc.eld and .newsrc.
4274 The hook `gnus-exit-gnus-hook' is called before actually exiting."
4275   (interactive)
4276   (if (or noninteractive                ;For gnus-batch-kill
4277           (zerop (buffer-size))         ;No news is good news.
4278           (not (gnus-server-opened gnus-select-method)) ;NNTP connection closed
4279           (not gnus-interactive-exit)   ;Without confirmation
4280           gnus-expert-user
4281           (gnus-y-or-n-p "Are you sure you want to quit reading news? "))
4282       (progn
4283         (run-hooks 'gnus-exit-gnus-hook)
4284         (gnus-save-newsrc-file)
4285         (gnus-close-backends)
4286         (gnus-clear-system))))
4287
4288 (defun gnus-close-backends ()
4289   ;; Send a close request to all backends that support such a request. 
4290   (let ((methods gnus-valid-select-methods)
4291         func)
4292     (while methods
4293       (if (fboundp (setq func (intern (concat (car (car methods))
4294                                               "-request-close"))))
4295           (funcall func))
4296       (setq methods (cdr methods)))))
4297
4298 (defun gnus-group-quit ()
4299   "Quit reading news without updating .newsrc.eld or .newsrc.
4300 The hook `gnus-exit-gnus-hook' is called before actually exiting."
4301   (interactive)
4302   (if (or noninteractive                ;For gnus-batch-kill
4303           (zerop (buffer-size))
4304           (not (gnus-server-opened gnus-select-method))
4305           gnus-expert-user
4306           (not gnus-current-startup-file)
4307           (gnus-yes-or-no-p
4308            (format "Quit reading news without saving %s? "
4309                    (file-name-nondirectory gnus-current-startup-file))))
4310       (progn
4311         (run-hooks 'gnus-exit-gnus-hook)
4312         (gnus-dribble-save)
4313         (gnus-close-backends)
4314         (gnus-clear-system))))
4315
4316 (defun gnus-group-describe-briefly ()
4317   "Give a one line description of the group mode commands."
4318   (interactive)
4319   (message
4320    (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")))
4321
4322 (defun gnus-group-browse-foreign-server (method)
4323   "Browse a foreign news server.
4324 If called interactively, this function will ask for a select method
4325  (nntp, nnspool, etc.) and a server address (eg. nntp.some.where). 
4326 If not, METHOD should be a list where the first element is the method
4327 and the second element is the address."
4328   (interactive
4329    (list (list (intern (completing-read 
4330                         "Which backend: "
4331                         gnus-valid-select-methods nil t "nntp"))
4332                ;; Suggested by mapjph@bath.ac.uk.
4333                (completing-read 
4334                 "Address: " 
4335                 (mapcar (lambda (server) (list server))
4336                         gnus-secondary-servers)))))
4337   (gnus-browse-foreign-server method))
4338
4339 \f
4340 ;;;
4341 ;;; Browse Server Mode
4342 ;;;
4343
4344 (defvar gnus-browse-server-mode-hook nil)
4345 (defvar gnus-browse-server-mode-map nil)
4346 (put 'gnus-browse-server-mode 'mode-class 'special)
4347
4348 (if gnus-browse-server-mode-map
4349     nil
4350   (setq gnus-browse-server-mode-map (make-keymap))
4351   (suppress-keymap gnus-browse-server-mode-map)
4352   (define-key gnus-browse-server-mode-map " " 'gnus-browse-read-group)
4353   (define-key gnus-browse-server-mode-map "=" 'gnus-browse-read-group)
4354   (define-key gnus-browse-server-mode-map "n" 'gnus-browse-next-group)
4355   (define-key gnus-browse-server-mode-map "p" 'gnus-browse-prev-group)
4356   (define-key gnus-browse-server-mode-map "\177" 'gnus-browse-prev-group)
4357   (define-key gnus-browse-server-mode-map "N" 'gnus-browse-next-group)
4358   (define-key gnus-browse-server-mode-map "P" 'gnus-browse-prev-group)
4359   (define-key gnus-browse-server-mode-map "\M-n" 'gnus-browse-next-group)
4360   (define-key gnus-browse-server-mode-map "\M-p" 'gnus-browse-prev-group)
4361   (define-key gnus-browse-server-mode-map "\r" 'gnus-browse-read-group)
4362   (define-key gnus-browse-server-mode-map "u" 'gnus-browse-unsubscribe-current-group)
4363   (define-key gnus-browse-server-mode-map "l" 'gnus-browse-exit)
4364   (define-key gnus-browse-server-mode-map "L" 'gnus-browse-exit)
4365   (define-key gnus-browse-server-mode-map "q" 'gnus-browse-exit)
4366   (define-key gnus-browse-server-mode-map "Q" 'gnus-browse-exit)
4367   (define-key gnus-browse-server-mode-map "\C-c\C-c" 'gnus-browse-exit)
4368   (define-key gnus-browse-server-mode-map "?" 'gnus-browse-describe-briefly)
4369   (define-key gnus-browse-server-mode-map "\C-c\C-i" 'gnus-info-find-node)
4370   )
4371
4372 (defvar gnus-browse-current-method nil)
4373
4374 (defun gnus-browse-foreign-server (method)
4375   (setq gnus-browse-current-method method)
4376   (let ((gnus-select-method method)
4377         groups group)
4378     (message "Connecting to %s..." (nth 1 method))
4379     (or (gnus-server-opened method)
4380         (gnus-open-server method)
4381         (error "Unable to contact server: %s" (gnus-status-message method)))
4382     (or (gnus-request-list method)
4383         (error "Couldn't request list: %s" (gnus-status-message method)))
4384     (set-buffer (get-buffer-create "*Gnus Browse Server*"))
4385     (gnus-add-current-to-buffer-list)
4386     (buffer-disable-undo (current-buffer))
4387     (let ((buffer-read-only nil))
4388       (erase-buffer))
4389     (gnus-browse-server-mode)
4390     (setq mode-line-buffer-identification
4391           (format
4392            "(ding) Browse Server {%s:%s}" (car method) (car (cdr method))))
4393     (save-excursion
4394       (set-buffer nntp-server-buffer)
4395       (let ((cur (current-buffer)))
4396         (goto-char 1)
4397         (delete-matching-lines gnus-ignored-newsgroups)
4398         (while (re-search-forward 
4399                 "\\(^[^ \t]+\\)[ \t]+[0-9]+[ \t]+[0-9]+" nil t)
4400           (goto-char (match-end 1))
4401           (setq groups (cons (cons (buffer-substring (match-beginning 1)
4402                                                      (match-end 1))
4403                                    (max 0 (- (1+ (read cur)) (read cur))))
4404                              groups)))))
4405     (setq groups (sort groups 
4406                        (lambda (l1 l2)
4407                          (string< (car l1) (car l2)))))
4408     (let ((buffer-read-only nil))
4409       (while groups
4410         (setq group (car groups))
4411         (insert 
4412          (format "K%7d: %s\n" (cdr group) (car group)))
4413         (setq groups (cdr groups))))
4414     (switch-to-buffer (current-buffer))
4415     (goto-char 1)
4416     (gnus-group-position-cursor)))
4417
4418 (defun gnus-browse-server-mode ()
4419   "Major mode for browsing a foreign server."
4420   (interactive)
4421   (kill-all-local-variables)
4422   (setq mode-line-modified "-- ")
4423   (make-local-variable 'mode-line-format)
4424   (setq mode-line-format (copy-sequence mode-line-format))
4425   (and (equal (nth 3 mode-line-format) "   ")
4426        (setcar (nthcdr 3 mode-line-format) ""))
4427   (setq major-mode 'gnus-browse-server-mode)
4428   (setq mode-name "Browse Server")
4429   (setq mode-line-process nil)
4430   (use-local-map gnus-browse-server-mode-map)
4431   (buffer-disable-undo (current-buffer))
4432   (setq truncate-lines t)
4433   (setq buffer-read-only t)
4434   (run-hooks 'gnus-browse-server-mode-hook))
4435
4436 (defun gnus-browse-read-group ()
4437   "Not implemented, and will probably never be."
4438   (interactive)
4439   (error "You can't read while browsing"))
4440
4441 (defun gnus-browse-next-group (n)
4442   "Go to the next group."
4443   (interactive "p")
4444   (prog1
4445       (forward-line n)
4446     (gnus-group-position-cursor)))
4447
4448 (defun gnus-browse-prev-group (n)
4449   "Go to the next group."
4450   (interactive "p")
4451   (gnus-browse-next-group (- n)))
4452
4453 (defun gnus-browse-unsubscribe-current-group (arg)
4454   "(Un)subscribe to the next ARG groups."
4455   (interactive "p")
4456   (and (eobp)
4457        (error "No group at current line."))
4458   (let ((ward (if (< arg 0) -1 1))
4459         (arg (abs arg)))
4460     (while (and (> arg 0)
4461                 (not (eobp))
4462                 (gnus-browse-unsubscribe-group)
4463                 (zerop (gnus-browse-next-group ward)))
4464       (setq arg (1- arg)))
4465     (gnus-group-position-cursor)
4466     (if (/= 0 arg) (message "No more newsgroups"))
4467     arg))
4468   
4469 (defun gnus-browse-unsubscribe-group ()
4470   (let ((sub nil)
4471         (buffer-read-only nil)
4472         group)
4473     (save-excursion
4474       (beginning-of-line)
4475       (if (= (following-char) ?K) (setq sub t))
4476       (re-search-forward ": \\(.*\\)$" nil t)
4477       (setq group (gnus-group-prefixed-name 
4478                    (buffer-substring (match-beginning 1) (match-end 1))
4479                    gnus-browse-current-method))
4480       (beginning-of-line)
4481       (delete-char 1)
4482       (if sub
4483           (progn
4484             (gnus-group-change-level 
4485              (list t group gnus-level-default-subscribed
4486                    nil nil gnus-browse-current-method) 
4487              gnus-level-default-subscribed gnus-level-killed
4488              (gnus-gethash (car (nth 1 gnus-newsrc-alist)) gnus-newsrc-hashtb)
4489              t)
4490             (insert ? ))
4491         (gnus-group-change-level 
4492          group gnus-level-killed gnus-level-default-subscribed)
4493         (insert ?K)))
4494     t))
4495
4496 (defun gnus-browse-exit ()
4497   "Quit browsing and return to the group buffer."
4498   (interactive)
4499   (if (eq major-mode 'gnus-browse-server-mode)
4500       (kill-buffer (current-buffer)))
4501   (switch-to-buffer gnus-group-buffer)
4502   (gnus-group-list-groups nil))
4503
4504 (defun gnus-browse-describe-briefly ()
4505   "Give a one line description of the group mode commands."
4506   (interactive)
4507   (message
4508    (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")))
4509       
4510 \f
4511 ;;;
4512 ;;; Gnus summary mode
4513 ;;;
4514
4515 (defvar gnus-summary-mode-map nil)
4516 (defvar gnus-summary-mark-map nil)
4517 (defvar gnus-summary-mscore-map nil)
4518 (defvar gnus-summary-extract-map nil)
4519 (defvar gnus-summary-extract-view-map nil)
4520 (defvar gnus-summary-article-map nil)
4521 (defvar gnus-summary-thread-map nil)
4522 (defvar gnus-summary-goto-map nil)
4523 (defvar gnus-summary-exit-map nil)
4524 (defvar gnus-summary-various-map nil)
4525 (defvar gnus-summary-interest-map nil)
4526 (defvar gnus-summary-process-map nil)
4527 (defvar gnus-summary-sort-map nil)
4528 (defvar gnus-summary-backend-map nil)
4529 (defvar gnus-summary-save-map nil)
4530 (defvar gnus-summary-wash-map nil)
4531 (defvar gnus-summary-help-map nil)
4532
4533 (put 'gnus-summary-mode 'mode-class 'special)
4534
4535 (if gnus-summary-mode-map
4536     nil
4537   (setq gnus-summary-mode-map (make-keymap))
4538   (suppress-keymap gnus-summary-mode-map)
4539
4540   ;; Non-orthogonal keys
4541
4542   (define-key gnus-summary-mode-map " " 'gnus-summary-next-page)
4543   (define-key gnus-summary-mode-map "\177" 'gnus-summary-prev-page)
4544   (define-key gnus-summary-mode-map "\r" 'gnus-summary-scroll-up)
4545   (define-key gnus-summary-mode-map "n" 'gnus-summary-next-unread-article)
4546   (define-key gnus-summary-mode-map "p" 'gnus-summary-prev-unread-article)
4547   (define-key gnus-summary-mode-map "N" 'gnus-summary-next-article)
4548   (define-key gnus-summary-mode-map "P" 'gnus-summary-prev-article)
4549   (define-key gnus-summary-mode-map "\M-\C-n" 'gnus-summary-next-same-subject)
4550   (define-key gnus-summary-mode-map "\M-\C-p" 'gnus-summary-prev-same-subject)
4551   (define-key gnus-summary-mode-map "\M-n" 'gnus-summary-next-unread-subject)
4552   (define-key gnus-summary-mode-map "\M-p" 'gnus-summary-prev-unread-subject)
4553   (define-key gnus-summary-mode-map "." 'gnus-summary-first-unread-article)
4554   (define-key gnus-summary-mode-map "," 'gnus-summary-best-unread-article)
4555   (define-key gnus-summary-mode-map "\M-s" 'gnus-summary-search-article-forward)
4556   (define-key gnus-summary-mode-map "\M-r" 'gnus-summary-search-article-backward)
4557   (define-key gnus-summary-mode-map "<" 'gnus-summary-beginning-of-article)
4558   (define-key gnus-summary-mode-map ">" 'gnus-summary-end-of-article)
4559   (define-key gnus-summary-mode-map "j" 'gnus-summary-goto-subject)
4560   (define-key gnus-summary-mode-map "^" 'gnus-summary-refer-parent-article)
4561   (define-key gnus-summary-mode-map "\M-^" 'gnus-summary-refer-article)
4562   (define-key gnus-summary-mode-map "u" 'gnus-summary-tick-article-forward)
4563   (define-key gnus-summary-mode-map "!" 'gnus-summary-tick-article-forward)
4564   (define-key gnus-summary-mode-map "U" 'gnus-summary-tick-article-backward)
4565   (define-key gnus-summary-mode-map "d" 'gnus-summary-mark-as-read-forward)
4566   (define-key gnus-summary-mode-map "D" 'gnus-summary-mark-as-read-backward)
4567   (define-key gnus-summary-mode-map "E" 'gnus-summary-mark-as-expirable)
4568   (define-key gnus-summary-mode-map "\M-u" 'gnus-summary-clear-mark-forward)
4569   (define-key gnus-summary-mode-map "\M-U" 'gnus-summary-clear-mark-backward)
4570   (define-key gnus-summary-mode-map "k" 'gnus-summary-kill-same-subject-and-select)
4571   (define-key gnus-summary-mode-map "\C-k" 'gnus-summary-kill-same-subject)
4572   (define-key gnus-summary-mode-map "\M-\C-k" 'gnus-summary-kill-thread)
4573   (define-key gnus-summary-mode-map "\M-\C-l" 'gnus-summary-lower-thread)
4574   (define-key gnus-summary-mode-map "e" 'gnus-summary-edit-article)
4575   (define-key gnus-summary-mode-map "#" 'gnus-summary-mark-as-processable)
4576   (define-key gnus-summary-mode-map "\M-#" 'gnus-summary-unmark-as-processable)
4577   (define-key gnus-summary-mode-map "\M-\C-t" 'gnus-summary-toggle-threads)
4578   (define-key gnus-summary-mode-map "\M-\C-s" 'gnus-summary-show-thread)
4579   (define-key gnus-summary-mode-map "\M-\C-h" 'gnus-summary-hide-thread)
4580   (define-key gnus-summary-mode-map "\M-\C-f" 'gnus-summary-next-thread)
4581   (define-key gnus-summary-mode-map "\M-\C-b" 'gnus-summary-prev-thread)
4582   (define-key gnus-summary-mode-map "\M-\C-u" 'gnus-summary-up-thread)
4583   (define-key gnus-summary-mode-map "\M-\C-d" 'gnus-summary-down-thread)
4584   (define-key gnus-summary-mode-map "&" 'gnus-summary-execute-command)
4585   (define-key gnus-summary-mode-map "c" 'gnus-summary-catchup-and-exit)
4586   (define-key gnus-summary-mode-map "\C-w" 'gnus-summary-mark-region-as-read)
4587   (define-key gnus-summary-mode-map "\C-t" 'gnus-summary-toggle-truncation)
4588   (define-key gnus-summary-mode-map "?" 'gnus-summary-mark-as-dormant)
4589   (define-key gnus-summary-mode-map "\C-c\M-\C-s" 'gnus-summary-show-all-expunged)
4590   (define-key gnus-summary-mode-map "\C-c\C-s\C-n" 'gnus-summary-sort-by-number)
4591   (define-key gnus-summary-mode-map "\C-c\C-s\C-a" 'gnus-summary-sort-by-author)
4592   (define-key gnus-summary-mode-map "\C-c\C-s\C-s" 'gnus-summary-sort-by-subject)
4593   (define-key gnus-summary-mode-map "\C-c\C-s\C-d" 'gnus-summary-sort-by-date)
4594   (define-key gnus-summary-mode-map "\C-c\C-s\C-i" 'gnus-summary-sort-by-score)
4595   (define-key gnus-summary-mode-map "=" 'gnus-summary-expand-window)
4596   (define-key gnus-summary-mode-map "\C-x\C-s" 'gnus-summary-reselect-current-group)
4597   (define-key gnus-summary-mode-map "\M-g" 'gnus-summary-rescan-group)
4598   (define-key gnus-summary-mode-map "w" 'gnus-summary-stop-page-breaking)
4599   (define-key gnus-summary-mode-map "\C-c\C-r" 'gnus-summary-caesar-message)
4600   (define-key gnus-summary-mode-map "\M-t" 'gnus-summary-toggle-mime)
4601   (define-key gnus-summary-mode-map "f" 'gnus-summary-followup)
4602   (define-key gnus-summary-mode-map "F" 'gnus-summary-followup-with-original)
4603   (define-key gnus-summary-mode-map "C" 'gnus-summary-cancel-article)
4604   (define-key gnus-summary-mode-map "r" 'gnus-summary-reply)
4605   (define-key gnus-summary-mode-map "R" 'gnus-summary-reply-with-original)
4606   (define-key gnus-summary-mode-map "\C-c\C-f" 'gnus-summary-mail-forward)
4607   (define-key gnus-summary-mode-map "o" 'gnus-summary-save-article)
4608   (define-key gnus-summary-mode-map "\C-o" 'gnus-summary-save-article-mail)
4609   (define-key gnus-summary-mode-map "|" 'gnus-summary-pipe-output)
4610   (define-key gnus-summary-mode-map "\M-k" 'gnus-summary-edit-local-kill)
4611   (define-key gnus-summary-mode-map "\M-K" 'gnus-summary-edit-global-kill)
4612   (define-key gnus-summary-mode-map "V" 'gnus-version)
4613   (define-key gnus-summary-mode-map "\C-c\C-d" 'gnus-summary-describe-group)
4614   (define-key gnus-summary-mode-map "q" 'gnus-summary-exit)
4615   (define-key gnus-summary-mode-map "Q" 'gnus-summary-exit-no-update)
4616   (define-key gnus-summary-mode-map "\C-c\C-i" 'gnus-info-find-node)
4617   (define-key gnus-summary-mode-map
4618     (if gnus-xemacs [button2] [mouse-2]) 'gnus-mouse-pick-article)
4619   (define-key gnus-summary-mode-map "m" 'gnus-summary-mail-other-window)
4620   (define-key gnus-summary-mode-map "a" 'gnus-summary-post-news)
4621   (define-key gnus-summary-mode-map "x" 'gnus-summary-remove-lines-marked-as-read)
4622 ; (define-key gnus-summary-mode-map "X" 'gnus-summary-remove-lines-marked-with)
4623   (define-key gnus-summary-mode-map "s" 'gnus-summary-isearch-article)
4624   (define-key gnus-summary-mode-map "t" 'gnus-summary-toggle-header)
4625   (define-key gnus-summary-mode-map "g" 'gnus-summary-show-article)
4626 ;  (define-key gnus-summary-mode-map "?" 'gnus-summary-describe-briefly)
4627   (define-key gnus-summary-mode-map "l" 'gnus-summary-goto-last-article)
4628   (define-key gnus-summary-mode-map "\C-c\C-v\C-v" 'gnus-uu-decode-uu-view)
4629   (define-key gnus-summary-mode-map "\C-d" 'gnus-summary-enter-digest-group)
4630
4631
4632   ;; Sort of orthogonal keymap
4633   (define-prefix-command 'gnus-summary-mark-map)
4634   (define-key gnus-summary-mode-map "M" 'gnus-summary-mark-map)
4635   (define-key gnus-summary-mark-map "t" 'gnus-summary-tick-article-forward)
4636   (define-key gnus-summary-mark-map "!" 'gnus-summary-tick-article-forward)
4637   (define-key gnus-summary-mark-map "d" 'gnus-summary-mark-as-read-forward)
4638   (define-key gnus-summary-mark-map "r" 'gnus-summary-mark-as-read-forward)
4639   (define-key gnus-summary-mark-map "c" 'gnus-summary-clear-mark-forward)
4640   (define-key gnus-summary-mark-map " " 'gnus-summary-clear-mark-forward)
4641   (define-key gnus-summary-mark-map "e" 'gnus-summary-mark-as-expirable)
4642   (define-key gnus-summary-mark-map "x" 'gnus-summary-mark-as-expirable)
4643   (define-key gnus-summary-mark-map "?" 'gnus-summary-mark-as-dormant)
4644   (define-key gnus-summary-mark-map "b" 'gnus-summary-set-bookmark)
4645   (define-key gnus-summary-mark-map "B" 'gnus-summary-remove-bookmark)
4646   (define-key gnus-summary-mark-map "#" 'gnus-summary-mark-as-processable)
4647   (define-key gnus-summary-mark-map "\M-#" 'gnus-summary-unmark-as-processable)
4648   (define-key gnus-summary-mark-map "\M-r" 'gnus-summary-remove-lines-marked-as-read)
4649   (define-key gnus-summary-mark-map "\M-\C-r" 'gnus-summary-remove-lines-marked-with)
4650   (define-key gnus-summary-mark-map "D" 'gnus-summary-show-all-dormant)
4651   (define-key gnus-summary-mark-map "\M-D" 'gnus-summary-hide-all-dormant)
4652   (define-key gnus-summary-mark-map "S" 'gnus-summary-show-all-expunged)
4653   (define-key gnus-summary-mark-map "C" 'gnus-summary-catchup)
4654   (define-key gnus-summary-mark-map "H" 'gnus-summary-catchup-to-here)
4655   (define-key gnus-summary-mark-map "\C-c" 'gnus-summary-catchup-all)
4656   (define-key gnus-summary-mark-map "k" 'gnus-summary-kill-same-subject-and-select)
4657   (define-key gnus-summary-mark-map "K" 'gnus-summary-kill-same-subject)
4658
4659   (define-prefix-command 'gnus-summary-mscore-map)
4660   (define-key gnus-summary-mark-map "s" 'gnus-summary-mscore-map)
4661   (define-key gnus-summary-mscore-map "c" 'gnus-summary-clear-above)
4662   (define-key gnus-summary-mscore-map "u" 'gnus-summary-tick-above)
4663   (define-key gnus-summary-mscore-map "m" 'gnus-summary-mark-above)
4664   (define-key gnus-summary-mscore-map "k" 'gnus-summary-kill-below)
4665
4666   (define-prefix-command 'gnus-summary-process-map)
4667   (define-key gnus-summary-mark-map "p" 'gnus-summary-process-map)
4668   (define-key gnus-summary-process-map "p" 'gnus-summary-mark-as-processable)
4669   (define-key gnus-summary-process-map "u" 'gnus-summary-unmark-as-processable)
4670   (define-key gnus-summary-process-map "U" 'gnus-summary-unmark-all-processable)
4671   (define-key gnus-summary-process-map "s" 'gnus-uu-mark-series)
4672   (define-key gnus-summary-process-map "r" 'gnus-uu-mark-region)
4673   (define-key gnus-summary-process-map "R" 'gnus-uu-mark-by-regexp)
4674   (define-key gnus-summary-process-map "t" 'gnus-uu-mark-thread)
4675   (define-key gnus-summary-process-map "a" 'gnus-uu-mark-all)
4676   (define-key gnus-summary-process-map "S" 'gnus-uu-mark-sparse)
4677   
4678   (define-key gnus-summary-mode-map "S" 'gnus-summary-send-map)
4679   
4680   (define-prefix-command 'gnus-summary-goto-map)
4681   (define-key gnus-summary-mode-map "G" 'gnus-summary-goto-map)
4682   (define-key gnus-summary-goto-map "n" 'gnus-summary-next-unread-article)
4683   (define-key gnus-summary-goto-map "p" 'gnus-summary-prev-unread-article)
4684   (define-key gnus-summary-goto-map "N" 'gnus-summary-next-article)
4685   (define-key gnus-summary-goto-map "P" 'gnus-summary-prev-article)
4686   (define-key gnus-summary-goto-map "\C-n" 'gnus-summary-next-same-subject)
4687   (define-key gnus-summary-goto-map "\C-p" 'gnus-summary-prev-same-subject)
4688   (define-key gnus-summary-goto-map "\M-n" 'gnus-summary-next-unread-subject)
4689   (define-key gnus-summary-goto-map "\M-p" 'gnus-summary-prev-unread-subject)
4690   (define-key gnus-summary-goto-map "f" 'gnus-summary-first-unread-article)
4691   (define-key gnus-summary-goto-map "b" 'gnus-summary-best-unread-article)
4692   (define-key gnus-summary-goto-map "g" 'gnus-summary-goto-subject)
4693   (define-key gnus-summary-goto-map "l" 'gnus-summary-goto-last-article)
4694   (define-key gnus-summary-goto-map "p" 'gnus-summary-pop-article)
4695
4696
4697   (define-prefix-command 'gnus-summary-thread-map)
4698   (define-key gnus-summary-mode-map "T" 'gnus-summary-thread-map)
4699   (define-key gnus-summary-thread-map "k" 'gnus-summary-kill-thread)
4700   (define-key gnus-summary-thread-map "l" 'gnus-summary-lower-thread)
4701   (define-key gnus-summary-thread-map "i" 'gnus-summary-raise-thread)
4702   (define-key gnus-summary-thread-map "T" 'gnus-summary-toggle-threads)
4703   (define-key gnus-summary-thread-map "s" 'gnus-summary-show-thread)
4704   (define-key gnus-summary-thread-map "S" 'gnus-summary-show-all-threads)
4705   (define-key gnus-summary-thread-map "h" 'gnus-summary-hide-thread)
4706   (define-key gnus-summary-thread-map "H" 'gnus-summary-hide-all-threads)
4707   (define-key gnus-summary-thread-map "n" 'gnus-summary-next-thread)
4708   (define-key gnus-summary-thread-map "p" 'gnus-summary-prev-thread)
4709   (define-key gnus-summary-thread-map "u" 'gnus-summary-up-thread)
4710   (define-key gnus-summary-thread-map "d" 'gnus-summary-down-thread)
4711   (define-key gnus-summary-thread-map "#" 'gnus-uu-mark-thread)
4712
4713   
4714   (define-prefix-command 'gnus-summary-exit-map)
4715   (define-key gnus-summary-mode-map "Z" 'gnus-summary-exit-map)
4716   (define-key gnus-summary-exit-map "c" 'gnus-summary-catchup-and-exit)
4717   (define-key gnus-summary-exit-map "C" 'gnus-summary-catchup-all-and-exit)
4718   (define-key gnus-summary-exit-map "E" 'gnus-summary-exit-no-update)
4719   (define-key gnus-summary-exit-map "Q" 'gnus-summary-exit)
4720   (define-key gnus-summary-exit-map "Z" 'gnus-summary-exit)
4721   (define-key gnus-summary-exit-map "n" 'gnus-summary-catchup-and-goto-next-group)
4722   (define-key gnus-summary-exit-map "R" 'gnus-summary-reselect-current-group)
4723   (define-key gnus-summary-exit-map "G" 'gnus-summary-rescan-group)
4724
4725
4726   (define-prefix-command 'gnus-summary-article-map)
4727   (define-key gnus-summary-mode-map "A" 'gnus-summary-article-map)
4728   (define-key gnus-summary-article-map " " 'gnus-summary-next-page)
4729   (define-key gnus-summary-article-map "n" 'gnus-summary-next-page)
4730   (define-key gnus-summary-article-map "\177" 'gnus-summary-prev-page)
4731   (define-key gnus-summary-article-map "p" 'gnus-summary-prev-page)
4732   (define-key gnus-summary-article-map "\r" 'gnus-summary-scroll-up)
4733   (define-key gnus-summary-article-map "<" 'gnus-summary-beginning-of-article)
4734   (define-key gnus-summary-article-map ">" 'gnus-summary-end-of-article)
4735   (define-key gnus-summary-article-map "b" 'gnus-summary-beginning-of-article)
4736   (define-key gnus-summary-article-map "e" 'gnus-summary-end-of-article)
4737   (define-key gnus-summary-article-map "^" 'gnus-summary-refer-parent-article)
4738   (define-key gnus-summary-article-map "r" 'gnus-summary-refer-parent-article)
4739   (define-key gnus-summary-article-map "w" 'gnus-summary-stop-page-breaking)
4740   (define-key gnus-summary-article-map "c" 'gnus-summary-caesar-message)
4741   (define-key gnus-summary-article-map "g" 'gnus-summary-show-article)
4742   (define-key gnus-summary-article-map "t" 'gnus-summary-toggle-header)
4743   (define-key gnus-summary-article-map "m" 'gnus-summary-toggle-mime)
4744   (define-key gnus-summary-article-map "s" 'gnus-summary-isearch-article)
4745
4746
4747   (define-prefix-command 'gnus-summary-wash-map)
4748   (define-key gnus-summary-mode-map "W" 'gnus-summary-wash-map)
4749   (define-key gnus-summary-wash-map "h" 'gnus-article-hide-headers)
4750   (define-key gnus-summary-wash-map "s" 'gnus-article-hide-signature)
4751   (define-key gnus-summary-wash-map "c" 'gnus-article-hide-citation)
4752   (define-key gnus-summary-wash-map "o" 'gnus-article-treat-overstrike)
4753   (define-key gnus-summary-wash-map "w" 'gnus-article-word-wrap)
4754   (define-key gnus-summary-wash-map "d" 'gnus-article-remove-cr)
4755   (define-key gnus-summary-wash-map "q" 'gnus-article-de-quoted-unreadable)
4756   (define-key gnus-summary-wash-map "f" 'gnus-article-display-x-face)
4757   (define-key gnus-summary-wash-map "t" 'gnus-article-date-ut)
4758   (define-key gnus-summary-wash-map "\C-t" 'gnus-article-date-local)
4759   (define-key gnus-summary-wash-map "T" 'gnus-article-date-lapsed)
4760
4761
4762   (define-prefix-command 'gnus-summary-help-map)
4763   (define-key gnus-summary-mode-map "H" 'gnus-summary-help-map)
4764   (define-key gnus-summary-help-map "v" 'gnus-version)
4765   (define-key gnus-summary-help-map "f" 'gnus-summary-fetch-faq)
4766   (define-key gnus-summary-help-map "d" 'gnus-summary-describe-group)
4767   (define-key gnus-summary-help-map "h" 'gnus-summary-describe-briefly)
4768   (define-key gnus-summary-help-map "i" 'gnus-info-find-node)
4769
4770
4771   (define-prefix-command 'gnus-summary-backend-map)
4772   (define-key gnus-summary-mode-map "B" 'gnus-summary-backend-map)
4773   (define-key gnus-summary-backend-map "e" 'gnus-summary-expire-articles)
4774   (define-key gnus-summary-backend-map "\177" 'gnus-summary-delete-article)
4775   (define-key gnus-summary-backend-map "m" 'gnus-summary-move-article)
4776   (define-key gnus-summary-backend-map "r" 'gnus-summary-respool-article)
4777   (define-key gnus-summary-backend-map "w" 'gnus-summary-edit-article)
4778   (define-key gnus-summary-backend-map "c" 'gnus-summary-copy-article)
4779
4780
4781   (define-prefix-command 'gnus-summary-save-map)
4782   (define-key gnus-summary-mode-map "O" 'gnus-summary-save-map)
4783   (define-key gnus-summary-save-map "o" 'gnus-summary-save-article)
4784   (define-key gnus-summary-save-map "m" 'gnus-summary-save-article-mail)
4785   (define-key gnus-summary-save-map "r" 'gnus-summary-save-article-rmail)
4786   (define-key gnus-summary-save-map "f" 'gnus-summary-save-article-file)
4787   (define-key gnus-summary-save-map "h" 'gnus-summary-save-article-folder)
4788   (define-key gnus-summary-save-map "p" 'gnus-summary-pipe-output)
4789
4790
4791   (define-prefix-command 'gnus-summary-extract-map)
4792   (define-key gnus-summary-mode-map "X" 'gnus-summary-extract-map)
4793 ;  (define-key gnus-summary-extract-map "x" 'gnus-summary-extract-any)
4794 ;  (define-key gnus-summary-extract-map "m" 'gnus-summary-extract-mime)
4795   (define-key gnus-summary-extract-map "u" 'gnus-uu-decode-uu)
4796   (define-key gnus-summary-extract-map "U" 'gnus-uu-decode-uu-and-save)
4797   (define-key gnus-summary-extract-map "s" 'gnus-uu-decode-unshar)
4798   (define-key gnus-summary-extract-map "S" 'gnus-uu-decode-unshar-and-save)
4799   (define-key gnus-summary-extract-map "o" 'gnus-uu-decode-save)
4800   (define-key gnus-summary-extract-map "O" 'gnus-uu-decode-save)
4801   (define-key gnus-summary-extract-map "b" 'gnus-uu-decode-binhex)
4802   (define-key gnus-summary-extract-map "B" 'gnus-uu-decode-binhex)
4803   (define-key gnus-summary-extract-map "p" 'gnus-uu-decode-postscript)
4804   (define-key gnus-summary-extract-map "P" 'gnus-uu-decode-postscript-and-save)
4805
4806
4807   (define-prefix-command 'gnus-summary-extract-view-map)
4808   (define-key gnus-summary-extract-map "v" 'gnus-summary-extract-view-map)
4809   (define-key gnus-summary-extract-view-map "u" 'gnus-uu-decode-uu-view)
4810   (define-key gnus-summary-extract-view-map "U" 'gnus-uu-decode-uu-and-save-view)
4811   (define-key gnus-summary-extract-view-map "s" 'gnus-uu-decode-unshar-view)
4812   (define-key gnus-summary-extract-view-map "S" 'gnus-uu-decode-unshar-and-save-view)
4813   (define-key gnus-summary-extract-view-map "o" 'gnus-uu-decode-save-view)
4814   (define-key gnus-summary-extract-view-map "O" 'gnus-uu-decode-save-view)
4815   (define-key gnus-summary-extract-view-map "b" 'gnus-uu-decode-binhex-view)
4816   (define-key gnus-summary-extract-view-map "B" 'gnus-uu-decode-binhex-view)
4817   (define-key gnus-summary-extract-view-map "p" 'gnus-uu-decode-postscript-view)
4818   (define-key gnus-summary-extract-view-map "P" 'gnus-uu-decode-postscript-and-save-view)
4819   
4820   
4821   (define-prefix-command 'gnus-summary-various-map)
4822   (define-key gnus-summary-mode-map "V" 'gnus-summary-various-map)
4823   (define-key gnus-summary-various-map "u" 'gnus-summary-universal-argument)
4824   (define-key gnus-summary-various-map "\C-s" 'gnus-summary-search-article-forward)
4825   (define-key gnus-summary-various-map "\C-r" 'gnus-summary-search-article-backward)
4826   (define-key gnus-summary-various-map "r" 'gnus-summary-refer-article)
4827   (define-key gnus-summary-various-map "&" 'gnus-summary-execute-command)
4828   (define-key gnus-summary-various-map "T" 'gnus-summary-toggle-truncation)
4829   (define-key gnus-summary-various-map "e" 'gnus-summary-expand-window)
4830   (define-key gnus-summary-various-map "D" 'gnus-summary-enter-digest-group)
4831   (define-key gnus-summary-various-map "k" 'gnus-summary-edit-local-kill)
4832   (define-key gnus-summary-various-map "K" 'gnus-summary-edit-global-kill)
4833
4834   (define-key gnus-summary-various-map "S" 'gnus-summary-score-map)
4835
4836   (define-prefix-command 'gnus-summary-sort-map)
4837   (define-key gnus-summary-various-map "s" 'gnus-summary-sort-map)
4838   (define-key gnus-summary-sort-map "n" 'gnus-summary-sort-by-number)
4839   (define-key gnus-summary-sort-map "a" 'gnus-summary-sort-by-author)
4840   (define-key gnus-summary-sort-map "s" 'gnus-summary-sort-by-subject)
4841   (define-key gnus-summary-sort-map "d" 'gnus-summary-sort-by-date)
4842   (define-key gnus-summary-sort-map "i" 'gnus-summary-sort-by-score)
4843
4844   (define-key gnus-summary-mode-map "I" 'gnus-summary-increase-map)
4845   (define-key gnus-summary-mode-map "L" 'gnus-summary-lower-map)
4846   )
4847
4848
4849 \f
4850
4851 (defun gnus-summary-mode ()
4852   "Major mode for reading articles.
4853
4854 All normal editing commands are switched off.
4855 \\<gnus-summary-mode-map>
4856 Each line in this buffer represents one article.  To read an
4857 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
4858 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]', 
4859 respectively.
4860
4861 You can also post articles and send mail from this buffer.  To 
4862 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author 
4863 of an article, type `\\[gnus-summary-reply]'.
4864
4865 There are approx. one gazillion commands you can execute in this 
4866 buffer; read the info pages for more information (`\\[gnus-info-find-node]'). 
4867
4868 The following commands are available:
4869
4870 \\{gnus-summary-mode-map}"
4871   (interactive)
4872   (if gnus-visual (gnus-summary-make-menu-bar))
4873   (kill-all-local-variables)
4874   (let ((locals gnus-summary-local-variables))
4875     (while locals
4876       (if (consp (car locals))
4877           (progn
4878             (make-local-variable (car (car locals)))
4879             (set (car (car locals)) (eval (cdr (car locals)))))
4880         (make-local-variable (car locals))
4881         (set (car locals) nil))
4882       (setq locals (cdr locals))))
4883   (gnus-update-format-specifications)
4884   (setq mode-line-modified "-- ")
4885   (make-local-variable 'mode-line-format)
4886   (setq mode-line-format (copy-sequence mode-line-format))
4887   (and (equal (nth 3 mode-line-format) "   ")
4888        (setcar (nthcdr 3 mode-line-format) ""))
4889   (setq major-mode 'gnus-summary-mode)
4890   (setq mode-name "Summary")
4891   (make-local-variable 'minor-mode-alist)
4892   (gnus-set-mode-line 'summary)
4893   (use-local-map gnus-summary-mode-map)
4894   (buffer-disable-undo (current-buffer))
4895   (setq buffer-read-only t)             ;Disable modification
4896   (setq truncate-lines t)
4897   (setq selective-display t)
4898   (setq selective-display-ellipses t)   ;Display `...'
4899   (run-hooks 'gnus-summary-mode-hook))
4900
4901 (defun gnus-summary-clear-local-variables ()
4902   (let ((locals gnus-summary-local-variables))
4903     (while locals
4904       (if (consp (car locals))
4905           (set (car (car locals)) nil)
4906         (set (car locals) nil))
4907       (setq locals (cdr locals)))))
4908
4909 (defun gnus-mouse-pick-article (e)
4910   (interactive "e")
4911   (mouse-set-point e)
4912   (gnus-summary-next-page nil t))
4913
4914 (defun gnus-summary-setup-buffer (group)
4915   "Initialize summary buffer."
4916   (let ((buffer (concat "*Summary " group "*")))
4917     ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
4918     (setq gnus-summary-buffer (set-buffer (get-buffer-create buffer)))
4919     (gnus-add-current-to-buffer-list)
4920     (gnus-summary-mode)))
4921
4922 (defun gnus-set-global-variables ()
4923   ;; Set the global equivalents of the summary buffer-local variables
4924   ;; to the latest values they had. These reflect the summary buffer
4925   ;; that was in action when the last article was fetched.
4926   (if (eq major-mode 'gnus-summary-mode) 
4927       (progn
4928         (setq gnus-summary-buffer (current-buffer))
4929         (let ((name gnus-newsgroup-name)
4930               (marked gnus-newsgroup-marked)
4931               (unread gnus-newsgroup-unreads)
4932               (headers gnus-current-headers))
4933           (save-excursion
4934             (set-buffer gnus-group-buffer)
4935             (setq gnus-newsgroup-name name)
4936             (setq gnus-newsgroup-marked marked)
4937             (setq gnus-newsgroup-unreads unread)
4938             (setq gnus-current-headers headers))))))
4939
4940 (defun gnus-summary-insert-dummy-line (sformat subject number)
4941   (if (not sformat) 
4942       (setq sformat gnus-summary-dummy-line-format-spec))
4943   (let (b)
4944     (beginning-of-line)
4945     (setq b (point))
4946     (insert (eval sformat))
4947     (add-text-properties
4948      b (1+ b)
4949      (list 'gnus-subject (gnus-simplify-subject-re subject)
4950            'gnus-number number
4951            'gnus-mark ?Z
4952            'gnus-thread 0))))
4953
4954 (defun gnus-summary-insert-line 
4955   (sformat header level current unread replied expirable subject-or-nil
4956            &optional dummy score)
4957   (or sformat (setq sformat gnus-summary-line-format-spec))
4958   (let* ((indentation 
4959           (make-string (* level gnus-thread-indent-level) ? ))
4960          (lines (or (header-lines header) 0))
4961          (score (or score gnus-summary-default-score 0))
4962          (score-char (if (or (null gnus-summary-default-score)
4963                              (= score gnus-summary-default-score)) ? 
4964                        (if (< score gnus-summary-default-score) 
4965                            gnus-score-below-mark gnus-score-over-mark)))
4966          (replied (if replied gnus-replied-mark ? ))
4967          (from (header-from header))
4968          (name-address (funcall gnus-extract-address-components from))
4969          (address (car (cdr name-address)))
4970          (name (or (car name-address) (car (cdr name-address))))
4971          (number (header-number header))
4972          (subject (header-subject header))
4973          (buffer-read-only nil)
4974          (opening-bracket (if dummy ?\< ?\[))
4975          (closing-bracket (if dummy ?\> ?\]))
4976          b)
4977     ;; Suggested by Brian Edmonds <bedmonds@prodigy.bc.ca>.
4978     (if (not (numberp lines)) (setq lines 0))
4979     (beginning-of-line)
4980     (setq b (point))
4981     (insert (eval sformat))
4982     (add-text-properties
4983      b (1+ b)
4984      (list 'gnus-subject (gnus-simplify-subject-re subject)
4985            'gnus-number number
4986            'gnus-mark (or unread gnus-unread-mark ? )
4987            'gnus-thread level))))
4988
4989 (defun gnus-summary-update-line (&optional dont-update)
4990   ;; Update summary line after change.
4991   (or (not gnus-summary-default-score)
4992       gnus-summary-inhibit-highlight
4993       (let ((gnus-summary-inhibit-highlight t)
4994             (article (gnus-summary-article-number)))
4995         (progn
4996           (or dont-update
4997               (if (and gnus-summary-mark-below
4998                        (< (gnus-summary-article-score)
4999                           gnus-summary-mark-below))
5000                   (and (not (memq article gnus-newsgroup-marked))
5001                        (not (memq article gnus-newsgroup-dormant))
5002                        (memq article gnus-newsgroup-unreads)
5003                        (gnus-summary-mark-article nil gnus-low-score-mark))
5004                 (and (eq (gnus-summary-article-mark) gnus-low-score-mark)
5005                      (gnus-summary-mark-article nil gnus-unread-mark))))
5006           (and gnus-visual
5007                (run-hooks 'gnus-visual-summary-update-hook))))))
5008
5009 (defun gnus-summary-update-lines ()
5010   ;; Rehighlight summary buffer according to `gnus-summary-highlight'.
5011   (and (save-excursion
5012          (set-buffer gnus-summary-buffer)
5013          (goto-char (point-min))
5014          (while (not (eobp))
5015            (gnus-summary-update-line)
5016            (forward-line 1)))))
5017
5018 (defun gnus-summary-number-of-articles-in-thread (thread &optional char)
5019   ;; Sum up all elements (and sub-elements) in a list.
5020   (let ((number 
5021          (if (listp thread) 
5022              (apply 
5023               '+ (mapcar 'gnus-summary-number-of-articles-in-thread thread))
5024            1)))
5025     (if char 
5026         (if (> number 1) gnus-not-empty-thread-mark
5027           gnus-empty-thread-mark)
5028       number)))
5029
5030 (defun gnus-summary-read-group (group &optional show-all no-article kill-buffer)
5031   "Start reading news in newsgroup GROUP.
5032 If SHOW-ALL is non-nil, already read articles are also listed.
5033 If NO-ARTICLE is non-nil, no article is selected initially."
5034   (message "Retrieving newsgroup: %s..." group)
5035   (gnus-summary-setup-buffer group)
5036   (let ((did-select (gnus-select-newsgroup group show-all)))
5037     (cond 
5038      ((null did-select) 
5039       (and (eq major-mode 'gnus-summary-mode)
5040            (not (equal (current-buffer) kill-buffer))
5041            (progn
5042              (kill-buffer (current-buffer))
5043              (switch-to-buffer (or kill-buffer gnus-group-buffer))
5044              (gnus-group-next-unread-group 1)))
5045       nil)
5046      ((eq did-select 'quit)
5047       (gnus-configure-windows 'summary)
5048       (gnus-configure-windows 'newsgroup)
5049       (and (eq major-mode 'gnus-summary-mode)
5050            (not (equal (current-buffer) kill-buffer))
5051            (kill-buffer (current-buffer)))
5052       (gnus-kill-buffer kill-buffer)
5053       (switch-to-buffer gnus-group-buffer)
5054       (gnus-group-next-unread-group 1)
5055       (signal 'quit nil))
5056      (t
5057       (gnus-set-global-variables)
5058       ;; Save the active value in effect when the group was entered.
5059       (setq gnus-newsgroup-active 
5060             (gnus-copy-sequence
5061              (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
5062       ;; You can change the subjects in this hook.
5063       (run-hooks 'gnus-select-group-hook)
5064       ;; Do score processing.
5065       (gnus-possibly-score-headers)
5066       ;; Update the format specifiers.
5067       (gnus-update-format-specifications)
5068       ;; Generate the summary buffer.
5069       (gnus-summary-prepare)
5070       (if (zerop (buffer-size))
5071           (cond (gnus-newsgroup-dormant
5072                  (gnus-summary-show-all-dormant))
5073                 ((and gnus-newsgroup-scored show-all)
5074                  (gnus-summary-show-all-expunged))))
5075       ;; Function `gnus-apply-kill-file' must be called in this hook.
5076       (run-hooks 'gnus-apply-kill-hook)
5077       (if (zerop (buffer-size))
5078           (progn
5079             ;; This newsgroup is empty.
5080             (gnus-summary-catchup-and-exit nil t) ;Without confirmations.
5081             (message "No unread news")
5082             (gnus-kill-buffer kill-buffer))
5083         (save-excursion
5084           (if kill-buffer
5085               (let ((gnus-summary-buffer kill-buffer))
5086                 (gnus-configure-windows 'newsgroups t))))
5087         ;; Hide conversation thread subtrees.  We cannot do this in
5088         ;; gnus-summary-prepare-hook since kill processing may not
5089         ;; work with hidden articles.
5090         (and gnus-show-threads
5091              gnus-thread-hide-subtree
5092              (gnus-summary-hide-all-threads))
5093         ;; Show first unread article if requested.
5094         (goto-char (point-min))
5095         (if (and (not no-article)
5096                  gnus-auto-select-first
5097                  (gnus-summary-first-unread-article))
5098             (gnus-configure-windows 'article)
5099           (gnus-configure-windows 'summary))
5100         (pop-to-buffer gnus-summary-buffer)
5101         (gnus-set-mode-line 'summary)
5102         (gnus-summary-position-cursor)
5103         ;; If in async mode, we send some info to the backend.
5104         (and gnus-newsgroup-async
5105              (setq gnus-newsgroup-threads (nreverse gnus-newsgroup-threads))
5106              (gnus-request-asynchronous 
5107               gnus-newsgroup-name
5108               (if (and gnus-asynchronous-article-function
5109                        (fboundp gnus-asynchronous-article-function))
5110                   (funcall gnus-asynchronous-article-function
5111                            gnus-newsgroup-threads)
5112                 gnus-newsgroup-threads)))
5113         (gnus-kill-buffer kill-buffer))))))
5114
5115 (defun gnus-summary-prepare ()
5116   ;; Generate the summary buffer.
5117   (let ((buffer-read-only nil))
5118     (erase-buffer)
5119     (gnus-summary-prepare-threads 
5120      (if gnus-show-threads
5121          (gnus-gather-threads 
5122           (gnus-sort-threads 
5123            (if gnus-summary-expunge-below
5124                (gnus-make-threads-and-expunge)
5125              (gnus-make-threads))))
5126        gnus-newsgroup-headers)
5127      0 nil nil t)
5128     ;; Erase header retrieval message.
5129     (gnus-summary-update-lines)
5130     (message "")
5131     ;; Call hooks for modifying summary buffer.
5132     ;; Suggested by sven@tde.LTH.Se (Sven Mattisson).
5133     (goto-char (point-min))
5134     (run-hooks 'gnus-summary-prepare-hook)))
5135
5136 (defun gnus-gather-threads (threads)
5137   "Gather threads that have lost their roots."
5138   (if (not gnus-summary-make-false-root)
5139       threads 
5140     (let ((hashtb (gnus-make-hashtable 1023))
5141           (prev threads)
5142           (result threads)
5143           thread subject hthread unre-subject whole-subject)
5144       (while threads
5145         (setq subject (header-subject (car (car threads)))
5146               whole-subject subject)
5147         (and gnus-summary-gather-subject-limit
5148              (or (and (numberp gnus-summary-gather-subject-limit)
5149                       (> (length subject) gnus-summary-gather-subject-limit)
5150                       (setq subject
5151                             (substring subject 0 
5152                                        gnus-summary-gather-subject-limit)))
5153                  (and (eq 'fuzzy gnus-summary-gather-subject-limit)
5154                       (setq subject (gnus-simplify-subject-fuzzy subject)))))
5155         (if (setq hthread 
5156                   (gnus-gethash 
5157                    (setq unre-subject (gnus-simplify-subject-re subject))
5158                    hashtb))
5159             (progn
5160               (or (stringp (car (car hthread)))
5161                   (setcar hthread (list whole-subject (car hthread))))
5162               (setcdr (car hthread) (nconc (cdr (car hthread)) 
5163                                            (list (car threads))))
5164               (setcdr prev (cdr threads))
5165               (setq threads prev))
5166           (gnus-sethash unre-subject threads hashtb))
5167         (setq prev threads)
5168         (setq threads (cdr threads)))
5169       result)))
5170
5171 (defun gnus-make-threads ()
5172   ;; This function takes the dependencies already made by 
5173   ;; `gnus-get-newsgroup-headers' and builds the trees. First we go
5174   ;; through the dependecies in the hash table and finds all the
5175   ;; roots. Roots do not refer back to any valid articles.
5176   (let (roots)
5177     (and gnus-fetch-old-headers (eq gnus-headers-retrieved-by 'nov)
5178          (gnus-build-old-threads))
5179     (mapatoms
5180      (lambda (refs)
5181        (if (not (car (symbol-value refs)))
5182            (setq roots (append (cdr (symbol-value refs)) roots))
5183          ;; Ok, these refer back to valid articles, but if
5184          ;; `gnus-thread-ignore-subject' is nil, we have to check that
5185          ;; the root has the same subject as its children. The children
5186          ;; that do not are made into roots and removed from the list
5187          ;; of children. 
5188          (or gnus-thread-ignore-subject
5189              (let* ((prev (symbol-value refs))
5190                     (subject (gnus-simplify-subject-re 
5191                               (header-subject (car prev))))
5192                     (headers (cdr prev)))
5193                (while headers
5194                  (if (not (string= subject
5195                                    (gnus-simplify-subject-re 
5196                                     (header-subject (car headers)))))
5197                      (progn
5198                        (setq roots (cons (car headers) roots))
5199                        (setcdr prev (cdr headers)))
5200                    (setq prev headers))
5201                  (setq headers (cdr headers)))))))
5202      gnus-newsgroup-dependencies)
5203     
5204     (mapcar 'gnus-trim-thread
5205             (apply 'append
5206                    (mapcar 'gnus-cut-thread
5207                            (mapcar 'gnus-make-sub-thread roots))))))
5208   
5209 (defun gnus-make-threads-and-expunge ()
5210   ;; This function takes the dependencies already made by 
5211   ;; `gnus-get-newsgroup-headers' and builds the trees. First we go
5212   ;; through the dependecies in the hash table and finds all the
5213   ;; roots. Roots do not refer back to any valid articles.
5214   (let (roots)
5215     (and gnus-fetch-old-headers (eq gnus-headers-retrieved-by 'nov)
5216          (gnus-build-old-threads))
5217     (mapatoms
5218      (lambda (refs)
5219        (if (not (car (symbol-value refs)))
5220            (if (and gnus-summary-expunge-below
5221                     (not gnus-fetch-old-headers))
5222                (let ((headers (cdr (symbol-value refs))))
5223                  (while headers
5224                   (if (not (< (or (cdr (assq (header-number (car headers))
5225                                              gnus-newsgroup-scored))
5226                                   gnus-summary-default-score 0)
5227                               gnus-summary-expunge-below))
5228                       (setq roots (cons (car headers) roots))
5229                     (setq gnus-newsgroup-unreads
5230                           (delq (header-number (car headers))
5231                                 gnus-newsgroup-unreads)))
5232                   (setq headers (cdr headers))))
5233              (setq roots (append (cdr (symbol-value refs)) roots)))
5234          ;; Ok, these refer back to valid articles, but if
5235          ;; `gnus-thread-ignore-subject' is nil, we have to check that
5236          ;; the root has the same subject as its children. The children
5237          ;; that do not are made into roots and removed from the list
5238          ;; of children. 
5239          (or gnus-thread-ignore-subject
5240              (let* ((prev (symbol-value refs))
5241                     (subject (gnus-simplify-subject-re 
5242                               (header-subject (car prev))))
5243                     (headers (cdr prev)))
5244                (while headers
5245                  (if (not (string= subject
5246                                    (gnus-simplify-subject-re 
5247                                     (header-subject (car headers)))))
5248                      (progn
5249                        (setq roots (cons (car headers) roots))
5250                        (setcdr prev (cdr headers)))
5251                    (setq prev headers))
5252                  (setq headers (cdr headers)))))
5253          (and gnus-summary-expunge-below
5254               (not gnus-fetch-old-headers)
5255               (let* ((prev (symbol-value refs))
5256                      (headers (cdr prev))
5257                      id)
5258                 (while headers
5259                   (if (not (< (or (cdr (assq (header-number (car headers))
5260                                              gnus-newsgroup-scored))
5261                                   gnus-summary-default-score 0)
5262                               gnus-summary-expunge-below))
5263                       (setq prev (cdr prev))
5264                     (setq gnus-newsgroup-unreads 
5265                           (delq (header-number (car headers))
5266                                 gnus-newsgroup-unreads))
5267                     (setcdr prev (cdr headers))
5268                     (setq id (gnus-gethash (header-id (car headers))
5269                                            gnus-newsgroup-dependencies))
5270                     (let ((h (cdr id)))
5271                       (while h
5272                         (if (not (< (or (cdr (assq (header-number (car h))
5273                                                    gnus-newsgroup-scored))
5274                                         gnus-summary-default-score 0)
5275                                     gnus-summary-expunge-below))
5276                             (setq roots (cons (car h) roots)))
5277                         (setq h (cdr h)))))
5278                   (setq headers (cdr headers)))))))
5279      gnus-newsgroup-dependencies)
5280     
5281     (mapcar 'gnus-trim-thread
5282             (apply 'append
5283                    (mapcar 'gnus-cut-thread
5284                            (mapcar 'gnus-make-sub-thread roots))))))
5285   
5286 (defun gnus-cut-thread (thread)
5287   ;; Remove leaf dormant or ancient articles from THREAD.
5288   (let ((head (car thread))
5289         (tail (apply 'append (mapcar 'gnus-cut-thread (cdr thread)))))
5290     (if (and (null tail)
5291              (let ((number (header-number head)))
5292                (or (memq number gnus-newsgroup-ancient)
5293                    (memq number gnus-newsgroup-dormant)
5294                    (and gnus-summary-expunge-below
5295                         (eq gnus-fetch-old-headers 'some)
5296                         (< (or (cdr (assq number gnus-newsgroup-scored))
5297                                gnus-summary-default-score 0)
5298                            gnus-summary-expunge-below)
5299                         (progn
5300                           (setq gnus-newsgroup-unreads
5301                                 (delq number gnus-newsgroup-unreads))
5302                           t)))))
5303         nil
5304       (list (cons head tail)))))
5305
5306 (defun gnus-trim-thread (thread)
5307   ;; Remove root ancient articles with only one child from THREAD.
5308   (if (and (eq gnus-fetch-old-headers 'some)
5309            (memq (header-number (car thread)) gnus-newsgroup-ancient)
5310            (= (length thread) 2))
5311       (gnus-trim-thread (nth 1 thread))
5312     thread))
5313
5314 (defun gnus-make-sub-thread (root)
5315   ;; This function makes a sub-tree for a node in the tree.
5316   (let ((children (reverse (cdr (gnus-gethash (downcase (header-id root))
5317                                               gnus-newsgroup-dependencies)))))
5318     (cons root (mapcar 'gnus-make-sub-thread children))))
5319
5320 (defun gnus-build-old-threads ()
5321   ;; Look at all the articles that refer back to old articles, and
5322   ;; fetch the headers for the articles that aren't there. This will
5323   ;; build complete threads - if the roots haven't been expired by the
5324   ;; server, that is.
5325   (let (id heads)
5326     (mapatoms
5327      (lambda (refs)
5328        (if (not (car (symbol-value refs)))
5329            (progn
5330              (setq heads (cdr (symbol-value refs)))
5331              (while heads
5332                (if (not (memq (header-number (car heads))
5333                               gnus-newsgroup-dormant))
5334                    (progn
5335                      (setq id (symbol-name refs))
5336                      (while (and (setq id (gnus-build-get-header id))
5337                                  (not (car (gnus-gethash 
5338                                             id gnus-newsgroup-dependencies)))))
5339                      (setq heads nil))
5340                  (setq heads (cdr heads)))))))
5341      gnus-newsgroup-dependencies)))
5342
5343 (defun gnus-build-get-header (id)
5344   ;; Look through the buffer of NOV lines and find the header to
5345   ;; ID. Enter this line into the dependencies hash table, and return
5346   ;; the id of the parent article (if any).
5347   (let ((deps gnus-newsgroup-dependencies)
5348         found header)
5349     (prog1
5350         (save-excursion
5351           (set-buffer nntp-server-buffer)
5352           (goto-char (point-min))
5353           (while (and (not found) (search-forward id nil t))
5354             (beginning-of-line)
5355             (setq found (looking-at (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
5356                                             (regexp-quote id))))
5357             (or found (beginning-of-line 2)))
5358           (if found
5359               (let (ref)
5360                 (beginning-of-line)
5361                 (and
5362                  (setq header (gnus-nov-parse-line 
5363                                (read (current-buffer)) deps))
5364                  (setq ref (header-references header))
5365                  (string-match "\\(<[^>]+>\\) *$" ref)
5366                  (substring ref (match-beginning 1) (match-end 1))))))
5367       (and header
5368            (setq gnus-newsgroup-headers (cons header gnus-newsgroup-headers)
5369                  gnus-newsgroup-ancient (cons (header-number header)
5370                                               gnus-newsgroup-ancient))))))
5371
5372 (defun gnus-sort-threads (threads)
5373   ;; Sort threads as specified in `gnus-thread-sort-functions'.
5374   (let ((fun gnus-thread-sort-functions))
5375     (while fun
5376       (setq threads (sort threads (car fun))
5377             fun (cdr fun))))
5378   threads)
5379
5380 (defun gnus-thread-header (thread)
5381   ;; Return header of first article in THREAD.
5382   (if (consp thread)
5383       (if (stringp (car thread))
5384           (car (car (cdr thread)))
5385         (car thread))
5386     thread))
5387
5388 (defun gnus-thread-sort-by-number (h1 h2)
5389   "Sort threads by root article number."
5390   (let ((h1 (gnus-thread-header h1))
5391         (h2 (gnus-thread-header h2)))
5392     (< (header-number h1) (header-number h2))))
5393
5394 (defun gnus-thread-sort-by-author (h1 h2)
5395   "Sort threads by root author."
5396   (let ((h1 (gnus-thread-header h1))
5397         (h2 (gnus-thread-header h2)))
5398     (string-lessp
5399      (let ((extract (funcall 
5400                      gnus-extract-address-components (header-from h1))))
5401        (or (car extract) (cdr extract)))
5402      (let ((extract (funcall
5403                      gnus-extract-address-components (header-from h2))))
5404        (or (car extract) (cdr extract))))))
5405
5406 (defun gnus-thread-sort-by-subject (h1 h2)
5407   "Sort threads by root subject."
5408   (let ((h1 (gnus-thread-header h1))
5409         (h2 (gnus-thread-header h2)))
5410     (string-lessp
5411      (downcase (gnus-simplify-subject (header-subject h1)))
5412      (downcase (gnus-simplify-subject (header-subject h2))))))
5413
5414 (defun gnus-thread-sort-by-date (h1 h2)
5415   "Sort threads by root article date."
5416   (let ((h1 (gnus-thread-header h1))
5417         (h2 (gnus-thread-header h2)))
5418     (string-lessp
5419      (gnus-sortable-date (header-date h1))
5420      (gnus-sortable-date (header-date h2)))))
5421
5422 (defun gnus-thread-sort-by-score (h1 h2)
5423   "Sort threads by root article score.
5424 Unscored articles will be counted as havin a score of zero."
5425   (let ((h1 (gnus-thread-header h1))
5426         (h2 (gnus-thread-header h2)))
5427     (let ((s1 (assq (header-number h1) gnus-newsgroup-scored))
5428           (s2 (assq (header-number h2) gnus-newsgroup-scored)))
5429       (> (or (cdr s1) gnus-summary-default-score 0)
5430          (or (cdr s2) gnus-summary-default-score 0)))))
5431
5432 (defun gnus-thread-sort-by-total-score (h1 h2)
5433   "Sort threads by the sum of all scores in the thread.
5434 Unscored articles will be counted as havin a score of zero."
5435   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
5436
5437 (defun gnus-thread-total-score (thread)
5438   ;;  This function find the total score of  THREAD.
5439   (if (consp thread)
5440       (if (stringp (car thread))
5441           (apply gnus-thread-score-function 0
5442                  (mapcar 'gnus-thread-total-score-1 (cdr thread)))
5443         (gnus-thread-total-score-1 thread))
5444     (gnus-thread-total-score-1 (list thread))))
5445
5446 (defun gnus-thread-total-score-1 (root)
5447   ;; This function find the total score of the thread below ROOT.
5448   (setq root (car root))
5449   (apply gnus-thread-score-function
5450          (or (cdr (assq (header-number root) gnus-newsgroup-scored))
5451              gnus-summary-default-score 0)
5452          (mapcar 'gnus-thread-total-score
5453                  (cdr (gnus-gethash (downcase (header-id root))
5454                                     gnus-newsgroup-dependencies)))))
5455
5456 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
5457 (defvar gnus-tmp-prev-subject "")
5458
5459 ;; Basic ideas by Paul Dworkin <paul@media-lab.media.mit.edu>.
5460 (defun gnus-summary-prepare-threads 
5461   (threads level &optional not-child no-subject cull)
5462   "Prepare summary buffer from THREADS and indentation LEVEL.  
5463 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'  
5464 or a straight list of headers."
5465   (let (thread header number subject clevel)
5466     (while threads
5467       (setq thread (car threads)
5468             threads (cdr threads))
5469       ;; If `thread' is a cons, hierarchical threads are used.  If not,
5470       ;; `thread' is the header.
5471       (if (consp thread)
5472           (setq header (car thread))
5473         (setq header thread)
5474         (and cull
5475              (or (memq (setq number (header-number header))
5476                        gnus-newsgroup-dormant)
5477                  (and gnus-summary-expunge-below
5478                       (< (or (cdr (assq number gnus-newsgroup-scored))
5479                              gnus-summary-default-score 0)
5480                          gnus-summary-expunge-below)))
5481              (progn
5482                (setq header nil)
5483                (setq gnus-newsgroup-unreads 
5484                      (delq number gnus-newsgroup-unreads)))))
5485       (cond 
5486        ((stringp header)
5487         ;; The header is a dummy root.
5488         (cond ((eq gnus-summary-make-false-root 'adopt)
5489                ;; We let the first article adopt the rest.
5490                (gnus-summary-prepare-threads (list (car (cdr thread))) 0)
5491                (setq thread (cdr (cdr thread)))
5492                (while thread
5493                  (gnus-summary-prepare-threads (list (car thread)) 1 t)
5494                  (setq thread (cdr thread))))
5495               ((eq gnus-summary-make-false-root 'dummy)
5496                ;; We output a dummy root.
5497                (gnus-summary-insert-dummy-line 
5498                 nil header (header-number (car (car (cdr thread)))))
5499                (setq clevel 1))
5500               ((eq gnus-summary-make-false-root 'empty)
5501                ;; We print the articles with empty subject fields. 
5502                (gnus-summary-prepare-threads (list (car (cdr thread))) 0)
5503                (setq thread (cdr (cdr thread)))
5504                (while thread
5505                  (gnus-summary-prepare-threads 
5506                   (list (car thread)) 0 nil
5507                   (not (and (eq gnus-summary-gather-subject-limit 'fuzzy)
5508                             (not (string=  
5509                                   (gnus-simplify-subject-re 
5510                                    (header-subject (car (car thread))))
5511                                   (gnus-simplify-subject-re header))))))
5512                  (setq thread (cdr thread))))
5513               (t
5514                ;; We do not make a root for the gathered
5515                ;; sub-threads at all.  
5516                (setq clevel 0)))
5517         ;; Print the sub-threads.
5518         (and (consp thread) (cdr thread)
5519              (gnus-summary-prepare-threads (cdr thread) clevel)))
5520        ;; The header is a real article.
5521        (header
5522         (setq number (header-number header)
5523               subject (header-subject header))
5524         (and gnus-newsgroup-async
5525              (setq gnus-newsgroup-threads
5526                    (cons (cons (header-number header)
5527                                (header-lines header)) gnus-newsgroup-threads)))
5528         (gnus-summary-insert-line
5529          nil header level nil 
5530          (cond ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
5531                ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
5532                ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
5533                ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
5534                (t gnus-ancient-mark))
5535          (memq number gnus-newsgroup-replied)
5536          (memq number gnus-newsgroup-expirable)
5537          (if no-subject 
5538              gnus-summary-same-subject
5539            (if (or (zerop level)
5540                    (and gnus-thread-ignore-subject
5541                         (not (string= 
5542                               (gnus-simplify-subject-re gnus-tmp-prev-subject)
5543                               (gnus-simplify-subject-re subject)))))
5544                subject
5545              gnus-summary-same-subject))
5546          not-child
5547          (cdr (assq number gnus-newsgroup-scored)))
5548         (setq gnus-tmp-prev-subject subject)
5549         ;; Recursively print subthreads.
5550         (and (consp thread) (cdr thread)
5551              (gnus-summary-prepare-threads (cdr thread) (1+ level))))))))
5552
5553 (defun gnus-select-newsgroup (group &optional read-all)
5554   "Select newsgroup GROUP.
5555 If READ-ALL is non-nil, all articles in the group are selected."
5556   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5557          (info (nth 2 entry))
5558          articles header-marks)
5559     (gnus-check-news-server
5560      (setq gnus-current-select-method (gnus-find-method-for-group group)))
5561
5562     (or (gnus-server-opened gnus-current-select-method)
5563         (gnus-open-server gnus-current-select-method)
5564         (error "Couldn't open server"))
5565     
5566     (or (and (eq (car entry) t)
5567              (gnus-activate-newsgroup (car info)))
5568         (gnus-request-group group t)
5569         (progn
5570           (kill-buffer (current-buffer))
5571           (error "Couldn't request group %s: %s" 
5572                  group (gnus-status-message group))))
5573
5574     (setq gnus-newsgroup-name group)
5575     (setq gnus-newsgroup-unselected nil)
5576     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5577
5578     (and gnus-asynchronous
5579          (gnus-check-backend-function 
5580           'request-asynchronous gnus-newsgroup-name)
5581          (setq gnus-newsgroup-async
5582                (gnus-request-asynchronous gnus-newsgroup-name)))
5583
5584     (setq articles (gnus-articles-to-read group read-all))
5585
5586     (cond 
5587      ((null articles) 
5588       (message "Couldn't select newsgroup")
5589       'quit)
5590      ((eq articles 0) nil)
5591      (t
5592       ;; Init the dependencies hash table.
5593       (setq gnus-newsgroup-dependencies 
5594             (gnus-make-hashtable (length articles)))
5595       ;; Retrieve the headers and read them in.
5596       (setq gnus-newsgroup-headers 
5597             (if (eq 'nov (setq gnus-headers-retrieved-by
5598                                (gnus-retrieve-headers 
5599                                 (if gnus-fetch-old-headers 
5600                                     (cons 1 articles) articles) 
5601                                 gnus-newsgroup-name)))
5602                 (progn
5603                   (gnus-get-newsgroup-headers-xover articles))
5604               (gnus-get-newsgroup-headers)))
5605       ;; If we were to fetch old headers, but the backend didn't
5606       ;; support XOVER, then it is possible we fetched one article
5607       ;; that we shouldn't have. If that's the case, we pop it off the
5608       ;; list of headers.
5609       (and (not (eq gnus-headers-retrieved-by 'nov))
5610            gnus-fetch-old-headers
5611            gnus-newsgroup-headers
5612            (/= (header-number (car gnus-newsgroup-headers)) (car articles))
5613            (setq gnus-newsgroup-headers (cdr gnus-newsgroup-headers)))
5614       ;; Remove cancelled articles from the list of unread articles.
5615       (setq gnus-newsgroup-unreads
5616             (gnus-set-sorted-intersection 
5617              gnus-newsgroup-unreads
5618              (mapcar (lambda (headers) (header-number headers))
5619                      gnus-newsgroup-headers)))
5620       ;; Adjust and set lists of article marks.
5621       (and info
5622            (let (marked)
5623              (gnus-adjust-marked-articles info)
5624              (setq gnus-newsgroup-marked 
5625                    (cdr (assq 'tick (setq marked (nth 3 info)))))
5626              (setq gnus-newsgroup-replied (cdr (assq 'reply marked)))
5627              (setq gnus-newsgroup-expirable (cdr (assq 'expire marked)))
5628              (setq gnus-newsgroup-killed (cdr (assq 'killed marked)))
5629              (setq gnus-newsgroup-bookmarks (cdr (assq 'bookmark marked)))
5630              (setq gnus-newsgroup-dormant (cdr (assq 'dormant marked)))
5631              (setq gnus-newsgroup-scored (cdr (assq 'score marked)))
5632              (setq gnus-newsgroup-processable nil)))
5633       ;; Check whether auto-expire is to be done in this group.
5634       (setq gnus-newsgroup-auto-expire
5635             (or (and (stringp gnus-auto-expirable-newsgroups)
5636                      (string-match gnus-auto-expirable-newsgroups group))
5637                 (memq 'auto-expire (nth 5 info))))
5638       ;; First and last article in this newsgroup.
5639       (and gnus-newsgroup-headers
5640            (setq gnus-newsgroup-begin 
5641                  (header-number (car gnus-newsgroup-headers)))
5642            (setq gnus-newsgroup-end
5643                  (header-number (gnus-last-element gnus-newsgroup-headers))))
5644       (setq gnus-reffed-article-number -1)
5645       ;; GROUP is successfully selected.
5646       (or gnus-newsgroup-headers t)))))
5647
5648 (defun gnus-articles-to-read (group read-all)
5649   ;; Find out what articles the user wants to read.
5650   (let* ((articles
5651           ;; Select all articles if `read-all' is non-nil, or if all the
5652           ;; unread articles are dormant articles.
5653           (if (or read-all
5654                   (= (length gnus-newsgroup-unreads) 
5655                      (length gnus-newsgroup-dormant)))
5656               (gnus-uncompress-range 
5657                (gnus-gethash group gnus-active-hashtb))
5658             gnus-newsgroup-unreads))
5659          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5660          (scored (length scored-list))
5661          (number (length articles))
5662          (marked (+ (length gnus-newsgroup-marked)
5663                     (length gnus-newsgroup-dormant)))
5664          (select
5665           (condition-case ()
5666               (cond ((and (or (<= scored marked)
5667                               (= scored number))
5668                           (numberp gnus-large-newsgroup)
5669                           (> number gnus-large-newsgroup))
5670                      (let ((input
5671                             (read-string
5672                              (format
5673                               "How many articles from %s (default %d): "
5674                               gnus-newsgroup-name number))))
5675                        (if (string-equal input "")
5676                            number input)))
5677                     ((and (> scored marked) (< scored number))
5678                      (let ((input
5679                             (read-string
5680                              (format 
5681                               "%s %s (%d scored, %d total): "
5682                               "How many articles from"
5683                               group scored number))))
5684                        (if (string-equal input "")
5685                            number input)))
5686                     (t number))
5687             (quit nil)))
5688          total-articles)
5689     (setq select (if (stringp select) (string-to-number select) select))
5690     (if (or (null select) (zerop select))
5691         select
5692       (if (and (not (zerop scored)) (<= (abs select) scored))
5693           (progn
5694             (setq articles (sort scored-list '<))
5695             (setq number (length articles)))
5696         (setq articles (copy-sequence articles)))
5697
5698       (setq total-articles articles)
5699       
5700       (if (< (abs select) number)
5701           (if (< select 0) 
5702               ;; Select the N oldest articles.
5703               (setcdr (nthcdr (1- (abs select)) articles) nil)
5704             ;; Select the N most recent articles.
5705             (setq articles (nthcdr (- number select) articles))))
5706       (setq gnus-newsgroup-unselected
5707             (gnus-sorted-intersection
5708              gnus-newsgroup-unreads
5709              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
5710       articles)))
5711
5712 (defun gnus-killed-articles (killed articles)
5713   (let (out)
5714     (while articles
5715       (if (inline (gnus-member-of-range (car articles) killed))
5716           (setq out (cons (car articles) out)))
5717       (setq articles (cdr articles)))
5718     out))
5719
5720 (defun gnus-adjust-marked-articles (info &optional active)
5721   "Remove all marked articles that are no longer legal."
5722   (let ((marked-lists (nth 3 info))
5723         (active (or active (gnus-gethash (car info) gnus-active-hashtb)))
5724         marked m prev)
5725     ;; There are many types of marked articles.
5726     (while marked-lists
5727       (setq m (cdr (setq prev (car marked-lists))))
5728       (cond ((or (eq 'tick (car prev)) (eq 'dormant (car prev)))
5729              ;; Make sure that all ticked articles are a subset of the
5730              ;; unread/unselected articles.
5731              (while m
5732                (if (or (memq (car m) gnus-newsgroup-unreads)
5733                        (memq (car m) gnus-newsgroup-unselected))
5734                    (setq prev m)
5735                  (setcdr prev (cdr m)))
5736                (setq m (cdr m))))
5737             ((eq 'score (car prev))
5738              ;; Scored articles should be a subset of
5739              ;; unread/unselected articles. 
5740              (while m
5741                (if (or (memq (car (car m)) gnus-newsgroup-unreads)
5742                        (memq (car (car m)) gnus-newsgroup-unreads))
5743                    (setq prev m)
5744                  (setcdr prev (cdr m)))
5745                (setq m (cdr m))))
5746             ((eq 'bookmark (car prev))
5747              ;; Bookmarks should be a subset of active articles.
5748              (while m
5749                (if (< (car (car m)) (car active))
5750                    (setcdr prev (cdr m))
5751                  (setq prev m))
5752                (setq m (cdr m))))
5753             ((eq 'killed (car prev))
5754              ;; Articles that have been through the kill process are
5755              ;; to be a subset of active articles.
5756              (while (and m (< (or (and (numberp (car m)) (car m))
5757                                   (cdr (car m)))
5758                               (car active)))
5759                (setcdr prev (cdr m))
5760                (setq m (cdr m)))
5761              (if (and m (< (or (and (numberp (car m)) (car m))
5762                                (car (car m)))
5763                            (car active))) 
5764                  (setcar (if (numberp (car m)) m (car m)) (car active))))
5765             ((or (eq 'reply (car prev)) (eq 'expire (car prev)))
5766              ;; The replied and expirable articles have to be articles
5767              ;; that are active. 
5768              (while m
5769                (if (< (car m) (car active))
5770                    (setcdr prev (cdr m))
5771                  (setq prev m))
5772                (setq m (cdr m)))))
5773       (setq marked-lists (cdr marked-lists)))
5774     ;; Remove all lists that are empty.
5775     (setq marked-lists (nth 3 info))
5776     (if marked-lists
5777         (progn
5778           (while (= 1 (length (car marked-lists)))
5779             (setq marked-lists (cdr marked-lists)))
5780           (setq m (cdr (setq prev marked-lists)))
5781           (while m
5782             (if (= 1 (length (car m)))
5783                 (setcdr prev (cdr m))
5784               (setq prev m))
5785             (setq m (cdr m)))
5786           (setcar (nthcdr 3 info) marked-lists)))
5787     ;; Finally, if there are no marked lists at all left, and if there
5788     ;; are no elements after the lists in the info list, we just chop
5789     ;; the info list off before the marked lists.
5790     (and (null marked-lists) 
5791          (not (nthcdr 4 info))
5792          (setcdr (nthcdr 2 info) nil)))
5793   info)
5794
5795 (defun gnus-set-marked-articles 
5796   (info ticked replied expirable killed dormant bookmark score) 
5797   "Enter the various lists of marked articles into the newsgroup info list."
5798   (let (newmarked)
5799     (and ticked (setq newmarked (cons (cons 'tick ticked) nil)))
5800     (and replied (setq newmarked (cons (cons 'reply replied) newmarked)))
5801     (and expirable (setq newmarked (cons (cons 'expire expirable) 
5802                                          newmarked)))
5803     (and killed (setq newmarked (cons (cons 'killed killed) newmarked)))
5804     (and dormant (setq newmarked (cons (cons 'dormant dormant) newmarked)))
5805     (and bookmark (setq newmarked (cons (cons 'bookmark bookmark) 
5806                                         newmarked)))
5807     (and score (setq newmarked (cons (cons 'score score) newmarked)))
5808     (if (nthcdr 3 info)
5809         (if newmarked
5810             (setcar (nthcdr 3 info) newmarked)
5811           (if (not (nthcdr 4 info))
5812               (setcdr (nthcdr 2 info) nil)
5813             (setcar (nthcdr 3 info) nil)))
5814       (if newmarked
5815           (setcdr (nthcdr 2 info) (cons newmarked nil))))))
5816
5817 (defun gnus-add-marked-articles (group type articles &optional info force)
5818   ;; Add ARTICLES of TYPE to the info of GROUP.
5819   ;; If INFO is non-nil, use that info. If FORCE is non-nil, don't
5820   ;; add, but replace marked articles of TYPE with ARTICLES.
5821   (let ((info (or info (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
5822         marked m)
5823     (or (not info)
5824         (and (not (setq marked (nthcdr 3 info)))
5825              (setcdr (nthcdr 2 info) (list (list (cons type articles)))))
5826         (and (not (setq m (assq type (car marked))))
5827              (setcar marked (cons (cons type articles) (car marked))))
5828         (if force
5829             (setcdr m articles)
5830           (nconc m articles)))))
5831          
5832 (defun gnus-set-mode-line (where)
5833   "This function sets the mode line of the article or summary buffers.
5834 If WHERE is `summary', the summary mode line format will be used."
5835   (if (memq where gnus-updated-mode-lines)
5836       (let (mode-string)
5837         (save-excursion
5838           (set-buffer gnus-summary-buffer)
5839           (let* ((mformat (if (eq where 'article) 
5840                               gnus-article-mode-line-format-spec
5841                             gnus-summary-mode-line-format-spec))
5842                  (group-name gnus-newsgroup-name)
5843                  (article-number (or gnus-current-article 0))
5844                  (unread (- (length gnus-newsgroup-unreads)
5845                             (length gnus-newsgroup-dormant)))
5846                  (unread-and-unticked 
5847                   (- unread (length gnus-newsgroup-marked)))
5848                  (unselected (length gnus-newsgroup-unselected))
5849                  (unread-and-unselected
5850                   (cond ((and (zerop unread-and-unticked)
5851                               (zerop unselected)) "")
5852                         ((zerop unselected) 
5853                          (format "{%d more}" unread-and-unticked))
5854                         (t (format "{%d(+%d) more}"
5855                                    unread-and-unticked unselected))))
5856                  (subject
5857                   (if gnus-current-headers
5858                       (header-subject gnus-current-headers) ""))
5859                  (max-len (and gnus-mode-non-string-length
5860                                (- (frame-width) gnus-mode-non-string-length)))
5861                  header) ;; passed as argument to any user-format-funcs
5862             (setq mode-string (eval mformat))
5863             (or (numberp max-len)
5864                 (setq max-len (length mode-string)))
5865             (if (< max-len 4) (setq max-len 4))
5866             (if (> (length mode-string) max-len)
5867                 (setq mode-string 
5868                       (concat (substring mode-string 0 (- max-len 3))
5869                               "...")))
5870             (setq mode-string (format (format "%%-%ds" max-len)
5871                                       mode-string))))
5872         (setq mode-line-buffer-identification mode-string)
5873         (set-buffer-modified-p t))))
5874
5875 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5876   "Go through the HEADERS list and add all Xrefs to a hash table.
5877 The resulting hash table is returned, or nil if no Xrefs were found."
5878   (let* ((from-method (gnus-find-method-for-group from-newsgroup))
5879          (prefix (if (and 
5880                       (gnus-group-foreign-p from-newsgroup)
5881                       (not (memq 'virtual 
5882                                  (assoc (symbol-name (car from-method))
5883                                         gnus-valid-select-methods))))
5884                      (gnus-group-real-prefix from-newsgroup)))
5885          (xref-hashtb (make-vector 63 0))
5886          start group entry number xrefs header)
5887     (while headers
5888       (setq header (car headers))
5889       (if (and (setq xrefs (header-xref header))
5890                (not (memq (header-number header) unreads)))
5891           (progn
5892             (setq start 0)
5893             (while (string-match "\\([^ ]+\\):\\([0-9]+\\)" xrefs start)
5894               (setq start (match-end 0))
5895               (setq group (concat prefix (substring xrefs (match-beginning 1) 
5896                                                     (match-end 1))))
5897               (setq number 
5898                     (string-to-int (substring xrefs (match-beginning 2) 
5899                                               (match-end 2))))
5900               (if (setq entry (gnus-gethash group xref-hashtb))
5901                   (setcdr entry (cons number (cdr entry)))
5902                 (gnus-sethash group (cons number nil) xref-hashtb)))))
5903       (setq headers (cdr headers)))
5904     (if start xref-hashtb nil)))
5905
5906 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads expirable)
5907   "Look through all the headers and mark the Xrefs as read."
5908   (let ((virtual (memq 'virtual 
5909                        (assoc (symbol-name (car (gnus-find-method-for-group 
5910                                                  from-newsgroup)))
5911                               gnus-valid-select-methods)))
5912         name entry read info xref-hashtb idlist active num range exps method
5913         nth4)
5914     (save-excursion
5915       (set-buffer gnus-group-buffer)
5916       (if (setq xref-hashtb 
5917                 (gnus-create-xref-hashtb from-newsgroup headers unreads))
5918           (mapatoms 
5919            (lambda (group)
5920              (if (string= from-newsgroup (setq name (symbol-name group)))
5921                  ()
5922                (setq idlist (symbol-value group))
5923                ;; Dead groups are not updated.
5924                (if (and (prog1 
5925                             (setq entry (gnus-gethash name gnus-newsrc-hashtb)
5926                                   info (nth 2 entry))
5927                           (if (stringp (setq nth4 (nth 4 info)))
5928                               (setq nth4 (gnus-server-to-method nth4))))
5929                         ;; Only do the xrefs if the group has the same
5930                         ;; select method as the group we have just read.
5931                         (or (gnus-methods-equal-p 
5932                              nth4 (gnus-find-method-for-group from-newsgroup))
5933                             virtual
5934                             (equal nth4 
5935                                    (setq method (gnus-find-method-for-group 
5936                                                  from-newsgroup)))
5937                             (and (equal (car nth4) (car method))
5938                                  (equal (nth 1 nth4) (nth 1 method))))
5939                         gnus-use-cross-reference
5940                         (or (not (eq gnus-use-cross-reference t))
5941                             virtual
5942                             ;; Only do cross-references on subscribed
5943                             ;; groups, if that is what is wanted.  
5944                             (<= (nth 1 info) gnus-level-subscribed)))
5945                    (progn
5946                      (setq num 0)
5947                      ;; Set the new list of read articles in this group.
5948                      (setq active (gnus-gethash name gnus-active-hashtb))
5949                      ;; First peel off all illegal article numbers.
5950                      (if active
5951                          (let ((ids idlist)
5952                                (ticked (cdr (assq 'tick (nth 3 info))))
5953                                (dormant (cdr (assq 'dormant (nth 3 info))))
5954                                id)
5955                            (setq exps nil)
5956                            (while ids
5957                              (setq id (car ids))
5958                              (if (or (> id (cdr active))
5959                                      (< id (car active))
5960                                      (memq id ticked)
5961                                      (memq id dormant))
5962                                  (setq idlist (delq id idlist)))
5963                              (and (memq id expirable)
5964                                   (setq exps (cons id exps)))
5965                              (setq ids (cdr ids)))))
5966                      ;; Update expirable articles.
5967                      (gnus-add-marked-articles nil 'expirable exps info)
5968                      (and (null (nth 2 info))
5969                           (> (car active) 1)
5970                           (setcar (nthcdr 2 info) (cons 1 (1- (car active)))))
5971                      (setcar (nthcdr 2 info)
5972                              (setq range
5973                                    (gnus-add-to-range 
5974                                     (nth 2 info) 
5975                                     (setq idlist (sort idlist '<)))))
5976                      ;; Then we have to re-compute how many unread
5977                      ;; articles there are in this group.
5978                      (if active
5979                          (progn
5980                            (cond 
5981                             ((not range)
5982                              (setq num (- (1+ (cdr active)) (car active))))
5983                             ((not (listp (cdr range)))
5984                              (setq num (- (cdr active) (- (1+ (cdr range)) 
5985                                                           (car range)))))
5986                             (t
5987                              (while range
5988                                (if (numberp (car range))
5989                                    (setq num (1+ num))
5990                                  (setq num (+ num (- (1+ (cdr (car range)))
5991                                                      (car (car range))))))
5992                                (setq range (cdr range)))
5993                              (setq num (- (cdr active) num))))
5994                            ;; Update the number of unread articles.
5995                            (setcar 
5996                             entry 
5997                             (max 0 (- num 
5998                                       (length (cdr (assq 'tick (nth 3 info))))
5999                                       (length 
6000                                        (cdr (assq 'dormant (nth 3 info)))))))
6001                            ;; Update the group buffer.
6002                            (gnus-group-update-group name t)))))))
6003            xref-hashtb)))))
6004
6005 (defun gnus-methods-equal-p (m1 m2)
6006   (let ((m1 (or m1 gnus-select-method))
6007         (m2 (or m2 gnus-select-method)))
6008     (or (equal m1 m2)
6009         (and (eq (car m1) (car m2))
6010              (or (not (memq 'address (assoc (symbol-name (car m1))
6011                                             gnus-valid-select-methods)))
6012                  (equal (nth 1 m1) (nth 1 m2)))))))
6013
6014 (defsubst gnus-header-value ()
6015   (buffer-substring (match-end 0) (gnus-point-at-eol)))
6016
6017 (defun gnus-get-newsgroup-headers ()
6018   (setq gnus-article-internal-prepare-hook nil)
6019   (let ((cur nntp-server-buffer)
6020         (dependencies gnus-newsgroup-dependencies)
6021         (none-id 0)
6022         headers char article id dep end)
6023     (save-excursion
6024       (set-buffer nntp-server-buffer)
6025       (goto-char 1)
6026       ;; Search to the beginning of the next header. Error messages
6027       ;; do not begin with 2 or 3.
6028       (while (re-search-forward "^[23][0-9]+ " nil t)
6029         (let ((header (make-vector 9 nil))
6030               (c (following-char))
6031               (case-fold-search t)
6032               (p (point))
6033               from subject in-reply-to references ref)
6034           (setq id nil
6035                 ref nil
6036                 references nil
6037                 subject nil
6038                 from nil)
6039           (header-set-number header (setq article (read cur)))
6040           ;; This implementation of this function, with nine
6041           ;; search-forwards instead of the one re-search-forward and
6042           ;; a case (which basically was the old function) is actually
6043           ;; about twice as fast, even though it looks messier. You
6044           ;; can't have everything, I guess. Speed and elegance
6045           ;; doesn't always come hand in hand.
6046           (save-restriction
6047             (narrow-to-region (point) (save-excursion 
6048                                         (search-forward "\n.\n" nil t)))
6049             (if (search-forward "\nfrom: " nil t)
6050                 (header-set-from header (gnus-header-value))
6051               (header-set-from header "(nobody)"))
6052             (goto-char p)
6053             (if (search-forward "\nsubject: " nil t)
6054                 (header-set-subject header (gnus-header-value))
6055               (header-set-subject header "(none)"))
6056             (goto-char p)
6057             (and (search-forward "\nxref: " nil t)
6058                  (header-set-xref header (gnus-header-value)))
6059             (goto-char p)
6060             (and (search-forward "\nlines: " nil t)
6061                  (header-set-lines header (read cur)))
6062             (goto-char p)
6063             (and (search-forward "\ndate: " nil t)
6064                  (header-set-date header (gnus-header-value)))
6065             (goto-char p)
6066             (if (search-forward "\nmessage-id: " nil t)
6067                 (header-set-id header (setq id (gnus-header-value)))
6068               ;; If there was no message-id, we just fake one to make
6069               ;; subsequent routines simpler.
6070               (header-set-id 
6071                header 
6072                (setq id (concat "none+" (int-to-string 
6073                                          (setq none-id (1+ none-id)))))))
6074             (goto-char p)
6075             (if (search-forward "\nreferences: " nil t)
6076                 (progn
6077                   (header-set-references header (gnus-header-value))
6078                   (setq end (match-end 0))
6079                   (save-excursion
6080                     (setq ref 
6081                           (downcase
6082                            (buffer-substring
6083                             (progn 
6084                               (end-of-line)
6085                               (search-backward ">" end t)
6086                               (1+ (point)))
6087                             (progn
6088                               (search-backward "<" end t)
6089                               (point)))))))
6090               ;; Get the references from the in-reply-to header if there
6091               ;; ware no references and the in-reply-to header looks
6092               ;; promising. 
6093               (if (and (search-forward "\nin-reply-to: " nil t)
6094                        (setq in-reply-to (gnus-header-value))
6095                        (string-match "<[^>]+>" in-reply-to))
6096                   (progn
6097                     (header-set-references 
6098                      header 
6099                      (setq ref (substring in-reply-to (match-beginning 0)
6100                                           (match-end 0))))
6101                     (setq ref (downcase ref)))
6102                 (setq ref "none")))
6103             ;; We do some threading while we read the headers. The
6104             ;; message-id and the last reference are both entered into
6105             ;; the same hash table. Some tippy-toeing around has to be
6106             ;; done in case an article has arrived before the article
6107             ;; which it refers to.
6108             (if (boundp (setq dep (intern (downcase id) dependencies)))
6109                 (if (car (symbol-value dep))
6110                     ;; An article with this Message-ID has already
6111                     ;; been seen, so we ignore this one, except we add
6112                     ;; any additional Xrefs (in case the two articles
6113                     ;; came from different servers.
6114                     (progn
6115                       (header-set-xref 
6116                        (car (symbol-value dep))
6117                        (concat (or (header-xref (car (symbol-value dep))) "")
6118                                (or (header-xref header) "")))
6119                       (setq header nil))
6120                   (setcar (symbol-value dep) header))
6121               (set dep (list header)))
6122             (if header
6123                 (progn
6124                   (if (boundp (setq dep (intern ref dependencies)))
6125                       (setcdr (symbol-value dep) 
6126                               (cons header (cdr (symbol-value dep))))
6127                     (set dep (list nil header)))
6128                   (setq headers (cons header headers))))
6129             (goto-char (point-max))))))
6130     (nreverse headers)))
6131
6132 ;; The following macros and functions were written by Felix Lee
6133 ;; <flee@cse.psu.edu>. 
6134
6135 ;; This is almost 4x faster than (string-to-int (buffer-substring ... ))
6136 ;; primarily because of garbage collection.  -jwz
6137 (defmacro gnus-read-integer (&optional point move-p)
6138   (` ((, (if move-p 'progn 'save-excursion))
6139       (,@ (if point (list (list 'goto-char point))))
6140       (if (and (<= (following-char) ?9)
6141                (>= (following-char) ?0))
6142           (read (current-buffer))
6143         0))))
6144
6145 (defmacro gnus-nov-skip-field ()
6146   '(search-forward "\t" eol 'end))
6147
6148 (defmacro gnus-nov-field ()
6149   '(buffer-substring
6150     (point)
6151     (progn (gnus-nov-skip-field) (1- (point)))))
6152
6153 ;; Goes through the xover lines and returns a list of vectors
6154 (defun gnus-get-newsgroup-headers-xover (sequence)
6155   "Parse the news overview data in the server buffer, and return a
6156 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
6157   ;; Get the Xref when the users reads the articles since most/some
6158   ;; NNTP servers do not include Xrefs when using XOVER.
6159   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
6160   (let ((cur nntp-server-buffer)
6161         (dependencies gnus-newsgroup-dependencies)
6162         (none 0)
6163         number headers header)
6164     (save-excursion
6165       (set-buffer nntp-server-buffer)
6166       (goto-char (point-min))
6167       (while (and sequence (not (eobp)))
6168         (setq number (read cur))
6169         (while (and sequence (< (car sequence) number))
6170           (setq sequence (cdr sequence)))
6171         (and sequence 
6172              (eq number (car sequence))
6173              (progn
6174                (setq sequence (cdr sequence))
6175                (if (setq header 
6176                          (inline (gnus-nov-parse-line number dependencies)))
6177                    (setq headers (cons header headers)))))
6178         (forward-line 1))
6179       (setq headers (nreverse headers)))
6180     headers))
6181
6182 (defun gnus-nov-parse-line (number dependencies)
6183   "Point has to be after the number on the beginning of the line."
6184   (let ((none 0)
6185         header (eol (gnus-point-at-eol)) ref id dep)
6186     (forward-char)
6187     ;; overview: [num subject from date id refs chars lines misc]
6188     (save-restriction
6189       (narrow-to-region (point) eol)
6190       (condition-case nil
6191           (setq header
6192                 (vector 
6193                  number                 ; number
6194                  (gnus-nov-field)       ; subject
6195                  (gnus-nov-field)       ; from
6196                  (gnus-nov-field)       ; date
6197                  (setq id (or (gnus-nov-field)
6198                               (concat "none+"
6199                                      (int-to-string 
6200                                       (setq none (1+ none)))))) ; id
6201                  (progn
6202                    (save-excursion
6203                      (let ((beg (point)))
6204                        (search-forward "\t" eol)
6205                        (if (search-backward ">" beg t)
6206                            (setq ref 
6207                                  (downcase 
6208                                   (buffer-substring 
6209                                    (1+ (point))
6210                                    (progn
6211                                      (search-backward "<" beg t)
6212                                      (point)))))
6213                          (setq ref nil))))
6214                    (gnus-nov-field))    ; refs
6215                  (read (current-buffer)) ; chars
6216                  (read (current-buffer)) ; lines
6217                  (if (/= (following-char) ?\t)
6218                      nil
6219                    (forward-char 1)
6220                    (gnus-nov-field))    ; misc
6221                  ))
6222         (error (progn 
6223                  (setq header nil)
6224                  (goto-char eol)))))
6225     ;; We build the thread tree.
6226     (and header
6227          (if (boundp (setq dep (intern (downcase id) dependencies)))
6228              (if (car (symbol-value dep))
6229                  ;; An article with this Message-ID has already been seen,
6230                  ;; so we ignore this one, except we add any additional
6231                  ;; Xrefs (in case the two articles came from different
6232                  ;; servers.
6233                  (progn
6234                    (header-set-xref 
6235                     (car (symbol-value dep))
6236                     (concat (or (header-xref (car (symbol-value dep))) "")
6237                             (or (header-xref header) "")))
6238                    (setq header nil))
6239                (setcar (symbol-value dep) header))
6240            (set dep (list header))))
6241     (if header
6242         (progn
6243           (if (boundp (setq dep (intern (or ref "none") 
6244                                         dependencies)))
6245               (setcdr (symbol-value dep) 
6246                       (cons header (cdr (symbol-value dep))))
6247             (set dep (list nil header)))))
6248     header))
6249
6250 (defun gnus-article-get-xrefs ()
6251   "Fill in the Xref value in `gnus-current-headers', if necessary.
6252 This is meant to be called in `gnus-article-internal-prepare-hook'."
6253   (or (not gnus-use-cross-reference)
6254       (header-xref gnus-current-headers)
6255       (let ((case-fold-search t)
6256             xref)
6257         (save-restriction
6258           (gnus-narrow-to-headers)
6259           (goto-char (point-min))
6260           (if (or (and (eq (downcase (following-char)) ?x)
6261                        (looking-at "Xref:"))
6262                   (search-forward "\nXref:" nil t))
6263               (progn
6264                 (goto-char (1+ (match-end 0)))
6265                 (setq xref (buffer-substring (point) 
6266                                              (progn (end-of-line) (point))))
6267                 (save-excursion
6268                   (set-buffer gnus-summary-buffer)
6269                   (header-set-xref gnus-current-headers xref))))))))
6270
6271 (defalias 'gnus-find-header-by-number 'gnus-get-header-by-number)
6272 (make-obsolete 'gnus-find-header-by-number 'gnus-get-header-by-number)
6273
6274 ;; Return a header specified by a NUMBER.
6275 (defun gnus-get-header-by-number (number)
6276   (save-excursion
6277     (set-buffer gnus-summary-buffer)
6278     (or gnus-newsgroup-headers-hashtb-by-number
6279         (gnus-make-headers-hashtable-by-number))
6280     (gnus-gethash (int-to-string number)
6281                   gnus-newsgroup-headers-hashtb-by-number)))
6282
6283 (defun gnus-make-headers-hashtable-by-number ()
6284   "Make hashtable for the variable gnus-newsgroup-headers by number."
6285   (save-excursion
6286     (set-buffer gnus-summary-buffer)
6287     (let ((headers gnus-newsgroup-headers)
6288           header)
6289       (setq gnus-newsgroup-headers-hashtb-by-number
6290             (gnus-make-hashtable (length headers)))
6291       (while headers
6292         (setq header (car headers))
6293         (gnus-sethash (int-to-string (header-number header))
6294                       header gnus-newsgroup-headers-hashtb-by-number)
6295         (setq headers (cdr headers))))))
6296
6297 (defun gnus-more-header-backward ()
6298   "Find new header backward."
6299   (let ((first (car (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
6300         (artnum gnus-newsgroup-begin)
6301         (header nil))
6302     (while (and (not header)
6303                 (> artnum first))
6304       (setq artnum (1- artnum))
6305       (setq header (gnus-read-header artnum)))
6306     header))
6307
6308 (defun gnus-more-header-forward (&optional backward)
6309   "Find new header forward.
6310 If BACKWARD, find new header backward instead."
6311   (if backward
6312       (gnus-more-header-backward)
6313     (let ((last (cdr (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
6314           (artnum gnus-newsgroup-end)
6315           (header nil))
6316       (while (and (not header)
6317                   (< artnum last))
6318         (setq artnum (1+ artnum))
6319         (setq header (gnus-read-header artnum)))
6320       header)))
6321
6322 (defun gnus-extend-newsgroup (header &optional backward)
6323   "Extend newsgroup selection with HEADER.
6324 Optional argument BACKWARD means extend toward backward."
6325   (if header
6326       (let ((artnum (header-number header)))
6327         (setq gnus-newsgroup-headers
6328               (if backward
6329                   (cons header gnus-newsgroup-headers)
6330                 (nconc gnus-newsgroup-headers (list header))))
6331         (setq gnus-newsgroup-unselected
6332               (delq artnum gnus-newsgroup-unselected))
6333         (setq gnus-newsgroup-begin (min gnus-newsgroup-begin artnum))
6334         (setq gnus-newsgroup-end (max gnus-newsgroup-end artnum)))))
6335
6336 (defun gnus-summary-work-articles (n)
6337   "Return a list of articles to be worked upon. The prefix argument,
6338 the list of process marked articles, and the current article will be
6339 taken into consideration."
6340   (let (articles)
6341     (if (and n (numberp n))
6342         (let ((backward (< n 0))
6343               (n (abs n)))
6344           (save-excursion
6345             (while (and (> n 0)
6346                         (setq articles (cons (gnus-summary-article-number) 
6347                                              articles))
6348                         (gnus-summary-search-forward nil nil backward))
6349               (setq n (1- n))))
6350           (sort articles (function <)))
6351       (or (reverse gnus-newsgroup-processable)
6352           (list (gnus-summary-article-number))))))
6353
6354 (defun gnus-summary-search-group (&optional backward use-level)
6355   "Search for next unread newsgroup.
6356 If optional argument BACKWARD is non-nil, search backward instead."
6357   (save-excursion
6358     (set-buffer gnus-group-buffer)
6359     (save-excursion
6360       ;; We don't want to alter current point of group mode buffer.
6361       (if (gnus-group-search-forward 
6362            backward nil
6363            (if use-level (gnus-group-group-level) nil))
6364           (gnus-group-group-name)))))
6365
6366 (defun gnus-summary-best-group (&optional exclude-group)
6367   "Find the name of the best unread group.
6368 If EXCLUDE-GROUP, do not go to this group."
6369   (save-excursion
6370     (set-buffer gnus-group-buffer)
6371     (save-excursion
6372       (gnus-group-best-unread-group exclude-group))))
6373
6374 (defun gnus-summary-search-subject (&optional backward unread subject)
6375   "Search for article forward.
6376 If BACKWARD is non-nil, search backward.
6377 If UNREAD is non-nil, only unread articles are selected.
6378 If SUBJECT is non-nil, the article which has the same subject will be
6379 searched for." 
6380   (let ((func (if backward 'previous-single-property-change
6381                 'next-single-property-change))
6382         (beg (point))
6383         (did t)
6384         pos)
6385     (beginning-of-line)
6386     (and gnus-summary-check-current unread
6387          (eq (get-text-property (point) 'gnus-mark) gnus-unread-mark)
6388          (setq did nil))
6389     (if (not did)
6390         ()
6391       (forward-char (if backward (if (bobp) 0 -1) (if (eobp) 0 1)))
6392       (while
6393           (and 
6394            (setq pos (funcall func (point) 'gnus-number))
6395            (goto-char (if backward (1- pos) pos))
6396            (setq did
6397                  (not (and
6398                        (or (not unread)
6399                            (eq (get-text-property (point) 'gnus-mark)
6400                                gnus-unread-mark))
6401                        (or (not subject)
6402                            (equal (gnus-simplify-subject-re subject)
6403                                   (gnus-simplify-subject-re
6404                                    (get-text-property (point)
6405                                                       'gnus-subject)))))))
6406            (if backward (if (bobp) nil (forward-char -1) t)
6407              (if (eobp) nil (forward-char 1) t)))))
6408     (if did
6409         (progn (goto-char beg) nil)
6410       (prog1
6411           (get-text-property (point) 'gnus-number)
6412         (gnus-summary-position-cursor)))))
6413
6414 (defun gnus-summary-search-forward (&optional unread subject backward)
6415   "Search for article forward.
6416 If UNREAD is non-nil, only unread articles are selected.
6417 If SUBJECT is non-nil, the article which has the same subject will be
6418 searched for. 
6419 If BACKWARD is non-nil, the search will be performed backwards instead."
6420   (gnus-summary-search-subject backward unread subject))
6421
6422 (defun gnus-summary-search-backward (&optional unread subject)
6423   "Search for article backward.
6424 If 1st optional argument UNREAD is non-nil, only unread article is selected.
6425 If 2nd optional argument SUBJECT is non-nil, the article which has
6426 the same subject will be searched for."
6427   (gnus-summary-search-forward unread subject t))
6428
6429 (defun gnus-summary-article-number (&optional number-or-nil)
6430   "The article number of the article on the current line.
6431 If there isn's an article number here, then we return the current
6432 article number."
6433   (let* ((p (point))
6434          (number (get-text-property 
6435                   (progn (beginning-of-line) (prog1 (point) (goto-char p)))
6436                   'gnus-number)))
6437     (if number-or-nil number (or number gnus-current-article))))
6438
6439 (defun gnus-summary-thread-level ()
6440   "The thread level of the article on the current line."
6441   (or (get-text-property (gnus-point-at-bol) 'gnus-thread)
6442       0))
6443
6444 (defun gnus-summary-pseudo-article ()
6445   "The thread level of the article on the current line."
6446   (get-text-property (gnus-point-at-bol) 'gnus-pseudo))
6447
6448 (defun gnus-summary-article-mark ()
6449   "The mark on the current line."
6450   (get-text-property (gnus-point-at-bol) 'gnus-mark))
6451
6452 (defun gnus-summary-subject-string ()
6453   "Return current subject string or nil if nothing."
6454   (get-text-property (gnus-point-at-bol) 'gnus-subject))
6455
6456 (defalias 'gnus-summary-score 'gnus-summary-article-score)
6457 (make-obsolete 'gnus-summary-score 'gnus-summary-article-score)
6458 (defun gnus-summary-article-score ()
6459   "Return current article score."
6460   (or (cdr (assq (gnus-summary-article-number) gnus-newsgroup-scored))
6461       gnus-summary-default-score 0))
6462
6463 ;; Written by Sudish Joseph <joseph@cis.ohio-state.edu>.
6464 (defun gnus-summary-recenter-new ()
6465   "Center point in the summary window.
6466 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6467 displayed, no centering will be performed." 
6468   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6469   ;; Recenter only when requested. Suggested by popovich@park.cs.columbia.edu.
6470   (sit-for 0)
6471   (and gnus-auto-center-summary
6472        (get-buffer-window gnus-article-buffer)
6473        (let* ((height (window-height))
6474               (w-end (window-end))
6475               (top (cond ((< height 4) 0)
6476                          ((< height 6) 1)
6477                          (t 2))))
6478          (and (= w-end (point-max))
6479              ;; adjust for blank lines at the bottom of the window
6480              (setq top (+ (count-lines (window-start) (point))
6481                           (- height (count-lines (window-start) w-end) 2))))
6482          (recenter top))))
6483
6484 (defun gnus-summary-recenter ()
6485   "Center point in the summary window.
6486 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6487 displayed, no centering will be performed." 
6488   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6489   ;; Recenter only when requested. Suggested by popovich@park.cs.columbia.edu.
6490   (let ((top (cond ((< (window-height) 4) 0)
6491                    ((< (window-height) 6) 1)
6492                    (t 2))))
6493     (and 
6494      ;; The user has to want it,
6495      gnus-auto-center-summary 
6496      ;; the article buffer must be displayed,
6497      (get-buffer-window gnus-article-buffer)
6498      ;; there must be lines left to scroll forward,
6499      (zerop (save-excursion (forward-line (- (window-height) 1 top))))
6500      ;; so we recenter.
6501      (set-window-start 
6502       (get-buffer-window (current-buffer)) 
6503       (save-excursion (forward-line (- top)) (point))))))
6504
6505 ;; Function written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
6506 (defun gnus-short-group-name (newsgroup)
6507   "Convert a long group name to an initialized form.
6508 The last part of the name is left intact: \"rec.arts.anime\" becomes
6509 \"r.a.anime\"."
6510   (let ((name ""))
6511     (while newsgroup
6512       (if (string-match "\\." newsgroup)
6513           (progn
6514             (setq name (concat name (substring newsgroup 0 1))
6515                   newsgroup (substring newsgroup (match-end 0))
6516                   name (concat name ".")))
6517           (setq name (concat name newsgroup)
6518                 newsgroup nil)))
6519     name))
6520
6521
6522 (defun gnus-summary-jump-to-group (newsgroup)
6523   "Move point to NEWSGROUP in group mode buffer."
6524   ;; Keep update point of group mode buffer if visible.
6525   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6526       (save-window-excursion
6527         ;; Take care of tree window mode.
6528         (if (get-buffer-window gnus-group-buffer)
6529             (pop-to-buffer gnus-group-buffer))
6530         (gnus-group-jump-to-group newsgroup))
6531     (save-excursion
6532       ;; Take care of tree window mode.
6533       (if (get-buffer-window gnus-group-buffer)
6534           (pop-to-buffer gnus-group-buffer)
6535         (set-buffer gnus-group-buffer))
6536       (gnus-group-jump-to-group newsgroup))))
6537
6538 ;; This function returns a list of article numbers based on the
6539 ;; difference between the ranges of read articles in this group and
6540 ;; the range of active articles.
6541 (defun gnus-list-of-unread-articles (group)
6542   (let* ((read (nth 2 (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
6543          (active (gnus-gethash group gnus-active-hashtb))
6544          (last (cdr active))
6545          unread first nlast unread)
6546     ;; If none are read, then all are unread. 
6547     (if (not read)
6548         (setq first (car active))
6549       ;; If the range of read articles is a single range, then the
6550       ;; first unread article is the article after the last read
6551       ;; article. Sounds logical, doesn't it?
6552       (if (not (listp (cdr read)))
6553           (setq first (1+ (cdr read)))
6554         ;; `read' is a list of ranges.
6555         (if (/= (setq nlast (or (and (numberp (car read)) (car read)) 
6556                                 (car (car read)))) 1)
6557             (setq first 1))
6558         (while read
6559           (if first 
6560               (while (< first nlast)
6561                 (setq unread (cons first unread))
6562                 (setq first (1+ first))))
6563           (setq first (1+ (if (atom (car read)) (car read) (cdr (car read)))))
6564           (setq nlast (if (atom (car (cdr read))) 
6565                           (car (cdr read))
6566                         (car (car (cdr read)))))
6567           (setq read (cdr read)))))
6568     ;; And add the last unread articles.
6569     (while (<= first last)
6570       (setq unread (cons first unread))
6571       (setq first (1+ first)))
6572     ;; Return the list of unread articles.
6573     (nreverse unread)))
6574
6575
6576 ;; Various summary commands
6577
6578 (defun gnus-summary-universal-argument ()
6579   "Perform any operation on all articles marked with the process mark."
6580   (interactive)
6581   (gnus-set-global-variables)
6582   (let ((articles (reverse gnus-newsgroup-processable))
6583         key func)
6584     (or articles (error "No articles marked"))
6585     (or (setq func (key-binding (read-key-sequence "C-c C-u")))
6586         (error "Undefined key"))
6587     (while articles
6588       (gnus-summary-goto-subject (car articles))
6589       (command-execute func)
6590       (gnus-summary-remove-process-mark (car articles))
6591       (setq articles (cdr articles)))))
6592
6593 (defun gnus-summary-toggle-truncation (arg)
6594   "Toggle truncation of summary lines.
6595 With arg, turn line truncation on iff arg is positive."
6596   (interactive "P")
6597   (setq truncate-lines
6598         (if (null arg) (not truncate-lines)
6599           (> (prefix-numeric-value arg) 0)))
6600   (redraw-display))
6601
6602 (defun gnus-summary-reselect-current-group (all)
6603   "Once exit and then reselect the current newsgroup.
6604 The prefix argument ALL means to select all articles."
6605   (interactive "P")
6606   (gnus-set-global-variables)
6607   (let ((current-subject (gnus-summary-article-number)))
6608     (gnus-summary-exit t)
6609     ;; We have to adjust the point of group mode buffer because the
6610     ;; current point was moved to the next unread newsgroup by
6611     ;; exiting.
6612     (gnus-summary-jump-to-group gnus-newsgroup-name)
6613     (gnus-group-read-group all t)
6614     (gnus-summary-goto-subject current-subject)))
6615
6616 (defun gnus-summary-rescan-group (all)
6617   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6618   (interactive "P")
6619   (gnus-set-global-variables)
6620   ;; Fix by Ilja Weis <kult@uni-paderborn.de>.
6621   (let ((group gnus-newsgroup-name))
6622     (gnus-summary-exit t)
6623     (gnus-summary-jump-to-group group)
6624     (save-excursion
6625       (set-buffer gnus-group-buffer)
6626       (gnus-group-get-new-news-this-group 1))
6627     (gnus-summary-jump-to-group group)
6628     (gnus-group-read-group all)))
6629
6630 (defun gnus-summary-exit (&optional temporary)
6631   "Exit reading current newsgroup, and then return to group selection mode.
6632 gnus-exit-group-hook is called with no arguments if that value is non-nil."
6633   (interactive)
6634   (gnus-set-global-variables)
6635   (gnus-kill-save-kill-buffer)
6636   (let* ((group gnus-newsgroup-name)
6637          (quit-buffer (cdr (assoc 'quit-buffer (gnus-find-method-for-group
6638                                                 gnus-newsgroup-name))))
6639          (mode major-mode)
6640          (method (car (gnus-find-method-for-group group)))
6641          (buf (current-buffer)))
6642     (if gnus-newsgroup-kill-headers
6643         (setq gnus-newsgroup-killed
6644               (gnus-compress-sequence
6645                (nconc
6646                 (gnus-set-sorted-intersection
6647                  (gnus-uncompress-range gnus-newsgroup-killed)
6648                  (setq gnus-newsgroup-unselected
6649                        (sort gnus-newsgroup-unselected '<)))
6650                 (setq gnus-newsgroup-unreads
6651                       (sort gnus-newsgroup-unreads '<))) t)))
6652     (or (listp (cdr gnus-newsgroup-killed))
6653         (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6654     (let ((updated nil)
6655           (headers gnus-newsgroup-headers))
6656       (gnus-close-group group)
6657       (run-hooks 'gnus-exit-group-hook)
6658       (gnus-update-read-articles 
6659        group gnus-newsgroup-unreads gnus-newsgroup-unselected 
6660        gnus-newsgroup-marked
6661        t gnus-newsgroup-replied gnus-newsgroup-expirable
6662        gnus-newsgroup-killed gnus-newsgroup-dormant
6663        gnus-newsgroup-bookmarks 
6664        (and gnus-save-score gnus-newsgroup-scored))
6665       (and gnus-use-cross-reference
6666            (gnus-mark-xrefs-as-read 
6667             group headers gnus-newsgroup-unreads gnus-newsgroup-expirable))
6668       ;; Do adaptive scoring, and possibly save score files.
6669       (and gnus-use-adaptive-scoring
6670            (gnus-score-adaptive))
6671       (and (fboundp 'gnus-score-save)
6672            (funcall 'gnus-score-save))
6673       ;; Do not switch windows but change the buffer to work.
6674       (set-buffer gnus-group-buffer)
6675       (or (eq 'nndigest method)
6676           (gnus-group-update-group group)))
6677     ;; Make sure where I was, and go to next newsgroup.
6678     (if (eq method 'nndigest)
6679         ()
6680       (gnus-group-jump-to-group group)
6681       (gnus-group-next-unread-group 1))
6682     (if temporary
6683         ;; If exiting temporary, caller should adjust group mode
6684         ;; buffer point by itself.
6685         nil                             ;Nothing to do.
6686       ;; We set all buffer-local variables to nil. It is unclear why
6687       ;; this is needed, but if we don't, buffer-local variables are
6688       ;; not garbage-collected, it seems. This would the lead to en
6689       ;; ever-growing Emacs.
6690       (set-buffer buf)
6691       (gnus-summary-clear-local-variables)
6692       ;; We clear the global counterparts of the buffer-local
6693       ;; variables as well, just to be on the safe side.
6694       (set-buffer gnus-group-buffer)
6695       (gnus-summary-clear-local-variables)
6696       (gnus-configure-windows 'article)
6697       (gnus-configure-windows 'newsgroups)
6698       ;; Return to group mode buffer. 
6699       (if (eq mode 'gnus-summary-mode)
6700           (gnus-kill-buffer buf))
6701       (if (get-buffer gnus-article-buffer)
6702           (bury-buffer gnus-article-buffer))
6703       (setq gnus-current-select-method gnus-select-method)
6704       (pop-to-buffer gnus-group-buffer)
6705       (if (eq method 'nndigest)
6706           ()
6707         (gnus-group-jump-to-group group)
6708         (gnus-group-next-group 1))
6709       (if (gnus-buffer-exists-p quit-buffer)
6710           (progn
6711             (switch-to-buffer quit-buffer)
6712             (gnus-set-global-variables)
6713             (gnus-configure-windows 'summary))))))
6714
6715 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6716 (defun gnus-summary-exit-no-update (&optional no-questions)
6717   "Quit reading current newsgroup without updating read article info."
6718   (interactive)
6719   (let* ((group gnus-newsgroup-name)
6720          (quit-buffer (cdr (assoc 'quit-buffer 
6721                                   (gnus-find-method-for-group group)))))
6722     (if (or no-questions
6723             gnus-expert-user
6724             (gnus-y-or-n-p "Do you really wanna quit reading this group? "))
6725         (progn
6726           (gnus-close-group group)
6727           (gnus-summary-clear-local-variables)
6728           (set-buffer gnus-group-buffer)
6729           (gnus-summary-clear-local-variables)
6730           ;; Return to group selection mode.
6731           (gnus-configure-windows 'newsgroups)
6732           (if (get-buffer gnus-summary-buffer)
6733               (kill-buffer gnus-summary-buffer))
6734           (if (get-buffer gnus-article-buffer)
6735               (bury-buffer gnus-article-buffer))
6736           (pop-to-buffer gnus-group-buffer)
6737           (if (equal (gnus-group-group-name) group)
6738               (gnus-group-next-group 1))
6739           (if (gnus-buffer-exists-p quit-buffer)
6740               (progn
6741                 (switch-to-buffer quit-buffer)
6742                 (gnus-configure-windows 'summary)))))))
6743
6744 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6745 (defun gnus-summary-fetch-faq (group)
6746   "Fetch the FAQ for the current group."
6747   (interactive (list gnus-newsgroup-name))
6748   (gnus-configure-windows 'article)
6749   (pop-to-buffer gnus-article-buffer)
6750   (find-file (concat gnus-group-faq-directory group)))
6751
6752 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6753 (defun gnus-summary-describe-group (force)
6754   "Describe the current newsgroup."
6755   (interactive "P")
6756   (gnus-group-describe-group force gnus-newsgroup-name))
6757
6758 (defun gnus-summary-describe-briefly ()
6759   "Describe summary mode commands briefly."
6760   (interactive)
6761   (message
6762     (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")))
6763
6764 ;; Walking around group mode buffer from summary mode.
6765
6766 (defun gnus-summary-next-group (&optional no-article group backward)
6767   "Exit current newsgroup and then select next unread newsgroup.
6768 If prefix argument NO-ARTICLE is non-nil, no article is selected initially.
6769 If BACKWARD, go to previous group instead."
6770   (interactive "P")
6771   (gnus-set-global-variables)
6772   (let ((ingroup gnus-newsgroup-name)
6773         (sumbuf (current-buffer))
6774         num)
6775     (gnus-summary-exit t)               ;Update all information.
6776     (if (and group
6777              (or (and (numberp (setq num (car (gnus-gethash
6778                                                group gnus-newsrc-hashtb))))
6779                       (< num 1))
6780                  (null num)))
6781         (progn
6782           (gnus-group-jump-to-group group)
6783           (setq group nil))
6784       (gnus-group-jump-to-group ingroup))
6785     (gnus-summary-search-group backward)
6786     (let ((group (or group (gnus-summary-search-group backward)))
6787           (buf gnus-summary-buffer))
6788       (if (null group)
6789           (gnus-summary-exit-no-update t)
6790         (message "Selecting %s..." group)
6791         ;; We are now in group mode buffer.
6792         ;; Make sure group mode buffer point is on GROUP.
6793         (gnus-group-jump-to-group group)
6794         (if (not (eq gnus-auto-select-next 'quietly))
6795             (progn
6796               (gnus-summary-read-group group nil no-article buf)
6797               (and (string= gnus-newsgroup-name ingroup)
6798                    (bufferp sumbuf) (buffer-name sumbuf)
6799                    (progn
6800                      (set-buffer (setq gnus-summary-buffer sumbuf))
6801                      (gnus-summary-exit-no-update t))))
6802           (gnus-summary-read-group group nil no-article buf)
6803           (while (and (string= gnus-newsgroup-name ingroup)
6804                       (bufferp sumbuf) (buffer-name sumbuf))
6805             (set-buffer gnus-group-buffer)
6806             (gnus-group-next-unread-group 1)
6807             (recenter)
6808             (gnus-summary-read-group 
6809              (gnus-group-group-name) nil no-article buf)))))))
6810
6811
6812 (defun gnus-summary-prev-group (no-article)
6813   "Exit current newsgroup and then select previous unread newsgroup.
6814 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6815   (interactive "P")
6816   (gnus-summary-next-group no-article nil t))
6817
6818 ;; Walking around summary lines.
6819
6820 (defun gnus-summary-first-subject (unread)
6821   "Go to the first unread subject.
6822 If UNREAD is non-nil, go to the first unread article.
6823 Returns nil if there are no unread articles."
6824   (interactive "P")
6825   (let ((begin (point)))
6826     (if unread
6827         (if (not (gnus-goto-char 
6828                   (text-property-any (point-min) (point-max)
6829                                      'gnus-mark gnus-unread-mark)))
6830             (progn
6831               ;; If there is no unread articles, stay where you are.
6832               (goto-char begin)
6833               (message "No more unread articles")
6834               nil)
6835           t)
6836       (goto-char (point-min)))))
6837
6838 (defun gnus-summary-next-subject (n &optional unread dont-display)
6839   "Go to next N'th summary line.
6840 If N is negative, go to the previous N'th subject line.
6841 If UNREAD is non-nil, only unread articles are selected.
6842 The difference between N and the actual number of steps taken is
6843 returned."
6844   (interactive "p")
6845   (let ((backward (< n 0))
6846         (n (abs n)))
6847     (while (and (> n 0)
6848                 (gnus-summary-search-forward unread nil backward))
6849       (setq n (1- n)))
6850     (if (/= 0 n) (message "No more%s articles" (if unread " unread" "")))
6851     (or dont-display
6852         (progn
6853           (gnus-summary-recenter)
6854           (gnus-summary-position-cursor)))
6855   n))
6856
6857 (defun gnus-summary-next-unread-subject (n)
6858   "Go to next N'th unread summary line."
6859   (interactive "p")
6860   (gnus-summary-next-subject n t))
6861
6862 (defun gnus-summary-prev-subject (n &optional unread)
6863   "Go to previous N'th summary line.
6864 If optional argument UNREAD is non-nil, only unread article is selected."
6865   (interactive "p")
6866   (gnus-summary-next-subject (- n) unread))
6867
6868 (defun gnus-summary-prev-unread-subject (n)
6869   "Go to previous N'th unread summary line."
6870   (interactive "p")
6871   (gnus-summary-next-subject (- n) t))
6872
6873 (defun gnus-summary-goto-subject (article)
6874   "Go the subject line of ARTICLE."
6875   (interactive
6876    (list
6877     (string-to-int
6878      (completing-read "Article number: "
6879                       (mapcar
6880                        (lambda (headers)
6881                          (list
6882                           (int-to-string (header-number headers))))
6883                        gnus-newsgroup-headers)
6884                       nil 'require-match))))
6885   (or article (error "No article number"))
6886   (if (or (eq article (gnus-summary-article-number t))
6887           (gnus-goto-char
6888            (text-property-any
6889             (point-min) (point-max) 'gnus-number article)))
6890       article))
6891
6892 ;; Walking around summary lines with displaying articles.
6893
6894 (defun gnus-summary-expand-window ()
6895   "Expand summary window to show headers full window."
6896   (interactive)
6897   (gnus-set-global-variables)
6898   (gnus-configure-windows 'summary)
6899   (pop-to-buffer gnus-summary-buffer))
6900
6901 (defun gnus-summary-display-article (article &optional all-header)
6902   "Display ARTICLE in article buffer."
6903   (gnus-set-global-variables)
6904   (if (null article)
6905       nil
6906     (gnus-article-prepare article all-header)
6907     (if (eq (gnus-summary-article-mark) ?Z)
6908         (progn
6909           (forward-line 1)
6910           (gnus-summary-position-cursor)))
6911     (run-hooks 'gnus-select-article-hook)
6912     (gnus-summary-recenter)
6913 ;    (set-window-point (get-buffer-window (current-buffer)) (point-max))
6914 ;    (sit-for 0)
6915     (gnus-summary-goto-subject article)
6916     ;; Successfully display article.
6917     (gnus-summary-update-line)
6918     t))
6919
6920 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6921   "Select the current article.
6922 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6923 non-nil, the article will be re-fetched even if it already present in
6924 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6925 be displayed."
6926   (and (not pseudo) (gnus-summary-pseudo-article)
6927        (error "This is a pseudo-article."))
6928   (let ((article (or article (gnus-summary-article-number)))
6929         (all-headers (not (not all-headers)))) ;Must be T or NIL.
6930     (if (or (null gnus-current-article)
6931             (null gnus-article-current)
6932             (/= article (cdr gnus-article-current))
6933             (not (equal (car gnus-article-current) gnus-newsgroup-name))
6934             force)
6935         ;; The requested article is different from the current article.
6936         (progn
6937           (gnus-summary-display-article article all-headers)
6938           article)
6939       (if all-headers (gnus-article-show-all-headers))
6940       (gnus-configure-windows 'article)
6941       (pop-to-buffer gnus-summary-buffer)
6942       nil)))
6943
6944 (defun gnus-summary-set-current-mark (&optional current-mark)
6945   "Obsolete function."
6946   nil)
6947
6948 (defun gnus-summary-next-article (unread &optional subject backward)
6949   "Select the next article.
6950 If UNREAD, only unread articles are selected.
6951 If SUBJECT, only articles with SUBJECT are selected.
6952 If BACKWARD, the previous article is selected instead of the next."
6953   (interactive "P")
6954   (let ((opoint (point))
6955         (method (car (gnus-find-method-for-group gnus-newsgroup-name)))
6956         header)
6957     (cond
6958      ;; Is there such an article?
6959      ((gnus-summary-display-article 
6960        (gnus-summary-search-forward unread subject backward))
6961       (gnus-summary-position-cursor))
6962      ;; If not, we try the first unread, if that is wanted.
6963      ((and subject
6964            gnus-auto-select-same
6965            (gnus-summary-first-unread-article))
6966       (message "Wrapped"))
6967      ;; Try to get next/previous article not displayed in this group.
6968      ((and gnus-auto-extend-newsgroup
6969            (not unread) (not subject)
6970            (setq header (gnus-more-header-forward backward)))
6971       (gnus-extend-newsgroup header backward)
6972       (let ((buffer-read-only nil))
6973         (goto-char (if backward (point-min) (point-max)))
6974         (gnus-summary-prepare-threads (list header) 0))
6975       (gnus-summary-goto-article (if backward gnus-newsgroup-begin
6976                                    gnus-newsgroup-end)))
6977      ;; Go to next/previous group.
6978      (t
6979       (or (eq method 'nndigest)
6980           (gnus-summary-jump-to-group gnus-newsgroup-name))
6981       (let ((cmd (aref (this-command-keys) 0))
6982             (group 
6983              (if (eq gnus-keep-same-level 'best) 
6984                  (gnus-summary-best-group gnus-newsgroup-name)
6985                (gnus-summary-search-group backward gnus-keep-same-level))))
6986         ;; Keep just the event type of CMD.
6987         (and (listp cmd) (setq cmd (car cmd)))
6988         ;; Select next unread newsgroup automagically.
6989         (cond 
6990          ((not gnus-auto-select-next)
6991           (message "No more%s articles" (if unread " unread" "")))
6992          ((eq gnus-auto-select-next 'quietly)
6993           ;; Select quietly.
6994           (if (eq method 'nndigest)
6995               (gnus-summary-exit)
6996             (message "No more%s articles (%s)..."
6997                      (if unread " unread" "") 
6998                      (if group (concat "selecting " group)
6999                        "exiting"))
7000             (gnus-summary-next-group nil group backward)))
7001          (t
7002           (let ((keystrokes '(?\C-n ?\C-p))
7003                 key)
7004             (while (or (null key) (memq key keystrokes))
7005               (message 
7006                "No more%s articles%s" (if unread " unread" "")
7007                (if (and group (not (eq method 'nndigest)))
7008                    (format " (Type %s for %s [%s])"
7009                            (single-key-description cmd) group
7010                            (car (gnus-gethash group gnus-newsrc-hashtb)))
7011                  (format " (Type %s to exit %s)"
7012                          (single-key-description cmd)
7013                          gnus-newsgroup-name)))
7014               ;; Confirm auto selection.
7015               (let* ((event (read-event)))
7016                 (setq key (if (listp event) (car event) event))
7017                 (if (member key keystrokes)
7018                     (let ((obuf (current-buffer)))
7019                       (switch-to-buffer gnus-group-buffer)
7020                       (gnus-group-jump-to-group group)
7021                       (execute-kbd-macro (char-to-string key))
7022                       (setq group (gnus-group-group-name))
7023                       (switch-to-buffer obuf)))))
7024             (if (eq key cmd)
7025                 (if (or (not group) (eq method 'nndigest))
7026                     (gnus-summary-exit)
7027                   (gnus-summary-next-group nil group backward))
7028               (setq unread-command-events (list key)))))))))))
7029
7030 (defun gnus-summary-next-unread-article ()
7031   "Select unread article after current one."
7032   (interactive)
7033   (gnus-summary-next-article t (and gnus-auto-select-same
7034                                     (gnus-summary-subject-string))))
7035
7036 (defun gnus-summary-prev-article (unread &optional subject)
7037   "Select the article after the current one.
7038 If UNREAD is non-nil, only unread articles are selected."
7039   (interactive "P")
7040   (gnus-summary-next-article unread subject t))
7041
7042 (defun gnus-summary-prev-unread-article ()
7043   "Select unred article before current one."
7044   (interactive)
7045   (gnus-summary-prev-article t (and gnus-auto-select-same
7046                                     (gnus-summary-subject-string))))
7047
7048 (defun gnus-summary-next-page (lines &optional circular)
7049   "Show next page of selected article.
7050 If end of article, select next article.
7051 Argument LINES specifies lines to be scrolled up.
7052 If CIRCULAR is non-nil, go to the start of the article instead of 
7053 instead of selecting the next article when reaching the end of the
7054 current article." 
7055   (interactive "P")
7056   (setq gnus-summary-buffer (current-buffer))
7057   (gnus-set-global-variables)
7058   (let ((article (gnus-summary-article-number))
7059         (endp nil))
7060     (if (or (null gnus-current-article)
7061             (null gnus-article-current)
7062             (/= article (cdr gnus-article-current))
7063             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7064         ;; Selected subject is different from current article's.
7065         (gnus-summary-display-article article)
7066       (gnus-configure-windows 'article)
7067       (gnus-eval-in-buffer-window
7068        gnus-article-buffer
7069        (setq endp (gnus-article-next-page lines)))
7070       (if endp
7071           (cond (circular
7072                  (gnus-summary-beginning-of-article))
7073                 (lines
7074                  (message "End of message"))
7075                 ((null lines)
7076                  (gnus-summary-next-unread-article))))
7077       (and gnus-summary-buffer
7078            (buffer-name gnus-summary-buffer)
7079            (progn
7080              (pop-to-buffer gnus-summary-buffer)
7081              (gnus-summary-position-cursor))))))
7082
7083 (defun gnus-summary-prev-page (lines)
7084   "Show previous page of selected article.
7085 Argument LINES specifies lines to be scrolled down."
7086   (interactive "P")
7087   (let ((article (gnus-summary-article-number)))
7088     (if (or (null gnus-current-article)
7089             (null gnus-article-current)
7090             (/= article (cdr gnus-article-current))
7091             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7092         ;; Selected subject is different from current article's.
7093         (gnus-summary-display-article article)
7094       (gnus-configure-windows 'article)
7095       (pop-to-buffer gnus-summary-buffer)
7096       (gnus-eval-in-buffer-window gnus-article-buffer
7097         (gnus-article-prev-page lines))))
7098   (gnus-summary-position-cursor))
7099
7100 (defun gnus-summary-scroll-up (lines)
7101   "Scroll up (or down) one line current article.
7102 Argument LINES specifies lines to be scrolled up (or down if negative)."
7103   (interactive "p")
7104   (or (gnus-summary-select-article nil nil 'pseudo)
7105       (gnus-eval-in-buffer-window 
7106        gnus-article-buffer
7107        (cond ((> lines 0)
7108               (if (gnus-article-next-page lines)
7109                   (message "End of message")))
7110              ((< lines 0)
7111               (gnus-article-prev-page (- lines))))))
7112   (gnus-summary-position-cursor))
7113
7114 (defun gnus-summary-next-same-subject ()
7115   "Select next article which has the same subject as current one."
7116   (interactive)
7117   (gnus-summary-next-article nil (gnus-summary-subject-string)))
7118
7119 (defun gnus-summary-prev-same-subject ()
7120   "Select previous article which has the same subject as current one."
7121   (interactive)
7122   (gnus-summary-prev-article nil (gnus-summary-subject-string)))
7123
7124 (defun gnus-summary-next-unread-same-subject ()
7125   "Select next unread article which has the same subject as current one."
7126   (interactive)
7127   (gnus-summary-next-article t (gnus-summary-subject-string)))
7128
7129 (defun gnus-summary-prev-unread-same-subject ()
7130   "Select previous unread article which has the same subject as current one."
7131   (interactive)
7132   (gnus-summary-prev-article t (gnus-summary-subject-string)))
7133
7134 (defun gnus-summary-first-unread-article ()
7135   "Select the first unread article. 
7136 Return nil if there are no unread articles."
7137   (interactive)
7138   (prog1
7139       (if (gnus-summary-first-subject t)
7140           (gnus-summary-display-article (gnus-summary-article-number)))
7141     (gnus-summary-position-cursor)))
7142
7143 (defun gnus-summary-best-unread-article ()
7144   "Select the unread article with the highest score."
7145   (interactive)
7146   (gnus-set-global-variables)
7147   (let ((scored gnus-newsgroup-scored)
7148         (best -1000000)
7149         article art)
7150     (while scored
7151       (or (> best (cdr (car scored)))
7152           (and (memq (setq art (car (car scored))) gnus-newsgroup-unreads)
7153                (not (memq art gnus-newsgroup-marked))
7154                (not (memq art gnus-newsgroup-dormant))
7155                (if (= best (cdr (car scored)))
7156                    (setq article (min art article))
7157                  (setq article art)
7158                  (setq best (cdr (car scored))))))
7159       (setq scored (cdr scored)))
7160     (if article 
7161         (gnus-summary-goto-article article)
7162       (gnus-summary-first-unread-article))
7163     (gnus-summary-position-cursor)))
7164
7165 (defun gnus-summary-goto-article (article &optional all-headers)
7166   "Fetch ARTICLE and display it if it exists.
7167 If ALL-HEADERS is non-nil, no header lines are hidden."
7168   (interactive
7169    (list
7170     (string-to-int
7171      (completing-read 
7172       "Article number: "
7173       (mapcar (lambda (headers) (list (int-to-string (header-number headers))))
7174               gnus-newsgroup-headers) 
7175       nil 'require-match))))
7176   (if (gnus-summary-goto-subject article)
7177       (gnus-summary-display-article article all-headers))
7178   (gnus-summary-position-cursor))
7179
7180 (defun gnus-summary-goto-last-article ()
7181   "Go to the last article."
7182   (interactive)
7183   (if gnus-last-article
7184       (gnus-summary-goto-article gnus-last-article))
7185   (gnus-summary-position-cursor))
7186
7187 (defun gnus-summary-pop-article (number)
7188   "Pop one article off the history and go to the previous.
7189 NUMBER articles will be popped off."
7190   (interactive "p")
7191   (let (to)
7192     (setq gnus-newsgroup-history
7193           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7194     (if to
7195         (gnus-summary-goto-article (car to))
7196       (error "Article history empty")))
7197   (gnus-summary-position-cursor))
7198
7199 ;; Summary article oriented commands
7200
7201 (defun gnus-summary-refer-parent-article (n)
7202   "Refer parent article N times.
7203 The difference between N and the number of articles fetched is returned."
7204   (interactive "p")
7205   (gnus-set-global-variables)
7206   (while 
7207       (and 
7208        (> n 0)
7209        (let ((ref (header-references (gnus-get-header-by-number
7210                                       (gnus-summary-article-number)))))
7211          (if (and ref (not (equal ref ""))
7212                   (string-match "<[^<>]*>[ \t]*$" ref))
7213              (gnus-summary-refer-article 
7214               (substring ref (match-beginning 0) (match-end 0))))))
7215     (setq n (1- n)))
7216   (or (zerop n) (message "No references in article or expired article."))
7217   (gnus-summary-position-cursor)
7218   n)
7219     
7220 (defun gnus-summary-refer-article (message-id)
7221   "Refer article specified by MESSAGE-ID.
7222 NOTE: This command only works with newsgroup that use NNTP."
7223   (interactive "sMessage-ID: ")
7224   (if (or (not (stringp message-id))
7225           (zerop (length message-id)))
7226       ()
7227     ;; Construct the correct Message-ID if necessary.
7228     ;; Suggested by tale@pawl.rpi.edu.
7229     (or (string-match "^<" message-id)
7230         (setq message-id (concat "<" message-id)))
7231     (or (string-match ">$" message-id)
7232         (setq message-id (concat message-id ">")))
7233     (let ((header (car (gnus-gethash (downcase message-id)
7234                                      gnus-newsgroup-dependencies))))
7235       (if header
7236           (gnus-summary-goto-article (header-number header))
7237         (let ((gnus-override-method gnus-refer-article-method))
7238           (and gnus-refer-article-method
7239                (or (gnus-server-opened gnus-refer-article-method)
7240                    (gnus-open-server gnus-refer-article-method)))
7241           (if (gnus-article-prepare 
7242                message-id nil (gnus-read-header message-id))
7243               (progn
7244                 (gnus-summary-insert-line 
7245                  nil gnus-current-headers 0 nil gnus-read-mark nil nil 
7246                  (header-subject gnus-current-headers))
7247                 (forward-line -1)
7248                 (gnus-summary-position-cursor)
7249                 (gnus-summary-update-line)
7250                 message-id)
7251             (message "No such references")
7252             nil))))))
7253
7254 (defun gnus-summary-enter-digest-group ()
7255   "Enter a digest group based on the current article."
7256   (interactive)
7257   (gnus-summary-select-article)
7258   (let ((name (format "%s/%d" 
7259                       (gnus-group-prefixed-name 
7260                        gnus-newsgroup-name (list 'nndigest "")) 
7261                       gnus-current-article))
7262         (buf (current-buffer)))
7263     (set-buffer gnus-group-buffer)
7264     (gnus-sethash 
7265      name 
7266      (list t nil (list name gnus-level-default-subscribed nil nil 
7267                        (list 'nndigest gnus-article-buffer
7268                              (cons 'quit-buffer buf))))
7269      gnus-newsrc-hashtb)
7270     (gnus-group-read-group t nil name)))
7271   
7272 (defun gnus-summary-isearch-article ()
7273   "Do incremental search forward on current article."
7274   (interactive)
7275   (gnus-summary-select-article)
7276   (gnus-eval-in-buffer-window gnus-article-buffer
7277                               (isearch-forward)))
7278
7279 (defun gnus-summary-search-article-forward (regexp)
7280   "Search for an article containing REGEXP forward.
7281 gnus-select-article-hook is not called during the search."
7282   (interactive
7283    (list (read-string
7284           (concat "Search forward (regexp): "
7285                   (if gnus-last-search-regexp
7286                       (concat "(default " gnus-last-search-regexp ") "))))))
7287   (if (string-equal regexp "")
7288       (setq regexp (or gnus-last-search-regexp ""))
7289     (setq gnus-last-search-regexp regexp))
7290   (if (gnus-summary-search-article regexp nil)
7291       (gnus-eval-in-buffer-window 
7292        gnus-article-buffer
7293        (recenter 0))
7294     (error "Search failed: \"%s\"" regexp)))
7295
7296 (defun gnus-summary-search-article-backward (regexp)
7297   "Search for an article containing REGEXP backward.
7298 gnus-select-article-hook is not called during the search."
7299   (interactive
7300    (list (read-string
7301           (concat "Search backward (regexp): "
7302                   (if gnus-last-search-regexp
7303                       (concat "(default " gnus-last-search-regexp ") "))))))
7304   (if (string-equal regexp "")
7305       (setq regexp (or gnus-last-search-regexp ""))
7306     (setq gnus-last-search-regexp regexp))
7307   (if (gnus-summary-search-article regexp t)
7308       (gnus-eval-in-buffer-window
7309        gnus-article-buffer
7310        (recenter 0))
7311     (error "Search failed: \"%s\"" regexp)))
7312
7313 (defun gnus-summary-search-article (regexp &optional backward)
7314   "Search for an article containing REGEXP.
7315 Optional argument BACKWARD means do search for backward.
7316 gnus-select-article-hook is not called during the search."
7317   (let ((gnus-select-article-hook nil)  ;Disable hook.
7318         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7319         (re-search
7320          (if backward
7321              (function re-search-backward) (function re-search-forward)))
7322         (found nil)
7323         (last nil))
7324     ;; Hidden thread subtrees must be searched for ,too.
7325     (gnus-summary-show-all-threads)
7326     (if (eobp) (forward-line -1))
7327     ;; First of all, search current article.
7328     ;; We don't want to read article again from NNTP server nor reset
7329     ;; current point.
7330     (gnus-summary-select-article)
7331     (message "Searching article: %d..." gnus-current-article)
7332     (setq last gnus-current-article)
7333     (gnus-eval-in-buffer-window gnus-article-buffer
7334       (save-restriction
7335         (widen)
7336         ;; Begin search from current point.
7337         (setq found (funcall re-search regexp nil t))))
7338     ;; Then search next articles.
7339     (while (and (not found)
7340                 (gnus-summary-display-article 
7341                  (gnus-summary-search-subject backward nil nil)))
7342       (message "Searching article: %d..." gnus-current-article)
7343       (gnus-eval-in-buffer-window gnus-article-buffer
7344         (save-restriction
7345           (widen)
7346           (goto-char (if backward (point-max) (point-min)))
7347           (setq found (funcall re-search regexp nil t)))))
7348     (message "")
7349     ;; Adjust article pointer.
7350     (or (eq last gnus-current-article)
7351         (setq gnus-last-article last))
7352     ;; Return T if found such article.
7353     found))
7354
7355 (defun gnus-summary-execute-command (header regexp command &optional backward)
7356   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7357 If HEADER is an empty string (or nil), the match is done on the entire
7358 article. If BACKWARD (the prefix) is non-nil, search backward instead."
7359   (interactive
7360    (list (let ((completion-ignore-case t))
7361            (completing-read 
7362             "Header name: "
7363             (mapcar (string) (list string)
7364                     '("Number" "Subject" "From" "Lines" "Date"
7365                       "Message-ID" "Xref" "References")
7366                     nil 'require-match)))
7367          (read-string "Regexp: ")
7368          (read-key-sequence "Command: ")
7369          current-prefix-arg))
7370   ;; Hidden thread subtrees must be searched as well.
7371   (gnus-summary-show-all-threads)
7372   ;; We don't want to change current point nor window configuration.
7373   (save-excursion
7374     (save-window-excursion
7375       (message "Executing %s..." (key-description command))
7376       ;; We'd like to execute COMMAND interactively so as to give arguments.
7377       (gnus-execute header regexp
7378                     (` (lambda ()
7379                          (call-interactively '(, (key-binding command)))))
7380                     backward)
7381       (message "Executing %s... done" (key-description command)))))
7382
7383 (defun gnus-summary-beginning-of-article ()
7384   "Scroll the article back to the beginning."
7385   (interactive)
7386   (gnus-summary-select-article)
7387   (gnus-eval-in-buffer-window
7388    gnus-article-buffer
7389    (widen)
7390    (goto-char (point-min))
7391    (and gnus-break-pages (gnus-narrow-to-page))))
7392
7393 (defun gnus-summary-end-of-article ()
7394   "Scroll to the end of the article."
7395   (interactive)
7396   (gnus-summary-select-article)
7397   (gnus-eval-in-buffer-window 
7398    gnus-article-buffer
7399    (widen)
7400    (goto-char (point-max))
7401    (and gnus-break-pages (gnus-narrow-to-page))))
7402
7403 (defun gnus-summary-show-article ()
7404   "Force re-fetching of the current article."
7405   (interactive)
7406   (gnus-summary-select-article gnus-have-all-headers t t))
7407
7408 (defun gnus-summary-toggle-header (arg)
7409   "Show the headers if they are hidden, or hide them if they are shown.
7410 If ARG is a positive number, show the entire header.
7411 If ARG is a negative number, hide the unwanted header lines."
7412   (interactive "P")
7413   (gnus-set-global-variables)
7414   (save-excursion
7415     (set-buffer gnus-article-buffer)
7416     (let ((buffer-read-only nil))
7417       (if (numberp arg) 
7418           (if (> arg 0) (remove-text-properties 1 (point-max) '(invisible t))
7419             (if (< arg 0) (run-hooks 'gnus-article-display-hook)))
7420         (if (text-property-any 1 (point-max) 'invisible t)
7421             (remove-text-properties 1 (point-max) '(invisible t))
7422           (let ((gnus-have-all-headers nil))
7423             (run-hooks 'gnus-article-display-hook))))
7424       (set-window-point (get-buffer-window (current-buffer)) 1))))
7425
7426 (defun gnus-summary-show-all-headers ()
7427   "Make all header lines visible."
7428   (interactive)
7429   (gnus-article-show-all-headers))
7430
7431 (defun gnus-summary-toggle-mime (arg)
7432   "Toggle MIME processing.
7433 If ARG is a positive number, turn MIME processing on."
7434   (interactive "P")
7435   (setq gnus-show-mime
7436         (if (null arg) (not gnus-show-mime)
7437           (> (prefix-numeric-value arg) 0)))
7438   (gnus-summary-select-article t 'force))
7439
7440 (defun gnus-summary-caesar-message (rotnum)
7441   "Caesar rotates all letters of current message by 13/47 places.
7442 With prefix arg, specifies the number of places to rotate each letter forward.
7443 Caesar rotates Japanese letters by 47 places in any case."
7444   (interactive "P")
7445   (gnus-summary-select-article)
7446   (let ((mail-header-separator "")) ; !!! Is this necessary?
7447     (gnus-overload-functions)
7448     (gnus-eval-in-buffer-window 
7449      gnus-article-buffer
7450      (save-restriction
7451        (widen)
7452        ;; We don't want to jump to the beginning of the message.
7453        ;; `save-excursion' does not do its job.
7454        (move-to-window-line 0)
7455        (let ((last (point)))
7456          (news-caesar-buffer-body rotnum)
7457          (goto-char last)
7458          (recenter 0))))))
7459
7460 (defun gnus-summary-stop-page-breaking ()
7461   "Stop page breaking in the current article."
7462   (interactive)
7463   (gnus-summary-select-article)
7464   (gnus-eval-in-buffer-window gnus-article-buffer (widen)))
7465
7466 ;; Suggested by Brian Edmonds <bedmonds@prodigy.bc.ca>.
7467
7468 (defun gnus-summary-move-article (n &optional to-newsgroup select-method)
7469   "Move the current article to a different newsgroup.
7470 If N is a positive number, move the N next articles.
7471 If N is a negative number, move the N previous articles.
7472 If N is nil and any articles have been marked with the process mark,
7473 move those articles instead.
7474 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to. 
7475 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
7476 re-spool using this method.
7477 For this function to work, both the current newsgroup and the
7478 newsgroup that you want to move to have to support the `request-move'
7479 and `request-accept' functions. (Ie. mail newsgroups at present.)"
7480   (interactive "P")
7481   (gnus-set-global-variables)
7482   (or (gnus-check-backend-function 'request-move-article gnus-newsgroup-name)
7483       (error "The current newsgroup does not support article moving"))
7484   (let ((articles (gnus-summary-work-articles n))
7485         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
7486         art-group)
7487     (if (and (not to-newsgroup) (not select-method))
7488         (setq to-newsgroup
7489               (completing-read 
7490                (format "Where do you want to move %s? %s"
7491                        (if (> (length articles) 1)
7492                            (format "these %d articles" (length articles))
7493                          "this article")
7494                        (if gnus-current-move-group
7495                            (format "(%s default) " gnus-current-move-group)
7496                          ""))
7497                gnus-active-hashtb nil nil prefix)))
7498     (if to-newsgroup
7499         (progn
7500           (if (or (string= to-newsgroup "") (string= to-newsgroup prefix))
7501               (setq to-newsgroup (or gnus-current-move-group "")))
7502           (or (gnus-gethash to-newsgroup gnus-active-hashtb)
7503               (gnus-activate-newsgroup to-newsgroup)
7504               (error "No such group: %s" to-newsgroup))
7505           (setq gnus-current-move-group to-newsgroup)))
7506     (or (gnus-check-backend-function 'request-accept-article 
7507                                      (or select-method to-newsgroup))
7508         (error "%s does not support article moving" to-newsgroup))
7509     (message "Moving to %s: %s..." (or select-method to-newsgroup) articles)
7510     (while articles
7511       (if (setq art-group
7512                 (gnus-request-move-article 
7513                  (car articles)                   ; Article to move
7514                  gnus-newsgroup-name              ; From newsgrouo
7515                  (nth 1 (gnus-find-method-for-group 
7516                          gnus-newsgroup-name))    ; Server
7517                  (list 'gnus-request-accept-article 
7518                        (if select-method
7519                            (quote select-method)
7520                          to-newsgroup)
7521                        (not (cdr articles)))     ; Accept form
7522                  (not (cdr articles))))          ; Only save nov last time
7523           (let* ((buffer-read-only nil)
7524                  (entry 
7525                   (or
7526                    (gnus-gethash (car art-group) gnus-newsrc-hashtb)
7527                    (gnus-gethash 
7528                     (gnus-group-prefixed-name 
7529                      (car art-group) 
7530                      (if select-method (list select-method "")
7531                        (gnus-find-method-for-group to-newsgroup)))
7532                     gnus-newsrc-hashtb)))
7533                  (info (nth 2 entry))
7534                  (article (car articles))
7535                  (marked (nth 3 info)))
7536             (gnus-summary-goto-subject article)
7537             (beginning-of-line)
7538             (delete-region (point)
7539                            (progn (forward-line 1) (point)))
7540             (if (not (memq article gnus-newsgroup-unreads))
7541                 (setcar (cdr (cdr info))
7542                         (gnus-add-to-range (nth 2 info) 
7543                                            (list (cdr art-group)))))
7544             ;; Copy any marks over to the new group.
7545             (let ((marks '((tick . gnus-newsgroup-marked)
7546                            (dormant . gnus-newsgroup-dormant)
7547                            (expire . gnus-newsgroup-expirable)
7548                            (bookmark . gnus-newsgroup-bookmarks)
7549                         ;   (score . gnus-newsgroup-scored)
7550                            (reply . gnus-newsgroup-replied)))
7551                   (to-article (cdr art-group)))
7552               (while marks
7553                 (if (memq article (symbol-value (cdr (car marks))))
7554                     (gnus-add-marked-articles 
7555                      (car info) (car (car marks)) (list to-article) info))
7556                 (setq marks (cdr marks))))
7557             (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7558             (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
7559             (setq gnus-newsgroup-dormant
7560                   (delq article gnus-newsgroup-dormant)))
7561         (message "Couldn't move article %s" (car articles)))
7562       (gnus-summary-remove-process-mark (car articles))
7563       (setq articles (cdr articles)))))
7564
7565 (defun gnus-summary-respool-article (n &optional respool-method)
7566   "Respool the current article.
7567 The article will be squeezed through the mail spooling process again,
7568 which means that it will be put in some mail newsgroup or other
7569 depending on `nnmail-split-methods'.
7570 If N is a positive number, respool the N next articles.
7571 If N is a negative number, respool the N previous articles.
7572 If N is nil and any articles have been marked with the process mark,
7573 respool those articles instead.
7574
7575 Respooling can be done both from mail groups and \"real\" newsgroups.
7576 In the former case, the articles in question will be moved from the
7577 current group into whatever groups they are destined to.  In the
7578 latter case, they will be copied into the relevant groups."
7579   (interactive "P")
7580   (gnus-set-global-variables)
7581   (let ((respool-methods (gnus-methods-using 'respool)))
7582     (or respool-method
7583         (setq respool-method
7584               (completing-read
7585                "What method do you want to use when respooling? "
7586                respool-methods nil t)))
7587     (if (assoc (symbol-name
7588                 (car (gnus-find-method-for-group gnus-newsgroup-name)))
7589                respool-methods)
7590         (gnus-summary-move-article n nil (intern respool-method)))
7591     (gnus-summary-copy-article n nil (intern respool-method))))
7592
7593 ;; Suggested by gregj@unidata.com (Gregory J. Grubbs).
7594 (defun gnus-summary-copy-article (n &optional to-newsgroup select-method)
7595   "Move the current article to a different newsgroup.
7596 If N is a positive number, move the N next articles.
7597 If N is a negative number, move the N previous articles.
7598 If N is nil and any articles have been marked with the process mark,
7599 move those articles instead.
7600 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to. 
7601 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
7602 re-spool using this method.
7603 For this function to work, the newsgroup that you want to move to have
7604 to support the `request-move' and `request-accept'
7605 functions. (Ie. mail newsgroups at present.)"
7606   (interactive "P")
7607   (gnus-set-global-variables)
7608   (let ((articles (gnus-summary-work-articles n))
7609         (copy-buf (get-buffer-create "*copy work*"))
7610         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
7611         art-group)
7612     (buffer-disable-undo copy-buf)
7613     (if (and (not to-newsgroup) (not select-method))
7614         (setq to-newsgroup
7615               (completing-read 
7616                (format "Where do you want to copy %s? %s"
7617                        (if (> (length articles) 1)
7618                            (format "these %d articles" (length articles))
7619                          "this article")
7620                        (if gnus-current-move-group
7621                            (format "(%s default) " gnus-current-move-group)
7622                          ""))
7623                gnus-active-hashtb nil nil prefix)))
7624     (if to-newsgroup
7625         (progn
7626           (if (or (string= to-newsgroup "") (string= to-newsgroup prefix))
7627               (setq to-newsgroup (or gnus-current-move-group "")))
7628           (or (gnus-gethash to-newsgroup gnus-active-hashtb)
7629               (gnus-activate-newsgroup to-newsgroup)
7630               (error "No such group: %s" to-newsgroup))
7631           (setq gnus-current-move-group to-newsgroup)))
7632     (or (gnus-check-backend-function 'request-accept-article 
7633                                      (or select-method to-newsgroup))
7634         (error "%s does not support article copying" to-newsgroup))
7635     (message "Copying to %s: %s..." (or select-method to-newsgroup) articles)
7636     (while articles
7637       (if (setq art-group
7638                 (save-excursion
7639                   (set-buffer copy-buf)
7640                   (gnus-request-article-this-buffer
7641                    (car articles) gnus-newsgroup-name)
7642                   (gnus-request-accept-article
7643                    (if select-method (quote select-method) to-newsgroup)
7644                    (not (cdr articles)))))
7645           (let* ((entry 
7646                   (or
7647                    (gnus-gethash (car art-group) gnus-newsrc-hashtb)
7648                    (gnus-gethash 
7649                     (gnus-group-prefixed-name 
7650                      (car art-group) 
7651                      (if select-method (list select-method "")
7652                        (gnus-find-method-for-group to-newsgroup)))
7653                     gnus-newsrc-hashtb)))
7654                  (info (nth 2 entry))
7655                  (article (car articles))
7656                  (marked (nth 3 info)))
7657             (if (not (memq article gnus-newsgroup-unreads))
7658                 (setcar (cdr (cdr info))
7659                         (gnus-add-to-range (nth 2 info) 
7660                                            (list (cdr art-group)))))
7661             ;; Copy any marks over to the new group.
7662             (let ((marks '((tick . gnus-newsgroup-marked)
7663                            (dormant . gnus-newsgroup-dormant)
7664                            (expire . gnus-newsgroup-expirable)
7665                            (bookmark . gnus-newsgroup-bookmarks)
7666                         ;   (score . gnus-newsgroup-scored)
7667                            (reply . gnus-newsgroup-replied)))
7668                   (to-article (cdr art-group)))
7669               (while marks
7670                 (if (memq article (symbol-value (cdr (car marks))))
7671                     (gnus-add-marked-articles 
7672                      (car info) (car (car marks)) (list to-article) info))
7673                 (setq marks (cdr marks)))))
7674         (message "Couldn't copy article %s" (car articles)))
7675       (gnus-summary-remove-process-mark (car articles))
7676       (setq articles (cdr articles)))
7677     (kill-buffer copy-buf)))
7678
7679 (defun gnus-summary-expire-articles ()
7680   "Expire all articles that are marked as expirable in the current group."
7681   (interactive)
7682   (if (and gnus-newsgroup-expirable
7683            (gnus-check-backend-function 
7684             'request-expire-articles gnus-newsgroup-name))
7685       (let ((expirable gnus-newsgroup-expirable))
7686         ;; The list of articles that weren't expired is returned.
7687         (setq gnus-newsgroup-expirable 
7688               (gnus-request-expire-articles gnus-newsgroup-expirable
7689                                             gnus-newsgroup-name))
7690         ;; We go through the old list of expirable, and mark all
7691         ;; really expired articles as non-existant.
7692         (while expirable
7693           (or (memq (car expirable) gnus-newsgroup-expirable)
7694               (gnus-summary-mark-as-read (car expirable) "%"))
7695           (setq expirable (cdr expirable))))))
7696
7697 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
7698 (defun gnus-summary-delete-article (n)
7699   "Delete the N next (mail) articles.
7700 This command actually deletes articles. This is not a marking
7701 command. The article will disappear forever from you life, never to
7702 return. 
7703 If N is negative, delete backwards.
7704 If N is nil and articles have been marked with the process mark,
7705 delete these instead."
7706   (interactive "P")
7707   (or (gnus-check-backend-function 'request-expire-articles 
7708                                    gnus-newsgroup-name)
7709       (error "The current newsgroup does not support article deletion."))
7710   ;; Compute the list of articles to delete.
7711   (let ((articles (gnus-summary-work-articles n)))
7712     (if (and gnus-novice-user
7713              (not (gnus-y-or-n-p 
7714                    (format "Do you really want to delete %s forever? "
7715                            (if (> (length articles) 1) "these articles"
7716                              "this article")))))
7717         ()
7718       ;; Delete the articles.
7719       (setq gnus-newsgroup-expirable 
7720             (gnus-request-expire-articles 
7721              articles gnus-newsgroup-name 'force))
7722       (while articles
7723         (gnus-summary-remove-process-mark (car articles))       
7724         ;; The backend might not have been able to delete the article
7725         ;; after all.  
7726         (or (memq (car articles) gnus-newsgroup-expirable)
7727             (gnus-summary-mark-as-read (car articles) gnus-canceled-mark))
7728         (setq articles (cdr articles)))))
7729   (gnus-summary-position-cursor))
7730
7731 (defun gnus-summary-edit-article ()
7732   "Enter into a buffer and edit the current article.
7733 This will have permanent effect only in mail groups."
7734   (interactive)
7735   (or (gnus-check-backend-function 
7736        'request-replace-article gnus-newsgroup-name)
7737       (error "The current newsgroup does not support article editing."))
7738   (gnus-summary-select-article t)
7739   (other-window 1)
7740   (message "C-c C-c to end edits")
7741   (setq buffer-read-only nil)
7742   (text-mode)
7743   (use-local-map (copy-keymap (current-local-map)))
7744   (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
7745   (goto-char 1)
7746   (search-forward "\n\n" nil t))
7747
7748 (defun gnus-summary-edit-article-done ()
7749   "Make edits to the current article permanent."
7750   (interactive)
7751   (if (not (gnus-request-replace-article 
7752             (cdr gnus-article-current) (car gnus-article-current) 
7753             (current-buffer)))
7754       (error "Couldn't replace article.")
7755     (gnus-article-mode)
7756     (use-local-map gnus-article-mode-map)
7757     (setq buffer-read-only t)
7758     (pop-to-buffer gnus-summary-buffer)))      
7759
7760 ;; Summary score commands.
7761
7762 ;; Suggested by boubaker@cenatls.cena.dgac.fr.
7763
7764 (defun gnus-summary-raise-score (n)
7765   "Raise the score of the current article by N."
7766   (interactive "p")
7767   (gnus-summary-set-score (+ (gnus-summary-article-score) n)))
7768
7769 (defun gnus-summary-lower-score (n)
7770   "Lower the score of the current article by N."
7771   (interactive "p")
7772   (gnus-summary-raise-score (- n)))
7773
7774 (defun gnus-summary-set-score (n)
7775   "Set the score of the current article to N."
7776   (interactive "p")
7777   ;; Skip dummy header line.
7778   (save-excursion
7779     (if (eq (gnus-summary-article-mark) ?Z)
7780         (forward-line 1))
7781     (let ((buffer-read-only nil))
7782       ;; Set score.
7783       (gnus-summary-update-mark
7784        (if (= n (or gnus-summary-default-score 0)) ? 
7785          (if (< n (or gnus-summary-default-score 0)) 
7786              gnus-score-below-mark gnus-score-over-mark)) 'score))
7787     (let* ((article (gnus-summary-article-number))
7788            (score (assq article gnus-newsgroup-scored)))
7789       (if score (setcdr score n)
7790         (setq gnus-newsgroup-scored 
7791               (cons (cons article n) gnus-newsgroup-scored))))
7792     (gnus-summary-update-line)))
7793
7794 ;; Summary marking commands.
7795
7796 (defun gnus-summary-raise-same-subject-and-select (score)
7797   "Raise articles which has the same subject with SCORE and select the next."
7798   (interactive "p")
7799   (let ((subject (gnus-summary-subject-string)))
7800     (gnus-summary-raise-score score)
7801     (while (gnus-summary-search-subject nil nil subject)
7802       (gnus-summary-raise-score score))
7803     (gnus-summary-next-article t)))
7804
7805 (defun gnus-summary-raise-same-subject (score)
7806   "Raise articles which has the same subject with SCORE."
7807   (interactive "p")
7808   (let ((subject (gnus-summary-subject-string)))
7809     (gnus-summary-raise-score score)
7810     (while (gnus-summary-search-subject nil nil subject)
7811       (gnus-summary-raise-score score))
7812     (gnus-summary-next-subject 1 t)))
7813
7814 (defun gnus-summary-raise-thread (score)
7815   "Raise articles under current thread with SCORE."
7816   (interactive "P")
7817   (setq score (1- (gnus-score-default score)))
7818   (let (e)
7819     (save-excursion
7820       (let ((level (gnus-summary-thread-level)))
7821         (gnus-summary-raise-score score)
7822         (while (and (zerop (gnus-summary-next-subject 1 nil t))
7823                     (> (gnus-summary-thread-level) level))
7824           (gnus-summary-raise-score score))
7825         (setq e (point))))
7826     (or (zerop (gnus-summary-next-subject 1 t))
7827         (goto-char e)))
7828   (gnus-summary-recenter)
7829   (gnus-summary-position-cursor)
7830   (gnus-set-mode-line 'summary))
7831
7832 (defun gnus-summary-lower-same-subject-and-select (score)
7833   "Raise articles which has the same subject with SCORE and select the next."
7834   (interactive "p")
7835   (gnus-summary-raise-same-subject-and-select (- score)))
7836
7837 (defun gnus-summary-lower-same-subject (score)
7838   "Raise articles which has the same subject with SCORE."
7839   (interactive "p")
7840   (gnus-summary-raise-same-subject (- score)))
7841
7842 (defun gnus-summary-lower-thread (score)
7843   "Raise articles under current thread with SCORE."
7844   (interactive "P")
7845   (gnus-summary-raise-thread (- (1- (gnus-score-default score)))))
7846
7847 (defun gnus-summary-kill-same-subject-and-select (unmark)
7848   "Mark articles which has the same subject as read, and then select the next.
7849 If UNMARK is positive, remove any kind of mark.
7850 If UNMARK is negative, tick articles."
7851   (interactive "P")
7852   (if unmark
7853       (setq unmark (prefix-numeric-value unmark)))
7854   (let ((count
7855          (gnus-summary-mark-same-subject
7856           (gnus-summary-subject-string) unmark)))
7857     ;; Select next unread article. If auto-select-same mode, should
7858     ;; select the first unread article.
7859     (gnus-summary-next-article t (and gnus-auto-select-same
7860                                       (gnus-summary-subject-string)))
7861     (message "%d articles are marked as %s"
7862              count (if unmark "unread" "read"))))
7863
7864 (defun gnus-summary-kill-same-subject (unmark)
7865   "Mark articles which has the same subject as read. 
7866 If UNMARK is positive, remove any kind of mark.
7867 If UNMARK is negative, tick articles."
7868   (interactive "P")
7869   (if unmark
7870       (setq unmark (prefix-numeric-value unmark)))
7871   (let ((count
7872          (gnus-summary-mark-same-subject
7873           (gnus-summary-subject-string) unmark)))
7874     ;; If marked as read, go to next unread subject.
7875     (if (null unmark)
7876         ;; Go to next unread subject.
7877         (gnus-summary-next-subject 1 t))
7878     (message "%d articles are marked as %s"
7879              count (if unmark "unread" "read"))))
7880
7881 (defun gnus-summary-mark-same-subject (subject &optional unmark)
7882   "Mark articles with same SUBJECT as read, and return marked number.
7883 If optional argument UNMARK is positive, remove any kinds of marks.
7884 If optional argument UNMARK is negative, mark articles as unread instead."
7885   (let ((count 1))
7886     (save-excursion
7887       (cond ((null unmark)
7888              (gnus-summary-mark-as-read nil gnus-killed-mark))
7889             ((> unmark 0)
7890              (gnus-summary-tick-article nil t))
7891             (t
7892              (gnus-summary-tick-article)))
7893       (while (and subject
7894                   (gnus-summary-search-forward nil subject))
7895         (cond ((null unmark)
7896                (gnus-summary-mark-as-read nil gnus-killed-mark))
7897               ((> unmark 0)
7898                (gnus-summary-tick-article nil t))
7899               (t
7900                (gnus-summary-tick-article)))
7901         (setq count (1+ count))))
7902     ;; Hide killed thread subtrees.  Does not work properly always.
7903     ;;(and (null unmark)
7904     ;;     gnus-thread-hide-killed
7905     ;;     (gnus-summary-hide-thread))
7906     ;; Return number of articles marked as read.
7907     count))
7908
7909 (defun gnus-summary-mark-as-processable (n &optional unmark)
7910   "Set the process mark on the next N articles.
7911 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
7912 the process mark instead.  The difference between N and the actual
7913 number of articles marked is returned."
7914   (interactive "p")
7915   (let ((backward (< n 0))
7916         (n (abs n)))
7917   (while (and 
7918           (> n 0)
7919           (if unmark
7920               (gnus-summary-remove-process-mark (gnus-summary-article-number))
7921             (gnus-summary-set-process-mark (gnus-summary-article-number)))
7922           (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
7923     (setq n (1- n)))
7924   (if (/= 0 n) (message "No more articles"))
7925   (gnus-summary-recenter)
7926   (gnus-summary-position-cursor)
7927   n))
7928
7929 (defun gnus-summary-unmark-as-processable (n)
7930   "Remove the process mark from the next N articles.
7931 If N is negative, mark backward instead.  The difference between N and
7932 the actual number of articles marked is returned."
7933   (interactive "p")
7934   (gnus-summary-mark-as-processable n t))
7935
7936 (defun gnus-summary-unmark-all-processable ()
7937   "Remove the process mark from all articles."
7938   (interactive)
7939   (save-excursion
7940     (while gnus-newsgroup-processable
7941       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
7942   (gnus-summary-position-cursor))
7943
7944 (defun gnus-summary-mark-as-expirable (n)
7945   "Mark N articles forward as expirable.
7946 If N is negative, mark backward instead. The difference between N and
7947 the actual number of articles marked is returned."
7948   (interactive "p")
7949   (gnus-summary-mark-forward n gnus-expirable-mark))
7950
7951 (defun gnus-summary-mark-article-as-replied (article)
7952   "Mark ARTICLE replied and update the summary line."
7953   (setq gnus-newsgroup-replied (cons article gnus-newsgroup-replied))
7954   (let ((buffer-read-only nil))
7955     (if (gnus-summary-goto-subject article)
7956         (progn
7957           (gnus-summary-update-mark gnus-replied-mark 'replied)
7958           t))))
7959
7960 (defun gnus-summary-set-bookmark (article)
7961   "Set a bookmark in current article."
7962   (interactive (list (gnus-summary-article-number)))
7963   (if (or (not (get-buffer gnus-article-buffer))
7964           (not gnus-current-article)
7965           (not gnus-article-current)
7966           (not (equal gnus-newsgroup-name (car gnus-article-current))))
7967       (error "No current article selected"))
7968   ;; Remove old bookmark, if one exists.
7969   (let ((old (assq article gnus-newsgroup-bookmarks)))
7970     (if old (setq gnus-newsgroup-bookmarks 
7971                   (delq old gnus-newsgroup-bookmarks))))
7972   ;; Set the new bookmark, which is on the form 
7973   ;; (article-number . line-number-in-body).
7974   (setq gnus-newsgroup-bookmarks 
7975         (cons 
7976          (cons article 
7977                (save-excursion
7978                  (set-buffer gnus-article-buffer)
7979                  (count-lines
7980                   (min (point)
7981                        (save-excursion
7982                          (goto-char 1)
7983                          (search-forward "\n\n" nil t)
7984                          (point)))
7985                   (point))))
7986          gnus-newsgroup-bookmarks))
7987   (message "A bookmark has been added to the current article."))
7988
7989 (defun gnus-summary-remove-bookmark (article)
7990   "Remove the bookmark from the current article."
7991   (interactive (list (gnus-summary-article-number)))
7992   ;; Remove old bookmark, if one exists.
7993   (let ((old (assq article gnus-newsgroup-bookmarks)))
7994     (if old 
7995         (progn
7996           (setq gnus-newsgroup-bookmarks 
7997                 (delq old gnus-newsgroup-bookmarks))
7998           (message "Removed bookmark."))
7999       (message "No bookmark in current article."))))
8000
8001 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8002 (defun gnus-summary-mark-as-dormant (n)
8003   "Mark N articles forward as dormant.
8004 If N is negative, mark backward instead.  The difference between N and
8005 the actual number of articles marked is returned."
8006   (interactive "p")
8007   (gnus-summary-mark-forward n gnus-dormant-mark))
8008
8009 (defun gnus-summary-set-process-mark (article)
8010   "Set the process mark on ARTICLE and update the summary line."
8011   (setq gnus-newsgroup-processable (cons article gnus-newsgroup-processable))
8012   (let ((buffer-read-only nil))
8013     (if (gnus-summary-goto-subject article)
8014         (progn
8015           (and (eq (gnus-summary-article-mark) ?Z)
8016                (forward-line 1))
8017           (gnus-summary-update-mark gnus-process-mark 'replied)
8018           t))))
8019
8020 (defun gnus-summary-remove-process-mark (article)
8021   "Remove the process mark from ARTICLE and update the summary line."
8022   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8023   (let ((buffer-read-only nil))
8024     (if (gnus-summary-goto-subject article)
8025         (progn
8026           (and (eq (gnus-summary-article-mark) ?Z)
8027                (forward-line 1))
8028           (gnus-summary-update-mark ?  'replied)
8029           (if (memq article gnus-newsgroup-replied) 
8030               (gnus-summary-update-mark gnus-replied-mark 'replied))
8031           t))))
8032
8033 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8034   "Mark N articles as read forwards.
8035 If N is negative, mark backwards instead.
8036 Mark with MARK. If MARK is ? , ?! or ??, articles will be
8037 marked as unread. 
8038 The difference between N and the actual number of articles marked is
8039 returned."
8040   (interactive "p")
8041   (gnus-set-global-variables)
8042   (let ((backward (< n 0))
8043         (n (abs n))
8044         (mark (or mark gnus-del-mark)))
8045   (while (and (> n 0)
8046               (gnus-summary-mark-article nil mark no-expire)
8047               (zerop (gnus-summary-next-subject 
8048                       (if backward -1 1) gnus-summary-goto-unread t)))
8049     (setq n (1- n)))
8050   (if (/= 0 n) (message "No more %sarticles" (if mark "" "unread ")))
8051   (gnus-summary-recenter)
8052   (gnus-summary-position-cursor)
8053   (gnus-set-mode-line 'summary)
8054   n))
8055
8056 (defun gnus-summary-mark-article (&optional article mark no-expire)
8057   "Mark ARTICLE with MARK.
8058 MARK can be any character.
8059 Five MARK strings are reserved: ?  (unread), 
8060 ?! (ticked), ?? (dormant), ?D (read), ?E (expirable).
8061 If MARK is nil, then the default character ?D is used.
8062 If ARTICLE is nil, then the article on the current line will be
8063 marked." 
8064   ;; If no mark is given, then we check auto-expiring.
8065   (and (not no-expire)
8066        gnus-newsgroup-auto-expire 
8067        (or (not mark)
8068            (and (numberp mark) (or (= mark gnus-killed-mark)
8069                                    (= mark gnus-del-mark)
8070                                    (= mark gnus-catchup-mark)
8071                                    (= mark gnus-low-score-mark)
8072                                    (= mark gnus-read-mark))))
8073        (setq mark gnus-expirable-mark))
8074   (let* ((buffer-read-only nil)
8075          (mark (or (and (stringp mark) (aref mark 0)) mark gnus-del-mark))
8076          (article (or article (gnus-summary-article-number))))
8077     (if (or (= mark gnus-unread-mark) 
8078             (= mark gnus-ticked-mark) 
8079             (= mark gnus-dormant-mark))
8080         (gnus-mark-article-as-unread article mark)
8081       (gnus-mark-article-as-read article mark))
8082     (if (gnus-summary-goto-subject article)
8083         (progn
8084           (gnus-summary-show-thread)
8085           (beginning-of-line)
8086           (and (eq (gnus-summary-article-mark) ?Z)
8087                (forward-line 1))
8088           ;; Fix the mark.
8089           (gnus-summary-update-mark mark 'unread)
8090           t))))
8091
8092 (defun gnus-summary-update-mark (mark type)
8093   (beginning-of-line)
8094   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8095         plist)
8096     (if (not forward)
8097         ()
8098       (forward-char forward)
8099       (setq plist (text-properties-at (point)))
8100       (delete-char 1)
8101       (and (memq 'gnus-mark plist) (setcar (cdr (memq 'gnus-mark plist)) mark))
8102       (insert mark)
8103       (and plist (add-text-properties (1- (point)) (point) plist))
8104       (gnus-summary-update-line (eq mark gnus-unread-mark)))))
8105   
8106 (defun gnus-mark-article-as-read (article &optional mark)
8107   "Enter ARTICLE in the pertinent lists and remove it from others."
8108   ;; Make the article expirable.
8109   (let ((mark (or (and (stringp mark) (aref mark 0)) mark gnus-del-mark)))
8110     (if (= mark gnus-expirable-mark)
8111         (setq gnus-newsgroup-expirable (cons article gnus-newsgroup-expirable))
8112       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8113     ;; Remove from unread and marked lists.
8114     (setq gnus-newsgroup-unreads
8115           (delq article gnus-newsgroup-unreads))
8116     (setq gnus-newsgroup-marked
8117           (delq article gnus-newsgroup-marked))
8118     (setq gnus-newsgroup-dormant
8119           (delq article gnus-newsgroup-dormant))))
8120
8121 (defun gnus-mark-article-as-unread (article &optional mark)
8122   "Enter ARTICLE in the pertinent lists and remove it from others."
8123   (let ((mark (or (and (stringp mark) (aref mark 0)) mark gnus-ticked-mark)))
8124     ;; Add to unread list.
8125     (or (memq article gnus-newsgroup-unreads)
8126         (setq gnus-newsgroup-unreads
8127               (cons article gnus-newsgroup-unreads)))
8128     ;; If CLEAR-MARK is non-nil, the article must be removed from marked
8129     ;; list.  Otherwise, it must be added to the list.
8130     (setq gnus-newsgroup-marked
8131           (delq article gnus-newsgroup-marked))
8132     (setq gnus-newsgroup-dormant
8133           (delq article gnus-newsgroup-dormant))
8134     (setq gnus-newsgroup-expirable 
8135           (delq article gnus-newsgroup-expirable))
8136     (if (= mark gnus-ticked-mark)
8137         (setq gnus-newsgroup-marked 
8138               (cons article gnus-newsgroup-marked)))
8139     (if (= mark gnus-dormant-mark)
8140         (setq gnus-newsgroup-dormant 
8141               (cons article gnus-newsgroup-dormant)))))
8142
8143 (defalias 'gnus-summary-mark-as-unread-forward 
8144   'gnus-summary-tick-article-forward)
8145 (make-obsolete 'gnus-summary-mark-as-unread-forward 
8146                'gnus-summary-tick-article-forward)
8147 (defun gnus-summary-tick-article-forward (n)
8148   "Tick N articles forwards.
8149 If N is negative, tick backwards instead.
8150 The difference between N and the number of articles ticked is returned."
8151   (interactive "p")
8152   (gnus-summary-mark-forward n gnus-ticked-mark))
8153
8154 (defalias 'gnus-summary-mark-as-unread-backward 
8155   'gnus-summary-tick-article-backward)
8156 (make-obsolete 'gnus-summary-mark-as-unread-backward 
8157                'gnus-summary-tick-article-backward)
8158 (defun gnus-summary-tick-article-backward (n)
8159   "Tick N articles backwards.
8160 The difference between N and the number of articles ticked is returned."
8161   (interactive "p")
8162   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8163
8164 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8165 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8166 (defun gnus-summary-tick-article (&optional article clear-mark)
8167   "Mark current article as unread.
8168 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8169 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8170   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8171                                        gnus-ticked-mark)))
8172
8173 (defun gnus-summary-mark-as-read-forward (n)
8174   "Mark N articles as read forwards.
8175 If N is negative, mark backwards instead.
8176 The difference between N and the actual number of articles marked is
8177 returned."
8178   (interactive "p")
8179   (gnus-summary-mark-forward n gnus-del-mark t))
8180
8181 (defun gnus-summary-mark-as-read-backward (n)
8182   "Mark the N articles as read backwards.
8183 The difference between N and the actual number of articles marked is
8184 returned."
8185   (interactive "p")
8186   (gnus-summary-mark-forward (- n) gnus-del-mark t))
8187
8188 (defun gnus-summary-mark-as-read (&optional article mark)
8189   "Mark current article as read.
8190 ARTICLE specifies the article to be marked as read.
8191 MARK specifies a string to be inserted at the beginning of the line."
8192   (gnus-summary-mark-article article mark))
8193
8194 (defun gnus-summary-clear-mark-forward (n)
8195   "Clear marks from N articles forward.
8196 If N is negative, clear backward instead.
8197 The difference between N and the number of marks cleared is returned."
8198   (interactive "p")
8199   (gnus-summary-mark-forward n gnus-unread-mark))
8200
8201 (defun gnus-summary-clear-mark-backward (n)
8202   "Clear marks from N articles backward.
8203 The difference between N and the number of marks cleared is returned."
8204   (interactive "p")
8205   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8206
8207 (defun gnus-summary-mark-unread-as-read ()
8208   "Intended to be used by `gnus-summary-mark-article-hook'."
8209   (or (memq gnus-current-article gnus-newsgroup-marked)
8210       (memq gnus-current-article gnus-newsgroup-dormant)
8211       (memq gnus-current-article gnus-newsgroup-expirable)
8212       (gnus-summary-mark-as-read gnus-current-article gnus-read-mark)))
8213
8214 (defun gnus-summary-mark-region-as-read (point mark all)
8215   "Mark all unread articles between point and mark as read.
8216 If given a prefix, mark all articles between point and mark as read,
8217 even ticked and dormant ones."
8218   (interactive "r\nP")
8219   (save-excursion
8220     (goto-char point)
8221     (beginning-of-line)
8222     (while (and 
8223             (< (point) mark)
8224             (progn
8225               (and
8226                (or all
8227                    (and
8228                     (not (memq (gnus-summary-article-number)
8229                                gnus-newsgroup-marked))
8230                     (not (memq (gnus-summary-article-number)
8231                                gnus-newsgroup-dormant))))
8232                (gnus-summary-mark-article
8233                 (gnus-summary-article-number) gnus-del-mark))
8234               t)
8235             (zerop (forward-line 1))))))
8236
8237 ;; Fix by Per Abrahamsen <amanda@iesd.auc.dk>.
8238 (defalias 'gnus-summary-delete-marked-as-read 
8239   'gnus-summary-remove-lines-marked-as-read)
8240 (make-obsolete 'gnus-summary-delete-marked-as-read 
8241                'gnus-summary-remove-lines-marked-as-read)
8242 (defun gnus-summary-remove-lines-marked-as-read ()
8243   "Remove lines that are marked as read."
8244   (interactive)
8245   (gnus-summary-remove-lines-marked-with 
8246    (concat (mapconcat
8247             (lambda (char) (char-to-string (symbol-value char)))
8248             '(gnus-del-mark gnus-read-mark
8249               gnus-killed-mark gnus-kill-file-mark
8250               gnus-low-score-mark gnus-expirable-mark)
8251             ""))))
8252
8253 (defalias 'gnus-summary-delete-marked-with 
8254   'gnus-summary-remove-lines-marked-with)
8255 (make-obsolete 'gnus-summary-delete-marked-with 
8256                'gnus-summary-remove-lines-marked-with)
8257 ;; Rewrite by Daniel Quinlan <quinlan@best.com>.
8258 (defun gnus-summary-remove-lines-marked-with (marks)
8259   "Remove lines that are marked with MARKS (e.g. \"DK\")."
8260   (interactive "sMarks: ")
8261   ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
8262   (save-excursion
8263     (set-buffer gnus-summary-buffer)
8264     (let ((buffer-read-only nil)
8265           (marks (concat "^[" marks "]")))
8266       (goto-char (point-min))
8267       (if gnus-use-adaptive-scoring
8268           (gnus-score-remove-lines-adaptive marks)
8269         (while (re-search-forward marks (point-max) t)
8270           (beginning-of-line)
8271           (delete-region (point)
8272                          (progn (forward-line 1) (point))))))
8273     (or (zerop (buffer-size))
8274         (if (eobp)
8275             (gnus-summary-prev-subject 1)
8276           (gnus-summary-position-cursor)))))
8277
8278 (defun gnus-summary-expunge-below (score)
8279   "Remove articles with score less than SCORE."
8280   (interactive "P")
8281   (setq score (if score
8282                   (prefix-numeric-value score)
8283                 (or gnus-summary-default-score 0)))
8284   (save-excursion
8285     (set-buffer gnus-summary-buffer)
8286     (goto-char (point-min))
8287     (let ((buffer-read-only nil)
8288           beg)
8289       (while (not (eobp))
8290         (if (< (gnus-summary-article-score) score)
8291             (progn
8292               (setq beg (point))
8293               (forward-line 1)
8294               (delete-region beg (point)))
8295           (forward-line 1)))
8296       ;; Adjust point.
8297       (or (zerop (buffer-size))
8298           (if (eobp)
8299               (gnus-summary-prev-subject 1)
8300             (gnus-summary-position-cursor))))))
8301
8302 (defun gnus-summary-mark-below (score mark)
8303   "Mark articles with score less than SCORE with MARK."
8304   (interactive "P\ncMark: ")
8305   (setq score (if score
8306                   (prefix-numeric-value score)
8307                 (or gnus-summary-default-score 0)))
8308   (save-excursion
8309     (set-buffer gnus-summary-buffer)
8310     (goto-char (point-min))
8311     (while (not (eobp))
8312       (and (< (gnus-summary-article-score) score)
8313            (gnus-summary-mark-article nil mark))
8314       (forward-line 1))))
8315
8316 (defun gnus-summary-kill-below (score)
8317   "Mark articles with score below SCORE as read."
8318   (interactive "P")
8319   (gnus-summary-mark-below score gnus-killed-mark))
8320
8321 (defun gnus-summary-clear-above (score)
8322   "Clear all marks from articles with score above SCORE."
8323   (interactive "P")
8324   (gnus-summary-mark-above score gnus-unread-mark))
8325
8326 (defun gnus-summary-tick-above (score)
8327   "Tick all articles with score above SCORE."
8328   (interactive "P")
8329   (gnus-summary-mark-above score gnus-ticked-mark))
8330
8331 (defun gnus-summary-mark-above (score mark)
8332   "Mark articles with score over SCORE with MARK."
8333   (interactive "P\ncMark: ")
8334   (setq score (if score
8335                   (prefix-numeric-value score)
8336                 (or gnus-summary-default-score 0)))
8337   (save-excursion
8338     (set-buffer gnus-summary-buffer)
8339     (goto-char (point-min))
8340     (while (not (eobp))
8341       (if (> (gnus-summary-article-score) score)
8342           (progn
8343             (gnus-summary-mark-article nil mark)
8344             (forward-line 1))
8345         (forward-line 1)))))
8346
8347 ;; Suggested by Daniel Quinlan <quinlan@best.com>.  
8348 (defun gnus-summary-show-all-expunged ()
8349   "Display all the hidden articles that were expunged for low scores."
8350   (interactive)
8351   (let ((buffer-read-only nil))
8352     (let ((scored gnus-newsgroup-scored)
8353           headers h)
8354       (while scored
8355         (or (gnus-summary-goto-subject (car (car scored)))
8356             (and (setq h (gnus-get-header-by-number (car (car scored))))
8357                  (< (cdr (car scored)) gnus-summary-expunge-below)
8358                  (setq headers (cons h headers))))
8359         (setq scored (cdr scored)))
8360       (or headers (error "No expunged articles hidden."))
8361       (goto-char (point-min))
8362       (save-excursion 
8363         (gnus-summary-prepare-threads (nreverse headers) 0)))
8364     (goto-char (point-min))
8365     (gnus-summary-position-cursor)))
8366
8367 (defun gnus-summary-show-all-dormant ()
8368   "Display all the hidden articles that are marked as dormant."
8369   (interactive)
8370   (let ((buffer-read-only nil))
8371     (goto-char (point-min))
8372     (let ((dormant gnus-newsgroup-dormant)
8373           headers h)
8374       (while dormant
8375         (or (gnus-summary-goto-subject (car dormant))
8376             (and (setq h (gnus-get-header-by-number (car dormant)))
8377                  (setq headers (cons h headers))))
8378         (setq dormant (cdr dormant)))
8379       (or headers (error "No dormant articles hidden."))
8380       (save-excursion (gnus-summary-prepare-threads (nreverse headers) 0)))
8381     (goto-char (point-min))
8382     (gnus-summary-position-cursor)))
8383
8384 (defun gnus-summary-hide-all-dormant ()
8385   "Hide all dormant articles."
8386   (interactive)
8387   (gnus-summary-remove-lines-marked-with (char-to-string gnus-dormant-mark))
8388   (gnus-summary-position-cursor))
8389
8390 (defun gnus-summary-catchup (all &optional quietly to-here)
8391   "Mark all articles not marked as unread in this newsgroup as read.
8392 If prefix argument ALL is non-nil, all articles are marked as read.
8393 If QUIETLY is non-nil, no questions will be asked.
8394 If TO-HERE is non-nil, it should be a point in the buffer. All
8395 articles before this point will be marked as read.
8396 The number of articles marked as read is returned."
8397   (interactive "P")
8398   (prog1
8399       (if (or quietly
8400               (not gnus-interactive-catchup) ;Without confirmation?
8401               gnus-expert-user
8402               (gnus-y-or-n-p
8403                (if all
8404                    "Mark absolutely all articles as read? "
8405                  "Mark all unread articles as read? ")))
8406           (let ((unreads (length gnus-newsgroup-unreads)))
8407             (if (gnus-summary-first-subject (not all))
8408                 (while (and (gnus-summary-mark-as-read nil gnus-catchup-mark)
8409                             (if to-here (< (point) to-here) t)
8410                             (gnus-summary-search-subject nil (not all)))))
8411             (- unreads (length gnus-newsgroup-unreads))
8412             (or to-here
8413                 (setq gnus-newsgroup-unreads gnus-newsgroup-marked))))
8414     (gnus-summary-position-cursor)))
8415
8416 (defun gnus-summary-catchup-to-here (&optional all)
8417   "Mark all unticked articles before the current one as read.
8418 If ALL is non-nil, also mark ticked and dormant articles as read."
8419   (interactive)
8420   (beginning-of-line)
8421   (gnus-summary-catchup all t (point))
8422   (gnus-summary-position-cursor))
8423
8424 (defun gnus-summary-catchup-all (&optional quietly)
8425   "Mark all articles in this newsgroup as read."
8426   (interactive)
8427   (gnus-summary-catchup t quietly))
8428
8429 (defun gnus-summary-catchup-and-exit (all &optional quietly)
8430   "Mark all articles not marked as unread in this newsgroup as read, then exit.
8431 If prefix argument ALL is non-nil, all articles are marked as read."
8432   (interactive "P")
8433   (gnus-summary-catchup all quietly)
8434   ;; Select next newsgroup or exit.
8435   (if (eq gnus-auto-select-next 'quietly)
8436       (gnus-summary-next-group nil)
8437     (gnus-summary-exit)))
8438
8439 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8440   "Mark all articles in this newsgroup as read, and then exit."
8441   (interactive)
8442   (gnus-summary-catchup-and-exit t quietly))
8443
8444 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
8445 (defun gnus-summary-catchup-and-goto-next-group (all)
8446   "Mark all articles in this group as read and select the next group.
8447 If given a prefix, mark all articles, unread as well as ticked, as
8448 read." 
8449   (interactive "P")
8450   (gnus-summary-catchup all)
8451   (gnus-summary-next-group))
8452
8453 ;; Thread-based commands.
8454
8455 (defun gnus-summary-toggle-threads (arg)
8456   "Toggle showing conversation threads.
8457 If ARG is positive number, turn showing conversation threads on."
8458   (interactive "P")
8459   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
8460     (setq gnus-show-threads
8461           (if (null arg) (not gnus-show-threads)
8462             (> (prefix-numeric-value arg) 0)))
8463     (gnus-summary-prepare)
8464     (gnus-summary-goto-subject current)
8465     (gnus-summary-position-cursor)))
8466
8467 (defun gnus-summary-show-all-threads ()
8468   "Show all threads."
8469   (interactive)
8470   (save-excursion
8471     (let ((buffer-read-only nil))
8472       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
8473   (gnus-summary-position-cursor))
8474
8475 (defun gnus-summary-show-thread ()
8476   "Show thread subtrees.
8477 Returns nil if no thread was there to be shown."
8478   (interactive)
8479   (prog1
8480       (save-excursion
8481         (let ((buffer-read-only nil)
8482               ;; first goto end then to beg, to have point at beg after let
8483               (end (progn (end-of-line) (point)))
8484               (beg (progn (beginning-of-line) (point))))
8485           (prog1
8486               ;; Any hidden lines here?
8487               (search-forward "\r" end t)
8488             (subst-char-in-region beg end ?\^M ?\n t))))
8489     (gnus-summary-position-cursor)))
8490
8491 (defun gnus-summary-hide-all-threads ()
8492   "Hide all thread subtrees."
8493   (interactive)
8494   (save-excursion
8495     (goto-char (point-min))
8496     (gnus-summary-hide-thread)
8497     (while (and (not (eobp)) (zerop (forward-line 1)))
8498       (gnus-summary-hide-thread)))
8499   (gnus-summary-position-cursor))
8500
8501 (defun gnus-summary-hide-thread ()
8502   "Hide thread subtrees.
8503 Returns nil if no threads were there to be hidden."
8504   (interactive)
8505   (let ((buffer-read-only nil)
8506         (start (point))
8507         (level (gnus-summary-thread-level))
8508         (end (point)))
8509     ;; Go forward until either the buffer ends or the subthread
8510     ;; ends. 
8511     (if (eobp)
8512         ()
8513       (while (and (zerop (forward-line 1))
8514                   (> (gnus-summary-thread-level) level))
8515         (setq end (point)))
8516       (prog1
8517           (save-excursion
8518             (goto-char end)
8519             (search-backward "\n" start t))
8520         (subst-char-in-region start end ?\n ?\^M t)
8521         (forward-line -1)))))
8522
8523 (defun gnus-summary-go-to-next-thread (&optional previous)
8524   "Go to the same level (or less) next thread.
8525 If PREVIOUS is non-nil, go to previous thread instead.
8526 Return the article number moved to, or nil if moving was impossible."
8527   (let ((level (gnus-summary-thread-level))
8528         (article (gnus-summary-article-number)))
8529     (if previous 
8530         (while (and (zerop (gnus-summary-prev-subject 1))
8531                     (> (gnus-summary-thread-level) level)))
8532       (while (and (zerop (gnus-summary-next-subject 1))
8533                   (> (gnus-summary-thread-level) level))))
8534     (gnus-summary-recenter)
8535     (gnus-summary-position-cursor)
8536     (let ((oart (gnus-summary-article-number)))
8537       (and (/= oart article) oart))))
8538
8539 (defun gnus-summary-next-thread (n)
8540   "Go to the same level next N'th thread.
8541 If N is negative, search backward instead.
8542 Returns the difference between N and the number of skips actually
8543 done."
8544   (interactive "p")
8545   (let ((backward (< n 0))
8546         (n (abs n)))
8547   (while (and (> n 0)
8548               (gnus-summary-go-to-next-thread backward))
8549     (setq n (1- n)))
8550   (gnus-summary-position-cursor)
8551   (if (/= 0 n) (message "No more threads"))
8552   n))
8553
8554 (defun gnus-summary-prev-thread (n)
8555   "Go to the same level previous N'th thread.
8556 Returns the difference between N and the number of skips actually
8557 done."
8558   (interactive "p")
8559   (gnus-summary-next-thread (- n)))
8560
8561 (defun gnus-summary-go-down-thread (&optional same)
8562   "Go down one level in the current thread.
8563 If SAME is non-nil, also move to articles of the same level."
8564   (let ((level (gnus-summary-thread-level))
8565         (start (point)))
8566     (if (and (zerop (forward-line 1))
8567              (> (gnus-summary-thread-level) level))
8568         t
8569       (goto-char start)
8570       nil)))
8571
8572 (defun gnus-summary-go-up-thread ()
8573   "Go up one level in the current thread."
8574   (let ((level (gnus-summary-thread-level))
8575         (start (point)))
8576     (while (and (zerop (forward-line -1))
8577                 (>= (gnus-summary-thread-level) level)))
8578     (if (>= (gnus-summary-thread-level) level)
8579         (progn
8580           (goto-char start)
8581           nil)
8582       t)))
8583
8584 (defun gnus-summary-down-thread (n)
8585   "Go down thread N steps.
8586 If N is negative, go up instead.
8587 Returns the difference between N and how many steps down that were
8588 taken."
8589   (interactive "p")
8590   (let ((up (< n 0))
8591         (n (abs n)))
8592   (while (and (> n 0)
8593               (if up (gnus-summary-go-up-thread)
8594                 (gnus-summary-go-down-thread)))
8595     (setq n (1- n)))
8596   (gnus-summary-position-cursor)
8597   (if (/= 0 n) (message "Can't go further"))
8598   n))
8599
8600 (defun gnus-summary-up-thread (n)
8601   "Go up thread N steps.
8602 If N is negative, go up instead.
8603 Returns the difference between N and how many steps down that were
8604 taken."
8605   (interactive "p")
8606   (gnus-summary-down-thread (- n)))
8607
8608 (defun gnus-summary-kill-thread (unmark)
8609   "Mark articles under current thread as read.
8610 If the prefix argument is positive, remove any kinds of marks.
8611 If the prefix argument is negative, tick articles instead."
8612   (interactive "P")
8613   (if unmark
8614       (setq unmark (prefix-numeric-value unmark)))
8615   (let ((killing t)
8616         (level (gnus-summary-thread-level)))
8617     (save-excursion
8618       (while killing
8619         ;; Mark the article...
8620         (cond ((null unmark) (gnus-summary-mark-as-read nil gnus-killed-mark))
8621               ((> unmark 0) (gnus-summary-tick-article nil t))
8622               (t (gnus-summary-tick-article)))
8623         ;; ...and go forward until either the buffer ends or the subtree
8624         ;; ends. 
8625         (if (not (and (zerop (forward-line 1))
8626                       (> (gnus-summary-thread-level) level)))
8627             (setq killing nil))))
8628     ;; Hide killed subtrees.
8629     (and (null unmark)
8630          gnus-thread-hide-killed
8631          (gnus-summary-hide-thread))
8632     ;; If marked as read, go to next unread subject.
8633     (if (null unmark)
8634         ;; Go to next unread subject.
8635         (gnus-summary-next-subject 1 t)))
8636   (gnus-set-mode-line 'summary))
8637
8638 ;; Summary sorting commands
8639
8640 (defun gnus-summary-sort-by-number (reverse)
8641   "Sort summary buffer by article number.
8642 Argument REVERSE means reverse order."
8643   (interactive "P")
8644   (gnus-summary-sort 
8645    (cons 'gnus-summary-article-number 'gnus-thread-sort-by-number) reverse))
8646
8647 (defun gnus-summary-sort-by-author (reverse)
8648   "Sort summary buffer by author name alphabetically.
8649 If case-fold-search is non-nil, case of letters is ignored.
8650 Argument REVERSE means reverse order."
8651   (interactive "P")
8652   (gnus-summary-sort
8653    (cons
8654     (lambda ()
8655       (let ((extract (funcall
8656                       gnus-extract-address-components
8657                       (header-from (gnus-get-header-by-number
8658                                     (gnus-summary-article-number))))))
8659         (or (car extract) (cdr extract))))
8660     'gnus-thread-sort-by-author)
8661    reverse))
8662
8663 (defun gnus-summary-sort-by-subject (reverse)
8664   "Sort summary buffer by subject alphabetically. `Re:'s are ignored.
8665 If case-fold-search is non-nil, case of letters is ignored.
8666 Argument REVERSE means reverse order."
8667   (interactive "P")
8668   (gnus-summary-sort
8669    (cons
8670     (lambda ()
8671       (downcase (gnus-simplify-subject (gnus-summary-subject-string))))
8672     'gnus-thread-sort-by-subject)
8673    reverse))
8674
8675 (defun gnus-summary-sort-by-date (reverse)
8676   "Sort summary buffer by date.
8677 Argument REVERSE means reverse order."
8678   (interactive "P")
8679   (gnus-summary-sort
8680    (cons
8681     (lambda ()
8682       (gnus-sortable-date
8683        (header-date 
8684         (gnus-get-header-by-number (gnus-summary-article-number)))))
8685     'gnus-thread-sort-by-date)
8686    reverse))
8687
8688 (defun gnus-summary-sort-by-score (reverse)
8689   "Sort summary buffer by score.
8690 Argument REVERSE means reverse order."
8691   (interactive "P")
8692   (gnus-summary-sort 
8693    (cons 'gnus-summary-article-score 'gnus-thread-sort-by-score)
8694    (not reverse)))
8695
8696 (defvar gnus-summary-already-sorted nil)
8697 (defun gnus-summary-sort (predicate reverse)
8698   ;; Sort summary buffer by PREDICATE.  REVERSE means reverse order. 
8699   (if gnus-summary-already-sorted
8700       ()
8701     (let (buffer-read-only)
8702       (if (not gnus-show-threads)
8703           (progn
8704             (goto-char (point-min))
8705             (sort-subr reverse 'forward-line 'end-of-line (car predicate)))
8706         (let ((gnus-thread-sort-functions (list (cdr predicate)))
8707               (gnus-summary-already-sorted nil))
8708           (gnus-summary-prepare))))))
8709   
8710 (defun gnus-sortable-date (date)
8711   "Make sortable string by string-lessp from DATE.
8712 Timezone package is used."
8713   (let* ((date   (timezone-fix-time date nil nil)) ;[Y M D H M S]
8714          (year   (aref date 0))
8715          (month  (aref date 1))
8716          (day    (aref date 2)))
8717     (timezone-make-sortable-date 
8718      year month day 
8719      (timezone-make-time-string
8720       (aref date 3) (aref date 4) (aref date 5)))))
8721
8722
8723 ;; Summary saving commands.
8724
8725 (defun gnus-summary-save-article (n)
8726   "Save the current article using the default saver function.
8727 If N is a positive number, save the N next articles.
8728 If N is a negative number, save the N previous articles.
8729 If N is nil and any articles have been marked with the process mark,
8730 save those articles instead.
8731 The variable `gnus-default-article-saver' specifies the saver function."
8732   (interactive "P")
8733   (let (articles process)
8734     (if (and n (numberp n))
8735         (let ((backward (< n 0))
8736               (n (abs n)))
8737           (save-excursion
8738             (while (and (> n 0)
8739                         (setq articles (cons (gnus-summary-article-number) 
8740                                              articles))
8741                         (gnus-summary-search-forward nil nil backward))
8742               (setq n (1- n))))
8743           (setq articles (sort articles (function <))))
8744       (if gnus-newsgroup-processable
8745           (progn
8746             (setq articles (setq gnus-newsgroup-processable
8747                                  (nreverse gnus-newsgroup-processable)))
8748             (setq process t))
8749         (setq articles (list (gnus-summary-article-number)))))
8750     (while articles
8751       (let ((header (gnus-gethash (int-to-string (car articles))
8752                                   gnus-newsgroup-headers-hashtb-by-number)))
8753         (if (vectorp header)
8754             (progn
8755               (gnus-summary-select-article t nil nil (car articles))
8756               (or gnus-save-all-headers
8757                   (gnus-article-hide-headers t))
8758               ;; Remove any X-Gnus lines.
8759               (save-excursion
8760                 (save-restriction
8761                   (set-buffer gnus-article-buffer)
8762                   (let ((buffer-read-only nil))
8763                     (goto-char (point-min))
8764                     (narrow-to-region (point) (or (search-forward "\n\n" nil t)
8765                                                   (point-max)))
8766                     (while (re-search-forward "^X-Gnus" nil t)
8767                       (beginning-of-line)
8768                       (delete-region (point)
8769                                      (progn (forward-line 1) (point))))
8770                     (widen))))
8771               (if gnus-default-article-saver
8772                   (funcall gnus-default-article-saver)
8773                 (error "No default saver is defined.")))
8774           (if (assq 'name header)
8775               (gnus-copy-file (cdr (assq 'name header)))
8776             (message "Article %d is unsaveable" (car articles)))))
8777       (if process
8778           (gnus-summary-remove-process-mark (car articles)))
8779       (setq articles (cdr articles)))
8780     (if process (setq gnus-newsgroup-processable 
8781                       (nreverse gnus-newsgroup-processable)))
8782     (gnus-summary-position-cursor)
8783     n))
8784
8785 (defun gnus-summary-pipe-output (arg)
8786   "Pipe the current article to a subprocess.
8787 If N is a positive number, pipe the N next articles.
8788 If N is a negative number, pipe the N previous articles.
8789 If N is nil and any articles have been marked with the process mark,
8790 pipe those articles instead."
8791   (interactive "P")
8792   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
8793     (gnus-summary-save-article arg)))
8794
8795 (defun gnus-summary-save-article-mail (arg)
8796   "Append the current article to an mail file.
8797 If N is a positive number, save the N next articles.
8798 If N is a negative number, save the N previous articles.
8799 If N is nil and any articles have been marked with the process mark,
8800 save those articles instead."
8801   (interactive "P")
8802   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
8803     (gnus-summary-save-article arg)))
8804
8805 (defun gnus-summary-save-article-rmail (arg)
8806   "Append the current article to an rmail file.
8807 If N is a positive number, save the N next articles.
8808 If N is a negative number, save the N previous articles.
8809 If N is nil and any articles have been marked with the process mark,
8810 save those articles instead."
8811   (interactive "P")
8812   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
8813     (gnus-summary-save-article arg)))
8814
8815 (defun gnus-summary-save-article-file (arg)
8816   "Append the current article to a file.
8817 If N is a positive number, save the N next articles.
8818 If N is a negative number, save the N previous articles.
8819 If N is nil and any articles have been marked with the process mark,
8820 save those articles instead."
8821   (interactive "P")
8822   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
8823     (gnus-summary-save-article arg)))
8824
8825 (defun gnus-summary-save-article-folder (arg)
8826   "Append the current article to an mh folder.
8827 If N is a positive number, save the N next articles.
8828 If N is a negative number, save the N previous articles.
8829 If N is nil and any articles have been marked with the process mark,
8830 save those articles instead."
8831   (interactive "P")
8832   (let ((gnus-default-article-saver 'gnus-summary-save-in-folder))
8833     (gnus-summary-save-article arg)))
8834
8835 (defun gnus-read-save-file-name (prompt default-name)
8836   (let ((methods gnus-split-methods)
8837         split-name)
8838     (if (not gnus-split-methods)
8839         ()
8840       (save-excursion
8841         (set-buffer gnus-article-buffer)
8842         (gnus-narrow-to-headers)
8843         (while methods
8844           (goto-char (point-min))
8845           (and (condition-case () 
8846                    (re-search-forward (car (car methods)) nil t)
8847                  (error nil))
8848                (setq split-name (cons (nth 1 (car methods)) split-name)))
8849           (setq methods (cdr methods)))
8850         (widen)))
8851     (cond ((null split-name)
8852            (read-file-name
8853             (concat prompt " (default "
8854                     (file-name-nondirectory default-name) ") ")
8855             (file-name-directory default-name)
8856             default-name))
8857           ((= 1 (length split-name))
8858            (read-file-name
8859             (concat prompt " (default " (car split-name) ") ")
8860             gnus-article-save-directory
8861             (concat gnus-article-save-directory (car split-name))))
8862           (t
8863            (setq split-name (mapcar (lambda (el) (list el))
8864                                     (nreverse split-name)))
8865            (let ((result (completing-read 
8866                           (concat prompt " ")
8867                           split-name nil nil)))
8868              (concat gnus-article-save-directory
8869                      (if (string= result "")
8870                          (car (car split-name))
8871                        result)))))))
8872
8873 (defun gnus-summary-save-in-rmail (&optional filename)
8874   "Append this article to Rmail file.
8875 Optional argument FILENAME specifies file name.
8876 Directory to save to is default to `gnus-article-save-directory' which
8877 is initialized from the SAVEDIR environment variable."
8878   (interactive)
8879   (let ((default-name
8880           (funcall gnus-rmail-save-name gnus-newsgroup-name
8881                    gnus-current-headers gnus-newsgroup-last-rmail)))
8882     (or filename
8883         (setq filename (gnus-read-save-file-name 
8884                         "Save in rmail file:" default-name)))
8885     (gnus-make-directory (file-name-directory filename))
8886     (gnus-eval-in-buffer-window 
8887      gnus-article-buffer
8888      (save-excursion
8889        (save-restriction
8890          (widen)
8891          (gnus-output-to-rmail filename))))
8892     ;; Remember the directory name to save articles.
8893     (setq gnus-newsgroup-last-rmail filename)))
8894
8895 (defun gnus-summary-save-in-mail (&optional filename)
8896   "Append this article to Unix mail file.
8897 Optional argument FILENAME specifies file name.
8898 Directory to save to is default to `gnus-article-save-directory' which
8899 is initialized from the SAVEDIR environment variable."
8900   (interactive)
8901   (let ((default-name
8902           (funcall gnus-mail-save-name gnus-newsgroup-name
8903                    gnus-current-headers gnus-newsgroup-last-mail)))
8904     (or filename
8905         (setq filename (gnus-read-save-file-name 
8906                         "Save in Unix mail file:" default-name)))
8907     (setq filename
8908           (expand-file-name filename
8909                             (and default-name
8910                                  (file-name-directory default-name))))
8911     (gnus-make-directory (file-name-directory filename))
8912     (gnus-eval-in-buffer-window 
8913      gnus-article-buffer
8914      (save-excursion
8915        (save-restriction
8916          (widen)
8917          (if (and (file-readable-p filename) (rmail-file-p filename))
8918              (gnus-output-to-rmail filename)
8919            (rmail-output filename 1 t t)))))
8920     ;; Remember the directory name to save articles.
8921     (setq gnus-newsgroup-last-mail filename)))
8922
8923 (defun gnus-summary-save-in-file (&optional filename)
8924   "Append this article to file.
8925 Optional argument FILENAME specifies file name.
8926 Directory to save to is default to `gnus-article-save-directory' which
8927 is initialized from the SAVEDIR environment variable."
8928   (interactive)
8929   (let ((default-name
8930           (funcall gnus-file-save-name gnus-newsgroup-name
8931                    gnus-current-headers gnus-newsgroup-last-file)))
8932     (or filename
8933         (setq filename (gnus-read-save-file-name 
8934                         "Save in file:" default-name)))
8935     (gnus-make-directory (file-name-directory filename))
8936     (gnus-eval-in-buffer-window 
8937      gnus-article-buffer
8938      (save-excursion
8939        (save-restriction
8940          (widen)
8941          (gnus-output-to-file filename))))
8942     ;; Remember the directory name to save articles.
8943     (setq gnus-newsgroup-last-file filename)))
8944
8945 (defun gnus-summary-save-in-pipe (&optional command)
8946   "Pipe this article to subprocess."
8947   (interactive)
8948   (let ((command (read-string "Shell command on article: "
8949                               gnus-last-shell-command)))
8950     (if (string-equal command "")
8951         (setq command gnus-last-shell-command))
8952     (gnus-eval-in-buffer-window 
8953      gnus-article-buffer
8954      (save-restriction
8955        (widen)
8956        (shell-command-on-region (point-min) (point-max) command nil)))
8957     (setq gnus-last-shell-command command)))
8958
8959 ;; Summary extract commands
8960
8961 (defun gnus-summary-insert-pseudos (pslist)
8962   (let ((buffer-read-only nil)
8963         (article (gnus-summary-article-number))
8964         b)
8965     (or (gnus-summary-goto-subject article)
8966         (error (format "No such article: %d" article)))
8967     (or gnus-newsgroup-headers-hashtb-by-number
8968         (gnus-make-headers-hashtable-by-number))
8969     (gnus-summary-position-cursor)
8970     ;; If all commands are to be bunched up on one line, we collect
8971     ;; them here.  
8972     (if gnus-view-pseudos-separately
8973         ()
8974       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
8975             files action)
8976         (while ps
8977           (setq action (cdr (assq 'action (car ps))))
8978           (setq files (list (cdr (assq 'name (car ps)))))
8979           (while (and ps (cdr ps)
8980                       (string= (or action "1")
8981                                (or (cdr (assq 'action (car (cdr ps)))) "2")))
8982             (setq files (cons (cdr (assq 'name (car (cdr ps)))) files))
8983             (setcdr ps (cdr (cdr ps))))
8984           (if (not files)
8985               ()
8986             (if (not (string-match "%s" action))
8987                 (setq files (cons " " files)))
8988             (setq files (cons " " files))
8989             (and (assq 'execute (car ps))
8990                  (setcdr (assq 'execute (car ps))
8991                          (funcall (if (string-match "%s" action)
8992                                       'format 'concat)
8993                                   action 
8994                                   (mapconcat (lambda (f) f) files " ")))))
8995           (setq ps (cdr ps)))))
8996     (if gnus-view-pseudos
8997         (while pslist
8998           (and (assq 'execute (car pslist))
8999                (gnus-execute-command (cdr (assq 'execute (car pslist)))
9000                                      (eq gnus-view-pseudos 'not-confirm)))
9001           (setq pslist (cdr pslist)))
9002       (save-excursion
9003         (while pslist
9004           (gnus-summary-goto-subject (or (cdr (assq 'article (car pslist)))
9005                                          (gnus-summary-article-number)))
9006           (forward-line 1)
9007           (setq b (point))
9008           (insert "          " (file-name-nondirectory 
9009                                 (cdr (assq 'name (car pslist))))
9010                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9011           (add-text-properties 
9012            b (1+ b) (list 'gnus-subject (cdr (assq 'name (car pslist)))
9013                           'gnus-number gnus-reffed-article-number
9014                           'gnus-mark gnus-unread-mark
9015                           'gnus-pseudo (car pslist)
9016                           'gnus-thread 0))
9017           (forward-line -1)
9018           (gnus-sethash (int-to-string gnus-reffed-article-number)
9019                         (car pslist) gnus-newsgroup-headers-hashtb-by-number)
9020           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9021           (setq pslist (cdr pslist)))))))
9022
9023 (defun gnus-pseudos< (p1 p2)
9024   (let ((c1 (cdr (assq 'action p1)))
9025         (c2 (cdr (assq 'action p2))))
9026     (and c1 c2 (string< c1 c2))))
9027
9028 (defun gnus-request-pseudo-article (props)
9029   (cond ((assq 'execute props)
9030          (gnus-execute-command (cdr (assq 'execute props)))))
9031   (let ((gnus-current-article (gnus-summary-article-number)))
9032     (run-hooks 'gnus-mark-article-hook)))
9033
9034 (defun gnus-execute-command (command &optional automatic)
9035   (save-excursion
9036     (gnus-article-setup-buffer)
9037     (set-buffer gnus-article-buffer)
9038     (let ((command (if automatic command (read-string "Command: " command)))
9039           (buffer-read-only nil))
9040       (erase-buffer)
9041       (insert "$ " command "\n\n")
9042       (if gnus-view-pseudo-asynchronously
9043           (start-process "gnus-execute" nil "sh" "-c" command)
9044         (call-process "sh" nil t nil "-c" command)))))
9045
9046 (defun gnus-copy-file (file &optional to)
9047   "Copy FILE to TO."
9048   (interactive
9049    (list (read-file-name "Copy file: " default-directory)
9050          (read-file-name "Copy file to: " default-directory)))
9051   (or to (setq to (read-file-name "Copy file to: " default-directory)))
9052   (and (file-directory-p to) 
9053        (setq to (concat (file-name-as-directory to)
9054                         (file-name-nondirectory file))))
9055   (copy-file file to))
9056
9057 ;; Summary kill commands.
9058
9059 (defun gnus-summary-edit-global-kill (article)
9060   "Edit the \"global\" kill file."
9061   (interactive (list (gnus-summary-article-number)))
9062   (gnus-group-edit-global-kill article))
9063
9064 (defun gnus-summary-edit-local-kill ()
9065   "Edit a local kill file applied to the current newsgroup."
9066   (interactive)
9067   (setq gnus-current-headers 
9068         (gnus-gethash 
9069          (int-to-string (gnus-summary-article-number))
9070          gnus-newsgroup-headers-hashtb-by-number))
9071   (gnus-set-global-variables)
9072   (gnus-group-edit-local-kill 
9073    (gnus-summary-article-number) gnus-newsgroup-name))
9074
9075 \f
9076 ;;;
9077 ;;; Gnus article mode
9078 ;;;
9079
9080 (put 'gnus-article-mode 'mode-class 'special)
9081
9082 (if gnus-article-mode-map
9083     nil
9084   (setq gnus-article-mode-map (make-keymap))
9085   (suppress-keymap gnus-article-mode-map)
9086   (define-key gnus-article-mode-map " " 'gnus-article-next-page)
9087   (define-key gnus-article-mode-map "\177" 'gnus-article-prev-page)
9088   (define-key gnus-article-mode-map "\C-c^" 'gnus-article-refer-article)
9089   (define-key gnus-article-mode-map "h" 'gnus-article-show-summary)
9090   (define-key gnus-article-mode-map "s" 'gnus-article-show-summary)
9091   (define-key gnus-article-mode-map "\C-c\C-m" 'gnus-article-mail)
9092   (define-key gnus-article-mode-map "\C-c\C-M" 'gnus-article-mail-with-original)
9093   (define-key gnus-article-mode-map "?" 'gnus-article-describe-briefly)
9094   
9095   ;; Duplicate almost all summary keystrokes in the article mode map.
9096   (let ((commands 
9097          (list "#" "\M-#" "\C-c\M-#" "\r" "n" "p"
9098                "N" "P" "\M-\C-n" "\M-\C-p" "." "\M-s" "\M-r"
9099                "<" ">" "l" "j" "^" "\M-^" "-" "u" "U" "d" "D"
9100                "\M-u" "\M-U" "k" "\C-k" "\M-\C-k" "c" "x" "X" 
9101                "\M-\C-x" "\M-\177" "b" "B" "$" "w" "\C-c\C-r"
9102                "t" "\M-t" "C" "S"
9103                "m" "o" "\C-o" "|" "\M-m" "\M-\C-m" "\M-k" "M"
9104                "V" "\C-c\C-d")))
9105     (while commands
9106       (define-key gnus-article-mode-map (car commands) 
9107         'gnus-article-summary-command)
9108       (setq commands (cdr commands))))
9109
9110   (let ((commands (list "q" "Q" "r" "R" "\C-c\C-f" "m"  "a" "f" "F")))
9111     (while commands
9112       (define-key gnus-article-mode-map (car commands) 
9113         'gnus-article-summary-command-nosave)
9114       (setq commands (cdr commands)))))
9115
9116
9117 (defun gnus-article-mode ()
9118   "Major mode for displaying an article.
9119
9120 All normal editing commands are switched off.
9121
9122 The following commands are available:
9123
9124 \\<gnus-article-mode-map>
9125 \\[gnus-article-next-page]\t Scroll the article one page forwards
9126 \\[gnus-article-prev-page]\t Scroll the article one page backwards
9127 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
9128 \\[gnus-article-show-summary]\t Display the summary buffer
9129 \\[gnus-article-mail]\t Send a reply to the address near point
9130 \\[gnus-article-mail-with-original]\t Send a reply to the address near point; include the original article
9131 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
9132 \\[gnus-info-find-node]\t Go to the Gnus info node"
9133   (interactive)
9134   (if gnus-visual (gnus-article-make-menu-bar))
9135   (kill-all-local-variables)
9136   (setq mode-line-modified "-- ")
9137   (make-local-variable 'mode-line-format)
9138   (setq mode-line-format (copy-sequence mode-line-format))
9139   (and (equal (nth 3 mode-line-format) "   ")
9140        (setcar (nthcdr 3 mode-line-format) ""))
9141   (setq mode-name "Article")
9142   (setq major-mode 'gnus-article-mode)
9143   (make-local-variable 'minor-mode-alist)
9144   (or (assq 'gnus-show-mime minor-mode-alist)
9145       (setq minor-mode-alist
9146             (cons (list 'gnus-show-mime " MIME") minor-mode-alist)))
9147   (use-local-map gnus-article-mode-map)
9148   (make-local-variable 'page-delimiter)
9149   (setq page-delimiter gnus-page-delimiter)
9150   (buffer-disable-undo (current-buffer))
9151   (setq buffer-read-only t)             ;Disable modification
9152   (run-hooks 'gnus-article-mode-hook))
9153
9154 (defun gnus-article-setup-buffer ()
9155   "Initialize article mode buffer."
9156   (or (get-buffer gnus-article-buffer)
9157       (save-excursion
9158         (set-buffer (get-buffer-create gnus-article-buffer))
9159         (gnus-add-current-to-buffer-list)
9160         (gnus-article-mode))))
9161
9162 (defun gnus-request-article-this-buffer (article &optional group)
9163   "Get an article and insert it into this buffer."
9164   (setq group (or group gnus-newsgroup-name))
9165   ;; Using `gnus-request-article' directly will insert the article into
9166   ;; `nntp-server-buffer' - so we'll save some time by not having to
9167   ;; copy it from the server buffer into the article buffer.
9168
9169   ;; We only request an article by message-id when we do not have the
9170   ;; headers for it, so we'll have to get those.
9171   (and (stringp article) 
9172        (let ((gnus-override-method gnus-refer-article-method))
9173          (gnus-read-header article)))
9174
9175   ;; If the article number is negative, that means that this article
9176   ;; doesn't belong in this newsgroup (possibly), so we find its
9177   ;; message-id and request it by id instead of number.
9178   (if (and (numberp article) (< article 0))
9179       (save-excursion
9180         (set-buffer gnus-summary-buffer)
9181         (let ((header (gnus-gethash (int-to-string article)
9182                                     gnus-newsgroup-headers-hashtb-by-number)))
9183           (if (vectorp header)
9184               ;; It's a real article.
9185               (setq article (header-id header))
9186             ;; It is an extracted pseudo-article.
9187             (setq article nil)
9188             (gnus-request-pseudo-article header)))))
9189   ;; Get the article and into the article buffer.
9190   (if article
9191       (progn
9192        (erase-buffer)
9193        (let ((gnus-override-method 
9194               (and (stringp article) gnus-refer-article-method)))
9195          (and (gnus-request-article article group (current-buffer))
9196               'article)))
9197     'pseudo))
9198
9199 (defun gnus-read-header (id)
9200   "Read the headers of article ID and enter them into the Gnus system."
9201   (or gnus-newsgroup-headers-hashtb-by-number
9202       (gnus-make-headers-hashtable-by-number))
9203   (let (header)
9204     (if (not (setq header 
9205                    (car (if (let ((gnus-nov-is-evil t))
9206                               (gnus-retrieve-headers 
9207                                (list id) gnus-newsgroup-name))
9208                             (gnus-get-newsgroup-headers)))))
9209         nil
9210       (if (stringp id)
9211           (header-set-number header gnus-reffed-article-number))
9212       (setq gnus-newsgroup-headers (cons header gnus-newsgroup-headers))
9213       (gnus-sethash (int-to-string (header-number header)) header
9214                     gnus-newsgroup-headers-hashtb-by-number)
9215       (if (stringp id)
9216           (setq gnus-reffed-article-number (1- gnus-reffed-article-number)))
9217       (setq gnus-current-headers header)
9218       header)))
9219
9220 (defun gnus-article-prepare (article &optional all-headers header)
9221   "Prepare ARTICLE in article mode buffer.
9222 ARTICLE should either be an article number or a Message-ID.
9223 If ARTICLE is an id, HEADER should be the article headers.
9224 If ALL-HEADERS is non-nil, no headers are hidden."
9225   (save-excursion
9226     ;; Make sure we start in a summary buffer.
9227     (or (eq major-mode 'gnus-summary-mode)
9228         (set-buffer gnus-summary-buffer))
9229     (setq gnus-summary-buffer (current-buffer))
9230     ;; Make sure the connection to the server is alive.
9231     (or (gnus-server-opened (gnus-find-method-for-group gnus-newsgroup-name))
9232         (progn
9233           (gnus-check-news-server 
9234            (gnus-find-method-for-group gnus-newsgroup-name))
9235           (gnus-request-group gnus-newsgroup-name t)))
9236     (or gnus-newsgroup-headers-hashtb-by-number
9237         (gnus-make-headers-hashtable-by-number))
9238     (let* ((article (if header (header-number header) article))
9239            (summary-buffer (current-buffer))
9240            (internal-hook gnus-article-internal-prepare-hook)
9241            (bookmark (cdr (assq article gnus-newsgroup-bookmarks)))
9242            (group gnus-newsgroup-name)
9243            result)
9244       (save-excursion
9245         (gnus-article-setup-buffer)
9246         (set-buffer gnus-article-buffer)
9247         (let ((buffer-read-only nil))
9248           (if (not (setq result (gnus-request-article-this-buffer 
9249                                  article group)))
9250               ;; There is no such article.
9251               (progn
9252                 (save-excursion
9253                   (set-buffer gnus-summary-buffer)
9254                   (setq gnus-current-article 0)
9255                   (and (numberp article) 
9256                        (gnus-summary-mark-as-read article gnus-canceled-mark))
9257                   (message "No such article (may be canceled)")
9258                   (and (numberp article)
9259                        (setq gnus-current-article article))
9260                   (ding))
9261                 (and (numberp article)
9262                      (setq gnus-article-current 
9263                            (cons gnus-newsgroup-name article)))
9264                 nil)
9265             (if (not (eq result 'article))
9266                 (progn
9267                   (save-excursion
9268                     (set-buffer summary-buffer)
9269                     (setq gnus-last-article gnus-current-article
9270                           gnus-newsgroup-history (cons gnus-current-article
9271                                                        gnus-newsgroup-history)
9272                           gnus-current-article 0
9273                           gnus-current-headers nil
9274                           gnus-article-current nil)
9275                     (gnus-configure-windows 'article)
9276                     (gnus-set-global-variables))
9277                   (gnus-set-mode-line 'article))
9278               ;; The result from the `request' was an actual article -
9279               ;; or at least some text that is now displayed in the
9280               ;; article buffer.
9281               (if (and (numberp article)
9282                        (not (eq article gnus-current-article)))
9283                   ;; Seems like a new article has been selected.
9284                   ;; `gnus-current-article' must be an article number.
9285                   (save-excursion
9286                     (set-buffer summary-buffer)
9287                     (setq gnus-last-article gnus-current-article
9288                           gnus-newsgroup-history (cons gnus-current-article
9289                                                        gnus-newsgroup-history)
9290                           gnus-current-article article
9291                           gnus-current-headers 
9292                           (gnus-get-header-by-number gnus-current-article)
9293                           gnus-article-current 
9294                           (cons gnus-newsgroup-name gnus-current-article))
9295                     (run-hooks 'gnus-mark-article-hook)
9296                     (gnus-set-mode-line 'summary)
9297                     (and gnus-visual 
9298                          (run-hooks 'gnus-visual-mark-article-hook))
9299                     ;; Set the global newsgroup variables here.
9300                     ;; Suggested by Jim Sisolak
9301                     ;; <sisolak@trans4.neep.wisc.edu>.
9302                     (gnus-set-global-variables)))
9303               ;; gnus-have-all-headers must be either T or NIL.
9304               (setq gnus-have-all-headers
9305                     (not (not (or all-headers gnus-show-all-headers))))
9306               ;; Hooks for getting information from the article.
9307               ;; This hook must be called before being narrowed.
9308               (run-hooks 'internal-hook)
9309               (run-hooks 'gnus-article-prepare-hook)
9310               ;; Decode MIME message.
9311               (if (and gnus-show-mime
9312                        (gnus-fetch-field "Mime-Version"))
9313                   (funcall gnus-show-mime-method))
9314               ;; Perform the article display hooks.
9315               (let ((buffer-read-only nil))
9316                 (run-hooks 'gnus-article-display-hook))
9317               ;; Do page break.
9318               (goto-char (point-min))
9319               (and gnus-break-pages (gnus-narrow-to-page))
9320               (gnus-set-mode-line 'article)
9321               (gnus-configure-windows 'article)
9322               (goto-char 1)
9323               (set-window-start 
9324                (get-buffer-window gnus-article-buffer)
9325                (cond (bookmark
9326                       (message "Moved to bookmark")
9327                       (search-forward "\n\n" nil t)
9328                       (forward-line bookmark)
9329                       (point))
9330                      (t
9331                       (point-min))))
9332               t)))))))
9333
9334 (defun gnus-article-show-all-headers ()
9335   "Show all article headers in article mode buffer."
9336   (save-excursion 
9337     (setq gnus-have-all-headers t)
9338     (gnus-article-setup-buffer)
9339     (set-buffer gnus-article-buffer)
9340     (let ((buffer-read-only nil))
9341       (remove-text-properties 1 (point-max) '(invisible t)))))
9342
9343 (defun gnus-article-hide-headers-if-wanted ()
9344   "Hide unwanted headers if `gnus-have-all-headers' is nil.
9345 Provided for backwards compatability."
9346   (or gnus-have-all-headers
9347       (gnus-article-hide-headers)))
9348
9349 (defun gnus-article-hide-headers (&optional delete)
9350   "Hide unwanted headers and possibly sort them as well."
9351   (interactive "P")
9352   (save-excursion
9353     (set-buffer gnus-article-buffer)
9354     (save-restriction
9355       (let ((sorted gnus-sorted-header-list)
9356             (buffer-read-only nil)
9357             want want-list beg want-l)
9358         ;; First we narrow to just the headers.
9359         (widen)
9360         (goto-char 1)
9361         ;; Hide any "From " lines at the beginning of (mail) articles. 
9362         (while (looking-at "From ")
9363           (forward-line 1))
9364         (if (/= (point) 1) 
9365             (add-text-properties 1 (point) '(invisible t)))
9366         ;; Then treat the rest of the header lines.
9367         (narrow-to-region 
9368          (point) 
9369          (progn (search-forward "\n\n" nil t) (forward-line -1) (point)))
9370         ;; Then we use the two regular expressions
9371         ;; `gnus-ignored-headers' and `gnus-visible-headers' to
9372         ;; select which header lines is to remain visible in the
9373         ;; article buffer.
9374         (goto-char 1)
9375         (while (re-search-forward "^[^ \t]*:" nil t)
9376           (beginning-of-line)
9377           ;; We add the headers we want to keep to a list and delete
9378           ;; them from the buffer.
9379           (if (or (and (stringp gnus-visible-headers)
9380                        (looking-at gnus-visible-headers))
9381                   (and (not (stringp gnus-visible-headers))
9382                        (stringp gnus-ignored-headers)
9383                        (not (looking-at gnus-ignored-headers))))
9384               (progn
9385                 (setq beg (point))
9386                 (forward-line 1)
9387                 ;; Be sure to get multi-line headers...
9388                 (re-search-forward "^[^ \t]*:" nil t)
9389                 (beginning-of-line)
9390                 (setq want-list 
9391                       (cons (buffer-substring beg (point)) want-list))
9392                 (delete-region beg (point))
9393                 (goto-char beg))
9394             (forward-line 1)))
9395         ;; Next we perform the sorting by looking at
9396         ;; `gnus-sorted-header-list'. 
9397         (goto-char 1)
9398         (while (and sorted want-list)
9399           (setq want-l want-list)
9400           (while (and want-l
9401                       (not (string-match (car sorted) (car want-l))))
9402             (setq want-l (cdr want-l)))
9403           (if want-l 
9404               (progn
9405                 (insert (car want-l))
9406                 (setq want-list (delq (car want-l) want-list))))
9407           (setq sorted (cdr sorted)))
9408         ;; Any headers that were not matched by the sorted list we
9409         ;; just tack on the end of the visible header list.
9410         (while want-list
9411           (insert (car want-list))
9412           (setq want-list (cdr want-list)))
9413         ;; And finally we make the unwanted headers invisible.
9414         (if delete
9415             (delete-region (point) (point-max))
9416           ;; Suggested by Sudish Joseph <joseph@cis.ohio-state.edu>.
9417           (add-text-properties (point) (point-max) '(invisible t)))))))
9418
9419 (defun gnus-article-hide-signature ()
9420   "Hides the signature in an article.
9421 It does this by hiding everyting after \"^-- *$\", which is what all
9422 signatures should be preceded by. Note that this may mean that parts
9423 of an article may disappear if the article has such a line in the
9424 middle of the text."
9425   (interactive)
9426   (save-excursion
9427     (set-buffer gnus-article-buffer)
9428     (let ((buffer-read-only nil))
9429       (goto-char (point-max))
9430       (if (re-search-backward "^-- *$" nil t)
9431           (progn
9432             (add-text-properties (point) (point-max) '(invisible t)))))))
9433
9434 (defun gnus-article-hide-citation ()
9435   "Hide all cited text.
9436 This function uses the famous, extremely intelligent \"shoot in foot\"
9437 algorithm - which is simply deleting all lines that start with
9438 \">\". Your mileage may vary. If you come up with anything better,
9439 please do mail it to me."
9440   (interactive)
9441   (save-excursion
9442     (set-buffer gnus-article-buffer)
9443     (let ((buffer-read-only nil))
9444       (goto-char 1)
9445       (search-forward "\n\n" nil t)
9446       (while (not (eobp))
9447         (if (looking-at ">")
9448             (add-text-properties 
9449              (point) (save-excursion (forward-line 1) (point))
9450              '(invisible t)))
9451         (forward-line 1)))))
9452
9453 ;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.
9454 (defun gnus-article-treat-overstrike ()
9455   "Translate overstrikes into bold text."
9456   (interactive)
9457   (save-excursion
9458     (set-buffer gnus-article-buffer)
9459     (let ((buffer-read-only nil))
9460       (while (search-forward "\b" nil t)
9461         (let ((next (following-char))
9462               (previous (char-after (- (point) 2))))
9463           (cond ((eq next previous)
9464                  (delete-region (- (point) 2) (point))
9465                  (put-text-property (point) (1+ (point))
9466                                     'face 'bold))
9467                 ((eq next ?_)
9468                  (delete-region (1- (point)) (1+ (point)))
9469                  (put-text-property (1- (point)) (point)
9470                                     'face 'underline))
9471                 ((eq previous ?_)
9472                  (delete-region (- (point) 2) (point))
9473                  (put-text-property (point) (1+ (point))
9474                                     'face 'underline))))))))
9475
9476 (defun gnus-article-word-wrap ()
9477   "Format too long lines."
9478   (interactive)
9479   (save-excursion
9480     (set-buffer gnus-article-buffer)
9481     (let ((buffer-read-only nil))
9482       (goto-char 1)
9483       (search-forward "\n\n" nil t)
9484       (end-of-line 1)
9485       (let ((paragraph-start "^\\W"))
9486         (while (not (eobp))
9487           (and (>= (current-column) (window-width))
9488                (/= (preceding-char) ?:)
9489                (fill-paragraph nil))
9490           (end-of-line 2))))))
9491
9492 (defun gnus-article-remove-cr ()
9493   "Remove carriage returns from an article."
9494   (interactive)
9495   (save-excursion
9496     (set-buffer gnus-article-buffer)
9497     (let ((buffer-read-only nil))
9498       (goto-char (point-min))
9499       (while (search-forward "\r" nil t)
9500         (replace-match "" t t)))))
9501
9502 (defun gnus-article-display-x-face ()
9503   "Look for an X-Face header and display it if present."
9504   (interactive)
9505   (save-excursion
9506     (set-buffer gnus-article-buffer)
9507     (goto-char (point-min))
9508     (if (or (not gnus-article-x-face-command)
9509             (and (re-search-forward "^\\($\\|X-Face: \\)" nil t)
9510                  (looking-at "^$")))
9511         ()
9512       (let ((face
9513              (buffer-substring
9514               (point) 
9515               (progn
9516                 (forward-line 1)
9517                 (while (and (looking-at "[ \t]") 
9518                             (zerop (forward-line 1))))
9519                 (point))))
9520             (command gnus-article-x-face-command))
9521         (if (symbolp command)
9522             (and (or (fboundp command) (error "%s is not a function" command))
9523                  (funcall command face))
9524           (let ((process (start-process "gnus-x-face" nil "sh" "-c" command)))
9525             (if process
9526                 (progn
9527                   (process-send-string process face)
9528                   (process-send-eof process))
9529               (error "Couldn't start process"))))))))
9530               
9531
9532 (defun gnus-mime-decode-quoted-printable (from to)
9533   ;; Decode quoted-printable from region between FROM and TO.
9534   (save-excursion
9535     (goto-char from)
9536     (while (search-forward "=" to t)
9537       (cond ((eq (following-char) ?\n)
9538              (delete-char -1)
9539              (delete-char 1))
9540             ((looking-at "[0-9A-F][0-9A-F]")
9541              (delete-char -1)
9542              (insert (hexl-hex-string-to-integer
9543                       (buffer-substring (point) (+ 2 (point)))))
9544              (delete-char 2))
9545             ((message "Malformed MIME quoted-printable message"))))))
9546
9547 (defun gnus-article-de-quoted-unreadable ()
9548   "Do a naive translation of a quoted-printable-encoded article.
9549 This is in no way, shape or form meant as a replacement for real MIME
9550 processing, but is simply a stop-gap measure until MIME support is
9551 written."
9552   ;; Unquote quoted-printable from news articles.
9553   (interactive)
9554   (save-excursion
9555     (set-buffer gnus-article-buffer)
9556     (let ((case-fold-search t)
9557           (type (gnus-fetch-field "content-transfer-encoding")))
9558       (cond ((and (stringp type) (string-match "quoted-printable" type))
9559              (goto-char (point-min))
9560              (search-forward "\n\n" nil 'move)
9561              (message  "MIME Unquoting printable...")
9562              (gnus-mime-decode-quoted-printable (point) (point-max))
9563              (message  "MIME Unquoting printable...done")))
9564       (set-buffer gnus-summary-buffer))))
9565
9566 (defun gnus-article-date-ut (date type)
9567   "Convert DATE date to universal time in the current article.
9568 If TYPE is `local', convert to local time; if it is `lapsed', output
9569 how much time has lapsed since DATE."
9570   (interactive (list (header-date gnus-current-headers) 'ut))
9571   (if (not date)
9572       ()
9573     (save-excursion
9574       (set-buffer gnus-article-buffer)
9575       (let ((buffer-read-only nil))
9576         (goto-char (point-min))
9577         (if (re-search-forward "^Date: \\|^X-Sent: " nil t)
9578             (delete-region (gnus-point-at-bol)
9579                            (progn (forward-line 1) (point)))
9580           (- (search-forward "\n\n") 2))
9581         (insert
9582          (cond 
9583           ((eq type 'local)
9584            (concat "Date: " (timezone-make-date-arpa-standard date) "\n"))
9585           ((eq type 'ut)
9586            (concat "Date: " (timezone-make-date-arpa-standard date nil "UT")
9587                    "\n"))
9588           ((eq type 'lapsed)
9589            (let* ((sec (- (gnus-seconds-since-epoch (current-time-string))
9590                           (gnus-seconds-since-epoch date)))
9591                   (units (list (cons 'year (* 1.0 365 24 60 60))
9592                                (cons 'week (* 7 24 60 60))
9593                                (cons 'day (* 24 60 60))
9594                                (cons 'hour (* 60 60))
9595                                (cons 'minute 60)
9596                                (cons 'second 1)))
9597                   num prev)
9598              (concat
9599               "X-Sent: "
9600               (mapconcat 
9601                (lambda (unit)
9602                  (if (zerop (setq num (floor (/ sec (cdr unit)))))
9603                      ""
9604                    (setq sec (- sec (* num (cdr unit))))
9605                    (prog1
9606                        (concat (if prev ", " "") (int-to-string num)
9607                                " " (symbol-name (car unit))
9608                                (if (> num 1) "s" ""))
9609                      (setq prev t))))
9610                units "")
9611               " ago\n")))
9612           (t
9613            (error "Unknown conversion type: %s" type))))))))
9614
9615 (defun gnus-article-date-local (date)
9616   "Convert the current article date to the local timezone."
9617   (interactive (list (header-date gnus-current-headers)))
9618   (gnus-article-date-ut date 'local))
9619
9620 (defun gnus-article-date-lapsed (date)
9621   "Convert the current article date to time lapsed since it was sent."
9622   (interactive (list (header-date gnus-current-headers)))
9623   (gnus-article-date-ut date 'lapsed))
9624
9625
9626 ;; Article savers.
9627
9628 (defun gnus-output-to-rmail (file-name)
9629   "Append the current article to an Rmail file named FILE-NAME."
9630   (require 'rmail)
9631   ;; Most of these codes are borrowed from rmailout.el.
9632   (setq file-name (expand-file-name file-name))
9633   (setq rmail-default-rmail-file file-name)
9634   (let ((artbuf (current-buffer))
9635         (tmpbuf (get-buffer-create " *Gnus-output*")))
9636     (save-excursion
9637       (or (get-file-buffer file-name)
9638           (file-exists-p file-name)
9639           (if (gnus-yes-or-no-p
9640                (concat "\"" file-name "\" does not exist, create it? "))
9641               (let ((file-buffer (create-file-buffer file-name)))
9642                 (save-excursion
9643                   (set-buffer file-buffer)
9644                   (rmail-insert-rmail-file-header)
9645                   (let ((require-final-newline nil))
9646                     (write-region (point-min) (point-max) file-name t 1)))
9647                 (kill-buffer file-buffer))
9648             (error "Output file does not exist")))
9649       (set-buffer tmpbuf)
9650       (buffer-disable-undo (current-buffer))
9651       (erase-buffer)
9652       (insert-buffer-substring artbuf)
9653       (gnus-convert-article-to-rmail)
9654       ;; Decide whether to append to a file or to an Emacs buffer.
9655       (let ((outbuf (get-file-buffer file-name)))
9656         (if (not outbuf)
9657             (append-to-file (point-min) (point-max) file-name)
9658           ;; File has been visited, in buffer OUTBUF.
9659           (set-buffer outbuf)
9660           (let ((buffer-read-only nil)
9661                 (msg (and (boundp 'rmail-current-message)
9662                           (symbol-value 'rmail-current-message))))
9663             ;; If MSG is non-nil, buffer is in RMAIL mode.
9664             (if msg
9665                 (progn (widen)
9666                        (narrow-to-region (point-max) (point-max))))
9667             (insert-buffer-substring tmpbuf)
9668             (if msg
9669                 (progn
9670                   (goto-char (point-min))
9671                   (widen)
9672                   (search-backward "\^_")
9673                   (narrow-to-region (point) (point-max))
9674                   (goto-char (1+ (point-min)))
9675                   (rmail-count-new-messages t)
9676                   (rmail-show-message msg)))))))
9677     (kill-buffer tmpbuf)))
9678
9679 (defun gnus-output-to-file (file-name)
9680   "Append the current article to a file named FILE-NAME."
9681   (setq file-name (expand-file-name file-name))
9682   (let ((artbuf (current-buffer))
9683         (tmpbuf (get-buffer-create " *Gnus-output*")))
9684     (save-excursion
9685       (set-buffer tmpbuf)
9686       (buffer-disable-undo (current-buffer))
9687       (erase-buffer)
9688       (insert-buffer-substring artbuf)
9689       ;; Append newline at end of the buffer as separator, and then
9690       ;; save it to file.
9691       (goto-char (point-max))
9692       (insert "\n")
9693       (append-to-file (point-min) (point-max) file-name))
9694     (kill-buffer tmpbuf)))
9695
9696 (defun gnus-convert-article-to-rmail ()
9697   "Convert article in current buffer to Rmail message format."
9698   (let ((buffer-read-only nil))
9699     ;; Convert article directly into Babyl format.
9700     ;; Suggested by Rob Austein <sra@lcs.mit.edu>
9701     (goto-char (point-min))
9702     (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
9703     (while (search-forward "\n\^_" nil t) ;single char
9704       (replace-match "\n^_" t t))               ;2 chars: "^" and "_"
9705     (goto-char (point-max))
9706     (insert "\^_")))
9707
9708 (defun gnus-narrow-to-page (&optional arg)
9709   "Make text outside current page invisible except for page delimiter.
9710 A numeric arg specifies to move forward or backward by that many pages,
9711 thus showing a page other than the one point was originally in."
9712   (interactive "P")
9713   (setq arg (if arg (prefix-numeric-value arg) 0))
9714   (save-excursion
9715     (forward-page -1)                   ;Beginning of current page.
9716     (widen)
9717     (if (> arg 0)
9718         (forward-page arg)
9719       (if (< arg 0)
9720           (forward-page (1- arg))))
9721     ;; Find the end of the page.
9722     (forward-page)
9723     ;; If we stopped due to end of buffer, stay there.
9724     ;; If we stopped after a page delimiter, put end of restriction
9725     ;; at the beginning of that line.
9726     ;; These are commented out.
9727     ;;    (if (save-excursion (beginning-of-line)
9728     ;;                  (looking-at page-delimiter))
9729     ;;  (beginning-of-line))
9730     (narrow-to-region (point)
9731                       (progn
9732                         ;; Find the top of the page.
9733                         (forward-page -1)
9734                         ;; If we found beginning of buffer, stay there.
9735                         ;; If extra text follows page delimiter on same line,
9736                         ;; include it.
9737                         ;; Otherwise, show text starting with following line.
9738                         (if (and (eolp) (not (bobp)))
9739                             (forward-line 1))
9740                         (point)))))
9741
9742 (defun gnus-gmt-to-local ()
9743   "Rewrite Date header described in GMT to local in current buffer.
9744 Intended to be used with gnus-article-prepare-hook."
9745   (save-excursion
9746     (save-restriction
9747       (widen)
9748       (goto-char (point-min))
9749       (narrow-to-region (point-min)
9750                         (progn (search-forward "\n\n" nil 'move) (point)))
9751       (goto-char (point-min))
9752       (if (re-search-forward "^Date:[ \t]\\(.*\\)$" nil t)
9753           (let ((buffer-read-only nil)
9754                 (date (buffer-substring (match-beginning 1) (match-end 1))))
9755             (delete-region (match-beginning 1) (match-end 1))
9756             (insert
9757              (timezone-make-date-arpa-standard 
9758               date nil (current-time-zone))))))))
9759
9760
9761 ;; Article mode commands
9762
9763 (defun gnus-article-next-page (lines)
9764   "Show next page of current article.
9765 If end of article, return non-nil. Otherwise return nil.
9766 Argument LINES specifies lines to be scrolled up."
9767   (interactive "P")
9768   (move-to-window-line -1)
9769   ;; Fixed by enami@ptgd.sony.co.jp (enami tsugutomo)
9770   (if (save-excursion
9771         (end-of-line)
9772         (and (pos-visible-in-window-p)  ;Not continuation line.
9773              (eobp)))
9774       ;; Nothing in this page.
9775       (if (or (not gnus-break-pages)
9776               (save-excursion
9777                 (save-restriction
9778                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
9779           t                             ;Nothing more.
9780         (gnus-narrow-to-page 1)         ;Go to next page.
9781         nil)
9782     ;; More in this page.
9783     (condition-case ()
9784         (scroll-up lines)
9785       (end-of-buffer
9786        ;; Long lines may cause an end-of-buffer error.
9787        (goto-char (point-max))))
9788     nil))
9789
9790 (defun gnus-article-prev-page (lines)
9791   "Show previous page of current article.
9792 Argument LINES specifies lines to be scrolled down."
9793   (interactive "P")
9794   (move-to-window-line 0)
9795   (if (and gnus-break-pages
9796            (bobp)
9797            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
9798       (progn
9799         (gnus-narrow-to-page -1) ;Go to previous page.
9800         (goto-char (point-max))
9801         (recenter -1))
9802     (scroll-down lines)))
9803
9804 (defun gnus-article-refer-article ()
9805   "Read article specified by message-id around point."
9806   (interactive)
9807   (search-forward ">" nil t)    ;Move point to end of "<....>".
9808   (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
9809       (let ((message-id
9810              (buffer-substring (match-beginning 1) (match-end 1))))
9811         (set-buffer gnus-summary-buffer)
9812         (gnus-summary-refer-article message-id))
9813     (error "No references around point")))
9814
9815 (defun gnus-article-mail (yank)
9816   "Send a reply to the address near point.
9817 If YANK is non-nil, include the original article."
9818   (interactive "P")
9819   (let ((address 
9820          (buffer-substring
9821           (save-excursion (re-search-backward "[ \t\n]" nil t) (1+ (point)))
9822           (save-excursion (re-search-forward "[ \t\n]" nil t) (1- (point))))))
9823     (and address
9824          (progn
9825            (switch-to-buffer gnus-summary-buffer)
9826            (funcall gnus-mail-reply-method yank address)))))
9827
9828 (defun gnus-article-mail-with-original ()
9829   "Send a reply to the address near point and include the original article."
9830   (interactive)
9831   (gnus-article-mail 'yank))
9832
9833 (defun gnus-article-show-summary ()
9834   "Reconfigure windows to show summary buffer."
9835   (interactive)
9836   (gnus-configure-windows 'article)
9837   (pop-to-buffer gnus-summary-buffer)
9838   (gnus-summary-goto-subject gnus-current-article))
9839
9840 (defun gnus-article-describe-briefly ()
9841   "Describe article mode commands briefly."
9842   (interactive)
9843   (message
9844    (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")))
9845
9846 (defun gnus-article-summary-command ()
9847   "Execute the last keystroke in the summary buffer."
9848   (interactive)
9849   (let ((obuf (current-buffer))
9850         (owin (current-window-configuration)))
9851     (switch-to-buffer gnus-summary-buffer 'norecord)
9852     (execute-kbd-macro (this-command-keys))
9853     (set-buffer obuf)
9854     (let ((npoint (point)))
9855       (set-window-configuration owin)
9856       (set-window-start (get-buffer-window (current-buffer)) (point)))))
9857
9858 (defun gnus-article-summary-command-nosave ()
9859   "Execute the last keystroke in the summary buffer."
9860   (interactive)
9861   (let ((obuf (current-buffer))
9862         (owin (current-window-configuration)))
9863     (switch-to-buffer gnus-summary-buffer 'norecord)
9864     (execute-kbd-macro (this-command-keys))))
9865
9866 ;; caesar-region written by phr@prep.ai.mit.edu  Nov 86
9867 ;; Modified by tower@prep Nov 86
9868 ;; Modified by umerin@flab.flab.Fujitsu.JUNET for ROT47.
9869
9870 (defun gnus-caesar-region (&optional n)
9871   "Caesar rotation of region by N, default 13, for decrypting netnews.
9872 ROT47 will be performed for Japanese text in any case."
9873   (interactive (if current-prefix-arg   ; Was there a prefix arg?
9874                    (list (prefix-numeric-value current-prefix-arg))
9875                  (list nil)))
9876   (cond ((not (numberp n)) (setq n 13))
9877         (t (setq n (mod n 26))))        ;canonicalize N
9878   (if (not (zerop n))           ; no action needed for a rot of 0
9879       (progn
9880         (if (or (not (boundp 'caesar-translate-table))
9881                 (not caesar-translate-table)
9882                 (/= (aref caesar-translate-table ?a) (+ ?a n)))
9883             (let ((i 0) 
9884                   (lower "abcdefghijklmnopqrstuvwxyz")
9885                   upper)
9886               (message "Building caesar-translate-table...")
9887               (setq caesar-translate-table (make-vector 256 0))
9888               (while (< i 256)
9889                 (aset caesar-translate-table i i)
9890                 (setq i (1+ i)))
9891               (setq lower (concat lower lower)
9892                     upper (upcase lower)
9893                     i 0)
9894               (while (< i 26)
9895                 (aset caesar-translate-table (+ ?a i) (aref lower (+ i n)))
9896                 (aset caesar-translate-table (+ ?A i) (aref upper (+ i n)))
9897                 (setq i (1+ i)))
9898               ;; ROT47 for Japanese text.
9899               ;; Thanks to ichikawa@flab.fujitsu.junet.
9900               (setq i 161)
9901               (let ((t1 (logior ?O 128))
9902                     (t2 (logior ?! 128))
9903                     (t3 (logior ?~ 128)))
9904                 (while (< i 256)
9905                   (aset caesar-translate-table i
9906                         (let ((v (aref caesar-translate-table i)))
9907                           (if (<= v t1) (if (< v t2) v (+ v 47))
9908                             (if (<= v t3) (- v 47) v))))
9909                   (setq i (1+ i))))
9910               (message "Building caesar-translate-table... done")))
9911         (let ((from (region-beginning))
9912               (to (region-end))
9913               (i 0) str len)
9914           (setq str (buffer-substring from to))
9915           (setq len (length str))
9916           (while (< i len)
9917             (aset str i (aref caesar-translate-table (aref str i)))
9918             (setq i (1+ i)))
9919           (goto-char from)
9920           (delete-region from to)
9921           (insert str)))))
9922
9923 \f
9924 ;; Basic ideas by emv@math.lsa.umich.edu (Edward Vielmetti)
9925
9926 ;;;###autoload
9927 (defalias 'gnus-batch-kill 'gnus-batch-score)
9928 ;;;###autoload
9929 (defun gnus-batch-score ()
9930   "Run batched scoring.
9931 Usage: emacs -batch -l gnus -f gnus-batch-score <newsgroups> ...
9932 Newsgroups is a list of strings in Bnews format.  If you want to score
9933 the comp hierarchy, you'd say \"comp.all\". If you would not like to
9934 score the alt hierarchy, you'd say \"!alt.all\"."
9935   (interactive)
9936   (let* ((yes-and-no
9937           (gnus-parse-n-options
9938            (apply (function concat)
9939                   (mapcar (lambda (g) (concat g " "))
9940                           command-line-args-left))))
9941          (gnus-expert-user t)
9942          (nnmail-spool-file nil)
9943          (gnus-use-dribble-file nil)
9944          (yes (car yes-and-no))
9945          (no (cdr yes-and-no))
9946          group subscribed newsrc entry
9947          ;; Disable verbose message.
9948          gnus-novice-user gnus-large-newsgroup)
9949     ;; Eat all arguments.
9950     (setq command-line-args-left nil)
9951     ;; Start Gnus.
9952     (gnus)
9953     ;; Apply kills to specified newsgroups in command line arguments.
9954     (setq newsrc (cdr gnus-newsrc-alist))
9955     (while newsrc
9956       (setq group (car (car newsrc)))
9957       (setq entry (gnus-gethash group gnus-newsrc-hashtb))
9958       (if (and (<= (nth 1 (car newsrc)) gnus-level-subscribed)
9959                (and (car entry)
9960                     (or (eq (car entry) t)
9961                         (not (zerop (car entry)))))
9962                (if yes (string-match yes group) t)
9963                (or (null no) (not (string-match no group))))
9964           (progn
9965             (gnus-summary-read-group group nil t)
9966             (and (eq (current-buffer) (get-buffer gnus-summary-buffer))
9967                  (gnus-summary-exit))))
9968       (setq newsrc (cdr newsrc)))
9969     ;; Exit Emacs.
9970     (switch-to-buffer gnus-group-buffer)
9971     (gnus-group-save-newsrc)))
9972
9973 (defun gnus-apply-kill-file ()
9974   "Apply a kill file to the current newsgroup.
9975 Returns the number of articles marked as read."
9976   (if (or (file-exists-p (gnus-newsgroup-kill-file nil))
9977           (file-exists-p (gnus-newsgroup-kill-file gnus-newsgroup-name)))
9978       (gnus-apply-kill-file-internal)
9979     0))
9980
9981 (defun gnus-kill-save-kill-buffer ()
9982   (save-excursion
9983     (let ((file (gnus-newsgroup-kill-file gnus-newsgroup-name)))
9984       (if (get-file-buffer file)
9985           (progn
9986             (set-buffer (get-file-buffer file))
9987             (and (buffer-modified-p) (save-buffer))
9988             (kill-buffer (current-buffer)))))))
9989
9990 (defvar gnus-kill-file-name "KILL"
9991   "Suffix of the kill files.")
9992
9993 (defun gnus-newsgroup-kill-file (newsgroup)
9994   "Return the name of a kill file name for NEWSGROUP.
9995 If NEWSGROUP is nil, return the global kill file name instead."
9996   (cond ((or (null newsgroup)
9997              (string-equal newsgroup ""))
9998          ;; The global KILL file is placed at top of the directory.
9999          (expand-file-name gnus-kill-file-name
10000                            (or gnus-kill-files-directory "~/News")))
10001         ((gnus-use-long-file-name 'not-kill)
10002          ;; Append ".KILL" to newsgroup name.
10003          (expand-file-name (concat newsgroup "." gnus-kill-file-name)
10004                            (or gnus-kill-files-directory "~/News")))
10005         (t
10006          ;; Place "KILL" under the hierarchical directory.
10007          (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
10008                                    "/" gnus-kill-file-name)
10009                            (or gnus-kill-files-directory "~/News")))))
10010
10011 \f
10012 ;;;
10013 ;;; Dribble file
10014 ;;;
10015
10016 (defvar gnus-dribble-ignore nil)
10017
10018 (defun gnus-dribble-file-name ()
10019   (concat gnus-startup-file "-dribble"))
10020
10021 (defun gnus-dribble-open ()
10022   (save-excursion 
10023     (set-buffer 
10024      (setq gnus-dribble-buffer (find-file-noselect (gnus-dribble-file-name))))
10025     (buffer-disable-undo (current-buffer))
10026     (bury-buffer gnus-dribble-buffer)
10027     (auto-save-mode t)
10028     (goto-char (point-max))))
10029
10030 (defun gnus-dribble-enter (string)
10031   (if (and (not gnus-dribble-ignore)
10032            gnus-dribble-buffer
10033            (buffer-name gnus-dribble-buffer))
10034       (let ((obuf (current-buffer)))
10035         (set-buffer gnus-dribble-buffer)
10036         (insert string "\n")
10037         (set-window-point (get-buffer-window (current-buffer)) (point-max))
10038         (set-buffer obuf))))
10039
10040 (defun gnus-dribble-read-file ()
10041   (let ((dribble-file (gnus-dribble-file-name)))
10042     (save-excursion 
10043       (set-buffer (setq gnus-dribble-buffer 
10044                         (get-buffer-create 
10045                          (file-name-nondirectory dribble-file))))
10046       (gnus-add-current-to-buffer-list)
10047       (erase-buffer)
10048       (set-visited-file-name dribble-file)
10049       (buffer-disable-undo (current-buffer))
10050       (bury-buffer (current-buffer))
10051       (set-buffer-modified-p nil)
10052       (let ((auto (make-auto-save-file-name))
10053             (gnus-dribble-ignore t))
10054         (if (or (file-exists-p auto) (file-exists-p dribble-file))
10055             (progn
10056               (if (file-newer-than-file-p auto dribble-file)
10057                   (setq dribble-file auto))
10058               (insert-file-contents dribble-file)
10059               (if (not (zerop (buffer-size)))
10060                   (set-buffer-modified-p t))
10061               (if (gnus-y-or-n-p 
10062                    "Auto-save file exists. Do you want to read it? ")
10063                   (progn
10064                     (message "Reading %s..." dribble-file) 
10065                     (eval-current-buffer)
10066                     (message "Reading %s...done" dribble-file)))))))))
10067
10068 (defun gnus-dribble-delete-file ()
10069   (if (file-exists-p (gnus-dribble-file-name))
10070       (delete-file (gnus-dribble-file-name)))
10071   (if gnus-dribble-buffer
10072       (save-excursion
10073         (set-buffer gnus-dribble-buffer)
10074         (let ((auto (make-auto-save-file-name)))
10075           (if (file-exists-p auto)
10076               (delete-file auto))
10077           (erase-buffer)
10078           (set-buffer-modified-p nil)))))
10079
10080 (defun gnus-dribble-save ()
10081   (if (and gnus-dribble-buffer
10082            (buffer-name gnus-dribble-buffer))
10083       (save-excursion
10084         (set-buffer gnus-dribble-buffer)
10085         (save-buffer))))
10086
10087 (defun gnus-dribble-clear ()
10088   (save-excursion
10089     (if (gnus-buffer-exists-p gnus-dribble-buffer)
10090         (progn
10091           (set-buffer gnus-dribble-buffer)
10092           (erase-buffer)
10093           (set-buffer-modified-p nil)
10094           (setq buffer-saved-size (buffer-size))))))
10095
10096 ;;;
10097 ;;; Server Communication
10098 ;;;
10099
10100 ;; All the Gnus backends have the same interface, and should return
10101 ;; data in a similar format. Below is an overview of what functions
10102 ;; these packages must supply and what results they should return.
10103 ;;
10104 ;; Variables:
10105 ;;
10106 ;; `nntp-server-buffer' - All data should be returned to Gnus in this
10107 ;; buffer. 
10108 ;;
10109 ;; Functions for the imaginary backend `choke':
10110 ;;
10111 ;; `choke-retrieve-headers ARTICLES &optional GROUP SERVER'
10112 ;; Should return all headers for all ARTICLES, or return NOV lines for
10113 ;; the same.
10114 ;;
10115 ;; `choke-request-group GROUP &optional SERVER DISCARD'
10116 ;; Switch to GROUP. If DISCARD is nil, active information on the group
10117 ;; must be returned.
10118 ;;
10119 ;; `choke-close-group GROUP &optional SERVER'
10120 ;; Close group. Most backends won't have to do anything with this
10121 ;; call, but it is an opportunity to clean up, if that is needed. It
10122 ;; is called when Gnus exits a group.
10123 ;;
10124 ;; `choke-request-article ARTICLE &optional GROUP SERVER'
10125 ;; Return ARTICLE, which is either an article number or
10126 ;; message-id. Note that not all backends can return articles based on
10127 ;; message-id. 
10128 ;;
10129 ;; `choke-request-list SERVER'
10130 ;; Return a list of all newsgroups on SERVER.
10131 ;;
10132 ;; `choke-request-list-newsgroups SERVER'
10133 ;; Return a list of descriptions of all newsgroups on SERVER.
10134 ;;
10135 ;; `choke-request-newgroups DATE &optional SERVER'
10136 ;; Return a list of all groups that have arrived after DATE on
10137 ;; SERVER. Note that the date doesn't have to be respected - Gnus will
10138 ;; always check whether the groups are old or not. Backends that do
10139 ;; not store date information may just return the entire list of
10140 ;; groups, although this might not be a good idea in general.
10141 ;;
10142 ;; `choke-request-post-buffer METHOD HEADER ARTICLE-BUFFER GROUP INFO'
10143 ;; Should return a buffer that is suitable for "posting". nnspool and
10144 ;; nntp return a `*post-buffer*', and nnmail return a `*mail*'
10145 ;; buffer. This function should fill out the appropriate headers. 
10146 ;;
10147 ;; `choke-request-post &optional SERVER'
10148 ;; Function that will be called from a buffer to be posted. 
10149 ;;
10150 ;; `choke-open-server SERVER &optional ARGUMENT'
10151 ;; Open a connection to SERVER.
10152 ;;
10153 ;; `choke-close-server &optional SERVER'
10154 ;; Close the connection to SERVER.
10155 ;;
10156 ;; `choke-server-opened &optional SERVER'
10157 ;; Whether the conenction to SERVER is opened or not.
10158 ;;
10159 ;; `choke-server-status &optional SERVER'
10160 ;; Should return a status string (not in the nntp buffer, but as the
10161 ;; result of the function).
10162 ;;
10163 ;; `choke-retrieve-groups GROUPS &optional SERVER'
10164 ;; Optional function for retrieving active file info on all groups in
10165 ;; GROUPS.  Two return formats are supported: The normal active file
10166 ;; format, and a list of GROUP lines.  This function should return (as
10167 ;; a function value) either `active' or `group', depending on what
10168 ;; format it returns.
10169 ;;
10170 ;; The following functions are optional and apply only to backends
10171 ;; that are able to control the contents of their groups totally
10172 ;; (ie. mail backends.)  Backends that aren't able to do that
10173 ;; shouldn't define these functions at all. Gnus will check for their
10174 ;; presence before attempting to call them.
10175 ;;
10176 ;; `choke-request-expire-articles ARTICLES &optional NEWSGROUP SERVER'
10177 ;; Should expire (according to some aging scheme) all ARTICLES. Most
10178 ;; backends will not be able to expire articles. Should return a list
10179 ;; of all articles that were not expired.
10180 ;;
10181 ;; `choke-request-move-article ARTICLE GROUP SERVER ACCEPT-FORM &optional LAST'
10182 ;; Should move ARTICLE from GROUP on SERVER by using ACCEPT-FORM.
10183 ;; Removes any information it has added to the article (extra headers,
10184 ;; whatever - make it as clean as possible), and then passes the
10185 ;; article on by evaling ACCEPT-FORM, which is normally a call to the
10186 ;; function described below. If the ACCEPT-FORM returns a non-nil
10187 ;; value, the article should then be deleted. If LAST is nil, that
10188 ;; means that there will be further calls to this function. This might
10189 ;; be taken as an advice not to save buffers/internal variables just
10190 ;; yet, but wait until the last call to speed things up.
10191 ;;
10192 ;; `choke-request-accept-article GROUP &optional LAST' 
10193 ;; The contents of the current buffer will be put into GROUP.  There
10194 ;; should, of course, be an article in the current buffer.  This
10195 ;; function is normally only called by the function described above,
10196 ;; and LAST works the same way as in that function.
10197 ;;
10198 ;; `choke-request-replace-article ARTICLE GROUP BUFFER'
10199 ;; Replace ARTICLE in GROUP with the contents of BUFFER.
10200 ;; This provides an easy interface for allowing editing of
10201 ;; articles. Note that even headers may be edited, so the backend has
10202 ;; to update any tables (nov buffers, etc) that it maintains after
10203 ;; replacing the article.
10204 ;;
10205 ;; `choke-request-create-group GROUP &optional SERVER'
10206 ;; Create GROUP on SERVER.  This might be a new, empty group, or it
10207 ;; might be a group that already exists, but hasn't been registered
10208 ;; yet. 
10209 ;;
10210 ;; All these functions must return nil if they couldn't service the
10211 ;; request. If the optional arguments are not supplied, some "current"
10212 ;; or "default" values should be used. In short, one should emulate an
10213 ;; NNTP server, in a way.
10214 ;;
10215 ;; If you want to write a new backend, you just have to supply the
10216 ;; functions listed above. In addition, you must enter the new backend
10217 ;; into the list of valid select methods:
10218 ;; (setq gnus-valid-select-methods 
10219 ;;       (cons '("choke" mail) gnus-valid-select-methods))
10220 ;; The first element in this list is the name of the backend. Other
10221 ;; elemnets may be `mail' (for mail groups),  `post' (for news
10222 ;; groups), `none' (neither), `respool' (for groups that can control
10223 ;; their contents). 
10224
10225 (defun gnus-start-news-server (&optional confirm)
10226   "Open a method for getting news.
10227 If CONFIRM is non-nil, the user will be asked for an NNTP server."
10228   (let (how where)
10229     (if gnus-current-select-method
10230         ;; Stream is already opened.
10231         nil
10232       ;; Open NNTP server.
10233       (if (null gnus-nntp-service) (setq gnus-nntp-server nil))
10234       (if confirm
10235           (progn
10236             ;; Read server name with completion.
10237             (setq gnus-nntp-server
10238                   (completing-read "NNTP server: "
10239                                    (mapcar (lambda (server) (list server))
10240                                            (cons (list gnus-nntp-server)
10241                                                  gnus-secondary-servers))
10242                                    nil nil gnus-nntp-server))))
10243
10244       (if (and gnus-nntp-server 
10245                (stringp gnus-nntp-server)
10246                (not (string= gnus-nntp-server "")))
10247           (setq gnus-select-method
10248                 (cond ((or (string= gnus-nntp-server "")
10249                            (string= gnus-nntp-server "::"))
10250                        (list 'nnspool (system-name)))
10251                       ((string-match "^:" gnus-nntp-server)
10252                        (list 'nnmh gnus-nntp-server 
10253                              (list 'nnmh-directory 
10254                                    (file-name-as-directory
10255                                     (expand-file-name
10256                                      (concat "~/" (substring
10257                                                    gnus-nntp-server 1)))))))
10258                       (t
10259                        (list 'nntp gnus-nntp-server)))))
10260
10261       (setq how (car gnus-select-method))
10262       (setq where (car (cdr gnus-select-method)))
10263       (cond ((eq how 'nnspool)
10264              (require 'nnspool)
10265              (message "Looking up local news spool..."))
10266             ((eq how 'nnmh)
10267              (require 'nnmh)
10268              (message "Looking up mh spool..."))
10269             (t
10270              (require 'nntp)))
10271       (setq gnus-current-select-method gnus-select-method)
10272       (run-hooks 'gnus-open-server-hook)
10273       (or 
10274        ;; gnus-open-server-hook might have opened it
10275        (gnus-server-opened gnus-select-method)  
10276        (gnus-open-server gnus-select-method)
10277        (gnus-y-or-n-p
10278         (format
10279          "%s server on %s can't be opened. Continue? "
10280          (car gnus-select-method) (nth 1 gnus-select-method)))
10281        (progn
10282          (message "Couldn't open server on %s" (nth 1 gnus-select-method))
10283          (ding)
10284          nil)))))
10285
10286 (defun gnus-check-news-server (method)
10287   "If the news server is down, start it up again."
10288   (let ((method (if method method gnus-select-method)))
10289     (and (stringp method)
10290          (setq method (gnus-server-to-method method)))
10291     (if (gnus-server-opened method)
10292         ;; Stream is already opened.
10293         t
10294       ;; Open server.
10295       (message "Opening server %s on %s..." (car method) (nth 1 method))
10296       (run-hooks 'gnus-open-server-hook)
10297       (or (gnus-server-opened method)
10298           (gnus-open-server method))
10299       (message ""))))
10300
10301 (defun gnus-nntp-message (&optional message)
10302   "Check the status of the NNTP server.
10303 If the status of the server is clear and MESSAGE is non-nil, MESSAGE
10304 is returned insted of the status string."
10305   (let ((status (gnus-status-message (gnus-find-method-for-group 
10306                                       gnus-newsgroup-name)))
10307         (message (or message "")))
10308     (if (and (stringp status) (> (length status) 0))
10309         status message)))
10310
10311 (defun gnus-get-function (method function)
10312   (and (stringp method)
10313        (setq method (gnus-server-to-method method)))
10314   (let ((func (intern (format "%s-%s" (car method) function))))
10315     (if (not (fboundp func)) 
10316         (progn
10317           (require (car method))
10318           (if (not (fboundp func)) 
10319               (error "No such function: %s" func))))
10320     func))
10321
10322 ;;; Interface functions to the backends.
10323
10324 (defun gnus-open-server (method)
10325   (funcall (gnus-get-function method 'open-server)
10326            (nth 1 method) (nthcdr 2 method)))
10327
10328 (defun gnus-close-server (method)
10329   (funcall (gnus-get-function method 'close-server) (nth 1 method)))
10330
10331 (defun gnus-request-list (method)
10332   (funcall (gnus-get-function method 'request-list) (nth 1 method)))
10333
10334 (defun gnus-request-list-newsgroups (method)
10335   (funcall (gnus-get-function method 'request-list-newsgroups) (nth 1 method)))
10336
10337 (defun gnus-request-newgroups (date method)
10338   (funcall (gnus-get-function method 'request-newgroups) 
10339            date (nth 1 method)))
10340
10341 (defun gnus-server-opened (method)
10342   (funcall (gnus-get-function method 'server-opened) (nth 1 method)))
10343
10344 (defun gnus-status-message (method)
10345   (let ((method (if (stringp method) (gnus-find-method-for-group method)
10346                   method)))
10347     (funcall (gnus-get-function method 'status-message) (nth 1 method))))
10348
10349 (defun gnus-request-group (group &optional dont-check)
10350   (let ((method (gnus-find-method-for-group group)))
10351     (funcall (gnus-get-function method 'request-group) 
10352              (gnus-group-real-name group) (nth 1 method) dont-check)))
10353
10354 (defun gnus-request-asynchronous (group &optional articles)
10355   (let ((method (gnus-find-method-for-group group)))
10356     (funcall (gnus-get-function method 'request-asynchronous) 
10357              (gnus-group-real-name group) (nth 1 method) articles)))
10358
10359 (defun gnus-list-active-group (group)
10360   (let ((method (gnus-find-method-for-group group))
10361         (func 'list-active-group))
10362     (and (gnus-check-backend-function func group)
10363          (funcall (gnus-get-function method func) 
10364                   (gnus-group-real-name group) (nth 1 method)))))
10365
10366 (defun gnus-request-group-description (group)
10367   (let ((method (gnus-find-method-for-group group))
10368         (func 'request-group-description))
10369     (and (gnus-check-backend-function func group)
10370          (funcall (gnus-get-function method func) 
10371                   (gnus-group-real-name group) (nth 1 method)))))
10372
10373 (defun gnus-close-group (group)
10374   (let ((method (gnus-find-method-for-group group)))
10375     (funcall (gnus-get-function method 'close-group) 
10376              (gnus-group-real-name group) (nth 1 method))))
10377
10378 (defun gnus-retrieve-headers (articles group)
10379   (let ((method (gnus-find-method-for-group group)))
10380     (funcall (gnus-get-function method 'retrieve-headers) 
10381              articles (gnus-group-real-name group) (nth 1 method))))
10382
10383 (defun gnus-retrieve-groups (groups method)
10384   (funcall (gnus-get-function method 'retrieve-groups) groups (nth 1 method)))
10385
10386 (defun gnus-request-article (article group &optional buffer)
10387   (let ((method (gnus-find-method-for-group group)))
10388     (funcall (gnus-get-function method 'request-article) 
10389              article (gnus-group-real-name group) (nth 1 method) buffer)))
10390
10391 (defun gnus-request-head (article group)
10392   (let ((method (gnus-find-method-for-group group)))
10393     (funcall (gnus-get-function method 'request-head) 
10394              article (gnus-group-real-name group) (nth 1 method))))
10395
10396 ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
10397 (defun gnus-request-post-buffer (post group subject header artbuf
10398                                       info follow-to respect-poster)
10399    (let* ((info (or info (and group (nth 2 (gnus-gethash 
10400                                             group gnus-newsrc-hashtb)))))
10401           (method
10402            (if (and gnus-post-method
10403                     ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
10404                     (memq 'post (assoc
10405                                  (format "%s" (car (gnus-find-method-for-group
10406                                                     gnus-newsgroup-name)))
10407                                         gnus-valid-select-methods)))
10408                gnus-post-method
10409              (gnus-find-method-for-group gnus-newsgroup-name))))
10410      (or (gnus-server-opened method)
10411          (gnus-open-server method)
10412          (error "Can't open server %s:%s" (car method) (nth 1 method)))
10413      (let ((mail-self-blind nil)
10414            (mail-archive-file-name nil))
10415        (funcall (gnus-get-function method 'request-post-buffer) 
10416                 post group subject header artbuf info follow-to
10417                 respect-poster))))
10418
10419 (defun gnus-request-post (method &optional force)
10420   (and (stringp method)
10421        (setq method (gnus-server-to-method method)))
10422   (and (not force) gnus-post-method
10423        (memq 'post (assoc (format "%s" (car method))
10424                           gnus-valid-select-methods))
10425        (setq method gnus-post-method))
10426   (funcall (gnus-get-function method 'request-post) 
10427            (nth 1 method)))
10428
10429 (defun gnus-request-expire-articles (articles group &optional force)
10430   (let ((method (gnus-find-method-for-group group)))
10431     (funcall (gnus-get-function method 'request-expire-articles) 
10432              articles (gnus-group-real-name group) (nth 1 method)
10433              force)))
10434
10435 (defun gnus-request-move-article 
10436   (article group server accept-function &optional last)
10437   (let ((method (gnus-find-method-for-group group)))
10438     (funcall (gnus-get-function method 'request-move-article) 
10439              article (gnus-group-real-name group) 
10440              (nth 1 method) accept-function last)))
10441
10442 (defun gnus-request-accept-article (group &optional last)
10443   (let ((func (if (symbolp group) group
10444                 (car (gnus-find-method-for-group group)))))
10445     (funcall (intern (format "%s-request-accept-article" func))
10446              (if (stringp group) (gnus-group-real-name group) group)
10447              last)))
10448
10449 (defun gnus-request-replace-article (article group buffer)
10450   (let ((func (car (gnus-find-method-for-group group))))
10451     (funcall (intern (format "%s-request-replace-article" func))
10452              article (gnus-group-real-name group) buffer)))
10453
10454 (defun gnus-request-create-group (group)
10455   (let ((method (gnus-find-method-for-group group)))
10456     (funcall (gnus-get-function method 'request-create-group) 
10457              (gnus-group-real-name group) (nth 1 method))))
10458
10459 (defun gnus-find-method-for-group (group &optional info)
10460   (or gnus-override-method
10461       (and (not group)
10462            gnus-select-method)
10463       (let ((info (or info (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
10464             method)
10465         (if (or (not info)
10466                 (not (setq method (nth 4 info))))
10467             (setq method gnus-select-method)
10468           (setq method
10469                 (cond ((stringp method)
10470                        (gnus-server-to-method method))
10471                       ((stringp (car method))
10472                        (gnus-server-extend-method group method))
10473                       (t
10474                        method))))
10475         (gnus-server-add-address method))))
10476
10477 (defun gnus-check-backend-function (func group)
10478   (let ((method (if (stringp group) (car (gnus-find-method-for-group group))
10479                  group)))
10480     (fboundp (intern (format "%s-%s" method func)))))
10481
10482 (defun gnus-methods-using (method)
10483   (let ((valids gnus-valid-select-methods)
10484         outs)
10485     (while valids
10486       (if (memq method (car valids)) 
10487           (setq outs (cons (car valids) outs)))
10488       (setq valids (cdr valids)))
10489     outs))
10490
10491 ;;; 
10492 ;;; Active & Newsrc File Handling
10493 ;;;
10494
10495 ;; Newsrc related functions.
10496 ;; Gnus internal format of gnus-newsrc-alist:
10497 ;; (("alt.general" 3 (1 . 1))
10498 ;;  ("alt.misc"    3 ((1 . 10) (12 . 15)))
10499 ;;  ("alt.test"    7 (1 . 99) (45 57 93)) ...)
10500 ;; The first item is the group name; the second is the subscription
10501 ;; level; the third is either a range of a list of ranges of read
10502 ;; articles, the optional fourth element is a list of marked articles,
10503 ;; the optional fifth element is the select method.
10504 ;;
10505 ;; Gnus internal format of gnus-newsrc-hashtb:
10506 ;; (95 ("alt.general" 3 (1 . 1)) ("alt.misc" 3 ((1 . 10) (12 . 15))) ...)
10507 ;; This is the entry for "alt.misc". The first element is the number
10508 ;; of unread articles in "alt.misc". The cdr of this entry is the
10509 ;; element *before* "alt.misc" in gnus-newsrc-alist, which makes is
10510 ;; trivial to remove or add new elements into gnus-newsrc-alist
10511 ;; without scanning the entire list. So, to get the actual information
10512 ;; of "alt.misc", you'd say something like 
10513 ;; (nth 2 (gnus-gethash "alt.misc" gnus-newsrc-hashtb))
10514 ;;
10515 ;; Gnus internal format of gnus-active-hashtb:
10516 ;; ((1 . 1))
10517 ;;  (5 . 10))
10518 ;;  (67 . 99)) ...)
10519 ;; The only element in each entry in this hash table is a range of
10520 ;; (possibly) available articles. (Articles in this range may have
10521 ;; been expired or cancelled.)
10522 ;;
10523 ;; Gnus internal format of gnus-killed-list and gnus-zombie-list:
10524 ;; ("alt.misc" "alt.test" "alt.general" ...)
10525
10526 (defun gnus-setup-news (&optional rawfile level)
10527   "Setup news information.
10528 If RAWFILE is non-nil, the .newsrc file will also be read.
10529 If LEVEL is non-nil, the news will be set up at level LEVEL."
10530   (let ((init (not (and gnus-newsrc-alist gnus-active-hashtb (not rawfile)))))
10531     ;; Clear some variables to re-initialize news information.
10532     (if init (setq gnus-newsrc-alist nil gnus-active-hashtb nil))
10533
10534     ;; Read the newsrc file and create `gnus-newsrc-hashtb'.
10535     (if init (gnus-read-newsrc-file rawfile))
10536
10537     ;; Read the active file and create `gnus-active-hashtb'.
10538     ;; If `gnus-read-active-file' is nil, then we just create an empty
10539     ;; hash table. The partial filling out of the hash table will be
10540     ;; done in `gnus-get-unread-articles'.
10541     (if (and gnus-read-active-file 
10542              (not level)
10543              (gnus-server-opened gnus-select-method))
10544         (gnus-read-active-file)
10545       (setq gnus-active-hashtb (make-vector 4095 0)))
10546
10547     (and init gnus-use-dribble-file (gnus-dribble-read-file))
10548
10549     ;; Find the number of unread articles in each non-dead group.
10550     (gnus-get-unread-articles (or level (1+ gnus-level-subscribed)))
10551     ;; Find new newsgroups and treat them.
10552     (if (and init gnus-check-new-newsgroups gnus-read-active-file (not level)
10553              (gnus-server-opened gnus-select-method))
10554         (gnus-find-new-newsgroups))
10555     (if (and init gnus-check-bogus-newsgroups 
10556              gnus-read-active-file (not level)
10557              (gnus-server-opened gnus-select-method))
10558         (gnus-check-bogus-newsgroups))))
10559
10560 (defun gnus-find-new-newsgroups ()
10561   "Search for new newsgroups and add them.
10562 Each new newsgroup will be treated with `gnus-subscribe-newsgroup-method.'
10563 The `-n' option line from .newsrc is respected."
10564   (interactive)
10565   (or (gnus-check-first-time-used)
10566       (if (or (consp gnus-check-new-newsgroups)
10567               (eq gnus-check-new-newsgroups 'ask-server))
10568           (gnus-ask-server-for-new-groups)
10569         (let ((groups 0)
10570               group new-newsgroups)
10571           (or gnus-have-read-active-file (gnus-read-active-file))
10572           (setq gnus-newsrc-last-checked-date (current-time-string))
10573           (if (not gnus-killed-hashtb) (gnus-make-hashtable-from-killed))
10574           ;; Go though every newsgroup in `gnus-active-hashtb' and compare
10575           ;; with `gnus-newsrc-hashtb' and `gnus-killed-hashtb'.
10576           (mapatoms
10577            (lambda (sym)
10578              (setq group (symbol-name sym))
10579              (if (or (gnus-gethash group gnus-killed-hashtb)
10580                      (gnus-gethash group gnus-newsrc-hashtb))
10581                  ()
10582                (let ((do-sub (gnus-matches-options-n group)))
10583                  (cond ((eq do-sub 'subscribe)
10584                         (setq groups (1+ groups))
10585                         (gnus-sethash group group gnus-killed-hashtb)
10586                         (funcall 
10587                          gnus-subscribe-options-newsgroup-method group))
10588                        ((eq do-sub 'ignore)
10589                         nil)
10590                        (t
10591                         (setq groups (1+ groups))
10592                         (gnus-sethash group group gnus-killed-hashtb)
10593                         (if gnus-subscribe-hierarchical-interactive
10594                             (setq new-newsgroups (cons group new-newsgroups))
10595                           (funcall gnus-subscribe-newsgroup-method group)))))))
10596            gnus-active-hashtb)
10597           (if new-newsgroups 
10598               (gnus-subscribe-hierarchical-interactive new-newsgroups))
10599           ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
10600           (if (> groups 0)
10601               (message "%d new newsgroup%s arrived." 
10602                        groups (if (> groups 1) "s have" " has")))))))
10603
10604 (defun gnus-matches-options-n (group)
10605   ;; Returns `subscribe' if the group is to be uncoditionally
10606   ;; subscribed, `ignore' if it is to be ignored, and nil if there is
10607   ;; no match for the group.
10608
10609   ;; First we check the two user variables.
10610   (cond
10611    ((and gnus-options-not-subscribe
10612          (string-match gnus-options-not-subscribe group))
10613     'subscribe)
10614    ((and gnus-options-subscribe
10615          (string-match gnus-options-subscribe group))
10616     'ignore)
10617    ;; Then we go through the list that was retrieved from the .newsrc
10618    ;; file.  This list has elements on the form 
10619    ;; `(REGEXP . {ignore,subscribe})'. The first match found (the list
10620    ;; is in the reverse order of the options line) is returned.
10621    (t
10622     (let ((regs gnus-newsrc-options-n))
10623       (while (and regs
10624                   (not (string-match (car (car gnus-newsrc-options-n)) group)))
10625         (setq regs (cdr regs)))
10626       (and regs (cdr (car regs)))))))
10627
10628 (defun gnus-ask-server-for-new-groups ()
10629   (let* ((date (or gnus-newsrc-last-checked-date (current-time-string)))
10630          (methods (cons gnus-select-method 
10631                         (append
10632                          (and (consp gnus-check-new-newsgroups)
10633                               gnus-check-new-newsgroups)
10634                          gnus-secondary-select-methods)))
10635          (groups 0)
10636          (new-date (current-time-string))
10637          hashtb group new-newsgroups got-new)
10638     ;; Go thorugh both primary and secondary select methods and
10639     ;; request new newsgroups.  
10640     (while methods
10641       (if (gnus-request-newgroups date (car methods))
10642           (save-excursion
10643             (setq got-new t)
10644             (or hashtb (setq hashtb (gnus-make-hashtable 
10645                                      (count-lines (point-min) (point-max)))))
10646             (set-buffer nntp-server-buffer)
10647             ;; Enter all the new groups in a hashtable.
10648             (gnus-active-to-gnus-format (car methods) hashtb)))
10649       (setq methods (cdr methods)))
10650     (and got-new (setq gnus-newsrc-last-checked-date new-date))
10651     ;; Now all new groups from all select methods are in `hashtb'.
10652     (mapatoms
10653      (lambda (group-sym)
10654        (setq group (symbol-name group-sym))
10655        (if (or (gnus-gethash group gnus-newsrc-hashtb)
10656                (member group gnus-zombie-list)
10657                (member group gnus-killed-list))
10658            ;; The group is already known.
10659            ()
10660          (gnus-sethash group (symbol-value group-sym) gnus-active-hashtb)
10661          (let ((do-sub (gnus-matches-options-n group)))
10662            (cond ((eq do-sub 'subscribe)
10663                   (setq groups (1+ groups))
10664                   (gnus-sethash group group gnus-killed-hashtb)
10665                   (funcall 
10666                    gnus-subscribe-options-newsgroup-method group))
10667                  ((eq do-sub 'ignore)
10668                   nil)
10669                  (t
10670                   (setq groups (1+ groups))
10671                   (gnus-sethash group group gnus-killed-hashtb)
10672                   (if gnus-subscribe-hierarchical-interactive
10673                       (setq new-newsgroups (cons group new-newsgroups))
10674                     (funcall gnus-subscribe-newsgroup-method group)))))))
10675      hashtb)
10676     (if new-newsgroups 
10677         (gnus-subscribe-hierarchical-interactive new-newsgroups))
10678     ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
10679     (if (> groups 0)
10680         (message "%d new newsgroup%s arrived." 
10681                  groups (if (> groups 1) "s have" " has")))
10682     got-new))
10683
10684 (defun gnus-check-first-time-used ()
10685   (if (or (> (length gnus-newsrc-alist) 1)
10686           (file-exists-p gnus-startup-file)
10687           (file-exists-p (concat gnus-startup-file ".el"))
10688           (file-exists-p (concat gnus-startup-file ".eld")))
10689       nil
10690     (message "First time user; subscribing you to default groups")
10691     (or gnus-have-read-active-file (gnus-read-active-file))
10692     (setq gnus-newsrc-last-checked-date (current-time-string))
10693     (let ((groups gnus-default-subscribed-newsgroups)
10694           group)
10695       (if (eq groups t)
10696           nil
10697         (setq groups (or groups gnus-backup-default-subscribed-newsgroups))
10698         (mapatoms
10699          (lambda (sym)
10700            (setq group (symbol-name sym))
10701            (let ((do-sub (gnus-matches-options-n group)))
10702              (cond ((eq do-sub 'subscribe)
10703                     (gnus-sethash group group gnus-killed-hashtb)
10704                     (funcall 
10705                      gnus-subscribe-options-newsgroup-method group))
10706                    ((eq do-sub 'ignore)
10707                     nil)
10708                    (t
10709                     (setq gnus-killed-list (cons group gnus-killed-list))))))
10710          gnus-active-hashtb)
10711         (while groups
10712           (if (gnus-gethash (car groups) gnus-active-hashtb)
10713               (gnus-group-change-level 
10714                (car groups) gnus-level-default-subscribed gnus-level-killed))
10715           (setq groups (cdr groups)))
10716         (gnus-group-make-help-group)
10717         (and gnus-novice-user
10718              (message (substitute-command-keys "\\<gnus-group-mode-map>\\[gnus-group-list-killed] to list killed groups")))))))
10719
10720 ;; `gnus-group-change-level' is the fundamental function for changing
10721 ;; subscription levels of newsgroups. This might mean just changing
10722 ;; from level 1 to 2, which is pretty trivial, from 2 to 6 or back
10723 ;; again, which subscribes/unsubscribes a group, which is equally
10724 ;; trivial. Changing from 1-7 to 8-9 means that you kill a group, and
10725 ;; from 8-9 to 1-7 means that you remove the group from the list of
10726 ;; killed (or zombie) groups and add them to the (kinda) subscribed
10727 ;; groups. And last but not least, moving from 8 to 9 and 9 to 8,
10728 ;; which is trivial.
10729 ;; ENTRY can either be a string (newsgroup name) or a list (if
10730 ;; FROMKILLED is t, it's a list on the format (NUM INFO-LIST),
10731 ;; otherwise it's a list in the format of the `gnus-newsrc-hashtb'
10732 ;; entries. 
10733 ;; LEVEL is the new level of the group, OLDLEVEL is the old level and
10734 ;; PREVIOUS is the group (in hashtb entry format) to insert this group
10735 ;; after. 
10736 (defun gnus-group-change-level (entry level &optional oldlevel
10737                                       previous fromkilled)
10738   (let ((pinfo entry)
10739         group info active num)
10740     ;; Glean what info we can from the arguments
10741     (if (consp entry)
10742         (if fromkilled (setq group (nth 1 entry))
10743           (setq group (car (nth 2 entry))))
10744       (setq group entry))
10745     (if (and (stringp entry)
10746              oldlevel 
10747              (< oldlevel gnus-level-zombie))
10748         (setq entry (gnus-gethash entry gnus-newsrc-hashtb)))
10749     (if (and (not oldlevel)
10750              (listp entry))
10751         (setq oldlevel (car (cdr (nth 2 entry)))))
10752     (if (stringp previous)
10753         (setq previous (gnus-gethash previous gnus-newsrc-hashtb)))
10754
10755     (gnus-dribble-enter
10756      (format "(gnus-group-change-level %S %S %S %S %S)" 
10757              group level oldlevel (car (nth 2 previous)) fromkilled))
10758     
10759     ;; Then we remove the newgroup from any old structures, if needed.
10760     ;; If the group was killed, we remove it from the killed or zombie
10761     ;; list. If not, and it is in fact going to be killed, we remove
10762     ;; it from the newsrc hash table and assoc.
10763     (cond ((>= oldlevel gnus-level-zombie)
10764            (if (= oldlevel gnus-level-zombie)
10765                (setq gnus-zombie-list (delete group gnus-zombie-list))
10766              (setq gnus-killed-list (delete group gnus-killed-list))))
10767           (t
10768            (if (>= level gnus-level-zombie)
10769                (progn
10770                  (gnus-sethash (car (nth 2 entry))
10771                                nil gnus-newsrc-hashtb)
10772                  (if (nth 3 entry)
10773                      (setcdr (gnus-gethash (car (nth 3 entry))
10774                                            gnus-newsrc-hashtb)
10775                              (cdr entry)))
10776                  (setcdr (cdr entry) (cdr (cdr (cdr entry))))))))
10777
10778     ;; Finally we enter (if needed) the list where it is supposed to
10779     ;; go, and change the subscription level. If it is to be killed,
10780     ;; we enter it into the killed or zombie list.
10781     (cond ((>= level gnus-level-zombie)
10782            (and (string= group (gnus-group-real-name group))
10783                 (if (= level gnus-level-zombie)
10784                     (setq gnus-zombie-list (cons group gnus-zombie-list))
10785                   (setq gnus-killed-list (cons group gnus-killed-list)))))
10786           (t
10787            ;; If the list is to be entered into the newsrc assoc, and
10788            ;; it was killed, we have to create an entry in the newsrc
10789            ;; hashtb format and fix the pointers in the newsrc assoc.
10790            (if (>= oldlevel gnus-level-zombie)
10791                (progn
10792                  (if (listp entry)
10793                      (progn
10794                        (setq info (cdr entry))
10795                        (setq num (car entry)))
10796                    (setq active (gnus-gethash group gnus-active-hashtb))
10797                    (setq num (if active (- (1+ (cdr active)) (car active)) t))
10798                    ;; Check whether the group is foreign. If so, the
10799                    ;; foreign select method has to be entered into the
10800                    ;; info. 
10801                    (let ((method (gnus-group-method-name group)))
10802                      (if (eq method gnus-select-method)
10803                          (setq info (list group level nil))
10804                        (setq info (list group level nil nil method)))))
10805                  (setq entry (cons info (if previous (cdr (cdr previous))
10806                                           (cdr gnus-newsrc-alist))))
10807                  (setcdr (if previous (cdr previous) gnus-newsrc-alist)
10808                          entry)
10809                  (gnus-sethash group (cons num (if previous (cdr previous)
10810                                                  gnus-newsrc-alist))
10811                                gnus-newsrc-hashtb)
10812                  (if (cdr entry)
10813                      (setcdr (gnus-gethash (car (car (cdr entry)))
10814                                            gnus-newsrc-hashtb)
10815                              entry)))
10816              ;; It was alive, and it is going to stay alive, so we
10817              ;; just change the level and don't change any pointers or
10818              ;; hash table entries.
10819              (setcar (cdr (car (cdr (cdr entry)))) level))))))
10820
10821 (defun gnus-kill-newsgroup (newsgroup)
10822   "Obsolete function. Kills a newsgroup."
10823   (gnus-group-change-level
10824    (gnus-gethash newsgroup gnus-newsrc-hashtb) gnus-level-killed))
10825
10826 (defun gnus-check-bogus-newsgroups (&optional confirm)
10827   "Remove bogus newsgroups.
10828 If CONFIRM is non-nil, the user has to confirm the deletion of every
10829 newsgroup." 
10830   (let ((newsrc (cdr gnus-newsrc-alist))
10831         bogus group)
10832     (message "Checking bogus newsgroups...")
10833     (or gnus-have-read-active-file (gnus-read-active-file))
10834     ;; Find all bogus newsgroup that are subscribed.
10835     (while newsrc
10836       (setq group (car (car newsrc)))
10837       (if (or (gnus-gethash group gnus-active-hashtb)
10838               (nth 4 (car newsrc))
10839               (and confirm
10840                    (not (gnus-y-or-n-p
10841                          (format "Remove bogus newsgroup: %s " group)))))
10842           ;; Active newsgroup.
10843           ()
10844         ;; Found a bogus newsgroup.
10845         (setq bogus (cons group bogus)))
10846       (setq newsrc (cdr newsrc)))
10847     ;; Remove all bogus subscribed groups by first killing them, and
10848     ;; then removing them from the list of killed groups.
10849     (while bogus
10850       (gnus-group-change-level 
10851        (gnus-gethash (car bogus) gnus-newsrc-hashtb) gnus-level-killed)
10852       (setq gnus-killed-list (delete (car bogus) gnus-killed-list))
10853       (setq bogus (cdr bogus)))
10854     ;; Then we remove all bogus groups from the list of killed and
10855     ;; zombie groups. They are are removed without confirmation.
10856     (let ((dead-lists '(gnus-killed-list gnus-zombie-list))
10857           killed)
10858       (while dead-lists
10859         (setq killed (symbol-value (car dead-lists)))
10860         (while killed
10861           (setq group (car killed))
10862           (or (gnus-gethash group gnus-active-hashtb)
10863               ;; The group is bogus.
10864               (set (car dead-lists)
10865                    (delete group (symbol-value (car dead-lists)))))
10866           (setq killed (cdr killed)))
10867         (setq dead-lists (cdr dead-lists))))
10868     (message "Checking bogus newsgroups... done")))
10869
10870 (defun gnus-check-duplicate-killed-groups ()
10871   "Remove duplicates from the list of killed groups."
10872   (interactive)
10873   (let ((killed gnus-killed-list))
10874     (while killed
10875       (message "%d" (length killed))
10876       (setcdr killed (delete (car killed) (cdr killed)))
10877       (setq killed (cdr killed)))))
10878
10879 ;; Go though `gnus-newsrc-alist' and compare with `gnus-active-hashtb'
10880 ;; and compute how many unread articles there are in each group.
10881 (defun gnus-get-unread-articles (&optional level)
10882   (let ((newsrc (cdr gnus-newsrc-alist))
10883         (level (or level (1+ gnus-level-subscribed)))
10884         info group active virtuals)
10885     (message "Checking new news...")
10886     (while newsrc
10887       (setq info (car newsrc))
10888       (setq group (car info))
10889       (setq active (gnus-gethash group gnus-active-hashtb))
10890
10891       ;; Check newsgroups. If the user doesn't want to check them, or
10892       ;; they can't be checked (for instance, if the news server can't
10893       ;; be reached) we just set the number of unread articles in this
10894       ;; newsgroup to t. This means that Gnus thinks that there are
10895       ;; unread articles, but it has no idea how many.
10896       (if (nth 4 info)
10897           (if (or (and gnus-activate-foreign-newsgroups 
10898                        (not (numberp gnus-activate-foreign-newsgroups)))
10899                   (and (numberp gnus-activate-foreign-newsgroups)
10900                        (<= (nth 1 info) gnus-activate-foreign-newsgroups)
10901                        (<= (nth 1 info) level)))
10902               (if (eq (car (nth 4 info)) 'nnvirtual)
10903                   (setq virtuals (cons info virtuals))
10904                 (setq active (gnus-activate-newsgroup (car info)))))
10905         (if (and (not gnus-read-active-file)
10906                  (<= (nth 1 info) level))
10907             (progn
10908               (setq active (gnus-activate-newsgroup (car info))))))
10909       
10910       (or active (progn (gnus-sethash group nil gnus-active-hashtb)
10911                         (setcar (gnus-gethash group gnus-newsrc-hashtb) t)))
10912       (and active 
10913            (gnus-get-unread-articles-in-group info active)
10914            ;; Close the groups as we look at them!
10915            (gnus-close-group group))
10916       (setq newsrc (cdr newsrc)))
10917
10918     ;; Activate the virtual groups. This has to be done after all the
10919     ;; other groups. 
10920     ;; !!! If one virtual group contains another virtual group, even
10921     ;; doing it this way might cause problems.
10922    (while virtuals
10923       (and (setq active (gnus-activate-newsgroup (car (car virtuals))))
10924            (gnus-get-unread-articles-in-group (car virtuals) active))
10925       (setq virtuals (cdr virtuals)))
10926
10927     (message "Checking new news... done")))
10928
10929 ;; Create a hash table out of the newsrc alist. The `car's of the
10930 ;; alist elements are used as keys.
10931 (defun gnus-make-hashtable-from-newsrc-alist ()
10932   (let ((alist gnus-newsrc-alist)
10933          prev)
10934     (setq gnus-newsrc-hashtb (gnus-make-hashtable (length alist)))
10935     (setq alist 
10936           (setq prev (setq gnus-newsrc-alist 
10937                            (cons (list "dummy.group" 0 nil) alist))))
10938     (while alist
10939       (gnus-sethash (car (car alist)) (cons nil prev) gnus-newsrc-hashtb)
10940       (setq prev alist)
10941       (setq alist (cdr alist)))))
10942
10943 (defun gnus-make-hashtable-from-killed ()
10944   "Create a hash table from the killed and zombie lists."
10945   (let ((lists '(gnus-killed-list gnus-zombie-list))
10946         list)
10947     (setq gnus-killed-hashtb 
10948           (gnus-make-hashtable 
10949            (+ (length gnus-killed-list) (length gnus-zombie-list))))
10950     (while lists
10951       (setq list (symbol-value (car lists)))
10952       (setq lists (cdr lists))
10953       (while list
10954         (gnus-sethash (car list) (car list) gnus-killed-hashtb)
10955         (setq list (cdr list))))))
10956
10957 (defun gnus-get-unread-articles-in-group (info active)
10958   (let* ((range (nth 2 info))
10959          (num 0)
10960          (marked (nth 3 info))
10961          srange lowest group highest)
10962     ;; Modify the list of read articles according to what articles 
10963     ;; are available; then tally the unread articles and add the
10964     ;; number to the group hash table entry.
10965     (cond ((zerop (cdr active))
10966            (setq num 0))
10967           ((not range)
10968            (setq num (- (1+ (cdr active)) (car active))))
10969           ((not (listp (cdr range)))
10970            ;; Fix a single (num . num) range according to the
10971            ;; active hash table.
10972            ;; Fix by Carsten Bormann <cabo@Informatik.Uni-Bremen.DE>.
10973            (and (< (cdr range) (car active)) (setcdr range (1- (car active))))
10974            (and (> (cdr range) (cdr active)) (setcdr range (cdr active)))
10975            ;; Compute number of unread articles.
10976            (setq num (max 0 (- (cdr active) 
10977                                (- (1+ (cdr range)) (car range))))))
10978           (t
10979            ;; The read list is a list of ranges. Fix them according to
10980            ;; the active hash table.
10981            ;; First peel off any elements that are below the lower
10982            ;; active limit. 
10983            (while (and (cdr range) 
10984                        (>= (car active) 
10985                            (or (and (atom (car (cdr range))) (car (cdr range)))
10986                                (car (car (cdr range))))))
10987              (if (numberp (car range))
10988                  (setcar range 
10989                          (cons (car range) 
10990                                (or (and (numberp (car (cdr range)))
10991                                         (car (cdr range))) 
10992                                    (cdr (car (cdr range))))))
10993                (setcdr (car range) 
10994                        (or (and (numberp (nth 1 range)) (nth 1 range))
10995                            (cdr (car (cdr range))))))
10996              (setcdr range (cdr (cdr range))))
10997            ;; Adjust the first element to be the same as the lower limit. 
10998            (if (and (not (atom (car range))) 
10999                     (< (cdr (car range)) (car active)))
11000                (setcdr (car range) (1- (car active))))
11001            ;; Then we want to peel off any elements that are higher
11002            ;; than the upper active limit.  
11003            (let ((srange range))
11004              ;; Go past all legal elements.
11005              (while (and (cdr srange) 
11006                          (<= (or (and (atom (car (cdr srange)))
11007                                       (car (cdr srange)))
11008                                  (car (car (cdr srange)))) (cdr active)))
11009                (setq srange (cdr srange)))
11010              (if (cdr srange)
11011                  ;; Nuke all remaining illegal elements.
11012                  (setcdr srange nil))
11013
11014              ;; Adjust the final element.
11015              (if (and (not (atom (car srange)))
11016                       (> (cdr (car srange)) (cdr active)))
11017                  (setcdr (car srange) (cdr active))))
11018            ;; Compute the number of unread articles.
11019            (while range
11020              (setq num (+ num (- (1+ (or (and (atom (car range)) (car range))
11021                                          (cdr (car range))))
11022                                  (or (and (atom (car range)) (car range))
11023                                      (car (car range))))))
11024              (setq range (cdr range)))
11025            (setq num (max 0 (- (cdr active) num)))))
11026     (and info
11027          (progn
11028            (and (assq 'tick marked)
11029                 (inline (gnus-remove-illegal-marked-articles
11030                          (assq 'tick marked) (nth 2 info))))
11031            (and (assq 'dormant marked)
11032                 (inline (gnus-remove-illegal-marked-articles
11033                          (assq 'dormant marked) (nth 2 info))))
11034            (setcar
11035             (gnus-gethash (car info) gnus-newsrc-hashtb) 
11036             (setq num (max 0 (- num (length (cdr (assq 'tick marked)))
11037                                 (length (cdr (assq 'dormant marked)))))))))
11038     num))
11039
11040 (defun gnus-remove-illegal-marked-articles (marked ranges)
11041   (let ((m (cdr marked)))
11042     ;; Make sure that all ticked articles are a subset of the unread
11043     ;; articles. 
11044     (while m
11045       (if (gnus-member-of-range (car m) ranges)
11046           (setcdr marked (cdr m))
11047         (setq marked m))
11048       (setq m (cdr m)))))
11049
11050 (defun gnus-activate-newsgroup (group)
11051   (let ((method (gnus-find-method-for-group group))
11052         active)
11053     (and (or (gnus-server-opened method) (gnus-open-server method))
11054          (gnus-request-group group)
11055          (save-excursion
11056            (set-buffer nntp-server-buffer)
11057            (goto-char 1)
11058            (and (looking-at "[0-9]+ [0-9]+ \\([0-9]+\\) [0-9]+")
11059                 (progn
11060                   (goto-char (match-beginning 1))
11061                   (gnus-sethash 
11062                    group (setq active (cons (read (current-buffer))
11063                                             (read (current-buffer))))
11064                    gnus-active-hashtb))
11065                 active)))))
11066
11067 (defun gnus-update-read-articles 
11068   (group unread unselected ticked &optional domarks replied expirable killed
11069          dormant bookmark score)
11070   "Update the list of read and ticked articles in GROUP using the
11071 UNREAD and TICKED lists.
11072 Note: UNSELECTED has to be sorted over `<'.
11073 Returns whether the updating was successful."
11074   (let* ((active (or gnus-newsgroup-active 
11075                      (gnus-gethash group gnus-active-hashtb)))
11076          (entry (gnus-gethash group gnus-newsrc-hashtb))
11077          (number (car entry))
11078          (info (nth 2 entry))
11079          (marked (nth 3 info))
11080          (prev 1)
11081          (unread (sort (copy-sequence unread) (function <)))
11082          last read)
11083     (if (or (not info) (not active))
11084         ;; There is no info on this group if it was, in fact,
11085         ;; killed. Gnus stores no information on killed groups, so
11086         ;; there's nothing to be done. 
11087         ;; One could store the information somewhere temporarily,
11088         ;; perhaps... Hmmm... 
11089         ()
11090       ;; Remove any negative articles numbers.
11091       (while (and unread (< (car unread) 0))
11092         (setq unread (cdr unread)))
11093       (setq unread (sort (append unselected unread) '<))
11094       ;; Set the number of unread articles in gnus-newsrc-hashtb.
11095       (setcar entry (max 0 (- (length unread) (length ticked) 
11096                               (length dormant))))
11097       ;; Compute the ranges of read articles by looking at the list of
11098       ;; unread articles.  
11099       (while unread
11100         (if (/= (car unread) prev)
11101             (setq read (cons (if (= prev (1- (car unread))) prev
11102                                (cons prev (1- (car unread)))) read)))
11103         (setq prev (1+ (car unread)))
11104         (setq unread (cdr unread)))
11105       (if (<= prev (cdr active))
11106           (setq read (cons (cons prev (cdr active)) read)))
11107       ;; Enter this list into the group info.
11108       (setcar (cdr (cdr info)) 
11109               (if (> (length read) 1) (nreverse read) read))
11110       ;; Enter the list of ticked articles.
11111       (gnus-set-marked-articles 
11112        info ticked
11113        (if domarks replied (cdr (assq 'reply marked)))
11114        (if domarks expirable (cdr (assq 'expire marked)))
11115        (if domarks killed (cdr (assq 'killed marked)))
11116        (if domarks dormant (cdr (assq 'dormant marked)))
11117        (if domarks bookmark (cdr (assq 'bookmark marked)))
11118        (if domarks score (cdr (assq 'score marked))))
11119       t)))
11120
11121 (defun gnus-make-articles-unread (group articles)
11122   "Mark ARTICLES in GROUP as unread."
11123   (let ((info (nth 2 (or (gnus-gethash group gnus-newsrc-hashtb)
11124                          (gnus-gethash (gnus-group-real-name group)
11125                                        gnus-newsrc-hashtb)))))
11126     (setcar (nthcdr 2 info)
11127             (gnus-remove-from-range (nth 2 info) articles))
11128     (gnus-group-update-group group t)))
11129
11130 (defun gnus-read-active-file ()
11131   "Get active file from NNTP server."
11132   (gnus-group-set-mode-line)
11133   (let ((methods (cons gnus-select-method gnus-secondary-select-methods))
11134         list-type)
11135     (setq gnus-have-read-active-file nil)
11136     (save-excursion
11137       (set-buffer nntp-server-buffer)
11138       (while methods
11139         (let* ((where (nth 1 (car methods)))
11140                (mesg (format "Reading active file%s via %s..."
11141                              (if (and where (not (zerop (length where))))
11142                                  (concat " from " where) "")
11143                              (car (car methods)))))
11144           (message mesg)
11145           (gnus-check-news-server (car methods))
11146           (cond 
11147            ((and (eq gnus-read-active-file 'some)
11148                  (gnus-check-backend-function
11149                   'retrieve-groups (car (car methods))))
11150             (let ((newsrc (cdr gnus-newsrc-alist))
11151                   groups)
11152               (while newsrc
11153                 (and (gnus-server-equal 
11154                       (gnus-find-method-for-group
11155                        (car (car newsrc)) (car newsrc))
11156                       (gnus-server-get-method nil (car methods)))
11157                      (setq groups (cons (car (car newsrc)) groups)))
11158                 (setq newsrc (cdr newsrc)))
11159               (setq list-type (gnus-retrieve-groups groups (car methods)))
11160               (cond ((not list-type)
11161                      (message "Cannot read partial active file from %s server." 
11162                               (car (car methods)))
11163                      (ding)
11164                      (sit-for 2))
11165                     ((eq list-type 'active)
11166                      (gnus-active-to-gnus-format 
11167                       (and gnus-have-read-active-file (car methods)))
11168                      (setq gnus-have-read-active-file t))
11169                     (t
11170                      (gnus-groups-to-gnus-format
11171                       (and gnus-have-read-active-file (car methods)))
11172                      (setq gnus-have-read-active-file t)))))
11173            (t
11174             (if (not (gnus-request-list (car methods)))
11175                 (progn
11176                   (message "Cannot read active file from %s server." 
11177                            (car (car methods)))
11178                   (ding))
11179               (gnus-active-to-gnus-format 
11180                (and gnus-have-read-active-file (car methods)))
11181               (setq gnus-have-read-active-file t)
11182               (message "%s...done" mesg)))))
11183         (setq methods (cdr methods))))))
11184
11185 ;; rewritten by jwz based on ideas from Rick Sladkey <jrs@world.std.com>
11186 ;; Further rewrites by lmi.
11187 (defun gnus-active-to-gnus-format (method &optional hashtb)
11188   "Convert active file format to internal format.
11189 Lines matching `gnus-ignored-newsgroups' are ignored."
11190   (let ((cur (current-buffer))
11191         (hashtb (or hashtb 
11192                     (if method
11193                         gnus-active-hashtb
11194                       (setq gnus-active-hashtb
11195                             (gnus-make-hashtable 
11196                              (count-lines (point-min) (point-max))))))))
11197     ;; Delete unnecessary lines.
11198     (goto-char (point-min))
11199     (delete-matching-lines gnus-ignored-newsgroups)
11200     (and method (not (eq method gnus-select-method))
11201          (let ((prefix (gnus-group-prefixed-name "" method)))
11202            (goto-char (point-min))
11203            (while (and (not (eobp))
11204                        (null (insert prefix))
11205                        (zerop (forward-line 1))))))
11206     (goto-char (point-min))
11207     ;; Store active file in hashtable.
11208     (save-restriction
11209       (if (or (re-search-forward "\n.\r?$" nil t)
11210               (goto-char (point-max)))
11211           (progn
11212             (beginning-of-line)
11213             (narrow-to-region (point-min) (point))))
11214       (goto-char (point-min))
11215       (if (string-match "%[oO]" gnus-group-line-format)
11216           ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
11217           ;; If we want information on moderated groups, we use this
11218           ;; loop...   
11219           (condition-case ()
11220               (let ((mod-hashtb (make-vector 7 0))
11221                     group max mod)
11222                 (while (not (eobp))
11223                   (setq group (let ((obarray hashtb))
11224                                 (read cur)))
11225                   (setq max (read cur))
11226                   (set group (cons (read cur) max))
11227                   ;; Enter moderated groups into a list.
11228                   (if (string= 
11229                        (symbol-name  (let ((obarray mod-hashtb)) (read cur)))
11230                        "m")
11231                       (setq gnus-moderated-list 
11232                             (cons (symbol-name group) gnus-moderated-list)))
11233                   (forward-line 1)))
11234             (error 
11235              (progn (ding) (message "Possible error in active file."))))
11236         ;; And if we do not care about moderation, we use this loop,
11237         ;; which is faster.
11238         (condition-case ()
11239             (let (group max)
11240               (while (not (eobp))
11241                 ;; group gets set to a symbol interned in the hash table
11242                 ;; (what a hack!!)
11243                 (setq group (let ((obarray hashtb)) (read cur)))
11244                 (setq max (read cur))
11245                 (set group (cons (read cur) max))
11246                 (forward-line 1)))
11247           (error 
11248            (progn (ding) (message "Possible error in active file."))))))))
11249
11250 (defun gnus-groups-to-gnus-format (method &optional hashtb)
11251   ;; Parse a "groups" active file.
11252   (let ((cur (current-buffer))
11253         (hashtb (or hashtb 
11254                     (if method
11255                         gnus-active-hashtb
11256                       (setq gnus-active-hashtb
11257                             (gnus-make-hashtable 
11258                              (count-lines (point-min) (point-max)))))))
11259         (prefix (and method (not (eq method gnus-select-method))
11260                      (gnus-group-prefixed-name "" method))))
11261
11262     (goto-char (point-min))
11263     (condition-case ()
11264         ;; We split this into to separate loops, one with the prefix
11265         ;; and one without to speed the reading up somewhat.
11266         (if prefix
11267             (let (min max opoint)
11268               (while (not (eobp))
11269                 (read cur) (read cur)
11270                 (setq min (read cur)
11271                       max (read cur)
11272                       opoint (point))
11273                 (skip-chars-forward " \t")
11274                 (insert prefix)
11275                 (goto-char opoint)
11276                 (set (let ((obarray hashtb)) (read cur)) 
11277                      (cons min max))
11278                 (forward-line 1)))
11279           (let (min max opoint)
11280             (while (not (eobp))
11281               (if (= (following-char) ?2)
11282                   (progn
11283                     (read cur) (read cur)
11284                     (setq min (read cur)
11285                           max (read cur))
11286                     (set (let ((obarray hashtb)) (read cur)) 
11287                          (cons min max))))
11288               (forward-line 1))))
11289       (error 
11290        (progn (ding) (message "Possible error in active file."))))))
11291
11292 (defun gnus-read-newsrc-file (&optional force)
11293   "Read startup file.
11294 If FORCE is non-nil, the .newsrc file is read."
11295   (setq gnus-current-startup-file (gnus-make-newsrc-file gnus-startup-file))
11296   ;; Reset variables that might be defined in the .newsrc.eld file.
11297   (let ((variables gnus-variable-list))
11298     (while variables
11299       (set (car variables) nil)
11300       (setq variables (cdr variables))))
11301   (let* ((newsrc-file gnus-current-startup-file)
11302          (quick-file (concat newsrc-file ".el")))
11303     (save-excursion
11304       ;; We always load the .newsrc.eld file. If always contains
11305       ;; much information that can not be gotten from the .newsrc
11306       ;; file (ticked articles, killed groups, foreign methods, etc.)
11307       (gnus-read-newsrc-el-file quick-file)
11308  
11309       (if (or force
11310               (and (file-newer-than-file-p newsrc-file quick-file)
11311                    (file-newer-than-file-p newsrc-file 
11312                                            (concat quick-file "d")))
11313               (not gnus-newsrc-alist))
11314           ;; We read the .newsrc file. Note that if there if a
11315           ;; .newsrc.eld file exists, it has already been read, and
11316           ;; the `gnus-newsrc-hashtb' has been created. While reading
11317           ;; the .newsrc file, Gnus will only use the information it
11318           ;; can find there for changing the data already read -
11319           ;; ie. reading the .newsrc file will not trash the data
11320           ;; already read (except for read articles).
11321           (save-excursion
11322             (message "Reading %s..." newsrc-file)
11323             (set-buffer (find-file-noselect newsrc-file))
11324             (buffer-disable-undo (current-buffer))
11325             (gnus-newsrc-to-gnus-format)
11326             (kill-buffer (current-buffer))
11327             (message "Reading %s... done" newsrc-file))))))
11328
11329 (defun gnus-read-newsrc-el-file (file)
11330   (let ((ding-file (concat file "d")))
11331     ;; We always, always read the .eld file.
11332     (message "Reading %s..." ding-file)
11333     (let (gnus-newsrc-assoc)
11334       (condition-case nil
11335           (load ding-file t t t)
11336         (error nil))
11337       (and gnus-newsrc-assoc (setq gnus-newsrc-alist gnus-newsrc-assoc)))
11338     (gnus-uncompress-newsrc-assoc)
11339     (gnus-make-hashtable-from-newsrc-alist)
11340     (if (not (file-newer-than-file-p file ding-file))
11341         ()
11342       ;; Old format quick file
11343       (message "Reading %s..." file)
11344       ;; The .el file is newer than the .eld file, so we read that one
11345       ;; as well. 
11346       (gnus-read-old-newsrc-el-file file))))
11347
11348 ;; Parse the old-style quick startup file
11349 (defun gnus-read-old-newsrc-el-file (file)
11350   (let (newsrc killed marked group g m len info)
11351     (prog1
11352         (let ((gnus-killed-assoc nil)
11353               gnus-marked-assoc gnus-newsrc-alist gnus-newsrc-assoc)
11354           (prog1
11355               (condition-case nil
11356                   (load file t t t)
11357                 (error nil))
11358             (setq newsrc gnus-newsrc-alist
11359                   killed gnus-killed-assoc
11360                   marked gnus-marked-assoc)))
11361       (setq gnus-newsrc-alist nil)
11362       (while newsrc
11363         (setq group (car newsrc))
11364         (let ((info (nth 2 (gnus-gethash (car group) gnus-newsrc-hashtb))))
11365           (if info
11366               (progn
11367                 (setcar (nthcdr 2 info) (cdr (cdr group)))
11368                 (setcar (cdr info)
11369                         (if (nth 1 group) gnus-level-default-subscribed 
11370                           gnus-level-default-unsubscribed))
11371                 (setq gnus-newsrc-alist (cons info gnus-newsrc-alist)))
11372             (setq gnus-newsrc-alist
11373                   (cons 
11374                    (setq info
11375                          (list (car group)
11376                                (if (nth 1 group) gnus-level-default-subscribed
11377                                  gnus-level-default-unsubscribed) 
11378                                (cdr (cdr group))))
11379                    gnus-newsrc-alist)))
11380           (if (setq m (assoc (car group) marked))
11381             (setcdr (cdr (cdr info)) (cons (list (cons 'tick (cdr m))) nil))))
11382         (setq newsrc (cdr newsrc)))
11383       (setq newsrc killed)
11384       (while newsrc
11385         (setcar newsrc (car (car newsrc)))
11386         (setq newsrc (cdr newsrc)))
11387       (setq gnus-killed-list killed))
11388     (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist))
11389     (gnus-make-hashtable-from-newsrc-alist)))
11390       
11391 (defun gnus-make-newsrc-file (file)
11392   "Make server dependent file name by catenating FILE and server host name."
11393   (let* ((file (expand-file-name file nil))
11394          (real-file (concat file "-" (nth 1 gnus-select-method))))
11395     (if (file-exists-p real-file)
11396         real-file file)))
11397
11398 (defun gnus-uncompress-newsrc-assoc ()
11399   ;; Uncompress all lists of marked articles in the newsrc assoc.
11400   (let ((newsrc gnus-newsrc-alist)
11401         marked)
11402     (while newsrc
11403       (if (not (setq marked (nth 3 (car newsrc))))
11404           ()
11405         (while marked
11406           (or (eq 'score (car (car marked)))
11407               (eq 'bookmark (car (car marked)))
11408               (eq 'killed (car (car marked)))
11409               (setcdr (car marked) (gnus-uncompress-range (cdr (car marked)))))
11410           (setq marked (cdr marked))))
11411       (setq newsrc (cdr newsrc)))))
11412
11413 (defun gnus-compress-newsrc-assoc ()
11414   ;; Compress all lists of marked articles in the newsrc assoc.
11415   (let ((newsrc gnus-newsrc-alist)
11416         marked)
11417     (while newsrc
11418       (if (not (setq marked (nth 3 (car newsrc))))
11419           ()
11420         (while marked
11421           (or (eq 'score (car (car marked)))
11422               (eq 'bookmark (car (car marked)))
11423               (eq 'killed (car (car marked)))
11424               (setcdr (car marked) 
11425                       (gnus-compress-sequence (sort (cdr (car marked)) '<) t)))
11426           (setq marked (cdr marked))))
11427       (setq newsrc (cdr newsrc)))))
11428
11429 ;; jwz: rewrote this function to be much more efficient, and not be
11430 ;; subject to regexp overflow errors when it encounters very long
11431 ;; lines -- the old behavior was to blow off the rest of the *file*
11432 ;; when a line was encountered that was too long to match!!  Now it
11433 ;; uses only simple looking-at calls, and doesn't create as many
11434 ;; temporary strings.  It also now handles multiple consecutive
11435 ;; options lines (before it only handled the first.)
11436 ;; Tiny rewrite by lmi. 
11437 (defun gnus-newsrc-to-gnus-format ()
11438   "Parse current buffer as .newsrc file."
11439   ;; We have to re-initialize these variables (except for
11440   ;; gnus-killed-list) because the quick startup file may contain bogus
11441   ;; values.
11442   (setq gnus-newsrc-options nil)
11443   (setq gnus-newsrc-options-n nil)
11444   (gnus-parse-options-lines)
11445   (gnus-parse-newsrc-body))
11446
11447 (defun gnus-parse-options-lines ()
11448   ;; newsrc.5 seems to indicate that the options line can come anywhere
11449   ;; in the file, and that there can be any number of them:
11450   ;;
11451   ;;       An  options  line  starts  with  the  word  options (left-
11452   ;;       justified).  Then there are the list of  options  just  as
11453   ;;       they would be on the readnews command line.  For instance:
11454   ;;
11455   ;;       options -n all !net.sf-lovers !mod.human-nets -r
11456   ;;       options -c -r
11457   ;;
11458   ;;       A string of lines beginning with a space or tab after  the
11459   ;;       initial  options  line  will  be  considered  continuation
11460   ;;       lines.
11461   ;;
11462   ;; For now, we only accept it at the beginning of the file.
11463
11464   (goto-char (point-min))
11465   (skip-chars-forward " \t\n")
11466   (setq gnus-newsrc-options nil)
11467   (while (looking-at "^options[ \t]*\\(.*\\)\n")
11468     ;; handle consecutive options lines
11469     (setq gnus-newsrc-options (concat gnus-newsrc-options
11470                                       (if gnus-newsrc-options "\n\t")
11471                                       (buffer-substring (match-beginning 1)
11472                                                         (match-end 1))))
11473     (forward-line 1)
11474     (while (looking-at "[ \t]+\\(.*\\)\n")
11475       ;; handle subsequent continuation lines of this options line
11476       (setq gnus-newsrc-options (concat gnus-newsrc-options "\n\t"
11477                                         (buffer-substring (match-beginning 1)
11478                                                           (match-end 1))))
11479       (forward-line 1)))
11480   ;; Gather all "-n" options lines.
11481   (let ((start 0)
11482         (result nil))
11483     (if gnus-newsrc-options
11484         (while (and (string-match "^[ \t]*-n\\([^\n]*\\)$"
11485                                   gnus-newsrc-options
11486                                   start)
11487                     (setq start (match-end 0)))
11488           (setq result (concat result
11489                                (and result " ")
11490                                (substring gnus-newsrc-options
11491                                           (match-beginning 1)
11492                                           (match-end 1))))))
11493
11494     (and result (gnus-parse-n-options result))
11495     nil))
11496
11497 (defun gnus-parse-newsrc-body ()
11498   ;; Point has been positioned after the options lines.  We shouldn't
11499   ;; see any more in here.
11500
11501   (let ((subscribe nil)
11502         (read-list nil)
11503         (line (1+ (count-lines (point-min) (point))))
11504         (already-read (> (length gnus-newsrc-alist) 1))
11505         newsgroup
11506         p p2)
11507     (save-restriction
11508       (skip-chars-forward " \t")
11509       (while (not (eobp))
11510         (cond
11511          ((= (following-char) ?\n)
11512           ;; skip blank lines
11513           nil)
11514          (t
11515           (setq p (point))
11516           (skip-chars-forward "^:!\n")
11517           (if (= (following-char) ?\n)
11518               (error "line %d is unparsable in %s" line (buffer-name)))
11519           (setq p2 (point))
11520           (skip-chars-backward " \t")
11521
11522           ;; #### note: we could avoid consing a string here by binding obarray
11523           ;; and reading the newsgroup directly into the gnus-newsrc-hashtb,
11524           ;; then setq'ing newsgroup to symbol-name of that, like we do in
11525           ;; gnus-active-to-gnus-format.
11526           (setq newsgroup (buffer-substring p (point)))
11527           (goto-char p2)
11528
11529           (setq subscribe (= (following-char) ?:))
11530           (setq read-list nil)
11531
11532           (forward-char 1)              ; after : or !
11533           (skip-chars-forward " \t")
11534           (while (not (= (following-char) ?\n))
11535             (skip-chars-forward " \t")
11536             (or
11537              (and (cond
11538                    ((looking-at "\\([0-9]+\\)-\\([0-9]+\\)") ; a range
11539                     (setq read-list
11540                           (cons
11541                            (cons
11542                             (progn
11543                               ;; faster that buffer-substring/string-to-int
11544                               (narrow-to-region (point-min) (match-end 1))
11545                               (read (current-buffer)))
11546                             (progn
11547                               (narrow-to-region (point-min) (match-end 2))
11548                               (forward-char) ; skip over "-"
11549                               (prog1
11550                                   (read (current-buffer))
11551                                 (widen))))
11552                            read-list))
11553                     t)
11554                    ((looking-at "[0-9]+")
11555                     ;; faster that buffer-substring/string-to-int
11556                     (narrow-to-region (point-min) (match-end 0))
11557                     (setq p (read (current-buffer)))
11558                     (widen)
11559                     (setq read-list (cons (cons p p) read-list))
11560                     t)
11561                    (t
11562                     ;; bogus chars in ranges
11563                     nil))
11564                   (progn
11565                     (goto-char (match-end 0))
11566                     (skip-chars-forward " \t")
11567                     (cond ((= (following-char) ?,)
11568                            (forward-char 1)
11569                            t)
11570                           ((= (following-char) ?\n)
11571                            t)
11572                           (t
11573                            ;; bogus char after range
11574                            nil))))
11575              ;; if we get here, the parse failed
11576              (progn
11577                (end-of-line)            ; give up on this line
11578                (ding)
11579                (message "Ignoring bogus line %d for %s in %s"
11580                         line newsgroup (buffer-name))
11581                (sleep-for 1))))
11582           ;; We have already read .newsrc.eld, so we gently update the
11583           ;; data in the hash table with the information we have just
11584           ;; read. 
11585           (let ((info (nth 2 (gnus-gethash newsgroup gnus-newsrc-hashtb)))
11586                 level)
11587             (if info
11588                 (progn
11589                   (setcar (nthcdr 2 info) (nreverse read-list))
11590                   ;; We update the level very gently.  In fact, we
11591                   ;; only change it if there's been a status change
11592                   ;; from subscribed to unsubscribed, or vice versa.
11593                   (setq level (nth 1 info))
11594                   (cond ((and (<= level gnus-level-subscribed) (not subscribe))
11595                          (setq level (if read-list 
11596                                          gnus-level-default-unsubscribed 
11597                                        (1+ gnus-level-default-unsubscribed))))
11598                         ((and (> level gnus-level-subscribed) subscribe)
11599                          (setq level gnus-level-default-subscribed)))
11600                   (setcar (cdr info) level))
11601               (setq gnus-newsrc-alist
11602                     (cons (list newsgroup 
11603                                 (if subscribe
11604                                     gnus-level-default-subscribed 
11605                                   (if read-list 
11606                                       (1+ gnus-level-default-subscribed)
11607                                     gnus-level-default-unsubscribed))
11608                                 (nreverse read-list))
11609                           gnus-newsrc-alist))))))
11610         (setq line (1+ line))
11611         (forward-line 1)))
11612     (setq gnus-newsrc-alist (cdr gnus-newsrc-alist))
11613     (and already-read (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist)))
11614     (gnus-make-hashtable-from-newsrc-alist)
11615     nil))
11616
11617 (defun gnus-parse-n-options (options)
11618   "Parse -n NEWSGROUPS options and return a cons of YES and NO regexps."
11619   (let (yes no yes-or-no out newsgroup)
11620     ;; Parse each newsgroup description such as "comp.all".  Commas
11621     ;; and white spaces can be a newsgroup separator.
11622     (while
11623         (string-match "^[ \t\n,]*\\(!?\\)\\([^- \t\n,][^ \t\n,]*\\)" options)
11624       (setq yes-or-no
11625             (substring options (match-beginning 1) (match-end 1)))
11626       (setq newsgroup
11627             (regexp-quote
11628              (substring options
11629                         (match-beginning 2) (match-end 2))))
11630       (setq options (substring options (match-end 2)))
11631       ;; Rewrite "all" to ".+" not ".*".  ".+" requires at least one
11632       ;; character.
11633       (while (string-match "\\(^\\|\\\\[.]\\)all\\(\\\\[.]\\|$\\)" newsgroup)
11634         (setq newsgroup
11635               (concat (substring newsgroup 0 (match-end 1))
11636                       ".+"
11637                       (substring newsgroup (match-beginning 2)))))
11638       ;; It is yes or no.
11639       (setq out (cons (cons (if (string= yes-or-no "!") 'ignore 'subscribe)
11640                             (concat "^" newsgroup "$")) 
11641                       out)))
11642     (setq gnus-newsrc-options-n out)))
11643
11644 (defun gnus-save-newsrc-file ()
11645   "Save .newsrc file."
11646   ;; Note: We cannot save .newsrc file if all newsgroups are removed
11647   ;; from the variable gnus-newsrc-alist.
11648   (and (or gnus-newsrc-alist gnus-killed-list)
11649        gnus-current-startup-file
11650        (let ((make-backup-files t)
11651              (version-control nil)
11652              (require-final-newline t)) ;Don't ask even if requested.
11653          ;; You can stop or change version control of backup file.
11654          ;; Suggested by jason@violet.berkeley.edu.
11655          (run-hooks 'gnus-save-newsrc-hook)
11656          (save-excursion
11657            (if (or (not gnus-dribble-buffer)
11658                    (not (buffer-name gnus-dribble-buffer))
11659                    (zerop (save-excursion
11660                             (set-buffer gnus-dribble-buffer)
11661                             (buffer-size))))
11662                (message "(No changes need to be saved)")
11663              (if gnus-save-newsrc-file
11664                  (progn
11665                    (message "Saving %s..." gnus-current-startup-file)
11666                    ;; Make backup file of master newsrc.
11667                    (gnus-gnus-to-newsrc-format)
11668                    (message "Saving %s... done" gnus-current-startup-file)))
11669              ;; Quickly loadable .newsrc.
11670              (set-buffer (get-buffer-create " *Gnus-newsrc*"))
11671              (gnus-add-current-to-buffer-list)
11672              (buffer-disable-undo (current-buffer))
11673              (erase-buffer)
11674              (message "Saving %s.eld..." gnus-current-startup-file)
11675              (gnus-gnus-to-quick-newsrc-format)
11676              (write-region 1 (point-max) 
11677                            (concat gnus-current-startup-file ".eld") 
11678                            nil 'nomesg)
11679              (kill-buffer (current-buffer))
11680              (message "Saving %s.eld... done" gnus-current-startup-file)
11681              (gnus-dribble-delete-file))))))
11682
11683 (defun gnus-gnus-to-quick-newsrc-format ()
11684   "Insert Gnus variables such as gnus-newsrc-alist in lisp format."
11685   (insert ";; (ding) Gnus startup file.\n")
11686   (insert ";; Never delete this file - touch .newsrc instead to force Gnus\n")
11687   (insert ";; to read .newsrc.\n")
11688   (let ((variables gnus-variable-list)
11689         (gnus-newsrc-alist (cdr gnus-newsrc-alist))
11690         variable)
11691     ;; insert lisp expressions.
11692     (gnus-compress-newsrc-assoc)
11693     (while variables
11694       (setq variable (car variables))
11695       (and (boundp variable)
11696            (symbol-value variable)
11697            (or gnus-save-killed-list (not (eq variable 'gnus-killed-list)))
11698            (insert "(setq " (symbol-name variable) " '"
11699                    (prin1-to-string (symbol-value variable))
11700                    ")\n"))
11701       (setq variables (cdr variables)))
11702     (gnus-uncompress-newsrc-assoc)))
11703
11704
11705 (defun gnus-gnus-to-newsrc-format ()
11706   ;; Generate and save the .newsrc file.
11707   (let ((newsrc (cdr gnus-newsrc-alist))
11708         info ranges range)
11709     (save-excursion
11710       (set-buffer (create-file-buffer gnus-startup-file))
11711       (buffer-disable-undo (current-buffer))
11712       (erase-buffer)
11713       ;; Write options.
11714       (if gnus-newsrc-options (insert "options " gnus-newsrc-options "\n"))
11715       ;; Write subscribed and unsubscribed.
11716       (while newsrc
11717         (setq info (car newsrc))
11718         (if (not (nth 4 info))          ;Don't write foreign groups to .newsrc.
11719             (progn
11720               (insert (car info) (if (> (nth 1 info) gnus-level-subscribed)
11721                                      "!" ":"))
11722               (if (setq ranges (nth 2 info))
11723                   (progn
11724                     (insert " ")
11725                     (if (not (listp (cdr ranges)))
11726                         (if (= (car ranges) (cdr ranges))
11727                             (insert (int-to-string (car ranges)))
11728                           (insert (int-to-string (car ranges)) "-" 
11729                                   (int-to-string (cdr ranges))))
11730                       (while ranges
11731                         (setq range (car ranges)
11732                               ranges (cdr ranges))
11733                         (if (or (atom range) (= (car range) (cdr range)))
11734                             (insert (int-to-string 
11735                                      (or (and (atom range) range) 
11736                                          (car range))))
11737                           (insert (int-to-string (car range)) "-"
11738                                   (int-to-string (cdr range))))
11739                         (if ranges (insert ","))))))
11740               (insert "\n")))
11741         (setq newsrc (cdr newsrc)))
11742       (write-region 1 (point-max) gnus-current-startup-file nil 'nomesg)
11743       (kill-buffer (current-buffer)))))
11744
11745 (defun gnus-read-descriptions-file ()
11746   (message "Reading descriptions file...")
11747   (if (not (gnus-request-list-newsgroups gnus-select-method))
11748       (progn
11749         (message "Couldn't read newsgroups descriptions")
11750         nil)
11751     (let (group)
11752       (setq gnus-description-hashtb 
11753             (gnus-make-hashtable (length gnus-active-hashtb)))
11754       (save-excursion
11755         (save-restriction
11756           (set-buffer nntp-server-buffer)
11757           (goto-char (point-min))
11758           (delete-non-matching-lines "^[-\\._+A-Za-z0-9]+[ \t]")
11759           (goto-char (point-min))
11760           (if (or (search-forward "\n.\n" nil t)
11761                   (goto-char (point-max)))
11762               (progn
11763                 (beginning-of-line)
11764                 (narrow-to-region (point-min) (point))))
11765           (goto-char (point-min))
11766           (while (not (eobp))
11767             (setq group (let ((obarray gnus-description-hashtb))
11768                           (read (current-buffer))))
11769             (skip-chars-forward " \t")
11770             (set group (buffer-substring 
11771                         (point) (gnus-point-at-eol)))
11772             (forward-line 1))))
11773       (message "Reading descriptions file...done")
11774       t)))
11775
11776 (defun gnus-group-get-description (group)
11777   ;; Get the description of a group by sending XGTITLE to the server.
11778   (and (gnus-request-group-description group)
11779        (save-excursion
11780          (set-buffer nntp-server-buffer)
11781          (goto-char (point-min))
11782          (and (looking-at "[^ \t]+[ \t]+\\(.*\\)")
11783               (buffer-substring (match-beginning 1) (match-end 1))))))
11784
11785 ;;;
11786 ;;; Server
11787 ;;;
11788
11789 (defvar gnus-server-mode-hook nil
11790   "Hook run in `gnus-server-mode' buffers.")
11791
11792 (defconst gnus-server-line-format "     {%(%h:%w%)}\n"
11793   "Format of server lines.
11794 It works along the same lines as a normal formatting string,
11795 with some simple extensions.")
11796
11797 (defvar gnus-server-mode-line-format "(ding) List of servers"
11798   "The format specification for the server mode line.")
11799
11800 (defconst gnus-server-line-format-alist
11801   (list (list ?h 'how ?s)
11802         (list ?n 'name ?s)
11803         (list ?w 'where ?s)
11804         ))
11805
11806 (defconst gnus-server-mode-line-format-alist 
11807   (list (list ?S 'news-server ?s)
11808         (list ?M 'news-method ?s)
11809         (list ?u 'user-defined ?s)))
11810
11811 (defvar gnus-server-line-format-spec nil)
11812 (defvar gnus-server-mode-line-format-spec nil)
11813 (defvar gnus-server-killed-servers nil)
11814
11815 (defvar gnus-server-mode-map nil)
11816 (put 'gnus-server-mode 'mode-class 'special)
11817
11818 (if gnus-server-mode-map
11819     nil
11820   (setq gnus-server-mode-map (make-sparse-keymap))
11821   (suppress-keymap gnus-server-mode-map)
11822   (define-key gnus-server-mode-map " " 'gnus-server-read-server)
11823   (define-key gnus-server-mode-map "q" 'gnus-server-exit)
11824   (define-key gnus-server-mode-map "l" 'gnus-server-list-servers)
11825   (define-key gnus-server-mode-map "k" 'gnus-server-kill-server)
11826   (define-key gnus-server-mode-map "y" 'gnus-server-yank-server)
11827   (define-key gnus-server-mode-map "c" 'gnus-server-copy-server)
11828   (define-key gnus-server-mode-map "a" 'gnus-server-add-server)
11829   (define-key gnus-server-mode-map "e" 'gnus-server-edit-server))
11830
11831 (defun gnus-server-mode ()
11832   "Major mode for listing and editing servers.
11833
11834 All normal editing commands are switched off.
11835 \\<gnus-server-mode-map>
11836
11837 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]'). 
11838
11839 The following commands are available:
11840
11841 \\{gnus-server-mode-map}"
11842   (interactive)
11843 ;  (if gnus-visual (gnus-server-make-menu-bar))
11844   (kill-all-local-variables)
11845   (setq mode-line-modified "-- ")
11846   (make-local-variable 'mode-line-format)
11847   (setq mode-line-format (copy-sequence mode-line-format))
11848   (and (equal (nth 3 mode-line-format) "   ")
11849        (setcar (nthcdr 3 mode-line-format) ""))
11850   (setq major-mode 'gnus-server-mode)
11851   (setq mode-name "Server")
11852 ;  (gnus-group-set-mode-line)
11853   (setq mode-line-process nil)
11854   (use-local-map gnus-server-mode-map)
11855   (buffer-disable-undo (current-buffer))
11856   (setq truncate-lines t)
11857   (setq buffer-read-only t)
11858   (run-hooks 'gnus-server-mode-hook))
11859
11860 (defun gnus-server-insert-server-line (sformat name method)
11861   (let* ((sformat (or sformat gnus-server-line-format-spec))
11862          (how (car method))
11863          (where (nth 1 method))
11864          b)
11865     (beginning-of-line)
11866     (setq b (point))
11867     ;; Insert the text.
11868     (insert (eval sformat))
11869     (add-text-properties 
11870      b (1+ b) (list 'gnus-server (intern name)))))
11871
11872 (defun gnus-server-setup-buffer ()
11873   (if (get-buffer gnus-server-buffer)
11874       ()
11875     (save-excursion
11876       (set-buffer (get-buffer-create gnus-server-buffer))
11877       (gnus-server-mode))))
11878
11879 (defun gnus-server-prepare ()
11880   (setq gnus-server-mode-line-format-spec 
11881         (gnus-parse-format gnus-server-mode-line-format 
11882                            gnus-server-mode-line-format-alist))
11883   (setq gnus-server-line-format-spec 
11884         (gnus-parse-format gnus-server-line-format 
11885                            gnus-server-line-format-alist))
11886   (let ((alist gnus-server-alist)
11887         (buffer-read-only nil))
11888     (erase-buffer)
11889     (while alist
11890       (gnus-server-insert-server-line nil (car (car alist)) (cdr (car alist)))
11891       (setq alist (cdr alist))))
11892   (goto-char (point-min))
11893   (gnus-server-position-cursor))
11894
11895 (defun gnus-server-server-name ()
11896   (let ((server (get-text-property 
11897                  (save-excursion (beginning-of-line) (point)) 'gnus-server)))
11898     (and server (symbol-name server))))
11899
11900 (defalias 'gnus-server-position-cursor 'gnus-goto-colon)
11901
11902 (defvar gnus-winconf-edit-server nil)
11903 (defconst gnus-server-edit-buffer "*Gnus edit server*")
11904
11905 (defun gnus-server-update-server (server)
11906   (save-excursion
11907     (set-buffer gnus-server-buffer)
11908     (let ((buffer-read-only nil)
11909           (info (cdr (assoc server gnus-server-alist))))
11910       (gnus-dribble-enter 
11911        (concat "(gnus-server-set-info \"" server "\" '"
11912                (prin1-to-string info) ")"))
11913       ;; Buffer may be narrowed.
11914       (save-restriction
11915         (widen)
11916         (if (gnus-server-goto-server server)
11917             (delete-region (progn (beginning-of-line) (point))
11918                            (progn (forward-line 1) (point))))
11919         (let ((entry (assoc server gnus-server-alist)))
11920           (gnus-server-insert-server-line nil (car entry) (cdr entry))
11921           (gnus-server-position-cursor))))))
11922
11923 (defun gnus-server-set-info (info)
11924   ;; Enter a select method into the virtual server alist.
11925   (gnus-dribble-enter 
11926    (concat "(gnus-server-set-info '"
11927            (prin1-to-string info) ")"))
11928   (let* ((server (nth 1 info))
11929          (entry (assoc server gnus-server-alist)))
11930     (if entry (setcdr entry info)
11931       (setq gnus-server-alist
11932             (nconc gnus-server-alist (list (cons server info)))))))
11933
11934 (defun gnus-server-to-method (server)
11935   ;; Map virtual server names to select methods.
11936   (or (and (equal server "native") gnus-select-method)
11937       (cdr (assoc server gnus-server-alist))))
11938
11939 (defun gnus-server-extend-method (group method)
11940   ;; This function "extends" a virtual server.  If the server is
11941   ;; "hello", and the select method is ("hello" (my-var "something")) 
11942   ;; in the group "alt.alt", this will result in a new virtual server
11943   ;; called "helly+alt.alt".
11944   (let ((entry
11945          (gnus-copy-sequence 
11946           (if (equal (car method) "native") gnus-select-method
11947               (cdr (assoc (car method) gnus-server-alist))))))
11948     (setcar (cdr entry) (concat (nth 1 entry) "+" group))
11949     (nconc entry (cdr method))))
11950
11951 (defun gnus-server-get-method (group method)
11952   ;; Input either a server name, and extended server name, or a
11953   ;; select method, and return a select method. 
11954   (cond ((stringp method)
11955          (gnus-server-to-method method))
11956         ((stringp (car method))
11957          (gnus-server-extend-method group method))
11958         (t
11959          (gnus-server-add-address method))))
11960
11961 (defun gnus-server-add-address (method)
11962   (let ((method-name (symbol-name (car method))))
11963     (if (and (memq 'address (assoc method-name gnus-valid-select-methods))
11964              (not (assq (intern (concat method-name "-address")) method)))
11965         (append method (list (list (intern (concat method-name "-address"))
11966                                    (nth 1 method))))
11967       method)))
11968
11969 (defun gnus-server-equal (s1 s2)
11970   (or (equal s1 s2)
11971       (and (= (length s1) (length s2))
11972            (progn
11973              (while (and s1 (member (car s1) s2))
11974                (setq s1 (cdr s1)))
11975              (null s1)))))
11976
11977 ;;; Interactive server functions.
11978
11979 (defun gnus-server-kill-server (server)
11980   "Kill the server on the current line."
11981   (interactive (list (gnus-server-server-name)))
11982   (or (gnus-server-goto-server server)
11983       (if server (error "No such server: %s" server)
11984         (error "No server on the current line")))
11985   (let ((buffer-read-only nil))
11986     (delete-region (progn (beginning-of-line) (point))
11987                    (progn (forward-line 1) (point))))
11988   (setq gnus-server-killed-servers 
11989         (cons (assoc server gnus-server-alist) gnus-server-killed-servers))
11990   (setq gnus-server-alist (delq (car gnus-server-killed-servers)
11991                                 gnus-server-alist))
11992   (gnus-server-position-cursor))
11993
11994 (defun gnus-server-yank-server ()
11995   "Yank the previously killed server."
11996   (interactive)
11997   (or gnus-server-killed-servers
11998       (error "No killed servers to be yanked"))
11999   (let ((alist gnus-server-alist)
12000         (server (gnus-server-server-name))
12001         (killed (car gnus-server-killed-servers)))
12002     (if (not server) 
12003         (setq gnus-server-alist (nconc gnus-server-alist (list killed)))
12004       (if (string= server (car (car gnus-server-alist)))
12005           (setq gnus-server-alist (cons killed gnus-server-alist))
12006         (while (and (cdr alist)
12007                     (not (string= server (car (car (cdr alist))))))
12008           (setq alist (cdr alist)))
12009         (setcdr alist (cons killed (cdr alist)))))
12010     (gnus-server-update-server (car killed))
12011     (setq gnus-server-killed-servers (cdr gnus-server-killed-servers))
12012     (gnus-server-position-cursor)))
12013
12014 (defun gnus-server-exit ()
12015   "Return to the group buffer."
12016   (interactive)
12017   (kill-buffer (current-buffer))
12018   (switch-to-buffer gnus-group-buffer))
12019
12020 (defun gnus-server-list-servers ()
12021   "List all available servers."
12022   (interactive)
12023   (let ((cur (gnus-server-server-name)))
12024     (gnus-server-prepare)
12025     (if cur (gnus-server-goto-server cur)
12026       (goto-char (point-max))
12027       (forward-line -1))
12028     (gnus-server-position-cursor)))
12029
12030 (defun gnus-server-copy-server (from to)
12031   (interactive
12032    (list
12033     (or (gnus-server-server-name)
12034         (error "No server on the current line"))
12035     (read-string "Copy to: ")))
12036   (or from (error "No server on current line"))
12037   (or (and to (not (string= to ""))) (error "No name to copy to"))
12038   (and (assoc to gnus-server-alist) (error "%s already exists" to))
12039   (or (assoc from gnus-server-alist) 
12040       (error "%s: no such server" from))
12041   (let ((to-entry (gnus-copy-sequence (assoc from gnus-server-alist))))
12042     (setcar to-entry to)
12043     (setcar (nthcdr 2 to-entry) to)
12044     (setq gnus-server-killed-servers 
12045           (cons to-entry gnus-server-killed-servers))
12046     (gnus-server-yank-server)))
12047
12048 (defun gnus-server-add-server (how where)
12049   (interactive 
12050    (list (intern (completing-read "Server method: "
12051                                   gnus-valid-select-methods nil t))
12052          (read-string "Server name: ")))
12053   (setq gnus-server-killed-servers 
12054         (cons (list where how where) gnus-server-killed-servers))
12055   (gnus-server-yank-server))
12056
12057 (defun gnus-server-goto-server (server)
12058   "Jump to a server line."
12059   (interactive
12060    (list (completing-read "Goto server: " gnus-server-alist nil t)))
12061   (let ((to (text-property-any (point-min) (point-max) 
12062                                'gnus-server (intern server))))
12063     (and to
12064          (progn
12065            (goto-char to) 
12066            (gnus-server-position-cursor)))))
12067
12068 (defun gnus-server-edit-server (server)
12069   "Edit the server on the current line."
12070   (interactive (list (gnus-server-server-name)))
12071   (or server
12072       (error "No server on current line"))
12073   (setq gnus-winconf-edit-server (current-window-configuration))
12074   (pop-to-buffer gnus-server-edit-buffer)
12075   (gnus-add-current-to-buffer-list)
12076   (emacs-lisp-mode)
12077   (use-local-map (copy-keymap (current-local-map)))
12078   (local-set-key "\C-c\C-c" 'gnus-server-edit-server-done)
12079   (erase-buffer)
12080   (insert ";; Type `C-c C-c' after you have edited the server.\n\n")
12081   (insert "(gnus-server-set-info \n  "
12082           (pp-to-string (list 'quote (cdr (assoc server gnus-server-alist))))
12083           "  )\n"))
12084
12085 (defun gnus-server-edit-server-done ()
12086   (interactive)
12087   (set-buffer (get-buffer-create gnus-server-edit-buffer))
12088   (eval-current-buffer)
12089   (kill-buffer (current-buffer))
12090   (and gnus-winconf-edit-server
12091        (set-window-configuration gnus-winconf-edit-server))
12092   (setq gnus-winconf-edit-server nil)
12093   (set-buffer gnus-server-buffer)
12094   (gnus-server-update-server (gnus-server-server-name))
12095   (gnus-server-position-cursor))
12096
12097 ;;; Gnus score functions.
12098
12099 (defvar gnus-global-score-files nil
12100   "*List of global score files and directories.
12101 Set this variable if you want to use people's score files.  One entry
12102 for each score file or each score file directory.  Gnus will decide
12103 by itself what score files are applicable to which group.
12104
12105 Say you want to use the single score file
12106 \"/ftp.ifi.uio.no@ftp:/pub/larsi/ding/score/soc.motss.SCORE\" and all
12107 score files in the \"/ftp.some-where:/pub/score\" directory.
12108
12109  (setq gnus-global-score-files
12110        '(\"/ftp.ifi.uio.no:/pub/larsi/ding/score/soc.motss.SCORE\"
12111          \"/ftp.some-where:/pub/score\"))")
12112
12113 (defun gnus-score-score-files (group)
12114   "Return a list of all possible score files."
12115   (and gnus-global-score-files 
12116        (or gnus-internal-global-score-files
12117            (gnus-score-search-global-directories gnus-global-score-files)))
12118   (setq gnus-kill-files-directory 
12119         (file-name-as-directory
12120          (or gnus-kill-files-directory "~/News/")))
12121   (if (not (file-readable-p gnus-kill-files-directory))
12122       (setq gnus-score-file-list nil)
12123     (if (gnus-use-long-file-name 'not-score)
12124         (if (or (not gnus-score-file-list)
12125                 (gnus-file-newer-than gnus-kill-files-directory
12126                                       (car gnus-score-file-list)))
12127               (setq gnus-score-file-list 
12128                     (cons (nth 5 (file-attributes gnus-kill-files-directory))
12129                           (nreverse 
12130                            (directory-files 
12131                             gnus-kill-files-directory t
12132                             (concat gnus-score-file-suffix "$"))))))
12133       (let ((dir (expand-file-name
12134                   (concat gnus-kill-files-directory
12135                           (gnus-replace-chars-in-string group ?. ?/))))
12136             (mdir (length (expand-file-name gnus-kill-files-directory)))
12137             files)
12138         (if (file-exists-p (concat dir "/" gnus-score-file-suffix))
12139             (setq files (list (concat dir "/" gnus-score-file-suffix))))
12140         (while (>= (1+ (length dir)) mdir)
12141           (and (file-exists-p (concat dir "/all/" gnus-score-file-suffix))
12142                (setq files (cons (concat dir "/all/" gnus-score-file-suffix)
12143                                  files)))
12144           (string-match "/[^/]*$" dir)
12145           (setq dir (substring dir 0 (match-beginning 0))))
12146         (setq gnus-score-file-list 
12147               (cons nil (nreverse files)))))
12148     (cdr gnus-score-file-list)))
12149         
12150 (defun gnus-score-find-bnews (group)
12151   "Return a list of score files for GROUP.
12152 The score files are those files in the ~/News directory which matches
12153 GROUP using BNews sys file syntax."
12154   (let* ((sfiles (append (gnus-score-score-files group)
12155                          gnus-internal-global-score-files))
12156          (kill-dir (file-name-as-directory 
12157                     (expand-file-name gnus-kill-files-directory)))
12158          (klen (length kill-dir))
12159          ofiles not-match regexp)
12160     (save-excursion
12161       (set-buffer (get-buffer-create "*gnus score files*"))
12162       (buffer-disable-undo (current-buffer))
12163       ;; Go through all score file names and create regexp with them
12164       ;; as the source.  
12165       (while sfiles
12166         (erase-buffer)
12167         (insert (car sfiles))
12168         (goto-char 1)
12169         ;; First remove the suffix itself.
12170         (re-search-forward (concat "." gnus-score-file-suffix "$"))
12171         (replace-match "" t t) 
12172         (goto-char 1)
12173         (if (looking-at (regexp-quote kill-dir))
12174             ;; If the file name was just "SCORE", `klen' is one character
12175             ;; too much.
12176             (delete-char (min (1- (point-max)) klen))
12177           (goto-char (point-max))
12178           (search-backward "/")
12179           (delete-region (1+ (point)) (point-min)))
12180         ;; Translate "all" to ".*".
12181         (while (search-forward "all" nil t)
12182           (replace-match ".*" t t))
12183         (goto-char 1)
12184         ;; Deal with "not."s.
12185         (if (looking-at "not.")
12186             (progn
12187               (setq not-match t)
12188               (setq regexp (buffer-substring 5 (point-max))))
12189           (setq regexp (buffer-substring 1 (point-max)))
12190           (setq not-match nil))
12191         ;; Finally - if this resulting regexp matches the group name,
12192         ;; we add this score file to the list of score files
12193         ;; applicable to this group.
12194         (if (or (and not-match
12195                      (not (string-match regexp group)))
12196                 (and (not not-match)
12197                      (string-match regexp group)))
12198             (setq ofiles (cons (car sfiles) ofiles)))
12199         (setq sfiles (cdr sfiles)))
12200       (kill-buffer (current-buffer))
12201       ;; Slight kludge here - the last score file returned should be
12202       ;; the local score file, whether it exists or not. This is so
12203       ;; that any score commands the user enters will go to the right
12204       ;; file, and not end up in some global score file.
12205       (let ((localscore
12206              (expand-file-name
12207               (if (gnus-use-long-file-name 'not-score)
12208                   (concat gnus-kill-files-directory group "." 
12209                           gnus-score-file-suffix)
12210                 (concat gnus-kill-files-directory
12211                         (gnus-replace-chars-in-string group ?. ?/)
12212                         "/" gnus-score-file-suffix)))))
12213         (and (member localscore ofiles)
12214              (delete localscore ofiles))
12215         (setq ofiles (cons localscore ofiles)))
12216       (nreverse ofiles))))
12217
12218 (defun gnus-score-find-single (group)
12219   "Return list containing the score file for GROUP."
12220   (list (gnus-score-file-name group)))
12221
12222 (defun gnus-score-find-hierarchical (group)
12223   "Return list of score files for GROUP.
12224 This includes the score file for the group and all its parents."
12225   (let ((all (copy-sequence '(nil)))
12226         (start 0))
12227     (while (string-match "\\." group (1+ start))
12228       (setq start (match-beginning 0))
12229       (setq all (cons (substring group 0 start) all)))
12230     (setq all (cons group all))
12231     (mapcar 'gnus-score-file-name (nreverse all))))
12232
12233 (defun gnus-possibly-score-headers ()
12234   (let ((func gnus-score-find-score-files-function)
12235         score-files scores)
12236     (and func (not (listp func))
12237          (setq func (list func)))
12238     ;; Go through all the functions for finding score files (or actual
12239     ;; scores) and add them to a list.
12240     (while func
12241       (and (symbolp (car func))
12242            (fboundp (car func))
12243            (setq score-files 
12244                  (nconc score-files (funcall (car func) gnus-newsgroup-name))))
12245       (setq func (cdr func)))
12246     (if score-files (gnus-score-headers score-files))))
12247
12248 (defun gnus-score-file-name (newsgroup)
12249   "Return the name of a score file for NEWSGROUP."
12250   (cond  ((or (null newsgroup)
12251               (string-equal newsgroup ""))
12252           ;; The global score file is placed at top of the directory.
12253           (expand-file-name gnus-score-file-suffix
12254                             (or gnus-kill-files-directory "~/News")))
12255          ((gnus-use-long-file-name 'not-score)
12256           ;; Append ".SCORE" to newsgroup name.
12257           (expand-file-name (concat newsgroup "." gnus-score-file-suffix)
12258                             (or gnus-kill-files-directory "~/News")))
12259          (t
12260           ;; Place "SCORE" under the hierarchical directory.
12261           (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
12262                                     "/" gnus-score-file-suffix)
12263                             (or gnus-kill-files-directory "~/News")))))
12264
12265 (defun gnus-score-search-global-directories (files)
12266   "Scan all global score directories for score files."
12267   ;; Set the variable `gnus-internal-global-score-files' to all
12268   ;; available global score files.
12269   (interactive (list gnus-global-score-files))
12270   (let (out)
12271     (while files
12272       (if (string-match "/$" (car files))
12273           (setq out (nconc (directory-files 
12274                             (car files) t
12275                             (concat gnus-score-file-suffix "$"))))
12276         (setq out (cons (car files) out)))
12277       (setq files (cdr files)))
12278     (setq gnus-internal-global-score-files out)))
12279
12280 (defun gnus-score-default (level)
12281   (if level (prefix-numeric-value level) 
12282     gnus-score-interactive-default-score))
12283
12284 (provide 'gnus)
12285
12286 ;;; gnus.el ends here