a486be5ddcde37728054b21f47822608365d78e1
[gnus] / lisp / gnus.el
1 ;;; gnus.el --- 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
68 In any case, if the string (either in the variable, in the environment
69 variable, or returned by the function) is a file name, the contents of
70 this file will be used as the organization.")
71
72 (defvar gnus-use-generic-from nil
73   "If nil, the full host name will be the system name prepended to the domain name.
74 If this is a string, the full host name will be this string.
75 If this is non-nil, non-string, the domain name will be used as the
76 full host name.")
77
78 (defvar gnus-use-generic-path nil
79   "If nil, use the NNTP server name in the Path header.
80 If stringp, use this; if non-nil, use no host name (user name only).")
81
82
83 ;; Customization variables
84
85 (defvar gnus-select-method 
86   (list 'nntp (or (getenv "NNTPSERVER") 
87                   (if (and gnus-default-nntp-server
88                            (not (string= gnus-default-nntp-server "")))
89                       gnus-default-nntp-server)
90                   (system-name)))
91   "*Default method for selecting a newsgroup.
92 This variable should be a list, where the first element is how the
93 news is to be fetched, the second is the address. 
94
95 For instance, if you want to get your news via NNTP from
96 \"flab.flab.edu\", you could say:
97
98 (setq gnus-select-method '(nntp \"flab.flab.edu\"))
99
100 If you want to use your local spool, say:
101
102 (setq gnus-select-method (list 'nnspool (system-name)))
103
104 If you use this variable, you must set `gnus-nntp-server' to nil.
105
106 There is a lot more to know about select methods and virtual servers -
107 see the manual for details.")
108
109 ;; Added by Sudish Joseph <joseph@cis.ohio-state.edu>.
110 (defvar gnus-post-method nil
111   "*Preferred method for posting USENET news.
112 If this variable is nil, Gnus will use the current method to decide
113 which method to use when posting.  If it is non-nil, it will override
114 the current method.  This method will not be used in mail groups and
115 the like, only in \"real\" newsgroups.
116
117 The value must be a valid method as discussed in the documentation of
118 `gnus-select-method'.")
119
120 (defvar gnus-refer-article-method nil
121   "*Preferred method for fetching an article by Message-ID.
122 If you are reading news from the local spool (with nnspool), fetching
123 articles by Message-ID is painfully slow. By setting this method to an
124 nntp method, you might get acceptable results.
125
126 The value of this variable must be a valid select method as discussed
127 in the documentation of `gnus-select-method'")
128
129 (defvar gnus-secondary-select-methods nil
130   "*A list of secondary methods that will be used for reading news.
131 This is a list where each element is a complete select method (see
132 `gnus-select-method').  
133
134 If, for instance, you want to read your mail with the nnml backend,
135 you could set this variable:
136
137 (setq gnus-secondary-select-methods '((nnml \"\"))")
138
139 (defvar gnus-secondary-servers nil
140   "*List of NNTP servers that the user can choose between interactively.
141 To make Gnus query you for a server, you have to give `gnus' a
142 non-numeric prefix - `C-u M-x gnus', in short.")
143
144 (defvar gnus-nntp-server nil
145   "*The name of the host running the NNTP server.
146 This variable is semi-obsolete. Use the `gnus-select-method'
147 variable instead.")
148
149 (defvar gnus-nntp-service "nntp"
150   "*NNTP service name (\"nntp\" or 119).
151 This is an obsolete variable, which is scarcely used. If you use an
152 nntp server for your newsgroup and want to change the port number
153 used to 899, you would say something along these lines:
154
155  (setq gnus-select-method '(nntp \"my.nntp.server\" (nntp-port-number 899)))")
156
157 (defvar gnus-startup-file "~/.newsrc"
158   "*Your `.newsrc' file.
159 `.newsrc-SERVER' will be used instead if that exists.")
160
161 (defvar gnus-init-file "~/.gnus"
162   "*Your Gnus elisp startup file.
163 If a file with the .el or .elc suffixes exist, it will be read
164 instead.") 
165
166 (defvar gnus-group-faq-directory
167   "/anonymous@rtfm.mit.edu:/pub/usenet-by-group/"
168   "*Directory where the group FAQs are stored.
169 This will most commonly be on a remote machine, and the file will be
170 fetched by ange-ftp.")
171
172 (defvar gnus-group-archive-directory
173   "/ftp@sina.tcamc.uh.edu:/pub/emacs/ding-list/" 
174   "*The address of the (ding) archives.")
175
176 (defvar gnus-default-subscribed-newsgroups nil
177   "*This variable lists what newsgroups should be subscribed the first time Gnus is used.
178 It should be a list of strings.
179 If it is `t', Gnus will not do anything special the first time it is
180 started; it'll just use the normal newsgroups subscription methods.")
181
182 (defvar gnus-use-cross-reference t
183   "*Non-nil means that cross referenced articles will be marked as read.
184 If nil, ignore cross references.  If t, mark articles as read in
185 subscribed newsgroups. If neither t nor nil, mark as read in all
186 newsgroups.") 
187
188 (defvar gnus-use-dribble-file t
189   "*Non-nil means that Gnus will use a dribble file to store user updates.
190 If Emacs should crash without saving the .newsrc files, complete
191 information can be restored from the dribble file.")
192
193 (defvar gnus-asynchronous nil
194   "*If non-nil, Gnus will supply backends with data needed for async article fetching.")
195
196 (defvar gnus-asynchronous-article-function nil
197   "*Function for picking articles to pre-fetch, possibly.")
198
199 (defvar gnus-score-file-single-match-alist nil
200   "*Alist mapping regexps to lists of score files.
201 Each element of this alist should be of the form
202         (\"REGEXP\" [ \"SCORE-FILE-1\" ] [ \"SCORE-FILE-2\" ] ... )
203
204 If the name of a group is matched by REGEXP, the corresponding scorefiles
205 will be used for that group.
206 The first match found is used, subsequent matching entries are ignored (to
207 use multiple matches, see gnus-score-file-multiple-match-alist).
208
209 These score files are loaded in addition to any files returned by
210 gnus-score-find-score-files-function (which see).")
211
212 (defvar gnus-score-file-multiple-match-alist nil
213   "*Alist mapping regexps to lists of score files.
214 Each element of this alist should be of the form
215         (\"REGEXP\" [ \"SCORE-FILE-1\" ] [ \"SCORE-FILE-2\" ] ... )
216
217 If the name of a group is matched by REGEXP, the corresponding scorefiles
218 will be used for that group.
219 If multiple REGEXPs match a group, the score files corresponding to each
220 match will be used (for only one match to be used, see
221 gnus-score-file-single-match-alist).
222
223 These score files are loaded in addition to any files returned by
224 gnus-score-find-score-files-function (which see).")
225
226
227 (defvar gnus-score-file-suffix "SCORE"
228   "*Suffix of the score files.")
229
230 (defvar gnus-adaptive-file-suffix "ADAPT"
231   "*Suffix of the adaptive score files.")
232
233 (defvar gnus-score-find-score-files-function 'gnus-score-find-bnews
234   "*Function used to find SCORE files.
235 The function will be called with the group name as the argument, and
236 should return a list of score files to apply to that group.  The score
237 files do not actually have to exist.
238
239 Predefined values are:
240
241 gnus-score-find-single: Only apply the group's own SCORE file.
242 gnus-score-find-hierarchical: Also apply SCORE files from parent groups.
243 gnus-score-find-bnews: Apply SCORE files whose names matches.
244
245 See the documentation to these functions for more information.
246
247 This variable can also be a list of functions to be called.  Each
248 function should either return a list of score files, or a list of
249 score alists.")
250
251 (defvar gnus-score-interactive-default-score 1000
252   "*Scoring commands will raise/lower the score with this number as the default.")
253
254 (defvar gnus-large-newsgroup 200
255   "*The number of articles which indicates a large newsgroup.
256 If the number of articles in a newsgroup is greater than this value,
257 confirmation is required for selecting the newsgroup.")
258
259 ;; Suggested by Andrew Eskilsson <pi92ae@lelle.pt.hk-r.se>.
260 (defvar gnus-no-groups-message "No news is horrible news"
261   "*Message displayed by Gnus when no groups are available.")
262
263 (defvar gnus-use-long-file-name (not (memq system-type '(usg-unix-v xenix)))
264   "*Non-nil means that the default name of a file to save articles in is the group name.
265 If it's nil, the directory form of the group name is used instead.
266
267 If this variable is a list, and the list contains the element
268 `not-score', long file names will not be used for score files; if it
269 contains the element `not-save', long file names will not be used for
270 saving; and if it contains the element `not-kill', long file names
271 will not be used for kill files.")
272
273 (defvar gnus-article-save-directory (or (getenv "SAVEDIR") "~/News/")
274   "*Name of the directory articles will be saved in (default \"~/News\").
275 Initialized from the SAVEDIR environment variable.")
276
277 (defvar gnus-kill-files-directory (or (getenv "SAVEDIR") "~/News/")
278   "*Name of the directory where kill files will be stored (default \"~/News\").
279 Initialized from the SAVEDIR environment variable.")
280
281 (defvar gnus-default-article-saver 'gnus-summary-save-in-rmail
282   "*A function to save articles in your favorite format.
283 The function must be interactively callable (in other words, it must
284 be an Emacs command).
285
286 Gnus provides the following functions:
287
288 * gnus-summary-save-in-rmail (Rmail format)
289 * gnus-summary-save-in-mail (Unix mail format)
290 * gnus-summary-save-in-folder (MH folder)
291 * gnus-summary-save-in-file (article format).
292 * gnus-summary-save-in-vm (use VM's folder format).")
293
294 (defvar gnus-rmail-save-name (function gnus-plain-save-name)
295   "*A function generating a file name to save articles in Rmail format.
296 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
297
298 (defvar gnus-mail-save-name (function gnus-plain-save-name)
299   "*A function generating a file name to save articles in Unix mail format.
300 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
301
302 (defvar gnus-folder-save-name (function gnus-folder-save-name)
303   "*A function generating a file name to save articles in MH folder.
304 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER.")
305
306 (defvar gnus-file-save-name (function gnus-numeric-save-name)
307   "*A function generating a file name to save articles in article format.
308 The function is called with NEWSGROUP, HEADERS, and optional
309 LAST-FILE.")
310
311 (defvar gnus-split-methods nil
312   "*Variable used to suggest where articles are to be saved.
313 The syntax of this variable is the same as `nnmail-split-methods'.  
314
315 For instance, if you would like to save articles related to Gnus in
316 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
317 you could set this variable to something like:
318
319  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
320    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))")
321
322 (defvar gnus-save-score nil
323   "*If non-nil, save group scoring info.")
324
325 (defvar gnus-use-adaptive-scoring nil
326   "*If non-nil, use some adaptive scoring scheme.")
327
328 (defvar gnus-use-cache nil
329   "*If non-nil, Gnus will cache (some) articles locally.")
330
331 (defvar gnus-use-scoring t
332   "*If non-nil, enable scoring.")
333
334 (defvar gnus-fetch-old-headers nil
335   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
336 If an unread article in the group refers to an older, already read (or
337 just marked as read) article, the old article will not normally be
338 displayed in the Summary buffer.  If this variable is non-nil, Gnus
339 will attempt to grab the headers to the old articles, and thereby
340 build complete threads.  If it has the value `some', only enough
341 headers to connect otherwise loose threads will be displayed.
342
343 The server has to support XOVER for any of this to work.")
344
345 (defvar gnus-visual t
346   "*If non-nil, will do various highlighting.
347 If nil, no mouse highlights (or any other highlights) will be
348 performed.  This might speed up Gnus some when generating large group
349 and summary buffers.")
350
351 (defvar gnus-novice-user t
352   "*Non-nil means that you are a usenet novice.
353 If non-nil, verbose messages may be displayed and confirmations may be
354 required.")
355
356 (defvar gnus-expert-user nil
357   "*Non-nil means that you will never be asked for confirmation about anything.
358 And that means *anything*.")
359
360 (defvar gnus-verbose 7
361   "*Integer that says how verbose Gnus should be.
362 The higher the number, the more messages Gnus will flash to say what
363 it's doing.  At zero, Gnus will be totally mute; at five, Gnus will
364 display most important messages; and at ten, Gnus will keep on
365 jabbering all the time.")
366
367 (defvar gnus-keep-same-level nil
368   "*Non-nil means that the next newsgroup after the current will be on the same level.
369 When you type, for instance, `n' after reading the last article in the
370 current newsgroup, you will go to the next newsgroup. If this variable
371 is nil, the next newsgroup will be the next from the group
372 buffer. 
373 If this variable is non-nil, Gnus will either put you in the
374 next newsgroup with the same level, or, if no such newsgroup is
375 available, the next newsgroup with the lowest possible level higher
376 than the current level.
377 If this variable is `best', Gnus will make the next newsgroup the one
378 with the best level.")
379
380 (defvar gnus-summary-make-false-root 'adopt
381   "*nil means that Gnus won't gather loose threads.
382 If the root of a thread has expired or been read in a previous
383 session, the information necessary to build a complete thread has been
384 lost. Instead of having many small sub-threads from this original thread
385 scattered all over the summary buffer, Gnus can gather them. 
386
387 If non-nil, Gnus will try to gather all loose sub-threads from an
388 original thread into one large thread.
389
390 If this variable is non-nil, it should be one of `none', `adopt',
391 `dummy' or `empty'.
392
393 If this variable is `none', Gnus will not make a false root, but just
394 present the sub-threads after another.
395 If this variable is `dummy', Gnus will create a dummy root that will
396 have all the sub-threads as children.
397 If this variable is `adopt', Gnus will make one of the \"children\"
398 the parent and mark all the step-children as such.
399 If this variable is `empty', the \"children\" are printed with empty
400 subject fields.  (Or rather, they will be printed with a string
401 given by the `gnus-summary-same-subject' variable.)")
402
403 (defvar gnus-summary-gather-subject-limit nil
404   "*Maximum length of subject comparisons when gathering loose threads.
405 Use nil to compare full subjects.  Setting this variable to a low
406 number will help gather threads that have been corrupted by
407 newsreaders chopping off subject lines, but it might also mean that
408 unrelated articles that have subject that happen to begin with the
409 same few characters will be incorrectly gathered.
410
411 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
412 comparing subjects.")
413
414 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
415 (defvar gnus-summary-same-subject ""
416   "*String indicating that the current article has the same subject as the previous.
417 This variable will only be used if the value of
418 `gnus-summary-make-false-root' is `empty'.")
419
420 (defvar gnus-summary-goto-unread nil
421   "*If non-nil, marking commands will go to the next unread article.")
422
423 (defvar gnus-group-goto-unread t
424   "*If non-nil, movement commands will go to the next unread and subscribed group.")
425
426 (defvar gnus-check-new-newsgroups t
427   "*Non-nil means that Gnus will add new newsgroups at startup.
428 If this variable is `ask-server', Gnus will ask the server for new
429 groups since the last time it checked. This means that the killed list
430 is no longer necessary, so you could set `gnus-save-killed-list' to
431 nil. 
432
433 A variant is to have this variable be a list of select methods. Gnus
434 will then use the `ask-server' method on all these select methods to
435 query for new groups from all those servers.
436
437 Eg.
438   (setq gnus-check-new-newsgroups 
439         '((nntp \"some.server\") (nntp \"other.server\")))
440
441 If this variable is nil, then you have to tell Gnus explicitly to
442 check for new newsgroups with \\<gnus-group-mode-map>\\[gnus-find-new-newsgroups].")
443
444 (defvar gnus-check-bogus-newsgroups nil
445   "*Non-nil means that Gnus will check and remove bogus newsgroup at startup.
446 If this variable is nil, then you have to tell Gnus explicitly to
447 check for bogus newsgroups with \\<gnus-group-mode-map>\\[gnus-group-check-bogus-groups].")
448
449 (defvar gnus-read-active-file t
450   "*Non-nil means that Gnus will read the entire active file at startup.
451 If this variable is nil, Gnus will only know about the groups in your
452 `.newsrc' file.
453
454 If this variable is `some', Gnus will try to only read the relevant
455 parts of the active file from the server.  Not all servers support
456 this, and it might be quite slow with other servers, but this should
457 generally be faster than both the t and nil value.
458
459 If you set this variable to nil or `some', you probably still want to
460 be told about new newsgroups that arrive.  To do that, set
461 `gnus-check-new-newsgroups' to `ask-server'.  This may not work
462 properly with all servers.")
463
464 (defvar gnus-level-subscribed 5
465   "*Groups with levels less than or equal to this variable are subscribed.")
466
467 (defvar gnus-level-unsubscribed 7
468   "*Groups with levels less than or equal to this variable are unsubscribed.
469 Groups with levels less than `gnus-level-subscribed', which should be
470 less than this variable, are subscribed.")
471
472 (defvar gnus-level-zombie 8
473   "*Groups with this level are zombie groups.")
474
475 (defvar gnus-level-killed 9
476   "*Groups with this level are killed.")
477
478 (defvar gnus-level-default-subscribed 3
479   "*New subscribed groups will be subscribed at this level.")
480
481 (defvar gnus-level-default-unsubscribed 6
482   "*New unsubscribed groups will be unsubscribed at this level.")
483
484 (defvar gnus-activate-foreign-newsgroups nil
485   "*If nil, Gnus will not check foreign newsgroups at startup.
486 If it is non-nil, it should be a number between one and nine. Foreign
487 newsgroups that have a level lower or equal to this number will be
488 activated on startup. For instance, if you want to active all
489 subscribed newsgroups, but not the rest, you'd set this variable to 
490 `gnus-level-subscribed'.
491
492 If you subscribe to lots of newsgroups from different servers, startup
493 might take a while. By setting this variable to nil, you'll save time,
494 but you won't be told how many unread articles there are in the
495 groups.")
496
497 (defvar gnus-save-newsrc-file t
498   "*Non-nil means that Gnus will save the `.newsrc' file.
499 Gnus always saves its own startup file, which is called
500 \".newsrc.eld\".  The file called \".newsrc\" is in a format that can
501 be readily understood by other newsreaders.  If you don't plan on
502 using other newsreaders, set this variable to nil to save some time on
503 exit.")
504
505 (defvar gnus-save-killed-list t
506   "*If non-nil, save the list of killed groups to the startup file.
507 This will save both time (when starting and quitting) and space (both
508 memory and disk), but it will also mean that Gnus has no record of
509 which groups are new and which are old, so the automatic new
510 newsgroups subscription methods become meaningless. You should always
511 set `gnus-check-new-newsgroups' to `ask-server' or nil if you set this
512 variable to nil.")
513
514 (defvar gnus-interactive-catchup t
515   "*If non-nil, require your confirmation when catching up a group.")
516
517 (defvar gnus-interactive-post t
518   "*If non-nil, group name will be asked for when posting.")
519
520 (defvar gnus-interactive-exit t
521   "*If non-nil, require your confirmation when exiting Gnus.")
522
523 (defvar gnus-kill-killed nil
524   "*If non-nil, Gnus will apply kill files to already killed articles.
525 If it is nil, Gnus will never apply kill files to articles that have
526 already been through the scoring process, which might very well save lots
527 of time.")
528
529 (defvar gnus-extract-address-components 'gnus-extract-address-components
530   "*Function for extracting address components from a From header.
531 Two pre-defined function exist: `gnus-extract-address-components',
532 which is the default, quite fast, and too simplistic solution, and
533 `mail-extract-address-components', which works much better, but is
534 slower.")
535
536 (defvar gnus-summary-default-score 0
537   "*Default article score level.
538 If this variable is nil, scoring will be disabled.")
539
540 (defvar gnus-summary-zcore-fuzz 0
541   "*Fuzziness factor for the zcore in the summary buffer.
542 Articles with scores closer than this to `gnus-summary-default-score'
543 will not be marked.")
544
545 (defvar gnus-simplify-subject-fuzzy-regexp nil
546   "*Regular expression that will be removed from subject strings if
547 fuzzy subject simplification is selected.")
548
549 (defvar gnus-group-default-list-level gnus-level-subscribed
550   "*Default listing level. 
551 Ignored if `gnus-group-use-permanent-levels' is nil.")
552
553 (defvar gnus-group-use-permanent-levels nil
554   "*If non-nil, once you set a level, Gnus will use this level.")
555
556 (defvar gnus-show-mime nil
557   "*If non-ni, do mime processing of articles.
558 The articles will simply be fed to the function given by
559 `gnus-show-mime-method'.")
560
561 (defvar gnus-strict-mime t
562   "*If nil, decode MIME header even if there is not Mime-Version field.")
563  
564 (defvar gnus-show-mime-method (function metamail-buffer)
565   "*Function to process a MIME message.
566 The function is called from the article buffer.")
567
568 (defvar gnus-show-threads t
569   "*If non-nil, display threads in summary mode.")
570
571 (defvar gnus-thread-hide-subtree nil
572   "*If non-nil, hide all threads initially.
573 If threads are hidden, you have to run the command
574 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
575 to expose hidden threads.")
576
577 (defvar gnus-thread-hide-killed t
578   "*If non-nil, hide killed threads automatically.")
579
580 (defvar gnus-thread-ignore-subject nil
581   "*If non-nil, ignore subjects and do all threading based on the Reference header.
582 If nil, which is the default, articles that have different subjects
583 from their parents will start separate threads.")
584
585 (defvar gnus-thread-indent-level 4
586   "*Number that says how much each sub-thread should be indented.")
587
588 (defvar gnus-ignored-newsgroups ""
589   "*A regexp to match uninteresting newsgroups in the active file.
590 Any lines in the active file matching this regular expression are
591 removed from the newsgroup list before anything else is done to it,
592 thus making them effectively non-existent.")
593
594 (defvar gnus-ignored-headers
595   "^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:"
596   "*All headers that match this regexp will be hidden.
597 Also see `gnus-visible-headers'.")
598
599 (defvar gnus-visible-headers "^From:\\|^Newsgroups:\\|^Subject:\\|^Date:\\|^Followup-To:\\|^Reply-To:\\|^Organization:\\|^Summary:\\|^Keywords:\\|^To:\\|^Cc:"
600   "*All headers that do not match this regexp will be hidden.
601 Also see `gnus-ignored-headers'.")
602
603 (defvar gnus-sorted-header-list
604   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:" "^To:" 
605     "^Cc:" "^Date:" "^Organization:")
606   "*This variable is a list of regular expressions.
607 If it is non-nil, headers that match the regular expressions will
608 be placed first in the article buffer in the sequence specified by
609 this list.")
610
611 (defvar gnus-show-all-headers nil
612   "*If non-nil, don't hide any headers.")
613
614 (defvar gnus-save-all-headers t
615   "*If non-nil, don't remove any headers before saving.")
616
617 (defvar gnus-inhibit-startup-message nil
618   "*If non-nil, the startup message will not be displayed.")
619
620 (defvar gnus-signature-separator "^-- *$"
621   "Regexp matching signature separator.")
622
623 (defvar gnus-auto-extend-newsgroup t
624   "*If non-nil, extend newsgroup forward and backward when requested.")
625
626 (defvar gnus-auto-select-first t
627   "*If non-nil, select the first unread article when entering a group.
628 If you want to prevent automatic selection of the first unread article
629 in some newsgroups, set the variable to nil in
630 `gnus-select-group-hook'.") 
631
632 (defvar gnus-auto-select-next t
633   "*If non-nil, offer to go to the next group from the end of the previous.
634 If the value is t and the next newsgroup is empty, Gnus will exit
635 summary mode and go back to group mode.  If the value is neither nil
636 nor t, Gnus will select the following unread newsgroup.  In
637 particular, if the value is the symbol `quietly', the next unread
638 newsgroup will be selected without any confirmations.")
639
640 (defvar gnus-auto-select-same nil
641   "*If non-nil, select the next article with the same subject.")
642
643 (defvar gnus-summary-check-current nil
644   "*If non-nil, consider the current article when moving.
645 The \"unread\" movement commands will stay on the same line if the
646 current article is unread.")
647
648 (defvar gnus-auto-center-summary t
649   "*If non-nil, always center the current summary buffer.")
650
651 (defvar gnus-break-pages t
652   "*If non-nil, do page breaking on articles.
653 The page delimiter is specified by the `gnus-page-delimiter'
654 variable.")
655
656 (defvar gnus-page-delimiter "^\^L"
657   "*Regexp describing what to use as article page delimiters.
658 The default value is \"^\^L\", which is a form linefeed at the
659 beginning of a line.")
660
661 (defvar gnus-use-full-window t
662   "*If non-nil, use the entire Emacs screen.")
663
664 (defvar gnus-window-configuration nil
665   "Obsolete variable.  See `gnus-buffer-configuration'.")
666
667 (defvar gnus-buffer-configuration
668   '((group ([group 1.0 point] 
669             (if gnus-carpal [group-carpal 4])))
670     (summary ([summary 1.0 point]
671               (if gnus-carpal [summary-carpal 4])))
672     (article ([summary 0.25 point] 
673               (if gnus-carpal [summary-carpal 4]) 
674               [article 1.0]))
675     (server ([server 1.0 point]
676              (if gnus-carpal [server-carpal 2])))
677     (browse ([browse 1.0 point]
678              (if gnus-carpal [browse-carpal 2])))
679     (group-mail ([mail 1.0 point]))
680     (summary-mail ([mail 1.0 point]))
681     (summary-reply ([article 0.5]
682                     [mail 1.0 point]))
683     (info ([nil 1.0 point]))
684     (summary-faq ([summary 0.25]
685                   [article 1.0 point]))
686     (edit-group ([group 0.5]
687                  [edit-group 1.0 point]))
688     (edit-server ([server 0.5]
689                   [edit-server 1.0 point]))
690     (post ([post 1.0 point]))
691     (reply ([article 0.5]
692             [mail 1.0 point]))
693     (mail-forward ([mail 1.0 point]))
694     (post-forward ([post 1.0 point]))
695     (reply-yank ([mail 1.0 point]))
696     (followup ([article 0.5]
697                [post 1.0 point]))
698     (followup-yank ([post 1.0 point])))
699   "Window configuration for all possible Gnus buffers.
700 This variable is a list of lists.  Each of these lists has a NAME and
701 a RULE.  The NAMEs are commonsense names like `group', which names a
702 rule used when displaying the group buffer; `summary', which names a
703 rule for what happens when you enter a group and do not display an
704 article buffer; and so on.  See the value of this variable for a
705 complete list of NAMEs.
706
707 Each RULE is a list of vectors.  The first element in this vector is
708 the name of the buffer to be displayed; the second element is the
709 percentage of the screen this buffer is to occupy (a number in the
710 0.0-0.99 range); the optional third element is `point', which should
711 be present to denote which buffer point is to go to after making this
712 buffer configuration.")
713
714 (defvar gnus-window-to-buffer
715   '((group . gnus-group-buffer)
716     (summary . gnus-summary-buffer)
717     (article . gnus-article-buffer)
718     (server . gnus-server-buffer)
719     (browse . "*Gnus Browse Server*")
720     (edit-group . gnus-group-edit-buffer)
721     (edit-server . gnus-server-edit-buffer)
722     (group-carpal . gnus-carpal-group-buffer)
723     (summary-carpal . gnus-carpal-summary-buffer)
724     (server-carpal . gnus-carpal-server-buffer)
725     (browse-carpal . gnus-carpal-browse-buffer)
726     (mail . gnus-mail-buffer)
727     (post . gnus-post-news-buffer))
728   "Mapping from short symbols to buffer names or buffer variables.")
729
730 (defvar gnus-carpal nil
731   "*If non-nil, display clickable icons.")
732
733 (defvar gnus-subscribe-newsgroup-method 'gnus-subscribe-zombies
734   "*Function called with a group name when new group is detected.
735 A few pre-made functions are supplied: `gnus-subscribe-randomly'
736 inserts new groups at the beginning of the list of groups;
737 `gnus-subscribe-alphabetically' inserts new groups in strict
738 alphabetic order; `gnus-subscribe-hierarchically' inserts new groups
739 in hierarchical newsgroup order; `gnus-subscribe-interactively' asks
740 for your decision.")
741
742 ;; Suggested by a bug report by Hallvard B Furuseth.
743 ;; <h.b.furuseth@usit.uio.no>. 
744 (defvar gnus-subscribe-options-newsgroup-method
745   (function gnus-subscribe-alphabetically)
746   "*This function is called to subscribe newsgroups mentioned on \"options -n\" lines.
747 If, for instance, you want to subscribe to all newsgroups in the
748 \"no\" and \"alt\" hierarchies, you'd put the following in your
749 .newsrc file:
750
751 options -n no.all alt.all
752
753 Gnus will the subscribe all new newsgroups in these hierarchies with
754 the subscription method in this variable.")
755
756 (defvar gnus-subscribe-hierarchical-interactive nil
757   "*If non-nil, Gnus will offer to subscribe hierarchically.
758 When a new hierarchy appears, Gnus will ask the user:
759
760 'alt.binaries': Do you want to subscribe to this hierarchy? ([d]ys):
761
762 If the user pressed `d', Gnus will descend the hierarchy, `y' will
763 subscribe to all newsgroups in the hierarchy and `s' will skip this
764 hierarchy in its entirety.")
765
766 (defvar gnus-group-sort-function 'gnus-group-sort-by-alphabet
767   "*Function used for sorting the group buffer.
768 This function will be called with group info entries as the arguments
769 for the groups to be sorted.  Pre-made functions include
770 `gnus-sort-by-alphabet', `gnus-sort-by-unread' and
771 `gnus-sort-by-level'")
772
773 ;; Mark variables suggested by Thomas Michanek
774 ;; <Thomas.Michanek@telelogic.se>. 
775 (defvar gnus-unread-mark ? 
776   "*Mark used for unread articles.")
777 (defvar gnus-ticked-mark ?!
778   "*Mark used for ticked articles.")
779 (defvar gnus-dormant-mark ??
780   "*Mark used for dormant articles.")
781 (defvar gnus-del-mark ?D
782   "*Mark used for del'd articles.")
783 (defvar gnus-read-mark ?d
784   "*Mark used for read articles.")
785 (defvar gnus-expirable-mark ?E
786   "*Mark used for expirable articles.")
787 (defvar gnus-killed-mark ?K
788   "*Mark used for killed articles.")
789 (defvar gnus-kill-file-mark ?X
790   "*Mark used for articles killed by kill files.")
791 (defvar gnus-low-score-mark ?Y
792   "*Mark used for articles with a low score.")
793 (defvar gnus-catchup-mark ?C
794   "*Mark used for articles that are caught up.")
795 (defvar gnus-replied-mark ?R
796   "*Mark used for articles that have been replied to.")
797 (defvar gnus-process-mark ?# 
798   "*Process mark.")
799 (defvar gnus-ancient-mark ?A
800   "*Mark used for ancient articles.")
801 (defvar gnus-canceled-mark ?G
802   "*Mark used for canceled articles.")
803 (defvar gnus-score-over-mark ?+
804   "*Score mark used for articles with high scores.")
805 (defvar gnus-score-below-mark ?-
806   "*Score mark used for articles with low scores.")
807 (defvar gnus-empty-thread-mark ? 
808   "*There is no thread under the article.")
809 (defvar gnus-not-empty-thread-mark ?=
810   "*There is a thread under the article.")
811 (defvar gnus-dummy-mark ?Z
812   "*This is a dummy article.")
813
814 (defvar gnus-view-pseudo-asynchronously nil
815   "*If non-nil, Gnus will view pseudo-articles asynchronously.")
816
817 (defvar gnus-view-pseudos nil
818   "*If `automatic', pseudo-articles will be viewed automatically.
819 If `not-confirm', pseudos will be viewed automatically, and the user
820 will not be asked to confirm the command.")
821
822 (defvar gnus-view-pseudos-separately t
823   "*If non-nil, one pseudo-article will be created for each file to be viewed.
824 If nil, all files that use the same viewing command will be given as a
825 list of parameters to that command.")
826
827 (defvar gnus-group-line-format "%M%S%p%5y: %(%g%)\n"
828   "*Format of group lines.
829 It works along the same lines as a normal formatting string,
830 with some simple extensions.
831
832 %M    Only marked articles (character, \"*\" or \" \")
833 %S    Whether the group is subscribed (character, \"U\", \"K\", \"Z\" or \" \")
834 %L    Level of subscribedness (integer)
835 %N    Number of unread articles (integer)
836 %I    Number of dormant articles (integer)
837 %i    Number of ticked and dormant (integer)
838 %T    Number of ticked articles (integer)
839 %R    Number of read articles (integer)
840 %t    Total number of articles (integer)
841 %y    Number of unread, unticked articles (integer)
842 %G    Group name (string)
843 %g    Qualified group name (string)
844 %D    Group description (string)
845 %s    Select method (string)
846 %o    Moderated group (char, \"m\")
847 %p    Process mark (char)
848 %O    Moderated group (string, \"(m)\" or \"\")
849 %n    Select from where (string)
850 %z    A string that look like `<%s:%n>' if a foreign select method is used
851 %u    User defined specifier. The next character in the format string should
852       be a letter.  Gnus will call the function gnus-user-format-function-X,
853       where X is the letter following %u. The function will be passed the
854       current header as argument. The function should return a string, which
855       will be inserted into the buffer just like information from any other
856       group specifier.
857
858 Text between %( and %) will be highlighted with `gnus-mouse-face' when
859 the mouse point move inside the area.  There can only be one such area.
860
861 Note that this format specification is not always respected. For
862 reasons of efficiency, when listing killed groups, this specification
863 is ignored altogether. If the spec is changed considerably, your
864 output may end up looking strange when listing both alive and killed
865 groups.
866
867 If you use %o or %O, reading the active file will be slower and quite
868 a bit of extra memory will be used. %D will also worsen performance.
869 Also note that if you change the format specification to include any
870 of these specs, you must probably re-start Gnus to see them go into
871 effect.") 
872
873 (defvar gnus-summary-line-format "%U%R%z%I%(%[%4L: %-20,20n%]%) %s\n"
874   "*The format specification of the lines in the summary buffer.
875
876 It works along the same lines as a normal formatting string,
877 with some simple extensions.
878
879 %N   Article number, left padded with spaces (string)
880 %S   Subject (string)
881 %s   Subject if it is at the root of a thread, and \"\" otherwise (string)
882 %n   Name of the poster (string)
883 %A   Address of the poster (string)
884 %F   Contents of the From: header (string)
885 %x   Contents of the Xref: header (string)
886 %D   Date of the article (string)
887 %d   Date of the article (string) in DD-MMM format
888 %M   Message-id of the article (string)
889 %r   References of the article (string)
890 %c   Number of characters in the article (integer)
891 %L   Number of lines in the article (integer)
892 %I   Indentation based on thread level (a string of spaces)
893 %T   A string with two possible values: 80 spaces if the article
894      is on thread level two or larger and 0 spaces on level one
895 %R   \"R\" if this article has been replied to, \" \" otherwise (character)
896 %U   Status of this article (character, \"D\", \"K\", \"-\" or \" \")
897 %[   Opening bracket (character, \"[\" or \"<\")
898 %]   Closing bracket (character, \"]\" or \">\")
899 %>   Spaces of length thread-level (string)
900 %<   Spaces of length (- 20 thread-level) (string)
901 %i   Article score (number)
902 %z   Article zcore (character)
903 %t   Number of articles under the current thread (number).
904 %e   Whether the thread is empty or not (character).
905 %u   User defined specifier. The next character in the format string should
906      be a letter.  Gnus will call the function gnus-user-format-function-X,
907      where X is the letter following %u. The function will be passed the
908      current header as argument. The function should return a string, which
909      will be inserted into the summary just like information from any other
910      summary specifier.
911
912 Text between %( and %) will be highlighted with `gnus-mouse-face'
913 when the mouse point is placed inside the area.  There can only be one
914 such area.
915
916 The %U (status), %R (replied) and %z (zcore) specs have to be handled
917 with care. For reasons of efficiency, Gnus will compute what column
918 these characters will end up in, and \"hard-code\" that. This means that
919 it is illegal to have these specs after a variable-length spec. Well,
920 you might not be arrested, but your summary buffer will look strange,
921 which is bad enough.
922
923 The smart choice is to have these specs as for to the left as
924 possible. 
925
926 This restriction may disappear in later versions of Gnus.")
927
928 (defvar gnus-summary-dummy-line-format "*  :                          : %S\n"
929   "*The format specification for the dummy roots in the summary buffer.
930 It works along the same lines as a normal formatting string,
931 with some simple extensions.
932
933 %S  The subject")
934
935 (defvar gnus-summary-mode-line-format "(ding) %G/%A %Z"
936   "*The format specification for the summary mode line.")
937
938 (defvar gnus-article-mode-line-format "(ding) %G/%A %S"
939   "*The format specification for the article mode line.")
940
941 (defvar gnus-group-mode-line-format "(ding) List of groups   {%M:%S}  "
942   "*The format specification for the group mode line.")
943
944 (defvar gnus-valid-select-methods
945   '(("nntp" post address prompt-address)
946     ("nnspool" post)
947     ("nnvirtual" none virtual prompt-address) 
948     ("nnmbox" mail respool) 
949     ("nnml" mail respool)
950     ("nnmh" mail respool) 
951     ("nndir" none prompt-address address)
952     ("nneething" none prompt-address)
953     ("nndigest" none) 
954     ("nndoc" none prompt-address) 
955     ("nnbabyl" mail respool) 
956     ("nnkiboze" post virtual) 
957     ("nnsoup" post)
958     ("nnfolder" mail respool))
959   "An alist of valid select methods.
960 The first element of each list lists should be a string with the name
961 of the select method. The other elements may be be the category of
962 this method (ie. `post', `mail', `none' or whatever) or other
963 properties that this method has (like being respoolable).
964 If you implement a new select method, all you should have to change is
965 this variable. I think.")
966
967 (defvar gnus-updated-mode-lines '(group article summary)
968   "*List of buffers that should update their mode lines.
969 The list may contain the symbols `group', `article' and `summary'. If
970 the corresponding symbol is present, Gnus will keep that mode line
971 updated with information that may be pertinent. 
972 If this variable is nil, screen refresh may be quicker.")
973
974 ;; Added by Keinonen Kari <kk85613@cs.tut.fi>.
975 (defvar gnus-mode-non-string-length 21
976   "*Max length of mode-line non-string contents.
977 If this is nil, Gnus will take space as is needed, leaving the rest
978 of the modeline intact.")
979
980 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
981 (defvar gnus-mouse-face 'highlight
982   "*Face used for mouse highlighting in Gnus.
983 No mouse highlights will be done if `gnus-visual' is nil.")
984
985 (defvar gnus-summary-mark-below nil
986   "*Mark all articles with a score below this variable as read.
987 This variable is local to each summary buffer and usually set by the
988 score file.")  
989
990 (defvar gnus-thread-sort-functions '(gnus-thread-sort-by-number)
991   "*List of functions used for sorting threads in the summary buffer.
992 By default, threads are sorted by article number.
993
994 Each function takes two threads and return non-nil if the first thread
995 should be sorted before the other.  If you use more than one function,
996 the primary sort function should be the last.
997
998 Ready-mady functions include `gnus-thread-sort-by-number',
999 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
1000 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
1001 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').")
1002
1003 (defvar gnus-thread-score-function '+
1004   "*Function used for calculating the total score of a thread.
1005
1006 The function is called with the scores of the article and each
1007 subthread and should then return the score of the thread.
1008
1009 Some functions you can use are `+', `max', or `min'.")
1010
1011 (defvar gnus-options-subscribe nil
1012   "*All new groups matching this regexp will be subscribed unconditionally.
1013 Note that this variable deals only with new newsgroups.  This variable
1014 does not affect old newsgroups.")
1015
1016 (defvar gnus-options-not-subscribe nil
1017   "*All new groups matching this regexp will be ignored.
1018 Note that this variable deals only with new newsgroups.  This variable
1019 does not affect old (already subscribed) newsgroups.")
1020
1021 (defvar gnus-auto-expirable-newsgroups nil
1022   "*Groups in which to automatically mark read articles as expirable.
1023 If non-nil, this should be a regexp that should match all groups in
1024 which to perform auto-expiry.  This only makes sense for mail groups.")
1025
1026 (defvar gnus-hidden-properties '(invisible t intangible t)
1027   "Property list to use for hiding text.")
1028
1029 ;; Hooks.
1030
1031 (defvar gnus-group-mode-hook nil
1032   "*A hook for Gnus group mode.")
1033
1034 (defvar gnus-summary-mode-hook nil
1035   "*A hook for Gnus summary mode.")
1036
1037 (defvar gnus-article-mode-hook nil
1038   "*A hook for Gnus article mode.")
1039
1040 (defvar gnus-open-server-hook nil
1041   "*A hook called just before opening connection to the news server.")
1042
1043 (defvar gnus-startup-hook nil
1044   "*A hook called at startup.
1045 This hook is called after Gnus is connected to the NNTP server.")
1046
1047 (defvar gnus-get-new-news-hook nil
1048   "*A hook run just before Gnus checks for new news.")
1049
1050 (defvar gnus-group-prepare-function 'gnus-group-prepare-flat
1051   "*A function that is called to generate the group buffer.
1052 The function is called with three arguments: The first is a number;
1053 all group with a level less or equal to that number should be listed,
1054 if the second is non-nil, empty groups should also be displayed. If
1055 the third is non-nil, it is a number. No groups with a level lower
1056 than this number should be displayed.
1057
1058 The only current function implemented is `gnus-group-prepare-flat'.")
1059
1060 (defvar gnus-group-prepare-hook nil
1061   "*A hook called after the group buffer has been generated.
1062 If you want to modify the group buffer, you can use this hook.")
1063
1064 (defvar gnus-summary-prepare-hook nil
1065   "*A hook called after the summary buffer has been generated.
1066 If you want to modify the summary buffer, you can use this hook.")
1067
1068 (defvar gnus-article-prepare-hook nil
1069   "*A hook called after an article has been prepared in the article buffer.
1070 If you want to run a special decoding program like nkf, use this hook.")
1071
1072 (defvar gnus-article-display-hook nil
1073   "*A hook called after the article is displayed in the article buffer.
1074 The hook is designed to change the contents of the article
1075 buffer. Typical functions that this hook may contain are
1076 `gnus-article-hide-headers' (hide selected headers),
1077 `gnus-article-maybe-highlight' (perform fancy article highlighting), 
1078 `gnus-article-hide-signature' (hide signature) and
1079 `gnus-article-treat-overstrike' (turn \"^H_\" into bold characters).")
1080 (add-hook 'gnus-article-display-hook 'gnus-article-hide-headers-if-wanted)
1081 (add-hook 'gnus-article-display-hook 'gnus-article-treat-overstrike)
1082
1083 (defvar gnus-article-x-face-command
1084   "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | xv -quit -"
1085   "String or function to be executed to display an X-Face header.
1086 If it is a string, the command will be executed in a sub-shell
1087 asynchronously. The compressed face will be piped to this command.") 
1088
1089 (defvar gnus-article-x-face-too-ugly nil
1090   "Regexp matching posters whose face shouldn't be shown automatically.")
1091
1092 (defvar gnus-select-group-hook nil
1093   "*A hook called when a newsgroup is selected.
1094
1095 If you'd like to simplify subjects like the
1096 `gnus-summary-next-same-subject' command does, you can use the
1097 following hook:
1098
1099  (setq gnus-select-group-hook
1100       (list
1101         (lambda ()
1102           (mapcar (lambda (header)
1103                      (header-set-subject
1104                       header
1105                       (gnus-simplify-subject
1106                        (header-subject header) 're-only)))
1107                   gnus-newsgroup-headers))))")
1108
1109 (defvar gnus-select-article-hook
1110   '(gnus-summary-show-thread)
1111   "*A hook called when an article is selected.
1112 The default hook shows conversation thread subtrees of the selected
1113 article automatically using `gnus-summary-show-thread'.")
1114
1115 (defvar gnus-apply-kill-hook '(gnus-apply-kill-file)
1116   "*A hook called to apply kill files to a group.
1117 This hook is intended to apply a kill file to the selected newsgroup.
1118 The function `gnus-apply-kill-file' is called by default.
1119
1120 Since a general kill file is too heavy to use only for a few
1121 newsgroups, I recommend you to use a lighter hook function. For
1122 example, if you'd like to apply a kill file to articles which contains
1123 a string `rmgroup' in subject in newsgroup `control', you can use the
1124 following hook:
1125
1126 \(setq gnus-apply-kill-hook
1127       (list
1128         (lambda ()
1129           (cond ((string-match \"control\" gnus-newsgroup-name)
1130                  (gnus-kill \"Subject\" \"rmgroup\")
1131                  (gnus-expunge \"X\"))))))")
1132
1133 (defvar gnus-visual-mark-article-hook 
1134   (list 'gnus-highlight-selected-summary)
1135   "*Hook run after selecting an article in the summary buffer.
1136 It is meant to be used for highlighting the article in some way.  It
1137 is not run if `gnus-visual' is nil.")
1138
1139 (defvar gnus-prepare-article-hook (list 'gnus-inews-insert-signature)
1140   "*A hook called after preparing body, but before preparing header headers.
1141 The default hook (`gnus-inews-insert-signature') inserts a signature
1142 file specified by the variable `gnus-signature-file'.")
1143
1144 (defvar gnus-exit-group-hook nil
1145   "*A hook called when exiting (not quitting) summary mode.")
1146
1147 (defvar gnus-suspend-gnus-hook nil
1148   "*A hook called when suspending (not exiting) Gnus.")
1149
1150 (defvar gnus-exit-gnus-hook nil
1151   "*A hook called when exiting Gnus.")
1152
1153 (defvar gnus-save-newsrc-hook nil
1154   "*A hook called when saving the newsrc file.")
1155
1156 (defvar gnus-summary-update-hook 
1157   (list 'gnus-summary-highlight-line)
1158   "*A hook called when a summary line is changed.
1159 The hook will not be called if `gnus-visual' is nil.
1160
1161 The default function `gnus-summary-highlight-line' will
1162 highlight the line according to the `gnus-summary-highlight'
1163 variable.")
1164
1165 (defvar gnus-mark-article-hook (list 'gnus-summary-mark-unread-as-read)
1166   "*A hook called when an article is selected for the first time.
1167 The hook is intended to mark an article as read (or unread)
1168 automatically when it is selected.")
1169
1170 \f
1171 ;; Internal variables
1172
1173 ;; Avoid highlighting in kill files.
1174 (defvar gnus-summary-inhibit-highlight nil)
1175 (defvar gnus-newsgroup-selected-overlay nil)
1176
1177 (defvar gnus-article-mode-map nil)
1178 (defvar caesar-translate-table nil)
1179 (defvar gnus-dribble-buffer nil)
1180 (defvar gnus-headers-retrieved-by nil)
1181 (defvar gnus-article-reply nil)
1182 (defvar gnus-override-method nil)
1183 (defvar gnus-article-check-size nil)
1184
1185 (defvar gnus-current-score-file nil)
1186 (defvar gnus-internal-global-score-files nil)
1187 (defvar gnus-score-file-list nil)
1188
1189
1190 (defvar gnus-current-move-group nil)
1191
1192 (defvar gnus-newsgroup-dependencies nil)
1193 (defvar gnus-newsgroup-threads nil)
1194 (defvar gnus-newsgroup-async nil)
1195 (defconst gnus-group-edit-buffer "*Gnus edit newsgroup*")
1196
1197 (defvar gnus-newsgroup-adaptive nil)
1198
1199 (defvar gnus-summary-display-table nil)
1200
1201 (defconst gnus-group-line-format-alist
1202   (list (list ?M 'marked ?c)
1203         (list ?S 'subscribed ?c)
1204         (list ?L 'level ?d)
1205         (list ?N 'number ?s)
1206         (list ?I 'number-of-dormant ?d)
1207         (list ?T 'number-of-ticked ?d)
1208         (list ?R 'number-of-read ?s)
1209         (list ?t 'number-total ?d)
1210         (list ?y 'number-of-unread-unticked ?s)
1211         (list ?i 'number-of-ticked-and-dormant ?d)
1212         (list ?g 'group ?s)
1213         (list ?G 'qualified-group ?s)
1214         (list ?D 'newsgroup-description ?s)
1215         (list ?o 'moderated ?c)
1216         (list ?O 'moderated-string ?s)
1217         (list ?p 'process-marked ?c)
1218         (list ?s 'news-server ?s)
1219         (list ?n 'news-method ?s)
1220         (list ?z 'news-method-string ?s)
1221         (list ?u 'user-defined ?s)))
1222
1223 (defconst gnus-summary-line-format-alist 
1224   (list (list ?N 'number ?d)
1225         (list ?S 'subject ?s)
1226         (list ?s 'subject-or-nil ?s)
1227         (list ?n 'name ?s)
1228         (list ?A 'address ?s)
1229         (list ?F 'from ?s)
1230         (list ?x (macroexpand '(header-xref header)) ?s)
1231         (list ?D (macroexpand '(header-date header)) ?s)
1232         (list ?d '(gnus-dd-mmm (header-date header)) ?s)
1233         (list ?M (macroexpand '(header-id header)) ?s)
1234         (list ?r (macroexpand '(header-references header)) ?s)
1235         (list ?c '(or (header-chars header) 0) ?d)
1236         (list ?L 'lines ?d)
1237         (list ?I 'indentation ?s)
1238         (list ?T '(if (= level 0) "" (make-string (frame-width) ? )) ?s)
1239         (list ?R 'replied ?c)
1240         (list ?\[ 'opening-bracket ?c)
1241         (list ?\] 'closing-bracket ?c)
1242         (list ?\> '(make-string level ? ) ?s)
1243         (list ?\< '(make-string (max 0 (- 20 level)) ? ) ?s)
1244         (list ?i 'score ?d)
1245         (list ?z 'score-char ?c)
1246         (list ?U 'unread ?c)
1247         (list ?t '(gnus-summary-number-of-articles-in-thread 
1248                    (or (prog1 gnus-tmp-adopt-thread 
1249                          (setq gnus-tmp-adopt-thread nil))
1250                        (if (boundp 'thread) (symbol-value 'thread)
1251                          thread nil)))
1252                    ?d)
1253         (list ?e '(gnus-summary-number-of-articles-in-thread 
1254                    (or gnus-tmp-adopt-thread 
1255                        (if (boundp 'thread) (symbol-value 'thread)
1256                          thread nil)) t)
1257                    ?d)
1258         (list ?u 'user-defined ?s))
1259   "An alist of format specifications that can appear in summary lines,
1260 and what variables they correspond with, along with the type of the
1261 variable (string, integer, character, etc).")
1262
1263 (defconst gnus-summary-dummy-line-format-alist
1264   (list (list ?S 'subject ?s)
1265         (list ?N 'number ?d)
1266         (list ?u 'user-defined ?s)))
1267
1268 (defconst gnus-summary-mode-line-format-alist 
1269   (list (list ?G 'group-name ?s)
1270         (list ?g '(gnus-short-group-name group-name) ?s)
1271         (list ?A 'article-number ?d)
1272         (list ?Z 'unread-and-unselected ?s)
1273         (list ?V 'gnus-version ?s)
1274         (list ?U 'unread ?d)
1275         (list ?S 'subject ?s)
1276         (list ?e 'unselected ?d)
1277         (list ?u 'user-defined ?s)
1278         (list ?s '(gnus-current-score-file-nondirectory) ?s)))
1279
1280 (defconst gnus-group-mode-line-format-alist 
1281   (list (list ?S 'news-server ?s)
1282         (list ?M 'news-method ?s)
1283         (list ?u 'user-defined ?s)))
1284
1285 (defvar gnus-have-read-active-file nil)
1286
1287 (defconst gnus-maintainer "gnus-bug@ifi.uio.no (The Gnus Bugfixing Girls & Boys)"
1288   "The mail address of the Gnus maintainers.")
1289
1290 (defconst gnus-version "(ding) Gnus v0.86"
1291   "Version number for this version of Gnus.")
1292
1293 (defvar gnus-info-nodes
1294   '((gnus-group-mode            "(gnus)The Group Buffer")
1295     (gnus-summary-mode          "(gnus)The Summary Buffer")
1296     (gnus-article-mode          "(gnus)The Article Buffer"))
1297   "Assoc list of major modes and related Info nodes.")
1298
1299 (defvar gnus-documentation-group-file "~/dgnus/lisp/doc.txt"
1300   "The location of the (ding) Gnus documentation group.")
1301
1302 (defvar gnus-group-buffer "*Group*")
1303 (defvar gnus-summary-buffer "*Summary*")
1304 (defvar gnus-article-buffer "*Article*")
1305 (defvar gnus-server-buffer "*Server*")
1306
1307 (defvar gnus-work-buffer " *gnus work*")
1308
1309 (defvar gnus-buffer-list nil
1310   "Gnus buffers that should be killed on exit.")
1311
1312 (defvar gnus-server-alist nil
1313   "List of available servers.")
1314
1315 (defvar gnus-variable-list
1316   '(gnus-newsrc-options gnus-newsrc-options-n
1317     gnus-newsrc-last-checked-date 
1318     gnus-newsrc-alist gnus-server-alist
1319     gnus-killed-list gnus-zombie-list)
1320   "Gnus variables saved in the quick startup file.")
1321
1322 (defvar gnus-overload-functions
1323   '((news-inews gnus-inews-news "rnewspost")
1324     (caesar-region gnus-caesar-region "rnews"))
1325   "Functions overloaded by gnus.
1326 It is a list of `(original overload &optional file)'.")
1327
1328 (defvar gnus-newsrc-options nil
1329   "Options line in the .newsrc file.")
1330
1331 (defvar gnus-newsrc-options-n nil
1332   "List of regexps representing groups to be subscribed/ignored unconditionally.") 
1333
1334 (defvar gnus-newsrc-last-checked-date nil
1335   "Date Gnus last asked server for new newsgroups.")
1336
1337 (defvar gnus-newsrc-alist nil
1338   "Assoc list of read articles.
1339 gnus-newsrc-hashtb should be kept so that both hold the same information.")
1340
1341 (defvar gnus-newsrc-hashtb nil
1342   "Hashtable of gnus-newsrc-alist.")
1343
1344 (defvar gnus-killed-list nil
1345   "List of killed newsgroups.")
1346
1347 (defvar gnus-killed-hashtb nil
1348   "Hash table equivalent of gnus-killed-list.")
1349
1350 (defvar gnus-zombie-list nil
1351   "List of almost dead newsgroups.")
1352
1353 (defvar gnus-description-hashtb nil
1354   "Descriptions of newsgroups.")
1355
1356 (defvar gnus-list-of-killed-groups nil
1357   "List of newsgroups that have recently been killed by the user.")
1358
1359 (defvar gnus-active-hashtb nil
1360   "Hashtable of active articles.")
1361
1362 (defvar gnus-moderated-list nil
1363   "List of moderated newsgroups.")
1364
1365 (defvar gnus-group-marked nil)
1366
1367 (defvar gnus-current-startup-file nil
1368   "Startup file for the current host.")
1369
1370 (defvar gnus-last-search-regexp nil
1371   "Default regexp for article search command.")
1372
1373 (defvar gnus-last-shell-command nil
1374   "Default shell command on article.")
1375
1376 (defvar gnus-current-select-method nil
1377   "The current method for selecting a newsgroup.")
1378
1379 (defvar gnus-have-all-newsgroups nil)
1380
1381 (defvar gnus-article-internal-prepare-hook nil)
1382
1383 (defvar gnus-newsgroup-name nil)
1384 (defvar gnus-newsgroup-begin nil)
1385 (defvar gnus-newsgroup-end nil)
1386 (defvar gnus-newsgroup-last-rmail nil)
1387 (defvar gnus-newsgroup-last-mail nil)
1388 (defvar gnus-newsgroup-last-folder nil)
1389 (defvar gnus-newsgroup-last-file nil)
1390 (defvar gnus-newsgroup-auto-expire nil)
1391 (defvar gnus-newsgroup-active nil)
1392
1393 (defvar gnus-newsgroup-unreads nil
1394   "List of unread articles in the current newsgroup.")
1395
1396 (defvar gnus-newsgroup-unselected nil
1397   "List of unselected unread articles in the current newsgroup.")
1398
1399 (defvar gnus-newsgroup-marked nil
1400   "List of ticked articles in the current newsgroup (a subset of unread art).")
1401
1402 (defvar gnus-newsgroup-killed nil
1403   "List of ranges of articles that have been through the scoring process.")
1404
1405 (defvar gnus-newsgroup-kill-headers nil)
1406
1407 (defvar gnus-newsgroup-replied nil
1408   "List of articles that have been replied to in the current newsgroup.")
1409
1410 (defvar gnus-newsgroup-expirable nil
1411   "List of articles in the current newsgroup that can be expired.")
1412
1413 (defvar gnus-newsgroup-processable nil
1414   "List of articles in the current newsgroup that can be processed.")
1415
1416 (defvar gnus-newsgroup-bookmarks nil
1417   "List of articles in the current newsgroup that have bookmarks.")
1418
1419 (defvar gnus-newsgroup-dormant nil
1420   "List of dormant articles in the current newsgroup.")
1421
1422 (defvar gnus-newsgroup-scored nil
1423   "List of scored articles in the current newsgroup.")
1424
1425 (defvar gnus-newsgroup-headers nil
1426   "List of article headers in the current newsgroup.")
1427 (defvar gnus-newsgroup-headers-hashtb-by-number nil)
1428
1429 (defvar gnus-newsgroup-ancient nil
1430   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1431
1432 (defvar gnus-current-article nil)
1433 (defvar gnus-article-current nil)
1434 (defvar gnus-current-headers nil)
1435 (defvar gnus-have-all-headers nil)
1436 (defvar gnus-last-article nil)
1437 (defvar gnus-newsgroup-history nil)
1438 (defvar gnus-current-kill-article nil)
1439
1440 ;; Save window configuration.
1441 (defvar gnus-prev-winconf nil)
1442
1443 ;; Format specs
1444 (defvar gnus-summary-line-format-spec nil)
1445 (defvar gnus-summary-dummy-line-format-spec nil)
1446 (defvar gnus-group-line-format-spec nil)
1447 (defvar gnus-summary-mode-line-format-spec nil)
1448 (defvar gnus-article-mode-line-format-spec nil)
1449 (defvar gnus-group-mode-line-format-spec nil)
1450 (defvar gnus-summary-mark-positions nil)
1451
1452 (defvar gnus-summary-expunge-below nil)
1453 (defvar gnus-reffed-article-number nil)
1454
1455 (defvar rmail-default-file (expand-file-name "~/XMBOX"))
1456 (defvar rmail-default-rmail-file (expand-file-name "~/XNEWS"))
1457
1458 (defconst gnus-summary-local-variables 
1459   '(gnus-newsgroup-name 
1460     gnus-newsgroup-begin gnus-newsgroup-end 
1461     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail 
1462     gnus-newsgroup-last-folder gnus-newsgroup-last-file 
1463     gnus-newsgroup-auto-expire gnus-newsgroup-unreads 
1464     gnus-newsgroup-unselected gnus-newsgroup-marked
1465     gnus-newsgroup-replied gnus-newsgroup-expirable
1466     gnus-newsgroup-processable gnus-newsgroup-killed
1467     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1468     gnus-newsgroup-headers gnus-newsgroup-headers-hashtb-by-number
1469     gnus-current-article gnus-current-headers gnus-have-all-headers
1470     gnus-last-article gnus-article-internal-prepare-hook
1471     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1472     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1473     gnus-newsgroup-threads gnus-newsgroup-async
1474     gnus-score-alist gnus-current-score-file gnus-summary-expunge-below 
1475     gnus-summary-mark-below gnus-newsgroup-active gnus-scores-exclude-files
1476     gnus-newsgroup-history gnus-newsgroup-ancient
1477     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring))
1478   "Variables that are buffer-local to the summary buffers.")
1479
1480 (defconst gnus-bug-message
1481   "Sending a bug report to the Gnus Towers.
1482 ========================================
1483
1484 The buffer below is a mail buffer.  When you press `C-c C-c', it will
1485 be sent to the Gnus Bug Exterminators. 
1486
1487 At the bottom of the buffer you'll see lots of variable settings.
1488 Please do not delete those.  They will tell the Bug People what your
1489 environment is, so that it will be easier to locate the bugs.
1490
1491 If you have found a bug that makes Emacs go \"beep\", set
1492 debug-on-error to t (`M-x set-variable RET debug-on-error RET t RET') 
1493 and include the backtrace in your bug report.
1494
1495 Please describe the bug in annoying, painstaking detail.
1496
1497 Thank you for your help in stamping out bugs.
1498 ")
1499
1500 ;;; End of variables.
1501
1502 ;; Define some autoload functions Gnus might use.
1503 (eval-and-compile
1504
1505   ;; Various 
1506   (autoload 'metamail-buffer "metamail")
1507   (autoload 'Info-goto-node "info")
1508   (autoload 'hexl-hex-string-to-integer "hexl")
1509   (autoload 'pp "pp")
1510   (autoload 'pp-to-string "pp")
1511   (autoload 'pp-eval-expression "pp")
1512   (autoload 'mail-extract-address-components "mail-extr")
1513
1514   (autoload 'nnmail-split-fancy "nnmail")
1515   (autoload 'nnvirtual-catchup-group "nnvirtual")
1516
1517   ;; timezone
1518   (autoload 'timezone-make-date-arpa-standard "timezone")
1519   (autoload 'timezone-fix-time "timezone")
1520   (autoload 'timezone-make-sortable-date "timezone")
1521   (autoload 'timezone-make-time-string "timezone")
1522
1523   ;; rmail & friends
1524   (autoload 'mail-position-on-field "sendmail")
1525   (autoload 'mail-setup "sendmail")
1526   (autoload 'rmail-output "rmailout")
1527   (autoload 'news-mail-other-window "rnewspost")
1528   (autoload 'news-reply-yank-original "rnewspost")
1529   (autoload 'news-caesar-buffer-body "rnewspost")
1530   (autoload 'rmail-insert-rmail-file-header "rmail")
1531   (autoload 'rmail-count-new-messages "rmail")
1532   (autoload 'rmail-show-message "rmail")
1533
1534   ;; gnus-soup
1535   (autoload 'gnus-group-brew-soup "gnus-soup" nil t)
1536   (autoload 'gnus-brew-soup "gnus-soup" nil t)
1537   (autoload 'gnus-soup-add-article "gnus-soup" nil t)
1538   (autoload 'gnus-soup-send-replies "gnus-soup" nil t)
1539   (autoload 'gnus-soup-save-areas "gnus-soup" nil t)
1540   (autoload 'gnus-soup-pack-packet "gnus-soup" nil t)
1541   (autoload 'nnsoup-pack-replies "nnsoup" nil t)
1542
1543   ;; gnus-mh
1544   (autoload 'gnus-mail-reply-using-mhe "gnus-mh")
1545   (autoload 'gnus-mail-forward-using-mhe "gnus-mh")
1546   (autoload 'gnus-mail-other-window-using-mhe "gnus-mh")
1547   (autoload 'gnus-summary-save-in-folder "gnus-mh")
1548   (autoload 'gnus-summary-save-article-folder "gnus-mh")
1549   (autoload 'gnus-Folder-save-name "gnus-mh")
1550   (autoload 'gnus-folder-save-name "gnus-mh")
1551
1552   ;; gnus-vis misc
1553   (autoload 'gnus-group-make-menu-bar "gnus-vis")
1554   (autoload 'gnus-summary-make-menu-bar "gnus-vis")
1555   (autoload 'gnus-server-make-menu-bar "gnus-vis")
1556   (autoload 'gnus-article-make-menu-bar "gnus-vis")
1557   (autoload 'gnus-browse-make-menu-bar "gnus-vis")
1558   (autoload 'gnus-highlight-selected-summary "gnus-vis")
1559   (autoload 'gnus-summary-highlight-line "gnus-vis")
1560   (autoload 'gnus-carpal-setup-buffer "gnus-vis")
1561
1562   ;; gnus-vis article
1563   (autoload 'gnus-article-push-button "gnus-vis" nil t)
1564   (autoload 'gnus-article-press-button "gnus-vis" nil t)
1565   (autoload 'gnus-article-highlight "gnus-vis" nil t)
1566   (autoload 'gnus-article-hide "gnus-vis" nil t)
1567   (autoload 'gnus-article-hide-signature "gnus-vis" nil t)
1568   (autoload 'gnus-article-highlight-headers "gnus-vis" nil t)
1569   (autoload 'gnus-article-highlight-signature "gnus-vis" nil t)
1570   (autoload 'gnus-article-add-buttons "gnus-vis" nil t)
1571   (autoload 'gnus-article-next-button "gnus-vis" nil t)
1572   (autoload 'gnus-article-add-button "gnus-vis")
1573
1574   ;; gnus-cite
1575   (autoload 'gnus-article-highlight-citation "gnus-cite" nil t)
1576   (autoload 'gnus-article-hide-citation-maybe "gnus-cite" nil t)
1577   (autoload 'gnus-article-hide-citation "gnus-cite" nil t)
1578
1579   ;; gnus-kill
1580   (autoload 'gnus-kill "gnus-kill")
1581   (autoload 'gnus-apply-kill-file-internal "gnus-kill")
1582   (autoload 'gnus-kill-file-edit-file "gnus-kill")
1583   (autoload 'gnus-kill-file-raise-followups-to-author "gnus-kill")
1584   (autoload 'gnus-execute "gnus-kill")
1585   (autoload 'gnus-expunge "gnus-kill")
1586
1587   ;; gnus-cache
1588   (autoload 'gnus-cache-possibly-enter-article "gnus-cache")
1589   (autoload 'gnus-cache-save-buffers "gnus-cache")
1590   (autoload 'gnus-cache-possibly-remove-article "gnus-cache")
1591   (autoload 'gnus-cache-request-article "gnus-cache")
1592   (autoload 'gnus-cache-retrieve-headers "gnus-cache")
1593   (autoload 'gnus-cache-possibly-alter-active "gnus-cache")
1594   (autoload 'gnus-jog-cache "gnus-cache" nil t)
1595
1596   ;; gnus-score
1597   (autoload 'gnus-summary-increase-score "gnus-score" nil t)
1598   (autoload 'gnus-summary-lower-score "gnus-score" nil t)
1599   (autoload 'gnus-summary-score-map "gnus-score" nil nil 'keymap)
1600   (autoload 'gnus-score-save "gnus-score")
1601   (autoload 'gnus-score-headers "gnus-score")
1602   (autoload 'gnus-current-score-file-nondirectory "gnus-score")
1603   (autoload 'gnus-score-adaptive "gnus-score")
1604   (autoload 'gnus-score-remove-lines-adaptive "gnus-score")
1605   (autoload 'gnus-score-find-trace "gnus-score")
1606
1607   ;; gnus-edit
1608   (autoload 'gnus-score-customize "gnus-edit" nil t)
1609
1610   ;; gnus-uu
1611   (autoload 'gnus-uu-extract-map "gnus-uu" nil nil 'keymap)
1612   (autoload 'gnus-uu-mark-map "gnus-uu" nil nil 'keymap)
1613   (autoload 'gnus-uu-digest-mail-forward "gnus-uu" nil t)
1614   (autoload 'gnus-uu-digest-post-forward "gnus-uu" nil t)
1615   (autoload 'gnus-uu-mark-series "gnus-uu" nil t)
1616   (autoload 'gnus-uu-mark-region "gnus-uu" nil t)
1617   (autoload 'gnus-uu-mark-by-regexp "gnus-uu" nil t)
1618   (autoload 'gnus-uu-mark-all "gnus-uu" nil t)
1619   (autoload 'gnus-uu-mark-sparse "gnus-uu" nil t)
1620   (autoload 'gnus-uu-mark-thread "gnus-uu" nil t)
1621   (autoload 'gnus-uu-decode-uu "gnus-uu" nil t)
1622   (autoload 'gnus-uu-decode-uu-and-save "gnus-uu" nil t)
1623   (autoload 'gnus-uu-decode-unshar "gnus-uu" nil t)
1624   (autoload 'gnus-uu-decode-unshar-and-save "gnus-uu" nil t)
1625   (autoload 'gnus-uu-decode-save "gnus-uu" nil t)
1626   (autoload 'gnus-uu-decode-binhex "gnus-uu" nil t)
1627   (autoload 'gnus-uu-decode-uu-view "gnus-uu" nil t)
1628   (autoload 'gnus-uu-decode-uu-and-save-view "gnus-uu" nil t)
1629   (autoload 'gnus-uu-decode-unshar-view "gnus-uu" nil t)
1630   (autoload 'gnus-uu-decode-unshar-and-save-view "gnus-uu" nil t)
1631   (autoload 'gnus-uu-decode-save-view "gnus-uu" nil t)
1632   (autoload 'gnus-uu-decode-binhex-view "gnus-uu" nil t)
1633
1634   ;; gnus-msg
1635   (autoload 'gnus-summary-send-map "gnus-msg" nil nil 'keymap)
1636   (autoload 'gnus-group-post-news "gnus-msg" nil t)
1637   (autoload 'gnus-group-mail "gnus-msg" nil t)
1638   (autoload 'gnus-summary-post-news "gnus-msg" nil t)
1639   (autoload 'gnus-summary-followup "gnus-msg" nil t)
1640   (autoload 'gnus-summary-followup-with-original "gnus-msg" nil t)
1641   (autoload 'gnus-summary-followup-and-reply "gnus-msg" nil t)
1642   (autoload 'gnus-summary-followup-and-reply-with-original "gnus-msg" nil t)
1643   (autoload 'gnus-summary-cancel-article "gnus-msg" nil t)
1644   (autoload 'gnus-summary-supersede-article "gnus-msg" nil t)
1645   (autoload 'gnus-post-news "gnus-msg" nil t)
1646   (autoload 'gnus-inews-news "gnus-msg" nil t)
1647   (autoload 'gnus-cancel-news "gnus-msg" nil t)
1648   (autoload 'gnus-summary-reply "gnus-msg" nil t)
1649   (autoload 'gnus-summary-reply-with-original "gnus-msg" nil t)
1650   (autoload 'gnus-summary-mail-forward "gnus-msg" nil t)
1651   (autoload 'gnus-summary-mail-other-window "gnus-msg" nil t)
1652   (autoload 'gnus-mail-reply-using-mail "gnus-msg")
1653   (autoload 'gnus-mail-yank-original "gnus-msg")
1654   (autoload 'gnus-mail-send-and-exit "gnus-msg")
1655   (autoload 'gnus-mail-forward-using-mail "gnus-msg")
1656   (autoload 'gnus-mail-other-window-using-mail "gnus-msg")
1657   (autoload 'gnus-article-mail-with-original "gnus-msg")
1658   (autoload 'gnus-article-mail "gnus-msg")
1659
1660   ;; gnus-vm
1661   (autoload 'gnus-summary-save-in-vm "gnus-vm" nil t)
1662   (autoload 'gnus-summary-save-article-vm "gnus-vm" nil t)
1663   (autoload 'gnus-mail-forward-using-vm "gnus-vm")
1664   (autoload 'gnus-mail-reply-using-vm "gnus-vm")
1665   (autoload 'gnus-mail-other-window-using-vm "gnus-vm" nil t)
1666   (autoload 'gnus-yank-article "gnus-vm" nil t)
1667
1668   )
1669
1670 \f
1671
1672 ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
1673 ;; If you want the cursor to go somewhere else, set these two
1674 ;; functions in some startup hook to whatever you want.
1675 (defalias 'gnus-summary-position-cursor 'gnus-goto-colon)
1676 (defalias 'gnus-group-position-cursor 'gnus-goto-colon)
1677
1678 ;;; Various macros and substs.
1679
1680 (defmacro gnus-eval-in-buffer-window (buffer &rest forms)
1681   "Pop to BUFFER, evaluate FORMS, and then returns to original window."
1682   (` (let ((GnusStartBufferWindow (selected-window)))
1683        (unwind-protect
1684            (progn
1685              (pop-to-buffer (, buffer))
1686              (,@ forms))
1687          (select-window GnusStartBufferWindow)))))
1688
1689 (defmacro gnus-gethash (string hashtable)
1690   "Get hash value of STRING in HASHTABLE."
1691   ;;(` (symbol-value (abbrev-symbol (, string) (, hashtable))))
1692   ;;(` (abbrev-expansion (, string) (, hashtable)))
1693   (` (symbol-value (intern-soft (, string) (, hashtable)))))
1694
1695 (defmacro gnus-sethash (string value hashtable)
1696   "Set hash value. Arguments are STRING, VALUE, and HASHTABLE."
1697   ;; We cannot use define-abbrev since it only accepts string as value.
1698   ;; (set (intern string hashtable) value))
1699   (` (set (intern (, string) (, hashtable)) (, value))))
1700
1701 (defsubst gnus-buffer-substring (beg end)
1702   (buffer-substring (match-beginning beg) (match-end end)))
1703
1704 (defsubst gnus-simplify-subject-re (subject)
1705   "Remove \"Re:\" from subject lines."
1706   (let ((case-fold-search t))
1707     (if (string-match "^re: *" subject)
1708         (substring subject (match-end 0))
1709       subject)))
1710
1711 (defsubst gnus-goto-char (point)
1712   (and point (goto-char point)))
1713
1714 (defmacro gnus-buffer-exists-p (buffer)
1715   (` (and (, buffer)
1716           (funcall (if (stringp (, buffer)) 'get-buffer 'buffer-name)
1717                    (, buffer)))))
1718
1719 (defmacro gnus-kill-buffer (buffer)
1720   (` (if (gnus-buffer-exists-p (, buffer))
1721          (kill-buffer (, buffer)))))
1722
1723 (defsubst gnus-point-at-bol ()
1724   "Return point at the beginning of line."
1725   (let ((p (point)))
1726     (beginning-of-line)
1727     (prog1
1728         (point)
1729       (goto-char p))))
1730
1731 (defsubst gnus-point-at-eol ()
1732   "Return point at the beginning of line."
1733   (let ((p (point)))
1734     (end-of-line)
1735     (prog1
1736         (point)
1737       (goto-char p))))
1738
1739 ;; Delete the current line (and the next N lines.);
1740 (defmacro gnus-delete-line (&optional n)
1741   (` (delete-region (progn (beginning-of-line) (point))
1742                     (progn (forward-line (, (or n 1))) (point)))))
1743
1744 ;;; Load the compatability functions. 
1745
1746 (require 'gnus-ems)
1747
1748 \f
1749 ;;;
1750 ;;; Gnus Utility Functions
1751 ;;;
1752
1753 (defun gnus-extract-address-components (from)
1754   (let (name address)
1755     ;; First find the address - the thing with the @ in it.  This may
1756     ;; not be accurate in mail addresses, but does the trick most of
1757     ;; the time in news messages.
1758     (if (string-match "\\b[^@ \t<>]+[!@][^@ \t<>]+\\b" from)
1759         (setq address (substring from (match-beginning 0) (match-end 0))))
1760     ;; Then we check whether the "name <address>" format is used.
1761     (and address
1762          (string-match (concat "<" (regexp-quote address) ">") from)
1763          (and (setq name (substring from 0 (1- (match-beginning 0))))
1764               ;; Strip any quotes from the name.
1765               (string-match "\".*\"" name)
1766               (setq name (substring name 1 (1- (match-end 0))))))
1767     ;; If not, then "address (name)" is used.
1768     (or name
1769         (and (string-match "(.+)" from)
1770              (setq name (substring from (1+ (match-beginning 0)) 
1771                                    (1- (match-end 0)))))
1772         (and (string-match "()" from)
1773              (setq name address))
1774         ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>.
1775         ;; XOVER might not support folded From headers.
1776         (and (string-match "(.*" from)
1777              (setq name (substring from (1+ (match-beginning 0)) 
1778                                    (match-end 0)))))
1779     ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
1780     (list (or name from) (or address from))))
1781
1782 (defun gnus-fetch-field (field)
1783   "Return the value of the header FIELD of current article."
1784   (save-excursion
1785     (save-restriction
1786       (let ((case-fold-search t))
1787         (gnus-narrow-to-headers)
1788         (mail-fetch-field field)))))
1789
1790 (defun gnus-goto-colon ()
1791   (beginning-of-line)
1792   (search-forward ":" (gnus-point-at-eol) t))
1793
1794 (defun gnus-narrow-to-headers ()
1795   (widen)
1796   (save-excursion
1797     (narrow-to-region
1798      (goto-char (point-min))
1799      (if (search-forward "\n\n" nil t)
1800          (1- (point))
1801        (point-max)))))
1802
1803 (defun gnus-update-format-specifications ()
1804   (gnus-make-thread-indent-array)
1805   (setq gnus-summary-line-format-spec 
1806         (gnus-parse-format
1807          gnus-summary-line-format gnus-summary-line-format-alist))
1808   (gnus-update-summary-mark-positions)
1809   (setq gnus-summary-dummy-line-format-spec 
1810         (gnus-parse-format gnus-summary-dummy-line-format 
1811                            gnus-summary-dummy-line-format-alist))
1812   (setq gnus-group-line-format-spec
1813         (gnus-parse-format 
1814          gnus-group-line-format 
1815          gnus-group-line-format-alist))
1816   (if (and (string-match "%D" gnus-group-line-format)
1817            (not gnus-description-hashtb)
1818            gnus-read-active-file)
1819       (gnus-read-all-descriptions-files))
1820   (setq gnus-summary-mode-line-format-spec 
1821         (gnus-parse-format gnus-summary-mode-line-format 
1822                            gnus-summary-mode-line-format-alist))
1823   (setq gnus-article-mode-line-format-spec 
1824         (gnus-parse-format gnus-article-mode-line-format 
1825                            gnus-summary-mode-line-format-alist))
1826   (setq gnus-group-mode-line-format-spec 
1827         (gnus-parse-format gnus-group-mode-line-format 
1828                            gnus-group-mode-line-format-alist)))
1829
1830 (defun gnus-update-summary-mark-positions ()
1831   (save-excursion
1832     (let ((gnus-replied-mark 129)
1833           (gnus-score-below-mark 130)
1834           (gnus-score-over-mark 130)
1835           (thread nil)
1836           pos)
1837       (gnus-set-work-buffer)
1838       (gnus-summary-insert-line 
1839        nil [0 "" "" "" "" "" 0 0 ""]  0 nil 128 t nil "" nil 1)
1840       (goto-char (point-min))
1841       (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
1842                                          (- (point) 2)))))
1843       (goto-char (point-min))
1844       (setq pos (cons (cons 'replied (and (search-forward "\201" nil t)
1845                                           (- (point) 2))) pos))
1846       (goto-char (point-min))
1847       (setq pos (cons (cons 'score (and (search-forward "\202" nil t)
1848                                         (- (point) 2))) pos))
1849       (setq gnus-summary-mark-positions pos))))
1850
1851 (defun gnus-format-max-width (form length)
1852   (let* ((val (eval form))
1853          (valstr (if (numberp val) (int-to-string val) val)))
1854     (if (> (length valstr) length)
1855         (substring valstr 0 length)
1856       valstr)))
1857
1858 (defun gnus-set-mouse-face (string)
1859   ;; Set mouse face property on STRING.
1860   (put-text-property 0 (length string) 'mouse-face gnus-mouse-face string)
1861   string)
1862
1863 (defun gnus-parse-format (format spec-alist)
1864   ;; This function parses the FORMAT string with the help of the
1865   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
1866   ;; string.  If the FORMAT string contains the specifiers %( and %)
1867   ;; the text between them will have the mouse-face text property.
1868   (if (string-match "\\`\\(.*\\)%(\\(.*\\)%)\\(.*\n?\\)\\'" format)
1869       (if (and gnus-visual gnus-mouse-face)
1870           (let ((pre (substring format (match-beginning 1) (match-end 1)))
1871                 (button (substring format (match-beginning 2) (match-end 2)))
1872                 (post (substring format (match-beginning 3) (match-end 3))))
1873             (list 'concat
1874                   (gnus-parse-simple-format pre spec-alist)
1875                   (list 'gnus-set-mouse-face
1876                         (gnus-parse-simple-format button spec-alist))
1877                   (gnus-parse-simple-format post spec-alist)))
1878         (gnus-parse-simple-format
1879          (concat (substring format (match-beginning 1) (match-end 1))
1880                  (substring format (match-beginning 2) (match-end 2))
1881                  (substring format (match-beginning 3) (match-end 3)))
1882          spec-alist))
1883     (gnus-parse-simple-format format spec-alist)))
1884
1885 (defun gnus-parse-simple-format (format spec-alist)
1886   ;; This function parses the FORMAT string with the help of the
1887   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
1888   ;; string. The list will consist of the symbol `format', a format
1889   ;; specification string, and a list of forms depending on the
1890   ;; SPEC-ALIST.
1891   (let ((max-width 0)
1892         spec flist fstring b newspec max-width elem beg)
1893     (save-excursion
1894       (gnus-set-work-buffer)
1895       (insert format)
1896       (goto-char (point-min))
1897       (while (re-search-forward "%[-0-9]*\\(,[0-9]*\\)*\\(.\\)\\(.\\)?" nil t)
1898         (setq spec (string-to-char (buffer-substring (match-beginning 2)
1899                                                      (match-end 2))))
1900         ;; First check if there are any specs that look anything like
1901         ;; "%12,12A", ie. with a "max width specification". These have
1902         ;; to be treated specially.
1903         (if (setq beg (match-beginning 1))
1904             (setq max-width 
1905                   (string-to-int 
1906                    (buffer-substring (1+ (match-beginning 1)) (match-end 1))))
1907           (setq max-width 0)
1908           (setq beg (match-beginning 2)))
1909         ;; Find the specification from `spec-alist'.
1910         (if (not (setq elem (cdr (assq spec spec-alist))))
1911             (setq elem '("*" ?s)))
1912         ;; Treat user defined format specifiers specially
1913         (and (eq (car elem) 'user-defined)
1914              (setq elem
1915                    (list 
1916                     (list (intern (concat "gnus-user-format-function-"
1917                                           (buffer-substring
1918                                            (match-beginning 3)
1919                                            (match-end 3))))
1920                           'header)
1921                     ?s))
1922              (delete-region (match-beginning 3) (match-end 3)))
1923         (if (not (zerop max-width))
1924             (let ((el (car elem)))
1925               (cond ((= (car (cdr elem)) ?c) 
1926                      (setq el (list 'char-to-string el)))
1927                     ((= (car (cdr elem)) ?d)
1928                      (numberp el) (setq el (list 'int-to-string el))))
1929               (setq flist (cons (list 'gnus-format-max-width el max-width) 
1930                                 flist))
1931               (setq newspec ?s))
1932           (setq flist (cons (car elem) flist))
1933           (setq newspec (car (cdr elem))))
1934         ;; Remove the old specification (and possibly a ",12" string).
1935         (delete-region beg (match-end 2))
1936         ;; Insert the new specification.
1937         (goto-char beg)
1938         (insert newspec))
1939       (setq fstring (buffer-substring 1 (point-max))))
1940     (cons 'format (cons fstring (nreverse flist)))))
1941
1942 ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
1943 (defun gnus-read-init-file ()
1944   (and gnus-init-file
1945        (or (and (file-exists-p gnus-init-file) 
1946                 ;; Don't try to load a directory.
1947                 (not (file-directory-p gnus-init-file)))
1948            (file-exists-p (concat gnus-init-file ".el"))
1949            (file-exists-p (concat gnus-init-file ".elc")))
1950        (load gnus-init-file nil t)))
1951
1952 (defun gnus-set-work-buffer ()
1953   (if (get-buffer gnus-work-buffer)
1954       (progn
1955         (set-buffer gnus-work-buffer)
1956         (erase-buffer))
1957     (set-buffer (get-buffer-create gnus-work-buffer))
1958     (kill-all-local-variables)
1959     (buffer-disable-undo (current-buffer))
1960     (gnus-add-current-to-buffer-list)))
1961
1962 ;; Article file names when saving.
1963
1964 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
1965   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1966 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group/num.
1967 Otherwise, it is like ~/News/news/group/num."
1968   (let ((default
1969           (expand-file-name
1970            (concat (if (gnus-use-long-file-name 'not-save)
1971                        (gnus-capitalize-newsgroup newsgroup)
1972                      (gnus-newsgroup-directory-form newsgroup))
1973                    "/" (int-to-string (header-number headers)))
1974            (or gnus-article-save-directory "~/News"))))
1975     (if (and last-file
1976              (string-equal (file-name-directory default)
1977                            (file-name-directory last-file))
1978              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
1979         default
1980       (or last-file default))))
1981
1982 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
1983   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1984 If variable `gnus-use-long-file-name' is nil, it is ~/News/news.group/num.
1985 Otherwise, it is like ~/News/news/group/num."
1986   (let ((default
1987           (expand-file-name
1988            (concat (if (gnus-use-long-file-name 'not-save)
1989                        newsgroup
1990                      (gnus-newsgroup-directory-form newsgroup))
1991                    "/" (int-to-string (header-number headers)))
1992            (or gnus-article-save-directory "~/News"))))
1993     (if (and last-file
1994              (string-equal (file-name-directory default)
1995                            (file-name-directory last-file))
1996              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
1997         default
1998       (or last-file default))))
1999
2000 (defun gnus-Plain-save-name (newsgroup headers &optional last-file)
2001   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2002 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group.
2003 Otherwise, it is like ~/News/news/group/news."
2004   (or last-file
2005       (expand-file-name
2006        (if (gnus-use-long-file-name 'not-save)
2007            (gnus-capitalize-newsgroup newsgroup)
2008          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2009        (or gnus-article-save-directory "~/News"))))
2010
2011 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
2012   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2013 If variable `gnus-use-long-file-name' is nil, it is ~/News/news.group.
2014 Otherwise, it is like ~/News/news/group/news."
2015   (or last-file
2016       (expand-file-name
2017        (if (gnus-use-long-file-name 'not-save)
2018            newsgroup
2019          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2020        (or gnus-article-save-directory "~/News"))))
2021
2022 ;; For subscribing new newsgroup
2023
2024 (defun gnus-subscribe-hierarchical-interactive (groups)
2025   (let ((groups (sort groups 'string<))
2026         prefixes prefix start rest ans group starts)
2027     (while groups
2028       (setq prefixes (list "^"))
2029       (while (and groups prefixes)
2030         (while (not (string-match (car prefixes) (car groups)))
2031           (setq prefixes (cdr prefixes)))
2032         (setq prefix (car prefixes))
2033         (setq start (1- (length prefix)))
2034         (if (and (string-match "[^\\.]\\." (car groups) start)
2035                  (cdr groups)
2036                  (setq prefix 
2037                        (concat "^" (substring (car groups) 0 (match-end 0))))
2038                  (string-match prefix (car (cdr groups))))
2039             (progn
2040               (setq prefixes (cons prefix prefixes))
2041               (message "Descend hierarchy %s? ([y]nsq): " 
2042                        (substring prefix 1 (1- (length prefix))))
2043               (setq ans (read-char))
2044               (cond ((= ans ?n)
2045                      (while (and groups 
2046                                  (string-match prefix 
2047                                                (setq group (car groups))))
2048                        (setq gnus-killed-list 
2049                              (cons group gnus-killed-list))
2050                        (gnus-sethash group group gnus-killed-hashtb)
2051                        (setq groups (cdr groups)))
2052                      (setq starts (cdr starts)))
2053                     ((= ans ?s)
2054                      (while (and groups 
2055                                  (string-match prefix 
2056                                                (setq group (car groups))))
2057                        (gnus-sethash group group gnus-killed-hashtb)
2058                        (gnus-subscribe-alphabetically (car groups))
2059                        (setq groups (cdr groups)))
2060                      (setq starts (cdr starts)))
2061                     ((= ans ?q)
2062                      (while groups
2063                        (setq group (car groups))
2064                        (setq gnus-killed-list (cons group gnus-killed-list))
2065                        (gnus-sethash group group gnus-killed-hashtb)
2066                        (setq groups (cdr groups))))
2067                     (t nil)))
2068           (message "Subscribe %s? ([n]yq)" (car groups))
2069           (setq ans (read-char))
2070           (setq group (car groups))
2071           (cond ((= ans ?y)
2072                  (gnus-subscribe-alphabetically (car groups))
2073                  (gnus-sethash group group gnus-killed-hashtb))
2074                 ((= ans ?q)
2075                  (while groups
2076                    (setq group (car groups))
2077                    (setq gnus-killed-list (cons group gnus-killed-list))
2078                    (gnus-sethash group group gnus-killed-hashtb)
2079                    (setq groups (cdr groups))))
2080                 (t 
2081                  (setq gnus-killed-list (cons group gnus-killed-list))
2082                  (gnus-sethash group group gnus-killed-hashtb)))
2083           (setq groups (cdr groups)))))))
2084
2085 (defun gnus-subscribe-randomly (newsgroup)
2086   "Subscribe new NEWSGROUP by making it the first newsgroup."
2087   (gnus-subscribe-newsgroup newsgroup))
2088
2089 (defun gnus-subscribe-alphabetically (newgroup)
2090   "Subscribe new NEWSGROUP and insert it in alphabetical order."
2091   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2092   (let ((groups (cdr gnus-newsrc-alist))
2093         before)
2094     (while (and (not before) groups)
2095       (if (string< newgroup (car (car groups)))
2096           (setq before (car (car groups)))
2097         (setq groups (cdr groups))))
2098     (gnus-subscribe-newsgroup newgroup before)))
2099
2100 (defun gnus-subscribe-hierarchically (newgroup)
2101   "Subscribe new NEWSGROUP and insert it in hierarchical newsgroup order."
2102   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2103   (save-excursion
2104     (set-buffer (find-file-noselect gnus-current-startup-file))
2105     (let ((groupkey newgroup)
2106           before)
2107       (while (and (not before) groupkey)
2108         (goto-char (point-min))
2109         (let ((groupkey-re
2110                (concat "^\\(" (regexp-quote groupkey) ".*\\)[!:]")))
2111           (while (and (re-search-forward groupkey-re nil t)
2112                       (progn
2113                         (setq before (buffer-substring
2114                                       (match-beginning 1) (match-end 1)))
2115                         (string< before newgroup)))))
2116         ;; Remove tail of newsgroup name (eg. a.b.c -> a.b)
2117         (setq groupkey
2118               (if (string-match "^\\(.*\\)\\.[^.]+$" groupkey)
2119                   (substring groupkey (match-beginning 1) (match-end 1)))))
2120       (gnus-subscribe-newsgroup newgroup before))))
2121
2122 (defun gnus-subscribe-interactively (newsgroup)
2123   "Subscribe new NEWSGROUP interactively.
2124 It is inserted in hierarchical newsgroup order if subscribed. If not,
2125 it is killed."
2126   (if (gnus-y-or-n-p (format "Subscribe new newsgroup: %s " newsgroup))
2127       (gnus-subscribe-hierarchically newsgroup)
2128     (setq gnus-killed-list (cons newsgroup gnus-killed-list))))
2129
2130 (defun gnus-subscribe-zombies (newsgroup)
2131   "Make new NEWSGROUP a zombie group."
2132   (setq gnus-zombie-list (cons newsgroup gnus-zombie-list)))
2133
2134 (defun gnus-subscribe-newsgroup (newsgroup &optional next)
2135   "Subscribe new NEWSGROUP.
2136 If NEXT is non-nil, it is inserted before NEXT. Otherwise it is made
2137 the first newsgroup."
2138   ;; We subscribe the group by changing its level to `subscribed'.
2139   (gnus-group-change-level 
2140    newsgroup gnus-level-default-subscribed
2141    gnus-level-killed (gnus-gethash (or next "dummy.group") gnus-newsrc-hashtb))
2142   (gnus-message 5 "Subscribe newsgroup: %s" newsgroup))
2143
2144 ;; For directories
2145
2146 (defun gnus-newsgroup-directory-form (newsgroup)
2147   "Make hierarchical directory name from NEWSGROUP name."
2148   (let ((newsgroup (substring newsgroup 0)) ;Copy string.
2149         (len (length newsgroup))
2150         idx)
2151     ;; If this is a foreign group, we don't want to translate the
2152     ;; entire name.  
2153     (if (setq idx (string-match ":" newsgroup))
2154         (aset newsgroup idx ?/)
2155       (setq idx 0))
2156     ;; Replace all occurrences of `.' with `/'.
2157     (while (< idx len)
2158       (if (= (aref newsgroup idx) ?.)
2159           (aset newsgroup idx ?/))
2160       (setq idx (1+ idx)))
2161     newsgroup))
2162
2163 (defun gnus-make-directory (dir)
2164   "Make DIRECTORY recursively."
2165   (let* ((dir (expand-file-name dir default-directory))
2166          dirs)
2167     (if (string-match "/$" dir)
2168         (setq dir (substring dir 0 (match-beginning 0))))
2169     (while (not (file-exists-p dir))
2170       (setq dirs (cons dir dirs))
2171       (string-match "/[^/]+$" dir)
2172       (setq dir (substring dir 0 (match-beginning 0))))
2173     (while dirs
2174       (make-directory (car dirs))
2175       (setq dirs (cdr dirs)))))
2176
2177 (defun gnus-capitalize-newsgroup (newsgroup)
2178   "Capitalize NEWSGROUP name."
2179   (and (not (zerop (length newsgroup)))
2180        (concat (char-to-string (upcase (aref newsgroup 0)))
2181                (substring newsgroup 1))))
2182
2183 ;; Var
2184
2185 (defun gnus-simplify-subject (subject &optional re-only)
2186   "Remove `Re:' and words in parentheses.
2187 If optional argument RE-ONLY is non-nil, strip `Re:' only."
2188   (let ((case-fold-search t))           ;Ignore case.
2189     ;; Remove `Re:' and `Re^N:'.
2190     (if (string-match "^re:[ \t]*" subject)
2191         (setq subject (substring subject (match-end 0))))
2192     ;; Remove words in parentheses from end.
2193     (or re-only
2194         (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
2195           (setq subject (substring subject 0 (match-beginning 0)))))
2196     ;; Return subject string.
2197     subject))
2198
2199 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
2200 ;; all whitespace.
2201 (defun gnus-simplify-subject-fuzzy (subject)
2202   (let ((case-fold-search t))
2203     (save-excursion
2204       (gnus-set-work-buffer)
2205       (insert subject)
2206       (inline (gnus-simplify-buffer-fuzzy))
2207       (buffer-string))))
2208
2209 (defun gnus-simplify-buffer-fuzzy ()
2210   (goto-char (point-min))
2211   ;; Fix by Stainless Steel Rat <ratinox@ccs.neu.edu>.
2212   (while (re-search-forward "^[ \t]*\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;][ \t]*"
2213                             nil t)
2214     (replace-match "" t t))
2215   (goto-char (point-min))
2216   (while (re-search-forward "[ \t\n]*([^()]*)[ \t\n]*$" nil t)
2217     (replace-match "" t t))
2218   (goto-char (point-min))
2219   (while (re-search-forward "[ \t]+" nil t)
2220     (replace-match " " t t))
2221   (goto-char (point-min))
2222   (while (re-search-forward "[ \t]+$" nil t)
2223     (replace-match "" t t))
2224   (goto-char (point-min))
2225   (while (re-search-forward "^[ \t]+" nil t)
2226     (replace-match "" t t))
2227   (if gnus-simplify-subject-fuzzy-regexp
2228       (while (re-search-forward gnus-simplify-subject-fuzzy-regexp nil t)
2229         (replace-match "" t t)))
2230     )
2231
2232 ;; Add the current buffer to the list of buffers to be killed on exit. 
2233 (defun gnus-add-current-to-buffer-list ()
2234   (or (memq (current-buffer) gnus-buffer-list)
2235       (setq gnus-buffer-list (cons (current-buffer) gnus-buffer-list))))
2236
2237 (defun gnus-string> (s1 s2)
2238   (not (or (string< s1 s2)
2239            (string= s1 s2))))
2240
2241 ;; Functions accessing headers.
2242 ;; Functions are more convenient than macros in some cases.
2243
2244 (defun gnus-header-number (header)
2245   (header-number header))
2246
2247 (defun gnus-header-subject (header)
2248   (header-subject header))
2249
2250 (defun gnus-header-from (header)
2251   (header-from header))
2252
2253 (defun gnus-header-xref (header)
2254   (header-xref header))
2255
2256 (defun gnus-header-lines (header)
2257   (header-lines header))
2258
2259 (defun gnus-header-date (header)
2260   (header-date header))
2261
2262 (defun gnus-header-id (header)
2263   (header-id header))
2264
2265 (defun gnus-header-references (header)
2266   (header-references header))
2267
2268 ;;; General various misc type functions.
2269
2270 (defun gnus-clear-system ()
2271   "Clear all variables and buffers."
2272   ;; Clear Gnus variables.
2273   (let ((variables gnus-variable-list))
2274     (while variables
2275       (set (car variables) nil)
2276       (setq variables (cdr variables))))
2277   ;; Clear other internal variables.
2278   (setq gnus-list-of-killed-groups nil
2279         gnus-have-read-active-file nil
2280         gnus-newsrc-alist nil
2281         gnus-newsrc-hashtb nil
2282         gnus-killed-list nil
2283         gnus-zombie-list nil
2284         gnus-killed-hashtb nil
2285         gnus-active-hashtb nil
2286         gnus-moderated-list nil
2287         gnus-description-hashtb nil
2288         gnus-newsgroup-headers nil
2289         gnus-newsgroup-headers-hashtb-by-number nil
2290         gnus-newsgroup-name nil
2291         gnus-server-alist nil
2292         gnus-current-select-method nil)
2293   ;; Reset any score variables.
2294   (and (boundp 'gnus-score-cache)
2295        (set 'gnus-score-cache nil))
2296   (and (boundp 'gnus-internal-global-score-files)
2297        (set 'gnus-internal-global-score-files nil))
2298   ;; Kill the startup file.
2299   (and gnus-current-startup-file
2300        (get-file-buffer gnus-current-startup-file)
2301        (kill-buffer (get-file-buffer gnus-current-startup-file)))
2302   ;; Save any cache buffers.
2303   (and gnus-use-cache (gnus-cache-save-buffers))
2304   ;; Clear the dribble buffer.
2305   (gnus-dribble-clear)
2306   ;; Kill global KILL file buffer.
2307   (if (get-file-buffer (gnus-newsgroup-kill-file nil))
2308       (kill-buffer (get-file-buffer (gnus-newsgroup-kill-file nil))))
2309   (gnus-kill-buffer nntp-server-buffer)
2310   ;; Kill Gnus buffers.
2311   (while gnus-buffer-list
2312     (gnus-kill-buffer (car gnus-buffer-list))
2313     (setq gnus-buffer-list (cdr gnus-buffer-list))))
2314
2315 (defun gnus-windows-old-to-new (setting)
2316   (if (symbolp setting)
2317       (setq setting 
2318             (cond ((eq setting 'SelectArticle)
2319                    'article)
2320                   ((eq setting 'SelectSubject)
2321                    'summary)
2322                   ((eq setting 'SelectNewsgroup)
2323                    'group)
2324                   (t setting))))
2325   (if (or (listp setting)
2326           (not (and gnus-window-configuration
2327                     (memq setting '(group summary article)))))
2328       setting
2329     (let* ((setting (if (eq setting 'group) 
2330                         (if (assq 'newsgroup gnus-window-configuration)
2331                             'newsgroup
2332                           'newsgroups) setting))
2333            (elem (car (cdr (assq setting gnus-window-configuration))))
2334            (total (apply '+ elem))
2335            (types '(group summary article))
2336            (pbuf (if (eq setting 'newsgroups) 'group 'summary))
2337            (i 0)
2338            perc
2339            out)
2340       (while (< i 3)
2341         (or (zerop (nth i elem))
2342             (progn
2343               (setq perc  (/ (* 1.0 (nth 0 elem)) total))
2344               (setq out (cons (if (eq pbuf (nth i types))
2345                                   (vector (nth i types) perc 'point)
2346                                 (vector (nth i types) perc))
2347                               out))))
2348         (setq i (1+ i)))
2349       (list (nreverse out)))))
2350            
2351 (defun gnus-add-configuration (conf)
2352   (setq gnus-buffer-configuration 
2353         (cons conf (delq (assq (car conf) gnus-buffer-configuration)
2354                          gnus-buffer-configuration))))
2355
2356 (defun gnus-configure-windows (setting)
2357   (setq setting (gnus-windows-old-to-new setting))
2358   (let ((r (if (symbolp setting)
2359                   (cdr (assq setting gnus-buffer-configuration))
2360                 setting))
2361         (in-buf (current-buffer))
2362         rule val window w height hor ohor heights sub jump-buffer
2363         rel total to-buf)
2364     (or r (error "No such setting: %s" setting))
2365
2366     ;; Either remove all windows or just remove all Gnus windows.
2367     (if gnus-use-full-window
2368         (delete-other-windows)
2369       (gnus-remove-some-windows)
2370       (switch-to-buffer nntp-server-buffer))
2371
2372     (while r
2373       (setq hor (car r)
2374             ohor nil)
2375
2376       ;; We have to do the (possible) horizontal splitting before the
2377       ;; vertical. 
2378       (if (and (listp (car hor)) 
2379                (eq (car (car hor)) 'horizontal))
2380           (progn
2381             (split-window 
2382              nil
2383              (if (integerp (nth 1 (car hor)))
2384                  (nth 1 (car hor))
2385                (- (frame-width) (floor (* (frame-width) (nth 1 (car hor))))))
2386              t)
2387             (setq hor (cdr hor))))
2388
2389       ;; Go through the rules and eval the elements that are to be
2390       ;; evaled.  
2391       (while hor
2392         (if (setq val (if (vectorp (car hor)) (car hor) (eval (car hor))))
2393             (progn
2394               ;; Expand short buffer name.
2395               (setq w (aref val 0))
2396               (and (setq w (cdr (assq w gnus-window-to-buffer)))
2397                    (progn
2398                      (setq val (apply 'vector (mapcar (lambda (v) v) val)))
2399                      (aset val 0 w)))
2400               (setq ohor (cons val ohor))))
2401         (setq hor (cdr hor)))
2402       (setq rule (cons (nreverse ohor) rule))
2403       (setq r (cdr r)))
2404     (setq rule (nreverse rule))
2405
2406     ;; We tally the window sizes.
2407     (setq total (window-height))
2408     (while rule
2409       (setq hor (car rule))
2410       (if (and (listp (car hor)) (eq (car (car hor)) 'horizontal))
2411           (setq hor (cdr hor)))
2412       (setq sub 0)
2413       (while hor
2414         (setq rel (aref (car hor) 1)
2415               heights (cons
2416                        (cond ((and (floatp rel) (= 1.0 rel))
2417                               'x)
2418                              ((integerp rel)
2419                               rel)
2420                              (t
2421                               (max (floor (* total rel)) 4)))
2422                        heights)
2423               sub (+ sub (if (numberp (car heights)) (car heights) 0))
2424               hor (cdr hor)))
2425       (setq heights (nreverse heights)
2426             hor (car rule))
2427
2428       ;; We then go through these heighs and create windows for them.
2429       (while heights
2430         (setq height (car heights)
2431               heights (cdr heights))
2432         (and (eq height 'x)
2433              (setq height (- total sub)))
2434         (and heights
2435              (split-window nil height))
2436         (setq to-buf (aref (car hor) 0))
2437         (switch-to-buffer 
2438          (cond ((not to-buf)
2439                 in-buf)
2440                ((symbolp to-buf)
2441                 (symbol-value (aref (car hor) 0)))
2442                (t
2443                 (aref (car hor) 0))))
2444         (and (> (length (car hor)) 2)
2445              (eq (aref (car hor) 2) 'point)
2446              (setq jump-buffer (current-buffer)))
2447         (other-window 1)
2448         (setq hor (cdr hor)))
2449       
2450       (setq rule (cdr rule)))
2451
2452     ;; Finally, we pop to the buffer that's supposed to have point. 
2453     (or jump-buffer (error "Missing `point' in spec for %s" setting))
2454
2455     (select-window (get-buffer-window jump-buffer))
2456     (set-buffer jump-buffer)))
2457       
2458 (defun gnus-remove-some-windows ()
2459   (let ((buffers gnus-window-to-buffer)
2460         (first t)
2461         buf)
2462     (while buffers
2463       (setq buf (cdr (car buffers)))
2464       (if (symbolp buf)
2465           (setq buf (and (boundp buf) (symbol-value buf))))
2466       (and buf 
2467            (get-buffer-window buf)
2468            (progn
2469              (set-buffer buf)
2470              (if first
2471                  (progn
2472                    (switch-to-buffer nntp-server-buffer)
2473                    (setq first nil))
2474                (delete-window (get-buffer-window buf)))))
2475       (setq buffers (cdr buffers)))
2476     (set-buffer nntp-server-buffer)))
2477
2478 (defun gnus-version ()
2479   "Version numbers of this version of Gnus."
2480   (interactive)
2481   (let ((methods gnus-valid-select-methods)
2482         (mess gnus-version)
2483         meth)
2484     ;; Go through all the legal select methods and add their version
2485     ;; numbers to the total version string. Only the backends that are
2486     ;; currently in use will have their message numbers taken into
2487     ;; consideration. 
2488     (while methods
2489       (setq meth (intern (concat (car (car methods)) "-version")))
2490       (and (boundp meth)
2491            (stringp (symbol-value meth))
2492            (setq mess (concat mess "; " (symbol-value meth))))
2493       (setq methods (cdr methods)))
2494     (gnus-message 2 mess)))
2495
2496 (defun gnus-info-find-node ()
2497   "Find Info documentation of Gnus."
2498   (interactive)
2499   ;; Enlarge info window if needed.
2500   (let ((mode major-mode))
2501     (gnus-configure-windows 'info)
2502     (Info-goto-node (car (cdr (assq mode gnus-info-nodes))))))
2503
2504 (defun gnus-bug ()
2505   "Send a bug report to the Gnus maintainers."
2506   (interactive)
2507   (let ((winconf (current-window-configuration)))
2508     (delete-other-windows)
2509     (switch-to-buffer "*Gnus Bug Help*")
2510     (erase-buffer)
2511     (insert gnus-bug-message)
2512     (goto-char (point-min))
2513     (pop-to-buffer "*Gnus Bug*")
2514     (erase-buffer)
2515     (mail-mode)
2516     (mail-setup gnus-maintainer nil nil nil nil nil)
2517     (make-local-variable 'gnus-prev-winconf)
2518     (setq gnus-prev-winconf winconf)
2519     (goto-char (point-min))
2520     (re-search-forward (concat "^" (regexp-quote mail-header-separator) "$"))
2521     (forward-line 1)
2522     (insert (format "%s\n%s\n\n\n\n\n" (gnus-version) (emacs-version)))
2523     (let ((b (point)))
2524       (gnus-debug)
2525       (goto-char (- b 3)))
2526     (message "")))
2527
2528 (defun gnus-debug ()
2529   "Attemps to go through the Gnus source file and report what variables have been changed.
2530 The source file has to be in the Emacs load path."
2531   (interactive)
2532   (let ((files '("gnus.el" "gnus-msg.el" "gnus-score.el"))
2533         file dirs expr olist)
2534     (save-excursion
2535       (set-buffer (get-buffer-create " *gnus bug info*"))
2536       (buffer-disable-undo (current-buffer))
2537       (message "Please wait while we snoop your variables...")
2538       (sit-for 0)
2539       (while files
2540         (erase-buffer)
2541         (setq dirs load-path)
2542         (while dirs
2543           (if (or (not (car dirs))
2544                   (not (stringp (car dirs)))
2545                   (not (file-exists-p 
2546                         (setq file (concat (file-name-as-directory 
2547                                             (car dirs)) (car files))))))
2548               (setq dirs (cdr dirs))
2549             (setq dirs nil)
2550             (insert-file-contents file)
2551             (goto-char (point-min))
2552             (or (re-search-forward "^;;* Internal variables" nil t)
2553                 (error "Malformed sources in file %s" file))
2554             (narrow-to-region (point-min) (point))
2555             (goto-char (point-min))
2556             (while (setq expr (condition-case () 
2557                                   (read (current-buffer)) (error nil)))
2558               (and (eq (car expr) 'defvar)
2559                    (stringp (nth 3 expr))
2560                    (or (not (boundp (nth 1 expr)))
2561                        (not (equal (eval (nth 2 expr))
2562                                    (symbol-value (nth 1 expr)))))
2563                    (setq olist (cons (nth 1 expr) olist))))))
2564         (setq files (cdr files)))
2565       (kill-buffer (current-buffer)))
2566     (insert "------------------- Environment follows -------------------\n\n")
2567     (while olist
2568       (if (boundp (car olist))
2569           (insert "(setq " (symbol-name (car olist)) " '" 
2570                   (prin1-to-string (symbol-value (car olist))) ")\n")
2571         (insert ";; (makeunbound '" (symbol-name (car olist)) ")\n"))
2572       (setq olist (cdr olist)))
2573     (insert "\n\n")))
2574
2575 (defun gnus-overload-functions (&optional overloads)
2576   "Overload functions specified by optional argument OVERLOADS.
2577 If nothing is specified, use the variable gnus-overload-functions."
2578   (let ((defs nil)
2579         (overloads (or overloads gnus-overload-functions)))
2580     (while overloads
2581       (setq defs (car overloads))
2582       (setq overloads (cdr overloads))
2583       ;; Load file before overloading function if necessary.  Make
2584       ;; sure we cannot use `require' always.
2585       (and (not (fboundp (car defs)))
2586            (car (cdr (cdr defs)))
2587            (load (car (cdr (cdr defs))) nil 'nomessage))
2588       (fset (car defs) (car (cdr defs))))))
2589
2590 (defun gnus-replace-chars-in-string (string from to)
2591   "Replace characters in STRING from FROM to TO."
2592   (let ((string (substring string 0))   ;Copy string.
2593         (len (length string))
2594         (idx 0))
2595     ;; Replace all occurrences of FROM with TO.
2596     (while (< idx len)
2597       (if (= (aref string idx) from)
2598           (aset string idx to))
2599       (setq idx (1+ idx)))
2600     string))
2601
2602 (defun gnus-days-between (date1 date2)
2603   ;; Return the number of days between date1 and date2.
2604   (- (gnus-day-number date1) (gnus-day-number date2)))
2605
2606 (defun gnus-day-number (date)
2607   (let ((dat (mapcar (lambda (s) (and s (string-to-int s)) )
2608                      (timezone-parse-date date))))
2609     (timezone-absolute-from-gregorian 
2610      (nth 1 dat) (nth 2 dat) (car dat))))
2611
2612 ;; Returns a floating point number that says how many seconds have
2613 ;; lapsed between Jan 1 12:00:00 1970 and DATE.
2614 (defun gnus-seconds-since-epoch (date)
2615   (let* ((tdate (mapcar (lambda (ti) (and ti (string-to-int ti)))
2616                         (timezone-parse-date date)))
2617          (ttime (mapcar (lambda (ti) (and ti (string-to-int ti)))
2618                         (timezone-parse-time
2619                          (aref (timezone-parse-date date) 3))))
2620          (edate (mapcar (lambda (ti) (and ti (string-to-int ti)))
2621                         (timezone-parse-date "Jan 1 12:00:00 1970")))
2622          (tday (- (timezone-absolute-from-gregorian 
2623                    (nth 1 tdate) (nth 2 tdate) (nth 0 tdate))
2624                   (timezone-absolute-from-gregorian 
2625                    (nth 1 edate) (nth 2 edate) (nth 0 edate)))))
2626     (+ (nth 2 ttime)
2627        (* (nth 1 ttime) 60)
2628        (* 1.0 (nth 0 ttime) 60 60)
2629        (* 1.0 tday 60 60 24))))
2630
2631 (defun gnus-file-newer-than (file date)
2632   (let ((fdate (nth 5 (file-attributes file))))
2633     (or (> (car fdate) (car date))
2634         (and (= (car fdate) (car date))
2635              (> (nth 1 fdate) (nth 1 date))))))
2636
2637 ;; Two silly functions to ensure that all `y-or-n-p' questions clear
2638 ;; the echo area.
2639 (defun gnus-y-or-n-p (prompt)
2640   (prog1
2641       (y-or-n-p prompt)
2642     (message "")))
2643
2644 (defun gnus-yes-or-no-p (prompt)
2645   (prog1
2646       (yes-or-no-p prompt)
2647     (message "")))
2648
2649 ;; Check whether to use long file names.
2650 (defun gnus-use-long-file-name (symbol)
2651   ;; The variable has to be set...
2652   (and gnus-use-long-file-name
2653        ;; If it isn't a list, then we return t.
2654        (or (not (listp gnus-use-long-file-name))
2655            ;; If it is a list, and the list contains `symbol', we
2656            ;; return nil.  
2657            (not (memq symbol gnus-use-long-file-name)))))
2658
2659 ;; I suspect there's a better way, but I haven't taken the time to do
2660 ;; it yet. -erik selberg@cs.washington.edu
2661 (defun gnus-dd-mmm (messy-date)
2662   "Return a string like DD-MMM from a big messy string"
2663   (let ((datevec (timezone-parse-date messy-date)))
2664     (format "%2s-%s"
2665             (or (aref datevec 2) "??")
2666             (capitalize
2667              (or (car 
2668                   (nth (1- (string-to-number (aref datevec 1)))
2669                        timezone-months-assoc))
2670                  "???")))))
2671
2672 ;; Make a hash table (default and minimum size is 255).
2673 ;; Optional argument HASHSIZE specifies the table size.
2674 (defun gnus-make-hashtable (&optional hashsize)
2675   (make-vector (if hashsize (max (gnus-create-hash-size hashsize) 255) 255) 0))
2676
2677 ;; Make a number that is suitable for hashing; bigger than MIN and one
2678 ;; less than 2^x.
2679 (defun gnus-create-hash-size (min)
2680   (let ((i 1))
2681     (while (< i min)
2682       (setq i (* 2 i)))
2683     (1- i)))
2684
2685 ;; Show message if message has a lower level than `gnus-verbose'. 
2686 ;; Guide-line for numbers:
2687 ;; 1 - error messages, 3 - non-serious error messages, 5 - messages
2688 ;; for things that take a long time, 7 - not very important messages
2689 ;; on stuff, 9 - messages inside loops.
2690 (defun gnus-message (level &rest args)
2691   (if (<= level gnus-verbose)
2692       (apply 'message args)
2693     ;; We have to do this format thingie here even if the result isn't
2694     ;; shown - the return value has to be the same as the return value
2695     ;; from `message'.
2696     (apply 'format args)))
2697
2698 ;; Generate a unique new group name.
2699 (defun gnus-generate-new-group-name (leaf)
2700   (let ((name leaf)
2701         (num 0))
2702     (while (gnus-gethash name gnus-newsrc-hashtb)
2703       (setq name (concat leaf "<" (int-to-string (setq num (1+ num))) ">")))
2704     name))
2705
2706 (defun gnus-find-file-noselect (file &optional force)
2707   "Does vaguely the same as find-file-noselect. No hooks are run."
2708   (let (buf insert)
2709     (if (setq buf (get-file-buffer file))
2710         (setq insert force)
2711       (setq buf (create-file-buffer file))
2712       (setq insert t))
2713     (if (not insert)
2714         buf
2715       (save-excursion
2716         (set-buffer buf)
2717         (erase-buffer)
2718         (and (file-readable-p file)
2719              (insert-file-contents file))
2720         (set-visited-file-name file)
2721         (set-buffer-modified-p nil)
2722         (current-buffer)))))
2723
2724 ;;; List and range functions
2725
2726 (defun gnus-last-element (list)
2727   "Return last element of LIST."
2728   (while (cdr list)
2729     (setq list (cdr list)))
2730   (car list))
2731
2732 (defun gnus-copy-sequence (list)
2733   "Do a complete, total copy of a list."
2734   (if (and (consp list) (not (consp (cdr list))))
2735       (cons (car list) (cdr list))
2736     (mapcar (lambda (elem) (if (consp elem) 
2737                                (if (consp (cdr elem))
2738                                    (gnus-copy-sequence elem)
2739                                  (cons (car elem) (cdr elem)))
2740                              elem))
2741             list)))
2742
2743 (defun gnus-set-difference (list1 list2)
2744   "Return a list of elements of LIST1 that do not appear in LIST2."
2745   (let ((list1 (copy-sequence list1)))
2746     (while list2
2747       (setq list1 (delq (car list2) list1))
2748       (setq list2 (cdr list2)))
2749     list1))
2750
2751 (defun gnus-sorted-complement (list1 list2)
2752   "Return a list of elements of LIST1 that do not appear in LIST2.
2753 Both lists have to be sorted over <."
2754   (let (out)
2755     (if (or (null list1) (null list2))
2756         (or list1 list2)
2757       (while (and list1 list2)
2758         (cond ((= (car list1) (car list2))
2759                (setq list1 (cdr list1)
2760                      list2 (cdr list2)))
2761               ((< (car list1) (car list2))
2762                (setq out (cons (car list1) out))
2763                (setq list1 (cdr list1)))
2764               (t
2765                (setq out (cons (car list2) out))
2766                (setq list2 (cdr list2)))))
2767       (nconc (nreverse out) (or list1 list2)))))
2768
2769 (defun gnus-intersection (list1 list2)      
2770   (let ((result nil))
2771     (while list2
2772       (if (memq (car list2) list1)
2773           (setq result (cons (car list2) result)))
2774       (setq list2 (cdr list2)))
2775     result))
2776
2777 (defun gnus-sorted-intersection (list1 list2)
2778   ;; LIST1 and LIST2 have to be sorted over <.
2779   (let (out)
2780     (while (and list1 list2)
2781       (cond ((= (car list1) (car list2))
2782              (setq out (cons (car list1) out)
2783                    list1 (cdr list1)
2784                    list2 (cdr list2)))
2785             ((< (car list1) (car list2))
2786              (setq list1 (cdr list1)))
2787             (t
2788              (setq list2 (cdr list2)))))
2789     (nreverse out)))
2790
2791 (defun gnus-set-sorted-intersection (list1 list2)
2792   ;; LIST1 and LIST2 have to be sorted over <.
2793   ;; This function modifies LIST1.
2794   (let* ((top (cons nil list1))
2795          (prev top))
2796   (while (and list1 list2)
2797     (cond ((= (car list1) (car list2))
2798            (setq prev list1
2799                  list1 (cdr list1)
2800                  list2 (cdr list2)))
2801           ((< (car list1) (car list2))
2802            (setcdr prev (cdr list1))
2803            (setq list1 (cdr list1)))
2804           (t
2805            (setq list2 (cdr list2)))))
2806   (setcdr prev nil)
2807   (cdr top)))
2808
2809 (defun gnus-compress-sequence (numbers &optional always-list)
2810   "Convert list of numbers to a list of ranges or a single range.
2811 If ALWAYS-LIST is non-nil, this function will always release a list of
2812 ranges."
2813   (let* ((first (car numbers))
2814          (last (car numbers))
2815          result)
2816     (if (null numbers)
2817         nil
2818       (if (not (listp (cdr numbers)))
2819           numbers
2820         (while numbers
2821           (cond ((= last (car numbers)) nil) ;Omit duplicated number
2822                 ((= (1+ last) (car numbers)) ;Still in sequence
2823                  (setq last (car numbers)))
2824                 (t                      ;End of one sequence
2825                  (setq result 
2826                        (cons (if (= first last) first
2827                                (cons first last)) result))
2828                  (setq first (car numbers))
2829                  (setq last  (car numbers))))
2830           (setq numbers (cdr numbers)))
2831         (if (and (not always-list) (null result))
2832             (if (= first last) (list first) (cons first last))
2833           (nreverse (cons (if (= first last) first (cons first last))
2834                           result)))))))
2835
2836 (defalias 'gnus-uncompress-sequence 'gnus-uncompress-range)
2837 (defun gnus-uncompress-range (ranges)
2838   "Expand a list of ranges into a list of numbers.
2839 RANGES is either a single range on the form `(num . num)' or a list of
2840 these ranges."
2841   (let (first last result)
2842     (cond 
2843      ((null ranges)
2844       nil)
2845      ((not (listp (cdr ranges)))
2846       (setq first (car ranges))
2847       (setq last (cdr ranges))
2848       (while (<= first last)
2849         (setq result (cons first result))
2850         (setq first (1+ first)))
2851       (nreverse result))
2852      (t
2853       (while ranges
2854         (if (atom (car ranges))
2855             (if (numberp (car ranges))
2856                 (setq result (cons (car ranges) result)))
2857           (setq first (car (car ranges)))
2858           (setq last  (cdr (car ranges)))
2859           (while (<= first last)
2860             (setq result (cons first result))
2861             (setq first (1+ first))))
2862         (setq ranges (cdr ranges)))
2863       (nreverse result)))))
2864
2865 (defun gnus-add-to-range (ranges list)
2866   "Return a list of ranges that has all articles from both RANGES and LIST.
2867 Note: LIST has to be sorted over `<'."
2868   (if (not ranges)
2869       (gnus-compress-sequence list t)
2870     (setq list (copy-sequence list))
2871     (or (listp (cdr ranges))
2872         (setq ranges (list ranges)))
2873     (let ((out ranges)
2874           ilist lowest highest temp)
2875       (while (and ranges list)
2876         (setq ilist list)
2877         (setq lowest (or (and (atom (car ranges)) (car ranges))
2878                          (car (car ranges))))
2879         (while (and list (cdr list) (< (car (cdr list)) lowest))
2880           (setq list (cdr list)))
2881         (if (< (car ilist) lowest)
2882             (progn
2883               (setq temp list)
2884               (setq list (cdr list))
2885               (setcdr temp nil)
2886               (setq out (nconc (gnus-compress-sequence ilist t) out))))
2887         (setq highest (or (and (atom (car ranges)) (car ranges))
2888                           (cdr (car ranges))))
2889         (while (and list (<= (car list) highest))
2890           (setq list (cdr list)))
2891         (setq ranges (cdr ranges)))
2892       (if list
2893           (setq out (nconc (gnus-compress-sequence list t) out)))
2894       (setq out (sort out (lambda (r1 r2) 
2895                             (< (or (and (atom r1) r1) (car r1))
2896                                (or (and (atom r2) r2) (car r2))))))
2897       (setq ranges out)
2898       (while ranges
2899         (if (atom (car ranges))
2900             (if (cdr ranges)
2901                 (if (atom (car (cdr ranges)))
2902                     (if (= (1+ (car ranges)) (car (cdr ranges)))
2903                         (progn
2904                           (setcar ranges (cons (car ranges) 
2905                                                (car (cdr ranges))))
2906                           (setcdr ranges (cdr (cdr ranges)))))
2907                   (if (= (1+ (car ranges)) (car (car (cdr ranges))))
2908                       (progn
2909                         (setcar (car (cdr ranges)) (car ranges))
2910                         (setcar ranges (car (cdr ranges)))
2911                         (setcdr ranges (cdr (cdr ranges)))))))
2912           (if (cdr ranges)
2913               (if (atom (car (cdr ranges)))
2914                   (if (= (1+ (cdr (car ranges))) (car (cdr ranges)))
2915                       (progn
2916                         (setcdr (car ranges) (car (cdr ranges)))
2917                         (setcdr ranges (cdr (cdr ranges)))))
2918                 (if (= (1+ (cdr (car ranges))) (car (car (cdr ranges))))
2919                     (progn
2920                       (setcdr (car ranges) (cdr (car (cdr ranges))))
2921                       (setcdr ranges (cdr (cdr ranges))))))))
2922         (setq ranges (cdr ranges)))
2923       out)))
2924
2925 (defun gnus-remove-from-range (ranges list)
2926   "Return a list of ranges that has all articles from LIST removed from RANGES.
2927 Note: LIST has to be sorted over `<'."
2928   ;; !!! This function shouldn't look like this, but I've got a headache.
2929   (gnus-compress-sequence 
2930    (gnus-sorted-complement
2931     (gnus-uncompress-range ranges) list)))
2932
2933 (defun gnus-member-of-range (number ranges)
2934   (if (not (listp (cdr ranges)))
2935       (and (>= number (car ranges)) 
2936            (<= number (cdr ranges)))
2937     (let ((not-stop t))
2938       (while (and ranges 
2939                   (if (numberp (car ranges))
2940                       (>= number (car ranges))
2941                     (>= number (car (car ranges))))
2942                   not-stop)
2943         (if (if (numberp (car ranges))
2944                 (= number (car ranges))
2945               (and (>= number (car (car ranges)))
2946                    (<= number (cdr (car ranges)))))
2947             (setq not-stop nil))
2948         (setq ranges (cdr ranges)))
2949       (not not-stop))))
2950
2951 \f
2952 ;;;
2953 ;;; Gnus group mode
2954 ;;;
2955
2956 (defvar gnus-group-mode-map nil)
2957 (defvar gnus-group-group-map nil)
2958 (defvar gnus-group-mark-map nil)
2959 (defvar gnus-group-list-map nil)
2960 (defvar gnus-group-sub-map nil)
2961 (put 'gnus-group-mode 'mode-class 'special)
2962
2963 (if gnus-group-mode-map
2964     nil
2965   (setq gnus-group-mode-map (make-keymap))
2966   (suppress-keymap gnus-group-mode-map)
2967   (define-key gnus-group-mode-map " " 'gnus-group-read-group)
2968   (define-key gnus-group-mode-map "=" 'gnus-group-select-group)
2969   (define-key gnus-group-mode-map "\r" 'gnus-group-select-group)
2970   (define-key gnus-group-mode-map "j" 'gnus-group-jump-to-group)
2971   (define-key gnus-group-mode-map "n" 'gnus-group-next-unread-group)
2972   (define-key gnus-group-mode-map "p" 'gnus-group-prev-unread-group)
2973   (define-key gnus-group-mode-map "\177" 'gnus-group-prev-unread-group)
2974   (define-key gnus-group-mode-map "N" 'gnus-group-next-group)
2975   (define-key gnus-group-mode-map "P" 'gnus-group-prev-group)
2976   (define-key gnus-group-mode-map "\M-n" 'gnus-group-next-unread-group-same-level)
2977   (define-key gnus-group-mode-map "\M-p" 'gnus-group-prev-unread-group-same-level)
2978   (define-key gnus-group-mode-map "," 'gnus-group-best-unread-group)
2979   (define-key gnus-group-mode-map "." 'gnus-group-first-unread-group)
2980   (define-key gnus-group-mode-map "u" 'gnus-group-unsubscribe-current-group)
2981   (define-key gnus-group-mode-map "U" 'gnus-group-unsubscribe-group)
2982   (define-key gnus-group-mode-map "c" 'gnus-group-catchup-current)
2983   (define-key gnus-group-mode-map "C" 'gnus-group-catchup-current-all)
2984   (define-key gnus-group-mode-map "l" 'gnus-group-list-groups)
2985   (define-key gnus-group-mode-map "L" 'gnus-group-list-all-groups)
2986   (define-key gnus-group-mode-map "m" 'gnus-group-mail)
2987   (define-key gnus-group-mode-map "g" 'gnus-group-get-new-news)
2988   (define-key gnus-group-mode-map "\M-g" 'gnus-group-get-new-news-this-group)
2989   (define-key gnus-group-mode-map "R" 'gnus-group-restart)
2990   (define-key gnus-group-mode-map "r" 'gnus-group-read-init-file)
2991   (define-key gnus-group-mode-map "B" 'gnus-group-browse-foreign-server)
2992   (define-key gnus-group-mode-map "b" 'gnus-group-check-bogus-groups)
2993   (define-key gnus-group-mode-map "F" 'gnus-find-new-newsgroups)
2994   (define-key gnus-group-mode-map "\C-c\C-d" 'gnus-group-describe-group)
2995   (define-key gnus-group-mode-map "\M-d" 'gnus-group-describe-all-groups)
2996   (define-key gnus-group-mode-map "\C-c\C-a" 'gnus-group-apropos)
2997   (define-key gnus-group-mode-map "\C-c\M-C-a" 'gnus-group-description-apropos)
2998   (define-key gnus-group-mode-map "a" 'gnus-group-post-news)
2999   (define-key gnus-group-mode-map "\ek" 'gnus-group-edit-local-kill)
3000   (define-key gnus-group-mode-map "\eK" 'gnus-group-edit-global-kill)
3001   (define-key gnus-group-mode-map "\C-k" 'gnus-group-kill-group)
3002   (define-key gnus-group-mode-map "\C-y" 'gnus-group-yank-group)
3003   (define-key gnus-group-mode-map "\C-w" 'gnus-group-kill-region)
3004   (define-key gnus-group-mode-map "\C-x\C-t" 'gnus-group-transpose-groups)
3005   (define-key gnus-group-mode-map "\C-c\C-l" 'gnus-group-list-killed)
3006   (define-key gnus-group-mode-map "\C-c\C-x" 'gnus-group-expire-articles)
3007   (define-key gnus-group-mode-map "\C-c\M-\C-x" 'gnus-group-expire-all-groups)
3008   (define-key gnus-group-mode-map "V" 'gnus-version)
3009   (define-key gnus-group-mode-map "s" 'gnus-group-save-newsrc)
3010   (define-key gnus-group-mode-map "z" 'gnus-group-suspend)
3011   (define-key gnus-group-mode-map "Z" 'gnus-group-clear-dribble)
3012   (define-key gnus-group-mode-map "q" 'gnus-group-exit)
3013   (define-key gnus-group-mode-map "Q" 'gnus-group-quit)
3014   (define-key gnus-group-mode-map "\M-f" 'gnus-group-fetch-faq)
3015   (define-key gnus-group-mode-map "?" 'gnus-group-describe-briefly)
3016   (define-key gnus-group-mode-map "\C-c\C-i" 'gnus-info-find-node)
3017   (define-key gnus-group-mode-map "\M-e" 'gnus-group-edit-group-method)
3018   (define-key gnus-group-mode-map "^" 'gnus-group-enter-server-mode)
3019   (define-key gnus-group-mode-map gnus-mouse-2 'gnus-mouse-pick-group)
3020   (define-key gnus-group-mode-map "<" 'beginning-of-buffer)
3021   (define-key gnus-group-mode-map ">" 'end-of-buffer)
3022   (define-key gnus-group-mode-map "\C-c\C-b" 'gnus-bug)
3023   (define-key gnus-group-mode-map "\C-c\C-s" 'gnus-group-sort-groups)
3024
3025   (define-key gnus-group-mode-map "#" 'gnus-group-mark-group)
3026   (define-key gnus-group-mode-map "\M-#" 'gnus-group-unmark-group)
3027   (define-prefix-command 'gnus-group-mark-map)
3028   (define-key gnus-group-mode-map "M" 'gnus-group-mark-map)
3029   (define-key gnus-group-mark-map "m" 'gnus-group-mark-group)
3030   (define-key gnus-group-mark-map "u" 'gnus-group-unmark-group)
3031   (define-key gnus-group-mark-map "w" 'gnus-group-mark-region)
3032
3033   (define-prefix-command 'gnus-group-group-map)
3034   (define-key gnus-group-mode-map "G" 'gnus-group-group-map)
3035   (define-key gnus-group-group-map "d" 'gnus-group-make-directory-group)
3036   (define-key gnus-group-group-map "h" 'gnus-group-make-help-group)
3037   (define-key gnus-group-group-map "a" 'gnus-group-make-archive-group)
3038   (define-key gnus-group-group-map "k" 'gnus-group-make-kiboze-group)
3039   (define-key gnus-group-group-map "m" 'gnus-group-make-group)
3040   (define-key gnus-group-group-map "E" 'gnus-group-edit-group)
3041   (define-key gnus-group-group-map "e" 'gnus-group-edit-group-method)
3042   (define-key gnus-group-group-map "p" 'gnus-group-edit-group-parameters)
3043   (define-key gnus-group-group-map "v" 'gnus-group-add-to-virtual)
3044   (define-key gnus-group-group-map "V" 'gnus-group-make-empty-virtual)
3045   (define-key gnus-group-group-map "D" 'gnus-group-enter-directory)
3046   (define-key gnus-group-group-map "f" 'gnus-group-make-doc-group)
3047   (define-key gnus-group-group-map "sb" 'gnus-group-brew-soup)
3048   (define-key gnus-group-group-map "sw" 'gnus-soup-save-areas)
3049   (define-key gnus-group-group-map "ss" 'gnus-soup-send-replies)
3050   (define-key gnus-group-group-map "sp" 'gnus-soup-pack-packet)
3051   (define-key gnus-group-group-map "sr" 'nnsoup-pack-replies)
3052
3053   (define-prefix-command 'gnus-group-list-map)
3054   (define-key gnus-group-mode-map "A" 'gnus-group-list-map)
3055   (define-key gnus-group-list-map "k" 'gnus-group-list-killed)
3056   (define-key gnus-group-list-map "z" 'gnus-group-list-zombies)
3057   (define-key gnus-group-list-map "s" 'gnus-group-list-groups)
3058   (define-key gnus-group-list-map "u" 'gnus-group-list-all-groups)
3059   (define-key gnus-group-list-map "a" 'gnus-group-apropos)
3060   (define-key gnus-group-list-map "d" 'gnus-group-description-apropos)
3061   (define-key gnus-group-list-map "m" 'gnus-group-list-matching)
3062   (define-key gnus-group-list-map "M" 'gnus-group-list-all-matching)
3063
3064   (define-prefix-command 'gnus-group-sub-map)
3065   (define-key gnus-group-mode-map "S" 'gnus-group-sub-map)
3066   (define-key gnus-group-sub-map "l" 'gnus-group-set-current-level)
3067   (define-key gnus-group-sub-map "t" 'gnus-group-unsubscribe-current-group)
3068   (define-key gnus-group-sub-map "s" 'gnus-group-unsubscribe-group)
3069   (define-key gnus-group-sub-map "k" 'gnus-group-kill-group)
3070   (define-key gnus-group-sub-map "y" 'gnus-group-yank-group)
3071   (define-key gnus-group-sub-map "w" 'gnus-group-kill-region)
3072   (define-key gnus-group-sub-map "z" 'gnus-group-kill-all-zombies))
3073
3074 (defun gnus-group-mode ()
3075   "Major mode for reading news.
3076
3077 All normal editing commands are switched off.
3078 \\<gnus-group-mode-map>
3079 The group buffer lists (some of) the groups available.  For instance,
3080 `\\[gnus-group-list-groups]' will list all subscribed groups with unread articles, while `\\[gnus-group-list-zombies]'
3081 lists all zombie groups. 
3082
3083 Groups that are displayed can be entered with `\\[gnus-group-read-group]'.  To subscribe 
3084 to a group not displayed, type `\\[gnus-group-unsubscribe-group]'. 
3085
3086 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]'). 
3087
3088 The following commands are available:
3089
3090 \\{gnus-group-mode-map}"
3091   (interactive)
3092   (if gnus-visual (gnus-group-make-menu-bar))
3093   (kill-all-local-variables)
3094   (setq mode-line-modified "-- ")
3095   (make-local-variable 'mode-line-format)
3096   (setq mode-line-format (copy-sequence mode-line-format))
3097   (and (equal (nth 3 mode-line-format) "   ")
3098        (setcar (nthcdr 3 mode-line-format) ""))
3099   (setq major-mode 'gnus-group-mode)
3100   (setq mode-name "Group")
3101   (gnus-group-set-mode-line)
3102   (setq mode-line-process nil)
3103   (use-local-map gnus-group-mode-map)
3104   (buffer-disable-undo (current-buffer))
3105   (setq truncate-lines t)
3106   (setq buffer-read-only t)
3107   (run-hooks 'gnus-group-mode-hook))
3108
3109 (defun gnus-mouse-pick-group (e)
3110   (interactive "e")
3111   (mouse-set-point e)
3112   (gnus-group-read-group nil))
3113
3114 ;;;###autoload
3115 (defun gnus-no-server (&optional arg)
3116   "Read network news.
3117 If ARG is a positive number, Gnus will use that as the
3118 startup level. If ARG is nil, Gnus will be started at level 2. 
3119 If ARG is non-nil and not a positive number, Gnus will
3120 prompt the user for the name of an NNTP server to use.
3121 As opposed to `gnus', this command will not connect to the local server."
3122   (interactive "P")
3123   (gnus (or arg (1- gnus-level-default-subscribed)) t))
3124
3125 (defalias '\(ding\) 'gnus)
3126
3127 ;;;###autoload
3128 (defun gnus (&optional arg dont-connect)
3129   "Read network news.
3130 If ARG is non-nil and a positive number, Gnus will use that as the
3131 startup level. If ARG is non-nil and not a positive number, Gnus will
3132 prompt the user for the name of an NNTP server to use."
3133   (interactive "P")
3134   (if (get-buffer gnus-group-buffer)
3135       (progn
3136         (switch-to-buffer gnus-group-buffer)
3137         (gnus-group-get-new-news))
3138     (gnus-clear-system)
3139     (nnheader-init-server-buffer)
3140     (gnus-read-init-file)
3141     (let ((level (and arg (numberp arg) (> arg 0) arg))
3142           did-connect)
3143       (unwind-protect
3144           (progn
3145             (gnus-group-setup-buffer)
3146             (or dont-connect 
3147                 (setq did-connect
3148                       (gnus-start-news-server (and arg (not level))))))
3149         (if (and (not dont-connect) 
3150                  (not did-connect))
3151             (gnus-group-quit)
3152           (run-hooks 'gnus-startup-hook)
3153           ;; NNTP server is successfully open. 
3154           (gnus-update-format-specifications)
3155           (gnus-summary-make-display-table)
3156           (let ((buffer-read-only nil))
3157             (erase-buffer)
3158             (if (not gnus-inhibit-startup-message)
3159                 (progn
3160                   (gnus-group-startup-message)
3161                   (sit-for 0))))
3162           (gnus-setup-news nil level)
3163           (and gnus-use-dribble-file (gnus-dribble-open))
3164           (gnus-group-list-groups level)
3165           (gnus-configure-windows 'group))))))
3166
3167 (defun gnus-group-startup-message (&optional x y)
3168   "Insert startup message in current buffer."
3169   ;; Insert the message.
3170   (erase-buffer)
3171   (insert
3172    (format "
3173     %s
3174            A newsreader 
3175       for GNU Emacs
3176
3177         Based on GNUS 
3178              written by 
3179      Masanobu UMEDA
3180
3181     Lars Magne 
3182          Ingebrigtsen 
3183       larsi@ifi.uio.no
3184
3185            gnus-version))
3186   ;; And then hack it.
3187   ;; 18 is the longest line.
3188   (indent-rigidly (point-min) (point-max) 
3189                   (/ (max (- (window-width) (or x 28)) 0) 2))
3190   (goto-char (point-min))
3191   ;; +4 is fuzzy factor.
3192   (insert-char ?\n (/ (max (- (window-height) (or y 12)) 0) 2)))
3193
3194 (defun gnus-group-setup-buffer ()
3195   (or (get-buffer gnus-group-buffer)
3196       (progn
3197         (switch-to-buffer gnus-group-buffer)
3198         (gnus-add-current-to-buffer-list)
3199         (gnus-group-mode)
3200         (and gnus-carpal (gnus-carpal-setup-buffer 'group)))))
3201
3202 (defun gnus-group-list-groups (level &optional unread)
3203   "List newsgroups with level LEVEL or lower that have unread articles.
3204 Default is all subscribed groups.
3205 If argument UNREAD is non-nil, groups with no unread articles are also listed."
3206   (interactive (list (and current-prefix-arg
3207                           (prefix-numeric-value current-prefix-arg))))
3208   (if gnus-group-use-permanent-levels
3209       (progn
3210         (setq gnus-group-default-list-level 
3211               (or level gnus-group-default-list-level))
3212         (setq level (or gnus-group-default-list-level gnus-level-subscribed)))
3213     (setq level (or level gnus-group-default-list-level 
3214                     gnus-level-subscribed)))
3215   (gnus-group-setup-buffer)     ;May call from out of group buffer
3216   (let ((case-fold-search nil)
3217         (group (gnus-group-group-name)))
3218     (funcall gnus-group-prepare-function level unread nil)
3219     (if (zerop (buffer-size))
3220         (gnus-message 5 gnus-no-groups-message)
3221       (goto-char (point-min))
3222       (if (not group)
3223           ;; Go to the first group with unread articles.
3224           (gnus-group-search-forward nil nil nil t)
3225         ;; Find the right group to put point on. If the current group
3226         ;; has disapeared in the new listing, try to find the next
3227         ;; one. If no next one can be found, just leave point at the
3228         ;; first newsgroup in the buffer.
3229         (if (not (gnus-goto-char
3230                   (text-property-any (point-min) (point-max) 
3231                                      'gnus-group (intern group))))
3232             (let ((newsrc (nthcdr 3 (gnus-gethash group gnus-newsrc-hashtb))))
3233               (while (and newsrc
3234                           (not (gnus-goto-char 
3235                                 (text-property-any 
3236                                  (point-min) (point-max) 'gnus-group 
3237                                  (intern (car (car newsrc)))))))
3238                 (setq newsrc (cdr newsrc)))
3239               (or newsrc (progn (goto-char (point-max))
3240                                 (forward-line -1))))))
3241       ;; Adjust cursor point.
3242       (gnus-group-position-cursor))))
3243
3244 (defun gnus-group-prepare-flat (level &optional all lowest regexp) 
3245   "List all newsgroups with unread articles of level LEVEL or lower.
3246 If ALL is non-nil, list groups that have no unread articles.
3247 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher.
3248 If REGEXP, only list groups matching REGEXP."
3249   (set-buffer gnus-group-buffer)
3250   (let ((buffer-read-only nil)
3251         (newsrc (cdr gnus-newsrc-alist))
3252         (lowest (or lowest 1))
3253         info clevel unread group)
3254     (erase-buffer)
3255     (if (< lowest gnus-level-zombie)
3256         ;; List living groups.
3257         (while newsrc
3258           (setq info (car newsrc)
3259                 group (car info)
3260                 newsrc (cdr newsrc)
3261                 unread (car (gnus-gethash group gnus-newsrc-hashtb)))
3262           (and unread ; This group might be bogus
3263                (or (not regexp)
3264                    (string-match regexp group))
3265                (<= (setq clevel (car (cdr info))) level) 
3266                (>= clevel lowest)
3267                (or all            ; We list all groups?
3268                    (eq unread t)  ; We list unactivated groups
3269                    (> unread 0)   ; We list groups with unread articles
3270                    (cdr (assq 'tick (nth 3 info)))) ; And groups with tickeds
3271                (gnus-group-insert-group-line 
3272                 nil group (car (cdr info)) (nth 3 info) unread (nth 4 info)))))
3273
3274     ;; List dead groups.
3275     (and (>= level gnus-level-zombie) (<= lowest gnus-level-zombie)
3276          (gnus-group-prepare-flat-list-dead 
3277           (setq gnus-zombie-list (sort gnus-zombie-list 'string<)) 
3278           gnus-level-zombie ?Z
3279           regexp))
3280     (and (>= level gnus-level-killed) (<= lowest gnus-level-killed)
3281          (gnus-group-prepare-flat-list-dead 
3282           (setq gnus-killed-list (sort gnus-killed-list 'string<)) 
3283           gnus-level-killed ?K regexp))
3284
3285     (gnus-group-set-mode-line)
3286     (setq gnus-have-all-newsgroups all)
3287     (run-hooks 'gnus-group-prepare-hook)))
3288
3289 (defun gnus-group-prepare-flat-list-dead (groups level mark regexp)
3290   ;; List zombies and killed lists somehwat faster, which was
3291   ;; suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>. It does
3292   ;; this by ignoring the group format specification altogether.
3293   (let (group beg)
3294     (while groups
3295       (setq group (car groups)
3296             groups (cdr groups))
3297       (if (or (not regexp)
3298               (string-match regexp group))
3299           (progn
3300             (setq beg (point))
3301             (insert (format " %c     *: %s\n" mark group))
3302             (add-text-properties 
3303              beg (1+ beg) 
3304              (list 'gnus-group (intern group)
3305                    'gnus-unread t
3306                    'gnus-level level)))))))
3307
3308 (defun gnus-group-real-name (group)
3309   "Find the real name of a foreign newsgroup."
3310   (if (string-match ":[^:]+$" group)
3311       (substring group (1+ (match-beginning 0)))
3312     group))
3313
3314 (defun gnus-group-prefixed-name (group method)
3315   "Return the whole name from GROUP and METHOD."
3316   (and (stringp method) (setq method (gnus-server-to-method method)))
3317   (concat (format "%s" (car method))
3318           (if (and 
3319                (assoc (format "%s" (car method)) (gnus-methods-using 'address))
3320                (not (string= (nth 1 method) "")))
3321               (concat "+" (nth 1 method)))
3322           ":" group))
3323
3324 (defun gnus-group-real-prefix (group)
3325   "Return the prefix of the current group name."
3326   (if (string-match "^[^:]+:" group)
3327       (substring group 0 (match-end 0))
3328     ""))
3329
3330 (defun gnus-group-method-name (group)
3331   "Return the method used for selecting GROUP."
3332   (let ((prefix (gnus-group-real-prefix group)))
3333     (if (equal prefix "")
3334         gnus-select-method
3335       (if (string-match "^[^\\+]+\\+" prefix)
3336           (list (intern (substring prefix 0 (1- (match-end 0))))
3337                 (substring prefix (match-end 0) (1- (length prefix))))
3338         (list (intern (substring prefix 0 (1- (length prefix)))) "")))))
3339
3340 (defun gnus-group-foreign-p (group)
3341   "Return nil if GROUP is native, non-nil if it is foreign."
3342   (string-match ":" group))
3343
3344 (defun gnus-group-set-info (info &optional method-only-group part)
3345   (let* ((entry (gnus-gethash
3346                  (or method-only-group (car info)) gnus-newsrc-hashtb))
3347          (part-info info)
3348          (info (if method-only-group (nth 2 entry) info)))
3349     (if (not method-only-group)
3350         ()
3351       (or entry
3352           (error "Trying to change non-existent group %s" method-only-group))
3353       ;; We have recevied parts of the actual group info - either the
3354       ;; select method or the group parameters.  We first check
3355       ;; whether we have to extend the info, and if so, do that.
3356       (let ((len (length info))
3357             (total (if (eq part 'method) 5 6)))
3358         (and (< len total)
3359              (setcdr (nthcdr (1- len) info)
3360                      (make-list (- total len) nil)))
3361         ;; Then we enter the new info.
3362         (setcar (nthcdr (1- total) info) part-info)))
3363     ;; We uncompress some lists of marked articles.
3364     (let (marked)
3365       (if (not (setq marked (nth 3 info)))
3366           ()
3367         (while marked
3368           (or (eq 'score (car (car marked)))
3369               (eq 'bookmark (car (car marked)))
3370               (eq 'killed (car (car marked)))
3371               (setcdr (car marked) 
3372                       (gnus-uncompress-range (cdr (car marked)))))
3373           (setq marked (cdr marked)))))
3374     (if entry
3375         ()
3376       ;; This is a new group, so we just create it.
3377       (save-excursion
3378         (set-buffer gnus-group-buffer)
3379         (if (nth 4 info)
3380             ;; It's a foreign group...
3381             (gnus-group-make-group 
3382              (gnus-group-real-name (car info))
3383              (prin1-to-string (car (nth 4 info)))
3384              (nth 1 (nth 4 info)))
3385           ;; It's a native group.
3386           (gnus-group-make-group
3387            (car info)
3388            (prin1-to-string (car gnus-select-method))
3389            (nth 1 gnus-select-method)))
3390         (gnus-message 6 "Note: New group created")
3391         (setq entry 
3392               (gnus-gethash (gnus-group-prefixed-name 
3393                              (gnus-group-real-name (car info))
3394                              (or (nth 4 info) gnus-select-method))
3395                             gnus-newsrc-hashtb))))
3396     ;; Whether it was a new group or not, we now have the entry, so we
3397     ;; can do the update.
3398     (if entry
3399         (progn
3400           (setcar (nthcdr 2 entry) info)
3401           (if (and (not (eq (car entry) t)) 
3402                    (gnus-gethash (car info) gnus-active-hashtb))
3403               (let ((marked (nth 3 info)))
3404                 (setcar entry 
3405                         (max 0 (- (length (gnus-list-of-unread-articles 
3406                                            (car info)))
3407                                   (length (cdr (assq 'tick marked)))
3408                                   (length (cdr (assq 'dormant marked)))))))))
3409       (error "No such group: %s" (car info)))))
3410
3411 (defun gnus-group-set-method-info (group select-method)
3412   (gnus-group-set-info select-method group 'method))
3413
3414 (defun gnus-group-set-params-info (group params)
3415   (gnus-group-set-info params group 'params))
3416
3417 (defun gnus-group-update-group-line ()
3418   "This function updates the current line in the newsgroup buffer and
3419 moves the point to the colon."
3420   (let* ((buffer-read-only nil)
3421          (group (gnus-group-group-name))
3422          (entry (and group (gnus-gethash group gnus-newsrc-hashtb))))
3423     (if entry
3424         (gnus-dribble-enter 
3425          (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
3426                  ")")))
3427     (beginning-of-line)
3428     (delete-region (point) (progn (forward-line 1) (point)))
3429     (gnus-group-insert-group-line-info group)
3430     (forward-line -1)
3431     (gnus-group-position-cursor)))
3432
3433 (defun gnus-group-insert-group-line-info (group)
3434   (let ((entry (gnus-gethash group gnus-newsrc-hashtb)) 
3435         active info)
3436     (if entry
3437         (progn
3438           (setq info (nth 2 entry))
3439           (gnus-group-insert-group-line 
3440            nil group (nth 1 info) (nth 3 info) (car entry) (nth 4 info)))
3441       (setq active (gnus-gethash group gnus-active-hashtb))
3442       (gnus-group-insert-group-line 
3443        nil group (if (member group gnus-zombie-list) gnus-level-zombie
3444                    gnus-level-killed)
3445        nil (if active (- (1+ (cdr active)) (car active)) 0) nil))))
3446
3447 (defun gnus-group-insert-group-line (gformat group level marked number method)
3448   (let* ((gformat (or gformat gnus-group-line-format-spec))
3449          (active (gnus-gethash group gnus-active-hashtb))
3450          (number-total (if active (1+ (- (cdr active) (car active))) 0))
3451          (number-of-dormant (length (cdr (assq 'dormant marked))))
3452          (number-of-ticked (length (cdr (assq 'tick marked))))
3453          (number-of-ticked-and-dormant
3454           (+ number-of-ticked number-of-dormant))
3455          (number-of-unread-unticked 
3456           (if (numberp number) (int-to-string (max 0 number))
3457             "*"))
3458          (number-of-read
3459           (if (numberp number)
3460               (max 0 (- number-total number))
3461             "*"))
3462          (subscribed (cond ((<= level gnus-level-subscribed) ? )
3463                            ((<= level gnus-level-unsubscribed) ?U)
3464                            ((= level gnus-level-zombie) ?Z)
3465                            (t ?K)))
3466          (qualified-group (gnus-group-real-name group))
3467          (newsgroup-description 
3468           (if gnus-description-hashtb
3469               (or (gnus-gethash group gnus-description-hashtb) "")
3470             ""))
3471          (moderated (if (member group gnus-moderated-list) ?m ? ))
3472          (moderated-string (if (eq moderated ?m) "(m)" ""))
3473          (method (gnus-server-get-method group method))
3474          (news-server (or (car (cdr method)) ""))
3475          (news-method (or (car method) ""))
3476          (news-method-string 
3477           (if method (format "(%s:%s)" (car method) (car (cdr method))) ""))
3478          (marked (if (and 
3479                       (numberp number) 
3480                       (zerop number)
3481                       (> number-of-ticked 0))
3482                      ?* ? ))
3483          (number (if (eq number t) "*" (+ number number-of-dormant 
3484                                           number-of-ticked)))
3485          (process-marked (if (member qualified-group gnus-group-marked)
3486                              gnus-process-mark ? ))
3487          (buffer-read-only nil)
3488          b)
3489     (beginning-of-line)
3490     (setq b (point))
3491     ;; Insert the text.
3492     (insert (eval gformat))
3493
3494     (add-text-properties 
3495      b (1+ b) (list 'gnus-group (intern group)
3496                     'gnus-unread (if (numberp number)
3497                                      (string-to-int number-of-unread-unticked)
3498                                    t)
3499                     'gnus-marked marked
3500                     'gnus-level level))))
3501
3502 (defun gnus-group-update-group (group &optional visible-only)
3503   "Update newsgroup info of GROUP.
3504 If VISIBLE-ONLY is non-nil, the group won't be displayed if it isn't already."
3505   (save-excursion
3506     (set-buffer gnus-group-buffer)
3507     (let ((buffer-read-only nil)
3508           visible)
3509       (let ((entry (gnus-gethash group gnus-newsrc-hashtb)))
3510         (if entry
3511             (gnus-dribble-enter 
3512              (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
3513                      ")"))))
3514       ;; Buffer may be narrowed.
3515       (save-restriction
3516         (widen)
3517         ;; Search a line to modify.  If the buffer is large, the search
3518         ;; takes long time.  In most cases, current point is on the line
3519         ;; we are looking for.  So, first of all, check current line. 
3520         (if (or (progn
3521                   (beginning-of-line)
3522                   (eq (get-text-property (point) 'gnus-group)
3523                       (intern group)))
3524                 (progn
3525                   (gnus-goto-char 
3526                    (text-property-any 
3527                     (point-min) (point-max) 'gnus-group (intern group)))))
3528             ;; GROUP is listed in current buffer. So, delete old line.
3529             (progn
3530               (setq visible t)
3531               (beginning-of-line)
3532               (delete-region (point) (progn (forward-line 1) (point))))
3533           ;; No such line in the buffer, find out where it's supposed to
3534           ;; go, and insert it there (or at the end of the buffer).
3535           ;; Fix by Per Abrahamsen <amanda@iesd.auc.dk>.
3536           (or visible-only
3537               (let ((entry 
3538                      (cdr (cdr (gnus-gethash group gnus-newsrc-hashtb)))))
3539                 (while (and entry
3540                             (car entry)
3541                             (not
3542                              (gnus-goto-char
3543                               (text-property-any
3544                                (point-min) (point-max) 
3545                                'gnus-group (intern (car (car entry)))))))
3546                   (setq entry (cdr entry)))
3547                 (or entry (goto-char (point-max)))))))
3548       (if (or visible (not visible-only))
3549           (gnus-group-insert-group-line-info group))
3550       (gnus-group-set-mode-line))))
3551
3552 (defun gnus-group-set-mode-line ()
3553   (if (memq 'group gnus-updated-mode-lines)
3554       (let* ((gformat (or gnus-group-mode-line-format-spec
3555                           (setq gnus-group-mode-line-format-spec
3556                                 (gnus-parse-format 
3557                                  gnus-group-mode-line-format 
3558                                  gnus-group-mode-line-format-alist))))
3559              (news-server (car (cdr gnus-select-method)))
3560              (news-method (car gnus-select-method))
3561              (max-len 60)
3562              (mode-string (eval gformat)))
3563         (setq mode-string (eval gformat))
3564         (if (> (length mode-string) max-len) 
3565             (setq mode-string (substring mode-string 0 (- max-len 4))))
3566         (setq mode-line-buffer-identification mode-string)
3567         (set-buffer-modified-p t))))
3568
3569 (defun gnus-group-group-name ()
3570   "Get the name of the newsgroup on the current line."
3571   (let ((group (get-text-property (gnus-point-at-bol) 'gnus-group)))
3572     (and group (symbol-name group))))
3573
3574 (defun gnus-group-group-level ()
3575   "Get the level of the newsgroup on the current line."
3576   (get-text-property (gnus-point-at-bol) 'gnus-level))
3577
3578 (defun gnus-group-search-forward (&optional backward all level first-too)
3579   "Find the next newsgroup with unread articles.
3580 If BACKWARD is non-nil, find the previous newsgroup instead.
3581 If ALL is non-nil, just find any newsgroup.
3582 If LEVEL is non-nil, find group with level LEVEL, or higher if no such
3583 group exists.
3584 If FIRST-TOO, the current line is also eligible as a target."
3585   (let ((way (if backward -1 1))
3586         (low 10)
3587         (beg (point))
3588         pos found)
3589     (if (and backward (progn (beginning-of-line)) (bobp))
3590         nil
3591       (or first-too (forward-line way))
3592       (while (and 
3593               (not (eobp))
3594               (not (setq 
3595                     found 
3596                     (and (or all
3597                              (and
3598                               (let ((unread 
3599                                      (get-text-property (point) 'gnus-unread)))
3600                                 (or (eq unread t) (and unread (> unread 0))))
3601                               (let ((lev (get-text-property
3602                                           (point) 'gnus-level)))
3603                                 (and lev (<= (get-text-property 
3604                                               (point) 'gnus-level)
3605                                              gnus-level-subscribed)))))
3606                          (or (not level)
3607                              (let ((lev (get-text-property (point) 'gnus-level)))
3608                                (if (and lev (<= lev level))
3609                                    t
3610                                  (if (< lev low)
3611                                      (progn
3612                                        (setq low lev)
3613                                        (setq pos (point))))
3614                                  nil))))))
3615               (zerop (forward-line way)))))
3616     (if found 
3617         (progn (gnus-group-position-cursor) t)
3618       (if pos (goto-char pos) (goto-char beg))
3619       nil)))
3620
3621 ;;; Gnus group mode commands
3622
3623 ;; Group marking.
3624
3625 (defun gnus-group-mark-group (n &optional unmark)
3626   "Mark the current group."
3627   (interactive "p")
3628   (let ((buffer-read-only nil)
3629         group)
3630     (while 
3631         (and (> n 0) 
3632              (setq group (gnus-group-group-name))
3633              (progn
3634                (beginning-of-line)
3635                (forward-char 2)
3636                (delete-char 1)
3637                (if unmark
3638                    (progn
3639                      (insert " ")
3640                      (setq gnus-group-marked (delete group gnus-group-marked)))
3641                  (insert "#")
3642                  (setq gnus-group-marked
3643                        (cons group (delete group gnus-group-marked))))
3644                t)
3645              (zerop (gnus-group-next-group 1)))
3646       (setq n (1- n)))
3647     (gnus-summary-position-cursor)
3648     n))
3649
3650 (defun gnus-group-unmark-group (n)
3651   "Remove the mark from the current group."
3652   (interactive "p")
3653   (gnus-group-mark-group n 'unmark))
3654
3655 (defun gnus-group-mark-region (unmark beg end)
3656   "Mark all groups between point and mark.
3657 If UNMARK, remove the mark instead."
3658   (interactive "P\nr")
3659   (let ((num (count-lines beg end)))
3660     (save-excursion
3661       (goto-char beg)
3662       (- num (gnus-group-mark-group num unmark)))))
3663
3664 (defun gnus-group-remove-mark (group)
3665   (and (gnus-group-goto-group group)
3666        (save-excursion
3667          (gnus-group-mark-group 1 'unmark))))
3668
3669 ;; Return a list of groups to work on.  Take into consideration N (the
3670 ;; prefix) and the list of marked groups.
3671 (defun gnus-group-process-prefix (n)
3672   (cond (n
3673          (setq n (prefix-numeric-value n))
3674          ;; There is a prefix, so we return a list of the N next
3675          ;; groups. 
3676          (let ((way (if (< n 0) -1 1))
3677                (n (abs n))
3678                group groups)
3679            (save-excursion
3680              (while (and (> n 0)
3681                          (setq group (gnus-group-group-name)))
3682                (setq groups (cons group groups))
3683                (setq n (1- n))
3684                (forward-line way)))
3685            (nreverse groups)))
3686         (gnus-group-marked
3687          ;; No prefix, but a list of marked articles.
3688          (reverse gnus-group-marked))
3689         (t
3690          ;; Neither marked articles or a prefix, so we return the
3691          ;; current group.
3692          (let ((group (gnus-group-group-name)))
3693            (and group (list group))))))
3694
3695 ;; Selecting groups.
3696
3697 (defun gnus-group-read-group (all &optional no-article group)
3698   "Read news in this newsgroup.
3699 If argument ALL is non-nil, already read articles become readable.
3700 If optional argument NO-ARTICLE is non-nil, no article body is displayed."
3701   (interactive "P")
3702   (let ((group (or group (gnus-group-group-name)))
3703         number active marked entry)
3704     (or group (error "No group on current line"))
3705     (setq marked 
3706           (nth 3 (nth 2 (setq entry (gnus-gethash group gnus-newsrc-hashtb)))))
3707     ;; This group might be a dead group. In that case we have to get
3708     ;; the number of unread articles from `gnus-active-hashtb'.
3709     (if entry
3710         (setq number (car entry))
3711       (if (setq active (gnus-gethash group gnus-active-hashtb))
3712           (setq number (- (1+ (cdr active)) (car active)))))
3713     (gnus-summary-read-group 
3714      group (or all (and (numberp number) 
3715                         (zerop (+ number (length (cdr (assq 'tick marked)))
3716                                   (length (cdr (assq 'dormant marked)))))))
3717      no-article)))
3718
3719 (defun gnus-group-select-group (all)
3720   "Select this newsgroup.
3721 No article is selected automatically.
3722 If argument ALL is non-nil, already read articles become readable."
3723   (interactive "P")
3724   (gnus-group-read-group all t))
3725
3726 ;; Enter a group that is not in the group buffer. Non-nil is returned
3727 ;; if selection was successful.
3728 (defun gnus-group-read-ephemeral-group 
3729   (group method &optional activate quit-config)
3730   (let ((group (if (gnus-group-foreign-p group) group
3731                  (gnus-group-prefixed-name group method))))
3732     (gnus-sethash 
3733      group
3734      (list t nil (list group gnus-level-default-subscribed nil nil 
3735                        (append method
3736                                (list
3737                                 (list 'quit-config 
3738                                       (if quit-config quit-config
3739                                         (cons (current-buffer) 'summary)))))))
3740      gnus-newsrc-hashtb)
3741     (set-buffer gnus-group-buffer)
3742     (or (gnus-server-opened method)
3743         (gnus-open-server method)
3744         (error "Unable to contact server: %s" (gnus-status-message method)))
3745     (if activate (gnus-request-group group))
3746     (condition-case ()
3747         (gnus-group-read-group t t group)
3748       (error nil)
3749       (quit nil))
3750     (not (equal major-mode 'gnus-group-mode))))
3751   
3752 (defun gnus-group-jump-to-group (group)
3753   "Jump to newsgroup GROUP."
3754   (interactive 
3755    (list (completing-read 
3756           "Group: " gnus-active-hashtb nil (not (not gnus-read-active-file)))))
3757
3758   (if (equal group "")
3759       (error "Empty group name"))
3760
3761   (let ((b (text-property-any 
3762             (point-min) (point-max) 'gnus-group (intern group))))
3763     (if b
3764         ;; Either go to the line in the group buffer...
3765         (goto-char b)
3766       ;; ... or insert the line.
3767       (or
3768        (gnus-gethash group gnus-active-hashtb)
3769        (gnus-activate-newsgroup group)
3770        (error "%s error: %s" group (gnus-status-message group)))
3771
3772       (gnus-group-update-group group)
3773       (goto-char (text-property-any 
3774                   (point-min) (point-max) 'gnus-group (intern group)))))
3775   ;; Adjust cursor point.
3776   (gnus-group-position-cursor))
3777
3778 (defun gnus-group-goto-group (group)
3779   "Goto to newsgroup GROUP."
3780   (let ((b (text-property-any (point-min) (point-max) 
3781                               'gnus-group (intern group))))
3782     (and b (goto-char b))))
3783
3784 (defun gnus-group-next-group (n)
3785   "Go to next N'th newsgroup.
3786 If N is negative, search backward instead.
3787 Returns the difference between N and the number of skips actually
3788 done."
3789   (interactive "p")
3790   (gnus-group-next-unread-group n t))
3791
3792 (defun gnus-group-next-unread-group (n &optional all level)
3793   "Go to next N'th unread newsgroup.
3794 If N is negative, search backward instead.
3795 If ALL is non-nil, choose any newsgroup, unread or not.
3796 If LEVEL is non-nil, choose the next group with level LEVEL, or, if no
3797 such group can be found, the next group with a level higher than
3798 LEVEL.
3799 Returns the difference between N and the number of skips actually
3800 made."
3801   (interactive "p")
3802   (let ((backward (< n 0))
3803         (n (abs n)))
3804     (while (and (> n 0)
3805                 (gnus-group-search-forward 
3806                  backward (or (not gnus-group-goto-unread) all) level))
3807       (setq n (1- n)))
3808     (if (/= 0 n) (gnus-message 7 "No more%s newsgroups%s" (if all "" " unread")
3809                                (if level " on this level or higher" "")))
3810     n))
3811
3812 (defun gnus-group-prev-group (n)
3813   "Go to previous N'th newsgroup.
3814 Returns the difference between N and the number of skips actually
3815 done."
3816   (interactive "p")
3817   (gnus-group-next-unread-group (- n) t))
3818
3819 (defun gnus-group-prev-unread-group (n)
3820   "Go to previous N'th unread newsgroup.
3821 Returns the difference between N and the number of skips actually
3822 done."  
3823   (interactive "p")
3824   (gnus-group-next-unread-group (- n)))
3825
3826 (defun gnus-group-next-unread-group-same-level (n)
3827   "Go to next N'th unread newsgroup on the same level.
3828 If N is negative, search backward instead.
3829 Returns the difference between N and the number of skips actually
3830 done."
3831   (interactive "p")
3832   (gnus-group-next-unread-group n t (gnus-group-group-level))
3833   (gnus-group-position-cursor))
3834
3835 (defun gnus-group-prev-unread-group-same-level (n)
3836   "Go to next N'th unread newsgroup on the same level.
3837 Returns the difference between N and the number of skips actually
3838 done."
3839   (interactive "p")
3840   (gnus-group-next-unread-group (- n) t (gnus-group-group-level))
3841   (gnus-group-position-cursor))
3842
3843 (defun gnus-group-best-unread-group (&optional exclude-group)
3844   "Go to the group with the highest level.
3845 If EXCLUDE-GROUP, do not go to that group."
3846   (interactive)
3847   (goto-char (point-min))
3848   (let ((best 100000)
3849         unread best-point)
3850     (while (setq unread (get-text-property (point) 'gnus-unread))
3851       (if (and (numberp unread) (> unread 0))
3852           (progn
3853             (or best-point (setq best-point (point)))
3854             (if (and (< (get-text-property (point) 'gnus-level) best)
3855                      (or (not exclude-group)
3856                          (not (equal exclude-group (gnus-group-group-name)))))
3857                 (progn 
3858                   (setq best (get-text-property (point) 'gnus-level))
3859                   (setq best-point (point))))))
3860       (forward-line 1))
3861     (if best-point (goto-char best-point))
3862     (gnus-summary-position-cursor)
3863     (and best-point (gnus-group-group-name))))
3864
3865 (defun gnus-group-first-unread-group ()
3866   "Go to the first group with unread articles."
3867   (interactive)
3868   (goto-char (point-min))
3869   (or (not (zerop (or (get-text-property (point) 'gnus-unread) 0)))
3870       (gnus-group-next-unread-group 1))
3871   (gnus-group-position-cursor))
3872
3873 (defun gnus-group-enter-server-mode ()
3874   "Jump to the server buffer."
3875   (interactive)
3876   (gnus-server-setup-buffer)
3877   (gnus-configure-windows 'server)
3878   (gnus-server-prepare))
3879
3880 (defun gnus-group-make-group (name method &optional address)
3881   "Add a new newsgroup.
3882 The user will be prompted for a NAME, for a select METHOD, and an
3883 ADDRESS."
3884   (interactive
3885    (cons 
3886     (read-string "Group name: ")
3887     (let ((method
3888            (completing-read 
3889             "Method: " (append gnus-valid-select-methods gnus-server-alist)
3890             nil t)))
3891       (if (assoc method gnus-valid-select-methods)
3892           (list method
3893                 (if (memq 'prompt-address
3894                           (assoc method gnus-valid-select-methods))
3895                     (read-string "Address: ")
3896                   ""))
3897         (list method nil)))))
3898   
3899   (let* ((meth (if address (list (intern method) address) method))
3900          (nname (gnus-group-prefixed-name name meth))
3901          info)
3902     (and (gnus-gethash nname gnus-newsrc-hashtb)
3903          (error "Group %s already exists" nname))
3904     (gnus-group-change-level 
3905      (setq info (list t nname gnus-level-default-subscribed nil nil meth))
3906      gnus-level-default-subscribed gnus-level-killed 
3907      (and (gnus-group-group-name)
3908           (gnus-gethash (gnus-group-group-name)
3909                         gnus-newsrc-hashtb))
3910      t)
3911     (gnus-sethash nname '(0 . 0) gnus-active-hashtb)
3912     (gnus-dribble-enter 
3913      (concat "(gnus-group-set-info '" (prin1-to-string (cdr info)) ")"))
3914     (gnus-group-insert-group-line-info nname)
3915
3916     (and (gnus-check-backend-function 'request-create-group nname)
3917          (gnus-request-create-group nname))))
3918
3919 (defun gnus-group-edit-group (group &optional part)
3920   "Edit the group on the current line."
3921   (interactive (list (gnus-group-group-name)))
3922   (let ((done-func '(lambda () 
3923                       "Exit editing mode and update the information."
3924                       (interactive)
3925                       (gnus-group-edit-group-done 'part 'group)))
3926         (part (or part 'info))
3927         (winconf (current-window-configuration))
3928         info)
3929     (or group (error "No group on current line"))
3930     (or (setq info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
3931         (error "Killed group; can't be edited"))
3932     (set-buffer (get-buffer-create gnus-group-edit-buffer))
3933     (gnus-configure-windows 'edit-group)
3934     (gnus-add-current-to-buffer-list)
3935     (emacs-lisp-mode)
3936     ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3937     (use-local-map (copy-keymap emacs-lisp-mode-map))
3938     (local-set-key "\C-c\C-c" done-func)
3939     (make-local-variable 'gnus-prev-winconf)
3940     (setq gnus-prev-winconf winconf)
3941     ;; We modify the func to let it know what part it is editing.
3942     (setcar (cdr (nth 4 done-func)) (list 'quote part))
3943     (setcar (cdr (cdr (nth 4 done-func))) group)
3944     (erase-buffer)
3945     (insert
3946      (cond 
3947       ((eq part 'method)
3948        ";; Type `C-c C-c' after editing the select method.\n\n")
3949       ((eq part 'params)
3950        ";; Type `C-c C-c' after editing the group parameters.\n\n")
3951       ((eq part 'info)
3952        ";; Type `C-c C-c' after editing the group info.\n\n")))
3953     (let ((cinfo (gnus-copy-sequence info))
3954           marked)
3955       (if (not (setq marked (nth 3 cinfo)))
3956           ()
3957         (while marked
3958           (or (eq 'score (car (car marked)))
3959               (eq 'bookmark (car (car marked)))
3960               (eq 'killed (car (car marked)))
3961               (setcdr (car marked) 
3962                       (gnus-compress-sequence (sort (cdr (car marked)) '<) t)))
3963           (setq marked (cdr marked))))
3964       (insert 
3965        (pp-to-string
3966         (cond ((eq part 'method)
3967                (or (nth 4 info) "native"))
3968               ((eq part 'params)
3969                (nth 5 info))
3970               (t
3971                cinfo)))
3972        "\n"))))
3973
3974 (defun gnus-group-edit-group-method (group)
3975   "Edit the select method of GROUP."
3976   (interactive (list (gnus-group-group-name)))
3977   (gnus-group-edit-group group 'method))
3978
3979 (defun gnus-group-edit-group-parameters (group)
3980   "Edit the group parameters of GROUP."
3981   (interactive (list (gnus-group-group-name)))
3982   (gnus-group-edit-group group 'params))
3983
3984 (defun gnus-group-edit-group-done (part group)
3985   "Get info from buffer, update variables and jump to the group buffer."
3986   (set-buffer (get-buffer-create gnus-group-edit-buffer))
3987   (goto-char (point-min))
3988   (let ((form (read (current-buffer)))
3989         (winconf gnus-prev-winconf))
3990     (if (eq part 'info) 
3991         (gnus-group-set-info form)
3992       (gnus-group-set-info form group part))
3993     (kill-buffer (current-buffer))
3994     (and winconf (set-window-configuration winconf))
3995     (set-buffer gnus-group-buffer)
3996     (gnus-group-update-group (gnus-group-group-name))
3997     (gnus-group-position-cursor)))
3998
3999 (defun gnus-group-make-help-group ()
4000   "Create the (ding) Gnus documentation group."
4001   (interactive)
4002   (let ((path load-path))
4003     (and (gnus-gethash (setq name (gnus-group-prefixed-name
4004                                    "gnus-help" '(nndoc "gnus-help")))
4005                        gnus-newsrc-hashtb)
4006          (error "Documentation group already exists"))
4007     (while (and path
4008                 (not (file-exists-p (concat (file-name-as-directory (car path))
4009                                             "doc.txt"))))
4010       (setq path (cdr path)))
4011     (or path (error "Couldn't find doc group"))
4012     (gnus-group-make-group 
4013      (gnus-group-real-name name)
4014      (list 'nndoc name
4015            (list 'nndoc-address (concat (file-name-as-directory (car path)) "doc.txt"))
4016            (list 'nndoc-article-type 'mbox))))
4017   (gnus-group-position-cursor))
4018
4019 (defun gnus-group-make-doc-group (file type)
4020   "Create a group that uses a single file as the source."
4021   (interactive 
4022    (list (read-file-name "File name: ") 
4023          (let ((err "")
4024                found char)
4025            (while (not found)
4026              (message "%sFile type (mbox, babyl, digest) [mbd]: " err)
4027              (setq found (cond ((= (setq char (read-char)) ?m) 'mbox)
4028                                ((= char ?b) 'babyl)
4029                                ((= char ?d) 'digest)
4030                                (t (setq mess "%c unknown. " char)
4031                                   nil))))
4032            found)))
4033   (let* ((file (expand-file-name file))
4034          (name (gnus-generate-new-group-name
4035                 (gnus-group-prefixed-name
4036                  (file-name-nondirectory file) '(nndoc "")))))
4037     (gnus-group-make-group 
4038      (gnus-group-real-name name)
4039      (list 'nndoc name
4040            (list 'nndoc-address file)
4041            (list 'nndoc-article-type type)))))
4042
4043 (defun gnus-group-make-archive-group ()
4044   "Create the (ding) Gnus archive group."
4045   (interactive)
4046   (and (gnus-gethash (gnus-group-prefixed-name "ding.archives" '(nndir ""))
4047                      gnus-newsrc-hashtb)
4048        (error "Archive group already exists"))
4049   (gnus-group-make-group "ding.archives" "nndir" gnus-group-archive-directory)
4050   (gnus-group-position-cursor))
4051
4052 (defun gnus-group-make-directory-group (dir)
4053   "Create an nndir group.
4054 The user will be prompted for a directory. The contents of this
4055 directory will be used as a newsgroup. The directory should contain
4056 mail messages or news articles in files that have numeric names."
4057   (interactive
4058    (list (read-file-name "Create group from directory: ")))
4059   (or (file-exists-p dir) (error "No such directory"))
4060   (or (file-directory-p dir) (error "Not a directory"))
4061   (gnus-group-make-group dir "nndir" dir)
4062   (gnus-group-position-cursor))
4063
4064 (defun gnus-group-make-kiboze-group (group address scores)
4065   "Create an nnkiboze group.
4066 The user will be prompted for a name, a regexp to match groups, and
4067 score file entries for articles to include in the group."
4068   (interactive
4069    (list
4070     (read-string "nnkiboze group name: ")
4071     (read-string "Source groups (regexp): ")
4072     (let ((headers (mapcar (lambda (group) (list group))
4073                            '("subject" "from" "number" "date" "message-id"
4074                              "references" "chars" "lines" "xref")))
4075           scores header regexp regexps)
4076       (while (not (equal "" (setq header (completing-read 
4077                                           "Match on header: " headers nil t))))
4078         (setq regexps nil)
4079         (while (not (equal "" (setq regexp (read-string 
4080                                             (format "Match on %s (string): "
4081                                                     header)))))
4082           (setq regexps (cons (list regexp nil nil 'r) regexps)))
4083         (setq scores (cons (cons header regexps) scores)))
4084       scores)))
4085   (gnus-group-make-group group "nnkiboze" address)
4086   (save-excursion
4087     (gnus-set-work-buffer)
4088     (let (emacs-lisp-mode-hook)
4089       (pp scores (current-buffer)))
4090     (write-region (point-min) (point-max) 
4091                   (concat (or gnus-kill-files-directory "~/News")
4092                           "nnkiboze:" group "." gnus-score-file-suffix)))
4093   (gnus-group-position-cursor))
4094
4095 (defun gnus-group-add-to-virtual (n vgroup)
4096   "Add the current group to a virtual group."
4097   (interactive
4098    (list current-prefix-arg
4099          (completing-read "Add to virtual group: " gnus-newsrc-hashtb nil t
4100                           "nnvirtual:")))
4101   (or (eq (car (gnus-find-method-for-group vgroup)) 'nnvirtual)
4102       (error "%s is not an nnvirtual group" vgroup))
4103   (let* ((groups (gnus-group-process-prefix n))
4104          (method (nth 4 (nth 2 (gnus-gethash vgroup gnus-newsrc-hashtb)))))
4105     (setcar (cdr method)
4106             (concat 
4107              (nth 1 method) "\\|"
4108              (mapconcat 
4109               (lambda (s) 
4110                 (gnus-group-remove-mark s)
4111                 (concat "\\(^" (regexp-quote s) "$\\)"))
4112               groups "\\|"))))
4113   (gnus-group-position-cursor))
4114
4115 (defun gnus-group-make-empty-virtual (group)
4116   "Create a new, fresh, empty virtual group."
4117   (interactive "sCreate new, empty virtual group: ")
4118   (let* ((method (list 'nnvirtual "^$"))
4119          (pgroup (gnus-group-prefixed-name group method)))
4120     ;; Check whether it exists already.
4121     (and (gnus-gethash pgroup gnus-newsrc-hashtb)
4122          (error "Group %s already exists." pgroup))
4123     ;; Subscribe the new group after the group on the current line.
4124     (gnus-subscribe-group pgroup (gnus-group-group-name) method)
4125     (gnus-group-update-group pgroup)
4126     (forward-line -1)
4127     (gnus-group-position-cursor)))
4128
4129 (defun gnus-group-enter-directory (dir)
4130   "Enter an ephemeral nneething group."
4131   (interactive "DDirectory to read: ")
4132   (let* ((method (list 'nneething dir))
4133          (leaf (gnus-group-prefixed-name
4134                 (file-name-nondirectory (directory-file-name dir))
4135                 method))
4136          (name (gnus-generate-new-group-name leaf))
4137          (num 0))
4138     (let ((nneething-read-only t))
4139       (or (gnus-group-read-ephemeral-group 
4140            name method t
4141            (cons (current-buffer) (if (eq major-mode 'gnus-summary-mode)
4142                                       'summary 'group)))
4143           (error "Couldn't enter %s" dir)))))
4144
4145 ;; Group sorting commands
4146 ;; Suggested by Joe Hildebrand <hildjj@idaho.fuentez.com>.
4147
4148 (defun gnus-group-sort-groups ()
4149   "Sort the group buffer using `gnus-group-sort-function'."
4150   (interactive)
4151   (setq gnus-newsrc-alist 
4152         (sort (cdr gnus-newsrc-alist) gnus-group-sort-function))
4153   (gnus-make-hashtable-from-newsrc-alist)
4154   (gnus-group-list-groups nil gnus-have-all-newsgroups))
4155
4156 (defun gnus-group-sort-by-alphabet (info1 info2)
4157   (string< (car info1) (car info2)))
4158
4159 (defun gnus-group-sort-by-unread (info1 info2)
4160   (let ((n1 (car (gnus-gethash (car info1) gnus-newsrc-hashtb)))
4161         (n2 (car (gnus-gethash (car info2) gnus-newsrc-hashtb))))
4162     (< (or (and (numberp n1) n1) 0)
4163        (or (and (numberp n2) n2) 0))))
4164
4165 (defun gnus-group-sort-by-level (info1 info2)
4166   (< (nth 1 info1) (nth 1 info2)))
4167
4168 ;; Group catching up.
4169
4170 (defun gnus-group-catchup-current (n &optional all)
4171   "Mark all articles not marked as unread in current newsgroup as read.
4172 If prefix argument N is numeric, the ARG next newsgroups will be
4173 caught up. If ALL is non-nil, marked articles will also be marked as
4174 read. Cross references (Xref: header) of articles are ignored.
4175 The difference between N and actual number of newsgroups that were
4176 caught up is returned."
4177   (interactive "P")
4178   (if (not (or (not gnus-interactive-catchup) ;Without confirmation?
4179                gnus-expert-user
4180                (gnus-y-or-n-p
4181                 (if all
4182                     "Do you really want to mark all articles as read? "
4183                   "Mark all unread articles as read? "))))
4184       n
4185     (let ((groups (gnus-group-process-prefix n))
4186           (ret 0))
4187       (while groups
4188         ;; Virtual groups have to be given special treatment. 
4189         (let ((method (gnus-find-method-for-group (car groups))))
4190           (if (eq 'nnvirtual (car method))
4191               (nnvirtual-catchup-group
4192                (gnus-group-real-name (car groups)) (nth 1 method) all)))
4193         (gnus-group-remove-mark (car groups))
4194         (if (prog1
4195                 (gnus-group-goto-group (car groups))
4196               (gnus-group-catchup (car groups) all))
4197             (gnus-group-update-group-line)
4198           (setq ret (1+ ret)))
4199         (setq groups (cdr groups)))
4200       (gnus-group-next-unread-group 1)
4201       ret)))
4202
4203 (defun gnus-group-catchup-current-all (n)
4204   "Mark all articles in current newsgroup as read.
4205 Cross references (Xref: header) of articles are ignored."
4206   (interactive "P")
4207   (gnus-group-catchup-current n 'all))
4208
4209 (defun gnus-group-catchup (group &optional all)
4210   "Mark all articles in GROUP as read.
4211 If ALL is non-nil, all articles are marked as read.
4212 The return value is the number of articles that were marked as read,
4213 or nil if no action could be taken."
4214   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4215          (num (car entry))
4216          (marked (nth 3 (nth 2 entry)))
4217          ticked)
4218     (if (not (numberp (car entry)))
4219         (gnus-message 1 "Can't catch up; non-active group")
4220       ;; Do the updating only if the newsgroup isn't killed.
4221       (if (not entry)
4222           ()
4223         (gnus-update-read-articles 
4224          group (and (not all) (append (cdr (assq 'tick marked))
4225                                       (cdr (assq 'dormant marked))))
4226          nil (and (not all) (cdr (assq 'tick marked))))
4227         (and all marked
4228              (setcar (nthcdr 3 (nth 2 entry)) 
4229                      (delq (assq 'dormant marked) 
4230                            (nth 3 (nth 2 entry)))))))
4231     num))
4232
4233 (defun gnus-group-expire-articles (n)
4234   "Expire all expirable articles in the current newsgroup."
4235   (interactive "P")
4236   (let ((groups (gnus-group-process-prefix n))
4237         group)
4238     (or groups (error "No groups to expire"))
4239     (while groups
4240       (setq group (car groups)
4241             groups (cdr groups))
4242       (gnus-group-remove-mark group)
4243       (if (not (gnus-check-backend-function 'request-expire-articles group))
4244           ()
4245         (let* ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
4246                (expirable (if (memq 'total-expire (nth 5 info))
4247                               (cons nil (gnus-list-of-read-articles group))
4248                             (assq 'expire (nth 3 info)))))
4249           (and expirable 
4250                (setcdr expirable
4251                        (gnus-request-expire-articles 
4252                         (cdr expirable) group))))))))
4253
4254 (defun gnus-group-expire-all-groups ()
4255   "Expire all expirable articles in all newsgroups."
4256   (interactive)
4257   (gnus-message 5 "Expiring...")
4258   (let ((gnus-group-marked (mapcar (lambda (info) (car info))
4259                                    (cdr gnus-newsrc-alist))))
4260     (gnus-group-expire-articles nil))
4261   (gnus-message 5 "Expiring...done"))
4262
4263 (defun gnus-group-set-current-level (n level)
4264   "Set the level of the next N groups to LEVEL."
4265   (interactive "P\nnLevel: ")
4266   (or (and (>= level 1) (<= level gnus-level-killed))
4267       (error "Illegal level: %d" level))
4268   (let ((groups (gnus-group-process-prefix n))
4269         group)
4270     (while groups
4271       (setq group (car groups)
4272             groups (cdr groups))
4273       (gnus-group-remove-mark group)
4274       (gnus-message 6 "Changed level of %s from %d to %d" 
4275                     group (gnus-group-group-level) level)
4276       (gnus-group-change-level group level
4277                                (gnus-group-group-level))
4278       (gnus-group-update-group-line)))
4279   (gnus-group-position-cursor))
4280
4281 (defun gnus-group-unsubscribe-current-group (arg)
4282   "Toggle subscribe from/to unsubscribe current group."
4283   (interactive "P")
4284   (let ((group (gnus-group-group-name)))
4285     (or group (error "No newsgroup on current line"))
4286     (or arg (setq arg (if (<= (gnus-group-group-level) gnus-level-subscribed)
4287                           gnus-level-default-unsubscribed
4288                         gnus-level-default-subscribed)))
4289     (gnus-group-unsubscribe-group group arg)
4290     (gnus-group-next-group 1)))
4291
4292 (defun gnus-group-unsubscribe-group (group &optional level)
4293   "Toggle subscribe from/to unsubscribe GROUP.
4294 New newsgroup is added to .newsrc automatically."
4295   (interactive
4296    (list (completing-read "Group: " gnus-active-hashtb nil 
4297                           gnus-have-read-active-file)))
4298   (let ((newsrc (gnus-gethash group gnus-newsrc-hashtb)))
4299     (cond (newsrc
4300            ;; Toggle subscription flag.
4301            (gnus-group-change-level 
4302             newsrc (if level level (if (<= (nth 1 (nth 2 newsrc)) 
4303                                            gnus-level-subscribed) 
4304                                        (1+ gnus-level-subscribed)
4305                                      gnus-level-default-subscribed)))
4306            (gnus-group-update-group group))
4307           ((and (stringp group)
4308                 (or (not gnus-have-read-active-file)
4309                     (gnus-gethash group gnus-active-hashtb)))
4310            ;; Add new newsgroup.
4311            (gnus-group-change-level 
4312             group 
4313             (if level level gnus-level-default-subscribed) 
4314             (or (and (member group gnus-zombie-list) 
4315                      gnus-level-zombie) 
4316                 gnus-level-killed)
4317             (and (gnus-group-group-name)
4318                  (gnus-gethash (gnus-group-group-name) gnus-newsrc-hashtb)))
4319            (gnus-group-update-group group))
4320           (t (error "No such newsgroup: %s" group)))
4321     (gnus-group-position-cursor)))
4322
4323 (defun gnus-group-transpose-groups (n)
4324   "Move the current newsgroup up N places.
4325 If given a negative prefix, move down instead. The difference between
4326 N and the number of steps taken is returned." 
4327   (interactive "p")
4328   (or (gnus-group-group-name)
4329       (error "No group on current line"))
4330   (gnus-group-kill-group 1)
4331   (prog1
4332       (forward-line (- n))
4333     (gnus-group-yank-group)
4334     (gnus-group-position-cursor)))
4335
4336 (defun gnus-group-kill-all-zombies ()
4337   "Kill all zombie newsgroups."
4338   (interactive)
4339   (setq gnus-killed-list (nconc gnus-zombie-list gnus-killed-list))
4340   (setq gnus-zombie-list nil)
4341   (funcall gnus-group-prepare-function gnus-level-subscribed nil nil)
4342   (goto-char (point-min))
4343   (gnus-group-position-cursor))
4344
4345 (defun gnus-group-kill-region (begin end)
4346   "Kill newsgroups in current region (excluding current point).
4347 The killed newsgroups can be yanked by using \\[gnus-group-yank-group]."
4348   (interactive "r")
4349   (let ((lines
4350          ;; Exclude a line where current point is on.
4351          (1-
4352           ;; Count lines.
4353           (save-excursion
4354             (count-lines
4355              (progn
4356                (goto-char begin)
4357                (beginning-of-line)
4358                (point))
4359              (progn
4360                (goto-char end)
4361                (end-of-line)
4362                (point)))))))
4363     (goto-char begin)
4364     (beginning-of-line)                 ;Important when LINES < 1
4365     (gnus-group-kill-group lines)))
4366
4367 (defun gnus-group-kill-group (n)
4368   "The the next N groups.
4369 The killed newsgroups can be yanked by using \\[gnus-group-yank-group].
4370 However, only groups that were alive can be yanked; already killed 
4371 groups or zombie groups can't be yanked.
4372 The return value is the name of the (last) group that was killed."
4373   (interactive "P")
4374   (let ((buffer-read-only nil)
4375         (groups (gnus-group-process-prefix n))
4376         group entry level)
4377     (while groups
4378       (setq group (car groups)
4379             groups (cdr groups))
4380       (gnus-group-remove-mark group)
4381       (setq level (gnus-group-group-level))
4382       (gnus-delete-line)
4383       (if (setq entry (gnus-gethash group gnus-newsrc-hashtb))
4384           (setq gnus-list-of-killed-groups 
4385                 (cons (cons (car entry) (nth 2 entry)) 
4386                       gnus-list-of-killed-groups)))
4387       (gnus-group-change-level 
4388        (if entry entry group) gnus-level-killed (if entry nil level)))
4389     (gnus-group-position-cursor)
4390     group))
4391
4392 (defun gnus-group-yank-group (&optional arg)
4393   "Yank the last newsgroups killed with \\[gnus-group-kill-group],
4394 inserting it before the current newsgroup.  The numeric ARG specifies
4395 how many newsgroups are to be yanked.  The name of the (last)
4396 newsgroup yanked is returned."
4397   (interactive "p")
4398   (if (not arg) (setq arg 1))
4399   (let (info group prev)
4400     (while (>= (setq arg (1- arg)) 0)
4401       (if (not (setq info (car gnus-list-of-killed-groups)))
4402           (error "No more newsgroups to yank"))
4403       (setq group (nth 2 info))
4404       ;; Find which newsgroup to insert this one before - search
4405       ;; backward until something suitable is found. If there are no
4406       ;; other newsgroups in this buffer, just make this newsgroup the
4407       ;; first newsgroup.
4408       (setq prev (gnus-group-group-name))
4409       (gnus-group-change-level 
4410        info (nth 2 info) gnus-level-killed 
4411        (and prev (gnus-gethash prev gnus-newsrc-hashtb))
4412        t)
4413       (gnus-group-insert-group-line-info (nth 1 info))
4414       (setq gnus-list-of-killed-groups 
4415             (cdr gnus-list-of-killed-groups)))
4416     (forward-line -1)
4417     (gnus-group-position-cursor)
4418     group))
4419       
4420 (defun gnus-group-list-all-groups (arg)
4421   "List all newsgroups with level ARG or lower.
4422 Default is gnus-level-unsubscribed, which lists all subscribed and most
4423 unsubscribed groups."
4424   (interactive "P")
4425   (setq arg (or arg gnus-level-unsubscribed))
4426   (gnus-group-list-groups arg t))
4427
4428 (defun gnus-group-list-killed ()
4429   "List all killed newsgroups in the group buffer."
4430   (interactive)
4431   (if (not gnus-killed-list)
4432       (gnus-message 6 "No killed groups")
4433     (funcall gnus-group-prepare-function gnus-level-killed t gnus-level-killed)
4434     (goto-char (point-min)))
4435   (gnus-group-position-cursor))
4436
4437 (defun gnus-group-list-zombies ()
4438   "List all zombie newsgroups in the group buffer."
4439   (interactive)
4440   (if (not gnus-zombie-list)
4441       (gnus-message 6 "No zombie groups")
4442     (funcall gnus-group-prepare-function gnus-level-zombie t gnus-level-zombie)
4443     (goto-char (point-min)))
4444   (gnus-group-position-cursor))
4445
4446 (defun gnus-group-get-new-news (&optional arg)
4447   "Get newly arrived articles.
4448 If ARG is non-nil, it should be a number between one and nine to
4449 specify which levels you are interested in re-scanning."
4450   (interactive "P")
4451   (run-hooks 'gnus-get-new-news-hook)
4452   (let ((level arg))
4453     (if gnus-group-use-permanent-levels
4454         (if level
4455             (setq gnus-group-default-list-level level)
4456           (setq level (or gnus-group-default-list-level 
4457                           gnus-level-subscribed))))
4458     (if (and gnus-read-active-file (not level))
4459         (progn
4460           (gnus-read-active-file)
4461           (gnus-get-unread-articles (or level (1+ gnus-level-subscribed))))
4462       (let ((gnus-read-active-file nil))
4463         (gnus-get-unread-articles (or level (1+ gnus-level-subscribed)))))
4464     (gnus-group-list-groups (or (and gnus-group-use-permanent-levels level)
4465                                 gnus-group-default-list-level
4466                                 gnus-level-subscribed)
4467                             gnus-have-all-newsgroups)))
4468
4469 (defun gnus-group-get-new-news-this-group (n)
4470   "Check for newly arrived news in the current group (and the N-1 next groups).
4471 The difference between N and the number of newsgroup checked is returned.
4472 If N is negative, this group and the N-1 previous groups will be checked."
4473   (interactive "P")
4474   (let* ((groups (gnus-group-process-prefix n))
4475          (ret (if (numberp n) (- n (length groups)) 0))
4476          (w-p (window-start))
4477          group)
4478     (while groups
4479       (setq group (car groups)
4480             groups (cdr groups))
4481       (gnus-group-remove-mark group)
4482       (or (gnus-get-new-news-in-group group)
4483           (progn 
4484             (ding) 
4485             (message "%s error: %s" group (gnus-status-message group))
4486             (sit-for 2))))
4487     ;; !!! I don't know why the buffer scrolls forward when updating
4488     ;; the first line in the group buffer, but it does. So we set the
4489     ;; window start forcibly.
4490 ;    (set-window-start (get-buffer-window (current-buffer)) w-p)
4491     (gnus-group-next-unread-group 1 t)
4492     (gnus-summary-position-cursor)
4493     ret))
4494
4495 (defun gnus-get-new-news-in-group (group)
4496   (and group 
4497        (gnus-activate-newsgroup group)
4498        (progn
4499          (gnus-get-unread-articles-in-group 
4500           (nth 2 (gnus-gethash group gnus-newsrc-hashtb))
4501           (gnus-gethash group gnus-active-hashtb))
4502          (gnus-group-update-group-line)
4503          t)))
4504
4505 (defun gnus-group-fetch-faq (group)
4506   "Fetch the FAQ for the current group."
4507   (interactive (list (gnus-group-real-name (gnus-group-group-name))))
4508   (or group (error "No group name given"))
4509   (let ((file (concat gnus-group-faq-directory group))) 
4510     (if (not (file-exists-p file))
4511         (error "No such file: %s" file)
4512       (find-file file))))
4513   
4514 (defun gnus-group-describe-group (force &optional group)
4515   "Display a description of the current newsgroup."
4516   (interactive (list current-prefix-arg (gnus-group-group-name)))
4517   (and force (setq gnus-description-hashtb nil))
4518   (let ((method (gnus-find-method-for-group group))
4519         desc)
4520     (or group (error "No group name given"))
4521     (and (or (and gnus-description-hashtb
4522                   ;; We check whether this group's method has been
4523                   ;; queried for a description file.  
4524                   (gnus-gethash 
4525                    (gnus-group-prefixed-name "" method) 
4526                    gnus-description-hashtb))
4527              (setq desc (gnus-group-get-description group))
4528              (gnus-read-descriptions-file method))
4529          (message
4530           (or desc (gnus-gethash group gnus-description-hashtb)
4531               "No description available")))))
4532
4533 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
4534 (defun gnus-group-describe-all-groups (force)
4535   "Pop up a buffer with descriptions of all newsgroups."
4536   (interactive "P")
4537   (and force (setq gnus-description-hashtb nil))
4538   (if (not (or gnus-description-hashtb
4539                (gnus-read-all-descriptions-files)))
4540       (error "Couldn't request descriptions file"))
4541   (let ((buffer-read-only nil)
4542         b)
4543     (erase-buffer)
4544     (mapatoms
4545      (lambda (group)
4546        (setq b (point))
4547        (insert (format "      *: %-20s %s\n" (symbol-name group)
4548                        (symbol-value group)))
4549        (add-text-properties 
4550         b (1+ b) (list 'gnus-group group
4551                        'gnus-unread t 'gnus-marked nil
4552                        'gnus-level (1+ gnus-level-subscribed))))
4553      gnus-description-hashtb)
4554     (goto-char (point-min))
4555     (gnus-group-position-cursor)))
4556
4557 ;; Suggested by by Daniel Quinlan <quinlan@best.com>.
4558 (defun gnus-group-apropos (regexp &optional search-description)
4559   "List all newsgroups that have names that match a regexp."
4560   (interactive "sGnus apropos (regexp): ")
4561   (let ((prev "")
4562         (obuf (current-buffer))
4563         groups des prev)
4564     ;; Go through all newsgroups that are known to Gnus.
4565     (mapatoms 
4566      (lambda (group)
4567        (and (string-match regexp (symbol-name group))
4568             (setq groups (cons (symbol-name group) groups))))
4569      gnus-active-hashtb)
4570     ;; Go through all descriptions that are known to Gnus. 
4571     (if search-description
4572         (mapatoms 
4573          (lambda (group)
4574            (and (string-match regexp (symbol-value group))
4575                 (gnus-gethash (symbol-name group) gnus-active-hashtb)
4576                 (setq groups (cons (symbol-name group) groups))))
4577          gnus-description-hashtb))
4578     (if (not groups)
4579         (gnus-message 3 "No groups matched \"%s\"." regexp)
4580       ;; Print out all the groups.
4581       (save-excursion
4582         (pop-to-buffer "*Gnus Help*")
4583         (buffer-disable-undo (current-buffer))
4584         (erase-buffer)
4585         (setq groups (sort groups 'string<))
4586         (while groups
4587           ;; Groups may be entered twice into the list of groups.
4588           (if (not (string= (car groups) prev))
4589               (progn
4590                 (insert (setq prev (car groups)) "\n")
4591                 (if (and gnus-description-hashtb
4592                          (setq des (gnus-gethash (car groups) 
4593                                                  gnus-description-hashtb)))
4594                     (insert "  " des "\n"))))
4595           (setq groups (cdr groups)))
4596         (goto-char (point-min))))
4597     (pop-to-buffer obuf)))
4598
4599 (defun gnus-group-description-apropos (regexp)
4600   "List all newsgroups that have names or descriptions that match a regexp."
4601   (interactive "sGnus description apropos (regexp): ")
4602   (if (not (or gnus-description-hashtb
4603                (gnus-read-all-descriptions-files)))
4604       (error "Couldn't request descriptions file"))
4605   (gnus-group-apropos regexp t))
4606
4607 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
4608 (defun gnus-group-list-matching (level regexp &optional all lowest) 
4609   "List all groups with unread articles that match REGEXP.
4610 If the prefix LEVEL is non-nil, it should be a number that says which
4611 level to cut off listing groups. 
4612 If ALL, also list groups with no unread articles.
4613 If LOWEST, don't list groups with level lower than LOWEST."
4614   (interactive "P\nsList newsgroups matching: ")
4615   (gnus-group-prepare-flat (or level gnus-level-subscribed)
4616                            all (or lowest 1) regexp)
4617   (goto-char (point-min))
4618   (gnus-group-position-cursor))
4619
4620 (defun gnus-group-list-all-matching (level regexp &optional lowest) 
4621   "List all groups that match REGEXP.
4622 If the prefix LEVEL is non-nil, it should be a number that says which
4623 level to cut off listing groups. 
4624 If LOWEST, don't list groups with level lower than LOWEST."
4625   (interactive "P\nsList newsgroups matching: ")
4626   (gnus-group-list-matching (or level gnus-level-killed) regexp t lowest))
4627
4628 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
4629 (defun gnus-group-save-newsrc ()
4630   "Save the Gnus startup files."
4631   (interactive)
4632   (gnus-save-newsrc-file))
4633
4634 (defun gnus-group-restart (&optional arg)
4635   "Force Gnus to read the .newsrc file."
4636   (interactive "P")
4637   (gnus-save-newsrc-file)
4638   (gnus-setup-news 'force)
4639   (gnus-group-list-groups arg gnus-have-all-newsgroups))
4640
4641 (defun gnus-group-read-init-file ()
4642   "Read the Gnus elisp init file."
4643   (interactive)
4644   (gnus-read-init-file))
4645
4646 (defun gnus-group-check-bogus-groups (silent)
4647   "Check bogus newsgroups.
4648 If given a prefix, don't ask for confirmation before removing a bogus
4649 group."
4650   (interactive "P")
4651   (gnus-check-bogus-newsgroups (and (not silent) (not gnus-expert-user)))
4652   (gnus-group-list-groups nil gnus-have-all-newsgroups))
4653
4654 (defun gnus-group-edit-global-kill (article &optional group)
4655   "Edit the global kill file.
4656 If GROUP, edit that local kill file instead."
4657   (interactive "P")
4658   (setq gnus-current-kill-article article)
4659   (gnus-kill-file-edit-file group)
4660   (gnus-message 6
4661    (substitute-command-keys
4662     "Editing a global kill file (Type \\[gnus-kill-file-exit] to exit)")))
4663
4664 (defun gnus-group-edit-local-kill (article group)
4665   "Edit a local kill file."
4666   (interactive (list nil (gnus-group-group-name)))
4667   (gnus-group-edit-global-kill article group))
4668
4669 (defun gnus-group-force-update ()
4670   "Update `.newsrc' file."
4671   (interactive)
4672   (gnus-save-newsrc-file))
4673
4674 (defun gnus-group-suspend ()
4675   "Suspend the current Gnus session.
4676 In fact, cleanup buffers except for group mode buffer.
4677 The hook gnus-suspend-gnus-hook is called before actually suspending."
4678   (interactive)
4679   (run-hooks 'gnus-suspend-gnus-hook)
4680   ;; Kill Gnus buffers except for group mode buffer.
4681   (let ((group-buf (get-buffer gnus-group-buffer)))
4682     ;; Do this on a separate list in case the user does a ^G before we finish
4683     (let ((gnus-buffer-list
4684            (delq group-buf (delq gnus-dribble-buffer
4685                                  (append gnus-buffer-list nil)))))
4686       (while gnus-buffer-list
4687         (gnus-kill-buffer (car gnus-buffer-list))
4688         (setq gnus-buffer-list (cdr gnus-buffer-list))))
4689     (if group-buf
4690         (progn
4691           (setq gnus-buffer-list (list group-buf))
4692           (bury-buffer group-buf)
4693           (delete-windows-on group-buf t)))))
4694
4695 (defun gnus-group-clear-dribble ()
4696   "Clear all information from the dribble buffer."
4697   (interactive)
4698   (gnus-dribble-clear))
4699
4700 (defun gnus-group-exit ()
4701   "Quit reading news after updating .newsrc.eld and .newsrc.
4702 The hook `gnus-exit-gnus-hook' is called before actually exiting."
4703   (interactive)
4704   (if (or noninteractive                ;For gnus-batch-kill
4705           (zerop (buffer-size))         ;No news is good news.
4706           (not (gnus-server-opened gnus-select-method)) ;NNTP connection closed
4707           (not gnus-interactive-exit)   ;Without confirmation
4708           gnus-expert-user
4709           (gnus-y-or-n-p "Are you sure you want to quit reading news? "))
4710       (progn
4711         (if gnus-use-full-window
4712             (delete-other-windows)
4713           (gnus-remove-some-windows))
4714         (run-hooks 'gnus-exit-gnus-hook)
4715         (gnus-offer-save-summaries)
4716         (gnus-save-newsrc-file)
4717         (gnus-close-backends)
4718         (gnus-clear-system))))
4719
4720 (defun gnus-close-backends ()
4721   ;; Send a close request to all backends that support such a request. 
4722   (let ((methods gnus-valid-select-methods)
4723         func)
4724     (while methods
4725       (if (fboundp (setq func (intern (concat (car (car methods))
4726                                               "-request-close"))))
4727           (funcall func))
4728       (setq methods (cdr methods)))))
4729
4730 (defun gnus-group-quit ()
4731   "Quit reading news without updating .newsrc.eld or .newsrc.
4732 The hook `gnus-exit-gnus-hook' is called before actually exiting."
4733   (interactive)
4734   (if (or noninteractive                ;For gnus-batch-kill
4735           (zerop (buffer-size))
4736           (not (gnus-server-opened gnus-select-method))
4737           gnus-expert-user
4738           (not gnus-current-startup-file)
4739           (gnus-yes-or-no-p
4740            (format "Quit reading news without saving %s? "
4741                    (file-name-nondirectory gnus-current-startup-file))))
4742       (progn
4743         (run-hooks 'gnus-exit-gnus-hook)
4744         (if gnus-use-full-window
4745             (delete-other-windows)
4746           (gnus-remove-some-windows))
4747         (gnus-dribble-save)
4748         (gnus-close-backends)
4749         (gnus-clear-system))))
4750
4751 (defun gnus-offer-save-summaries ()
4752   (let ((buffers (buffer-list)))
4753     (save-excursion
4754       (while buffers
4755         (and 
4756          ;; We look for buffers with "Summary" in the name.
4757          (string-match "Summary" (or (buffer-name (car buffers)) ""))
4758          (progn
4759            (set-buffer (car buffers))
4760            ;; We check that this is, indeed, a summary buffer.
4761            (eq major-mode 'gnus-summary-mode)) 
4762          ;; We ask the user whether she wants to save the info.
4763          (gnus-y-or-n-p
4764                (format "Update summary buffer %s? " (buffer-name)))
4765          ;; We do it by simply exiting.
4766          (gnus-summary-exit))
4767         (setq buffers (cdr buffers))))))
4768
4769 (defun gnus-group-describe-briefly ()
4770   "Give a one line description of the group mode commands."
4771   (interactive)
4772   (gnus-message 6
4773    (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")))
4774
4775 (defun gnus-group-browse-foreign-server (method)
4776   "Browse a foreign news server.
4777 If called interactively, this function will ask for a select method
4778  (nntp, nnspool, etc.) and a server address (eg. nntp.some.where). 
4779 If not, METHOD should be a list where the first element is the method
4780 and the second element is the address."
4781   (interactive
4782    (list (let ((how (completing-read 
4783                      "Which backend: "
4784                      (append gnus-valid-select-methods gnus-server-alist)
4785                      nil t "nntp")))
4786            ;; We either got a backend name or a virtual server name.
4787            ;; If the first, we also need an address.
4788            (if (assoc how gnus-valid-select-methods)
4789                (list (intern how)
4790                      ;; Suggested by mapjph@bath.ac.uk.
4791                      (completing-read 
4792                       "Address: " 
4793                       (mapcar (lambda (server) (list server))
4794                               gnus-secondary-servers)))
4795              ;; We got a server name, so we find the method.
4796              (gnus-server-to-method how)))))
4797   (gnus-browse-foreign-server method))
4798
4799 \f
4800 ;;;
4801 ;;; Browse Server Mode
4802 ;;;
4803
4804 (defvar gnus-browse-mode-hook nil)
4805 (defvar gnus-browse-mode-map nil)
4806 (put 'gnus-browse-mode 'mode-class 'special)
4807
4808 (if gnus-browse-mode-map
4809     nil
4810   (setq gnus-browse-mode-map (make-keymap))
4811   (suppress-keymap gnus-browse-mode-map)
4812   (define-key gnus-browse-mode-map " " 'gnus-browse-read-group)
4813   (define-key gnus-browse-mode-map "=" 'gnus-browse-select-group)
4814   (define-key gnus-browse-mode-map "n" 'gnus-browse-next-group)
4815   (define-key gnus-browse-mode-map "p" 'gnus-browse-prev-group)
4816   (define-key gnus-browse-mode-map "\177" 'gnus-browse-prev-group)
4817   (define-key gnus-browse-mode-map "N" 'gnus-browse-next-group)
4818   (define-key gnus-browse-mode-map "P" 'gnus-browse-prev-group)
4819   (define-key gnus-browse-mode-map "\M-n" 'gnus-browse-next-group)
4820   (define-key gnus-browse-mode-map "\M-p" 'gnus-browse-prev-group)
4821   (define-key gnus-browse-mode-map "\r" 'gnus-browse-select-group)
4822   (define-key gnus-browse-mode-map "u" 'gnus-browse-unsubscribe-current-group)
4823   (define-key gnus-browse-mode-map "l" 'gnus-browse-exit)
4824   (define-key gnus-browse-mode-map "L" 'gnus-browse-exit)
4825   (define-key gnus-browse-mode-map "q" 'gnus-browse-exit)
4826   (define-key gnus-browse-mode-map "Q" 'gnus-browse-exit)
4827   (define-key gnus-browse-mode-map "\C-c\C-c" 'gnus-browse-exit)
4828   (define-key gnus-browse-mode-map "?" 'gnus-browse-describe-briefly)
4829   (define-key gnus-browse-mode-map "\C-c\C-i" 'gnus-info-find-node)
4830   )
4831
4832 (defvar gnus-browse-current-method nil)
4833 (defvar gnus-browse-return-buffer nil)
4834
4835 (defvar gnus-browse-buffer "*Gnus Browse Server*")
4836
4837 (defun gnus-browse-foreign-server (method &optional return-buffer)
4838   (setq gnus-browse-current-method method)
4839   (setq gnus-browse-return-buffer return-buffer)
4840   (let ((gnus-select-method method)
4841         groups group)
4842     (gnus-message 5 "Connecting to %s..." (nth 1 method))
4843     (or (gnus-server-opened method)
4844         (gnus-open-server method)
4845         (error "Unable to contact server: %s" (gnus-status-message method)))
4846     (or (gnus-request-list method)
4847         (error "Couldn't request list: %s" (gnus-status-message method)))
4848     (get-buffer-create gnus-browse-buffer)
4849     (gnus-add-current-to-buffer-list)
4850     (and gnus-carpal (gnus-carpal-setup-buffer 'browse))
4851     (gnus-configure-windows 'browse)
4852     (buffer-disable-undo (current-buffer))
4853     (let ((buffer-read-only nil))
4854       (erase-buffer))
4855     (gnus-browse-mode)
4856     (setq mode-line-buffer-identification
4857           (format
4858            "(ding) Browse Server {%s:%s}" (car method) (car (cdr method))))
4859     (save-excursion
4860       (set-buffer nntp-server-buffer)
4861       (let ((cur (current-buffer)))
4862         (goto-char (point-min))
4863         (or (string= gnus-ignored-newsgroups "")
4864             (delete-matching-lines gnus-ignored-newsgroups))
4865         (while (re-search-forward 
4866                 "\\(^[^ \t]+\\)[ \t]+[0-9]+[ \t]+[0-9]+" nil t)
4867           (goto-char (match-end 1))
4868           (setq groups (cons (cons (buffer-substring (match-beginning 1)
4869                                                      (match-end 1))
4870                                    (max 0 (- (1+ (read cur)) (read cur))))
4871                              groups)))))
4872     (setq groups (sort groups 
4873                        (lambda (l1 l2)
4874                          (string< (car l1) (car l2)))))
4875     (let ((buffer-read-only nil))
4876       (while groups
4877         (setq group (car groups))
4878         (insert 
4879          (format "K%7d: %s\n" (cdr group) (car group)))
4880         (setq groups (cdr groups))))
4881     (switch-to-buffer (current-buffer))
4882     (goto-char (point-min))
4883     (gnus-group-position-cursor)))
4884
4885 (defun gnus-browse-mode ()
4886   "Major mode for browsing a foreign server.
4887
4888 All normal editing commands are switched off.
4889
4890 \\<gnus-browse-mode-map>
4891 The only things you can do in this buffer is
4892
4893 1) `\\[gnus-browse-unsubscribe-current-group]' to subscribe to a group.
4894 The group will be inserted into the group buffer upon exit from this
4895 buffer.  
4896
4897 2) `\\[gnus-browse-read-group]' to read a group ephemerally.
4898
4899 3) `\\[gnus-browse-exit]' to return to the group buffer."
4900   (interactive)
4901   (kill-all-local-variables)
4902   (if gnus-visual (gnus-browse-make-menu-bar))
4903   (setq mode-line-modified "-- ")
4904   (make-local-variable 'mode-line-format)
4905   (setq mode-line-format (copy-sequence mode-line-format))
4906   (and (equal (nth 3 mode-line-format) "   ")
4907        (setcar (nthcdr 3 mode-line-format) ""))
4908   (setq major-mode 'gnus-browse-mode)
4909   (setq mode-name "Browse Server")
4910   (setq mode-line-process nil)
4911   (use-local-map gnus-browse-mode-map)
4912   (buffer-disable-undo (current-buffer))
4913   (setq truncate-lines t)
4914   (setq buffer-read-only t)
4915   (run-hooks 'gnus-browse-mode-hook))
4916
4917 (defun gnus-browse-read-group (&optional no-article)
4918   "Enter the group at the current line."
4919   (interactive)
4920   (let ((group (gnus-browse-group-name)))
4921     (or (gnus-group-read-ephemeral-group 
4922          group gnus-browse-current-method nil
4923          (cons (current-buffer) 'browse))
4924         (error "Couldn't enter %s" group))))
4925
4926 (defun gnus-browse-select-group ()
4927   "Select the current group."
4928   (interactive)
4929   (gnus-browse-read-group 'no))
4930
4931 (defun gnus-browse-next-group (n)
4932   "Go to the next group."
4933   (interactive "p")
4934   (prog1
4935       (forward-line n)
4936     (gnus-group-position-cursor)))
4937
4938 (defun gnus-browse-prev-group (n)
4939   "Go to the next group."
4940   (interactive "p")
4941   (gnus-browse-next-group (- n)))
4942
4943 (defun gnus-browse-unsubscribe-current-group (arg)
4944   "(Un)subscribe to the next ARG groups."
4945   (interactive "p")
4946   (and (eobp)
4947        (error "No group at current line."))
4948   (let ((ward (if (< arg 0) -1 1))
4949         (arg (abs arg)))
4950     (while (and (> arg 0)
4951                 (not (eobp))
4952                 (gnus-browse-unsubscribe-group)
4953                 (zerop (gnus-browse-next-group ward)))
4954       (setq arg (1- arg)))
4955     (gnus-group-position-cursor)
4956     (if (/= 0 arg) (gnus-message 7 "No more newsgroups"))
4957     arg))
4958
4959 (defun gnus-browse-group-name ()
4960   (save-excursion
4961     (beginning-of-line)
4962     (if (not (re-search-forward ": \\(.*\\)$" (gnus-point-at-eol) t))
4963         ()
4964       (gnus-group-prefixed-name 
4965        (buffer-substring (match-beginning 1) (match-end 1))
4966        gnus-browse-current-method))))
4967   
4968 (defun gnus-browse-unsubscribe-group ()
4969   (let ((sub nil)
4970         (buffer-read-only nil)
4971         group)
4972     (save-excursion
4973       (beginning-of-line)
4974       (if (= (following-char) ?K) (setq sub t))
4975       (setq group (gnus-browse-group-name))
4976       (beginning-of-line)
4977       (delete-char 1)
4978       (if sub
4979           (progn
4980             (gnus-group-change-level 
4981              (list t group gnus-level-default-subscribed
4982                    nil nil gnus-browse-current-method) 
4983              gnus-level-default-subscribed gnus-level-killed
4984              (gnus-gethash (car (nth 1 gnus-newsrc-alist)) gnus-newsrc-hashtb)
4985              t)
4986             (insert ? ))
4987         (gnus-group-change-level 
4988          group gnus-level-killed gnus-level-default-subscribed)
4989         (insert ?K)))
4990     t))
4991
4992 (defun gnus-browse-exit ()
4993   "Quit browsing and return to the group buffer."
4994   (interactive)
4995   (if (eq major-mode 'gnus-browse-mode)
4996       (kill-buffer (current-buffer)))
4997   (if gnus-browse-return-buffer
4998       (gnus-configure-windows 'server)
4999     (gnus-configure-windows 'group)))
5000
5001 (defun gnus-browse-describe-briefly ()
5002   "Give a one line description of the group mode commands."
5003   (interactive)
5004   (gnus-message 6
5005    (substitute-command-keys "\\<gnus-browse-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")))
5006       
5007 \f
5008 ;;;
5009 ;;; Gnus summary mode
5010 ;;;
5011
5012 (defvar gnus-summary-mode-map nil)
5013 (defvar gnus-summary-mark-map nil)
5014 (defvar gnus-summary-mscore-map nil)
5015 (defvar gnus-summary-article-map nil)
5016 (defvar gnus-summary-thread-map nil)
5017 (defvar gnus-summary-goto-map nil)
5018 (defvar gnus-summary-exit-map nil)
5019 (defvar gnus-summary-various-map nil)
5020 (defvar gnus-summary-interest-map nil)
5021 (defvar gnus-summary-sort-map nil)
5022 (defvar gnus-summary-backend-map nil)
5023 (defvar gnus-summary-save-map nil)
5024 (defvar gnus-summary-wash-map nil)
5025 (defvar gnus-summary-help-map nil)
5026
5027 (put 'gnus-summary-mode 'mode-class 'special)
5028
5029 (if gnus-summary-mode-map
5030     nil
5031   (setq gnus-summary-mode-map (make-keymap))
5032   (suppress-keymap gnus-summary-mode-map)
5033
5034   ;; Non-orthogonal keys
5035
5036   (define-key gnus-summary-mode-map " " 'gnus-summary-next-page)
5037   (define-key gnus-summary-mode-map "\177" 'gnus-summary-prev-page)
5038   (define-key gnus-summary-mode-map "\r" 'gnus-summary-scroll-up)
5039   (define-key gnus-summary-mode-map "n" 'gnus-summary-next-unread-article)
5040   (define-key gnus-summary-mode-map "p" 'gnus-summary-prev-unread-article)
5041   (define-key gnus-summary-mode-map "N" 'gnus-summary-next-article)
5042   (define-key gnus-summary-mode-map "P" 'gnus-summary-prev-article)
5043   (define-key gnus-summary-mode-map "\M-\C-n" 'gnus-summary-next-same-subject)
5044   (define-key gnus-summary-mode-map "\M-\C-p" 'gnus-summary-prev-same-subject)
5045   (define-key gnus-summary-mode-map "\M-n" 'gnus-summary-next-unread-subject)
5046   (define-key gnus-summary-mode-map "\M-p" 'gnus-summary-prev-unread-subject)
5047   (define-key gnus-summary-mode-map "." 'gnus-summary-first-unread-article)
5048   (define-key gnus-summary-mode-map "," 'gnus-summary-best-unread-article)
5049   (define-key gnus-summary-mode-map "\M-s" 'gnus-summary-search-article-forward)
5050   (define-key gnus-summary-mode-map "\M-r" 'gnus-summary-search-article-backward)
5051   (define-key gnus-summary-mode-map "<" 'gnus-summary-beginning-of-article)
5052   (define-key gnus-summary-mode-map ">" 'gnus-summary-end-of-article)
5053   (define-key gnus-summary-mode-map "j" 'gnus-summary-goto-subject)
5054   (define-key gnus-summary-mode-map "^" 'gnus-summary-refer-parent-article)
5055   (define-key gnus-summary-mode-map "\M-^" 'gnus-summary-refer-article)
5056   (define-key gnus-summary-mode-map "u" 'gnus-summary-tick-article-forward)
5057   (define-key gnus-summary-mode-map "!" 'gnus-summary-tick-article-forward)
5058   (define-key gnus-summary-mode-map "U" 'gnus-summary-tick-article-backward)
5059   (define-key gnus-summary-mode-map "d" 'gnus-summary-mark-as-read-forward)
5060   (define-key gnus-summary-mode-map "D" 'gnus-summary-mark-as-read-backward)
5061   (define-key gnus-summary-mode-map "E" 'gnus-summary-mark-as-expirable)
5062   (define-key gnus-summary-mode-map "\M-u" 'gnus-summary-clear-mark-forward)
5063   (define-key gnus-summary-mode-map "\M-U" 'gnus-summary-clear-mark-backward)
5064   (define-key gnus-summary-mode-map "k" 'gnus-summary-kill-same-subject-and-select)
5065   (define-key gnus-summary-mode-map "\C-k" 'gnus-summary-kill-same-subject)
5066   (define-key gnus-summary-mode-map "\M-\C-k" 'gnus-summary-kill-thread)
5067   (define-key gnus-summary-mode-map "\M-\C-l" 'gnus-summary-lower-thread)
5068   (define-key gnus-summary-mode-map "e" 'gnus-summary-edit-article)
5069   (define-key gnus-summary-mode-map "#" 'gnus-summary-mark-as-processable)
5070   (define-key gnus-summary-mode-map "\M-#" 'gnus-summary-unmark-as-processable)
5071   (define-key gnus-summary-mode-map "\M-\C-t" 'gnus-summary-toggle-threads)
5072   (define-key gnus-summary-mode-map "\M-\C-s" 'gnus-summary-show-thread)
5073   (define-key gnus-summary-mode-map "\M-\C-h" 'gnus-summary-hide-thread)
5074   (define-key gnus-summary-mode-map "\M-\C-f" 'gnus-summary-next-thread)
5075   (define-key gnus-summary-mode-map "\M-\C-b" 'gnus-summary-prev-thread)
5076   (define-key gnus-summary-mode-map "\M-\C-u" 'gnus-summary-up-thread)
5077   (define-key gnus-summary-mode-map "\M-\C-d" 'gnus-summary-down-thread)
5078   (define-key gnus-summary-mode-map "&" 'gnus-summary-execute-command)
5079   (define-key gnus-summary-mode-map "c" 'gnus-summary-catchup-and-exit)
5080   (define-key gnus-summary-mode-map "\C-w" 'gnus-summary-mark-region-as-read)
5081   (define-key gnus-summary-mode-map "\C-t" 'gnus-summary-toggle-truncation)
5082   (define-key gnus-summary-mode-map "?" 'gnus-summary-mark-as-dormant)
5083   (define-key gnus-summary-mode-map "\C-c\M-\C-s" 'gnus-summary-show-all-expunged)
5084   (define-key gnus-summary-mode-map "\C-c\C-s\C-n" 'gnus-summary-sort-by-number)
5085   (define-key gnus-summary-mode-map "\C-c\C-s\C-a" 'gnus-summary-sort-by-author)
5086   (define-key gnus-summary-mode-map "\C-c\C-s\C-s" 'gnus-summary-sort-by-subject)
5087   (define-key gnus-summary-mode-map "\C-c\C-s\C-d" 'gnus-summary-sort-by-date)
5088   (define-key gnus-summary-mode-map "\C-c\C-s\C-i" 'gnus-summary-sort-by-score)
5089   (define-key gnus-summary-mode-map "=" 'gnus-summary-expand-window)
5090   (define-key gnus-summary-mode-map "\C-x\C-s" 'gnus-summary-reselect-current-group)
5091   (define-key gnus-summary-mode-map "\M-g" 'gnus-summary-rescan-group)
5092   (define-key gnus-summary-mode-map "w" 'gnus-summary-stop-page-breaking)
5093   (define-key gnus-summary-mode-map "\C-c\C-r" 'gnus-summary-caesar-message)
5094   (define-key gnus-summary-mode-map "\M-t" 'gnus-summary-toggle-mime)
5095   (define-key gnus-summary-mode-map "f" 'gnus-summary-followup)
5096   (define-key gnus-summary-mode-map "F" 'gnus-summary-followup-with-original)
5097   (define-key gnus-summary-mode-map "C" 'gnus-summary-cancel-article)
5098   (define-key gnus-summary-mode-map "r" 'gnus-summary-reply)
5099   (define-key gnus-summary-mode-map "R" 'gnus-summary-reply-with-original)
5100   (define-key gnus-summary-mode-map "\C-c\C-f" 'gnus-summary-mail-forward)
5101   (define-key gnus-summary-mode-map "o" 'gnus-summary-save-article)
5102   (define-key gnus-summary-mode-map "\C-o" 'gnus-summary-save-article-mail)
5103   (define-key gnus-summary-mode-map "|" 'gnus-summary-pipe-output)
5104   (define-key gnus-summary-mode-map "\M-k" 'gnus-summary-edit-local-kill)
5105   (define-key gnus-summary-mode-map "\M-K" 'gnus-summary-edit-global-kill)
5106   (define-key gnus-summary-mode-map "V" 'gnus-version)
5107   (define-key gnus-summary-mode-map "\C-c\C-d" 'gnus-summary-describe-group)
5108   (define-key gnus-summary-mode-map "q" 'gnus-summary-exit)
5109   (define-key gnus-summary-mode-map "Q" 'gnus-summary-exit-no-update)
5110   (define-key gnus-summary-mode-map "\C-c\C-i" 'gnus-info-find-node)
5111   (define-key gnus-summary-mode-map gnus-mouse-2 'gnus-mouse-pick-article)
5112   (define-key gnus-summary-mode-map "m" 'gnus-summary-mail-other-window)
5113   (define-key gnus-summary-mode-map "a" 'gnus-summary-post-news)
5114   (define-key gnus-summary-mode-map "x" 'gnus-summary-remove-lines-marked-as-read)
5115 ; (define-key gnus-summary-mode-map "X" 'gnus-summary-remove-lines-marked-with)
5116   (define-key gnus-summary-mode-map "s" 'gnus-summary-isearch-article)
5117   (define-key gnus-summary-mode-map "t" 'gnus-summary-toggle-header)
5118   (define-key gnus-summary-mode-map "g" 'gnus-summary-show-article)
5119 ;  (define-key gnus-summary-mode-map "?" 'gnus-summary-describe-briefly)
5120   (define-key gnus-summary-mode-map "l" 'gnus-summary-goto-last-article)
5121   (define-key gnus-summary-mode-map "\C-c\C-v\C-v" 'gnus-uu-decode-uu-view)
5122   (define-key gnus-summary-mode-map "\C-d" 'gnus-summary-enter-digest-group)
5123
5124
5125   ;; Sort of orthogonal keymap
5126   (define-prefix-command 'gnus-summary-mark-map)
5127   (define-key gnus-summary-mode-map "M" 'gnus-summary-mark-map)
5128   (define-key gnus-summary-mark-map "t" 'gnus-summary-tick-article-forward)
5129   (define-key gnus-summary-mark-map "!" 'gnus-summary-tick-article-forward)
5130   (define-key gnus-summary-mark-map "d" 'gnus-summary-mark-as-read-forward)
5131   (define-key gnus-summary-mark-map "r" 'gnus-summary-mark-as-read-forward)
5132   (define-key gnus-summary-mark-map "c" 'gnus-summary-clear-mark-forward)
5133   (define-key gnus-summary-mark-map " " 'gnus-summary-clear-mark-forward)
5134   (define-key gnus-summary-mark-map "e" 'gnus-summary-mark-as-expirable)
5135   (define-key gnus-summary-mark-map "x" 'gnus-summary-mark-as-expirable)
5136   (define-key gnus-summary-mark-map "?" 'gnus-summary-mark-as-dormant)
5137   (define-key gnus-summary-mark-map "b" 'gnus-summary-set-bookmark)
5138   (define-key gnus-summary-mark-map "B" 'gnus-summary-remove-bookmark)
5139   (define-key gnus-summary-mark-map "#" 'gnus-summary-mark-as-processable)
5140   (define-key gnus-summary-mark-map "\M-#" 'gnus-summary-unmark-as-processable)
5141   (define-key gnus-summary-mark-map "\M-r" 'gnus-summary-remove-lines-marked-as-read)
5142   (define-key gnus-summary-mark-map "\M-\C-r" 'gnus-summary-remove-lines-marked-with)
5143   (define-key gnus-summary-mark-map "D" 'gnus-summary-show-all-dormant)
5144   (define-key gnus-summary-mark-map "\M-D" 'gnus-summary-hide-all-dormant)
5145   (define-key gnus-summary-mark-map "S" 'gnus-summary-show-all-expunged)
5146   (define-key gnus-summary-mark-map "C" 'gnus-summary-catchup)
5147   (define-key gnus-summary-mark-map "H" 'gnus-summary-catchup-to-here)
5148   (define-key gnus-summary-mark-map "\C-c" 'gnus-summary-catchup-all)
5149   (define-key gnus-summary-mark-map "k" 'gnus-summary-kill-same-subject-and-select)
5150   (define-key gnus-summary-mark-map "K" 'gnus-summary-kill-same-subject)
5151
5152   (define-prefix-command 'gnus-summary-mscore-map)
5153   (define-key gnus-summary-mark-map "s" 'gnus-summary-mscore-map)
5154   (define-key gnus-summary-mscore-map "c" 'gnus-summary-clear-above)
5155   (define-key gnus-summary-mscore-map "u" 'gnus-summary-tick-above)
5156   (define-key gnus-summary-mscore-map "m" 'gnus-summary-mark-above)
5157   (define-key gnus-summary-mscore-map "k" 'gnus-summary-kill-below)
5158
5159   (define-key gnus-summary-mark-map "p" 'gnus-uu-mark-map)
5160   
5161   (define-key gnus-summary-mode-map "S" 'gnus-summary-send-map)
5162   
5163   (define-prefix-command 'gnus-summary-goto-map)
5164   (define-key gnus-summary-mode-map "G" 'gnus-summary-goto-map)
5165   (define-key gnus-summary-goto-map "n" 'gnus-summary-next-unread-article)
5166   (define-key gnus-summary-goto-map "p" 'gnus-summary-prev-unread-article)
5167   (define-key gnus-summary-goto-map "N" 'gnus-summary-next-article)
5168   (define-key gnus-summary-goto-map "P" 'gnus-summary-prev-article)
5169   (define-key gnus-summary-goto-map "\C-n" 'gnus-summary-next-same-subject)
5170   (define-key gnus-summary-goto-map "\C-p" 'gnus-summary-prev-same-subject)
5171   (define-key gnus-summary-goto-map "\M-n" 'gnus-summary-next-unread-subject)
5172   (define-key gnus-summary-goto-map "\M-p" 'gnus-summary-prev-unread-subject)
5173   (define-key gnus-summary-goto-map "f" 'gnus-summary-first-unread-article)
5174   (define-key gnus-summary-goto-map "b" 'gnus-summary-best-unread-article)
5175   (define-key gnus-summary-goto-map "g" 'gnus-summary-goto-subject)
5176   (define-key gnus-summary-goto-map "l" 'gnus-summary-goto-last-article)
5177   (define-key gnus-summary-goto-map "p" 'gnus-summary-pop-article)
5178
5179
5180   (define-prefix-command 'gnus-summary-thread-map)
5181   (define-key gnus-summary-mode-map "T" 'gnus-summary-thread-map)
5182   (define-key gnus-summary-thread-map "k" 'gnus-summary-kill-thread)
5183   (define-key gnus-summary-thread-map "l" 'gnus-summary-lower-thread)
5184   (define-key gnus-summary-thread-map "i" 'gnus-summary-raise-thread)
5185   (define-key gnus-summary-thread-map "T" 'gnus-summary-toggle-threads)
5186   (define-key gnus-summary-thread-map "s" 'gnus-summary-show-thread)
5187   (define-key gnus-summary-thread-map "S" 'gnus-summary-show-all-threads)
5188   (define-key gnus-summary-thread-map "h" 'gnus-summary-hide-thread)
5189   (define-key gnus-summary-thread-map "H" 'gnus-summary-hide-all-threads)
5190   (define-key gnus-summary-thread-map "n" 'gnus-summary-next-thread)
5191   (define-key gnus-summary-thread-map "p" 'gnus-summary-prev-thread)
5192   (define-key gnus-summary-thread-map "u" 'gnus-summary-up-thread)
5193   (define-key gnus-summary-thread-map "d" 'gnus-summary-down-thread)
5194   (define-key gnus-summary-thread-map "#" 'gnus-uu-mark-thread)
5195
5196   
5197   (define-prefix-command 'gnus-summary-exit-map)
5198   (define-key gnus-summary-mode-map "Z" 'gnus-summary-exit-map)
5199   (define-key gnus-summary-exit-map "c" 'gnus-summary-catchup-and-exit)
5200   (define-key gnus-summary-exit-map "C" 'gnus-summary-catchup-all-and-exit)
5201   (define-key gnus-summary-exit-map "E" 'gnus-summary-exit-no-update)
5202   (define-key gnus-summary-exit-map "Q" 'gnus-summary-exit)
5203   (define-key gnus-summary-exit-map "Z" 'gnus-summary-exit)
5204   (define-key gnus-summary-exit-map "n" 'gnus-summary-catchup-and-goto-next-group)
5205   (define-key gnus-summary-exit-map "R" 'gnus-summary-reselect-current-group)
5206   (define-key gnus-summary-exit-map "G" 'gnus-summary-rescan-group)
5207   (define-key gnus-summary-exit-map "N" 'gnus-summary-next-group)
5208   (define-key gnus-summary-exit-map "P" 'gnus-summary-prev-group)
5209
5210
5211   (define-prefix-command 'gnus-summary-article-map)
5212   (define-key gnus-summary-mode-map "A" 'gnus-summary-article-map)
5213   (define-key gnus-summary-article-map " " 'gnus-summary-next-page)
5214   (define-key gnus-summary-article-map "n" 'gnus-summary-next-page)
5215   (define-key gnus-summary-article-map "\177" 'gnus-summary-prev-page)
5216   (define-key gnus-summary-article-map "p" 'gnus-summary-prev-page)
5217   (define-key gnus-summary-article-map "\r" 'gnus-summary-scroll-up)
5218   (define-key gnus-summary-article-map "<" 'gnus-summary-beginning-of-article)
5219   (define-key gnus-summary-article-map ">" 'gnus-summary-end-of-article)
5220   (define-key gnus-summary-article-map "b" 'gnus-summary-beginning-of-article)
5221   (define-key gnus-summary-article-map "e" 'gnus-summary-end-of-article)
5222   (define-key gnus-summary-article-map "^" 'gnus-summary-refer-parent-article)
5223   (define-key gnus-summary-article-map "r" 'gnus-summary-refer-parent-article)
5224   (define-key gnus-summary-article-map "w" 'gnus-summary-stop-page-breaking)
5225   (define-key gnus-summary-article-map "c" 'gnus-summary-caesar-message)
5226   (define-key gnus-summary-article-map "g" 'gnus-summary-show-article)
5227   (define-key gnus-summary-article-map "t" 'gnus-summary-toggle-header)
5228   (define-key gnus-summary-article-map "m" 'gnus-summary-toggle-mime)
5229   (define-key gnus-summary-article-map "s" 'gnus-summary-isearch-article)
5230
5231
5232   (define-prefix-command 'gnus-summary-wash-map)
5233   (define-key gnus-summary-mode-map "W" 'gnus-summary-wash-map)
5234   (define-key gnus-summary-wash-map "h" 'gnus-article-hide-headers)
5235   (define-key gnus-summary-wash-map "s" 'gnus-article-hide-signature)
5236   (define-key gnus-summary-wash-map "c" 'gnus-article-hide-citation)
5237   (define-key gnus-summary-wash-map "o" 'gnus-article-treat-overstrike)
5238   (define-key gnus-summary-wash-map "w" 'gnus-article-word-wrap)
5239   (define-key gnus-summary-wash-map "d" 'gnus-article-remove-cr)
5240   (define-key gnus-summary-wash-map "q" 'gnus-article-de-quoted-unreadable)
5241   (define-key gnus-summary-wash-map "f" 'gnus-article-display-x-face)
5242   (define-key gnus-summary-wash-map "t" 'gnus-article-date-ut)
5243   (define-key gnus-summary-wash-map "\C-t" 'gnus-article-date-local)
5244   (define-key gnus-summary-wash-map "T" 'gnus-article-date-lapsed)
5245
5246   (define-key gnus-summary-wash-map "A" 'gnus-article-highlight)
5247   (define-key gnus-summary-wash-map "a" 'gnus-article-hide)
5248   (define-key gnus-summary-wash-map "H" 'gnus-article-highlight-headers)
5249   (define-key gnus-summary-wash-map "C" 'gnus-article-highlight-citation)
5250   (define-key gnus-summary-wash-map "S" 'gnus-article-highlight-signature)
5251   (define-key gnus-summary-wash-map "b" 'gnus-article-add-buttons)
5252
5253
5254   (define-prefix-command 'gnus-summary-help-map)
5255   (define-key gnus-summary-mode-map "H" 'gnus-summary-help-map)
5256   (define-key gnus-summary-help-map "v" 'gnus-version)
5257   (define-key gnus-summary-help-map "f" 'gnus-summary-fetch-faq)
5258   (define-key gnus-summary-help-map "d" 'gnus-summary-describe-group)
5259   (define-key gnus-summary-help-map "h" 'gnus-summary-describe-briefly)
5260   (define-key gnus-summary-help-map "i" 'gnus-info-find-node)
5261
5262
5263   (define-prefix-command 'gnus-summary-backend-map)
5264   (define-key gnus-summary-mode-map "B" 'gnus-summary-backend-map)
5265   (define-key gnus-summary-backend-map "e" 'gnus-summary-expire-articles)
5266   (define-key gnus-summary-backend-map "\177" 'gnus-summary-delete-article)
5267   (define-key gnus-summary-backend-map "m" 'gnus-summary-move-article)
5268   (define-key gnus-summary-backend-map "r" 'gnus-summary-respool-article)
5269   (define-key gnus-summary-backend-map "w" 'gnus-summary-edit-article)
5270   (define-key gnus-summary-backend-map "c" 'gnus-summary-copy-article)
5271   (define-key gnus-summary-backend-map "q" 'gnus-summary-fancy-query)
5272   (define-key gnus-summary-backend-map "i" 'gnus-summary-import-article)
5273
5274
5275   (define-prefix-command 'gnus-summary-save-map)
5276   (define-key gnus-summary-mode-map "O" 'gnus-summary-save-map)
5277   (define-key gnus-summary-save-map "o" 'gnus-summary-save-article)
5278   (define-key gnus-summary-save-map "m" 'gnus-summary-save-article-mail)
5279   (define-key gnus-summary-save-map "r" 'gnus-summary-save-article-rmail)
5280   (define-key gnus-summary-save-map "f" 'gnus-summary-save-article-file)
5281   (define-key gnus-summary-save-map "h" 'gnus-summary-save-article-folder)
5282   (define-key gnus-summary-save-map "v" 'gnus-summary-save-article-vm)
5283   (define-key gnus-summary-save-map "p" 'gnus-summary-pipe-output)
5284 ;  (define-key gnus-summary-save-map "s" 'gnus-soup-add-article)
5285
5286   (define-key gnus-summary-mode-map "X" 'gnus-uu-extract-map)
5287   
5288   (define-prefix-command 'gnus-summary-various-map)
5289   (define-key gnus-summary-mode-map "V" 'gnus-summary-various-map)
5290   (define-key gnus-summary-various-map "u" 'gnus-summary-universal-argument)
5291   (define-key gnus-summary-various-map "\C-s" 'gnus-summary-search-article-forward)
5292   (define-key gnus-summary-various-map "\C-r" 'gnus-summary-search-article-backward)
5293   (define-key gnus-summary-various-map "r" 'gnus-summary-refer-article)
5294   (define-key gnus-summary-various-map "&" 'gnus-summary-execute-command)
5295   (define-key gnus-summary-various-map "T" 'gnus-summary-toggle-truncation)
5296   (define-key gnus-summary-various-map "e" 'gnus-summary-expand-window)
5297   (define-key gnus-summary-various-map "D" 'gnus-summary-enter-digest-group)
5298   (define-key gnus-summary-various-map "k" 'gnus-summary-edit-local-kill)
5299   (define-key gnus-summary-various-map "K" 'gnus-summary-edit-global-kill)
5300
5301   (define-key gnus-summary-various-map "S" 'gnus-summary-score-map)
5302
5303   (define-prefix-command 'gnus-summary-sort-map)
5304   (define-key gnus-summary-various-map "s" 'gnus-summary-sort-map)
5305   (define-key gnus-summary-sort-map "n" 'gnus-summary-sort-by-number)
5306   (define-key gnus-summary-sort-map "a" 'gnus-summary-sort-by-author)
5307   (define-key gnus-summary-sort-map "s" 'gnus-summary-sort-by-subject)
5308   (define-key gnus-summary-sort-map "d" 'gnus-summary-sort-by-date)
5309   (define-key gnus-summary-sort-map "i" 'gnus-summary-sort-by-score)
5310
5311   (define-key gnus-summary-mode-map "I" 'gnus-summary-increase-score)
5312   (define-key gnus-summary-mode-map "L" 'gnus-summary-lower-score)
5313   )
5314
5315
5316 \f
5317
5318 (defun gnus-summary-mode ()
5319   "Major mode for reading articles.
5320
5321 All normal editing commands are switched off.
5322 \\<gnus-summary-mode-map>
5323 Each line in this buffer represents one article.  To read an
5324 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
5325 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]', 
5326 respectively.
5327
5328 You can also post articles and send mail from this buffer.  To 
5329 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author 
5330 of an article, type `\\[gnus-summary-reply]'.
5331
5332 There are approx. one gazillion commands you can execute in this 
5333 buffer; read the info pages for more information (`\\[gnus-info-find-node]'). 
5334
5335 The following commands are available:
5336
5337 \\{gnus-summary-mode-map}"
5338   (interactive)
5339   (if gnus-visual (gnus-summary-make-menu-bar))
5340   (kill-all-local-variables)
5341   (let ((locals gnus-summary-local-variables))
5342     (while locals
5343       (if (consp (car locals))
5344           (progn
5345             (make-local-variable (car (car locals)))
5346             (set (car (car locals)) (eval (cdr (car locals)))))
5347         (make-local-variable (car locals))
5348         (set (car locals) nil))
5349       (setq locals (cdr locals))))
5350   (gnus-make-thread-indent-array)
5351   (gnus-update-format-specifications)
5352   (setq mode-line-modified "-- ")
5353   (make-local-variable 'mode-line-format)
5354   (setq mode-line-format (copy-sequence mode-line-format))
5355   (and (equal (nth 3 mode-line-format) "   ")
5356        (setcar (nthcdr 3 mode-line-format) ""))
5357   (setq major-mode 'gnus-summary-mode)
5358   (setq mode-name "Summary")
5359   (make-local-variable 'minor-mode-alist)
5360   (use-local-map gnus-summary-mode-map)
5361   (buffer-disable-undo (current-buffer))
5362   (setq buffer-read-only t)             ;Disable modification
5363   (setq truncate-lines t)
5364   (setq selective-display t)
5365   (setq selective-display-ellipses t)   ;Display `...'
5366   (setq buffer-display-table gnus-summary-display-table)
5367   (run-hooks 'gnus-summary-mode-hook))
5368
5369 (defun gnus-summary-make-display-table ()
5370   ;; Change the display table.  Odd characters have a tendency to mess
5371   ;; up nicely formatted displays - we make all possible glyphs
5372   ;; display only a single character.
5373
5374   ;; We start from the standard display table, if any.
5375   (setq gnus-summary-display-table 
5376         (or (copy-sequence standard-display-table)
5377             (make-display-table)))
5378   ;; Nix out all the control chars...
5379   (let ((i 32))
5380     (while (>= (setq i (1- i)) 0)
5381       (aset gnus-summary-display-table i [??])))
5382   ;; ... but not newline and cr, of course. (cr is necessary for the
5383   ;; selective display).  
5384   (aset gnus-summary-display-table ?\n nil)
5385   (aset gnus-summary-display-table ?\r nil)
5386   ;; We nix out any glyphs over 126 that are not set already.  
5387   (let ((i 256))
5388     (while (>= (setq i (1- i)) 127)
5389       ;; Only modify if the entry is nil.
5390       (or (aref gnus-summary-display-table i) 
5391           (aset gnus-summary-display-table i [??])))))
5392
5393 (defun gnus-summary-clear-local-variables ()
5394   (let ((locals gnus-summary-local-variables))
5395     (while locals
5396       (if (consp (car locals))
5397           (and (vectorp (car (car locals)))
5398                (set (car (car locals)) nil))
5399         (and (vectorp (car locals))
5400              (set (car locals) nil)))
5401       (setq locals (cdr locals)))))
5402
5403 (defun gnus-mouse-pick-article (e)
5404   (interactive "e")
5405   (mouse-set-point e)
5406   (gnus-summary-next-page nil t))
5407
5408 (defun gnus-summary-setup-buffer (group)
5409   "Initialize summary buffer."
5410   (let ((buffer (concat "*Summary " group "*")))
5411     (if (get-buffer buffer)
5412         (progn
5413           (set-buffer buffer)
5414           (not gnus-newsgroup-begin))
5415       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
5416       (setq gnus-summary-buffer (set-buffer (get-buffer-create buffer)))
5417       (gnus-add-current-to-buffer-list)
5418       (gnus-summary-mode)
5419       (and gnus-carpal (gnus-carpal-setup-buffer 'summary))
5420       (setq gnus-newsgroup-name group)
5421       t)))
5422
5423 (defun gnus-set-global-variables ()
5424   ;; Set the global equivalents of the summary buffer-local variables
5425   ;; to the latest values they had. These reflect the summary buffer
5426   ;; that was in action when the last article was fetched.
5427   (if (eq major-mode 'gnus-summary-mode) 
5428       (progn
5429         (setq gnus-summary-buffer (current-buffer))
5430         (let ((name gnus-newsgroup-name)
5431               (marked gnus-newsgroup-marked)
5432               (unread gnus-newsgroup-unreads)
5433               (headers gnus-current-headers)
5434               (score-file gnus-current-score-file))
5435           (save-excursion
5436             (set-buffer gnus-group-buffer)
5437             (setq gnus-newsgroup-name name)
5438             (setq gnus-newsgroup-marked marked)
5439             (setq gnus-newsgroup-unreads unread)
5440             (setq gnus-current-headers headers)
5441             (setq gnus-current-score-file score-file))))))
5442
5443 (defun gnus-summary-insert-dummy-line (sformat subject number)
5444   (if (not sformat) 
5445       (setq sformat gnus-summary-dummy-line-format-spec))
5446   (let (b)
5447     (beginning-of-line)
5448     (setq b (point))
5449     (insert (eval sformat))
5450     (add-text-properties
5451      b (1+ b)
5452      (list 'gnus-number number 
5453            'gnus-mark gnus-dummy-mark
5454            'gnus-level 0))))
5455
5456 (defvar gnus-thread-indent-array nil)
5457 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
5458 (defun gnus-make-thread-indent-array ()
5459   (let ((n 200))
5460     (if (and gnus-thread-indent-array
5461              (= gnus-thread-indent-level gnus-thread-indent-array-level))
5462         nil
5463       (setq gnus-thread-indent-array (make-vector 201 "")
5464             gnus-thread-indent-array-level gnus-thread-indent-level)
5465       (while (>= n 0)
5466         (aset gnus-thread-indent-array n
5467               (make-string (* n gnus-thread-indent-level) ? ))
5468         (setq n (1- n))))))
5469
5470 (defun gnus-summary-insert-line 
5471   (sformat header level current unread replied expirable subject-or-nil
5472            &optional dummy score)
5473   (or sformat (setq sformat gnus-summary-line-format-spec))
5474   (let* ((indentation (aref gnus-thread-indent-array level))
5475          (lines (header-lines header))
5476          (score (or score gnus-summary-default-score 0))
5477          (score-char
5478           (if (or (null gnus-summary-default-score)
5479                   (<= (abs (- score gnus-summary-default-score))
5480                       gnus-summary-zcore-fuzz)) ? 
5481             (if (< score gnus-summary-default-score)
5482                 gnus-score-below-mark gnus-score-over-mark)))
5483          (replied (if replied gnus-replied-mark ? ))
5484          (from (header-from header))
5485          (name-address (funcall gnus-extract-address-components from))
5486          (address (car (cdr name-address)))
5487          (name (or (car name-address) (car (cdr name-address))))
5488          (subject (header-subject header))
5489          (number (header-number header))
5490          (opening-bracket (if dummy ?\< ?\[))
5491          (closing-bracket (if dummy ?\> ?\]))
5492          (buffer-read-only nil)
5493          (b (progn (beginning-of-line) (point))))
5494     (or (numberp lines) (setq lines 0))
5495     (insert (eval sformat))
5496     (add-text-properties
5497      b (1+ b) (list 'gnus-number number 
5498                     'gnus-mark (or unread gnus-unread-mark)
5499                     'gnus-level level))))
5500
5501 (defun gnus-summary-update-line (&optional dont-update)
5502   ;; Update summary line after change.
5503   (or (not gnus-summary-default-score)
5504       gnus-summary-inhibit-highlight
5505       (let ((gnus-summary-inhibit-highlight t)
5506             (article (gnus-summary-article-number)))
5507         (progn
5508           (or dont-update
5509               (if (and gnus-summary-mark-below
5510                        (< (gnus-summary-article-score)
5511                           gnus-summary-mark-below))
5512                   (and (not (memq article gnus-newsgroup-marked))
5513                        (not (memq article gnus-newsgroup-dormant))
5514                        (memq article gnus-newsgroup-unreads)
5515                        (gnus-summary-mark-article nil gnus-low-score-mark))
5516                 (and (eq (gnus-summary-article-mark) gnus-low-score-mark)
5517                      (gnus-summary-mark-article nil gnus-unread-mark))))
5518           (and gnus-visual
5519                (run-hooks 'gnus-summary-update-hook))))))
5520
5521 (defun gnus-summary-update-lines (&optional beg end)
5522   ;; Rehighlight summary buffer according to `gnus-summary-highlight'.
5523   (let ((beg (or beg (point-min)))
5524         (end (or end (point-max))))
5525     (save-excursion
5526       (set-buffer gnus-summary-buffer)
5527       (goto-char beg)
5528       (while (and (not (eobp)) (< (point) end))
5529         (gnus-summary-update-line)
5530         (forward-line 1)))))
5531
5532 (defun gnus-summary-number-of-articles-in-thread (thread &optional char)
5533   ;; Sum up all elements (and sub-elements) in a list.
5534   (let ((number 
5535          (if (listp thread) 
5536              (apply 
5537               '+ (mapcar 'gnus-summary-number-of-articles-in-thread thread))
5538            1)))
5539     (if char 
5540         (if (> number 1) gnus-not-empty-thread-mark
5541           gnus-empty-thread-mark)
5542       number)))
5543
5544 (defun gnus-summary-read-group (group &optional show-all no-article kill-buffer)
5545   "Start reading news in newsgroup GROUP.
5546 If SHOW-ALL is non-nil, already read articles are also listed.
5547 If NO-ARTICLE is non-nil, no article is selected initially."
5548   (gnus-message 5 "Retrieving newsgroup: %s..." group)
5549   (let* ((new-group (gnus-summary-setup-buffer group))
5550          (did-select (and new-group (gnus-select-newsgroup group show-all)))
5551          (method (car (gnus-find-method-for-group group))))
5552     (cond 
5553      ((not new-group)
5554       (gnus-set-global-variables)
5555       (gnus-kill-buffer kill-buffer)
5556       (gnus-configure-windows 'summary)
5557       (gnus-set-mode-line 'summary)
5558       (gnus-summary-position-cursor)
5559       (message "")
5560       t)
5561      ((null did-select) 
5562       (and (eq major-mode 'gnus-summary-mode)
5563            (not (equal (current-buffer) kill-buffer))
5564            (progn
5565              (kill-buffer (current-buffer))
5566              (set-buffer gnus-group-buffer)
5567              (gnus-group-next-unread-group 1)))
5568       (message "Can't select group")
5569       nil)
5570      ((eq did-select 'quit)
5571       (and (eq major-mode 'gnus-summary-mode)
5572            (not (equal (current-buffer) kill-buffer))
5573            (kill-buffer (current-buffer)))
5574       (gnus-kill-buffer kill-buffer)
5575       (gnus-configure-windows 'group)
5576       (gnus-group-next-unread-group 1)
5577       (signal 'quit nil))
5578      (t
5579       (gnus-set-global-variables)
5580       ;; Save the active value in effect when the group was entered.
5581       (setq gnus-newsgroup-active 
5582             (gnus-copy-sequence
5583              (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
5584       ;; You can change the subjects in this hook.
5585       (run-hooks 'gnus-select-group-hook)
5586       ;; Do score processing.
5587       (and gnus-use-scoring (gnus-possibly-score-headers))
5588       ;; Update the format specifiers.
5589       (gnus-update-format-specifications)
5590       ;; Generate the summary buffer.
5591       (gnus-summary-prepare)
5592       (if (zerop (buffer-size))
5593           (cond (gnus-newsgroup-dormant
5594                  (gnus-summary-show-all-dormant))
5595                 ((and gnus-newsgroup-scored show-all)
5596                  (gnus-summary-show-all-expunged))))
5597       ;; Function `gnus-apply-kill-file' must be called in this hook.
5598       (run-hooks 'gnus-apply-kill-hook)
5599       (if (zerop (buffer-size))
5600           (progn
5601             ;; This newsgroup is empty.
5602             (gnus-summary-catchup-and-exit nil t) ;Without confirmations.
5603             (gnus-message 6 "No unread news")
5604             (gnus-kill-buffer kill-buffer)
5605             nil)
5606         ;;(save-excursion
5607         ;;  (if kill-buffer
5608         ;;      (let ((gnus-summary-buffer kill-buffer))
5609         ;;      (gnus-configure-windows 'group))))
5610         ;; Hide conversation thread subtrees.  We cannot do this in
5611         ;; gnus-summary-prepare-hook since kill processing may not
5612         ;; work with hidden articles.
5613         (and gnus-show-threads
5614              gnus-thread-hide-subtree
5615              (gnus-summary-hide-all-threads))
5616         ;; Show first unread article if requested.
5617         (goto-char (point-min))
5618         (if (and (not no-article)
5619                  gnus-auto-select-first
5620                  (gnus-summary-first-unread-article))
5621             ()
5622           (gnus-configure-windows 'summary))
5623         (gnus-set-mode-line 'summary)
5624         (gnus-summary-position-cursor)
5625         ;; If in async mode, we send some info to the backend.
5626         (and gnus-newsgroup-async
5627              (setq gnus-newsgroup-threads (nreverse gnus-newsgroup-threads))
5628              (gnus-request-asynchronous 
5629               gnus-newsgroup-name
5630               (if (and gnus-asynchronous-article-function
5631                        (fboundp gnus-asynchronous-article-function))
5632                   (funcall gnus-asynchronous-article-function
5633                            gnus-newsgroup-threads)
5634                 gnus-newsgroup-threads)))
5635         (gnus-kill-buffer kill-buffer)
5636         (if (not (get-buffer-window gnus-group-buffer))
5637             ()
5638           ;; gotta use windows, because recenter does wierd stuff if
5639           ;; the current buffer ain't the displayed window.
5640           (let ((owin (selected-window))) 
5641             (select-window (get-buffer-window gnus-group-buffer))
5642             (and (gnus-group-goto-group group)
5643                  (recenter))
5644             (select-window owin))))
5645       t))))
5646
5647 (defun gnus-summary-prepare ()
5648   ;; Generate the summary buffer.
5649   (let ((buffer-read-only nil))
5650     (erase-buffer)
5651     (gnus-summary-prepare-threads 
5652      (if gnus-show-threads
5653          (gnus-gather-threads 
5654           (gnus-sort-threads 
5655            (if (and gnus-summary-expunge-below
5656                     (not gnus-fetch-old-headers))
5657                (gnus-make-threads-and-expunge)
5658              (gnus-make-threads))))
5659        gnus-newsgroup-headers)
5660      0 nil nil t)
5661     ;; Erase header retrieval message.
5662     (gnus-summary-update-lines)
5663     (message "")
5664     ;; Remove the final newline.
5665     ;;(goto-char (point-max))
5666     ;;(delete-char -1)
5667     ;; Call hooks for modifying summary buffer.
5668     ;; Suggested by sven@tde.LTH.Se (Sven Mattisson).
5669     (goto-char (point-min))
5670     (run-hooks 'gnus-summary-prepare-hook)))
5671
5672 (defun gnus-subject-equal (s1 s2)
5673   (cond 
5674    ((numberp gnus-summary-gather-subject-limit)
5675     (string= (if (> (length s1) gnus-summary-gather-subject-limit)
5676                  (substring s1 0 gnus-summary-gather-subject-limit)
5677                s1)
5678              (if (> (length s2) gnus-summary-gather-subject-limit)
5679                  (substring s2 0 gnus-summary-gather-subject-limit)
5680                s2)))
5681    ((eq 'fuzzy gnus-summary-gather-subject-limit)
5682     (string= (gnus-simplify-subject-fuzzy s1)
5683              (gnus-simplify-subject-fuzzy s2)))
5684    (t
5685     (string= s1 s2))))
5686
5687 (defun gnus-gather-threads (threads)
5688   "Gather threads that have lost their roots."
5689   (if (not gnus-summary-make-false-root)
5690       threads 
5691     (let ((hashtb (gnus-make-hashtable 1023))
5692           (prev threads)
5693           (result threads)
5694           thread subject hthread whole-subject)
5695       (while threads
5696         (setq whole-subject 
5697               (setq subject (header-subject (car (car threads)))))
5698         (if gnus-summary-gather-subject-limit
5699             (or (and (numberp gnus-summary-gather-subject-limit)
5700                      (> (length subject) gnus-summary-gather-subject-limit)
5701                      (setq subject
5702                            (substring subject 0 
5703                                       gnus-summary-gather-subject-limit)))
5704                 (and (eq 'fuzzy gnus-summary-gather-subject-limit)
5705                      (setq subject (gnus-simplify-subject-fuzzy subject))))
5706           (setq subject (gnus-simplify-subject-re subject)))
5707         (if (setq hthread 
5708                   (gnus-gethash subject hashtb))
5709             (progn
5710               (or (stringp (car (car hthread)))
5711                   (setcar hthread (list whole-subject (car hthread))))
5712               (setcdr (car hthread) (nconc (cdr (car hthread)) 
5713                                            (list (car threads))))
5714               (setcdr prev (cdr threads))
5715               (setq threads prev))
5716           (gnus-sethash subject threads hashtb))
5717         (setq prev threads)
5718         (setq threads (cdr threads)))
5719       result)))
5720
5721 (defun gnus-make-threads ()
5722   ;; This function takes the dependencies already made by 
5723   ;; `gnus-get-newsgroup-headers' and builds the trees. First we go
5724   ;; through the dependecies in the hash table and finds all the
5725   ;; roots. Roots do not refer back to any valid articles.
5726   (let (roots)
5727     (and gnus-fetch-old-headers (eq gnus-headers-retrieved-by 'nov)
5728          (gnus-build-old-threads))
5729     (mapatoms
5730      (lambda (refs)
5731        (if (not (car (symbol-value refs)))
5732            (setq roots (append (cdr (symbol-value refs)) roots))
5733          ;; Ok, these refer back to valid articles, but if
5734          ;; `gnus-thread-ignore-subject' is nil, we have to check that
5735          ;; the root has the same subject as its children. The children
5736          ;; that do not are made into roots and removed from the list
5737          ;; of children. 
5738          (or gnus-thread-ignore-subject
5739              (let* ((prev (symbol-value refs))
5740                     (subject (gnus-simplify-subject-re 
5741                               (header-subject (car prev))))
5742                     (headers (cdr prev)))
5743                (while headers
5744                  (if (not (string= subject
5745                                    (gnus-simplify-subject-re 
5746                                     (header-subject (car headers)))))
5747                      (progn
5748                        (setq roots (cons (car headers) roots))
5749                        (setcdr prev (cdr headers)))
5750                    (setq prev headers))
5751                  (setq headers (cdr headers)))))))
5752      gnus-newsgroup-dependencies)
5753     
5754     (mapcar 'gnus-trim-thread
5755             (apply 'append
5756                    (mapcar 'gnus-cut-thread
5757                            (mapcar 'gnus-make-sub-thread roots))))))
5758   
5759 (defun gnus-make-threads-and-expunge ()
5760   ;; This function takes the dependencies already made by 
5761   ;; `gnus-get-newsgroup-headers' and builds the trees. First we go
5762   ;; through the dependecies in the hash table and finds all the
5763   ;; roots. Roots do not refer back to any valid articles.
5764   (let ((default (or gnus-summary-default-score 0))
5765         (below gnus-summary-expunge-below)
5766         roots article)
5767     (and gnus-fetch-old-headers (eq gnus-headers-retrieved-by 'nov)
5768          (gnus-build-old-threads))
5769     (mapatoms
5770      (lambda (refs)
5771        (if (not (car (symbol-value refs)))
5772            ;; These articles do not refer back to any other articles -
5773            ;; they are roots.
5774            (let ((headers (cdr (symbol-value refs))))
5775              ;; We weed out the low-scored articles.
5776              (while headers
5777                (if (not (< (or (cdr (assq (header-number (car headers))
5778                                           gnus-newsgroup-scored)) default)
5779                            below))
5780                    ;; It is over.
5781                    (setq roots (cons (car headers) roots))
5782                  ;; It is below, so we mark it as read.
5783                  (setq gnus-newsgroup-unreads
5784                        (delq (header-number (car headers))
5785                              gnus-newsgroup-unreads)))
5786                (setq headers (cdr headers))))
5787          ;; Ok, these refer back to valid articles, but if
5788          ;; `gnus-thread-ignore-subject' is nil, we have to check that
5789          ;; the root has the same subject as its children. The children
5790          ;; that do not are made into roots and removed from the list
5791          ;; of children. 
5792          (or gnus-thread-ignore-subject
5793              (let* ((prev (symbol-value refs))
5794                     (subject (gnus-simplify-subject-re 
5795                               (header-subject (car prev))))
5796                     (headers (cdr prev)))
5797                (while headers
5798                  (if (not (string= subject
5799                                    (gnus-simplify-subject-re 
5800                                     (header-subject (car headers)))))
5801                      (progn
5802                        (if (not (< (or (cdr (assq (header-number (car headers))
5803                                                   gnus-newsgroup-scored))
5804                                        default) below))
5805                            (setq roots (cons (car headers) roots))
5806                          (setq gnus-newsgroup-unreads
5807                                (delq (header-number (car headers))
5808                                      gnus-newsgroup-unreads)))
5809                        (setcdr prev (cdr headers)))
5810                    (setq prev headers))
5811                  (setq headers (cdr headers)))))
5812          ;; If this article is expunged, some of the children might be
5813          ;; roots.  
5814          (if (< (or (cdr (assq (header-number (car (symbol-value refs)))
5815                                gnus-newsgroup-scored)) default)
5816                 below)
5817              (let* ((prev (symbol-value refs))
5818                     (headers (cdr prev)))
5819                (while headers
5820                  (setq article (header-number (car headers)))
5821                  (if (not (< (or (cdr (assq article gnus-newsgroup-scored))
5822                                  default) below))
5823                      (progn (setq roots (cons (car headers) roots))
5824                             (setq prev headers))
5825                    (setq gnus-newsgroup-unreads 
5826                          (delq article gnus-newsgroup-unreads))
5827                    (setcdr prev (cdr headers)))
5828                  (setq headers (cdr headers))))
5829            ;; It was not expunged, but we look at expunged children.
5830            (let* ((prev (symbol-value refs))
5831                   (headers (cdr prev))
5832                   article id)
5833              (while headers
5834                (setq article (header-number (car headers)))
5835                (if (not (< (or (cdr (assq article gnus-newsgroup-scored))
5836                                default) below))
5837                    (setq prev headers)
5838                  (setq gnus-newsgroup-unreads 
5839                        (delq article gnus-newsgroup-unreads))
5840                  (setcdr prev (cdr headers)))
5841                (setq headers (cdr headers)))))))
5842      gnus-newsgroup-dependencies)
5843
5844     (mapcar 'gnus-trim-thread
5845             (apply 'append
5846                    (mapcar 'gnus-cut-thread
5847                            (mapcar 'gnus-make-sub-thread roots))))))
5848   
5849 (defun gnus-cut-thread (thread)
5850   ;; Remove leaf dormant or ancient articles from THREAD.
5851   (let ((head (car thread))
5852         (tail (apply 'append (mapcar 'gnus-cut-thread (cdr thread)))))
5853     (if (and (null tail)
5854              (let ((number (header-number head)))
5855                (or (memq number gnus-newsgroup-ancient)
5856                    (memq number gnus-newsgroup-dormant)
5857                    (and gnus-summary-expunge-below
5858                         (eq gnus-fetch-old-headers 'some)
5859                         (< (or (cdr (assq number gnus-newsgroup-scored))
5860                                gnus-summary-default-score 0)
5861                            gnus-summary-expunge-below)
5862                         (progn
5863                           (setq gnus-newsgroup-unreads
5864                                 (delq number gnus-newsgroup-unreads))
5865                           t)))))
5866         nil
5867       (list (cons head tail)))))
5868
5869 (defun gnus-trim-thread (thread)
5870   ;; Remove root ancient articles with only one child from THREAD.
5871   (if (and (eq gnus-fetch-old-headers 'some)
5872            (memq (header-number (car thread)) gnus-newsgroup-ancient)
5873            (= (length thread) 2))
5874       (gnus-trim-thread (nth 1 thread))
5875     thread))
5876
5877 (defun gnus-make-sub-thread (root)
5878   ;; This function makes a sub-tree for a node in the tree.
5879   (let ((children (reverse (cdr (gnus-gethash (downcase (header-id root))
5880                                               gnus-newsgroup-dependencies)))))
5881     (cons root (mapcar 'gnus-make-sub-thread children))))
5882
5883 (defun gnus-build-old-threads ()
5884   ;; Look at all the articles that refer back to old articles, and
5885   ;; fetch the headers for the articles that aren't there. This will
5886   ;; build complete threads - if the roots haven't been expired by the
5887   ;; server, that is.
5888   (let (id heads)
5889     (mapatoms
5890      (lambda (refs)
5891        (if (not (car (symbol-value refs)))
5892            (progn
5893              (setq heads (cdr (symbol-value refs)))
5894              (while heads
5895                (if (not (memq (header-number (car heads))
5896                               gnus-newsgroup-dormant))
5897                    (progn
5898                      (setq id (symbol-name refs))
5899                      (while (and (setq id (gnus-build-get-header id))
5900                                  (not (car (gnus-gethash 
5901                                             id gnus-newsgroup-dependencies)))))
5902                      (setq heads nil))
5903                  (setq heads (cdr heads)))))))
5904      gnus-newsgroup-dependencies)))
5905
5906 (defun gnus-build-get-header (id)
5907   ;; Look through the buffer of NOV lines and find the header to
5908   ;; ID. Enter this line into the dependencies hash table, and return
5909   ;; the id of the parent article (if any).
5910   (let ((deps gnus-newsgroup-dependencies)
5911         found header)
5912     (prog1
5913         (save-excursion
5914           (set-buffer nntp-server-buffer)
5915           (goto-char (point-min))
5916           (while (and (not found) (search-forward id nil t))
5917             (beginning-of-line)
5918             (setq found (looking-at 
5919                          (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
5920                                  (regexp-quote id))))
5921             (or found (beginning-of-line 2)))
5922           (if found
5923               (let (ref)
5924                 (beginning-of-line)
5925                 (and
5926                  (setq header (gnus-nov-parse-line 
5927                                (read (current-buffer)) deps))
5928                  (setq ref (header-references header))
5929                  (string-match "\\(<[^>]+>\\) *$" ref)
5930                  (substring ref (match-beginning 1) (match-end 1))))))
5931       (and header
5932            (setq gnus-newsgroup-headers (cons header gnus-newsgroup-headers)
5933                  gnus-newsgroup-ancient (cons (header-number header)
5934                                               gnus-newsgroup-ancient))))))
5935
5936 ;; Re-build the thread containing ID.
5937 (defun gnus-rebuild-thread (id)
5938   (let ((dep gnus-newsgroup-dependencies)
5939         (buffer-read-only nil)
5940         parent headers refs thread art)
5941     (while (and id (setq headers
5942                          (car (setq art (gnus-gethash (downcase id) dep)))))
5943       (setq parent art)
5944       (setq id (and (setq refs (header-references headers))
5945                     (string-match "\\(<[^>]+>\\) *$" refs)
5946                     (substring refs (match-beginning 1) (match-end 1)))))
5947     (setq thread (gnus-make-sub-thread (car parent)))
5948     (gnus-rebuild-remove-articles thread)
5949     (let ((beg (point)))
5950       (gnus-summary-prepare-threads (list thread) 0)
5951       (gnus-summary-update-lines beg (point)))))
5952
5953 ;; Delete all lines in the summary buffer that correspond to articles
5954 ;; in this thread.
5955 (defun gnus-rebuild-remove-articles (thread)
5956   (and (gnus-summary-goto-subject (header-number (car thread)))
5957        (gnus-delete-line))
5958   (mapcar (lambda (th) (gnus-rebuild-remove-articles th)) (cdr thread)))
5959
5960 (defun gnus-sort-threads (threads)
5961   ;; Sort threads as specified in `gnus-thread-sort-functions'.
5962   (let ((fun gnus-thread-sort-functions))
5963     (while fun
5964       (setq threads (sort threads (car fun))
5965             fun (cdr fun))))
5966   threads)
5967
5968 (defun gnus-thread-header (thread)
5969   ;; Return header of first article in THREAD.
5970   (if (consp thread)
5971       (if (stringp (car thread))
5972           (car (car (cdr thread)))
5973         (car thread))
5974     thread))
5975
5976 (defun gnus-thread-sort-by-number (h1 h2)
5977   "Sort threads by root article number."
5978   (let ((h1 (gnus-thread-header h1))
5979         (h2 (gnus-thread-header h2)))
5980     (< (header-number h1) (header-number h2))))
5981
5982 (defun gnus-thread-sort-by-author (h1 h2)
5983   "Sort threads by root author."
5984   (let ((h1 (gnus-thread-header h1))
5985         (h2 (gnus-thread-header h2)))
5986     (string-lessp
5987      (let ((extract (funcall 
5988                      gnus-extract-address-components (header-from h1))))
5989        (or (car extract) (cdr extract)))
5990      (let ((extract (funcall
5991                      gnus-extract-address-components (header-from h2))))
5992        (or (car extract) (cdr extract))))))
5993
5994 (defun gnus-thread-sort-by-subject (h1 h2)
5995   "Sort threads by root subject."
5996   (let ((h1 (gnus-thread-header h1))
5997         (h2 (gnus-thread-header h2)))
5998     (string-lessp
5999      (downcase (gnus-simplify-subject (header-subject h1)))
6000      (downcase (gnus-simplify-subject (header-subject h2))))))
6001
6002 (defun gnus-thread-sort-by-date (h1 h2)
6003   "Sort threads by root article date."
6004   (let ((h1 (gnus-thread-header h1))
6005         (h2 (gnus-thread-header h2)))
6006     (string-lessp
6007      (gnus-sortable-date (header-date h1))
6008      (gnus-sortable-date (header-date h2)))))
6009
6010 (defun gnus-thread-sort-by-score (h1 h2)
6011   "Sort threads by root article score.
6012 Unscored articles will be counted as having a score of zero."
6013   (let ((h1 (gnus-thread-header h1))
6014         (h2 (gnus-thread-header h2)))
6015     (let ((s1 (assq (header-number h1) gnus-newsgroup-scored))
6016           (s2 (assq (header-number h2) gnus-newsgroup-scored)))
6017       (> (or (cdr s1) gnus-summary-default-score 0)
6018          (or (cdr s2) gnus-summary-default-score 0)))))
6019
6020 (defun gnus-thread-sort-by-total-score (h1 h2)
6021   "Sort threads by the sum of all scores in the thread.
6022 Unscored articles will be counted as having a score of zero."
6023   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
6024
6025 (defun gnus-thread-total-score (thread)
6026   ;;  This function find the total score of THREAD.
6027   (if (consp thread)
6028       (if (stringp (car thread))
6029           (apply gnus-thread-score-function 0
6030                  (mapcar 'gnus-thread-total-score-1 (cdr thread)))
6031         (gnus-thread-total-score-1 thread))
6032     (gnus-thread-total-score-1 (list thread))))
6033
6034 (defun gnus-thread-total-score-1 (root)
6035   ;; This function find the total score of the thread below ROOT.
6036   (setq root (car root))
6037   (apply gnus-thread-score-function
6038          (or (cdr (assq (header-number root) gnus-newsgroup-scored))
6039              gnus-summary-default-score 0)
6040          (mapcar 'gnus-thread-total-score
6041                  (cdr (gnus-gethash (downcase (header-id root))
6042                                     gnus-newsgroup-dependencies)))))
6043
6044 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
6045 (defvar gnus-tmp-prev-subject "")
6046 (defvar gnus-tmp-adopt-thread nil)
6047
6048 ;; Basic ideas by Paul Dworkin <paul@media-lab.media.mit.edu>.
6049 (defun gnus-summary-prepare-threads 
6050   (threads level &optional not-child no-subject cull)
6051   "Prepare summary buffer from THREADS and indentation LEVEL.  
6052 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'  
6053 or a straight list of headers."
6054   (let (thread header number subject clevel)
6055     (while threads
6056       (setq thread (car threads)
6057             threads (cdr threads))
6058       ;; If `thread' is a cons, hierarchical threads are used.  If not,
6059       ;; `thread' is the header.
6060       (if (consp thread)
6061           (setq header (car thread))
6062         (setq header thread)
6063         (and cull
6064              (or (memq (setq number (header-number header))
6065                        gnus-newsgroup-dormant)
6066                  (and gnus-summary-expunge-below
6067                       (< (or (cdr (assq number gnus-newsgroup-scored))
6068                              gnus-summary-default-score 0)
6069                          gnus-summary-expunge-below)))
6070              (progn
6071                (setq header nil)
6072                (setq gnus-newsgroup-unreads 
6073                      (delq number gnus-newsgroup-unreads)))))
6074       (cond 
6075        ((stringp header)
6076         ;; The header is a dummy root.
6077         (cond ((eq gnus-summary-make-false-root 'adopt)
6078                ;; We let the first article adopt the rest.
6079                (let ((gnus-tmp-adopt-thread (list (cdr thread))))
6080                  (gnus-summary-prepare-threads (list (car (cdr thread))) 0))
6081                (setq thread (cdr (cdr thread)))
6082                (while thread
6083                  (gnus-summary-prepare-threads (list (car thread)) 1 t)
6084                  (setq thread (cdr thread))))
6085               ((eq gnus-summary-make-false-root 'dummy)
6086                ;; We output a dummy root.
6087                (gnus-summary-insert-dummy-line 
6088                 nil header (header-number (car (car (cdr thread)))))
6089                (setq clevel 1))
6090               ((eq gnus-summary-make-false-root 'empty)
6091                ;; We print the articles with empty subject fields. 
6092                (let ((gnus-tmp-adopt-thread (list (cdr thread))))
6093                  (gnus-summary-prepare-threads (list (car (cdr thread))) 0))
6094                (setq thread (cdr (cdr thread)))
6095                (while thread
6096                  (gnus-summary-prepare-threads 
6097                   (list (car thread)) 0 nil
6098                   (not (and (eq gnus-summary-gather-subject-limit 'fuzzy)
6099                             (not (string=  
6100                                   (gnus-simplify-subject-re 
6101                                    (header-subject (car (car thread))))
6102                                   (gnus-simplify-subject-re header))))))
6103                  (setq thread (cdr thread))))
6104               (t
6105                ;; We do not make a root for the gathered
6106                ;; sub-threads at all.  
6107                (setq clevel 0)))
6108         ;; Print the sub-threads.
6109         (and (consp thread) (cdr thread)
6110              (gnus-summary-prepare-threads (cdr thread) clevel)))
6111        ;; The header is a real article.
6112        (header
6113         (setq number (header-number header)
6114               subject (header-subject header))
6115         (and gnus-newsgroup-async
6116              (setq gnus-newsgroup-threads
6117                    (cons (cons (header-number header)
6118                                (header-lines header)) gnus-newsgroup-threads)))
6119         (gnus-summary-insert-line
6120          nil header level nil 
6121          (cond ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
6122                ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
6123                ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
6124                ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
6125                (t gnus-ancient-mark))
6126          (memq number gnus-newsgroup-replied)
6127          (memq number gnus-newsgroup-expirable)
6128          (if no-subject 
6129              gnus-summary-same-subject
6130            (if (or (zerop level)
6131                    (and gnus-thread-ignore-subject
6132                         (not (string= 
6133                               (gnus-simplify-subject-re gnus-tmp-prev-subject)
6134                               (gnus-simplify-subject-re subject)))))
6135                subject
6136              gnus-summary-same-subject))
6137          not-child
6138          (cdr (assq number gnus-newsgroup-scored)))
6139         (setq gnus-tmp-prev-subject subject)
6140         ;; Recursively print subthreads.
6141         (and (consp thread) (cdr thread)
6142              (gnus-summary-prepare-threads (cdr thread) (1+ level))))))))
6143
6144 (defun gnus-select-newsgroup (group &optional read-all)
6145   "Select newsgroup GROUP.
6146 If READ-ALL is non-nil, all articles in the group are selected."
6147   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
6148          (info (nth 2 entry))
6149          articles header-marks)
6150     (gnus-check-news-server
6151      (setq gnus-current-select-method (gnus-find-method-for-group group)))
6152
6153     (or (gnus-server-opened gnus-current-select-method)
6154         (gnus-open-server gnus-current-select-method)
6155         (error "Couldn't open server"))
6156     
6157     (or (and (eq (car entry) t)
6158              (gnus-activate-newsgroup (car info)))
6159         (gnus-request-group group t)
6160         (progn
6161           (kill-buffer (current-buffer))
6162           (error "Couldn't request group %s: %s" 
6163                  group (gnus-status-message group))))
6164
6165     (setq gnus-newsgroup-name group)
6166     (setq gnus-newsgroup-unselected nil)
6167     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
6168
6169     (and gnus-asynchronous
6170          (gnus-check-backend-function 
6171           'request-asynchronous gnus-newsgroup-name)
6172          (setq gnus-newsgroup-async
6173                (gnus-request-asynchronous gnus-newsgroup-name)))
6174
6175     (setq articles (gnus-articles-to-read group read-all))
6176
6177     (cond 
6178      ((null articles) 
6179       (gnus-message 3 "Couldn't select newsgroup")
6180       'quit)
6181      ((eq articles 0) nil)
6182      (t
6183       ;; Init the dependencies hash table.
6184       (setq gnus-newsgroup-dependencies 
6185             (gnus-make-hashtable (length articles)))
6186       ;; Retrieve the headers and read them in.
6187       (setq gnus-newsgroup-headers 
6188             (if (eq 'nov (setq gnus-headers-retrieved-by
6189                                (gnus-retrieve-headers 
6190                                 (if (and gnus-fetch-old-headers 
6191                                          (not (eq 1 (car articles))))
6192                                     (cons 1 articles)
6193                                   articles)
6194                                 gnus-newsgroup-name)))
6195                 (progn
6196                   (gnus-get-newsgroup-headers-xover articles))
6197               ;; If we were to fetch old headers, but the backend didn't
6198               ;; support XOVER, then it is possible we fetched one article
6199               ;; that we shouldn't have. If that's the case, we pop it off the
6200               ;; list of headers.
6201               (if (not gnus-fetch-old-headers)
6202                   ()
6203                 (save-excursion
6204                   (set-buffer nntp-server-buffer)
6205                   (goto-char (point-min))
6206                   (and (looking-at "[0-9]+[ \t]+1[ \t]")
6207                        (delete-region 
6208                         (point) 
6209                         (search-forward "\n.\n" nil t)))))
6210               (gnus-get-newsgroup-headers)))
6211       ;; Remove canceled articles from the list of unread articles.
6212       (setq gnus-newsgroup-unreads
6213             (gnus-set-sorted-intersection 
6214              gnus-newsgroup-unreads
6215              (mapcar (lambda (headers) (header-number headers))
6216                      gnus-newsgroup-headers)))
6217       ;; Adjust and set lists of article marks.
6218       (and info
6219            (let (marked)
6220              (gnus-adjust-marked-articles info)
6221              (setq gnus-newsgroup-marked 
6222                    (cdr (assq 'tick (setq marked (nth 3 info)))))
6223              (setq gnus-newsgroup-replied (cdr (assq 'reply marked)))
6224              (setq gnus-newsgroup-expirable (cdr (assq 'expire marked)))
6225              (setq gnus-newsgroup-killed (cdr (assq 'killed marked)))
6226              (setq gnus-newsgroup-bookmarks (cdr (assq 'bookmark marked)))
6227              (setq gnus-newsgroup-dormant (cdr (assq 'dormant marked)))
6228              (setq gnus-newsgroup-scored (cdr (assq 'score marked)))
6229              (setq gnus-newsgroup-processable nil)))
6230       ;; Check whether auto-expire is to be done in this group.
6231       (setq gnus-newsgroup-auto-expire
6232             (or (and (stringp gnus-auto-expirable-newsgroups)
6233                      (string-match gnus-auto-expirable-newsgroups group))
6234                 (memq 'auto-expire (nth 5 info))))
6235       ;; First and last article in this newsgroup.
6236       (and gnus-newsgroup-headers
6237            (setq gnus-newsgroup-begin 
6238                  (header-number (car gnus-newsgroup-headers)))
6239            (setq gnus-newsgroup-end
6240                  (header-number (gnus-last-element gnus-newsgroup-headers))))
6241       (setq gnus-reffed-article-number -1)
6242       ;; GROUP is successfully selected.
6243       (or gnus-newsgroup-headers t)))))
6244
6245 (defun gnus-articles-to-read (group read-all)
6246   ;; Find out what articles the user wants to read.
6247   (let* ((articles
6248           ;; Select all articles if `read-all' is non-nil, or if all the
6249           ;; unread articles are dormant articles.
6250           (if (or (and read-all (not (numberp read-all)))
6251                   (= (length gnus-newsgroup-unreads) 
6252                      (length gnus-newsgroup-dormant)))
6253               (gnus-uncompress-range 
6254                (gnus-gethash group gnus-active-hashtb))
6255             gnus-newsgroup-unreads))
6256          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
6257          (scored (length scored-list))
6258          (number (length articles))
6259          (marked (+ (length gnus-newsgroup-marked)
6260                     (length gnus-newsgroup-dormant)))
6261          (select
6262           (cond 
6263            ((numberp read-all)
6264             read-all)
6265            (t
6266             (condition-case ()
6267                 (cond ((and (or (<= scored marked)
6268                                 (= scored number))
6269                             (numberp gnus-large-newsgroup)
6270                             (> number gnus-large-newsgroup))
6271                        (let ((input
6272                               (read-string
6273                                (format
6274                                 "How many articles from %s (default %d): "
6275                                 gnus-newsgroup-name number))))
6276                          (if (string-equal input "")
6277                              number input)))
6278                       ((and (> scored marked) (< scored number))
6279                        (let ((input
6280                               (read-string
6281                                (format 
6282                                 "%s %s (%d scored, %d total): "
6283                                 "How many articles from"
6284                                 group scored number))))
6285                          (if (string-equal input "")
6286                              number input)))
6287                       (t number))
6288               (quit nil)))))
6289          total-articles)
6290     (setq select (if (stringp select) (string-to-number select) select))
6291     (if (or (null select) (zerop select))
6292         select
6293       (if (and (not (zerop scored)) (<= (abs select) scored))
6294           (progn
6295             (setq articles (sort scored-list '<))
6296             (setq number (length articles)))
6297         (setq articles (copy-sequence articles)))
6298
6299       (setq total-articles articles)
6300       
6301       (if (< (abs select) number)
6302           (if (< select 0) 
6303               ;; Select the N oldest articles.
6304               (setcdr (nthcdr (1- (abs select)) articles) nil)
6305             ;; Select the N most recent articles.
6306             (setq articles (nthcdr (- number select) articles))))
6307       (setq gnus-newsgroup-unselected
6308             (gnus-sorted-intersection
6309              gnus-newsgroup-unreads
6310              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
6311       articles)))
6312
6313 (defun gnus-killed-articles (killed articles)
6314   (let (out)
6315     (while articles
6316       (if (inline (gnus-member-of-range (car articles) killed))
6317           (setq out (cons (car articles) out)))
6318       (setq articles (cdr articles)))
6319     out))
6320
6321 (defun gnus-adjust-marked-articles (info &optional active)
6322   "Remove all marked articles that are no longer legal."
6323   (let ((marked-lists (nth 3 info))
6324         (active (or active (gnus-gethash (car info) gnus-active-hashtb)))
6325         marked m prev)
6326     ;; There are many types of marked articles.
6327     (while marked-lists
6328       (setq m (cdr (setq prev (car marked-lists))))
6329       (cond ((or (eq 'tick (car prev)) (eq 'dormant (car prev)))
6330              ;; Make sure that all ticked articles are a subset of the
6331              ;; unread/unselected articles.
6332              (while m
6333                (if (or (memq (car m) gnus-newsgroup-unreads)
6334                        (memq (car m) gnus-newsgroup-unselected))
6335                    (setq prev m)
6336                  (setcdr prev (cdr m)))
6337                (setq m (cdr m))))
6338             ((eq 'score (car prev))
6339              ;; Scored articles should be a subset of
6340              ;; unread/unselected articles. 
6341              (while m
6342                (if (or (memq (car (car m)) gnus-newsgroup-unreads)
6343                        (memq (car (car m)) gnus-newsgroup-unreads))
6344                    (setq prev m)
6345                  (setcdr prev (cdr m)))
6346                (setq m (cdr m))))
6347             ((eq 'bookmark (car prev))
6348              ;; Bookmarks should be a subset of active articles.
6349              (while m
6350                (if (< (car (car m)) (car active))
6351                    (setcdr prev (cdr m))
6352                  (setq prev m))
6353                (setq m (cdr m))))
6354             ((eq 'killed (car prev))
6355              ;; Articles that have been through the kill process are
6356              ;; to be a subset of active articles.
6357              (while (and m (< (or (and (numberp (car m)) (car m))
6358                                   (cdr (car m)))
6359                               (car active)))
6360                (setcdr prev (cdr m))
6361                (setq m (cdr m)))
6362              (if (and m (< (or (and (numberp (car m)) (car m))
6363                                (car (car m)))
6364                            (car active))) 
6365                  (setcar (if (numberp (car m)) m (car m)) (car active))))
6366             ((or (eq 'reply (car prev)) (eq 'expire (car prev)))
6367              ;; The replied and expirable articles have to be articles
6368              ;; that are active. 
6369              (while m
6370                (if (< (car m) (car active))
6371                    (setcdr prev (cdr m))
6372                  (setq prev m))
6373                (setq m (cdr m)))))
6374       (setq marked-lists (cdr marked-lists)))
6375     ;; Remove all lists that are empty.
6376     (setq marked-lists (nth 3 info))
6377     (if marked-lists
6378         (progn
6379           (while (= 1 (length (car marked-lists)))
6380             (setq marked-lists (cdr marked-lists)))
6381           (setq m (cdr (setq prev marked-lists)))
6382           (while m
6383             (if (= 1 (length (car m)))
6384                 (setcdr prev (cdr m))
6385               (setq prev m))
6386             (setq m (cdr m)))
6387           (setcar (nthcdr 3 info) marked-lists)))
6388     ;; Finally, if there are no marked lists at all left, and if there
6389     ;; are no elements after the lists in the info list, we just chop
6390     ;; the info list off before the marked lists.
6391     (and (null marked-lists) 
6392          (not (nthcdr 4 info))
6393          (setcdr (nthcdr 2 info) nil)))
6394   info)
6395
6396 (defun gnus-set-marked-articles 
6397   (info ticked replied expirable killed dormant bookmark score) 
6398   "Enter the various lists of marked articles into the newsgroup info list."
6399   (let (newmarked)
6400     (and ticked (setq newmarked (cons (cons 'tick ticked) nil)))
6401     (and replied (setq newmarked (cons (cons 'reply replied) newmarked)))
6402     (and expirable (setq newmarked (cons (cons 'expire expirable) 
6403                                          newmarked)))
6404     (and killed (setq newmarked (cons (cons 'killed killed) newmarked)))
6405     (and dormant (setq newmarked (cons (cons 'dormant dormant) newmarked)))
6406     (and bookmark (setq newmarked (cons (cons 'bookmark bookmark) 
6407                                         newmarked)))
6408     (and score (setq newmarked (cons (cons 'score score) newmarked)))
6409     (if (nthcdr 3 info)
6410         (progn
6411           (setcar (nthcdr 3 info) newmarked)
6412           (and (not newmarked)
6413                (not (nthcdr 4 info))
6414                (setcdr (nthcdr 2 info) nil)))
6415       (if newmarked
6416           (setcdr (nthcdr 2 info) (list newmarked))))))
6417
6418 (defun gnus-add-marked-articles (group type articles &optional info force)
6419   ;; Add ARTICLES of TYPE to the info of GROUP.
6420   ;; If INFO is non-nil, use that info. If FORCE is non-nil, don't
6421   ;; add, but replace marked articles of TYPE with ARTICLES.
6422   (let ((info (or info (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
6423         marked m)
6424     (or (not info)
6425         (and (not (setq marked (nthcdr 3 info)))
6426              (setcdr (nthcdr 2 info) (list (list (cons type articles)))))
6427         (and (not (setq m (assq type (car marked))))
6428              (setcar marked (cons (cons type articles) (car marked))))
6429         (if force
6430             (setcdr m articles)
6431           (nconc m articles)))))
6432          
6433 (defun gnus-set-mode-line (where)
6434   "This function sets the mode line of the article or summary buffers.
6435 If WHERE is `summary', the summary mode line format will be used."
6436   (if (memq where gnus-updated-mode-lines)
6437       (let (mode-string)
6438         (save-excursion
6439           (set-buffer gnus-summary-buffer)
6440           (let* ((mformat (if (eq where 'article) 
6441                               gnus-article-mode-line-format-spec
6442                             gnus-summary-mode-line-format-spec))
6443                  (group-name gnus-newsgroup-name)
6444                  (article-number (or gnus-current-article 0))
6445                  (unread (- (length gnus-newsgroup-unreads)
6446                             (length gnus-newsgroup-dormant)))
6447                  (unread-and-unticked 
6448                   (- unread (length gnus-newsgroup-marked)))
6449                  (unselected (length gnus-newsgroup-unselected))
6450                  (unread-and-unselected
6451                   (cond ((and (zerop unread-and-unticked)
6452                               (zerop unselected)) "")
6453                         ((zerop unselected) 
6454                          (format "{%d more}" unread-and-unticked))
6455                         (t (format "{%d(+%d) more}"
6456                                    unread-and-unticked unselected))))
6457                  (subject
6458                   (if gnus-current-headers
6459                       (header-subject gnus-current-headers) ""))
6460                  (max-len (and gnus-mode-non-string-length
6461                                (- (frame-width) gnus-mode-non-string-length)))
6462                  header) ;; passed as argument to any user-format-funcs
6463             (setq mode-string (eval mformat))
6464             (or (numberp max-len)
6465                 (setq max-len (length mode-string)))
6466             (if (< max-len 4) (setq max-len 4))
6467             (if (> (length mode-string) max-len)
6468                 (setq mode-string 
6469                       (concat (substring mode-string 0 (- max-len 3))
6470                               "...")))
6471             (setq mode-string (format (format "%%-%ds" max-len)
6472                                       mode-string))))
6473         (setq mode-line-buffer-identification mode-string)
6474         (set-buffer-modified-p t))))
6475
6476 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
6477   "Go through the HEADERS list and add all Xrefs to a hash table.
6478 The resulting hash table is returned, or nil if no Xrefs were found."
6479   (let* ((from-method (gnus-find-method-for-group from-newsgroup))
6480          (prefix (if (and 
6481                       (gnus-group-foreign-p from-newsgroup)
6482                       (not (memq 'virtual 
6483                                  (assoc (symbol-name (car from-method))
6484                                         gnus-valid-select-methods))))
6485                      (gnus-group-real-prefix from-newsgroup)))
6486          (xref-hashtb (make-vector 63 0))
6487          start group entry number xrefs header)
6488     (while headers
6489       (setq header (car headers))
6490       (if (and (setq xrefs (header-xref header))
6491                (not (memq (header-number header) unreads)))
6492           (progn
6493             (setq start 0)
6494             (while (string-match "\\([^ ]+\\):\\([0-9]+\\)" xrefs start)
6495               (setq start (match-end 0))
6496               (setq group (concat prefix (substring xrefs (match-beginning 1) 
6497                                                     (match-end 1))))
6498               (setq number 
6499                     (string-to-int (substring xrefs (match-beginning 2) 
6500                                               (match-end 2))))
6501               (if (setq entry (gnus-gethash group xref-hashtb))
6502                   (setcdr entry (cons number (cdr entry)))
6503                 (gnus-sethash group (cons number nil) xref-hashtb)))))
6504       (setq headers (cdr headers)))
6505     (if start xref-hashtb nil)))
6506
6507 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads expirable)
6508   "Look through all the headers and mark the Xrefs as read."
6509   (let ((virtual (memq 'virtual 
6510                        (assoc (symbol-name (car (gnus-find-method-for-group 
6511                                                  from-newsgroup)))
6512                               gnus-valid-select-methods)))
6513         name entry read info xref-hashtb idlist active num range exps method
6514         nth4)
6515     (save-excursion
6516       (set-buffer gnus-group-buffer)
6517       (if (setq xref-hashtb 
6518                 (gnus-create-xref-hashtb from-newsgroup headers unreads))
6519           (mapatoms 
6520            (lambda (group)
6521              (if (string= from-newsgroup (setq name (symbol-name group)))
6522                  ()
6523                (setq idlist (symbol-value group))
6524                ;; Dead groups are not updated.
6525                (if (and (prog1 
6526                             (setq entry (gnus-gethash name gnus-newsrc-hashtb)
6527                                   info (nth 2 entry))
6528                           (if (stringp (setq nth4 (nth 4 info)))
6529                               (setq nth4 (gnus-server-to-method nth4))))
6530                         ;; Only do the xrefs if the group has the same
6531                         ;; select method as the group we have just read.
6532                         (or (gnus-methods-equal-p 
6533                              nth4 (gnus-find-method-for-group from-newsgroup))
6534                             virtual
6535                             (equal nth4 
6536                                    (setq method (gnus-find-method-for-group 
6537                                                  from-newsgroup)))
6538                             (and (equal (car nth4) (car method))
6539                                  (equal (nth 1 nth4) (nth 1 method))))
6540                         gnus-use-cross-reference
6541                         (or (not (eq gnus-use-cross-reference t))
6542                             virtual
6543                             ;; Only do cross-references on subscribed
6544                             ;; groups, if that is what is wanted.  
6545                             (<= (nth 1 info) gnus-level-subscribed)))
6546                    (progn
6547                      (setq num 0)
6548                      ;; Set the new list of read articles in this group.
6549                      (setq active (gnus-gethash name gnus-active-hashtb))
6550                      ;; First peel off all illegal article numbers.
6551                      (if active
6552                          (let ((ids idlist)
6553                                (ticked (cdr (assq 'tick (nth 3 info))))
6554                                (dormant (cdr (assq 'dormant (nth 3 info))))
6555                                id)
6556                            (setq exps nil)
6557                            (while ids
6558                              (setq id (car ids))
6559                              (if (or (> id (cdr active))
6560                                      (< id (car active))
6561                                      (memq id ticked)
6562                                      (memq id dormant))
6563                                  (setq idlist (delq id idlist)))
6564                              (and (memq id expirable)
6565                                   (setq exps (cons id exps)))
6566                              (setq ids (cdr ids)))))
6567                      ;; Update expirable articles.
6568                      (gnus-add-marked-articles nil 'expirable exps info)
6569                      (and active
6570                           (null (nth 2 info))
6571                           (> (car active) 1)
6572                           (setcar (nthcdr 2 info) (cons 1 (1- (car active)))))
6573                      (setcar (nthcdr 2 info)
6574                              (setq range
6575                                    (gnus-add-to-range 
6576                                     (nth 2 info) 
6577                                     (setq idlist (sort idlist '<)))))
6578                      ;; Then we have to re-compute how many unread
6579                      ;; articles there are in this group.
6580                      (if active
6581                          (progn
6582                            (cond 
6583                             ((not range)
6584                              (setq num (- (1+ (cdr active)) (car active))))
6585                             ((not (listp (cdr range)))
6586                              (setq num (- (cdr active) (- (1+ (cdr range)) 
6587                                                           (car range)))))
6588                             (t
6589                              (while range
6590                                (if (numberp (car range))
6591                                    (setq num (1+ num))
6592                                  (setq num (+ num (- (1+ (cdr (car range)))
6593                                                      (car (car range))))))
6594                                (setq range (cdr range)))
6595                              (setq num (- (cdr active) num))))
6596                            ;; Update the number of unread articles.
6597                            (setcar 
6598                             entry 
6599                             (max 0 (- num 
6600                                       (length (cdr (assq 'tick (nth 3 info))))
6601                                       (length 
6602                                        (cdr (assq 'dormant (nth 3 info)))))))
6603                            ;; Update the group buffer.
6604                            (gnus-group-update-group name t)))))))
6605            xref-hashtb)))))
6606
6607 (defun gnus-methods-equal-p (m1 m2)
6608   (let ((m1 (or m1 gnus-select-method))
6609         (m2 (or m2 gnus-select-method)))
6610     (or (equal m1 m2)
6611         (and (eq (car m1) (car m2))
6612              (or (not (memq 'address (assoc (symbol-name (car m1))
6613                                             gnus-valid-select-methods)))
6614                  (equal (nth 1 m1) (nth 1 m2)))))))
6615
6616 (defsubst gnus-header-value ()
6617   (buffer-substring (match-end 0) (gnus-point-at-eol)))
6618
6619 (defvar gnus-newsgroup-none-id 0)
6620
6621 (defun gnus-get-newsgroup-headers ()
6622   (setq gnus-article-internal-prepare-hook nil)
6623   (let ((cur nntp-server-buffer)
6624         (dependencies gnus-newsgroup-dependencies)
6625         headers char article id dep end)
6626     (save-excursion
6627       (set-buffer nntp-server-buffer)
6628       (goto-char (point-min))
6629       ;; Search to the beginning of the next header. Error messages
6630       ;; do not begin with 2 or 3.
6631       (while (re-search-forward "^[23][0-9]+ " nil t)
6632         (let ((header (make-vector 9 nil))
6633               (c (following-char))
6634               (case-fold-search t)
6635               (p (point))
6636               from subject in-reply-to references ref)
6637           (setq id nil
6638                 ref nil
6639                 references nil
6640                 subject nil
6641                 from nil)
6642           (header-set-number header (setq article (read cur)))
6643           ;; This implementation of this function, with nine
6644           ;; search-forwards instead of the one re-search-forward and
6645           ;; a case (which basically was the old function) is actually
6646           ;; about twice as fast, even though it looks messier. You
6647           ;; can't have everything, I guess. Speed and elegance
6648           ;; doesn't always come hand in hand.
6649           (save-restriction
6650             (narrow-to-region (point) (or (save-excursion 
6651                                             (search-forward "\n.\n" nil t))
6652                                           (point)))
6653             (if (search-forward "\nfrom: " nil t)
6654                 (header-set-from header (gnus-header-value))
6655               (header-set-from header "(nobody)"))
6656             (goto-char p)
6657             (if (search-forward "\nsubject: " nil t)
6658                 (header-set-subject header (gnus-header-value))
6659               (header-set-subject header "(none)"))
6660             (goto-char p)
6661             (and (search-forward "\nxref: " nil t)
6662                  (header-set-xref header (gnus-header-value)))
6663             (goto-char p)
6664             (or (numberp (and (search-forward "\nlines: " nil t)
6665                               (header-set-lines header (read cur))))
6666                 (header-set-lines header 0))
6667             (goto-char p)
6668             (and (search-forward "\ndate: " nil t)
6669                  (header-set-date header (gnus-header-value)))
6670             (goto-char p)
6671             (if (search-forward "\nmessage-id: " nil t)
6672                 (header-set-id header (setq id (gnus-header-value)))
6673               ;; If there was no message-id, we just fake one to make
6674               ;; subsequent routines simpler.
6675               (header-set-id 
6676                header 
6677                (setq id (concat "none+" 
6678                                 (int-to-string 
6679                                  (setq gnus-newsgroup-none-id 
6680                                        (1+ gnus-newsgroup-none-id)))))))
6681             (goto-char p)
6682             (if (search-forward "\nreferences: " nil t)
6683                 (progn
6684                   (header-set-references header (gnus-header-value))
6685                   (setq end (match-end 0))
6686                   (save-excursion
6687                     (setq ref 
6688                           (downcase
6689                            (buffer-substring
6690                             (progn 
6691                               (end-of-line)
6692                               (search-backward ">" end t)
6693                               (1+ (point)))
6694                             (progn
6695                               (search-backward "<" end t)
6696                               (point)))))))
6697               ;; Get the references from the in-reply-to header if there
6698               ;; ware no references and the in-reply-to header looks
6699               ;; promising. 
6700               (if (and (search-forward "\nin-reply-to: " nil t)
6701                        (setq in-reply-to (gnus-header-value))
6702                        (string-match "<[^>]+>" in-reply-to))
6703                   (progn
6704                     (header-set-references 
6705                      header 
6706                      (setq ref (substring in-reply-to (match-beginning 0)
6707                                           (match-end 0))))
6708                     (setq ref (downcase ref)))
6709                 (setq ref "none")))
6710             ;; We do some threading while we read the headers. The
6711             ;; message-id and the last reference are both entered into
6712             ;; the same hash table. Some tippy-toeing around has to be
6713             ;; done in case an article has arrived before the article
6714             ;; which it refers to.
6715             (if (boundp (setq dep (intern (downcase id) dependencies)))
6716                 (if (car (symbol-value dep))
6717                     ;; An article with this Message-ID has already
6718                     ;; been seen, so we ignore this one, except we add
6719                     ;; any additional Xrefs (in case the two articles
6720                     ;; came from different servers.
6721                     (progn
6722                       (header-set-xref 
6723                        (car (symbol-value dep))
6724                        (concat (or (header-xref (car (symbol-value dep))) "")
6725                                (or (header-xref header) "")))
6726                       (setq header nil))
6727                   (setcar (symbol-value dep) header))
6728               (set dep (list header)))
6729             (if header
6730                 (progn
6731                   (if (boundp (setq dep (intern ref dependencies)))
6732                       (setcdr (symbol-value dep) 
6733                               (cons header (cdr (symbol-value dep))))
6734                     (set dep (list nil header)))
6735                   (setq headers (cons header headers))))
6736             (goto-char (point-max))))))
6737     (nreverse headers)))
6738
6739 ;; The following macros and functions were written by Felix Lee
6740 ;; <flee@cse.psu.edu>. 
6741
6742 (defmacro gnus-nov-read-integer ()
6743   '(prog1
6744        (if (= (following-char) ?\t)
6745            0
6746          (let ((num (condition-case nil (read buffer) (error nil))))
6747            (if (numberp num) num 0)))
6748      (or (eobp) (forward-char 1))))
6749
6750 (defmacro gnus-nov-skip-field ()
6751   '(search-forward "\t" eol 'move))
6752
6753 (defmacro gnus-nov-field ()
6754   '(buffer-substring (point) (if (gnus-nov-skip-field) (1- (point)) eol)))
6755
6756 ;; Goes through the xover lines and returns a list of vectors
6757 (defun gnus-get-newsgroup-headers-xover (sequence)
6758   "Parse the news overview data in the server buffer, and return a
6759 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
6760   ;; Get the Xref when the users reads the articles since most/some
6761   ;; NNTP servers do not include Xrefs when using XOVER.
6762   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
6763   (let ((cur nntp-server-buffer)
6764         (dependencies gnus-newsgroup-dependencies)
6765         (none 0)
6766         number headers header)
6767     (save-excursion
6768       (set-buffer nntp-server-buffer)
6769       (goto-char (point-min))
6770       (while (and sequence (not (eobp)))
6771         (setq number (read cur))
6772         (while (and sequence (< (car sequence) number))
6773           (setq sequence (cdr sequence)))
6774         (and sequence 
6775              (eq number (car sequence))
6776              (progn
6777                (setq sequence (cdr sequence))
6778                (if (setq header 
6779                          (inline (gnus-nov-parse-line number dependencies)))
6780                    (setq headers (cons header headers)))))
6781         (forward-line 1))
6782       (setq headers (nreverse headers)))
6783     headers))
6784
6785 ;; This function has to be called with point after the article number
6786 ;; on the beginning of the line.
6787 (defun gnus-nov-parse-line (number dependencies)
6788   (let ((none 0)
6789         (eol (gnus-point-at-eol)) 
6790         (buffer (current-buffer))
6791         header ref id dep)
6792
6793     ;; overview: [num subject from date id refs chars lines misc]
6794     (narrow-to-region (point) eol)
6795     (forward-char)
6796
6797     (condition-case nil
6798         (setq header
6799               (vector 
6800                number                   ; number
6801                (gnus-nov-field)         ; subject
6802                (gnus-nov-field)         ; from
6803                (gnus-nov-field)         ; date
6804                (setq id (or (gnus-nov-field)
6805                             (concat "none+"
6806                                     (int-to-string 
6807                                      (setq none (1+ none)))))) ; id
6808                (progn
6809                  (save-excursion
6810                    (let ((beg (point)))
6811                      (search-forward "\t" eol)
6812                      (if (search-backward ">" beg t)
6813                          (setq ref 
6814                                (downcase 
6815                                 (buffer-substring 
6816                                  (1+ (point))
6817                                  (progn
6818                                    (search-backward "<" beg t)
6819                                    (point)))))
6820                        (setq ref nil))))
6821                  (gnus-nov-field))      ; refs
6822                (gnus-nov-read-integer)  ; chars
6823                (gnus-nov-read-integer)  ; lines
6824                (if (= (following-char) ?\n)
6825                    nil
6826                  (gnus-nov-field))      ; misc
6827                ))
6828       (error (progn 
6829                (ding)
6830                (message "Strange nov line.")
6831                (setq header nil)
6832                (goto-char eol))))
6833
6834     (widen)
6835
6836     ;; We build the thread tree.
6837     (and header
6838          (if (boundp (setq dep (intern (downcase id) dependencies)))
6839              (if (car (symbol-value dep))
6840                  ;; An article with this Message-ID has already been seen,
6841                  ;; so we ignore this one, except we add any additional
6842                  ;; Xrefs (in case the two articles came from different
6843                  ;; servers.
6844                  (progn
6845                    (header-set-xref 
6846                     (car (symbol-value dep))
6847                     (concat (or (header-xref (car (symbol-value dep))) "")
6848                             (or (header-xref header) "")))
6849                    (setq header nil))
6850                (setcar (symbol-value dep) header))
6851            (set dep (list header))))
6852     (if header
6853         (progn
6854           (if (boundp (setq dep (intern (or ref "none") 
6855                                         dependencies)))
6856               (setcdr (symbol-value dep) 
6857                       (cons header (cdr (symbol-value dep))))
6858             (set dep (list nil header)))))
6859     header))
6860
6861 (defun gnus-article-get-xrefs ()
6862   "Fill in the Xref value in `gnus-current-headers', if necessary.
6863 This is meant to be called in `gnus-article-internal-prepare-hook'."
6864   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
6865                                  gnus-current-headers)))
6866     (or (not gnus-use-cross-reference)
6867         (not headers)
6868         (and (header-xref headers)
6869              (not (string= (header-xref headers) "")))
6870         (let ((case-fold-search t)
6871               xref)
6872           (save-restriction
6873             (gnus-narrow-to-headers)
6874             (goto-char (point-min))
6875             (if (or (and (eq (downcase (following-char)) ?x)
6876                          (looking-at "Xref:"))
6877                     (search-forward "\nXref:" nil t))
6878                 (progn
6879                   (goto-char (1+ (match-end 0)))
6880                   (setq xref (buffer-substring (point) 
6881                                                (progn (end-of-line) (point))))
6882                   (header-set-xref headers xref))))))))
6883
6884 (defalias 'gnus-find-header-by-number 'gnus-get-header-by-number)
6885 (make-obsolete 'gnus-find-header-by-number 'gnus-get-header-by-number)
6886
6887 ;; Return a header specified by a NUMBER.
6888 (defun gnus-get-header-by-number (number)
6889   (save-excursion
6890     (set-buffer gnus-summary-buffer)
6891     (or gnus-newsgroup-headers-hashtb-by-number
6892         (gnus-make-headers-hashtable-by-number))
6893     (gnus-gethash (int-to-string number)
6894                   gnus-newsgroup-headers-hashtb-by-number)))
6895
6896 (defun gnus-make-headers-hashtable-by-number ()
6897   "Make hashtable for the variable gnus-newsgroup-headers by number."
6898   (save-excursion
6899     (set-buffer gnus-summary-buffer)
6900     (let ((headers gnus-newsgroup-headers)
6901           header)
6902       (setq gnus-newsgroup-headers-hashtb-by-number
6903             (gnus-make-hashtable (length headers)))
6904       (while headers
6905         (setq header (car headers))
6906         (gnus-sethash (int-to-string (header-number header))
6907                       header gnus-newsgroup-headers-hashtb-by-number)
6908         (setq headers (cdr headers))))))
6909
6910 (defun gnus-more-header-backward ()
6911   "Find new header backward."
6912   (let ((first (car (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
6913         (artnum gnus-newsgroup-begin)
6914         (header nil))
6915     (while (and (not header)
6916                 (> artnum first))
6917       (setq artnum (1- artnum))
6918       (setq header (gnus-read-header artnum)))
6919     header))
6920
6921 (defun gnus-more-header-forward (&optional backward)
6922   "Find new header forward.
6923 If BACKWARD, find new header backward instead."
6924   (if backward
6925       (gnus-more-header-backward)
6926     (let ((last (cdr (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
6927           (artnum gnus-newsgroup-end)
6928           (header nil))
6929       (while (and (not header)
6930                   (< artnum last))
6931         (setq artnum (1+ artnum))
6932         (setq header (gnus-read-header artnum)))
6933       header)))
6934
6935 (defun gnus-extend-newsgroup (header &optional backward)
6936   "Extend newsgroup selection with HEADER.
6937 Optional argument BACKWARD means extend toward backward."
6938   (if header
6939       (let ((artnum (header-number header)))
6940         (setq gnus-newsgroup-headers
6941               (if backward
6942                   (cons header gnus-newsgroup-headers)
6943                 (nconc gnus-newsgroup-headers (list header))))
6944         (setq gnus-newsgroup-unselected
6945               (delq artnum gnus-newsgroup-unselected))
6946         (setq gnus-newsgroup-begin (min gnus-newsgroup-begin artnum))
6947         (setq gnus-newsgroup-end (max gnus-newsgroup-end artnum)))))
6948
6949 (defun gnus-summary-work-articles (n)
6950   "Return a list of articles to be worked upon. The prefix argument,
6951 the list of process marked articles, and the current article will be
6952 taken into consideration."
6953   (let (articles)
6954     (if (and n (numberp n))
6955         (let ((backward (< n 0))
6956               (n (abs n)))
6957           (save-excursion
6958             (while (and (> n 0)
6959                         (setq articles (cons (gnus-summary-article-number) 
6960                                              articles))
6961                         (gnus-summary-search-forward nil nil backward))
6962               (setq n (1- n))))
6963           (sort articles (function <)))
6964       (or (reverse gnus-newsgroup-processable)
6965           (list (gnus-summary-article-number))))))
6966
6967 (defun gnus-summary-search-group (&optional backward use-level)
6968   "Search for next unread newsgroup.
6969 If optional argument BACKWARD is non-nil, search backward instead."
6970   (save-excursion
6971     (set-buffer gnus-group-buffer)
6972     (if (gnus-group-search-forward 
6973          backward nil (if use-level (gnus-group-group-level) nil))
6974         (gnus-group-group-name))))
6975
6976 (defun gnus-summary-best-group (&optional exclude-group)
6977   "Find the name of the best unread group.
6978 If EXCLUDE-GROUP, do not go to this group."
6979   (save-excursion
6980     (set-buffer gnus-group-buffer)
6981     (save-excursion
6982       (gnus-group-best-unread-group exclude-group))))
6983
6984 (defun gnus-summary-search-subject (&optional backward unread subject)
6985   "Search for article forward.
6986 If BACKWARD is non-nil, search backward.
6987 If UNREAD is non-nil, only unread articles are selected.
6988 If SUBJECT is non-nil, the article which has the same subject will be
6989 searched for." 
6990   (let ((func (if backward 'previous-single-property-change
6991                 'next-single-property-change))
6992         (beg (point))
6993         (did t)
6994         pos psubject)
6995     (beginning-of-line)
6996     (and gnus-summary-check-current unread
6997          (eq (get-text-property (point) 'gnus-mark) gnus-unread-mark)
6998          (setq did nil))
6999     (if (not did)
7000         ()
7001       (forward-char (if backward (if (bobp) 0 -1) (if (eobp) 0 1)))
7002       (while
7003           (and 
7004            (setq pos (funcall func (point) 'gnus-number))
7005            (goto-char (if backward (1- pos) pos))
7006            (setq did
7007                  (not (and
7008                        (or (not unread)
7009                            (eq (get-text-property (point) 'gnus-mark)
7010                                gnus-unread-mark))
7011                        (or (not subject)
7012                            (and (setq psubject (gnus-summary-subject-string))
7013                                 (equal (gnus-simplify-subject-re subject)
7014                                        (gnus-simplify-subject-re
7015                                         psubject)))))))
7016            (if backward (if (bobp) nil (forward-char -1) t)
7017              (if (eobp) nil (forward-char 1) t)))))
7018     (if did
7019         (progn (goto-char beg) nil)
7020       (prog1
7021           (get-text-property (point) 'gnus-number)
7022         (gnus-summary-position-cursor)))))
7023
7024 (defun gnus-summary-search-forward (&optional unread subject backward)
7025   "Search for article forward.
7026 If UNREAD is non-nil, only unread articles are selected.
7027 If SUBJECT is non-nil, the article which has the same subject will be
7028 searched for. 
7029 If BACKWARD is non-nil, the search will be performed backwards instead."
7030   (gnus-summary-search-subject backward unread subject))
7031
7032 (defun gnus-summary-search-backward (&optional unread subject)
7033   "Search for article backward.
7034 If 1st optional argument UNREAD is non-nil, only unread article is selected.
7035 If 2nd optional argument SUBJECT is non-nil, the article which has
7036 the same subject will be searched for."
7037   (gnus-summary-search-forward unread subject t))
7038
7039 (defun gnus-summary-article-number (&optional number-or-nil)
7040   "The article number of the article on the current line.
7041 If there isn's an article number here, then we return the current
7042 article number."
7043   (let* ((number (get-text-property (gnus-point-at-bol) 'gnus-number)))
7044     (if number-or-nil number (or number gnus-current-article))))
7045
7046 (defun gnus-summary-thread-level ()
7047   "The thread level of the article on the current line."
7048   (or (get-text-property (gnus-point-at-bol) 'gnus-level)
7049       0))
7050
7051 (defun gnus-summary-pseudo-article ()
7052   "The thread level of the article on the current line."
7053   (get-text-property (gnus-point-at-bol) 'gnus-pseudo))
7054
7055 (defun gnus-summary-article-mark ()
7056   "The mark on the current line."
7057   (get-text-property (gnus-point-at-bol) 'gnus-mark))
7058
7059 (defun gnus-summary-subject-string ()
7060   "Return current subject string or nil if nothing."
7061   (let ((article (gnus-summary-article-number))
7062         header)
7063     (and article 
7064          (setq header (gnus-get-header-by-number article))
7065          (vectorp header)
7066          (header-subject header))))
7067
7068 (defalias 'gnus-summary-score 'gnus-summary-article-score)
7069 (make-obsolete 'gnus-summary-score 'gnus-summary-article-score)
7070 (defun gnus-summary-article-score ()
7071   "Return current article score."
7072   (or (cdr (assq (gnus-summary-article-number) gnus-newsgroup-scored))
7073       gnus-summary-default-score 0))
7074
7075 ;; Written by Sudish Joseph <joseph@cis.ohio-state.edu>.
7076
7077 (defun gnus-summary-recenter ()
7078   "Center point in the summary window.
7079 If `gnus-auto-center-summary' is nil, or the article buffer isn't
7080 displayed, no centering will be performed." 
7081   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
7082   ;; Recenter only when requested. Suggested by popovich@park.cs.columbia.edu.
7083   (let* ((top (cond ((< (window-height) 4) 0)
7084                     ((< (window-height) 7) 1)
7085                     (t 2)))
7086          (height (1- (window-height)))
7087          (bottom (save-excursion (goto-char (point-max))
7088                                  (forward-line (- height))
7089                                  (point)))
7090          (window (get-buffer-window (current-buffer))))
7091     (and 
7092      ;; The user has to want it,
7093      gnus-auto-center-summary 
7094      ;; the article buffer must be displayed,
7095      (get-buffer-window gnus-article-buffer)
7096      ;; Set the window start to either `bottom', which is the biggest
7097      ;; possible valid number, or the second line from the top,
7098      ;; whichever is the least.
7099      (set-window-start
7100       window (min bottom (save-excursion (forward-line (- top)) (point)))))))
7101
7102 ;; Function written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
7103 (defun gnus-short-group-name (group &optional levels)
7104   "Collapse GROUP name LEVELS."
7105   (let* ((name "") (foreign "") (depth -1) (skip 1)
7106          (levels (or levels
7107                      (progn
7108                        (while (string-match "\\." group skip)
7109                          (setq skip (match-end 0)
7110                                depth (+ depth 1)))
7111                        depth))))
7112     (if (string-match ":" group)
7113         (setq foreign (substring group 0 (match-end 0))
7114               group (substring group (match-end 0))))
7115     (while group
7116       (if (and (string-match "\\." group) (> levels 0))
7117           (setq name (concat name (substring group 0 1))
7118                 group (substring group (match-end 0))
7119                 levels (- levels 1)
7120                 name (concat name "."))
7121         (setq name (concat foreign name group)
7122               group nil)))
7123     name))
7124
7125 (defun gnus-summary-jump-to-group (newsgroup)
7126   "Move point to NEWSGROUP in group mode buffer."
7127   ;; Keep update point of group mode buffer if visible.
7128   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
7129       (save-window-excursion
7130         ;; Take care of tree window mode.
7131         (if (get-buffer-window gnus-group-buffer)
7132             (pop-to-buffer gnus-group-buffer))
7133         (gnus-group-jump-to-group newsgroup))
7134     (save-excursion
7135       ;; Take care of tree window mode.
7136       (if (get-buffer-window gnus-group-buffer)
7137           (pop-to-buffer gnus-group-buffer)
7138         (set-buffer gnus-group-buffer))
7139       (gnus-group-jump-to-group newsgroup))))
7140
7141 ;; This function returns a list of article numbers based on the
7142 ;; difference between the ranges of read articles in this group and
7143 ;; the range of active articles.
7144 (defun gnus-list-of-unread-articles (group)
7145   (let* ((read (nth 2 (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
7146          (active (gnus-gethash group gnus-active-hashtb))
7147          (last (cdr active))
7148          unread first nlast unread)
7149     ;; If none are read, then all are unread. 
7150     (if (not read)
7151         (setq first (car active))
7152       ;; If the range of read articles is a single range, then the
7153       ;; first unread article is the article after the last read
7154       ;; article. Sounds logical, doesn't it?
7155       (if (not (listp (cdr read)))
7156           (setq first (1+ (cdr read)))
7157         ;; `read' is a list of ranges.
7158         (if (/= (setq nlast (or (and (numberp (car read)) (car read)) 
7159                                 (car (car read)))) 1)
7160             (setq first 1))
7161         (while read
7162           (if first 
7163               (while (< first nlast)
7164                 (setq unread (cons first unread))
7165                 (setq first (1+ first))))
7166           (setq first (1+ (if (atom (car read)) (car read) (cdr (car read)))))
7167           (setq nlast (if (atom (car (cdr read))) 
7168                           (car (cdr read))
7169                         (car (car (cdr read)))))
7170           (setq read (cdr read)))))
7171     ;; And add the last unread articles.
7172     (while (<= first last)
7173       (setq unread (cons first unread))
7174       (setq first (1+ first)))
7175     ;; Return the list of unread articles.
7176     (nreverse unread)))
7177
7178 (defun gnus-list-of-read-articles (group)
7179   (let ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
7180         (active (gnus-gethash group gnus-active-hashtb)))
7181     (and info active
7182          (gnus-sorted-complement 
7183           (gnus-uncompress-range active) 
7184           (gnus-list-of-unread-articles group)))))
7185
7186 ;; Various summary commands
7187
7188 (defun gnus-summary-universal-argument ()
7189   "Perform any operation on all articles marked with the process mark."
7190   (interactive)
7191   (gnus-set-global-variables)
7192   (let ((articles (reverse gnus-newsgroup-processable))
7193         key func)
7194     (or articles (error "No articles marked"))
7195     (or (setq func (key-binding (read-key-sequence "C-c C-u")))
7196         (error "Undefined key"))
7197     (while articles
7198       (gnus-summary-goto-subject (car articles))
7199       (command-execute func)
7200       (gnus-summary-remove-process-mark (car articles))
7201       (setq articles (cdr articles)))))
7202
7203 (defun gnus-summary-toggle-truncation (arg)
7204   "Toggle truncation of summary lines.
7205 With arg, turn line truncation on iff arg is positive."
7206   (interactive "P")
7207   (setq truncate-lines
7208         (if (null arg) (not truncate-lines)
7209           (> (prefix-numeric-value arg) 0)))
7210   (redraw-display))
7211
7212 (defun gnus-summary-reselect-current-group (all)
7213   "Once exit and then reselect the current newsgroup.
7214 The prefix argument ALL means to select all articles."
7215   (interactive "P")
7216   (gnus-set-global-variables)
7217   (let ((current-subject (gnus-summary-article-number))
7218         (group gnus-newsgroup-name))
7219     (setq gnus-newsgroup-begin nil)
7220     (gnus-summary-exit t)
7221     ;; We have to adjust the point of group mode buffer because the
7222     ;; current point was moved to the next unread newsgroup by
7223     ;; exiting.
7224     (gnus-summary-jump-to-group group)
7225     (gnus-group-read-group all t)
7226     (gnus-summary-goto-subject current-subject)))
7227
7228 (defun gnus-summary-rescan-group (all)
7229   "Exit the newsgroup, ask for new articles, and select the newsgroup."
7230   (interactive "P")
7231   (gnus-set-global-variables)
7232   ;; Fix by Ilja Weis <kult@uni-paderborn.de>.
7233   (let ((group gnus-newsgroup-name))
7234     (gnus-summary-exit)
7235     (gnus-summary-jump-to-group group)
7236     (save-excursion
7237       (set-buffer gnus-group-buffer)
7238       (gnus-group-get-new-news-this-group 1))
7239     (gnus-summary-jump-to-group group)
7240     (gnus-group-read-group all)))
7241
7242 (defun gnus-summary-update-info ()
7243   (let* ((group gnus-newsgroup-name)
7244          (method (car (gnus-find-method-for-group group))))
7245     (if gnus-newsgroup-kill-headers
7246         (setq gnus-newsgroup-killed
7247               (gnus-compress-sequence
7248                (nconc
7249                 (gnus-set-sorted-intersection
7250                  (gnus-uncompress-range gnus-newsgroup-killed)
7251                  (setq gnus-newsgroup-unselected
7252                        (sort gnus-newsgroup-unselected '<)))
7253                 (setq gnus-newsgroup-unreads
7254                       (sort gnus-newsgroup-unreads '<))) t)))
7255     (or (listp (cdr gnus-newsgroup-killed))
7256         (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
7257     (let ((updated nil)
7258           (headers gnus-newsgroup-headers))
7259       (gnus-close-group group)
7260       (run-hooks 'gnus-exit-group-hook)
7261       (gnus-update-read-articles 
7262        group gnus-newsgroup-unreads gnus-newsgroup-unselected 
7263        gnus-newsgroup-marked
7264        t gnus-newsgroup-replied gnus-newsgroup-expirable
7265        gnus-newsgroup-killed gnus-newsgroup-dormant
7266        gnus-newsgroup-bookmarks 
7267        (and gnus-save-score gnus-newsgroup-scored))
7268       (and gnus-use-cross-reference
7269            (gnus-mark-xrefs-as-read 
7270             group headers gnus-newsgroup-unreads gnus-newsgroup-expirable))
7271       ;; Do adaptive scoring, and possibly save score files.
7272       (and gnus-newsgroup-adaptive
7273            (gnus-score-adaptive))
7274       (and gnus-use-scoring 
7275            (fboundp 'gnus-score-save)
7276            (funcall 'gnus-score-save))
7277       ;; Do not switch windows but change the buffer to work.
7278       (set-buffer gnus-group-buffer)
7279       (or (assoc 'quit-config (gnus-find-method-for-group gnus-newsgroup-name))
7280           (gnus-group-update-group group)))))
7281   
7282 (defun gnus-summary-exit (&optional temporary)
7283   "Exit reading current newsgroup, and then return to group selection mode.
7284 gnus-exit-group-hook is called with no arguments if that value is non-nil."
7285   (interactive)
7286   (gnus-set-global-variables)
7287   (gnus-kill-save-kill-buffer)
7288   (let* ((group gnus-newsgroup-name)
7289          (quit-config (nth 1 (assoc 'quit-config (gnus-find-method-for-group
7290                                                   gnus-newsgroup-name))))
7291          (mode major-mode)
7292          (method (car (gnus-find-method-for-group group)))
7293          (buf (current-buffer)))
7294     (gnus-summary-update-info) ; Make all changes in this group permanent.
7295     ;; Make sure where I was, and go to next newsgroup.
7296     (or quit-config
7297         (progn
7298           (gnus-group-jump-to-group group)
7299           (gnus-group-next-unread-group 1)))
7300     (if temporary
7301         nil                             ;Nothing to do.
7302       ;; We set all buffer-local variables to nil. It is unclear why
7303       ;; this is needed, but if we don't, buffer-local variables are
7304       ;; not garbage-collected, it seems. This would the lead to en
7305       ;; ever-growing Emacs.
7306       (set-buffer buf)
7307       (gnus-summary-clear-local-variables)
7308       ;; We clear the global counterparts of the buffer-local
7309       ;; variables as well, just to be on the safe side.
7310       (gnus-configure-windows 'group)
7311       (gnus-summary-clear-local-variables)
7312       ;; Return to group mode buffer. 
7313       (if (eq mode 'gnus-summary-mode)
7314           (gnus-kill-buffer buf))
7315       (if (get-buffer gnus-article-buffer)
7316           (bury-buffer gnus-article-buffer))
7317       (setq gnus-current-select-method gnus-select-method)
7318       (pop-to-buffer gnus-group-buffer)
7319       (if (not quit-config)
7320           (progn
7321             (gnus-group-jump-to-group group)
7322             (gnus-group-next-unread-group 1))
7323         (if (not (buffer-name (car quit-config)))
7324             (gnus-configure-windows 'group)
7325           (set-buffer (car quit-config))
7326           (and (eq major-mode 'gnus-summary-mode)
7327                (gnus-set-global-variables))
7328           (gnus-configure-windows (cdr quit-config)))))))
7329
7330 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
7331 (defun gnus-summary-exit-no-update (&optional no-questions)
7332   "Quit reading current newsgroup without updating read article info."
7333   (interactive)
7334   (let* ((group gnus-newsgroup-name)
7335          (quit-config (nth 1 (assoc 'quit-config 
7336                                     (gnus-find-method-for-group group)))))
7337     (if (or no-questions
7338             gnus-expert-user
7339             (gnus-y-or-n-p "Do you really wanna quit reading this group? "))
7340         (progn
7341           (gnus-close-group group)
7342           (gnus-summary-clear-local-variables)
7343           (set-buffer gnus-group-buffer)
7344           (gnus-summary-clear-local-variables)
7345           ;; Return to group selection mode.
7346           (gnus-configure-windows 'group)
7347           (if (get-buffer gnus-summary-buffer)
7348               (kill-buffer gnus-summary-buffer))
7349           (if (get-buffer gnus-article-buffer)
7350               (bury-buffer gnus-article-buffer))
7351           (if (equal (gnus-group-group-name) group)
7352               (gnus-group-next-unread-group 1))
7353           (if quit-config
7354               (progn
7355                 (if (not (buffer-name (car quit-config)))
7356                     (gnus-configure-windows 'group)
7357                   (set-buffer (car quit-config))
7358                   (and (eq major-mode 'gnus-summary-mode)
7359                        (gnus-set-global-variables))
7360                   (gnus-configure-windows (cdr quit-config)))))))))
7361
7362 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
7363 (defun gnus-summary-fetch-faq (group)
7364   "Fetch the FAQ for the current group."
7365   (interactive (list gnus-newsgroup-name))
7366   (gnus-configure-windows 'summary-faq)
7367   (find-file (concat gnus-group-faq-directory group)))
7368
7369 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
7370 (defun gnus-summary-describe-group (force)
7371   "Describe the current newsgroup."
7372   (interactive "P")
7373   (gnus-group-describe-group force gnus-newsgroup-name))
7374
7375 (defun gnus-summary-describe-briefly ()
7376   "Describe summary mode commands briefly."
7377   (interactive)
7378   (gnus-message 6
7379     (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")))
7380
7381 ;; Walking around group mode buffer from summary mode.
7382
7383 (defun gnus-summary-next-group (&optional no-article group backward)
7384   "Exit current newsgroup and then select next unread newsgroup.
7385 If prefix argument NO-ARTICLE is non-nil, no article is selected initially.
7386 If BACKWARD, go to previous group instead."
7387   (interactive "P")
7388   (gnus-set-global-variables)
7389   (let ((ingroup gnus-newsgroup-name)
7390         (sumbuf (current-buffer))
7391         num)
7392     (gnus-summary-exit t)               ;Update all information.
7393     (if (and group
7394              (or (and (numberp (setq num (car (gnus-gethash
7395                                                group gnus-newsrc-hashtb))))
7396                       (< num 1))
7397                  (null num)))
7398         (progn
7399           (gnus-group-jump-to-group group)
7400           (setq group nil))
7401       (gnus-group-jump-to-group ingroup))
7402     (gnus-summary-search-group backward)
7403     (let ((group (or group (gnus-summary-search-group backward)))
7404           (buf gnus-summary-buffer))
7405       (if (null group)
7406           (gnus-summary-exit-no-update t)
7407         (gnus-message 5 "Selecting %s..." group)
7408         ;; We are now in group mode buffer.
7409         ;; Make sure group mode buffer point is on GROUP.
7410         (gnus-group-jump-to-group group)
7411         (if (not (eq gnus-auto-select-next 'quietly))
7412             (progn
7413               (gnus-summary-read-group group nil no-article buf)
7414               (and (string= gnus-newsgroup-name ingroup)
7415                    (bufferp sumbuf) (buffer-name sumbuf)
7416                    (progn
7417                      (set-buffer (setq gnus-summary-buffer sumbuf))
7418                      (gnus-summary-exit-no-update t))))
7419           (let ((prevgroup group))
7420             (gnus-summary-read-group group nil no-article buf)
7421             (while (and (string= gnus-newsgroup-name ingroup)
7422                         (bufferp sumbuf) 
7423                         (buffer-name sumbuf)
7424                         (not (string= prevgroup (gnus-group-group-name))))
7425               (set-buffer gnus-group-buffer)
7426               (gnus-summary-read-group 
7427                (setq prevgroup (gnus-group-group-name)) 
7428                nil no-article buf))
7429             (and (string= prevgroup (gnus-group-group-name))
7430                  ;; We have reached the final group in the group
7431                  ;; buffer.
7432                  (progn
7433                    (if (buffer-name sumbuf)
7434                        (progn
7435                          (set-buffer sumbuf)
7436                          (gnus-summary-exit)))))))))))
7437
7438 (defun gnus-summary-prev-group (no-article)
7439   "Exit current newsgroup and then select previous unread newsgroup.
7440 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7441   (interactive "P")
7442   (gnus-summary-next-group no-article nil t))
7443
7444 ;; Walking around summary lines.
7445
7446 (defun gnus-summary-first-subject (unread)
7447   "Go to the first unread subject.
7448 If UNREAD is non-nil, go to the first unread article.
7449 Returns nil if there are no unread articles."
7450   (interactive "P")
7451   (prog1
7452       (if (or (not unread)
7453               (gnus-goto-char 
7454                (text-property-any 
7455                 (point-min) (point-max) 'gnus-mark gnus-unread-mark)))
7456           t 
7457         ;; If there are no unread articles.
7458         (gnus-message 3 "No more unread articles")
7459         nil)
7460     (gnus-summary-position-cursor)))
7461
7462 (defun gnus-summary-next-subject (n &optional unread dont-display)
7463   "Go to next N'th summary line.
7464 If N is negative, go to the previous N'th subject line.
7465 If UNREAD is non-nil, only unread articles are selected.
7466 The difference between N and the actual number of steps taken is
7467 returned."
7468   (interactive "p")
7469   (let ((backward (< n 0))
7470         (n (abs n)))
7471     (while (and (> n 0)
7472                 (gnus-summary-search-forward unread nil backward))
7473       (setq n (1- n)))
7474     (if (/= 0 n) (gnus-message 7 "No more%s articles"
7475                                (if unread " unread" "")))
7476     (or dont-display
7477         (progn
7478           (gnus-summary-recenter)
7479           (gnus-summary-position-cursor)))
7480   n))
7481
7482 (defun gnus-summary-next-unread-subject (n)
7483   "Go to next N'th unread summary line."
7484   (interactive "p")
7485   (gnus-summary-next-subject n t))
7486
7487 (defun gnus-summary-prev-subject (n &optional unread)
7488   "Go to previous N'th summary line.
7489 If optional argument UNREAD is non-nil, only unread article is selected."
7490   (interactive "p")
7491   (gnus-summary-next-subject (- n) unread))
7492
7493 (defun gnus-summary-prev-unread-subject (n)
7494   "Go to previous N'th unread summary line."
7495   (interactive "p")
7496   (gnus-summary-next-subject (- n) t))
7497
7498 (defun gnus-summary-goto-subject (article)
7499   "Go the subject line of ARTICLE."
7500   (interactive
7501    (list
7502     (string-to-int
7503      (completing-read "Article number: "
7504                       (mapcar
7505                        (lambda (headers)
7506                          (list
7507                           (int-to-string (header-number headers))))
7508                        gnus-newsgroup-headers)
7509                       nil 'require-match))))
7510   (or article (error "No article number"))
7511   (let ((b (point)))
7512     (if (not (gnus-goto-char (text-property-any (point-min) (point-max)
7513                                                 'gnus-number article)))
7514         ()
7515       (gnus-summary-show-thread)
7516       ;; Skip dummy articles. 
7517       (if (eq (gnus-summary-article-mark) gnus-dummy-mark)
7518           (forward-line 1))
7519       (prog1
7520           (if (not (eobp))
7521               article
7522             (goto-char b)
7523             nil)
7524         (gnus-summary-position-cursor)))))
7525
7526 ;; Walking around summary lines with displaying articles.
7527
7528 (defun gnus-summary-expand-window ()
7529   "Make the summary buffer take up the entire Emacs frame."
7530   (interactive)
7531   (gnus-set-global-variables)
7532   (gnus-configure-windows 'summary))
7533
7534 (defun gnus-summary-display-article (article &optional all-header)
7535   "Display ARTICLE in article buffer."
7536   (gnus-set-global-variables)
7537   (if (null article)
7538       nil
7539     (gnus-article-prepare article all-header)
7540     (gnus-summary-show-thread)
7541     (if (eq (gnus-summary-article-mark) gnus-dummy-mark)
7542         (progn
7543           (forward-line 1)
7544           (gnus-summary-position-cursor)))
7545     (run-hooks 'gnus-select-article-hook)
7546     (gnus-summary-recenter)
7547 ;    (set-window-point (get-buffer-window (current-buffer)) (point-max))
7548 ;    (sit-for 0)
7549     (gnus-summary-goto-subject article)
7550     ;; Successfully display article.
7551     (gnus-summary-update-line)
7552     (gnus-article-set-window-start 
7553      (cdr (assq article gnus-newsgroup-bookmarks)))
7554     t))
7555
7556 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7557   "Select the current article.
7558 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7559 non-nil, the article will be re-fetched even if it already present in
7560 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7561 be displayed."
7562   (and (not pseudo) (gnus-summary-pseudo-article)
7563        (error "This is a pseudo-article."))
7564   (let ((article (or article (gnus-summary-article-number)))
7565         (all-headers (not (not all-headers))) ;Must be T or NIL.
7566         did) 
7567     (prog1
7568         (save-excursion
7569           (set-buffer gnus-summary-buffer)
7570           (if (or (null gnus-current-article)
7571                   (null gnus-article-current)
7572                   (not (eq article (cdr gnus-article-current)))
7573                   (not (equal (car gnus-article-current) gnus-newsgroup-name))
7574                   force)
7575               ;; The requested article is different from the current article.
7576               (progn
7577                 (gnus-summary-display-article article all-headers)
7578                 (setq did article))
7579             (if all-headers (gnus-article-show-all-headers))
7580             nil))
7581       (if did 
7582           (gnus-article-set-window-start 
7583            (cdr (assq article gnus-newsgroup-bookmarks)))))))
7584
7585 (defun gnus-summary-set-current-mark (&optional current-mark)
7586   "Obsolete function."
7587   nil)
7588
7589 (defun gnus-summary-next-article (unread &optional subject backward)
7590   "Select the next article.
7591 If UNREAD, only unread articles are selected.
7592 If SUBJECT, only articles with SUBJECT are selected.
7593 If BACKWARD, the previous article is selected instead of the next."
7594   (interactive "P")
7595   (gnus-set-global-variables)
7596   (let ((opoint (point))
7597         (method (car (gnus-find-method-for-group gnus-newsgroup-name)))
7598         header)
7599     (cond
7600      ;; Is there such an article?
7601      ((gnus-summary-display-article 
7602        (gnus-summary-search-forward unread subject backward))
7603       (gnus-summary-position-cursor))
7604      ;; If not, we try the first unread, if that is wanted.
7605      ((and subject
7606            gnus-auto-select-same
7607            (gnus-summary-first-unread-article))
7608       (gnus-message 6 "Wrapped"))
7609      ;; Try to get next/previous article not displayed in this group.
7610      ((and gnus-auto-extend-newsgroup
7611            (not unread) (not subject)
7612            (setq header (gnus-more-header-forward backward)))
7613       (gnus-extend-newsgroup header backward)
7614       (let ((buffer-read-only nil))
7615         (goto-char (if backward (point-min) (point-max)))
7616         (gnus-summary-prepare-threads (list header) 0))
7617       (gnus-summary-goto-article (if backward gnus-newsgroup-begin
7618                                    gnus-newsgroup-end)))
7619      ;; Go to next/previous group.
7620      (t
7621       (or (assoc 'quit-config (gnus-find-method-for-group gnus-newsgroup-name))
7622           (gnus-summary-jump-to-group gnus-newsgroup-name))
7623       (let ((cmd (aref (this-command-keys) 0))
7624             (group 
7625              (if (eq gnus-keep-same-level 'best) 
7626                  (gnus-summary-best-group gnus-newsgroup-name)
7627                (gnus-summary-search-group backward gnus-keep-same-level))))
7628         ;; For some reason, the group window gets selected. We change
7629         ;; it back.  
7630         (select-window (get-buffer-window (current-buffer)))
7631         ;; Keep just the event type of CMD.
7632         (and (listp cmd) (setq cmd (car cmd)))
7633         ;; Select next unread newsgroup automagically.
7634         (cond 
7635          ((not gnus-auto-select-next)
7636           (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7637          ((eq gnus-auto-select-next 'quietly)
7638           ;; Select quietly.
7639           (if (assoc 'quit-config (gnus-find-method-for-group 
7640                                    gnus-newsgroup-name))
7641               (gnus-summary-exit)
7642             (gnus-message 7 "No more%s articles (%s)..."
7643                           (if unread " unread" "") 
7644                           (if group (concat "selecting " group)
7645                             "exiting"))
7646             (gnus-summary-next-group nil group backward)))
7647          (t
7648           (let ((keystrokes '(?\C-n ?\C-p))
7649                 key)
7650             (while (or (null key) (memq key keystrokes))
7651               (gnus-message 
7652                7 "No more%s articles%s" (if unread " unread" "")
7653                (if (and group (not (assoc 'quit-config
7654                                           (gnus-find-method-for-group 
7655                                            gnus-newsgroup-name))))
7656                    (format " (Type %s for %s [%s])"
7657                            (single-key-description cmd) group
7658                            (car (gnus-gethash group gnus-newsrc-hashtb)))
7659                  (format " (Type %s to exit %s)"
7660                          (single-key-description cmd)
7661                          gnus-newsgroup-name)))
7662               ;; Confirm auto selection.
7663               (let* ((event (read-event)))
7664                 (setq key (if (listp event) (car event) event))
7665                 (if (memq key keystrokes)
7666                     (let ((obuf (current-buffer)))
7667                       (switch-to-buffer gnus-group-buffer)
7668                       (gnus-group-jump-to-group group)
7669                       (execute-kbd-macro (char-to-string key))
7670                       (setq group (gnus-group-group-name))
7671                       (switch-to-buffer obuf)))))
7672             (if (equal key cmd)
7673                 (if (or (not group) (assoc 'quit-config
7674                                            (gnus-find-method-for-group
7675                                             gnus-newsgroup-name)))
7676                     (gnus-summary-exit)
7677                   (gnus-summary-next-group nil group backward))
7678               (setq unread-command-events (list key)))))))))))
7679
7680 (defun gnus-summary-next-unread-article ()
7681   "Select unread article after current one."
7682   (interactive)
7683   (gnus-summary-next-article t (and gnus-auto-select-same
7684                                     (gnus-summary-subject-string))))
7685
7686 (defun gnus-summary-prev-article (unread &optional subject)
7687   "Select the article after the current one.
7688 If UNREAD is non-nil, only unread articles are selected."
7689   (interactive "P")
7690   (gnus-summary-next-article unread subject t))
7691
7692 (defun gnus-summary-prev-unread-article ()
7693   "Select unred article before current one."
7694   (interactive)
7695   (gnus-summary-prev-article t (and gnus-auto-select-same
7696                                     (gnus-summary-subject-string))))
7697
7698 (defun gnus-summary-next-page (lines &optional circular)
7699   "Show next page of selected article.
7700 If end of article, select next article.
7701 Argument LINES specifies lines to be scrolled up.
7702 If CIRCULAR is non-nil, go to the start of the article instead of 
7703 instead of selecting the next article when reaching the end of the
7704 current article." 
7705   (interactive "P")
7706   (setq gnus-summary-buffer (current-buffer))
7707   (gnus-set-global-variables)
7708   (let ((article (gnus-summary-article-number))
7709         (endp nil))
7710     (gnus-configure-windows 'article)
7711     (if (or (null gnus-current-article)
7712             (null gnus-article-current)
7713             (/= article (cdr gnus-article-current))
7714             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7715         ;; Selected subject is different from current article's.
7716         (gnus-summary-display-article article)
7717       (gnus-eval-in-buffer-window
7718        gnus-article-buffer
7719        (setq endp (gnus-article-next-page lines)))
7720       (if endp
7721           (cond (circular
7722                  (gnus-summary-beginning-of-article))
7723                 (lines
7724                  (gnus-message 3 "End of message"))
7725                 ((null lines)
7726                  (gnus-summary-next-unread-article)))))
7727     (gnus-summary-recenter)
7728     (gnus-summary-position-cursor)))
7729
7730 (defun gnus-summary-prev-page (lines)
7731   "Show previous page of selected article.
7732 Argument LINES specifies lines to be scrolled down."
7733   (interactive "P")
7734   (gnus-set-global-variables)
7735   (let ((article (gnus-summary-article-number)))
7736     (gnus-configure-windows 'article)
7737     (if (or (null gnus-current-article)
7738             (null gnus-article-current)
7739             (/= article (cdr gnus-article-current))
7740             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7741         ;; Selected subject is different from current article's.
7742         (gnus-summary-display-article article)
7743       (gnus-summary-recenter)
7744       (gnus-eval-in-buffer-window gnus-article-buffer
7745         (gnus-article-prev-page lines))))
7746   (gnus-summary-position-cursor))
7747
7748 (defun gnus-summary-scroll-up (lines)
7749   "Scroll up (or down) one line current article.
7750 Argument LINES specifies lines to be scrolled up (or down if negative)."
7751   (interactive "p")
7752   (gnus-set-global-variables)
7753   (gnus-configure-windows 'article)
7754   (or (gnus-summary-select-article nil nil 'pseudo)
7755       (gnus-eval-in-buffer-window 
7756        gnus-article-buffer
7757        (cond ((> lines 0)
7758               (if (gnus-article-next-page lines)
7759                   (gnus-message 3 "End of message")))
7760              ((< lines 0)
7761               (gnus-article-prev-page (- lines))))))
7762   (gnus-summary-recenter)
7763   (gnus-summary-position-cursor))
7764
7765 (defun gnus-summary-next-same-subject ()
7766   "Select next article which has the same subject as current one."
7767   (interactive)
7768   (gnus-set-global-variables)
7769   (gnus-summary-next-article nil (gnus-summary-subject-string)))
7770
7771 (defun gnus-summary-prev-same-subject ()
7772   "Select previous article which has the same subject as current one."
7773   (interactive)
7774   (gnus-set-global-variables)
7775   (gnus-summary-prev-article nil (gnus-summary-subject-string)))
7776
7777 (defun gnus-summary-next-unread-same-subject ()
7778   "Select next unread article which has the same subject as current one."
7779   (interactive)
7780   (gnus-set-global-variables)
7781   (gnus-summary-next-article t (gnus-summary-subject-string)))
7782
7783 (defun gnus-summary-prev-unread-same-subject ()
7784   "Select previous unread article which has the same subject as current one."
7785   (interactive)
7786   (gnus-set-global-variables)
7787   (gnus-summary-prev-article t (gnus-summary-subject-string)))
7788
7789 (defun gnus-summary-first-unread-article ()
7790   "Select the first unread article. 
7791 Return nil if there are no unread articles."
7792   (interactive)
7793   (gnus-set-global-variables)
7794   (prog1
7795       (if (gnus-summary-first-subject t)
7796           (gnus-summary-display-article (gnus-summary-article-number)))
7797     (gnus-summary-position-cursor)))
7798
7799 (defun gnus-summary-best-unread-article ()
7800   "Select the unread article with the highest score."
7801   (interactive)
7802   (gnus-set-global-variables)
7803   (let ((scored gnus-newsgroup-scored)
7804         (best -1000000)
7805         article art)
7806     (while scored
7807       (or (> best (cdr (car scored)))
7808           (and (memq (setq art (car (car scored))) gnus-newsgroup-unreads)
7809                (not (memq art gnus-newsgroup-marked))
7810                (not (memq art gnus-newsgroup-dormant))
7811                (if (= best (cdr (car scored)))
7812                    (setq article (min art article))
7813                  (setq article art)
7814                  (setq best (cdr (car scored))))))
7815       (setq scored (cdr scored)))
7816     (if article 
7817         (gnus-summary-goto-article article)
7818       (gnus-summary-first-unread-article))
7819     (gnus-summary-position-cursor)))
7820
7821 (defun gnus-summary-goto-article (article &optional all-headers)
7822   "Fetch ARTICLE and display it if it exists.
7823 If ALL-HEADERS is non-nil, no header lines are hidden."
7824   (interactive
7825    (list
7826     (string-to-int
7827      (completing-read 
7828       "Article number: "
7829       (mapcar (lambda (headers) (list (int-to-string (header-number headers))))
7830               gnus-newsgroup-headers) 
7831       nil 'require-match))))
7832   (prog1
7833       (and (gnus-summary-goto-subject article)
7834            (gnus-summary-display-article article all-headers))
7835     (gnus-summary-position-cursor)))
7836
7837 (defun gnus-summary-goto-last-article ()
7838   "Go to the last article."
7839   (interactive)
7840   (prog1
7841       (and gnus-last-article
7842            (gnus-summary-goto-article gnus-last-article))
7843     (gnus-summary-position-cursor)))
7844
7845 (defun gnus-summary-pop-article (number)
7846   "Pop one article off the history and go to the previous.
7847 NUMBER articles will be popped off."
7848   (interactive "p")
7849   (let (to)
7850     (setq gnus-newsgroup-history
7851           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7852     (if to
7853         (gnus-summary-goto-article (car to))
7854       (error "Article history empty")))
7855   (gnus-summary-position-cursor))
7856
7857 ;; Summary article oriented commands
7858
7859 (defun gnus-summary-refer-parent-article (n)
7860   "Refer parent article N times.
7861 The difference between N and the number of articles fetched is returned."
7862   (interactive "p")
7863   (gnus-set-global-variables)
7864   (while 
7865       (and 
7866        (> n 0)
7867        (let ((ref (header-references (gnus-get-header-by-number
7868                                       (gnus-summary-article-number)))))
7869          (if (and ref (not (equal ref ""))
7870                   (string-match "<[^<>]*>[ \t]*$" ref))
7871              (gnus-summary-refer-article 
7872               (substring ref (match-beginning 0) (match-end 0))))))
7873     (setq n (1- n)))
7874   (or (zerop n) 
7875       (gnus-message 1 "No references in article or expired article."))
7876   (gnus-summary-position-cursor)
7877   n)
7878     
7879 (defun gnus-summary-refer-article (message-id)
7880   "Refer article specified by MESSAGE-ID.
7881 NOTE: This command only works with newsgroups that use real or simulated NNTP."
7882   (interactive "sMessage-ID: ")
7883   (if (or (not (stringp message-id))
7884           (zerop (length message-id)))
7885       ()
7886     ;; Construct the correct Message-ID if necessary.
7887     ;; Suggested by tale@pawl.rpi.edu.
7888     (or (string-match "^<" message-id)
7889         (setq message-id (concat "<" message-id)))
7890     (or (string-match ">$" message-id)
7891         (setq message-id (concat message-id ">")))
7892     (let ((header (car (gnus-gethash (downcase message-id)
7893                                      gnus-newsgroup-dependencies))))
7894       (if header
7895           (or (gnus-summary-goto-article (header-number header))
7896               ;; The header has been read, but the article had been
7897               ;; expunged, so we insert it again.
7898               (progn
7899                 (gnus-summary-insert-line
7900                  nil header 0 nil gnus-read-mark nil nil
7901                  (header-subject header))
7902                 (forward-line -1)
7903                 (header-number header)))
7904         (let ((gnus-override-method gnus-refer-article-method)
7905               (gnus-ancient-mark gnus-read-mark)
7906               number)
7907           (and gnus-refer-article-method
7908                (or (gnus-server-opened gnus-refer-article-method)
7909                    (gnus-open-server gnus-refer-article-method)))
7910           (if (gnus-article-prepare 
7911                message-id nil (gnus-read-header message-id))
7912               (progn
7913                 (setq number (header-number gnus-current-headers))
7914                 (gnus-rebuild-thread message-id)
7915                 (gnus-summary-goto-subject number)
7916                 (gnus-summary-recenter)
7917                 (gnus-article-set-window-start 
7918                  (cdr (assq number gnus-newsgroup-bookmarks)))
7919                 message-id)
7920             (gnus-message 1 "No such references")
7921             nil))))))
7922
7923 (defun gnus-summary-enter-digest-group ()
7924   "Enter a digest group based on the current article."
7925   (interactive)
7926   (gnus-set-global-variables)
7927   (gnus-summary-select-article)
7928   ;; We do not want a narrowed article.
7929   (gnus-summary-stop-page-breaking)
7930   (let ((name (format "%s-%d" 
7931                       (gnus-group-prefixed-name 
7932                        gnus-newsgroup-name (list 'nndoc "")) 
7933                       gnus-current-article))
7934         (ogroup gnus-newsgroup-name)
7935         (buf (current-buffer)))
7936     (if (gnus-group-read-ephemeral-group 
7937          name (list 'nndoc name
7938                     (list 'nndoc-address (get-buffer gnus-article-buffer))
7939                     '(nndoc-article-type digest))
7940          t)
7941         (setcdr (nthcdr 4 (nth 2 (gnus-gethash name gnus-newsrc-hashtb)))
7942                 (list (list (cons 'to-group ogroup))))
7943       (switch-to-buffer buf)
7944       (gnus-set-global-variables)
7945       (gnus-configure-windows 'summary)
7946       (gnus-message 3 "Article not a digest?"))))
7947
7948 (defun gnus-summary-isearch-article ()
7949   "Do incremental search forward on current article."
7950   (interactive)
7951   (gnus-set-global-variables)
7952   (gnus-summary-select-article)
7953   (gnus-eval-in-buffer-window 
7954    gnus-article-buffer (isearch-forward)))
7955
7956 (defun gnus-summary-search-article-forward (regexp)
7957   "Search for an article containing REGEXP forward.
7958 gnus-select-article-hook is not called during the search."
7959   (interactive
7960    (list (read-string
7961           (concat "Search forward (regexp): "
7962                   (if gnus-last-search-regexp
7963                       (concat "(default " gnus-last-search-regexp ") "))))))
7964   (gnus-set-global-variables)
7965   (if (string-equal regexp "")
7966       (setq regexp (or gnus-last-search-regexp ""))
7967     (setq gnus-last-search-regexp regexp))
7968   (if (gnus-summary-search-article regexp nil)
7969       (gnus-eval-in-buffer-window 
7970        gnus-article-buffer
7971        (recenter 0))
7972     (error "Search failed: \"%s\"" regexp)))
7973
7974 (defun gnus-summary-search-article-backward (regexp)
7975   "Search for an article containing REGEXP backward.
7976 gnus-select-article-hook is not called during the search."
7977   (interactive
7978    (list (read-string
7979           (concat "Search backward (regexp): "
7980                   (if gnus-last-search-regexp
7981                       (concat "(default " gnus-last-search-regexp ") "))))))
7982   (gnus-set-global-variables)
7983   (if (string-equal regexp "")
7984       (setq regexp (or gnus-last-search-regexp ""))
7985     (setq gnus-last-search-regexp regexp))
7986   (if (gnus-summary-search-article regexp t)
7987       (gnus-eval-in-buffer-window
7988        gnus-article-buffer
7989        (recenter 0))
7990     (error "Search failed: \"%s\"" regexp)))
7991
7992 (defun gnus-summary-search-article (regexp &optional backward)
7993   "Search for an article containing REGEXP.
7994 Optional argument BACKWARD means do search for backward.
7995 gnus-select-article-hook is not called during the search."
7996   (let ((gnus-select-article-hook nil)  ;Disable hook.
7997         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7998         (re-search
7999          (if backward
8000              (function re-search-backward) (function re-search-forward)))
8001         (found nil)
8002         (last nil))
8003     ;; Hidden thread subtrees must be searched for ,too.
8004     (gnus-summary-show-all-threads)
8005     (if (eobp) (forward-line -1))
8006     ;; First of all, search current article.
8007     ;; We don't want to read article again from NNTP server nor reset
8008     ;; current point.
8009     (gnus-summary-select-article)
8010     (gnus-message 9 "Searching article: %d..." gnus-current-article)
8011     (setq last gnus-current-article)
8012     (gnus-eval-in-buffer-window gnus-article-buffer
8013       (save-restriction
8014         (widen)
8015         ;; Begin search from current point.
8016         (setq found (funcall re-search regexp nil t))))
8017     ;; Then search next articles.
8018     (while (and (not found)
8019                 (gnus-summary-display-article 
8020                  (gnus-summary-search-subject backward nil nil)))
8021       (gnus-message 9 "Searching article: %d..." gnus-current-article)
8022       (gnus-eval-in-buffer-window gnus-article-buffer
8023         (save-restriction
8024           (widen)
8025           (goto-char (if backward (point-max) (point-min)))
8026           (setq found (funcall re-search regexp nil t)))))
8027     (message "")
8028     ;; Adjust article pointer.
8029     (or (eq last gnus-current-article)
8030         (setq gnus-last-article last))
8031     ;; Return T if found such article.
8032     found))
8033
8034 (defun gnus-summary-execute-command (header regexp command &optional backward)
8035   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8036 If HEADER is an empty string (or nil), the match is done on the entire
8037 article. If BACKWARD (the prefix) is non-nil, search backward instead."
8038   (interactive
8039    (list (let ((completion-ignore-case t))
8040            (completing-read 
8041             "Header name: "
8042             (mapcar (lambda (string) (list string))
8043                     '("Number" "Subject" "From" "Lines" "Date"
8044                       "Message-ID" "Xref" "References"))
8045             nil 'require-match))
8046          (read-string "Regexp: ")
8047          (read-key-sequence "Command: ")
8048          current-prefix-arg))
8049   (gnus-set-global-variables)
8050   ;; Hidden thread subtrees must be searched as well.
8051   (gnus-summary-show-all-threads)
8052   ;; We don't want to change current point nor window configuration.
8053   (save-excursion
8054     (save-window-excursion
8055       (gnus-message 6 "Executing %s..." (key-description command))
8056       ;; We'd like to execute COMMAND interactively so as to give arguments.
8057       (gnus-execute header regexp
8058                     (` (lambda ()
8059                          (call-interactively '(, (key-binding command)))))
8060                     backward)
8061       (gnus-message 6 "Executing %s...done" (key-description command)))))
8062
8063 (defun gnus-summary-beginning-of-article ()
8064   "Scroll the article back to the beginning."
8065   (interactive)
8066   (gnus-set-global-variables)
8067   (gnus-summary-select-article)
8068   (gnus-eval-in-buffer-window
8069    gnus-article-buffer
8070    (widen)
8071    (goto-char (point-min))
8072    (and gnus-break-pages (gnus-narrow-to-page))))
8073
8074 (defun gnus-summary-end-of-article ()
8075   "Scroll to the end of the article."
8076   (interactive)
8077   (gnus-set-global-variables)
8078   (gnus-summary-select-article)
8079   (gnus-eval-in-buffer-window 
8080    gnus-article-buffer
8081    (widen)
8082    (goto-char (point-max))
8083    (and gnus-break-pages (gnus-narrow-to-page))))
8084
8085 (defun gnus-summary-show-article ()
8086   "Force re-fetching of the current article."
8087   (interactive)
8088   (gnus-set-global-variables)
8089   (gnus-summary-select-article gnus-have-all-headers t))
8090
8091 (defun gnus-summary-toggle-header (arg)
8092   "Show the headers if they are hidden, or hide them if they are shown.
8093 If ARG is a positive number, show the entire header.
8094 If ARG is a negative number, hide the unwanted header lines."
8095   (interactive "P")
8096   (gnus-set-global-variables)
8097   (save-excursion
8098     (set-buffer gnus-article-buffer)
8099     (let ((buffer-read-only nil))
8100       (if (numberp arg) 
8101           (if (> arg 0) (remove-text-properties (point-min) (point-max) 
8102                                                 gnus-hidden-properties)
8103             (if (< arg 0) (run-hooks 'gnus-article-display-hook)))
8104         (if (text-property-any (point-min) (point-max) 'invisible t)
8105             (remove-text-properties (point-min) (point-max)
8106                                     gnus-hidden-properties)
8107           (let ((gnus-have-all-headers nil))
8108             (run-hooks 'gnus-article-display-hook))))
8109       (set-window-point (get-buffer-window (current-buffer)) (point-min)))))
8110
8111 (defun gnus-summary-show-all-headers ()
8112   "Make all header lines visible."
8113   (interactive)
8114   (gnus-set-global-variables)
8115   (gnus-article-show-all-headers))
8116
8117 (defun gnus-summary-toggle-mime (arg)
8118   "Toggle MIME processing.
8119 If ARG is a positive number, turn MIME processing on."
8120   (interactive "P")
8121   (gnus-set-global-variables)
8122   (setq gnus-show-mime
8123         (if (null arg) (not gnus-show-mime)
8124           (> (prefix-numeric-value arg) 0)))
8125   (gnus-summary-select-article t 'force))
8126
8127 (defun gnus-summary-caesar-message (rotnum)
8128   "Caesar rotates all letters of current message by 13/47 places.
8129 With prefix arg, specifies the number of places to rotate each letter forward.
8130 Caesar rotates Japanese letters by 47 places in any case."
8131   (interactive "P")
8132   (gnus-set-global-variables)
8133   (gnus-summary-select-article)
8134   (let ((mail-header-separator "")) ; !!! Is this necessary?
8135     (gnus-overload-functions)
8136     (gnus-eval-in-buffer-window 
8137      gnus-article-buffer
8138      (save-restriction
8139        (widen)
8140        ;; We don't want to jump to the beginning of the message.
8141        ;; `save-excursion' does not do its job.
8142        (move-to-window-line 0)
8143        (let ((last (point)))
8144          (news-caesar-buffer-body rotnum)
8145          (goto-char last)
8146          (recenter 0))))))
8147
8148 (defun gnus-summary-stop-page-breaking ()
8149   "Stop page breaking in the current article."
8150   (interactive)
8151   (gnus-set-global-variables)
8152   (gnus-summary-select-article)
8153   (gnus-eval-in-buffer-window gnus-article-buffer (widen)))
8154
8155 ;; Suggested by Brian Edmonds <bedmonds@prodigy.bc.ca>.
8156
8157 (defun gnus-summary-move-article (n &optional to-newsgroup select-method)
8158   "Move the current article to a different newsgroup.
8159 If N is a positive number, move the N next articles.
8160 If N is a negative number, move the N previous articles.
8161 If N is nil and any articles have been marked with the process mark,
8162 move those articles instead.
8163 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to. 
8164 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
8165 re-spool using this method.
8166 For this function to work, both the current newsgroup and the
8167 newsgroup that you want to move to have to support the `request-move'
8168 and `request-accept' functions. (Ie. mail newsgroups at present.)"
8169   (interactive "P")
8170   (gnus-set-global-variables)
8171   (or (gnus-check-backend-function 'request-move-article gnus-newsgroup-name)
8172       (error "The current newsgroup does not support article moving"))
8173   (let ((articles (gnus-summary-work-articles n))
8174         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
8175         art-group)
8176     (if (and (not to-newsgroup) (not select-method))
8177         (setq to-newsgroup
8178               (completing-read 
8179                (format "Where do you want to move %s? %s"
8180                        (if (> (length articles) 1)
8181                            (format "these %d articles" (length articles))
8182                          "this article")
8183                        (if gnus-current-move-group
8184                            (format "(%s default) " gnus-current-move-group)
8185                          ""))
8186                gnus-active-hashtb nil nil prefix)))
8187     (if to-newsgroup
8188         (progn
8189           (if (or (string= to-newsgroup "") (string= to-newsgroup prefix))
8190               (setq to-newsgroup (or gnus-current-move-group "")))
8191           (or (gnus-gethash to-newsgroup gnus-active-hashtb)
8192               (gnus-activate-newsgroup to-newsgroup)
8193               (error "No such group: %s" to-newsgroup))
8194           (setq gnus-current-move-group to-newsgroup)))
8195     (or (gnus-check-backend-function 'request-accept-article 
8196                                      (or select-method to-newsgroup))
8197         (error "%s does not support article moving" to-newsgroup))
8198     (gnus-message 6 "Moving to %s: %s..." 
8199                   (or select-method to-newsgroup) articles)
8200     (while articles
8201       (if (setq art-group
8202                 (gnus-request-move-article 
8203                  (car articles)                   ; Article to move
8204                  gnus-newsgroup-name              ; From newsgrouo
8205                  (nth 1 (gnus-find-method-for-group 
8206                          gnus-newsgroup-name))    ; Server
8207                  (list 'gnus-request-accept-article 
8208                        (if select-method
8209                            (list 'quote select-method)
8210                          to-newsgroup)
8211                        (not (cdr articles)))     ; Accept form
8212                  (not (cdr articles))))          ; Only save nov last time
8213           (let* ((buffer-read-only nil)
8214                  (entry 
8215                   (or
8216                    (gnus-gethash (car art-group) gnus-newsrc-hashtb)
8217                    (gnus-gethash 
8218                     (gnus-group-prefixed-name 
8219                      (car art-group) 
8220                      (if select-method (list select-method "")
8221                        (gnus-find-method-for-group to-newsgroup)))
8222                     gnus-newsrc-hashtb)))
8223                  (info (nth 2 entry))
8224                  (article (car articles))
8225                  (marked (nth 3 info)))
8226             (gnus-summary-goto-subject article)
8227             (beginning-of-line)
8228             (delete-region (point)
8229                            (progn (forward-line 1) (point)))
8230             (if (not (memq article gnus-newsgroup-unreads))
8231                 (setcar (cdr (cdr info))
8232                         (gnus-add-to-range (nth 2 info) 
8233                                            (list (cdr art-group)))))
8234             ;; Copy any marks over to the new group.
8235             (let ((marks '((tick . gnus-newsgroup-marked)
8236                            (dormant . gnus-newsgroup-dormant)
8237                            (expire . gnus-newsgroup-expirable)
8238                            (bookmark . gnus-newsgroup-bookmarks)
8239                         ;   (score . gnus-newsgroup-scored)
8240                            (reply . gnus-newsgroup-replied)))
8241                   (to-article (cdr art-group)))
8242               (while marks
8243                 (if (memq article (symbol-value (cdr (car marks))))
8244                     (gnus-add-marked-articles 
8245                      (car info) (car (car marks)) (list to-article) info))
8246                 (setq marks (cdr marks))))
8247             (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8248             (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8249             (setq gnus-newsgroup-dormant
8250                   (delq article gnus-newsgroup-dormant)))
8251         (gnus-message 1 "Couldn't move article %s" (car articles)))
8252       (gnus-summary-remove-process-mark (car articles))
8253       (setq articles (cdr articles)))))
8254
8255 (defun gnus-summary-respool-article (n &optional respool-method)
8256   "Respool the current article.
8257 The article will be squeezed through the mail spooling process again,
8258 which means that it will be put in some mail newsgroup or other
8259 depending on `nnmail-split-methods'.
8260 If N is a positive number, respool the N next articles.
8261 If N is a negative number, respool the N previous articles.
8262 If N is nil and any articles have been marked with the process mark,
8263 respool those articles instead.
8264
8265 Respooling can be done both from mail groups and \"real\" newsgroups.
8266 In the former case, the articles in question will be moved from the
8267 current group into whatever groups they are destined to.  In the
8268 latter case, they will be copied into the relevant groups."
8269   (interactive "P")
8270   (gnus-set-global-variables)
8271   (let ((respool-methods (gnus-methods-using 'respool))
8272         (methname 
8273          (symbol-name (car (gnus-find-method-for-group gnus-newsgroup-name)))))
8274     (or respool-method
8275         (setq respool-method
8276               (completing-read
8277                "What method do you want to use when respooling? "
8278                respool-methods nil t methname)))
8279     (or (string= respool-method "")
8280         (if (assoc (symbol-name
8281                     (car (gnus-find-method-for-group gnus-newsgroup-name)))
8282                    respool-methods)
8283             (gnus-summary-move-article n nil (intern respool-method))
8284           (gnus-summary-copy-article n nil (intern respool-method))))))
8285
8286 ;; Suggested by gregj@unidata.com (Gregory J. Grubbs).
8287 (defun gnus-summary-copy-article (n &optional to-newsgroup select-method)
8288   "Move the current article to a different newsgroup.
8289 If N is a positive number, move the N next articles.
8290 If N is a negative number, move the N previous articles.
8291 If N is nil and any articles have been marked with the process mark,
8292 move those articles instead.
8293 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to. 
8294 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
8295 re-spool using this method.
8296 For this function to work, the newsgroup that you want to move to have
8297 to support the `request-move' and `request-accept'
8298 functions. (Ie. mail newsgroups at present.)"
8299   (interactive "P")
8300   (gnus-set-global-variables)
8301   (let ((articles (gnus-summary-work-articles n))
8302         (copy-buf (get-buffer-create "*copy work*"))
8303         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
8304         art-group)
8305     (buffer-disable-undo copy-buf)
8306     (if (and (not to-newsgroup) (not select-method))
8307         (setq to-newsgroup
8308               (completing-read 
8309                (format "Where do you want to copy %s? %s"
8310                        (if (> (length articles) 1)
8311                            (format "these %d articles" (length articles))
8312                          "this article")
8313                        (if gnus-current-move-group
8314                            (format "(%s default) " gnus-current-move-group)
8315                          ""))
8316                gnus-active-hashtb nil nil prefix)))
8317     (if to-newsgroup
8318         (progn
8319           (if (or (string= to-newsgroup "") (string= to-newsgroup prefix))
8320               (setq to-newsgroup (or gnus-current-move-group "")))
8321           (or (gnus-gethash to-newsgroup gnus-active-hashtb)
8322               (gnus-activate-newsgroup to-newsgroup)
8323               (error "No such group: %s" to-newsgroup))
8324           (setq gnus-current-move-group to-newsgroup)))
8325     (or (gnus-check-backend-function 'request-accept-article 
8326                                      (or select-method to-newsgroup))
8327         (error "%s does not support article copying" to-newsgroup))
8328     (gnus-message 6 "Copying to %s: %s..." 
8329                   (or select-method to-newsgroup) articles)
8330     (while articles
8331       (if (setq art-group
8332                 (save-excursion
8333                   (set-buffer copy-buf)
8334                   (gnus-request-article-this-buffer
8335                    (car articles) gnus-newsgroup-name)
8336                   (gnus-request-accept-article
8337                    (if select-method (quote select-method) to-newsgroup)
8338                    (not (cdr articles)))))
8339           (let* ((entry 
8340                   (or
8341                    (gnus-gethash (car art-group) gnus-newsrc-hashtb)
8342                    (gnus-gethash 
8343                     (gnus-group-prefixed-name 
8344                      (car art-group) 
8345                      (if select-method (list select-method "")
8346                        (gnus-find-method-for-group to-newsgroup)))
8347                     gnus-newsrc-hashtb)))
8348                  (info (nth 2 entry))
8349                  (article (car articles))
8350                  (marked (nth 3 info)))
8351             (if (not (memq article gnus-newsgroup-unreads))
8352                 (setcar (cdr (cdr info))
8353                         (gnus-add-to-range (nth 2 info) 
8354                                            (list (cdr art-group)))))
8355             ;; Copy any marks over to the new group.
8356             (let ((marks '((tick . gnus-newsgroup-marked)
8357                            (dormant . gnus-newsgroup-dormant)
8358                            (expire . gnus-newsgroup-expirable)
8359                            (bookmark . gnus-newsgroup-bookmarks)
8360                         ;   (score . gnus-newsgroup-scored)
8361                            (reply . gnus-newsgroup-replied)))
8362                   (to-article (cdr art-group)))
8363               (while marks
8364                 (if (memq article (symbol-value (cdr (car marks))))
8365                     (gnus-add-marked-articles 
8366                      (car info) (car (car marks)) (list to-article) info))
8367                 (setq marks (cdr marks)))))
8368         (gnus-message 1 "Couldn't copy article %s" (car articles)))
8369       (gnus-summary-remove-process-mark (car articles))
8370       (setq articles (cdr articles)))
8371     (kill-buffer copy-buf)))
8372
8373 (defun gnus-summary-import-article (file)
8374   "Import a random file into a mail newsgroup."
8375   (interactive "fImport file: ")
8376   (let ((group gnus-newsgroup-name)
8377         atts)
8378     (or (gnus-check-backend-function 'request-accept-article group)
8379         (error "%s does not support article importing" group))
8380     (or (file-readable-p file)
8381         (not (file-regular-p file))
8382         (error "Can't read %s" file))
8383     (save-excursion
8384       (set-buffer (get-buffer-create " *import file*"))
8385       (buffer-disable-undo (current-buffer))
8386       (erase-buffer)
8387       (insert-file-contents file)
8388       (goto-char (point-min))
8389       (if (nnheader-article-p)
8390           ()
8391         (setq atts (file-attributes file))
8392         (insert "From: " (read-string "From: ") "\n"
8393                 "Subject: " (read-string "Subject: ") "\n"
8394                 "Date: " (current-time-string (nth 5 atts)) "\n"
8395                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8396       (gnus-request-accept-article group t)
8397       (kill-buffer (current-buffer)))))
8398
8399 (defun gnus-summary-expire-articles ()
8400   "Expire all articles that are marked as expirable in the current group."
8401   (interactive)
8402   (if (and gnus-newsgroup-expirable
8403            (gnus-check-backend-function 
8404             'request-expire-articles gnus-newsgroup-name))
8405       (let ((expirable gnus-newsgroup-expirable))
8406         ;; The list of articles that weren't expired is returned.
8407         (setq gnus-newsgroup-expirable 
8408               (gnus-request-expire-articles gnus-newsgroup-expirable
8409                                             gnus-newsgroup-name))
8410         ;; We go through the old list of expirable, and mark all
8411         ;; really expired articles as non-existent.
8412         (while expirable
8413           (or (memq (car expirable) gnus-newsgroup-expirable)
8414               (gnus-summary-mark-as-read (car expirable) gnus-canceled-mark))
8415           (setq expirable (cdr expirable))))))
8416
8417 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
8418 (defun gnus-summary-delete-article (n)
8419   "Delete the N next (mail) articles.
8420 This command actually deletes articles. This is not a marking
8421 command. The article will disappear forever from you life, never to
8422 return. 
8423 If N is negative, delete backwards.
8424 If N is nil and articles have been marked with the process mark,
8425 delete these instead."
8426   (interactive "P")
8427   (or (gnus-check-backend-function 'request-expire-articles 
8428                                    gnus-newsgroup-name)
8429       (error "The current newsgroup does not support article deletion."))
8430   ;; Compute the list of articles to delete.
8431   (let ((articles (gnus-summary-work-articles n))
8432         not-deleted)
8433     (if (and gnus-novice-user
8434              (not (gnus-y-or-n-p 
8435                    (format "Do you really want to delete %s forever? "
8436                            (if (> (length articles) 1) "these articles"
8437                              "this article")))))
8438         ()
8439       ;; Delete the articles.
8440       (setq not-deleted (gnus-request-expire-articles 
8441                          articles gnus-newsgroup-name 'force))
8442       (while articles
8443         (gnus-summary-remove-process-mark (car articles))       
8444         ;; The backend might not have been able to delete the article
8445         ;; after all.  
8446         (or (memq (car articles) not-deleted)
8447             (gnus-summary-mark-as-read (car articles) gnus-canceled-mark))
8448         (setq articles (cdr articles))))
8449     (gnus-summary-position-cursor)
8450     not-deleted))
8451
8452 (defun gnus-summary-edit-article ()
8453   "Enter into a buffer and edit the current article.
8454 This will have permanent effect only in mail groups."
8455   (interactive)
8456   (or (gnus-check-backend-function 
8457        'request-replace-article gnus-newsgroup-name)
8458       (error "The current newsgroup does not support article editing."))
8459   (gnus-summary-select-article t)
8460   (other-window 1)
8461   (gnus-message 6 "C-c C-c to end edits")
8462   (setq buffer-read-only nil)
8463   (text-mode)
8464   (use-local-map (copy-keymap (current-local-map)))
8465   (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
8466   (goto-char (point-min))
8467   (search-forward "\n\n" nil t))
8468
8469 (defun gnus-summary-edit-article-done ()
8470   "Make edits to the current article permanent."
8471   (interactive)
8472   (if (not (gnus-request-replace-article 
8473             (cdr gnus-article-current) (car gnus-article-current) 
8474             (current-buffer)))
8475       (error "Couldn't replace article.")
8476     (gnus-article-mode)
8477     (use-local-map gnus-article-mode-map)
8478     (setq buffer-read-only t)
8479     (pop-to-buffer gnus-summary-buffer)))      
8480
8481 (defun gnus-summary-fancy-query ()
8482   "Query where the fancy respool algorithm would put this article."
8483   (interactive)
8484   (gnus-summary-select-article)
8485   (save-excursion
8486     (set-buffer gnus-article-buffer)
8487     (save-restriction
8488       (goto-char (point-min))
8489       (search-forward "\n\n")
8490       (narrow-to-region (point-min) (point))
8491       (pp-eval-expression (list 'quote (nnmail-split-fancy))))))
8492
8493 ;; Summary score commands.
8494
8495 ;; Suggested by boubaker@cenatls.cena.dgac.fr.
8496
8497 (defun gnus-summary-raise-score (n)
8498   "Raise the score of the current article by N."
8499   (interactive "p")
8500   (gnus-summary-set-score (+ (gnus-summary-article-score) n)))
8501
8502 (defun gnus-summary-lower-score (n)
8503   "Lower the score of the current article by N."
8504   (interactive "p")
8505   (gnus-summary-raise-score (- n)))
8506
8507 (defun gnus-summary-set-score (n)
8508   "Set the score of the current article to N."
8509   (interactive "p")
8510   ;; Skip dummy header line.
8511   (save-excursion
8512     (gnus-summary-show-thread)
8513     (if (eq (gnus-summary-article-mark) gnus-dummy-mark)
8514         (forward-line 1))
8515     (let ((buffer-read-only nil))
8516       ;; Set score.
8517       (gnus-summary-update-mark
8518        (if (= n (or gnus-summary-default-score 0)) ? 
8519          (if (< n (or gnus-summary-default-score 0)) 
8520              gnus-score-below-mark gnus-score-over-mark)) 'score))
8521     (let* ((article (gnus-summary-article-number))
8522            (score (assq article gnus-newsgroup-scored)))
8523       (if score (setcdr score n)
8524         (setq gnus-newsgroup-scored 
8525               (cons (cons article n) gnus-newsgroup-scored))))
8526     (gnus-summary-update-line)))
8527
8528 (defun gnus-summary-current-score ()
8529   "Return the score of the current article."
8530   (interactive)
8531   (message "%s" (gnus-summary-article-score)))
8532
8533 ;; Summary marking commands.
8534
8535 (defun gnus-summary-raise-same-subject-and-select (score)
8536   "Raise articles which has the same subject with SCORE and select the next."
8537   (interactive "p")
8538   (let ((subject (gnus-summary-subject-string)))
8539     (gnus-summary-raise-score score)
8540     (while (gnus-summary-search-subject nil nil subject)
8541       (gnus-summary-raise-score score))
8542     (gnus-summary-next-article t)))
8543
8544 (defun gnus-summary-raise-same-subject (score)
8545   "Raise articles which has the same subject with SCORE."
8546   (interactive "p")
8547   (let ((subject (gnus-summary-subject-string)))
8548     (gnus-summary-raise-score score)
8549     (while (gnus-summary-search-subject nil nil subject)
8550       (gnus-summary-raise-score score))
8551     (gnus-summary-next-subject 1 t)))
8552
8553 (defun gnus-score-default (level)
8554   (if level (prefix-numeric-value level) 
8555     gnus-score-interactive-default-score))
8556
8557 (defun gnus-summary-raise-thread (score)
8558   "Raise the score of the articles in the current thread with SCORE."
8559   (interactive "P")
8560   (setq score (1- (gnus-score-default score)))
8561   (let (e)
8562     (save-excursion
8563       (let ((level (gnus-summary-thread-level)))
8564         (gnus-summary-raise-score score)
8565         (while (and (zerop (gnus-summary-next-subject 1 nil t))
8566                     (> (gnus-summary-thread-level) level))
8567           (gnus-summary-raise-score score))
8568         (setq e (point))))
8569     (let ((gnus-summary-check-current t))
8570       (or (zerop (gnus-summary-next-subject 1 t))
8571           (goto-char e))))
8572   (gnus-summary-recenter)
8573   (gnus-summary-position-cursor)
8574   (gnus-set-mode-line 'summary))
8575
8576 (defun gnus-summary-lower-same-subject-and-select (score)
8577   "Raise articles which has the same subject with SCORE and select the next."
8578   (interactive "p")
8579   (gnus-summary-raise-same-subject-and-select (- score)))
8580
8581 (defun gnus-summary-lower-same-subject (score)
8582   "Raise articles which has the same subject with SCORE."
8583   (interactive "p")
8584   (gnus-summary-raise-same-subject (- score)))
8585
8586 (defun gnus-summary-lower-thread (score)
8587   "Lower score of articles in the current thread with SCORE."
8588   (interactive "P")
8589   (gnus-summary-raise-thread (- (1- (gnus-score-default score)))))
8590
8591 (defun gnus-summary-kill-same-subject-and-select (unmark)
8592   "Mark articles which has the same subject as read, and then select the next.
8593 If UNMARK is positive, remove any kind of mark.
8594 If UNMARK is negative, tick articles."
8595   (interactive "P")
8596   (if unmark
8597       (setq unmark (prefix-numeric-value unmark)))
8598   (let ((count
8599          (gnus-summary-mark-same-subject
8600           (gnus-summary-subject-string) unmark)))
8601     ;; Select next unread article. If auto-select-same mode, should
8602     ;; select the first unread article.
8603     (gnus-summary-next-article t (and gnus-auto-select-same
8604                                       (gnus-summary-subject-string)))
8605     (gnus-message 7 "%d articles are marked as %s"
8606                   count (if unmark "unread" "read"))))
8607
8608 (defun gnus-summary-kill-same-subject (unmark)
8609   "Mark articles which has the same subject as read. 
8610 If UNMARK is positive, remove any kind of mark.
8611 If UNMARK is negative, tick articles."
8612   (interactive "P")
8613   (if unmark
8614       (setq unmark (prefix-numeric-value unmark)))
8615   (let ((count
8616          (gnus-summary-mark-same-subject
8617           (gnus-summary-subject-string) unmark)))
8618     ;; If marked as read, go to next unread subject.
8619     (if (null unmark)
8620         ;; Go to next unread subject.
8621         (gnus-summary-next-subject 1 t))
8622     (gnus-message 7 "%d articles are marked as %s"
8623                   count (if unmark "unread" "read"))))
8624
8625 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8626   "Mark articles with same SUBJECT as read, and return marked number.
8627 If optional argument UNMARK is positive, remove any kinds of marks.
8628 If optional argument UNMARK is negative, mark articles as unread instead."
8629   (let ((count 1))
8630     (save-excursion
8631       (cond ((null unmark)
8632              (gnus-summary-mark-as-read nil gnus-killed-mark))
8633             ((> unmark 0)
8634              (gnus-summary-tick-article nil t))
8635             (t
8636              (gnus-summary-tick-article)))
8637       (while (and subject
8638                   (gnus-summary-search-forward nil subject))
8639         (cond ((null unmark)
8640                (gnus-summary-mark-as-read nil gnus-killed-mark))
8641               ((> unmark 0)
8642                (gnus-summary-tick-article nil t))
8643               (t
8644                (gnus-summary-tick-article)))
8645         (setq count (1+ count))))
8646     ;; Hide killed thread subtrees.  Does not work properly always.
8647     ;;(and (null unmark)
8648     ;;     gnus-thread-hide-killed
8649     ;;     (gnus-summary-hide-thread))
8650     ;; Return number of articles marked as read.
8651     count))
8652
8653 (defun gnus-summary-mark-as-processable (n &optional unmark)
8654   "Set the process mark on the next N articles.
8655 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8656 the process mark instead.  The difference between N and the actual
8657 number of articles marked is returned."
8658   (interactive "p")
8659   (let ((backward (< n 0))
8660         (n (abs n)))
8661   (while (and 
8662           (> n 0)
8663           (if unmark
8664               (gnus-summary-remove-process-mark (gnus-summary-article-number))
8665             (gnus-summary-set-process-mark (gnus-summary-article-number)))
8666           (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8667     (setq n (1- n)))
8668   (if (/= 0 n) (gnus-message 7 "No more articles"))
8669   (gnus-summary-recenter)
8670   (gnus-summary-position-cursor)
8671   n))
8672
8673 (defun gnus-summary-unmark-as-processable (n)
8674   "Remove the process mark from the next N articles.
8675 If N is negative, mark backward instead.  The difference between N and
8676 the actual number of articles marked is returned."
8677   (interactive "p")
8678   (gnus-summary-mark-as-processable n t))
8679
8680 (defun gnus-summary-unmark-all-processable ()
8681   "Remove the process mark from all articles."
8682   (interactive)
8683   (save-excursion
8684     (while gnus-newsgroup-processable
8685       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8686   (gnus-summary-position-cursor))
8687
8688 (defun gnus-summary-mark-as-expirable (n)
8689   "Mark N articles forward as expirable.
8690 If N is negative, mark backward instead. The difference between N and
8691 the actual number of articles marked is returned."
8692   (interactive "p")
8693   (gnus-summary-mark-forward n gnus-expirable-mark))
8694
8695 (defun gnus-summary-mark-article-as-replied (article)
8696   "Mark ARTICLE replied and update the summary line."
8697   (setq gnus-newsgroup-replied (cons article gnus-newsgroup-replied))
8698   (let ((buffer-read-only nil))
8699     (if (gnus-summary-goto-subject article)
8700         (progn
8701           (gnus-summary-update-mark gnus-replied-mark 'replied)
8702           t))))
8703
8704 (defun gnus-summary-set-bookmark (article)
8705   "Set a bookmark in current article."
8706   (interactive (list (gnus-summary-article-number)))
8707   (if (or (not (get-buffer gnus-article-buffer))
8708           (not gnus-current-article)
8709           (not gnus-article-current)
8710           (not (equal gnus-newsgroup-name (car gnus-article-current))))
8711       (error "No current article selected"))
8712   ;; Remove old bookmark, if one exists.
8713   (let ((old (assq article gnus-newsgroup-bookmarks)))
8714     (if old (setq gnus-newsgroup-bookmarks 
8715                   (delq old gnus-newsgroup-bookmarks))))
8716   ;; Set the new bookmark, which is on the form 
8717   ;; (article-number . line-number-in-body).
8718   (setq gnus-newsgroup-bookmarks 
8719         (cons 
8720          (cons article 
8721                (save-excursion
8722                  (set-buffer gnus-article-buffer)
8723                  (count-lines
8724                   (min (point)
8725                        (save-excursion
8726                          (goto-char (point-min))
8727                          (search-forward "\n\n" nil t)
8728                          (point)))
8729                   (point))))
8730          gnus-newsgroup-bookmarks))
8731   (gnus-message 6 "A bookmark has been added to the current article."))
8732
8733 (defun gnus-summary-remove-bookmark (article)
8734   "Remove the bookmark from the current article."
8735   (interactive (list (gnus-summary-article-number)))
8736   ;; Remove old bookmark, if one exists.
8737   (let ((old (assq article gnus-newsgroup-bookmarks)))
8738     (if old 
8739         (progn
8740           (setq gnus-newsgroup-bookmarks 
8741                 (delq old gnus-newsgroup-bookmarks))
8742           (gnus-message 6 "Removed bookmark."))
8743       (gnus-message 6 "No bookmark in current article."))))
8744
8745 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8746 (defun gnus-summary-mark-as-dormant (n)
8747   "Mark N articles forward as dormant.
8748 If N is negative, mark backward instead.  The difference between N and
8749 the actual number of articles marked is returned."
8750   (interactive "p")
8751   (gnus-summary-mark-forward n gnus-dormant-mark))
8752
8753 (defun gnus-summary-set-process-mark (article)
8754   "Set the process mark on ARTICLE and update the summary line."
8755   (setq gnus-newsgroup-processable (cons article gnus-newsgroup-processable))
8756   (let ((buffer-read-only nil))
8757     (if (gnus-summary-goto-subject article)
8758         (progn
8759           (gnus-summary-show-thread)
8760           (and (eq (gnus-summary-article-mark) gnus-dummy-mark)
8761                (forward-line 1))
8762           (gnus-summary-update-mark gnus-process-mark 'replied)
8763           t))))
8764
8765 (defun gnus-summary-remove-process-mark (article)
8766   "Remove the process mark from ARTICLE and update the summary line."
8767   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8768   (let ((buffer-read-only nil))
8769     (if (gnus-summary-goto-subject article)
8770         (progn
8771           (gnus-summary-show-thread)
8772           (and (eq (gnus-summary-article-mark) gnus-dummy-mark)
8773                (forward-line 1))
8774           (gnus-summary-update-mark ?  'replied)
8775           (if (memq article gnus-newsgroup-replied) 
8776               (gnus-summary-update-mark gnus-replied-mark 'replied))
8777           t))))
8778
8779 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8780   "Mark N articles as read forwards.
8781 If N is negative, mark backwards instead.
8782 Mark with MARK. If MARK is ? , ?! or ??, articles will be
8783 marked as unread. 
8784 The difference between N and the actual number of articles marked is
8785 returned."
8786   (interactive "p")
8787   (gnus-set-global-variables)
8788   (let ((backward (< n 0))
8789         (n (abs n))
8790         (mark (or mark gnus-del-mark)))
8791   (while (and (> n 0)
8792               (gnus-summary-mark-article nil mark no-expire)
8793               (zerop (gnus-summary-next-subject 
8794                       (if backward -1 1) gnus-summary-goto-unread t)))
8795     (setq n (1- n)))
8796   (if (/= 0 n) (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8797   (gnus-summary-recenter)
8798   (gnus-summary-position-cursor)
8799   (gnus-set-mode-line 'summary)
8800   n))
8801
8802 (defun gnus-summary-mark-article (&optional article mark no-expire)
8803   "Mark ARTICLE with MARK.
8804 MARK can be any character.
8805 Five MARK strings are reserved: ?  (unread), 
8806 ?! (ticked), ?? (dormant), ?D (read), ?E (expirable).
8807 If MARK is nil, then the default character ?D is used.
8808 If ARTICLE is nil, then the article on the current line will be
8809 marked." 
8810   ;; If no mark is given, then we check auto-expiring.
8811   (and (not no-expire)
8812        gnus-newsgroup-auto-expire 
8813        (or (not mark)
8814            (and (numberp mark) (or (= mark gnus-killed-mark)
8815                                    (= mark gnus-del-mark)
8816                                    (= mark gnus-catchup-mark)
8817                                    (= mark gnus-low-score-mark)
8818                                    (= mark gnus-read-mark))))
8819        (setq mark gnus-expirable-mark))
8820   (let* ((mark (or (and (stringp mark) (aref mark 0)) mark gnus-del-mark))
8821          (article (or article (gnus-summary-article-number))))
8822     (if (or (= mark gnus-unread-mark) 
8823             (= mark gnus-ticked-mark) 
8824             (= mark gnus-dormant-mark))
8825         (gnus-mark-article-as-unread article mark)
8826       (gnus-mark-article-as-read article mark))
8827
8828     ;; See whether the article is to be put in the cache.
8829     (and gnus-use-cache
8830          (save-excursion
8831            (gnus-cache-possibly-enter-article 
8832             gnus-newsgroup-name article 
8833             (gnus-get-header-by-number article)
8834             (= mark gnus-ticked-mark)
8835             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8836
8837     (if (gnus-summary-goto-subject article)
8838         (let ((buffer-read-only nil))
8839           (gnus-summary-show-thread)
8840           (and (eq (gnus-summary-article-mark) gnus-dummy-mark)
8841                (forward-line 1))
8842           ;; Fix the mark.
8843           (gnus-summary-update-mark mark 'unread)
8844           t))))
8845
8846 (defun gnus-summary-update-mark (mark type)
8847   (beginning-of-line)
8848   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8849         plist)
8850     (if (not forward)
8851         ()
8852       (forward-char forward)
8853       (setq plist (text-properties-at (point)))
8854       (delete-char 1)
8855       (insert mark)
8856       (and plist (add-text-properties (1- (point)) (point) plist))
8857       (and (eq type 'unread)
8858            (add-text-properties (1- (point)) (point) (list 'gnus-mark mark)))
8859       (gnus-summary-update-line (eq mark gnus-unread-mark)))))
8860   
8861 (defun gnus-mark-article-as-read (article &optional mark)
8862   "Enter ARTICLE in the pertinent lists and remove it from others."
8863   ;; Make the article expirable.
8864   (let ((mark (or (and (stringp mark) (aref mark 0)) mark gnus-del-mark)))
8865     (if (= mark gnus-expirable-mark)
8866         (setq gnus-newsgroup-expirable (cons article gnus-newsgroup-expirable))
8867       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8868     ;; Remove from unread and marked lists.
8869     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8870     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8871     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8872     ;; Possibly remove from cache, if that is used. 
8873     (and gnus-use-cache 
8874          (gnus-cache-possibly-remove-article
8875           gnus-newsgroup-name article 
8876           (memq article gnus-newsgroup-marked)
8877           (memq article gnus-newsgroup-dormant)
8878           (or (memq article gnus-newsgroup-unreads)
8879               (memq article gnus-newsgroup-unselected))))))
8880
8881 (defun gnus-mark-article-as-unread (article &optional mark)
8882   "Enter ARTICLE in the pertinent lists and remove it from others."
8883   (let ((mark (or (and (stringp mark) (aref mark 0)) mark gnus-ticked-mark)))
8884     ;; Add to unread list.
8885     (or (memq article gnus-newsgroup-unreads)
8886         (setq gnus-newsgroup-unreads (cons article gnus-newsgroup-unreads)))
8887     ;; If CLEAR-MARK is non-nil, the article must be removed from mark
8888     ;; lists.  Otherwise, it must be added to the list.
8889     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8890     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8891     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8892     (if (= mark gnus-ticked-mark)
8893         (setq gnus-newsgroup-marked (cons article gnus-newsgroup-marked)))
8894     (if (= mark gnus-dormant-mark)
8895         (setq gnus-newsgroup-dormant (cons article gnus-newsgroup-dormant)))))
8896
8897 (defalias 'gnus-summary-mark-as-unread-forward 
8898   'gnus-summary-tick-article-forward)
8899 (make-obsolete 'gnus-summary-mark-as-unread-forward 
8900                'gnus-summary-tick-article-forward)
8901 (defun gnus-summary-tick-article-forward (n)
8902   "Tick N articles forwards.
8903 If N is negative, tick backwards instead.
8904 The difference between N and the number of articles ticked is returned."
8905   (interactive "p")
8906   (gnus-summary-mark-forward n gnus-ticked-mark))
8907
8908 (defalias 'gnus-summary-mark-as-unread-backward 
8909   'gnus-summary-tick-article-backward)
8910 (make-obsolete 'gnus-summary-mark-as-unread-backward 
8911                'gnus-summary-tick-article-backward)
8912 (defun gnus-summary-tick-article-backward (n)
8913   "Tick N articles backwards.
8914 The difference between N and the number of articles ticked is returned."
8915   (interactive "p")
8916   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8917
8918 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8919 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8920 (defun gnus-summary-tick-article (&optional article clear-mark)
8921   "Mark current article as unread.
8922 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8923 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8924   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8925                                        gnus-ticked-mark)))
8926
8927 (defun gnus-summary-mark-as-read-forward (n)
8928   "Mark N articles as read forwards.
8929 If N is negative, mark backwards instead.
8930 The difference between N and the actual number of articles marked is
8931 returned."
8932   (interactive "p")
8933   (gnus-summary-mark-forward n gnus-del-mark t))
8934
8935 (defun gnus-summary-mark-as-read-backward (n)
8936   "Mark the N articles as read backwards.
8937 The difference between N and the actual number of articles marked is
8938 returned."
8939   (interactive "p")
8940   (gnus-summary-mark-forward (- n) gnus-del-mark t))
8941
8942 (defun gnus-summary-mark-as-read (&optional article mark)
8943   "Mark current article as read.
8944 ARTICLE specifies the article to be marked as read.
8945 MARK specifies a string to be inserted at the beginning of the line."
8946   (gnus-summary-mark-article article mark))
8947
8948 (defun gnus-summary-clear-mark-forward (n)
8949   "Clear marks from N articles forward.
8950 If N is negative, clear backward instead.
8951 The difference between N and the number of marks cleared is returned."
8952   (interactive "p")
8953   (gnus-summary-mark-forward n gnus-unread-mark))
8954
8955 (defun gnus-summary-clear-mark-backward (n)
8956   "Clear marks from N articles backward.
8957 The difference between N and the number of marks cleared is returned."
8958   (interactive "p")
8959   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8960
8961 (defun gnus-summary-mark-unread-as-read ()
8962   "Intended to be used by `gnus-summary-mark-article-hook'."
8963   (or (memq gnus-current-article gnus-newsgroup-marked)
8964       (memq gnus-current-article gnus-newsgroup-dormant)
8965       (memq gnus-current-article gnus-newsgroup-expirable)
8966       (gnus-summary-mark-as-read gnus-current-article gnus-read-mark)))
8967
8968 (defun gnus-summary-mark-region-as-read (point mark all)
8969   "Mark all unread articles between point and mark as read.
8970 If given a prefix, mark all articles between point and mark as read,
8971 even ticked and dormant ones."
8972   (interactive "r\nP")
8973   (save-excursion
8974     (goto-char point)
8975     (beginning-of-line)
8976     (while (and 
8977             (< (point) mark)
8978             (progn
8979               (and
8980                (or all
8981                    (and
8982                     (not (memq (gnus-summary-article-number)
8983                                gnus-newsgroup-marked))
8984                     (not (memq (gnus-summary-article-number)
8985                                gnus-newsgroup-dormant))))
8986                (gnus-summary-mark-article
8987                 (gnus-summary-article-number) gnus-del-mark))
8988               t)
8989             (zerop (forward-line 1))))))
8990
8991 ;; Fix by Per Abrahamsen <amanda@iesd.auc.dk>.
8992 (defalias 'gnus-summary-delete-marked-as-read 
8993   'gnus-summary-remove-lines-marked-as-read)
8994 (make-obsolete 'gnus-summary-delete-marked-as-read 
8995                'gnus-summary-remove-lines-marked-as-read)
8996 (defun gnus-summary-remove-lines-marked-as-read ()
8997   "Remove lines that are marked as read."
8998   (interactive)
8999   (gnus-summary-remove-lines-marked-with 
9000    (concat (mapconcat
9001             (lambda (char) (char-to-string (symbol-value char)))
9002             '(gnus-del-mark gnus-read-mark gnus-ancient-mark
9003               gnus-killed-mark gnus-kill-file-mark
9004               gnus-low-score-mark gnus-expirable-mark)
9005             ""))))
9006
9007 (defalias 'gnus-summary-delete-marked-with 
9008   'gnus-summary-remove-lines-marked-with)
9009 (make-obsolete 'gnus-summary-delete-marked-with 
9010                'gnus-summary-remove-lines-marked-with)
9011 ;; Rewrite by Daniel Quinlan <quinlan@best.com>.
9012 (defun gnus-summary-remove-lines-marked-with (marks)
9013   "Remove lines that are marked with MARKS (e.g. \"DK\")."
9014   (interactive "sMarks: ")
9015   ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
9016   (gnus-set-global-variables)
9017   (let ((buffer-read-only nil)
9018         (marks (concat "^[" marks "]")))
9019     (goto-char (point-min))
9020     (if gnus-newsgroup-adaptive
9021         (gnus-score-remove-lines-adaptive marks)
9022       (while (re-search-forward marks nil t)
9023         (gnus-delete-line)))
9024     ;; If we use dummy roots, we have to do an additional sweep over
9025     ;; the buffer.
9026     (if (not (eq gnus-summary-make-false-root 'dummy))
9027         ()
9028       (goto-char (point-min))
9029       (setq marks (concat "^[" (char-to-string gnus-dummy-mark) "]"))
9030       (while (re-search-forward marks nil t)
9031         (if (gnus-subject-equal
9032              (gnus-summary-subject-string)
9033              (progn
9034                (forward-line 1)
9035                (gnus-summary-subject-string)))
9036             ()
9037           (forward-line -1)
9038           (gnus-delete-line)))))
9039   (or (zerop (buffer-size))
9040       (if (eobp)
9041           (gnus-summary-prev-subject 1)
9042         (gnus-summary-position-cursor))))
9043
9044 (defun gnus-summary-expunge-below (score)
9045   "Remove articles with score less than SCORE."
9046   (interactive "P")
9047   (gnus-set-global-variables)
9048   (setq score (if score
9049                   (prefix-numeric-value score)
9050                 (or gnus-summary-default-score 0)))
9051   (save-excursion
9052     (set-buffer gnus-summary-buffer)
9053     (goto-char (point-min))
9054     (let ((buffer-read-only nil)
9055           beg)
9056       (while (not (eobp))
9057         (if (< (gnus-summary-article-score) score)
9058             (progn
9059               (setq beg (point))
9060               (forward-line 1)
9061               (delete-region beg (point)))
9062           (forward-line 1)))
9063       ;; Adjust point.
9064       (or (zerop (buffer-size))
9065           (if (eobp)
9066               (gnus-summary-prev-subject 1)
9067             (gnus-summary-position-cursor))))))
9068
9069 (defun gnus-summary-mark-below (score mark)
9070   "Mark articles with score less than SCORE with MARK."
9071   (interactive "P\ncMark: ")
9072   (gnus-set-global-variables)
9073   (setq score (if score
9074                   (prefix-numeric-value score)
9075                 (or gnus-summary-default-score 0)))
9076   (save-excursion
9077     (set-buffer gnus-summary-buffer)
9078     (goto-char (point-min))
9079     (while (not (eobp))
9080       (and (< (gnus-summary-article-score) score)
9081            (gnus-summary-mark-article nil mark))
9082       (forward-line 1))))
9083
9084 (defun gnus-summary-kill-below (score)
9085   "Mark articles with score below SCORE as read."
9086   (interactive "P")
9087   (gnus-summary-mark-below score gnus-killed-mark))
9088
9089 (defun gnus-summary-clear-above (score)
9090   "Clear all marks from articles with score above SCORE."
9091   (interactive "P")
9092   (gnus-summary-mark-above score gnus-unread-mark))
9093
9094 (defun gnus-summary-tick-above (score)
9095   "Tick all articles with score above SCORE."
9096   (interactive "P")
9097   (gnus-summary-mark-above score gnus-ticked-mark))
9098
9099 (defun gnus-summary-mark-above (score mark)
9100   "Mark articles with score over SCORE with MARK."
9101   (interactive "P\ncMark: ")
9102   (setq score (if score
9103                   (prefix-numeric-value score)
9104                 (or gnus-summary-default-score 0)))
9105   (save-excursion
9106     (set-buffer gnus-summary-buffer)
9107     (goto-char (point-min))
9108     (while (not (eobp))
9109       (if (> (gnus-summary-article-score) score)
9110           (progn
9111             (gnus-summary-mark-article nil mark)
9112             (forward-line 1))
9113         (forward-line 1)))))
9114
9115 ;; Suggested by Daniel Quinlan <quinlan@best.com>.  
9116 (defun gnus-summary-show-all-expunged ()
9117   "Display all the hidden articles that were expunged for low scores."
9118   (interactive)
9119   (let ((buffer-read-only nil))
9120     (let ((scored gnus-newsgroup-scored)
9121           headers h)
9122       (while scored
9123         (or (gnus-summary-goto-subject (car (car scored)))
9124             (and (setq h (gnus-get-header-by-number (car (car scored))))
9125                  (< (cdr (car scored)) gnus-summary-expunge-below)
9126                  (setq headers (cons h headers))))
9127         (setq scored (cdr scored)))
9128       (or headers (error "No expunged articles hidden."))
9129       (goto-char (point-min))
9130       (save-excursion 
9131         (gnus-summary-update-lines 
9132          (point)
9133          (progn
9134            (gnus-summary-prepare-threads (nreverse headers) 0)
9135            (point)))))
9136     (goto-char (point-min))
9137     (gnus-summary-position-cursor)))
9138
9139 (defun gnus-summary-show-all-dormant ()
9140   "Display all the hidden articles that are marked as dormant."
9141   (interactive)
9142   (let ((buffer-read-only nil))
9143     (goto-char (point-min))
9144     (let ((dormant gnus-newsgroup-dormant)
9145           headers h)
9146       (while dormant
9147         (or (gnus-summary-goto-subject (car dormant))
9148             (and (setq h (gnus-get-header-by-number (car dormant)))
9149                  (setq headers (cons h headers))))
9150         (setq dormant (cdr dormant)))
9151       (or headers (error "No dormant articles hidden."))
9152       (save-excursion 
9153         (gnus-summary-update-lines 
9154          (point)
9155          (progn
9156            (gnus-summary-prepare-threads (nreverse headers) 0)
9157            (point)))))
9158     (goto-char (point-min))
9159     (gnus-summary-position-cursor)))
9160
9161 (defun gnus-summary-hide-all-dormant ()
9162   "Hide all dormant articles."
9163   (interactive)
9164   (gnus-summary-remove-lines-marked-with (char-to-string gnus-dormant-mark))
9165   (gnus-summary-position-cursor))
9166
9167 (defun gnus-summary-catchup (all &optional quietly to-here not-mark)
9168   "Mark all articles not marked as unread in this newsgroup as read.
9169 If prefix argument ALL is non-nil, all articles are marked as read.
9170 If QUIETLY is non-nil, no questions will be asked.
9171 If TO-HERE is non-nil, it should be a point in the buffer. All
9172 articles before this point will be marked as read.
9173 The number of articles marked as read is returned."
9174   (interactive "P")
9175   (prog1
9176       (if (or quietly
9177               (not gnus-interactive-catchup) ;Without confirmation?
9178               gnus-expert-user
9179               (gnus-y-or-n-p
9180                (if all
9181                    "Mark absolutely all articles as read? "
9182                  "Mark all unread articles as read? ")))
9183           (if (and not-mark 
9184                    (not gnus-newsgroup-adaptive)
9185                    (not gnus-newsgroup-auto-expire))
9186               (progn
9187                 (and all (setq gnus-newsgroup-marked nil
9188                                gnus-newsgroup-dormant nil))
9189                 (setq gnus-newsgroup-unreads 
9190                       (append gnus-newsgroup-marked gnus-newsgroup-dormant)))
9191             ;; We actually mark all articles as canceled, which we
9192             ;; have to do when using auto-expiry or adaptive scoring. 
9193             (let ((unreads (length gnus-newsgroup-unreads)))
9194               (if (gnus-summary-first-subject (not all))
9195                   (while (and (gnus-summary-mark-as-read nil gnus-catchup-mark)
9196                               (if to-here (< (point) to-here) t)
9197                               (gnus-summary-search-subject nil (not all)))))
9198               (- unreads (length gnus-newsgroup-unreads))
9199               (or to-here
9200                   (setq gnus-newsgroup-unreads gnus-newsgroup-marked)))))
9201     (gnus-summary-position-cursor)))
9202
9203 (defun gnus-summary-catchup-to-here (&optional all)
9204   "Mark all unticked articles before the current one as read.
9205 If ALL is non-nil, also mark ticked and dormant articles as read."
9206   (interactive)
9207   (beginning-of-line)
9208   (gnus-summary-catchup all t (point))
9209   (gnus-set-mode-line 'summary)
9210   (gnus-summary-position-cursor))
9211
9212 (defun gnus-summary-catchup-all (&optional quietly)
9213   "Mark all articles in this newsgroup as read."
9214   (interactive)
9215   (gnus-summary-catchup t quietly))
9216
9217 (defun gnus-summary-catchup-and-exit (all &optional quietly)
9218   "Mark all articles not marked as unread in this newsgroup as read, then exit.
9219 If prefix argument ALL is non-nil, all articles are marked as read."
9220   (interactive "P")
9221   (gnus-summary-catchup all quietly nil 'fast)
9222   ;; Select next newsgroup or exit.
9223   (if (eq gnus-auto-select-next 'quietly)
9224       (gnus-summary-next-group nil)
9225     (gnus-summary-exit)))
9226
9227 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
9228   "Mark all articles in this newsgroup as read, and then exit."
9229   (interactive)
9230   (gnus-summary-catchup-and-exit t quietly))
9231
9232 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
9233 (defun gnus-summary-catchup-and-goto-next-group (all)
9234   "Mark all articles in this group as read and select the next group.
9235 If given a prefix, mark all articles, unread as well as ticked, as
9236 read." 
9237   (interactive "P")
9238   (gnus-summary-catchup all)
9239   (gnus-summary-next-group))
9240
9241 ;; Thread-based commands.
9242
9243 (defun gnus-summary-toggle-threads (arg)
9244   "Toggle showing conversation threads.
9245 If ARG is positive number, turn showing conversation threads on."
9246   (interactive "P")
9247   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
9248     (setq gnus-show-threads
9249           (if (null arg) (not gnus-show-threads)
9250             (> (prefix-numeric-value arg) 0)))
9251     (gnus-summary-prepare)
9252     (gnus-summary-goto-subject current)
9253     (gnus-summary-position-cursor)))
9254
9255 (defun gnus-summary-show-all-threads ()
9256   "Show all threads."
9257   (interactive)
9258   (save-excursion
9259     (let ((buffer-read-only nil))
9260       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
9261   (gnus-summary-position-cursor))
9262
9263 (defun gnus-summary-show-thread ()
9264   "Show thread subtrees.
9265 Returns nil if no thread was there to be shown."
9266   (interactive)
9267   (prog1
9268       (save-excursion
9269         (let ((buffer-read-only nil)
9270               ;; first goto end then to beg, to have point at beg after let
9271               (end (progn (end-of-line) (point)))
9272               (beg (progn (beginning-of-line) (point))))
9273           (prog1
9274               ;; Any hidden lines here?
9275               (search-forward "\r" end t)
9276             (subst-char-in-region beg end ?\^M ?\n t))))
9277     (gnus-summary-position-cursor)))
9278
9279 (defun gnus-summary-hide-all-threads ()
9280   "Hide all thread subtrees."
9281   (interactive)
9282   (save-excursion
9283     (goto-char (point-min))
9284     (gnus-summary-hide-thread)
9285     (while (and (not (eobp)) (zerop (forward-line 1)))
9286       (gnus-summary-hide-thread)))
9287   (gnus-summary-position-cursor))
9288
9289 (defun gnus-summary-hide-thread ()
9290   "Hide thread subtrees.
9291 Returns nil if no threads were there to be hidden."
9292   (interactive)
9293   (let ((buffer-read-only nil)
9294         (start (point))
9295         (level (gnus-summary-thread-level))
9296         (end (point)))
9297     ;; Go forward until either the buffer ends or the subthread
9298     ;; ends. 
9299     (if (eobp)
9300         ()
9301       (while (and (zerop (forward-line 1))
9302                   (> (gnus-summary-thread-level) level))
9303         (setq end (point)))
9304       (prog1
9305           (save-excursion
9306             (goto-char end)
9307             (search-backward "\n" start t))
9308         (subst-char-in-region start end ?\n ?\^M t)
9309         (forward-line -1)))))
9310
9311 (defun gnus-summary-go-to-next-thread (&optional previous)
9312   "Go to the same level (or less) next thread.
9313 If PREVIOUS is non-nil, go to previous thread instead.
9314 Return the article number moved to, or nil if moving was impossible."
9315   (let ((level (gnus-summary-thread-level))
9316         (article (gnus-summary-article-number)))
9317     (if previous 
9318         (while (and (zerop (gnus-summary-prev-subject 1))
9319                     (> (gnus-summary-thread-level) level)))
9320       (while (and (zerop (gnus-summary-next-subject 1))
9321                   (> (gnus-summary-thread-level) level))))
9322     (gnus-summary-recenter)
9323     (gnus-summary-position-cursor)
9324     (let ((oart (gnus-summary-article-number)))
9325       (and (/= oart article) oart))))
9326
9327 (defun gnus-summary-next-thread (n)
9328   "Go to the same level next N'th thread.
9329 If N is negative, search backward instead.
9330 Returns the difference between N and the number of skips actually
9331 done."
9332   (interactive "p")
9333   (let ((backward (< n 0))
9334         (n (abs n)))
9335   (while (and (> n 0)
9336               (gnus-summary-go-to-next-thread backward))
9337     (setq n (1- n)))
9338   (gnus-summary-position-cursor)
9339   (if (/= 0 n) (gnus-message 7 "No more threads"))
9340   n))
9341
9342 (defun gnus-summary-prev-thread (n)
9343   "Go to the same level previous N'th thread.
9344 Returns the difference between N and the number of skips actually
9345 done."
9346   (interactive "p")
9347   (gnus-summary-next-thread (- n)))
9348
9349 (defun gnus-summary-go-down-thread (&optional same)
9350   "Go down one level in the current thread.
9351 If SAME is non-nil, also move to articles of the same level."
9352   (let ((level (gnus-summary-thread-level))
9353         (start (point)))
9354     (if (and (zerop (forward-line 1))
9355              (> (gnus-summary-thread-level) level))
9356         t
9357       (goto-char start)
9358       nil)))
9359
9360 (defun gnus-summary-go-up-thread ()
9361   "Go up one level in the current thread."
9362   (let ((level (gnus-summary-thread-level))
9363         (start (point)))
9364     (while (and (zerop (forward-line -1))
9365                 (>= (gnus-summary-thread-level) level)))
9366     (if (>= (gnus-summary-thread-level) level)
9367         (progn
9368           (goto-char start)
9369           nil)
9370       t)))
9371
9372 (defun gnus-summary-down-thread (n)
9373   "Go down thread N steps.
9374 If N is negative, go up instead.
9375 Returns the difference between N and how many steps down that were
9376 taken."
9377   (interactive "p")
9378   (let ((up (< n 0))
9379         (n (abs n)))
9380   (while (and (> n 0)
9381               (if up (gnus-summary-go-up-thread)
9382                 (gnus-summary-go-down-thread)))
9383     (setq n (1- n)))
9384   (gnus-summary-position-cursor)
9385   (if (/= 0 n) (gnus-message 7 "Can't go further"))
9386   n))
9387
9388 (defun gnus-summary-up-thread (n)
9389   "Go up thread N steps.
9390 If N is negative, go up instead.
9391 Returns the difference between N and how many steps down that were
9392 taken."
9393   (interactive "p")
9394   (gnus-summary-down-thread (- n)))
9395
9396 (defun gnus-summary-kill-thread (unmark)
9397   "Mark articles under current thread as read.
9398 If the prefix argument is positive, remove any kinds of marks.
9399 If the prefix argument is negative, tick articles instead."
9400   (interactive "P")
9401   (if unmark
9402       (setq unmark (prefix-numeric-value unmark)))
9403   (let ((killing t)
9404         (level (gnus-summary-thread-level)))
9405     (save-excursion
9406       (while killing
9407         ;; Mark the article...
9408         (cond ((null unmark) (gnus-summary-mark-as-read nil gnus-killed-mark))
9409               ((> unmark 0) (gnus-summary-tick-article nil t))
9410               (t (gnus-summary-tick-article)))
9411         ;; ...and go forward until either the buffer ends or the subtree
9412         ;; ends. 
9413         (if (not (and (zerop (forward-line 1))
9414                       (> (gnus-summary-thread-level) level)))
9415             (setq killing nil))))
9416     ;; Hide killed subtrees.
9417     (and (null unmark)
9418          gnus-thread-hide-killed
9419          (gnus-summary-hide-thread))
9420     ;; If marked as read, go to next unread subject.
9421     (if (null unmark)
9422         ;; Go to next unread subject.
9423         (gnus-summary-next-subject 1 t)))
9424   (gnus-set-mode-line 'summary))
9425
9426 ;; Summary sorting commands
9427
9428 (defun gnus-summary-sort-by-number (&optional reverse)
9429   "Sort summary buffer by article number.
9430 Argument REVERSE means reverse order."
9431   (interactive "P")
9432   (gnus-summary-sort 
9433    (cons 'gnus-summary-article-number 'gnus-thread-sort-by-number) reverse))
9434
9435 (defun gnus-summary-sort-by-author (&optional reverse)
9436   "Sort summary buffer by author name alphabetically.
9437 If case-fold-search is non-nil, case of letters is ignored.
9438 Argument REVERSE means reverse order."
9439   (interactive "P")
9440   (gnus-summary-sort
9441    (cons
9442     (lambda ()
9443       (let ((extract (funcall
9444                       gnus-extract-address-components
9445                       (header-from (gnus-get-header-by-number
9446                                     (gnus-summary-article-number))))))
9447         (or (car extract) (cdr extract))))
9448     'gnus-thread-sort-by-author)
9449    reverse))
9450
9451 (defun gnus-summary-sort-by-subject (&optional reverse)
9452   "Sort summary buffer by subject alphabetically. `Re:'s are ignored.
9453 If case-fold-search is non-nil, case of letters is ignored.
9454 Argument REVERSE means reverse order."
9455   (interactive "P")
9456   (gnus-summary-sort
9457    (cons
9458     (lambda ()
9459       (downcase (gnus-simplify-subject (gnus-summary-subject-string))))
9460     'gnus-thread-sort-by-subject)
9461    reverse))
9462
9463 (defun gnus-summary-sort-by-date (&optional reverse)
9464   "Sort summary buffer by date.
9465 Argument REVERSE means reverse order."
9466   (interactive "P")
9467   (gnus-summary-sort
9468    (cons
9469     (lambda ()
9470       (gnus-sortable-date
9471        (header-date 
9472         (gnus-get-header-by-number (gnus-summary-article-number)))))
9473     'gnus-thread-sort-by-date)
9474    reverse))
9475
9476 (defun gnus-summary-sort-by-score (&optional reverse)
9477   "Sort summary buffer by score.
9478 Argument REVERSE means reverse order."
9479   (interactive "P")
9480   (gnus-summary-sort 
9481    (cons 'gnus-summary-article-score 'gnus-thread-sort-by-score)
9482    (not reverse)))
9483
9484 (defvar gnus-summary-already-sorted nil)
9485 (defun gnus-summary-sort (predicate reverse)
9486   ;; Sort summary buffer by PREDICATE.  REVERSE means reverse order. 
9487   (if gnus-summary-already-sorted
9488       ()
9489     (let (buffer-read-only)
9490       (if (not gnus-show-threads)
9491           (progn
9492             (goto-char (point-min))
9493             (sort-subr reverse 'forward-line 'end-of-line (car predicate)))
9494         (let ((gnus-thread-sort-functions (list (cdr predicate)))
9495               (gnus-summary-prepare-hook nil)
9496               (gnus-summary-already-sorted nil))
9497           (gnus-summary-prepare)
9498           (and gnus-show-threads
9499                gnus-thread-hide-subtree
9500                (gnus-summary-hide-all-threads))
9501           ;; If in async mode, we send some info to the backend.
9502           (and gnus-newsgroup-async
9503                (setq gnus-newsgroup-threads (nreverse gnus-newsgroup-threads))
9504                (gnus-request-asynchronous 
9505                 gnus-newsgroup-name
9506                 (if (and gnus-asynchronous-article-function
9507                          (fboundp gnus-asynchronous-article-function))
9508                     (funcall gnus-asynchronous-article-function
9509                              gnus-newsgroup-threads)))))))))
9510
9511   
9512 (defun gnus-sortable-date (date)
9513   "Make sortable string by string-lessp from DATE.
9514 Timezone package is used."
9515   (let* ((date   (timezone-fix-time date nil nil)) ;[Y M D H M S]
9516          (year   (aref date 0))
9517          (month  (aref date 1))
9518          (day    (aref date 2)))
9519     (timezone-make-sortable-date 
9520      year month day 
9521      (timezone-make-time-string
9522       (aref date 3) (aref date 4) (aref date 5)))))
9523
9524
9525 ;; Summary saving commands.
9526
9527 (defun gnus-summary-save-article (n)
9528   "Save the current article using the default saver function.
9529 If N is a positive number, save the N next articles.
9530 If N is a negative number, save the N previous articles.
9531 If N is nil and any articles have been marked with the process mark,
9532 save those articles instead.
9533 The variable `gnus-default-article-saver' specifies the saver function."
9534   (interactive "P")
9535   (let ((articles (gnus-summary-work-articles n)))
9536     (while articles
9537       (let ((header (gnus-get-header-by-number (car articles))))
9538         (if (vectorp header)
9539             (progn
9540               (gnus-summary-select-article t nil nil (car articles))
9541               (or gnus-save-all-headers
9542                   (gnus-article-hide-headers t))
9543               ;; Remove any X-Gnus lines.
9544               (save-excursion
9545                 (save-restriction
9546                   (set-buffer gnus-article-buffer)
9547                   (let ((buffer-read-only nil))
9548                     (goto-char (point-min))
9549                     (narrow-to-region (point) (or (search-forward "\n\n" nil t)
9550                                                   (point-max)))
9551                     (while (re-search-forward "^X-Gnus" nil t)
9552                       (beginning-of-line)
9553                       (delete-region (point)
9554                                      (progn (forward-line 1) (point))))
9555                     (widen))))
9556               (save-excursion
9557                 (if gnus-default-article-saver
9558                     (funcall gnus-default-article-saver)
9559                   (error "No default saver is defined."))))
9560           (if (assq 'name header)
9561               (gnus-copy-file (cdr (assq 'name header)))
9562             (gnus-message 1 "Article %d is unsaveable" (car articles)))))
9563       (gnus-summary-remove-process-mark (car articles))
9564       (setq articles (cdr articles)))
9565     (gnus-summary-position-cursor)
9566     n))
9567
9568 (defun gnus-summary-pipe-output (arg)
9569   "Pipe the current article to a subprocess.
9570 If N is a positive number, pipe the N next articles.
9571 If N is a negative number, pipe the N previous articles.
9572 If N is nil and any articles have been marked with the process mark,
9573 pipe those articles instead."
9574   (interactive "P")
9575   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9576     (gnus-summary-save-article arg)))
9577
9578 (defun gnus-summary-save-article-mail (arg)
9579   "Append the current article to an mail file.
9580 If N is a positive number, save the N next articles.
9581 If N is a negative number, save the N previous articles.
9582 If N is nil and any articles have been marked with the process mark,
9583 save those articles instead."
9584   (interactive "P")
9585   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9586     (gnus-summary-save-article arg)))
9587
9588 (defun gnus-summary-save-article-rmail (arg)
9589   "Append the current article to an rmail file.
9590 If N is a positive number, save the N next articles.
9591 If N is a negative number, save the N previous articles.
9592 If N is nil and any articles have been marked with the process mark,
9593 save those articles instead."
9594   (interactive "P")
9595   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9596     (gnus-summary-save-article arg)))
9597
9598 (defun gnus-summary-save-article-file (arg)
9599   "Append the current article to a file.
9600 If N is a positive number, save the N next articles.
9601 If N is a negative number, save the N previous articles.
9602 If N is nil and any articles have been marked with the process mark,
9603 save those articles instead."
9604   (interactive "P")
9605   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9606     (gnus-summary-save-article arg)))
9607
9608 (defun gnus-read-save-file-name (prompt default-name)
9609   (let ((methods gnus-split-methods)
9610         split-name)
9611     (if (not gnus-split-methods)
9612         ()
9613       (save-excursion
9614         (set-buffer gnus-article-buffer)
9615         (gnus-narrow-to-headers)
9616         (while methods
9617           (goto-char (point-min))
9618           (and (condition-case () 
9619                    (re-search-forward (car (car methods)) nil t)
9620                  (error nil))
9621                (setq split-name (cons (nth 1 (car methods)) split-name)))
9622           (setq methods (cdr methods)))
9623         (widen)))
9624     (cond ((null split-name)
9625            (read-file-name
9626             (concat prompt " (default "
9627                     (file-name-nondirectory default-name) ") ")
9628             (file-name-directory default-name)
9629             default-name))
9630           ((= 1 (length split-name))
9631            (read-file-name
9632             (concat prompt " (default " (car split-name) ") ")
9633             gnus-article-save-directory
9634             (concat gnus-article-save-directory (car split-name))))
9635           (t
9636            (setq split-name (mapcar (lambda (el) (list el))
9637                                     (nreverse split-name)))
9638            (let ((result (completing-read 
9639                           (concat prompt " ")
9640                           split-name nil nil)))
9641              (concat gnus-article-save-directory
9642                      (if (string= result "")
9643                          (car (car split-name))
9644                        result)))))))
9645
9646 (defun gnus-summary-save-in-rmail (&optional filename)
9647   "Append this article to Rmail file.
9648 Optional argument FILENAME specifies file name.
9649 Directory to save to is default to `gnus-article-save-directory' which
9650 is initialized from the SAVEDIR environment variable."
9651   (interactive)
9652   (let ((default-name
9653           (funcall gnus-rmail-save-name gnus-newsgroup-name
9654                    gnus-current-headers gnus-newsgroup-last-rmail)))
9655     (or filename
9656         (setq filename (gnus-read-save-file-name 
9657                         "Save in rmail file:" default-name)))
9658     (gnus-make-directory (file-name-directory filename))
9659     (gnus-eval-in-buffer-window 
9660      gnus-article-buffer
9661      (save-excursion
9662        (save-restriction
9663          (widen)
9664          (gnus-output-to-rmail filename))))
9665     ;; Remember the directory name to save articles.
9666     (setq gnus-newsgroup-last-rmail filename)))
9667
9668 (defun gnus-summary-save-in-mail (&optional filename)
9669   "Append this article to Unix mail file.
9670 Optional argument FILENAME specifies file name.
9671 Directory to save to is default to `gnus-article-save-directory' which
9672 is initialized from the SAVEDIR environment variable."
9673   (interactive)
9674   (let ((default-name
9675           (funcall gnus-mail-save-name gnus-newsgroup-name
9676                    gnus-current-headers gnus-newsgroup-last-mail)))
9677     (or filename
9678         (setq filename (gnus-read-save-file-name 
9679                         "Save in Unix mail file:" default-name)))
9680     (setq filename
9681           (expand-file-name filename
9682                             (and default-name
9683                                  (file-name-directory default-name))))
9684     (gnus-make-directory (file-name-directory filename))
9685     (gnus-eval-in-buffer-window 
9686      gnus-article-buffer
9687      (save-excursion
9688        (save-restriction
9689          (widen)
9690          (if (and (file-readable-p filename) (rmail-file-p filename))
9691              (gnus-output-to-rmail filename)
9692            (rmail-output filename 1 t t)))))
9693     ;; Remember the directory name to save articles.
9694     (setq gnus-newsgroup-last-mail filename)))
9695
9696 (defun gnus-summary-save-in-file (&optional filename)
9697   "Append this article to file.
9698 Optional argument FILENAME specifies file name.
9699 Directory to save to is default to `gnus-article-save-directory' which
9700 is initialized from the SAVEDIR environment variable."
9701   (interactive)
9702   (let ((default-name
9703           (funcall gnus-file-save-name gnus-newsgroup-name
9704                    gnus-current-headers gnus-newsgroup-last-file)))
9705     (or filename
9706         (setq filename (gnus-read-save-file-name 
9707                         "Save in file:" default-name)))
9708     (gnus-make-directory (file-name-directory filename))
9709     (gnus-eval-in-buffer-window 
9710      gnus-article-buffer
9711      (save-excursion
9712        (save-restriction
9713          (widen)
9714          (gnus-output-to-file filename))))
9715     ;; Remember the directory name to save articles.
9716     (setq gnus-newsgroup-last-file filename)))
9717
9718 (defun gnus-summary-save-in-pipe (&optional command)
9719   "Pipe this article to subprocess."
9720   (interactive)
9721   (let ((command (read-string "Shell command on article: "
9722                               gnus-last-shell-command)))
9723     (if (string-equal command "")
9724         (setq command gnus-last-shell-command))
9725     (gnus-eval-in-buffer-window 
9726      gnus-article-buffer
9727      (save-restriction
9728        (widen)
9729        (shell-command-on-region (point-min) (point-max) command nil)))
9730     (setq gnus-last-shell-command command)))
9731
9732 ;; Summary extract commands
9733
9734 (defun gnus-summary-insert-pseudos (pslist)
9735   (let ((buffer-read-only nil)
9736         (article (gnus-summary-article-number))
9737         b)
9738     (or (gnus-summary-goto-subject article)
9739         (error (format "No such article: %d" article)))
9740     (or gnus-newsgroup-headers-hashtb-by-number
9741         (gnus-make-headers-hashtable-by-number))
9742     (gnus-summary-position-cursor)
9743     ;; If all commands are to be bunched up on one line, we collect
9744     ;; them here.  
9745     (if gnus-view-pseudos-separately
9746         ()
9747       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9748             files action)
9749         (while ps
9750           (setq action (cdr (assq 'action (car ps))))
9751           (setq files (list (cdr (assq 'name (car ps)))))
9752           (while (and ps (cdr ps)
9753                       (string= (or action "1")
9754                                (or (cdr (assq 'action (car (cdr ps)))) "2")))
9755             (setq files (cons (cdr (assq 'name (car (cdr ps)))) files))
9756             (setcdr ps (cdr (cdr ps))))
9757           (if (not files)
9758               ()
9759             (if (not (string-match "%s" action))
9760                 (setq files (cons " " files)))
9761             (setq files (cons " " files))
9762             (and (assq 'execute (car ps))
9763                  (setcdr (assq 'execute (car ps))
9764                          (funcall (if (string-match "%s" action)
9765                                       'format 'concat)
9766                                   action 
9767                                   (mapconcat (lambda (f) f) files " ")))))
9768           (setq ps (cdr ps)))))
9769     (if gnus-view-pseudos
9770         (while pslist
9771           (and (assq 'execute (car pslist))
9772                (gnus-execute-command (cdr (assq 'execute (car pslist)))
9773                                      (eq gnus-view-pseudos 'not-confirm)))
9774           (setq pslist (cdr pslist)))
9775       (save-excursion
9776         (while pslist
9777           (gnus-summary-goto-subject (or (cdr (assq 'article (car pslist)))
9778                                          (gnus-summary-article-number)))
9779           (forward-line 1)
9780           (setq b (point))
9781           (insert "          " (file-name-nondirectory 
9782                                 (cdr (assq 'name (car pslist))))
9783                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9784           (add-text-properties 
9785            b (1+ b) (list 'gnus-number gnus-reffed-article-number
9786                           'gnus-mark gnus-unread-mark 
9787                           'gnus-level 0
9788                           'gnus-pseudo (car pslist)))
9789           (forward-line -1)
9790           (gnus-sethash (int-to-string gnus-reffed-article-number)
9791                         (car pslist) gnus-newsgroup-headers-hashtb-by-number)
9792           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9793           (setq pslist (cdr pslist)))))))
9794
9795 (defun gnus-pseudos< (p1 p2)
9796   (let ((c1 (cdr (assq 'action p1)))
9797         (c2 (cdr (assq 'action p2))))
9798     (and c1 c2 (string< c1 c2))))
9799
9800 (defun gnus-request-pseudo-article (props)
9801   (cond ((assq 'execute props)
9802          (gnus-execute-command (cdr (assq 'execute props)))))
9803   (let ((gnus-current-article (gnus-summary-article-number)))
9804     (run-hooks 'gnus-mark-article-hook)))
9805
9806 (defun gnus-execute-command (command &optional automatic)
9807   (save-excursion
9808     (gnus-article-setup-buffer)
9809     (set-buffer gnus-article-buffer)
9810     (let ((command (if automatic command (read-string "Command: " command)))
9811           (buffer-read-only nil))
9812       (erase-buffer)
9813       (insert "$ " command "\n\n")
9814       (if gnus-view-pseudo-asynchronously
9815           (start-process "gnus-execute" nil "sh" "-c" command)
9816         (call-process "sh" nil t nil "-c" command)))))
9817
9818 (defun gnus-copy-file (file &optional to)
9819   "Copy FILE to TO."
9820   (interactive
9821    (list (read-file-name "Copy file: " default-directory)
9822          (read-file-name "Copy file to: " default-directory)))
9823   (or to (setq to (read-file-name "Copy file to: " default-directory)))
9824   (and (file-directory-p to) 
9825        (setq to (concat (file-name-as-directory to)
9826                         (file-name-nondirectory file))))
9827   (copy-file file to))
9828
9829 ;; Summary kill commands.
9830
9831 (defun gnus-summary-edit-global-kill (article)
9832   "Edit the \"global\" kill file."
9833   (interactive (list (gnus-summary-article-number)))
9834   (gnus-group-edit-global-kill article))
9835
9836 (defun gnus-summary-edit-local-kill ()
9837   "Edit a local kill file applied to the current newsgroup."
9838   (interactive)
9839   (setq gnus-current-headers 
9840         (gnus-gethash 
9841          (int-to-string (gnus-summary-article-number))
9842          gnus-newsgroup-headers-hashtb-by-number))
9843   (gnus-set-global-variables)
9844   (gnus-group-edit-local-kill 
9845    (gnus-summary-article-number) gnus-newsgroup-name))
9846
9847 \f
9848 ;;;
9849 ;;; Gnus article mode
9850 ;;;
9851
9852 (put 'gnus-article-mode 'mode-class 'special)
9853
9854 (if gnus-article-mode-map
9855     nil
9856   (setq gnus-article-mode-map (make-keymap))
9857   (suppress-keymap gnus-article-mode-map)
9858   (define-key gnus-article-mode-map " " 'gnus-article-next-page)
9859   (define-key gnus-article-mode-map "\177" 'gnus-article-prev-page)
9860   (define-key gnus-article-mode-map "\C-c^" 'gnus-article-refer-article)
9861   (define-key gnus-article-mode-map "h" 'gnus-article-show-summary)
9862   (define-key gnus-article-mode-map "s" 'gnus-article-show-summary)
9863   (define-key gnus-article-mode-map "\C-c\C-m" 'gnus-article-mail)
9864   (define-key gnus-article-mode-map "\C-c\C-M" 'gnus-article-mail-with-original)
9865   (define-key gnus-article-mode-map "?" 'gnus-article-describe-briefly)
9866   (define-key gnus-article-mode-map gnus-mouse-2 'gnus-article-push-button)
9867   (define-key gnus-article-mode-map "\r" 'gnus-article-press-button)
9868   (define-key gnus-article-mode-map "\t" 'gnus-article-next-button)
9869   
9870   ;; Duplicate almost all summary keystrokes in the article mode map.
9871   (let ((commands 
9872          (list "#" "\M-#" "\C-c\M-#" "n" "p"
9873                "N" "P" "\M-\C-n" "\M-\C-p" "." "\M-s" "\M-r"
9874                "<" ">" "l" "j" "^" "\M-^" "-" "u" "U" "d" "D"
9875                "\M-u" "\M-U" "k" "\C-k" "\M-\C-k""x" "X" 
9876                "\M-\C-x" "\M-\177" "b" "B" "$" "w" "\C-c\C-r"
9877                "t" "\M-t" "C" "S"
9878                "m" "o" "\C-o" "|" "\M-m" "\M-\C-m" "\M-k" "M"
9879                "V" "\C-c\C-d")))
9880     (while commands
9881       (define-key gnus-article-mode-map (car commands) 
9882         'gnus-article-summary-command)
9883       (setq commands (cdr commands))))
9884
9885   (let ((commands (list "q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F")))
9886     (while commands
9887       (define-key gnus-article-mode-map (car commands) 
9888         'gnus-article-summary-command-nosave)
9889       (setq commands (cdr commands)))))
9890
9891
9892 (defun gnus-article-mode ()
9893   "Major mode for displaying an article.
9894
9895 All normal editing commands are switched off.
9896
9897 The following commands are available:
9898
9899 \\<gnus-article-mode-map>
9900 \\[gnus-article-next-page]\t Scroll the article one page forwards
9901 \\[gnus-article-prev-page]\t Scroll the article one page backwards
9902 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
9903 \\[gnus-article-show-summary]\t Display the summary buffer
9904 \\[gnus-article-mail]\t Send a reply to the address near point
9905 \\[gnus-article-mail-with-original]\t Send a reply to the address near point; include the original article
9906 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
9907 \\[gnus-info-find-node]\t Go to the Gnus info node"
9908   (interactive)
9909   (if gnus-visual (gnus-article-make-menu-bar))
9910   (kill-all-local-variables)
9911   (setq mode-line-modified "-- ")
9912   (make-local-variable 'mode-line-format)
9913   (setq mode-line-format (copy-sequence mode-line-format))
9914   (and (equal (nth 3 mode-line-format) "   ")
9915        (setcar (nthcdr 3 mode-line-format) ""))
9916   (setq mode-name "Article")
9917   (setq major-mode 'gnus-article-mode)
9918   (make-local-variable 'minor-mode-alist)
9919   (or (assq 'gnus-show-mime minor-mode-alist)
9920       (setq minor-mode-alist
9921             (cons (list 'gnus-show-mime " MIME") minor-mode-alist)))
9922   (use-local-map gnus-article-mode-map)
9923   (make-local-variable 'page-delimiter)
9924   (setq page-delimiter gnus-page-delimiter)
9925   (buffer-disable-undo (current-buffer))
9926   (setq buffer-read-only t)             ;Disable modification
9927   (run-hooks 'gnus-article-mode-hook))
9928
9929 (defun gnus-article-setup-buffer ()
9930   "Initialize article mode buffer."
9931   (if (get-buffer gnus-article-buffer)
9932       (save-excursion
9933         (set-buffer gnus-article-buffer)
9934         (gnus-add-current-to-buffer-list)
9935         (or (eq major-mode 'gnus-article-mode)
9936             (gnus-article-mode)))
9937     (save-excursion
9938       (set-buffer (get-buffer-create gnus-article-buffer))
9939       (gnus-add-current-to-buffer-list)
9940       (gnus-article-mode))))
9941
9942 ;; Set article window start at LINE, where LINE is the number of lines
9943 ;; from the head of the article.
9944 (defun gnus-article-set-window-start (&optional line)
9945   (set-window-start 
9946    (get-buffer-window gnus-article-buffer)
9947    (save-excursion
9948      (set-buffer gnus-article-buffer)
9949      (goto-char (point-min))
9950      (if (not line)
9951          (point-min)
9952        (gnus-message 6 "Moved to bookmark")
9953        (search-forward "\n\n" nil t)
9954        (forward-line line)
9955        (point)))))
9956
9957 (defun gnus-request-article-this-buffer (article group)
9958   "Get an article and insert it into this buffer."
9959   (setq group (or group gnus-newsgroup-name))
9960
9961   ;; Open server if it has closed.
9962   (gnus-check-news-server (gnus-find-method-for-group group))
9963
9964   ;; Using `gnus-request-article' directly will insert the article into
9965   ;; `nntp-server-buffer' - so we'll save some time by not having to
9966   ;; copy it from the server buffer into the article buffer.
9967
9968   ;; We only request an article by message-id when we do not have the
9969   ;; headers for it, so we'll have to get those.
9970   (and (stringp article) 
9971        (let ((gnus-override-method gnus-refer-article-method))
9972          (gnus-read-header article)))
9973
9974   ;; If the article number is negative, that means that this article
9975   ;; doesn't belong in this newsgroup (possibly), so we find its
9976   ;; message-id and request it by id instead of number.
9977   (if (not (numberp article))
9978       ()
9979     (save-excursion
9980       (set-buffer gnus-summary-buffer)
9981       (let ((header (gnus-get-header-by-number article)))
9982         (if (< article 0)
9983             (if (vectorp header)
9984                 ;; It's a real article.
9985                 (setq article (header-id header))
9986               ;; It is an extracted pseudo-article.
9987               (setq article 'pseudo)
9988               (gnus-request-pseudo-article header)))
9989
9990         (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
9991           (if (not (eq (car method) 'nneething))
9992               ()
9993             (let ((dir (concat (file-name-as-directory (nth 1 method))
9994                                (header-subject header))))
9995               (if (file-directory-p dir)
9996                   (progn
9997                     (setq article 'nneething)
9998                     (gnus-group-enter-directory dir)))))))))
9999
10000   ;; Check the cache.
10001   (if (and gnus-use-cache
10002            (numberp article)
10003            (gnus-cache-request-article article group))
10004       'article
10005     ;; Get the article and into the article buffer.
10006     (if (or (stringp article) (numberp article))
10007         (progn
10008           (erase-buffer)
10009           (let ((gnus-override-method 
10010                  (and (stringp article) gnus-refer-article-method)))
10011             (and (gnus-request-article article group (current-buffer))
10012                  'article)))
10013       article)))
10014
10015 (defun gnus-read-header (id)
10016   "Read the headers of article ID and enter them into the Gnus system."
10017   (or gnus-newsgroup-headers-hashtb-by-number
10018       (gnus-make-headers-hashtable-by-number))
10019   (let (header)
10020     (if (not (setq header 
10021                    (car (if (let ((gnus-nov-is-evil t))
10022                               (gnus-retrieve-headers 
10023                                (list id) gnus-newsgroup-name))
10024                             (gnus-get-newsgroup-headers)))))
10025         nil
10026       (if (stringp id)
10027           (header-set-number header gnus-reffed-article-number))
10028       (setq gnus-newsgroup-headers (cons header gnus-newsgroup-headers))
10029       (gnus-sethash (int-to-string (header-number header)) header
10030                     gnus-newsgroup-headers-hashtb-by-number)
10031       (if (stringp id)
10032           (setq gnus-reffed-article-number (1- gnus-reffed-article-number)))
10033       (setq gnus-current-headers header)
10034       header)))
10035
10036 (defun gnus-article-prepare (article &optional all-headers header)
10037   "Prepare ARTICLE in article mode buffer.
10038 ARTICLE should either be an article number or a Message-ID.
10039 If ARTICLE is an id, HEADER should be the article headers.
10040 If ALL-HEADERS is non-nil, no headers are hidden."
10041   (save-excursion
10042     ;; Make sure we start in a summary buffer.
10043     (or (eq major-mode 'gnus-summary-mode)
10044         (set-buffer gnus-summary-buffer))
10045     (setq gnus-summary-buffer (current-buffer))
10046     ;; Make sure the connection to the server is alive.
10047     (or (gnus-server-opened (gnus-find-method-for-group gnus-newsgroup-name))
10048         (progn
10049           (gnus-check-news-server 
10050            (gnus-find-method-for-group gnus-newsgroup-name))
10051           (gnus-request-group gnus-newsgroup-name t)))
10052     (or gnus-newsgroup-headers-hashtb-by-number
10053         (gnus-make-headers-hashtable-by-number))
10054     (let* ((article (if header (header-number header) article))
10055            (summary-buffer (current-buffer))
10056            (internal-hook gnus-article-internal-prepare-hook)
10057            (group gnus-newsgroup-name)
10058            result)
10059       (save-excursion
10060         (gnus-article-setup-buffer)
10061         (set-buffer gnus-article-buffer)
10062         (if (not (setq result (let ((buffer-read-only nil))
10063                                 (gnus-request-article-this-buffer 
10064                                  article group))))
10065             ;; There is no such article.
10066             (progn
10067               (save-excursion
10068                 (set-buffer gnus-summary-buffer)
10069                 (setq gnus-current-article 0)
10070                 (and (numberp article) 
10071                      (gnus-summary-mark-as-read article gnus-canceled-mark))
10072                 (gnus-message 1 "No such article (may be canceled)")
10073                 (and (numberp article)
10074                      (setq gnus-current-article article))
10075                 (ding))
10076               (and (numberp article)
10077                    (setq gnus-article-current 
10078                          (cons gnus-newsgroup-name article)))
10079               nil)
10080           (if (or (eq result 'pseudo) (eq result 'nneething))
10081               (progn
10082                 (save-excursion
10083                   (set-buffer summary-buffer)
10084                   (setq gnus-last-article gnus-current-article
10085                         gnus-newsgroup-history (cons gnus-current-article
10086                                                      gnus-newsgroup-history)
10087                         gnus-current-article 0
10088                         gnus-current-headers nil
10089                         gnus-article-current nil)
10090                   (if (eq result 'nneething)
10091                       (gnus-configure-windows 'summary)
10092                     (gnus-configure-windows 'article))
10093                   (gnus-set-global-variables))
10094                 (gnus-set-mode-line 'article))
10095             ;; The result from the `request' was an actual article -
10096             ;; or at least some text that is now displayed in the
10097             ;; article buffer.
10098             (if (and (numberp article)
10099                      (not (eq article gnus-current-article)))
10100                 ;; Seems like a new article has been selected.
10101                 ;; `gnus-current-article' must be an article number.
10102                 (save-excursion
10103                   (set-buffer summary-buffer)
10104                   (setq gnus-last-article gnus-current-article
10105                         gnus-newsgroup-history (cons gnus-current-article
10106                                                      gnus-newsgroup-history)
10107                         gnus-current-article article
10108                         gnus-current-headers 
10109                         (gnus-get-header-by-number gnus-current-article)
10110                         gnus-article-current 
10111                         (cons gnus-newsgroup-name gnus-current-article))
10112                   (gnus-summary-show-thread)
10113                   (run-hooks 'gnus-mark-article-hook)
10114                   (gnus-set-mode-line 'summary)
10115                   (and gnus-visual 
10116                        (run-hooks 'gnus-visual-mark-article-hook))
10117                   ;; Set the global newsgroup variables here.
10118                   ;; Suggested by Jim Sisolak
10119                   ;; <sisolak@trans4.neep.wisc.edu>.
10120                   (gnus-set-global-variables)
10121                   (and gnus-use-cache 
10122                        (gnus-cache-possibly-enter-article
10123                         group article
10124                         (gnus-get-header-by-number article)
10125                         (memq article gnus-newsgroup-marked)
10126                         (memq article gnus-newsgroup-dormant)
10127                         (memq article gnus-newsgroup-unreads)))))
10128             ;; gnus-have-all-headers must be either T or NIL.
10129             (setq gnus-have-all-headers
10130                   (not (not (or all-headers gnus-show-all-headers))))
10131             ;; Hooks for getting information from the article.
10132             ;; This hook must be called before being narrowed.
10133             (let (buffer-read-only)
10134               (run-hooks 'internal-hook)
10135               (run-hooks 'gnus-article-prepare-hook)
10136               ;; Decode MIME message.
10137               (if (and gnus-show-mime
10138                        (or (not gnus-strict-mime)
10139                            (gnus-fetch-field "Mime-Version")))
10140                   (funcall gnus-show-mime-method))
10141               ;; Perform the article display hooks.
10142               (run-hooks 'gnus-article-display-hook))
10143             ;; Do page break.
10144             (goto-char (point-min))
10145             (and gnus-break-pages (gnus-narrow-to-page))
10146             (gnus-set-mode-line 'article)
10147             (gnus-configure-windows 'article)
10148             (goto-char (point-min))
10149             t))))))
10150
10151 (defun gnus-article-show-all-headers ()
10152   "Show all article headers in article mode buffer."
10153   (save-excursion 
10154     (gnus-article-setup-buffer)
10155     (set-buffer gnus-article-buffer)
10156     (let ((buffer-read-only nil))
10157       (remove-text-properties (point-min) (point-max) 
10158                               gnus-hidden-properties))))
10159
10160 (defun gnus-article-hide-headers-if-wanted ()
10161   "Hide unwanted headers if `gnus-have-all-headers' is nil.
10162 Provided for backwards compatability."
10163   (or gnus-have-all-headers
10164       (gnus-article-hide-headers)))
10165
10166 (defun gnus-article-hide-headers (&optional delete)
10167   "Hide unwanted headers and possibly sort them as well."
10168   (interactive "P")
10169   (save-excursion
10170     (set-buffer gnus-article-buffer)
10171     (save-restriction
10172       (let ((sorted gnus-sorted-header-list)
10173             (buffer-read-only nil)
10174             want want-list beg want-l)
10175         ;; First we narrow to just the headers.
10176         (widen)
10177         (goto-char (point-min))
10178         ;; Hide any "From " lines at the beginning of (mail) articles. 
10179         (while (looking-at "From ")
10180           (forward-line 1))
10181         (if (bobp) 
10182             (add-text-properties (point-min) (point) gnus-hidden-properties))
10183         ;; Then treat the rest of the header lines.
10184         (narrow-to-region 
10185          (point) 
10186          (progn (search-forward "\n\n" nil t) (forward-line -1) (point)))
10187         ;; Then we use the two regular expressions
10188         ;; `gnus-ignored-headers' and `gnus-visible-headers' to
10189         ;; select which header lines is to remain visible in the
10190         ;; article buffer.
10191         (goto-char (point-min))
10192         (while (re-search-forward "^[^ \t]*:" nil t)
10193           (beginning-of-line)
10194           ;; We add the headers we want to keep to a list and delete
10195           ;; them from the buffer.
10196           (if (or (and (stringp gnus-visible-headers)
10197                        (looking-at gnus-visible-headers))
10198                   (and (not (stringp gnus-visible-headers))
10199                        (stringp gnus-ignored-headers)
10200                        (not (looking-at gnus-ignored-headers))))
10201               (progn
10202                 (setq beg (point))
10203                 (forward-line 1)
10204                 ;; Be sure to get multi-line headers...
10205                 (re-search-forward "^[^ \t]*:" nil t)
10206                 (beginning-of-line)
10207                 (setq want-list 
10208                       (cons (buffer-substring beg (point)) want-list))
10209                 (delete-region beg (point))
10210                 (goto-char beg))
10211             (forward-line 1)))
10212         ;; Next we perform the sorting by looking at
10213         ;; `gnus-sorted-header-list'. 
10214         (goto-char (point-min))
10215         (while (and sorted want-list)
10216           (setq want-l want-list)
10217           (while (and want-l
10218                       (not (string-match (car sorted) (car want-l))))
10219             (setq want-l (cdr want-l)))
10220           (if want-l 
10221               (progn
10222                 (insert (car want-l))
10223                 (setq want-list (delq (car want-l) want-list))))
10224           (setq sorted (cdr sorted)))
10225         ;; Any headers that were not matched by the sorted list we
10226         ;; just tack on the end of the visible header list.
10227         (while want-list
10228           (insert (car want-list))
10229           (setq want-list (cdr want-list)))
10230         ;; And finally we make the unwanted headers invisible.
10231         (if delete
10232             (delete-region (point) (point-max))
10233           ;; Suggested by Sudish Joseph <joseph@cis.ohio-state.edu>.
10234           (add-text-properties (point) (point-max) gnus-hidden-properties))))))
10235
10236 ;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.
10237 (defun gnus-article-treat-overstrike ()
10238   "Translate overstrikes into bold text."
10239   (interactive)
10240   (save-excursion
10241     (set-buffer gnus-article-buffer)
10242     (let ((buffer-read-only nil))
10243       (while (search-forward "\b" nil t)
10244         (let ((next (following-char))
10245               (previous (char-after (- (point) 2))))
10246           (cond ((eq next previous)
10247                  (delete-region (- (point) 2) (point))
10248                  (put-text-property (point) (1+ (point))
10249                                     'face 'bold))
10250                 ((eq next ?_)
10251                  (delete-region (1- (point)) (1+ (point)))
10252                  (put-text-property (1- (point)) (point)
10253                                     'face 'underline))
10254                 ((eq previous ?_)
10255                  (delete-region (- (point) 2) (point))
10256                  (put-text-property (point) (1+ (point))
10257                                     'face 'underline))))))))
10258
10259 (defun gnus-article-word-wrap ()
10260   "Format too long lines."
10261   (interactive)
10262   (save-excursion
10263     (set-buffer gnus-article-buffer)
10264     (let ((buffer-read-only nil))
10265       (goto-char (point-min))
10266       (search-forward "\n\n" nil t)
10267       (end-of-line 1)
10268       (let ((paragraph-start "^\\W"))
10269         (while (not (eobp))
10270           (and (>= (current-column) (min fill-column (window-width)))
10271                (/= (preceding-char) ?:)
10272                (fill-paragraph nil))
10273           (end-of-line 2))))))
10274
10275 (defun gnus-article-remove-cr ()
10276   "Remove carriage returns from an article."
10277   (interactive)
10278   (save-excursion
10279     (set-buffer gnus-article-buffer)
10280     (let ((buffer-read-only nil))
10281       (goto-char (point-min))
10282       (while (search-forward "\r" nil t)
10283         (replace-match "" t t)))))
10284
10285 (defun gnus-article-display-x-face (&optional force)
10286   "Look for an X-Face header and display it if present."
10287   (interactive (list 'force))
10288   (save-excursion
10289     (set-buffer gnus-article-buffer)
10290     (let ((inhibit-point-motion-hooks t)
10291           (case-fold-search nil))
10292       (save-restriction
10293         (goto-char (point-min))
10294         (search-forward "\n\n")
10295         (narrow-to-region (point-min) (point))
10296         (goto-char (point-min))
10297         (if (or (not gnus-article-x-face-command)
10298                 (and (not force)
10299                      (or (not gnus-article-x-face-too-ugly)
10300                          (string-match gnus-article-x-face-too-ugly
10301                                        (mail-fetch-field "from"))))
10302                 (progn
10303                   (goto-char (point-min))
10304                   (not (re-search-forward "^X-Face: " nil t))))
10305             nil
10306           (let ((beg (point))
10307                 (end (1- (re-search-forward "^\\($\\|[^ \t]\\)" nil t))))
10308             (if (symbolp gnus-article-x-face-command)
10309                 (and (or (fboundp gnus-article-x-face-command)
10310                          (error "%s is not a function"
10311                                 gnus-article-x-face-command))
10312                      (funcall gnus-article-x-face-command beg end))
10313               (call-process-region beg end "sh" nil 0 nil
10314                                    "-c" gnus-article-x-face-command))))))))
10315
10316 (defun gnus-article-de-quoted-unreadable (&optional force)
10317   "Do a naïve translation of a quoted-printable-encoded article.
10318 This is in no way, shape or form meant as a replacement for real MIME
10319 processing, but is simply a stop-gap measure until MIME support is
10320 written.
10321 If FORCE, decode the article whether it is marked as quoted-printable
10322 or not." 
10323   (interactive (list 'force))
10324   (save-excursion
10325     (set-buffer gnus-article-buffer)
10326     (let ((case-fold-search t)
10327           (buffer-read-only nil)
10328           (type (gnus-fetch-field "content-transfer-encoding")))
10329       (if (or force (and type (string-match "quoted-printable" type)))
10330           (progn
10331             (goto-char (point-min))
10332             (search-forward "\n\n" nil 'move)
10333             (gnus-mime-decode-quoted-printable (point) (point-max)))))))
10334
10335 (defun gnus-mime-decode-quoted-printable (from to)
10336   ;; Decode quoted-printable from region between FROM and TO.
10337   (save-excursion
10338     (goto-char from)
10339     (while (search-forward "=" to t)
10340       (cond ((eq (following-char) ?\n)
10341              (delete-char -1)
10342              (delete-char 1))
10343             ((looking-at "[0-9A-F][0-9A-F]")
10344              (delete-char -1)
10345              (insert (hexl-hex-string-to-integer
10346                       (buffer-substring (point) (+ 2 (point)))))
10347              (delete-char 2))
10348             ((gnus-message 3 "Malformed MIME quoted-printable message"))))))
10349
10350 (defvar gnus-article-time-units
10351   (list (cons 'year (* 365.25 24 60 60))
10352         (cons 'week (* 7 24 60 60))
10353         (cons 'day (* 24 60 60))
10354         (cons 'hour (* 60 60))
10355         (cons 'minute 60)
10356         (cons 'second 1)))
10357
10358 (defun gnus-article-date-ut (&optional type)
10359   "Convert DATE date to universal time in the current article.
10360 If TYPE is `local', convert to local time; if it is `lapsed', output
10361 how much time has lapsed since DATE."
10362   (interactive (list 'ut))
10363   (let ((date (header-date (or gnus-current-headers 
10364                                (gnus-get-header-by-number
10365                                 (gnus-summary-article-number))"")))
10366         (date-regexp "^Date: \\|^X-Sent: "))
10367     (if (or (not date)
10368             (string= date ""))
10369         ()
10370       (save-excursion
10371         (set-buffer gnus-article-buffer)
10372         (let ((buffer-read-only nil))
10373           (goto-char (point-min))
10374           (if (and (re-search-forward date-regexp nil t)
10375                    (progn 
10376                      (beginning-of-line)
10377                      (looking-at date-regexp)))
10378               (delete-region (gnus-point-at-bol)
10379                              (progn (end-of-line) (1+ (point))))
10380             (goto-char (point-min))
10381             (goto-char (- (search-forward "\n\n") 2)))
10382           (insert
10383            (cond 
10384             ((eq type 'local)
10385              (concat "Date: " (timezone-make-date-arpa-standard date) "\n"))
10386             ((eq type 'ut)
10387              (concat "Date: " (timezone-make-date-arpa-standard date nil "UT")
10388                      "\n"))
10389             ((eq type 'lapsed)
10390              (let* ((sec (max (- (gnus-seconds-since-epoch 
10391                                   (timezone-make-date-arpa-standard
10392                                    (current-time-string) 
10393                                    (current-time-zone) "UT"))
10394                                  (gnus-seconds-since-epoch 
10395                                   (timezone-make-date-arpa-standard 
10396                                    date nil "UT")))
10397                               0))
10398                     num prev)
10399                (concat
10400                 "X-Sent: "
10401                 (mapconcat 
10402                  (lambda (unit)
10403                    (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
10404                        ""
10405                      (setq sec (- sec (* num (cdr unit))))
10406                      (prog1
10407                          (concat (if prev ", " "") (int-to-string (floor num))
10408                                  " " (symbol-name (car unit))
10409                                  (if (> num 1) "s" ""))
10410                        (setq prev t))))
10411                  gnus-article-time-units "")
10412                 " ago\n")))
10413             (t
10414              (error "Unknown conversion type: %s" type)))))))))
10415
10416 (defun gnus-article-date-local ()
10417   "Convert the current article date to the local timezone."
10418   (interactive)
10419   (gnus-article-date-ut 'local))
10420
10421 (defun gnus-article-date-lapsed ()
10422   "Convert the current article date to time lapsed since it was sent."
10423   (interactive)
10424   (gnus-article-date-ut 'lapsed))
10425
10426 (defun gnus-article-maybe-highlight ()
10427   (if gnus-visual (gnus-article-highlight)))
10428
10429 ;; Article savers.
10430
10431 (defun gnus-output-to-rmail (file-name)
10432   "Append the current article to an Rmail file named FILE-NAME."
10433   (require 'rmail)
10434   ;; Most of these codes are borrowed from rmailout.el.
10435   (setq file-name (expand-file-name file-name))
10436   (setq rmail-default-rmail-file file-name)
10437   (let ((artbuf (current-buffer))
10438         (tmpbuf (get-buffer-create " *Gnus-output*")))
10439     (save-excursion
10440       (or (get-file-buffer file-name)
10441           (file-exists-p file-name)
10442           (if (gnus-yes-or-no-p
10443                (concat "\"" file-name "\" does not exist, create it? "))
10444               (let ((file-buffer (create-file-buffer file-name)))
10445                 (save-excursion
10446                   (set-buffer file-buffer)
10447                   (rmail-insert-rmail-file-header)
10448                   (let ((require-final-newline nil))
10449                     (write-region (point-min) (point-max) file-name t 1)))
10450                 (kill-buffer file-buffer))
10451             (error "Output file does not exist")))
10452       (set-buffer tmpbuf)
10453       (buffer-disable-undo (current-buffer))
10454       (erase-buffer)
10455       (insert-buffer-substring artbuf)
10456       (gnus-convert-article-to-rmail)
10457       ;; Decide whether to append to a file or to an Emacs buffer.
10458       (let ((outbuf (get-file-buffer file-name)))
10459         (if (not outbuf)
10460             (append-to-file (point-min) (point-max) file-name)
10461           ;; File has been visited, in buffer OUTBUF.
10462           (set-buffer outbuf)
10463           (let ((buffer-read-only nil)
10464                 (msg (and (boundp 'rmail-current-message)
10465                           (symbol-value 'rmail-current-message))))
10466             ;; If MSG is non-nil, buffer is in RMAIL mode.
10467             (if msg
10468                 (progn (widen)
10469                        (narrow-to-region (point-max) (point-max))))
10470             (insert-buffer-substring tmpbuf)
10471             (if msg
10472                 (progn
10473                   (goto-char (point-min))
10474                   (widen)
10475                   (search-backward "\^_")
10476                   (narrow-to-region (point) (point-max))
10477                   (goto-char (1+ (point-min)))
10478                   (rmail-count-new-messages t)
10479                   (rmail-show-message msg)))))))
10480     (kill-buffer tmpbuf)))
10481
10482 (defun gnus-output-to-file (file-name)
10483   "Append the current article to a file named FILE-NAME."
10484   (setq file-name (expand-file-name file-name))
10485   (let ((artbuf (current-buffer))
10486         (tmpbuf (get-buffer-create " *Gnus-output*")))
10487     (save-excursion
10488       (set-buffer tmpbuf)
10489       (buffer-disable-undo (current-buffer))
10490       (erase-buffer)
10491       (insert-buffer-substring artbuf)
10492       ;; Append newline at end of the buffer as separator, and then
10493       ;; save it to file.
10494       (goto-char (point-max))
10495       (insert "\n")
10496       (append-to-file (point-min) (point-max) file-name))
10497     (kill-buffer tmpbuf)))
10498
10499 (defun gnus-convert-article-to-rmail ()
10500   "Convert article in current buffer to Rmail message format."
10501   (let ((buffer-read-only nil))
10502     ;; Convert article directly into Babyl format.
10503     ;; Suggested by Rob Austein <sra@lcs.mit.edu>
10504     (goto-char (point-min))
10505     (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
10506     (while (search-forward "\n\^_" nil t) ;single char
10507       (replace-match "\n^_" t t))               ;2 chars: "^" and "_"
10508     (goto-char (point-max))
10509     (insert "\^_")))
10510
10511 (defun gnus-narrow-to-page (&optional arg)
10512   "Make text outside current page invisible except for page delimiter.
10513 A numeric arg specifies to move forward or backward by that many pages,
10514 thus showing a page other than the one point was originally in."
10515   (interactive "P")
10516   (setq arg (if arg (prefix-numeric-value arg) 0))
10517   (save-excursion
10518     (forward-page -1)                   ;Beginning of current page.
10519     (widen)
10520     (if (> arg 0)
10521         (forward-page arg)
10522       (if (< arg 0)
10523           (forward-page (1- arg))))
10524     ;; Find the end of the page.
10525     (forward-page)
10526     ;; If we stopped due to end of buffer, stay there.
10527     ;; If we stopped after a page delimiter, put end of restriction
10528     ;; at the beginning of that line.
10529     ;; These are commented out.
10530     ;;    (if (save-excursion (beginning-of-line)
10531     ;;                  (looking-at page-delimiter))
10532     ;;  (beginning-of-line))
10533     (narrow-to-region (point)
10534                       (progn
10535                         ;; Find the top of the page.
10536                         (forward-page -1)
10537                         ;; If we found beginning of buffer, stay there.
10538                         ;; If extra text follows page delimiter on same line,
10539                         ;; include it.
10540                         ;; Otherwise, show text starting with following line.
10541                         (if (and (eolp) (not (bobp)))
10542                             (forward-line 1))
10543                         (point)))))
10544
10545 (defun gnus-gmt-to-local ()
10546   "Rewrite Date header described in GMT to local in current buffer.
10547 Intended to be used with gnus-article-prepare-hook."
10548   (save-excursion
10549     (save-restriction
10550       (widen)
10551       (goto-char (point-min))
10552       (narrow-to-region (point-min)
10553                         (progn (search-forward "\n\n" nil 'move) (point)))
10554       (goto-char (point-min))
10555       (if (re-search-forward "^Date:[ \t]\\(.*\\)$" nil t)
10556           (let ((buffer-read-only nil)
10557                 (date (buffer-substring-no-properties
10558                        (match-beginning 1) (match-end 1))))
10559             (delete-region (match-beginning 1) (match-end 1))
10560             (insert
10561              (timezone-make-date-arpa-standard 
10562               date nil (current-time-zone))))))))
10563
10564
10565 ;; Article mode commands
10566
10567 (defun gnus-article-next-page (lines)
10568   "Show next page of current article.
10569 If end of article, return non-nil. Otherwise return nil.
10570 Argument LINES specifies lines to be scrolled up."
10571   (interactive "P")
10572   (move-to-window-line -1)
10573   ;; Fixed by enami@ptgd.sony.co.jp (enami tsugutomo)
10574   (if (save-excursion
10575         (end-of-line)
10576         (and (pos-visible-in-window-p)  ;Not continuation line.
10577              (eobp)))
10578       ;; Nothing in this page.
10579       (if (or (not gnus-break-pages)
10580               (save-excursion
10581                 (save-restriction
10582                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
10583           t                             ;Nothing more.
10584         (gnus-narrow-to-page 1)         ;Go to next page.
10585         nil)
10586     ;; More in this page.
10587     (condition-case ()
10588         (scroll-up lines)
10589       (end-of-buffer
10590        ;; Long lines may cause an end-of-buffer error.
10591        (goto-char (point-max))))
10592     nil))
10593
10594 (defun gnus-article-prev-page (lines)
10595   "Show previous page of current article.
10596 Argument LINES specifies lines to be scrolled down."
10597   (interactive "P")
10598   (move-to-window-line 0)
10599   (if (and gnus-break-pages
10600            (bobp)
10601            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
10602       (progn
10603         (gnus-narrow-to-page -1) ;Go to previous page.
10604         (goto-char (point-max))
10605         (recenter -1))
10606     (scroll-down lines)))
10607
10608 (defun gnus-article-refer-article ()
10609   "Read article specified by message-id around point."
10610   (interactive)
10611   (search-forward ">" nil t)    ;Move point to end of "<....>".
10612   (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
10613       (let ((message-id
10614              (buffer-substring (match-beginning 1) (match-end 1))))
10615         (set-buffer gnus-summary-buffer)
10616         (gnus-summary-refer-article message-id))
10617     (error "No references around point")))
10618
10619 (defun gnus-article-show-summary ()
10620   "Reconfigure windows to show summary buffer."
10621   (interactive)
10622   (gnus-configure-windows 'article)
10623   (gnus-summary-goto-subject gnus-current-article))
10624
10625 (defun gnus-article-describe-briefly ()
10626   "Describe article mode commands briefly."
10627   (interactive)
10628   (gnus-message 6
10629    (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")))
10630
10631 (defun gnus-article-summary-command ()
10632   "Execute the last keystroke in the summary buffer."
10633   (interactive)
10634   (let ((obuf (current-buffer))
10635         (owin (current-window-configuration))
10636         func)
10637     (switch-to-buffer gnus-summary-buffer 'norecord)
10638     (setq func (lookup-key (current-local-map) (this-command-keys)))
10639     (call-interactively func)
10640     (set-buffer obuf)
10641     (let ((npoint (point)))
10642       (set-window-configuration owin)
10643       (set-window-start (get-buffer-window (current-buffer)) (point)))))
10644
10645 (defun gnus-article-summary-command-nosave ()
10646   "Execute the last keystroke in the summary buffer."
10647   (interactive)
10648   (let ((obuf (current-buffer))
10649         (owin (current-window-configuration))
10650         func)
10651     (switch-to-buffer gnus-summary-buffer 'norecord)
10652     (setq func (lookup-key (current-local-map) (this-command-keys)))
10653     (call-interactively func)))
10654
10655 ;; caesar-region written by phr@prep.ai.mit.edu  Nov 86
10656 ;; Modified by tower@prep Nov 86
10657 ;; Modified by umerin@flab.flab.Fujitsu.JUNET for ROT47.
10658
10659 (defun gnus-caesar-region (&optional n)
10660   "Caesar rotation of region by N, default 13, for decrypting netnews.
10661 ROT47 will be performed for Japanese text in any case."
10662   (interactive (if current-prefix-arg   ; Was there a prefix arg?
10663                    (list (prefix-numeric-value current-prefix-arg))
10664                  (list nil)))
10665   (cond ((not (numberp n)) (setq n 13))
10666         (t (setq n (mod n 26))))        ;canonicalize N
10667   (if (not (zerop n))           ; no action needed for a rot of 0
10668       (progn
10669         (if (or (not (boundp 'caesar-translate-table))
10670                 (not caesar-translate-table)
10671                 (/= (aref caesar-translate-table ?a) (+ ?a n)))
10672             (let ((i 0) 
10673                   (lower "abcdefghijklmnopqrstuvwxyz")
10674                   upper)
10675               (gnus-message 9 "Building caesar-translate-table...")
10676               (setq caesar-translate-table (make-vector 256 0))
10677               (while (< i 256)
10678                 (aset caesar-translate-table i i)
10679                 (setq i (1+ i)))
10680               (setq lower (concat lower lower)
10681                     upper (upcase lower)
10682                     i 0)
10683               (while (< i 26)
10684                 (aset caesar-translate-table (+ ?a i) (aref lower (+ i n)))
10685                 (aset caesar-translate-table (+ ?A i) (aref upper (+ i n)))
10686                 (setq i (1+ i)))
10687               ;; ROT47 for Japanese text.
10688               ;; Thanks to ichikawa@flab.fujitsu.junet.
10689               (setq i 161)
10690               (let ((t1 (logior ?O 128))
10691                     (t2 (logior ?! 128))
10692                     (t3 (logior ?~ 128)))
10693                 (while (< i 256)
10694                   (aset caesar-translate-table i
10695                         (let ((v (aref caesar-translate-table i)))
10696                           (if (<= v t1) (if (< v t2) v (+ v 47))
10697                             (if (<= v t3) (- v 47) v))))
10698                   (setq i (1+ i))))
10699               (gnus-message 9 "Building caesar-translate-table...done")))
10700         (let ((from (region-beginning))
10701               (to (region-end))
10702               (i 0) str len)
10703           (setq str (buffer-substring from to))
10704           (setq len (length str))
10705           (while (< i len)
10706             (aset str i (aref caesar-translate-table (aref str i)))
10707             (setq i (1+ i)))
10708           (goto-char from)
10709           (delete-region from to)
10710           (insert str)))))
10711
10712 \f
10713 ;; Basic ideas by emv@math.lsa.umich.edu (Edward Vielmetti)
10714
10715 ;;;###autoload
10716 (defalias 'gnus-batch-kill 'gnus-batch-score)
10717 ;;;###autoload
10718 (defun gnus-batch-score ()
10719   "Run batched scoring.
10720 Usage: emacs -batch -l gnus -f gnus-batch-score <newsgroups> ...
10721 Newsgroups is a list of strings in Bnews format.  If you want to score
10722 the comp hierarchy, you'd say \"comp.all\". If you would not like to
10723 score the alt hierarchy, you'd say \"!alt.all\"."
10724   (interactive)
10725   (let* ((yes-and-no
10726           (gnus-newsrc-parse-options
10727            (apply (function concat)
10728                   (mapcar (lambda (g) (concat g " "))
10729                           command-line-args-left))))
10730          (gnus-expert-user t)
10731          (nnmail-spool-file nil)
10732          (gnus-use-dribble-file nil)
10733          (yes (car yes-and-no))
10734          (no (cdr yes-and-no))
10735          group subscribed newsrc entry
10736          ;; Disable verbose message.
10737          gnus-novice-user gnus-large-newsgroup)
10738     ;; Eat all arguments.
10739     (setq command-line-args-left nil)
10740     ;; Start Gnus.
10741     (gnus)
10742     ;; Apply kills to specified newsgroups in command line arguments.
10743     (setq newsrc (cdr gnus-newsrc-alist))
10744     (while newsrc
10745       (setq group (car (car newsrc)))
10746       (setq entry (gnus-gethash group gnus-newsrc-hashtb))
10747       (if (and (<= (nth 1 (car newsrc)) gnus-level-subscribed)
10748                (and (car entry)
10749                     (or (eq (car entry) t)
10750                         (not (zerop (car entry)))))
10751                (if yes (string-match yes group) t)
10752                (or (null no) (not (string-match no group))))
10753           (progn
10754             (gnus-summary-read-group group nil t)
10755             (and (eq (current-buffer) (get-buffer gnus-summary-buffer))
10756                  (gnus-summary-exit))))
10757       (setq newsrc (cdr newsrc)))
10758     ;; Exit Emacs.
10759     (switch-to-buffer gnus-group-buffer)
10760     (gnus-group-save-newsrc)))
10761
10762 (defun gnus-apply-kill-file ()
10763   "Apply a kill file to the current newsgroup.
10764 Returns the number of articles marked as read."
10765   (if (or (file-exists-p (gnus-newsgroup-kill-file nil))
10766           (file-exists-p (gnus-newsgroup-kill-file gnus-newsgroup-name)))
10767       (gnus-apply-kill-file-internal)
10768     0))
10769
10770 (defun gnus-kill-save-kill-buffer ()
10771   (save-excursion
10772     (let ((file (gnus-newsgroup-kill-file gnus-newsgroup-name)))
10773       (if (get-file-buffer file)
10774           (progn
10775             (set-buffer (get-file-buffer file))
10776             (and (buffer-modified-p) (save-buffer))
10777             (kill-buffer (current-buffer)))))))
10778
10779 (defvar gnus-kill-file-name "KILL"
10780   "Suffix of the kill files.")
10781
10782 (defun gnus-newsgroup-kill-file (newsgroup)
10783   "Return the name of a kill file name for NEWSGROUP.
10784 If NEWSGROUP is nil, return the global kill file name instead."
10785   (cond ((or (null newsgroup)
10786              (string-equal newsgroup ""))
10787          ;; The global KILL file is placed at top of the directory.
10788          (expand-file-name gnus-kill-file-name
10789                            (or gnus-kill-files-directory "~/News")))
10790         ((gnus-use-long-file-name 'not-kill)
10791          ;; Append ".KILL" to newsgroup name.
10792          (expand-file-name (concat newsgroup "." gnus-kill-file-name)
10793                            (or gnus-kill-files-directory "~/News")))
10794         (t
10795          ;; Place "KILL" under the hierarchical directory.
10796          (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
10797                                    "/" gnus-kill-file-name)
10798                            (or gnus-kill-files-directory "~/News")))))
10799
10800 \f
10801 ;;;
10802 ;;; Dribble file
10803 ;;;
10804
10805 (defvar gnus-dribble-ignore nil)
10806
10807 (defun gnus-dribble-file-name ()
10808   (concat gnus-startup-file "-dribble"))
10809
10810 (defun gnus-dribble-open ()
10811   (save-excursion 
10812     (set-buffer 
10813      (setq gnus-dribble-buffer (find-file-noselect (gnus-dribble-file-name))))
10814     (buffer-disable-undo (current-buffer))
10815     (bury-buffer gnus-dribble-buffer)
10816     (auto-save-mode t)
10817     (goto-char (point-max))))
10818
10819 (defun gnus-dribble-enter (string)
10820   (if (and (not gnus-dribble-ignore)
10821            gnus-dribble-buffer
10822            (buffer-name gnus-dribble-buffer))
10823       (let ((obuf (current-buffer)))
10824         (set-buffer gnus-dribble-buffer)
10825         (insert string "\n")
10826         (set-window-point (get-buffer-window (current-buffer)) (point-max))
10827         (set-buffer obuf))))
10828
10829 (defun gnus-dribble-read-file ()
10830   (let ((dribble-file (gnus-dribble-file-name)))
10831     (save-excursion 
10832       (set-buffer (setq gnus-dribble-buffer 
10833                         (get-buffer-create 
10834                          (file-name-nondirectory dribble-file))))
10835       (gnus-add-current-to-buffer-list)
10836       (erase-buffer)
10837       (set-visited-file-name dribble-file)
10838       (buffer-disable-undo (current-buffer))
10839       (bury-buffer (current-buffer))
10840       (set-buffer-modified-p nil)
10841       (let ((auto (make-auto-save-file-name))
10842             (gnus-dribble-ignore t))
10843         (if (or (file-exists-p auto) (file-exists-p dribble-file))
10844             (progn
10845               (if (file-newer-than-file-p auto dribble-file)
10846                   (setq dribble-file auto))
10847               (insert-file-contents dribble-file)
10848               (if (not (zerop (buffer-size)))
10849                   (set-buffer-modified-p t))
10850               (if (gnus-y-or-n-p 
10851                    "Auto-save file exists. Do you want to read it? ")
10852                   (progn
10853                     (gnus-message 5 "Reading %s..." dribble-file) 
10854                     (eval-current-buffer)
10855                     (gnus-message 5 "Reading %s...done" dribble-file)))))))))
10856
10857 (defun gnus-dribble-delete-file ()
10858   (if (file-exists-p (gnus-dribble-file-name))
10859       (delete-file (gnus-dribble-file-name)))
10860   (if gnus-dribble-buffer
10861       (save-excursion
10862         (set-buffer gnus-dribble-buffer)
10863         (let ((auto (make-auto-save-file-name)))
10864           (if (file-exists-p auto)
10865               (delete-file auto))
10866           (erase-buffer)
10867           (set-buffer-modified-p nil)))))
10868
10869 (defun gnus-dribble-save ()
10870   (if (and gnus-dribble-buffer
10871            (buffer-name gnus-dribble-buffer))
10872       (save-excursion
10873         (set-buffer gnus-dribble-buffer)
10874         (save-buffer))))
10875
10876 (defun gnus-dribble-clear ()
10877   (save-excursion
10878     (if (gnus-buffer-exists-p gnus-dribble-buffer)
10879         (progn
10880           (set-buffer gnus-dribble-buffer)
10881           (erase-buffer)
10882           (set-buffer-modified-p nil)
10883           (setq buffer-saved-size (buffer-size))))))
10884
10885 ;;;
10886 ;;; Server Communication
10887 ;;;
10888
10889 ;; All the Gnus backends have the same interface, and should return
10890 ;; data in a similar format. Below is an overview of what functions
10891 ;; these packages must supply and what results they should return.
10892 ;;
10893 ;; Variables:
10894 ;;
10895 ;; `nntp-server-buffer' - All data should be returned to Gnus in this
10896 ;; buffer. 
10897 ;;
10898 ;; Functions for the imaginary backend `choke':
10899 ;;
10900 ;; `choke-retrieve-headers ARTICLES &optional GROUP SERVER'
10901 ;; Should return all headers for all ARTICLES, or return NOV lines for
10902 ;; the same.
10903 ;;
10904 ;; `choke-request-group GROUP &optional SERVER DISCARD'
10905 ;; Switch to GROUP. If DISCARD is nil, active information on the group
10906 ;; must be returned.
10907 ;;
10908 ;; `choke-close-group GROUP &optional SERVER'
10909 ;; Close group. Most backends won't have to do anything with this
10910 ;; call, but it is an opportunity to clean up, if that is needed. It
10911 ;; is called when Gnus exits a group.
10912 ;;
10913 ;; `choke-request-article ARTICLE &optional GROUP SERVER'
10914 ;; Return ARTICLE, which is either an article number or
10915 ;; message-id. Note that not all backends can return articles based on
10916 ;; message-id. 
10917 ;;
10918 ;; `choke-request-list SERVER'
10919 ;; Return a list of all newsgroups on SERVER.
10920 ;;
10921 ;; `choke-request-list-newsgroups SERVER'
10922 ;; Return a list of descriptions of all newsgroups on SERVER.
10923 ;;
10924 ;; `choke-request-newgroups DATE &optional SERVER'
10925 ;; Return a list of all groups that have arrived after DATE on
10926 ;; SERVER. Note that the date doesn't have to be respected - Gnus will
10927 ;; always check whether the groups are old or not. Backends that do
10928 ;; not store date information may just return the entire list of
10929 ;; groups, although this might not be a good idea in general.
10930 ;;
10931 ;; `choke-request-post-buffer METHOD HEADER ARTICLE-BUFFER GROUP INFO'
10932 ;; Should return a buffer that is suitable for "posting". nnspool and
10933 ;; nntp return a `*post-buffer*', and nnmail return a `*mail*'
10934 ;; buffer. This function should fill out the appropriate headers. 
10935 ;;
10936 ;; `choke-request-post &optional SERVER'
10937 ;; Function that will be called from a buffer to be posted. 
10938 ;;
10939 ;; `choke-open-server SERVER &optional ARGUMENT'
10940 ;; Open a connection to SERVER.
10941 ;;
10942 ;; `choke-close-server &optional SERVER'
10943 ;; Close the connection to SERVER.
10944 ;;
10945 ;; `choke-server-opened &optional SERVER'
10946 ;; Whether the conenction to SERVER is opened or not.
10947 ;;
10948 ;; `choke-server-status &optional SERVER'
10949 ;; Should return a status string (not in the nntp buffer, but as the
10950 ;; result of the function).
10951 ;;
10952 ;; `choke-retrieve-groups GROUPS &optional SERVER'
10953 ;; Optional function for retrieving active file info on all groups in
10954 ;; GROUPS.  Two return formats are supported: The normal active file
10955 ;; format, and a list of GROUP lines.  This function should return (as
10956 ;; a function value) either `active' or `group', depending on what
10957 ;; format it returns.
10958 ;;
10959 ;; The following functions are optional and apply only to backends
10960 ;; that are able to control the contents of their groups totally
10961 ;; (ie. mail backends.)  Backends that aren't able to do that
10962 ;; shouldn't define these functions at all. Gnus will check for their
10963 ;; presence before attempting to call them.
10964 ;;
10965 ;; `choke-request-expire-articles ARTICLES &optional NEWSGROUP SERVER'
10966 ;; Should expire (according to some aging scheme) all ARTICLES. Most
10967 ;; backends will not be able to expire articles. Should return a list
10968 ;; of all articles that were not expired.
10969 ;;
10970 ;; `choke-request-move-article ARTICLE GROUP SERVER ACCEPT-FORM &optional LAST'
10971 ;; Should move ARTICLE from GROUP on SERVER by using ACCEPT-FORM.
10972 ;; Removes any information it has added to the article (extra headers,
10973 ;; whatever - make it as clean as possible), and then passes the
10974 ;; article on by evaling ACCEPT-FORM, which is normally a call to the
10975 ;; function described below. If the ACCEPT-FORM returns a non-nil
10976 ;; value, the article should then be deleted. If LAST is nil, that
10977 ;; means that there will be further calls to this function. This might
10978 ;; be taken as an advice not to save buffers/internal variables just
10979 ;; yet, but wait until the last call to speed things up.
10980 ;;
10981 ;; `choke-request-accept-article GROUP &optional LAST' 
10982 ;; The contents of the current buffer will be put into GROUP.  There
10983 ;; should, of course, be an article in the current buffer.  This
10984 ;; function is normally only called by the function described above,
10985 ;; and LAST works the same way as in that function.
10986 ;;
10987 ;; `choke-request-replace-article ARTICLE GROUP BUFFER'
10988 ;; Replace ARTICLE in GROUP with the contents of BUFFER.
10989 ;; This provides an easy interface for allowing editing of
10990 ;; articles. Note that even headers may be edited, so the backend has
10991 ;; to update any tables (nov buffers, etc) that it maintains after
10992 ;; replacing the article.
10993 ;;
10994 ;; `choke-request-create-group GROUP &optional SERVER'
10995 ;; Create GROUP on SERVER.  This might be a new, empty group, or it
10996 ;; might be a group that already exists, but hasn't been registered
10997 ;; yet. 
10998 ;;
10999 ;; All these functions must return nil if they couldn't service the
11000 ;; request. If the optional arguments are not supplied, some "current"
11001 ;; or "default" values should be used. In short, one should emulate an
11002 ;; NNTP server, in a way.
11003 ;;
11004 ;; If you want to write a new backend, you just have to supply the
11005 ;; functions listed above. In addition, you must enter the new backend
11006 ;; into the list of valid select methods:
11007 ;; (setq gnus-valid-select-methods 
11008 ;;       (cons '("choke" mail) gnus-valid-select-methods))
11009 ;; The first element in this list is the name of the backend. Other
11010 ;; elemnets may be `mail' (for mail groups),  `post' (for news
11011 ;; groups), `none' (neither), `respool' (for groups that can control
11012 ;; their contents). 
11013
11014 (defun gnus-start-news-server (&optional confirm)
11015   "Open a method for getting news.
11016 If CONFIRM is non-nil, the user will be asked for an NNTP server."
11017   (let (how where)
11018     (if gnus-current-select-method
11019         ;; Stream is already opened.
11020         nil
11021       ;; Open NNTP server.
11022       (if (null gnus-nntp-service) (setq gnus-nntp-server nil))
11023       (if confirm
11024           (progn
11025             ;; Read server name with completion.
11026             (setq gnus-nntp-server
11027                   (completing-read "NNTP server: "
11028                                    (mapcar (lambda (server) (list server))
11029                                            (cons (list gnus-nntp-server)
11030                                                  gnus-secondary-servers))
11031                                    nil nil gnus-nntp-server))))
11032
11033       (if (and gnus-nntp-server 
11034                (stringp gnus-nntp-server)
11035                (not (string= gnus-nntp-server "")))
11036           (setq gnus-select-method
11037                 (cond ((or (string= gnus-nntp-server "")
11038                            (string= gnus-nntp-server "::"))
11039                        (list 'nnspool (system-name)))
11040                       ((string-match "^:" gnus-nntp-server)
11041                        (list 'nnmh gnus-nntp-server 
11042                              (list 'nnmh-directory 
11043                                    (file-name-as-directory
11044                                     (expand-file-name
11045                                      (concat "~/" (substring
11046                                                    gnus-nntp-server 1)))))
11047                              (list 'nnmh-get-new-mail nil)))
11048                       (t
11049                        (list 'nntp gnus-nntp-server)))))
11050
11051       (setq how (car gnus-select-method))
11052       (setq where (car (cdr gnus-select-method)))
11053       (cond ((eq how 'nnspool)
11054              (require 'nnspool)
11055              (gnus-message 5 "Looking up local news spool..."))
11056             ((eq how 'nnmh)
11057              (require 'nnmh)
11058              (gnus-message 5 "Looking up mh spool..."))
11059             (t
11060              (require 'nntp)))
11061       (setq gnus-current-select-method gnus-select-method)
11062       (run-hooks 'gnus-open-server-hook)
11063       (or 
11064        ;; gnus-open-server-hook might have opened it
11065        (gnus-server-opened gnus-select-method)  
11066        (gnus-open-server gnus-select-method)
11067        (gnus-y-or-n-p
11068         (format
11069          "%s server on %s can't be opened. Continue? "
11070          (car gnus-select-method) (nth 1 gnus-select-method)))
11071        (progn
11072          (gnus-message 1 "Couldn't open server on %s" 
11073                        (nth 1 gnus-select-method))
11074          (ding)
11075          nil)))))
11076
11077 (defun gnus-check-news-server (&optional method)
11078   "If the news server is down, start it up again."
11079   (let ((method (if method method gnus-select-method)))
11080     (and (stringp method)
11081          (setq method (gnus-server-to-method method)))
11082     (if (gnus-server-opened method)
11083         ;; Stream is already opened.
11084         t
11085       ;; Open server.
11086       (gnus-message 5 "Opening server %s on %s..." (car method) (nth 1 method))
11087       (run-hooks 'gnus-open-server-hook)
11088       (or (gnus-server-opened method)
11089           (gnus-open-server method))
11090       (message ""))))
11091
11092 (defun gnus-nntp-message (&optional message)
11093   "Check the status of the NNTP server.
11094 If the status of the server is clear and MESSAGE is non-nil, MESSAGE
11095 is returned insted of the status string."
11096   (let ((status (gnus-status-message (gnus-find-method-for-group 
11097                                       gnus-newsgroup-name)))
11098         (message (or message "")))
11099     (if (and (stringp status) (> (length status) 0))
11100         status message)))
11101
11102 (defun gnus-get-function (method function)
11103   (and (stringp method)
11104        (setq method (gnus-server-to-method method)))
11105   (let ((func (intern (format "%s-%s" (car method) function))))
11106     (if (not (fboundp func)) 
11107         (progn
11108           (require (car method))
11109           (if (not (fboundp func)) 
11110               (error "No such function: %s" func))))
11111     func))
11112
11113 ;;; Interface functions to the backends.
11114
11115 (defun gnus-open-server (method)
11116   (funcall (gnus-get-function method 'open-server)
11117            (nth 1 method) (nthcdr 2 method)))
11118
11119 (defun gnus-close-server (method)
11120   (funcall (gnus-get-function method 'close-server) (nth 1 method)))
11121
11122 (defun gnus-request-list (method)
11123   (funcall (gnus-get-function method 'request-list) (nth 1 method)))
11124
11125 (defun gnus-request-list-newsgroups (method)
11126   (funcall (gnus-get-function method 'request-list-newsgroups) (nth 1 method)))
11127
11128 (defun gnus-request-newgroups (date method)
11129   (funcall (gnus-get-function method 'request-newgroups) 
11130            date (nth 1 method)))
11131
11132 (defun gnus-server-opened (method)
11133   (funcall (gnus-get-function method 'server-opened) (nth 1 method)))
11134
11135 (defun gnus-status-message (method)
11136   (let ((method (if (stringp method) (gnus-find-method-for-group method)
11137                   method)))
11138     (funcall (gnus-get-function method 'status-message) (nth 1 method))))
11139
11140 (defun gnus-request-group (group &optional dont-check)
11141   (let ((method (gnus-find-method-for-group group)))
11142 ;    (and t (message "%s GROUP %s" (car method) group))
11143     (funcall (gnus-get-function method 'request-group) 
11144              (gnus-group-real-name group) (nth 1 method) dont-check)))
11145
11146 (defun gnus-request-asynchronous (group &optional articles)
11147   (let ((method (gnus-find-method-for-group group)))
11148     (funcall (gnus-get-function method 'request-asynchronous) 
11149              (gnus-group-real-name group) (nth 1 method) articles)))
11150
11151 (defun gnus-list-active-group (group)
11152   (let ((method (gnus-find-method-for-group group))
11153         (func 'list-active-group))
11154     (and (gnus-check-backend-function func group)
11155          (funcall (gnus-get-function method func) 
11156                   (gnus-group-real-name group) (nth 1 method)))))
11157
11158 (defun gnus-request-group-description (group)
11159   (let ((method (gnus-find-method-for-group group))
11160         (func 'request-group-description))
11161     (and (gnus-check-backend-function func group)
11162          (funcall (gnus-get-function method func) 
11163                   (gnus-group-real-name group) (nth 1 method)))))
11164
11165 (defun gnus-close-group (group)
11166   (let ((method (gnus-find-method-for-group group)))
11167     (funcall (gnus-get-function method 'close-group) 
11168              (gnus-group-real-name group) (nth 1 method))))
11169
11170 (defun gnus-retrieve-headers (articles group)
11171   (let ((method (gnus-find-method-for-group group)))
11172     (if (and gnus-use-cache (numberp (car articles)))
11173         (gnus-cache-retrieve-headers articles group)
11174       (funcall (gnus-get-function method 'retrieve-headers) 
11175                articles (gnus-group-real-name group) (nth 1 method)))))
11176
11177 (defun gnus-retrieve-groups (groups method)
11178   (funcall (gnus-get-function method 'retrieve-groups) groups (nth 1 method)))
11179
11180 (defun gnus-request-article (article group &optional buffer)
11181   (let ((method (gnus-find-method-for-group group)))
11182     (funcall (gnus-get-function method 'request-article) 
11183              article (gnus-group-real-name group) (nth 1 method) buffer)))
11184
11185 (defun gnus-request-head (article group)
11186   (let ((method (gnus-find-method-for-group group)))
11187     (funcall (gnus-get-function method 'request-head) 
11188              article (gnus-group-real-name group) (nth 1 method))))
11189
11190 ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
11191 (defun gnus-request-post-buffer (post group subject header artbuf
11192                                       info follow-to respect-poster)
11193    (let* ((info (or info (and group (nth 2 (gnus-gethash 
11194                                             group gnus-newsrc-hashtb)))))
11195           (method
11196            (if (and gnus-post-method
11197                     ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
11198                     (memq 'post (assoc
11199                                  (format "%s" (car (gnus-find-method-for-group
11200                                                     gnus-newsgroup-name)))
11201                                         gnus-valid-select-methods)))
11202                gnus-post-method
11203              (gnus-find-method-for-group gnus-newsgroup-name))))
11204      (or (gnus-server-opened method)
11205          (gnus-open-server method)
11206          (error "Can't open server %s:%s" (car method) (nth 1 method)))
11207      (let ((mail-self-blind nil)
11208            (mail-archive-file-name nil))
11209        (funcall (gnus-get-function method 'request-post-buffer) 
11210                 post group subject header artbuf info follow-to
11211                 respect-poster))))
11212
11213 (defun gnus-request-post (method &optional force)
11214   (and (stringp method)
11215        (setq method (gnus-server-to-method method)))
11216   (and (not force) gnus-post-method
11217        (memq 'post (assoc (format "%s" (car method))
11218                           gnus-valid-select-methods))
11219        (setq method gnus-post-method))
11220   (funcall (gnus-get-function method 'request-post) 
11221            (nth 1 method)))
11222
11223 (defun gnus-request-expire-articles (articles group &optional force)
11224   (let ((method (gnus-find-method-for-group group)))
11225     (funcall (gnus-get-function method 'request-expire-articles) 
11226              articles (gnus-group-real-name group) (nth 1 method)
11227              force)))
11228
11229 (defun gnus-request-move-article 
11230   (article group server accept-function &optional last)
11231   (let ((method (gnus-find-method-for-group group)))
11232     (funcall (gnus-get-function method 'request-move-article) 
11233              article (gnus-group-real-name group) 
11234              (nth 1 method) accept-function last)))
11235
11236 (defun gnus-request-accept-article (group &optional last)
11237   (let ((func (if (symbolp group) group
11238                 (car (gnus-find-method-for-group group)))))
11239     (funcall (intern (format "%s-request-accept-article" func))
11240              (if (stringp group) (gnus-group-real-name group) group)
11241              last)))
11242
11243 (defun gnus-request-replace-article (article group buffer)
11244   (let ((func (car (gnus-find-method-for-group group))))
11245     (funcall (intern (format "%s-request-replace-article" func))
11246              article (gnus-group-real-name group) buffer)))
11247
11248 (defun gnus-request-create-group (group)
11249   (let ((method (gnus-find-method-for-group group)))
11250     (funcall (gnus-get-function method 'request-create-group) 
11251              (gnus-group-real-name group) (nth 1 method))))
11252
11253 (defun gnus-member-of-valid (symbol group)
11254   (memq symbol (assoc
11255                 (format "%s" (car (gnus-find-method-for-group group)))
11256                 gnus-valid-select-methods)))
11257
11258 (defsubst gnus-secondary-method-p (method)
11259   (member method gnus-secondary-select-methods))
11260
11261 (defun gnus-find-method-for-group (group &optional info)
11262   (or gnus-override-method
11263       (and (not group)
11264            gnus-select-method)
11265       (let ((info (or info (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
11266             method)
11267         (if (or (not info)
11268                 (not (setq method (nth 4 info))))
11269             (setq method gnus-select-method)
11270           (setq method
11271                 (cond ((stringp method)
11272                        (gnus-server-to-method method))
11273                       ((stringp (car method))
11274                        (gnus-server-extend-method group method))
11275                       (t
11276                        method))))
11277         (gnus-server-add-address method))))
11278
11279 (defun gnus-check-backend-function (func group)
11280   (let ((method (if (stringp group) (car (gnus-find-method-for-group group))
11281                  group)))
11282     (fboundp (intern (format "%s-%s" method func)))))
11283
11284 (defun gnus-methods-using (method)
11285   (let ((valids gnus-valid-select-methods)
11286         outs)
11287     (while valids
11288       (if (memq method (car valids)) 
11289           (setq outs (cons (car valids) outs)))
11290       (setq valids (cdr valids)))
11291     outs))
11292
11293 ;;; 
11294 ;;; Active & Newsrc File Handling
11295 ;;;
11296
11297 ;; Newsrc related functions.
11298 ;; Gnus internal format of gnus-newsrc-alist:
11299 ;; (("alt.general" 3 (1 . 1))
11300 ;;  ("alt.misc"    3 ((1 . 10) (12 . 15)))
11301 ;;  ("alt.test"    7 (1 . 99) (45 57 93)) ...)
11302 ;; The first item is the group name; the second is the subscription
11303 ;; level; the third is either a range of a list of ranges of read
11304 ;; articles, the optional fourth element is a list of marked articles,
11305 ;; the optional fifth element is the select method.
11306 ;;
11307 ;; Gnus internal format of gnus-newsrc-hashtb:
11308 ;; (95 ("alt.general" 3 (1 . 1)) ("alt.misc" 3 ((1 . 10) (12 . 15))) ...)
11309 ;; This is the entry for "alt.misc". The first element is the number
11310 ;; of unread articles in "alt.misc". The cdr of this entry is the
11311 ;; element *before* "alt.misc" in gnus-newsrc-alist, which makes is
11312 ;; trivial to remove or add new elements into gnus-newsrc-alist
11313 ;; without scanning the entire list. So, to get the actual information
11314 ;; of "alt.misc", you'd say something like 
11315 ;; (nth 2 (gnus-gethash "alt.misc" gnus-newsrc-hashtb))
11316 ;;
11317 ;; Gnus internal format of gnus-active-hashtb:
11318 ;; ((1 . 1))
11319 ;;  (5 . 10))
11320 ;;  (67 . 99)) ...)
11321 ;; The only element in each entry in this hash table is a range of
11322 ;; (possibly) available articles. (Articles in this range may have
11323 ;; been expired or canceled.)
11324 ;;
11325 ;; Gnus internal format of gnus-killed-list and gnus-zombie-list:
11326 ;; ("alt.misc" "alt.test" "alt.general" ...)
11327
11328 (defun gnus-setup-news (&optional rawfile level)
11329   "Setup news information.
11330 If RAWFILE is non-nil, the .newsrc file will also be read.
11331 If LEVEL is non-nil, the news will be set up at level LEVEL."
11332   (let ((init (not (and gnus-newsrc-alist gnus-active-hashtb (not rawfile)))))
11333     ;; Clear some variables to re-initialize news information.
11334     (if init (setq gnus-newsrc-alist nil gnus-active-hashtb nil))
11335
11336     ;; Read the newsrc file and create `gnus-newsrc-hashtb'.
11337     (if init (gnus-read-newsrc-file rawfile))
11338
11339     ;; Read the active file and create `gnus-active-hashtb'.
11340     ;; If `gnus-read-active-file' is nil, then we just create an empty
11341     ;; hash table. The partial filling out of the hash table will be
11342     ;; done in `gnus-get-unread-articles'.
11343     (if (and gnus-read-active-file 
11344              (not level)
11345              (gnus-server-opened gnus-select-method))
11346         (gnus-read-active-file)
11347       (setq gnus-active-hashtb (make-vector 4095 0)))
11348
11349     (and init gnus-use-dribble-file (gnus-dribble-read-file))
11350
11351     ;; Find the number of unread articles in each non-dead group.
11352     (gnus-get-unread-articles (or level (1+ gnus-level-subscribed)))
11353     ;; Find new newsgroups and treat them.
11354     (if (and init gnus-check-new-newsgroups gnus-read-active-file (not level)
11355              (gnus-server-opened gnus-select-method))
11356         (gnus-find-new-newsgroups))
11357     (if (and init gnus-check-bogus-newsgroups 
11358              gnus-read-active-file (not level)
11359              (gnus-server-opened gnus-select-method))
11360         (gnus-check-bogus-newsgroups))))
11361
11362 (defun gnus-find-new-newsgroups ()
11363   "Search for new newsgroups and add them.
11364 Each new newsgroup will be treated with `gnus-subscribe-newsgroup-method.'
11365 The `-n' option line from .newsrc is respected."
11366   (interactive)
11367   (or (gnus-check-first-time-used)
11368       (if (or (consp gnus-check-new-newsgroups)
11369               (eq gnus-check-new-newsgroups 'ask-server))
11370           (gnus-ask-server-for-new-groups)
11371         (let ((groups 0)
11372               group new-newsgroups)
11373           (or gnus-have-read-active-file (gnus-read-active-file))
11374           (setq gnus-newsrc-last-checked-date (current-time-string))
11375           (if (not gnus-killed-hashtb) (gnus-make-hashtable-from-killed))
11376           ;; Go though every newsgroup in `gnus-active-hashtb' and compare
11377           ;; with `gnus-newsrc-hashtb' and `gnus-killed-hashtb'.
11378           (mapatoms
11379            (lambda (sym)
11380              (setq group (symbol-name sym))
11381              (if (or (gnus-gethash group gnus-killed-hashtb)
11382                      (gnus-gethash group gnus-newsrc-hashtb))
11383                  ()
11384                (let ((do-sub (gnus-matches-options-n group)))
11385                  (cond ((eq do-sub 'subscribe)
11386                         (setq groups (1+ groups))
11387                         (gnus-sethash group group gnus-killed-hashtb)
11388                         (funcall 
11389                          gnus-subscribe-options-newsgroup-method group))
11390                        ((eq do-sub 'ignore)
11391                         nil)
11392                        (t
11393                         (setq groups (1+ groups))
11394                         (gnus-sethash group group gnus-killed-hashtb)
11395                         (if gnus-subscribe-hierarchical-interactive
11396                             (setq new-newsgroups (cons group new-newsgroups))
11397                           (funcall gnus-subscribe-newsgroup-method group)))))))
11398            gnus-active-hashtb)
11399           (if new-newsgroups 
11400               (gnus-subscribe-hierarchical-interactive new-newsgroups))
11401           ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
11402           (if (> groups 0)
11403               (gnus-message 6 "%d new newsgroup%s arrived." 
11404                             groups (if (> groups 1) "s have" " has")))))))
11405
11406 (defun gnus-matches-options-n (group)
11407   ;; Returns `subscribe' if the group is to be uncoditionally
11408   ;; subscribed, `ignore' if it is to be ignored, and nil if there is
11409   ;; no match for the group.
11410
11411   ;; First we check the two user variables.
11412   (cond
11413    ((and gnus-options-subscribe
11414          (string-match gnus-options-subscribe group))
11415     'subscribe)
11416    ((and gnus-options-not-subscribe
11417          (string-match gnus-options-not-subscribe group))
11418     'ignore)
11419    ;; Then we go through the list that was retrieved from the .newsrc
11420    ;; file.  This list has elements on the form 
11421    ;; `(REGEXP . {ignore,subscribe})'. The first match found (the list
11422    ;; is in the reverse order of the options line) is returned.
11423    (t
11424     (let ((regs gnus-newsrc-options-n))
11425       (while (and regs
11426                   (not (string-match (car (car regs)) group)))
11427         (setq regs (cdr regs)))
11428       (and regs (cdr (car regs)))))))
11429
11430 (defun gnus-ask-server-for-new-groups ()
11431   (let* ((date (or gnus-newsrc-last-checked-date (current-time-string)))
11432          (methods (cons gnus-select-method 
11433                         (append
11434                          (and (consp gnus-check-new-newsgroups)
11435                               gnus-check-new-newsgroups)
11436                          gnus-secondary-select-methods)))
11437          (groups 0)
11438          (new-date (current-time-string))
11439          hashtb group new-newsgroups got-new)
11440     ;; Go thorugh both primary and secondary select methods and
11441     ;; request new newsgroups.  
11442     (while methods
11443       (and (or (gnus-server-opened (car methods))
11444                (gnus-open-server (car methods)))
11445            (gnus-request-newgroups date (car methods))
11446            (save-excursion
11447              (setq got-new t)
11448              (set-buffer nntp-server-buffer)
11449              (or hashtb (setq hashtb (gnus-make-hashtable 
11450                                       (count-lines (point-min) (point-max)))))
11451              ;; Enter all the new groups in a hashtable.
11452              (gnus-active-to-gnus-format (car methods) hashtb 'ignore)))
11453       (setq methods (cdr methods)))
11454     (and got-new (setq gnus-newsrc-last-checked-date new-date))
11455     ;; Now all new groups from all select methods are in `hashtb'.
11456     (mapatoms
11457      (lambda (group-sym)
11458        (setq group (symbol-name group-sym))
11459        (if (or (gnus-gethash group gnus-newsrc-hashtb)
11460                (member group gnus-zombie-list)
11461                (member group gnus-killed-list))
11462            ;; The group is already known.
11463            ()
11464          (and (symbol-value group-sym)
11465               (gnus-sethash group (symbol-value group-sym) gnus-active-hashtb))
11466          (let ((do-sub (gnus-matches-options-n group)))
11467            (cond ((eq do-sub 'subscribe)
11468                   (setq groups (1+ groups))
11469                   (gnus-sethash group group gnus-killed-hashtb)
11470                   (funcall 
11471                    gnus-subscribe-options-newsgroup-method group))
11472                  ((eq do-sub 'ignore)
11473                   nil)
11474                  (t
11475                   (setq groups (1+ groups))
11476                   (gnus-sethash group group gnus-killed-hashtb)
11477                   (if gnus-subscribe-hierarchical-interactive
11478                       (setq new-newsgroups (cons group new-newsgroups))
11479                     (funcall gnus-subscribe-newsgroup-method group)))))))
11480      hashtb)
11481     (if new-newsgroups 
11482         (gnus-subscribe-hierarchical-interactive new-newsgroups))
11483     ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
11484     (if (> groups 0)
11485         (gnus-message 6 "%d new newsgroup%s arrived." 
11486                       groups (if (> groups 1) "s have" " has")))
11487     got-new))
11488
11489 (defun gnus-check-first-time-used ()
11490   (if (or (> (length gnus-newsrc-alist) 1)
11491           (file-exists-p gnus-startup-file)
11492           (file-exists-p (concat gnus-startup-file ".el"))
11493           (file-exists-p (concat gnus-startup-file ".eld")))
11494       nil
11495     (gnus-message 6 "First time user; subscribing you to default groups")
11496     (or gnus-have-read-active-file (gnus-read-active-file))
11497     (setq gnus-newsrc-last-checked-date (current-time-string))
11498     (let ((groups gnus-default-subscribed-newsgroups)
11499           group)
11500       (if (eq groups t)
11501           nil
11502         (setq groups (or groups gnus-backup-default-subscribed-newsgroups))
11503         (mapatoms
11504          (lambda (sym)
11505            (setq group (symbol-name sym))
11506            (let ((do-sub (gnus-matches-options-n group)))
11507              (cond ((eq do-sub 'subscribe)
11508                     (gnus-sethash group group gnus-killed-hashtb)
11509                     (funcall 
11510                      gnus-subscribe-options-newsgroup-method group))
11511                    ((eq do-sub 'ignore)
11512                     nil)
11513                    (t
11514                     (setq gnus-killed-list (cons group gnus-killed-list))))))
11515          gnus-active-hashtb)
11516         (while groups
11517           (if (gnus-gethash (car groups) gnus-active-hashtb)
11518               (gnus-group-change-level 
11519                (car groups) gnus-level-default-subscribed gnus-level-killed))
11520           (setq groups (cdr groups)))
11521         (gnus-group-make-help-group)
11522         (and gnus-novice-user
11523              (gnus-message 7 "`A k' to list killed groups"))))))
11524
11525 (defun gnus-subscribe-group (group previous &optional method)
11526   (gnus-group-change-level 
11527    (if method
11528        (list t group gnus-level-default-subscribed nil nil method)
11529      group) 
11530    gnus-level-default-subscribed gnus-level-killed previous t))
11531
11532 ;; `gnus-group-change-level' is the fundamental function for changing
11533 ;; subscription levels of newsgroups. This might mean just changing
11534 ;; from level 1 to 2, which is pretty trivial, from 2 to 6 or back
11535 ;; again, which subscribes/unsubscribes a group, which is equally
11536 ;; trivial. Changing from 1-7 to 8-9 means that you kill a group, and
11537 ;; from 8-9 to 1-7 means that you remove the group from the list of
11538 ;; killed (or zombie) groups and add them to the (kinda) subscribed
11539 ;; groups. And last but not least, moving from 8 to 9 and 9 to 8,
11540 ;; which is trivial.
11541 ;; ENTRY can either be a string (newsgroup name) or a list (if
11542 ;; FROMKILLED is t, it's a list on the format (NUM INFO-LIST),
11543 ;; otherwise it's a list in the format of the `gnus-newsrc-hashtb'
11544 ;; entries. 
11545 ;; LEVEL is the new level of the group, OLDLEVEL is the old level and
11546 ;; PREVIOUS is the group (in hashtb entry format) to insert this group
11547 ;; after. 
11548 (defun gnus-group-change-level (entry level &optional oldlevel
11549                                       previous fromkilled)
11550   (let ((pinfo entry)
11551         group info active num)
11552     ;; Glean what info we can from the arguments
11553     (if (consp entry)
11554         (if fromkilled (setq group (nth 1 entry))
11555           (setq group (car (nth 2 entry))))
11556       (setq group entry))
11557     (if (and (stringp entry)
11558              oldlevel 
11559              (< oldlevel gnus-level-zombie))
11560         (setq entry (gnus-gethash entry gnus-newsrc-hashtb)))
11561     (if (and (not oldlevel)
11562              (consp entry))
11563         (setq oldlevel (car (cdr (nth 2 entry)))))
11564     (if (stringp previous)
11565         (setq previous (gnus-gethash previous gnus-newsrc-hashtb)))
11566
11567     (gnus-dribble-enter
11568      (format "(gnus-group-change-level %S %S %S %S %S)" 
11569              group level oldlevel (car (nth 2 previous)) fromkilled))
11570     
11571     ;; Then we remove the newgroup from any old structures, if needed.
11572     ;; If the group was killed, we remove it from the killed or zombie
11573     ;; list. If not, and it is in fact going to be killed, we remove
11574     ;; it from the newsrc hash table and assoc.
11575     (cond ((>= oldlevel gnus-level-zombie)
11576            (if (= oldlevel gnus-level-zombie)
11577                (setq gnus-zombie-list (delete group gnus-zombie-list))
11578              (setq gnus-killed-list (delete group gnus-killed-list))))
11579           (t
11580            (if (>= level gnus-level-zombie)
11581                (progn
11582                  (gnus-sethash (car (nth 2 entry))
11583                                nil gnus-newsrc-hashtb)
11584                  (if (nth 3 entry)
11585                      (setcdr (gnus-gethash (car (nth 3 entry))
11586                                            gnus-newsrc-hashtb)
11587                              (cdr entry)))
11588                  (setcdr (cdr entry) (cdr (cdr (cdr entry))))))))
11589
11590     ;; Finally we enter (if needed) the list where it is supposed to
11591     ;; go, and change the subscription level. If it is to be killed,
11592     ;; we enter it into the killed or zombie list.
11593     (cond ((>= level gnus-level-zombie)
11594            ;; Remove from the hash table.
11595            (gnus-sethash group nil gnus-newsrc-hashtb)
11596            (or (gnus-group-foreign-p group)
11597                ;; We do not enter foreign groups into the list of dead
11598                ;; groups.  
11599                (if (= level gnus-level-zombie)
11600                    (setq gnus-zombie-list (cons group gnus-zombie-list))
11601                  (setq gnus-killed-list (cons group gnus-killed-list)))))
11602           (t
11603            ;; If the list is to be entered into the newsrc assoc, and
11604            ;; it was killed, we have to create an entry in the newsrc
11605            ;; hashtb format and fix the pointers in the newsrc assoc.
11606            (if (>= oldlevel gnus-level-zombie)
11607                (progn
11608                  (if (listp entry)
11609                      (progn
11610                        (setq info (cdr entry))
11611                        (setq num (car entry)))
11612                    (setq active (gnus-gethash group gnus-active-hashtb))
11613                    (setq num (if active (- (1+ (cdr active)) (car active)) t))
11614                    ;; Check whether the group is foreign. If so, the
11615                    ;; foreign select method has to be entered into the
11616                    ;; info. 
11617                    (let ((method (gnus-group-method-name group)))
11618                      (if (eq method gnus-select-method)
11619                          (setq info (list group level nil))
11620                        (setq info (list group level nil nil method)))))
11621                  (or previous 
11622                      (setq previous 
11623                            (let ((p gnus-newsrc-alist))
11624                              (while (cdr (cdr p))
11625                                (setq p (cdr p)))
11626                              p)))
11627                  (setq entry (cons info (cdr (cdr previous))))
11628                  (if (cdr previous)
11629                      (progn
11630                        (setcdr (cdr previous) entry)
11631                        (gnus-sethash group (cons num (cdr previous)) 
11632                                      gnus-newsrc-hashtb))
11633                    (setcdr previous entry)
11634                    (gnus-sethash group (cons num previous)
11635                                  gnus-newsrc-hashtb))
11636                  (if (cdr entry)
11637                      (setcdr (gnus-gethash (car (car (cdr entry)))
11638                                            gnus-newsrc-hashtb)
11639                              entry)))
11640              ;; It was alive, and it is going to stay alive, so we
11641              ;; just change the level and don't change any pointers or
11642              ;; hash table entries.
11643              (setcar (cdr (car (cdr (cdr entry)))) level))))))
11644
11645 (defun gnus-kill-newsgroup (newsgroup)
11646   "Obsolete function. Kills a newsgroup."
11647   (gnus-group-change-level
11648    (gnus-gethash newsgroup gnus-newsrc-hashtb) gnus-level-killed))
11649
11650 (defun gnus-check-bogus-newsgroups (&optional confirm)
11651   "Remove bogus newsgroups.
11652 If CONFIRM is non-nil, the user has to confirm the deletion of every
11653 newsgroup." 
11654   (let ((newsrc (cdr gnus-newsrc-alist))
11655         bogus group entry)
11656     (gnus-message 5 "Checking bogus newsgroups...")
11657     (or gnus-have-read-active-file (gnus-read-active-file))
11658     ;; Find all bogus newsgroup that are subscribed.
11659     (while newsrc
11660       (setq group (car (car newsrc)))
11661       (if (or (gnus-gethash group gnus-active-hashtb) ; Active
11662               (nth 4 (car newsrc))      ; Foreign
11663               (and confirm
11664                    (not (gnus-y-or-n-p
11665                          (format "Remove bogus newsgroup: %s " group)))))
11666           ;; Don't remove.
11667           ()
11668         ;; Found a bogus newsgroup.
11669         (setq bogus (cons group bogus)))
11670       (setq newsrc (cdr newsrc)))
11671     ;; Remove all bogus subscribed groups by first killing them, and
11672     ;; then removing them from the list of killed groups.
11673     (while bogus
11674       (and (setq entry (gnus-gethash (car bogus) gnus-newsrc-hashtb))
11675            (progn
11676              (gnus-group-change-level entry gnus-level-killed)
11677              (setq gnus-killed-list (delete (car bogus) gnus-killed-list))))
11678       (setq bogus (cdr bogus)))
11679     ;; Then we remove all bogus groups from the list of killed and
11680     ;; zombie groups. They are are removed without confirmation.
11681     (let ((dead-lists '(gnus-killed-list gnus-zombie-list))
11682           killed)
11683       (while dead-lists
11684         (setq killed (symbol-value (car dead-lists)))
11685         (while killed
11686           (setq group (car killed))
11687           (or (gnus-gethash group gnus-active-hashtb)
11688               ;; The group is bogus.
11689               (set (car dead-lists)
11690                    (delete group (symbol-value (car dead-lists)))))
11691           (setq killed (cdr killed)))
11692         (setq dead-lists (cdr dead-lists))))
11693     (gnus-message 5 "Checking bogus newsgroups...done")))
11694
11695 (defun gnus-check-duplicate-killed-groups ()
11696   "Remove duplicates from the list of killed groups."
11697   (interactive)
11698   (let ((killed gnus-killed-list))
11699     (while killed
11700       (gnus-message 9 "%d" (length killed))
11701       (setcdr killed (delete (car killed) (cdr killed)))
11702       (setq killed (cdr killed)))))
11703
11704 ;; Go though `gnus-newsrc-alist' and compare with `gnus-active-hashtb'
11705 ;; and compute how many unread articles there are in each group.
11706 (defun gnus-get-unread-articles (&optional level) 
11707   (let* ((newsrc (cdr gnus-newsrc-alist))
11708          (conditional level)
11709          (level (or level (1+ gnus-level-subscribed)))
11710          info group active virtuals method)
11711     (gnus-message 5 "Checking new news...")
11712     (while newsrc
11713       (setq info (car newsrc))
11714       (setq group (car info))
11715       (setq active (gnus-gethash group gnus-active-hashtb))
11716
11717       ;; Check newsgroups. If the user doesn't want to check them, or
11718       ;; they can't be checked (for instance, if the news server can't
11719       ;; be reached) we just set the number of unread articles in this
11720       ;; newsgroup to t. This means that Gnus thinks that there are
11721       ;; unread articles, but it has no idea how many.
11722       (if (and (setq method (nth 4 info))
11723                (not (gnus-server-equal gnus-select-method
11724                                        (gnus-server-get-method nil method)))
11725                (not (gnus-secondary-method-p method)))
11726           ;; These groups are foreign.
11727           (if (or (and gnus-activate-foreign-newsgroups 
11728                        (not (numberp gnus-activate-foreign-newsgroups)))
11729                   (and (numberp gnus-activate-foreign-newsgroups)
11730                        (<= (nth 1 info) gnus-activate-foreign-newsgroups)
11731                        (<= (nth 1 info) level)))
11732               (if (eq (car (if (stringp method) 
11733                                (gnus-server-to-method method)
11734                              (nth 4 info))) 'nnvirtual)
11735                   (setq virtuals (cons info virtuals))
11736                 (setq active (gnus-activate-newsgroup (car info)))))
11737         ;; These groups are native or secondary. 
11738         (if (and (not gnus-read-active-file)
11739                  (<= (nth 1 info) level))
11740             (progn
11741               (setq active (gnus-activate-newsgroup (car info))))))
11742       
11743       (or active (progn (gnus-sethash group nil gnus-active-hashtb)
11744                         (setcar (gnus-gethash group gnus-newsrc-hashtb) t)))
11745       (and active 
11746            (gnus-get-unread-articles-in-group info active)
11747            ;; Close the groups as we look at them!
11748            (gnus-close-group group))
11749       (setq newsrc (cdr newsrc)))
11750
11751     ;; Activate the virtual groups. This has to be done after all the
11752     ;; other groups. 
11753     ;; !!! If one virtual group contains another virtual group, even
11754     ;; doing it this way might cause problems.
11755    (while virtuals
11756       (and (setq active (gnus-activate-newsgroup (car (car virtuals))))
11757            (gnus-get-unread-articles-in-group (car virtuals) active))
11758       (setq virtuals (cdr virtuals)))
11759
11760     (gnus-message 5 "Checking new news...done")))
11761
11762 ;; Create a hash table out of the newsrc alist. The `car's of the
11763 ;; alist elements are used as keys.
11764 (defun gnus-make-hashtable-from-newsrc-alist ()
11765   (let ((alist gnus-newsrc-alist)
11766         (ohashtb gnus-newsrc-hashtb)
11767         prev)
11768     (setq gnus-newsrc-hashtb (gnus-make-hashtable (length alist)))
11769     (setq alist 
11770           (setq prev (setq gnus-newsrc-alist 
11771                            (if (equal (car (car gnus-newsrc-alist))
11772                                       "dummy.group")
11773                                gnus-newsrc-alist
11774                              (cons (list "dummy.group" 0 nil) alist)))))
11775     (while alist
11776       (gnus-sethash (car (car alist)) 
11777                     (cons (and ohashtb (car (gnus-gethash 
11778                                              (car (car alist)) ohashtb))) 
11779                           prev) gnus-newsrc-hashtb)
11780       (setq prev alist
11781             alist (cdr alist)))))
11782
11783 (defun gnus-make-hashtable-from-killed ()
11784   "Create a hash table from the killed and zombie lists."
11785   (let ((lists '(gnus-killed-list gnus-zombie-list))
11786         list)
11787     (setq gnus-killed-hashtb 
11788           (gnus-make-hashtable 
11789            (+ (length gnus-killed-list) (length gnus-zombie-list))))
11790     (while lists
11791       (setq list (symbol-value (car lists)))
11792       (setq lists (cdr lists))
11793       (while list
11794         (gnus-sethash (car list) (car list) gnus-killed-hashtb)
11795         (setq list (cdr list))))))
11796
11797 (defun gnus-get-unread-articles-in-group (info active)
11798   (let* ((range (nth 2 info))
11799          (num 0)
11800          (marked (nth 3 info))
11801          srange lowest group highest)
11802     ;; If a cache is present, we may have to alter the active info.
11803     (and gnus-use-cache
11804          (gnus-cache-possibly-alter-active (car info) active))
11805     ;; Modify the list of read articles according to what articles 
11806     ;; are available; then tally the unread articles and add the
11807     ;; number to the group hash table entry.
11808     (cond ((zerop (cdr active))
11809            (setq num 0))
11810           ((not range)
11811            (setq num (- (1+ (cdr active)) (car active))))
11812           ((not (listp (cdr range)))
11813            ;; Fix a single (num . num) range according to the
11814            ;; active hash table.
11815            ;; Fix by Carsten Bormann <cabo@Informatik.Uni-Bremen.DE>.
11816            (and (< (cdr range) (car active)) (setcdr range (1- (car active))))
11817            (and (> (cdr range) (cdr active)) (setcdr range (cdr active)))
11818            ;; Compute number of unread articles.
11819            (setq num (max 0 (- (cdr active) 
11820                                (- (1+ (cdr range)) (car range))))))
11821           (t
11822            ;; The read list is a list of ranges. Fix them according to
11823            ;; the active hash table.
11824            ;; First peel off any elements that are below the lower
11825            ;; active limit. 
11826            (while (and (cdr range) 
11827                        (>= (car active) 
11828                            (or (and (atom (car (cdr range))) (car (cdr range)))
11829                                (car (car (cdr range))))))
11830              (if (numberp (car range))
11831                  (setcar range 
11832                          (cons (car range) 
11833                                (or (and (numberp (car (cdr range)))
11834                                         (car (cdr range))) 
11835                                    (cdr (car (cdr range))))))
11836                (setcdr (car range) 
11837                        (or (and (numberp (nth 1 range)) (nth 1 range))
11838                            (cdr (car (cdr range))))))
11839              (setcdr range (cdr (cdr range))))
11840            ;; Adjust the first element to be the same as the lower limit. 
11841            (if (and (not (atom (car range))) 
11842                     (< (cdr (car range)) (car active)))
11843                (setcdr (car range) (1- (car active))))
11844            ;; Then we want to peel off any elements that are higher
11845            ;; than the upper active limit.  
11846            (let ((srange range))
11847              ;; Go past all legal elements.
11848              (while (and (cdr srange) 
11849                          (<= (or (and (atom (car (cdr srange)))
11850                                       (car (cdr srange)))
11851                                  (car (car (cdr srange)))) (cdr active)))
11852                (setq srange (cdr srange)))
11853              (if (cdr srange)
11854                  ;; Nuke all remaining illegal elements.
11855                  (setcdr srange nil))
11856
11857              ;; Adjust the final element.
11858              (if (and (not (atom (car srange)))
11859                       (> (cdr (car srange)) (cdr active)))
11860                  (setcdr (car srange) (cdr active))))
11861            ;; Compute the number of unread articles.
11862            (while range
11863              (setq num (+ num (- (1+ (or (and (atom (car range)) (car range))
11864                                          (cdr (car range))))
11865                                  (or (and (atom (car range)) (car range))
11866                                      (car (car range))))))
11867              (setq range (cdr range)))
11868            (setq num (max 0 (- (cdr active) num)))))
11869     (and info
11870          (progn
11871            (and (assq 'tick marked)
11872                 (inline (gnus-remove-illegal-marked-articles
11873                          (assq 'tick marked) (nth 2 info))))
11874            (and (assq 'dormant marked)
11875                 (inline (gnus-remove-illegal-marked-articles
11876                          (assq 'dormant marked) (nth 2 info))))
11877            (setcar
11878             (gnus-gethash (car info) gnus-newsrc-hashtb) 
11879             (setq num (max 0 (- num (length (cdr (assq 'tick marked)))
11880                                 (length (cdr (assq 'dormant marked)))))))))
11881     num))
11882
11883 (defun gnus-remove-illegal-marked-articles (marked ranges)
11884   (let ((m (cdr marked)))
11885     ;; Make sure that all ticked articles are a subset of the unread
11886     ;; articles. 
11887     (while m
11888       (if (gnus-member-of-range (car m) ranges)
11889           (setcdr marked (cdr m))
11890         (setq marked m))
11891       (setq m (cdr m)))))
11892
11893 (defun gnus-activate-newsgroup (group)
11894   (let ((method (gnus-find-method-for-group group))
11895         active)
11896     (and (or (gnus-server-opened method) (gnus-open-server method))
11897          (gnus-request-group group)
11898          (save-excursion
11899            (set-buffer nntp-server-buffer)
11900            (goto-char (point-min))
11901            (and (looking-at "[0-9]+ [0-9]+ \\([0-9]+\\) [0-9]+")
11902                 (progn
11903                   (goto-char (match-beginning 1))
11904                   (gnus-sethash 
11905                    group (setq active (cons (read (current-buffer))
11906                                             (read (current-buffer))))
11907                    gnus-active-hashtb))
11908                 active)))))
11909
11910 (defun gnus-update-read-articles 
11911   (group unread unselected ticked &optional domarks replied expirable killed
11912          dormant bookmark score)
11913   "Update the list of read and ticked articles in GROUP using the
11914 UNREAD and TICKED lists.
11915 Note: UNSELECTED has to be sorted over `<'.
11916 Returns whether the updating was successful."
11917   (let* ((active (or gnus-newsgroup-active 
11918                      (gnus-gethash group gnus-active-hashtb)))
11919          (entry (gnus-gethash group gnus-newsrc-hashtb))
11920          (number (car entry))
11921          (info (nth 2 entry))
11922          (marked (nth 3 info))
11923          (prev 1)
11924          (unread (sort (copy-sequence unread) (function <)))
11925          last read)
11926     (if (or (not info) (not active))
11927         ;; There is no info on this group if it was, in fact,
11928         ;; killed. Gnus stores no information on killed groups, so
11929         ;; there's nothing to be done. 
11930         ;; One could store the information somewhere temporarily,
11931         ;; perhaps... Hmmm... 
11932         ()
11933       ;; Remove any negative articles numbers.
11934       (while (and unread (< (car unread) 0))
11935         (setq unread (cdr unread)))
11936       ;; Remove any expired article numbers
11937       (while (and unread (< (car unread) (car active)))
11938         (setq unread (cdr unread)))
11939       (while (and ticked (< (car ticked) (car active)))
11940         (setq ticked (cdr ticked)))
11941       (while (and dormant (< (car dormant) (car active)))
11942         (setq dormant (cdr dormant)))
11943       (setq unread (sort (append unselected unread) '<))
11944       ;; Set the number of unread articles in gnus-newsrc-hashtb.
11945       (setcar entry (max 0 (- (length unread) (length ticked) 
11946                               (length dormant))))
11947       ;; Compute the ranges of read articles by looking at the list of
11948       ;; unread articles.  
11949       (while unread
11950         (if (/= (car unread) prev)
11951             (setq read (cons (if (= prev (1- (car unread))) prev
11952                                (cons prev (1- (car unread)))) read)))
11953         (setq prev (1+ (car unread)))
11954         (setq unread (cdr unread)))
11955       (if (<= prev (cdr active))
11956           (setq read (cons (cons prev (cdr active)) read)))
11957       ;; Enter this list into the group info.
11958       (setcar (cdr (cdr info)) 
11959               (if (> (length read) 1) (nreverse read) read))
11960       ;; Enter the list of ticked articles.
11961       (gnus-set-marked-articles 
11962        info ticked
11963        (if domarks replied (cdr (assq 'reply marked)))
11964        (if domarks expirable (cdr (assq 'expire marked)))
11965        (if domarks killed (cdr (assq 'killed marked)))
11966        (if domarks dormant (cdr (assq 'dormant marked)))
11967        (if domarks bookmark (cdr (assq 'bookmark marked)))
11968        (if domarks score (cdr (assq 'score marked))))
11969       t)))
11970
11971 (defun gnus-make-articles-unread (group articles)
11972   "Mark ARTICLES in GROUP as unread."
11973   (let* ((info (nth 2 (or (gnus-gethash group gnus-newsrc-hashtb)
11974                           (gnus-gethash (gnus-group-real-name group)
11975                                         gnus-newsrc-hashtb))))
11976          (ranges (nth 2 info))
11977          news)
11978     (while articles
11979       (and (gnus-member-of-range (car articles) ranges)
11980            (setq news (cons (car articles) news)))
11981       (setq articles (cdr articles)))
11982     (if (not news)
11983         ()
11984       (setcar (nthcdr 2 info)
11985               (gnus-remove-from-range (nth 2 info) (nreverse news)))
11986       (gnus-group-update-group group t))))
11987
11988 ;; Get the active file(s) from the backend(s).
11989 (defun gnus-read-active-file ()
11990   (gnus-group-set-mode-line)
11991   (let ((methods (cons gnus-select-method gnus-secondary-select-methods))
11992         (not-first nil)
11993         list-type)
11994     (setq gnus-have-read-active-file nil)
11995     (save-excursion
11996       (set-buffer nntp-server-buffer)
11997       (while methods
11998         (let* ((where (nth 1 (car methods)))
11999                (mesg (format "Reading active file%s via %s..."
12000                              (if (and where (not (zerop (length where))))
12001                                  (concat " from " where) "")
12002                              (car (car methods)))))
12003           (gnus-message 5 mesg)
12004           (gnus-check-news-server (car methods))
12005           (cond 
12006            ((and (eq gnus-read-active-file 'some)
12007                  (gnus-check-backend-function
12008                   'retrieve-groups (car (car methods))))
12009             (let ((newsrc (cdr gnus-newsrc-alist))
12010                   groups)
12011               (while newsrc
12012                 (and (gnus-server-equal 
12013                       (gnus-find-method-for-group
12014                        (car (car newsrc)) (car newsrc))
12015                       (gnus-server-get-method nil (car methods)))
12016                      (setq groups (cons (car (car newsrc)) groups)))
12017                 (setq newsrc (cdr newsrc)))
12018               (setq list-type (gnus-retrieve-groups groups (car methods)))
12019               (cond ((not list-type)
12020                      (gnus-message 
12021                       1 "Cannot read partial active file from %s server." 
12022                       (car (car methods)))
12023                      (ding)
12024                      (sit-for 2))
12025                     ((eq list-type 'active)
12026                      (gnus-active-to-gnus-format (and not-first (car methods)))
12027                      (setq not-first t))
12028                     (t
12029                      (gnus-groups-to-gnus-format (and not-first (car methods)))
12030                      (setq not-first t)))))
12031            (t
12032             (if (not (gnus-request-list (car methods)))
12033                 (progn
12034                   (gnus-message 1 "Cannot read active file from %s server." 
12035                                 (car (car methods)))
12036                   (ding))
12037               (gnus-active-to-gnus-format (and not-first (car methods)))
12038               (setq gnus-have-read-active-file t
12039                     not-first t)
12040               (gnus-message 5 "%sdone" mesg)))))
12041         (setq methods (cdr methods))))))
12042
12043 ;; Read an active file and place the results in `gnus-active-hashtb'.
12044 (defun gnus-active-to-gnus-format (method &optional hashtb ignore-errors)
12045   (let ((cur (current-buffer))
12046         (hashtb (or hashtb 
12047                     (if method
12048                         gnus-active-hashtb
12049                       (setq gnus-active-hashtb
12050                             (gnus-make-hashtable 
12051                              (count-lines (point-min) (point-max))))))))
12052     ;; Delete unnecessary lines.
12053     (goto-char (point-min))
12054     (while (search-forward "\nto." nil t)
12055       (delete-region (1+ (match-beginning 0)) 
12056                      (progn (forward-line 1) (point))))
12057     (or (string= gnus-ignored-newsgroups "")
12058         (progn
12059           (goto-char (point-min))
12060           (delete-matching-lines gnus-ignored-newsgroups)))
12061     ;; If these are groups from a foreign select method, we insert the
12062     ;; group prefix in front of the group names. 
12063     (and method (not (eq method gnus-select-method))
12064          (let ((prefix (gnus-group-prefixed-name "" method)))
12065            (goto-char (point-min))
12066            (while (and (not (eobp))
12067                        (progn (insert prefix)
12068                               (zerop (forward-line 1)))))))
12069     (goto-char (point-min))
12070     ;; Store active file in hashtable.
12071     (goto-char (point-min))
12072     (if (string-match "%[oO]" gnus-group-line-format)
12073         ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
12074         ;; If we want information on moderated groups, we use this
12075         ;; loop...   
12076         (let* ((mod-hashtb (make-vector 7 0))
12077                (m (intern "m" mod-hashtb))
12078                group max mod min)
12079           (while (not (eobp))
12080             (condition-case nil
12081                 (progn
12082                   (narrow-to-region (point) (gnus-point-at-eol))
12083                   (setq group (let ((obarray hashtb)) (read cur)))
12084                   (if (and (numberp (setq max (read cur)))
12085                            (numberp (setq min (read cur))))
12086                       (set group (cons min max))
12087                     (set group nil))
12088                   ;; Enter moderated groups into a list.
12089                   (if (eq (let ((obarray mod-hashtb)) (read cur)) m)
12090                       (setq gnus-moderated-list 
12091                             (cons (symbol-name group) gnus-moderated-list))))
12092               (error nil))
12093             (widen)
12094             (forward-line 1)))
12095       ;; And if we do not care about moderation, we use this loop,
12096       ;; which is faster.
12097       (let (group max min)
12098         (while (not (eobp))
12099           (condition-case ()
12100               (progn
12101                 (narrow-to-region (point) (gnus-point-at-eol))
12102                 ;; group gets set to a symbol interned in the hash table
12103                 ;; (what a hack!!)
12104                 (setq group (let ((obarray hashtb)) (read cur)))
12105                 (if (and (numberp (setq max (read cur)))
12106                          (numberp (setq min (read cur))))
12107                     (set group (cons min max))
12108                   (set group nil)))
12109             (error 
12110              (progn 
12111                (if ignore-errors
12112                    (set group nil)
12113                  (ding) 
12114                  (gnus-message 3 "Warning - illegal active: %s"
12115                                (buffer-substring 
12116                                 (gnus-point-at-bol) (gnus-point-at-eol)))
12117                  nil))))
12118           (widen)
12119           (forward-line 1))))))
12120
12121 (defun gnus-groups-to-gnus-format (method &optional hashtb)
12122   ;; Parse a "groups" active file.
12123   (let ((cur (current-buffer))
12124         (hashtb (or hashtb 
12125                     (if method
12126                         gnus-active-hashtb
12127                       (setq gnus-active-hashtb
12128                             (gnus-make-hashtable 
12129                              (count-lines (point-min) (point-max)))))))
12130         (prefix (and method (not (eq method gnus-select-method))
12131                      (gnus-group-prefixed-name "" method))))
12132
12133     (goto-char (point-min))
12134     (condition-case ()
12135         ;; We split this into to separate loops, one with the prefix
12136         ;; and one without to speed the reading up somewhat.
12137         (if prefix
12138             (let (min max opoint)
12139               (while (not (eobp))
12140                 (read cur) (read cur)
12141                 (setq min (read cur)
12142                       max (read cur)
12143                       opoint (point))
12144                 (skip-chars-forward " \t")
12145                 (insert prefix)
12146                 (goto-char opoint)
12147                 (set (let ((obarray hashtb)) (read cur)) 
12148                      (cons min max))
12149                 (forward-line 1)))
12150           (let (min max opoint)
12151             (while (not (eobp))
12152               (if (= (following-char) ?2)
12153                   (progn
12154                     (read cur) (read cur)
12155                     (setq min (read cur)
12156                           max (read cur))
12157                     (set (let ((obarray hashtb)) (read cur)) 
12158                          (cons min max))))
12159               (forward-line 1))))
12160       (error 
12161        (progn (ding) (gnus-message 3 "Possible error in active file."))))))
12162
12163 (defun gnus-read-newsrc-file (&optional force)
12164   "Read startup file.
12165 If FORCE is non-nil, the .newsrc file is read."
12166   (setq gnus-current-startup-file (gnus-make-newsrc-file gnus-startup-file))
12167   ;; Reset variables that might be defined in the .newsrc.eld file.
12168   (let ((variables gnus-variable-list))
12169     (while variables
12170       (set (car variables) nil)
12171       (setq variables (cdr variables))))
12172   (let* ((newsrc-file gnus-current-startup-file)
12173          (quick-file (concat newsrc-file ".el")))
12174     (save-excursion
12175       ;; We always load the .newsrc.eld file. If always contains
12176       ;; much information that can not be gotten from the .newsrc
12177       ;; file (ticked articles, killed groups, foreign methods, etc.)
12178       (gnus-read-newsrc-el-file quick-file)
12179  
12180       (if (or force
12181               (and (file-newer-than-file-p newsrc-file quick-file)
12182                    (file-newer-than-file-p newsrc-file 
12183                                            (concat quick-file "d")))
12184               (not gnus-newsrc-alist))
12185           ;; We read the .newsrc file. Note that if there if a
12186           ;; .newsrc.eld file exists, it has already been read, and
12187           ;; the `gnus-newsrc-hashtb' has been created. While reading
12188           ;; the .newsrc file, Gnus will only use the information it
12189           ;; can find there for changing the data already read -
12190           ;; ie. reading the .newsrc file will not trash the data
12191           ;; already read (except for read articles).
12192           (save-excursion
12193             (gnus-message 5 "Reading %s..." newsrc-file)
12194             (set-buffer (find-file-noselect newsrc-file))
12195             (buffer-disable-undo (current-buffer))
12196             (gnus-newsrc-to-gnus-format)
12197             (kill-buffer (current-buffer))
12198             (gnus-message 5 "Reading %s...done" newsrc-file))))))
12199
12200 (defun gnus-read-newsrc-el-file (file)
12201   (let ((ding-file (concat file "d")))
12202     ;; We always, always read the .eld file.
12203     (gnus-message 5 "Reading %s..." ding-file)
12204     (let (gnus-newsrc-assoc)
12205       (condition-case nil
12206           (load ding-file t t t)
12207         (error nil))
12208       (and gnus-newsrc-assoc (setq gnus-newsrc-alist gnus-newsrc-assoc)))
12209     (let ((inhibit-quit t))
12210       (gnus-uncompress-newsrc-assoc))
12211     (gnus-make-hashtable-from-newsrc-alist)
12212     (if (not (file-newer-than-file-p file ding-file))
12213         ()
12214       ;; Old format quick file
12215       (gnus-message 5 "Reading %s..." file)
12216       ;; The .el file is newer than the .eld file, so we read that one
12217       ;; as well. 
12218       (gnus-read-old-newsrc-el-file file))))
12219
12220 ;; Parse the old-style quick startup file
12221 (defun gnus-read-old-newsrc-el-file (file)
12222   (let (newsrc killed marked group g m len info)
12223     (prog1
12224         (let ((gnus-killed-assoc nil)
12225               gnus-marked-assoc gnus-newsrc-alist gnus-newsrc-assoc)
12226           (prog1
12227               (condition-case nil
12228                   (load file t t t)
12229                 (error nil))
12230             (setq newsrc gnus-newsrc-assoc
12231                   killed gnus-killed-assoc
12232                   marked gnus-marked-assoc)))
12233       (setq gnus-newsrc-alist nil)
12234       (while newsrc
12235         (setq group (car newsrc))
12236         (let ((info (nth 2 (gnus-gethash (car group) gnus-newsrc-hashtb))))
12237           (if info
12238               (progn
12239                 (setcar (nthcdr 2 info) (cdr (cdr group)))
12240                 (setcar (cdr info)
12241                         (if (nth 1 group) gnus-level-default-subscribed 
12242                           gnus-level-default-unsubscribed))
12243                 (setq gnus-newsrc-alist (cons info gnus-newsrc-alist)))
12244             (setq gnus-newsrc-alist
12245                   (cons 
12246                    (setq info
12247                          (list (car group)
12248                                (if (nth 1 group) gnus-level-default-subscribed
12249                                  gnus-level-default-unsubscribed) 
12250                                (cdr (cdr group))))
12251                    gnus-newsrc-alist)))
12252           (if (setq m (assoc (car group) marked))
12253             (setcdr (cdr (cdr info)) (cons (list (cons 'tick (cdr m))) nil))))
12254         (setq newsrc (cdr newsrc)))
12255       (setq newsrc killed)
12256       (while newsrc
12257         (setcar newsrc (car (car newsrc)))
12258         (setq newsrc (cdr newsrc)))
12259       (setq gnus-killed-list killed))
12260     (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist))
12261     (gnus-make-hashtable-from-newsrc-alist)))
12262       
12263 (defun gnus-make-newsrc-file (file)
12264   "Make server dependent file name by catenating FILE and server host name."
12265   (let* ((file (expand-file-name file nil))
12266          (real-file (concat file "-" (nth 1 gnus-select-method))))
12267     (if (file-exists-p real-file)
12268         real-file file)))
12269
12270 (defun gnus-uncompress-newsrc-assoc ()
12271   ;; Uncompress all lists of marked articles in the newsrc assoc.
12272   (let ((newsrc gnus-newsrc-alist)
12273         marked)
12274     (while newsrc
12275       (if (not (setq marked (nth 3 (car newsrc))))
12276           ()
12277         (while marked
12278           (or (eq 'score (car (car marked)))
12279               (eq 'bookmark (car (car marked)))
12280               (eq 'killed (car (car marked)))
12281               (setcdr (car marked) (gnus-uncompress-range (cdr (car marked)))))
12282           (setq marked (cdr marked))))
12283       (setq newsrc (cdr newsrc)))))
12284
12285 (defun gnus-compress-newsrc-assoc ()
12286   ;; Compress all lists of marked articles in the newsrc assoc.
12287   (let ((newsrc gnus-newsrc-alist)
12288         marked)
12289     (while newsrc
12290       (if (not (setq marked (nth 3 (car newsrc))))
12291           ()
12292         (while marked
12293           (or (eq 'score (car (car marked)))
12294               (eq 'bookmark (car (car marked)))
12295               (eq 'killed (car (car marked)))
12296               (setcdr (car marked) 
12297                       (gnus-compress-sequence (sort (cdr (car marked)) '<) t)))
12298           (setq marked (cdr marked))))
12299       (setq newsrc (cdr newsrc)))))
12300
12301 (defun gnus-newsrc-to-gnus-format ()
12302   (setq gnus-newsrc-options "")
12303   (setq gnus-newsrc-options-n nil)
12304
12305   (or gnus-active-hashtb
12306       (setq gnus-active-hashtb (make-vector 4095 0)))
12307   (let ((buf (current-buffer))
12308         (already-read (> (length gnus-newsrc-alist) 1))
12309         group level subscribed info options-symbol newsrc
12310         symbol reads num1)
12311     (goto-char (point-min))
12312     ;; We intern the symbol `options' in the active hashtb so that we
12313     ;; can `eq' against it later.
12314     (setq options-symbol (intern "options" gnus-active-hashtb))
12315   
12316     (while (not (eobp))
12317       ;; We first read the first word on the line by narrowing and
12318       ;; then reading into `gnus-active-hashtb'.  Most groups will
12319       ;; already exist in that hashtb, so this will save some string
12320       ;; space.
12321       (narrow-to-region
12322        (point)
12323        (progn (skip-chars-forward "^ \t!:\n") (point)))
12324       (goto-char (point-min))
12325       (setq symbol 
12326             (and (/= (point-min) (point-max))
12327                  (let ((obarray gnus-active-hashtb)) (read buf))))
12328       (widen)
12329       ;; Now, the symbol we have read is either `options' or a group
12330       ;; name.  If it is an options line, we just add it to a string. 
12331       (cond 
12332        ((eq symbol options-symbol)
12333         (setq gnus-newsrc-options
12334               ;; This concatting is quite inefficient, but since our
12335               ;; thorough studies show that approx 99.37% of all
12336               ;; .newsrc files only contain a single options line, we
12337               ;; don't give a damn, frankly, my dear.
12338               (concat gnus-newsrc-options
12339                       (buffer-substring 
12340                        (gnus-point-at-bol)
12341                        ;; Options may continue on the next line.
12342                        (or (and (re-search-forward "^[^ \t]" nil 'move)
12343                                 (progn (beginning-of-line) (point)))
12344                            (point))))))
12345        (symbol
12346         ;; It was a group name.
12347         (setq subscribed (= (following-char) ?:)
12348               group (symbol-name symbol)
12349               reads nil)
12350         (if (eolp)
12351             ;; If the line ends here, this is clearly a buggy line, so
12352             ;; we put point a the beginning of line and let the cond
12353             ;; below do the error handling.
12354             (beginning-of-line)
12355           ;; We skip to the beginning of the ranges.
12356           (skip-chars-forward "!: \t"))
12357         ;; We are now at the beginning of the list of read articles.
12358         ;; We read them range by range.
12359         (while
12360             (cond 
12361              ((looking-at "[0-9]+")
12362               ;; We narrow and read a number instead of buffer-substring/
12363               ;; string-to-int because it's faster. narrow/widen is
12364               ;; faster than save-restriction/narrow, and save-restriction
12365               ;; produces a garbage object.
12366               (setq num1 (progn
12367                            (narrow-to-region (match-beginning 0) (match-end 0))
12368                            (read buf)))
12369               (widen)
12370               ;; If the next character is a dash, then this is a range.
12371               (if (= (following-char) ?-)
12372                   (progn
12373                     ;; We read the upper bound of the range.
12374                     (forward-char 1)
12375                     (if (not (looking-at "[0-9]+"))
12376                         ;; This is a buggy line, by we pretend that
12377                         ;; it's kinda OK. Perhaps the user should be
12378                         ;; dinged? 
12379                         (setq reads (cons num1 reads))
12380                       (setq reads 
12381                             (cons 
12382                              (cons num1 (progn
12383                                           (narrow-to-region (match-beginning 0) 
12384                                                             (match-end 0))
12385                                           (read buf)))
12386                              reads))
12387                       (widen)))
12388                 ;; It was just a simple number, so we add it to the
12389                 ;; list of ranges.
12390                 (setq reads (cons num1 reads)))
12391               ;; If the next char in ?\n, then we have reached the end
12392               ;; of the line and return nil.
12393               (/= (following-char) ?\n))
12394              ((= (following-char) ?\n)
12395               ;; End of line, so we end.
12396               nil)
12397              (t
12398               ;; Not numbers and not eol, so this might be a buggy
12399               ;; line... 
12400               (or (eobp) ; If it was eob instead of ?\n, we allow it.
12401                   (progn
12402                     ;; The line was buggy.
12403                     (setq group nil)
12404                     (gnus-message 3 "Mangled line: %s" 
12405                                   (buffer-substring (gnus-point-at-bol) 
12406                                                     (gnus-point-at-eol)))
12407                     (ding)
12408                     (sit-for 1)))
12409               nil))
12410           ;; Skip past ", ". Spaces are illegal in these ranges, but
12411           ;; we allow them, because it's a common mistake to put a
12412           ;; space after the comma.
12413           (skip-chars-forward ", "))
12414
12415         ;; We have already read .newsrc.eld, so we gently update the
12416         ;; data in the hash table with the information we have just
12417         ;; read. 
12418         (if (not group)
12419             ()
12420           (let ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
12421                 level)
12422             (if info
12423                 ;; There is an entry for this file in the alist.
12424                 (progn
12425                   (setcar (nthcdr 2 info) (nreverse reads))
12426                   ;; We update the level very gently.  In fact, we
12427                   ;; only change it if there's been a status change
12428                   ;; from subscribed to unsubscribed, or vice versa.
12429                   (setq level (nth 1 info))
12430                   (cond ((and (<= level gnus-level-subscribed)
12431                               (not subscribed))
12432                          (setq level (if reads
12433                                          gnus-level-default-unsubscribed 
12434                                        (1+ gnus-level-default-unsubscribed))))
12435                         ((and (> level gnus-level-subscribed) subscribed)
12436                          (setq level gnus-level-default-subscribed)))
12437                   (setcar (cdr info) level))
12438               ;; This is a new group.
12439               (setq info (list group 
12440                                (if subscribed
12441                                    gnus-level-default-subscribed 
12442                                  (if reads
12443                                      (1+ gnus-level-subscribed)
12444                                    gnus-level-default-unsubscribed))
12445                                (nreverse reads))))
12446             (setq newsrc (cons info newsrc))))))
12447       (forward-line 1))
12448     
12449     (setq newsrc (nreverse newsrc))
12450
12451     (if (not already-read)
12452         ()
12453       ;; We now have two newsrc lists - `newsrc', which is what we
12454       ;; have read from .newsrc, and `gnus-newsrc-alist', which is
12455       ;; what we've read from .newsrc.eld. We have to merge these
12456       ;; lists. We do this by "attaching" any (foreign) groups in the
12457       ;; gnus-newsrc-alist to the (native) group that precedes them. 
12458       (let ((rc (cdr gnus-newsrc-alist))
12459             (prev gnus-newsrc-alist)
12460             entry mentry)
12461         (while rc
12462           (or (null (nth 4 (car rc))) ; It's a native group.
12463               (assoc (car (car rc)) newsrc) ; It's already in the alist.
12464               (if (setq entry (assoc (car (car prev)) newsrc))
12465                   (setcdr (setq mentry (memq entry newsrc))
12466                           (cons (car rc) (cdr mentry)))
12467                 (setq newsrc (cons (car rc) newsrc))))
12468           (setq prev rc
12469                 rc (cdr rc)))))
12470
12471     (setq gnus-newsrc-alist newsrc)
12472     ;; We make the newsrc hashtb.
12473     (gnus-make-hashtable-from-newsrc-alist)
12474
12475     ;; Finally, if we read some options lines, we parse them.
12476     (or (string= gnus-newsrc-options "")
12477         (gnus-newsrc-parse-options gnus-newsrc-options))))
12478
12479 ;; Parse options lines to find "options -n !all rec.all" and stuff.
12480 ;; The return value will be a list on the form
12481 ;; ((regexp1 . ignore)
12482 ;;  (regexp2 . subscribe)...)
12483 ;; When handling new newsgroups, groups that match a `ignore' regexp
12484 ;; will be ignored, and groups that match a `subscribe' regexp will be
12485 ;; subscribed. A line like
12486 ;; options -n !all rec.all
12487 ;; will lead to a list that looks like
12488 ;; (("^rec\\..+" . subscribe) 
12489 ;;  ("^.+" . ignore))
12490 ;; So all "rec.*" groups will be subscribed, while all the other
12491 ;; groups will be ignored. Note that "options -n !all rec.all" is very
12492 ;; different from "options -n rec.all !all". 
12493 (defun gnus-newsrc-parse-options (options)
12494   (let (out eol)
12495     (save-excursion
12496       (gnus-set-work-buffer)
12497       (insert (regexp-quote options))
12498       ;; First we treat all continuation lines.
12499       (goto-char (point-min))
12500       (while (re-search-forward "\n[ \t]+" nil t)
12501         (replace-match " " t t))
12502       ;; Then we transform all "all"s into ".+"s.
12503       (goto-char (point-min))
12504       (while (re-search-forward "\\ball\\b" nil t)
12505         (replace-match ".+" t t))
12506       (goto-char (point-min))
12507       ;; We remove all other options than the "-n" ones.
12508       (while (re-search-forward "[ \t]-[^n][^-]*" nil t)
12509         (replace-match " ")
12510         (forward-char -1))
12511       (goto-char (point-min))
12512
12513       ;; We are only interested in "options -n" lines - we
12514       ;; ignore the other option lines.
12515       (while (re-search-forward "[ \t]-n" nil t)
12516         (setq eol 
12517               (or (save-excursion
12518                     (and (re-search-forward "[ \t]-n" (gnus-point-at-eol) t)
12519                          (- (point) 2)))
12520                   (gnus-point-at-eol)))
12521         ;; Search for all "words"...
12522         (while (re-search-forward "[^ \t,\n-]+" eol t)
12523           (if (= (char-after (match-beginning 0)) ?!)
12524               ;; If the word begins with a bang (!), this is a "not"
12525               ;; spec. We put this spec (minus the bang) and the
12526               ;; symbol `ignore' into the list.
12527               (setq out (cons (cons (concat 
12528                                      "^" (buffer-substring 
12529                                           (1+ (match-beginning 0))
12530                                           (match-end 0)))
12531                                     'ignore) out))
12532             ;; There was no bang, so this is a "yes" spec.
12533             (setq out (cons (cons (concat 
12534                                    "^" (buffer-substring (match-beginning 0)
12535                                                          (match-end 0)))
12536                                   'subscribe) out)))))
12537     
12538       (setq gnus-newsrc-options-n out))))
12539                
12540
12541 (defun gnus-save-newsrc-file ()
12542   "Save .newsrc file."
12543   ;; Note: We cannot save .newsrc file if all newsgroups are removed
12544   ;; from the variable gnus-newsrc-alist.
12545   (and (or gnus-newsrc-alist gnus-killed-list)
12546        gnus-current-startup-file
12547        (let ((make-backup-files t)
12548              (version-control nil)
12549              (require-final-newline t)) ;Don't ask even if requested.
12550          ;; You can stop or change version control of backup file.
12551          ;; Suggested by jason@violet.berkeley.edu.
12552          (run-hooks 'gnus-save-newsrc-hook)
12553          (save-excursion
12554            (if (or (not gnus-dribble-buffer)
12555                    (not (buffer-name gnus-dribble-buffer))
12556                    (zerop (save-excursion
12557                             (set-buffer gnus-dribble-buffer)
12558                             (buffer-size))))
12559                (gnus-message 4 "(No changes need to be saved)")
12560              (if gnus-save-newsrc-file
12561                  (progn
12562                    (gnus-message 5 "Saving %s..." gnus-current-startup-file)
12563                    ;; Make backup file of master newsrc.
12564                    (gnus-gnus-to-newsrc-format)
12565                    (gnus-message 5 "Saving %s...done"
12566                                  gnus-current-startup-file)))
12567              ;; Quickly loadable .newsrc.
12568              (set-buffer (get-buffer-create " *Gnus-newsrc*"))
12569              (gnus-add-current-to-buffer-list)
12570              (buffer-disable-undo (current-buffer))
12571              (erase-buffer)
12572              (gnus-message 5 "Saving %s.eld..." gnus-current-startup-file)
12573              (gnus-gnus-to-quick-newsrc-format)
12574              (write-region 1 (point-max) 
12575                            (concat gnus-current-startup-file ".eld") 
12576                            nil 'nomesg)
12577              (kill-buffer (current-buffer))
12578              (gnus-message 5 "Saving %s.eld...done" gnus-current-startup-file)
12579              (gnus-dribble-delete-file))))))
12580
12581 (defun gnus-gnus-to-quick-newsrc-format ()
12582   "Insert Gnus variables such as gnus-newsrc-alist in lisp format."
12583   (insert ";; (ding) Gnus startup file.\n")
12584   (insert ";; Never delete this file - touch .newsrc instead to force Gnus\n")
12585   (insert ";; to read .newsrc.\n")
12586   (let ((variables gnus-variable-list)
12587         (inhibit-quit t)
12588         (gnus-newsrc-alist (cdr gnus-newsrc-alist))
12589         variable)
12590     ;; insert lisp expressions.
12591     (gnus-compress-newsrc-assoc)
12592     (while variables
12593       (setq variable (car variables))
12594       (and (boundp variable)
12595            (symbol-value variable)
12596            (or gnus-save-killed-list (not (eq variable 'gnus-killed-list)))
12597            (insert "(setq " (symbol-name variable) " '"
12598                    (prin1-to-string (symbol-value variable))
12599                    ")\n"))
12600       (setq variables (cdr variables)))
12601     (gnus-uncompress-newsrc-assoc)))
12602
12603
12604 (defun gnus-gnus-to-newsrc-format ()
12605   ;; Generate and save the .newsrc file.
12606   (let ((newsrc (cdr gnus-newsrc-alist))
12607         info ranges range)
12608     (save-excursion
12609       (set-buffer (create-file-buffer gnus-startup-file))
12610       (buffer-disable-undo (current-buffer))
12611       (erase-buffer)
12612       ;; Write options.
12613       (if gnus-newsrc-options (insert gnus-newsrc-options))
12614       ;; Write subscribed and unsubscribed.
12615       (while newsrc
12616         (setq info (car newsrc))
12617         (if (not (nth 4 info))          ;Don't write foreign groups to .newsrc.
12618             (progn
12619               (insert (car info) (if (> (nth 1 info) gnus-level-subscribed)
12620                                      "!" ":"))
12621               (if (setq ranges (nth 2 info))
12622                   (progn
12623                     (insert " ")
12624                     (if (not (listp (cdr ranges)))
12625                         (if (= (car ranges) (cdr ranges))
12626                             (insert (int-to-string (car ranges)))
12627                           (insert (int-to-string (car ranges)) "-" 
12628                                   (int-to-string (cdr ranges))))
12629                       (while ranges
12630                         (setq range (car ranges)
12631                               ranges (cdr ranges))
12632                         (if (or (atom range) (= (car range) (cdr range)))
12633                             (insert (int-to-string 
12634                                      (or (and (atom range) range) 
12635                                          (car range))))
12636                           (insert (int-to-string (car range)) "-"
12637                                   (int-to-string (cdr range))))
12638                         (if ranges (insert ","))))))
12639               (insert "\n")))
12640         (setq newsrc (cdr newsrc)))
12641       (write-region 1 (point-max) gnus-current-startup-file nil 'nomesg)
12642       (kill-buffer (current-buffer)))))
12643
12644 (defun gnus-read-all-descriptions-files ()
12645   (let ((methods (nconc (list gnus-select-method) 
12646                         gnus-secondary-select-methods)))
12647     (while methods
12648       (gnus-read-descriptions-file (car methods))
12649       (setq methods (cdr methods)))
12650     t))
12651
12652 (defun gnus-read-descriptions-file (&optional method)
12653   (let ((method (or method gnus-select-method)))
12654     ;; We create the hashtable whether we manage to read the desc file
12655     ;; to avoid trying to re-read after a failed read.
12656     (or gnus-description-hashtb
12657         (setq gnus-description-hashtb 
12658               (gnus-make-hashtable (length gnus-active-hashtb))))
12659     ;; Mark this method's desc file as read.
12660     (gnus-sethash (gnus-group-prefixed-name "" method) "Has read"
12661                   gnus-description-hashtb)
12662
12663     (gnus-message 5 "Reading descriptions file via %s..." (car method))
12664     (cond 
12665      ((not (or (gnus-server-opened method)
12666                (gnus-open-server method)))
12667       (gnus-message 1 "Couldn't open server")
12668       nil)
12669      ((not (gnus-request-list-newsgroups method))
12670       (gnus-message 1 "Couldn't read newsgroups descriptions")
12671       nil)
12672      (t
12673       (let (group)
12674         (save-excursion
12675           (save-restriction
12676             (set-buffer nntp-server-buffer)
12677             (goto-char (point-min))
12678             (if (or (search-forward "\n.\n" nil t)
12679                     (goto-char (point-max)))
12680                 (progn
12681                   (beginning-of-line)
12682                   (narrow-to-region (point-min) (point))))
12683             (goto-char (point-min))
12684             (while (not (eobp))
12685               ;; If we get an error, we set group to 0, which is not a
12686               ;; symbol... 
12687               (setq group 
12688                     (condition-case ()
12689                         (let ((obarray gnus-description-hashtb))
12690                           ;; Group is set to a symbol interned in this
12691                           ;; hash table.
12692                           (read nntp-server-buffer))
12693                       (error 0)))
12694               (skip-chars-forward " \t")
12695               ;; ... which leads to this line being effectively ignored.
12696               (and (symbolp group)
12697                    (set group (buffer-substring 
12698                                (point) (progn (end-of-line) (point)))))
12699               (forward-line 1))))
12700         (gnus-message 5 "Reading descriptions file...done")
12701         t)))))
12702
12703 (defun gnus-group-get-description (group)
12704   ;; Get the description of a group by sending XGTITLE to the server.
12705   (and (gnus-request-group-description group)
12706        (save-excursion
12707          (set-buffer nntp-server-buffer)
12708          (goto-char (point-min))
12709          (and (looking-at "[^ \t]+[ \t]+\\(.*\\)")
12710               (buffer-substring (match-beginning 1) (match-end 1))))))
12711
12712 ;;;
12713 ;;; Server
12714 ;;;
12715
12716 (defvar gnus-server-mode-hook nil
12717   "Hook run in `gnus-server-mode' buffers.")
12718
12719 (defconst gnus-server-line-format "     {%(%h:%w%)}\n"
12720   "Format of server lines.
12721 It works along the same lines as a normal formatting string,
12722 with some simple extensions.")
12723
12724 (defvar gnus-server-mode-line-format "(ding) List of servers"
12725   "The format specification for the server mode line.")
12726
12727 (defconst gnus-server-line-format-alist
12728   (list (list ?h 'how ?s)
12729         (list ?n 'name ?s)
12730         (list ?w 'where ?s)
12731         ))
12732
12733 (defconst gnus-server-mode-line-format-alist 
12734   (list (list ?S 'news-server ?s)
12735         (list ?M 'news-method ?s)
12736         (list ?u 'user-defined ?s)))
12737
12738 (defvar gnus-server-line-format-spec nil)
12739 (defvar gnus-server-mode-line-format-spec nil)
12740 (defvar gnus-server-killed-servers nil)
12741
12742 (defvar gnus-server-mode-map nil)
12743 (put 'gnus-server-mode 'mode-class 'special)
12744
12745 (if gnus-server-mode-map
12746     nil
12747   (setq gnus-server-mode-map (make-sparse-keymap))
12748   (suppress-keymap gnus-server-mode-map)
12749   (define-key gnus-server-mode-map " " 'gnus-server-read-server)
12750   (define-key gnus-server-mode-map "\r" 'gnus-server-read-server)
12751   (define-key gnus-server-mode-map gnus-mouse-2 'gnus-server-pick-server)
12752   (define-key gnus-server-mode-map "q" 'gnus-server-exit)
12753   (define-key gnus-server-mode-map "l" 'gnus-server-list-servers)
12754   (define-key gnus-server-mode-map "k" 'gnus-server-kill-server)
12755   (define-key gnus-server-mode-map "y" 'gnus-server-yank-server)
12756   (define-key gnus-server-mode-map "c" 'gnus-server-copy-server)
12757   (define-key gnus-server-mode-map "a" 'gnus-server-add-server)
12758   (define-key gnus-server-mode-map "e" 'gnus-server-edit-server))
12759
12760 (defun gnus-server-mode ()
12761   "Major mode for listing and editing servers.
12762
12763 All normal editing commands are switched off.
12764 \\<gnus-server-mode-map>
12765
12766 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]'). 
12767
12768 The following commands are available:
12769
12770 \\{gnus-server-mode-map}"
12771   (interactive)
12772   (if gnus-visual (gnus-server-make-menu-bar))
12773   (kill-all-local-variables)
12774   (setq mode-line-modified "-- ")
12775   (make-local-variable 'mode-line-format)
12776   (setq mode-line-format (copy-sequence mode-line-format))
12777   (and (equal (nth 3 mode-line-format) "   ")
12778        (setcar (nthcdr 3 mode-line-format) ""))
12779   (setq major-mode 'gnus-server-mode)
12780   (setq mode-name "Server")
12781 ;  (gnus-group-set-mode-line)
12782   (setq mode-line-process nil)
12783   (use-local-map gnus-server-mode-map)
12784   (buffer-disable-undo (current-buffer))
12785   (setq truncate-lines t)
12786   (setq buffer-read-only t)
12787   (run-hooks 'gnus-server-mode-hook))
12788
12789 (defun gnus-server-insert-server-line (sformat name method)
12790   (let* ((sformat (or sformat gnus-server-line-format-spec))
12791          (how (car method))
12792          (where (nth 1 method))
12793          b)
12794     (beginning-of-line)
12795     (setq b (point))
12796     ;; Insert the text.
12797     (insert (eval sformat))
12798     (add-text-properties 
12799      b (1+ b) (list 'gnus-server (intern name)))))
12800
12801 (defun gnus-server-setup-buffer ()
12802   (if (get-buffer gnus-server-buffer)
12803       ()
12804     (save-excursion
12805       (set-buffer (get-buffer-create gnus-server-buffer))
12806       (gnus-server-mode)
12807       (and gnus-carpal (gnus-carpal-setup-buffer 'server)))))
12808
12809 (defun gnus-server-prepare ()
12810   (setq gnus-server-mode-line-format-spec 
12811         (gnus-parse-format gnus-server-mode-line-format 
12812                            gnus-server-mode-line-format-alist))
12813   (setq gnus-server-line-format-spec 
12814         (gnus-parse-format gnus-server-line-format 
12815                            gnus-server-line-format-alist))
12816   (let ((alist gnus-server-alist)
12817         (buffer-read-only nil))
12818     (erase-buffer)
12819     (while alist
12820       (gnus-server-insert-server-line nil (car (car alist)) (cdr (car alist)))
12821       (setq alist (cdr alist))))
12822   (goto-char (point-min))
12823   (gnus-server-position-cursor))
12824
12825 (defun gnus-server-server-name ()
12826   (let ((server (get-text-property (gnus-point-at-bol) 'gnus-server)))
12827     (and server (symbol-name server))))
12828
12829 (defalias 'gnus-server-position-cursor 'gnus-goto-colon)
12830
12831 (defconst gnus-server-edit-buffer "*Gnus edit server*")
12832
12833 (defun gnus-server-update-server (server)
12834   (save-excursion
12835     (set-buffer gnus-server-buffer)
12836     (let ((buffer-read-only nil)
12837           (info (cdr (assoc server gnus-server-alist))))
12838       (gnus-dribble-enter 
12839        (concat "(gnus-server-set-info \"" server "\" '"
12840                (prin1-to-string info) ")"))
12841       ;; Buffer may be narrowed.
12842       (save-restriction
12843         (widen)
12844         (if (gnus-server-goto-server server)
12845             (delete-region (progn (beginning-of-line) (point))
12846                            (progn (forward-line 1) (point))))
12847         (let ((entry (assoc server gnus-server-alist)))
12848           (gnus-server-insert-server-line nil (car entry) (cdr entry))
12849           (gnus-server-position-cursor))))))
12850
12851 (defun gnus-server-set-info (server info)
12852   ;; Enter a select method into the virtual server alist.
12853   (gnus-dribble-enter 
12854    (concat "(gnus-server-set-info \"" server "\" '"
12855            (prin1-to-string info) ")"))
12856   (let* ((server (nth 1 info))
12857          (entry (assoc server gnus-server-alist)))
12858     (if entry (setcdr entry info)
12859       (setq gnus-server-alist
12860             (nconc gnus-server-alist (list (cons server info)))))))
12861
12862 (defun gnus-server-to-method (server)
12863   ;; Map virtual server names to select methods.
12864   (or (and (equal server "native") gnus-select-method)
12865       (cdr (assoc server gnus-server-alist))))
12866
12867 (defun gnus-server-extend-method (group method)
12868   ;; This function "extends" a virtual server.  If the server is
12869   ;; "hello", and the select method is ("hello" (my-var "something")) 
12870   ;; in the group "alt.alt", this will result in a new virtual server
12871   ;; called "helly+alt.alt".
12872   (let ((entry
12873          (gnus-copy-sequence 
12874           (if (equal (car method) "native") gnus-select-method
12875               (cdr (assoc (car method) gnus-server-alist))))))
12876     (setcar (cdr entry) (concat (nth 1 entry) "+" group))
12877     (nconc entry (cdr method))))
12878
12879 (defun gnus-server-get-method (group method)
12880   ;; Input either a server name, and extended server name, or a
12881   ;; select method, and return a select method. 
12882   (cond ((stringp method)
12883          (gnus-server-to-method method))
12884         ((stringp (car method))
12885          (gnus-server-extend-method group method))
12886         (t
12887          (gnus-server-add-address method))))
12888
12889 (defun gnus-server-add-address (method)
12890   (let ((method-name (symbol-name (car method))))
12891     (if (and (memq 'address (assoc method-name gnus-valid-select-methods))
12892              (not (assq (intern (concat method-name "-address")) method)))
12893         (append method (list (list (intern (concat method-name "-address"))
12894                                    (nth 1 method))))
12895       method)))
12896
12897 (defun gnus-server-equal (s1 s2)
12898   (or (equal s1 s2)
12899       (and (= (length s1) (length s2))
12900            (progn
12901              (while (and s1 (member (car s1) s2))
12902                (setq s1 (cdr s1)))
12903              (null s1)))))
12904
12905 ;;; Interactive server functions.
12906
12907 (defun gnus-server-kill-server (server)
12908   "Kill the server on the current line."
12909   (interactive (list (gnus-server-server-name)))
12910   (or (gnus-server-goto-server server)
12911       (if server (error "No such server: %s" server)
12912         (error "No server on the current line")))
12913   (let ((buffer-read-only nil))
12914     (delete-region (progn (beginning-of-line) (point))
12915                    (progn (forward-line 1) (point))))
12916   (setq gnus-server-killed-servers 
12917         (cons (assoc server gnus-server-alist) gnus-server-killed-servers))
12918   (setq gnus-server-alist (delq (car gnus-server-killed-servers)
12919                                 gnus-server-alist))
12920   (gnus-server-position-cursor))
12921
12922 (defun gnus-server-yank-server ()
12923   "Yank the previously killed server."
12924   (interactive)
12925   (or gnus-server-killed-servers
12926       (error "No killed servers to be yanked"))
12927   (let ((alist gnus-server-alist)
12928         (server (gnus-server-server-name))
12929         (killed (car gnus-server-killed-servers)))
12930     (if (not server) 
12931         (setq gnus-server-alist (nconc gnus-server-alist (list killed)))
12932       (if (string= server (car (car gnus-server-alist)))
12933           (setq gnus-server-alist (cons killed gnus-server-alist))
12934         (while (and (cdr alist)
12935                     (not (string= server (car (car (cdr alist))))))
12936           (setq alist (cdr alist)))
12937         (setcdr alist (cons killed (cdr alist)))))
12938     (gnus-server-update-server (car killed))
12939     (setq gnus-server-killed-servers (cdr gnus-server-killed-servers))
12940     (gnus-server-position-cursor)))
12941
12942 (defun gnus-server-exit ()
12943   "Return to the group buffer."
12944   (interactive)
12945   (kill-buffer (current-buffer))
12946   (switch-to-buffer gnus-group-buffer))
12947
12948 (defun gnus-server-list-servers ()
12949   "List all available servers."
12950   (interactive)
12951   (let ((cur (gnus-server-server-name)))
12952     (gnus-server-prepare)
12953     (if cur (gnus-server-goto-server cur)
12954       (goto-char (point-max))
12955       (forward-line -1))
12956     (gnus-server-position-cursor)))
12957
12958 (defun gnus-server-copy-server (from to)
12959   (interactive
12960    (list
12961     (or (gnus-server-server-name)
12962         (error "No server on the current line"))
12963     (read-string "Copy to: ")))
12964   (or from (error "No server on current line"))
12965   (or (and to (not (string= to ""))) (error "No name to copy to"))
12966   (and (assoc to gnus-server-alist) (error "%s already exists" to))
12967   (or (assoc from gnus-server-alist) 
12968       (error "%s: no such server" from))
12969   (let ((to-entry (gnus-copy-sequence (assoc from gnus-server-alist))))
12970     (setcar to-entry to)
12971     (setcar (nthcdr 2 to-entry) to)
12972     (setq gnus-server-killed-servers 
12973           (cons to-entry gnus-server-killed-servers))
12974     (gnus-server-yank-server)))
12975
12976 (defun gnus-server-add-server (how where)
12977   (interactive 
12978    (list (intern (completing-read "Server method: "
12979                                   gnus-valid-select-methods nil t))
12980          (read-string "Server name: ")))
12981   (setq gnus-server-killed-servers 
12982         (cons (list where how where) gnus-server-killed-servers))
12983   (gnus-server-yank-server))
12984
12985 (defun gnus-server-goto-server (server)
12986   "Jump to a server line."
12987   (interactive
12988    (list (completing-read "Goto server: " gnus-server-alist nil t)))
12989   (let ((to (text-property-any (point-min) (point-max) 
12990                                'gnus-server (intern server))))
12991     (and to
12992          (progn
12993            (goto-char to) 
12994            (gnus-server-position-cursor)))))
12995
12996 (defun gnus-server-edit-server (server)
12997   "Edit the server on the current line."
12998   (interactive (list (gnus-server-server-name)))
12999   (or server
13000       (error "No server on current line"))
13001   (let ((winconf (current-window-configuration)))
13002     (get-buffer-create gnus-server-edit-buffer)
13003     (gnus-configure-windows 'edit-server)
13004     (gnus-add-current-to-buffer-list)
13005     (emacs-lisp-mode)
13006     (make-local-variable 'gnus-prev-winconf)
13007     (setq gnus-prev-winconf winconf)
13008     (use-local-map (copy-keymap (current-local-map)))
13009     (let ((done-func '(lambda () 
13010                         "Exit editing mode and update the information."
13011                         (interactive)
13012                         (gnus-server-edit-server-done 'group))))
13013       (setcar (cdr (nth 4 done-func)) server)
13014       (local-set-key "\C-c\C-c" done-func))
13015     (erase-buffer)
13016     (insert ";; Type `C-c C-c' after you have edited the server.\n\n")
13017     (insert (pp-to-string (cdr (assoc server gnus-server-alist))))))
13018
13019 (defun gnus-server-edit-server-done (server)
13020   (interactive)
13021   (set-buffer (get-buffer-create gnus-server-edit-buffer))
13022   (goto-char (point-min))
13023   (let ((form (read (current-buffer)))
13024         (winconf gnus-prev-winconf))
13025     (gnus-server-set-info server form)
13026     (kill-buffer (current-buffer))
13027     (and winconf (set-window-configuration winconf))
13028     (set-buffer gnus-server-buffer)
13029     (gnus-server-update-server (gnus-server-server-name))
13030     (gnus-server-position-cursor)))
13031
13032 (defun gnus-server-read-server (server)
13033   "Browse a server."
13034   (interactive (list (gnus-server-server-name)))
13035   (gnus-browse-foreign-server (gnus-server-to-method server) (current-buffer)))
13036
13037 (defun gnus-mouse-pick-server (e)
13038   (interactive "e")
13039   (mouse-set-point e)
13040   (gnus-server-read-server (gnus-server-server-name)))
13041
13042 ;;;
13043 ;;; entry points into gnus-score.el
13044 ;;;
13045
13046 ;;; Finding score files. 
13047
13048 (defvar gnus-global-score-files nil
13049   "*List of global score files and directories.
13050 Set this variable if you want to use people's score files.  One entry
13051 for each score file or each score file directory.  Gnus will decide
13052 by itself what score files are applicable to which group.
13053
13054 Say you want to use the single score file
13055 \"/ftp.ifi.uio.no@ftp:/pub/larsi/ding/score/soc.motss.SCORE\" and all
13056 score files in the \"/ftp.some-where:/pub/score\" directory.
13057
13058  (setq gnus-global-score-files
13059        '(\"/ftp.ifi.uio.no:/pub/larsi/ding/score/soc.motss.SCORE\"
13060          \"/ftp.some-where:/pub/score\"))")
13061
13062 (defun gnus-score-score-files (group)
13063   "Return a list of all possible score files."
13064   ;; Search and set any global score files.
13065   (and gnus-global-score-files 
13066        (or gnus-internal-global-score-files
13067            (gnus-score-search-global-directories gnus-global-score-files)))
13068   ;; Fix the kill-file dir variable.
13069   (setq gnus-kill-files-directory 
13070         (file-name-as-directory
13071          (or gnus-kill-files-directory "~/News/")))
13072   ;; If we can't read it, there are no score files.
13073   (if (not (file-exists-p (expand-file-name gnus-kill-files-directory)))
13074       (setq gnus-score-file-list nil)
13075     (if (gnus-use-long-file-name 'not-score)
13076         ;; We want long file names.
13077         (if (or (not gnus-score-file-list)
13078                 (not (car gnus-score-file-list))
13079                 (gnus-file-newer-than gnus-kill-files-directory
13080                                       (car gnus-score-file-list)))
13081               (setq gnus-score-file-list 
13082                     (cons (nth 5 (file-attributes gnus-kill-files-directory))
13083                           (nreverse 
13084                            (directory-files 
13085                             gnus-kill-files-directory t 
13086                             (gnus-score-file-regexp))))))
13087       ;; We do not use long file names, so we have to do some
13088       ;; directory traversing.  
13089       (let ((mdir (length (expand-file-name gnus-kill-files-directory)))
13090             (suffixes (list gnus-score-file-suffix gnus-adaptive-file-suffix))
13091             dir files suffix)
13092         (while suffixes
13093           (setq dir (expand-file-name
13094                      (concat gnus-kill-files-directory
13095                              (gnus-replace-chars-in-string group ?. ?/))))
13096           (setq suffix (car suffixes)
13097                 suffixes (cdr suffixes))
13098           (if (file-exists-p (concat dir "/" suffix))
13099               (setq files (cons (concat dir "/" suffix) files)))
13100           (while (>= (1+ (length dir)) mdir)
13101             (and (file-exists-p (concat dir "/all/" suffix))
13102                  (setq files (cons (concat dir "/all/" suffix) files)))
13103             (string-match "/[^/]*$" dir)
13104             (setq dir (substring dir 0 (match-beginning 0)))))
13105         (setq gnus-score-file-list 
13106               (cons nil (nreverse files)))))
13107     (cdr gnus-score-file-list)))
13108
13109 (defun gnus-score-file-regexp ()
13110   (concat "\\(" gnus-score-file-suffix 
13111           "\\|" gnus-adaptive-file-suffix "\\)$"))
13112         
13113 (defun gnus-score-find-bnews (group)
13114   "Return a list of score files for GROUP.
13115 The score files are those files in the ~/News directory which matches
13116 GROUP using BNews sys file syntax."
13117   (let* ((sfiles (append (gnus-score-score-files group)
13118                          gnus-internal-global-score-files))
13119          (kill-dir (file-name-as-directory 
13120                     (expand-file-name gnus-kill-files-directory)))
13121          (klen (length kill-dir))
13122          ofiles not-match regexp)
13123     (save-excursion
13124       (set-buffer (get-buffer-create "*gnus score files*"))
13125       (buffer-disable-undo (current-buffer))
13126       ;; Go through all score file names and create regexp with them
13127       ;; as the source.  
13128       (while sfiles
13129         (erase-buffer)
13130         (insert (car sfiles))
13131         (goto-char (point-min))
13132         ;; First remove the suffix itself.
13133         (re-search-forward (concat "." (gnus-score-file-regexp)))
13134         (replace-match "" t t) 
13135         (goto-char (point-min))
13136         (if (looking-at (regexp-quote kill-dir))
13137             ;; If the file name was just "SCORE", `klen' is one character
13138             ;; too much.
13139             (delete-char (min (1- (point-max)) klen))
13140           (goto-char (point-max))
13141           (search-backward "/")
13142           (delete-region (1+ (point)) (point-min)))
13143         ;; If short file names were used, we have to translate slashes.
13144         (goto-char (point-min))
13145         (while (search-forward "/" nil t)
13146           (replace-match "." t t))
13147         ;; Translate "all" to ".*".
13148         (while (search-forward "all" nil t)
13149           (replace-match ".*" t t))
13150         (goto-char (point-min))
13151         ;; Deal with "not."s.
13152         (if (looking-at "not.")
13153             (progn
13154               (setq not-match t)
13155               (setq regexp (buffer-substring 5 (point-max))))
13156           (setq regexp (buffer-substring 1 (point-max)))
13157           (setq not-match nil))
13158         ;; Finally - if this resulting regexp matches the group name,
13159         ;; we add this score file to the list of score files
13160         ;; applicable to this group.
13161         (if (or (and not-match
13162                      (not (string-match regexp group)))
13163                 (and (not not-match)
13164                      (string-match regexp group)))
13165             (setq ofiles (cons (car sfiles) ofiles)))
13166         (setq sfiles (cdr sfiles)))
13167       (kill-buffer (current-buffer))
13168       ;; Slight kludge here - the last score file returned should be
13169       ;; the local score file, whether it exists or not. This is so
13170       ;; that any score commands the user enters will go to the right
13171       ;; file, and not end up in some global score file.
13172       (let ((localscore
13173              (expand-file-name
13174               (if (gnus-use-long-file-name 'not-score)
13175                   (concat gnus-kill-files-directory group "." 
13176                           gnus-score-file-suffix)
13177                 (concat gnus-kill-files-directory
13178                         (gnus-replace-chars-in-string group ?. ?/)
13179                         "/" gnus-score-file-suffix)))))
13180         (and (member localscore ofiles)
13181              (delete localscore ofiles))
13182         (setq ofiles (cons localscore ofiles)))
13183       (nreverse ofiles))))
13184
13185 (defun gnus-score-find-single (group)
13186   "Return list containing the score file for GROUP."
13187   (list (gnus-score-file-name group gnus-adaptive-file-suffix)
13188         (gnus-score-file-name group)))
13189
13190 (defun gnus-score-find-hierarchical (group)
13191   "Return list of score files for GROUP.
13192 This includes the score file for the group and all its parents."
13193   (let ((all (copy-sequence '(nil)))
13194         (start 0))
13195     (while (string-match "\\." group (1+ start))
13196       (setq start (match-beginning 0))
13197       (setq all (cons (substring group 0 start) all)))
13198     (setq all (cons group all))
13199     (nconc
13200      (mapcar (lambda (newsgroup)
13201                (gnus-score-file-name newsgroup gnus-adaptive-file-suffix))
13202              (setq all (nreverse all)))
13203      (mapcar 'gnus-score-file-name all))))
13204
13205 (defvar gnus-score-file-alist-cache nil)
13206
13207 (defun gnus-score-find-alist (group)
13208   "Return list of score files for GROUP.
13209 The list is determined from the variable gnus-score-file-alist."
13210   (let ((alist gnus-score-file-multiple-match-alist)
13211         score-files)
13212     ;; if this group has been seen before, return the cached entry
13213     (if (setq score-files (assoc group gnus-score-file-alist-cache))
13214         (cdr score-files)       ; ensures caching of groups with no matches
13215       ;; handle the multiple match alist
13216       (while alist
13217         (and (string-match (car (car alist)) group)
13218              (setq score-files
13219                    (nconc score-files (cdr (car alist)))))
13220         (setq alist (cdr alist)))
13221       (setq alist gnus-score-file-single-match-alist)
13222       ;; handle the single match alist
13223       (catch 'done
13224         (while alist
13225           (and (string-match (car (car alist)) group)
13226                ;; progn used just in case ("regexp") has no files
13227                ;; and score-files is still nil. -sj
13228                ;; this can be construed as a "stop searching here" feature :>
13229                ;; and used to simplify regexps in the single-alist 
13230                (progn
13231                  (setq score-files
13232                        (nconc score-files (cdr (car alist))))
13233                  (throw 'done nil)))
13234           (setq alist (cdr alist))))
13235       ;; cache the score files
13236       (setq gnus-score-file-alist-cache
13237             (cons (cons group score-files) gnus-score-file-alist-cache))
13238       score-files)))
13239
13240
13241 (defun gnus-possibly-score-headers (&optional trace)
13242   (let ((func gnus-score-find-score-files-function)
13243         score-files scores)
13244     (and func (not (listp func))
13245          (setq func (list func)))
13246     ;; Go through all the functions for finding score files (or actual
13247     ;; scores) and add them to a list.
13248     (setq score-files (gnus-score-find-alist gnus-newsgroup-name))
13249     (while func
13250       (and (symbolp (car func))
13251            (fboundp (car func))
13252            (setq score-files 
13253                  (nconc score-files (funcall (car func) gnus-newsgroup-name))))
13254       (setq func (cdr func)))
13255     (if score-files (gnus-score-headers score-files trace))))
13256
13257 (defun gnus-score-file-name (newsgroup &optional suffix)
13258   "Return the name of a score file for NEWSGROUP."
13259   (let ((suffix (or suffix gnus-score-file-suffix)))
13260     (cond  ((or (null newsgroup)
13261                 (string-equal newsgroup ""))
13262             ;; The global score file is placed at top of the directory.
13263             (expand-file-name 
13264              suffix (or gnus-kill-files-directory "~/News")))
13265            ((gnus-use-long-file-name 'not-score)
13266             ;; Append ".SCORE" to newsgroup name.
13267             (expand-file-name (concat newsgroup "." suffix)
13268                               (or gnus-kill-files-directory "~/News")))
13269            (t
13270             ;; Place "SCORE" under the hierarchical directory.
13271             (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
13272                                       "/" suffix)
13273                               (or gnus-kill-files-directory "~/News"))))))
13274
13275 (defun gnus-score-search-global-directories (files)
13276   "Scan all global score directories for score files."
13277   ;; Set the variable `gnus-internal-global-score-files' to all
13278   ;; available global score files.
13279   (interactive (list gnus-global-score-files))
13280   (let (out)
13281     (while files
13282       (if (string-match "/$" (car files))
13283           (setq out (nconc (directory-files 
13284                             (car files) t
13285                             (concat (gnus-score-file-regexp) "$"))))
13286         (setq out (cons (car files) out)))
13287       (setq files (cdr files)))
13288     (setq gnus-internal-global-score-files out)))
13289
13290 ;; Allow redefinition of Gnus functions.
13291
13292 (gnus-ems-redefine)
13293
13294 (provide 'gnus)
13295
13296 ;;; gnus.el ends here