*** 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 ?d)
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 '(or (header-chars header) 0) ?d)
1270         (list ?L 'lines ?d)
1271         (list ?I 'indentation ?s)
1272         (list ?T '(if (= level 0) "" (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.62"
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   (gnus-make-thread-indent-array)
1737   (setq gnus-summary-line-format-spec 
1738         (gnus-parse-format
1739          gnus-summary-line-format gnus-summary-line-format-alist))
1740   (gnus-update-summary-mark-positions)
1741   (setq gnus-summary-dummy-line-format-spec 
1742         (gnus-parse-format gnus-summary-dummy-line-format 
1743                            gnus-summary-dummy-line-format-alist))
1744   (setq gnus-group-line-format-spec
1745         (gnus-parse-format 
1746          gnus-group-line-format 
1747          gnus-group-line-format-alist))
1748   (if (and (string-match "%D" gnus-group-line-format)
1749            (not gnus-description-hashtb)
1750            gnus-read-active-file)
1751       (gnus-read-descriptions-file))
1752   (setq gnus-summary-mode-line-format-spec 
1753         (gnus-parse-format gnus-summary-mode-line-format 
1754                            gnus-summary-mode-line-format-alist))
1755   (setq gnus-article-mode-line-format-spec 
1756         (gnus-parse-format gnus-article-mode-line-format 
1757                            gnus-summary-mode-line-format-alist))
1758   (setq gnus-group-mode-line-format-spec 
1759         (gnus-parse-format gnus-group-mode-line-format 
1760                            gnus-group-mode-line-format-alist)))
1761
1762 (defun gnus-update-summary-mark-positions ()
1763   (save-excursion
1764     (let ((gnus-replied-mark 129)
1765           (gnus-score-below-mark 130)
1766           (gnus-score-over-mark 130)
1767           (thread nil)
1768           pos)
1769       (gnus-set-work-buffer)
1770       (gnus-summary-insert-line 
1771        nil [0 "" "" "" "" "" 0 0 ""]  0 nil 128 t nil "" nil 1)
1772       (goto-char (point-min))
1773       (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
1774                                          (- (point) 2)))))
1775       (goto-char (point-min))
1776       (setq pos (cons (cons 'replied (and (search-forward "\201" nil t)
1777                                           (- (point) 2))) pos))
1778       (goto-char (point-min))
1779       (setq pos (cons (cons 'score (and (search-forward "\202" nil t)
1780                                         (- (point) 2))) pos))
1781       (setq gnus-summary-mark-positions pos))))
1782
1783 (defun gnus-format-max-width (form length)
1784   (let ((val (eval form)))
1785     (if (> (length val) length)
1786         (substring val 0 length)
1787       val)))
1788
1789 (defun gnus-set-mouse-face (string)
1790   ;; Set mouse face property on STRING.
1791   (or gnus-xemacs
1792       (put-text-property 0 (length string) 'mouse-face gnus-mouse-face string))
1793   string)
1794
1795 (defun gnus-parse-format (format spec-alist)
1796   ;; This function parses the FORMAT string with the help of the
1797   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
1798   ;; string.  If the FORMAT string contains the specifiers %( and %)
1799   ;; the text between them will have the mouse-face text property.
1800   (if (string-match "\\`\\(.*\\)%(\\(.*\\)%)\\(.*\n?\\)\\'" format)
1801       (if (and gnus-visual gnus-mouse-face)
1802           (let ((pre (substring format (match-beginning 1) (match-end 1)))
1803                 (button (substring format (match-beginning 2) (match-end 2)))
1804                 (post (substring format (match-beginning 3) (match-end 3))))
1805             (list 'concat
1806                   (gnus-parse-simple-format pre spec-alist)
1807                   (list 'gnus-set-mouse-face
1808                         (gnus-parse-simple-format button spec-alist))
1809                   (gnus-parse-simple-format post spec-alist)))
1810         (gnus-parse-simple-format
1811          (concat (substring format (match-beginning 1) (match-end 1))
1812                  (substring format (match-beginning 2) (match-end 2))
1813                  (substring format (match-beginning 3) (match-end 3)))
1814          spec-alist))
1815     (gnus-parse-simple-format format spec-alist)))
1816
1817 (defun gnus-parse-simple-format (format spec-alist)
1818   ;; This function parses the FORMAT string with the help of the
1819   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
1820   ;; string. The list will consist of the symbol `format', a format
1821   ;; specification string, and a list of forms depending on the
1822   ;; SPEC-ALIST.
1823   (let ((max-width 0)
1824         spec flist fstring b newspec max-width elem beg)
1825     (save-excursion
1826       (gnus-set-work-buffer)
1827       (insert format)
1828       (goto-char 1)
1829       (while (re-search-forward "%[-0-9]*\\(,[0-9]*\\)*\\(.\\)\\(.\\)?" nil t)
1830         (setq spec (string-to-char (buffer-substring (match-beginning 2)
1831                                                      (match-end 2))))
1832         ;; First check if there are any specs that look anything like
1833         ;; "%12,12A", ie. with a "max width specification". These have
1834         ;; to be treated specially.
1835         (if (setq beg (match-beginning 1))
1836             (setq max-width 
1837                   (string-to-int 
1838                    (buffer-substring (1+ (match-beginning 1)) (match-end 1))))
1839           (setq max-width 0)
1840           (setq beg (match-beginning 2)))
1841         ;; Find the specification from `spec-alist'.
1842         (if (not (setq elem (cdr (assq spec spec-alist))))
1843             (setq elem '("*" ?s)))
1844         ;; Treat user defined format specifiers specially
1845         (and (eq (car elem) 'user-defined)
1846              (setq elem
1847                    (list 
1848                     (list (intern (concat "gnus-user-format-function-"
1849                                           (buffer-substring
1850                                            (match-beginning 3)
1851                                            (match-end 3))))
1852                           'header)
1853                     ?s))
1854              (delete-region (match-beginning 3) (match-end 3)))
1855         (if (not (zerop max-width))
1856             (let ((el (car elem)))
1857               (cond ((= (car (cdr elem)) ?c) 
1858                      (setq el (list 'char-to-string el)))
1859                     ((= (car (cdr elem)) ?d)
1860                      (numberp el) (setq el (list 'int-to-string el))))
1861               (setq flist (cons (list 'gnus-format-max-width el max-width) 
1862                                 flist))
1863               (setq newspec ?s))
1864           (setq flist (cons (car elem) flist))
1865           (setq newspec (car (cdr elem))))
1866         ;; Remove the old specification (and possibly a ",12" string).
1867         (delete-region beg (match-end 2))
1868         ;; Insert the new specification.
1869         (goto-char beg)
1870         (insert newspec))
1871       (setq fstring (buffer-substring 1 (point-max))))
1872     (cons 'format (cons fstring (nreverse flist)))))
1873
1874 ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
1875 (defun gnus-read-init-file ()
1876   (and gnus-init-file
1877        (or (and (file-exists-p gnus-init-file) 
1878                 ;; Don't try to load a directory.
1879                 (not (file-directory-p gnus-init-file)))
1880            (file-exists-p (concat gnus-init-file ".el"))
1881            (file-exists-p (concat gnus-init-file ".elc")))
1882        (load gnus-init-file nil t)))
1883
1884 (defun gnus-set-work-buffer ()
1885   (if (get-buffer gnus-work-buffer)
1886       (progn
1887         (set-buffer gnus-work-buffer)
1888         (gnus-add-current-to-buffer-list)
1889         (erase-buffer))
1890     (set-buffer (get-buffer-create gnus-work-buffer))
1891     (buffer-disable-undo (current-buffer))))
1892
1893 ;; Article file names when saving.
1894
1895 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
1896   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1897 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group/num.
1898 Otherwise, it is like ~/News/news/group/num."
1899   (let ((default
1900           (expand-file-name
1901            (concat (if (gnus-use-long-file-name 'not-save)
1902                        (gnus-capitalize-newsgroup newsgroup)
1903                      (gnus-newsgroup-directory-form newsgroup))
1904                    "/" (int-to-string (header-number headers)))
1905            (or gnus-article-save-directory "~/News"))))
1906     (if (and last-file
1907              (string-equal (file-name-directory default)
1908                            (file-name-directory last-file))
1909              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
1910         default
1911       (or last-file default))))
1912
1913 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
1914   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1915 If variable `gnus-use-long-file-name' is nil, it is ~/News/news.group/num.
1916 Otherwise, it is like ~/News/news/group/num."
1917   (let ((default
1918           (expand-file-name
1919            (concat (if (gnus-use-long-file-name 'not-save)
1920                        newsgroup
1921                      (gnus-newsgroup-directory-form newsgroup))
1922                    "/" (int-to-string (header-number headers)))
1923            (or gnus-article-save-directory "~/News"))))
1924     (if (and last-file
1925              (string-equal (file-name-directory default)
1926                            (file-name-directory last-file))
1927              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
1928         default
1929       (or last-file default))))
1930
1931 (defun gnus-Plain-save-name (newsgroup headers &optional last-file)
1932   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1933 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group.
1934 Otherwise, it is like ~/News/news/group/news."
1935   (or last-file
1936       (expand-file-name
1937        (if (gnus-use-long-file-name 'not-save)
1938            (gnus-capitalize-newsgroup newsgroup)
1939          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
1940        (or gnus-article-save-directory "~/News"))))
1941
1942 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
1943   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1944 If variable `gnus-use-long-file-name' is nil, it is ~/News/news.group.
1945 Otherwise, it is like ~/News/news/group/news."
1946   (or last-file
1947       (expand-file-name
1948        (if (gnus-use-long-file-name 'not-save)
1949            newsgroup
1950          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
1951        (or gnus-article-save-directory "~/News"))))
1952
1953 ;; For subscribing new newsgroup
1954
1955 (defun gnus-subscribe-hierarchical-interactive (groups)
1956   (let ((groups (sort groups 'string<))
1957         prefixes prefix start rest ans group starts)
1958     (while groups
1959       (setq prefixes (list "^"))
1960       (while (and groups prefixes)
1961         (while (not (string-match (car prefixes) (car groups)))
1962           (setq prefixes (cdr prefixes)))
1963         (setq prefix (car prefixes))
1964         (setq start (1- (length prefix)))
1965         (if (and (string-match "[^\\.]\\." (car groups) start)
1966                  (cdr groups)
1967                  (setq prefix 
1968                        (concat "^" (substring (car groups) 0 (match-end 0))))
1969                  (string-match prefix (car (cdr groups))))
1970             (progn
1971               (setq prefixes (cons prefix prefixes))
1972               (message "Descend hierarchy %s? ([y]nsq): " 
1973                        (substring prefix 1 (1- (length prefix))))
1974               (setq ans (read-char))
1975               (cond ((= ans ?n)
1976                      (while (and groups 
1977                                  (string-match prefix 
1978                                                (setq group (car groups))))
1979                        (setq gnus-killed-list 
1980                              (cons group gnus-killed-list))
1981                        (gnus-sethash group group gnus-killed-hashtb)
1982                        (setq groups (cdr groups)))
1983                      (setq starts (cdr starts)))
1984                     ((= ans ?s)
1985                      (while (and groups 
1986                                  (string-match prefix 
1987                                                (setq group (car groups))))
1988                        (gnus-sethash group group gnus-killed-hashtb)
1989                        (gnus-subscribe-alphabetically (car groups))
1990                        (setq groups (cdr groups)))
1991                      (setq starts (cdr starts)))
1992                     ((= ans ?q)
1993                      (while groups
1994                        (setq group (car groups))
1995                        (setq gnus-killed-list (cons group gnus-killed-list))
1996                        (gnus-sethash group group gnus-killed-hashtb)
1997                        (setq groups (cdr groups))))
1998                     (t nil)))
1999           (message "Subscribe %s? ([n]yq)" (car groups))
2000           (setq ans (read-char))
2001           (setq group (car groups))
2002           (cond ((= ans ?y)
2003                  (gnus-subscribe-alphabetically (car groups))
2004                  (gnus-sethash group group gnus-killed-hashtb))
2005                 ((= ans ?q)
2006                  (while groups
2007                    (setq group (car groups))
2008                    (setq gnus-killed-list (cons group gnus-killed-list))
2009                    (gnus-sethash group group gnus-killed-hashtb)
2010                    (setq groups (cdr groups))))
2011                 (t 
2012                  (setq gnus-killed-list (cons group gnus-killed-list))
2013                  (gnus-sethash group group gnus-killed-hashtb)))
2014           (setq groups (cdr groups)))))))
2015
2016 (defun gnus-subscribe-randomly (newsgroup)
2017   "Subscribe new NEWSGROUP by making it the first newsgroup."
2018   (gnus-subscribe-newsgroup newsgroup))
2019
2020 (defun gnus-subscribe-alphabetically (newgroup)
2021   "Subscribe new NEWSGROUP and insert it in alphabetical order."
2022   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2023   (let ((groups (cdr gnus-newsrc-alist))
2024         before)
2025     (while (and (not before) groups)
2026       (if (string< newgroup (car (car groups)))
2027           (setq before (car (car groups)))
2028         (setq groups (cdr groups))))
2029     (gnus-subscribe-newsgroup newgroup before)))
2030
2031 (defun gnus-subscribe-hierarchically (newgroup)
2032   "Subscribe new NEWSGROUP and insert it in hierarchical newsgroup order."
2033   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2034   (save-excursion
2035     (set-buffer (find-file-noselect gnus-current-startup-file))
2036     (let ((groupkey newgroup)
2037           before)
2038       (while (and (not before) groupkey)
2039         (goto-char (point-min))
2040         (let ((groupkey-re
2041                (concat "^\\(" (regexp-quote groupkey) ".*\\)[!:]")))
2042           (while (and (re-search-forward groupkey-re nil t)
2043                       (progn
2044                         (setq before (buffer-substring
2045                                       (match-beginning 1) (match-end 1)))
2046                         (string< before newgroup)))))
2047         ;; Remove tail of newsgroup name (eg. a.b.c -> a.b)
2048         (setq groupkey
2049               (if (string-match "^\\(.*\\)\\.[^.]+$" groupkey)
2050                   (substring groupkey (match-beginning 1) (match-end 1)))))
2051       (gnus-subscribe-newsgroup newgroup before))))
2052
2053 (defun gnus-subscribe-interactively (newsgroup)
2054   "Subscribe new NEWSGROUP interactively.
2055 It is inserted in hierarchical newsgroup order if subscribed. If not,
2056 it is killed."
2057   (if (gnus-y-or-n-p (format "Subscribe new newsgroup: %s " newsgroup))
2058       (gnus-subscribe-hierarchically newsgroup)
2059     (setq gnus-killed-list (cons newsgroup gnus-killed-list))))
2060
2061 (defun gnus-subscribe-zombies (newsgroup)
2062   "Make new NEWSGROUP a zombie group."
2063   (setq gnus-zombie-list (cons newsgroup gnus-zombie-list)))
2064
2065 (defun gnus-subscribe-newsgroup (newsgroup &optional next)
2066   "Subscribe new NEWSGROUP.
2067 If NEXT is non-nil, it is inserted before NEXT. Otherwise it is made
2068 the first newsgroup."
2069   ;; We subscribe the group by changing its level to `subscribed'.
2070   (gnus-group-change-level 
2071    newsgroup gnus-level-default-subscribed
2072    gnus-level-killed (gnus-gethash (or next "dummy.group") gnus-newsrc-hashtb))
2073   (message "Subscribe newsgroup: %s" newsgroup))
2074
2075 ;; For directories
2076
2077 (defun gnus-newsgroup-directory-form (newsgroup)
2078   "Make hierarchical directory name from NEWSGROUP name."
2079   (let ((newsgroup (substring newsgroup 0)) ;Copy string.
2080         (len (length newsgroup))
2081         (idx 0))
2082     ;; Replace all occurrences of `.' with `/'.
2083     (while (< idx len)
2084       (if (= (aref newsgroup idx) ?.)
2085           (aset newsgroup idx ?/))
2086       (setq idx (1+ idx)))
2087     newsgroup
2088     ))
2089
2090 (defun gnus-make-directory (dir)
2091   "Make DIRECTORY recursively."
2092   (let* ((dir (expand-file-name dir default-directory))
2093          dirs)
2094     (if (string-match "/$" dir)
2095         (setq dir (substring dir 0 (match-beginning 0))))
2096     (while (not (file-exists-p dir))
2097       (setq dirs (cons dir dirs))
2098       (string-match "/[^/]+$" dir)
2099       (setq dir (substring dir 0 (match-beginning 0))))
2100     (while dirs
2101       (make-directory (car dirs))
2102       (setq dirs (cdr dirs)))))
2103
2104 (defun gnus-capitalize-newsgroup (newsgroup)
2105   "Capitalize NEWSGROUP name."
2106   (and (not (zerop (length newsgroup)))
2107        (concat (char-to-string (upcase (aref newsgroup 0)))
2108                (substring newsgroup 1))))
2109
2110 ;; Var
2111
2112 (defun gnus-simplify-subject (subject &optional re-only)
2113   "Remove `Re:' and words in parentheses.
2114 If optional argument RE-ONLY is non-nil, strip `Re:' only."
2115   (let ((case-fold-search t))           ;Ignore case.
2116     ;; Remove `Re:' and `Re^N:'.
2117     (if (string-match "^re:[ \t]*" subject)
2118         (setq subject (substring subject (match-end 0))))
2119     ;; Remove words in parentheses from end.
2120     (or re-only
2121         (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
2122           (setq subject (substring subject 0 (match-beginning 0)))))
2123     ;; Return subject string.
2124     subject))
2125
2126 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
2127 ;; all whitespace.
2128 (defun gnus-simplify-subject-fuzzy (subject)
2129   (let ((case-fold-search t))
2130     (save-excursion
2131       (gnus-set-work-buffer)
2132       (insert subject)
2133       (goto-char (point-min))
2134       (and (looking-at "^[ \t]*re:[ \t]*")
2135            (replace-match "" t t))
2136       (while (re-search-forward "[ \t\n]*([^()]*)[ \t\n]*\\'" nil t)
2137         (replace-match "" t t)
2138         (goto-char (point-min)))
2139       (while (re-search-forward "[ \t]+" nil 'move)
2140         (replace-match " " t t))
2141       ;; the 'move above makes sure we are at (point-max)
2142       (and (= (preceding-char) ? )
2143            (delete-char -1))
2144       (buffer-string))))
2145
2146 ;; Add the current buffer to the list of buffers to be killed on exit. 
2147 (defun gnus-add-current-to-buffer-list ()
2148   (setq gnus-buffer-list (cons (current-buffer) gnus-buffer-list)))
2149
2150 (defun gnus-string> (s1 s2)
2151   (not (or (string< s1 s2)
2152            (string= s1 s2))))
2153
2154 ;; Functions accessing headers.
2155 ;; Functions are more convenient than macros in some cases.
2156
2157 (defun gnus-header-number (header)
2158   (header-number header))
2159
2160 (defun gnus-header-subject (header)
2161   (header-subject header))
2162
2163 (defun gnus-header-from (header)
2164   (header-from header))
2165
2166 (defun gnus-header-xref (header)
2167   (header-xref header))
2168
2169 (defun gnus-header-lines (header)
2170   (header-lines header))
2171
2172 (defun gnus-header-date (header)
2173   (header-date header))
2174
2175 (defun gnus-header-id (header)
2176   (header-id header))
2177
2178 (defun gnus-header-references (header)
2179   (header-references header))
2180
2181 ;;; General various misc type functions.
2182
2183 (defun gnus-clear-system ()
2184   "Clear all variables and buffers."
2185   ;; Clear Gnus variables.
2186   (let ((variables gnus-variable-list))
2187     (while variables
2188       (set (car variables) nil)
2189       (setq variables (cdr variables))))
2190   ;; Clear other internal variables.
2191   (setq gnus-list-of-killed-groups nil
2192         gnus-have-read-active-file nil
2193         gnus-newsrc-alist nil
2194         gnus-newsrc-hashtb nil
2195         gnus-killed-list nil
2196         gnus-zombie-list nil
2197         gnus-killed-hashtb nil
2198         gnus-active-hashtb nil
2199         gnus-moderated-list nil
2200         gnus-description-hashtb nil
2201         gnus-newsgroup-headers nil
2202         gnus-newsgroup-headers-hashtb-by-number nil
2203         gnus-newsgroup-name nil
2204         gnus-server-alist nil
2205         gnus-internal-global-score-files nil
2206         gnus-current-select-method nil)
2207   ;; Reset any score variables.
2208   (and (boundp 'gnus-score-cache)
2209        (set 'gnus-score-cache nil))
2210   ;; Kill the startup file.
2211   (and gnus-current-startup-file
2212        (get-file-buffer gnus-current-startup-file)
2213        (kill-buffer (get-file-buffer gnus-current-startup-file)))
2214   ;; Clear the dribble buffer.
2215   (gnus-dribble-clear)
2216   ;; Kill global KILL file buffer.
2217   (if (get-file-buffer (gnus-newsgroup-kill-file nil))
2218       (kill-buffer (get-file-buffer (gnus-newsgroup-kill-file nil))))
2219   (gnus-kill-buffer nntp-server-buffer)
2220   ;; Kill Gnus buffers.
2221   (while gnus-buffer-list
2222     (gnus-kill-buffer (car gnus-buffer-list))
2223     (setq gnus-buffer-list (cdr gnus-buffer-list))))
2224
2225 (defun gnus-configure-windows (action &optional force)
2226   "Configure Gnus windows according to the next ACTION.
2227 The ACTION is either a symbol, such as `summary', or a
2228 configuration list such as `(1 1 2)'.  If ACTION is not a list,
2229 configuration list is got from the variable gnus-window-configuration.
2230 If FORCE is non-nil, the updating will be done whether it is necessary
2231 or not."
2232   (let* ((windows
2233           (if (listp action) action 
2234             (if (listp gnus-window-configuration)
2235                 (car (cdr (assq action gnus-window-configuration)))
2236               gnus-window-configuration)))
2237          (grpwin (get-buffer-window gnus-group-buffer))
2238          (subwin (get-buffer-window gnus-summary-buffer))
2239          (artwin (get-buffer-window gnus-article-buffer))
2240          (winsum nil)
2241          (height nil)
2242          (grpheight 0)
2243          (subheight 0)
2244          (artheight 0)
2245          ;; Make split-window-vertically leave focus in upper window.
2246          (split-window-keep-point t))
2247     (if (and (symbolp windows) (fboundp windows))
2248         (funcall windows action)
2249       (if (and (not force)
2250                (or (null windows)               ;No configuration is specified.
2251                    (and (eq (null grpwin)
2252                             (zerop (nth 0 windows)))
2253                         (eq (null subwin)
2254                             (zerop (nth 1 windows)))
2255                         (eq (null artwin)
2256                             (zerop (nth 2 windows))))))
2257           ;; No need to change window configuration.
2258           nil
2259         (select-window (or grpwin subwin artwin (selected-window)))
2260         ;; First of all, compute the height of each window.
2261         (cond (gnus-use-full-window
2262                ;; Take up the entire screen.
2263                (delete-other-windows)
2264                (setq height (window-height (selected-window))))
2265               (t
2266                (setq height (+ (if grpwin (window-height grpwin) 0)
2267                                (if subwin (window-height subwin) 0)
2268                                (if artwin (window-height artwin) 0)))))
2269         ;; The group buffer exits always. So, use it to extend the
2270         ;; group window so as to get enough window space.
2271         (switch-to-buffer gnus-group-buffer 'norecord)
2272         (and (get-buffer gnus-summary-buffer)
2273              (delete-windows-on gnus-summary-buffer))
2274         (and (get-buffer gnus-article-buffer)
2275              (delete-windows-on gnus-article-buffer))
2276         ;; Compute expected window height.
2277         (setq winsum (apply (function +) windows))
2278         (if (not (zerop (nth 0 windows)))
2279             (setq grpheight (max window-min-height
2280                                  (/ (* height (nth 0 windows)) winsum))))
2281         (if (not (zerop (nth 1 windows)))
2282             (setq subheight (max window-min-height
2283                                  (/ (* height (nth 1 windows)) winsum))))
2284         (if (not (zerop (nth 2 windows)))
2285             (if gnus-split-window ;hack by erik
2286                 (setq artheight height)
2287               (setq artheight (max window-min-height
2288                                    (/ (* height (nth 2 windows)) winsum)))))
2289         (setq height (+ grpheight subheight artheight))
2290         (enlarge-window (max 0 (- height (window-height (selected-window)))))
2291         ;; Then split the window.
2292         (if (and (not (zerop artheight))
2293                  (or (not (zerop grpheight))
2294                      (not (zerop subheight))))
2295             (if gnus-split-window
2296                 (split-window-horizontally)
2297               (split-window-vertically (+ grpheight subheight))))
2298         (and (not (zerop grpheight))
2299              (not (zerop subheight))
2300              (split-window-vertically grpheight))
2301         ;; Then select buffers in each window.
2302         (or (zerop grpheight)
2303             (progn
2304               (switch-to-buffer gnus-group-buffer 'norecord)
2305               (other-window 1)))
2306         (or (zerop subheight)
2307             (progn
2308               (switch-to-buffer gnus-summary-buffer 'norecord)
2309               (other-window 1)))
2310         (or (zerop artheight)
2311             (progn
2312               ;; If article buffer does not exist, it will be created
2313               ;; and initialized.
2314               (gnus-article-setup-buffer)
2315               (switch-to-buffer gnus-article-buffer 'norecord)
2316               (setq buffer-read-only t) ; !!! Why!?! 
2317               (bury-buffer gnus-summary-buffer)
2318               (bury-buffer gnus-group-buffer)))
2319         (or (zerop subheight)
2320             (progn
2321               (pop-to-buffer gnus-summary-buffer)
2322               ;; It seems that some code in this function will set
2323               ;; buffer-read-only to nil. I have absolutely no idea
2324               ;; why. 
2325               (setq buffer-read-only t))))))) ; !!! Why!?! 
2326
2327 (defun gnus-window-configuration-split (action)
2328   (switch-to-buffer gnus-group-buffer t)
2329   (delete-other-windows)
2330   (split-window-horizontally)
2331   (cond ((memq action '(newsgoups summary))
2332          (if (gnus-buffer-exists-p gnus-summary-buffer)
2333              (switch-to-buffer-other-window gnus-summary-buffer)))
2334         ((eq action 'article)
2335          (switch-to-buffer gnus-summary-buffer t)
2336          (other-window 1)
2337          (gnus-article-setup-buffer)
2338          (switch-to-buffer gnus-article-buffer t))))
2339
2340 (defun gnus-version ()
2341   "Version numbers of this version of Gnus."
2342   (interactive)
2343   (let ((methods gnus-valid-select-methods)
2344         (mess gnus-version)
2345         meth)
2346     ;; Go through all the legal select methods and add their version
2347     ;; numbers to the total version string. Only the backends that are
2348     ;; currently in use will have their message numbers taken into
2349     ;; consideration. 
2350     (while methods
2351       (setq meth (intern (concat (car (car methods)) "-version")))
2352       (and (boundp meth)
2353            (stringp (symbol-value meth))
2354            (setq mess (concat mess "; " (symbol-value meth))))
2355       (setq methods (cdr methods)))
2356     (message mess)))
2357
2358 (defun gnus-info-find-node ()
2359   "Find Info documentation of Gnus."
2360   (interactive)
2361   ;; Enlarge info window if needed.
2362   (cond ((eq major-mode 'gnus-group-mode)
2363          (gnus-configure-windows '(1 0 0)) ;Take all windows.
2364          (pop-to-buffer gnus-group-buffer))
2365         ((eq major-mode 'gnus-summary-mode)
2366          (gnus-configure-windows '(0 1 0)) ;Take all windows.
2367          (pop-to-buffer gnus-summary-buffer)))
2368   (Info-goto-node (car (cdr (assq major-mode gnus-info-nodes)))))
2369
2370 (defun gnus-bug ()
2371   "Send a bug report to the Gnus maintainers."
2372   (interactive)
2373   (pop-to-buffer "*Gnus Bug*")
2374   (erase-buffer)
2375   (mail-mode)
2376   (mail-setup gnus-maintainer "[Gnus Bug Report] " nil nil nil nil)
2377   (goto-char (point-min))
2378   (re-search-forward (concat "^" (regexp-quote mail-header-separator) "$"))
2379   (forward-line 1)
2380   (insert (format "%s\n%s\n\n" (gnus-version) (emacs-version)))
2381   (gnus-debug)
2382   (message ""))
2383
2384 (defun gnus-debug ()
2385   "Attemps to go through the Gnus source file and report what variables have been changed.
2386 The source file has to be in the Emacs load path."
2387   (interactive)
2388   (let ((dirs load-path)
2389         file expr olist)
2390     (while dirs
2391       (if (file-exists-p (setq file (concat (car dirs) "/gnus.el")))
2392           (save-excursion
2393             (setq dirs nil)
2394             (set-buffer (get-buffer-create "*gnus bug info*"))
2395             (buffer-disable-undo (current-buffer))
2396             (erase-buffer)
2397             (insert-file-contents file)
2398             (goto-char (point-min))
2399             (or (search-forward "\n;; Internal variables" nil t)
2400                 (error "Malformed sources"))
2401             (narrow-to-region (point-min) (point))
2402             (goto-char (point-min))
2403             (while (setq expr (condition-case () 
2404                                   (read (current-buffer)) (error nil)))
2405               (and (eq (car expr) 'defvar)
2406                    (stringp (nth 3 expr))
2407                    (not (equal (eval (nth 2 expr))
2408                                (and (boundp (nth 1 expr))
2409                                     (symbol-value (nth 1 expr)))))
2410                    (setq olist (cons (nth 1 expr) olist))))
2411             (kill-buffer (current-buffer)))
2412         (setq dirs (cdr dirs))))
2413     (while olist
2414       (insert "(setq " (symbol-name (car olist)) " '" 
2415               (prin1-to-string (symbol-value (car olist))) ")\n")
2416       (setq olist (cdr olist)))
2417     (insert "\n\n")))
2418
2419 (defun gnus-overload-functions (&optional overloads)
2420   "Overload functions specified by optional argument OVERLOADS.
2421 If nothing is specified, use the variable gnus-overload-functions."
2422   (let ((defs nil)
2423         (overloads (or overloads gnus-overload-functions)))
2424     (while overloads
2425       (setq defs (car overloads))
2426       (setq overloads (cdr overloads))
2427       ;; Load file before overloading function if necessary.  Make
2428       ;; sure we cannot use `require' always.
2429       (and (not (fboundp (car defs)))
2430            (car (cdr (cdr defs)))
2431            (load (car (cdr (cdr defs))) nil 'nomessage))
2432       (fset (car defs) (car (cdr defs))))))
2433
2434 (defun gnus-replace-chars-in-string (string from to)
2435   "Replace characters in STRING from FROM to TO."
2436   (let ((string (substring string 0))   ;Copy string.
2437         (len (length string))
2438         (idx 0))
2439     ;; Replace all occurrences of FROM with TO.
2440     (while (< idx len)
2441       (if (= (aref string idx) from)
2442           (aset string idx to))
2443       (setq idx (1+ idx)))
2444     string))
2445
2446 (defun gnus-days-between (date1 date2)
2447   ;; Return the number of days between date1 and date2.
2448   (- (gnus-day-number date1) (gnus-day-number date2)))
2449
2450 (defun gnus-day-number (date)
2451   (let ((dat (mapcar (lambda (s) (and s (string-to-int s)) )
2452                      (timezone-parse-date date))))
2453     (timezone-absolute-from-gregorian 
2454      (nth 1 dat) (nth 2 dat) (car dat))))
2455
2456 ;; Returns a floating point number that says how many seconds have
2457 ;; lapsed between Jan 1 12:00:00 1970 and DATE.
2458 (defun gnus-seconds-since-epoch (date)
2459   (let* ((tdate (mapcar (lambda (ti) (and ti (string-to-int ti)))
2460                         (timezone-parse-date date)))
2461          (ttime (mapcar (lambda (ti) (and ti (string-to-int ti)))
2462                         (timezone-parse-time
2463                          (aref (timezone-parse-date date) 3))))
2464          (edate (mapcar (lambda (ti) (and ti (string-to-int ti)))
2465                         (timezone-parse-date "Jan 1 12:00:00 1970")))
2466          (tday (- (timezone-absolute-from-gregorian 
2467                    (nth 1 tdate) (nth 2 tdate) (nth 0 tdate))
2468                   (timezone-absolute-from-gregorian 
2469                    (nth 1 edate) (nth 2 edate) (nth 0 edate)))))
2470     (+ (nth 2 ttime)
2471        (* (nth 1 ttime) 60)
2472        (* 1.0 (nth 0 ttime) 60 60)
2473        (* 1.0 tday 60 60 24))))
2474
2475 (defun gnus-file-newer-than (file date)
2476   (let ((fdate (nth 5 (file-attributes file))))
2477     (or (> (car fdate) (car date))
2478         (and (= (car fdate) (car date))
2479              (> (nth 1 fdate) (nth 1 date))))))
2480
2481 ;; Two silly functions to ensure that all `y-or-n-p' questions clear
2482 ;; the echo area.
2483 (defun gnus-y-or-n-p (prompt)
2484   (prog1
2485       (y-or-n-p prompt)
2486     (message "")))
2487
2488 (defun gnus-yes-or-no-p (prompt)
2489   (prog1
2490       (yes-or-no-p prompt)
2491     (message "")))
2492
2493 ;; Check whether to use long file names.
2494 (defun gnus-use-long-file-name (symbol)
2495   ;; The variable has to be set...
2496   (and gnus-use-long-file-name
2497        ;; If it isn't a list, then we return t.
2498        (or (not (listp gnus-use-long-file-name))
2499            ;; If it is a list, and the list contains `symbol', we
2500            ;; return nil.  
2501            (not (memq symbol gnus-use-long-file-name)))))
2502
2503 ;; I suspect there's a better way, but I haven't taken the time to do
2504 ;; it yet. -erik selberg@cs.washington.edu
2505 (defun gnus-dd-mmm (messy-date)
2506   "Return a string like DD-MMM from a big messy string"
2507   (let ((datevec (timezone-parse-date messy-date)))
2508     (format "%2s-%s"
2509             (or (aref datevec 2) "??")
2510             (capitalize
2511              (or (car 
2512                   (nth (1- (string-to-number (aref datevec 1)))
2513                        timezone-months-assoc))
2514                  "???")))))
2515
2516 ;; Make a hash table (default and minimum size is 255).
2517 ;; Optional argument HASHSIZE specifies the table size.
2518 (defun gnus-make-hashtable (&optional hashsize)
2519   (make-vector (if hashsize (max (gnus-create-hash-size hashsize) 255) 255) 0))
2520
2521 ;; Make a number that is suitable for hashing; bigger than MIN and one
2522 ;; less than 2^x.
2523 (defun gnus-create-hash-size (min)
2524   (let ((i 1))
2525     (while (< i min)
2526       (setq i (* 2 i)))
2527     (1- i)))
2528
2529 ;;; List and range functions
2530
2531 (defun gnus-last-element (list)
2532   "Return last element of LIST."
2533   (while (cdr list)
2534     (setq list (cdr list)))
2535   (car list))
2536
2537 (defun gnus-copy-sequence (list)
2538   "Do a complete, total copy of a list."
2539   (if (and (consp list) (not (consp (cdr list))))
2540       (cons (car list) (cdr list))
2541     (mapcar (lambda (elem) (if (consp elem) 
2542                                (if (consp (cdr elem))
2543                                    (gnus-copy-sequence elem)
2544                                  (cons (car elem) (cdr elem)))
2545                              elem))
2546             list)))
2547
2548 (defun gnus-set-difference (list1 list2)
2549   "Return a list of elements of LIST1 that do not appear in LIST2."
2550   (let ((list1 (copy-sequence list1)))
2551     (while list2
2552       (setq list1 (delq (car list2) list1))
2553       (setq list2 (cdr list2)))
2554     list1))
2555
2556 (defun gnus-sorted-complement (list1 list2)
2557   "Return a list of elements of LIST1 that do not appear in LIST2.
2558 Both lists have to be sorted over <."
2559   (let (out)
2560     (while (and list1 list2)
2561       (cond ((= (car list1) (car list2))
2562              (setq list1 (cdr list1)
2563                    list2 (cdr list2)))
2564             ((< (car list1) (car list2))
2565              (setq out (cons (car list1) out))
2566              (setq list1 (cdr list1)))
2567             (t
2568              (setq out (cons (car list2) out))
2569              (setq list2 (cdr list2)))))
2570     (nreverse (append (or list1 list2) out))))
2571
2572 (defun gnus-intersection (list1 list2)      
2573   (let ((result nil))
2574     (while list2
2575       (if (memq (car list2) list1)
2576           (setq result (cons (car list2) result)))
2577       (setq list2 (cdr list2)))
2578     result))
2579
2580 (defun gnus-sorted-intersection (list1 list2)
2581   ;; LIST1 and LIST2 have to be sorted over <.
2582   (let (out)
2583     (while (and list1 list2)
2584       (cond ((= (car list1) (car list2))
2585              (setq out (cons (car list1) out)
2586                    list1 (cdr list1)
2587                    list2 (cdr list2)))
2588             ((< (car list1) (car list2))
2589              (setq list1 (cdr list1)))
2590             (t
2591              (setq list2 (cdr list2)))))
2592     (nreverse out)))
2593
2594 (defun gnus-set-sorted-intersection (list1 list2)
2595   ;; LIST1 and LIST2 have to be sorted over <.
2596   ;; This function modifies LIST1.
2597   (let* ((top (cons nil list1))
2598          (prev top))
2599   (while (and list1 list2)
2600     (cond ((= (car list1) (car list2))
2601            (setq prev list1
2602                  list1 (cdr list1)
2603                  list2 (cdr list2)))
2604           ((< (car list1) (car list2))
2605            (setcdr prev (cdr list1))
2606            (setq list1 (cdr list1)))
2607           (t
2608            (setq list2 (cdr list2)))))
2609   (setcdr prev nil)
2610   (cdr top)))
2611
2612 (defun gnus-compress-sequence (numbers &optional always-list)
2613   "Convert list of numbers to a list of ranges or a single range.
2614 If ALWAYS-LIST is non-nil, this function will always release a list of
2615 ranges."
2616   (let* ((first (car numbers))
2617          (last (car numbers))
2618          result)
2619     (if (null numbers)
2620         nil
2621       (if (not (listp (cdr numbers)))
2622           numbers
2623         (while numbers
2624           (cond ((= last (car numbers)) nil) ;Omit duplicated number
2625                 ((= (1+ last) (car numbers)) ;Still in sequence
2626                  (setq last (car numbers)))
2627                 (t                      ;End of one sequence
2628                  (setq result 
2629                        (cons (if (= first last) first
2630                                (cons first last)) result))
2631                  (setq first (car numbers))
2632                  (setq last  (car numbers))))
2633           (setq numbers (cdr numbers)))
2634         (if (and (not always-list) (null result))
2635             (if (= first last) (list first) (cons first last))
2636           (nreverse (cons (if (= first last) first (cons first last))
2637                           result)))))))
2638
2639 (defalias 'gnus-uncompress-sequence 'gnus-uncompress-range)
2640 (defun gnus-uncompress-range (ranges)
2641   "Expand a list of ranges into a list of numbers.
2642 RANGES is either a single range on the form `(num . num)' or a list of
2643 these ranges."
2644   (let (first last result)
2645     (cond 
2646      ((null ranges)
2647       nil)
2648      ((not (listp (cdr ranges)))
2649       (setq first (car ranges))
2650       (setq last (cdr ranges))
2651       (while (<= first last)
2652         (setq result (cons first result))
2653         (setq first (1+ first)))
2654       (nreverse result))
2655      (t
2656       (while ranges
2657         (if (atom (car ranges))
2658             (if (numberp (car ranges))
2659                 (setq result (cons (car ranges) result)))
2660           (setq first (car (car ranges)))
2661           (setq last  (cdr (car ranges)))
2662           (while (<= first last)
2663             (setq result (cons first result))
2664             (setq first (1+ first))))
2665         (setq ranges (cdr ranges)))
2666       (nreverse result)))))
2667
2668 (defun gnus-add-to-range (ranges list)
2669   "Return a list of ranges that has all articles from both RANGES and LIST.
2670 Note: LIST has to be sorted over `<'."
2671   (if (not ranges)
2672       (gnus-compress-sequence list t)
2673     (setq list (copy-sequence list))
2674     (or (listp (cdr ranges))
2675         (setq ranges (list ranges)))
2676     (let ((out ranges)
2677           ilist lowest highest temp)
2678       (while (and ranges list)
2679         (setq ilist list)
2680         (setq lowest (or (and (atom (car ranges)) (car ranges))
2681                          (car (car ranges))))
2682         (while (and list (cdr list) (< (car (cdr list)) lowest))
2683           (setq list (cdr list)))
2684         (if (< (car ilist) lowest)
2685             (progn
2686               (setq temp list)
2687               (setq list (cdr list))
2688               (setcdr temp nil)
2689               (setq out (nconc (gnus-compress-sequence ilist t) out))))
2690         (setq highest (or (and (atom (car ranges)) (car ranges))
2691                           (cdr (car ranges))))
2692         (while (and list (<= (car list) highest))
2693           (setq list (cdr list)))
2694         (setq ranges (cdr ranges)))
2695       (if list
2696           (setq out (nconc (gnus-compress-sequence list t) out)))
2697       (setq out (sort out (lambda (r1 r2) 
2698                             (< (or (and (atom r1) r1) (car r1))
2699                                (or (and (atom r2) r2) (car r2))))))
2700       (setq ranges out)
2701       (while ranges
2702         (if (atom (car ranges))
2703             (if (cdr ranges)
2704                 (if (atom (car (cdr ranges)))
2705                     (if (= (1+ (car ranges)) (car (cdr ranges)))
2706                         (progn
2707                           (setcar ranges (cons (car ranges) 
2708                                                (car (cdr ranges))))
2709                           (setcdr ranges (cdr (cdr ranges)))))
2710                   (if (= (1+ (car ranges)) (car (car (cdr ranges))))
2711                       (progn
2712                         (setcar (car (cdr ranges)) (car ranges))
2713                         (setcar ranges (car (cdr ranges)))
2714                         (setcdr ranges (cdr (cdr ranges)))))))
2715           (if (cdr ranges)
2716               (if (atom (car (cdr ranges)))
2717                   (if (= (1+ (cdr (car ranges))) (car (cdr ranges)))
2718                       (progn
2719                         (setcdr (car ranges) (car (cdr ranges)))
2720                         (setcdr ranges (cdr (cdr ranges)))))
2721                 (if (= (1+ (cdr (car ranges))) (car (car (cdr ranges))))
2722                     (progn
2723                       (setcdr (car ranges) (cdr (car (cdr ranges))))
2724                       (setcdr ranges (cdr (cdr ranges))))))))
2725         (setq ranges (cdr ranges)))
2726       out)))
2727
2728 (defun gnus-remove-from-range (ranges list)
2729   "Return a list of ranges that has all articles from LIST removed from RANGES.
2730 Note: LIST has to be sorted over `<'."
2731   ;; !!! This function shouldn't look like this, but I've got a headache.
2732   (gnus-compress-sequence 
2733    (gnus-sorted-complement
2734     (gnus-uncompress-range ranges) list)))
2735
2736 (defun gnus-member-of-range (number ranges)
2737   (if (not (listp (cdr ranges)))
2738       (and (>= number (car ranges)) 
2739            (<= number (cdr ranges)))
2740     (let ((not-stop t))
2741       (while (and ranges 
2742                   (if (numberp (car ranges))
2743                       (>= number (car ranges))
2744                     (>= number (car (car ranges))))
2745                   not-stop)
2746         (if (if (numberp (car ranges))
2747                 (= number (car ranges))
2748               (and (>= number (car (car ranges)))
2749                    (<= number (cdr (car ranges)))))
2750             (setq not-stop nil))
2751         (setq ranges (cdr ranges)))
2752       (not not-stop))))
2753
2754 \f
2755 ;;;
2756 ;;; Gnus group mode
2757 ;;;
2758
2759 (defvar gnus-group-mode-map nil)
2760 (defvar gnus-group-make-map nil)
2761 (defvar gnus-group-list-map nil)
2762 (defvar gnus-group-sub-map nil)
2763 (put 'gnus-group-mode 'mode-class 'special)
2764
2765 (if gnus-group-mode-map
2766     nil
2767   (setq gnus-group-mode-map (make-keymap))
2768   (suppress-keymap gnus-group-mode-map)
2769   (define-key gnus-group-mode-map " " 'gnus-group-read-group)
2770   (define-key gnus-group-mode-map "=" 'gnus-group-select-group)
2771   (define-key gnus-group-mode-map "\r" 'gnus-group-select-group)
2772   (define-key gnus-group-mode-map "j" 'gnus-group-jump-to-group)
2773   (define-key gnus-group-mode-map "n" 'gnus-group-next-unread-group)
2774   (define-key gnus-group-mode-map "p" 'gnus-group-prev-unread-group)
2775   (define-key gnus-group-mode-map "\177" 'gnus-group-prev-unread-group)
2776   (define-key gnus-group-mode-map "N" 'gnus-group-next-group)
2777   (define-key gnus-group-mode-map "P" 'gnus-group-prev-group)
2778   (define-key gnus-group-mode-map "\M-n" 'gnus-group-next-unread-group-same-level)
2779   (define-key gnus-group-mode-map "\M-p" 'gnus-group-prev-unread-group-same-level)
2780   (define-key gnus-group-mode-map "," 'gnus-group-best-unread-group)
2781   (define-key gnus-group-mode-map "." 'gnus-group-first-unread-group)
2782   (define-key gnus-group-mode-map "u" 'gnus-group-unsubscribe-current-group)
2783   (define-key gnus-group-mode-map "U" 'gnus-group-unsubscribe-group)
2784   (define-key gnus-group-mode-map "c" 'gnus-group-catchup-current)
2785   (define-key gnus-group-mode-map "C" 'gnus-group-catchup-current-all)
2786   (define-key gnus-group-mode-map "l" 'gnus-group-list-groups)
2787   (define-key gnus-group-mode-map "L" 'gnus-group-list-all-groups)
2788   (define-key gnus-group-mode-map "m" 'gnus-group-mail)
2789   (define-key gnus-group-mode-map "g" 'gnus-group-get-new-news)
2790   (define-key gnus-group-mode-map "\M-g" 'gnus-group-get-new-news-this-group)
2791   (define-key gnus-group-mode-map "R" 'gnus-group-restart)
2792   (define-key gnus-group-mode-map "r" 'gnus-group-read-init-file)
2793   (define-key gnus-group-mode-map "B" 'gnus-group-browse-foreign-server)
2794   (define-key gnus-group-mode-map "b" 'gnus-group-check-bogus-groups)
2795   (define-key gnus-group-mode-map "F" 'gnus-find-new-newsgroups)
2796   (define-key gnus-group-mode-map "\C-c\C-d" 'gnus-group-describe-group)
2797   (define-key gnus-group-mode-map "\M-d" 'gnus-group-describe-all-groups)
2798   (define-key gnus-group-mode-map "\C-c\C-a" 'gnus-group-apropos)
2799   (define-key gnus-group-mode-map "\C-c\M-C-a" 'gnus-group-description-apropos)
2800   (define-key gnus-group-mode-map "a" 'gnus-group-post-news)
2801   (define-key gnus-group-mode-map "\ek" 'gnus-group-edit-local-kill)
2802   (define-key gnus-group-mode-map "\eK" 'gnus-group-edit-global-kill)
2803   (define-key gnus-group-mode-map "\C-k" 'gnus-group-kill-group)
2804   (define-key gnus-group-mode-map "\C-y" 'gnus-group-yank-group)
2805   (define-key gnus-group-mode-map "\C-w" 'gnus-group-kill-region)
2806   (define-key gnus-group-mode-map "\C-x\C-t" 'gnus-group-transpose-groups)
2807   (define-key gnus-group-mode-map "\C-c\C-l" 'gnus-group-list-killed)
2808   (define-key gnus-group-mode-map "\C-c\C-x" 'gnus-group-expire-articles)
2809   (define-key gnus-group-mode-map "\C-c\M-\C-x" 'gnus-group-expire-all-groups)
2810   (define-key gnus-group-mode-map "V" 'gnus-version)
2811   (define-key gnus-group-mode-map "s" 'gnus-group-save-newsrc)
2812   (define-key gnus-group-mode-map "z" 'gnus-group-suspend)
2813   (define-key gnus-group-mode-map "Z" 'gnus-group-clear-dribble)
2814   (define-key gnus-group-mode-map "q" 'gnus-group-exit)
2815   (define-key gnus-group-mode-map "Q" 'gnus-group-quit)
2816   (define-key gnus-group-mode-map "\M-f" 'gnus-group-fetch-faq)
2817   (define-key gnus-group-mode-map "?" 'gnus-group-describe-briefly)
2818   (define-key gnus-group-mode-map "\C-c\C-i" 'gnus-info-find-node)
2819   (define-key gnus-group-mode-map "\M-e" 'gnus-group-edit-group-method)
2820   (define-key gnus-group-mode-map "^" 'gnus-group-enter-server-mode)
2821   (define-key gnus-group-mode-map
2822     (if gnus-xemacs [button2] [mouse-2]) 'gnus-mouse-pick-group)
2823   (define-key gnus-group-mode-map "<" 'beginning-of-buffer)
2824   (define-key gnus-group-mode-map ">" 'end-of-buffer)
2825
2826   (define-prefix-command 'gnus-group-make-map)
2827   (define-key gnus-group-mode-map "M" 'gnus-group-make-map)
2828   (define-key gnus-group-make-map "d" 'gnus-group-make-directory-group)
2829   (define-key gnus-group-make-map "h" 'gnus-group-make-help-group)
2830   (define-key gnus-group-make-map "a" 'gnus-group-make-archive-group)
2831   (define-key gnus-group-make-map "k" 'gnus-group-make-kiboze-group)
2832   (define-key gnus-group-make-map "m" 'gnus-group-make-group)
2833   (define-key gnus-group-make-map "E" 'gnus-group-edit-group)
2834   (define-key gnus-group-make-map "e" 'gnus-group-edit-group-method)
2835   (define-key gnus-group-make-map "p" 'gnus-group-edit-group-parameters)
2836
2837   (define-prefix-command 'gnus-group-list-map)
2838   (define-key gnus-group-mode-map "G" 'gnus-group-list-map)
2839   (define-key gnus-group-list-map "k" 'gnus-group-list-killed)
2840   (define-key gnus-group-list-map "z" 'gnus-group-list-zombies)
2841   (define-key gnus-group-list-map "s" 'gnus-group-list-groups)
2842   (define-key gnus-group-list-map "u" 'gnus-group-list-all-groups)
2843   (define-key gnus-group-list-map "a" 'gnus-group-apropos)
2844   (define-key gnus-group-list-map "d" 'gnus-group-description-apropos)
2845   (define-key gnus-group-list-map "m" 'gnus-group-list-matching)
2846   (define-key gnus-group-list-map "M" 'gnus-group-list-all-matching)
2847
2848   (define-prefix-command 'gnus-group-sub-map)
2849   (define-key gnus-group-mode-map "S" 'gnus-group-sub-map)
2850   (define-key gnus-group-sub-map "l" 'gnus-group-set-current-level)
2851   (define-key gnus-group-sub-map "t" 'gnus-group-unsubscribe-current-group)
2852   (define-key gnus-group-sub-map "s" 'gnus-group-unsubscribe-group)
2853   (define-key gnus-group-sub-map "k" 'gnus-group-kill-group)
2854   (define-key gnus-group-sub-map "y" 'gnus-group-yank-group)
2855   (define-key gnus-group-sub-map "w" 'gnus-group-kill-region)
2856   (define-key gnus-group-sub-map "z" 'gnus-group-kill-all-zombies))
2857
2858 (defun gnus-group-mode ()
2859   "Major mode for reading news.
2860
2861 All normal editing commands are switched off.
2862 \\<gnus-group-mode-map>
2863 The group buffer lists (some of) the groups available.  For instance,
2864 `\\[gnus-group-list-groups]' will list all subscribed groups with unread articles, while `\\[gnus-group-list-zombies]'
2865 lists all zombie groups. 
2866
2867 Groups that are displayed can be entered with `\\[gnus-group-read-group]'.  To subscribe 
2868 to a group not displayed, type `\\[gnus-group-unsubscribe-group]'. 
2869
2870 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]'). 
2871
2872 The following commands are available:
2873
2874 \\{gnus-group-mode-map}"
2875   (interactive)
2876   (if gnus-visual (gnus-group-make-menu-bar))
2877   (kill-all-local-variables)
2878   (setq mode-line-modified "-- ")
2879   (make-local-variable 'mode-line-format)
2880   (setq mode-line-format (copy-sequence mode-line-format))
2881   (and (equal (nth 3 mode-line-format) "   ")
2882        (setcar (nthcdr 3 mode-line-format) ""))
2883   (setq major-mode 'gnus-group-mode)
2884   (setq mode-name "Group")
2885   (gnus-group-set-mode-line)
2886   (setq mode-line-process nil)
2887   (use-local-map gnus-group-mode-map)
2888   (buffer-disable-undo (current-buffer))
2889   (setq truncate-lines t)
2890   (setq buffer-read-only t)
2891   (run-hooks 'gnus-group-mode-hook))
2892
2893 (defun gnus-mouse-pick-group (e)
2894   (interactive "e")
2895   (mouse-set-point e)
2896   (gnus-group-read-group nil))
2897
2898 ;;;###autoload
2899 (defun gnus-no-server (&optional arg)
2900   "Read network news.
2901 If ARG is a positive number, Gnus will use that as the
2902 startup level. If ARG is nil, Gnus will be started at level 2. 
2903 If ARG is non-nil and not a positive number, Gnus will
2904 prompt the user for the name of an NNTP server to use.
2905 As opposed to `gnus', this command will not connect to the local server."
2906   (interactive "P")
2907   (gnus (or arg (1- gnus-level-default-subscribed)) t))
2908
2909 (defalias '\(ding\) 'gnus)
2910
2911 ;;;###autoload
2912 (defun gnus (&optional arg dont-connect)
2913   "Read network news.
2914 If ARG is non-nil and a positive number, Gnus will use that as the
2915 startup level. If ARG is non-nil and not a positive number, Gnus will
2916 prompt the user for the name of an NNTP server to use."
2917   (interactive "P")
2918   (if (get-buffer gnus-group-buffer)
2919       (progn
2920         (switch-to-buffer gnus-group-buffer)
2921         (gnus-group-get-new-news))
2922     (gnus-clear-system)
2923     (gnus-read-init-file)
2924     (let ((level (and arg (numberp arg) (> arg 0) arg))
2925           did-connect)
2926       (unwind-protect
2927           (progn
2928             (switch-to-buffer gnus-group-buffer)
2929             (gnus-add-current-to-buffer-list)
2930             (gnus-group-mode)
2931             (or dont-connect 
2932                 (setq did-connect
2933                       (gnus-start-news-server (and arg (not level))))))
2934         (if (and (not dont-connect) 
2935                  (not did-connect))
2936             (gnus-group-quit)
2937           (run-hooks 'gnus-startup-hook)
2938           ;; NNTP server is successfully open. 
2939           (gnus-update-format-specifications)
2940           (let ((buffer-read-only nil))
2941             (erase-buffer)
2942             (if (not gnus-inhibit-startup-message)
2943                 (progn
2944                   (gnus-group-startup-message)
2945                   (sit-for 0))))
2946           (gnus-setup-news nil level)
2947           (and gnus-use-dribble-file (gnus-dribble-open))
2948           (or t (not gnus-novice-user)
2949               gnus-expert-user
2950               (gnus-group-describe-briefly)) ;Show brief help message.
2951           (gnus-group-list-groups level))))))
2952
2953 (defun gnus-group-startup-message (&optional x y)
2954   "Insert startup message in current buffer."
2955   ;; Insert the message.
2956   (erase-buffer)
2957   (insert
2958    (format "
2959     %s
2960            A newsreader 
2961       for GNU Emacs
2962
2963         Based on GNUS 
2964              written by 
2965      Masanobu UMEDA
2966
2967     Lars Magne 
2968          Ingebrigtsen 
2969       larsi@ifi.uio.no
2970
2971            gnus-version))
2972   ;; And then hack it.
2973   ;; 18 is the longest line.
2974   (indent-rigidly (point-min) (point-max) 
2975                   (/ (max (- (window-width) (or x 28)) 0) 2))
2976   (goto-char (point-min))
2977   ;; +4 is fuzzy factor.
2978   (insert-char ?\n (/ (max (- (window-height) (or y 12)) 0) 2)))
2979
2980 (defun gnus-group-setup-buffer ()
2981   (or (get-buffer gnus-group-buffer)
2982       (progn
2983         (switch-to-buffer gnus-group-buffer)
2984         (gnus-add-current-to-buffer-list)
2985         (gnus-group-mode))))
2986
2987 (defun gnus-group-list-groups (level &optional unread)
2988   "List newsgroups with level LEVEL or lower that have unread alticles.
2989 Default is all subscribed groups.
2990 If argument UNREAD is non-nil, groups with no unread articles are also listed."
2991   (interactive (list (and current-prefix-arg
2992                           (prefix-numeric-value current-prefix-arg))))
2993   (setq level (or level gnus-group-default-list-level gnus-level-subscribed))
2994   (gnus-group-setup-buffer)     ;May call from out of group buffer
2995   (let ((case-fold-search nil)
2996         (group (gnus-group-group-name)))
2997     (funcall gnus-group-prepare-function level unread nil)
2998     (if (zerop (buffer-size))
2999         (message gnus-no-groups-message)
3000       (goto-char (point-min))
3001       (if (not group)
3002           ;; Go to the first group with unread articles.
3003           (gnus-group-search-forward nil nil nil t)
3004         ;; Find the right group to put point on. If the current group
3005         ;; has disapeared in the new listing, try to find the next
3006         ;; one. If no next one can be found, just leave point at the
3007         ;; first newsgroup in the buffer.
3008         (if (not (gnus-goto-char
3009                   (text-property-any (point-min) (point-max) 
3010                                      'gnus-group (intern group))))
3011             (let ((newsrc (nthcdr 3 (gnus-gethash group gnus-newsrc-hashtb))))
3012               (while (and newsrc
3013                           (not (gnus-goto-char 
3014                                 (text-property-any 
3015                                  (point-min) (point-max) 'gnus-group 
3016                                  (intern (car (car newsrc)))))))
3017                 (setq newsrc (cdr newsrc)))
3018               (or newsrc (progn (goto-char (point-max))
3019                                 (forward-line -1))))))
3020       ;; Adjust cursor point.
3021       (gnus-group-position-cursor))))
3022
3023 (defun gnus-group-prepare-flat (level &optional all lowest regexp) 
3024   "List all newsgroups with unread articles of level LEVEL or lower.
3025 If ALL is non-nil, list groups that have no unread articles.
3026 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher.
3027 If REGEXP, only list groups matching REGEXP."
3028   (set-buffer gnus-group-buffer)
3029   (let ((buffer-read-only nil)
3030         (newsrc (cdr gnus-newsrc-alist))
3031         (lowest (or lowest 1))
3032         info clevel unread group)
3033     (erase-buffer)
3034     (if (< lowest gnus-level-zombie)
3035         ;; List living groups.
3036         (while newsrc
3037           (setq info (car newsrc)
3038                 group (car info)
3039                 newsrc (cdr newsrc)
3040                 unread (car (gnus-gethash group gnus-newsrc-hashtb)))
3041           (and unread ; This group might be bogus
3042                (or (not regexp)
3043                    (string-match regexp group))
3044                (<= (setq clevel (car (cdr info))) level) 
3045                (>= clevel lowest)
3046                (or all            ; We list all groups?
3047                    (eq unread t)  ; We list unactivated groups
3048                    (> unread 0)   ; We list groups with unread articles
3049                    (cdr (assq 'tick (nth 3 info)))) ; And groups with tickeds
3050                (gnus-group-insert-group-line 
3051                 nil group (car (cdr info)) (nth 3 info) unread (nth 4 info)))))
3052
3053     ;; List dead groups.
3054     (and (>= level gnus-level-zombie) (<= lowest gnus-level-zombie)
3055          (gnus-group-prepare-flat-list-dead 
3056           (setq gnus-zombie-list (sort gnus-zombie-list 'string<)) 
3057           gnus-level-zombie ?Z
3058           regexp))
3059     (and (>= level gnus-level-killed) (<= lowest gnus-level-killed)
3060          (gnus-group-prepare-flat-list-dead 
3061           (setq gnus-killed-list (sort gnus-killed-list 'string<)) 
3062           gnus-level-killed ?K regexp))
3063
3064     (gnus-group-set-mode-line)
3065     (setq gnus-have-all-newsgroups all)
3066     (run-hooks 'gnus-group-prepare-hook)))
3067
3068 (defun gnus-group-prepare-flat-list-dead (groups level mark regexp)
3069   ;; List zombies and killed lists somehwat faster, which was
3070   ;; suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>. It does
3071   ;; this by ignoring the group format specification altogether.
3072   (let (group beg)
3073     (while groups
3074       (setq group (car groups)
3075             groups (cdr groups))
3076       (if (or (not regexp)
3077               (string-match regexp group))
3078           (progn
3079             (setq beg (point))
3080             (insert (format " %c    *: %s\n" mark group))
3081             (add-text-properties 
3082              beg (1+ beg) 
3083              (list 'gnus-group (intern group)
3084                    'gnus-unread t
3085                    'gnus-level level)))))))
3086
3087 (defun gnus-group-real-name (group)
3088   "Find the real name of a foreign newsgroup."
3089   (if (string-match "^[^:]+:" group)
3090       (substring group (match-end 0))
3091     group))
3092
3093 (defun gnus-group-prefixed-name (group method)
3094   "Return the whole name from GROUP and METHOD."
3095   (and (stringp method) (setq method (gnus-server-to-method method)))
3096   (concat (format "%s" (car method))
3097           (if (and 
3098                (assoc (format "%s" (car method)) (gnus-methods-using 'address))
3099                (not (string= (nth 1 method) "")))
3100               (concat "+" (nth 1 method)))
3101           ":" group))
3102
3103 (defun gnus-group-real-prefix (group)
3104   "Return the prefix of the current group name."
3105   (if (string-match "^[^:]+:" group)
3106       (substring group 0 (match-end 0))
3107     ""))
3108
3109 (defun gnus-group-method-name (group)
3110   "Return the method used for selecting GROUP."
3111   (let ((prefix (gnus-group-real-prefix group)))
3112     (if (equal prefix "")
3113         gnus-select-method
3114       (if (string-match "^[^\\+]+\\+" prefix)
3115           (list (intern (substring prefix 0 (1- (match-end 0))))
3116                 (substring prefix (match-end 0) (1- (length prefix))))
3117         (list (intern (substring prefix 0 (1- (length prefix)))) "")))))
3118
3119 (defun gnus-group-foreign-p (group)
3120   "Return nil if GROUP is native, non-nil if it is foreign."
3121   (string-match ":" group))
3122
3123 (defun gnus-group-set-info (info &optional method-only-group part)
3124   (let* ((entry (gnus-gethash
3125                  (or method-only-group (car info)) gnus-newsrc-hashtb))
3126          (part-info info)
3127          (info (if method-only-group (nth 2 entry) info)))
3128     (if (not method-only-group)
3129         ()
3130       (or entry
3131           (error "Trying to change non-existant group %s" method-only-group))
3132       ;; We have recevied parts of the actual group info - either the
3133       ;; select method or the group parameters.  We first check
3134       ;; whether we have to extend the info, and if so, do that.
3135       (let ((len (length info))
3136             (total (if (eq part 'method) 5 6)))
3137         (and (< len total)
3138              (setcdr (nthcdr (1- len) info)
3139                      (make-list (- total len) nil)))
3140         ;; Then we enter the new info.
3141         (setcar (nthcdr (1- total) info) part-info)))
3142     ;; We uncompress some lists of marked articles.
3143     (let (marked)
3144       (if (not (setq marked (nth 3 info)))
3145           ()
3146         (while marked
3147           (or (eq 'score (car (car marked)))
3148               (eq 'bookmark (car (car marked)))
3149               (eq 'killed (car (car marked)))
3150               (setcdr (car marked) 
3151                       (gnus-uncompress-range (cdr (car marked)))))
3152           (setq marked (cdr marked)))))
3153     (if entry
3154         ()
3155       ;; This is a new group, so we just create it.
3156       (save-excursion
3157         (set-buffer gnus-group-buffer)
3158         (if (nth 4 info)
3159             ;; It's a foreign group...
3160             (gnus-group-make-group 
3161              (gnus-group-real-name (car info))
3162              (prin1-to-string (car (nth 4 info)))
3163              (nth 1 (nth 4 info)))
3164           ;; It's a native group.
3165           (gnus-group-make-group
3166            (car info)
3167            (prin1-to-string (car gnus-select-method))
3168            (nth 1 gnus-select-method)))
3169         (message "Note: New group created")
3170         (setq entry 
3171               (gnus-gethash (gnus-group-prefixed-name 
3172                              (gnus-group-real-name (car info))
3173                              (or (nth 4 info) gnus-select-method))
3174                             gnus-newsrc-hashtb))))
3175     ;; Whether it was a new group or not, we now have the entry, so we
3176     ;; can do the update.
3177     (if entry
3178         (progn
3179           (setcar (nthcdr 2 entry) info)
3180           (if (and (not (eq (car entry) t)) 
3181                    (gnus-gethash (car info) gnus-active-hashtb))
3182               (let ((marked (nth 3 info)))
3183                 (setcar entry 
3184                         (max 0 (- (length (gnus-list-of-unread-articles 
3185                                            (car info)))
3186                                   (length (cdr (assq 'tick marked)))
3187                                   (length (cdr (assq 'dormant marked)))))))))
3188       (error "No such group: %s" (car info)))))
3189
3190 (defun gnus-group-set-method-info (group select-method)
3191   (gnus-group-set-info select-method group 'method))
3192
3193 (defun gnus-group-set-params-info (group params)
3194   (gnus-group-set-info params group 'params))
3195
3196 (defun gnus-group-update-group-line ()
3197   "This function updates the current line in the newsgroup buffer and
3198 moves the point to the colon."
3199   (let* ((buffer-read-only nil)
3200          (group (gnus-group-group-name))
3201          (entry (and group (gnus-gethash group gnus-newsrc-hashtb))))
3202     (if entry
3203         (gnus-dribble-enter 
3204          (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
3205                  ")")))
3206     (beginning-of-line)
3207     (delete-region (point) (progn (forward-line 1) (point)))
3208     (gnus-group-insert-group-line-info group)
3209     (forward-line -1)
3210     (gnus-group-position-cursor)))
3211
3212 (defun gnus-group-insert-group-line-info (group)
3213   (let ((entry (gnus-gethash group gnus-newsrc-hashtb)) 
3214         active info)
3215     (if entry
3216         (progn
3217           (setq info (nth 2 entry))
3218           (gnus-group-insert-group-line 
3219            nil group (nth 1 info) (nth 3 info) (car entry) (nth 4 info)))
3220       (setq active (gnus-gethash group gnus-active-hashtb))
3221       (gnus-group-insert-group-line 
3222        nil group (if (member group gnus-zombie-list) gnus-level-zombie
3223                    gnus-level-killed)
3224        nil (if active (- (1+ (cdr active)) (car active)) 0) nil))))
3225
3226 (defun gnus-group-insert-group-line (gformat group level marked number method)
3227   (let* ((gformat (or gformat gnus-group-line-format-spec))
3228          (active (gnus-gethash group gnus-active-hashtb))
3229          (number-total (if active (1+ (- (cdr active) (car active))) 0))
3230          (number-of-dormant (length (cdr (assq 'dormant marked))))
3231          (number-of-ticked (length (cdr (assq 'tick marked))))
3232          (number-of-ticked-and-dormant
3233           (+ number-of-ticked number-of-dormant))
3234          (number-of-unread-unticked 
3235           (if (numberp number) (int-to-string (max 0 number))
3236             "*"))
3237          (number-of-read
3238           (if (numberp number)
3239               (max 0 (- number-total number))
3240             "*"))
3241          (subscribed (cond ((<= level gnus-level-subscribed) ? )
3242                            ((<= level gnus-level-unsubscribed) ?U)
3243                            ((= level gnus-level-zombie) ?Z)
3244                            (t ?K)))
3245          (qualified-group (gnus-group-real-name group))
3246          (newsgroup-description 
3247           (if gnus-description-hashtb
3248               (or (gnus-gethash group gnus-description-hashtb) "")
3249             ""))
3250          (moderated (if (member group gnus-moderated-list) ?m ? ))
3251          (moderated-string (if (eq moderated ?m) "(m)" ""))
3252          (method (gnus-server-get-method group method))
3253          (news-server (or (car (cdr method)) ""))
3254          (news-method (or (car method) ""))
3255          (news-method-string 
3256           (if method (format "(%s:%s)" (car method) (car (cdr method))) ""))
3257          (marked (if (and 
3258                       (numberp number) 
3259                       (zerop number)
3260                       (> number-of-ticked 0))
3261                      ?* ? ))
3262          (number (if (eq number t) "*" (+ number number-of-dormant 
3263                                           number-of-ticked)))
3264          (buffer-read-only nil)
3265          b)
3266     (beginning-of-line)
3267     (setq b (point))
3268     ;; Insert the text.
3269     (insert (eval gformat))
3270
3271     (add-text-properties 
3272      b (1+ b) (list 'gnus-group (intern group)
3273                     'gnus-unread (if (numberp number)
3274                                      (string-to-int number-of-unread-unticked)
3275                                    t)
3276                     'gnus-marked marked
3277                     'gnus-level level))))
3278
3279 (defun gnus-group-update-group (group &optional visible-only)
3280   "Update newsgroup info of GROUP.
3281 If VISIBLE-ONLY is non-nil, the group won't be displayed if it isn't already."
3282   (save-excursion
3283     (set-buffer gnus-group-buffer)
3284     (let ((buffer-read-only nil)
3285           visible)
3286       (let ((entry (gnus-gethash group gnus-newsrc-hashtb)))
3287         (if entry
3288             (gnus-dribble-enter 
3289              (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
3290                      ")"))))
3291       ;; Buffer may be narrowed.
3292       (save-restriction
3293         (widen)
3294         ;; Search a line to modify.  If the buffer is large, the search
3295         ;; takes long time.  In most cases, current point is on the line
3296         ;; we are looking for.  So, first of all, check current line. 
3297         (if (or (progn
3298                   (beginning-of-line)
3299                   (eq (get-text-property (point) 'gnus-group)
3300                       (intern group)))
3301                 (progn
3302                   (gnus-goto-char 
3303                    (text-property-any 
3304                     (point-min) (point-max) 'gnus-group (intern group)))))
3305             ;; GROUP is listed in current buffer. So, delete old line.
3306             (progn
3307               (setq visible t)
3308               (beginning-of-line)
3309               (delete-region (point) (progn (forward-line 1) (point))))
3310           ;; No such line in the buffer, find out where it's supposed to
3311           ;; go, and insert it there (or at the end of the buffer).
3312           ;; Fix by Per Abrahamsen <amanda@iesd.auc.dk>.
3313           (or visible-only
3314               (let ((entry (cdr (gnus-gethash group gnus-newsrc-hashtb))))
3315                 (while (and entry
3316                             (car entry)
3317                             (not
3318                              (gnus-goto-char
3319                               (text-property-any
3320                                (point-min) (point-max) 
3321                                'gnus-group (intern (car (car entry)))))))
3322                   (setq entry (cdr entry)))
3323                 (or entry (goto-char (point-max)))))))
3324       (if (or visible (not visible-only))
3325           (gnus-group-insert-group-line-info group))
3326       (gnus-group-set-mode-line))))
3327
3328 (defun gnus-group-set-mode-line ()
3329   (if (memq 'group gnus-updated-mode-lines)
3330       (let* ((gformat (or gnus-group-mode-line-format-spec
3331                           (setq gnus-group-mode-line-format-spec
3332                                 (gnus-parse-format 
3333                                  gnus-group-mode-line-format 
3334                                  gnus-group-mode-line-format-alist))))
3335              (news-server (car (cdr gnus-select-method)))
3336              (news-method (car gnus-select-method))
3337              (mode-string (eval gformat))
3338              (max-len 60))
3339         (if (> (length mode-string) max-len) 
3340             (setq mode-string (substring mode-string 0 (- max-len 4))))
3341         (setq mode-line-buffer-identification mode-string)
3342         (set-buffer-modified-p t))))
3343
3344 (defun gnus-group-group-name ()
3345   "Get the name of the newsgroup on the current line."
3346   (let ((group (get-text-property (gnus-point-at-bol) 'gnus-group)))
3347     (and group (symbol-name group))))
3348
3349 (defun gnus-group-group-level ()
3350   "Get the level of the newsgroup on the current line."
3351   (get-text-property (gnus-point-at-bol) 'gnus-level))
3352
3353 (defun gnus-group-search-forward (&optional backward all level first-too)
3354   "Find the next newsgroup with unread articles.
3355 If BACKWARD is non-nil, find the previous newsgroup instead.
3356 If ALL is non-nil, just find any newsgroup.
3357 If LEVEL is non-nil, find group with level LEVEL, or higher if no such
3358 group exists.
3359 If FIRST-TOO, the current line is also eligeble as a target."
3360   (let ((way (if backward -1 1))
3361         (low 10)
3362         (beg (point))
3363         pos found)
3364     (or first-too (forward-line way))
3365     (while (and 
3366             (not (eobp))
3367             (not (setq 
3368                   found 
3369                   (and (or all
3370                            (and
3371                             (let ((unread 
3372                                    (get-text-property (point) 'gnus-unread)))
3373                               (or (eq unread t) (and unread (> unread 0))))
3374                             (let ((lev (get-text-property
3375                                         (point) 'gnus-level)))
3376                               (and lev (<= (get-text-property 
3377                                            (point) 'gnus-level)
3378                                            gnus-level-subscribed)))))
3379                        (or (not level)
3380                            (let ((lev (get-text-property (point) 'gnus-level)))
3381                              (if (and lev (<= lev level))
3382                                  t
3383                                (if (< lev low)
3384                                    (progn
3385                                      (setq low lev)
3386                                      (setq pos (point))))
3387                                nil))))))
3388             (zerop (forward-line way))))
3389     (if found 
3390         (progn (gnus-group-position-cursor) t)
3391       (if pos (goto-char pos) (goto-char beg))
3392       nil)))
3393
3394 ;; Gnus group mode commands
3395
3396 (defun gnus-group-read-group (all &optional no-article group)
3397   "Read news in this newsgroup.
3398 If argument ALL is non-nil, already read articles become readable.
3399 If optional argument NO-ARTICLE is non-nil, no article body is displayed."
3400   (interactive "P")
3401   (let ((group (or group (gnus-group-group-name)))
3402         number active marked entry)
3403     (or group (error "No group on current line"))
3404     (setq marked 
3405           (nth 3 (nth 2 (setq entry (gnus-gethash group gnus-newsrc-hashtb)))))
3406     ;; This group might be a dead group. In that case we have to get
3407     ;; the number of unread articles from `gnus-active-hashtb'.
3408     (if entry
3409         (setq number (car entry))
3410       (if (setq active (gnus-gethash group gnus-active-hashtb))
3411           (setq number (- (1+ (cdr active)) (car active)))))
3412     (gnus-summary-read-group 
3413      group (or all (and (numberp number) 
3414                         (zerop (+ number (length (cdr (assq 'tick marked)))
3415                                   (length (cdr (assq 'dormant marked)))))))
3416      no-article)))
3417
3418 (defun gnus-group-select-group (all)
3419   "Select this newsgroup.
3420 No article is selected automatically.
3421 If argument ALL is non-nil, already read articles become readable."
3422   (interactive "P")
3423   (gnus-group-read-group all t))
3424
3425 (defun gnus-group-jump-to-group (group)
3426   "Jump to newsgroup GROUP."
3427   (interactive (list (completing-read "Group: " gnus-active-hashtb nil
3428                                       (not (not gnus-read-active-file)))))
3429
3430   (if (equal group "")
3431       (error "empty group name"))
3432
3433   (let ((b (text-property-any (point-min) (point-max) 
3434                               'gnus-group (intern group))))
3435     (if b
3436         ;; Either go to the line in the group buffer...
3437         (goto-char b)
3438       ;; ... or insert the line.
3439       (or
3440        (gnus-gethash group gnus-active-hashtb)
3441        (gnus-activate-newsgroup group)
3442        (error "%s error: %s" group (gnus-status-message group)))
3443
3444       (gnus-group-update-group group)
3445       (goto-char (text-property-any (point-min) (point-max) 
3446                                     'gnus-group (intern group)))))
3447   ;; Adjust cursor point.
3448   (gnus-group-position-cursor))
3449
3450 (defun gnus-group-next-group (n)
3451   "Go to next N'th newsgroup.
3452 If N is negative, search backward instead.
3453 Returns the difference between N and the number of skips actually
3454 done."
3455   (interactive "p")
3456   (gnus-group-next-unread-group n t))
3457
3458 (defun gnus-group-next-unread-group (n &optional all level)
3459   "Go to next N'th unread newsgroup.
3460 If N is negative, search backward instead.
3461 If ALL is non-nil, choose any newsgroup, unread or not.
3462 If LEVEL is non-nil, choose the next group with level LEVEL, or, if no
3463 such group can be found, the next group with a level higher than
3464 LEVEL.
3465 Returns the difference between N and the number of skips actually
3466 made."
3467   (interactive "p")
3468   (let ((backward (< n 0))
3469         (n (abs n)))
3470     (while (and (> n 0)
3471                 (gnus-group-search-forward backward all level))
3472       (setq n (1- n)))
3473     (if (/= 0 n) (message "No more%s newsgroups%s" (if all "" " unread")
3474                           (if level " on this level or higher" "")))
3475     n))
3476
3477 (defun gnus-group-prev-group (n)
3478   "Go to previous N'th newsgroup.
3479 Returns the difference between N and the number of skips actually
3480 done."
3481   (interactive "p")
3482   (gnus-group-next-unread-group (- n) t))
3483
3484 (defun gnus-group-prev-unread-group (n)
3485   "Go to previous N'th unread newsgroup.
3486 Returns the difference between N and the number of skips actually
3487 done."  
3488   (interactive "p")
3489   (gnus-group-next-unread-group (- n)))
3490
3491 (defun gnus-group-next-unread-group-same-level (n)
3492   "Go to next N'th unread newsgroup on the same level.
3493 If N is negative, search backward instead.
3494 Returns the difference between N and the number of skips actually
3495 done."
3496   (interactive "p")
3497   (gnus-group-next-unread-group n t (gnus-group-group-level))
3498   (gnus-group-position-cursor))
3499
3500 (defun gnus-group-prev-unread-group-same-level (n)
3501   "Go to next N'th unread newsgroup on the same level.
3502 Returns the difference between N and the number of skips actually
3503 done."
3504   (interactive "p")
3505   (gnus-group-next-unread-group (- n) t (gnus-group-group-level))
3506   (gnus-group-position-cursor))
3507
3508 (defun gnus-group-best-unread-group (&optional exclude-group)
3509   "Go to the group with the highest level.
3510 If EXCLUDE-GROUP, do not go to that group."
3511   (interactive)
3512   (goto-char (point-min))
3513   (let ((best 10)
3514         unread best-point)
3515     (while (setq unread (get-text-property (point) 'gnus-unread))
3516       (if (and (numberp unread) (> unread 0))
3517           (progn
3518             (or best-point (setq best-point (point)))
3519             (if (and (< (get-text-property (point) 'gnus-level) best)
3520                      (or (not exclude-group)
3521                          (not (equal exclude-group (gnus-group-group-name)))))
3522                 (progn 
3523                   (setq best (get-text-property (point) 'gnus-level))
3524                   (setq best-point (point))))))
3525       (forward-line 1))
3526     (if best-point (goto-char best-point))
3527     (gnus-summary-position-cursor)
3528     (and best-point (gnus-group-group-name))))
3529
3530 (defun gnus-group-first-unread-group ()
3531   "Go to the first group with unread articles."
3532   (interactive)
3533   (goto-char (point-min))
3534   (or (get-text-property (point) 'gnus-unread)
3535       (gnus-group-next-unread-group 1))
3536   (gnus-group-position-cursor))
3537
3538 (defun gnus-group-enter-server-mode ()
3539   "Jump to the server buffer."
3540   (interactive)
3541   (gnus-server-setup-buffer)
3542   (switch-to-buffer gnus-server-buffer)
3543   (gnus-server-prepare))
3544
3545 (defun gnus-group-make-group (name method address)
3546   "Add a new newsgroup.
3547 The user will be prompted for a NAME, for a select METHOD, and an
3548 ADDRESS."
3549   (interactive
3550    (cons 
3551     (read-string "Group name: ")
3552     (let ((method
3553            (completing-read 
3554             "Method: " (append gnus-valid-select-methods gnus-server-alist)
3555             nil t)))
3556       (if (assoc method gnus-valid-select-methods)
3557           (list method
3558                 (if (memq 'prompt-address
3559                           (assoc method gnus-valid-select-methods))
3560                     (read-string "Address: ")
3561                   ""))
3562         (list method nil)))))
3563   
3564   (let* ((meth (if address (list (intern method) address) method))
3565          (nname (gnus-group-prefixed-name name meth))
3566          info)
3567     (and (gnus-gethash nname gnus-newsrc-hashtb)
3568          (error "Group %s already exists" nname))
3569     (gnus-group-change-level 
3570      (setq info (list t nname gnus-level-default-subscribed nil nil meth))
3571      gnus-level-default-subscribed gnus-level-killed 
3572      (gnus-gethash (or (gnus-group-group-name) "dummy.group")
3573                        gnus-newsrc-hashtb) t)
3574     (gnus-sethash nname '(0 . 0) gnus-active-hashtb)
3575     (gnus-dribble-enter 
3576      (concat "(gnus-group-set-info '" (prin1-to-string (cdr info)) ")"))
3577     (gnus-group-insert-group-line-info nname)
3578
3579     (and (gnus-check-backend-function 'request-create-group nname)
3580          (gnus-request-create-group nname))))
3581
3582 (defun gnus-group-edit-group (group &optional part)
3583   "Edit the group on the current line."
3584   (interactive (list (gnus-group-group-name)))
3585   (let ((done-func '(lambda () 
3586                       "Exit editing mode and update the information."
3587                       (interactive)
3588                       (gnus-group-edit-group-done 'part 'group)))
3589         (part (or part 'info))
3590         info)
3591     (if group (setq info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
3592       (error "No group on current line"))
3593     (setq gnus-winconf-edit-group (current-window-configuration))
3594     (pop-to-buffer gnus-group-edit-buffer)
3595     (gnus-add-current-to-buffer-list)
3596     (emacs-lisp-mode)
3597     ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3598     (use-local-map (copy-keymap emacs-lisp-mode-map))
3599     (local-set-key "\C-c\C-c" done-func)
3600     ;; We modify the func to let it know what part it is editing.
3601     (setcar (cdr (nth 4 done-func)) (list 'quote part))
3602     (setcar (cdr (cdr (nth 4 done-func))) group)
3603     (erase-buffer)
3604     (insert
3605      (cond 
3606       ((eq part 'method)
3607        ";; Type `C-c C-c' after editing the select method.\n\n")
3608       ((eq part 'params)
3609        ";; Type `C-c C-c' after editing the group parameters.\n\n")
3610       ((eq part 'info)
3611        ";; Type `C-c C-c' after editing the group info.\n\n")))
3612     (let ((cinfo (gnus-copy-sequence info))
3613           marked)
3614       (if (not (setq marked (nth 3 cinfo)))
3615           ()
3616         (while marked
3617           (or (eq 'score (car (car marked)))
3618               (eq 'bookmark (car (car marked)))
3619               (eq 'killed (car (car marked)))
3620               (setcdr (car marked) 
3621                       (gnus-compress-sequence (sort (cdr (car marked)) '<) t)))
3622           (setq marked (cdr marked))))
3623       (insert 
3624        (pp-to-string
3625         (cond ((eq part 'method)
3626                (or (nth 4 info) "native"))
3627               ((eq part 'params)
3628                (nth 5 info))
3629               (t
3630                cinfo)))
3631        "\n"))))
3632
3633 (defun gnus-group-edit-group-method (group)
3634   "Edit the select method of GROUP."
3635   (interactive (list (gnus-group-group-name)))
3636   (gnus-group-edit-group group 'method))
3637
3638 (defun gnus-group-edit-group-parameters (group)
3639   "Edit the group parameters of GROUP."
3640   (interactive (list (gnus-group-group-name)))
3641   (gnus-group-edit-group group 'params))
3642
3643 (defun gnus-group-edit-group-done (part group)
3644   "Get info from buffer, update variables and jump to the group buffer."
3645   (set-buffer (get-buffer-create gnus-group-edit-buffer))
3646   (goto-char (point-min))
3647   (let ((form (read (current-buffer))))
3648     (if (eq part 'info) 
3649         (gnus-group-set-info form)
3650       (gnus-group-set-info form group part))
3651     (kill-buffer (current-buffer))
3652     (and gnus-winconf-edit-group
3653          (set-window-configuration gnus-winconf-edit-group))
3654     (setq gnus-winconf-edit-group nil)
3655     (set-buffer gnus-group-buffer)
3656     (gnus-group-update-group (gnus-group-group-name))
3657     (gnus-group-position-cursor)))
3658
3659 (defun gnus-group-make-help-group ()
3660   "Create the (ding) Gnus documentation group."
3661   (interactive)
3662   (and (gnus-gethash (gnus-group-prefixed-name "gnus-help" '(nndoc ""))
3663                      gnus-newsrc-hashtb)
3664        (error "Documentation group already exists"))
3665   (let ((path load-path))
3666     (while (and path
3667                 (not (file-exists-p (concat (file-name-as-directory (car path))
3668                                             "doc.txt"))))
3669       (setq path (cdr path)))
3670     (or path (error "Couldn't find doc group"))
3671     (gnus-group-make-group 
3672      "gnus-help" "nndoc" 
3673      (concat (file-name-as-directory (car path)) "doc.txt"))
3674     (gnus-group-position-cursor)))
3675
3676 (defun gnus-group-make-archive-group ()
3677   "Create the (ding) Gnus archive group."
3678   (interactive)
3679   (and (gnus-gethash (gnus-group-prefixed-name "ding.archives" '(nndir ""))
3680                      gnus-newsrc-hashtb)
3681        (error "Archive group already exists"))
3682   (gnus-group-make-group "ding.archives" "nndir" gnus-group-archive-directory)
3683   (gnus-group-position-cursor))
3684
3685 (defun gnus-group-make-directory-group (dir)
3686   "Create an nndir group.
3687 The user will be prompted for a directory. The contents of this
3688 directory will be used as a newsgroup. The directory should contain
3689 mail messages or news articles in files that have numeric names."
3690   (interactive
3691    (list (read-file-name "Create group from directory: ")))
3692   (or (file-exists-p dir) (error "No such directory"))
3693   (or (file-directory-p dir) (error "Not a directory"))
3694   (gnus-group-make-group dir "nndir" dir)
3695   (gnus-group-position-cursor))
3696
3697 (defun gnus-group-make-kiboze-group (group address scores)
3698   "Create an nnkiboze group.
3699 The user will be prompted for a name, a regexp to match groups, and
3700 score file entries for articles to include in the group."
3701   (interactive
3702    (list
3703     (read-string "nnkiboze group name: ")
3704     (read-string "Source groups (regexp): ")
3705     (let ((headers (mapcar (lambda (group) (list group))
3706                            '("subject" "from" "number" "date" "message-id"
3707                              "references" "chars" "lines" "xref")))
3708           scores header regexp regexps)
3709       (while (not (equal "" (setq header (completing-read 
3710                                           "Match on header: " headers nil t))))
3711         (setq regexps nil)
3712         (while (not (equal "" (setq regexp (read-string 
3713                                             (format "Match on %s (string): "
3714                                                     header)))))
3715           (setq regexps (cons (list regexp nil 1000 nil) regexps)))
3716         (setq scores (cons (cons header regexps) scores)))
3717       (car scores))))
3718   (gnus-group-make-group group "nnkiboze" address)
3719   (save-excursion
3720     (gnus-set-work-buffer)
3721     (let (emacs-lisp-mode-hook)
3722       (pp (list 'setq 'gnus-score-alist 
3723                 (list 'quote (list scores)))
3724           (current-buffer)))
3725     (write-region (point-min) (point-max) 
3726                   (concat (or gnus-kill-files-directory "~/News")
3727                           "nnkiboze:" group "." gnus-score-file-suffix)))
3728   (gnus-group-position-cursor))
3729
3730 ;; Group sorting commands
3731 ;; Suggested by Joe Hildebrand <hildjj@idaho.fuentez.com>.
3732
3733 (defun gnus-group-sort-groups ()
3734   "Sort the group buffer using `gnus-group-sort-function'."
3735   (interactive)
3736   (setq gnus-newsrc-alist 
3737         (sort (cdr gnus-newsrc-alist) gnus-group-sort-function))
3738   (gnus-make-hashtable-from-newsrc-alist)
3739   (gnus-get-unread-articles (1+ gnus-level-subscribed))
3740   (gnus-group-list-groups nil))
3741
3742 (defun gnus-group-sort-by-alphabet (info1 info2)
3743   (string< (car info1) (car info2)))
3744
3745 (defun gnus-group-sort-by-unread (info1 info2)
3746   (let ((n1 (car (gnus-gethash (car info1) gnus-newsrc-hashtb)))
3747         (n2 (car (gnus-gethash (car info2) gnus-newsrc-hashtb))))
3748     (< (or (and (numberp n1) n1) 0)
3749        (or (and (numberp n2) n2) 0))))
3750
3751 (defun gnus-group-sort-by-level (info1 info2)
3752   (< (nth 1 info1) (nth 1 info2)))
3753
3754 ;; Group catching up.
3755
3756 (defun gnus-group-catchup-current (n &optional all)
3757   "Mark all articles not marked as unread in current newsgroup as read.
3758 If prefix argument N is numeric, the ARG next newsgroups will be
3759 caught up. If ALL is non-nil, marked articles will also be marked as
3760 read. Cross references (Xref: header) of articles are ignored.
3761 The difference between N and actual number of newsgroups that were
3762 caught up is returned."
3763   (interactive "p")
3764   (if (or (not gnus-interactive-catchup) ;Without confirmation?
3765           gnus-expert-user
3766           (gnus-y-or-n-p
3767            (if all
3768                "Do you really want to mark all articles as read? "
3769              "Mark all unread articles as read? ")))
3770       (progn
3771         (while 
3772             (and (> n 0)
3773                  (progn
3774                    (setq n (1- n))
3775                    (gnus-group-catchup (gnus-group-group-name) all)
3776                    (gnus-group-update-group-line)
3777                    t)
3778                  (zerop (gnus-group-next-unread-group 1))))))
3779   n)
3780
3781 (defun gnus-group-catchup-current-all (n)
3782   "Mark all articles in current newsgroup as read.
3783 Cross references (Xref: header) of articles are ignored."
3784   (interactive "p")
3785   (gnus-group-catchup-current n 'all))
3786
3787 (defun gnus-group-catchup (group &optional all)
3788   "Mark all articles in GROUP as read.
3789 If ALL is non-nil, all articles are marked as read.
3790 The return value is the number of articles that were marked as read,
3791 or nil if no action could be taken."
3792   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
3793          (num (car entry))
3794          (marked (nth 3 (nth 2 entry)))
3795          ticked)
3796     (if (not (numberp (car entry)))
3797         (message "Can't catch up; non-active group")
3798       ;; Do the updating only if the newsgroup isn't killed
3799       (if entry
3800           (progn
3801             (setq ticked (if all nil (cdr (assq 'tick marked))))
3802             (gnus-update-read-articles group ticked nil ticked)
3803             (if (and all marked)
3804                 (setcar (nthcdr 3 (nth 2 entry)) 
3805                         (delq (assq 'dormant marked) marked))))))
3806     num))
3807
3808 (defun gnus-group-expire-articles (newsgroup)
3809   "Expire all expirable articles in the current newsgroup."
3810   (interactive (list (gnus-group-group-name)))
3811   (if (not newsgroup) (error "No current newsgroup"))
3812   (let ((expirable 
3813          (assq 'expire (nth 3 (nth 2 (gnus-gethash newsgroup 
3814                                                    gnus-newsrc-hashtb))))))
3815  (and expirable 
3816       (gnus-check-backend-function 'request-expire-articles newsgroup)
3817       (setcdr expirable
3818               (gnus-request-expire-articles (cdr expirable) newsgroup)))))
3819
3820 (defun gnus-group-expire-all-groups ()
3821   "Expire all expirable articles in all newsgroups."
3822   (interactive)
3823   (message "Expiring...")
3824   (let ((newsrc (cdr gnus-newsrc-alist)))
3825     (while newsrc
3826       (gnus-group-expire-articles (car (car newsrc)))
3827       (setq newsrc (cdr newsrc))))
3828   (message "Expiring...done"))
3829
3830 (defun gnus-group-set-current-level (n level)
3831   "Set the level of the next N groups to LEVEL."
3832   (interactive "p\nnLevel: ")
3833   (or (and (>= level 1) (<= level gnus-level-killed))
3834       (error "Illegal level: %d" level))
3835   (let (group)
3836     (while (and (> n 0)
3837                 (setq group (gnus-group-group-name)))
3838       (and (setq group (gnus-group-group-name))
3839            (message "Changed level of %s from %d to %d" 
3840                     group (gnus-group-group-level) level)
3841            (gnus-group-change-level group level
3842                                     (gnus-group-group-level))
3843            (gnus-group-update-group-line))
3844       (forward-line 1)
3845       (setq n (1- n))))
3846   (gnus-group-position-cursor)
3847   n)
3848
3849 (defun gnus-group-unsubscribe-current-group (arg)
3850   "Toggle subscribe from/to unsubscribe current group."
3851   (interactive "P")
3852   (let ((group (gnus-group-group-name)))
3853     (or group (error "No newsgroup on current line"))
3854     (or arg (setq arg (if (<= (gnus-group-group-level) gnus-level-subscribed)
3855                           gnus-level-default-unsubscribed
3856                         gnus-level-default-subscribed)))
3857     (gnus-group-unsubscribe-group group arg)
3858     (gnus-group-next-group 1)))
3859
3860 (defun gnus-group-unsubscribe-group (group &optional level)
3861   "Toggle subscribe from/to unsubscribe GROUP.
3862 New newsgroup is added to .newsrc automatically."
3863   (interactive
3864    (list (completing-read "Group: " gnus-active-hashtb nil 
3865                           gnus-have-read-active-file)))
3866   (let ((newsrc (gnus-gethash group gnus-newsrc-hashtb)))
3867     (cond (newsrc
3868            ;; Toggle subscription flag.
3869            (gnus-group-change-level 
3870             newsrc (if level level (if (<= (nth 1 (nth 2 newsrc)) 
3871                                            gnus-level-subscribed) 
3872                                        (1+ gnus-level-subscribed)
3873                                      gnus-level-default-subscribed)))
3874            (gnus-group-update-group group))
3875           ((and (stringp group)
3876                 (or (not gnus-have-read-active-file)
3877                     (gnus-gethash group gnus-active-hashtb)))
3878            ;; Add new newsgroup.
3879            (gnus-group-change-level 
3880             group 
3881             (if level level gnus-level-default-subscribed) 
3882             (or (and (member group gnus-zombie-list) 
3883                      gnus-level-zombie) 
3884                 gnus-level-killed)
3885             (or (and (gnus-group-group-name)
3886                      (gnus-gethash (gnus-group-group-name) gnus-newsrc-hashtb))
3887                 (gnus-gethash (car (car gnus-newsrc-alist)) 
3888                               gnus-newsrc-hashtb)))
3889            (gnus-group-update-group group))
3890           (t (error "No such newsgroup: %s" group)))
3891     (gnus-group-position-cursor)))
3892
3893 (defun gnus-group-transpose-groups (arg)
3894   "Exchange current newsgroup and previous newsgroup.
3895 With argument ARG, takes previous newsgroup and moves it past ARG newsgroup."
3896   (interactive "p")
3897   ;; BUG: last newsgroup and the last but one cannot be transposed
3898   ;; since gnus-group-search-forward does not move forward beyond the
3899   ;; last.  If we instead use forward-line, no problem, but I don't
3900   ;; want to use it for later extension.
3901   (while (> arg 0)
3902     (gnus-group-search-forward t t)
3903     (gnus-group-kill-group 1)
3904     (gnus-group-search-forward nil t)
3905     (gnus-group-yank-group)
3906     (gnus-group-search-forward nil t)
3907     (setq arg (1- arg))))
3908
3909 (defun gnus-group-kill-all-zombies ()
3910   "Kill all zombie newsgroups."
3911   (interactive)
3912   (setq gnus-killed-list (nconc gnus-zombie-list gnus-killed-list))
3913   (setq gnus-zombie-list nil)
3914   (funcall gnus-group-prepare-function gnus-level-subscribed nil nil)
3915   (goto-char (point-min))
3916   (gnus-group-position-cursor))
3917
3918 (defun gnus-group-kill-region (begin end)
3919   "Kill newsgroups in current region (excluding current point).
3920 The killed newsgroups can be yanked by using \\[gnus-group-yank-group]."
3921   (interactive "r")
3922   (let ((lines
3923          ;; Exclude a line where current point is on.
3924          (1-
3925           ;; Count lines.
3926           (save-excursion
3927             (count-lines
3928              (progn
3929                (goto-char begin)
3930                (beginning-of-line)
3931                (point))
3932              (progn
3933                (goto-char end)
3934                (end-of-line)
3935                (point)))))))
3936     (goto-char begin)
3937     (beginning-of-line)                 ;Important when LINES < 1
3938     (gnus-group-kill-group lines)))
3939
3940 (defun gnus-group-kill-group (n)
3941   "The the next N groups.
3942 The killed newsgroups can be yanked by using \\[gnus-group-yank-group].
3943 However, only groups that were alive can be yanked; already killed 
3944 groups or zombie groups can't be yanked.
3945 The return value is the name of the (last) newsgroup that was killed."
3946   (interactive "p")
3947   (let ((buffer-read-only nil)
3948         group entry level)
3949     (while (>= (setq n  (1- n)) 0)
3950       (setq group (gnus-group-group-name))
3951       (or group
3952           (signal 'end-of-buffer nil))
3953       (setq level (gnus-group-group-level))
3954       (beginning-of-line)
3955       (delete-region (point) (progn (forward-line 1) (point)))
3956       (if (setq entry (gnus-gethash group gnus-newsrc-hashtb))
3957           (setq gnus-list-of-killed-groups 
3958                 (cons (cons (car entry) (nth 2 entry)) 
3959                       gnus-list-of-killed-groups)))
3960       (gnus-group-change-level 
3961        (if entry entry group) gnus-level-killed (if entry nil level)))
3962     (if (eobp)
3963         (forward-line -1))
3964     (gnus-group-position-cursor)
3965     group))
3966
3967 (defun gnus-group-yank-group (&optional arg)
3968   "Yank the last newsgroups killed with \\[gnus-group-kill-group],
3969 inserting it before the current newsgroup.  The numeric ARG specifies
3970 how many newsgroups are to be yanked.  The name of the (last)
3971 newsgroup yanked is returned."
3972   (interactive "p")
3973   (if (not arg) (setq arg 1))
3974   (let (info group prev)
3975     (while (>= (setq arg (1- arg)) 0)
3976       (if (not (setq info (car gnus-list-of-killed-groups)))
3977           (error "No more newsgroups to yank"))
3978       (setq group (nth 2 info))
3979       ;; Find which newsgroup to insert this one before - search
3980       ;; backward until something suitable is found. If there are no
3981       ;; other newsgroups in this buffer, just make this newsgroup the
3982       ;; first newsgroup.
3983       (while (and (not (setq prev (gnus-group-group-name)))
3984                   (zerop (forward-line -1))))
3985       (if (not prev)
3986           (setq prev (car (car gnus-newsrc-alist))))
3987       (gnus-group-change-level 
3988        info (nth 2 info) gnus-level-killed 
3989        (gnus-gethash prev gnus-newsrc-hashtb)
3990        t)
3991       (gnus-group-insert-group-line-info (nth 1 info))
3992       (setq gnus-list-of-killed-groups 
3993             (cdr gnus-list-of-killed-groups)))
3994     (forward-line -1)
3995     (gnus-group-position-cursor)
3996     group))
3997       
3998 (defun gnus-group-list-all-groups (arg)
3999   "List all newsgroups with level ARG or lower.
4000 Default is 7, which lists all subscribed and most unsubscribed groups."
4001   (interactive "P")
4002   (setq arg (or arg 7))
4003   (gnus-group-list-groups arg t))
4004
4005 (defun gnus-group-list-killed ()
4006   "List all killed newsgroups in the group buffer."
4007   (interactive)
4008   (if (not gnus-killed-list)
4009       (message "No killed groups")
4010     (funcall gnus-group-prepare-function gnus-level-killed t gnus-level-killed)
4011     (goto-char (point-min)))
4012   (gnus-group-position-cursor))
4013
4014 (defun gnus-group-list-zombies ()
4015   "List all zombie newsgroups in the group buffer."
4016   (interactive)
4017   (if (not gnus-zombie-list)
4018       (message "No zombie groups")
4019     (funcall gnus-group-prepare-function gnus-level-zombie t gnus-level-zombie)
4020     (goto-char (point-min)))
4021   (gnus-group-position-cursor))
4022
4023 (defun gnus-group-get-new-news (&optional arg)
4024   "Get newly arrived articles.
4025 If ARG is non-nil, it should be a number between one and nine to
4026 specify which levels you are interested in re-scanning."
4027   (interactive "P")
4028   (run-hooks 'gnus-get-new-news-hook)
4029   (let ((level arg))
4030     (if (and gnus-read-active-file (not level))
4031         (progn
4032           (gnus-read-active-file)
4033           (gnus-get-unread-articles (or level (1+ gnus-level-subscribed))))
4034       (let ((gnus-read-active-file nil))
4035         (gnus-get-unread-articles (or level (1+ gnus-level-subscribed)))))
4036     (gnus-group-list-groups 
4037      (or gnus-group-always-list-unread level gnus-level-subscribed)
4038      gnus-have-all-newsgroups)))
4039
4040 (defun gnus-group-get-new-news-this-group (n)
4041   "Check for newly arrived news in the current group (and the N-1 next groups).
4042 The difference between N and the number of newsgroup checked is returned.
4043 If N is negative, this group and the N-1 previous groups will be checked."
4044   (interactive "p")
4045   (let ((way (if (< n 0) -1 1))
4046         (n (abs n))
4047         (w-p (window-start))
4048         group)
4049     (while (and (> n 0)
4050                 (progn
4051                   (or (gnus-get-new-news-in-group
4052                        (setq group (gnus-group-group-name)))
4053                       (progn 
4054                         (ding) 
4055                         (message "%s error: %s" 
4056                                  group (gnus-status-message group))))
4057                   t)
4058                 (zerop (gnus-group-next-group way)))
4059       (setq n (1- n)))
4060     (if (/= 0 n) (message "No more newsgroups"))
4061     ;; !!! I don't know why the buffer scrolls forward when updating
4062     ;; the first line in the group buffer, but it does. So we set the
4063     ;; window start forcibly.
4064     (set-window-start (get-buffer-window (current-buffer)) w-p)
4065     n))
4066
4067 (defun gnus-get-new-news-in-group (group)
4068   (and group 
4069        (gnus-activate-newsgroup group)
4070        (progn
4071          (gnus-get-unread-articles-in-group 
4072           (nth 2 (gnus-gethash group gnus-newsrc-hashtb))
4073           (gnus-gethash group gnus-active-hashtb))
4074          (gnus-group-update-group-line)
4075          t)))
4076
4077 (defun gnus-group-fetch-faq (group)
4078   "Fetch the FAQ for the current group."
4079   (interactive (list (gnus-group-group-name)))
4080   (or group (error "No group name given"))
4081   (let ((file (concat gnus-group-faq-directory group))) 
4082     (if (not (file-exists-p file))
4083         (error "No such file: %s" file)
4084       (find-file file))))
4085   
4086 (defun gnus-group-describe-group (force &optional group)
4087   "Display a description of the current newsgroup."
4088   (interactive "P")
4089   (and force (setq gnus-description-hashtb nil))
4090   (let ((group (or group (gnus-group-group-name)))
4091         desc)
4092     (or group (message "No group name given"))
4093     (and (or gnus-description-hashtb
4094              (setq desc (gnus-group-get-description group))
4095              (gnus-read-descriptions-file))
4096          (message
4097           (or desc (gnus-gethash group gnus-description-hashtb)
4098               "No description available")))))
4099
4100 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
4101 (defun gnus-group-describe-all-groups (force)
4102   "Pop up a buffer with descriptons of all newsgroups."
4103   (interactive "P")
4104   (and force (setq gnus-description-hashtb nil))
4105   (if (not (or gnus-description-hashtb
4106                (gnus-read-descriptions-file)))
4107       (error "Couldn't request descriptions file"))
4108   (let ((buffer-read-only nil)
4109         b)
4110     (erase-buffer)
4111     (mapatoms
4112      (lambda (group)
4113        (setq b (point))
4114        (insert (format "      *: %-20s %s\n" (symbol-name group)
4115                        (symbol-value group)))
4116        (add-text-properties 
4117         b (1+ b) (list 'gnus-group group
4118                        'gnus-unread t 'gnus-marked nil
4119                        'gnus-level (1+ gnus-level-subscribed))))
4120      gnus-description-hashtb)
4121     (goto-char (point-min))
4122     (gnus-group-position-cursor)))
4123
4124 ;; Suggested by by Daniel Quinlan <quinlan@best.com>.
4125 (defun gnus-group-apropos (regexp &optional search-description)
4126   "List all newsgroups that have names that match a regexp."
4127   (interactive "sGnus apropos (regexp): ")
4128   (let ((prev "")
4129         (obuf (current-buffer))
4130         groups des prev)
4131     ;; Go through all newsgroups that are known to Gnus.
4132     (mapatoms 
4133      (lambda (group)
4134        (and (string-match regexp (symbol-name group))
4135             (setq groups (cons (symbol-name group) groups))))
4136      gnus-active-hashtb)
4137     ;; Go through all descriptions that are known to Gnus. 
4138     (if search-description
4139         (mapatoms 
4140          (lambda (group)
4141            (and (string-match regexp (symbol-value group))
4142                 (gnus-gethash (symbol-name group) gnus-active-hashtb)
4143                 (setq groups (cons (symbol-name group) groups))))
4144          gnus-description-hashtb))
4145     (if (not groups)
4146         (message "No groups matched \"%s\"." regexp)
4147       ;; Print out all the groups.
4148       (save-excursion
4149         (pop-to-buffer "*Gnus Help*")
4150         (buffer-disable-undo (current-buffer))
4151         (erase-buffer)
4152         (setq groups (sort groups 'string<))
4153         (while groups
4154           ;; Groups may be entered twice into the list of groups.
4155           (if (not (string= (car groups) prev))
4156               (progn
4157                 (insert (setq prev (car groups)) "\n")
4158                 (if (and gnus-description-hashtb
4159                          (setq des (gnus-gethash (car groups) 
4160                                                  gnus-description-hashtb)))
4161                     (insert "  " des "\n"))))
4162           (setq groups (cdr groups)))
4163         (goto-char 1)))
4164     (pop-to-buffer obuf)))
4165
4166 (defun gnus-group-description-apropos (regexp)
4167   "List all newsgroups that have names or desccriptions that match a regexp."
4168   (interactive "sGnus description apropos (regexp): ")
4169   (if (not (or gnus-description-hashtb
4170                (gnus-read-descriptions-file)))
4171       (error "Couldn't request descriptions file"))
4172   (gnus-group-apropos regexp t))
4173
4174 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
4175 (defun gnus-group-list-matching (level regexp &optional all lowest) 
4176   "List all groups with unread articles that match REGEXP.
4177 If the prefix LEVEL is non-nil, it should be a number that says which
4178 level to cut off listing groups. 
4179 If ALL, also list groups with no unread articles.
4180 If LOWEST, don't list groups with level lower than LOWEST."
4181   (interactive "P\nsList newsgroups matching: ")
4182   (gnus-group-prepare-flat (or level gnus-level-subscribed)
4183                            all (or lowest 1) regexp)
4184   (goto-char (point-min))
4185   (gnus-group-position-cursor))
4186
4187 (defun gnus-group-list-all-matching (level regexp &optional lowest) 
4188   "List all groups that match REGEXP.
4189 If the prefix LEVEL is non-nil, it should be a number that says which
4190 level to cut off listing groups. 
4191 If LOWEST, don't list groups with level lower than LOWEST."
4192   (interactive "P\nsList newsgroups matching: ")
4193   (gnus-group-list-matching (or level gnus-level-killed) regexp t lowest))
4194
4195 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
4196 (defun gnus-group-save-newsrc ()
4197   "Save the Gnus startup files."
4198   (interactive)
4199   (gnus-save-newsrc-file))
4200
4201 (defun gnus-group-restart (&optional arg)
4202   "Force Gnus to read the .newsrc file."
4203   (interactive "P")
4204   (gnus-save-newsrc-file)
4205   (gnus-setup-news 'force)
4206   (gnus-group-list-groups arg gnus-have-all-newsgroups))
4207
4208 (defun gnus-group-read-init-file ()
4209   "Read the Gnus elisp init file."
4210   (interactive)
4211   (gnus-read-init-file))
4212
4213 (defun gnus-group-check-bogus-groups ()
4214   "Check bogus newsgroups."
4215   (interactive)
4216   (gnus-check-bogus-newsgroups (not gnus-expert-user))  ;Require confirmation.
4217   (gnus-group-list-groups nil gnus-have-all-newsgroups))
4218
4219 (defun gnus-group-mail ()
4220   "Start composing a mail."
4221   (interactive)
4222   (mail))
4223
4224 (defun gnus-group-edit-global-kill (article &optional group)
4225   "Edit the global kill file.
4226 If GROUP, edit that local kill file instead."
4227   (interactive "P")
4228   (setq gnus-current-kill-article article)
4229   (gnus-kill-file-edit-file group)
4230   (message
4231    (substitute-command-keys
4232     "Editing a global kill file (Type \\[gnus-kill-file-exit] to exit)")))
4233
4234 (defun gnus-group-edit-local-kill (article group)
4235   "Edit a local kill file."
4236   (interactive (list nil (gnus-group-group-name)))
4237   (gnus-group-edit-global-kill article group))
4238
4239 (defun gnus-group-force-update ()
4240   "Update `.newsrc' file."
4241   (interactive)
4242   (gnus-save-newsrc-file))
4243
4244 (defun gnus-group-suspend ()
4245   "Suspend the current Gnus session.
4246 In fact, cleanup buffers except for group mode buffer.
4247 The hook gnus-suspend-gnus-hook is called before actually suspending."
4248   (interactive)
4249   (run-hooks 'gnus-suspend-gnus-hook)
4250   ;; Kill Gnus buffers except for group mode buffer.
4251   (let ((group-buf (get-buffer gnus-group-buffer)))
4252     ;; Do this on a separate list in case the user does a ^G before we finish
4253     (let ((gnus-buffer-list
4254            (delq group-buf (delq gnus-dribble-buffer
4255                                  (append gnus-buffer-list nil)))))
4256       (while gnus-buffer-list
4257         (gnus-kill-buffer (car gnus-buffer-list))
4258         (setq gnus-buffer-list (cdr gnus-buffer-list))))
4259     (if group-buf
4260         (progn
4261           (setq gnus-buffer-list (list group-buf))
4262           (bury-buffer group-buf)
4263           (delete-windows-on group-buf t)))))
4264
4265 (defun gnus-group-clear-dribble ()
4266   "Clear all information from the dribble buffer."
4267   (interactive)
4268   (gnus-dribble-clear))
4269
4270 (defun gnus-group-exit ()
4271   "Quit reading news after updating .newsrc.eld and .newsrc.
4272 The hook `gnus-exit-gnus-hook' is called before actually exiting."
4273   (interactive)
4274   (if (or noninteractive                ;For gnus-batch-kill
4275           (zerop (buffer-size))         ;No news is good news.
4276           (not (gnus-server-opened gnus-select-method)) ;NNTP connection closed
4277           (not gnus-interactive-exit)   ;Without confirmation
4278           gnus-expert-user
4279           (gnus-y-or-n-p "Are you sure you want to quit reading news? "))
4280       (progn
4281         (run-hooks 'gnus-exit-gnus-hook)
4282         (gnus-save-newsrc-file)
4283         (gnus-close-backends)
4284         (gnus-clear-system))))
4285
4286 (defun gnus-close-backends ()
4287   ;; Send a close request to all backends that support such a request. 
4288   (let ((methods gnus-valid-select-methods)
4289         func)
4290     (while methods
4291       (if (fboundp (setq func (intern (concat (car (car methods))
4292                                               "-request-close"))))
4293           (funcall func))
4294       (setq methods (cdr methods)))))
4295
4296 (defun gnus-group-quit ()
4297   "Quit reading news without updating .newsrc.eld or .newsrc.
4298 The hook `gnus-exit-gnus-hook' is called before actually exiting."
4299   (interactive)
4300   (if (or noninteractive                ;For gnus-batch-kill
4301           (zerop (buffer-size))
4302           (not (gnus-server-opened gnus-select-method))
4303           gnus-expert-user
4304           (not gnus-current-startup-file)
4305           (gnus-yes-or-no-p
4306            (format "Quit reading news without saving %s? "
4307                    (file-name-nondirectory gnus-current-startup-file))))
4308       (progn
4309         (run-hooks 'gnus-exit-gnus-hook)
4310         (gnus-dribble-save)
4311         (gnus-close-backends)
4312         (gnus-clear-system))))
4313
4314 (defun gnus-group-describe-briefly ()
4315   "Give a one line description of the group mode commands."
4316   (interactive)
4317   (message
4318    (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")))
4319
4320 (defun gnus-group-browse-foreign-server (method)
4321   "Browse a foreign news server.
4322 If called interactively, this function will ask for a select method
4323  (nntp, nnspool, etc.) and a server address (eg. nntp.some.where). 
4324 If not, METHOD should be a list where the first element is the method
4325 and the second element is the address."
4326   (interactive
4327    (list (list (intern (completing-read 
4328                         "Which backend: "
4329                         gnus-valid-select-methods nil t "nntp"))
4330                ;; Suggested by mapjph@bath.ac.uk.
4331                (completing-read 
4332                 "Address: " 
4333                 (mapcar (lambda (server) (list server))
4334                         gnus-secondary-servers)))))
4335   (gnus-browse-foreign-server method))
4336
4337 \f
4338 ;;;
4339 ;;; Browse Server Mode
4340 ;;;
4341
4342 (defvar gnus-browse-server-mode-hook nil)
4343 (defvar gnus-browse-server-mode-map nil)
4344 (put 'gnus-browse-server-mode 'mode-class 'special)
4345
4346 (if gnus-browse-server-mode-map
4347     nil
4348   (setq gnus-browse-server-mode-map (make-keymap))
4349   (suppress-keymap gnus-browse-server-mode-map)
4350   (define-key gnus-browse-server-mode-map " " 'gnus-browse-read-group)
4351   (define-key gnus-browse-server-mode-map "=" 'gnus-browse-read-group)
4352   (define-key gnus-browse-server-mode-map "n" 'gnus-browse-next-group)
4353   (define-key gnus-browse-server-mode-map "p" 'gnus-browse-prev-group)
4354   (define-key gnus-browse-server-mode-map "\177" 'gnus-browse-prev-group)
4355   (define-key gnus-browse-server-mode-map "N" 'gnus-browse-next-group)
4356   (define-key gnus-browse-server-mode-map "P" 'gnus-browse-prev-group)
4357   (define-key gnus-browse-server-mode-map "\M-n" 'gnus-browse-next-group)
4358   (define-key gnus-browse-server-mode-map "\M-p" 'gnus-browse-prev-group)
4359   (define-key gnus-browse-server-mode-map "\r" 'gnus-browse-read-group)
4360   (define-key gnus-browse-server-mode-map "u" 'gnus-browse-unsubscribe-current-group)
4361   (define-key gnus-browse-server-mode-map "l" 'gnus-browse-exit)
4362   (define-key gnus-browse-server-mode-map "L" 'gnus-browse-exit)
4363   (define-key gnus-browse-server-mode-map "q" 'gnus-browse-exit)
4364   (define-key gnus-browse-server-mode-map "Q" 'gnus-browse-exit)
4365   (define-key gnus-browse-server-mode-map "\C-c\C-c" 'gnus-browse-exit)
4366   (define-key gnus-browse-server-mode-map "?" 'gnus-browse-describe-briefly)
4367   (define-key gnus-browse-server-mode-map "\C-c\C-i" 'gnus-info-find-node)
4368   )
4369
4370 (defvar gnus-browse-current-method nil)
4371 (defvar gnus-browse-return-buffer nil)
4372
4373 (defun gnus-browse-foreign-server (method &optional return-buffer)
4374   (setq gnus-browse-current-method method)
4375   (setq gnus-browse-return-buffer return-buffer)
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 (or gnus-browse-return-buffer gnus-group-buffer)))
4502
4503 (defun gnus-browse-describe-briefly ()
4504   "Give a one line description of the group mode commands."
4505   (interactive)
4506   (message
4507    (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")))
4508       
4509 \f
4510 ;;;
4511 ;;; Gnus summary mode
4512 ;;;
4513
4514 (defvar gnus-summary-mode-map nil)
4515 (defvar gnus-summary-mark-map nil)
4516 (defvar gnus-summary-mscore-map nil)
4517 (defvar gnus-summary-extract-map nil)
4518 (defvar gnus-summary-extract-view-map nil)
4519 (defvar gnus-summary-article-map nil)
4520 (defvar gnus-summary-thread-map nil)
4521 (defvar gnus-summary-goto-map nil)
4522 (defvar gnus-summary-exit-map nil)
4523 (defvar gnus-summary-various-map nil)
4524 (defvar gnus-summary-interest-map nil)
4525 (defvar gnus-summary-process-map nil)
4526 (defvar gnus-summary-sort-map nil)
4527 (defvar gnus-summary-backend-map nil)
4528 (defvar gnus-summary-save-map nil)
4529 (defvar gnus-summary-wash-map nil)
4530 (defvar gnus-summary-help-map nil)
4531
4532 (put 'gnus-summary-mode 'mode-class 'special)
4533
4534 (if gnus-summary-mode-map
4535     nil
4536   (setq gnus-summary-mode-map (make-keymap))
4537   (suppress-keymap gnus-summary-mode-map)
4538
4539   ;; Non-orthogonal keys
4540
4541   (define-key gnus-summary-mode-map " " 'gnus-summary-next-page)
4542   (define-key gnus-summary-mode-map "\177" 'gnus-summary-prev-page)
4543   (define-key gnus-summary-mode-map "\r" 'gnus-summary-scroll-up)
4544   (define-key gnus-summary-mode-map "n" 'gnus-summary-next-unread-article)
4545   (define-key gnus-summary-mode-map "p" 'gnus-summary-prev-unread-article)
4546   (define-key gnus-summary-mode-map "N" 'gnus-summary-next-article)
4547   (define-key gnus-summary-mode-map "P" 'gnus-summary-prev-article)
4548   (define-key gnus-summary-mode-map "\M-\C-n" 'gnus-summary-next-same-subject)
4549   (define-key gnus-summary-mode-map "\M-\C-p" 'gnus-summary-prev-same-subject)
4550   (define-key gnus-summary-mode-map "\M-n" 'gnus-summary-next-unread-subject)
4551   (define-key gnus-summary-mode-map "\M-p" 'gnus-summary-prev-unread-subject)
4552   (define-key gnus-summary-mode-map "." 'gnus-summary-first-unread-article)
4553   (define-key gnus-summary-mode-map "," 'gnus-summary-best-unread-article)
4554   (define-key gnus-summary-mode-map "\M-s" 'gnus-summary-search-article-forward)
4555   (define-key gnus-summary-mode-map "\M-r" 'gnus-summary-search-article-backward)
4556   (define-key gnus-summary-mode-map "<" 'gnus-summary-beginning-of-article)
4557   (define-key gnus-summary-mode-map ">" 'gnus-summary-end-of-article)
4558   (define-key gnus-summary-mode-map "j" 'gnus-summary-goto-subject)
4559   (define-key gnus-summary-mode-map "^" 'gnus-summary-refer-parent-article)
4560   (define-key gnus-summary-mode-map "\M-^" 'gnus-summary-refer-article)
4561   (define-key gnus-summary-mode-map "u" 'gnus-summary-tick-article-forward)
4562   (define-key gnus-summary-mode-map "!" 'gnus-summary-tick-article-forward)
4563   (define-key gnus-summary-mode-map "U" 'gnus-summary-tick-article-backward)
4564   (define-key gnus-summary-mode-map "d" 'gnus-summary-mark-as-read-forward)
4565   (define-key gnus-summary-mode-map "D" 'gnus-summary-mark-as-read-backward)
4566   (define-key gnus-summary-mode-map "E" 'gnus-summary-mark-as-expirable)
4567   (define-key gnus-summary-mode-map "\M-u" 'gnus-summary-clear-mark-forward)
4568   (define-key gnus-summary-mode-map "\M-U" 'gnus-summary-clear-mark-backward)
4569   (define-key gnus-summary-mode-map "k" 'gnus-summary-kill-same-subject-and-select)
4570   (define-key gnus-summary-mode-map "\C-k" 'gnus-summary-kill-same-subject)
4571   (define-key gnus-summary-mode-map "\M-\C-k" 'gnus-summary-kill-thread)
4572   (define-key gnus-summary-mode-map "\M-\C-l" 'gnus-summary-lower-thread)
4573   (define-key gnus-summary-mode-map "e" 'gnus-summary-edit-article)
4574   (define-key gnus-summary-mode-map "#" 'gnus-summary-mark-as-processable)
4575   (define-key gnus-summary-mode-map "\M-#" 'gnus-summary-unmark-as-processable)
4576   (define-key gnus-summary-mode-map "\M-\C-t" 'gnus-summary-toggle-threads)
4577   (define-key gnus-summary-mode-map "\M-\C-s" 'gnus-summary-show-thread)
4578   (define-key gnus-summary-mode-map "\M-\C-h" 'gnus-summary-hide-thread)
4579   (define-key gnus-summary-mode-map "\M-\C-f" 'gnus-summary-next-thread)
4580   (define-key gnus-summary-mode-map "\M-\C-b" 'gnus-summary-prev-thread)
4581   (define-key gnus-summary-mode-map "\M-\C-u" 'gnus-summary-up-thread)
4582   (define-key gnus-summary-mode-map "\M-\C-d" 'gnus-summary-down-thread)
4583   (define-key gnus-summary-mode-map "&" 'gnus-summary-execute-command)
4584   (define-key gnus-summary-mode-map "c" 'gnus-summary-catchup-and-exit)
4585   (define-key gnus-summary-mode-map "\C-w" 'gnus-summary-mark-region-as-read)
4586   (define-key gnus-summary-mode-map "\C-t" 'gnus-summary-toggle-truncation)
4587   (define-key gnus-summary-mode-map "?" 'gnus-summary-mark-as-dormant)
4588   (define-key gnus-summary-mode-map "\C-c\M-\C-s" 'gnus-summary-show-all-expunged)
4589   (define-key gnus-summary-mode-map "\C-c\C-s\C-n" 'gnus-summary-sort-by-number)
4590   (define-key gnus-summary-mode-map "\C-c\C-s\C-a" 'gnus-summary-sort-by-author)
4591   (define-key gnus-summary-mode-map "\C-c\C-s\C-s" 'gnus-summary-sort-by-subject)
4592   (define-key gnus-summary-mode-map "\C-c\C-s\C-d" 'gnus-summary-sort-by-date)
4593   (define-key gnus-summary-mode-map "\C-c\C-s\C-i" 'gnus-summary-sort-by-score)
4594   (define-key gnus-summary-mode-map "=" 'gnus-summary-expand-window)
4595   (define-key gnus-summary-mode-map "\C-x\C-s" 'gnus-summary-reselect-current-group)
4596   (define-key gnus-summary-mode-map "\M-g" 'gnus-summary-rescan-group)
4597   (define-key gnus-summary-mode-map "w" 'gnus-summary-stop-page-breaking)
4598   (define-key gnus-summary-mode-map "\C-c\C-r" 'gnus-summary-caesar-message)
4599   (define-key gnus-summary-mode-map "\M-t" 'gnus-summary-toggle-mime)
4600   (define-key gnus-summary-mode-map "f" 'gnus-summary-followup)
4601   (define-key gnus-summary-mode-map "F" 'gnus-summary-followup-with-original)
4602   (define-key gnus-summary-mode-map "C" 'gnus-summary-cancel-article)
4603   (define-key gnus-summary-mode-map "r" 'gnus-summary-reply)
4604   (define-key gnus-summary-mode-map "R" 'gnus-summary-reply-with-original)
4605   (define-key gnus-summary-mode-map "\C-c\C-f" 'gnus-summary-mail-forward)
4606   (define-key gnus-summary-mode-map "o" 'gnus-summary-save-article)
4607   (define-key gnus-summary-mode-map "\C-o" 'gnus-summary-save-article-mail)
4608   (define-key gnus-summary-mode-map "|" 'gnus-summary-pipe-output)
4609   (define-key gnus-summary-mode-map "\M-k" 'gnus-summary-edit-local-kill)
4610   (define-key gnus-summary-mode-map "\M-K" 'gnus-summary-edit-global-kill)
4611   (define-key gnus-summary-mode-map "V" 'gnus-version)
4612   (define-key gnus-summary-mode-map "\C-c\C-d" 'gnus-summary-describe-group)
4613   (define-key gnus-summary-mode-map "q" 'gnus-summary-exit)
4614   (define-key gnus-summary-mode-map "Q" 'gnus-summary-exit-no-update)
4615   (define-key gnus-summary-mode-map "\C-c\C-i" 'gnus-info-find-node)
4616   (define-key gnus-summary-mode-map
4617     (if gnus-xemacs [button2] [mouse-2]) 'gnus-mouse-pick-article)
4618   (define-key gnus-summary-mode-map "m" 'gnus-summary-mail-other-window)
4619   (define-key gnus-summary-mode-map "a" 'gnus-summary-post-news)
4620   (define-key gnus-summary-mode-map "x" 'gnus-summary-remove-lines-marked-as-read)
4621 ; (define-key gnus-summary-mode-map "X" 'gnus-summary-remove-lines-marked-with)
4622   (define-key gnus-summary-mode-map "s" 'gnus-summary-isearch-article)
4623   (define-key gnus-summary-mode-map "t" 'gnus-summary-toggle-header)
4624   (define-key gnus-summary-mode-map "g" 'gnus-summary-show-article)
4625 ;  (define-key gnus-summary-mode-map "?" 'gnus-summary-describe-briefly)
4626   (define-key gnus-summary-mode-map "l" 'gnus-summary-goto-last-article)
4627   (define-key gnus-summary-mode-map "\C-c\C-v\C-v" 'gnus-uu-decode-uu-view)
4628   (define-key gnus-summary-mode-map "\C-d" 'gnus-summary-enter-digest-group)
4629
4630
4631   ;; Sort of orthogonal keymap
4632   (define-prefix-command 'gnus-summary-mark-map)
4633   (define-key gnus-summary-mode-map "M" 'gnus-summary-mark-map)
4634   (define-key gnus-summary-mark-map "t" 'gnus-summary-tick-article-forward)
4635   (define-key gnus-summary-mark-map "!" 'gnus-summary-tick-article-forward)
4636   (define-key gnus-summary-mark-map "d" 'gnus-summary-mark-as-read-forward)
4637   (define-key gnus-summary-mark-map "r" 'gnus-summary-mark-as-read-forward)
4638   (define-key gnus-summary-mark-map "c" 'gnus-summary-clear-mark-forward)
4639   (define-key gnus-summary-mark-map " " 'gnus-summary-clear-mark-forward)
4640   (define-key gnus-summary-mark-map "e" 'gnus-summary-mark-as-expirable)
4641   (define-key gnus-summary-mark-map "x" 'gnus-summary-mark-as-expirable)
4642   (define-key gnus-summary-mark-map "?" 'gnus-summary-mark-as-dormant)
4643   (define-key gnus-summary-mark-map "b" 'gnus-summary-set-bookmark)
4644   (define-key gnus-summary-mark-map "B" 'gnus-summary-remove-bookmark)
4645   (define-key gnus-summary-mark-map "#" 'gnus-summary-mark-as-processable)
4646   (define-key gnus-summary-mark-map "\M-#" 'gnus-summary-unmark-as-processable)
4647   (define-key gnus-summary-mark-map "\M-r" 'gnus-summary-remove-lines-marked-as-read)
4648   (define-key gnus-summary-mark-map "\M-\C-r" 'gnus-summary-remove-lines-marked-with)
4649   (define-key gnus-summary-mark-map "D" 'gnus-summary-show-all-dormant)
4650   (define-key gnus-summary-mark-map "\M-D" 'gnus-summary-hide-all-dormant)
4651   (define-key gnus-summary-mark-map "S" 'gnus-summary-show-all-expunged)
4652   (define-key gnus-summary-mark-map "C" 'gnus-summary-catchup)
4653   (define-key gnus-summary-mark-map "H" 'gnus-summary-catchup-to-here)
4654   (define-key gnus-summary-mark-map "\C-c" 'gnus-summary-catchup-all)
4655   (define-key gnus-summary-mark-map "k" 'gnus-summary-kill-same-subject-and-select)
4656   (define-key gnus-summary-mark-map "K" 'gnus-summary-kill-same-subject)
4657
4658   (define-prefix-command 'gnus-summary-mscore-map)
4659   (define-key gnus-summary-mark-map "s" 'gnus-summary-mscore-map)
4660   (define-key gnus-summary-mscore-map "c" 'gnus-summary-clear-above)
4661   (define-key gnus-summary-mscore-map "u" 'gnus-summary-tick-above)
4662   (define-key gnus-summary-mscore-map "m" 'gnus-summary-mark-above)
4663   (define-key gnus-summary-mscore-map "k" 'gnus-summary-kill-below)
4664
4665   (define-prefix-command 'gnus-summary-process-map)
4666   (define-key gnus-summary-mark-map "p" 'gnus-summary-process-map)
4667   (define-key gnus-summary-process-map "p" 'gnus-summary-mark-as-processable)
4668   (define-key gnus-summary-process-map "u" 'gnus-summary-unmark-as-processable)
4669   (define-key gnus-summary-process-map "U" 'gnus-summary-unmark-all-processable)
4670   (define-key gnus-summary-process-map "s" 'gnus-uu-mark-series)
4671   (define-key gnus-summary-process-map "r" 'gnus-uu-mark-region)
4672   (define-key gnus-summary-process-map "R" 'gnus-uu-mark-by-regexp)
4673   (define-key gnus-summary-process-map "t" 'gnus-uu-mark-thread)
4674   (define-key gnus-summary-process-map "a" 'gnus-uu-mark-all)
4675   (define-key gnus-summary-process-map "S" 'gnus-uu-mark-sparse)
4676   
4677   (define-key gnus-summary-mode-map "S" 'gnus-summary-send-map)
4678   
4679   (define-prefix-command 'gnus-summary-goto-map)
4680   (define-key gnus-summary-mode-map "G" 'gnus-summary-goto-map)
4681   (define-key gnus-summary-goto-map "n" 'gnus-summary-next-unread-article)
4682   (define-key gnus-summary-goto-map "p" 'gnus-summary-prev-unread-article)
4683   (define-key gnus-summary-goto-map "N" 'gnus-summary-next-article)
4684   (define-key gnus-summary-goto-map "P" 'gnus-summary-prev-article)
4685   (define-key gnus-summary-goto-map "\C-n" 'gnus-summary-next-same-subject)
4686   (define-key gnus-summary-goto-map "\C-p" 'gnus-summary-prev-same-subject)
4687   (define-key gnus-summary-goto-map "\M-n" 'gnus-summary-next-unread-subject)
4688   (define-key gnus-summary-goto-map "\M-p" 'gnus-summary-prev-unread-subject)
4689   (define-key gnus-summary-goto-map "f" 'gnus-summary-first-unread-article)
4690   (define-key gnus-summary-goto-map "b" 'gnus-summary-best-unread-article)
4691   (define-key gnus-summary-goto-map "g" 'gnus-summary-goto-subject)
4692   (define-key gnus-summary-goto-map "l" 'gnus-summary-goto-last-article)
4693   (define-key gnus-summary-goto-map "p" 'gnus-summary-pop-article)
4694
4695
4696   (define-prefix-command 'gnus-summary-thread-map)
4697   (define-key gnus-summary-mode-map "T" 'gnus-summary-thread-map)
4698   (define-key gnus-summary-thread-map "k" 'gnus-summary-kill-thread)
4699   (define-key gnus-summary-thread-map "l" 'gnus-summary-lower-thread)
4700   (define-key gnus-summary-thread-map "i" 'gnus-summary-raise-thread)
4701   (define-key gnus-summary-thread-map "T" 'gnus-summary-toggle-threads)
4702   (define-key gnus-summary-thread-map "s" 'gnus-summary-show-thread)
4703   (define-key gnus-summary-thread-map "S" 'gnus-summary-show-all-threads)
4704   (define-key gnus-summary-thread-map "h" 'gnus-summary-hide-thread)
4705   (define-key gnus-summary-thread-map "H" 'gnus-summary-hide-all-threads)
4706   (define-key gnus-summary-thread-map "n" 'gnus-summary-next-thread)
4707   (define-key gnus-summary-thread-map "p" 'gnus-summary-prev-thread)
4708   (define-key gnus-summary-thread-map "u" 'gnus-summary-up-thread)
4709   (define-key gnus-summary-thread-map "d" 'gnus-summary-down-thread)
4710   (define-key gnus-summary-thread-map "#" 'gnus-uu-mark-thread)
4711
4712   
4713   (define-prefix-command 'gnus-summary-exit-map)
4714   (define-key gnus-summary-mode-map "Z" 'gnus-summary-exit-map)
4715   (define-key gnus-summary-exit-map "c" 'gnus-summary-catchup-and-exit)
4716   (define-key gnus-summary-exit-map "C" 'gnus-summary-catchup-all-and-exit)
4717   (define-key gnus-summary-exit-map "E" 'gnus-summary-exit-no-update)
4718   (define-key gnus-summary-exit-map "Q" 'gnus-summary-exit)
4719   (define-key gnus-summary-exit-map "Z" 'gnus-summary-exit)
4720   (define-key gnus-summary-exit-map "n" 'gnus-summary-catchup-and-goto-next-group)
4721   (define-key gnus-summary-exit-map "R" 'gnus-summary-reselect-current-group)
4722   (define-key gnus-summary-exit-map "G" 'gnus-summary-rescan-group)
4723
4724
4725   (define-prefix-command 'gnus-summary-article-map)
4726   (define-key gnus-summary-mode-map "A" 'gnus-summary-article-map)
4727   (define-key gnus-summary-article-map " " 'gnus-summary-next-page)
4728   (define-key gnus-summary-article-map "n" 'gnus-summary-next-page)
4729   (define-key gnus-summary-article-map "\177" 'gnus-summary-prev-page)
4730   (define-key gnus-summary-article-map "p" 'gnus-summary-prev-page)
4731   (define-key gnus-summary-article-map "\r" 'gnus-summary-scroll-up)
4732   (define-key gnus-summary-article-map "<" 'gnus-summary-beginning-of-article)
4733   (define-key gnus-summary-article-map ">" 'gnus-summary-end-of-article)
4734   (define-key gnus-summary-article-map "b" 'gnus-summary-beginning-of-article)
4735   (define-key gnus-summary-article-map "e" 'gnus-summary-end-of-article)
4736   (define-key gnus-summary-article-map "^" 'gnus-summary-refer-parent-article)
4737   (define-key gnus-summary-article-map "r" 'gnus-summary-refer-parent-article)
4738   (define-key gnus-summary-article-map "w" 'gnus-summary-stop-page-breaking)
4739   (define-key gnus-summary-article-map "c" 'gnus-summary-caesar-message)
4740   (define-key gnus-summary-article-map "g" 'gnus-summary-show-article)
4741   (define-key gnus-summary-article-map "t" 'gnus-summary-toggle-header)
4742   (define-key gnus-summary-article-map "m" 'gnus-summary-toggle-mime)
4743   (define-key gnus-summary-article-map "s" 'gnus-summary-isearch-article)
4744
4745
4746   (define-prefix-command 'gnus-summary-wash-map)
4747   (define-key gnus-summary-mode-map "W" 'gnus-summary-wash-map)
4748   (define-key gnus-summary-wash-map "h" 'gnus-article-hide-headers)
4749   (define-key gnus-summary-wash-map "s" 'gnus-article-hide-signature)
4750   (define-key gnus-summary-wash-map "c" 'gnus-article-hide-citation)
4751   (define-key gnus-summary-wash-map "o" 'gnus-article-treat-overstrike)
4752   (define-key gnus-summary-wash-map "w" 'gnus-article-word-wrap)
4753   (define-key gnus-summary-wash-map "d" 'gnus-article-remove-cr)
4754   (define-key gnus-summary-wash-map "q" 'gnus-article-de-quoted-unreadable)
4755   (define-key gnus-summary-wash-map "f" 'gnus-article-display-x-face)
4756   (define-key gnus-summary-wash-map "t" 'gnus-article-date-ut)
4757   (define-key gnus-summary-wash-map "\C-t" 'gnus-article-date-local)
4758   (define-key gnus-summary-wash-map "T" 'gnus-article-date-lapsed)
4759
4760
4761   (define-prefix-command 'gnus-summary-help-map)
4762   (define-key gnus-summary-mode-map "H" 'gnus-summary-help-map)
4763   (define-key gnus-summary-help-map "v" 'gnus-version)
4764   (define-key gnus-summary-help-map "f" 'gnus-summary-fetch-faq)
4765   (define-key gnus-summary-help-map "d" 'gnus-summary-describe-group)
4766   (define-key gnus-summary-help-map "h" 'gnus-summary-describe-briefly)
4767   (define-key gnus-summary-help-map "i" 'gnus-info-find-node)
4768
4769
4770   (define-prefix-command 'gnus-summary-backend-map)
4771   (define-key gnus-summary-mode-map "B" 'gnus-summary-backend-map)
4772   (define-key gnus-summary-backend-map "e" 'gnus-summary-expire-articles)
4773   (define-key gnus-summary-backend-map "\177" 'gnus-summary-delete-article)
4774   (define-key gnus-summary-backend-map "m" 'gnus-summary-move-article)
4775   (define-key gnus-summary-backend-map "r" 'gnus-summary-respool-article)
4776   (define-key gnus-summary-backend-map "w" 'gnus-summary-edit-article)
4777   (define-key gnus-summary-backend-map "c" 'gnus-summary-copy-article)
4778
4779
4780   (define-prefix-command 'gnus-summary-save-map)
4781   (define-key gnus-summary-mode-map "O" 'gnus-summary-save-map)
4782   (define-key gnus-summary-save-map "o" 'gnus-summary-save-article)
4783   (define-key gnus-summary-save-map "m" 'gnus-summary-save-article-mail)
4784   (define-key gnus-summary-save-map "r" 'gnus-summary-save-article-rmail)
4785   (define-key gnus-summary-save-map "f" 'gnus-summary-save-article-file)
4786   (define-key gnus-summary-save-map "h" 'gnus-summary-save-article-folder)
4787   (define-key gnus-summary-save-map "p" 'gnus-summary-pipe-output)
4788
4789
4790   (define-prefix-command 'gnus-summary-extract-map)
4791   (define-key gnus-summary-mode-map "X" 'gnus-summary-extract-map)
4792 ;  (define-key gnus-summary-extract-map "x" 'gnus-summary-extract-any)
4793 ;  (define-key gnus-summary-extract-map "m" 'gnus-summary-extract-mime)
4794   (define-key gnus-summary-extract-map "u" 'gnus-uu-decode-uu)
4795   (define-key gnus-summary-extract-map "U" 'gnus-uu-decode-uu-and-save)
4796   (define-key gnus-summary-extract-map "s" 'gnus-uu-decode-unshar)
4797   (define-key gnus-summary-extract-map "S" 'gnus-uu-decode-unshar-and-save)
4798   (define-key gnus-summary-extract-map "o" 'gnus-uu-decode-save)
4799   (define-key gnus-summary-extract-map "O" 'gnus-uu-decode-save)
4800   (define-key gnus-summary-extract-map "b" 'gnus-uu-decode-binhex)
4801   (define-key gnus-summary-extract-map "B" 'gnus-uu-decode-binhex)
4802   (define-key gnus-summary-extract-map "p" 'gnus-uu-decode-postscript)
4803   (define-key gnus-summary-extract-map "P" 'gnus-uu-decode-postscript-and-save)
4804
4805
4806   (define-prefix-command 'gnus-summary-extract-view-map)
4807   (define-key gnus-summary-extract-map "v" 'gnus-summary-extract-view-map)
4808   (define-key gnus-summary-extract-view-map "u" 'gnus-uu-decode-uu-view)
4809   (define-key gnus-summary-extract-view-map "U" 'gnus-uu-decode-uu-and-save-view)
4810   (define-key gnus-summary-extract-view-map "s" 'gnus-uu-decode-unshar-view)
4811   (define-key gnus-summary-extract-view-map "S" 'gnus-uu-decode-unshar-and-save-view)
4812   (define-key gnus-summary-extract-view-map "o" 'gnus-uu-decode-save-view)
4813   (define-key gnus-summary-extract-view-map "O" 'gnus-uu-decode-save-view)
4814   (define-key gnus-summary-extract-view-map "b" 'gnus-uu-decode-binhex-view)
4815   (define-key gnus-summary-extract-view-map "B" 'gnus-uu-decode-binhex-view)
4816   (define-key gnus-summary-extract-view-map "p" 'gnus-uu-decode-postscript-view)
4817   (define-key gnus-summary-extract-view-map "P" 'gnus-uu-decode-postscript-and-save-view)
4818   
4819   
4820   (define-prefix-command 'gnus-summary-various-map)
4821   (define-key gnus-summary-mode-map "V" 'gnus-summary-various-map)
4822   (define-key gnus-summary-various-map "u" 'gnus-summary-universal-argument)
4823   (define-key gnus-summary-various-map "\C-s" 'gnus-summary-search-article-forward)
4824   (define-key gnus-summary-various-map "\C-r" 'gnus-summary-search-article-backward)
4825   (define-key gnus-summary-various-map "r" 'gnus-summary-refer-article)
4826   (define-key gnus-summary-various-map "&" 'gnus-summary-execute-command)
4827   (define-key gnus-summary-various-map "T" 'gnus-summary-toggle-truncation)
4828   (define-key gnus-summary-various-map "e" 'gnus-summary-expand-window)
4829   (define-key gnus-summary-various-map "D" 'gnus-summary-enter-digest-group)
4830   (define-key gnus-summary-various-map "k" 'gnus-summary-edit-local-kill)
4831   (define-key gnus-summary-various-map "K" 'gnus-summary-edit-global-kill)
4832
4833   (define-key gnus-summary-various-map "S" 'gnus-summary-score-map)
4834
4835   (define-prefix-command 'gnus-summary-sort-map)
4836   (define-key gnus-summary-various-map "s" 'gnus-summary-sort-map)
4837   (define-key gnus-summary-sort-map "n" 'gnus-summary-sort-by-number)
4838   (define-key gnus-summary-sort-map "a" 'gnus-summary-sort-by-author)
4839   (define-key gnus-summary-sort-map "s" 'gnus-summary-sort-by-subject)
4840   (define-key gnus-summary-sort-map "d" 'gnus-summary-sort-by-date)
4841   (define-key gnus-summary-sort-map "i" 'gnus-summary-sort-by-score)
4842
4843   (define-key gnus-summary-mode-map "I" 'gnus-summary-increase-map)
4844   (define-key gnus-summary-mode-map "L" 'gnus-summary-lower-map)
4845   )
4846
4847
4848 \f
4849
4850 (defun gnus-summary-mode ()
4851   "Major mode for reading articles.
4852
4853 All normal editing commands are switched off.
4854 \\<gnus-summary-mode-map>
4855 Each line in this buffer represents one article.  To read an
4856 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
4857 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]', 
4858 respectively.
4859
4860 You can also post articles and send mail from this buffer.  To 
4861 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author 
4862 of an article, type `\\[gnus-summary-reply]'.
4863
4864 There are approx. one gazillion commands you can execute in this 
4865 buffer; read the info pages for more information (`\\[gnus-info-find-node]'). 
4866
4867 The following commands are available:
4868
4869 \\{gnus-summary-mode-map}"
4870   (interactive)
4871   (if gnus-visual (gnus-summary-make-menu-bar))
4872   (kill-all-local-variables)
4873   (let ((locals gnus-summary-local-variables))
4874     (while locals
4875       (if (consp (car locals))
4876           (progn
4877             (make-local-variable (car (car locals)))
4878             (set (car (car locals)) (eval (cdr (car locals)))))
4879         (make-local-variable (car locals))
4880         (set (car locals) nil))
4881       (setq locals (cdr locals))))
4882   (gnus-make-thread-indent-array)
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 (list number ?Z 0)))))
4950
4951 (defvar gnus-thread-indent-array nil)
4952 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
4953 (defun gnus-make-thread-indent-array ()
4954   (let ((n 200))
4955     (if (and gnus-thread-indent-array
4956              (= gnus-thread-indent-level gnus-thread-indent-array-level))
4957         nil
4958       (setq gnus-thread-indent-array (make-vector 201 "")
4959             gnus-thread-indent-array-level gnus-thread-indent-level)
4960       (while (>= n 0)
4961         (aset gnus-thread-indent-array n
4962               (make-string (* n gnus-thread-indent-level) ? ))
4963         (setq n (1- n))))))
4964
4965 (defun gnus-summary-insert-line 
4966   (sformat header level current unread replied expirable subject-or-nil
4967            &optional dummy score)
4968   (or sformat (setq sformat gnus-summary-line-format-spec))
4969   (let* ((indentation (aref gnus-thread-indent-array level))
4970          (lines (header-lines header))
4971          (score (or score gnus-summary-default-score 0))
4972          (score-char (if (or (null gnus-summary-default-score)
4973                              (= score gnus-summary-default-score)) ? 
4974                        (if (< score gnus-summary-default-score) 
4975                            gnus-score-below-mark gnus-score-over-mark)))
4976          (replied (if replied gnus-replied-mark ? ))
4977          (from (header-from header))
4978          (name-address (funcall gnus-extract-address-components from))
4979          (address (car (cdr name-address)))
4980          (name (or (car name-address) (car (cdr name-address))))
4981          (subject (header-subject header))
4982          (number (header-number header))
4983          (opening-bracket (if dummy ?\< ?\[))
4984          (closing-bracket (if dummy ?\> ?\]))
4985          (buffer-read-only nil)
4986          (b (progn (beginning-of-line) (point))))
4987     (insert (eval sformat))
4988     (add-text-properties
4989      b (1+ b) (list 'gnus (list number (or unread gnus-unread-mark) level)))))
4990
4991 (defun gnus-summary-update-line (&optional dont-update)
4992   ;; Update summary line after change.
4993   (or (not gnus-summary-default-score)
4994       gnus-summary-inhibit-highlight
4995       (let ((gnus-summary-inhibit-highlight t)
4996             (article (gnus-summary-article-number)))
4997         (progn
4998           (or dont-update
4999               (if (and gnus-summary-mark-below
5000                        (< (gnus-summary-article-score)
5001                           gnus-summary-mark-below))
5002                   (and (not (memq article gnus-newsgroup-marked))
5003                        (not (memq article gnus-newsgroup-dormant))
5004                        (memq article gnus-newsgroup-unreads)
5005                        (gnus-summary-mark-article nil gnus-low-score-mark))
5006                 (and (eq (gnus-summary-article-mark) gnus-low-score-mark)
5007                      (gnus-summary-mark-article nil gnus-unread-mark))))
5008           (and gnus-visual
5009                (run-hooks 'gnus-visual-summary-update-hook))))))
5010
5011 (defun gnus-summary-update-lines ()
5012   ;; Rehighlight summary buffer according to `gnus-summary-highlight'.
5013   (and (save-excursion
5014          (set-buffer gnus-summary-buffer)
5015          (goto-char (point-min))
5016          (while (not (eobp))
5017            (gnus-summary-update-line)
5018            (forward-line 1)))))
5019
5020 (defun gnus-summary-number-of-articles-in-thread (thread &optional char)
5021   ;; Sum up all elements (and sub-elements) in a list.
5022   (let ((number 
5023          (if (listp thread) 
5024              (apply 
5025               '+ (mapcar 'gnus-summary-number-of-articles-in-thread thread))
5026            1)))
5027     (if char 
5028         (if (> number 1) gnus-not-empty-thread-mark
5029           gnus-empty-thread-mark)
5030       number)))
5031
5032 (defun gnus-summary-read-group (group &optional show-all no-article kill-buffer)
5033   "Start reading news in newsgroup GROUP.
5034 If SHOW-ALL is non-nil, already read articles are also listed.
5035 If NO-ARTICLE is non-nil, no article is selected initially."
5036   (message "Retrieving newsgroup: %s..." group)
5037   (gnus-summary-setup-buffer group)
5038   (let ((did-select (gnus-select-newsgroup group show-all))
5039         (method (car (gnus-find-method-for-group group))))
5040     (cond 
5041      ((null did-select) 
5042       (and (eq major-mode 'gnus-summary-mode)
5043            (not (equal (current-buffer) kill-buffer))
5044            (progn
5045              (kill-buffer (current-buffer))
5046              (switch-to-buffer gnus-group-buffer)
5047              (gnus-group-next-unread-group 1)))
5048       nil)
5049      ((eq did-select 'quit)
5050       (gnus-configure-windows 'summary)
5051       (gnus-configure-windows 'newsgroup)
5052       (and (eq major-mode 'gnus-summary-mode)
5053            (not (equal (current-buffer) kill-buffer))
5054            (kill-buffer (current-buffer)))
5055       (gnus-kill-buffer kill-buffer)
5056       (switch-to-buffer gnus-group-buffer)
5057       (gnus-group-next-unread-group 1)
5058       (signal 'quit nil))
5059      (t
5060       (gnus-set-global-variables)
5061       ;; Save the active value in effect when the group was entered.
5062       (setq gnus-newsgroup-active 
5063             (gnus-copy-sequence
5064              (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
5065       ;; You can change the subjects in this hook.
5066       (run-hooks 'gnus-select-group-hook)
5067       ;; Do score processing.
5068       (gnus-possibly-score-headers)
5069       ;; Update the format specifiers.
5070       (gnus-update-format-specifications)
5071       ;; Generate the summary buffer.
5072       (gnus-summary-prepare)
5073       (if (zerop (buffer-size))
5074           (cond (gnus-newsgroup-dormant
5075                  (gnus-summary-show-all-dormant))
5076                 ((and gnus-newsgroup-scored show-all)
5077                  (gnus-summary-show-all-expunged))))
5078       ;; Function `gnus-apply-kill-file' must be called in this hook.
5079       (run-hooks 'gnus-apply-kill-hook)
5080       (if (zerop (buffer-size))
5081           (progn
5082             ;; This newsgroup is empty.
5083             (gnus-summary-catchup-and-exit nil t) ;Without confirmations.
5084             (message "No unread news")
5085             (gnus-kill-buffer kill-buffer))
5086         (save-excursion
5087           (if kill-buffer
5088               (let ((gnus-summary-buffer kill-buffer))
5089                 (gnus-configure-windows 'newsgroups t))))
5090         ;; Hide conversation thread subtrees.  We cannot do this in
5091         ;; gnus-summary-prepare-hook since kill processing may not
5092         ;; work with hidden articles.
5093         (and gnus-show-threads
5094              gnus-thread-hide-subtree
5095              (gnus-summary-hide-all-threads))
5096         ;; Show first unread article if requested.
5097         (goto-char (point-min))
5098         (if (and (not no-article)
5099                  gnus-auto-select-first
5100                  (gnus-summary-first-unread-article))
5101             (gnus-configure-windows 'article)
5102           (gnus-configure-windows 'summary))
5103         (pop-to-buffer gnus-summary-buffer)
5104         (gnus-set-mode-line 'summary)
5105         (gnus-summary-position-cursor)
5106         ;; If in async mode, we send some info to the backend.
5107         (and gnus-newsgroup-async
5108              (setq gnus-newsgroup-threads (nreverse gnus-newsgroup-threads))
5109              (gnus-request-asynchronous 
5110               gnus-newsgroup-name
5111               (if (and gnus-asynchronous-article-function
5112                        (fboundp gnus-asynchronous-article-function))
5113                   (funcall gnus-asynchronous-article-function
5114                            gnus-newsgroup-threads)
5115                 gnus-newsgroup-threads)))
5116         (gnus-kill-buffer kill-buffer))))))
5117
5118 (defun gnus-summary-prepare ()
5119   ;; Generate the summary buffer.
5120   (let ((buffer-read-only nil))
5121     (erase-buffer)
5122     (gnus-summary-prepare-threads 
5123      (if gnus-show-threads
5124          (gnus-gather-threads 
5125           (gnus-sort-threads 
5126            (if gnus-summary-expunge-below
5127                (gnus-make-threads-and-expunge)
5128              (gnus-make-threads))))
5129        gnus-newsgroup-headers)
5130      0 nil nil t)
5131     ;; Erase header retrieval message.
5132     (gnus-summary-update-lines)
5133     (message "")
5134     ;; Call hooks for modifying summary buffer.
5135     ;; Suggested by sven@tde.LTH.Se (Sven Mattisson).
5136     (goto-char (point-min))
5137     (run-hooks 'gnus-summary-prepare-hook)))
5138
5139 (defun gnus-gather-threads (threads)
5140   "Gather threads that have lost their roots."
5141   (if (not gnus-summary-make-false-root)
5142       threads 
5143     (let ((hashtb (gnus-make-hashtable 1023))
5144           (prev threads)
5145           (result threads)
5146           thread subject hthread unre-subject whole-subject)
5147       (while threads
5148         (setq subject (header-subject (car (car threads)))
5149               whole-subject subject)
5150         (and gnus-summary-gather-subject-limit
5151              (or (and (numberp gnus-summary-gather-subject-limit)
5152                       (> (length subject) gnus-summary-gather-subject-limit)
5153                       (setq subject
5154                             (substring subject 0 
5155                                        gnus-summary-gather-subject-limit)))
5156                  (and (eq 'fuzzy gnus-summary-gather-subject-limit)
5157                       (setq subject (gnus-simplify-subject-fuzzy subject)))))
5158         (if (setq hthread 
5159                   (gnus-gethash 
5160                    (setq unre-subject (gnus-simplify-subject-re subject))
5161                    hashtb))
5162             (progn
5163               (or (stringp (car (car hthread)))
5164                   (setcar hthread (list whole-subject (car hthread))))
5165               (setcdr (car hthread) (nconc (cdr (car hthread)) 
5166                                            (list (car threads))))
5167               (setcdr prev (cdr threads))
5168               (setq threads prev))
5169           (gnus-sethash unre-subject threads hashtb))
5170         (setq prev threads)
5171         (setq threads (cdr threads)))
5172       result)))
5173
5174 (defun gnus-make-threads ()
5175   ;; This function takes the dependencies already made by 
5176   ;; `gnus-get-newsgroup-headers' and builds the trees. First we go
5177   ;; through the dependecies in the hash table and finds all the
5178   ;; roots. Roots do not refer back to any valid articles.
5179   (let (roots)
5180     (and gnus-fetch-old-headers (eq gnus-headers-retrieved-by 'nov)
5181          (gnus-build-old-threads))
5182     (mapatoms
5183      (lambda (refs)
5184        (if (not (car (symbol-value refs)))
5185            (setq roots (append (cdr (symbol-value refs)) roots))
5186          ;; Ok, these refer back to valid articles, but if
5187          ;; `gnus-thread-ignore-subject' is nil, we have to check that
5188          ;; the root has the same subject as its children. The children
5189          ;; that do not are made into roots and removed from the list
5190          ;; of children. 
5191          (or gnus-thread-ignore-subject
5192              (let* ((prev (symbol-value refs))
5193                     (subject (gnus-simplify-subject-re 
5194                               (header-subject (car prev))))
5195                     (headers (cdr prev)))
5196                (while headers
5197                  (if (not (string= subject
5198                                    (gnus-simplify-subject-re 
5199                                     (header-subject (car headers)))))
5200                      (progn
5201                        (setq roots (cons (car headers) roots))
5202                        (setcdr prev (cdr headers)))
5203                    (setq prev headers))
5204                  (setq headers (cdr headers)))))))
5205      gnus-newsgroup-dependencies)
5206     
5207     (mapcar 'gnus-trim-thread
5208             (apply 'append
5209                    (mapcar 'gnus-cut-thread
5210                            (mapcar 'gnus-make-sub-thread roots))))))
5211   
5212 (defun gnus-make-threads-and-expunge ()
5213   ;; This function takes the dependencies already made by 
5214   ;; `gnus-get-newsgroup-headers' and builds the trees. First we go
5215   ;; through the dependecies in the hash table and finds all the
5216   ;; roots. Roots do not refer back to any valid articles.
5217   (let (roots)
5218     (and gnus-fetch-old-headers (eq gnus-headers-retrieved-by 'nov)
5219          (gnus-build-old-threads))
5220     (mapatoms
5221      (lambda (refs)
5222        (if (not (car (symbol-value refs)))
5223            (if (and gnus-summary-expunge-below
5224                     (not gnus-fetch-old-headers))
5225                (let ((headers (cdr (symbol-value refs))))
5226                  (while headers
5227                   (if (not (< (or (cdr (assq (header-number (car headers))
5228                                              gnus-newsgroup-scored))
5229                                   gnus-summary-default-score 0)
5230                               gnus-summary-expunge-below))
5231                       (setq roots (cons (car headers) roots))
5232                     (setq gnus-newsgroup-unreads
5233                           (delq (header-number (car headers))
5234                                 gnus-newsgroup-unreads)))
5235                   (setq headers (cdr headers))))
5236              (setq roots (append (cdr (symbol-value refs)) roots)))
5237          ;; Ok, these refer back to valid articles, but if
5238          ;; `gnus-thread-ignore-subject' is nil, we have to check that
5239          ;; the root has the same subject as its children. The children
5240          ;; that do not are made into roots and removed from the list
5241          ;; of children. 
5242          (or gnus-thread-ignore-subject
5243              (let* ((prev (symbol-value refs))
5244                     (subject (gnus-simplify-subject-re 
5245                               (header-subject (car prev))))
5246                     (headers (cdr prev)))
5247                (while headers
5248                  (if (not (string= subject
5249                                    (gnus-simplify-subject-re 
5250                                     (header-subject (car headers)))))
5251                      (progn
5252                        (setq roots (cons (car headers) roots))
5253                        (setcdr prev (cdr headers)))
5254                    (setq prev headers))
5255                  (setq headers (cdr headers)))))
5256          (and gnus-summary-expunge-below
5257               (not gnus-fetch-old-headers)
5258               (let* ((prev (symbol-value refs))
5259                      (headers (cdr prev))
5260                      id)
5261                 (while headers
5262                   (if (not (< (or (cdr (assq (header-number (car headers))
5263                                              gnus-newsgroup-scored))
5264                                   gnus-summary-default-score 0)
5265                               gnus-summary-expunge-below))
5266                       (setq prev (cdr prev))
5267                     (setq gnus-newsgroup-unreads 
5268                           (delq (header-number (car headers))
5269                                 gnus-newsgroup-unreads))
5270                     (setcdr prev (cdr headers))
5271                     (setq id (gnus-gethash (header-id (car headers))
5272                                            gnus-newsgroup-dependencies))
5273                     (let ((h (cdr id)))
5274                       (while h
5275                         (if (not (< (or (cdr (assq (header-number (car h))
5276                                                    gnus-newsgroup-scored))
5277                                         gnus-summary-default-score 0)
5278                                     gnus-summary-expunge-below))
5279                             (setq roots (cons (car h) roots)))
5280                         (setq h (cdr h)))))
5281                   (setq headers (cdr headers)))))))
5282      gnus-newsgroup-dependencies)
5283     
5284     (mapcar 'gnus-trim-thread
5285             (apply 'append
5286                    (mapcar 'gnus-cut-thread
5287                            (mapcar 'gnus-make-sub-thread roots))))))
5288   
5289 (defun gnus-cut-thread (thread)
5290   ;; Remove leaf dormant or ancient articles from THREAD.
5291   (let ((head (car thread))
5292         (tail (apply 'append (mapcar 'gnus-cut-thread (cdr thread)))))
5293     (if (and (null tail)
5294              (let ((number (header-number head)))
5295                (or (memq number gnus-newsgroup-ancient)
5296                    (memq number gnus-newsgroup-dormant)
5297                    (and gnus-summary-expunge-below
5298                         (eq gnus-fetch-old-headers 'some)
5299                         (< (or (cdr (assq number gnus-newsgroup-scored))
5300                                gnus-summary-default-score 0)
5301                            gnus-summary-expunge-below)
5302                         (progn
5303                           (setq gnus-newsgroup-unreads
5304                                 (delq number gnus-newsgroup-unreads))
5305                           t)))))
5306         nil
5307       (list (cons head tail)))))
5308
5309 (defun gnus-trim-thread (thread)
5310   ;; Remove root ancient articles with only one child from THREAD.
5311   (if (and (eq gnus-fetch-old-headers 'some)
5312            (memq (header-number (car thread)) gnus-newsgroup-ancient)
5313            (= (length thread) 2))
5314       (gnus-trim-thread (nth 1 thread))
5315     thread))
5316
5317 (defun gnus-make-sub-thread (root)
5318   ;; This function makes a sub-tree for a node in the tree.
5319   (let ((children (reverse (cdr (gnus-gethash (downcase (header-id root))
5320                                               gnus-newsgroup-dependencies)))))
5321     (cons root (mapcar 'gnus-make-sub-thread children))))
5322
5323 (defun gnus-build-old-threads ()
5324   ;; Look at all the articles that refer back to old articles, and
5325   ;; fetch the headers for the articles that aren't there. This will
5326   ;; build complete threads - if the roots haven't been expired by the
5327   ;; server, that is.
5328   (let (id heads)
5329     (mapatoms
5330      (lambda (refs)
5331        (if (not (car (symbol-value refs)))
5332            (progn
5333              (setq heads (cdr (symbol-value refs)))
5334              (while heads
5335                (if (not (memq (header-number (car heads))
5336                               gnus-newsgroup-dormant))
5337                    (progn
5338                      (setq id (symbol-name refs))
5339                      (while (and (setq id (gnus-build-get-header id))
5340                                  (not (car (gnus-gethash 
5341                                             id gnus-newsgroup-dependencies)))))
5342                      (setq heads nil))
5343                  (setq heads (cdr heads)))))))
5344      gnus-newsgroup-dependencies)))
5345
5346 (defun gnus-build-get-header (id)
5347   ;; Look through the buffer of NOV lines and find the header to
5348   ;; ID. Enter this line into the dependencies hash table, and return
5349   ;; the id of the parent article (if any).
5350   (let ((deps gnus-newsgroup-dependencies)
5351         found header)
5352     (prog1
5353         (save-excursion
5354           (set-buffer nntp-server-buffer)
5355           (goto-char (point-min))
5356           (while (and (not found) (search-forward id nil t))
5357             (beginning-of-line)
5358             (setq found (looking-at (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
5359                                             (regexp-quote id))))
5360             (or found (beginning-of-line 2)))
5361           (if found
5362               (let (ref)
5363                 (beginning-of-line)
5364                 (and
5365                  (setq header (gnus-nov-parse-line 
5366                                (read (current-buffer)) deps))
5367                  (setq ref (header-references header))
5368                  (string-match "\\(<[^>]+>\\) *$" ref)
5369                  (substring ref (match-beginning 1) (match-end 1))))))
5370       (and header
5371            (setq gnus-newsgroup-headers (cons header gnus-newsgroup-headers)
5372                  gnus-newsgroup-ancient (cons (header-number header)
5373                                               gnus-newsgroup-ancient))))))
5374
5375 (defun gnus-sort-threads (threads)
5376   ;; Sort threads as specified in `gnus-thread-sort-functions'.
5377   (let ((fun gnus-thread-sort-functions))
5378     (while fun
5379       (setq threads (sort threads (car fun))
5380             fun (cdr fun))))
5381   threads)
5382
5383 (defun gnus-thread-header (thread)
5384   ;; Return header of first article in THREAD.
5385   (if (consp thread)
5386       (if (stringp (car thread))
5387           (car (car (cdr thread)))
5388         (car thread))
5389     thread))
5390
5391 (defun gnus-thread-sort-by-number (h1 h2)
5392   "Sort threads by root article number."
5393   (let ((h1 (gnus-thread-header h1))
5394         (h2 (gnus-thread-header h2)))
5395     (< (header-number h1) (header-number h2))))
5396
5397 (defun gnus-thread-sort-by-author (h1 h2)
5398   "Sort threads by root author."
5399   (let ((h1 (gnus-thread-header h1))
5400         (h2 (gnus-thread-header h2)))
5401     (string-lessp
5402      (let ((extract (funcall 
5403                      gnus-extract-address-components (header-from h1))))
5404        (or (car extract) (cdr extract)))
5405      (let ((extract (funcall
5406                      gnus-extract-address-components (header-from h2))))
5407        (or (car extract) (cdr extract))))))
5408
5409 (defun gnus-thread-sort-by-subject (h1 h2)
5410   "Sort threads by root subject."
5411   (let ((h1 (gnus-thread-header h1))
5412         (h2 (gnus-thread-header h2)))
5413     (string-lessp
5414      (downcase (gnus-simplify-subject (header-subject h1)))
5415      (downcase (gnus-simplify-subject (header-subject h2))))))
5416
5417 (defun gnus-thread-sort-by-date (h1 h2)
5418   "Sort threads by root article date."
5419   (let ((h1 (gnus-thread-header h1))
5420         (h2 (gnus-thread-header h2)))
5421     (string-lessp
5422      (gnus-sortable-date (header-date h1))
5423      (gnus-sortable-date (header-date h2)))))
5424
5425 (defun gnus-thread-sort-by-score (h1 h2)
5426   "Sort threads by root article score.
5427 Unscored articles will be counted as havin a score of zero."
5428   (let ((h1 (gnus-thread-header h1))
5429         (h2 (gnus-thread-header h2)))
5430     (let ((s1 (assq (header-number h1) gnus-newsgroup-scored))
5431           (s2 (assq (header-number h2) gnus-newsgroup-scored)))
5432       (> (or (cdr s1) gnus-summary-default-score 0)
5433          (or (cdr s2) gnus-summary-default-score 0)))))
5434
5435 (defun gnus-thread-sort-by-total-score (h1 h2)
5436   "Sort threads by the sum of all scores in the thread.
5437 Unscored articles will be counted as havin a score of zero."
5438   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
5439
5440 (defun gnus-thread-total-score (thread)
5441   ;;  This function find the total score of  THREAD.
5442   (if (consp thread)
5443       (if (stringp (car thread))
5444           (apply gnus-thread-score-function 0
5445                  (mapcar 'gnus-thread-total-score-1 (cdr thread)))
5446         (gnus-thread-total-score-1 thread))
5447     (gnus-thread-total-score-1 (list thread))))
5448
5449 (defun gnus-thread-total-score-1 (root)
5450   ;; This function find the total score of the thread below ROOT.
5451   (setq root (car root))
5452   (apply gnus-thread-score-function
5453          (or (cdr (assq (header-number root) gnus-newsgroup-scored))
5454              gnus-summary-default-score 0)
5455          (mapcar 'gnus-thread-total-score
5456                  (cdr (gnus-gethash (downcase (header-id root))
5457                                     gnus-newsgroup-dependencies)))))
5458
5459 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
5460 (defvar gnus-tmp-prev-subject "")
5461
5462 ;; Basic ideas by Paul Dworkin <paul@media-lab.media.mit.edu>.
5463 (defun gnus-summary-prepare-threads 
5464   (threads level &optional not-child no-subject cull)
5465   "Prepare summary buffer from THREADS and indentation LEVEL.  
5466 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'  
5467 or a straight list of headers."
5468   (let (thread header number subject clevel)
5469     (while threads
5470       (setq thread (car threads)
5471             threads (cdr threads))
5472       ;; If `thread' is a cons, hierarchical threads are used.  If not,
5473       ;; `thread' is the header.
5474       (if (consp thread)
5475           (setq header (car thread))
5476         (setq header thread)
5477         (and cull
5478              (or (memq (setq number (header-number header))
5479                        gnus-newsgroup-dormant)
5480                  (and gnus-summary-expunge-below
5481                       (< (or (cdr (assq number gnus-newsgroup-scored))
5482                              gnus-summary-default-score 0)
5483                          gnus-summary-expunge-below)))
5484              (progn
5485                (setq header nil)
5486                (setq gnus-newsgroup-unreads 
5487                      (delq number gnus-newsgroup-unreads)))))
5488       (cond 
5489        ((stringp header)
5490         ;; The header is a dummy root.
5491         (cond ((eq gnus-summary-make-false-root 'adopt)
5492                ;; We let the first article adopt the rest.
5493                (gnus-summary-prepare-threads (list (car (cdr thread))) 0)
5494                (setq thread (cdr (cdr thread)))
5495                (while thread
5496                  (gnus-summary-prepare-threads (list (car thread)) 1 t)
5497                  (setq thread (cdr thread))))
5498               ((eq gnus-summary-make-false-root 'dummy)
5499                ;; We output a dummy root.
5500                (gnus-summary-insert-dummy-line 
5501                 nil header (header-number (car (car (cdr thread)))))
5502                (setq clevel 1))
5503               ((eq gnus-summary-make-false-root 'empty)
5504                ;; We print the articles with empty subject fields. 
5505                (gnus-summary-prepare-threads (list (car (cdr thread))) 0)
5506                (setq thread (cdr (cdr thread)))
5507                (while thread
5508                  (gnus-summary-prepare-threads 
5509                   (list (car thread)) 0 nil
5510                   (not (and (eq gnus-summary-gather-subject-limit 'fuzzy)
5511                             (not (string=  
5512                                   (gnus-simplify-subject-re 
5513                                    (header-subject (car (car thread))))
5514                                   (gnus-simplify-subject-re header))))))
5515                  (setq thread (cdr thread))))
5516               (t
5517                ;; We do not make a root for the gathered
5518                ;; sub-threads at all.  
5519                (setq clevel 0)))
5520         ;; Print the sub-threads.
5521         (and (consp thread) (cdr thread)
5522              (gnus-summary-prepare-threads (cdr thread) clevel)))
5523        ;; The header is a real article.
5524        (header
5525         (setq number (header-number header)
5526               subject (header-subject header))
5527         (and gnus-newsgroup-async
5528              (setq gnus-newsgroup-threads
5529                    (cons (cons (header-number header)
5530                                (header-lines header)) gnus-newsgroup-threads)))
5531         (gnus-summary-insert-line
5532          nil header level nil 
5533          (cond ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
5534                ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
5535                ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
5536                ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
5537                (t gnus-ancient-mark))
5538          (memq number gnus-newsgroup-replied)
5539          (memq number gnus-newsgroup-expirable)
5540          (if no-subject 
5541              gnus-summary-same-subject
5542            (if (or (zerop level)
5543                    (and gnus-thread-ignore-subject
5544                         (not (string= 
5545                               (gnus-simplify-subject-re gnus-tmp-prev-subject)
5546                               (gnus-simplify-subject-re subject)))))
5547                subject
5548              gnus-summary-same-subject))
5549          not-child
5550          (cdr (assq number gnus-newsgroup-scored)))
5551         (setq gnus-tmp-prev-subject subject)
5552         ;; Recursively print subthreads.
5553         (and (consp thread) (cdr thread)
5554              (gnus-summary-prepare-threads (cdr thread) (1+ level))))))))
5555
5556 (defun gnus-select-newsgroup (group &optional read-all)
5557   "Select newsgroup GROUP.
5558 If READ-ALL is non-nil, all articles in the group are selected."
5559   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5560          (info (nth 2 entry))
5561          articles header-marks)
5562     (gnus-check-news-server
5563      (setq gnus-current-select-method (gnus-find-method-for-group group)))
5564
5565     (or (gnus-server-opened gnus-current-select-method)
5566         (gnus-open-server gnus-current-select-method)
5567         (error "Couldn't open server"))
5568     
5569     (or (and (eq (car entry) t)
5570              (gnus-activate-newsgroup (car info)))
5571         (gnus-request-group group t)
5572         (progn
5573           (kill-buffer (current-buffer))
5574           (error "Couldn't request group %s: %s" 
5575                  group (gnus-status-message group))))
5576
5577     (setq gnus-newsgroup-name group)
5578     (setq gnus-newsgroup-unselected nil)
5579     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5580
5581     (and gnus-asynchronous
5582          (gnus-check-backend-function 
5583           'request-asynchronous gnus-newsgroup-name)
5584          (setq gnus-newsgroup-async
5585                (gnus-request-asynchronous gnus-newsgroup-name)))
5586
5587     (setq articles (gnus-articles-to-read group read-all))
5588
5589     (cond 
5590      ((null articles) 
5591       (message "Couldn't select newsgroup")
5592       'quit)
5593      ((eq articles 0) nil)
5594      (t
5595       ;; Init the dependencies hash table.
5596       (setq gnus-newsgroup-dependencies 
5597             (gnus-make-hashtable (length articles)))
5598       ;; Retrieve the headers and read them in.
5599       (setq gnus-newsgroup-headers 
5600             (if (eq 'nov (setq gnus-headers-retrieved-by
5601                                (gnus-retrieve-headers 
5602                                 (if gnus-fetch-old-headers 
5603                                     (cons 1 articles) articles) 
5604                                 gnus-newsgroup-name)))
5605                 (progn
5606                   (gnus-get-newsgroup-headers-xover articles))
5607               (gnus-get-newsgroup-headers)))
5608       ;; If we were to fetch old headers, but the backend didn't
5609       ;; support XOVER, then it is possible we fetched one article
5610       ;; that we shouldn't have. If that's the case, we pop it off the
5611       ;; list of headers.
5612       (and (not (eq gnus-headers-retrieved-by 'nov))
5613            gnus-fetch-old-headers
5614            gnus-newsgroup-headers
5615            (/= (header-number (car gnus-newsgroup-headers)) (car articles))
5616            (setq gnus-newsgroup-headers (cdr gnus-newsgroup-headers)))
5617       ;; Remove cancelled articles from the list of unread articles.
5618       (setq gnus-newsgroup-unreads
5619             (gnus-set-sorted-intersection 
5620              gnus-newsgroup-unreads
5621              (mapcar (lambda (headers) (header-number headers))
5622                      gnus-newsgroup-headers)))
5623       ;; Adjust and set lists of article marks.
5624       (and info
5625            (let (marked)
5626              (gnus-adjust-marked-articles info)
5627              (setq gnus-newsgroup-marked 
5628                    (cdr (assq 'tick (setq marked (nth 3 info)))))
5629              (setq gnus-newsgroup-replied (cdr (assq 'reply marked)))
5630              (setq gnus-newsgroup-expirable (cdr (assq 'expire marked)))
5631              (setq gnus-newsgroup-killed (cdr (assq 'killed marked)))
5632              (setq gnus-newsgroup-bookmarks (cdr (assq 'bookmark marked)))
5633              (setq gnus-newsgroup-dormant (cdr (assq 'dormant marked)))
5634              (setq gnus-newsgroup-scored (cdr (assq 'score marked)))
5635              (setq gnus-newsgroup-processable nil)))
5636       ;; Check whether auto-expire is to be done in this group.
5637       (setq gnus-newsgroup-auto-expire
5638             (or (and (stringp gnus-auto-expirable-newsgroups)
5639                      (string-match gnus-auto-expirable-newsgroups group))
5640                 (memq 'auto-expire (nth 5 info))))
5641       ;; First and last article in this newsgroup.
5642       (and gnus-newsgroup-headers
5643            (setq gnus-newsgroup-begin 
5644                  (header-number (car gnus-newsgroup-headers)))
5645            (setq gnus-newsgroup-end
5646                  (header-number (gnus-last-element gnus-newsgroup-headers))))
5647       (setq gnus-reffed-article-number -1)
5648       ;; GROUP is successfully selected.
5649       (or gnus-newsgroup-headers t)))))
5650
5651 (defun gnus-articles-to-read (group read-all)
5652   ;; Find out what articles the user wants to read.
5653   (let* ((articles
5654           ;; Select all articles if `read-all' is non-nil, or if all the
5655           ;; unread articles are dormant articles.
5656           (if (or read-all
5657                   (= (length gnus-newsgroup-unreads) 
5658                      (length gnus-newsgroup-dormant)))
5659               (gnus-uncompress-range 
5660                (gnus-gethash group gnus-active-hashtb))
5661             gnus-newsgroup-unreads))
5662          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5663          (scored (length scored-list))
5664          (number (length articles))
5665          (marked (+ (length gnus-newsgroup-marked)
5666                     (length gnus-newsgroup-dormant)))
5667          (select
5668           (condition-case ()
5669               (cond ((and (or (<= scored marked)
5670                               (= scored number))
5671                           (numberp gnus-large-newsgroup)
5672                           (> number gnus-large-newsgroup))
5673                      (let ((input
5674                             (read-string
5675                              (format
5676                               "How many articles from %s (default %d): "
5677                               gnus-newsgroup-name number))))
5678                        (if (string-equal input "")
5679                            number input)))
5680                     ((and (> scored marked) (< scored number))
5681                      (let ((input
5682                             (read-string
5683                              (format 
5684                               "%s %s (%d scored, %d total): "
5685                               "How many articles from"
5686                               group scored number))))
5687                        (if (string-equal input "")
5688                            number input)))
5689                     (t number))
5690             (quit nil)))
5691          total-articles)
5692     (setq select (if (stringp select) (string-to-number select) select))
5693     (if (or (null select) (zerop select))
5694         select
5695       (if (and (not (zerop scored)) (<= (abs select) scored))
5696           (progn
5697             (setq articles (sort scored-list '<))
5698             (setq number (length articles)))
5699         (setq articles (copy-sequence articles)))
5700
5701       (setq total-articles articles)
5702       
5703       (if (< (abs select) number)
5704           (if (< select 0) 
5705               ;; Select the N oldest articles.
5706               (setcdr (nthcdr (1- (abs select)) articles) nil)
5707             ;; Select the N most recent articles.
5708             (setq articles (nthcdr (- number select) articles))))
5709       (setq gnus-newsgroup-unselected
5710             (gnus-sorted-intersection
5711              gnus-newsgroup-unreads
5712              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
5713       articles)))
5714
5715 (defun gnus-killed-articles (killed articles)
5716   (let (out)
5717     (while articles
5718       (if (inline (gnus-member-of-range (car articles) killed))
5719           (setq out (cons (car articles) out)))
5720       (setq articles (cdr articles)))
5721     out))
5722
5723 (defun gnus-adjust-marked-articles (info &optional active)
5724   "Remove all marked articles that are no longer legal."
5725   (let ((marked-lists (nth 3 info))
5726         (active (or active (gnus-gethash (car info) gnus-active-hashtb)))
5727         marked m prev)
5728     ;; There are many types of marked articles.
5729     (while marked-lists
5730       (setq m (cdr (setq prev (car marked-lists))))
5731       (cond ((or (eq 'tick (car prev)) (eq 'dormant (car prev)))
5732              ;; Make sure that all ticked articles are a subset of the
5733              ;; unread/unselected articles.
5734              (while m
5735                (if (or (memq (car m) gnus-newsgroup-unreads)
5736                        (memq (car m) gnus-newsgroup-unselected))
5737                    (setq prev m)
5738                  (setcdr prev (cdr m)))
5739                (setq m (cdr m))))
5740             ((eq 'score (car prev))
5741              ;; Scored articles should be a subset of
5742              ;; unread/unselected articles. 
5743              (while m
5744                (if (or (memq (car (car m)) gnus-newsgroup-unreads)
5745                        (memq (car (car m)) gnus-newsgroup-unreads))
5746                    (setq prev m)
5747                  (setcdr prev (cdr m)))
5748                (setq m (cdr m))))
5749             ((eq 'bookmark (car prev))
5750              ;; Bookmarks should be a subset of active articles.
5751              (while m
5752                (if (< (car (car m)) (car active))
5753                    (setcdr prev (cdr m))
5754                  (setq prev m))
5755                (setq m (cdr m))))
5756             ((eq 'killed (car prev))
5757              ;; Articles that have been through the kill process are
5758              ;; to be a subset of active articles.
5759              (while (and m (< (or (and (numberp (car m)) (car m))
5760                                   (cdr (car m)))
5761                               (car active)))
5762                (setcdr prev (cdr m))
5763                (setq m (cdr m)))
5764              (if (and m (< (or (and (numberp (car m)) (car m))
5765                                (car (car m)))
5766                            (car active))) 
5767                  (setcar (if (numberp (car m)) m (car m)) (car active))))
5768             ((or (eq 'reply (car prev)) (eq 'expire (car prev)))
5769              ;; The replied and expirable articles have to be articles
5770              ;; that are active. 
5771              (while m
5772                (if (< (car m) (car active))
5773                    (setcdr prev (cdr m))
5774                  (setq prev m))
5775                (setq m (cdr m)))))
5776       (setq marked-lists (cdr marked-lists)))
5777     ;; Remove all lists that are empty.
5778     (setq marked-lists (nth 3 info))
5779     (if marked-lists
5780         (progn
5781           (while (= 1 (length (car marked-lists)))
5782             (setq marked-lists (cdr marked-lists)))
5783           (setq m (cdr (setq prev marked-lists)))
5784           (while m
5785             (if (= 1 (length (car m)))
5786                 (setcdr prev (cdr m))
5787               (setq prev m))
5788             (setq m (cdr m)))
5789           (setcar (nthcdr 3 info) marked-lists)))
5790     ;; Finally, if there are no marked lists at all left, and if there
5791     ;; are no elements after the lists in the info list, we just chop
5792     ;; the info list off before the marked lists.
5793     (and (null marked-lists) 
5794          (not (nthcdr 4 info))
5795          (setcdr (nthcdr 2 info) nil)))
5796   info)
5797
5798 (defun gnus-set-marked-articles 
5799   (info ticked replied expirable killed dormant bookmark score) 
5800   "Enter the various lists of marked articles into the newsgroup info list."
5801   (let (newmarked)
5802     (and ticked (setq newmarked (cons (cons 'tick ticked) nil)))
5803     (and replied (setq newmarked (cons (cons 'reply replied) newmarked)))
5804     (and expirable (setq newmarked (cons (cons 'expire expirable) 
5805                                          newmarked)))
5806     (and killed (setq newmarked (cons (cons 'killed killed) newmarked)))
5807     (and dormant (setq newmarked (cons (cons 'dormant dormant) newmarked)))
5808     (and bookmark (setq newmarked (cons (cons 'bookmark bookmark) 
5809                                         newmarked)))
5810     (and score (setq newmarked (cons (cons 'score score) newmarked)))
5811     (if (nthcdr 3 info)
5812         (if newmarked
5813             (setcar (nthcdr 3 info) newmarked)
5814           (if (not (nthcdr 4 info))
5815               (setcdr (nthcdr 2 info) nil)
5816             (setcar (nthcdr 3 info) nil)))
5817       (if newmarked
5818           (setcdr (nthcdr 2 info) (cons newmarked nil))))))
5819
5820 (defun gnus-add-marked-articles (group type articles &optional info force)
5821   ;; Add ARTICLES of TYPE to the info of GROUP.
5822   ;; If INFO is non-nil, use that info. If FORCE is non-nil, don't
5823   ;; add, but replace marked articles of TYPE with ARTICLES.
5824   (let ((info (or info (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
5825         marked m)
5826     (or (not info)
5827         (and (not (setq marked (nthcdr 3 info)))
5828              (setcdr (nthcdr 2 info) (list (list (cons type articles)))))
5829         (and (not (setq m (assq type (car marked))))
5830              (setcar marked (cons (cons type articles) (car marked))))
5831         (if force
5832             (setcdr m articles)
5833           (nconc m articles)))))
5834          
5835 (defun gnus-set-mode-line (where)
5836   "This function sets the mode line of the article or summary buffers.
5837 If WHERE is `summary', the summary mode line format will be used."
5838   (if (memq where gnus-updated-mode-lines)
5839       (let (mode-string)
5840         (save-excursion
5841           (set-buffer gnus-summary-buffer)
5842           (let* ((mformat (if (eq where 'article) 
5843                               gnus-article-mode-line-format-spec
5844                             gnus-summary-mode-line-format-spec))
5845                  (group-name gnus-newsgroup-name)
5846                  (article-number (or gnus-current-article 0))
5847                  (unread (- (length gnus-newsgroup-unreads)
5848                             (length gnus-newsgroup-dormant)))
5849                  (unread-and-unticked 
5850                   (- unread (length gnus-newsgroup-marked)))
5851                  (unselected (length gnus-newsgroup-unselected))
5852                  (unread-and-unselected
5853                   (cond ((and (zerop unread-and-unticked)
5854                               (zerop unselected)) "")
5855                         ((zerop unselected) 
5856                          (format "{%d more}" unread-and-unticked))
5857                         (t (format "{%d(+%d) more}"
5858                                    unread-and-unticked unselected))))
5859                  (subject
5860                   (if gnus-current-headers
5861                       (header-subject gnus-current-headers) ""))
5862                  (max-len (and gnus-mode-non-string-length
5863                                (- (frame-width) gnus-mode-non-string-length)))
5864                  header) ;; passed as argument to any user-format-funcs
5865             (setq mode-string (eval mformat))
5866             (or (numberp max-len)
5867                 (setq max-len (length mode-string)))
5868             (if (< max-len 4) (setq max-len 4))
5869             (if (> (length mode-string) max-len)
5870                 (setq mode-string 
5871                       (concat (substring mode-string 0 (- max-len 3))
5872                               "...")))
5873             (setq mode-string (format (format "%%-%ds" max-len)
5874                                       mode-string))))
5875         (setq mode-line-buffer-identification mode-string)
5876         (set-buffer-modified-p t))))
5877
5878 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5879   "Go through the HEADERS list and add all Xrefs to a hash table.
5880 The resulting hash table is returned, or nil if no Xrefs were found."
5881   (let* ((from-method (gnus-find-method-for-group from-newsgroup))
5882          (prefix (if (and 
5883                       (gnus-group-foreign-p from-newsgroup)
5884                       (not (memq 'virtual 
5885                                  (assoc (symbol-name (car from-method))
5886                                         gnus-valid-select-methods))))
5887                      (gnus-group-real-prefix from-newsgroup)))
5888          (xref-hashtb (make-vector 63 0))
5889          start group entry number xrefs header)
5890     (while headers
5891       (setq header (car headers))
5892       (if (and (setq xrefs (header-xref header))
5893                (not (memq (header-number header) unreads)))
5894           (progn
5895             (setq start 0)
5896             (while (string-match "\\([^ ]+\\):\\([0-9]+\\)" xrefs start)
5897               (setq start (match-end 0))
5898               (setq group (concat prefix (substring xrefs (match-beginning 1) 
5899                                                     (match-end 1))))
5900               (setq number 
5901                     (string-to-int (substring xrefs (match-beginning 2) 
5902                                               (match-end 2))))
5903               (if (setq entry (gnus-gethash group xref-hashtb))
5904                   (setcdr entry (cons number (cdr entry)))
5905                 (gnus-sethash group (cons number nil) xref-hashtb)))))
5906       (setq headers (cdr headers)))
5907     (if start xref-hashtb nil)))
5908
5909 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads expirable)
5910   "Look through all the headers and mark the Xrefs as read."
5911   (let ((virtual (memq 'virtual 
5912                        (assoc (symbol-name (car (gnus-find-method-for-group 
5913                                                  from-newsgroup)))
5914                               gnus-valid-select-methods)))
5915         name entry read info xref-hashtb idlist active num range exps method
5916         nth4)
5917     (save-excursion
5918       (set-buffer gnus-group-buffer)
5919       (if (setq xref-hashtb 
5920                 (gnus-create-xref-hashtb from-newsgroup headers unreads))
5921           (mapatoms 
5922            (lambda (group)
5923              (if (string= from-newsgroup (setq name (symbol-name group)))
5924                  ()
5925                (setq idlist (symbol-value group))
5926                ;; Dead groups are not updated.
5927                (if (and (prog1 
5928                             (setq entry (gnus-gethash name gnus-newsrc-hashtb)
5929                                   info (nth 2 entry))
5930                           (if (stringp (setq nth4 (nth 4 info)))
5931                               (setq nth4 (gnus-server-to-method nth4))))
5932                         ;; Only do the xrefs if the group has the same
5933                         ;; select method as the group we have just read.
5934                         (or (gnus-methods-equal-p 
5935                              nth4 (gnus-find-method-for-group from-newsgroup))
5936                             virtual
5937                             (equal nth4 
5938                                    (setq method (gnus-find-method-for-group 
5939                                                  from-newsgroup)))
5940                             (and (equal (car nth4) (car method))
5941                                  (equal (nth 1 nth4) (nth 1 method))))
5942                         gnus-use-cross-reference
5943                         (or (not (eq gnus-use-cross-reference t))
5944                             virtual
5945                             ;; Only do cross-references on subscribed
5946                             ;; groups, if that is what is wanted.  
5947                             (<= (nth 1 info) gnus-level-subscribed)))
5948                    (progn
5949                      (setq num 0)
5950                      ;; Set the new list of read articles in this group.
5951                      (setq active (gnus-gethash name gnus-active-hashtb))
5952                      ;; First peel off all illegal article numbers.
5953                      (if active
5954                          (let ((ids idlist)
5955                                (ticked (cdr (assq 'tick (nth 3 info))))
5956                                (dormant (cdr (assq 'dormant (nth 3 info))))
5957                                id)
5958                            (setq exps nil)
5959                            (while ids
5960                              (setq id (car ids))
5961                              (if (or (> id (cdr active))
5962                                      (< id (car active))
5963                                      (memq id ticked)
5964                                      (memq id dormant))
5965                                  (setq idlist (delq id idlist)))
5966                              (and (memq id expirable)
5967                                   (setq exps (cons id exps)))
5968                              (setq ids (cdr ids)))))
5969                      ;; Update expirable articles.
5970                      (gnus-add-marked-articles nil 'expirable exps info)
5971                      (and (null (nth 2 info))
5972                           (> (car active) 1)
5973                           (setcar (nthcdr 2 info) (cons 1 (1- (car active)))))
5974                      (setcar (nthcdr 2 info)
5975                              (setq range
5976                                    (gnus-add-to-range 
5977                                     (nth 2 info) 
5978                                     (setq idlist (sort idlist '<)))))
5979                      ;; Then we have to re-compute how many unread
5980                      ;; articles there are in this group.
5981                      (if active
5982                          (progn
5983                            (cond 
5984                             ((not range)
5985                              (setq num (- (1+ (cdr active)) (car active))))
5986                             ((not (listp (cdr range)))
5987                              (setq num (- (cdr active) (- (1+ (cdr range)) 
5988                                                           (car range)))))
5989                             (t
5990                              (while range
5991                                (if (numberp (car range))
5992                                    (setq num (1+ num))
5993                                  (setq num (+ num (- (1+ (cdr (car range)))
5994                                                      (car (car range))))))
5995                                (setq range (cdr range)))
5996                              (setq num (- (cdr active) num))))
5997                            ;; Update the number of unread articles.
5998                            (setcar 
5999                             entry 
6000                             (max 0 (- num 
6001                                       (length (cdr (assq 'tick (nth 3 info))))
6002                                       (length 
6003                                        (cdr (assq 'dormant (nth 3 info)))))))
6004                            ;; Update the group buffer.
6005                            (gnus-group-update-group name t)))))))
6006            xref-hashtb)))))
6007
6008 (defun gnus-methods-equal-p (m1 m2)
6009   (let ((m1 (or m1 gnus-select-method))
6010         (m2 (or m2 gnus-select-method)))
6011     (or (equal m1 m2)
6012         (and (eq (car m1) (car m2))
6013              (or (not (memq 'address (assoc (symbol-name (car m1))
6014                                             gnus-valid-select-methods)))
6015                  (equal (nth 1 m1) (nth 1 m2)))))))
6016
6017 (defsubst gnus-header-value ()
6018   (buffer-substring (match-end 0) (gnus-point-at-eol)))
6019
6020 (defun gnus-get-newsgroup-headers ()
6021   (setq gnus-article-internal-prepare-hook nil)
6022   (let ((cur nntp-server-buffer)
6023         (dependencies gnus-newsgroup-dependencies)
6024         (none-id 0)
6025         headers char article id dep end)
6026     (save-excursion
6027       (set-buffer nntp-server-buffer)
6028       (goto-char 1)
6029       ;; Search to the beginning of the next header. Error messages
6030       ;; do not begin with 2 or 3.
6031       (while (re-search-forward "^[23][0-9]+ " nil t)
6032         (let ((header (make-vector 9 nil))
6033               (c (following-char))
6034               (case-fold-search t)
6035               (p (point))
6036               from subject in-reply-to references ref)
6037           (setq id nil
6038                 ref nil
6039                 references nil
6040                 subject nil
6041                 from nil)
6042           (header-set-number header (setq article (read cur)))
6043           ;; This implementation of this function, with nine
6044           ;; search-forwards instead of the one re-search-forward and
6045           ;; a case (which basically was the old function) is actually
6046           ;; about twice as fast, even though it looks messier. You
6047           ;; can't have everything, I guess. Speed and elegance
6048           ;; doesn't always come hand in hand.
6049           (save-restriction
6050             (narrow-to-region (point) (save-excursion 
6051                                         (search-forward "\n.\n" nil t)))
6052             (if (search-forward "\nfrom: " nil t)
6053                 (header-set-from header (gnus-header-value))
6054               (header-set-from header "(nobody)"))
6055             (goto-char p)
6056             (if (search-forward "\nsubject: " nil t)
6057                 (header-set-subject header (gnus-header-value))
6058               (header-set-subject header "(none)"))
6059             (goto-char p)
6060             (and (search-forward "\nxref: " nil t)
6061                  (header-set-xref header (gnus-header-value)))
6062             (goto-char p)
6063             (or (numberp (and (search-forward "\nlines: " nil t)
6064                               (header-set-lines header (read cur))))
6065                 (header-set-lines header 0))
6066             (goto-char p)
6067             (and (search-forward "\ndate: " nil t)
6068                  (header-set-date header (gnus-header-value)))
6069             (goto-char p)
6070             (if (search-forward "\nmessage-id: " nil t)
6071                 (header-set-id header (setq id (gnus-header-value)))
6072               ;; If there was no message-id, we just fake one to make
6073               ;; subsequent routines simpler.
6074               (header-set-id 
6075                header 
6076                (setq id (concat "none+" (int-to-string 
6077                                          (setq none-id (1+ none-id)))))))
6078             (goto-char p)
6079             (if (search-forward "\nreferences: " nil t)
6080                 (progn
6081                   (header-set-references header (gnus-header-value))
6082                   (setq end (match-end 0))
6083                   (save-excursion
6084                     (setq ref 
6085                           (downcase
6086                            (buffer-substring
6087                             (progn 
6088                               (end-of-line)
6089                               (search-backward ">" end t)
6090                               (1+ (point)))
6091                             (progn
6092                               (search-backward "<" end t)
6093                               (point)))))))
6094               ;; Get the references from the in-reply-to header if there
6095               ;; ware no references and the in-reply-to header looks
6096               ;; promising. 
6097               (if (and (search-forward "\nin-reply-to: " nil t)
6098                        (setq in-reply-to (gnus-header-value))
6099                        (string-match "<[^>]+>" in-reply-to))
6100                   (progn
6101                     (header-set-references 
6102                      header 
6103                      (setq ref (substring in-reply-to (match-beginning 0)
6104                                           (match-end 0))))
6105                     (setq ref (downcase ref)))
6106                 (setq ref "none")))
6107             ;; We do some threading while we read the headers. The
6108             ;; message-id and the last reference are both entered into
6109             ;; the same hash table. Some tippy-toeing around has to be
6110             ;; done in case an article has arrived before the article
6111             ;; which it refers to.
6112             (if (boundp (setq dep (intern (downcase id) dependencies)))
6113                 (if (car (symbol-value dep))
6114                     ;; An article with this Message-ID has already
6115                     ;; been seen, so we ignore this one, except we add
6116                     ;; any additional Xrefs (in case the two articles
6117                     ;; came from different servers.
6118                     (progn
6119                       (header-set-xref 
6120                        (car (symbol-value dep))
6121                        (concat (or (header-xref (car (symbol-value dep))) "")
6122                                (or (header-xref header) "")))
6123                       (setq header nil))
6124                   (setcar (symbol-value dep) header))
6125               (set dep (list header)))
6126             (if header
6127                 (progn
6128                   (if (boundp (setq dep (intern ref dependencies)))
6129                       (setcdr (symbol-value dep) 
6130                               (cons header (cdr (symbol-value dep))))
6131                     (set dep (list nil header)))
6132                   (setq headers (cons header headers))))
6133             (goto-char (point-max))))))
6134     (nreverse headers)))
6135
6136 ;; The following macros and functions were written by Felix Lee
6137 ;; <flee@cse.psu.edu>. 
6138
6139 ;; This is almost 4x faster than (string-to-int (buffer-substring ... ))
6140 ;; primarily because of garbage collection.  -jwz
6141 (defmacro gnus-read-integer (&optional point move-p)
6142   (` ((, (if move-p 'progn 'save-excursion))
6143       (,@ (if point (list (list 'goto-char point))))
6144       (if (and (<= (following-char) ?9)
6145                (>= (following-char) ?0))
6146           (read (current-buffer))
6147         0))))
6148
6149 (defmacro gnus-nov-skip-field ()
6150   '(search-forward "\t" eol 'move))
6151
6152 (defmacro gnus-nov-field ()
6153   '(buffer-substring (point) (if (gnus-nov-skip-field) (1- (point)) eol)))
6154
6155 ;; Goes through the xover lines and returns a list of vectors
6156 (defun gnus-get-newsgroup-headers-xover (sequence)
6157   "Parse the news overview data in the server buffer, and return a
6158 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
6159   ;; Get the Xref when the users reads the articles since most/some
6160   ;; NNTP servers do not include Xrefs when using XOVER.
6161   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
6162   (let ((cur nntp-server-buffer)
6163         (dependencies gnus-newsgroup-dependencies)
6164         (none 0)
6165         number headers header)
6166     (save-excursion
6167       (set-buffer nntp-server-buffer)
6168       (goto-char (point-min))
6169       (while (and sequence (not (eobp)))
6170         (setq number (read cur))
6171         (while (and sequence (< (car sequence) number))
6172           (setq sequence (cdr sequence)))
6173         (and sequence 
6174              (eq number (car sequence))
6175              (progn
6176                (setq sequence (cdr sequence))
6177                (if (setq header 
6178                          (inline (gnus-nov-parse-line number dependencies)))
6179                    (setq headers (cons header headers)))))
6180         (forward-line 1))
6181       (setq headers (nreverse headers)))
6182     headers))
6183
6184 (defun gnus-nov-parse-line (number dependencies)
6185   "Point has to be after the number on the beginning of the line."
6186   (let ((none 0)
6187         header (eol (gnus-point-at-eol)) ref id dep)
6188     (forward-char)
6189     ;; overview: [num subject from date id refs chars lines misc]
6190     (save-restriction
6191       (narrow-to-region (point) eol)
6192       (condition-case nil
6193           (setq header
6194                 (vector 
6195                  number                 ; number
6196                  (gnus-nov-field)       ; subject
6197                  (gnus-nov-field)       ; from
6198                  (gnus-nov-field)       ; date
6199                  (setq id (or (gnus-nov-field)
6200                               (concat "none+"
6201                                      (int-to-string 
6202                                       (setq none (1+ none)))))) ; id
6203                  (progn
6204                    (save-excursion
6205                      (let ((beg (point)))
6206                        (search-forward "\t" eol)
6207                        (if (search-backward ">" beg t)
6208                            (setq ref 
6209                                  (downcase 
6210                                   (buffer-substring 
6211                                    (1+ (point))
6212                                    (progn
6213                                      (search-backward "<" beg t)
6214                                      (point)))))
6215                          (setq ref nil))))
6216                    (gnus-nov-field))    ; refs
6217                  (read (current-buffer)) ; chars
6218                  (read (current-buffer)) ; lines
6219                  (if (/= (following-char) ?\t)
6220                      nil
6221                    (forward-char 1)
6222                    (gnus-nov-field))    ; misc
6223                  ))
6224         (error (progn 
6225                  (setq header nil)
6226                  (goto-char eol)))))
6227     ;; We build the thread tree.
6228     (and header
6229          (if (boundp (setq dep (intern (downcase id) dependencies)))
6230              (if (car (symbol-value dep))
6231                  ;; An article with this Message-ID has already been seen,
6232                  ;; so we ignore this one, except we add any additional
6233                  ;; Xrefs (in case the two articles came from different
6234                  ;; servers.
6235                  (progn
6236                    (header-set-xref 
6237                     (car (symbol-value dep))
6238                     (concat (or (header-xref (car (symbol-value dep))) "")
6239                             (or (header-xref header) "")))
6240                    (setq header nil))
6241                (setcar (symbol-value dep) header))
6242            (set dep (list header))))
6243     (if header
6244         (progn
6245           (if (boundp (setq dep (intern (or ref "none") 
6246                                         dependencies)))
6247               (setcdr (symbol-value dep) 
6248                       (cons header (cdr (symbol-value dep))))
6249             (set dep (list nil header)))))
6250     header))
6251
6252 (defun gnus-article-get-xrefs ()
6253   "Fill in the Xref value in `gnus-current-headers', if necessary.
6254 This is meant to be called in `gnus-article-internal-prepare-hook'."
6255   (or (not gnus-use-cross-reference)
6256       (header-xref gnus-current-headers)
6257       (let ((case-fold-search t)
6258             xref)
6259         (save-restriction
6260           (gnus-narrow-to-headers)
6261           (goto-char (point-min))
6262           (if (or (and (eq (downcase (following-char)) ?x)
6263                        (looking-at "Xref:"))
6264                   (search-forward "\nXref:" nil t))
6265               (progn
6266                 (goto-char (1+ (match-end 0)))
6267                 (setq xref (buffer-substring (point) 
6268                                              (progn (end-of-line) (point))))
6269                 (save-excursion
6270                   (set-buffer gnus-summary-buffer)
6271                   (header-set-xref gnus-current-headers xref))))))))
6272
6273 (defalias 'gnus-find-header-by-number 'gnus-get-header-by-number)
6274 (make-obsolete 'gnus-find-header-by-number 'gnus-get-header-by-number)
6275
6276 ;; Return a header specified by a NUMBER.
6277 (defun gnus-get-header-by-number (number)
6278   (save-excursion
6279     (set-buffer gnus-summary-buffer)
6280     (or gnus-newsgroup-headers-hashtb-by-number
6281         (gnus-make-headers-hashtable-by-number))
6282     (gnus-gethash (int-to-string number)
6283                   gnus-newsgroup-headers-hashtb-by-number)))
6284
6285 (defun gnus-make-headers-hashtable-by-number ()
6286   "Make hashtable for the variable gnus-newsgroup-headers by number."
6287   (save-excursion
6288     (set-buffer gnus-summary-buffer)
6289     (let ((headers gnus-newsgroup-headers)
6290           header)
6291       (setq gnus-newsgroup-headers-hashtb-by-number
6292             (gnus-make-hashtable (length headers)))
6293       (while headers
6294         (setq header (car headers))
6295         (gnus-sethash (int-to-string (header-number header))
6296                       header gnus-newsgroup-headers-hashtb-by-number)
6297         (setq headers (cdr headers))))))
6298
6299 (defun gnus-more-header-backward ()
6300   "Find new header backward."
6301   (let ((first (car (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
6302         (artnum gnus-newsgroup-begin)
6303         (header nil))
6304     (while (and (not header)
6305                 (> artnum first))
6306       (setq artnum (1- artnum))
6307       (setq header (gnus-read-header artnum)))
6308     header))
6309
6310 (defun gnus-more-header-forward (&optional backward)
6311   "Find new header forward.
6312 If BACKWARD, find new header backward instead."
6313   (if backward
6314       (gnus-more-header-backward)
6315     (let ((last (cdr (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
6316           (artnum gnus-newsgroup-end)
6317           (header nil))
6318       (while (and (not header)
6319                   (< artnum last))
6320         (setq artnum (1+ artnum))
6321         (setq header (gnus-read-header artnum)))
6322       header)))
6323
6324 (defun gnus-extend-newsgroup (header &optional backward)
6325   "Extend newsgroup selection with HEADER.
6326 Optional argument BACKWARD means extend toward backward."
6327   (if header
6328       (let ((artnum (header-number header)))
6329         (setq gnus-newsgroup-headers
6330               (if backward
6331                   (cons header gnus-newsgroup-headers)
6332                 (nconc gnus-newsgroup-headers (list header))))
6333         (setq gnus-newsgroup-unselected
6334               (delq artnum gnus-newsgroup-unselected))
6335         (setq gnus-newsgroup-begin (min gnus-newsgroup-begin artnum))
6336         (setq gnus-newsgroup-end (max gnus-newsgroup-end artnum)))))
6337
6338 (defun gnus-summary-work-articles (n)
6339   "Return a list of articles to be worked upon. The prefix argument,
6340 the list of process marked articles, and the current article will be
6341 taken into consideration."
6342   (let (articles)
6343     (if (and n (numberp n))
6344         (let ((backward (< n 0))
6345               (n (abs n)))
6346           (save-excursion
6347             (while (and (> n 0)
6348                         (setq articles (cons (gnus-summary-article-number) 
6349                                              articles))
6350                         (gnus-summary-search-forward nil nil backward))
6351               (setq n (1- n))))
6352           (sort articles (function <)))
6353       (or (reverse gnus-newsgroup-processable)
6354           (list (gnus-summary-article-number))))))
6355
6356 (defun gnus-summary-search-group (&optional backward use-level)
6357   "Search for next unread newsgroup.
6358 If optional argument BACKWARD is non-nil, search backward instead."
6359   (save-excursion
6360     (set-buffer gnus-group-buffer)
6361     (save-excursion
6362       ;; We don't want to alter current point of group mode buffer.
6363       (if (gnus-group-search-forward 
6364            backward nil
6365            (if use-level (gnus-group-group-level) nil))
6366           (gnus-group-group-name)))))
6367
6368 (defun gnus-summary-best-group (&optional exclude-group)
6369   "Find the name of the best unread group.
6370 If EXCLUDE-GROUP, do not go to this group."
6371   (save-excursion
6372     (set-buffer gnus-group-buffer)
6373     (save-excursion
6374       (gnus-group-best-unread-group exclude-group))))
6375
6376 (defun gnus-summary-search-subject (&optional backward unread subject)
6377   "Search for article forward.
6378 If BACKWARD is non-nil, search backward.
6379 If UNREAD is non-nil, only unread articles are selected.
6380 If SUBJECT is non-nil, the article which has the same subject will be
6381 searched for." 
6382   (let ((func (if backward 'previous-single-property-change
6383                 'next-single-property-change))
6384         (beg (point))
6385         (did t)
6386         pos)
6387     (beginning-of-line)
6388     (and gnus-summary-check-current unread
6389          (eq (nth 1 (get-text-property (point) 'gnus)) gnus-unread-mark)
6390          (setq did nil))
6391     (if (not did)
6392         ()
6393       (forward-char (if backward (if (bobp) 0 -1) (if (eobp) 0 1)))
6394       (while
6395           (and 
6396            (setq pos (funcall func (point) 'gnus))
6397            (goto-char (if backward (1- pos) pos))
6398            (setq did
6399                  (not (and
6400                        (or (not unread)
6401                            (eq (nth 1 (get-text-property (point) 'gnus))
6402                                gnus-unread-mark))
6403                        (or (not subject)
6404                            (equal (gnus-simplify-subject-re subject)
6405                                   (gnus-simplify-subject-re
6406                                    (gnus-summary-subject-string)))))))
6407            (if backward (if (bobp) nil (forward-char -1) t)
6408              (if (eobp) nil (forward-char 1) t)))))
6409     (if did
6410         (progn (goto-char beg) nil)
6411       (prog1
6412           (car (get-text-property (point) 'gnus))
6413         (gnus-summary-position-cursor)))))
6414
6415 (defun gnus-summary-search-forward (&optional unread subject backward)
6416   "Search for article forward.
6417 If UNREAD is non-nil, only unread articles are selected.
6418 If SUBJECT is non-nil, the article which has the same subject will be
6419 searched for. 
6420 If BACKWARD is non-nil, the search will be performed backwards instead."
6421   (gnus-summary-search-subject backward unread subject))
6422
6423 (defun gnus-summary-search-backward (&optional unread subject)
6424   "Search for article backward.
6425 If 1st optional argument UNREAD is non-nil, only unread article is selected.
6426 If 2nd optional argument SUBJECT is non-nil, the article which has
6427 the same subject will be searched for."
6428   (gnus-summary-search-forward unread subject t))
6429
6430 (defun gnus-summary-article-number (&optional number-or-nil)
6431   "The article number of the article on the current line.
6432 If there isn's an article number here, then we return the current
6433 article number."
6434   (let* ((p (point))
6435          (number (car (get-text-property 
6436                        (progn (beginning-of-line) 
6437                               (prog1 (point) (goto-char p)))
6438                        'gnus))))
6439     (if number-or-nil number (or number gnus-current-article))))
6440
6441 (defun gnus-summary-thread-level ()
6442   "The thread level of the article on the current line."
6443   (or (nth 2 (get-text-property (gnus-point-at-bol) 'gnus))
6444       0))
6445
6446 (defun gnus-summary-pseudo-article ()
6447   "The thread level of the article on the current line."
6448   (get-text-property (gnus-point-at-bol) 'gnus-pseudo))
6449
6450 (defun gnus-summary-article-mark ()
6451   "The mark on the current line."
6452   (nth 1 (get-text-property (gnus-point-at-bol) 'gnus)))
6453
6454 (defun gnus-summary-subject-string ()
6455   "Return current subject string or nil if nothing."
6456   (let ((article (gnus-summary-article-number))
6457         header)
6458     (and article 
6459          (setq header (gnus-get-header-by-number article))
6460          (header-subject header))))
6461
6462 (defalias 'gnus-summary-score 'gnus-summary-article-score)
6463 (make-obsolete 'gnus-summary-score 'gnus-summary-article-score)
6464 (defun gnus-summary-article-score ()
6465   "Return current article score."
6466   (or (cdr (assq (gnus-summary-article-number) gnus-newsgroup-scored))
6467       gnus-summary-default-score 0))
6468
6469 ;; Written by Sudish Joseph <joseph@cis.ohio-state.edu>.
6470 (defun gnus-summary-recenter-new ()
6471   "Center point in the summary window.
6472 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6473 displayed, no centering will be performed." 
6474   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6475   ;; Recenter only when requested. Suggested by popovich@park.cs.columbia.edu.
6476   (sit-for 0)
6477   (and gnus-auto-center-summary
6478        (get-buffer-window gnus-article-buffer)
6479        (let* ((height (window-height))
6480               (w-end (window-end))
6481               (top (cond ((< height 4) 0)
6482                          ((< height 6) 1)
6483                          (t 2))))
6484          (and (= w-end (point-max))
6485              ;; adjust for blank lines at the bottom of the window
6486              (setq top (+ (count-lines (window-start) (point))
6487                           (- height (count-lines (window-start) w-end) 2))))
6488          (recenter top))))
6489
6490 (defun gnus-summary-recenter ()
6491   "Center point in the summary window.
6492 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6493 displayed, no centering will be performed." 
6494   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6495   ;; Recenter only when requested. Suggested by popovich@park.cs.columbia.edu.
6496   (let ((top (cond ((< (window-height) 4) 0)
6497                    ((< (window-height) 6) 1)
6498                    (t 2))))
6499     (and 
6500      ;; The user has to want it,
6501      gnus-auto-center-summary 
6502      ;; the article buffer must be displayed,
6503      (get-buffer-window gnus-article-buffer)
6504      ;; there must be lines left to scroll forward,
6505      (zerop (save-excursion (forward-line (- (window-height) 1 top))))
6506      ;; so we recenter.
6507      (set-window-start 
6508       (get-buffer-window (current-buffer)) 
6509       (save-excursion (forward-line (- top)) (point))))))
6510
6511 ;; Function written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
6512 (defun gnus-short-group-name (newsgroup)
6513   "Convert a long group name to an initialized form.
6514 The last part of the name is left intact: \"rec.arts.anime\" becomes
6515 \"r.a.anime\"."
6516   (let ((name ""))
6517     (while newsgroup
6518       (if (string-match "\\." newsgroup)
6519           (progn
6520             (setq name (concat name (substring newsgroup 0 1))
6521                   newsgroup (substring newsgroup (match-end 0))
6522                   name (concat name ".")))
6523           (setq name (concat name newsgroup)
6524                 newsgroup nil)))
6525     name))
6526
6527
6528 (defun gnus-summary-jump-to-group (newsgroup)
6529   "Move point to NEWSGROUP in group mode buffer."
6530   ;; Keep update point of group mode buffer if visible.
6531   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6532       (save-window-excursion
6533         ;; Take care of tree window mode.
6534         (if (get-buffer-window gnus-group-buffer)
6535             (pop-to-buffer gnus-group-buffer))
6536         (gnus-group-jump-to-group newsgroup))
6537     (save-excursion
6538       ;; Take care of tree window mode.
6539       (if (get-buffer-window gnus-group-buffer)
6540           (pop-to-buffer gnus-group-buffer)
6541         (set-buffer gnus-group-buffer))
6542       (gnus-group-jump-to-group newsgroup))))
6543
6544 ;; This function returns a list of article numbers based on the
6545 ;; difference between the ranges of read articles in this group and
6546 ;; the range of active articles.
6547 (defun gnus-list-of-unread-articles (group)
6548   (let* ((read (nth 2 (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
6549          (active (gnus-gethash group gnus-active-hashtb))
6550          (last (cdr active))
6551          unread first nlast unread)
6552     ;; If none are read, then all are unread. 
6553     (if (not read)
6554         (setq first (car active))
6555       ;; If the range of read articles is a single range, then the
6556       ;; first unread article is the article after the last read
6557       ;; article. Sounds logical, doesn't it?
6558       (if (not (listp (cdr read)))
6559           (setq first (1+ (cdr read)))
6560         ;; `read' is a list of ranges.
6561         (if (/= (setq nlast (or (and (numberp (car read)) (car read)) 
6562                                 (car (car read)))) 1)
6563             (setq first 1))
6564         (while read
6565           (if first 
6566               (while (< first nlast)
6567                 (setq unread (cons first unread))
6568                 (setq first (1+ first))))
6569           (setq first (1+ (if (atom (car read)) (car read) (cdr (car read)))))
6570           (setq nlast (if (atom (car (cdr read))) 
6571                           (car (cdr read))
6572                         (car (car (cdr read)))))
6573           (setq read (cdr read)))))
6574     ;; And add the last unread articles.
6575     (while (<= first last)
6576       (setq unread (cons first unread))
6577       (setq first (1+ first)))
6578     ;; Return the list of unread articles.
6579     (nreverse unread)))
6580
6581
6582 ;; Various summary commands
6583
6584 (defun gnus-summary-universal-argument ()
6585   "Perform any operation on all articles marked with the process mark."
6586   (interactive)
6587   (gnus-set-global-variables)
6588   (let ((articles (reverse gnus-newsgroup-processable))
6589         key func)
6590     (or articles (error "No articles marked"))
6591     (or (setq func (key-binding (read-key-sequence "C-c C-u")))
6592         (error "Undefined key"))
6593     (while articles
6594       (gnus-summary-goto-subject (car articles))
6595       (command-execute func)
6596       (gnus-summary-remove-process-mark (car articles))
6597       (setq articles (cdr articles)))))
6598
6599 (defun gnus-summary-toggle-truncation (arg)
6600   "Toggle truncation of summary lines.
6601 With arg, turn line truncation on iff arg is positive."
6602   (interactive "P")
6603   (setq truncate-lines
6604         (if (null arg) (not truncate-lines)
6605           (> (prefix-numeric-value arg) 0)))
6606   (redraw-display))
6607
6608 (defun gnus-summary-reselect-current-group (all)
6609   "Once exit and then reselect the current newsgroup.
6610 The prefix argument ALL means to select all articles."
6611   (interactive "P")
6612   (gnus-set-global-variables)
6613   (let ((current-subject (gnus-summary-article-number)))
6614     (gnus-summary-exit t)
6615     ;; We have to adjust the point of group mode buffer because the
6616     ;; current point was moved to the next unread newsgroup by
6617     ;; exiting.
6618     (gnus-summary-jump-to-group gnus-newsgroup-name)
6619     (gnus-group-read-group all t)
6620     (gnus-summary-goto-subject current-subject)))
6621
6622 (defun gnus-summary-rescan-group (all)
6623   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6624   (interactive "P")
6625   (gnus-set-global-variables)
6626   ;; Fix by Ilja Weis <kult@uni-paderborn.de>.
6627   (let ((group gnus-newsgroup-name))
6628     (gnus-summary-exit t)
6629     (gnus-summary-jump-to-group group)
6630     (save-excursion
6631       (set-buffer gnus-group-buffer)
6632       (gnus-group-get-new-news-this-group 1))
6633     (gnus-summary-jump-to-group group)
6634     (gnus-group-read-group all)))
6635
6636 (defun gnus-summary-exit (&optional temporary)
6637   "Exit reading current newsgroup, and then return to group selection mode.
6638 gnus-exit-group-hook is called with no arguments if that value is non-nil."
6639   (interactive)
6640   (gnus-set-global-variables)
6641   (gnus-kill-save-kill-buffer)
6642   (let* ((group gnus-newsgroup-name)
6643          (quit-buffer (cdr (assoc 'quit-buffer (gnus-find-method-for-group
6644                                                 gnus-newsgroup-name))))
6645          (mode major-mode)
6646          (method (car (gnus-find-method-for-group group)))
6647          (buf (current-buffer)))
6648     (if gnus-newsgroup-kill-headers
6649         (setq gnus-newsgroup-killed
6650               (gnus-compress-sequence
6651                (nconc
6652                 (gnus-set-sorted-intersection
6653                  (gnus-uncompress-range gnus-newsgroup-killed)
6654                  (setq gnus-newsgroup-unselected
6655                        (sort gnus-newsgroup-unselected '<)))
6656                 (setq gnus-newsgroup-unreads
6657                       (sort gnus-newsgroup-unreads '<))) t)))
6658     (or (listp (cdr gnus-newsgroup-killed))
6659         (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6660     (let ((updated nil)
6661           (headers gnus-newsgroup-headers))
6662       (gnus-close-group group)
6663       (run-hooks 'gnus-exit-group-hook)
6664       (gnus-update-read-articles 
6665        group gnus-newsgroup-unreads gnus-newsgroup-unselected 
6666        gnus-newsgroup-marked
6667        t gnus-newsgroup-replied gnus-newsgroup-expirable
6668        gnus-newsgroup-killed gnus-newsgroup-dormant
6669        gnus-newsgroup-bookmarks 
6670        (and gnus-save-score gnus-newsgroup-scored))
6671       (and gnus-use-cross-reference
6672            (gnus-mark-xrefs-as-read 
6673             group headers gnus-newsgroup-unreads gnus-newsgroup-expirable))
6674       ;; Do adaptive scoring, and possibly save score files.
6675       (and gnus-use-adaptive-scoring
6676            (gnus-score-adaptive))
6677       (and (fboundp 'gnus-score-save)
6678            (funcall 'gnus-score-save))
6679       ;; Do not switch windows but change the buffer to work.
6680       (set-buffer gnus-group-buffer)
6681       (or (eq 'nndigest method)
6682           (gnus-group-update-group group)))
6683     ;; Make sure where I was, and go to next newsgroup.
6684     (if (eq method 'nndigest)
6685         ()
6686       (gnus-group-jump-to-group group)
6687       (gnus-group-next-unread-group 1))
6688     (if temporary
6689         ;; If exiting temporary, caller should adjust group mode
6690         ;; buffer point by itself.
6691         nil                             ;Nothing to do.
6692       ;; We set all buffer-local variables to nil. It is unclear why
6693       ;; this is needed, but if we don't, buffer-local variables are
6694       ;; not garbage-collected, it seems. This would the lead to en
6695       ;; ever-growing Emacs.
6696       (set-buffer buf)
6697       (gnus-summary-clear-local-variables)
6698       ;; We clear the global counterparts of the buffer-local
6699       ;; variables as well, just to be on the safe side.
6700       (set-buffer gnus-group-buffer)
6701       (gnus-summary-clear-local-variables)
6702       (gnus-configure-windows 'article)
6703       (gnus-configure-windows 'newsgroups)
6704       ;; Return to group mode buffer. 
6705       (if (eq mode 'gnus-summary-mode)
6706           (gnus-kill-buffer buf))
6707       (if (get-buffer gnus-article-buffer)
6708           (bury-buffer gnus-article-buffer))
6709       (setq gnus-current-select-method gnus-select-method)
6710       (pop-to-buffer gnus-group-buffer)
6711       (if (eq method 'nndigest)
6712           ()
6713         (gnus-group-jump-to-group group)
6714         (gnus-group-next-unread-group 1))
6715       (if (gnus-buffer-exists-p quit-buffer)
6716           (progn
6717             (switch-to-buffer quit-buffer)
6718             (gnus-set-global-variables)
6719             (gnus-configure-windows 'summary))))))
6720
6721 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6722 (defun gnus-summary-exit-no-update (&optional no-questions)
6723   "Quit reading current newsgroup without updating read article info."
6724   (interactive)
6725   (let* ((group gnus-newsgroup-name)
6726          (quit-buffer (cdr (assoc 'quit-buffer 
6727                                   (gnus-find-method-for-group group)))))
6728     (if (or no-questions
6729             gnus-expert-user
6730             (gnus-y-or-n-p "Do you really wanna quit reading this group? "))
6731         (progn
6732           (gnus-close-group group)
6733           (gnus-summary-clear-local-variables)
6734           (set-buffer gnus-group-buffer)
6735           (gnus-summary-clear-local-variables)
6736           ;; Return to group selection mode.
6737           (gnus-configure-windows 'newsgroups)
6738           (if (get-buffer gnus-summary-buffer)
6739               (kill-buffer gnus-summary-buffer))
6740           (if (get-buffer gnus-article-buffer)
6741               (bury-buffer gnus-article-buffer))
6742           (pop-to-buffer gnus-group-buffer)
6743           (if (equal (gnus-group-group-name) group)
6744               (gnus-group-next-unread-group 1))
6745           (if (gnus-buffer-exists-p quit-buffer)
6746               (progn
6747                 (switch-to-buffer quit-buffer)
6748                 (gnus-configure-windows 'summary)))))))
6749
6750 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6751 (defun gnus-summary-fetch-faq (group)
6752   "Fetch the FAQ for the current group."
6753   (interactive (list gnus-newsgroup-name))
6754   (gnus-configure-windows 'article)
6755   (pop-to-buffer gnus-article-buffer)
6756   (find-file (concat gnus-group-faq-directory group)))
6757
6758 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6759 (defun gnus-summary-describe-group (force)
6760   "Describe the current newsgroup."
6761   (interactive "P")
6762   (gnus-group-describe-group force gnus-newsgroup-name))
6763
6764 (defun gnus-summary-describe-briefly ()
6765   "Describe summary mode commands briefly."
6766   (interactive)
6767   (message
6768     (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")))
6769
6770 ;; Walking around group mode buffer from summary mode.
6771
6772 (defun gnus-summary-next-group (&optional no-article group backward)
6773   "Exit current newsgroup and then select next unread newsgroup.
6774 If prefix argument NO-ARTICLE is non-nil, no article is selected initially.
6775 If BACKWARD, go to previous group instead."
6776   (interactive "P")
6777   (gnus-set-global-variables)
6778   (let ((ingroup gnus-newsgroup-name)
6779         (sumbuf (current-buffer))
6780         num)
6781     (gnus-summary-exit t)               ;Update all information.
6782     (if (and group
6783              (or (and (numberp (setq num (car (gnus-gethash
6784                                                group gnus-newsrc-hashtb))))
6785                       (< num 1))
6786                  (null num)))
6787         (progn
6788           (gnus-group-jump-to-group group)
6789           (setq group nil))
6790       (gnus-group-jump-to-group ingroup))
6791     (gnus-summary-search-group backward)
6792     (let ((group (or group (gnus-summary-search-group backward)))
6793           (buf gnus-summary-buffer))
6794       (if (null group)
6795           (gnus-summary-exit-no-update t)
6796         (message "Selecting %s..." group)
6797         ;; We are now in group mode buffer.
6798         ;; Make sure group mode buffer point is on GROUP.
6799         (gnus-group-jump-to-group group)
6800         (if (not (eq gnus-auto-select-next 'quietly))
6801             (progn
6802               (gnus-summary-read-group group nil no-article buf)
6803               (and (string= gnus-newsgroup-name ingroup)
6804                    (bufferp sumbuf) (buffer-name sumbuf)
6805                    (progn
6806                      (set-buffer (setq gnus-summary-buffer sumbuf))
6807                      (gnus-summary-exit-no-update t))))
6808           (gnus-summary-read-group group nil no-article buf)
6809           (while (and (string= gnus-newsgroup-name ingroup)
6810                       (bufferp sumbuf) (buffer-name sumbuf))
6811             (set-buffer gnus-group-buffer)
6812             (gnus-group-next-unread-group 1)
6813             (recenter)
6814             (gnus-summary-read-group 
6815              (gnus-group-group-name) nil no-article buf)))))))
6816
6817
6818 (defun gnus-summary-prev-group (no-article)
6819   "Exit current newsgroup and then select previous unread newsgroup.
6820 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6821   (interactive "P")
6822   (gnus-summary-next-group no-article nil t))
6823
6824 ;; Walking around summary lines.
6825
6826 (defun gnus-summary-first-subject (unread)
6827   "Go to the first unread subject.
6828 If UNREAD is non-nil, go to the first unread article.
6829 Returns nil if there are no unread articles."
6830   (interactive "P")
6831   (let ((begin (point)))
6832     (goto-char (point-min))
6833     (if (not unread)
6834         t
6835       (while (and (not (eq (nth 1 (get-text-property (point) 'gnus))
6836                            gnus-unread-mark))
6837                   (zerop (forward-line 1))))
6838       (prog1
6839           (if (not (eobp))
6840               t
6841             ;; If there is no unread articles, stay where you are.
6842             (goto-char begin)
6843             (message "No more unread articles")
6844             nil)
6845         (gnus-summary-position-cursor)))))
6846
6847 (defun gnus-summary-next-subject (n &optional unread dont-display)
6848   "Go to next N'th summary line.
6849 If N is negative, go to the previous N'th subject line.
6850 If UNREAD is non-nil, only unread articles are selected.
6851 The difference between N and the actual number of steps taken is
6852 returned."
6853   (interactive "p")
6854   (let ((backward (< n 0))
6855         (n (abs n)))
6856     (while (and (> n 0)
6857                 (gnus-summary-search-forward unread nil backward))
6858       (setq n (1- n)))
6859     (if (/= 0 n) (message "No more%s articles" (if unread " unread" "")))
6860     (or dont-display
6861         (progn
6862           (gnus-summary-recenter)
6863           (gnus-summary-position-cursor)))
6864   n))
6865
6866 (defun gnus-summary-next-unread-subject (n)
6867   "Go to next N'th unread summary line."
6868   (interactive "p")
6869   (gnus-summary-next-subject n t))
6870
6871 (defun gnus-summary-prev-subject (n &optional unread)
6872   "Go to previous N'th summary line.
6873 If optional argument UNREAD is non-nil, only unread article is selected."
6874   (interactive "p")
6875   (gnus-summary-next-subject (- n) unread))
6876
6877 (defun gnus-summary-prev-unread-subject (n)
6878   "Go to previous N'th unread summary line."
6879   (interactive "p")
6880   (gnus-summary-next-subject (- n) t))
6881
6882 (defun gnus-summary-goto-subject (article)
6883   "Go the subject line of ARTICLE."
6884   (interactive
6885    (list
6886     (string-to-int
6887      (completing-read "Article number: "
6888                       (mapcar
6889                        (lambda (headers)
6890                          (list
6891                           (int-to-string (header-number headers))))
6892                        gnus-newsgroup-headers)
6893                       nil 'require-match))))
6894   (or article (error "No article number"))
6895   (let ((b (point)))
6896     (goto-char (point-min))
6897     (while (and (not (eq (car (get-text-property (point) 'gnus)) article))
6898                 (zerop (forward-line 1))))
6899     (prog1
6900         (if (not (eobp))
6901             article
6902           (goto-char b)
6903           nil)
6904       (gnus-summary-position-cursor))))
6905
6906 ;; Walking around summary lines with displaying articles.
6907
6908 (defun gnus-summary-expand-window ()
6909   "Expand summary window to show headers full window."
6910   (interactive)
6911   (gnus-set-global-variables)
6912   (gnus-configure-windows 'summary)
6913   (pop-to-buffer gnus-summary-buffer))
6914
6915 (defun gnus-summary-display-article (article &optional all-header)
6916   "Display ARTICLE in article buffer."
6917   (gnus-set-global-variables)
6918   (if (null article)
6919       nil
6920     (gnus-article-prepare article all-header)
6921     (if (eq (gnus-summary-article-mark) ?Z)
6922         (progn
6923           (forward-line 1)
6924           (gnus-summary-position-cursor)))
6925     (run-hooks 'gnus-select-article-hook)
6926     (gnus-summary-recenter)
6927 ;    (set-window-point (get-buffer-window (current-buffer)) (point-max))
6928 ;    (sit-for 0)
6929     (gnus-summary-goto-subject article)
6930     ;; Successfully display article.
6931     (gnus-summary-update-line)
6932     t))
6933
6934 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6935   "Select the current article.
6936 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6937 non-nil, the article will be re-fetched even if it already present in
6938 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6939 be displayed."
6940   (and (not pseudo) (gnus-summary-pseudo-article)
6941        (error "This is a pseudo-article."))
6942   (let ((article (or article (gnus-summary-article-number)))
6943         (all-headers (not (not all-headers)))) ;Must be T or NIL.
6944     (if (or (null gnus-current-article)
6945             (null gnus-article-current)
6946             (/= article (cdr gnus-article-current))
6947             (not (equal (car gnus-article-current) gnus-newsgroup-name))
6948             force)
6949         ;; The requested article is different from the current article.
6950         (progn
6951           (gnus-summary-display-article article all-headers)
6952           (gnus-configure-windows 'article)
6953           article)
6954       (if all-headers (gnus-article-show-all-headers))
6955       (gnus-configure-windows 'article)
6956       (pop-to-buffer gnus-summary-buffer)
6957       nil)))
6958
6959 (defun gnus-summary-set-current-mark (&optional current-mark)
6960   "Obsolete function."
6961   nil)
6962
6963 (defun gnus-summary-next-article (unread &optional subject backward)
6964   "Select the next article.
6965 If UNREAD, only unread articles are selected.
6966 If SUBJECT, only articles with SUBJECT are selected.
6967 If BACKWARD, the previous article is selected instead of the next."
6968   (interactive "P")
6969   (let ((opoint (point))
6970         (method (car (gnus-find-method-for-group gnus-newsgroup-name)))
6971         header)
6972     (cond
6973      ;; Is there such an article?
6974      ((gnus-summary-display-article 
6975        (gnus-summary-search-forward unread subject backward))
6976       (gnus-summary-position-cursor))
6977      ;; If not, we try the first unread, if that is wanted.
6978      ((and subject
6979            gnus-auto-select-same
6980            (gnus-summary-first-unread-article))
6981       (message "Wrapped"))
6982      ;; Try to get next/previous article not displayed in this group.
6983      ((and gnus-auto-extend-newsgroup
6984            (not unread) (not subject)
6985            (setq header (gnus-more-header-forward backward)))
6986       (gnus-extend-newsgroup header backward)
6987       (let ((buffer-read-only nil))
6988         (goto-char (if backward (point-min) (point-max)))
6989         (gnus-summary-prepare-threads (list header) 0))
6990       (gnus-summary-goto-article (if backward gnus-newsgroup-begin
6991                                    gnus-newsgroup-end)))
6992      ;; Go to next/previous group.
6993      (t
6994       (or (eq method 'nndigest)
6995           (gnus-summary-jump-to-group gnus-newsgroup-name))
6996       (let ((cmd (aref (this-command-keys) 0))
6997             (group 
6998              (if (eq gnus-keep-same-level 'best) 
6999                  (gnus-summary-best-group gnus-newsgroup-name)
7000                (gnus-summary-search-group backward gnus-keep-same-level))))
7001         ;; Keep just the event type of CMD.
7002         (and (listp cmd) (setq cmd (car cmd)))
7003         ;; Select next unread newsgroup automagically.
7004         (cond 
7005          ((not gnus-auto-select-next)
7006           (message "No more%s articles" (if unread " unread" "")))
7007          ((eq gnus-auto-select-next 'quietly)
7008           ;; Select quietly.
7009           (if (eq method 'nndigest)
7010               (gnus-summary-exit)
7011             (message "No more%s articles (%s)..."
7012                      (if unread " unread" "") 
7013                      (if group (concat "selecting " group)
7014                        "exiting"))
7015             (gnus-summary-next-group nil group backward)))
7016          (t
7017           (let ((keystrokes '(?\C-n ?\C-p))
7018                 key)
7019             (while (or (null key) (memq key keystrokes))
7020               (message 
7021                "No more%s articles%s" (if unread " unread" "")
7022                (if (and group (not (eq method 'nndigest)))
7023                    (format " (Type %s for %s [%s])"
7024                            (single-key-description cmd) group
7025                            (car (gnus-gethash group gnus-newsrc-hashtb)))
7026                  (format " (Type %s to exit %s)"
7027                          (single-key-description cmd)
7028                          gnus-newsgroup-name)))
7029               ;; Confirm auto selection.
7030               (let* ((event (read-event)))
7031                 (setq key (if (listp event) (car event) event))
7032                 (if (member key keystrokes)
7033                     (let ((obuf (current-buffer)))
7034                       (switch-to-buffer gnus-group-buffer)
7035                       (gnus-group-jump-to-group group)
7036                       (execute-kbd-macro (char-to-string key))
7037                       (setq group (gnus-group-group-name))
7038                       (switch-to-buffer obuf)))))
7039             (if (eq key cmd)
7040                 (if (or (not group) (eq method 'nndigest))
7041                     (gnus-summary-exit)
7042                   (gnus-summary-next-group nil group backward))
7043               (setq unread-command-events (list key)))))))))))
7044
7045 (defun gnus-summary-next-unread-article ()
7046   "Select unread article after current one."
7047   (interactive)
7048   (gnus-summary-next-article t (and gnus-auto-select-same
7049                                     (gnus-summary-subject-string))))
7050
7051 (defun gnus-summary-prev-article (unread &optional subject)
7052   "Select the article after the current one.
7053 If UNREAD is non-nil, only unread articles are selected."
7054   (interactive "P")
7055   (gnus-summary-next-article unread subject t))
7056
7057 (defun gnus-summary-prev-unread-article ()
7058   "Select unred article before current one."
7059   (interactive)
7060   (gnus-summary-prev-article t (and gnus-auto-select-same
7061                                     (gnus-summary-subject-string))))
7062
7063 (defun gnus-summary-next-page (lines &optional circular)
7064   "Show next page of selected article.
7065 If end of article, select next article.
7066 Argument LINES specifies lines to be scrolled up.
7067 If CIRCULAR is non-nil, go to the start of the article instead of 
7068 instead of selecting the next article when reaching the end of the
7069 current article." 
7070   (interactive "P")
7071   (setq gnus-summary-buffer (current-buffer))
7072   (gnus-set-global-variables)
7073   (let ((article (gnus-summary-article-number))
7074         (endp nil))
7075     (if (or (null gnus-current-article)
7076             (null gnus-article-current)
7077             (/= article (cdr gnus-article-current))
7078             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7079         ;; Selected subject is different from current article's.
7080         (gnus-summary-display-article article)
7081       (gnus-configure-windows 'article)
7082       (gnus-eval-in-buffer-window
7083        gnus-article-buffer
7084        (setq endp (gnus-article-next-page lines)))
7085       (if endp
7086           (cond (circular
7087                  (gnus-summary-beginning-of-article))
7088                 (lines
7089                  (message "End of message"))
7090                 ((null lines)
7091                  (gnus-summary-next-unread-article))))
7092       (and gnus-summary-buffer
7093            (buffer-name gnus-summary-buffer)
7094            (progn
7095              (pop-to-buffer gnus-summary-buffer)
7096              (gnus-summary-position-cursor))))))
7097
7098 (defun gnus-summary-prev-page (lines)
7099   "Show previous page of selected article.
7100 Argument LINES specifies lines to be scrolled down."
7101   (interactive "P")
7102   (let ((article (gnus-summary-article-number)))
7103     (if (or (null gnus-current-article)
7104             (null gnus-article-current)
7105             (/= article (cdr gnus-article-current))
7106             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7107         ;; Selected subject is different from current article's.
7108         (gnus-summary-display-article article)
7109       (gnus-configure-windows 'article)
7110       (pop-to-buffer gnus-summary-buffer)
7111       (gnus-eval-in-buffer-window gnus-article-buffer
7112         (gnus-article-prev-page lines))))
7113   (gnus-summary-position-cursor))
7114
7115 (defun gnus-summary-scroll-up (lines)
7116   "Scroll up (or down) one line current article.
7117 Argument LINES specifies lines to be scrolled up (or down if negative)."
7118   (interactive "p")
7119   (or (gnus-summary-select-article nil nil 'pseudo)
7120       (gnus-eval-in-buffer-window 
7121        gnus-article-buffer
7122        (cond ((> lines 0)
7123               (if (gnus-article-next-page lines)
7124                   (message "End of message")))
7125              ((< lines 0)
7126               (gnus-article-prev-page (- lines))))))
7127   (gnus-summary-position-cursor))
7128
7129 (defun gnus-summary-next-same-subject ()
7130   "Select next article which has the same subject as current one."
7131   (interactive)
7132   (gnus-summary-next-article nil (gnus-summary-subject-string)))
7133
7134 (defun gnus-summary-prev-same-subject ()
7135   "Select previous article which has the same subject as current one."
7136   (interactive)
7137   (gnus-summary-prev-article nil (gnus-summary-subject-string)))
7138
7139 (defun gnus-summary-next-unread-same-subject ()
7140   "Select next unread article which has the same subject as current one."
7141   (interactive)
7142   (gnus-summary-next-article t (gnus-summary-subject-string)))
7143
7144 (defun gnus-summary-prev-unread-same-subject ()
7145   "Select previous unread article which has the same subject as current one."
7146   (interactive)
7147   (gnus-summary-prev-article t (gnus-summary-subject-string)))
7148
7149 (defun gnus-summary-first-unread-article ()
7150   "Select the first unread article. 
7151 Return nil if there are no unread articles."
7152   (interactive)
7153   (prog1
7154       (if (gnus-summary-first-subject t)
7155           (gnus-summary-display-article (gnus-summary-article-number)))
7156     (gnus-summary-position-cursor)))
7157
7158 (defun gnus-summary-best-unread-article ()
7159   "Select the unread article with the highest score."
7160   (interactive)
7161   (gnus-set-global-variables)
7162   (let ((scored gnus-newsgroup-scored)
7163         (best -1000000)
7164         article art)
7165     (while scored
7166       (or (> best (cdr (car scored)))
7167           (and (memq (setq art (car (car scored))) gnus-newsgroup-unreads)
7168                (not (memq art gnus-newsgroup-marked))
7169                (not (memq art gnus-newsgroup-dormant))
7170                (if (= best (cdr (car scored)))
7171                    (setq article (min art article))
7172                  (setq article art)
7173                  (setq best (cdr (car scored))))))
7174       (setq scored (cdr scored)))
7175     (if article 
7176         (gnus-summary-goto-article article)
7177       (gnus-summary-first-unread-article))
7178     (gnus-summary-position-cursor)))
7179
7180 (defun gnus-summary-goto-article (article &optional all-headers)
7181   "Fetch ARTICLE and display it if it exists.
7182 If ALL-HEADERS is non-nil, no header lines are hidden."
7183   (interactive
7184    (list
7185     (string-to-int
7186      (completing-read 
7187       "Article number: "
7188       (mapcar (lambda (headers) (list (int-to-string (header-number headers))))
7189               gnus-newsgroup-headers) 
7190       nil 'require-match))))
7191   (if (gnus-summary-goto-subject article)
7192       (gnus-summary-display-article article all-headers))
7193   (gnus-summary-position-cursor))
7194
7195 (defun gnus-summary-goto-last-article ()
7196   "Go to the last article."
7197   (interactive)
7198   (if gnus-last-article
7199       (gnus-summary-goto-article gnus-last-article))
7200   (gnus-summary-position-cursor))
7201
7202 (defun gnus-summary-pop-article (number)
7203   "Pop one article off the history and go to the previous.
7204 NUMBER articles will be popped off."
7205   (interactive "p")
7206   (let (to)
7207     (setq gnus-newsgroup-history
7208           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7209     (if to
7210         (gnus-summary-goto-article (car to))
7211       (error "Article history empty")))
7212   (gnus-summary-position-cursor))
7213
7214 ;; Summary article oriented commands
7215
7216 (defun gnus-summary-refer-parent-article (n)
7217   "Refer parent article N times.
7218 The difference between N and the number of articles fetched is returned."
7219   (interactive "p")
7220   (gnus-set-global-variables)
7221   (while 
7222       (and 
7223        (> n 0)
7224        (let ((ref (header-references (gnus-get-header-by-number
7225                                       (gnus-summary-article-number)))))
7226          (if (and ref (not (equal ref ""))
7227                   (string-match "<[^<>]*>[ \t]*$" ref))
7228              (gnus-summary-refer-article 
7229               (substring ref (match-beginning 0) (match-end 0))))))
7230     (setq n (1- n)))
7231   (or (zerop n) (message "No references in article or expired article."))
7232   (gnus-summary-position-cursor)
7233   n)
7234     
7235 (defun gnus-summary-refer-article (message-id)
7236   "Refer article specified by MESSAGE-ID.
7237 NOTE: This command only works with newsgroup that use NNTP."
7238   (interactive "sMessage-ID: ")
7239   (if (or (not (stringp message-id))
7240           (zerop (length message-id)))
7241       ()
7242     ;; Construct the correct Message-ID if necessary.
7243     ;; Suggested by tale@pawl.rpi.edu.
7244     (or (string-match "^<" message-id)
7245         (setq message-id (concat "<" message-id)))
7246     (or (string-match ">$" message-id)
7247         (setq message-id (concat message-id ">")))
7248     (let ((header (car (gnus-gethash (downcase message-id)
7249                                      gnus-newsgroup-dependencies))))
7250       (if header
7251           (gnus-summary-goto-article (header-number header))
7252         (let ((gnus-override-method gnus-refer-article-method))
7253           (and gnus-refer-article-method
7254                (or (gnus-server-opened gnus-refer-article-method)
7255                    (gnus-open-server gnus-refer-article-method)))
7256           (if (gnus-article-prepare 
7257                message-id nil (gnus-read-header message-id))
7258               (progn
7259                 (gnus-summary-insert-line 
7260                  nil gnus-current-headers 0 nil gnus-read-mark nil nil 
7261                  (header-subject gnus-current-headers))
7262                 (forward-line -1)
7263                 (gnus-summary-position-cursor)
7264                 (gnus-summary-update-line)
7265                 message-id)
7266             (message "No such references")
7267             nil))))))
7268
7269 (defun gnus-summary-enter-digest-group ()
7270   "Enter a digest group based on the current article."
7271   (interactive)
7272   (gnus-set-global-variables)
7273   (gnus-summary-select-article)
7274   (let ((name (format "%s/%d" 
7275                       (gnus-group-prefixed-name 
7276                        gnus-newsgroup-name (list 'nndigest "")) 
7277                       gnus-current-article))
7278         (buf (current-buffer)))
7279     (set-buffer gnus-group-buffer)
7280     (gnus-sethash 
7281      name 
7282      (list t nil (list name gnus-level-default-subscribed nil nil 
7283                        (list 'nndigest gnus-article-buffer
7284                              (cons 'quit-buffer buf))))
7285      gnus-newsrc-hashtb)
7286     (condition-case ()
7287         (gnus-group-read-group t nil name)
7288       (error nil)
7289       (quit nil))
7290     (if (not (equal major-mode 'gnus-group-mode))
7291         ()
7292       (switch-to-buffer buf)
7293       (gnus-set-global-variables)
7294       (gnus-configure-windows 'summary)
7295       (message "Article not a digest?"))))
7296   
7297 (defun gnus-summary-isearch-article ()
7298   "Do incremental search forward on current article."
7299   (interactive)
7300   (gnus-summary-select-article)
7301   (gnus-eval-in-buffer-window gnus-article-buffer
7302                               (isearch-forward)))
7303
7304 (defun gnus-summary-search-article-forward (regexp)
7305   "Search for an article containing REGEXP forward.
7306 gnus-select-article-hook is not called during the search."
7307   (interactive
7308    (list (read-string
7309           (concat "Search forward (regexp): "
7310                   (if gnus-last-search-regexp
7311                       (concat "(default " gnus-last-search-regexp ") "))))))
7312   (if (string-equal regexp "")
7313       (setq regexp (or gnus-last-search-regexp ""))
7314     (setq gnus-last-search-regexp regexp))
7315   (if (gnus-summary-search-article regexp nil)
7316       (gnus-eval-in-buffer-window 
7317        gnus-article-buffer
7318        (recenter 0))
7319     (error "Search failed: \"%s\"" regexp)))
7320
7321 (defun gnus-summary-search-article-backward (regexp)
7322   "Search for an article containing REGEXP backward.
7323 gnus-select-article-hook is not called during the search."
7324   (interactive
7325    (list (read-string
7326           (concat "Search backward (regexp): "
7327                   (if gnus-last-search-regexp
7328                       (concat "(default " gnus-last-search-regexp ") "))))))
7329   (if (string-equal regexp "")
7330       (setq regexp (or gnus-last-search-regexp ""))
7331     (setq gnus-last-search-regexp regexp))
7332   (if (gnus-summary-search-article regexp t)
7333       (gnus-eval-in-buffer-window
7334        gnus-article-buffer
7335        (recenter 0))
7336     (error "Search failed: \"%s\"" regexp)))
7337
7338 (defun gnus-summary-search-article (regexp &optional backward)
7339   "Search for an article containing REGEXP.
7340 Optional argument BACKWARD means do search for backward.
7341 gnus-select-article-hook is not called during the search."
7342   (let ((gnus-select-article-hook nil)  ;Disable hook.
7343         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7344         (re-search
7345          (if backward
7346              (function re-search-backward) (function re-search-forward)))
7347         (found nil)
7348         (last nil))
7349     ;; Hidden thread subtrees must be searched for ,too.
7350     (gnus-summary-show-all-threads)
7351     (if (eobp) (forward-line -1))
7352     ;; First of all, search current article.
7353     ;; We don't want to read article again from NNTP server nor reset
7354     ;; current point.
7355     (gnus-summary-select-article)
7356     (message "Searching article: %d..." gnus-current-article)
7357     (setq last gnus-current-article)
7358     (gnus-eval-in-buffer-window gnus-article-buffer
7359       (save-restriction
7360         (widen)
7361         ;; Begin search from current point.
7362         (setq found (funcall re-search regexp nil t))))
7363     ;; Then search next articles.
7364     (while (and (not found)
7365                 (gnus-summary-display-article 
7366                  (gnus-summary-search-subject backward nil nil)))
7367       (message "Searching article: %d..." gnus-current-article)
7368       (gnus-eval-in-buffer-window gnus-article-buffer
7369         (save-restriction
7370           (widen)
7371           (goto-char (if backward (point-max) (point-min)))
7372           (setq found (funcall re-search regexp nil t)))))
7373     (message "")
7374     ;; Adjust article pointer.
7375     (or (eq last gnus-current-article)
7376         (setq gnus-last-article last))
7377     ;; Return T if found such article.
7378     found))
7379
7380 (defun gnus-summary-execute-command (header regexp command &optional backward)
7381   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7382 If HEADER is an empty string (or nil), the match is done on the entire
7383 article. If BACKWARD (the prefix) is non-nil, search backward instead."
7384   (interactive
7385    (list (let ((completion-ignore-case t))
7386            (completing-read 
7387             "Header name: "
7388             (mapcar (string) (list string)
7389                     '("Number" "Subject" "From" "Lines" "Date"
7390                       "Message-ID" "Xref" "References")
7391                     nil 'require-match)))
7392          (read-string "Regexp: ")
7393          (read-key-sequence "Command: ")
7394          current-prefix-arg))
7395   ;; Hidden thread subtrees must be searched as well.
7396   (gnus-summary-show-all-threads)
7397   ;; We don't want to change current point nor window configuration.
7398   (save-excursion
7399     (save-window-excursion
7400       (message "Executing %s..." (key-description command))
7401       ;; We'd like to execute COMMAND interactively so as to give arguments.
7402       (gnus-execute header regexp
7403                     (` (lambda ()
7404                          (call-interactively '(, (key-binding command)))))
7405                     backward)
7406       (message "Executing %s... done" (key-description command)))))
7407
7408 (defun gnus-summary-beginning-of-article ()
7409   "Scroll the article back to the beginning."
7410   (interactive)
7411   (gnus-summary-select-article)
7412   (gnus-eval-in-buffer-window
7413    gnus-article-buffer
7414    (widen)
7415    (goto-char (point-min))
7416    (and gnus-break-pages (gnus-narrow-to-page))))
7417
7418 (defun gnus-summary-end-of-article ()
7419   "Scroll to the end of the article."
7420   (interactive)
7421   (gnus-summary-select-article)
7422   (gnus-eval-in-buffer-window 
7423    gnus-article-buffer
7424    (widen)
7425    (goto-char (point-max))
7426    (and gnus-break-pages (gnus-narrow-to-page))))
7427
7428 (defun gnus-summary-show-article ()
7429   "Force re-fetching of the current article."
7430   (interactive)
7431   (gnus-summary-select-article gnus-have-all-headers t t))
7432
7433 (defun gnus-summary-toggle-header (arg)
7434   "Show the headers if they are hidden, or hide them if they are shown.
7435 If ARG is a positive number, show the entire header.
7436 If ARG is a negative number, hide the unwanted header lines."
7437   (interactive "P")
7438   (gnus-set-global-variables)
7439   (save-excursion
7440     (set-buffer gnus-article-buffer)
7441     (let ((buffer-read-only nil))
7442       (if (numberp arg) 
7443           (if (> arg 0) (remove-text-properties 1 (point-max) '(invisible t))
7444             (if (< arg 0) (run-hooks 'gnus-article-display-hook)))
7445         (if (text-property-any 1 (point-max) 'invisible t)
7446             (remove-text-properties 1 (point-max) '(invisible t))
7447           (let ((gnus-have-all-headers nil))
7448             (run-hooks 'gnus-article-display-hook))))
7449       (set-window-point (get-buffer-window (current-buffer)) 1))))
7450
7451 (defun gnus-summary-show-all-headers ()
7452   "Make all header lines visible."
7453   (interactive)
7454   (gnus-article-show-all-headers))
7455
7456 (defun gnus-summary-toggle-mime (arg)
7457   "Toggle MIME processing.
7458 If ARG is a positive number, turn MIME processing on."
7459   (interactive "P")
7460   (setq gnus-show-mime
7461         (if (null arg) (not gnus-show-mime)
7462           (> (prefix-numeric-value arg) 0)))
7463   (gnus-summary-select-article t 'force))
7464
7465 (defun gnus-summary-caesar-message (rotnum)
7466   "Caesar rotates all letters of current message by 13/47 places.
7467 With prefix arg, specifies the number of places to rotate each letter forward.
7468 Caesar rotates Japanese letters by 47 places in any case."
7469   (interactive "P")
7470   (gnus-summary-select-article)
7471   (let ((mail-header-separator "")) ; !!! Is this necessary?
7472     (gnus-overload-functions)
7473     (gnus-eval-in-buffer-window 
7474      gnus-article-buffer
7475      (save-restriction
7476        (widen)
7477        ;; We don't want to jump to the beginning of the message.
7478        ;; `save-excursion' does not do its job.
7479        (move-to-window-line 0)
7480        (let ((last (point)))
7481          (news-caesar-buffer-body rotnum)
7482          (goto-char last)
7483          (recenter 0))))))
7484
7485 (defun gnus-summary-stop-page-breaking ()
7486   "Stop page breaking in the current article."
7487   (interactive)
7488   (gnus-summary-select-article)
7489   (gnus-eval-in-buffer-window gnus-article-buffer (widen)))
7490
7491 ;; Suggested by Brian Edmonds <bedmonds@prodigy.bc.ca>.
7492
7493 (defun gnus-summary-move-article (n &optional to-newsgroup select-method)
7494   "Move the current article to a different newsgroup.
7495 If N is a positive number, move the N next articles.
7496 If N is a negative number, move the N previous articles.
7497 If N is nil and any articles have been marked with the process mark,
7498 move those articles instead.
7499 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to. 
7500 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
7501 re-spool using this method.
7502 For this function to work, both the current newsgroup and the
7503 newsgroup that you want to move to have to support the `request-move'
7504 and `request-accept' functions. (Ie. mail newsgroups at present.)"
7505   (interactive "P")
7506   (gnus-set-global-variables)
7507   (or (gnus-check-backend-function 'request-move-article gnus-newsgroup-name)
7508       (error "The current newsgroup does not support article moving"))
7509   (let ((articles (gnus-summary-work-articles n))
7510         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
7511         art-group)
7512     (if (and (not to-newsgroup) (not select-method))
7513         (setq to-newsgroup
7514               (completing-read 
7515                (format "Where do you want to move %s? %s"
7516                        (if (> (length articles) 1)
7517                            (format "these %d articles" (length articles))
7518                          "this article")
7519                        (if gnus-current-move-group
7520                            (format "(%s default) " gnus-current-move-group)
7521                          ""))
7522                gnus-active-hashtb nil nil prefix)))
7523     (if to-newsgroup
7524         (progn
7525           (if (or (string= to-newsgroup "") (string= to-newsgroup prefix))
7526               (setq to-newsgroup (or gnus-current-move-group "")))
7527           (or (gnus-gethash to-newsgroup gnus-active-hashtb)
7528               (gnus-activate-newsgroup to-newsgroup)
7529               (error "No such group: %s" to-newsgroup))
7530           (setq gnus-current-move-group to-newsgroup)))
7531     (or (gnus-check-backend-function 'request-accept-article 
7532                                      (or select-method to-newsgroup))
7533         (error "%s does not support article moving" to-newsgroup))
7534     (message "Moving to %s: %s..." (or select-method to-newsgroup) articles)
7535     (while articles
7536       (if (setq art-group
7537                 (gnus-request-move-article 
7538                  (car articles)                   ; Article to move
7539                  gnus-newsgroup-name              ; From newsgrouo
7540                  (nth 1 (gnus-find-method-for-group 
7541                          gnus-newsgroup-name))    ; Server
7542                  (list 'gnus-request-accept-article 
7543                        (if select-method
7544                            (quote select-method)
7545                          to-newsgroup)
7546                        (not (cdr articles)))     ; Accept form
7547                  (not (cdr articles))))          ; Only save nov last time
7548           (let* ((buffer-read-only nil)
7549                  (entry 
7550                   (or
7551                    (gnus-gethash (car art-group) gnus-newsrc-hashtb)
7552                    (gnus-gethash 
7553                     (gnus-group-prefixed-name 
7554                      (car art-group) 
7555                      (if select-method (list select-method "")
7556                        (gnus-find-method-for-group to-newsgroup)))
7557                     gnus-newsrc-hashtb)))
7558                  (info (nth 2 entry))
7559                  (article (car articles))
7560                  (marked (nth 3 info)))
7561             (gnus-summary-goto-subject article)
7562             (beginning-of-line)
7563             (delete-region (point)
7564                            (progn (forward-line 1) (point)))
7565             (if (not (memq article gnus-newsgroup-unreads))
7566                 (setcar (cdr (cdr info))
7567                         (gnus-add-to-range (nth 2 info) 
7568                                            (list (cdr art-group)))))
7569             ;; Copy any marks over to the new group.
7570             (let ((marks '((tick . gnus-newsgroup-marked)
7571                            (dormant . gnus-newsgroup-dormant)
7572                            (expire . gnus-newsgroup-expirable)
7573                            (bookmark . gnus-newsgroup-bookmarks)
7574                         ;   (score . gnus-newsgroup-scored)
7575                            (reply . gnus-newsgroup-replied)))
7576                   (to-article (cdr art-group)))
7577               (while marks
7578                 (if (memq article (symbol-value (cdr (car marks))))
7579                     (gnus-add-marked-articles 
7580                      (car info) (car (car marks)) (list to-article) info))
7581                 (setq marks (cdr marks))))
7582             (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7583             (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
7584             (setq gnus-newsgroup-dormant
7585                   (delq article gnus-newsgroup-dormant)))
7586         (message "Couldn't move article %s" (car articles)))
7587       (gnus-summary-remove-process-mark (car articles))
7588       (setq articles (cdr articles)))))
7589
7590 (defun gnus-summary-respool-article (n &optional respool-method)
7591   "Respool the current article.
7592 The article will be squeezed through the mail spooling process again,
7593 which means that it will be put in some mail newsgroup or other
7594 depending on `nnmail-split-methods'.
7595 If N is a positive number, respool the N next articles.
7596 If N is a negative number, respool the N previous articles.
7597 If N is nil and any articles have been marked with the process mark,
7598 respool those articles instead.
7599
7600 Respooling can be done both from mail groups and \"real\" newsgroups.
7601 In the former case, the articles in question will be moved from the
7602 current group into whatever groups they are destined to.  In the
7603 latter case, they will be copied into the relevant groups."
7604   (interactive "P")
7605   (gnus-set-global-variables)
7606   (let ((respool-methods (gnus-methods-using 'respool)))
7607     (or respool-method
7608         (setq respool-method
7609               (completing-read
7610                "What method do you want to use when respooling? "
7611                respool-methods nil t)))
7612     (if (assoc (symbol-name
7613                 (car (gnus-find-method-for-group gnus-newsgroup-name)))
7614                respool-methods)
7615         (gnus-summary-move-article n nil (intern respool-method)))
7616     (gnus-summary-copy-article n nil (intern respool-method))))
7617
7618 ;; Suggested by gregj@unidata.com (Gregory J. Grubbs).
7619 (defun gnus-summary-copy-article (n &optional to-newsgroup select-method)
7620   "Move the current article to a different newsgroup.
7621 If N is a positive number, move the N next articles.
7622 If N is a negative number, move the N previous articles.
7623 If N is nil and any articles have been marked with the process mark,
7624 move those articles instead.
7625 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to. 
7626 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
7627 re-spool using this method.
7628 For this function to work, the newsgroup that you want to move to have
7629 to support the `request-move' and `request-accept'
7630 functions. (Ie. mail newsgroups at present.)"
7631   (interactive "P")
7632   (gnus-set-global-variables)
7633   (let ((articles (gnus-summary-work-articles n))
7634         (copy-buf (get-buffer-create "*copy work*"))
7635         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
7636         art-group)
7637     (buffer-disable-undo copy-buf)
7638     (if (and (not to-newsgroup) (not select-method))
7639         (setq to-newsgroup
7640               (completing-read 
7641                (format "Where do you want to copy %s? %s"
7642                        (if (> (length articles) 1)
7643                            (format "these %d articles" (length articles))
7644                          "this article")
7645                        (if gnus-current-move-group
7646                            (format "(%s default) " gnus-current-move-group)
7647                          ""))
7648                gnus-active-hashtb nil nil prefix)))
7649     (if to-newsgroup
7650         (progn
7651           (if (or (string= to-newsgroup "") (string= to-newsgroup prefix))
7652               (setq to-newsgroup (or gnus-current-move-group "")))
7653           (or (gnus-gethash to-newsgroup gnus-active-hashtb)
7654               (gnus-activate-newsgroup to-newsgroup)
7655               (error "No such group: %s" to-newsgroup))
7656           (setq gnus-current-move-group to-newsgroup)))
7657     (or (gnus-check-backend-function 'request-accept-article 
7658                                      (or select-method to-newsgroup))
7659         (error "%s does not support article copying" to-newsgroup))
7660     (message "Copying to %s: %s..." (or select-method to-newsgroup) articles)
7661     (while articles
7662       (if (setq art-group
7663                 (save-excursion
7664                   (set-buffer copy-buf)
7665                   (gnus-request-article-this-buffer
7666                    (car articles) gnus-newsgroup-name)
7667                   (gnus-request-accept-article
7668                    (if select-method (quote select-method) to-newsgroup)
7669                    (not (cdr articles)))))
7670           (let* ((entry 
7671                   (or
7672                    (gnus-gethash (car art-group) gnus-newsrc-hashtb)
7673                    (gnus-gethash 
7674                     (gnus-group-prefixed-name 
7675                      (car art-group) 
7676                      (if select-method (list select-method "")
7677                        (gnus-find-method-for-group to-newsgroup)))
7678                     gnus-newsrc-hashtb)))
7679                  (info (nth 2 entry))
7680                  (article (car articles))
7681                  (marked (nth 3 info)))
7682             (if (not (memq article gnus-newsgroup-unreads))
7683                 (setcar (cdr (cdr info))
7684                         (gnus-add-to-range (nth 2 info) 
7685                                            (list (cdr art-group)))))
7686             ;; Copy any marks over to the new group.
7687             (let ((marks '((tick . gnus-newsgroup-marked)
7688                            (dormant . gnus-newsgroup-dormant)
7689                            (expire . gnus-newsgroup-expirable)
7690                            (bookmark . gnus-newsgroup-bookmarks)
7691                         ;   (score . gnus-newsgroup-scored)
7692                            (reply . gnus-newsgroup-replied)))
7693                   (to-article (cdr art-group)))
7694               (while marks
7695                 (if (memq article (symbol-value (cdr (car marks))))
7696                     (gnus-add-marked-articles 
7697                      (car info) (car (car marks)) (list to-article) info))
7698                 (setq marks (cdr marks)))))
7699         (message "Couldn't copy article %s" (car articles)))
7700       (gnus-summary-remove-process-mark (car articles))
7701       (setq articles (cdr articles)))
7702     (kill-buffer copy-buf)))
7703
7704 (defun gnus-summary-expire-articles ()
7705   "Expire all articles that are marked as expirable in the current group."
7706   (interactive)
7707   (if (and gnus-newsgroup-expirable
7708            (gnus-check-backend-function 
7709             'request-expire-articles gnus-newsgroup-name))
7710       (let ((expirable gnus-newsgroup-expirable))
7711         ;; The list of articles that weren't expired is returned.
7712         (setq gnus-newsgroup-expirable 
7713               (gnus-request-expire-articles gnus-newsgroup-expirable
7714                                             gnus-newsgroup-name))
7715         ;; We go through the old list of expirable, and mark all
7716         ;; really expired articles as non-existant.
7717         (while expirable
7718           (or (memq (car expirable) gnus-newsgroup-expirable)
7719               (gnus-summary-mark-as-read (car expirable) "%"))
7720           (setq expirable (cdr expirable))))))
7721
7722 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
7723 (defun gnus-summary-delete-article (n)
7724   "Delete the N next (mail) articles.
7725 This command actually deletes articles. This is not a marking
7726 command. The article will disappear forever from you life, never to
7727 return. 
7728 If N is negative, delete backwards.
7729 If N is nil and articles have been marked with the process mark,
7730 delete these instead."
7731   (interactive "P")
7732   (or (gnus-check-backend-function 'request-expire-articles 
7733                                    gnus-newsgroup-name)
7734       (error "The current newsgroup does not support article deletion."))
7735   ;; Compute the list of articles to delete.
7736   (let ((articles (gnus-summary-work-articles n)))
7737     (if (and gnus-novice-user
7738              (not (gnus-y-or-n-p 
7739                    (format "Do you really want to delete %s forever? "
7740                            (if (> (length articles) 1) "these articles"
7741                              "this article")))))
7742         ()
7743       ;; Delete the articles.
7744       (setq gnus-newsgroup-expirable 
7745             (gnus-request-expire-articles 
7746              articles gnus-newsgroup-name 'force))
7747       (while articles
7748         (gnus-summary-remove-process-mark (car articles))       
7749         ;; The backend might not have been able to delete the article
7750         ;; after all.  
7751         (or (memq (car articles) gnus-newsgroup-expirable)
7752             (gnus-summary-mark-as-read (car articles) gnus-canceled-mark))
7753         (setq articles (cdr articles)))))
7754   (gnus-summary-position-cursor))
7755
7756 (defun gnus-summary-edit-article ()
7757   "Enter into a buffer and edit the current article.
7758 This will have permanent effect only in mail groups."
7759   (interactive)
7760   (or (gnus-check-backend-function 
7761        'request-replace-article gnus-newsgroup-name)
7762       (error "The current newsgroup does not support article editing."))
7763   (gnus-summary-select-article t)
7764   (other-window 1)
7765   (message "C-c C-c to end edits")
7766   (setq buffer-read-only nil)
7767   (text-mode)
7768   (use-local-map (copy-keymap (current-local-map)))
7769   (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
7770   (goto-char 1)
7771   (search-forward "\n\n" nil t))
7772
7773 (defun gnus-summary-edit-article-done ()
7774   "Make edits to the current article permanent."
7775   (interactive)
7776   (if (not (gnus-request-replace-article 
7777             (cdr gnus-article-current) (car gnus-article-current) 
7778             (current-buffer)))
7779       (error "Couldn't replace article.")
7780     (gnus-article-mode)
7781     (use-local-map gnus-article-mode-map)
7782     (setq buffer-read-only t)
7783     (pop-to-buffer gnus-summary-buffer)))      
7784
7785 ;; Summary score commands.
7786
7787 ;; Suggested by boubaker@cenatls.cena.dgac.fr.
7788
7789 (defun gnus-summary-raise-score (n)
7790   "Raise the score of the current article by N."
7791   (interactive "p")
7792   (gnus-summary-set-score (+ (gnus-summary-article-score) n)))
7793
7794 (defun gnus-summary-lower-score (n)
7795   "Lower the score of the current article by N."
7796   (interactive "p")
7797   (gnus-summary-raise-score (- n)))
7798
7799 (defun gnus-summary-set-score (n)
7800   "Set the score of the current article to N."
7801   (interactive "p")
7802   ;; Skip dummy header line.
7803   (save-excursion
7804     (if (eq (gnus-summary-article-mark) ?Z)
7805         (forward-line 1))
7806     (let ((buffer-read-only nil))
7807       ;; Set score.
7808       (gnus-summary-update-mark
7809        (if (= n (or gnus-summary-default-score 0)) ? 
7810          (if (< n (or gnus-summary-default-score 0)) 
7811              gnus-score-below-mark gnus-score-over-mark)) 'score))
7812     (let* ((article (gnus-summary-article-number))
7813            (score (assq article gnus-newsgroup-scored)))
7814       (if score (setcdr score n)
7815         (setq gnus-newsgroup-scored 
7816               (cons (cons article n) gnus-newsgroup-scored))))
7817     (gnus-summary-update-line)))
7818
7819 ;; Summary marking commands.
7820
7821 (defun gnus-summary-raise-same-subject-and-select (score)
7822   "Raise articles which has the same subject with SCORE and select the next."
7823   (interactive "p")
7824   (let ((subject (gnus-summary-subject-string)))
7825     (gnus-summary-raise-score score)
7826     (while (gnus-summary-search-subject nil nil subject)
7827       (gnus-summary-raise-score score))
7828     (gnus-summary-next-article t)))
7829
7830 (defun gnus-summary-raise-same-subject (score)
7831   "Raise articles which has the same subject with SCORE."
7832   (interactive "p")
7833   (let ((subject (gnus-summary-subject-string)))
7834     (gnus-summary-raise-score score)
7835     (while (gnus-summary-search-subject nil nil subject)
7836       (gnus-summary-raise-score score))
7837     (gnus-summary-next-subject 1 t)))
7838
7839 (defun gnus-summary-raise-thread (score)
7840   "Raise articles under current thread with SCORE."
7841   (interactive "P")
7842   (setq score (1- (gnus-score-default score)))
7843   (let (e)
7844     (save-excursion
7845       (let ((level (gnus-summary-thread-level)))
7846         (gnus-summary-raise-score score)
7847         (while (and (zerop (gnus-summary-next-subject 1 nil t))
7848                     (> (gnus-summary-thread-level) level))
7849           (gnus-summary-raise-score score))
7850         (setq e (point))))
7851     (or (zerop (gnus-summary-next-subject 1 t))
7852         (goto-char e)))
7853   (gnus-summary-recenter)
7854   (gnus-summary-position-cursor)
7855   (gnus-set-mode-line 'summary))
7856
7857 (defun gnus-summary-lower-same-subject-and-select (score)
7858   "Raise articles which has the same subject with SCORE and select the next."
7859   (interactive "p")
7860   (gnus-summary-raise-same-subject-and-select (- score)))
7861
7862 (defun gnus-summary-lower-same-subject (score)
7863   "Raise articles which has the same subject with SCORE."
7864   (interactive "p")
7865   (gnus-summary-raise-same-subject (- score)))
7866
7867 (defun gnus-summary-lower-thread (score)
7868   "Raise articles under current thread with SCORE."
7869   (interactive "P")
7870   (gnus-summary-raise-thread (- (1- (gnus-score-default score)))))
7871
7872 (defun gnus-summary-kill-same-subject-and-select (unmark)
7873   "Mark articles which has the same subject as read, and then select the next.
7874 If UNMARK is positive, remove any kind of mark.
7875 If UNMARK is negative, tick articles."
7876   (interactive "P")
7877   (if unmark
7878       (setq unmark (prefix-numeric-value unmark)))
7879   (let ((count
7880          (gnus-summary-mark-same-subject
7881           (gnus-summary-subject-string) unmark)))
7882     ;; Select next unread article. If auto-select-same mode, should
7883     ;; select the first unread article.
7884     (gnus-summary-next-article t (and gnus-auto-select-same
7885                                       (gnus-summary-subject-string)))
7886     (message "%d articles are marked as %s"
7887              count (if unmark "unread" "read"))))
7888
7889 (defun gnus-summary-kill-same-subject (unmark)
7890   "Mark articles which has the same subject as read. 
7891 If UNMARK is positive, remove any kind of mark.
7892 If UNMARK is negative, tick articles."
7893   (interactive "P")
7894   (if unmark
7895       (setq unmark (prefix-numeric-value unmark)))
7896   (let ((count
7897          (gnus-summary-mark-same-subject
7898           (gnus-summary-subject-string) unmark)))
7899     ;; If marked as read, go to next unread subject.
7900     (if (null unmark)
7901         ;; Go to next unread subject.
7902         (gnus-summary-next-subject 1 t))
7903     (message "%d articles are marked as %s"
7904              count (if unmark "unread" "read"))))
7905
7906 (defun gnus-summary-mark-same-subject (subject &optional unmark)
7907   "Mark articles with same SUBJECT as read, and return marked number.
7908 If optional argument UNMARK is positive, remove any kinds of marks.
7909 If optional argument UNMARK is negative, mark articles as unread instead."
7910   (let ((count 1))
7911     (save-excursion
7912       (cond ((null unmark)
7913              (gnus-summary-mark-as-read nil gnus-killed-mark))
7914             ((> unmark 0)
7915              (gnus-summary-tick-article nil t))
7916             (t
7917              (gnus-summary-tick-article)))
7918       (while (and subject
7919                   (gnus-summary-search-forward nil subject))
7920         (cond ((null unmark)
7921                (gnus-summary-mark-as-read nil gnus-killed-mark))
7922               ((> unmark 0)
7923                (gnus-summary-tick-article nil t))
7924               (t
7925                (gnus-summary-tick-article)))
7926         (setq count (1+ count))))
7927     ;; Hide killed thread subtrees.  Does not work properly always.
7928     ;;(and (null unmark)
7929     ;;     gnus-thread-hide-killed
7930     ;;     (gnus-summary-hide-thread))
7931     ;; Return number of articles marked as read.
7932     count))
7933
7934 (defun gnus-summary-mark-as-processable (n &optional unmark)
7935   "Set the process mark on the next N articles.
7936 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
7937 the process mark instead.  The difference between N and the actual
7938 number of articles marked is returned."
7939   (interactive "p")
7940   (let ((backward (< n 0))
7941         (n (abs n)))
7942   (while (and 
7943           (> n 0)
7944           (if unmark
7945               (gnus-summary-remove-process-mark (gnus-summary-article-number))
7946             (gnus-summary-set-process-mark (gnus-summary-article-number)))
7947           (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
7948     (setq n (1- n)))
7949   (if (/= 0 n) (message "No more articles"))
7950   (gnus-summary-recenter)
7951   (gnus-summary-position-cursor)
7952   n))
7953
7954 (defun gnus-summary-unmark-as-processable (n)
7955   "Remove the process mark from the next N articles.
7956 If N is negative, mark backward instead.  The difference between N and
7957 the actual number of articles marked is returned."
7958   (interactive "p")
7959   (gnus-summary-mark-as-processable n t))
7960
7961 (defun gnus-summary-unmark-all-processable ()
7962   "Remove the process mark from all articles."
7963   (interactive)
7964   (save-excursion
7965     (while gnus-newsgroup-processable
7966       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
7967   (gnus-summary-position-cursor))
7968
7969 (defun gnus-summary-mark-as-expirable (n)
7970   "Mark N articles forward as expirable.
7971 If N is negative, mark backward instead. The difference between N and
7972 the actual number of articles marked is returned."
7973   (interactive "p")
7974   (gnus-summary-mark-forward n gnus-expirable-mark))
7975
7976 (defun gnus-summary-mark-article-as-replied (article)
7977   "Mark ARTICLE replied and update the summary line."
7978   (setq gnus-newsgroup-replied (cons article gnus-newsgroup-replied))
7979   (let ((buffer-read-only nil))
7980     (if (gnus-summary-goto-subject article)
7981         (progn
7982           (gnus-summary-update-mark gnus-replied-mark 'replied)
7983           t))))
7984
7985 (defun gnus-summary-set-bookmark (article)
7986   "Set a bookmark in current article."
7987   (interactive (list (gnus-summary-article-number)))
7988   (if (or (not (get-buffer gnus-article-buffer))
7989           (not gnus-current-article)
7990           (not gnus-article-current)
7991           (not (equal gnus-newsgroup-name (car gnus-article-current))))
7992       (error "No current article selected"))
7993   ;; Remove old bookmark, if one exists.
7994   (let ((old (assq article gnus-newsgroup-bookmarks)))
7995     (if old (setq gnus-newsgroup-bookmarks 
7996                   (delq old gnus-newsgroup-bookmarks))))
7997   ;; Set the new bookmark, which is on the form 
7998   ;; (article-number . line-number-in-body).
7999   (setq gnus-newsgroup-bookmarks 
8000         (cons 
8001          (cons article 
8002                (save-excursion
8003                  (set-buffer gnus-article-buffer)
8004                  (count-lines
8005                   (min (point)
8006                        (save-excursion
8007                          (goto-char 1)
8008                          (search-forward "\n\n" nil t)
8009                          (point)))
8010                   (point))))
8011          gnus-newsgroup-bookmarks))
8012   (message "A bookmark has been added to the current article."))
8013
8014 (defun gnus-summary-remove-bookmark (article)
8015   "Remove the bookmark from the current article."
8016   (interactive (list (gnus-summary-article-number)))
8017   ;; Remove old bookmark, if one exists.
8018   (let ((old (assq article gnus-newsgroup-bookmarks)))
8019     (if old 
8020         (progn
8021           (setq gnus-newsgroup-bookmarks 
8022                 (delq old gnus-newsgroup-bookmarks))
8023           (message "Removed bookmark."))
8024       (message "No bookmark in current article."))))
8025
8026 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8027 (defun gnus-summary-mark-as-dormant (n)
8028   "Mark N articles forward as dormant.
8029 If N is negative, mark backward instead.  The difference between N and
8030 the actual number of articles marked is returned."
8031   (interactive "p")
8032   (gnus-summary-mark-forward n gnus-dormant-mark))
8033
8034 (defun gnus-summary-set-process-mark (article)
8035   "Set the process mark on ARTICLE and update the summary line."
8036   (setq gnus-newsgroup-processable (cons article gnus-newsgroup-processable))
8037   (let ((buffer-read-only nil))
8038     (if (gnus-summary-goto-subject article)
8039         (progn
8040           (and (eq (gnus-summary-article-mark) ?Z)
8041                (forward-line 1))
8042           (gnus-summary-update-mark gnus-process-mark 'replied)
8043           t))))
8044
8045 (defun gnus-summary-remove-process-mark (article)
8046   "Remove the process mark from ARTICLE and update the summary line."
8047   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8048   (let ((buffer-read-only nil))
8049     (if (gnus-summary-goto-subject article)
8050         (progn
8051           (and (eq (gnus-summary-article-mark) ?Z)
8052                (forward-line 1))
8053           (gnus-summary-update-mark ?  'replied)
8054           (if (memq article gnus-newsgroup-replied) 
8055               (gnus-summary-update-mark gnus-replied-mark 'replied))
8056           t))))
8057
8058 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8059   "Mark N articles as read forwards.
8060 If N is negative, mark backwards instead.
8061 Mark with MARK. If MARK is ? , ?! or ??, articles will be
8062 marked as unread. 
8063 The difference between N and the actual number of articles marked is
8064 returned."
8065   (interactive "p")
8066   (gnus-set-global-variables)
8067   (let ((backward (< n 0))
8068         (n (abs n))
8069         (mark (or mark gnus-del-mark)))
8070   (while (and (> n 0)
8071               (gnus-summary-mark-article nil mark no-expire)
8072               (zerop (gnus-summary-next-subject 
8073                       (if backward -1 1) gnus-summary-goto-unread t)))
8074     (setq n (1- n)))
8075   (if (/= 0 n) (message "No more %sarticles" (if mark "" "unread ")))
8076   (gnus-summary-recenter)
8077   (gnus-summary-position-cursor)
8078   (gnus-set-mode-line 'summary)
8079   n))
8080
8081 (defun gnus-summary-mark-article (&optional article mark no-expire)
8082   "Mark ARTICLE with MARK.
8083 MARK can be any character.
8084 Five MARK strings are reserved: ?  (unread), 
8085 ?! (ticked), ?? (dormant), ?D (read), ?E (expirable).
8086 If MARK is nil, then the default character ?D is used.
8087 If ARTICLE is nil, then the article on the current line will be
8088 marked." 
8089   ;; If no mark is given, then we check auto-expiring.
8090   (and (not no-expire)
8091        gnus-newsgroup-auto-expire 
8092        (or (not mark)
8093            (and (numberp mark) (or (= mark gnus-killed-mark)
8094                                    (= mark gnus-del-mark)
8095                                    (= mark gnus-catchup-mark)
8096                                    (= mark gnus-low-score-mark)
8097                                    (= mark gnus-read-mark))))
8098        (setq mark gnus-expirable-mark))
8099   (let* ((buffer-read-only nil)
8100          (mark (or (and (stringp mark) (aref mark 0)) mark gnus-del-mark))
8101          (article (or article (gnus-summary-article-number))))
8102     (if (or (= mark gnus-unread-mark) 
8103             (= mark gnus-ticked-mark) 
8104             (= mark gnus-dormant-mark))
8105         (gnus-mark-article-as-unread article mark)
8106       (gnus-mark-article-as-read article mark))
8107     (if (gnus-summary-goto-subject article)
8108         (progn
8109           (gnus-summary-show-thread)
8110           (beginning-of-line)
8111           (and (eq (gnus-summary-article-mark) ?Z)
8112                (forward-line 1))
8113           ;; Fix the mark.
8114           (gnus-summary-update-mark mark 'unread)
8115           t))))
8116
8117 (defun gnus-summary-update-mark (mark type)
8118   (beginning-of-line)
8119   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8120         plist)
8121     (if (not forward)
8122         ()
8123       (forward-char forward)
8124       (setq plist (text-properties-at (point)))
8125       (delete-char 1)
8126       (and (memq 'gnus plist) 
8127            (setcar (cdr (car (cdr (memq 'gnus plist)))) mark))
8128       (insert mark)
8129       (and plist (add-text-properties (1- (point)) (point) plist))
8130       (gnus-summary-update-line (eq mark gnus-unread-mark)))))
8131   
8132 (defun gnus-mark-article-as-read (article &optional mark)
8133   "Enter ARTICLE in the pertinent lists and remove it from others."
8134   ;; Make the article expirable.
8135   (let ((mark (or (and (stringp mark) (aref mark 0)) mark gnus-del-mark)))
8136     (if (= mark gnus-expirable-mark)
8137         (setq gnus-newsgroup-expirable (cons article gnus-newsgroup-expirable))
8138       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8139     ;; Remove from unread and marked lists.
8140     (setq gnus-newsgroup-unreads
8141           (delq article gnus-newsgroup-unreads))
8142     (setq gnus-newsgroup-marked
8143           (delq article gnus-newsgroup-marked))
8144     (setq gnus-newsgroup-dormant
8145           (delq article gnus-newsgroup-dormant))))
8146
8147 (defun gnus-mark-article-as-unread (article &optional mark)
8148   "Enter ARTICLE in the pertinent lists and remove it from others."
8149   (let ((mark (or (and (stringp mark) (aref mark 0)) mark gnus-ticked-mark)))
8150     ;; Add to unread list.
8151     (or (memq article gnus-newsgroup-unreads)
8152         (setq gnus-newsgroup-unreads
8153               (cons article gnus-newsgroup-unreads)))
8154     ;; If CLEAR-MARK is non-nil, the article must be removed from marked
8155     ;; list.  Otherwise, it must be added to the list.
8156     (setq gnus-newsgroup-marked
8157           (delq article gnus-newsgroup-marked))
8158     (setq gnus-newsgroup-dormant
8159           (delq article gnus-newsgroup-dormant))
8160     (setq gnus-newsgroup-expirable 
8161           (delq article gnus-newsgroup-expirable))
8162     (if (= mark gnus-ticked-mark)
8163         (setq gnus-newsgroup-marked 
8164               (cons article gnus-newsgroup-marked)))
8165     (if (= mark gnus-dormant-mark)
8166         (setq gnus-newsgroup-dormant 
8167               (cons article gnus-newsgroup-dormant)))))
8168
8169 (defalias 'gnus-summary-mark-as-unread-forward 
8170   'gnus-summary-tick-article-forward)
8171 (make-obsolete 'gnus-summary-mark-as-unread-forward 
8172                'gnus-summary-tick-article-forward)
8173 (defun gnus-summary-tick-article-forward (n)
8174   "Tick N articles forwards.
8175 If N is negative, tick backwards instead.
8176 The difference between N and the number of articles ticked is returned."
8177   (interactive "p")
8178   (gnus-summary-mark-forward n gnus-ticked-mark))
8179
8180 (defalias 'gnus-summary-mark-as-unread-backward 
8181   'gnus-summary-tick-article-backward)
8182 (make-obsolete 'gnus-summary-mark-as-unread-backward 
8183                'gnus-summary-tick-article-backward)
8184 (defun gnus-summary-tick-article-backward (n)
8185   "Tick N articles backwards.
8186 The difference between N and the number of articles ticked is returned."
8187   (interactive "p")
8188   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8189
8190 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8191 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8192 (defun gnus-summary-tick-article (&optional article clear-mark)
8193   "Mark current article as unread.
8194 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8195 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8196   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8197                                        gnus-ticked-mark)))
8198
8199 (defun gnus-summary-mark-as-read-forward (n)
8200   "Mark N articles as read forwards.
8201 If N is negative, mark backwards instead.
8202 The difference between N and the actual number of articles marked is
8203 returned."
8204   (interactive "p")
8205   (gnus-summary-mark-forward n gnus-del-mark t))
8206
8207 (defun gnus-summary-mark-as-read-backward (n)
8208   "Mark the N articles as read backwards.
8209 The difference between N and the actual number of articles marked is
8210 returned."
8211   (interactive "p")
8212   (gnus-summary-mark-forward (- n) gnus-del-mark t))
8213
8214 (defun gnus-summary-mark-as-read (&optional article mark)
8215   "Mark current article as read.
8216 ARTICLE specifies the article to be marked as read.
8217 MARK specifies a string to be inserted at the beginning of the line."
8218   (gnus-summary-mark-article article mark))
8219
8220 (defun gnus-summary-clear-mark-forward (n)
8221   "Clear marks from N articles forward.
8222 If N is negative, clear backward instead.
8223 The difference between N and the number of marks cleared is returned."
8224   (interactive "p")
8225   (gnus-summary-mark-forward n gnus-unread-mark))
8226
8227 (defun gnus-summary-clear-mark-backward (n)
8228   "Clear marks from N articles backward.
8229 The difference between N and the number of marks cleared is returned."
8230   (interactive "p")
8231   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8232
8233 (defun gnus-summary-mark-unread-as-read ()
8234   "Intended to be used by `gnus-summary-mark-article-hook'."
8235   (or (memq gnus-current-article gnus-newsgroup-marked)
8236       (memq gnus-current-article gnus-newsgroup-dormant)
8237       (memq gnus-current-article gnus-newsgroup-expirable)
8238       (gnus-summary-mark-as-read gnus-current-article gnus-read-mark)))
8239
8240 (defun gnus-summary-mark-region-as-read (point mark all)
8241   "Mark all unread articles between point and mark as read.
8242 If given a prefix, mark all articles between point and mark as read,
8243 even ticked and dormant ones."
8244   (interactive "r\nP")
8245   (save-excursion
8246     (goto-char point)
8247     (beginning-of-line)
8248     (while (and 
8249             (< (point) mark)
8250             (progn
8251               (and
8252                (or all
8253                    (and
8254                     (not (memq (gnus-summary-article-number)
8255                                gnus-newsgroup-marked))
8256                     (not (memq (gnus-summary-article-number)
8257                                gnus-newsgroup-dormant))))
8258                (gnus-summary-mark-article
8259                 (gnus-summary-article-number) gnus-del-mark))
8260               t)
8261             (zerop (forward-line 1))))))
8262
8263 ;; Fix by Per Abrahamsen <amanda@iesd.auc.dk>.
8264 (defalias 'gnus-summary-delete-marked-as-read 
8265   'gnus-summary-remove-lines-marked-as-read)
8266 (make-obsolete 'gnus-summary-delete-marked-as-read 
8267                'gnus-summary-remove-lines-marked-as-read)
8268 (defun gnus-summary-remove-lines-marked-as-read ()
8269   "Remove lines that are marked as read."
8270   (interactive)
8271   (gnus-summary-remove-lines-marked-with 
8272    (concat (mapconcat
8273             (lambda (char) (char-to-string (symbol-value char)))
8274             '(gnus-del-mark gnus-read-mark
8275               gnus-killed-mark gnus-kill-file-mark
8276               gnus-low-score-mark gnus-expirable-mark)
8277             ""))))
8278
8279 (defalias 'gnus-summary-delete-marked-with 
8280   'gnus-summary-remove-lines-marked-with)
8281 (make-obsolete 'gnus-summary-delete-marked-with 
8282                'gnus-summary-remove-lines-marked-with)
8283 ;; Rewrite by Daniel Quinlan <quinlan@best.com>.
8284 (defun gnus-summary-remove-lines-marked-with (marks)
8285   "Remove lines that are marked with MARKS (e.g. \"DK\")."
8286   (interactive "sMarks: ")
8287   ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
8288   (save-excursion
8289     (set-buffer gnus-summary-buffer)
8290     (let ((buffer-read-only nil)
8291           (marks (concat "^[" marks "]")))
8292       (goto-char (point-min))
8293       (if gnus-use-adaptive-scoring
8294           (gnus-score-remove-lines-adaptive marks)
8295         (while (re-search-forward marks (point-max) t)
8296           (beginning-of-line)
8297           (delete-region (point)
8298                          (progn (forward-line 1) (point))))))
8299     (or (zerop (buffer-size))
8300         (if (eobp)
8301             (gnus-summary-prev-subject 1)
8302           (gnus-summary-position-cursor)))))
8303
8304 (defun gnus-summary-expunge-below (score)
8305   "Remove articles with score less than SCORE."
8306   (interactive "P")
8307   (setq score (if score
8308                   (prefix-numeric-value score)
8309                 (or gnus-summary-default-score 0)))
8310   (save-excursion
8311     (set-buffer gnus-summary-buffer)
8312     (goto-char (point-min))
8313     (let ((buffer-read-only nil)
8314           beg)
8315       (while (not (eobp))
8316         (if (< (gnus-summary-article-score) score)
8317             (progn
8318               (setq beg (point))
8319               (forward-line 1)
8320               (delete-region beg (point)))
8321           (forward-line 1)))
8322       ;; Adjust point.
8323       (or (zerop (buffer-size))
8324           (if (eobp)
8325               (gnus-summary-prev-subject 1)
8326             (gnus-summary-position-cursor))))))
8327
8328 (defun gnus-summary-mark-below (score mark)
8329   "Mark articles with score less than SCORE with MARK."
8330   (interactive "P\ncMark: ")
8331   (setq score (if score
8332                   (prefix-numeric-value score)
8333                 (or gnus-summary-default-score 0)))
8334   (save-excursion
8335     (set-buffer gnus-summary-buffer)
8336     (goto-char (point-min))
8337     (while (not (eobp))
8338       (and (< (gnus-summary-article-score) score)
8339            (gnus-summary-mark-article nil mark))
8340       (forward-line 1))))
8341
8342 (defun gnus-summary-kill-below (score)
8343   "Mark articles with score below SCORE as read."
8344   (interactive "P")
8345   (gnus-summary-mark-below score gnus-killed-mark))
8346
8347 (defun gnus-summary-clear-above (score)
8348   "Clear all marks from articles with score above SCORE."
8349   (interactive "P")
8350   (gnus-summary-mark-above score gnus-unread-mark))
8351
8352 (defun gnus-summary-tick-above (score)
8353   "Tick all articles with score above SCORE."
8354   (interactive "P")
8355   (gnus-summary-mark-above score gnus-ticked-mark))
8356
8357 (defun gnus-summary-mark-above (score mark)
8358   "Mark articles with score over SCORE with MARK."
8359   (interactive "P\ncMark: ")
8360   (setq score (if score
8361                   (prefix-numeric-value score)
8362                 (or gnus-summary-default-score 0)))
8363   (save-excursion
8364     (set-buffer gnus-summary-buffer)
8365     (goto-char (point-min))
8366     (while (not (eobp))
8367       (if (> (gnus-summary-article-score) score)
8368           (progn
8369             (gnus-summary-mark-article nil mark)
8370             (forward-line 1))
8371         (forward-line 1)))))
8372
8373 ;; Suggested by Daniel Quinlan <quinlan@best.com>.  
8374 (defun gnus-summary-show-all-expunged ()
8375   "Display all the hidden articles that were expunged for low scores."
8376   (interactive)
8377   (let ((buffer-read-only nil))
8378     (let ((scored gnus-newsgroup-scored)
8379           headers h)
8380       (while scored
8381         (or (gnus-summary-goto-subject (car (car scored)))
8382             (and (setq h (gnus-get-header-by-number (car (car scored))))
8383                  (< (cdr (car scored)) gnus-summary-expunge-below)
8384                  (setq headers (cons h headers))))
8385         (setq scored (cdr scored)))
8386       (or headers (error "No expunged articles hidden."))
8387       (goto-char (point-min))
8388       (save-excursion 
8389         (gnus-summary-prepare-threads (nreverse headers) 0)))
8390     (goto-char (point-min))
8391     (gnus-summary-position-cursor)))
8392
8393 (defun gnus-summary-show-all-dormant ()
8394   "Display all the hidden articles that are marked as dormant."
8395   (interactive)
8396   (let ((buffer-read-only nil))
8397     (goto-char (point-min))
8398     (let ((dormant gnus-newsgroup-dormant)
8399           headers h)
8400       (while dormant
8401         (or (gnus-summary-goto-subject (car dormant))
8402             (and (setq h (gnus-get-header-by-number (car dormant)))
8403                  (setq headers (cons h headers))))
8404         (setq dormant (cdr dormant)))
8405       (or headers (error "No dormant articles hidden."))
8406       (save-excursion (gnus-summary-prepare-threads (nreverse headers) 0)))
8407     (goto-char (point-min))
8408     (gnus-summary-position-cursor)))
8409
8410 (defun gnus-summary-hide-all-dormant ()
8411   "Hide all dormant articles."
8412   (interactive)
8413   (gnus-summary-remove-lines-marked-with (char-to-string gnus-dormant-mark))
8414   (gnus-summary-position-cursor))
8415
8416 (defun gnus-summary-catchup (all &optional quietly to-here)
8417   "Mark all articles not marked as unread in this newsgroup as read.
8418 If prefix argument ALL is non-nil, all articles are marked as read.
8419 If QUIETLY is non-nil, no questions will be asked.
8420 If TO-HERE is non-nil, it should be a point in the buffer. All
8421 articles before this point will be marked as read.
8422 The number of articles marked as read is returned."
8423   (interactive "P")
8424   (prog1
8425       (if (or quietly
8426               (not gnus-interactive-catchup) ;Without confirmation?
8427               gnus-expert-user
8428               (gnus-y-or-n-p
8429                (if all
8430                    "Mark absolutely all articles as read? "
8431                  "Mark all unread articles as read? ")))
8432           (let ((unreads (length gnus-newsgroup-unreads)))
8433             (if (gnus-summary-first-subject (not all))
8434                 (while (and (gnus-summary-mark-as-read nil gnus-catchup-mark)
8435                             (if to-here (< (point) to-here) t)
8436                             (gnus-summary-search-subject nil (not all)))))
8437             (- unreads (length gnus-newsgroup-unreads))
8438             (or to-here
8439                 (setq gnus-newsgroup-unreads gnus-newsgroup-marked))))
8440     (gnus-summary-position-cursor)))
8441
8442 (defun gnus-summary-catchup-to-here (&optional all)
8443   "Mark all unticked articles before the current one as read.
8444 If ALL is non-nil, also mark ticked and dormant articles as read."
8445   (interactive)
8446   (beginning-of-line)
8447   (gnus-summary-catchup all t (point))
8448   (gnus-summary-position-cursor))
8449
8450 (defun gnus-summary-catchup-all (&optional quietly)
8451   "Mark all articles in this newsgroup as read."
8452   (interactive)
8453   (gnus-summary-catchup t quietly))
8454
8455 (defun gnus-summary-catchup-and-exit (all &optional quietly)
8456   "Mark all articles not marked as unread in this newsgroup as read, then exit.
8457 If prefix argument ALL is non-nil, all articles are marked as read."
8458   (interactive "P")
8459   (gnus-summary-catchup all quietly)
8460   ;; Select next newsgroup or exit.
8461   (if (eq gnus-auto-select-next 'quietly)
8462       (gnus-summary-next-group nil)
8463     (gnus-summary-exit)))
8464
8465 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8466   "Mark all articles in this newsgroup as read, and then exit."
8467   (interactive)
8468   (gnus-summary-catchup-and-exit t quietly))
8469
8470 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
8471 (defun gnus-summary-catchup-and-goto-next-group (all)
8472   "Mark all articles in this group as read and select the next group.
8473 If given a prefix, mark all articles, unread as well as ticked, as
8474 read." 
8475   (interactive "P")
8476   (gnus-summary-catchup all)
8477   (gnus-summary-next-group))
8478
8479 ;; Thread-based commands.
8480
8481 (defun gnus-summary-toggle-threads (arg)
8482   "Toggle showing conversation threads.
8483 If ARG is positive number, turn showing conversation threads on."
8484   (interactive "P")
8485   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
8486     (setq gnus-show-threads
8487           (if (null arg) (not gnus-show-threads)
8488             (> (prefix-numeric-value arg) 0)))
8489     (gnus-summary-prepare)
8490     (gnus-summary-goto-subject current)
8491     (gnus-summary-position-cursor)))
8492
8493 (defun gnus-summary-show-all-threads ()
8494   "Show all threads."
8495   (interactive)
8496   (save-excursion
8497     (let ((buffer-read-only nil))
8498       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
8499   (gnus-summary-position-cursor))
8500
8501 (defun gnus-summary-show-thread ()
8502   "Show thread subtrees.
8503 Returns nil if no thread was there to be shown."
8504   (interactive)
8505   (prog1
8506       (save-excursion
8507         (let ((buffer-read-only nil)
8508               ;; first goto end then to beg, to have point at beg after let
8509               (end (progn (end-of-line) (point)))
8510               (beg (progn (beginning-of-line) (point))))
8511           (prog1
8512               ;; Any hidden lines here?
8513               (search-forward "\r" end t)
8514             (subst-char-in-region beg end ?\^M ?\n t))))
8515     (gnus-summary-position-cursor)))
8516
8517 (defun gnus-summary-hide-all-threads ()
8518   "Hide all thread subtrees."
8519   (interactive)
8520   (save-excursion
8521     (goto-char (point-min))
8522     (gnus-summary-hide-thread)
8523     (while (and (not (eobp)) (zerop (forward-line 1)))
8524       (gnus-summary-hide-thread)))
8525   (gnus-summary-position-cursor))
8526
8527 (defun gnus-summary-hide-thread ()
8528   "Hide thread subtrees.
8529 Returns nil if no threads were there to be hidden."
8530   (interactive)
8531   (let ((buffer-read-only nil)
8532         (start (point))
8533         (level (gnus-summary-thread-level))
8534         (end (point)))
8535     ;; Go forward until either the buffer ends or the subthread
8536     ;; ends. 
8537     (if (eobp)
8538         ()
8539       (while (and (zerop (forward-line 1))
8540                   (> (gnus-summary-thread-level) level))
8541         (setq end (point)))
8542       (prog1
8543           (save-excursion
8544             (goto-char end)
8545             (search-backward "\n" start t))
8546         (subst-char-in-region start end ?\n ?\^M t)
8547         (forward-line -1)))))
8548
8549 (defun gnus-summary-go-to-next-thread (&optional previous)
8550   "Go to the same level (or less) next thread.
8551 If PREVIOUS is non-nil, go to previous thread instead.
8552 Return the article number moved to, or nil if moving was impossible."
8553   (let ((level (gnus-summary-thread-level))
8554         (article (gnus-summary-article-number)))
8555     (if previous 
8556         (while (and (zerop (gnus-summary-prev-subject 1))
8557                     (> (gnus-summary-thread-level) level)))
8558       (while (and (zerop (gnus-summary-next-subject 1))
8559                   (> (gnus-summary-thread-level) level))))
8560     (gnus-summary-recenter)
8561     (gnus-summary-position-cursor)
8562     (let ((oart (gnus-summary-article-number)))
8563       (and (/= oart article) oart))))
8564
8565 (defun gnus-summary-next-thread (n)
8566   "Go to the same level next N'th thread.
8567 If N is negative, search backward instead.
8568 Returns the difference between N and the number of skips actually
8569 done."
8570   (interactive "p")
8571   (let ((backward (< n 0))
8572         (n (abs n)))
8573   (while (and (> n 0)
8574               (gnus-summary-go-to-next-thread backward))
8575     (setq n (1- n)))
8576   (gnus-summary-position-cursor)
8577   (if (/= 0 n) (message "No more threads"))
8578   n))
8579
8580 (defun gnus-summary-prev-thread (n)
8581   "Go to the same level previous N'th thread.
8582 Returns the difference between N and the number of skips actually
8583 done."
8584   (interactive "p")
8585   (gnus-summary-next-thread (- n)))
8586
8587 (defun gnus-summary-go-down-thread (&optional same)
8588   "Go down one level in the current thread.
8589 If SAME is non-nil, also move to articles of the same level."
8590   (let ((level (gnus-summary-thread-level))
8591         (start (point)))
8592     (if (and (zerop (forward-line 1))
8593              (> (gnus-summary-thread-level) level))
8594         t
8595       (goto-char start)
8596       nil)))
8597
8598 (defun gnus-summary-go-up-thread ()
8599   "Go up one level in the current thread."
8600   (let ((level (gnus-summary-thread-level))
8601         (start (point)))
8602     (while (and (zerop (forward-line -1))
8603                 (>= (gnus-summary-thread-level) level)))
8604     (if (>= (gnus-summary-thread-level) level)
8605         (progn
8606           (goto-char start)
8607           nil)
8608       t)))
8609
8610 (defun gnus-summary-down-thread (n)
8611   "Go down thread N steps.
8612 If N is negative, go up instead.
8613 Returns the difference between N and how many steps down that were
8614 taken."
8615   (interactive "p")
8616   (let ((up (< n 0))
8617         (n (abs n)))
8618   (while (and (> n 0)
8619               (if up (gnus-summary-go-up-thread)
8620                 (gnus-summary-go-down-thread)))
8621     (setq n (1- n)))
8622   (gnus-summary-position-cursor)
8623   (if (/= 0 n) (message "Can't go further"))
8624   n))
8625
8626 (defun gnus-summary-up-thread (n)
8627   "Go up thread N steps.
8628 If N is negative, go up instead.
8629 Returns the difference between N and how many steps down that were
8630 taken."
8631   (interactive "p")
8632   (gnus-summary-down-thread (- n)))
8633
8634 (defun gnus-summary-kill-thread (unmark)
8635   "Mark articles under current thread as read.
8636 If the prefix argument is positive, remove any kinds of marks.
8637 If the prefix argument is negative, tick articles instead."
8638   (interactive "P")
8639   (if unmark
8640       (setq unmark (prefix-numeric-value unmark)))
8641   (let ((killing t)
8642         (level (gnus-summary-thread-level)))
8643     (save-excursion
8644       (while killing
8645         ;; Mark the article...
8646         (cond ((null unmark) (gnus-summary-mark-as-read nil gnus-killed-mark))
8647               ((> unmark 0) (gnus-summary-tick-article nil t))
8648               (t (gnus-summary-tick-article)))
8649         ;; ...and go forward until either the buffer ends or the subtree
8650         ;; ends. 
8651         (if (not (and (zerop (forward-line 1))
8652                       (> (gnus-summary-thread-level) level)))
8653             (setq killing nil))))
8654     ;; Hide killed subtrees.
8655     (and (null unmark)
8656          gnus-thread-hide-killed
8657          (gnus-summary-hide-thread))
8658     ;; If marked as read, go to next unread subject.
8659     (if (null unmark)
8660         ;; Go to next unread subject.
8661         (gnus-summary-next-subject 1 t)))
8662   (gnus-set-mode-line 'summary))
8663
8664 ;; Summary sorting commands
8665
8666 (defun gnus-summary-sort-by-number (reverse)
8667   "Sort summary buffer by article number.
8668 Argument REVERSE means reverse order."
8669   (interactive "P")
8670   (gnus-summary-sort 
8671    (cons 'gnus-summary-article-number 'gnus-thread-sort-by-number) reverse))
8672
8673 (defun gnus-summary-sort-by-author (reverse)
8674   "Sort summary buffer by author name alphabetically.
8675 If case-fold-search is non-nil, case of letters is ignored.
8676 Argument REVERSE means reverse order."
8677   (interactive "P")
8678   (gnus-summary-sort
8679    (cons
8680     (lambda ()
8681       (let ((extract (funcall
8682                       gnus-extract-address-components
8683                       (header-from (gnus-get-header-by-number
8684                                     (gnus-summary-article-number))))))
8685         (or (car extract) (cdr extract))))
8686     'gnus-thread-sort-by-author)
8687    reverse))
8688
8689 (defun gnus-summary-sort-by-subject (reverse)
8690   "Sort summary buffer by subject alphabetically. `Re:'s are ignored.
8691 If case-fold-search is non-nil, case of letters is ignored.
8692 Argument REVERSE means reverse order."
8693   (interactive "P")
8694   (gnus-summary-sort
8695    (cons
8696     (lambda ()
8697       (downcase (gnus-simplify-subject (gnus-summary-subject-string))))
8698     'gnus-thread-sort-by-subject)
8699    reverse))
8700
8701 (defun gnus-summary-sort-by-date (reverse)
8702   "Sort summary buffer by date.
8703 Argument REVERSE means reverse order."
8704   (interactive "P")
8705   (gnus-summary-sort
8706    (cons
8707     (lambda ()
8708       (gnus-sortable-date
8709        (header-date 
8710         (gnus-get-header-by-number (gnus-summary-article-number)))))
8711     'gnus-thread-sort-by-date)
8712    reverse))
8713
8714 (defun gnus-summary-sort-by-score (reverse)
8715   "Sort summary buffer by score.
8716 Argument REVERSE means reverse order."
8717   (interactive "P")
8718   (gnus-summary-sort 
8719    (cons 'gnus-summary-article-score 'gnus-thread-sort-by-score)
8720    (not reverse)))
8721
8722 (defvar gnus-summary-already-sorted nil)
8723 (defun gnus-summary-sort (predicate reverse)
8724   ;; Sort summary buffer by PREDICATE.  REVERSE means reverse order. 
8725   (if gnus-summary-already-sorted
8726       ()
8727     (let (buffer-read-only)
8728       (if (not gnus-show-threads)
8729           (progn
8730             (goto-char (point-min))
8731             (sort-subr reverse 'forward-line 'end-of-line (car predicate)))
8732         (let ((gnus-thread-sort-functions (list (cdr predicate)))
8733               (gnus-summary-already-sorted nil))
8734           (gnus-summary-prepare))))))
8735   
8736 (defun gnus-sortable-date (date)
8737   "Make sortable string by string-lessp from DATE.
8738 Timezone package is used."
8739   (let* ((date   (timezone-fix-time date nil nil)) ;[Y M D H M S]
8740          (year   (aref date 0))
8741          (month  (aref date 1))
8742          (day    (aref date 2)))
8743     (timezone-make-sortable-date 
8744      year month day 
8745      (timezone-make-time-string
8746       (aref date 3) (aref date 4) (aref date 5)))))
8747
8748
8749 ;; Summary saving commands.
8750
8751 (defun gnus-summary-save-article (n)
8752   "Save the current article using the default saver function.
8753 If N is a positive number, save the N next articles.
8754 If N is a negative number, save the N previous articles.
8755 If N is nil and any articles have been marked with the process mark,
8756 save those articles instead.
8757 The variable `gnus-default-article-saver' specifies the saver function."
8758   (interactive "P")
8759   (let (articles process)
8760     (if (and n (numberp n))
8761         (let ((backward (< n 0))
8762               (n (abs n)))
8763           (save-excursion
8764             (while (and (> n 0)
8765                         (setq articles (cons (gnus-summary-article-number) 
8766                                              articles))
8767                         (gnus-summary-search-forward nil nil backward))
8768               (setq n (1- n))))
8769           (setq articles (sort articles (function <))))
8770       (if gnus-newsgroup-processable
8771           (progn
8772             (setq articles (setq gnus-newsgroup-processable
8773                                  (nreverse gnus-newsgroup-processable)))
8774             (setq process t))
8775         (setq articles (list (gnus-summary-article-number)))))
8776     (while articles
8777       (let ((header (gnus-gethash (int-to-string (car articles))
8778                                   gnus-newsgroup-headers-hashtb-by-number)))
8779         (if (vectorp header)
8780             (progn
8781               (gnus-summary-select-article t nil nil (car articles))
8782               (or gnus-save-all-headers
8783                   (gnus-article-hide-headers t))
8784               ;; Remove any X-Gnus lines.
8785               (save-excursion
8786                 (save-restriction
8787                   (set-buffer gnus-article-buffer)
8788                   (let ((buffer-read-only nil))
8789                     (goto-char (point-min))
8790                     (narrow-to-region (point) (or (search-forward "\n\n" nil t)
8791                                                   (point-max)))
8792                     (while (re-search-forward "^X-Gnus" nil t)
8793                       (beginning-of-line)
8794                       (delete-region (point)
8795                                      (progn (forward-line 1) (point))))
8796                     (widen))))
8797               (if gnus-default-article-saver
8798                   (funcall gnus-default-article-saver)
8799                 (error "No default saver is defined.")))
8800           (if (assq 'name header)
8801               (gnus-copy-file (cdr (assq 'name header)))
8802             (message "Article %d is unsaveable" (car articles)))))
8803       (if process
8804           (gnus-summary-remove-process-mark (car articles)))
8805       (setq articles (cdr articles)))
8806     (if process (setq gnus-newsgroup-processable 
8807                       (nreverse gnus-newsgroup-processable)))
8808     (gnus-summary-position-cursor)
8809     n))
8810
8811 (defun gnus-summary-pipe-output (arg)
8812   "Pipe the current article to a subprocess.
8813 If N is a positive number, pipe the N next articles.
8814 If N is a negative number, pipe the N previous articles.
8815 If N is nil and any articles have been marked with the process mark,
8816 pipe those articles instead."
8817   (interactive "P")
8818   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
8819     (gnus-summary-save-article arg)))
8820
8821 (defun gnus-summary-save-article-mail (arg)
8822   "Append the current article to an mail file.
8823 If N is a positive number, save the N next articles.
8824 If N is a negative number, save the N previous articles.
8825 If N is nil and any articles have been marked with the process mark,
8826 save those articles instead."
8827   (interactive "P")
8828   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
8829     (gnus-summary-save-article arg)))
8830
8831 (defun gnus-summary-save-article-rmail (arg)
8832   "Append the current article to an rmail file.
8833 If N is a positive number, save the N next articles.
8834 If N is a negative number, save the N previous articles.
8835 If N is nil and any articles have been marked with the process mark,
8836 save those articles instead."
8837   (interactive "P")
8838   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
8839     (gnus-summary-save-article arg)))
8840
8841 (defun gnus-summary-save-article-file (arg)
8842   "Append the current article to a file.
8843 If N is a positive number, save the N next articles.
8844 If N is a negative number, save the N previous articles.
8845 If N is nil and any articles have been marked with the process mark,
8846 save those articles instead."
8847   (interactive "P")
8848   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
8849     (gnus-summary-save-article arg)))
8850
8851 (defun gnus-summary-save-article-folder (arg)
8852   "Append the current article to an mh folder.
8853 If N is a positive number, save the N next articles.
8854 If N is a negative number, save the N previous articles.
8855 If N is nil and any articles have been marked with the process mark,
8856 save those articles instead."
8857   (interactive "P")
8858   (let ((gnus-default-article-saver 'gnus-summary-save-in-folder))
8859     (gnus-summary-save-article arg)))
8860
8861 (defun gnus-read-save-file-name (prompt default-name)
8862   (let ((methods gnus-split-methods)
8863         split-name)
8864     (if (not gnus-split-methods)
8865         ()
8866       (save-excursion
8867         (set-buffer gnus-article-buffer)
8868         (gnus-narrow-to-headers)
8869         (while methods
8870           (goto-char (point-min))
8871           (and (condition-case () 
8872                    (re-search-forward (car (car methods)) nil t)
8873                  (error nil))
8874                (setq split-name (cons (nth 1 (car methods)) split-name)))
8875           (setq methods (cdr methods)))
8876         (widen)))
8877     (cond ((null split-name)
8878            (read-file-name
8879             (concat prompt " (default "
8880                     (file-name-nondirectory default-name) ") ")
8881             (file-name-directory default-name)
8882             default-name))
8883           ((= 1 (length split-name))
8884            (read-file-name
8885             (concat prompt " (default " (car split-name) ") ")
8886             gnus-article-save-directory
8887             (concat gnus-article-save-directory (car split-name))))
8888           (t
8889            (setq split-name (mapcar (lambda (el) (list el))
8890                                     (nreverse split-name)))
8891            (let ((result (completing-read 
8892                           (concat prompt " ")
8893                           split-name nil nil)))
8894              (concat gnus-article-save-directory
8895                      (if (string= result "")
8896                          (car (car split-name))
8897                        result)))))))
8898
8899 (defun gnus-summary-save-in-rmail (&optional filename)
8900   "Append this article to Rmail file.
8901 Optional argument FILENAME specifies file name.
8902 Directory to save to is default to `gnus-article-save-directory' which
8903 is initialized from the SAVEDIR environment variable."
8904   (interactive)
8905   (let ((default-name
8906           (funcall gnus-rmail-save-name gnus-newsgroup-name
8907                    gnus-current-headers gnus-newsgroup-last-rmail)))
8908     (or filename
8909         (setq filename (gnus-read-save-file-name 
8910                         "Save in rmail file:" 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          (gnus-output-to-rmail filename))))
8918     ;; Remember the directory name to save articles.
8919     (setq gnus-newsgroup-last-rmail filename)))
8920
8921 (defun gnus-summary-save-in-mail (&optional filename)
8922   "Append this article to Unix mail file.
8923 Optional argument FILENAME specifies file name.
8924 Directory to save to is default to `gnus-article-save-directory' which
8925 is initialized from the SAVEDIR environment variable."
8926   (interactive)
8927   (let ((default-name
8928           (funcall gnus-mail-save-name gnus-newsgroup-name
8929                    gnus-current-headers gnus-newsgroup-last-mail)))
8930     (or filename
8931         (setq filename (gnus-read-save-file-name 
8932                         "Save in Unix mail file:" default-name)))
8933     (setq filename
8934           (expand-file-name filename
8935                             (and default-name
8936                                  (file-name-directory default-name))))
8937     (gnus-make-directory (file-name-directory filename))
8938     (gnus-eval-in-buffer-window 
8939      gnus-article-buffer
8940      (save-excursion
8941        (save-restriction
8942          (widen)
8943          (if (and (file-readable-p filename) (rmail-file-p filename))
8944              (gnus-output-to-rmail filename)
8945            (rmail-output filename 1 t t)))))
8946     ;; Remember the directory name to save articles.
8947     (setq gnus-newsgroup-last-mail filename)))
8948
8949 (defun gnus-summary-save-in-file (&optional filename)
8950   "Append this article to file.
8951 Optional argument FILENAME specifies file name.
8952 Directory to save to is default to `gnus-article-save-directory' which
8953 is initialized from the SAVEDIR environment variable."
8954   (interactive)
8955   (let ((default-name
8956           (funcall gnus-file-save-name gnus-newsgroup-name
8957                    gnus-current-headers gnus-newsgroup-last-file)))
8958     (or filename
8959         (setq filename (gnus-read-save-file-name 
8960                         "Save in file:" default-name)))
8961     (gnus-make-directory (file-name-directory filename))
8962     (gnus-eval-in-buffer-window 
8963      gnus-article-buffer
8964      (save-excursion
8965        (save-restriction
8966          (widen)
8967          (gnus-output-to-file filename))))
8968     ;; Remember the directory name to save articles.
8969     (setq gnus-newsgroup-last-file filename)))
8970
8971 (defun gnus-summary-save-in-pipe (&optional command)
8972   "Pipe this article to subprocess."
8973   (interactive)
8974   (let ((command (read-string "Shell command on article: "
8975                               gnus-last-shell-command)))
8976     (if (string-equal command "")
8977         (setq command gnus-last-shell-command))
8978     (gnus-eval-in-buffer-window 
8979      gnus-article-buffer
8980      (save-restriction
8981        (widen)
8982        (shell-command-on-region (point-min) (point-max) command nil)))
8983     (setq gnus-last-shell-command command)))
8984
8985 ;; Summary extract commands
8986
8987 (defun gnus-summary-insert-pseudos (pslist)
8988   (let ((buffer-read-only nil)
8989         (article (gnus-summary-article-number))
8990         b)
8991     (or (gnus-summary-goto-subject article)
8992         (error (format "No such article: %d" article)))
8993     (or gnus-newsgroup-headers-hashtb-by-number
8994         (gnus-make-headers-hashtable-by-number))
8995     (gnus-summary-position-cursor)
8996     ;; If all commands are to be bunched up on one line, we collect
8997     ;; them here.  
8998     (if gnus-view-pseudos-separately
8999         ()
9000       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9001             files action)
9002         (while ps
9003           (setq action (cdr (assq 'action (car ps))))
9004           (setq files (list (cdr (assq 'name (car ps)))))
9005           (while (and ps (cdr ps)
9006                       (string= (or action "1")
9007                                (or (cdr (assq 'action (car (cdr ps)))) "2")))
9008             (setq files (cons (cdr (assq 'name (car (cdr ps)))) files))
9009             (setcdr ps (cdr (cdr ps))))
9010           (if (not files)
9011               ()
9012             (if (not (string-match "%s" action))
9013                 (setq files (cons " " files)))
9014             (setq files (cons " " files))
9015             (and (assq 'execute (car ps))
9016                  (setcdr (assq 'execute (car ps))
9017                          (funcall (if (string-match "%s" action)
9018                                       'format 'concat)
9019                                   action 
9020                                   (mapconcat (lambda (f) f) files " ")))))
9021           (setq ps (cdr ps)))))
9022     (if gnus-view-pseudos
9023         (while pslist
9024           (and (assq 'execute (car pslist))
9025                (gnus-execute-command (cdr (assq 'execute (car pslist)))
9026                                      (eq gnus-view-pseudos 'not-confirm)))
9027           (setq pslist (cdr pslist)))
9028       (save-excursion
9029         (while pslist
9030           (gnus-summary-goto-subject (or (cdr (assq 'article (car pslist)))
9031                                          (gnus-summary-article-number)))
9032           (forward-line 1)
9033           (setq b (point))
9034           (insert "          " (file-name-nondirectory 
9035                                 (cdr (assq 'name (car pslist))))
9036                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9037           (add-text-properties 
9038            b (1+ b) (list 'gnus (list gnus-reffed-article-number
9039                                       gnus-unread-mark 0)
9040                           'gnus-pseudo (car pslist)))
9041           (forward-line -1)
9042           (gnus-sethash (int-to-string gnus-reffed-article-number)
9043                         (car pslist) gnus-newsgroup-headers-hashtb-by-number)
9044           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9045           (setq pslist (cdr pslist)))))))
9046
9047 (defun gnus-pseudos< (p1 p2)
9048   (let ((c1 (cdr (assq 'action p1)))
9049         (c2 (cdr (assq 'action p2))))
9050     (and c1 c2 (string< c1 c2))))
9051
9052 (defun gnus-request-pseudo-article (props)
9053   (cond ((assq 'execute props)
9054          (gnus-execute-command (cdr (assq 'execute props)))))
9055   (let ((gnus-current-article (gnus-summary-article-number)))
9056     (run-hooks 'gnus-mark-article-hook)))
9057
9058 (defun gnus-execute-command (command &optional automatic)
9059   (save-excursion
9060     (gnus-article-setup-buffer)
9061     (set-buffer gnus-article-buffer)
9062     (let ((command (if automatic command (read-string "Command: " command)))
9063           (buffer-read-only nil))
9064       (erase-buffer)
9065       (insert "$ " command "\n\n")
9066       (if gnus-view-pseudo-asynchronously
9067           (start-process "gnus-execute" nil "sh" "-c" command)
9068         (call-process "sh" nil t nil "-c" command)))))
9069
9070 (defun gnus-copy-file (file &optional to)
9071   "Copy FILE to TO."
9072   (interactive
9073    (list (read-file-name "Copy file: " default-directory)
9074          (read-file-name "Copy file to: " default-directory)))
9075   (or to (setq to (read-file-name "Copy file to: " default-directory)))
9076   (and (file-directory-p to) 
9077        (setq to (concat (file-name-as-directory to)
9078                         (file-name-nondirectory file))))
9079   (copy-file file to))
9080
9081 ;; Summary kill commands.
9082
9083 (defun gnus-summary-edit-global-kill (article)
9084   "Edit the \"global\" kill file."
9085   (interactive (list (gnus-summary-article-number)))
9086   (gnus-group-edit-global-kill article))
9087
9088 (defun gnus-summary-edit-local-kill ()
9089   "Edit a local kill file applied to the current newsgroup."
9090   (interactive)
9091   (setq gnus-current-headers 
9092         (gnus-gethash 
9093          (int-to-string (gnus-summary-article-number))
9094          gnus-newsgroup-headers-hashtb-by-number))
9095   (gnus-set-global-variables)
9096   (gnus-group-edit-local-kill 
9097    (gnus-summary-article-number) gnus-newsgroup-name))
9098
9099 \f
9100 ;;;
9101 ;;; Gnus article mode
9102 ;;;
9103
9104 (put 'gnus-article-mode 'mode-class 'special)
9105
9106 (if gnus-article-mode-map
9107     nil
9108   (setq gnus-article-mode-map (make-keymap))
9109   (suppress-keymap gnus-article-mode-map)
9110   (define-key gnus-article-mode-map " " 'gnus-article-next-page)
9111   (define-key gnus-article-mode-map "\177" 'gnus-article-prev-page)
9112   (define-key gnus-article-mode-map "\C-c^" 'gnus-article-refer-article)
9113   (define-key gnus-article-mode-map "h" 'gnus-article-show-summary)
9114   (define-key gnus-article-mode-map "s" 'gnus-article-show-summary)
9115   (define-key gnus-article-mode-map "\C-c\C-m" 'gnus-article-mail)
9116   (define-key gnus-article-mode-map "\C-c\C-M" 'gnus-article-mail-with-original)
9117   (define-key gnus-article-mode-map "?" 'gnus-article-describe-briefly)
9118   
9119   ;; Duplicate almost all summary keystrokes in the article mode map.
9120   (let ((commands 
9121          (list "#" "\M-#" "\C-c\M-#" "\r" "n" "p"
9122                "N" "P" "\M-\C-n" "\M-\C-p" "." "\M-s" "\M-r"
9123                "<" ">" "l" "j" "^" "\M-^" "-" "u" "U" "d" "D"
9124                "\M-u" "\M-U" "k" "\C-k" "\M-\C-k" "c" "x" "X" 
9125                "\M-\C-x" "\M-\177" "b" "B" "$" "w" "\C-c\C-r"
9126                "t" "\M-t" "C" "S"
9127                "m" "o" "\C-o" "|" "\M-m" "\M-\C-m" "\M-k" "M"
9128                "V" "\C-c\C-d")))
9129     (while commands
9130       (define-key gnus-article-mode-map (car commands) 
9131         'gnus-article-summary-command)
9132       (setq commands (cdr commands))))
9133
9134   (let ((commands (list "q" "Q" "r" "R" "\C-c\C-f" "m"  "a" "f" "F")))
9135     (while commands
9136       (define-key gnus-article-mode-map (car commands) 
9137         'gnus-article-summary-command-nosave)
9138       (setq commands (cdr commands)))))
9139
9140
9141 (defun gnus-article-mode ()
9142   "Major mode for displaying an article.
9143
9144 All normal editing commands are switched off.
9145
9146 The following commands are available:
9147
9148 \\<gnus-article-mode-map>
9149 \\[gnus-article-next-page]\t Scroll the article one page forwards
9150 \\[gnus-article-prev-page]\t Scroll the article one page backwards
9151 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
9152 \\[gnus-article-show-summary]\t Display the summary buffer
9153 \\[gnus-article-mail]\t Send a reply to the address near point
9154 \\[gnus-article-mail-with-original]\t Send a reply to the address near point; include the original article
9155 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
9156 \\[gnus-info-find-node]\t Go to the Gnus info node"
9157   (interactive)
9158   (if gnus-visual (gnus-article-make-menu-bar))
9159   (kill-all-local-variables)
9160   (setq mode-line-modified "-- ")
9161   (make-local-variable 'mode-line-format)
9162   (setq mode-line-format (copy-sequence mode-line-format))
9163   (and (equal (nth 3 mode-line-format) "   ")
9164        (setcar (nthcdr 3 mode-line-format) ""))
9165   (setq mode-name "Article")
9166   (setq major-mode 'gnus-article-mode)
9167   (make-local-variable 'minor-mode-alist)
9168   (or (assq 'gnus-show-mime minor-mode-alist)
9169       (setq minor-mode-alist
9170             (cons (list 'gnus-show-mime " MIME") minor-mode-alist)))
9171   (use-local-map gnus-article-mode-map)
9172   (make-local-variable 'page-delimiter)
9173   (setq page-delimiter gnus-page-delimiter)
9174   (buffer-disable-undo (current-buffer))
9175   (setq buffer-read-only t)             ;Disable modification
9176   (run-hooks 'gnus-article-mode-hook))
9177
9178 (defun gnus-article-setup-buffer ()
9179   "Initialize article mode buffer."
9180   (or (get-buffer gnus-article-buffer)
9181       (save-excursion
9182         (set-buffer (get-buffer-create gnus-article-buffer))
9183         (gnus-add-current-to-buffer-list)
9184         (gnus-article-mode))))
9185
9186 (defun gnus-request-article-this-buffer (article &optional group)
9187   "Get an article and insert it into this buffer."
9188   (setq group (or group gnus-newsgroup-name))
9189   ;; Using `gnus-request-article' directly will insert the article into
9190   ;; `nntp-server-buffer' - so we'll save some time by not having to
9191   ;; copy it from the server buffer into the article buffer.
9192
9193   ;; We only request an article by message-id when we do not have the
9194   ;; headers for it, so we'll have to get those.
9195   (and (stringp article) 
9196        (let ((gnus-override-method gnus-refer-article-method))
9197          (gnus-read-header article)))
9198
9199   ;; If the article number is negative, that means that this article
9200   ;; doesn't belong in this newsgroup (possibly), so we find its
9201   ;; message-id and request it by id instead of number.
9202   (if (and (numberp article) (< article 0))
9203       (save-excursion
9204         (set-buffer gnus-summary-buffer)
9205         (let ((header (gnus-gethash (int-to-string article)
9206                                     gnus-newsgroup-headers-hashtb-by-number)))
9207           (if (vectorp header)
9208               ;; It's a real article.
9209               (setq article (header-id header))
9210             ;; It is an extracted pseudo-article.
9211             (setq article nil)
9212             (gnus-request-pseudo-article header)))))
9213   ;; Get the article and into the article buffer.
9214   (if article
9215       (progn
9216        (erase-buffer)
9217        (let ((gnus-override-method 
9218               (and (stringp article) gnus-refer-article-method)))
9219          (and (gnus-request-article article group (current-buffer))
9220               'article)))
9221     'pseudo))
9222
9223 (defun gnus-read-header (id)
9224   "Read the headers of article ID and enter them into the Gnus system."
9225   (or gnus-newsgroup-headers-hashtb-by-number
9226       (gnus-make-headers-hashtable-by-number))
9227   (let (header)
9228     (if (not (setq header 
9229                    (car (if (let ((gnus-nov-is-evil t))
9230                               (gnus-retrieve-headers 
9231                                (list id) gnus-newsgroup-name))
9232                             (gnus-get-newsgroup-headers)))))
9233         nil
9234       (if (stringp id)
9235           (header-set-number header gnus-reffed-article-number))
9236       (setq gnus-newsgroup-headers (cons header gnus-newsgroup-headers))
9237       (gnus-sethash (int-to-string (header-number header)) header
9238                     gnus-newsgroup-headers-hashtb-by-number)
9239       (if (stringp id)
9240           (setq gnus-reffed-article-number (1- gnus-reffed-article-number)))
9241       (setq gnus-current-headers header)
9242       header)))
9243
9244 (defun gnus-article-prepare (article &optional all-headers header)
9245   "Prepare ARTICLE in article mode buffer.
9246 ARTICLE should either be an article number or a Message-ID.
9247 If ARTICLE is an id, HEADER should be the article headers.
9248 If ALL-HEADERS is non-nil, no headers are hidden."
9249   (save-excursion
9250     ;; Make sure we start in a summary buffer.
9251     (or (eq major-mode 'gnus-summary-mode)
9252         (set-buffer gnus-summary-buffer))
9253     (setq gnus-summary-buffer (current-buffer))
9254     ;; Make sure the connection to the server is alive.
9255     (or (gnus-server-opened (gnus-find-method-for-group gnus-newsgroup-name))
9256         (progn
9257           (gnus-check-news-server 
9258            (gnus-find-method-for-group gnus-newsgroup-name))
9259           (gnus-request-group gnus-newsgroup-name t)))
9260     (or gnus-newsgroup-headers-hashtb-by-number
9261         (gnus-make-headers-hashtable-by-number))
9262     (let* ((article (if header (header-number header) article))
9263            (summary-buffer (current-buffer))
9264            (internal-hook gnus-article-internal-prepare-hook)
9265            (bookmark (cdr (assq article gnus-newsgroup-bookmarks)))
9266            (group gnus-newsgroup-name)
9267            result)
9268       (save-excursion
9269         (gnus-article-setup-buffer)
9270         (set-buffer gnus-article-buffer)
9271         (let ((buffer-read-only nil))
9272           (if (not (setq result (gnus-request-article-this-buffer 
9273                                  article group)))
9274               ;; There is no such article.
9275               (progn
9276                 (save-excursion
9277                   (set-buffer gnus-summary-buffer)
9278                   (setq gnus-current-article 0)
9279                   (and (numberp article) 
9280                        (gnus-summary-mark-as-read article gnus-canceled-mark))
9281                   (message "No such article (may be canceled)")
9282                   (and (numberp article)
9283                        (setq gnus-current-article article))
9284                   (ding))
9285                 (and (numberp article)
9286                      (setq gnus-article-current 
9287                            (cons gnus-newsgroup-name article)))
9288                 nil)
9289             (if (not (eq result 'article))
9290                 (progn
9291                   (save-excursion
9292                     (set-buffer summary-buffer)
9293                     (setq gnus-last-article gnus-current-article
9294                           gnus-newsgroup-history (cons gnus-current-article
9295                                                        gnus-newsgroup-history)
9296                           gnus-current-article 0
9297                           gnus-current-headers nil
9298                           gnus-article-current nil)
9299                     (gnus-configure-windows 'article)
9300                     (gnus-set-global-variables))
9301                   (gnus-set-mode-line 'article))
9302               ;; The result from the `request' was an actual article -
9303               ;; or at least some text that is now displayed in the
9304               ;; article buffer.
9305               (if (and (numberp article)
9306                        (not (eq article gnus-current-article)))
9307                   ;; Seems like a new article has been selected.
9308                   ;; `gnus-current-article' must be an article number.
9309                   (save-excursion
9310                     (set-buffer summary-buffer)
9311                     (setq gnus-last-article gnus-current-article
9312                           gnus-newsgroup-history (cons gnus-current-article
9313                                                        gnus-newsgroup-history)
9314                           gnus-current-article article
9315                           gnus-current-headers 
9316                           (gnus-get-header-by-number gnus-current-article)
9317                           gnus-article-current 
9318                           (cons gnus-newsgroup-name gnus-current-article))
9319                     (run-hooks 'gnus-mark-article-hook)
9320                     (gnus-set-mode-line 'summary)
9321                     (and gnus-visual 
9322                          (run-hooks 'gnus-visual-mark-article-hook))
9323                     ;; Set the global newsgroup variables here.
9324                     ;; Suggested by Jim Sisolak
9325                     ;; <sisolak@trans4.neep.wisc.edu>.
9326                     (gnus-set-global-variables)))
9327               ;; gnus-have-all-headers must be either T or NIL.
9328               (setq gnus-have-all-headers
9329                     (not (not (or all-headers gnus-show-all-headers))))
9330               ;; Hooks for getting information from the article.
9331               ;; This hook must be called before being narrowed.
9332               (run-hooks 'internal-hook)
9333               (run-hooks 'gnus-article-prepare-hook)
9334               ;; Decode MIME message.
9335               (if (and gnus-show-mime
9336                        (gnus-fetch-field "Mime-Version"))
9337                   (funcall gnus-show-mime-method))
9338               ;; Perform the article display hooks.
9339               (let ((buffer-read-only nil))
9340                 (run-hooks 'gnus-article-display-hook))
9341               ;; Do page break.
9342               (goto-char (point-min))
9343               (and gnus-break-pages (gnus-narrow-to-page))
9344               (gnus-set-mode-line 'article)
9345               (gnus-configure-windows 'article)
9346               (goto-char 1)
9347               (set-window-start 
9348                (get-buffer-window gnus-article-buffer)
9349                (cond (bookmark
9350                       (message "Moved to bookmark")
9351                       (search-forward "\n\n" nil t)
9352                       (forward-line bookmark)
9353                       (point))
9354                      (t
9355                       (point-min))))
9356               t)))))))
9357
9358 (defun gnus-article-show-all-headers ()
9359   "Show all article headers in article mode buffer."
9360   (save-excursion 
9361     (setq gnus-have-all-headers t)
9362     (gnus-article-setup-buffer)
9363     (set-buffer gnus-article-buffer)
9364     (let ((buffer-read-only nil))
9365       (remove-text-properties 1 (point-max) '(invisible t)))))
9366
9367 (defun gnus-article-hide-headers-if-wanted ()
9368   "Hide unwanted headers if `gnus-have-all-headers' is nil.
9369 Provided for backwards compatability."
9370   (or gnus-have-all-headers
9371       (gnus-article-hide-headers)))
9372
9373 (defun gnus-article-hide-headers (&optional delete)
9374   "Hide unwanted headers and possibly sort them as well."
9375   (interactive "P")
9376   (save-excursion
9377     (set-buffer gnus-article-buffer)
9378     (save-restriction
9379       (let ((sorted gnus-sorted-header-list)
9380             (buffer-read-only nil)
9381             want want-list beg want-l)
9382         ;; First we narrow to just the headers.
9383         (widen)
9384         (goto-char 1)
9385         ;; Hide any "From " lines at the beginning of (mail) articles. 
9386         (while (looking-at "From ")
9387           (forward-line 1))
9388         (if (/= (point) 1) 
9389             (add-text-properties 1 (point) '(invisible t)))
9390         ;; Then treat the rest of the header lines.
9391         (narrow-to-region 
9392          (point) 
9393          (progn (search-forward "\n\n" nil t) (forward-line -1) (point)))
9394         ;; Then we use the two regular expressions
9395         ;; `gnus-ignored-headers' and `gnus-visible-headers' to
9396         ;; select which header lines is to remain visible in the
9397         ;; article buffer.
9398         (goto-char 1)
9399         (while (re-search-forward "^[^ \t]*:" nil t)
9400           (beginning-of-line)
9401           ;; We add the headers we want to keep to a list and delete
9402           ;; them from the buffer.
9403           (if (or (and (stringp gnus-visible-headers)
9404                        (looking-at gnus-visible-headers))
9405                   (and (not (stringp gnus-visible-headers))
9406                        (stringp gnus-ignored-headers)
9407                        (not (looking-at gnus-ignored-headers))))
9408               (progn
9409                 (setq beg (point))
9410                 (forward-line 1)
9411                 ;; Be sure to get multi-line headers...
9412                 (re-search-forward "^[^ \t]*:" nil t)
9413                 (beginning-of-line)
9414                 (setq want-list 
9415                       (cons (buffer-substring beg (point)) want-list))
9416                 (delete-region beg (point))
9417                 (goto-char beg))
9418             (forward-line 1)))
9419         ;; Next we perform the sorting by looking at
9420         ;; `gnus-sorted-header-list'. 
9421         (goto-char 1)
9422         (while (and sorted want-list)
9423           (setq want-l want-list)
9424           (while (and want-l
9425                       (not (string-match (car sorted) (car want-l))))
9426             (setq want-l (cdr want-l)))
9427           (if want-l 
9428               (progn
9429                 (insert (car want-l))
9430                 (setq want-list (delq (car want-l) want-list))))
9431           (setq sorted (cdr sorted)))
9432         ;; Any headers that were not matched by the sorted list we
9433         ;; just tack on the end of the visible header list.
9434         (while want-list
9435           (insert (car want-list))
9436           (setq want-list (cdr want-list)))
9437         ;; And finally we make the unwanted headers invisible.
9438         (if delete
9439             (delete-region (point) (point-max))
9440           ;; Suggested by Sudish Joseph <joseph@cis.ohio-state.edu>.
9441           (add-text-properties (point) (point-max) '(invisible t)))))))
9442
9443 (defun gnus-article-hide-signature ()
9444   "Hides the signature in an article.
9445 It does this by hiding everyting after \"^-- *$\", which is what all
9446 signatures should be preceded by. Note that this may mean that parts
9447 of an article may disappear if the article has such a line in the
9448 middle of the text."
9449   (interactive)
9450   (save-excursion
9451     (set-buffer gnus-article-buffer)
9452     (let ((buffer-read-only nil))
9453       (goto-char (point-max))
9454       (if (re-search-backward "^-- *$" nil t)
9455           (progn
9456             (add-text-properties (point) (point-max) '(invisible t)))))))
9457
9458 (defun gnus-article-hide-citation ()
9459   "Hide all cited text.
9460 This function uses the famous, extremely intelligent \"shoot in foot\"
9461 algorithm - which is simply deleting all lines that start with
9462 \">\". Your mileage may vary. If you come up with anything better,
9463 please do mail it to me."
9464   (interactive)
9465   (save-excursion
9466     (set-buffer gnus-article-buffer)
9467     (let ((buffer-read-only nil))
9468       (goto-char 1)
9469       (search-forward "\n\n" nil t)
9470       (while (not (eobp))
9471         (if (looking-at ">")
9472             (add-text-properties 
9473              (point) (save-excursion (forward-line 1) (point))
9474              '(invisible t)))
9475         (forward-line 1)))))
9476
9477 ;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.
9478 (defun gnus-article-treat-overstrike ()
9479   "Translate overstrikes into bold text."
9480   (interactive)
9481   (save-excursion
9482     (set-buffer gnus-article-buffer)
9483     (let ((buffer-read-only nil))
9484       (while (search-forward "\b" nil t)
9485         (let ((next (following-char))
9486               (previous (char-after (- (point) 2))))
9487           (cond ((eq next previous)
9488                  (delete-region (- (point) 2) (point))
9489                  (put-text-property (point) (1+ (point))
9490                                     'face 'bold))
9491                 ((eq next ?_)
9492                  (delete-region (1- (point)) (1+ (point)))
9493                  (put-text-property (1- (point)) (point)
9494                                     'face 'underline))
9495                 ((eq previous ?_)
9496                  (delete-region (- (point) 2) (point))
9497                  (put-text-property (point) (1+ (point))
9498                                     'face 'underline))))))))
9499
9500 (defun gnus-article-word-wrap ()
9501   "Format too long lines."
9502   (interactive)
9503   (save-excursion
9504     (set-buffer gnus-article-buffer)
9505     (let ((buffer-read-only nil))
9506       (goto-char 1)
9507       (search-forward "\n\n" nil t)
9508       (end-of-line 1)
9509       (let ((paragraph-start "^\\W"))
9510         (while (not (eobp))
9511           (and (>= (current-column) (window-width))
9512                (/= (preceding-char) ?:)
9513                (fill-paragraph nil))
9514           (end-of-line 2))))))
9515
9516 (defun gnus-article-remove-cr ()
9517   "Remove carriage returns from an article."
9518   (interactive)
9519   (save-excursion
9520     (set-buffer gnus-article-buffer)
9521     (let ((buffer-read-only nil))
9522       (goto-char (point-min))
9523       (while (search-forward "\r" nil t)
9524         (replace-match "" t t)))))
9525
9526 (defun gnus-article-display-x-face ()
9527   "Look for an X-Face header and display it if present."
9528   (interactive)
9529   (save-excursion
9530     (set-buffer gnus-article-buffer)
9531     (goto-char (point-min))
9532     (if (or (not gnus-article-x-face-command)
9533             (and (re-search-forward "^\\($\\|X-Face: \\)" nil t)
9534                  (looking-at "^$")))
9535         ()
9536       (let ((face
9537              (buffer-substring
9538               (point) 
9539               (progn
9540                 (forward-line 1)
9541                 (while (and (looking-at "[ \t]") 
9542                             (zerop (forward-line 1))))
9543                 (point))))
9544             (command gnus-article-x-face-command))
9545         (if (symbolp command)
9546             (and (or (fboundp command) (error "%s is not a function" command))
9547                  (funcall command face))
9548           (let ((process (start-process "gnus-x-face" nil "sh" "-c" command)))
9549             (if process
9550                 (progn
9551                   (process-send-string process face)
9552                   (process-send-eof process))
9553               (error "Couldn't start process"))))))))
9554               
9555
9556 (defun gnus-mime-decode-quoted-printable (from to)
9557   ;; Decode quoted-printable from region between FROM and TO.
9558   (save-excursion
9559     (goto-char from)
9560     (while (search-forward "=" to t)
9561       (cond ((eq (following-char) ?\n)
9562              (delete-char -1)
9563              (delete-char 1))
9564             ((looking-at "[0-9A-F][0-9A-F]")
9565              (delete-char -1)
9566              (insert (hexl-hex-string-to-integer
9567                       (buffer-substring (point) (+ 2 (point)))))
9568              (delete-char 2))
9569             ((message "Malformed MIME quoted-printable message"))))))
9570
9571 (defun gnus-article-de-quoted-unreadable ()
9572   "Do a naive translation of a quoted-printable-encoded article.
9573 This is in no way, shape or form meant as a replacement for real MIME
9574 processing, but is simply a stop-gap measure until MIME support is
9575 written."
9576   ;; Unquote quoted-printable from news articles.
9577   (interactive)
9578   (save-excursion
9579     (set-buffer gnus-article-buffer)
9580     (let ((case-fold-search t)
9581           (type (gnus-fetch-field "content-transfer-encoding")))
9582       (cond ((and (stringp type) (string-match "quoted-printable" type))
9583              (goto-char (point-min))
9584              (search-forward "\n\n" nil 'move)
9585              (message  "MIME Unquoting printable...")
9586              (gnus-mime-decode-quoted-printable (point) (point-max))
9587              (message  "MIME Unquoting printable...done")))
9588       (set-buffer gnus-summary-buffer))))
9589
9590 (defun gnus-article-date-ut (date type)
9591   "Convert DATE date to universal time in the current article.
9592 If TYPE is `local', convert to local time; if it is `lapsed', output
9593 how much time has lapsed since DATE."
9594   (interactive (list (header-date gnus-current-headers) 'ut))
9595   (if (not date)
9596       ()
9597     (save-excursion
9598       (set-buffer gnus-article-buffer)
9599       (let ((buffer-read-only nil))
9600         (goto-char (point-min))
9601         (if (re-search-forward "^Date: \\|^X-Sent: " nil t)
9602             (delete-region (gnus-point-at-bol)
9603                            (progn (forward-line 1) (point)))
9604           (- (search-forward "\n\n") 2))
9605         (insert
9606          (cond 
9607           ((eq type 'local)
9608            (concat "Date: " (timezone-make-date-arpa-standard date) "\n"))
9609           ((eq type 'ut)
9610            (concat "Date: " (timezone-make-date-arpa-standard date nil "UT")
9611                    "\n"))
9612           ((eq type 'lapsed)
9613            (let* ((sec (- (gnus-seconds-since-epoch (current-time-string))
9614                           (gnus-seconds-since-epoch date)))
9615                   (units (list (cons 'year (* 1.0 365 24 60 60))
9616                                (cons 'week (* 7 24 60 60))
9617                                (cons 'day (* 24 60 60))
9618                                (cons 'hour (* 60 60))
9619                                (cons 'minute 60)
9620                                (cons 'second 1)))
9621                   num prev)
9622              (concat
9623               "X-Sent: "
9624               (mapconcat 
9625                (lambda (unit)
9626                  (if (zerop (setq num (floor (/ sec (cdr unit)))))
9627                      ""
9628                    (setq sec (- sec (* num (cdr unit))))
9629                    (prog1
9630                        (concat (if prev ", " "") (int-to-string num)
9631                                " " (symbol-name (car unit))
9632                                (if (> num 1) "s" ""))
9633                      (setq prev t))))
9634                units "")
9635               " ago\n")))
9636           (t
9637            (error "Unknown conversion type: %s" type))))))))
9638
9639 (defun gnus-article-date-local (date)
9640   "Convert the current article date to the local timezone."
9641   (interactive (list (header-date gnus-current-headers)))
9642   (gnus-article-date-ut date 'local))
9643
9644 (defun gnus-article-date-lapsed (date)
9645   "Convert the current article date to time lapsed since it was sent."
9646   (interactive (list (header-date gnus-current-headers)))
9647   (gnus-article-date-ut date 'lapsed))
9648
9649
9650 ;; Article savers.
9651
9652 (defun gnus-output-to-rmail (file-name)
9653   "Append the current article to an Rmail file named FILE-NAME."
9654   (require 'rmail)
9655   ;; Most of these codes are borrowed from rmailout.el.
9656   (setq file-name (expand-file-name file-name))
9657   (setq rmail-default-rmail-file file-name)
9658   (let ((artbuf (current-buffer))
9659         (tmpbuf (get-buffer-create " *Gnus-output*")))
9660     (save-excursion
9661       (or (get-file-buffer file-name)
9662           (file-exists-p file-name)
9663           (if (gnus-yes-or-no-p
9664                (concat "\"" file-name "\" does not exist, create it? "))
9665               (let ((file-buffer (create-file-buffer file-name)))
9666                 (save-excursion
9667                   (set-buffer file-buffer)
9668                   (rmail-insert-rmail-file-header)
9669                   (let ((require-final-newline nil))
9670                     (write-region (point-min) (point-max) file-name t 1)))
9671                 (kill-buffer file-buffer))
9672             (error "Output file does not exist")))
9673       (set-buffer tmpbuf)
9674       (buffer-disable-undo (current-buffer))
9675       (erase-buffer)
9676       (insert-buffer-substring artbuf)
9677       (gnus-convert-article-to-rmail)
9678       ;; Decide whether to append to a file or to an Emacs buffer.
9679       (let ((outbuf (get-file-buffer file-name)))
9680         (if (not outbuf)
9681             (append-to-file (point-min) (point-max) file-name)
9682           ;; File has been visited, in buffer OUTBUF.
9683           (set-buffer outbuf)
9684           (let ((buffer-read-only nil)
9685                 (msg (and (boundp 'rmail-current-message)
9686                           (symbol-value 'rmail-current-message))))
9687             ;; If MSG is non-nil, buffer is in RMAIL mode.
9688             (if msg
9689                 (progn (widen)
9690                        (narrow-to-region (point-max) (point-max))))
9691             (insert-buffer-substring tmpbuf)
9692             (if msg
9693                 (progn
9694                   (goto-char (point-min))
9695                   (widen)
9696                   (search-backward "\^_")
9697                   (narrow-to-region (point) (point-max))
9698                   (goto-char (1+ (point-min)))
9699                   (rmail-count-new-messages t)
9700                   (rmail-show-message msg)))))))
9701     (kill-buffer tmpbuf)))
9702
9703 (defun gnus-output-to-file (file-name)
9704   "Append the current article to a file named FILE-NAME."
9705   (setq file-name (expand-file-name file-name))
9706   (let ((artbuf (current-buffer))
9707         (tmpbuf (get-buffer-create " *Gnus-output*")))
9708     (save-excursion
9709       (set-buffer tmpbuf)
9710       (buffer-disable-undo (current-buffer))
9711       (erase-buffer)
9712       (insert-buffer-substring artbuf)
9713       ;; Append newline at end of the buffer as separator, and then
9714       ;; save it to file.
9715       (goto-char (point-max))
9716       (insert "\n")
9717       (append-to-file (point-min) (point-max) file-name))
9718     (kill-buffer tmpbuf)))
9719
9720 (defun gnus-convert-article-to-rmail ()
9721   "Convert article in current buffer to Rmail message format."
9722   (let ((buffer-read-only nil))
9723     ;; Convert article directly into Babyl format.
9724     ;; Suggested by Rob Austein <sra@lcs.mit.edu>
9725     (goto-char (point-min))
9726     (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
9727     (while (search-forward "\n\^_" nil t) ;single char
9728       (replace-match "\n^_" t t))               ;2 chars: "^" and "_"
9729     (goto-char (point-max))
9730     (insert "\^_")))
9731
9732 (defun gnus-narrow-to-page (&optional arg)
9733   "Make text outside current page invisible except for page delimiter.
9734 A numeric arg specifies to move forward or backward by that many pages,
9735 thus showing a page other than the one point was originally in."
9736   (interactive "P")
9737   (setq arg (if arg (prefix-numeric-value arg) 0))
9738   (save-excursion
9739     (forward-page -1)                   ;Beginning of current page.
9740     (widen)
9741     (if (> arg 0)
9742         (forward-page arg)
9743       (if (< arg 0)
9744           (forward-page (1- arg))))
9745     ;; Find the end of the page.
9746     (forward-page)
9747     ;; If we stopped due to end of buffer, stay there.
9748     ;; If we stopped after a page delimiter, put end of restriction
9749     ;; at the beginning of that line.
9750     ;; These are commented out.
9751     ;;    (if (save-excursion (beginning-of-line)
9752     ;;                  (looking-at page-delimiter))
9753     ;;  (beginning-of-line))
9754     (narrow-to-region (point)
9755                       (progn
9756                         ;; Find the top of the page.
9757                         (forward-page -1)
9758                         ;; If we found beginning of buffer, stay there.
9759                         ;; If extra text follows page delimiter on same line,
9760                         ;; include it.
9761                         ;; Otherwise, show text starting with following line.
9762                         (if (and (eolp) (not (bobp)))
9763                             (forward-line 1))
9764                         (point)))))
9765
9766 (defun gnus-gmt-to-local ()
9767   "Rewrite Date header described in GMT to local in current buffer.
9768 Intended to be used with gnus-article-prepare-hook."
9769   (save-excursion
9770     (save-restriction
9771       (widen)
9772       (goto-char (point-min))
9773       (narrow-to-region (point-min)
9774                         (progn (search-forward "\n\n" nil 'move) (point)))
9775       (goto-char (point-min))
9776       (if (re-search-forward "^Date:[ \t]\\(.*\\)$" nil t)
9777           (let ((buffer-read-only nil)
9778                 (date (buffer-substring (match-beginning 1) (match-end 1))))
9779             (delete-region (match-beginning 1) (match-end 1))
9780             (insert
9781              (timezone-make-date-arpa-standard 
9782               date nil (current-time-zone))))))))
9783
9784
9785 ;; Article mode commands
9786
9787 (defun gnus-article-next-page (lines)
9788   "Show next page of current article.
9789 If end of article, return non-nil. Otherwise return nil.
9790 Argument LINES specifies lines to be scrolled up."
9791   (interactive "P")
9792   (move-to-window-line -1)
9793   ;; Fixed by enami@ptgd.sony.co.jp (enami tsugutomo)
9794   (if (save-excursion
9795         (end-of-line)
9796         (and (pos-visible-in-window-p)  ;Not continuation line.
9797              (eobp)))
9798       ;; Nothing in this page.
9799       (if (or (not gnus-break-pages)
9800               (save-excursion
9801                 (save-restriction
9802                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
9803           t                             ;Nothing more.
9804         (gnus-narrow-to-page 1)         ;Go to next page.
9805         nil)
9806     ;; More in this page.
9807     (condition-case ()
9808         (scroll-up lines)
9809       (end-of-buffer
9810        ;; Long lines may cause an end-of-buffer error.
9811        (goto-char (point-max))))
9812     nil))
9813
9814 (defun gnus-article-prev-page (lines)
9815   "Show previous page of current article.
9816 Argument LINES specifies lines to be scrolled down."
9817   (interactive "P")
9818   (move-to-window-line 0)
9819   (if (and gnus-break-pages
9820            (bobp)
9821            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
9822       (progn
9823         (gnus-narrow-to-page -1) ;Go to previous page.
9824         (goto-char (point-max))
9825         (recenter -1))
9826     (scroll-down lines)))
9827
9828 (defun gnus-article-refer-article ()
9829   "Read article specified by message-id around point."
9830   (interactive)
9831   (search-forward ">" nil t)    ;Move point to end of "<....>".
9832   (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
9833       (let ((message-id
9834              (buffer-substring (match-beginning 1) (match-end 1))))
9835         (set-buffer gnus-summary-buffer)
9836         (gnus-summary-refer-article message-id))
9837     (error "No references around point")))
9838
9839 (defun gnus-article-mail (yank)
9840   "Send a reply to the address near point.
9841 If YANK is non-nil, include the original article."
9842   (interactive "P")
9843   (let ((address 
9844          (buffer-substring
9845           (save-excursion (re-search-backward "[ \t\n]" nil t) (1+ (point)))
9846           (save-excursion (re-search-forward "[ \t\n]" nil t) (1- (point))))))
9847     (and address
9848          (progn
9849            (switch-to-buffer gnus-summary-buffer)
9850            (funcall gnus-mail-reply-method yank address)))))
9851
9852 (defun gnus-article-mail-with-original ()
9853   "Send a reply to the address near point and include the original article."
9854   (interactive)
9855   (gnus-article-mail 'yank))
9856
9857 (defun gnus-article-show-summary ()
9858   "Reconfigure windows to show summary buffer."
9859   (interactive)
9860   (gnus-configure-windows 'article)
9861   (pop-to-buffer gnus-summary-buffer)
9862   (gnus-summary-goto-subject gnus-current-article))
9863
9864 (defun gnus-article-describe-briefly ()
9865   "Describe article mode commands briefly."
9866   (interactive)
9867   (message
9868    (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")))
9869
9870 (defun gnus-article-summary-command ()
9871   "Execute the last keystroke in the summary buffer."
9872   (interactive)
9873   (let ((obuf (current-buffer))
9874         (owin (current-window-configuration)))
9875     (switch-to-buffer gnus-summary-buffer 'norecord)
9876     (execute-kbd-macro (this-command-keys))
9877     (set-buffer obuf)
9878     (let ((npoint (point)))
9879       (set-window-configuration owin)
9880       (set-window-start (get-buffer-window (current-buffer)) (point)))))
9881
9882 (defun gnus-article-summary-command-nosave ()
9883   "Execute the last keystroke in the summary buffer."
9884   (interactive)
9885   (let ((obuf (current-buffer))
9886         (owin (current-window-configuration)))
9887     (switch-to-buffer gnus-summary-buffer 'norecord)
9888     (execute-kbd-macro (this-command-keys))))
9889
9890 ;; caesar-region written by phr@prep.ai.mit.edu  Nov 86
9891 ;; Modified by tower@prep Nov 86
9892 ;; Modified by umerin@flab.flab.Fujitsu.JUNET for ROT47.
9893
9894 (defun gnus-caesar-region (&optional n)
9895   "Caesar rotation of region by N, default 13, for decrypting netnews.
9896 ROT47 will be performed for Japanese text in any case."
9897   (interactive (if current-prefix-arg   ; Was there a prefix arg?
9898                    (list (prefix-numeric-value current-prefix-arg))
9899                  (list nil)))
9900   (cond ((not (numberp n)) (setq n 13))
9901         (t (setq n (mod n 26))))        ;canonicalize N
9902   (if (not (zerop n))           ; no action needed for a rot of 0
9903       (progn
9904         (if (or (not (boundp 'caesar-translate-table))
9905                 (not caesar-translate-table)
9906                 (/= (aref caesar-translate-table ?a) (+ ?a n)))
9907             (let ((i 0) 
9908                   (lower "abcdefghijklmnopqrstuvwxyz")
9909                   upper)
9910               (message "Building caesar-translate-table...")
9911               (setq caesar-translate-table (make-vector 256 0))
9912               (while (< i 256)
9913                 (aset caesar-translate-table i i)
9914                 (setq i (1+ i)))
9915               (setq lower (concat lower lower)
9916                     upper (upcase lower)
9917                     i 0)
9918               (while (< i 26)
9919                 (aset caesar-translate-table (+ ?a i) (aref lower (+ i n)))
9920                 (aset caesar-translate-table (+ ?A i) (aref upper (+ i n)))
9921                 (setq i (1+ i)))
9922               ;; ROT47 for Japanese text.
9923               ;; Thanks to ichikawa@flab.fujitsu.junet.
9924               (setq i 161)
9925               (let ((t1 (logior ?O 128))
9926                     (t2 (logior ?! 128))
9927                     (t3 (logior ?~ 128)))
9928                 (while (< i 256)
9929                   (aset caesar-translate-table i
9930                         (let ((v (aref caesar-translate-table i)))
9931                           (if (<= v t1) (if (< v t2) v (+ v 47))
9932                             (if (<= v t3) (- v 47) v))))
9933                   (setq i (1+ i))))
9934               (message "Building caesar-translate-table... done")))
9935         (let ((from (region-beginning))
9936               (to (region-end))
9937               (i 0) str len)
9938           (setq str (buffer-substring from to))
9939           (setq len (length str))
9940           (while (< i len)
9941             (aset str i (aref caesar-translate-table (aref str i)))
9942             (setq i (1+ i)))
9943           (goto-char from)
9944           (delete-region from to)
9945           (insert str)))))
9946
9947 \f
9948 ;; Basic ideas by emv@math.lsa.umich.edu (Edward Vielmetti)
9949
9950 ;;;###autoload
9951 (defalias 'gnus-batch-kill 'gnus-batch-score)
9952 ;;;###autoload
9953 (defun gnus-batch-score ()
9954   "Run batched scoring.
9955 Usage: emacs -batch -l gnus -f gnus-batch-score <newsgroups> ...
9956 Newsgroups is a list of strings in Bnews format.  If you want to score
9957 the comp hierarchy, you'd say \"comp.all\". If you would not like to
9958 score the alt hierarchy, you'd say \"!alt.all\"."
9959   (interactive)
9960   (let* ((yes-and-no
9961           (gnus-parse-n-options
9962            (apply (function concat)
9963                   (mapcar (lambda (g) (concat g " "))
9964                           command-line-args-left))))
9965          (gnus-expert-user t)
9966          (nnmail-spool-file nil)
9967          (gnus-use-dribble-file nil)
9968          (yes (car yes-and-no))
9969          (no (cdr yes-and-no))
9970          group subscribed newsrc entry
9971          ;; Disable verbose message.
9972          gnus-novice-user gnus-large-newsgroup)
9973     ;; Eat all arguments.
9974     (setq command-line-args-left nil)
9975     ;; Start Gnus.
9976     (gnus)
9977     ;; Apply kills to specified newsgroups in command line arguments.
9978     (setq newsrc (cdr gnus-newsrc-alist))
9979     (while newsrc
9980       (setq group (car (car newsrc)))
9981       (setq entry (gnus-gethash group gnus-newsrc-hashtb))
9982       (if (and (<= (nth 1 (car newsrc)) gnus-level-subscribed)
9983                (and (car entry)
9984                     (or (eq (car entry) t)
9985                         (not (zerop (car entry)))))
9986                (if yes (string-match yes group) t)
9987                (or (null no) (not (string-match no group))))
9988           (progn
9989             (gnus-summary-read-group group nil t)
9990             (and (eq (current-buffer) (get-buffer gnus-summary-buffer))
9991                  (gnus-summary-exit))))
9992       (setq newsrc (cdr newsrc)))
9993     ;; Exit Emacs.
9994     (switch-to-buffer gnus-group-buffer)
9995     (gnus-group-save-newsrc)))
9996
9997 (defun gnus-apply-kill-file ()
9998   "Apply a kill file to the current newsgroup.
9999 Returns the number of articles marked as read."
10000   (if (or (file-exists-p (gnus-newsgroup-kill-file nil))
10001           (file-exists-p (gnus-newsgroup-kill-file gnus-newsgroup-name)))
10002       (gnus-apply-kill-file-internal)
10003     0))
10004
10005 (defun gnus-kill-save-kill-buffer ()
10006   (save-excursion
10007     (let ((file (gnus-newsgroup-kill-file gnus-newsgroup-name)))
10008       (if (get-file-buffer file)
10009           (progn
10010             (set-buffer (get-file-buffer file))
10011             (and (buffer-modified-p) (save-buffer))
10012             (kill-buffer (current-buffer)))))))
10013
10014 (defvar gnus-kill-file-name "KILL"
10015   "Suffix of the kill files.")
10016
10017 (defun gnus-newsgroup-kill-file (newsgroup)
10018   "Return the name of a kill file name for NEWSGROUP.
10019 If NEWSGROUP is nil, return the global kill file name instead."
10020   (cond ((or (null newsgroup)
10021              (string-equal newsgroup ""))
10022          ;; The global KILL file is placed at top of the directory.
10023          (expand-file-name gnus-kill-file-name
10024                            (or gnus-kill-files-directory "~/News")))
10025         ((gnus-use-long-file-name 'not-kill)
10026          ;; Append ".KILL" to newsgroup name.
10027          (expand-file-name (concat newsgroup "." gnus-kill-file-name)
10028                            (or gnus-kill-files-directory "~/News")))
10029         (t
10030          ;; Place "KILL" under the hierarchical directory.
10031          (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
10032                                    "/" gnus-kill-file-name)
10033                            (or gnus-kill-files-directory "~/News")))))
10034
10035 \f
10036 ;;;
10037 ;;; Dribble file
10038 ;;;
10039
10040 (defvar gnus-dribble-ignore nil)
10041
10042 (defun gnus-dribble-file-name ()
10043   (concat gnus-startup-file "-dribble"))
10044
10045 (defun gnus-dribble-open ()
10046   (save-excursion 
10047     (set-buffer 
10048      (setq gnus-dribble-buffer (find-file-noselect (gnus-dribble-file-name))))
10049     (buffer-disable-undo (current-buffer))
10050     (bury-buffer gnus-dribble-buffer)
10051     (auto-save-mode t)
10052     (goto-char (point-max))))
10053
10054 (defun gnus-dribble-enter (string)
10055   (if (and (not gnus-dribble-ignore)
10056            gnus-dribble-buffer
10057            (buffer-name gnus-dribble-buffer))
10058       (let ((obuf (current-buffer)))
10059         (set-buffer gnus-dribble-buffer)
10060         (insert string "\n")
10061         (set-window-point (get-buffer-window (current-buffer)) (point-max))
10062         (set-buffer obuf))))
10063
10064 (defun gnus-dribble-read-file ()
10065   (let ((dribble-file (gnus-dribble-file-name)))
10066     (save-excursion 
10067       (set-buffer (setq gnus-dribble-buffer 
10068                         (get-buffer-create 
10069                          (file-name-nondirectory dribble-file))))
10070       (gnus-add-current-to-buffer-list)
10071       (erase-buffer)
10072       (set-visited-file-name dribble-file)
10073       (buffer-disable-undo (current-buffer))
10074       (bury-buffer (current-buffer))
10075       (set-buffer-modified-p nil)
10076       (let ((auto (make-auto-save-file-name))
10077             (gnus-dribble-ignore t))
10078         (if (or (file-exists-p auto) (file-exists-p dribble-file))
10079             (progn
10080               (if (file-newer-than-file-p auto dribble-file)
10081                   (setq dribble-file auto))
10082               (insert-file-contents dribble-file)
10083               (if (not (zerop (buffer-size)))
10084                   (set-buffer-modified-p t))
10085               (if (gnus-y-or-n-p 
10086                    "Auto-save file exists. Do you want to read it? ")
10087                   (progn
10088                     (message "Reading %s..." dribble-file) 
10089                     (eval-current-buffer)
10090                     (message "Reading %s...done" dribble-file)))))))))
10091
10092 (defun gnus-dribble-delete-file ()
10093   (if (file-exists-p (gnus-dribble-file-name))
10094       (delete-file (gnus-dribble-file-name)))
10095   (if gnus-dribble-buffer
10096       (save-excursion
10097         (set-buffer gnus-dribble-buffer)
10098         (let ((auto (make-auto-save-file-name)))
10099           (if (file-exists-p auto)
10100               (delete-file auto))
10101           (erase-buffer)
10102           (set-buffer-modified-p nil)))))
10103
10104 (defun gnus-dribble-save ()
10105   (if (and gnus-dribble-buffer
10106            (buffer-name gnus-dribble-buffer))
10107       (save-excursion
10108         (set-buffer gnus-dribble-buffer)
10109         (save-buffer))))
10110
10111 (defun gnus-dribble-clear ()
10112   (save-excursion
10113     (if (gnus-buffer-exists-p gnus-dribble-buffer)
10114         (progn
10115           (set-buffer gnus-dribble-buffer)
10116           (erase-buffer)
10117           (set-buffer-modified-p nil)
10118           (setq buffer-saved-size (buffer-size))))))
10119
10120 ;;;
10121 ;;; Server Communication
10122 ;;;
10123
10124 ;; All the Gnus backends have the same interface, and should return
10125 ;; data in a similar format. Below is an overview of what functions
10126 ;; these packages must supply and what results they should return.
10127 ;;
10128 ;; Variables:
10129 ;;
10130 ;; `nntp-server-buffer' - All data should be returned to Gnus in this
10131 ;; buffer. 
10132 ;;
10133 ;; Functions for the imaginary backend `choke':
10134 ;;
10135 ;; `choke-retrieve-headers ARTICLES &optional GROUP SERVER'
10136 ;; Should return all headers for all ARTICLES, or return NOV lines for
10137 ;; the same.
10138 ;;
10139 ;; `choke-request-group GROUP &optional SERVER DISCARD'
10140 ;; Switch to GROUP. If DISCARD is nil, active information on the group
10141 ;; must be returned.
10142 ;;
10143 ;; `choke-close-group GROUP &optional SERVER'
10144 ;; Close group. Most backends won't have to do anything with this
10145 ;; call, but it is an opportunity to clean up, if that is needed. It
10146 ;; is called when Gnus exits a group.
10147 ;;
10148 ;; `choke-request-article ARTICLE &optional GROUP SERVER'
10149 ;; Return ARTICLE, which is either an article number or
10150 ;; message-id. Note that not all backends can return articles based on
10151 ;; message-id. 
10152 ;;
10153 ;; `choke-request-list SERVER'
10154 ;; Return a list of all newsgroups on SERVER.
10155 ;;
10156 ;; `choke-request-list-newsgroups SERVER'
10157 ;; Return a list of descriptions of all newsgroups on SERVER.
10158 ;;
10159 ;; `choke-request-newgroups DATE &optional SERVER'
10160 ;; Return a list of all groups that have arrived after DATE on
10161 ;; SERVER. Note that the date doesn't have to be respected - Gnus will
10162 ;; always check whether the groups are old or not. Backends that do
10163 ;; not store date information may just return the entire list of
10164 ;; groups, although this might not be a good idea in general.
10165 ;;
10166 ;; `choke-request-post-buffer METHOD HEADER ARTICLE-BUFFER GROUP INFO'
10167 ;; Should return a buffer that is suitable for "posting". nnspool and
10168 ;; nntp return a `*post-buffer*', and nnmail return a `*mail*'
10169 ;; buffer. This function should fill out the appropriate headers. 
10170 ;;
10171 ;; `choke-request-post &optional SERVER'
10172 ;; Function that will be called from a buffer to be posted. 
10173 ;;
10174 ;; `choke-open-server SERVER &optional ARGUMENT'
10175 ;; Open a connection to SERVER.
10176 ;;
10177 ;; `choke-close-server &optional SERVER'
10178 ;; Close the connection to SERVER.
10179 ;;
10180 ;; `choke-server-opened &optional SERVER'
10181 ;; Whether the conenction to SERVER is opened or not.
10182 ;;
10183 ;; `choke-server-status &optional SERVER'
10184 ;; Should return a status string (not in the nntp buffer, but as the
10185 ;; result of the function).
10186 ;;
10187 ;; `choke-retrieve-groups GROUPS &optional SERVER'
10188 ;; Optional function for retrieving active file info on all groups in
10189 ;; GROUPS.  Two return formats are supported: The normal active file
10190 ;; format, and a list of GROUP lines.  This function should return (as
10191 ;; a function value) either `active' or `group', depending on what
10192 ;; format it returns.
10193 ;;
10194 ;; The following functions are optional and apply only to backends
10195 ;; that are able to control the contents of their groups totally
10196 ;; (ie. mail backends.)  Backends that aren't able to do that
10197 ;; shouldn't define these functions at all. Gnus will check for their
10198 ;; presence before attempting to call them.
10199 ;;
10200 ;; `choke-request-expire-articles ARTICLES &optional NEWSGROUP SERVER'
10201 ;; Should expire (according to some aging scheme) all ARTICLES. Most
10202 ;; backends will not be able to expire articles. Should return a list
10203 ;; of all articles that were not expired.
10204 ;;
10205 ;; `choke-request-move-article ARTICLE GROUP SERVER ACCEPT-FORM &optional LAST'
10206 ;; Should move ARTICLE from GROUP on SERVER by using ACCEPT-FORM.
10207 ;; Removes any information it has added to the article (extra headers,
10208 ;; whatever - make it as clean as possible), and then passes the
10209 ;; article on by evaling ACCEPT-FORM, which is normally a call to the
10210 ;; function described below. If the ACCEPT-FORM returns a non-nil
10211 ;; value, the article should then be deleted. If LAST is nil, that
10212 ;; means that there will be further calls to this function. This might
10213 ;; be taken as an advice not to save buffers/internal variables just
10214 ;; yet, but wait until the last call to speed things up.
10215 ;;
10216 ;; `choke-request-accept-article GROUP &optional LAST' 
10217 ;; The contents of the current buffer will be put into GROUP.  There
10218 ;; should, of course, be an article in the current buffer.  This
10219 ;; function is normally only called by the function described above,
10220 ;; and LAST works the same way as in that function.
10221 ;;
10222 ;; `choke-request-replace-article ARTICLE GROUP BUFFER'
10223 ;; Replace ARTICLE in GROUP with the contents of BUFFER.
10224 ;; This provides an easy interface for allowing editing of
10225 ;; articles. Note that even headers may be edited, so the backend has
10226 ;; to update any tables (nov buffers, etc) that it maintains after
10227 ;; replacing the article.
10228 ;;
10229 ;; `choke-request-create-group GROUP &optional SERVER'
10230 ;; Create GROUP on SERVER.  This might be a new, empty group, or it
10231 ;; might be a group that already exists, but hasn't been registered
10232 ;; yet. 
10233 ;;
10234 ;; All these functions must return nil if they couldn't service the
10235 ;; request. If the optional arguments are not supplied, some "current"
10236 ;; or "default" values should be used. In short, one should emulate an
10237 ;; NNTP server, in a way.
10238 ;;
10239 ;; If you want to write a new backend, you just have to supply the
10240 ;; functions listed above. In addition, you must enter the new backend
10241 ;; into the list of valid select methods:
10242 ;; (setq gnus-valid-select-methods 
10243 ;;       (cons '("choke" mail) gnus-valid-select-methods))
10244 ;; The first element in this list is the name of the backend. Other
10245 ;; elemnets may be `mail' (for mail groups),  `post' (for news
10246 ;; groups), `none' (neither), `respool' (for groups that can control
10247 ;; their contents). 
10248
10249 (defun gnus-start-news-server (&optional confirm)
10250   "Open a method for getting news.
10251 If CONFIRM is non-nil, the user will be asked for an NNTP server."
10252   (let (how where)
10253     (if gnus-current-select-method
10254         ;; Stream is already opened.
10255         nil
10256       ;; Open NNTP server.
10257       (if (null gnus-nntp-service) (setq gnus-nntp-server nil))
10258       (if confirm
10259           (progn
10260             ;; Read server name with completion.
10261             (setq gnus-nntp-server
10262                   (completing-read "NNTP server: "
10263                                    (mapcar (lambda (server) (list server))
10264                                            (cons (list gnus-nntp-server)
10265                                                  gnus-secondary-servers))
10266                                    nil nil gnus-nntp-server))))
10267
10268       (if (and gnus-nntp-server 
10269                (stringp gnus-nntp-server)
10270                (not (string= gnus-nntp-server "")))
10271           (setq gnus-select-method
10272                 (cond ((or (string= gnus-nntp-server "")
10273                            (string= gnus-nntp-server "::"))
10274                        (list 'nnspool (system-name)))
10275                       ((string-match "^:" gnus-nntp-server)
10276                        (list 'nnmh gnus-nntp-server 
10277                              (list 'nnmh-directory 
10278                                    (file-name-as-directory
10279                                     (expand-file-name
10280                                      (concat "~/" (substring
10281                                                    gnus-nntp-server 1)))))))
10282                       (t
10283                        (list 'nntp gnus-nntp-server)))))
10284
10285       (setq how (car gnus-select-method))
10286       (setq where (car (cdr gnus-select-method)))
10287       (cond ((eq how 'nnspool)
10288              (require 'nnspool)
10289              (message "Looking up local news spool..."))
10290             ((eq how 'nnmh)
10291              (require 'nnmh)
10292              (message "Looking up mh spool..."))
10293             (t
10294              (require 'nntp)))
10295       (setq gnus-current-select-method gnus-select-method)
10296       (run-hooks 'gnus-open-server-hook)
10297       (or 
10298        ;; gnus-open-server-hook might have opened it
10299        (gnus-server-opened gnus-select-method)  
10300        (gnus-open-server gnus-select-method)
10301        (gnus-y-or-n-p
10302         (format
10303          "%s server on %s can't be opened. Continue? "
10304          (car gnus-select-method) (nth 1 gnus-select-method)))
10305        (progn
10306          (message "Couldn't open server on %s" (nth 1 gnus-select-method))
10307          (ding)
10308          nil)))))
10309
10310 (defun gnus-check-news-server (method)
10311   "If the news server is down, start it up again."
10312   (let ((method (if method method gnus-select-method)))
10313     (and (stringp method)
10314          (setq method (gnus-server-to-method method)))
10315     (if (gnus-server-opened method)
10316         ;; Stream is already opened.
10317         t
10318       ;; Open server.
10319       (message "Opening server %s on %s..." (car method) (nth 1 method))
10320       (run-hooks 'gnus-open-server-hook)
10321       (or (gnus-server-opened method)
10322           (gnus-open-server method))
10323       (message ""))))
10324
10325 (defun gnus-nntp-message (&optional message)
10326   "Check the status of the NNTP server.
10327 If the status of the server is clear and MESSAGE is non-nil, MESSAGE
10328 is returned insted of the status string."
10329   (let ((status (gnus-status-message (gnus-find-method-for-group 
10330                                       gnus-newsgroup-name)))
10331         (message (or message "")))
10332     (if (and (stringp status) (> (length status) 0))
10333         status message)))
10334
10335 (defun gnus-get-function (method function)
10336   (and (stringp method)
10337        (setq method (gnus-server-to-method method)))
10338   (let ((func (intern (format "%s-%s" (car method) function))))
10339     (if (not (fboundp func)) 
10340         (progn
10341           (require (car method))
10342           (if (not (fboundp func)) 
10343               (error "No such function: %s" func))))
10344     func))
10345
10346 ;;; Interface functions to the backends.
10347
10348 (defun gnus-open-server (method)
10349   (funcall (gnus-get-function method 'open-server)
10350            (nth 1 method) (nthcdr 2 method)))
10351
10352 (defun gnus-close-server (method)
10353   (funcall (gnus-get-function method 'close-server) (nth 1 method)))
10354
10355 (defun gnus-request-list (method)
10356   (funcall (gnus-get-function method 'request-list) (nth 1 method)))
10357
10358 (defun gnus-request-list-newsgroups (method)
10359   (funcall (gnus-get-function method 'request-list-newsgroups) (nth 1 method)))
10360
10361 (defun gnus-request-newgroups (date method)
10362   (funcall (gnus-get-function method 'request-newgroups) 
10363            date (nth 1 method)))
10364
10365 (defun gnus-server-opened (method)
10366   (funcall (gnus-get-function method 'server-opened) (nth 1 method)))
10367
10368 (defun gnus-status-message (method)
10369   (let ((method (if (stringp method) (gnus-find-method-for-group method)
10370                   method)))
10371     (funcall (gnus-get-function method 'status-message) (nth 1 method))))
10372
10373 (defun gnus-request-group (group &optional dont-check)
10374   (let ((method (gnus-find-method-for-group group)))
10375     (funcall (gnus-get-function method 'request-group) 
10376              (gnus-group-real-name group) (nth 1 method) dont-check)))
10377
10378 (defun gnus-request-asynchronous (group &optional articles)
10379   (let ((method (gnus-find-method-for-group group)))
10380     (funcall (gnus-get-function method 'request-asynchronous) 
10381              (gnus-group-real-name group) (nth 1 method) articles)))
10382
10383 (defun gnus-list-active-group (group)
10384   (let ((method (gnus-find-method-for-group group))
10385         (func 'list-active-group))
10386     (and (gnus-check-backend-function func group)
10387          (funcall (gnus-get-function method func) 
10388                   (gnus-group-real-name group) (nth 1 method)))))
10389
10390 (defun gnus-request-group-description (group)
10391   (let ((method (gnus-find-method-for-group group))
10392         (func 'request-group-description))
10393     (and (gnus-check-backend-function func group)
10394          (funcall (gnus-get-function method func) 
10395                   (gnus-group-real-name group) (nth 1 method)))))
10396
10397 (defun gnus-close-group (group)
10398   (let ((method (gnus-find-method-for-group group)))
10399     (funcall (gnus-get-function method 'close-group) 
10400              (gnus-group-real-name group) (nth 1 method))))
10401
10402 (defun gnus-retrieve-headers (articles group)
10403   (let ((method (gnus-find-method-for-group group)))
10404     (funcall (gnus-get-function method 'retrieve-headers) 
10405              articles (gnus-group-real-name group) (nth 1 method))))
10406
10407 (defun gnus-retrieve-groups (groups method)
10408   (funcall (gnus-get-function method 'retrieve-groups) groups (nth 1 method)))
10409
10410 (defun gnus-request-article (article group &optional buffer)
10411   (let ((method (gnus-find-method-for-group group)))
10412     (funcall (gnus-get-function method 'request-article) 
10413              article (gnus-group-real-name group) (nth 1 method) buffer)))
10414
10415 (defun gnus-request-head (article group)
10416   (let ((method (gnus-find-method-for-group group)))
10417     (funcall (gnus-get-function method 'request-head) 
10418              article (gnus-group-real-name group) (nth 1 method))))
10419
10420 ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
10421 (defun gnus-request-post-buffer (post group subject header artbuf
10422                                       info follow-to respect-poster)
10423    (let* ((info (or info (and group (nth 2 (gnus-gethash 
10424                                             group gnus-newsrc-hashtb)))))
10425           (method
10426            (if (and gnus-post-method
10427                     ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
10428                     (memq 'post (assoc
10429                                  (format "%s" (car (gnus-find-method-for-group
10430                                                     gnus-newsgroup-name)))
10431                                         gnus-valid-select-methods)))
10432                gnus-post-method
10433              (gnus-find-method-for-group gnus-newsgroup-name))))
10434      (or (gnus-server-opened method)
10435          (gnus-open-server method)
10436          (error "Can't open server %s:%s" (car method) (nth 1 method)))
10437      (let ((mail-self-blind nil)
10438            (mail-archive-file-name nil))
10439        (funcall (gnus-get-function method 'request-post-buffer) 
10440                 post group subject header artbuf info follow-to
10441                 respect-poster))))
10442
10443 (defun gnus-request-post (method &optional force)
10444   (and (stringp method)
10445        (setq method (gnus-server-to-method method)))
10446   (and (not force) gnus-post-method
10447        (memq 'post (assoc (format "%s" (car method))
10448                           gnus-valid-select-methods))
10449        (setq method gnus-post-method))
10450   (funcall (gnus-get-function method 'request-post) 
10451            (nth 1 method)))
10452
10453 (defun gnus-request-expire-articles (articles group &optional force)
10454   (let ((method (gnus-find-method-for-group group)))
10455     (funcall (gnus-get-function method 'request-expire-articles) 
10456              articles (gnus-group-real-name group) (nth 1 method)
10457              force)))
10458
10459 (defun gnus-request-move-article 
10460   (article group server accept-function &optional last)
10461   (let ((method (gnus-find-method-for-group group)))
10462     (funcall (gnus-get-function method 'request-move-article) 
10463              article (gnus-group-real-name group) 
10464              (nth 1 method) accept-function last)))
10465
10466 (defun gnus-request-accept-article (group &optional last)
10467   (let ((func (if (symbolp group) group
10468                 (car (gnus-find-method-for-group group)))))
10469     (funcall (intern (format "%s-request-accept-article" func))
10470              (if (stringp group) (gnus-group-real-name group) group)
10471              last)))
10472
10473 (defun gnus-request-replace-article (article group buffer)
10474   (let ((func (car (gnus-find-method-for-group group))))
10475     (funcall (intern (format "%s-request-replace-article" func))
10476              article (gnus-group-real-name group) buffer)))
10477
10478 (defun gnus-request-create-group (group)
10479   (let ((method (gnus-find-method-for-group group)))
10480     (funcall (gnus-get-function method 'request-create-group) 
10481              (gnus-group-real-name group) (nth 1 method))))
10482
10483 (defun gnus-find-method-for-group (group &optional info)
10484   (or gnus-override-method
10485       (and (not group)
10486            gnus-select-method)
10487       (let ((info (or info (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
10488             method)
10489         (if (or (not info)
10490                 (not (setq method (nth 4 info))))
10491             (setq method gnus-select-method)
10492           (setq method
10493                 (cond ((stringp method)
10494                        (gnus-server-to-method method))
10495                       ((stringp (car method))
10496                        (gnus-server-extend-method group method))
10497                       (t
10498                        method))))
10499         (gnus-server-add-address method))))
10500
10501 (defun gnus-check-backend-function (func group)
10502   (let ((method (if (stringp group) (car (gnus-find-method-for-group group))
10503                  group)))
10504     (fboundp (intern (format "%s-%s" method func)))))
10505
10506 (defun gnus-methods-using (method)
10507   (let ((valids gnus-valid-select-methods)
10508         outs)
10509     (while valids
10510       (if (memq method (car valids)) 
10511           (setq outs (cons (car valids) outs)))
10512       (setq valids (cdr valids)))
10513     outs))
10514
10515 ;;; 
10516 ;;; Active & Newsrc File Handling
10517 ;;;
10518
10519 ;; Newsrc related functions.
10520 ;; Gnus internal format of gnus-newsrc-alist:
10521 ;; (("alt.general" 3 (1 . 1))
10522 ;;  ("alt.misc"    3 ((1 . 10) (12 . 15)))
10523 ;;  ("alt.test"    7 (1 . 99) (45 57 93)) ...)
10524 ;; The first item is the group name; the second is the subscription
10525 ;; level; the third is either a range of a list of ranges of read
10526 ;; articles, the optional fourth element is a list of marked articles,
10527 ;; the optional fifth element is the select method.
10528 ;;
10529 ;; Gnus internal format of gnus-newsrc-hashtb:
10530 ;; (95 ("alt.general" 3 (1 . 1)) ("alt.misc" 3 ((1 . 10) (12 . 15))) ...)
10531 ;; This is the entry for "alt.misc". The first element is the number
10532 ;; of unread articles in "alt.misc". The cdr of this entry is the
10533 ;; element *before* "alt.misc" in gnus-newsrc-alist, which makes is
10534 ;; trivial to remove or add new elements into gnus-newsrc-alist
10535 ;; without scanning the entire list. So, to get the actual information
10536 ;; of "alt.misc", you'd say something like 
10537 ;; (nth 2 (gnus-gethash "alt.misc" gnus-newsrc-hashtb))
10538 ;;
10539 ;; Gnus internal format of gnus-active-hashtb:
10540 ;; ((1 . 1))
10541 ;;  (5 . 10))
10542 ;;  (67 . 99)) ...)
10543 ;; The only element in each entry in this hash table is a range of
10544 ;; (possibly) available articles. (Articles in this range may have
10545 ;; been expired or cancelled.)
10546 ;;
10547 ;; Gnus internal format of gnus-killed-list and gnus-zombie-list:
10548 ;; ("alt.misc" "alt.test" "alt.general" ...)
10549
10550 (defun gnus-setup-news (&optional rawfile level)
10551   "Setup news information.
10552 If RAWFILE is non-nil, the .newsrc file will also be read.
10553 If LEVEL is non-nil, the news will be set up at level LEVEL."
10554   (let ((init (not (and gnus-newsrc-alist gnus-active-hashtb (not rawfile)))))
10555     ;; Clear some variables to re-initialize news information.
10556     (if init (setq gnus-newsrc-alist nil gnus-active-hashtb nil))
10557
10558     ;; Read the newsrc file and create `gnus-newsrc-hashtb'.
10559     (if init (gnus-read-newsrc-file rawfile))
10560
10561     ;; Read the active file and create `gnus-active-hashtb'.
10562     ;; If `gnus-read-active-file' is nil, then we just create an empty
10563     ;; hash table. The partial filling out of the hash table will be
10564     ;; done in `gnus-get-unread-articles'.
10565     (if (and gnus-read-active-file 
10566              (not level)
10567              (gnus-server-opened gnus-select-method))
10568         (gnus-read-active-file)
10569       (setq gnus-active-hashtb (make-vector 4095 0)))
10570
10571     (and init gnus-use-dribble-file (gnus-dribble-read-file))
10572
10573     ;; Find the number of unread articles in each non-dead group.
10574     (gnus-get-unread-articles (or level (1+ gnus-level-subscribed)))
10575     ;; Find new newsgroups and treat them.
10576     (if (and init gnus-check-new-newsgroups gnus-read-active-file (not level)
10577              (gnus-server-opened gnus-select-method))
10578         (gnus-find-new-newsgroups))
10579     (if (and init gnus-check-bogus-newsgroups 
10580              gnus-read-active-file (not level)
10581              (gnus-server-opened gnus-select-method))
10582         (gnus-check-bogus-newsgroups))))
10583
10584 (defun gnus-find-new-newsgroups ()
10585   "Search for new newsgroups and add them.
10586 Each new newsgroup will be treated with `gnus-subscribe-newsgroup-method.'
10587 The `-n' option line from .newsrc is respected."
10588   (interactive)
10589   (or (gnus-check-first-time-used)
10590       (if (or (consp gnus-check-new-newsgroups)
10591               (eq gnus-check-new-newsgroups 'ask-server))
10592           (gnus-ask-server-for-new-groups)
10593         (let ((groups 0)
10594               group new-newsgroups)
10595           (or gnus-have-read-active-file (gnus-read-active-file))
10596           (setq gnus-newsrc-last-checked-date (current-time-string))
10597           (if (not gnus-killed-hashtb) (gnus-make-hashtable-from-killed))
10598           ;; Go though every newsgroup in `gnus-active-hashtb' and compare
10599           ;; with `gnus-newsrc-hashtb' and `gnus-killed-hashtb'.
10600           (mapatoms
10601            (lambda (sym)
10602              (setq group (symbol-name sym))
10603              (if (or (gnus-gethash group gnus-killed-hashtb)
10604                      (gnus-gethash group gnus-newsrc-hashtb))
10605                  ()
10606                (let ((do-sub (gnus-matches-options-n group)))
10607                  (cond ((eq do-sub 'subscribe)
10608                         (setq groups (1+ groups))
10609                         (gnus-sethash group group gnus-killed-hashtb)
10610                         (funcall 
10611                          gnus-subscribe-options-newsgroup-method group))
10612                        ((eq do-sub 'ignore)
10613                         nil)
10614                        (t
10615                         (setq groups (1+ groups))
10616                         (gnus-sethash group group gnus-killed-hashtb)
10617                         (if gnus-subscribe-hierarchical-interactive
10618                             (setq new-newsgroups (cons group new-newsgroups))
10619                           (funcall gnus-subscribe-newsgroup-method group)))))))
10620            gnus-active-hashtb)
10621           (if new-newsgroups 
10622               (gnus-subscribe-hierarchical-interactive new-newsgroups))
10623           ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
10624           (if (> groups 0)
10625               (message "%d new newsgroup%s arrived." 
10626                        groups (if (> groups 1) "s have" " has")))))))
10627
10628 (defun gnus-matches-options-n (group)
10629   ;; Returns `subscribe' if the group is to be uncoditionally
10630   ;; subscribed, `ignore' if it is to be ignored, and nil if there is
10631   ;; no match for the group.
10632
10633   ;; First we check the two user variables.
10634   (cond
10635    ((and gnus-options-not-subscribe
10636          (string-match gnus-options-not-subscribe group))
10637     'subscribe)
10638    ((and gnus-options-subscribe
10639          (string-match gnus-options-subscribe group))
10640     'ignore)
10641    ;; Then we go through the list that was retrieved from the .newsrc
10642    ;; file.  This list has elements on the form 
10643    ;; `(REGEXP . {ignore,subscribe})'. The first match found (the list
10644    ;; is in the reverse order of the options line) is returned.
10645    (t
10646     (let ((regs gnus-newsrc-options-n))
10647       (while (and regs
10648                   (not (string-match (car (car gnus-newsrc-options-n)) group)))
10649         (setq regs (cdr regs)))
10650       (and regs (cdr (car regs)))))))
10651
10652 (defun gnus-ask-server-for-new-groups ()
10653   (let* ((date (or gnus-newsrc-last-checked-date (current-time-string)))
10654          (methods (cons gnus-select-method 
10655                         (append
10656                          (and (consp gnus-check-new-newsgroups)
10657                               gnus-check-new-newsgroups)
10658                          gnus-secondary-select-methods)))
10659          (groups 0)
10660          (new-date (current-time-string))
10661          hashtb group new-newsgroups got-new)
10662     ;; Go thorugh both primary and secondary select methods and
10663     ;; request new newsgroups.  
10664     (while methods
10665       (if (gnus-request-newgroups date (car methods))
10666           (save-excursion
10667             (setq got-new t)
10668             (or hashtb (setq hashtb (gnus-make-hashtable 
10669                                      (count-lines (point-min) (point-max)))))
10670             (set-buffer nntp-server-buffer)
10671             ;; Enter all the new groups in a hashtable.
10672             (gnus-active-to-gnus-format (car methods) hashtb)))
10673       (setq methods (cdr methods)))
10674     (and got-new (setq gnus-newsrc-last-checked-date new-date))
10675     ;; Now all new groups from all select methods are in `hashtb'.
10676     (mapatoms
10677      (lambda (group-sym)
10678        (setq group (symbol-name group-sym))
10679        (if (or (gnus-gethash group gnus-newsrc-hashtb)
10680                (member group gnus-zombie-list)
10681                (member group gnus-killed-list))
10682            ;; The group is already known.
10683            ()
10684          (gnus-sethash group (symbol-value group-sym) gnus-active-hashtb)
10685          (let ((do-sub (gnus-matches-options-n group)))
10686            (cond ((eq do-sub 'subscribe)
10687                   (setq groups (1+ groups))
10688                   (gnus-sethash group group gnus-killed-hashtb)
10689                   (funcall 
10690                    gnus-subscribe-options-newsgroup-method group))
10691                  ((eq do-sub 'ignore)
10692                   nil)
10693                  (t
10694                   (setq groups (1+ groups))
10695                   (gnus-sethash group group gnus-killed-hashtb)
10696                   (if gnus-subscribe-hierarchical-interactive
10697                       (setq new-newsgroups (cons group new-newsgroups))
10698                     (funcall gnus-subscribe-newsgroup-method group)))))))
10699      hashtb)
10700     (if new-newsgroups 
10701         (gnus-subscribe-hierarchical-interactive new-newsgroups))
10702     ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
10703     (if (> groups 0)
10704         (message "%d new newsgroup%s arrived." 
10705                  groups (if (> groups 1) "s have" " has")))
10706     got-new))
10707
10708 (defun gnus-check-first-time-used ()
10709   (if (or (> (length gnus-newsrc-alist) 1)
10710           (file-exists-p gnus-startup-file)
10711           (file-exists-p (concat gnus-startup-file ".el"))
10712           (file-exists-p (concat gnus-startup-file ".eld")))
10713       nil
10714     (message "First time user; subscribing you to default groups")
10715     (or gnus-have-read-active-file (gnus-read-active-file))
10716     (setq gnus-newsrc-last-checked-date (current-time-string))
10717     (let ((groups gnus-default-subscribed-newsgroups)
10718           group)
10719       (if (eq groups t)
10720           nil
10721         (setq groups (or groups gnus-backup-default-subscribed-newsgroups))
10722         (mapatoms
10723          (lambda (sym)
10724            (setq group (symbol-name sym))
10725            (let ((do-sub (gnus-matches-options-n group)))
10726              (cond ((eq do-sub 'subscribe)
10727                     (gnus-sethash group group gnus-killed-hashtb)
10728                     (funcall 
10729                      gnus-subscribe-options-newsgroup-method group))
10730                    ((eq do-sub 'ignore)
10731                     nil)
10732                    (t
10733                     (setq gnus-killed-list (cons group gnus-killed-list))))))
10734          gnus-active-hashtb)
10735         (while groups
10736           (if (gnus-gethash (car groups) gnus-active-hashtb)
10737               (gnus-group-change-level 
10738                (car groups) gnus-level-default-subscribed gnus-level-killed))
10739           (setq groups (cdr groups)))
10740         (gnus-group-make-help-group)
10741         (and gnus-novice-user
10742              (message (substitute-command-keys "\\<gnus-group-mode-map>\\[gnus-group-list-killed] to list killed groups")))))))
10743
10744 ;; `gnus-group-change-level' is the fundamental function for changing
10745 ;; subscription levels of newsgroups. This might mean just changing
10746 ;; from level 1 to 2, which is pretty trivial, from 2 to 6 or back
10747 ;; again, which subscribes/unsubscribes a group, which is equally
10748 ;; trivial. Changing from 1-7 to 8-9 means that you kill a group, and
10749 ;; from 8-9 to 1-7 means that you remove the group from the list of
10750 ;; killed (or zombie) groups and add them to the (kinda) subscribed
10751 ;; groups. And last but not least, moving from 8 to 9 and 9 to 8,
10752 ;; which is trivial.
10753 ;; ENTRY can either be a string (newsgroup name) or a list (if
10754 ;; FROMKILLED is t, it's a list on the format (NUM INFO-LIST),
10755 ;; otherwise it's a list in the format of the `gnus-newsrc-hashtb'
10756 ;; entries. 
10757 ;; LEVEL is the new level of the group, OLDLEVEL is the old level and
10758 ;; PREVIOUS is the group (in hashtb entry format) to insert this group
10759 ;; after. 
10760 (defun gnus-group-change-level (entry level &optional oldlevel
10761                                       previous fromkilled)
10762   (let ((pinfo entry)
10763         group info active num)
10764     ;; Glean what info we can from the arguments
10765     (if (consp entry)
10766         (if fromkilled (setq group (nth 1 entry))
10767           (setq group (car (nth 2 entry))))
10768       (setq group entry))
10769     (if (and (stringp entry)
10770              oldlevel 
10771              (< oldlevel gnus-level-zombie))
10772         (setq entry (gnus-gethash entry gnus-newsrc-hashtb)))
10773     (if (and (not oldlevel)
10774              (listp entry))
10775         (setq oldlevel (car (cdr (nth 2 entry)))))
10776     (if (stringp previous)
10777         (setq previous (gnus-gethash previous gnus-newsrc-hashtb)))
10778
10779     (gnus-dribble-enter
10780      (format "(gnus-group-change-level %S %S %S %S %S)" 
10781              group level oldlevel (car (nth 2 previous)) fromkilled))
10782     
10783     ;; Then we remove the newgroup from any old structures, if needed.
10784     ;; If the group was killed, we remove it from the killed or zombie
10785     ;; list. If not, and it is in fact going to be killed, we remove
10786     ;; it from the newsrc hash table and assoc.
10787     (cond ((>= oldlevel gnus-level-zombie)
10788            (if (= oldlevel gnus-level-zombie)
10789                (setq gnus-zombie-list (delete group gnus-zombie-list))
10790              (setq gnus-killed-list (delete group gnus-killed-list))))
10791           (t
10792            (if (>= level gnus-level-zombie)
10793                (progn
10794                  (gnus-sethash (car (nth 2 entry))
10795                                nil gnus-newsrc-hashtb)
10796                  (if (nth 3 entry)
10797                      (setcdr (gnus-gethash (car (nth 3 entry))
10798                                            gnus-newsrc-hashtb)
10799                              (cdr entry)))
10800                  (setcdr (cdr entry) (cdr (cdr (cdr entry))))))))
10801
10802     ;; Finally we enter (if needed) the list where it is supposed to
10803     ;; go, and change the subscription level. If it is to be killed,
10804     ;; we enter it into the killed or zombie list.
10805     (cond ((>= level gnus-level-zombie)
10806            (and (string= group (gnus-group-real-name group))
10807                 (if (= level gnus-level-zombie)
10808                     (setq gnus-zombie-list (cons group gnus-zombie-list))
10809                   (setq gnus-killed-list (cons group gnus-killed-list)))))
10810           (t
10811            ;; If the list is to be entered into the newsrc assoc, and
10812            ;; it was killed, we have to create an entry in the newsrc
10813            ;; hashtb format and fix the pointers in the newsrc assoc.
10814            (if (>= oldlevel gnus-level-zombie)
10815                (progn
10816                  (if (listp entry)
10817                      (progn
10818                        (setq info (cdr entry))
10819                        (setq num (car entry)))
10820                    (setq active (gnus-gethash group gnus-active-hashtb))
10821                    (setq num (if active (- (1+ (cdr active)) (car active)) t))
10822                    ;; Check whether the group is foreign. If so, the
10823                    ;; foreign select method has to be entered into the
10824                    ;; info. 
10825                    (let ((method (gnus-group-method-name group)))
10826                      (if (eq method gnus-select-method)
10827                          (setq info (list group level nil))
10828                        (setq info (list group level nil nil method)))))
10829                  (setq entry (cons info (if previous (cdr (cdr previous))
10830                                           (cdr gnus-newsrc-alist))))
10831                  (setcdr (if previous (cdr previous) gnus-newsrc-alist)
10832                          entry)
10833                  (gnus-sethash group (cons num (if previous (cdr previous)
10834                                                  gnus-newsrc-alist))
10835                                gnus-newsrc-hashtb)
10836                  (if (cdr entry)
10837                      (setcdr (gnus-gethash (car (car (cdr entry)))
10838                                            gnus-newsrc-hashtb)
10839                              entry)))
10840              ;; It was alive, and it is going to stay alive, so we
10841              ;; just change the level and don't change any pointers or
10842              ;; hash table entries.
10843              (setcar (cdr (car (cdr (cdr entry)))) level))))))
10844
10845 (defun gnus-kill-newsgroup (newsgroup)
10846   "Obsolete function. Kills a newsgroup."
10847   (gnus-group-change-level
10848    (gnus-gethash newsgroup gnus-newsrc-hashtb) gnus-level-killed))
10849
10850 (defun gnus-check-bogus-newsgroups (&optional confirm)
10851   "Remove bogus newsgroups.
10852 If CONFIRM is non-nil, the user has to confirm the deletion of every
10853 newsgroup." 
10854   (let ((newsrc (cdr gnus-newsrc-alist))
10855         bogus group)
10856     (message "Checking bogus newsgroups...")
10857     (or gnus-have-read-active-file (gnus-read-active-file))
10858     ;; Find all bogus newsgroup that are subscribed.
10859     (while newsrc
10860       (setq group (car (car newsrc)))
10861       (if (or (gnus-gethash group gnus-active-hashtb)
10862               (nth 4 (car newsrc))
10863               (and confirm
10864                    (not (gnus-y-or-n-p
10865                          (format "Remove bogus newsgroup: %s " group)))))
10866           ;; Active newsgroup.
10867           ()
10868         ;; Found a bogus newsgroup.
10869         (setq bogus (cons group bogus)))
10870       (setq newsrc (cdr newsrc)))
10871     ;; Remove all bogus subscribed groups by first killing them, and
10872     ;; then removing them from the list of killed groups.
10873     (while bogus
10874       (gnus-group-change-level 
10875        (gnus-gethash (car bogus) gnus-newsrc-hashtb) gnus-level-killed)
10876       (setq gnus-killed-list (delete (car bogus) gnus-killed-list))
10877       (setq bogus (cdr bogus)))
10878     ;; Then we remove all bogus groups from the list of killed and
10879     ;; zombie groups. They are are removed without confirmation.
10880     (let ((dead-lists '(gnus-killed-list gnus-zombie-list))
10881           killed)
10882       (while dead-lists
10883         (setq killed (symbol-value (car dead-lists)))
10884         (while killed
10885           (setq group (car killed))
10886           (or (gnus-gethash group gnus-active-hashtb)
10887               ;; The group is bogus.
10888               (set (car dead-lists)
10889                    (delete group (symbol-value (car dead-lists)))))
10890           (setq killed (cdr killed)))
10891         (setq dead-lists (cdr dead-lists))))
10892     (message "Checking bogus newsgroups... done")))
10893
10894 (defun gnus-check-duplicate-killed-groups ()
10895   "Remove duplicates from the list of killed groups."
10896   (interactive)
10897   (let ((killed gnus-killed-list))
10898     (while killed
10899       (message "%d" (length killed))
10900       (setcdr killed (delete (car killed) (cdr killed)))
10901       (setq killed (cdr killed)))))
10902
10903 ;; Go though `gnus-newsrc-alist' and compare with `gnus-active-hashtb'
10904 ;; and compute how many unread articles there are in each group.
10905 (defun gnus-get-unread-articles (&optional level)
10906   (let ((newsrc (cdr gnus-newsrc-alist))
10907         (level (or level (1+ gnus-level-subscribed)))
10908         info group active virtuals)
10909     (message "Checking new news...")
10910     (while newsrc
10911       (setq info (car newsrc))
10912       (setq group (car info))
10913       (setq active (gnus-gethash group gnus-active-hashtb))
10914
10915       ;; Check newsgroups. If the user doesn't want to check them, or
10916       ;; they can't be checked (for instance, if the news server can't
10917       ;; be reached) we just set the number of unread articles in this
10918       ;; newsgroup to t. This means that Gnus thinks that there are
10919       ;; unread articles, but it has no idea how many.
10920       (if (nth 4 info)
10921           (if (or (and gnus-activate-foreign-newsgroups 
10922                        (not (numberp gnus-activate-foreign-newsgroups)))
10923                   (and (numberp gnus-activate-foreign-newsgroups)
10924                        (<= (nth 1 info) gnus-activate-foreign-newsgroups)
10925                        (<= (nth 1 info) level)))
10926               (if (eq (car (nth 4 info)) 'nnvirtual)
10927                   (setq virtuals (cons info virtuals))
10928                 (setq active (gnus-activate-newsgroup (car info)))))
10929         (if (and (not gnus-read-active-file)
10930                  (<= (nth 1 info) level))
10931             (progn
10932               (setq active (gnus-activate-newsgroup (car info))))))
10933       
10934       (or active (progn (gnus-sethash group nil gnus-active-hashtb)
10935                         (setcar (gnus-gethash group gnus-newsrc-hashtb) t)))
10936       (and active 
10937            (gnus-get-unread-articles-in-group info active)
10938            ;; Close the groups as we look at them!
10939            (gnus-close-group group))
10940       (setq newsrc (cdr newsrc)))
10941
10942     ;; Activate the virtual groups. This has to be done after all the
10943     ;; other groups. 
10944     ;; !!! If one virtual group contains another virtual group, even
10945     ;; doing it this way might cause problems.
10946    (while virtuals
10947       (and (setq active (gnus-activate-newsgroup (car (car virtuals))))
10948            (gnus-get-unread-articles-in-group (car virtuals) active))
10949       (setq virtuals (cdr virtuals)))
10950
10951     (message "Checking new news... done")))
10952
10953 ;; Create a hash table out of the newsrc alist. The `car's of the
10954 ;; alist elements are used as keys.
10955 (defun gnus-make-hashtable-from-newsrc-alist ()
10956   (let ((alist gnus-newsrc-alist)
10957          prev)
10958     (setq gnus-newsrc-hashtb (gnus-make-hashtable (length alist)))
10959     (setq alist 
10960           (setq prev (setq gnus-newsrc-alist 
10961                            (cons (list "dummy.group" 0 nil) alist))))
10962     (while alist
10963       (gnus-sethash (car (car alist)) (cons nil prev) gnus-newsrc-hashtb)
10964       (setq prev alist)
10965       (setq alist (cdr alist)))))
10966
10967 (defun gnus-make-hashtable-from-killed ()
10968   "Create a hash table from the killed and zombie lists."
10969   (let ((lists '(gnus-killed-list gnus-zombie-list))
10970         list)
10971     (setq gnus-killed-hashtb 
10972           (gnus-make-hashtable 
10973            (+ (length gnus-killed-list) (length gnus-zombie-list))))
10974     (while lists
10975       (setq list (symbol-value (car lists)))
10976       (setq lists (cdr lists))
10977       (while list
10978         (gnus-sethash (car list) (car list) gnus-killed-hashtb)
10979         (setq list (cdr list))))))
10980
10981 (defun gnus-get-unread-articles-in-group (info active)
10982   (let* ((range (nth 2 info))
10983          (num 0)
10984          (marked (nth 3 info))
10985          srange lowest group highest)
10986     ;; Modify the list of read articles according to what articles 
10987     ;; are available; then tally the unread articles and add the
10988     ;; number to the group hash table entry.
10989     (cond ((zerop (cdr active))
10990            (setq num 0))
10991           ((not range)
10992            (setq num (- (1+ (cdr active)) (car active))))
10993           ((not (listp (cdr range)))
10994            ;; Fix a single (num . num) range according to the
10995            ;; active hash table.
10996            ;; Fix by Carsten Bormann <cabo@Informatik.Uni-Bremen.DE>.
10997            (and (< (cdr range) (car active)) (setcdr range (1- (car active))))
10998            (and (> (cdr range) (cdr active)) (setcdr range (cdr active)))
10999            ;; Compute number of unread articles.
11000            (setq num (max 0 (- (cdr active) 
11001                                (- (1+ (cdr range)) (car range))))))
11002           (t
11003            ;; The read list is a list of ranges. Fix them according to
11004            ;; the active hash table.
11005            ;; First peel off any elements that are below the lower
11006            ;; active limit. 
11007            (while (and (cdr range) 
11008                        (>= (car active) 
11009                            (or (and (atom (car (cdr range))) (car (cdr range)))
11010                                (car (car (cdr range))))))
11011              (if (numberp (car range))
11012                  (setcar range 
11013                          (cons (car range) 
11014                                (or (and (numberp (car (cdr range)))
11015                                         (car (cdr range))) 
11016                                    (cdr (car (cdr range))))))
11017                (setcdr (car range) 
11018                        (or (and (numberp (nth 1 range)) (nth 1 range))
11019                            (cdr (car (cdr range))))))
11020              (setcdr range (cdr (cdr range))))
11021            ;; Adjust the first element to be the same as the lower limit. 
11022            (if (and (not (atom (car range))) 
11023                     (< (cdr (car range)) (car active)))
11024                (setcdr (car range) (1- (car active))))
11025            ;; Then we want to peel off any elements that are higher
11026            ;; than the upper active limit.  
11027            (let ((srange range))
11028              ;; Go past all legal elements.
11029              (while (and (cdr srange) 
11030                          (<= (or (and (atom (car (cdr srange)))
11031                                       (car (cdr srange)))
11032                                  (car (car (cdr srange)))) (cdr active)))
11033                (setq srange (cdr srange)))
11034              (if (cdr srange)
11035                  ;; Nuke all remaining illegal elements.
11036                  (setcdr srange nil))
11037
11038              ;; Adjust the final element.
11039              (if (and (not (atom (car srange)))
11040                       (> (cdr (car srange)) (cdr active)))
11041                  (setcdr (car srange) (cdr active))))
11042            ;; Compute the number of unread articles.
11043            (while range
11044              (setq num (+ num (- (1+ (or (and (atom (car range)) (car range))
11045                                          (cdr (car range))))
11046                                  (or (and (atom (car range)) (car range))
11047                                      (car (car range))))))
11048              (setq range (cdr range)))
11049            (setq num (max 0 (- (cdr active) num)))))
11050     (and info
11051          (progn
11052            (and (assq 'tick marked)
11053                 (inline (gnus-remove-illegal-marked-articles
11054                          (assq 'tick marked) (nth 2 info))))
11055            (and (assq 'dormant marked)
11056                 (inline (gnus-remove-illegal-marked-articles
11057                          (assq 'dormant marked) (nth 2 info))))
11058            (setcar
11059             (gnus-gethash (car info) gnus-newsrc-hashtb) 
11060             (setq num (max 0 (- num (length (cdr (assq 'tick marked)))
11061                                 (length (cdr (assq 'dormant marked)))))))))
11062     num))
11063
11064 (defun gnus-remove-illegal-marked-articles (marked ranges)
11065   (let ((m (cdr marked)))
11066     ;; Make sure that all ticked articles are a subset of the unread
11067     ;; articles. 
11068     (while m
11069       (if (gnus-member-of-range (car m) ranges)
11070           (setcdr marked (cdr m))
11071         (setq marked m))
11072       (setq m (cdr m)))))
11073
11074 (defun gnus-activate-newsgroup (group)
11075   (let ((method (gnus-find-method-for-group group))
11076         active)
11077     (and (or (gnus-server-opened method) (gnus-open-server method))
11078          (gnus-request-group group)
11079          (save-excursion
11080            (set-buffer nntp-server-buffer)
11081            (goto-char 1)
11082            (and (looking-at "[0-9]+ [0-9]+ \\([0-9]+\\) [0-9]+")
11083                 (progn
11084                   (goto-char (match-beginning 1))
11085                   (gnus-sethash 
11086                    group (setq active (cons (read (current-buffer))
11087                                             (read (current-buffer))))
11088                    gnus-active-hashtb))
11089                 active)))))
11090
11091 (defun gnus-update-read-articles 
11092   (group unread unselected ticked &optional domarks replied expirable killed
11093          dormant bookmark score)
11094   "Update the list of read and ticked articles in GROUP using the
11095 UNREAD and TICKED lists.
11096 Note: UNSELECTED has to be sorted over `<'.
11097 Returns whether the updating was successful."
11098   (let* ((active (or gnus-newsgroup-active 
11099                      (gnus-gethash group gnus-active-hashtb)))
11100          (entry (gnus-gethash group gnus-newsrc-hashtb))
11101          (number (car entry))
11102          (info (nth 2 entry))
11103          (marked (nth 3 info))
11104          (prev 1)
11105          (unread (sort (copy-sequence unread) (function <)))
11106          last read)
11107     (if (or (not info) (not active))
11108         ;; There is no info on this group if it was, in fact,
11109         ;; killed. Gnus stores no information on killed groups, so
11110         ;; there's nothing to be done. 
11111         ;; One could store the information somewhere temporarily,
11112         ;; perhaps... Hmmm... 
11113         ()
11114       ;; Remove any negative articles numbers.
11115       (while (and unread (< (car unread) 0))
11116         (setq unread (cdr unread)))
11117       (setq unread (sort (append unselected unread) '<))
11118       ;; Set the number of unread articles in gnus-newsrc-hashtb.
11119       (setcar entry (max 0 (- (length unread) (length ticked) 
11120                               (length dormant))))
11121       ;; Compute the ranges of read articles by looking at the list of
11122       ;; unread articles.  
11123       (while unread
11124         (if (/= (car unread) prev)
11125             (setq read (cons (if (= prev (1- (car unread))) prev
11126                                (cons prev (1- (car unread)))) read)))
11127         (setq prev (1+ (car unread)))
11128         (setq unread (cdr unread)))
11129       (if (<= prev (cdr active))
11130           (setq read (cons (cons prev (cdr active)) read)))
11131       ;; Enter this list into the group info.
11132       (setcar (cdr (cdr info)) 
11133               (if (> (length read) 1) (nreverse read) read))
11134       ;; Enter the list of ticked articles.
11135       (gnus-set-marked-articles 
11136        info ticked
11137        (if domarks replied (cdr (assq 'reply marked)))
11138        (if domarks expirable (cdr (assq 'expire marked)))
11139        (if domarks killed (cdr (assq 'killed marked)))
11140        (if domarks dormant (cdr (assq 'dormant marked)))
11141        (if domarks bookmark (cdr (assq 'bookmark marked)))
11142        (if domarks score (cdr (assq 'score marked))))
11143       t)))
11144
11145 (defun gnus-make-articles-unread (group articles)
11146   "Mark ARTICLES in GROUP as unread."
11147   (let ((info (nth 2 (or (gnus-gethash group gnus-newsrc-hashtb)
11148                          (gnus-gethash (gnus-group-real-name group)
11149                                        gnus-newsrc-hashtb)))))
11150     (setcar (nthcdr 2 info)
11151             (gnus-remove-from-range (nth 2 info) articles))
11152     (gnus-group-update-group group t)))
11153
11154 (defun gnus-read-active-file ()
11155   "Get active file from NNTP server."
11156   (gnus-group-set-mode-line)
11157   (let ((methods (cons gnus-select-method gnus-secondary-select-methods))
11158         list-type)
11159     (setq gnus-have-read-active-file nil)
11160     (save-excursion
11161       (set-buffer nntp-server-buffer)
11162       (while methods
11163         (let* ((where (nth 1 (car methods)))
11164                (mesg (format "Reading active file%s via %s..."
11165                              (if (and where (not (zerop (length where))))
11166                                  (concat " from " where) "")
11167                              (car (car methods)))))
11168           (message mesg)
11169           (gnus-check-news-server (car methods))
11170           (cond 
11171            ((and (eq gnus-read-active-file 'some)
11172                  (gnus-check-backend-function
11173                   'retrieve-groups (car (car methods))))
11174             (let ((newsrc (cdr gnus-newsrc-alist))
11175                   groups)
11176               (while newsrc
11177                 (and (gnus-server-equal 
11178                       (gnus-find-method-for-group
11179                        (car (car newsrc)) (car newsrc))
11180                       (gnus-server-get-method nil (car methods)))
11181                      (setq groups (cons (car (car newsrc)) groups)))
11182                 (setq newsrc (cdr newsrc)))
11183               (setq list-type (gnus-retrieve-groups groups (car methods)))
11184               (cond ((not list-type)
11185                      (message "Cannot read partial active file from %s server." 
11186                               (car (car methods)))
11187                      (ding)
11188                      (sit-for 2))
11189                     ((eq list-type 'active)
11190                      (gnus-active-to-gnus-format 
11191                       (and gnus-have-read-active-file (car methods)))
11192                      (setq gnus-have-read-active-file t))
11193                     (t
11194                      (gnus-groups-to-gnus-format
11195                       (and gnus-have-read-active-file (car methods)))
11196                      (setq gnus-have-read-active-file t)))))
11197            (t
11198             (if (not (gnus-request-list (car methods)))
11199                 (progn
11200                   (message "Cannot read active file from %s server." 
11201                            (car (car methods)))
11202                   (ding))
11203               (gnus-active-to-gnus-format 
11204                (and gnus-have-read-active-file (car methods)))
11205               (setq gnus-have-read-active-file t)
11206               (message "%s...done" mesg)))))
11207         (setq methods (cdr methods))))))
11208
11209 ;; rewritten by jwz based on ideas from Rick Sladkey <jrs@world.std.com>
11210 ;; Further rewrites by lmi.
11211 (defun gnus-active-to-gnus-format (method &optional hashtb)
11212   "Convert active file format to internal format.
11213 Lines matching `gnus-ignored-newsgroups' are ignored."
11214   (let ((cur (current-buffer))
11215         (hashtb (or hashtb 
11216                     (if method
11217                         gnus-active-hashtb
11218                       (setq gnus-active-hashtb
11219                             (gnus-make-hashtable 
11220                              (count-lines (point-min) (point-max))))))))
11221     ;; Delete unnecessary lines.
11222     (goto-char (point-min))
11223     (delete-matching-lines gnus-ignored-newsgroups)
11224     (and method (not (eq method gnus-select-method))
11225          (let ((prefix (gnus-group-prefixed-name "" method)))
11226            (goto-char (point-min))
11227            (while (and (not (eobp))
11228                        (null (insert prefix))
11229                        (zerop (forward-line 1))))))
11230     (goto-char (point-min))
11231     ;; Store active file in hashtable.
11232     (save-restriction
11233       (if (or (re-search-forward "\n.\r?$" nil t)
11234               (goto-char (point-max)))
11235           (progn
11236             (beginning-of-line)
11237             (narrow-to-region (point-min) (point))))
11238       (goto-char (point-min))
11239       (if (string-match "%[oO]" gnus-group-line-format)
11240           ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
11241           ;; If we want information on moderated groups, we use this
11242           ;; loop...   
11243           (condition-case ()
11244               (let ((mod-hashtb (make-vector 7 0))
11245                     group max mod)
11246                 (while (not (eobp))
11247                   (setq group (let ((obarray hashtb))
11248                                 (read cur)))
11249                   (setq max (read cur))
11250                   (set group (cons (read cur) max))
11251                   ;; Enter moderated groups into a list.
11252                   (if (string= 
11253                        (symbol-name  (let ((obarray mod-hashtb)) (read cur)))
11254                        "m")
11255                       (setq gnus-moderated-list 
11256                             (cons (symbol-name group) gnus-moderated-list)))
11257                   (forward-line 1)))
11258             (error 
11259              (progn (ding) (message "Possible error in active file."))))
11260         ;; And if we do not care about moderation, we use this loop,
11261         ;; which is faster.
11262         (condition-case ()
11263             (let (group max)
11264               (while (not (eobp))
11265                 ;; group gets set to a symbol interned in the hash table
11266                 ;; (what a hack!!)
11267                 (setq group (let ((obarray hashtb)) (read cur)))
11268                 (setq max (read cur))
11269                 (set group (cons (read cur) max))
11270                 (forward-line 1)))
11271           (error 
11272            (progn (ding) (message "Possible error in active file."))))))))
11273
11274 (defun gnus-groups-to-gnus-format (method &optional hashtb)
11275   ;; Parse a "groups" active file.
11276   (let ((cur (current-buffer))
11277         (hashtb (or hashtb 
11278                     (if method
11279                         gnus-active-hashtb
11280                       (setq gnus-active-hashtb
11281                             (gnus-make-hashtable 
11282                              (count-lines (point-min) (point-max)))))))
11283         (prefix (and method (not (eq method gnus-select-method))
11284                      (gnus-group-prefixed-name "" method))))
11285
11286     (goto-char (point-min))
11287     (condition-case ()
11288         ;; We split this into to separate loops, one with the prefix
11289         ;; and one without to speed the reading up somewhat.
11290         (if prefix
11291             (let (min max opoint)
11292               (while (not (eobp))
11293                 (read cur) (read cur)
11294                 (setq min (read cur)
11295                       max (read cur)
11296                       opoint (point))
11297                 (skip-chars-forward " \t")
11298                 (insert prefix)
11299                 (goto-char opoint)
11300                 (set (let ((obarray hashtb)) (read cur)) 
11301                      (cons min max))
11302                 (forward-line 1)))
11303           (let (min max opoint)
11304             (while (not (eobp))
11305               (if (= (following-char) ?2)
11306                   (progn
11307                     (read cur) (read cur)
11308                     (setq min (read cur)
11309                           max (read cur))
11310                     (set (let ((obarray hashtb)) (read cur)) 
11311                          (cons min max))))
11312               (forward-line 1))))
11313       (error 
11314        (progn (ding) (message "Possible error in active file."))))))
11315
11316 (defun gnus-read-newsrc-file (&optional force)
11317   "Read startup file.
11318 If FORCE is non-nil, the .newsrc file is read."
11319   (setq gnus-current-startup-file (gnus-make-newsrc-file gnus-startup-file))
11320   ;; Reset variables that might be defined in the .newsrc.eld file.
11321   (let ((variables gnus-variable-list))
11322     (while variables
11323       (set (car variables) nil)
11324       (setq variables (cdr variables))))
11325   (let* ((newsrc-file gnus-current-startup-file)
11326          (quick-file (concat newsrc-file ".el")))
11327     (save-excursion
11328       ;; We always load the .newsrc.eld file. If always contains
11329       ;; much information that can not be gotten from the .newsrc
11330       ;; file (ticked articles, killed groups, foreign methods, etc.)
11331       (gnus-read-newsrc-el-file quick-file)
11332  
11333       (if (or force
11334               (and (file-newer-than-file-p newsrc-file quick-file)
11335                    (file-newer-than-file-p newsrc-file 
11336                                            (concat quick-file "d")))
11337               (not gnus-newsrc-alist))
11338           ;; We read the .newsrc file. Note that if there if a
11339           ;; .newsrc.eld file exists, it has already been read, and
11340           ;; the `gnus-newsrc-hashtb' has been created. While reading
11341           ;; the .newsrc file, Gnus will only use the information it
11342           ;; can find there for changing the data already read -
11343           ;; ie. reading the .newsrc file will not trash the data
11344           ;; already read (except for read articles).
11345           (save-excursion
11346             (message "Reading %s..." newsrc-file)
11347             (set-buffer (find-file-noselect newsrc-file))
11348             (buffer-disable-undo (current-buffer))
11349             (gnus-newsrc-to-gnus-format)
11350             (kill-buffer (current-buffer))
11351             (message "Reading %s... done" newsrc-file))))))
11352
11353 (defun gnus-read-newsrc-el-file (file)
11354   (let ((ding-file (concat file "d")))
11355     ;; We always, always read the .eld file.
11356     (message "Reading %s..." ding-file)
11357     (let (gnus-newsrc-assoc)
11358       (condition-case nil
11359           (load ding-file t t t)
11360         (error nil))
11361       (and gnus-newsrc-assoc (setq gnus-newsrc-alist gnus-newsrc-assoc)))
11362     (gnus-uncompress-newsrc-assoc)
11363     (gnus-make-hashtable-from-newsrc-alist)
11364     (if (not (file-newer-than-file-p file ding-file))
11365         ()
11366       ;; Old format quick file
11367       (message "Reading %s..." file)
11368       ;; The .el file is newer than the .eld file, so we read that one
11369       ;; as well. 
11370       (gnus-read-old-newsrc-el-file file))))
11371
11372 ;; Parse the old-style quick startup file
11373 (defun gnus-read-old-newsrc-el-file (file)
11374   (let (newsrc killed marked group g m len info)
11375     (prog1
11376         (let ((gnus-killed-assoc nil)
11377               gnus-marked-assoc gnus-newsrc-alist gnus-newsrc-assoc)
11378           (prog1
11379               (condition-case nil
11380                   (load file t t t)
11381                 (error nil))
11382             (setq newsrc gnus-newsrc-assoc
11383                   killed gnus-killed-assoc
11384                   marked gnus-marked-assoc)))
11385       (setq gnus-newsrc-alist nil)
11386       (while newsrc
11387         (setq group (car newsrc))
11388         (let ((info (nth 2 (gnus-gethash (car group) gnus-newsrc-hashtb))))
11389           (if info
11390               (progn
11391                 (setcar (nthcdr 2 info) (cdr (cdr group)))
11392                 (setcar (cdr info)
11393                         (if (nth 1 group) gnus-level-default-subscribed 
11394                           gnus-level-default-unsubscribed))
11395                 (setq gnus-newsrc-alist (cons info gnus-newsrc-alist)))
11396             (setq gnus-newsrc-alist
11397                   (cons 
11398                    (setq info
11399                          (list (car group)
11400                                (if (nth 1 group) gnus-level-default-subscribed
11401                                  gnus-level-default-unsubscribed) 
11402                                (cdr (cdr group))))
11403                    gnus-newsrc-alist)))
11404           (if (setq m (assoc (car group) marked))
11405             (setcdr (cdr (cdr info)) (cons (list (cons 'tick (cdr m))) nil))))
11406         (setq newsrc (cdr newsrc)))
11407       (setq newsrc killed)
11408       (while newsrc
11409         (setcar newsrc (car (car newsrc)))
11410         (setq newsrc (cdr newsrc)))
11411       (setq gnus-killed-list killed))
11412     (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist))
11413     (gnus-make-hashtable-from-newsrc-alist)))
11414       
11415 (defun gnus-make-newsrc-file (file)
11416   "Make server dependent file name by catenating FILE and server host name."
11417   (let* ((file (expand-file-name file nil))
11418          (real-file (concat file "-" (nth 1 gnus-select-method))))
11419     (if (file-exists-p real-file)
11420         real-file file)))
11421
11422 (defun gnus-uncompress-newsrc-assoc ()
11423   ;; Uncompress all lists of marked articles in the newsrc assoc.
11424   (let ((newsrc gnus-newsrc-alist)
11425         marked)
11426     (while newsrc
11427       (if (not (setq marked (nth 3 (car newsrc))))
11428           ()
11429         (while marked
11430           (or (eq 'score (car (car marked)))
11431               (eq 'bookmark (car (car marked)))
11432               (eq 'killed (car (car marked)))
11433               (setcdr (car marked) (gnus-uncompress-range (cdr (car marked)))))
11434           (setq marked (cdr marked))))
11435       (setq newsrc (cdr newsrc)))))
11436
11437 (defun gnus-compress-newsrc-assoc ()
11438   ;; Compress all lists of marked articles in the newsrc assoc.
11439   (let ((newsrc gnus-newsrc-alist)
11440         marked)
11441     (while newsrc
11442       (if (not (setq marked (nth 3 (car newsrc))))
11443           ()
11444         (while marked
11445           (or (eq 'score (car (car marked)))
11446               (eq 'bookmark (car (car marked)))
11447               (eq 'killed (car (car marked)))
11448               (setcdr (car marked) 
11449                       (gnus-compress-sequence (sort (cdr (car marked)) '<) t)))
11450           (setq marked (cdr marked))))
11451       (setq newsrc (cdr newsrc)))))
11452
11453 ;; jwz: rewrote this function to be much more efficient, and not be
11454 ;; subject to regexp overflow errors when it encounters very long
11455 ;; lines -- the old behavior was to blow off the rest of the *file*
11456 ;; when a line was encountered that was too long to match!!  Now it
11457 ;; uses only simple looking-at calls, and doesn't create as many
11458 ;; temporary strings.  It also now handles multiple consecutive
11459 ;; options lines (before it only handled the first.)
11460 ;; Tiny rewrite by lmi. 
11461 (defun gnus-newsrc-to-gnus-format ()
11462   "Parse current buffer as .newsrc file."
11463   ;; We have to re-initialize these variables (except for
11464   ;; gnus-killed-list) because the quick startup file may contain bogus
11465   ;; values.
11466   (setq gnus-newsrc-options nil)
11467   (setq gnus-newsrc-options-n nil)
11468   (gnus-parse-options-lines)
11469   (gnus-parse-newsrc-body))
11470
11471 (defun gnus-parse-options-lines ()
11472   ;; newsrc.5 seems to indicate that the options line can come anywhere
11473   ;; in the file, and that there can be any number of them:
11474   ;;
11475   ;;       An  options  line  starts  with  the  word  options (left-
11476   ;;       justified).  Then there are the list of  options  just  as
11477   ;;       they would be on the readnews command line.  For instance:
11478   ;;
11479   ;;       options -n all !net.sf-lovers !mod.human-nets -r
11480   ;;       options -c -r
11481   ;;
11482   ;;       A string of lines beginning with a space or tab after  the
11483   ;;       initial  options  line  will  be  considered  continuation
11484   ;;       lines.
11485   ;;
11486   ;; For now, we only accept it at the beginning of the file.
11487
11488   (goto-char (point-min))
11489   (skip-chars-forward " \t\n")
11490   (setq gnus-newsrc-options nil)
11491   (while (looking-at "^options[ \t]*\\(.*\\)\n")
11492     ;; handle consecutive options lines
11493     (setq gnus-newsrc-options (concat gnus-newsrc-options
11494                                       (if gnus-newsrc-options "\n\t")
11495                                       (buffer-substring (match-beginning 1)
11496                                                         (match-end 1))))
11497     (forward-line 1)
11498     (while (looking-at "[ \t]+\\(.*\\)\n")
11499       ;; handle subsequent continuation lines of this options line
11500       (setq gnus-newsrc-options (concat gnus-newsrc-options "\n\t"
11501                                         (buffer-substring (match-beginning 1)
11502                                                           (match-end 1))))
11503       (forward-line 1)))
11504   ;; Gather all "-n" options lines.
11505   (let ((start 0)
11506         (result nil))
11507     (if gnus-newsrc-options
11508         (while (and (string-match "^[ \t]*-n\\([^\n]*\\)$"
11509                                   gnus-newsrc-options
11510                                   start)
11511                     (setq start (match-end 0)))
11512           (setq result (concat result
11513                                (and result " ")
11514                                (substring gnus-newsrc-options
11515                                           (match-beginning 1)
11516                                           (match-end 1))))))
11517
11518     (and result (gnus-parse-n-options result))
11519     nil))
11520
11521 (defun gnus-parse-newsrc-body ()
11522   ;; Point has been positioned after the options lines.  We shouldn't
11523   ;; see any more in here.
11524
11525   (let ((subscribe nil)
11526         (read-list nil)
11527         (line (1+ (count-lines (point-min) (point))))
11528         (already-read (> (length gnus-newsrc-alist) 1))
11529         newsgroup
11530         p p2)
11531     (save-restriction
11532       (skip-chars-forward " \t")
11533       (while (not (eobp))
11534         (cond
11535          ((= (following-char) ?\n)
11536           ;; skip blank lines
11537           nil)
11538          (t
11539           (setq p (point))
11540           (skip-chars-forward "^:!\n")
11541           (if (= (following-char) ?\n)
11542               (error "line %d is unparsable in %s" line (buffer-name)))
11543           (setq p2 (point))
11544           (skip-chars-backward " \t")
11545
11546           ;; #### note: we could avoid consing a string here by binding obarray
11547           ;; and reading the newsgroup directly into the gnus-newsrc-hashtb,
11548           ;; then setq'ing newsgroup to symbol-name of that, like we do in
11549           ;; gnus-active-to-gnus-format.
11550           (setq newsgroup (buffer-substring p (point)))
11551           (goto-char p2)
11552
11553           (setq subscribe (= (following-char) ?:))
11554           (setq read-list nil)
11555
11556           (forward-char 1)              ; after : or !
11557           (skip-chars-forward " \t")
11558           (while (not (= (following-char) ?\n))
11559             (skip-chars-forward " \t")
11560             (or
11561              (and (cond
11562                    ((looking-at "\\([0-9]+\\)-\\([0-9]+\\)") ; a range
11563                     (setq read-list
11564                           (cons
11565                            (cons
11566                             (progn
11567                               ;; faster that buffer-substring/string-to-int
11568                               (narrow-to-region (point-min) (match-end 1))
11569                               (read (current-buffer)))
11570                             (progn
11571                               (narrow-to-region (point-min) (match-end 2))
11572                               (forward-char) ; skip over "-"
11573                               (prog1
11574                                   (read (current-buffer))
11575                                 (widen))))
11576                            read-list))
11577                     t)
11578                    ((looking-at "[0-9]+")
11579                     ;; faster that buffer-substring/string-to-int
11580                     (narrow-to-region (point-min) (match-end 0))
11581                     (setq p (read (current-buffer)))
11582                     (widen)
11583                     (setq read-list (cons (cons p p) read-list))
11584                     t)
11585                    (t
11586                     ;; bogus chars in ranges
11587                     nil))
11588                   (progn
11589                     (goto-char (match-end 0))
11590                     (skip-chars-forward " \t")
11591                     (cond ((= (following-char) ?,)
11592                            (forward-char 1)
11593                            t)
11594                           ((= (following-char) ?\n)
11595                            t)
11596                           (t
11597                            ;; bogus char after range
11598                            nil))))
11599              ;; if we get here, the parse failed
11600              (progn
11601                (end-of-line)            ; give up on this line
11602                (ding)
11603                (message "Ignoring bogus line %d for %s in %s"
11604                         line newsgroup (buffer-name))
11605                (sleep-for 1))))
11606           ;; We have already read .newsrc.eld, so we gently update the
11607           ;; data in the hash table with the information we have just
11608           ;; read. 
11609           (let ((info (nth 2 (gnus-gethash newsgroup gnus-newsrc-hashtb)))
11610                 level)
11611             (if info
11612                 (progn
11613                   (setcar (nthcdr 2 info) (nreverse read-list))
11614                   ;; We update the level very gently.  In fact, we
11615                   ;; only change it if there's been a status change
11616                   ;; from subscribed to unsubscribed, or vice versa.
11617                   (setq level (nth 1 info))
11618                   (cond ((and (<= level gnus-level-subscribed) (not subscribe))
11619                          (setq level (if read-list 
11620                                          gnus-level-default-unsubscribed 
11621                                        (1+ gnus-level-default-unsubscribed))))
11622                         ((and (> level gnus-level-subscribed) subscribe)
11623                          (setq level gnus-level-default-subscribed)))
11624                   (setcar (cdr info) level))
11625               (setq gnus-newsrc-alist
11626                     (cons (list newsgroup 
11627                                 (if subscribe
11628                                     gnus-level-default-subscribed 
11629                                   (if read-list 
11630                                       (1+ gnus-level-subscribed)
11631                                     gnus-level-default-unsubscribed))
11632                                 (nreverse read-list))
11633                           gnus-newsrc-alist))))))
11634         (setq line (1+ line))
11635         (forward-line 1)))
11636     (setq gnus-newsrc-alist (cdr gnus-newsrc-alist))
11637     (or already-read (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist)))
11638     (gnus-make-hashtable-from-newsrc-alist)
11639     nil))
11640
11641 (defun gnus-parse-n-options (options)
11642   "Parse -n NEWSGROUPS options and return a cons of YES and NO regexps."
11643   (let (yes no yes-or-no out newsgroup)
11644     ;; Parse each newsgroup description such as "comp.all".  Commas
11645     ;; and white spaces can be a newsgroup separator.
11646     (while
11647         (string-match "^[ \t\n,]*\\(!?\\)\\([^- \t\n,][^ \t\n,]*\\)" options)
11648       (setq yes-or-no
11649             (substring options (match-beginning 1) (match-end 1)))
11650       (setq newsgroup
11651             (regexp-quote
11652              (substring options
11653                         (match-beginning 2) (match-end 2))))
11654       (setq options (substring options (match-end 2)))
11655       ;; Rewrite "all" to ".+" not ".*".  ".+" requires at least one
11656       ;; character.
11657       (while (string-match "\\(^\\|\\\\[.]\\)all\\(\\\\[.]\\|$\\)" newsgroup)
11658         (setq newsgroup
11659               (concat (substring newsgroup 0 (match-end 1))
11660                       ".+"
11661                       (substring newsgroup (match-beginning 2)))))
11662       ;; It is yes or no.
11663       (setq out (cons (cons (if (string= yes-or-no "!") 'ignore 'subscribe)
11664                             (concat "^" newsgroup "$")) 
11665                       out)))
11666     (setq gnus-newsrc-options-n out)))
11667
11668 (defun gnus-save-newsrc-file ()
11669   "Save .newsrc file."
11670   ;; Note: We cannot save .newsrc file if all newsgroups are removed
11671   ;; from the variable gnus-newsrc-alist.
11672   (and (or gnus-newsrc-alist gnus-killed-list)
11673        gnus-current-startup-file
11674        (let ((make-backup-files t)
11675              (version-control nil)
11676              (require-final-newline t)) ;Don't ask even if requested.
11677          ;; You can stop or change version control of backup file.
11678          ;; Suggested by jason@violet.berkeley.edu.
11679          (run-hooks 'gnus-save-newsrc-hook)
11680          (save-excursion
11681            (if (or (not gnus-dribble-buffer)
11682                    (not (buffer-name gnus-dribble-buffer))
11683                    (zerop (save-excursion
11684                             (set-buffer gnus-dribble-buffer)
11685                             (buffer-size))))
11686                (message "(No changes need to be saved)")
11687              (if gnus-save-newsrc-file
11688                  (progn
11689                    (message "Saving %s..." gnus-current-startup-file)
11690                    ;; Make backup file of master newsrc.
11691                    (gnus-gnus-to-newsrc-format)
11692                    (message "Saving %s... done" gnus-current-startup-file)))
11693              ;; Quickly loadable .newsrc.
11694              (set-buffer (get-buffer-create " *Gnus-newsrc*"))
11695              (gnus-add-current-to-buffer-list)
11696              (buffer-disable-undo (current-buffer))
11697              (erase-buffer)
11698              (message "Saving %s.eld..." gnus-current-startup-file)
11699              (gnus-gnus-to-quick-newsrc-format)
11700              (write-region 1 (point-max) 
11701                            (concat gnus-current-startup-file ".eld") 
11702                            nil 'nomesg)
11703              (kill-buffer (current-buffer))
11704              (message "Saving %s.eld... done" gnus-current-startup-file)
11705              (gnus-dribble-delete-file))))))
11706
11707 (defun gnus-gnus-to-quick-newsrc-format ()
11708   "Insert Gnus variables such as gnus-newsrc-alist in lisp format."
11709   (insert ";; (ding) Gnus startup file.\n")
11710   (insert ";; Never delete this file - touch .newsrc instead to force Gnus\n")
11711   (insert ";; to read .newsrc.\n")
11712   (let ((variables gnus-variable-list)
11713         (gnus-newsrc-alist (cdr gnus-newsrc-alist))
11714         variable)
11715     ;; insert lisp expressions.
11716     (gnus-compress-newsrc-assoc)
11717     (while variables
11718       (setq variable (car variables))
11719       (and (boundp variable)
11720            (symbol-value variable)
11721            (or gnus-save-killed-list (not (eq variable 'gnus-killed-list)))
11722            (insert "(setq " (symbol-name variable) " '"
11723                    (prin1-to-string (symbol-value variable))
11724                    ")\n"))
11725       (setq variables (cdr variables)))
11726     (gnus-uncompress-newsrc-assoc)))
11727
11728
11729 (defun gnus-gnus-to-newsrc-format ()
11730   ;; Generate and save the .newsrc file.
11731   (let ((newsrc (cdr gnus-newsrc-alist))
11732         info ranges range)
11733     (save-excursion
11734       (set-buffer (create-file-buffer gnus-startup-file))
11735       (buffer-disable-undo (current-buffer))
11736       (erase-buffer)
11737       ;; Write options.
11738       (if gnus-newsrc-options (insert "options " gnus-newsrc-options "\n"))
11739       ;; Write subscribed and unsubscribed.
11740       (while newsrc
11741         (setq info (car newsrc))
11742         (if (not (nth 4 info))          ;Don't write foreign groups to .newsrc.
11743             (progn
11744               (insert (car info) (if (> (nth 1 info) gnus-level-subscribed)
11745                                      "!" ":"))
11746               (if (setq ranges (nth 2 info))
11747                   (progn
11748                     (insert " ")
11749                     (if (not (listp (cdr ranges)))
11750                         (if (= (car ranges) (cdr ranges))
11751                             (insert (int-to-string (car ranges)))
11752                           (insert (int-to-string (car ranges)) "-" 
11753                                   (int-to-string (cdr ranges))))
11754                       (while ranges
11755                         (setq range (car ranges)
11756                               ranges (cdr ranges))
11757                         (if (or (atom range) (= (car range) (cdr range)))
11758                             (insert (int-to-string 
11759                                      (or (and (atom range) range) 
11760                                          (car range))))
11761                           (insert (int-to-string (car range)) "-"
11762                                   (int-to-string (cdr range))))
11763                         (if ranges (insert ","))))))
11764               (insert "\n")))
11765         (setq newsrc (cdr newsrc)))
11766       (write-region 1 (point-max) gnus-current-startup-file nil 'nomesg)
11767       (kill-buffer (current-buffer)))))
11768
11769 (defun gnus-read-descriptions-file ()
11770   (message "Reading descriptions file...")
11771   (if (not (gnus-request-list-newsgroups gnus-select-method))
11772       (progn
11773         (message "Couldn't read newsgroups descriptions")
11774         nil)
11775     (let (group)
11776       (setq gnus-description-hashtb 
11777             (gnus-make-hashtable (length gnus-active-hashtb)))
11778       (save-excursion
11779         (save-restriction
11780           (set-buffer nntp-server-buffer)
11781           (goto-char (point-min))
11782           (delete-non-matching-lines "^[-\\._+A-Za-z0-9]+[ \t]")
11783           (goto-char (point-min))
11784           (if (or (search-forward "\n.\n" nil t)
11785                   (goto-char (point-max)))
11786               (progn
11787                 (beginning-of-line)
11788                 (narrow-to-region (point-min) (point))))
11789           (goto-char (point-min))
11790           (while (not (eobp))
11791             (setq group (let ((obarray gnus-description-hashtb))
11792                           (read (current-buffer))))
11793             (skip-chars-forward " \t")
11794             (set group (buffer-substring 
11795                         (point) (gnus-point-at-eol)))
11796             (forward-line 1))))
11797       (message "Reading descriptions file...done")
11798       t)))
11799
11800 (defun gnus-group-get-description (group)
11801   ;; Get the description of a group by sending XGTITLE to the server.
11802   (and (gnus-request-group-description group)
11803        (save-excursion
11804          (set-buffer nntp-server-buffer)
11805          (goto-char (point-min))
11806          (and (looking-at "[^ \t]+[ \t]+\\(.*\\)")
11807               (buffer-substring (match-beginning 1) (match-end 1))))))
11808
11809 ;;;
11810 ;;; Server
11811 ;;;
11812
11813 (defvar gnus-server-mode-hook nil
11814   "Hook run in `gnus-server-mode' buffers.")
11815
11816 (defconst gnus-server-line-format "     {%(%h:%w%)}\n"
11817   "Format of server lines.
11818 It works along the same lines as a normal formatting string,
11819 with some simple extensions.")
11820
11821 (defvar gnus-server-mode-line-format "(ding) List of servers"
11822   "The format specification for the server mode line.")
11823
11824 (defconst gnus-server-line-format-alist
11825   (list (list ?h 'how ?s)
11826         (list ?n 'name ?s)
11827         (list ?w 'where ?s)
11828         ))
11829
11830 (defconst gnus-server-mode-line-format-alist 
11831   (list (list ?S 'news-server ?s)
11832         (list ?M 'news-method ?s)
11833         (list ?u 'user-defined ?s)))
11834
11835 (defvar gnus-server-line-format-spec nil)
11836 (defvar gnus-server-mode-line-format-spec nil)
11837 (defvar gnus-server-killed-servers nil)
11838
11839 (defvar gnus-server-mode-map nil)
11840 (put 'gnus-server-mode 'mode-class 'special)
11841
11842 (if gnus-server-mode-map
11843     nil
11844   (setq gnus-server-mode-map (make-sparse-keymap))
11845   (suppress-keymap gnus-server-mode-map)
11846   (define-key gnus-server-mode-map " " 'gnus-server-read-server)
11847   (define-key gnus-server-mode-map "q" 'gnus-server-exit)
11848   (define-key gnus-server-mode-map "l" 'gnus-server-list-servers)
11849   (define-key gnus-server-mode-map "k" 'gnus-server-kill-server)
11850   (define-key gnus-server-mode-map "y" 'gnus-server-yank-server)
11851   (define-key gnus-server-mode-map "c" 'gnus-server-copy-server)
11852   (define-key gnus-server-mode-map "a" 'gnus-server-add-server)
11853   (define-key gnus-server-mode-map "e" 'gnus-server-edit-server))
11854
11855 (defun gnus-server-mode ()
11856   "Major mode for listing and editing servers.
11857
11858 All normal editing commands are switched off.
11859 \\<gnus-server-mode-map>
11860
11861 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]'). 
11862
11863 The following commands are available:
11864
11865 \\{gnus-server-mode-map}"
11866   (interactive)
11867 ;  (if gnus-visual (gnus-server-make-menu-bar))
11868   (kill-all-local-variables)
11869   (setq mode-line-modified "-- ")
11870   (make-local-variable 'mode-line-format)
11871   (setq mode-line-format (copy-sequence mode-line-format))
11872   (and (equal (nth 3 mode-line-format) "   ")
11873        (setcar (nthcdr 3 mode-line-format) ""))
11874   (setq major-mode 'gnus-server-mode)
11875   (setq mode-name "Server")
11876 ;  (gnus-group-set-mode-line)
11877   (setq mode-line-process nil)
11878   (use-local-map gnus-server-mode-map)
11879   (buffer-disable-undo (current-buffer))
11880   (setq truncate-lines t)
11881   (setq buffer-read-only t)
11882   (run-hooks 'gnus-server-mode-hook))
11883
11884 (defun gnus-server-insert-server-line (sformat name method)
11885   (let* ((sformat (or sformat gnus-server-line-format-spec))
11886          (how (car method))
11887          (where (nth 1 method))
11888          b)
11889     (beginning-of-line)
11890     (setq b (point))
11891     ;; Insert the text.
11892     (insert (eval sformat))
11893     (add-text-properties 
11894      b (1+ b) (list 'gnus-server (intern name)))))
11895
11896 (defun gnus-server-setup-buffer ()
11897   (if (get-buffer gnus-server-buffer)
11898       ()
11899     (save-excursion
11900       (set-buffer (get-buffer-create gnus-server-buffer))
11901       (gnus-server-mode))))
11902
11903 (defun gnus-server-prepare ()
11904   (setq gnus-server-mode-line-format-spec 
11905         (gnus-parse-format gnus-server-mode-line-format 
11906                            gnus-server-mode-line-format-alist))
11907   (setq gnus-server-line-format-spec 
11908         (gnus-parse-format gnus-server-line-format 
11909                            gnus-server-line-format-alist))
11910   (let ((alist gnus-server-alist)
11911         (buffer-read-only nil))
11912     (erase-buffer)
11913     (while alist
11914       (gnus-server-insert-server-line nil (car (car alist)) (cdr (car alist)))
11915       (setq alist (cdr alist))))
11916   (goto-char (point-min))
11917   (gnus-server-position-cursor))
11918
11919 (defun gnus-server-server-name ()
11920   (let ((server (get-text-property 
11921                  (save-excursion (beginning-of-line) (point)) 'gnus-server)))
11922     (and server (symbol-name server))))
11923
11924 (defalias 'gnus-server-position-cursor 'gnus-goto-colon)
11925
11926 (defvar gnus-winconf-edit-server nil)
11927 (defconst gnus-server-edit-buffer "*Gnus edit server*")
11928
11929 (defun gnus-server-update-server (server)
11930   (save-excursion
11931     (set-buffer gnus-server-buffer)
11932     (let ((buffer-read-only nil)
11933           (info (cdr (assoc server gnus-server-alist))))
11934       (gnus-dribble-enter 
11935        (concat "(gnus-server-set-info \"" server "\" '"
11936                (prin1-to-string info) ")"))
11937       ;; Buffer may be narrowed.
11938       (save-restriction
11939         (widen)
11940         (if (gnus-server-goto-server server)
11941             (delete-region (progn (beginning-of-line) (point))
11942                            (progn (forward-line 1) (point))))
11943         (let ((entry (assoc server gnus-server-alist)))
11944           (gnus-server-insert-server-line nil (car entry) (cdr entry))
11945           (gnus-server-position-cursor))))))
11946
11947 (defun gnus-server-set-info (info)
11948   ;; Enter a select method into the virtual server alist.
11949   (gnus-dribble-enter 
11950    (concat "(gnus-server-set-info '"
11951            (prin1-to-string info) ")"))
11952   (let* ((server (nth 1 info))
11953          (entry (assoc server gnus-server-alist)))
11954     (if entry (setcdr entry info)
11955       (setq gnus-server-alist
11956             (nconc gnus-server-alist (list (cons server info)))))))
11957
11958 (defun gnus-server-to-method (server)
11959   ;; Map virtual server names to select methods.
11960   (or (and (equal server "native") gnus-select-method)
11961       (cdr (assoc server gnus-server-alist))))
11962
11963 (defun gnus-server-extend-method (group method)
11964   ;; This function "extends" a virtual server.  If the server is
11965   ;; "hello", and the select method is ("hello" (my-var "something")) 
11966   ;; in the group "alt.alt", this will result in a new virtual server
11967   ;; called "helly+alt.alt".
11968   (let ((entry
11969          (gnus-copy-sequence 
11970           (if (equal (car method) "native") gnus-select-method
11971               (cdr (assoc (car method) gnus-server-alist))))))
11972     (setcar (cdr entry) (concat (nth 1 entry) "+" group))
11973     (nconc entry (cdr method))))
11974
11975 (defun gnus-server-get-method (group method)
11976   ;; Input either a server name, and extended server name, or a
11977   ;; select method, and return a select method. 
11978   (cond ((stringp method)
11979          (gnus-server-to-method method))
11980         ((stringp (car method))
11981          (gnus-server-extend-method group method))
11982         (t
11983          (gnus-server-add-address method))))
11984
11985 (defun gnus-server-add-address (method)
11986   (let ((method-name (symbol-name (car method))))
11987     (if (and (memq 'address (assoc method-name gnus-valid-select-methods))
11988              (not (assq (intern (concat method-name "-address")) method)))
11989         (append method (list (list (intern (concat method-name "-address"))
11990                                    (nth 1 method))))
11991       method)))
11992
11993 (defun gnus-server-equal (s1 s2)
11994   (or (equal s1 s2)
11995       (and (= (length s1) (length s2))
11996            (progn
11997              (while (and s1 (member (car s1) s2))
11998                (setq s1 (cdr s1)))
11999              (null s1)))))
12000
12001 ;;; Interactive server functions.
12002
12003 (defun gnus-server-kill-server (server)
12004   "Kill the server on the current line."
12005   (interactive (list (gnus-server-server-name)))
12006   (or (gnus-server-goto-server server)
12007       (if server (error "No such server: %s" server)
12008         (error "No server on the current line")))
12009   (let ((buffer-read-only nil))
12010     (delete-region (progn (beginning-of-line) (point))
12011                    (progn (forward-line 1) (point))))
12012   (setq gnus-server-killed-servers 
12013         (cons (assoc server gnus-server-alist) gnus-server-killed-servers))
12014   (setq gnus-server-alist (delq (car gnus-server-killed-servers)
12015                                 gnus-server-alist))
12016   (gnus-server-position-cursor))
12017
12018 (defun gnus-server-yank-server ()
12019   "Yank the previously killed server."
12020   (interactive)
12021   (or gnus-server-killed-servers
12022       (error "No killed servers to be yanked"))
12023   (let ((alist gnus-server-alist)
12024         (server (gnus-server-server-name))
12025         (killed (car gnus-server-killed-servers)))
12026     (if (not server) 
12027         (setq gnus-server-alist (nconc gnus-server-alist (list killed)))
12028       (if (string= server (car (car gnus-server-alist)))
12029           (setq gnus-server-alist (cons killed gnus-server-alist))
12030         (while (and (cdr alist)
12031                     (not (string= server (car (car (cdr alist))))))
12032           (setq alist (cdr alist)))
12033         (setcdr alist (cons killed (cdr alist)))))
12034     (gnus-server-update-server (car killed))
12035     (setq gnus-server-killed-servers (cdr gnus-server-killed-servers))
12036     (gnus-server-position-cursor)))
12037
12038 (defun gnus-server-exit ()
12039   "Return to the group buffer."
12040   (interactive)
12041   (kill-buffer (current-buffer))
12042   (switch-to-buffer gnus-group-buffer))
12043
12044 (defun gnus-server-list-servers ()
12045   "List all available servers."
12046   (interactive)
12047   (let ((cur (gnus-server-server-name)))
12048     (gnus-server-prepare)
12049     (if cur (gnus-server-goto-server cur)
12050       (goto-char (point-max))
12051       (forward-line -1))
12052     (gnus-server-position-cursor)))
12053
12054 (defun gnus-server-copy-server (from to)
12055   (interactive
12056    (list
12057     (or (gnus-server-server-name)
12058         (error "No server on the current line"))
12059     (read-string "Copy to: ")))
12060   (or from (error "No server on current line"))
12061   (or (and to (not (string= to ""))) (error "No name to copy to"))
12062   (and (assoc to gnus-server-alist) (error "%s already exists" to))
12063   (or (assoc from gnus-server-alist) 
12064       (error "%s: no such server" from))
12065   (let ((to-entry (gnus-copy-sequence (assoc from gnus-server-alist))))
12066     (setcar to-entry to)
12067     (setcar (nthcdr 2 to-entry) to)
12068     (setq gnus-server-killed-servers 
12069           (cons to-entry gnus-server-killed-servers))
12070     (gnus-server-yank-server)))
12071
12072 (defun gnus-server-add-server (how where)
12073   (interactive 
12074    (list (intern (completing-read "Server method: "
12075                                   gnus-valid-select-methods nil t))
12076          (read-string "Server name: ")))
12077   (setq gnus-server-killed-servers 
12078         (cons (list where how where) gnus-server-killed-servers))
12079   (gnus-server-yank-server))
12080
12081 (defun gnus-server-goto-server (server)
12082   "Jump to a server line."
12083   (interactive
12084    (list (completing-read "Goto server: " gnus-server-alist nil t)))
12085   (let ((to (text-property-any (point-min) (point-max) 
12086                                'gnus-server (intern server))))
12087     (and to
12088          (progn
12089            (goto-char to) 
12090            (gnus-server-position-cursor)))))
12091
12092 (defun gnus-server-edit-server (server)
12093   "Edit the server on the current line."
12094   (interactive (list (gnus-server-server-name)))
12095   (or server
12096       (error "No server on current line"))
12097   (setq gnus-winconf-edit-server (current-window-configuration))
12098   (pop-to-buffer gnus-server-edit-buffer)
12099   (gnus-add-current-to-buffer-list)
12100   (emacs-lisp-mode)
12101   (use-local-map (copy-keymap (current-local-map)))
12102   (local-set-key "\C-c\C-c" 'gnus-server-edit-server-done)
12103   (erase-buffer)
12104   (insert ";; Type `C-c C-c' after you have edited the server.\n\n")
12105   (insert "(gnus-server-set-info \n  "
12106           (pp-to-string (list 'quote (cdr (assoc server gnus-server-alist))))
12107           "  )\n"))
12108
12109 (defun gnus-server-edit-server-done ()
12110   (interactive)
12111   (set-buffer (get-buffer-create gnus-server-edit-buffer))
12112   (eval-current-buffer)
12113   (kill-buffer (current-buffer))
12114   (and gnus-winconf-edit-server
12115        (set-window-configuration gnus-winconf-edit-server))
12116   (setq gnus-winconf-edit-server nil)
12117   (set-buffer gnus-server-buffer)
12118   (gnus-server-update-server (gnus-server-server-name))
12119   (gnus-server-position-cursor))
12120
12121 (defun gnus-server-read-server (server)
12122   "Browse a server."
12123   (interactive (gnus-server-server-name))
12124   (gnus-browse-foreign-server (gnus-server-to-method server)))
12125
12126 ;;; Gnus score functions.
12127
12128 (defvar gnus-global-score-files nil
12129   "*List of global score files and directories.
12130 Set this variable if you want to use people's score files.  One entry
12131 for each score file or each score file directory.  Gnus will decide
12132 by itself what score files are applicable to which group.
12133
12134 Say you want to use the single score file
12135 \"/ftp.ifi.uio.no@ftp:/pub/larsi/ding/score/soc.motss.SCORE\" and all
12136 score files in the \"/ftp.some-where:/pub/score\" directory.
12137
12138  (setq gnus-global-score-files
12139        '(\"/ftp.ifi.uio.no:/pub/larsi/ding/score/soc.motss.SCORE\"
12140          \"/ftp.some-where:/pub/score\"))")
12141
12142 (defun gnus-score-score-files (group)
12143   "Return a list of all possible score files."
12144   (and gnus-global-score-files 
12145        (or gnus-internal-global-score-files
12146            (gnus-score-search-global-directories gnus-global-score-files)))
12147   (setq gnus-kill-files-directory 
12148         (file-name-as-directory
12149          (or gnus-kill-files-directory "~/News/")))
12150   (if (not (file-readable-p gnus-kill-files-directory))
12151       (setq gnus-score-file-list nil)
12152     (if (gnus-use-long-file-name 'not-score)
12153         (if (or (not gnus-score-file-list)
12154                 (gnus-file-newer-than gnus-kill-files-directory
12155                                       (car gnus-score-file-list)))
12156               (setq gnus-score-file-list 
12157                     (cons (nth 5 (file-attributes gnus-kill-files-directory))
12158                           (nreverse 
12159                            (directory-files 
12160                             gnus-kill-files-directory t
12161                             (concat gnus-score-file-suffix "$"))))))
12162       (let ((dir (expand-file-name
12163                   (concat gnus-kill-files-directory
12164                           (gnus-replace-chars-in-string group ?. ?/))))
12165             (mdir (length (expand-file-name gnus-kill-files-directory)))
12166             files)
12167         (if (file-exists-p (concat dir "/" gnus-score-file-suffix))
12168             (setq files (list (concat dir "/" gnus-score-file-suffix))))
12169         (while (>= (1+ (length dir)) mdir)
12170           (and (file-exists-p (concat dir "/all/" gnus-score-file-suffix))
12171                (setq files (cons (concat dir "/all/" gnus-score-file-suffix)
12172                                  files)))
12173           (string-match "/[^/]*$" dir)
12174           (setq dir (substring dir 0 (match-beginning 0))))
12175         (setq gnus-score-file-list 
12176               (cons nil (nreverse files)))))
12177     (cdr gnus-score-file-list)))
12178         
12179 (defun gnus-score-find-bnews (group)
12180   "Return a list of score files for GROUP.
12181 The score files are those files in the ~/News directory which matches
12182 GROUP using BNews sys file syntax."
12183   (let* ((sfiles (append (gnus-score-score-files group)
12184                          gnus-internal-global-score-files))
12185          (kill-dir (file-name-as-directory 
12186                     (expand-file-name gnus-kill-files-directory)))
12187          (klen (length kill-dir))
12188          ofiles not-match regexp)
12189     (save-excursion
12190       (set-buffer (get-buffer-create "*gnus score files*"))
12191       (buffer-disable-undo (current-buffer))
12192       ;; Go through all score file names and create regexp with them
12193       ;; as the source.  
12194       (while sfiles
12195         (erase-buffer)
12196         (insert (car sfiles))
12197         (goto-char 1)
12198         ;; First remove the suffix itself.
12199         (re-search-forward (concat "." gnus-score-file-suffix "$"))
12200         (replace-match "" t t) 
12201         (goto-char 1)
12202         (if (looking-at (regexp-quote kill-dir))
12203             ;; If the file name was just "SCORE", `klen' is one character
12204             ;; too much.
12205             (delete-char (min (1- (point-max)) klen))
12206           (goto-char (point-max))
12207           (search-backward "/")
12208           (delete-region (1+ (point)) (point-min)))
12209         ;; Translate "all" to ".*".
12210         (while (search-forward "all" nil t)
12211           (replace-match ".*" t t))
12212         (goto-char 1)
12213         ;; Deal with "not."s.
12214         (if (looking-at "not.")
12215             (progn
12216               (setq not-match t)
12217               (setq regexp (buffer-substring 5 (point-max))))
12218           (setq regexp (buffer-substring 1 (point-max)))
12219           (setq not-match nil))
12220         ;; Finally - if this resulting regexp matches the group name,
12221         ;; we add this score file to the list of score files
12222         ;; applicable to this group.
12223         (if (or (and not-match
12224                      (not (string-match regexp group)))
12225                 (and (not not-match)
12226                      (string-match regexp group)))
12227             (setq ofiles (cons (car sfiles) ofiles)))
12228         (setq sfiles (cdr sfiles)))
12229       (kill-buffer (current-buffer))
12230       ;; Slight kludge here - the last score file returned should be
12231       ;; the local score file, whether it exists or not. This is so
12232       ;; that any score commands the user enters will go to the right
12233       ;; file, and not end up in some global score file.
12234       (let ((localscore
12235              (expand-file-name
12236               (if (gnus-use-long-file-name 'not-score)
12237                   (concat gnus-kill-files-directory group "." 
12238                           gnus-score-file-suffix)
12239                 (concat gnus-kill-files-directory
12240                         (gnus-replace-chars-in-string group ?. ?/)
12241                         "/" gnus-score-file-suffix)))))
12242         (and (member localscore ofiles)
12243              (delete localscore ofiles))
12244         (setq ofiles (cons localscore ofiles)))
12245       (nreverse ofiles))))
12246
12247 (defun gnus-score-find-single (group)
12248   "Return list containing the score file for GROUP."
12249   (list (gnus-score-file-name group)))
12250
12251 (defun gnus-score-find-hierarchical (group)
12252   "Return list of score files for GROUP.
12253 This includes the score file for the group and all its parents."
12254   (let ((all (copy-sequence '(nil)))
12255         (start 0))
12256     (while (string-match "\\." group (1+ start))
12257       (setq start (match-beginning 0))
12258       (setq all (cons (substring group 0 start) all)))
12259     (setq all (cons group all))
12260     (mapcar 'gnus-score-file-name (nreverse all))))
12261
12262 (defun gnus-possibly-score-headers ()
12263   (let ((func gnus-score-find-score-files-function)
12264         score-files scores)
12265     (and func (not (listp func))
12266          (setq func (list func)))
12267     ;; Go through all the functions for finding score files (or actual
12268     ;; scores) and add them to a list.
12269     (while func
12270       (and (symbolp (car func))
12271            (fboundp (car func))
12272            (setq score-files 
12273                  (nconc score-files (funcall (car func) gnus-newsgroup-name))))
12274       (setq func (cdr func)))
12275     (if score-files (gnus-score-headers score-files))))
12276
12277 (defun gnus-score-file-name (newsgroup)
12278   "Return the name of a score file for NEWSGROUP."
12279   (cond  ((or (null newsgroup)
12280               (string-equal newsgroup ""))
12281           ;; The global score file is placed at top of the directory.
12282           (expand-file-name gnus-score-file-suffix
12283                             (or gnus-kill-files-directory "~/News")))
12284          ((gnus-use-long-file-name 'not-score)
12285           ;; Append ".SCORE" to newsgroup name.
12286           (expand-file-name (concat newsgroup "." gnus-score-file-suffix)
12287                             (or gnus-kill-files-directory "~/News")))
12288          (t
12289           ;; Place "SCORE" under the hierarchical directory.
12290           (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
12291                                     "/" gnus-score-file-suffix)
12292                             (or gnus-kill-files-directory "~/News")))))
12293
12294 (defun gnus-score-search-global-directories (files)
12295   "Scan all global score directories for score files."
12296   ;; Set the variable `gnus-internal-global-score-files' to all
12297   ;; available global score files.
12298   (interactive (list gnus-global-score-files))
12299   (let (out)
12300     (while files
12301       (if (string-match "/$" (car files))
12302           (setq out (nconc (directory-files 
12303                             (car files) t
12304                             (concat gnus-score-file-suffix "$"))))
12305         (setq out (cons (car files) out)))
12306       (setq files (cdr files)))
12307     (setq gnus-internal-global-score-files out)))
12308
12309 (defun gnus-score-default (level)
12310   (if level (prefix-numeric-value level) 
12311     gnus-score-interactive-default-score))
12312
12313 (provide 'gnus)
12314
12315 ;;; gnus.el ends here