*** empty log message ***
[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@ftp.hpc.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 t
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 non-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-nil, 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                   [faq 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     (edit-score ([summary 0.25]
691                  [edit-score 1.0 point]))
692     (post ([post 1.0 point]))
693     (reply ([article 0.5]
694             [mail 1.0 point]))
695     (mail-forward ([mail 1.0 point]))
696     (post-forward ([post 1.0 point]))
697     (reply-yank ([mail 1.0 point]))
698     (followup ([article 0.5]
699                [post 1.0 point]))
700     (followup-yank ([post 1.0 point])))
701   "Window configuration for all possible Gnus buffers.
702 This variable is a list of lists.  Each of these lists has a NAME and
703 a RULE.  The NAMEs are commonsense names like `group', which names a
704 rule used when displaying the group buffer; `summary', which names a
705 rule for what happens when you enter a group and do not display an
706 article buffer; and so on.  See the value of this variable for a
707 complete list of NAMEs.
708
709 Each RULE is a list of vectors.  The first element in this vector is
710 the name of the buffer to be displayed; the second element is the
711 percentage of the screen this buffer is to occupy (a number in the
712 0.0-0.99 range); the optional third element is `point', which should
713 be present to denote which buffer point is to go to after making this
714 buffer configuration.")
715
716 (defvar gnus-window-to-buffer
717   '((group . gnus-group-buffer)
718     (summary . gnus-summary-buffer)
719     (article . gnus-article-buffer)
720     (server . gnus-server-buffer)
721     (browse . "*Gnus Browse Server*")
722     (edit-group . gnus-group-edit-buffer)
723     (edit-server . gnus-server-edit-buffer)
724     (group-carpal . gnus-carpal-group-buffer)
725     (summary-carpal . gnus-carpal-summary-buffer)
726     (server-carpal . gnus-carpal-server-buffer)
727     (browse-carpal . gnus-carpal-browse-buffer)
728     (edit-score . gnus-score-edit-buffer)
729     (mail . gnus-mail-buffer)
730     (post . gnus-post-news-buffer)
731     (faq . gnus-faq-buffer))
732   "Mapping from short symbols to buffer names or buffer variables.")
733
734 (defvar gnus-carpal nil
735   "*If non-nil, display clickable icons.")
736
737 (defvar gnus-subscribe-newsgroup-method 'gnus-subscribe-zombies
738   "*Function called with a group name when new group is detected.
739 A few pre-made functions are supplied: `gnus-subscribe-randomly'
740 inserts new groups at the beginning of the list of groups;
741 `gnus-subscribe-alphabetically' inserts new groups in strict
742 alphabetic order; `gnus-subscribe-hierarchically' inserts new groups
743 in hierarchical newsgroup order; `gnus-subscribe-interactively' asks
744 for your decision.")
745
746 ;; Suggested by a bug report by Hallvard B Furuseth.
747 ;; <h.b.furuseth@usit.uio.no>. 
748 (defvar gnus-subscribe-options-newsgroup-method
749   (function gnus-subscribe-alphabetically)
750   "*This function is called to subscribe newsgroups mentioned on \"options -n\" lines.
751 If, for instance, you want to subscribe to all newsgroups in the
752 \"no\" and \"alt\" hierarchies, you'd put the following in your
753 .newsrc file:
754
755 options -n no.all alt.all
756
757 Gnus will the subscribe all new newsgroups in these hierarchies with
758 the subscription method in this variable.")
759
760 (defvar gnus-subscribe-hierarchical-interactive nil
761   "*If non-nil, Gnus will offer to subscribe hierarchically.
762 When a new hierarchy appears, Gnus will ask the user:
763
764 'alt.binaries': Do you want to subscribe to this hierarchy? ([d]ys):
765
766 If the user pressed `d', Gnus will descend the hierarchy, `y' will
767 subscribe to all newsgroups in the hierarchy and `s' will skip this
768 hierarchy in its entirety.")
769
770 (defvar gnus-group-sort-function 'gnus-group-sort-by-alphabet
771   "*Function used for sorting the group buffer.
772 This function will be called with group info entries as the arguments
773 for the groups to be sorted.  Pre-made functions include
774 `gnus-sort-by-alphabet', `gnus-sort-by-unread' and
775 `gnus-sort-by-level'")
776
777 ;; Mark variables suggested by Thomas Michanek
778 ;; <Thomas.Michanek@telelogic.se>. 
779 (defvar gnus-unread-mark ? 
780   "*Mark used for unread articles.")
781 (defvar gnus-ticked-mark ?!
782   "*Mark used for ticked articles.")
783 (defvar gnus-dormant-mark ??
784   "*Mark used for dormant articles.")
785 (defvar gnus-del-mark ?r
786   "*Mark used for del'd articles.")
787 (defvar gnus-read-mark ?R
788   "*Mark used for read articles.")
789 (defvar gnus-expirable-mark ?E
790   "*Mark used for expirable articles.")
791 (defvar gnus-killed-mark ?K
792   "*Mark used for killed articles.")
793 (defvar gnus-kill-file-mark ?X
794   "*Mark used for articles killed by kill files.")
795 (defvar gnus-low-score-mark ?Y
796   "*Mark used for articles with a low score.")
797 (defvar gnus-catchup-mark ?C
798   "*Mark used for articles that are caught up.")
799 (defvar gnus-replied-mark ?A
800   "*Mark used for articles that have been replied to.")
801 (defvar gnus-process-mark ?# 
802   "*Process mark.")
803 (defvar gnus-ancient-mark ?O
804   "*Mark used for ancient articles.")
805 (defvar gnus-canceled-mark ?G
806   "*Mark used for canceled articles.")
807 (defvar gnus-score-over-mark ?+
808   "*Score mark used for articles with high scores.")
809 (defvar gnus-score-below-mark ?-
810   "*Score mark used for articles with low scores.")
811 (defvar gnus-empty-thread-mark ? 
812   "*There is no thread under the article.")
813 (defvar gnus-not-empty-thread-mark ?=
814   "*There is a thread under the article.")
815 (defvar gnus-dummy-mark ?Z
816   "*This is a dummy article.")
817
818 (defvar gnus-view-pseudo-asynchronously nil
819   "*If non-nil, Gnus will view pseudo-articles asynchronously.")
820
821 (defvar gnus-view-pseudos nil
822   "*If `automatic', pseudo-articles will be viewed automatically.
823 If `not-confirm', pseudos will be viewed automatically, and the user
824 will not be asked to confirm the command.")
825
826 (defvar gnus-view-pseudos-separately t
827   "*If non-nil, one pseudo-article will be created for each file to be viewed.
828 If nil, all files that use the same viewing command will be given as a
829 list of parameters to that command.")
830
831 (defvar gnus-group-line-format "%M%S%p%5y: %(%g%)\n"
832   "*Format of group lines.
833 It works along the same lines as a normal formatting string,
834 with some simple extensions.
835
836 %M    Only marked articles (character, \"*\" or \" \")
837 %S    Whether the group is subscribed (character, \"U\", \"K\", \"Z\" or \" \")
838 %L    Level of subscribedness (integer)
839 %N    Number of unread articles (integer)
840 %I    Number of dormant articles (integer)
841 %i    Number of ticked and dormant (integer)
842 %T    Number of ticked articles (integer)
843 %R    Number of read articles (integer)
844 %t    Total number of articles (integer)
845 %y    Number of unread, unticked articles (integer)
846 %G    Group name (string)
847 %g    Qualified group name (string)
848 %D    Group description (string)
849 %s    Select method (string)
850 %o    Moderated group (char, \"m\")
851 %p    Process mark (char)
852 %O    Moderated group (string, \"(m)\" or \"\")
853 %n    Select from where (string)
854 %z    A string that look like `<%s:%n>' if a foreign select method is used
855 %u    User defined specifier. The next character in the format string should
856       be a letter.  Gnus will call the function gnus-user-format-function-X,
857       where X is the letter following %u. The function will be passed the
858       current header as argument. The function should return a string, which
859       will be inserted into the buffer just like information from any other
860       group specifier.
861
862 Text between %( and %) will be highlighted with `gnus-mouse-face' when
863 the mouse point move inside the area.  There can only be one such area.
864
865 Note that this format specification is not always respected. For
866 reasons of efficiency, when listing killed groups, this specification
867 is ignored altogether. If the spec is changed considerably, your
868 output may end up looking strange when listing both alive and killed
869 groups.
870
871 If you use %o or %O, reading the active file will be slower and quite
872 a bit of extra memory will be used. %D will also worsen performance.
873 Also note that if you change the format specification to include any
874 of these specs, you must probably re-start Gnus to see them go into
875 effect.") 
876
877 (defvar gnus-summary-line-format "%U%R%z%I%(%[%4L: %-20,20n%]%) %s\n"
878   "*The format specification of the lines in the summary buffer.
879
880 It works along the same lines as a normal formatting string,
881 with some simple extensions.
882
883 %N   Article number, left padded with spaces (string)
884 %S   Subject (string)
885 %s   Subject if it is at the root of a thread, and \"\" otherwise (string)
886 %n   Name of the poster (string)
887 %a   Extracted name of the poster (string)
888 %A   Extracted address of the poster (string)
889 %F   Contents of the From: header (string)
890 %x   Contents of the Xref: header (string)
891 %D   Date of the article (string)
892 %d   Date of the article (string) in DD-MMM format
893 %M   Message-id of the article (string)
894 %r   References of the article (string)
895 %c   Number of characters in the article (integer)
896 %L   Number of lines in the article (integer)
897 %I   Indentation based on thread level (a string of spaces)
898 %T   A string with two possible values: 80 spaces if the article
899      is on thread level two or larger and 0 spaces on level one
900 %R   \"R\" if this article has been replied to, \" \" otherwise (character)
901 %U   Status of this article (character, \"D\", \"K\", \"-\" or \" \")
902 %[   Opening bracket (character, \"[\" or \"<\")
903 %]   Closing bracket (character, \"]\" or \">\")
904 %>   Spaces of length thread-level (string)
905 %<   Spaces of length (- 20 thread-level) (string)
906 %i   Article score (number)
907 %z   Article zcore (character)
908 %t   Number of articles under the current thread (number).
909 %e   Whether the thread is empty or not (character).
910 %u   User defined specifier. The next character in the format string should
911      be a letter.  Gnus will call the function gnus-user-format-function-X,
912      where X is the letter following %u. The function will be passed the
913      current header as argument. The function should return a string, which
914      will be inserted into the summary just like information from any other
915      summary specifier.
916
917 Text between %( and %) will be highlighted with `gnus-mouse-face'
918 when the mouse point is placed inside the area.  There can only be one
919 such area.
920
921 The %U (status), %R (replied) and %z (zcore) specs have to be handled
922 with care. For reasons of efficiency, Gnus will compute what column
923 these characters will end up in, and \"hard-code\" that. This means that
924 it is illegal to have these specs after a variable-length spec. Well,
925 you might not be arrested, but your summary buffer will look strange,
926 which is bad enough.
927
928 The smart choice is to have these specs as for to the left as
929 possible. 
930
931 This restriction may disappear in later versions of Gnus.")
932
933 (defvar gnus-summary-dummy-line-format "*  :                          : %S\n"
934   "*The format specification for the dummy roots in the summary buffer.
935 It works along the same lines as a normal formatting string,
936 with some simple extensions.
937
938 %S  The subject")
939
940 (defvar gnus-summary-mode-line-format "(ding) %G/%A %Z"
941   "*The format specification for the summary mode line.")
942
943 (defvar gnus-article-mode-line-format "(ding) %G/%A %S"
944   "*The format specification for the article mode line.")
945
946 (defvar gnus-group-mode-line-format "(ding) List of groups   {%M:%S}  "
947   "*The format specification for the group mode line.")
948
949 (defvar gnus-valid-select-methods
950   '(("nntp" post address prompt-address)
951     ("nnspool" post)
952     ("nnvirtual" none virtual prompt-address) 
953     ("nnmbox" mail respool) 
954     ("nnml" mail respool)
955     ("nnmh" mail respool) 
956     ("nndir" none prompt-address address)
957     ("nneething" none prompt-address)
958     ("nndigest" none) 
959     ("nndoc" none prompt-address) 
960     ("nnbabyl" mail respool) 
961     ("nnkiboze" post virtual) 
962     ("nnsoup" post)
963     ("nnfolder" mail respool))
964   "An alist of valid select methods.
965 The first element of each list lists should be a string with the name
966 of the select method. The other elements may be be the category of
967 this method (ie. `post', `mail', `none' or whatever) or other
968 properties that this method has (like being respoolable).
969 If you implement a new select method, all you should have to change is
970 this variable. I think.")
971
972 (defvar gnus-updated-mode-lines '(group article summary)
973   "*List of buffers that should update their mode lines.
974 The list may contain the symbols `group', `article' and `summary'. If
975 the corresponding symbol is present, Gnus will keep that mode line
976 updated with information that may be pertinent. 
977 If this variable is nil, screen refresh may be quicker.")
978
979 ;; Added by Keinonen Kari <kk85613@cs.tut.fi>.
980 (defvar gnus-mode-non-string-length 21
981   "*Max length of mode-line non-string contents.
982 If this is nil, Gnus will take space as is needed, leaving the rest
983 of the modeline intact.")
984
985 (defvar gnus-mouse-face 'highlight
986   "*Face used for mouse highlighting in Gnus.
987 No mouse highlights will be done if `gnus-visual' is nil.")
988
989 (defvar gnus-summary-mark-below nil
990   "*Mark all articles with a score below this variable as read.
991 This variable is local to each summary buffer and usually set by the
992 score file.")  
993
994 (defvar gnus-thread-sort-functions '(gnus-thread-sort-by-number)
995   "*List of functions used for sorting threads in the summary buffer.
996 By default, threads are sorted by article number.
997
998 Each function takes two threads and return non-nil if the first thread
999 should be sorted before the other.  If you use more than one function,
1000 the primary sort function should be the last.
1001
1002 Ready-mady functions include `gnus-thread-sort-by-number',
1003 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
1004 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
1005 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').")
1006
1007 (defvar gnus-thread-score-function '+
1008   "*Function used for calculating the total score of a thread.
1009
1010 The function is called with the scores of the article and each
1011 subthread and should then return the score of the thread.
1012
1013 Some functions you can use are `+', `max', or `min'.")
1014
1015 (defvar gnus-options-subscribe nil
1016   "*All new groups matching this regexp will be subscribed unconditionally.
1017 Note that this variable deals only with new newsgroups.  This variable
1018 does not affect old newsgroups.")
1019
1020 (defvar gnus-options-not-subscribe nil
1021   "*All new groups matching this regexp will be ignored.
1022 Note that this variable deals only with new newsgroups.  This variable
1023 does not affect old (already subscribed) newsgroups.")
1024
1025 (defvar gnus-auto-expirable-newsgroups nil
1026   "*Groups in which to automatically mark read articles as expirable.
1027 If non-nil, this should be a regexp that should match all groups in
1028 which to perform auto-expiry.  This only makes sense for mail groups.")
1029
1030 (defvar gnus-hidden-properties '(invisible t intangible t)
1031   "Property list to use for hiding text.")
1032
1033 ;; Hooks.
1034
1035 (defvar gnus-group-mode-hook nil
1036   "*A hook for Gnus group mode.")
1037
1038 (defvar gnus-summary-mode-hook nil
1039   "*A hook for Gnus summary mode.
1040 This hook is run before any variables are set in the summary buffer.")
1041
1042 (defvar gnus-article-mode-hook nil
1043   "*A hook for Gnus article mode.")
1044
1045 (defun gnus-summary-prepare-exit-hook nil
1046   "*A hook called when preparing to exit from the summary buffer.
1047 It calls `gnus-summary-expire-articles' by default.")
1048 (add-hook 'gnus-summary-prepare-exit-hook 'gnus-summary-expire-articles)
1049
1050 (defun gnus-summary-exit-hook nil
1051   "*A hook called on exit from the summary buffer.")
1052
1053 (defvar gnus-open-server-hook nil
1054   "*A hook called just before opening connection to the news server.")
1055
1056 (defvar gnus-startup-hook nil
1057   "*A hook called at startup.
1058 This hook is called after Gnus is connected to the NNTP server.")
1059
1060 (defvar gnus-get-new-news-hook nil
1061   "*A hook run just before Gnus checks for new news.")
1062
1063 (defvar gnus-group-prepare-function 'gnus-group-prepare-flat
1064   "*A function that is called to generate the group buffer.
1065 The function is called with three arguments: The first is a number;
1066 all group with a level less or equal to that number should be listed,
1067 if the second is non-nil, empty groups should also be displayed. If
1068 the third is non-nil, it is a number. No groups with a level lower
1069 than this number should be displayed.
1070
1071 The only current function implemented is `gnus-group-prepare-flat'.")
1072
1073 (defvar gnus-group-prepare-hook nil
1074   "*A hook called after the group buffer has been generated.
1075 If you want to modify the group buffer, you can use this hook.")
1076
1077 (defvar gnus-summary-prepare-hook nil
1078   "*A hook called after the summary buffer has been generated.
1079 If you want to modify the summary buffer, you can use this hook.")
1080
1081 (defvar gnus-article-prepare-hook nil
1082   "*A hook called after an article has been prepared in the article buffer.
1083 If you want to run a special decoding program like nkf, use this hook.")
1084
1085 (defvar gnus-article-display-hook nil
1086   "*A hook called after the article is displayed in the article buffer.
1087 The hook is designed to change the contents of the article
1088 buffer. Typical functions that this hook may contain are
1089 `gnus-article-hide-headers' (hide selected headers),
1090 `gnus-article-maybe-highlight' (perform fancy article highlighting), 
1091 `gnus-article-hide-signature' (hide signature) and
1092 `gnus-article-treat-overstrike' (turn \"^H_\" into bold characters).")
1093 (add-hook 'gnus-article-display-hook 'gnus-article-hide-headers-if-wanted)
1094 (add-hook 'gnus-article-display-hook 'gnus-article-treat-overstrike)
1095
1096 (defvar gnus-article-x-face-command
1097   "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | xv -quit -"
1098   "String or function to be executed to display an X-Face header.
1099 If it is a string, the command will be executed in a sub-shell
1100 asynchronously. The compressed face will be piped to this command.") 
1101
1102 (defvar gnus-article-x-face-too-ugly nil
1103   "Regexp matching posters whose face shouldn't be shown automatically.")
1104
1105 (defvar gnus-select-group-hook nil
1106   "*A hook called when a newsgroup is selected.
1107
1108 If you'd like to simplify subjects like the
1109 `gnus-summary-next-same-subject' command does, you can use the
1110 following hook:
1111
1112  (setq gnus-select-group-hook
1113       (list
1114         (lambda ()
1115           (mapcar (lambda (header)
1116                      (header-set-subject
1117                       header
1118                       (gnus-simplify-subject
1119                        (header-subject header) 're-only)))
1120                   gnus-newsgroup-headers))))")
1121
1122 (defvar gnus-select-article-hook
1123   '(gnus-summary-show-thread)
1124   "*A hook called when an article is selected.
1125 The default hook shows conversation thread subtrees of the selected
1126 article automatically using `gnus-summary-show-thread'.")
1127
1128 (defvar gnus-apply-kill-hook '(gnus-apply-kill-file)
1129   "*A hook called to apply kill files to a group.
1130 This hook is intended to apply a kill file to the selected newsgroup.
1131 The function `gnus-apply-kill-file' is called by default.
1132
1133 Since a general kill file is too heavy to use only for a few
1134 newsgroups, I recommend you to use a lighter hook function. For
1135 example, if you'd like to apply a kill file to articles which contains
1136 a string `rmgroup' in subject in newsgroup `control', you can use the
1137 following hook:
1138
1139 \(setq gnus-apply-kill-hook
1140       (list
1141         (lambda ()
1142           (cond ((string-match \"control\" gnus-newsgroup-name)
1143                  (gnus-kill \"Subject\" \"rmgroup\")
1144                  (gnus-expunge \"X\"))))))")
1145
1146 (defvar gnus-visual-mark-article-hook 
1147   (list 'gnus-highlight-selected-summary)
1148   "*Hook run after selecting an article in the summary buffer.
1149 It is meant to be used for highlighting the article in some way.  It
1150 is not run if `gnus-visual' is nil.")
1151
1152 (defvar gnus-prepare-article-hook (list 'gnus-inews-insert-signature)
1153   "*A hook called after preparing body, but before preparing header headers.
1154 The default hook (`gnus-inews-insert-signature') inserts a signature
1155 file specified by the variable `gnus-signature-file'.")
1156
1157 (defvar gnus-exit-group-hook nil
1158   "*A hook called when exiting (not quitting) summary mode.")
1159
1160 (defvar gnus-suspend-gnus-hook nil
1161   "*A hook called when suspending (not exiting) Gnus.")
1162
1163 (defvar gnus-exit-gnus-hook nil
1164   "*A hook called when exiting Gnus.")
1165
1166 (defvar gnus-save-newsrc-hook nil
1167   "*A hook called when saving the newsrc file.")
1168
1169 (defvar gnus-summary-update-hook 
1170   (list 'gnus-summary-highlight-line)
1171   "*A hook called when a summary line is changed.
1172 The hook will not be called if `gnus-visual' is nil.
1173
1174 The default function `gnus-summary-highlight-line' will
1175 highlight the line according to the `gnus-summary-highlight'
1176 variable.")
1177
1178 (defvar gnus-mark-article-hook (list 'gnus-summary-mark-unread-as-read)
1179   "*A hook called when an article is selected for the first time.
1180 The hook is intended to mark an article as read (or unread)
1181 automatically when it is selected.")
1182
1183 ;; Remove any hilit infestation.
1184 (add-hook 'gnus-startup-hook
1185           (lambda ()
1186             (remove-hook 'gnus-summary-prepare-hook
1187                          'hilit-rehighlight-buffer-quietly)
1188             (remove-hook 'gnus-summary-prepare-hook 'hilit-install-line-hooks)
1189             (setq gnus-mark-article-hook '(gnus-summary-mark-unread-as-read))
1190             (remove-hook 'gnus-article-prepare-hook
1191                          'hilit-rehighlight-buffer-quietly)))
1192
1193
1194 \f
1195 ;; Internal variables
1196
1197 ;; Avoid highlighting in kill files.
1198 (defvar gnus-summary-inhibit-highlight nil)
1199 (defvar gnus-newsgroup-selected-overlay nil)
1200
1201 (defvar gnus-article-mode-map nil)
1202 (defvar gnus-dribble-buffer nil)
1203 (defvar gnus-headers-retrieved-by nil)
1204 (defvar gnus-article-reply nil)
1205 (defvar gnus-override-method nil)
1206 (defvar gnus-article-check-size nil)
1207
1208 (defvar gnus-current-score-file nil)
1209 (defvar gnus-internal-global-score-files nil)
1210 (defvar gnus-score-file-list nil)
1211
1212
1213 (defvar gnus-current-move-group nil)
1214
1215 (defvar gnus-newsgroup-dependencies nil)
1216 (defvar gnus-newsgroup-threads nil)
1217 (defvar gnus-newsgroup-async nil)
1218 (defconst gnus-group-edit-buffer "*Gnus edit newsgroup*")
1219
1220 (defvar gnus-newsgroup-adaptive nil)
1221
1222 (defvar gnus-summary-display-table nil)
1223
1224 (defconst gnus-group-line-format-alist
1225   (list (list ?M 'marked ?c)
1226         (list ?S 'subscribed ?c)
1227         (list ?L 'level ?d)
1228         (list ?N 'number ?s)
1229         (list ?I 'number-of-dormant ?d)
1230         (list ?T 'number-of-ticked ?d)
1231         (list ?R 'number-of-read ?s)
1232         (list ?t 'number-total ?d)
1233         (list ?y 'number-of-unread-unticked ?s)
1234         (list ?i 'number-of-ticked-and-dormant ?d)
1235         (list ?g 'group ?s)
1236         (list ?G 'qualified-group ?s)
1237         (list ?D 'newsgroup-description ?s)
1238         (list ?o 'moderated ?c)
1239         (list ?O 'moderated-string ?s)
1240         (list ?p 'process-marked ?c)
1241         (list ?s 'news-server ?s)
1242         (list ?n 'news-method ?s)
1243         (list ?z 'news-method-string ?s)
1244         (list ?u 'user-defined ?s)))
1245
1246 (defconst gnus-summary-line-format-alist 
1247   (list (list ?N 'number ?d)
1248         (list ?S 'subject ?s)
1249         (list ?s 'subject-or-nil ?s)
1250         (list ?n 'name ?s)
1251         (list ?A '(car (cdr (funcall gnus-extract-address-components from)))
1252               ?s)
1253         (list ?a '(or (car (funcall gnus-extract-address-components from)) 
1254                       from) ?s)
1255         (list ?F 'from ?s)
1256         (list ?x (macroexpand '(header-xref header)) ?s)
1257         (list ?D (macroexpand '(header-date header)) ?s)
1258         (list ?d '(gnus-dd-mmm (header-date header)) ?s)
1259         (list ?M (macroexpand '(header-id header)) ?s)
1260         (list ?r (macroexpand '(header-references header)) ?s)
1261         (list ?c '(or (header-chars header) 0) ?d)
1262         (list ?L 'lines ?d)
1263         (list ?I 'indentation ?s)
1264         (list ?T '(if (= level 0) "" (make-string (frame-width) ? )) ?s)
1265         (list ?R 'replied ?c)
1266         (list ?\[ 'opening-bracket ?c)
1267         (list ?\] 'closing-bracket ?c)
1268         (list ?\> '(make-string level ? ) ?s)
1269         (list ?\< '(make-string (max 0 (- 20 level)) ? ) ?s)
1270         (list ?i 'score ?d)
1271         (list ?z 'score-char ?c)
1272         (list ?U 'unread ?c)
1273         (list ?t '(gnus-summary-number-of-articles-in-thread 
1274                    (and (boundp 'thread) (car thread)))
1275               ?d)
1276         (list ?e '(gnus-summary-number-of-articles-in-thread 
1277                    (and (boundp 'thread) (car thread)) t)
1278               ?c)
1279         (list ?u 'user-defined ?s))
1280   "An alist of format specifications that can appear in summary lines,
1281 and what variables they correspond with, along with the type of the
1282 variable (string, integer, character, etc).")
1283
1284 (defconst gnus-summary-dummy-line-format-alist
1285   (list (list ?S 'subject ?s)
1286         (list ?N 'number ?d)
1287         (list ?u 'user-defined ?s)))
1288
1289 (defconst gnus-summary-mode-line-format-alist 
1290   (list (list ?G 'group-name ?s)
1291         (list ?g '(gnus-short-group-name group-name) ?s)
1292         (list ?A 'article-number ?d)
1293         (list ?Z 'unread-and-unselected ?s)
1294         (list ?V 'gnus-version ?s)
1295         (list ?U 'unread ?d)
1296         (list ?S 'subject ?s)
1297         (list ?e 'unselected ?d)
1298         (list ?u 'user-defined ?s)
1299         (list ?s '(gnus-current-score-file-nondirectory) ?s)))
1300
1301 (defconst gnus-group-mode-line-format-alist 
1302   (list (list ?S 'news-server ?s)
1303         (list ?M 'news-method ?s)
1304         (list ?u 'user-defined ?s)))
1305
1306 (defvar gnus-have-read-active-file nil)
1307
1308 (defconst gnus-maintainer "gnus-bug@ifi.uio.no (The Gnus Bugfixing Girls + Boys)"
1309   "The mail address of the Gnus maintainers.")
1310
1311 (defconst gnus-version "(ding) Gnus v0.99.1"
1312   "Version number for this version of Gnus.")
1313
1314 (defvar gnus-info-nodes
1315   '((gnus-group-mode            "(gnus)The Group Buffer")
1316     (gnus-summary-mode          "(gnus)The Summary Buffer")
1317     (gnus-article-mode          "(gnus)The Article Buffer"))
1318   "Assoc list of major modes and related Info nodes.")
1319
1320 (defvar gnus-documentation-group-file "~/dgnus/lisp/doc.txt"
1321   "The location of the (ding) Gnus documentation group.")
1322
1323 (defvar gnus-group-buffer "*Group*")
1324 (defvar gnus-summary-buffer "*Summary*")
1325 (defvar gnus-article-buffer "*Article*")
1326 (defvar gnus-server-buffer "*Server*")
1327
1328 (defvar gnus-work-buffer " *gnus work*")
1329
1330 (defvar gnus-buffer-list nil
1331   "Gnus buffers that should be killed on exit.")
1332
1333 (defvar gnus-server-alist nil
1334   "List of available servers.")
1335
1336 (defvar gnus-variable-list
1337   '(gnus-newsrc-options gnus-newsrc-options-n
1338     gnus-newsrc-last-checked-date 
1339     gnus-newsrc-alist gnus-server-alist
1340     gnus-killed-list gnus-zombie-list)
1341   "Gnus variables saved in the quick startup file.")
1342
1343 (defvar gnus-overload-functions
1344   '((news-inews gnus-inews-news "rnewspost"))
1345   "Functions overloaded by gnus.
1346 It is a list of `(original overload &optional file)'.")
1347
1348 (defvar gnus-newsrc-options nil
1349   "Options line in the .newsrc file.")
1350
1351 (defvar gnus-newsrc-options-n nil
1352   "List of regexps representing groups to be subscribed/ignored unconditionally.") 
1353
1354 (defvar gnus-newsrc-last-checked-date nil
1355   "Date Gnus last asked server for new newsgroups.")
1356
1357 (defvar gnus-newsrc-alist nil
1358   "Assoc list of read articles.
1359 gnus-newsrc-hashtb should be kept so that both hold the same information.")
1360
1361 (defvar gnus-newsrc-hashtb nil
1362   "Hashtable of gnus-newsrc-alist.")
1363
1364 (defvar gnus-killed-list nil
1365   "List of killed newsgroups.")
1366
1367 (defvar gnus-killed-hashtb nil
1368   "Hash table equivalent of gnus-killed-list.")
1369
1370 (defvar gnus-zombie-list nil
1371   "List of almost dead newsgroups.")
1372
1373 (defvar gnus-description-hashtb nil
1374   "Descriptions of newsgroups.")
1375
1376 (defvar gnus-list-of-killed-groups nil
1377   "List of newsgroups that have recently been killed by the user.")
1378
1379 (defvar gnus-active-hashtb nil
1380   "Hashtable of active articles.")
1381
1382 (defvar gnus-moderated-list nil
1383   "List of moderated newsgroups.")
1384
1385 (defvar gnus-group-marked nil)
1386
1387 (defvar gnus-current-startup-file nil
1388   "Startup file for the current host.")
1389
1390 (defvar gnus-last-search-regexp nil
1391   "Default regexp for article search command.")
1392
1393 (defvar gnus-last-shell-command nil
1394   "Default shell command on article.")
1395
1396 (defvar gnus-current-select-method nil
1397   "The current method for selecting a newsgroup.")
1398
1399 (defvar gnus-have-all-newsgroups nil)
1400
1401 (defvar gnus-article-internal-prepare-hook nil)
1402
1403 (defvar gnus-newsgroup-name nil)
1404 (defvar gnus-newsgroup-begin nil)
1405 (defvar gnus-newsgroup-end nil)
1406 (defvar gnus-newsgroup-last-rmail nil)
1407 (defvar gnus-newsgroup-last-mail nil)
1408 (defvar gnus-newsgroup-last-folder nil)
1409 (defvar gnus-newsgroup-last-file nil)
1410 (defvar gnus-newsgroup-auto-expire nil)
1411 (defvar gnus-newsgroup-active nil)
1412
1413 (defvar gnus-newsgroup-unreads nil
1414   "List of unread articles in the current newsgroup.")
1415
1416 (defvar gnus-newsgroup-unselected nil
1417   "List of unselected unread articles in the current newsgroup.")
1418
1419 (defvar gnus-newsgroup-marked nil
1420   "List of ticked articles in the current newsgroup (a subset of unread art).")
1421
1422 (defvar gnus-newsgroup-killed nil
1423   "List of ranges of articles that have been through the scoring process.")
1424
1425 (defvar gnus-newsgroup-kill-headers nil)
1426
1427 (defvar gnus-newsgroup-replied nil
1428   "List of articles that have been replied to in the current newsgroup.")
1429
1430 (defvar gnus-newsgroup-expirable nil
1431   "List of articles in the current newsgroup that can be expired.")
1432
1433 (defvar gnus-newsgroup-processable nil
1434   "List of articles in the current newsgroup that can be processed.")
1435
1436 (defvar gnus-newsgroup-bookmarks nil
1437   "List of articles in the current newsgroup that have bookmarks.")
1438
1439 (defvar gnus-newsgroup-dormant nil
1440   "List of dormant articles in the current newsgroup.")
1441
1442 (defvar gnus-newsgroup-scored nil
1443   "List of scored articles in the current newsgroup.")
1444
1445 (defvar gnus-newsgroup-headers nil
1446   "List of article headers in the current newsgroup.")
1447 (defvar gnus-newsgroup-headers-hashtb-by-number nil)
1448
1449 (defvar gnus-newsgroup-ancient nil
1450   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1451
1452 (defvar gnus-current-article nil)
1453 (defvar gnus-article-current nil)
1454 (defvar gnus-current-headers nil)
1455 (defvar gnus-have-all-headers nil)
1456 (defvar gnus-last-article nil)
1457 (defvar gnus-newsgroup-history nil)
1458 (defvar gnus-current-kill-article nil)
1459
1460 ;; Save window configuration.
1461 (defvar gnus-prev-winconf nil)
1462
1463 ;; Format specs
1464 (defvar gnus-summary-line-format-spec nil)
1465 (defvar gnus-summary-dummy-line-format-spec nil)
1466 (defvar gnus-group-line-format-spec nil)
1467 (defvar gnus-summary-mode-line-format-spec nil)
1468 (defvar gnus-article-mode-line-format-spec nil)
1469 (defvar gnus-group-mode-line-format-spec nil)
1470 (defvar gnus-summary-mark-positions nil)
1471
1472 (defvar gnus-summary-expunge-below nil)
1473 (defvar gnus-reffed-article-number nil)
1474
1475 (defvar rmail-default-file (expand-file-name "~/XMBOX"))
1476 (defvar rmail-default-rmail-file (expand-file-name "~/XNEWS"))
1477
1478 (defvar gnus-cache-removeable-articles nil)
1479
1480 (defconst gnus-summary-local-variables 
1481   '(gnus-newsgroup-name 
1482     gnus-newsgroup-begin gnus-newsgroup-end 
1483     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail 
1484     gnus-newsgroup-last-folder gnus-newsgroup-last-file 
1485     gnus-newsgroup-auto-expire gnus-newsgroup-unreads 
1486     gnus-newsgroup-unselected gnus-newsgroup-marked
1487     gnus-newsgroup-replied gnus-newsgroup-expirable
1488     gnus-newsgroup-processable gnus-newsgroup-killed
1489     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1490     gnus-newsgroup-headers gnus-newsgroup-headers-hashtb-by-number
1491     gnus-current-article gnus-current-headers gnus-have-all-headers
1492     gnus-last-article gnus-article-internal-prepare-hook
1493     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1494     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1495     gnus-newsgroup-threads gnus-newsgroup-async
1496     gnus-score-alist gnus-current-score-file gnus-summary-expunge-below 
1497     gnus-summary-mark-below gnus-newsgroup-active gnus-scores-exclude-files
1498     gnus-newsgroup-history gnus-newsgroup-ancient
1499     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1500     gnus-cache-removeable-articles)
1501   "Variables that are buffer-local to the summary buffers.")
1502
1503 (defconst gnus-bug-message
1504   "Sending a bug report to the Gnus Towers.
1505 ========================================
1506
1507 The buffer below is a mail buffer.  When you press `C-c C-c', it will
1508 be sent to the Gnus Bug Exterminators. 
1509
1510 At the bottom of the buffer you'll see lots of variable settings.
1511 Please do not delete those.  They will tell the Bug People what your
1512 environment is, so that it will be easier to locate the bugs.
1513
1514 If you have found a bug that makes Emacs go \"beep\", set
1515 debug-on-error to t (`M-x set-variable RET debug-on-error RET t RET') 
1516 and include the backtrace in your bug report.
1517
1518 Please describe the bug in annoying, painstaking detail.
1519
1520 Thank you for your help in stamping out bugs.
1521 ")
1522
1523 ;;; End of variables.
1524
1525 ;; Define some autoload functions Gnus might use.
1526 (eval-and-compile
1527
1528   ;; Various 
1529   (autoload 'metamail-buffer "metamail")
1530   (autoload 'Info-goto-node "info")
1531   (autoload 'hexl-hex-string-to-integer "hexl")
1532   (autoload 'pp "pp")
1533   (autoload 'pp-to-string "pp")
1534   (autoload 'pp-eval-expression "pp")
1535   (autoload 'mail-extract-address-components "mail-extr")
1536
1537   (autoload 'nnmail-split-fancy "nnmail")
1538   (autoload 'nnvirtual-catchup-group "nnvirtual")
1539
1540   ;; timezone
1541   (autoload 'timezone-make-date-arpa-standard "timezone")
1542   (autoload 'timezone-fix-time "timezone")
1543   (autoload 'timezone-make-sortable-date "timezone")
1544   (autoload 'timezone-make-time-string "timezone")
1545
1546   ;; rmail & friends
1547   (autoload 'mail-position-on-field "sendmail")
1548   (autoload 'mail-setup "sendmail")
1549   (autoload 'rmail-output "rmailout")
1550   (autoload 'news-mail-other-window "rnewspost")
1551   (autoload 'news-reply-yank-original "rnewspost")
1552   (autoload 'news-caesar-buffer-body "rnewspost")
1553   (autoload 'rmail-insert-rmail-file-header "rmail")
1554   (autoload 'rmail-count-new-messages "rmail")
1555   (autoload 'rmail-show-message "rmail")
1556
1557   ;; gnus-soup
1558   (autoload 'gnus-group-brew-soup "gnus-soup" nil t)
1559   (autoload 'gnus-brew-soup "gnus-soup" nil t)
1560   (autoload 'gnus-soup-add-article "gnus-soup" nil t)
1561   (autoload 'gnus-soup-send-replies "gnus-soup" nil t)
1562   (autoload 'gnus-soup-save-areas "gnus-soup" nil t)
1563   (autoload 'gnus-soup-pack-packet "gnus-soup" nil t)
1564   (autoload 'nnsoup-pack-replies "nnsoup" nil t)
1565
1566   ;; gnus-mh
1567   (autoload 'gnus-mail-reply-using-mhe "gnus-mh")
1568   (autoload 'gnus-mail-forward-using-mhe "gnus-mh")
1569   (autoload 'gnus-mail-other-window-using-mhe "gnus-mh")
1570   (autoload 'gnus-summary-save-in-folder "gnus-mh")
1571   (autoload 'gnus-summary-save-article-folder "gnus-mh")
1572   (autoload 'gnus-Folder-save-name "gnus-mh")
1573   (autoload 'gnus-folder-save-name "gnus-mh")
1574
1575   ;; gnus-vis misc
1576   (autoload 'gnus-group-make-menu-bar "gnus-vis")
1577   (autoload 'gnus-summary-make-menu-bar "gnus-vis")
1578   (autoload 'gnus-server-make-menu-bar "gnus-vis")
1579   (autoload 'gnus-article-make-menu-bar "gnus-vis")
1580   (autoload 'gnus-browse-make-menu-bar "gnus-vis")
1581   (autoload 'gnus-highlight-selected-summary "gnus-vis")
1582   (autoload 'gnus-summary-highlight-line "gnus-vis")
1583   (autoload 'gnus-carpal-setup-buffer "gnus-vis")
1584
1585   ;; gnus-vis article
1586   (autoload 'gnus-article-push-button "gnus-vis" nil t)
1587   (autoload 'gnus-article-press-button "gnus-vis" nil t)
1588   (autoload 'gnus-article-highlight "gnus-vis" nil t)
1589   (autoload 'gnus-article-hide "gnus-vis" nil t)
1590   (autoload 'gnus-article-hide-signature "gnus-vis" nil t)
1591   (autoload 'gnus-article-highlight-headers "gnus-vis" nil t)
1592   (autoload 'gnus-article-highlight-signature "gnus-vis" nil t)
1593   (autoload 'gnus-article-add-buttons "gnus-vis" nil t)
1594   (autoload 'gnus-article-next-button "gnus-vis" nil t)
1595   (autoload 'gnus-article-add-button "gnus-vis")
1596
1597   ;; gnus-cite
1598   (autoload 'gnus-article-highlight-citation "gnus-cite" nil t)
1599   (autoload 'gnus-article-hide-citation-maybe "gnus-cite" nil t)
1600   (autoload 'gnus-article-hide-citation "gnus-cite" nil t)
1601
1602   ;; gnus-kill
1603   (autoload 'gnus-kill "gnus-kill")
1604   (autoload 'gnus-apply-kill-file-internal "gnus-kill")
1605   (autoload 'gnus-kill-file-edit-file "gnus-kill")
1606   (autoload 'gnus-kill-file-raise-followups-to-author "gnus-kill")
1607   (autoload 'gnus-execute "gnus-kill")
1608   (autoload 'gnus-expunge "gnus-kill")
1609
1610   ;; gnus-cache
1611   (autoload 'gnus-cache-possibly-enter-article "gnus-cache")
1612   (autoload 'gnus-cache-save-buffers "gnus-cache")
1613   (autoload 'gnus-cache-possibly-remove-articles "gnus-cache")
1614   (autoload 'gnus-cache-request-article "gnus-cache")
1615   (autoload 'gnus-cache-retrieve-headers "gnus-cache")
1616   (autoload 'gnus-cache-possibly-alter-active "gnus-cache")
1617   (autoload 'gnus-jog-cache "gnus-cache" nil t)
1618   (autoload 'gnus-cache-enter-remove-article "gnus-cache")
1619
1620   ;; gnus-score
1621   (autoload 'gnus-summary-increase-score "gnus-score" nil t)
1622   (autoload 'gnus-summary-lower-score "gnus-score" nil t)
1623   (autoload 'gnus-summary-score-map "gnus-score" nil nil 'keymap)
1624   (autoload 'gnus-score-save "gnus-score")
1625   (autoload 'gnus-score-headers "gnus-score")
1626   (autoload 'gnus-current-score-file-nondirectory "gnus-score")
1627   (autoload 'gnus-score-adaptive "gnus-score")
1628   (autoload 'gnus-score-remove-lines-adaptive "gnus-score")
1629   (autoload 'gnus-score-find-trace "gnus-score")
1630
1631   ;; gnus-edit
1632   (autoload 'gnus-score-customize "gnus-edit" nil t)
1633
1634   ;; gnus-uu
1635   (autoload 'gnus-uu-extract-map "gnus-uu" nil nil 'keymap)
1636   (autoload 'gnus-uu-mark-map "gnus-uu" nil nil 'keymap)
1637   (autoload 'gnus-uu-digest-mail-forward "gnus-uu" nil t)
1638   (autoload 'gnus-uu-digest-post-forward "gnus-uu" nil t)
1639   (autoload 'gnus-uu-mark-series "gnus-uu" nil t)
1640   (autoload 'gnus-uu-mark-region "gnus-uu" nil t)
1641   (autoload 'gnus-uu-mark-by-regexp "gnus-uu" nil t)
1642   (autoload 'gnus-uu-mark-all "gnus-uu" nil t)
1643   (autoload 'gnus-uu-mark-sparse "gnus-uu" nil t)
1644   (autoload 'gnus-uu-mark-thread "gnus-uu" nil t)
1645   (autoload 'gnus-uu-decode-uu "gnus-uu" nil t)
1646   (autoload 'gnus-uu-decode-uu-and-save "gnus-uu" nil t)
1647   (autoload 'gnus-uu-decode-unshar "gnus-uu" nil t)
1648   (autoload 'gnus-uu-decode-unshar-and-save "gnus-uu" nil t)
1649   (autoload 'gnus-uu-decode-save "gnus-uu" nil t)
1650   (autoload 'gnus-uu-decode-binhex "gnus-uu" nil t)
1651   (autoload 'gnus-uu-decode-uu-view "gnus-uu" nil t)
1652   (autoload 'gnus-uu-decode-uu-and-save-view "gnus-uu" nil t)
1653   (autoload 'gnus-uu-decode-unshar-view "gnus-uu" nil t)
1654   (autoload 'gnus-uu-decode-unshar-and-save-view "gnus-uu" nil t)
1655   (autoload 'gnus-uu-decode-save-view "gnus-uu" nil t)
1656   (autoload 'gnus-uu-decode-binhex-view "gnus-uu" nil t)
1657
1658   ;; gnus-msg
1659   (autoload 'gnus-summary-send-map "gnus-msg" nil nil 'keymap)
1660   (autoload 'gnus-group-post-news "gnus-msg" nil t)
1661   (autoload 'gnus-group-mail "gnus-msg" nil t)
1662   (autoload 'gnus-summary-post-news "gnus-msg" nil t)
1663   (autoload 'gnus-summary-followup "gnus-msg" nil t)
1664   (autoload 'gnus-summary-followup-with-original "gnus-msg" nil t)
1665   (autoload 'gnus-summary-followup-and-reply "gnus-msg" nil t)
1666   (autoload 'gnus-summary-followup-and-reply-with-original "gnus-msg" nil t)
1667   (autoload 'gnus-summary-cancel-article "gnus-msg" nil t)
1668   (autoload 'gnus-summary-supersede-article "gnus-msg" nil t)
1669   (autoload 'gnus-post-news "gnus-msg" nil t)
1670   (autoload 'gnus-inews-news "gnus-msg" nil t)
1671   (autoload 'gnus-cancel-news "gnus-msg" nil t)
1672   (autoload 'gnus-summary-reply "gnus-msg" nil t)
1673   (autoload 'gnus-summary-reply-with-original "gnus-msg" nil t)
1674   (autoload 'gnus-summary-mail-forward "gnus-msg" nil t)
1675   (autoload 'gnus-summary-mail-other-window "gnus-msg" nil t)
1676   (autoload 'gnus-mail-reply-using-mail "gnus-msg")
1677   (autoload 'gnus-mail-yank-original "gnus-msg")
1678   (autoload 'gnus-mail-send-and-exit "gnus-msg")
1679   (autoload 'gnus-mail-forward-using-mail "gnus-msg")
1680   (autoload 'gnus-mail-other-window-using-mail "gnus-msg")
1681   (autoload 'gnus-article-mail-with-original "gnus-msg")
1682   (autoload 'gnus-article-mail "gnus-msg")
1683   (autoload 'gnus-bug "gnus-msg" nil t)
1684
1685   ;; gnus-vm
1686   (autoload 'gnus-summary-save-in-vm "gnus-vm" nil t)
1687   (autoload 'gnus-summary-save-article-vm "gnus-vm" nil t)
1688   (autoload 'gnus-mail-forward-using-vm "gnus-vm")
1689   (autoload 'gnus-mail-reply-using-vm "gnus-vm")
1690   (autoload 'gnus-mail-other-window-using-vm "gnus-vm" nil t)
1691   (autoload 'gnus-yank-article "gnus-vm" nil t)
1692
1693   )
1694
1695 \f
1696
1697 ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
1698 ;; If you want the cursor to go somewhere else, set these two
1699 ;; functions in some startup hook to whatever you want.
1700 (defalias 'gnus-summary-position-cursor 'gnus-goto-colon)
1701 (defalias 'gnus-group-position-cursor 'gnus-goto-colon)
1702
1703 ;;; Various macros and substs.
1704
1705 (defmacro gnus-eval-in-buffer-window (buffer &rest forms)
1706   "Pop to BUFFER, evaluate FORMS, and then returns to original window."
1707   (` (let ((GnusStartBufferWindow (selected-window)))
1708        (unwind-protect
1709            (progn
1710              (pop-to-buffer (, buffer))
1711              (,@ forms))
1712          (select-window GnusStartBufferWindow)))))
1713
1714 (defmacro gnus-gethash (string hashtable)
1715   "Get hash value of STRING in HASHTABLE."
1716   ;;(` (symbol-value (abbrev-symbol (, string) (, hashtable))))
1717   ;;(` (abbrev-expansion (, string) (, hashtable)))
1718   (` (symbol-value (intern-soft (, string) (, hashtable)))))
1719
1720 (defmacro gnus-sethash (string value hashtable)
1721   "Set hash value. Arguments are STRING, VALUE, and HASHTABLE."
1722   ;; We cannot use define-abbrev since it only accepts string as value.
1723   ;; (set (intern string hashtable) value))
1724   (` (set (intern (, string) (, hashtable)) (, value))))
1725
1726 (defsubst gnus-buffer-substring (beg end)
1727   (buffer-substring (match-beginning beg) (match-end end)))
1728
1729 ;; modified by MORIOKA Tomohiko <morioka@jaist.ac.jp>
1730 ;;   function `substring' might cut on a middle of multi-octet
1731 ;;   character.
1732 (defun gnus-truncate-string (str width)
1733   (substring str 0 width))
1734
1735 ;; Added by Geoffrey T. Dairiki <dairiki@u.washington.edu>. A safe way
1736 ;; to limit the length of a string. This function is necessary since
1737 ;; `(substr "abc" 0 30)' pukes with "Args out of range".
1738 (defsubst gnus-limit-string (str width)
1739   (if (> (length str) width)
1740       (substring str 0 width)
1741     str))
1742
1743 (defsubst gnus-simplify-subject-re (subject)
1744   "Remove \"Re:\" from subject lines."
1745   (let ((case-fold-search t))
1746     (if (string-match "^re: *" subject)
1747         (substring subject (match-end 0))
1748       subject)))
1749
1750 (defsubst gnus-goto-char (point)
1751   (and point (goto-char point)))
1752
1753 (defmacro gnus-buffer-exists-p (buffer)
1754   (` (and (, buffer)
1755           (funcall (if (stringp (, buffer)) 'get-buffer 'buffer-name)
1756                    (, buffer)))))
1757
1758 (defmacro gnus-kill-buffer (buffer)
1759   (` (if (gnus-buffer-exists-p (, buffer))
1760          (kill-buffer (, buffer)))))
1761
1762 (defsubst gnus-point-at-bol ()
1763   "Return point at the beginning of line."
1764   (let ((p (point)))
1765     (beginning-of-line)
1766     (prog1
1767         (point)
1768       (goto-char p))))
1769
1770 (defsubst gnus-point-at-eol ()
1771   "Return point at the beginning of line."
1772   (let ((p (point)))
1773     (end-of-line)
1774     (prog1
1775         (point)
1776       (goto-char p))))
1777
1778 ;; Delete the current line (and the next N lines.);
1779 (defmacro gnus-delete-line (&optional n)
1780   (` (delete-region (progn (beginning-of-line) (point))
1781                     (progn (forward-line (, (or n 1))) (point)))))
1782
1783 ;;; Load the compatability functions. 
1784
1785 (require 'gnus-ems)
1786
1787 \f
1788 ;;;
1789 ;;; Gnus Utility Functions
1790 ;;;
1791
1792 (defun gnus-extract-address-components (from)
1793   (let (name address)
1794     ;; First find the address - the thing with the @ in it.  This may
1795     ;; not be accurate in mail addresses, but does the trick most of
1796     ;; the time in news messages.
1797     (if (string-match "\\b[^@ \t<>]+[!@][^@ \t<>]+\\b" from)
1798         (setq address (substring from (match-beginning 0) (match-end 0))))
1799     ;; Then we check whether the "name <address>" format is used.
1800     (and address
1801          (string-match (concat "<" (regexp-quote address) ">") from)
1802          (and (setq name (substring from 0 (1- (match-beginning 0))))
1803               ;; Strip any quotes from the name.
1804               (string-match "\".*\"" name)
1805               (setq name (substring name 1 (1- (match-end 0))))))
1806     ;; If not, then "address (name)" is used.
1807     (or name
1808         (and (string-match "(.+)" from)
1809              (setq name (substring from (1+ (match-beginning 0)) 
1810                                    (1- (match-end 0)))))
1811         (and (string-match "()" from)
1812              (setq name address))
1813         ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>.
1814         ;; XOVER might not support folded From headers.
1815         (and (string-match "(.*" from)
1816              (setq name (substring from (1+ (match-beginning 0)) 
1817                                    (match-end 0)))))
1818     ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
1819     (list (or name from) (or address from))))
1820
1821 (defun gnus-fetch-field (field)
1822   "Return the value of the header FIELD of current article."
1823   (save-excursion
1824     (save-restriction
1825       (let ((case-fold-search t))
1826         (gnus-narrow-to-headers)
1827         (mail-fetch-field field)))))
1828
1829 (defun gnus-goto-colon ()
1830   (beginning-of-line)
1831   (search-forward ":" (gnus-point-at-eol) t))
1832
1833 (defun gnus-narrow-to-headers ()
1834   (widen)
1835   (save-excursion
1836     (narrow-to-region
1837      (goto-char (point-min))
1838      (if (search-forward "\n\n" nil t)
1839          (1- (point))
1840        (point-max)))))
1841
1842 (defvar gnus-old-specs nil)
1843
1844 (defun gnus-update-format-specifications ()
1845   (gnus-make-thread-indent-array)
1846
1847   (let ((formats '(summary summary-dummy group 
1848                            summary-mode group-mode article-mode))
1849         old-format new-format)
1850     (while formats
1851       (setq new-format (symbol-value
1852                         (intern (format "gnus-%s-line-format" (car formats)))))
1853       (or (and (setq old-format (cdr (assq (car formats) gnus-old-specs)))
1854                (equal old-format new-format))
1855           (set (intern (format "gnus-%s-line-format-spec" (car formats)))
1856                (gnus-parse-format
1857                 new-format
1858                 (symbol-value 
1859                  (intern (format "gnus-%s-line-format-alist"
1860                                  (if (eq (car formats) 'article-mode)
1861                                      'summary-mode (car formats))))))))
1862       (setq gnus-old-specs (cons (cons (car formats) new-format)
1863                                  (delq (car formats) gnus-old-specs)))
1864       (setq formats (cdr formats))))
1865       
1866   (gnus-update-summary-mark-positions)
1867
1868   (if (and (string-match "%D" gnus-group-line-format)
1869            (not gnus-description-hashtb)
1870            gnus-read-active-file)
1871       (gnus-read-all-descriptions-files)))
1872
1873 (defun gnus-update-summary-mark-positions ()
1874   (save-excursion
1875     (let ((gnus-replied-mark 129)
1876           (gnus-score-below-mark 130)
1877           (gnus-score-over-mark 130)
1878           (thread nil)
1879           pos)
1880       (gnus-set-work-buffer)
1881       (gnus-summary-insert-line 
1882        nil [0 "" "" "" "" "" 0 0 ""]  0 nil 128 t nil "" nil 1)
1883       (goto-char (point-min))
1884       (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
1885                                          (- (point) 2)))))
1886       (goto-char (point-min))
1887       (setq pos (cons (cons 'replied (and (search-forward "\201" nil t)
1888                                           (- (point) 2))) pos))
1889       (goto-char (point-min))
1890       (setq pos (cons (cons 'score (and (search-forward "\202" nil t)
1891                                         (- (point) 2))) pos))
1892       (setq gnus-summary-mark-positions pos))))
1893
1894 (defun gnus-mouse-face-function (form)
1895   (` (let ((string (, form)))
1896        (put-text-property 0 (length string) 'mouse-face gnus-mouse-face string)
1897        string)))
1898
1899 (defun gnus-max-width-function (el max-width)
1900   (or (numberp max-width) (signal 'wrong-type-argument '(numberp max-width)))
1901   (` (let* ((val (eval (, el)))
1902             (valstr (if (numberp val)
1903                         (int-to-string val) val)))
1904        (if (> (length valstr) (, max-width))
1905            (substring valstr 0 (, max-width))
1906          valstr))))
1907
1908 (defun gnus-parse-format (format spec-alist)
1909   ;; This function parses the FORMAT string with the help of the
1910   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
1911   ;; string.  If the FORMAT string contains the specifiers %( and %)
1912   ;; the text between them will have the mouse-face text property.
1913   (if (string-match "\\`\\(.*\\)%(\\(.*\\)%)\\(.*\n?\\)\\'" format)
1914       (if (and gnus-visual gnus-mouse-face)
1915           (let ((pre (substring format (match-beginning 1) (match-end 1)))
1916                 (button (substring format (match-beginning 2) (match-end 2)))
1917                 (post (substring format (match-beginning 3) (match-end 3))))
1918             (list 'concat
1919                   (gnus-parse-simple-format pre spec-alist)
1920                   (gnus-mouse-face-function 
1921                    (gnus-parse-simple-format button spec-alist))
1922                   (gnus-parse-simple-format post spec-alist)))
1923         (gnus-parse-simple-format
1924          (concat (substring format (match-beginning 1) (match-end 1))
1925                  (substring format (match-beginning 2) (match-end 2))
1926                  (substring format (match-beginning 3) (match-end 3)))
1927          spec-alist))
1928     (gnus-parse-simple-format format spec-alist)))
1929
1930 (defun gnus-parse-simple-format (format spec-alist)
1931   ;; This function parses the FORMAT string with the help of the
1932   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
1933   ;; string. The list will consist of the symbol `format', a format
1934   ;; specification string, and a list of forms depending on the
1935   ;; SPEC-ALIST.
1936   (let ((max-width 0)
1937         spec flist fstring newspec elem beg)
1938     (save-excursion
1939       (gnus-set-work-buffer)
1940       (insert format)
1941       (goto-char (point-min))
1942       (while (re-search-forward "%[-0-9]*\\(,[0-9]*\\)*\\(.\\)\\(.\\)?" nil t)
1943         (setq spec (string-to-char (buffer-substring (match-beginning 2)
1944                                                      (match-end 2))))
1945         ;; First check if there are any specs that look anything like
1946         ;; "%12,12A", ie. with a "max width specification". These have
1947         ;; to be treated specially.
1948         (if (setq beg (match-beginning 1))
1949             (setq max-width 
1950                   (string-to-int 
1951                    (buffer-substring (1+ (match-beginning 1)) (match-end 1))))
1952           (setq max-width 0)
1953           (setq beg (match-beginning 2)))
1954         ;; Find the specification from `spec-alist'.
1955         (if (not (setq elem (cdr (assq spec spec-alist))))
1956             (setq elem '("*" ?s)))
1957         ;; Treat user defined format specifiers specially
1958         (and (eq (car elem) 'user-defined)
1959              (setq elem
1960                    (list 
1961                     (list (intern (concat "gnus-user-format-function-"
1962                                           (buffer-substring
1963                                            (match-beginning 3)
1964                                            (match-end 3))))
1965                           'header)
1966                     ?s))
1967              (delete-region (match-beginning 3) (match-end 3)))
1968         (if (not (zerop max-width))
1969             (let ((el (car elem)))
1970               (cond ((= (car (cdr elem)) ?c) 
1971                      (setq el (list 'char-to-string el)))
1972                     ((= (car (cdr elem)) ?d)
1973                      (numberp el) (setq el (list 'int-to-string el))))
1974               (setq flist (cons (gnus-max-width-function el max-width)
1975                                 flist))
1976               (setq newspec ?s))
1977           (setq flist (cons (car elem) flist))
1978           (setq newspec (car (cdr elem))))
1979         ;; Remove the old specification (and possibly a ",12" string).
1980         (delete-region beg (match-end 2))
1981         ;; Insert the new specification.
1982         (goto-char beg)
1983         (insert newspec))
1984       (setq fstring (buffer-substring 1 (point-max))))
1985     (cons 'format (cons fstring (nreverse flist)))))
1986
1987 ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
1988 (defun gnus-read-init-file ()
1989   (and gnus-init-file
1990        (or (and (file-exists-p gnus-init-file) 
1991                 ;; Don't try to load a directory.
1992                 (not (file-directory-p gnus-init-file)))
1993            (file-exists-p (concat gnus-init-file ".el"))
1994            (file-exists-p (concat gnus-init-file ".elc")))
1995        (load gnus-init-file nil t)))
1996
1997 (defun gnus-set-work-buffer ()
1998   (if (get-buffer gnus-work-buffer)
1999       (progn
2000         (set-buffer gnus-work-buffer)
2001         (erase-buffer))
2002     (set-buffer (get-buffer-create gnus-work-buffer))
2003     (kill-all-local-variables)
2004     (buffer-disable-undo (current-buffer))
2005     (gnus-add-current-to-buffer-list)))
2006
2007 ;; Article file names when saving.
2008
2009 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
2010   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2011 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group/num.
2012 Otherwise, it is like ~/News/news/group/num."
2013   (let ((default
2014           (expand-file-name
2015            (concat (if (gnus-use-long-file-name 'not-save)
2016                        (gnus-capitalize-newsgroup newsgroup)
2017                      (gnus-newsgroup-directory-form newsgroup))
2018                    "/" (int-to-string (header-number headers)))
2019            (or gnus-article-save-directory "~/News"))))
2020     (if (and last-file
2021              (string-equal (file-name-directory default)
2022                            (file-name-directory last-file))
2023              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2024         default
2025       (or last-file default))))
2026
2027 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
2028   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2029 If variable `gnus-use-long-file-name' is nil, it is ~/News/news.group/num.
2030 Otherwise, it is like ~/News/news/group/num."
2031   (let ((default
2032           (expand-file-name
2033            (concat (if (gnus-use-long-file-name 'not-save)
2034                        newsgroup
2035                      (gnus-newsgroup-directory-form newsgroup))
2036                    "/" (int-to-string (header-number headers)))
2037            (or gnus-article-save-directory "~/News"))))
2038     (if (and last-file
2039              (string-equal (file-name-directory default)
2040                            (file-name-directory last-file))
2041              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2042         default
2043       (or last-file default))))
2044
2045 (defun gnus-Plain-save-name (newsgroup headers &optional last-file)
2046   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2047 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group.
2048 Otherwise, it is like ~/News/news/group/news."
2049   (or last-file
2050       (expand-file-name
2051        (if (gnus-use-long-file-name 'not-save)
2052            (gnus-capitalize-newsgroup newsgroup)
2053          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2054        (or gnus-article-save-directory "~/News"))))
2055
2056 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
2057   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2058 If variable `gnus-use-long-file-name' is nil, it is ~/News/news.group.
2059 Otherwise, it is like ~/News/news/group/news."
2060   (or last-file
2061       (expand-file-name
2062        (if (gnus-use-long-file-name 'not-save)
2063            newsgroup
2064          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2065        (or gnus-article-save-directory "~/News"))))
2066
2067 ;; For subscribing new newsgroup
2068
2069 (defun gnus-subscribe-hierarchical-interactive (groups)
2070   (let ((groups (sort groups 'string<))
2071         prefixes prefix start ans group starts)
2072     (while groups
2073       (setq prefixes (list "^"))
2074       (while (and groups prefixes)
2075         (while (not (string-match (car prefixes) (car groups)))
2076           (setq prefixes (cdr prefixes)))
2077         (setq prefix (car prefixes))
2078         (setq start (1- (length prefix)))
2079         (if (and (string-match "[^\\.]\\." (car groups) start)
2080                  (cdr groups)
2081                  (setq prefix 
2082                        (concat "^" (substring (car groups) 0 (match-end 0))))
2083                  (string-match prefix (car (cdr groups))))
2084             (progn
2085               (setq prefixes (cons prefix prefixes))
2086               (message "Descend hierarchy %s? ([y]nsq): " 
2087                        (substring prefix 1 (1- (length prefix))))
2088               (setq ans (read-char))
2089               (cond ((= ans ?n)
2090                      (while (and groups 
2091                                  (string-match prefix 
2092                                                (setq group (car groups))))
2093                        (setq gnus-killed-list 
2094                              (cons group gnus-killed-list))
2095                        (gnus-sethash group group gnus-killed-hashtb)
2096                        (setq groups (cdr groups)))
2097                      (setq starts (cdr starts)))
2098                     ((= ans ?s)
2099                      (while (and groups 
2100                                  (string-match prefix 
2101                                                (setq group (car groups))))
2102                        (gnus-sethash group group gnus-killed-hashtb)
2103                        (gnus-subscribe-alphabetically (car groups))
2104                        (setq groups (cdr groups)))
2105                      (setq starts (cdr starts)))
2106                     ((= ans ?q)
2107                      (while groups
2108                        (setq group (car groups))
2109                        (setq gnus-killed-list (cons group gnus-killed-list))
2110                        (gnus-sethash group group gnus-killed-hashtb)
2111                        (setq groups (cdr groups))))
2112                     (t nil)))
2113           (message "Subscribe %s? ([n]yq)" (car groups))
2114           (setq ans (read-char))
2115           (setq group (car groups))
2116           (cond ((= ans ?y)
2117                  (gnus-subscribe-alphabetically (car groups))
2118                  (gnus-sethash group group gnus-killed-hashtb))
2119                 ((= ans ?q)
2120                  (while groups
2121                    (setq group (car groups))
2122                    (setq gnus-killed-list (cons group gnus-killed-list))
2123                    (gnus-sethash group group gnus-killed-hashtb)
2124                    (setq groups (cdr groups))))
2125                 (t 
2126                  (setq gnus-killed-list (cons group gnus-killed-list))
2127                  (gnus-sethash group group gnus-killed-hashtb)))
2128           (setq groups (cdr groups)))))))
2129
2130 (defun gnus-subscribe-randomly (newsgroup)
2131   "Subscribe new NEWSGROUP by making it the first newsgroup."
2132   (gnus-subscribe-newsgroup newsgroup))
2133
2134 (defun gnus-subscribe-alphabetically (newgroup)
2135   "Subscribe new NEWSGROUP and insert it in alphabetical order."
2136   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2137   (let ((groups (cdr gnus-newsrc-alist))
2138         before)
2139     (while (and (not before) groups)
2140       (if (string< newgroup (car (car groups)))
2141           (setq before (car (car groups)))
2142         (setq groups (cdr groups))))
2143     (gnus-subscribe-newsgroup newgroup before)))
2144
2145 (defun gnus-subscribe-hierarchically (newgroup)
2146   "Subscribe new NEWSGROUP and insert it in hierarchical newsgroup order."
2147   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2148   (save-excursion
2149     (set-buffer (find-file-noselect gnus-current-startup-file))
2150     (let ((groupkey newgroup)
2151           before)
2152       (while (and (not before) groupkey)
2153         (goto-char (point-min))
2154         (let ((groupkey-re
2155                (concat "^\\(" (regexp-quote groupkey) ".*\\)[!:]")))
2156           (while (and (re-search-forward groupkey-re nil t)
2157                       (progn
2158                         (setq before (buffer-substring
2159                                       (match-beginning 1) (match-end 1)))
2160                         (string< before newgroup)))))
2161         ;; Remove tail of newsgroup name (eg. a.b.c -> a.b)
2162         (setq groupkey
2163               (if (string-match "^\\(.*\\)\\.[^.]+$" groupkey)
2164                   (substring groupkey (match-beginning 1) (match-end 1)))))
2165       (gnus-subscribe-newsgroup newgroup before))))
2166
2167 (defun gnus-subscribe-interactively (newsgroup)
2168   "Subscribe new NEWSGROUP interactively.
2169 It is inserted in hierarchical newsgroup order if subscribed. If not,
2170 it is killed."
2171   (if (gnus-y-or-n-p (format "Subscribe new newsgroup: %s " newsgroup))
2172       (gnus-subscribe-hierarchically newsgroup)
2173     (setq gnus-killed-list (cons newsgroup gnus-killed-list))))
2174
2175 (defun gnus-subscribe-zombies (newsgroup)
2176   "Make new NEWSGROUP a zombie group."
2177   (setq gnus-zombie-list (cons newsgroup gnus-zombie-list)))
2178
2179 (defun gnus-subscribe-newsgroup (newsgroup &optional next)
2180   "Subscribe new NEWSGROUP.
2181 If NEXT is non-nil, it is inserted before NEXT. Otherwise it is made
2182 the first newsgroup."
2183   ;; We subscribe the group by changing its level to `subscribed'.
2184   (gnus-group-change-level 
2185    newsgroup gnus-level-default-subscribed
2186    gnus-level-killed (gnus-gethash (or next "dummy.group") gnus-newsrc-hashtb))
2187   (gnus-message 5 "Subscribe newsgroup: %s" newsgroup))
2188
2189 ;; For directories
2190
2191 (defun gnus-newsgroup-directory-form (newsgroup)
2192   "Make hierarchical directory name from NEWSGROUP name."
2193   (let ((newsgroup (substring newsgroup 0)) ;Copy string.
2194         (len (length newsgroup))
2195         idx)
2196     ;; If this is a foreign group, we don't want to translate the
2197     ;; entire name.  
2198     (if (setq idx (string-match ":" newsgroup))
2199         (aset newsgroup idx ?/)
2200       (setq idx 0))
2201     ;; Replace all occurrences of `.' with `/'.
2202     (while (< idx len)
2203       (if (= (aref newsgroup idx) ?.)
2204           (aset newsgroup idx ?/))
2205       (setq idx (1+ idx)))
2206     newsgroup))
2207
2208 (defun gnus-make-directory (dir)
2209   "Make DIRECTORY recursively."
2210   (let* ((dir (expand-file-name dir default-directory))
2211          dirs)
2212     (if (string-match "/$" dir)
2213         (setq dir (substring dir 0 (match-beginning 0))))
2214     (while (not (file-exists-p dir))
2215       (setq dirs (cons dir dirs))
2216       (string-match "/[^/]+$" dir)
2217       (setq dir (substring dir 0 (match-beginning 0))))
2218     (while dirs
2219       (make-directory (car dirs))
2220       (setq dirs (cdr dirs)))))
2221
2222 (defun gnus-capitalize-newsgroup (newsgroup)
2223   "Capitalize NEWSGROUP name."
2224   (and (not (zerop (length newsgroup)))
2225        (concat (char-to-string (upcase (aref newsgroup 0)))
2226                (substring newsgroup 1))))
2227
2228 ;; Var
2229
2230 (defun gnus-simplify-subject (subject &optional re-only)
2231   "Remove `Re:' and words in parentheses.
2232 If optional argument RE-ONLY is non-nil, strip `Re:' only."
2233   (let ((case-fold-search t))           ;Ignore case.
2234     ;; Remove `Re:' and `Re^N:'.
2235     (if (string-match "^re:[ \t]*" subject)
2236         (setq subject (substring subject (match-end 0))))
2237     ;; Remove words in parentheses from end.
2238     (or re-only
2239         (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
2240           (setq subject (substring subject 0 (match-beginning 0)))))
2241     ;; Return subject string.
2242     subject))
2243
2244 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
2245 ;; all whitespace.
2246 (defun gnus-simplify-subject-fuzzy (subject)
2247   (let ((case-fold-search t))
2248     (save-excursion
2249       (gnus-set-work-buffer)
2250       (insert subject)
2251       (inline (gnus-simplify-buffer-fuzzy))
2252       (buffer-string))))
2253
2254 (defun gnus-simplify-buffer-fuzzy ()
2255   (goto-char (point-min))
2256   ;; Fix by Stainless Steel Rat <ratinox@ccs.neu.edu>.
2257   (while (re-search-forward "^[ \t]*\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;][ \t]*"
2258                             nil t)
2259     (replace-match "" t t))
2260   (goto-char (point-min))
2261   (while (re-search-forward "[ \t\n]*([^()]*)[ \t\n]*$" nil t)
2262     (replace-match "" t t))
2263   (goto-char (point-min))
2264   (while (re-search-forward "[ \t]+" nil t)
2265     (replace-match " " t t))
2266   (goto-char (point-min))
2267   (while (re-search-forward "[ \t]+$" nil t)
2268     (replace-match "" t t))
2269   (goto-char (point-min))
2270   (while (re-search-forward "^[ \t]+" nil t)
2271     (replace-match "" t t))
2272   (if gnus-simplify-subject-fuzzy-regexp
2273       (while (re-search-forward gnus-simplify-subject-fuzzy-regexp nil t)
2274         (replace-match "" t t))))
2275
2276 ;; Add the current buffer to the list of buffers to be killed on exit. 
2277 (defun gnus-add-current-to-buffer-list ()
2278   (or (memq (current-buffer) gnus-buffer-list)
2279       (setq gnus-buffer-list (cons (current-buffer) gnus-buffer-list))))
2280
2281 (defun gnus-string> (s1 s2)
2282   (not (or (string< s1 s2)
2283            (string= s1 s2))))
2284
2285 ;; Functions accessing headers.
2286 ;; Functions are more convenient than macros in some cases.
2287
2288 (defun gnus-header-number (header)
2289   (header-number header))
2290
2291 (defun gnus-header-subject (header)
2292   (header-subject header))
2293
2294 (defun gnus-header-from (header)
2295   (header-from header))
2296
2297 (defun gnus-header-xref (header)
2298   (header-xref header))
2299
2300 (defun gnus-header-lines (header)
2301   (header-lines header))
2302
2303 (defun gnus-header-date (header)
2304   (header-date header))
2305
2306 (defun gnus-header-id (header)
2307   (header-id header))
2308
2309 (defun gnus-header-references (header)
2310   (header-references header))
2311
2312 ;;; General various misc type functions.
2313
2314 (defun gnus-clear-system ()
2315   "Clear all variables and buffers."
2316   ;; Clear Gnus variables.
2317   (let ((variables gnus-variable-list))
2318     (while variables
2319       (set (car variables) nil)
2320       (setq variables (cdr variables))))
2321   ;; Clear other internal variables.
2322   (setq gnus-list-of-killed-groups nil
2323         gnus-have-read-active-file nil
2324         gnus-newsrc-alist nil
2325         gnus-newsrc-hashtb nil
2326         gnus-killed-list nil
2327         gnus-zombie-list nil
2328         gnus-killed-hashtb nil
2329         gnus-active-hashtb nil
2330         gnus-moderated-list nil
2331         gnus-description-hashtb nil
2332         gnus-newsgroup-headers nil
2333         gnus-newsgroup-headers-hashtb-by-number nil
2334         gnus-newsgroup-name nil
2335         gnus-server-alist nil
2336         gnus-current-select-method nil)
2337   ;; Reset any score variables.
2338   (and (boundp 'gnus-score-cache)
2339        (set 'gnus-score-cache nil))
2340   (and (boundp 'gnus-internal-global-score-files)
2341        (set 'gnus-internal-global-score-files nil))
2342   ;; Kill the startup file.
2343   (and gnus-current-startup-file
2344        (get-file-buffer gnus-current-startup-file)
2345        (kill-buffer (get-file-buffer gnus-current-startup-file)))
2346   ;; Save any cache buffers.
2347   (and gnus-use-cache (gnus-cache-save-buffers))
2348   ;; Clear the dribble buffer.
2349   (gnus-dribble-clear)
2350   ;; Kill global KILL file buffer.
2351   (if (get-file-buffer (gnus-newsgroup-kill-file nil))
2352       (kill-buffer (get-file-buffer (gnus-newsgroup-kill-file nil))))
2353   (gnus-kill-buffer nntp-server-buffer)
2354   ;; Kill Gnus buffers.
2355   (while gnus-buffer-list
2356     (gnus-kill-buffer (car gnus-buffer-list))
2357     (setq gnus-buffer-list (cdr gnus-buffer-list))))
2358
2359 (defun gnus-windows-old-to-new (setting)
2360   (if (symbolp setting)
2361       (setq setting 
2362             (cond ((eq setting 'SelectArticle)
2363                    'article)
2364                   ((eq setting 'SelectSubject)
2365                    'summary)
2366                   ((eq setting 'SelectNewsgroup)
2367                    'group)
2368                   (t setting))))
2369   (if (or (listp setting)
2370           (not (and gnus-window-configuration
2371                     (memq setting '(group summary article)))))
2372       setting
2373     (let* ((setting (if (eq setting 'group) 
2374                         (if (assq 'newsgroup gnus-window-configuration)
2375                             'newsgroup
2376                           'newsgroups) setting))
2377            (elem (car (cdr (assq setting gnus-window-configuration))))
2378            (total (apply '+ elem))
2379            (types '(group summary article))
2380            (pbuf (if (eq setting 'newsgroups) 'group 'summary))
2381            (i 0)
2382            perc
2383            out)
2384       (while (< i 3)
2385         (or (zerop (nth i elem))
2386             (progn
2387               (setq perc  (/ (* 1.0 (nth 0 elem)) total))
2388               (setq out (cons (if (eq pbuf (nth i types))
2389                                   (vector (nth i types) perc 'point)
2390                                 (vector (nth i types) perc))
2391                               out))))
2392         (setq i (1+ i)))
2393       (list (nreverse out)))))
2394            
2395 (defun gnus-add-configuration (conf)
2396   (setq gnus-buffer-configuration 
2397         (cons conf (delq (assq (car conf) gnus-buffer-configuration)
2398                          gnus-buffer-configuration))))
2399
2400 (defun gnus-configure-windows (setting &optional force)
2401   (setq setting (gnus-windows-old-to-new setting))
2402   (let ((r (if (symbolp setting)
2403                (cdr (assq setting gnus-buffer-configuration))
2404              setting))
2405         (in-buf (current-buffer))
2406         rule val w height hor ohor heights sub jump-buffer
2407         rel total to-buf all-visible)
2408     (or r (error "No such setting: %s" setting))
2409
2410     (if (and (not force) (setq all-visible (gnus-all-windows-visible-p r)))
2411         ;; All the windows mentioned are already visibe, so we just
2412         ;; put point in the assigned buffer, and do not touch the
2413         ;; winconf. 
2414         (select-window (get-buffer-window all-visible))
2415
2416       ;; Either remove all windows or just remove all Gnus windows.
2417       (if gnus-use-full-window
2418           (delete-other-windows)
2419         (gnus-remove-some-windows)
2420         (switch-to-buffer nntp-server-buffer))
2421
2422       (while r
2423         (setq hor (car r)
2424               ohor nil)
2425
2426         ;; We have to do the (possible) horizontal splitting before the
2427         ;; vertical. 
2428         (if (and (listp (car hor)) 
2429                  (eq (car (car hor)) 'horizontal))
2430             (progn
2431               (split-window 
2432                nil
2433                (if (integerp (nth 1 (car hor)))
2434                    (nth 1 (car hor))
2435                  (- (frame-width) (floor (* (frame-width) (nth 1 (car hor))))))
2436                t)
2437               (setq hor (cdr hor))))
2438
2439         ;; Go through the rules and eval the elements that are to be
2440         ;; evaled.  
2441         (while hor
2442           (if (setq val (if (vectorp (car hor)) (car hor) (eval (car hor))))
2443               (progn
2444                 ;; Expand short buffer name.
2445                 (setq w (aref val 0))
2446                 (and (setq w (cdr (assq w gnus-window-to-buffer)))
2447                      (progn
2448                        (setq val (apply 'vector (mapcar 'identity val)))
2449                        (aset val 0 w)))
2450                 (setq ohor (cons val ohor))))
2451           (setq hor (cdr hor)))
2452         (setq rule (cons (nreverse ohor) rule))
2453         (setq r (cdr r)))
2454       (setq rule (nreverse rule))
2455
2456       ;; We tally the window sizes.
2457       (setq total (window-height))
2458       (while rule
2459         (setq hor (car rule))
2460         (if (and (listp (car hor)) (eq (car (car hor)) 'horizontal))
2461             (setq hor (cdr hor)))
2462         (setq sub 0)
2463         (while hor
2464           (setq rel (aref (car hor) 1)
2465                 heights (cons
2466                          (cond ((and (floatp rel) (= 1.0 rel))
2467                                 'x)
2468                                ((integerp rel)
2469                                 rel)
2470                                (t
2471                                 (max (floor (* total rel)) 4)))
2472                          heights)
2473                 sub (+ sub (if (numberp (car heights)) (car heights) 0))
2474                 hor (cdr hor)))
2475         (setq heights (nreverse heights)
2476               hor (car rule))
2477
2478         ;; We then go through these heighs and create windows for them.
2479         (while heights
2480           (setq height (car heights)
2481                 heights (cdr heights))
2482           (and (eq height 'x)
2483                (setq height (- total sub)))
2484           (and heights
2485                (split-window nil height))
2486           (setq to-buf (aref (car hor) 0))
2487           (switch-to-buffer 
2488            (cond ((not to-buf)
2489                   in-buf)
2490                  ((symbolp to-buf)
2491                   (symbol-value (aref (car hor) 0)))
2492                  (t
2493                   (aref (car hor) 0))))
2494           (and (> (length (car hor)) 2)
2495                (eq (aref (car hor) 2) 'point)
2496                (setq jump-buffer (current-buffer)))
2497           (other-window 1)
2498           (setq hor (cdr hor)))
2499       
2500         (setq rule (cdr rule)))
2501
2502       ;; Finally, we pop to the buffer that's supposed to have point. 
2503       (or jump-buffer (error "Missing `point' in spec for %s" setting))
2504
2505       (select-window (get-buffer-window jump-buffer))
2506       (set-buffer jump-buffer))))
2507
2508 (defun gnus-all-windows-visible-p (rule)
2509   (let (invisible hor jump-buffer val buffer)
2510     ;; Go through the rules and eval the elements that are to be
2511     ;; evaled.  
2512     (while (and rule (not invisible))
2513       (setq hor (car rule)
2514             rule (cdr rule))
2515       (while (and hor (not invisible))
2516         (if (setq val (if (vectorp (car hor)) 
2517                           (car hor)
2518                         (if (not (eq (car (car hor)) 'horizontal))
2519                             (eval (car hor)))))
2520             (progn
2521               ;; Expand short buffer name.
2522               (setq buffer (or (cdr (assq (aref val 0) gnus-window-to-buffer))
2523                                (aref val 0)))
2524               (setq buffer (if (symbolp buffer) (symbol-value buffer)
2525                              buffer))
2526               (and (> (length val) 2) (eq 'point (aref val 2))
2527                    (setq jump-buffer buffer))
2528               (setq invisible (not (and buffer (get-buffer-window buffer))))))
2529         (setq hor (cdr hor))))
2530     (and (not invisible) jump-buffer)))
2531
2532       
2533 (defun gnus-remove-some-windows ()
2534   (let ((buffers gnus-window-to-buffer)
2535         (first t)
2536         buf)
2537     (save-excursion
2538       ;; Remove windows on all known Gnus buffers.
2539       (while buffers
2540         (setq buf (cdr (car buffers)))
2541         (if (symbolp buf)
2542             (setq buf (and (boundp buf) (symbol-value buf))))
2543         (and buf 
2544              (get-buffer-window buf)
2545              (progn
2546                (if first
2547                    (progn
2548                      (pop-to-buffer buf)
2549                      (switch-to-buffer nntp-server-buffer)
2550                      (setq first nil))
2551                  (delete-window (get-buffer-window buf)))))
2552         (setq buffers (cdr buffers)))
2553       ;; Remove windows on *all* summary buffers.
2554       (let ((buffers (buffer-list)))
2555         (while buffers
2556           (if (and (string-match 
2557                     "^\\*Summary" (or (buffer-name (car buffers)) ""))
2558                    (get-buffer-window (car buffers)))
2559               (delete-window (get-buffer-window (car buffers))))
2560           (setq buffers (cdr buffers)))))))
2561                           
2562 (defun gnus-version ()
2563   "Version numbers of this version of Gnus."
2564   (interactive)
2565   (let ((methods gnus-valid-select-methods)
2566         (mess gnus-version)
2567         meth)
2568     ;; Go through all the legal select methods and add their version
2569     ;; numbers to the total version string. Only the backends that are
2570     ;; currently in use will have their message numbers taken into
2571     ;; consideration. 
2572     (while methods
2573       (setq meth (intern (concat (car (car methods)) "-version")))
2574       (and (boundp meth)
2575            (stringp (symbol-value meth))
2576            (setq mess (concat mess "; " (symbol-value meth))))
2577       (setq methods (cdr methods)))
2578     (gnus-message 2 mess)))
2579
2580 (defun gnus-info-find-node ()
2581   "Find Info documentation of Gnus."
2582   (interactive)
2583   ;; Enlarge info window if needed.
2584   (let ((mode major-mode))
2585     (gnus-configure-windows 'info)
2586     (Info-goto-node (car (cdr (assq mode gnus-info-nodes))))))
2587
2588 (defun gnus-overload-functions (&optional overloads)
2589   "Overload functions specified by optional argument OVERLOADS.
2590 If nothing is specified, use the variable gnus-overload-functions."
2591   (let ((defs nil)
2592         (overloads (or overloads gnus-overload-functions)))
2593     (while overloads
2594       (setq defs (car overloads))
2595       (setq overloads (cdr overloads))
2596       ;; Load file before overloading function if necessary.  Make
2597       ;; sure we cannot use `require' always.
2598       (and (not (fboundp (car defs)))
2599            (car (cdr (cdr defs)))
2600            (load (car (cdr (cdr defs))) nil 'nomessage))
2601       (fset (car defs) (car (cdr defs))))))
2602
2603 (defun gnus-replace-chars-in-string (string from to)
2604   "Replace characters in STRING from FROM to TO."
2605   (let ((string (substring string 0))   ;Copy string.
2606         (len (length string))
2607         (idx 0))
2608     ;; Replace all occurrences of FROM with TO.
2609     (while (< idx len)
2610       (if (= (aref string idx) from)
2611           (aset string idx to))
2612       (setq idx (1+ idx)))
2613     string))
2614
2615 (defun gnus-days-between (date1 date2)
2616   ;; Return the number of days between date1 and date2.
2617   (- (gnus-day-number date1) (gnus-day-number date2)))
2618
2619 (defun gnus-day-number (date)
2620   (let ((dat (mapcar (lambda (s) (and s (string-to-int s)) )
2621                      (timezone-parse-date date))))
2622     (timezone-absolute-from-gregorian 
2623      (nth 1 dat) (nth 2 dat) (car dat))))
2624
2625 ;; Returns a floating point number that says how many seconds have
2626 ;; lapsed between Jan 1 12:00:00 1970 and DATE.
2627 (defun gnus-seconds-since-epoch (date)
2628   (let* ((tdate (mapcar (lambda (ti) (and ti (string-to-int ti)))
2629                         (timezone-parse-date date)))
2630          (ttime (mapcar (lambda (ti) (and ti (string-to-int ti)))
2631                         (timezone-parse-time
2632                          (aref (timezone-parse-date date) 3))))
2633          (edate (mapcar (lambda (ti) (and ti (string-to-int ti)))
2634                         (timezone-parse-date "Jan 1 12:00:00 1970")))
2635          (tday (- (timezone-absolute-from-gregorian 
2636                    (nth 1 tdate) (nth 2 tdate) (nth 0 tdate))
2637                   (timezone-absolute-from-gregorian 
2638                    (nth 1 edate) (nth 2 edate) (nth 0 edate)))))
2639     (+ (nth 2 ttime)
2640        (* (nth 1 ttime) 60)
2641        (* 1.0 (nth 0 ttime) 60 60)
2642        (* 1.0 tday 60 60 24))))
2643
2644 (defun gnus-file-newer-than (file date)
2645   (let ((fdate (nth 5 (file-attributes file))))
2646     (or (> (car fdate) (car date))
2647         (and (= (car fdate) (car date))
2648              (> (nth 1 fdate) (nth 1 date))))))
2649
2650 ;; Two silly functions to ensure that all `y-or-n-p' questions clear
2651 ;; the echo area.
2652 (defun gnus-y-or-n-p (prompt)
2653   (prog1
2654       (y-or-n-p prompt)
2655     (message "")))
2656
2657 (defun gnus-yes-or-no-p (prompt)
2658   (prog1
2659       (yes-or-no-p prompt)
2660     (message "")))
2661
2662 ;; Check whether to use long file names.
2663 (defun gnus-use-long-file-name (symbol)
2664   ;; The variable has to be set...
2665   (and gnus-use-long-file-name
2666        ;; If it isn't a list, then we return t.
2667        (or (not (listp gnus-use-long-file-name))
2668            ;; If it is a list, and the list contains `symbol', we
2669            ;; return nil.  
2670            (not (memq symbol gnus-use-long-file-name)))))
2671
2672 ;; I suspect there's a better way, but I haven't taken the time to do
2673 ;; it yet. -erik selberg@cs.washington.edu
2674 (defun gnus-dd-mmm (messy-date)
2675   "Return a string like DD-MMM from a big messy string"
2676   (let ((datevec (timezone-parse-date messy-date)))
2677     (format "%2s-%s"
2678             (or (aref datevec 2) "??")
2679             (capitalize
2680              (or (car 
2681                   (nth (1- (string-to-number (aref datevec 1)))
2682                        timezone-months-assoc))
2683                  "???")))))
2684
2685 ;; Make a hash table (default and minimum size is 255).
2686 ;; Optional argument HASHSIZE specifies the table size.
2687 (defun gnus-make-hashtable (&optional hashsize)
2688   (make-vector (if hashsize (max (gnus-create-hash-size hashsize) 255) 255) 0))
2689
2690 ;; Make a number that is suitable for hashing; bigger than MIN and one
2691 ;; less than 2^x.
2692 (defun gnus-create-hash-size (min)
2693   (let ((i 1))
2694     (while (< i min)
2695       (setq i (* 2 i)))
2696     (1- i)))
2697
2698 ;; Show message if message has a lower level than `gnus-verbose'. 
2699 ;; Guide-line for numbers:
2700 ;; 1 - error messages, 3 - non-serious error messages, 5 - messages
2701 ;; for things that take a long time, 7 - not very important messages
2702 ;; on stuff, 9 - messages inside loops.
2703 (defun gnus-message (level &rest args)
2704   (if (<= level gnus-verbose)
2705       (apply 'message args)
2706     ;; We have to do this format thingie here even if the result isn't
2707     ;; shown - the return value has to be the same as the return value
2708     ;; from `message'.
2709     (apply 'format args)))
2710
2711 ;; Generate a unique new group name.
2712 (defun gnus-generate-new-group-name (leaf)
2713   (let ((name leaf)
2714         (num 0))
2715     (while (gnus-gethash name gnus-newsrc-hashtb)
2716       (setq name (concat leaf "<" (int-to-string (setq num (1+ num))) ">")))
2717     name))
2718
2719 (defun gnus-find-file-noselect (file &optional force)
2720   "Does vaguely the same as find-file-noselect. No hooks are run."
2721   (let (buf insert)
2722     (if (setq buf (get-file-buffer file))
2723         (setq insert force)
2724       (setq buf (create-file-buffer file))
2725       (setq insert t))
2726     (if (not insert)
2727         buf
2728       (save-excursion
2729         (set-buffer buf)
2730         (erase-buffer)
2731         (and (file-readable-p file)
2732              (insert-file-contents file))
2733         (set-visited-file-name file)
2734         (set-buffer-modified-p nil)
2735         (current-buffer)))))
2736
2737 ;;; List and range functions
2738
2739 (defun gnus-last-element (list)
2740   "Return last element of LIST."
2741   (while (cdr list)
2742     (setq list (cdr list)))
2743   (car list))
2744
2745 (defun gnus-copy-sequence (list)
2746   "Do a complete, total copy of a list."
2747   (if (and (consp list) (not (consp (cdr list))))
2748       (cons (car list) (cdr list))
2749     (mapcar (lambda (elem) (if (consp elem) 
2750                                (if (consp (cdr elem))
2751                                    (gnus-copy-sequence elem)
2752                                  (cons (car elem) (cdr elem)))
2753                              elem))
2754             list)))
2755
2756 (defun gnus-set-difference (list1 list2)
2757   "Return a list of elements of LIST1 that do not appear in LIST2."
2758   (let ((list1 (copy-sequence list1)))
2759     (while list2
2760       (setq list1 (delq (car list2) list1))
2761       (setq list2 (cdr list2)))
2762     list1))
2763
2764 (defun gnus-sorted-complement (list1 list2)
2765   "Return a list of elements of LIST1 that do not appear in LIST2.
2766 Both lists have to be sorted over <."
2767   (let (out)
2768     (if (or (null list1) (null list2))
2769         (or list1 list2)
2770       (while (and list1 list2)
2771         (cond ((= (car list1) (car list2))
2772                (setq list1 (cdr list1)
2773                      list2 (cdr list2)))
2774               ((< (car list1) (car list2))
2775                (setq out (cons (car list1) out))
2776                (setq list1 (cdr list1)))
2777               (t
2778                (setq out (cons (car list2) out))
2779                (setq list2 (cdr list2)))))
2780       (nconc (nreverse out) (or list1 list2)))))
2781
2782 (defun gnus-intersection (list1 list2)      
2783   (let ((result nil))
2784     (while list2
2785       (if (memq (car list2) list1)
2786           (setq result (cons (car list2) result)))
2787       (setq list2 (cdr list2)))
2788     result))
2789
2790 (defun gnus-sorted-intersection (list1 list2)
2791   ;; LIST1 and LIST2 have to be sorted over <.
2792   (let (out)
2793     (while (and list1 list2)
2794       (cond ((= (car list1) (car list2))
2795              (setq out (cons (car list1) out)
2796                    list1 (cdr list1)
2797                    list2 (cdr list2)))
2798             ((< (car list1) (car list2))
2799              (setq list1 (cdr list1)))
2800             (t
2801              (setq list2 (cdr list2)))))
2802     (nreverse out)))
2803
2804 (defun gnus-set-sorted-intersection (list1 list2)
2805   ;; LIST1 and LIST2 have to be sorted over <.
2806   ;; This function modifies LIST1.
2807   (let* ((top (cons nil list1))
2808          (prev top))
2809   (while (and list1 list2)
2810     (cond ((= (car list1) (car list2))
2811            (setq prev list1
2812                  list1 (cdr list1)
2813                  list2 (cdr list2)))
2814           ((< (car list1) (car list2))
2815            (setcdr prev (cdr list1))
2816            (setq list1 (cdr list1)))
2817           (t
2818            (setq list2 (cdr list2)))))
2819   (setcdr prev nil)
2820   (cdr top)))
2821
2822 (defun gnus-compress-sequence (numbers &optional always-list)
2823   "Convert list of numbers to a list of ranges or a single range.
2824 If ALWAYS-LIST is non-nil, this function will always release a list of
2825 ranges."
2826   (let* ((first (car numbers))
2827          (last (car numbers))
2828          result)
2829     (if (null numbers)
2830         nil
2831       (if (not (listp (cdr numbers)))
2832           numbers
2833         (while numbers
2834           (cond ((= last (car numbers)) nil) ;Omit duplicated number
2835                 ((= (1+ last) (car numbers)) ;Still in sequence
2836                  (setq last (car numbers)))
2837                 (t                      ;End of one sequence
2838                  (setq result 
2839                        (cons (if (= first last) first
2840                                (cons first last)) result))
2841                  (setq first (car numbers))
2842                  (setq last  (car numbers))))
2843           (setq numbers (cdr numbers)))
2844         (if (and (not always-list) (null result))
2845             (if (= first last) (list first) (cons first last))
2846           (nreverse (cons (if (= first last) first (cons first last))
2847                           result)))))))
2848
2849 (defalias 'gnus-uncompress-sequence 'gnus-uncompress-range)
2850 (defun gnus-uncompress-range (ranges)
2851   "Expand a list of ranges into a list of numbers.
2852 RANGES is either a single range on the form `(num . num)' or a list of
2853 these ranges."
2854   (let (first last result)
2855     (cond 
2856      ((null ranges)
2857       nil)
2858      ((not (listp (cdr ranges)))
2859       (setq first (car ranges))
2860       (setq last (cdr ranges))
2861       (while (<= first last)
2862         (setq result (cons first result))
2863         (setq first (1+ first)))
2864       (nreverse result))
2865      (t
2866       (while ranges
2867         (if (atom (car ranges))
2868             (if (numberp (car ranges))
2869                 (setq result (cons (car ranges) result)))
2870           (setq first (car (car ranges)))
2871           (setq last  (cdr (car ranges)))
2872           (while (<= first last)
2873             (setq result (cons first result))
2874             (setq first (1+ first))))
2875         (setq ranges (cdr ranges)))
2876       (nreverse result)))))
2877
2878 (defun gnus-add-to-range (ranges list)
2879   "Return a list of ranges that has all articles from both RANGES and LIST.
2880 Note: LIST has to be sorted over `<'."
2881   (if (not ranges)
2882       (gnus-compress-sequence list t)
2883     (setq list (copy-sequence list))
2884     (or (listp (cdr ranges))
2885         (setq ranges (list ranges)))
2886     (let ((out ranges)
2887           ilist lowest highest temp)
2888       (while (and ranges list)
2889         (setq ilist list)
2890         (setq lowest (or (and (atom (car ranges)) (car ranges))
2891                          (car (car ranges))))
2892         (while (and list (cdr list) (< (car (cdr list)) lowest))
2893           (setq list (cdr list)))
2894         (if (< (car ilist) lowest)
2895             (progn
2896               (setq temp list)
2897               (setq list (cdr list))
2898               (setcdr temp nil)
2899               (setq out (nconc (gnus-compress-sequence ilist t) out))))
2900         (setq highest (or (and (atom (car ranges)) (car ranges))
2901                           (cdr (car ranges))))
2902         (while (and list (<= (car list) highest))
2903           (setq list (cdr list)))
2904         (setq ranges (cdr ranges)))
2905       (if list
2906           (setq out (nconc (gnus-compress-sequence list t) out)))
2907       (setq out (sort out (lambda (r1 r2) 
2908                             (< (or (and (atom r1) r1) (car r1))
2909                                (or (and (atom r2) r2) (car r2))))))
2910       (setq ranges out)
2911       (while ranges
2912         (if (atom (car ranges))
2913             (if (cdr ranges)
2914                 (if (atom (car (cdr ranges)))
2915                     (if (= (1+ (car ranges)) (car (cdr ranges)))
2916                         (progn
2917                           (setcar ranges (cons (car ranges) 
2918                                                (car (cdr ranges))))
2919                           (setcdr ranges (cdr (cdr ranges)))))
2920                   (if (= (1+ (car ranges)) (car (car (cdr ranges))))
2921                       (progn
2922                         (setcar (car (cdr ranges)) (car ranges))
2923                         (setcar ranges (car (cdr ranges)))
2924                         (setcdr ranges (cdr (cdr ranges)))))))
2925           (if (cdr ranges)
2926               (if (atom (car (cdr ranges)))
2927                   (if (= (1+ (cdr (car ranges))) (car (cdr ranges)))
2928                       (progn
2929                         (setcdr (car ranges) (car (cdr ranges)))
2930                         (setcdr ranges (cdr (cdr ranges)))))
2931                 (if (= (1+ (cdr (car ranges))) (car (car (cdr ranges))))
2932                     (progn
2933                       (setcdr (car ranges) (cdr (car (cdr ranges))))
2934                       (setcdr ranges (cdr (cdr ranges))))))))
2935         (setq ranges (cdr ranges)))
2936       out)))
2937
2938 (defun gnus-remove-from-range (ranges list)
2939   "Return a list of ranges that has all articles from LIST removed from RANGES.
2940 Note: LIST has to be sorted over `<'."
2941   ;; !!! This function shouldn't look like this, but I've got a headache.
2942   (gnus-compress-sequence 
2943    (gnus-sorted-complement
2944     (gnus-uncompress-range ranges) list)))
2945
2946 (defun gnus-member-of-range (number ranges)
2947   (if (not (listp (cdr ranges)))
2948       (and (>= number (car ranges)) 
2949            (<= number (cdr ranges)))
2950     (let ((not-stop t))
2951       (while (and ranges 
2952                   (if (numberp (car ranges))
2953                       (>= number (car ranges))
2954                     (>= number (car (car ranges))))
2955                   not-stop)
2956         (if (if (numberp (car ranges))
2957                 (= number (car ranges))
2958               (and (>= number (car (car ranges)))
2959                    (<= number (cdr (car ranges)))))
2960             (setq not-stop nil))
2961         (setq ranges (cdr ranges)))
2962       (not not-stop))))
2963
2964 \f
2965 ;;;
2966 ;;; Gnus group mode
2967 ;;;
2968
2969 (defvar gnus-group-mode-map nil)
2970 (defvar gnus-group-group-map nil)
2971 (defvar gnus-group-mark-map nil)
2972 (defvar gnus-group-list-map nil)
2973 (defvar gnus-group-sub-map nil)
2974 (put 'gnus-group-mode 'mode-class 'special)
2975
2976 (if gnus-group-mode-map
2977     nil
2978   (setq gnus-group-mode-map (make-keymap))
2979   (suppress-keymap gnus-group-mode-map)
2980   (define-key gnus-group-mode-map " " 'gnus-group-read-group)
2981   (define-key gnus-group-mode-map "=" 'gnus-group-select-group)
2982   (define-key gnus-group-mode-map "\r" 'gnus-group-select-group)
2983   (define-key gnus-group-mode-map "j" 'gnus-group-jump-to-group)
2984   (define-key gnus-group-mode-map "n" 'gnus-group-next-unread-group)
2985   (define-key gnus-group-mode-map "p" 'gnus-group-prev-unread-group)
2986   (define-key gnus-group-mode-map "\177" 'gnus-group-prev-unread-group)
2987   (define-key gnus-group-mode-map "N" 'gnus-group-next-group)
2988   (define-key gnus-group-mode-map "P" 'gnus-group-prev-group)
2989   (define-key gnus-group-mode-map "\M-n" 'gnus-group-next-unread-group-same-level)
2990   (define-key gnus-group-mode-map "\M-p" 'gnus-group-prev-unread-group-same-level)
2991   (define-key gnus-group-mode-map "," 'gnus-group-best-unread-group)
2992   (define-key gnus-group-mode-map "." 'gnus-group-first-unread-group)
2993   (define-key gnus-group-mode-map "u" 'gnus-group-unsubscribe-current-group)
2994   (define-key gnus-group-mode-map "U" 'gnus-group-unsubscribe-group)
2995   (define-key gnus-group-mode-map "c" 'gnus-group-catchup-current)
2996   (define-key gnus-group-mode-map "C" 'gnus-group-catchup-current-all)
2997   (define-key gnus-group-mode-map "l" 'gnus-group-list-groups)
2998   (define-key gnus-group-mode-map "L" 'gnus-group-list-all-groups)
2999   (define-key gnus-group-mode-map "m" 'gnus-group-mail)
3000   (define-key gnus-group-mode-map "g" 'gnus-group-get-new-news)
3001   (define-key gnus-group-mode-map "\M-g" 'gnus-group-get-new-news-this-group)
3002   (define-key gnus-group-mode-map "R" 'gnus-group-restart)
3003   (define-key gnus-group-mode-map "r" 'gnus-group-read-init-file)
3004   (define-key gnus-group-mode-map "B" 'gnus-group-browse-foreign-server)
3005   (define-key gnus-group-mode-map "b" 'gnus-group-check-bogus-groups)
3006   (define-key gnus-group-mode-map "F" 'gnus-find-new-newsgroups)
3007   (define-key gnus-group-mode-map "\C-c\C-d" 'gnus-group-describe-group)
3008   (define-key gnus-group-mode-map "\M-d" 'gnus-group-describe-all-groups)
3009   (define-key gnus-group-mode-map "\C-c\C-a" 'gnus-group-apropos)
3010   (define-key gnus-group-mode-map "\C-c\M-C-a" 'gnus-group-description-apropos)
3011   (define-key gnus-group-mode-map "a" 'gnus-group-post-news)
3012   (define-key gnus-group-mode-map "\ek" 'gnus-group-edit-local-kill)
3013   (define-key gnus-group-mode-map "\eK" 'gnus-group-edit-global-kill)
3014   (define-key gnus-group-mode-map "\C-k" 'gnus-group-kill-group)
3015   (define-key gnus-group-mode-map "\C-y" 'gnus-group-yank-group)
3016   (define-key gnus-group-mode-map "\C-w" 'gnus-group-kill-region)
3017   (define-key gnus-group-mode-map "\C-x\C-t" 'gnus-group-transpose-groups)
3018   (define-key gnus-group-mode-map "\C-c\C-l" 'gnus-group-list-killed)
3019   (define-key gnus-group-mode-map "\C-c\C-x" 'gnus-group-expire-articles)
3020   (define-key gnus-group-mode-map "\C-c\M-\C-x" 'gnus-group-expire-all-groups)
3021   (define-key gnus-group-mode-map "V" 'gnus-version)
3022   (define-key gnus-group-mode-map "s" 'gnus-group-save-newsrc)
3023   (define-key gnus-group-mode-map "z" 'gnus-group-suspend)
3024   (define-key gnus-group-mode-map "Z" 'gnus-group-clear-dribble)
3025   (define-key gnus-group-mode-map "q" 'gnus-group-exit)
3026   (define-key gnus-group-mode-map "Q" 'gnus-group-quit)
3027   (define-key gnus-group-mode-map "\M-f" 'gnus-group-fetch-faq)
3028   (define-key gnus-group-mode-map "?" 'gnus-group-describe-briefly)
3029   (define-key gnus-group-mode-map "\C-c\C-i" 'gnus-info-find-node)
3030   (define-key gnus-group-mode-map "\M-e" 'gnus-group-edit-group-method)
3031   (define-key gnus-group-mode-map "^" 'gnus-group-enter-server-mode)
3032   (define-key gnus-group-mode-map gnus-mouse-2 'gnus-mouse-pick-group)
3033   (define-key gnus-group-mode-map "<" 'beginning-of-buffer)
3034   (define-key gnus-group-mode-map ">" 'end-of-buffer)
3035   (define-key gnus-group-mode-map "\C-c\C-b" 'gnus-bug)
3036   (define-key gnus-group-mode-map "\C-c\C-s" 'gnus-group-sort-groups)
3037
3038   (define-key gnus-group-mode-map "#" 'gnus-group-mark-group)
3039   (define-key gnus-group-mode-map "\M-#" 'gnus-group-unmark-group)
3040   (define-prefix-command 'gnus-group-mark-map)
3041   (define-key gnus-group-mode-map "M" 'gnus-group-mark-map)
3042   (define-key gnus-group-mark-map "m" 'gnus-group-mark-group)
3043   (define-key gnus-group-mark-map "u" 'gnus-group-unmark-group)
3044   (define-key gnus-group-mark-map "w" 'gnus-group-mark-region)
3045
3046   (define-prefix-command 'gnus-group-group-map)
3047   (define-key gnus-group-mode-map "G" 'gnus-group-group-map)
3048   (define-key gnus-group-group-map "d" 'gnus-group-make-directory-group)
3049   (define-key gnus-group-group-map "h" 'gnus-group-make-help-group)
3050   (define-key gnus-group-group-map "a" 'gnus-group-make-archive-group)
3051   (define-key gnus-group-group-map "k" 'gnus-group-make-kiboze-group)
3052   (define-key gnus-group-group-map "m" 'gnus-group-make-group)
3053   (define-key gnus-group-group-map "E" 'gnus-group-edit-group)
3054   (define-key gnus-group-group-map "e" 'gnus-group-edit-group-method)
3055   (define-key gnus-group-group-map "p" 'gnus-group-edit-group-parameters)
3056   (define-key gnus-group-group-map "v" 'gnus-group-add-to-virtual)
3057   (define-key gnus-group-group-map "V" 'gnus-group-make-empty-virtual)
3058   (define-key gnus-group-group-map "D" 'gnus-group-enter-directory)
3059   (define-key gnus-group-group-map "f" 'gnus-group-make-doc-group)
3060   (define-key gnus-group-group-map "sb" 'gnus-group-brew-soup)
3061   (define-key gnus-group-group-map "sw" 'gnus-soup-save-areas)
3062   (define-key gnus-group-group-map "ss" 'gnus-soup-send-replies)
3063   (define-key gnus-group-group-map "sp" 'gnus-soup-pack-packet)
3064   (define-key gnus-group-group-map "sr" 'nnsoup-pack-replies)
3065
3066   (define-prefix-command 'gnus-group-list-map)
3067   (define-key gnus-group-mode-map "A" 'gnus-group-list-map)
3068   (define-key gnus-group-list-map "k" 'gnus-group-list-killed)
3069   (define-key gnus-group-list-map "z" 'gnus-group-list-zombies)
3070   (define-key gnus-group-list-map "s" 'gnus-group-list-groups)
3071   (define-key gnus-group-list-map "u" 'gnus-group-list-all-groups)
3072   (define-key gnus-group-list-map "a" 'gnus-group-apropos)
3073   (define-key gnus-group-list-map "d" 'gnus-group-description-apropos)
3074   (define-key gnus-group-list-map "m" 'gnus-group-list-matching)
3075   (define-key gnus-group-list-map "M" 'gnus-group-list-all-matching)
3076
3077   (define-prefix-command 'gnus-group-sub-map)
3078   (define-key gnus-group-mode-map "S" 'gnus-group-sub-map)
3079   (define-key gnus-group-sub-map "l" 'gnus-group-set-current-level)
3080   (define-key gnus-group-sub-map "t" 'gnus-group-unsubscribe-current-group)
3081   (define-key gnus-group-sub-map "s" 'gnus-group-unsubscribe-group)
3082   (define-key gnus-group-sub-map "k" 'gnus-group-kill-group)
3083   (define-key gnus-group-sub-map "y" 'gnus-group-yank-group)
3084   (define-key gnus-group-sub-map "w" 'gnus-group-kill-region)
3085   (define-key gnus-group-sub-map "z" 'gnus-group-kill-all-zombies))
3086
3087 (defun gnus-group-mode ()
3088   "Major mode for reading news.
3089
3090 All normal editing commands are switched off.
3091 \\<gnus-group-mode-map>
3092 The group buffer lists (some of) the groups available.  For instance,
3093 `\\[gnus-group-list-groups]' will list all subscribed groups with unread articles, while `\\[gnus-group-list-zombies]'
3094 lists all zombie groups. 
3095
3096 Groups that are displayed can be entered with `\\[gnus-group-read-group]'.  To subscribe 
3097 to a group not displayed, type `\\[gnus-group-unsubscribe-group]'. 
3098
3099 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]'). 
3100
3101 The following commands are available:
3102
3103 \\{gnus-group-mode-map}"
3104   (interactive)
3105   (if gnus-visual (gnus-group-make-menu-bar))
3106   (kill-all-local-variables)
3107   (setq mode-line-modified "-- ")
3108   (make-local-variable 'mode-line-format)
3109   (setq mode-line-format (copy-sequence mode-line-format))
3110   (and (equal (nth 3 mode-line-format) "   ")
3111        (setcar (nthcdr 3 mode-line-format) ""))
3112   (setq major-mode 'gnus-group-mode)
3113   (setq mode-name "Group")
3114   (gnus-group-set-mode-line)
3115   (setq mode-line-process nil)
3116   (use-local-map gnus-group-mode-map)
3117   (buffer-disable-undo (current-buffer))
3118   (setq truncate-lines t)
3119   (setq buffer-read-only t)
3120   (run-hooks 'gnus-group-mode-hook))
3121
3122 (defun gnus-mouse-pick-group (e)
3123   (interactive "e")
3124   (mouse-set-point e)
3125   (gnus-group-read-group nil))
3126
3127 ;;;###autoload
3128 (defun gnus-no-server (&optional arg)
3129   "Read network news.
3130 If ARG is a positive number, Gnus will use that as the
3131 startup level. If ARG is nil, Gnus will be started at level 2. 
3132 If ARG is non-nil and not a positive number, Gnus will
3133 prompt the user for the name of an NNTP server to use.
3134 As opposed to `gnus', this command will not connect to the local server."
3135   (interactive "P")
3136   (setq gnus-group-use-permanent-levels t)
3137   (gnus (or arg (1- gnus-level-default-subscribed)) t))
3138
3139 (defalias '\(ding\) 'gnus)
3140
3141 ;;;###autoload
3142 (defun gnus (&optional arg dont-connect)
3143   "Read network news.
3144 If ARG is non-nil and a positive number, Gnus will use that as the
3145 startup level. If ARG is non-nil and not a positive number, Gnus will
3146 prompt the user for the name of an NNTP server to use."
3147   (interactive "P")
3148   (if (get-buffer gnus-group-buffer)
3149       (progn
3150         (switch-to-buffer gnus-group-buffer)
3151         (gnus-group-get-new-news))
3152     (gnus-clear-system)
3153     (nnheader-init-server-buffer)
3154     (gnus-read-init-file)
3155
3156     ;; Read the dribble file.
3157     (and gnus-use-dribble-file (gnus-dribble-read-file))
3158
3159     (let ((level (and arg (numberp arg) (> arg 0) arg))
3160           did-connect)
3161       (unwind-protect
3162           (progn
3163             (gnus-group-setup-buffer)
3164             (or dont-connect 
3165                 (setq did-connect
3166                       (gnus-start-news-server (and arg (not level))))))
3167         (if (and (not dont-connect) 
3168                  (not did-connect))
3169             (gnus-group-quit)
3170           (run-hooks 'gnus-startup-hook)
3171           ;; NNTP server is successfully open. 
3172           (gnus-update-format-specifications)
3173           (gnus-summary-make-display-table)
3174           (let ((buffer-read-only nil))
3175             (erase-buffer)
3176             (if (not gnus-inhibit-startup-message)
3177                 (progn
3178                   (gnus-group-startup-message)
3179                   (sit-for 0))))
3180           (gnus-setup-news nil level)
3181           (and gnus-use-dribble-file (gnus-dribble-open))
3182           (gnus-group-list-groups level)
3183           (gnus-configure-windows 'group))))))
3184
3185 (defun gnus-unload ()
3186   "Unload all Gnus features."
3187   (interactive)
3188   (let ((history load-history)
3189         feature)
3190     (while history
3191       (and (string-match "^gnus" (car (car history)))
3192            (setq feature (cdr (assq 'provide (car history))))
3193            (unload-feature feature 'force))
3194       (setq history (cdr history)))))
3195
3196 (defun gnus-group-startup-message (&optional x y)
3197   "Insert startup message in current buffer."
3198   ;; Insert the message.
3199   (erase-buffer)
3200   (insert
3201    (format "
3202      %s
3203            A newsreader 
3204       for GNU Emacs
3205
3206         Based on GNUS 
3207              written by 
3208      Masanobu UMEDA
3209
3210        A Praxis Release
3211       larsi@ifi.uio.no
3212
3213            gnus-version))
3214   ;; And then hack it.
3215   ;; 18 is the longest line.
3216   (indent-rigidly (point-min) (point-max) 
3217                   (/ (max (- (window-width) (or x 28)) 0) 2))
3218   (goto-char (point-min))
3219   ;; +4 is fuzzy factor.
3220   (insert-char ?\n (/ (max (- (window-height) (or y 12)) 0) 2))
3221
3222   ;; Fontify some.
3223   (goto-char (point-min))
3224   (search-forward "Praxis")
3225   (put-text-property (match-beginning 0) (match-end 0) 'face 'bold)
3226   (goto-char (point-min)))
3227
3228 (defun gnus-group-setup-buffer ()
3229   (or (get-buffer gnus-group-buffer)
3230       (progn
3231         (switch-to-buffer gnus-group-buffer)
3232         (gnus-add-current-to-buffer-list)
3233         (gnus-group-mode)
3234         (and gnus-carpal (gnus-carpal-setup-buffer 'group)))))
3235
3236 (defun gnus-group-list-groups (level &optional unread)
3237   "List newsgroups with level LEVEL or lower that have unread articles.
3238 Default is all subscribed groups.
3239 If argument UNREAD is non-nil, groups with no unread articles are also listed."
3240   (interactive (list (and current-prefix-arg
3241                           (prefix-numeric-value current-prefix-arg))))
3242   (if gnus-group-use-permanent-levels
3243       (progn
3244         (setq gnus-group-default-list-level 
3245               (or level gnus-group-default-list-level))
3246         (setq level (or gnus-group-default-list-level gnus-level-subscribed)))
3247     (setq level (or level gnus-group-default-list-level 
3248                     gnus-level-subscribed)))
3249   (gnus-group-setup-buffer)     ;May call from out of group buffer
3250   (let ((case-fold-search nil)
3251         (group (gnus-group-group-name)))
3252     (funcall gnus-group-prepare-function level unread nil)
3253     (if (zerop (buffer-size))
3254         (gnus-message 5 gnus-no-groups-message)
3255       (goto-char (point-min))
3256       (if (not group)
3257           ;; Go to the first group with unread articles.
3258           (gnus-group-search-forward nil nil nil t)
3259         ;; Find the right group to put point on. If the current group
3260         ;; has disapeared in the new listing, try to find the next
3261         ;; one. If no next one can be found, just leave point at the
3262         ;; first newsgroup in the buffer.
3263         (if (not (gnus-goto-char
3264                   (text-property-any (point-min) (point-max) 
3265                                      'gnus-group (intern group))))
3266             (let ((newsrc (nthcdr 3 (gnus-gethash group gnus-newsrc-hashtb))))
3267               (while (and newsrc
3268                           (not (gnus-goto-char 
3269                                 (text-property-any 
3270                                  (point-min) (point-max) 'gnus-group 
3271                                  (intern (car (car newsrc)))))))
3272                 (setq newsrc (cdr newsrc)))
3273               (or newsrc (progn (goto-char (point-max))
3274                                 (forward-line -1))))))
3275       ;; Adjust cursor point.
3276       (gnus-group-position-cursor))))
3277
3278 (defun gnus-group-prepare-flat (level &optional all lowest regexp) 
3279   "List all newsgroups with unread articles of level LEVEL or lower.
3280 If ALL is non-nil, list groups that have no unread articles.
3281 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher.
3282 If REGEXP, only list groups matching REGEXP."
3283   (set-buffer gnus-group-buffer)
3284   (let ((buffer-read-only nil)
3285         (newsrc (cdr gnus-newsrc-alist))
3286         (lowest (or lowest 1))
3287         info clevel unread group)
3288     (erase-buffer)
3289     (if (< lowest gnus-level-zombie)
3290         ;; List living groups.
3291         (while newsrc
3292           (setq info (car newsrc)
3293                 group (car info)
3294                 newsrc (cdr newsrc)
3295                 unread (car (gnus-gethash group gnus-newsrc-hashtb)))
3296           (and unread ; This group might be bogus
3297                (or (not regexp)
3298                    (string-match regexp group))
3299                (<= (setq clevel (car (cdr info))) level) 
3300                (>= clevel lowest)
3301                (or all            ; We list all groups?
3302                    (eq unread t)  ; We list unactivated groups
3303                    (> unread 0)   ; We list groups with unread articles
3304                    (cdr (assq 'tick (nth 3 info)))) ; And groups with tickeds
3305                (gnus-group-insert-group-line 
3306                 nil group (car (cdr info)) (nth 3 info) unread (nth 4 info)))))
3307
3308     ;; List dead groups.
3309     (and (>= level gnus-level-zombie) (<= lowest gnus-level-zombie)
3310          (gnus-group-prepare-flat-list-dead 
3311           (setq gnus-zombie-list (sort gnus-zombie-list 'string<)) 
3312           gnus-level-zombie ?Z
3313           regexp))
3314     (and (>= level gnus-level-killed) (<= lowest gnus-level-killed)
3315          (gnus-group-prepare-flat-list-dead 
3316           (setq gnus-killed-list (sort gnus-killed-list 'string<)) 
3317           gnus-level-killed ?K regexp))
3318
3319     (gnus-group-set-mode-line)
3320     (setq gnus-have-all-newsgroups all)
3321     (run-hooks 'gnus-group-prepare-hook)))
3322
3323 (defun gnus-group-prepare-flat-list-dead (groups level mark regexp)
3324   ;; List zombies and killed lists somehwat faster, which was
3325   ;; suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>. It does
3326   ;; this by ignoring the group format specification altogether.
3327   (let (group beg)
3328     (while groups
3329       (setq group (car groups)
3330             groups (cdr groups))
3331       (if (or (not regexp)
3332               (string-match regexp group))
3333           (progn
3334             (setq beg (point))
3335             (insert (format " %c     *: %s\n" mark group))
3336             (add-text-properties 
3337              beg (1+ beg) 
3338              (list 'gnus-group (intern group)
3339                    'gnus-unread t
3340                    'gnus-level level)))))))
3341
3342 (defun gnus-group-real-name (group)
3343   "Find the real name of a foreign newsgroup."
3344   (if (string-match ":[^:]+$" group)
3345       (substring group (1+ (match-beginning 0)))
3346     group))
3347
3348 (defun gnus-group-prefixed-name (group method)
3349   "Return the whole name from GROUP and METHOD."
3350   (and (stringp method) (setq method (gnus-server-to-method method)))
3351   (concat (format "%s" (car method))
3352           (if (and 
3353                (assoc (format "%s" (car method)) (gnus-methods-using 'address))
3354                (not (string= (nth 1 method) "")))
3355               (concat "+" (nth 1 method)))
3356           ":" group))
3357
3358 (defun gnus-group-real-prefix (group)
3359   "Return the prefix of the current group name."
3360   (if (string-match "^[^:]+:" group)
3361       (substring group 0 (match-end 0))
3362     ""))
3363
3364 (defun gnus-group-method-name (group)
3365   "Return the method used for selecting GROUP."
3366   (let ((prefix (gnus-group-real-prefix group)))
3367     (if (equal prefix "")
3368         gnus-select-method
3369       (if (string-match "^[^\\+]+\\+" prefix)
3370           (list (intern (substring prefix 0 (1- (match-end 0))))
3371                 (substring prefix (match-end 0) (1- (length prefix))))
3372         (list (intern (substring prefix 0 (1- (length prefix)))) "")))))
3373
3374 (defun gnus-group-foreign-p (group)
3375   "Return nil if GROUP is native, non-nil if it is foreign."
3376   (string-match ":" group))
3377
3378 (defun gnus-group-set-info (info &optional method-only-group part)
3379   (let* ((entry (gnus-gethash
3380                  (or method-only-group (car info)) gnus-newsrc-hashtb))
3381          (part-info info)
3382          (info (if method-only-group (nth 2 entry) info)))
3383     (if (not method-only-group)
3384         ()
3385       (or entry
3386           (error "Trying to change non-existent group %s" method-only-group))
3387       ;; We have recevied parts of the actual group info - either the
3388       ;; select method or the group parameters.  We first check
3389       ;; whether we have to extend the info, and if so, do that.
3390       (let ((len (length info))
3391             (total (if (eq part 'method) 5 6)))
3392         (and (< len total)
3393              (setcdr (nthcdr (1- len) info)
3394                      (make-list (- total len) nil)))
3395         ;; Then we enter the new info.
3396         (setcar (nthcdr (1- total) info) part-info)))
3397     ;; We uncompress some lists of marked articles.
3398     (let (marked)
3399       (if (not (setq marked (nth 3 info)))
3400           ()
3401         (while marked
3402           (or (eq 'score (car (car marked)))
3403               (eq 'bookmark (car (car marked)))
3404               (eq 'killed (car (car marked)))
3405               (setcdr (car marked) 
3406                       (gnus-uncompress-range (cdr (car marked)))))
3407           (setq marked (cdr marked)))))
3408     (if entry
3409         ()
3410       ;; This is a new group, so we just create it.
3411       (save-excursion
3412         (set-buffer gnus-group-buffer)
3413         (if (nth 4 info)
3414             ;; It's a foreign group...
3415             (gnus-group-make-group 
3416              (gnus-group-real-name (car info))
3417              (prin1-to-string (car (nth 4 info)))
3418              (nth 1 (nth 4 info)))
3419           ;; It's a native group.
3420           (gnus-group-make-group
3421            (car info)
3422            (prin1-to-string (car gnus-select-method))
3423            (nth 1 gnus-select-method)))
3424         (gnus-message 6 "Note: New group created")
3425         (setq entry 
3426               (gnus-gethash (gnus-group-prefixed-name 
3427                              (gnus-group-real-name (car info))
3428                              (or (nth 4 info) gnus-select-method))
3429                             gnus-newsrc-hashtb))))
3430     ;; Whether it was a new group or not, we now have the entry, so we
3431     ;; can do the update.
3432     (if entry
3433         (progn
3434           (setcar (nthcdr 2 entry) info)
3435           (if (and (not (eq (car entry) t)) 
3436                    (gnus-gethash (car info) gnus-active-hashtb))
3437               (let ((marked (nth 3 info)))
3438                 (setcar entry 
3439                         (max 0 (- (length (gnus-list-of-unread-articles 
3440                                            (car info)))
3441                                   (length (cdr (assq 'tick marked)))
3442                                   (length (cdr (assq 'dormant marked)))))))))
3443       (error "No such group: %s" (car info)))))
3444
3445 (defun gnus-group-set-method-info (group select-method)
3446   (gnus-group-set-info select-method group 'method))
3447
3448 (defun gnus-group-set-params-info (group params)
3449   (gnus-group-set-info params group 'params))
3450
3451 (defun gnus-group-update-group-line ()
3452   "This function updates the current line in the newsgroup buffer and
3453 moves the point to the colon."
3454   (let* ((buffer-read-only nil)
3455          (group (gnus-group-group-name))
3456          (entry (and group (gnus-gethash group gnus-newsrc-hashtb))))
3457     (if entry
3458         (gnus-dribble-enter 
3459          (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
3460                  ")")))
3461     (beginning-of-line)
3462     (delete-region (point) (progn (forward-line 1) (point)))
3463     (gnus-group-insert-group-line-info group)
3464     (forward-line -1)
3465     (gnus-group-position-cursor)))
3466
3467 (defun gnus-group-insert-group-line-info (group)
3468   (let ((entry (gnus-gethash group gnus-newsrc-hashtb)) 
3469         active info)
3470     (if entry
3471         (progn
3472           (setq info (nth 2 entry))
3473           (gnus-group-insert-group-line 
3474            nil group (nth 1 info) (nth 3 info) (car entry) (nth 4 info)))
3475       (setq active (gnus-gethash group gnus-active-hashtb))
3476       (gnus-group-insert-group-line 
3477        nil group 
3478        (if (member group gnus-zombie-list) gnus-level-zombie gnus-level-killed)
3479        nil (if active (- (1+ (cdr active)) (car active)) 0) nil))))
3480
3481 (defun gnus-group-insert-group-line (gformat group level marked number method)
3482   (let* ((gformat (or gformat gnus-group-line-format-spec))
3483          (active (gnus-gethash group gnus-active-hashtb))
3484          (number-total (if active (1+ (- (cdr active) (car active))) 0))
3485          (number-of-dormant (length (cdr (assq 'dormant marked))))
3486          (number-of-ticked (length (cdr (assq 'tick marked))))
3487          (number-of-ticked-and-dormant
3488           (+ number-of-ticked number-of-dormant))
3489          (number-of-unread-unticked 
3490           (if (numberp number) (int-to-string (max 0 number))
3491             "*"))
3492          (number-of-read
3493           (if (numberp number)
3494               (max 0 (- number-total number))
3495             "*"))
3496          (subscribed (cond ((<= level gnus-level-subscribed) ? )
3497                            ((<= level gnus-level-unsubscribed) ?U)
3498                            ((= level gnus-level-zombie) ?Z)
3499                            (t ?K)))
3500          (qualified-group (gnus-group-real-name group))
3501          (newsgroup-description 
3502           (if gnus-description-hashtb
3503               (or (gnus-gethash group gnus-description-hashtb) "")
3504             ""))
3505          (moderated (if (member group gnus-moderated-list) ?m ? ))
3506          (moderated-string (if (eq moderated ?m) "(m)" ""))
3507          (method (gnus-server-get-method group method))
3508          (news-server (or (car (cdr method)) ""))
3509          (news-method (or (car method) ""))
3510          (news-method-string 
3511           (if method (format "(%s:%s)" (car method) (car (cdr method))) ""))
3512          (marked (if (and 
3513                       (numberp number) 
3514                       (zerop number)
3515                       (> number-of-ticked 0))
3516                      ?* ? ))
3517          (number (if (eq number t) "*" (+ number number-of-dormant 
3518                                           number-of-ticked)))
3519          (process-marked (if (member qualified-group gnus-group-marked)
3520                              gnus-process-mark ? ))
3521          (buffer-read-only nil)
3522          header ; passed as parameter to user-funcs.
3523          b)
3524     (beginning-of-line)
3525     (setq b (point))
3526     ;; Insert the text.
3527     (insert (eval gformat))
3528
3529     (add-text-properties 
3530      b (1+ b) (list 'gnus-group (intern group)
3531                     'gnus-unread (if (numberp number)
3532                                      (string-to-int number-of-unread-unticked)
3533                                    t)
3534                     'gnus-marked marked
3535                     'gnus-level level))))
3536
3537 (defun gnus-group-update-group (group &optional visible-only)
3538   "Update newsgroup info of GROUP.
3539 If VISIBLE-ONLY is non-nil, the group won't be displayed if it isn't already."
3540   (save-excursion
3541     (set-buffer gnus-group-buffer)
3542     (let ((buffer-read-only nil)
3543           visible)
3544       (let ((entry (gnus-gethash group gnus-newsrc-hashtb)))
3545         (if entry
3546             (gnus-dribble-enter 
3547              (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
3548                      ")"))))
3549       ;; Buffer may be narrowed.
3550       (save-restriction
3551         (widen)
3552         ;; Search a line to modify.  If the buffer is large, the search
3553         ;; takes long time.  In most cases, current point is on the line
3554         ;; we are looking for.  So, first of all, check current line. 
3555         (if (or (progn
3556                   (beginning-of-line)
3557                   (eq (get-text-property (point) 'gnus-group)
3558                       (intern group)))
3559                 (progn
3560                   (gnus-goto-char 
3561                    (text-property-any 
3562                     (point-min) (point-max) 'gnus-group (intern group)))))
3563             ;; GROUP is listed in current buffer. So, delete old line.
3564             (progn
3565               (setq visible t)
3566               (beginning-of-line)
3567               (delete-region (point) (progn (forward-line 1) (point))))
3568           ;; No such line in the buffer, find out where it's supposed to
3569           ;; go, and insert it there (or at the end of the buffer).
3570           ;; Fix by Per Abrahamsen <amanda@iesd.auc.dk>.
3571           (or visible-only
3572               (let ((entry 
3573                      (cdr (cdr (gnus-gethash group gnus-newsrc-hashtb)))))
3574                 (while (and entry
3575                             (car entry)
3576                             (not
3577                              (gnus-goto-char
3578                               (text-property-any
3579                                (point-min) (point-max) 
3580                                'gnus-group (intern (car (car entry)))))))
3581                   (setq entry (cdr entry)))
3582                 (or entry (goto-char (point-max)))))))
3583       (if (or visible (not visible-only))
3584           (gnus-group-insert-group-line-info group))
3585       (gnus-group-set-mode-line))))
3586
3587 (defun gnus-group-set-mode-line ()
3588   (if (memq 'group gnus-updated-mode-lines)
3589       (let* ((gformat (or gnus-group-mode-line-format-spec
3590                           (setq gnus-group-mode-line-format-spec
3591                                 (gnus-parse-format 
3592                                  gnus-group-mode-line-format 
3593                                  gnus-group-mode-line-format-alist))))
3594              (news-server (car (cdr gnus-select-method)))
3595              (news-method (car gnus-select-method))
3596              (max-len 60)
3597              (mode-string (eval gformat)))
3598         (setq mode-string (eval gformat))
3599         (if (> (length mode-string) max-len) 
3600             (setq mode-string (substring mode-string 0 (- max-len 4))))
3601         (setq mode-line-buffer-identification mode-string)
3602         (set-buffer-modified-p t))))
3603
3604 (defun gnus-group-group-name ()
3605   "Get the name of the newsgroup on the current line."
3606   (let ((group (get-text-property (gnus-point-at-bol) 'gnus-group)))
3607     (and group (symbol-name group))))
3608
3609 (defun gnus-group-group-level ()
3610   "Get the level of the newsgroup on the current line."
3611   (get-text-property (gnus-point-at-bol) 'gnus-level))
3612
3613 (defun gnus-group-group-unread ()
3614   "Get the number of unread articles of the newsgroup on the current line."
3615   (get-text-property (gnus-point-at-bol) 'gnus-unread))
3616
3617 (defun gnus-group-search-forward (&optional backward all level first-too)
3618   "Find the next newsgroup with unread articles.
3619 If BACKWARD is non-nil, find the previous newsgroup instead.
3620 If ALL is non-nil, just find any newsgroup.
3621 If LEVEL is non-nil, find group with level LEVEL, or higher if no such
3622 group exists.
3623 If FIRST-TOO, the current line is also eligible as a target."
3624   (let ((way (if backward -1 1))
3625         (low gnus-level-killed)
3626         (beg (point))
3627         pos found lev)
3628     (if (and backward (progn (beginning-of-line)) (bobp))
3629         nil
3630       (or first-too (forward-line way))
3631       (while (and 
3632               (not (eobp))
3633               (not (setq 
3634                     found 
3635                     (and (or all
3636                              (and
3637                               (let ((unread 
3638                                      (get-text-property (point) 'gnus-unread)))
3639                                 (or (eq unread t) (and unread (> unread 0))))
3640                               (setq lev (get-text-property (point)
3641                                                            'gnus-level))
3642                               (<= lev gnus-level-subscribed)))
3643                          (or (not level)
3644                              (and (setq lev (get-text-property (point)
3645                                                                'gnus-level))
3646                                   (or (= lev level)
3647                                       (and (< lev low)
3648                                            (< level lev)
3649                                            (progn
3650                                              (setq low lev)
3651                                              (setq pos (point))
3652                                              nil))))))))
3653               (zerop (forward-line way)))))
3654     (if found 
3655         (progn (gnus-group-position-cursor) t)
3656       (goto-char (or pos beg))
3657       (and pos t))))
3658
3659 ;;; Gnus group mode commands
3660
3661 ;; Group marking.
3662
3663 (defun gnus-group-mark-group (n &optional unmark no-advance)
3664   "Mark the current group."
3665   (interactive "p")
3666   (let ((buffer-read-only nil)
3667         group)
3668     (while 
3669         (and (> n 0) 
3670              (setq group (gnus-group-group-name))
3671              (progn
3672                (beginning-of-line)
3673                (forward-char 2)
3674                (delete-char 1)
3675                (if unmark
3676                    (progn
3677                      (insert " ")
3678                      (setq gnus-group-marked (delete group gnus-group-marked)))
3679                  (insert "#")
3680                  (setq gnus-group-marked
3681                        (cons group (delete group gnus-group-marked))))
3682                t)
3683              (or no-advance (zerop (gnus-group-next-group 1))))
3684       (setq n (1- n)))
3685     (gnus-summary-position-cursor)
3686     n))
3687
3688 (defun gnus-group-unmark-group (n)
3689   "Remove the mark from the current group."
3690   (interactive "p")
3691   (gnus-group-mark-group n 'unmark))
3692
3693 (defun gnus-group-mark-region (unmark beg end)
3694   "Mark all groups between point and mark.
3695 If UNMARK, remove the mark instead."
3696   (interactive "P\nr")
3697   (let ((num (count-lines beg end)))
3698     (save-excursion
3699       (goto-char beg)
3700       (- num (gnus-group-mark-group num unmark)))))
3701
3702 (defun gnus-group-remove-mark (group)
3703   (and (gnus-group-goto-group group)
3704        (save-excursion
3705          (gnus-group-mark-group 1 'unmark t))))
3706
3707 ;; Return a list of groups to work on.  Take into consideration N (the
3708 ;; prefix) and the list of marked groups.
3709 (defun gnus-group-process-prefix (n)
3710   (cond (n
3711          (setq n (prefix-numeric-value n))
3712          ;; There is a prefix, so we return a list of the N next
3713          ;; groups. 
3714          (let ((way (if (< n 0) -1 1))
3715                (n (abs n))
3716                group groups)
3717            (save-excursion
3718              (while (and (> n 0)
3719                          (setq group (gnus-group-group-name)))
3720                (setq groups (cons group groups))
3721                (setq n (1- n))
3722                (forward-line way)))
3723            (nreverse groups)))
3724         (gnus-group-marked
3725          ;; No prefix, but a list of marked articles.
3726          (reverse gnus-group-marked))
3727         (t
3728          ;; Neither marked articles or a prefix, so we return the
3729          ;; current group.
3730          (let ((group (gnus-group-group-name)))
3731            (and group (list group))))))
3732
3733 ;; Selecting groups.
3734
3735 (defun gnus-group-read-group (&optional all no-article group)
3736   "Read news in this newsgroup.
3737 If the prefix argument ALL is non-nil, already read articles become
3738 readable. If the optional argument NO-ARTICLE is non-nil, no article
3739 will be auto-selected upon group entry."
3740   (interactive "P")
3741   (let ((group (or group (gnus-group-group-name)))
3742         number active marked entry)
3743     (or group (error "No group on current line"))
3744     (setq marked 
3745           (nth 3 (nth 2 (setq entry (gnus-gethash group gnus-newsrc-hashtb)))))
3746     ;; This group might be a dead group. In that case we have to get
3747     ;; the number of unread articles from `gnus-active-hashtb'.
3748     (if entry
3749         (setq number (car entry))
3750       (if (setq active (gnus-gethash group gnus-active-hashtb))
3751           (setq number (- (1+ (cdr active)) (car active)))))
3752     (gnus-summary-read-group 
3753      group (or all (and (numberp number) 
3754                         (zerop (+ number (length (cdr (assq 'tick marked)))
3755                                   (length (cdr (assq 'dormant marked)))))))
3756      no-article)))
3757
3758 (defun gnus-group-select-group (&optional all)
3759   "Select this newsgroup.
3760 No article is selected automatically.
3761 If argument ALL is non-nil, already read articles become readable."
3762   (interactive "P")
3763   (gnus-group-read-group all t))
3764
3765 ;; Enter a group that is not in the group buffer. Non-nil is returned
3766 ;; if selection was successful.
3767 (defun gnus-group-read-ephemeral-group 
3768   (group method &optional activate quit-config)
3769   (let ((group (if (gnus-group-foreign-p group) group
3770                  (gnus-group-prefixed-name group method))))
3771     (gnus-sethash 
3772      group
3773      (list t nil (list group gnus-level-default-subscribed nil nil 
3774                        (append method
3775                                (list
3776                                 (list 'quit-config 
3777                                       (if quit-config quit-config
3778                                         (cons (current-buffer) 'summary)))))))
3779      gnus-newsrc-hashtb)
3780     (set-buffer gnus-group-buffer)
3781     (or (gnus-server-opened method)
3782         (gnus-open-server method)
3783         (error "Unable to contact server: %s" (gnus-status-message method)))
3784     (if activate (or (gnus-request-group group)
3785                      (error "Couldn't request group")))
3786     (condition-case ()
3787         (gnus-group-read-group t t group)
3788       (error nil)
3789       (quit nil))
3790     (not (equal major-mode 'gnus-group-mode))))
3791   
3792 (defun gnus-group-jump-to-group (group)
3793   "Jump to newsgroup GROUP."
3794   (interactive 
3795    (list (completing-read 
3796           "Group: " gnus-active-hashtb nil 
3797           (memq gnus-select-method gnus-have-read-active-file))))
3798
3799   (if (equal group "")
3800       (error "Empty group name"))
3801
3802   (let ((b (text-property-any 
3803             (point-min) (point-max) 'gnus-group (intern group))))
3804     (if b
3805         ;; Either go to the line in the group buffer...
3806         (goto-char b)
3807       ;; ... or insert the line.
3808       (or
3809        (gnus-gethash group gnus-active-hashtb)
3810        (gnus-activate-newsgroup group)
3811        (error "%s error: %s" group (gnus-status-message group)))
3812
3813       (gnus-group-update-group group)
3814       (goto-char (text-property-any 
3815                   (point-min) (point-max) 'gnus-group (intern group)))))
3816   ;; Adjust cursor point.
3817   (gnus-group-position-cursor))
3818
3819 (defun gnus-group-goto-group (group)
3820   "Goto to newsgroup GROUP."
3821   (let ((b (text-property-any (point-min) (point-max) 
3822                               'gnus-group (intern group))))
3823     (and b (goto-char b))))
3824
3825 (defun gnus-group-next-group (n)
3826   "Go to next N'th newsgroup.
3827 If N is negative, search backward instead.
3828 Returns the difference between N and the number of skips actually
3829 done."
3830   (interactive "p")
3831   (gnus-group-next-unread-group n t))
3832
3833 (defun gnus-group-next-unread-group (n &optional all level)
3834   "Go to next N'th unread newsgroup.
3835 If N is negative, search backward instead.
3836 If ALL is non-nil, choose any newsgroup, unread or not.
3837 If LEVEL is non-nil, choose the next group with level LEVEL, or, if no
3838 such group can be found, the next group with a level higher than
3839 LEVEL.
3840 Returns the difference between N and the number of skips actually
3841 made."
3842   (interactive "p")
3843   (let ((backward (< n 0))
3844         (n (abs n)))
3845     (while (and (> n 0)
3846                 (gnus-group-search-forward 
3847                  backward (or (not gnus-group-goto-unread) all) level))
3848       (setq n (1- n)))
3849     (if (/= 0 n) (gnus-message 7 "No more%s newsgroups%s" (if all "" " unread")
3850                                (if level " on this level or higher" "")))
3851     n))
3852
3853 (defun gnus-group-prev-group (n)
3854   "Go to previous N'th newsgroup.
3855 Returns the difference between N and the number of skips actually
3856 done."
3857   (interactive "p")
3858   (gnus-group-next-unread-group (- n) t))
3859
3860 (defun gnus-group-prev-unread-group (n)
3861   "Go to previous N'th unread newsgroup.
3862 Returns the difference between N and the number of skips actually
3863 done."  
3864   (interactive "p")
3865   (gnus-group-next-unread-group (- n)))
3866
3867 (defun gnus-group-next-unread-group-same-level (n)
3868   "Go to next N'th unread newsgroup on the same level.
3869 If N is negative, search backward instead.
3870 Returns the difference between N and the number of skips actually
3871 done."
3872   (interactive "p")
3873   (gnus-group-next-unread-group n t (gnus-group-group-level))
3874   (gnus-group-position-cursor))
3875
3876 (defun gnus-group-prev-unread-group-same-level (n)
3877   "Go to next N'th unread newsgroup on the same level.
3878 Returns the difference between N and the number of skips actually
3879 done."
3880   (interactive "p")
3881   (gnus-group-next-unread-group (- n) t (gnus-group-group-level))
3882   (gnus-group-position-cursor))
3883
3884 (defun gnus-group-best-unread-group (&optional exclude-group)
3885   "Go to the group with the highest level.
3886 If EXCLUDE-GROUP, do not go to that group."
3887   (interactive)
3888   (goto-char (point-min))
3889   (let ((best 100000)
3890         unread best-point)
3891     (while (setq unread (get-text-property (point) 'gnus-unread))
3892       (if (and (numberp unread) (> unread 0))
3893           (progn
3894             (if (and (< (get-text-property (point) 'gnus-level) best)
3895                      (or (not exclude-group)
3896                          (not (equal exclude-group (gnus-group-group-name)))))
3897                 (progn 
3898                   (setq best (get-text-property (point) 'gnus-level))
3899                   (setq best-point (point))))))
3900       (forward-line 1))
3901     (if best-point (goto-char best-point))
3902     (gnus-summary-position-cursor)
3903     (and best-point (gnus-group-group-name))))
3904
3905 (defun gnus-group-first-unread-group ()
3906   "Go to the first group with unread articles."
3907   (interactive)
3908   (goto-char (point-min))
3909   (or (not (zerop (or (get-text-property (point) 'gnus-unread) 0)))
3910       (gnus-group-next-unread-group 1))
3911   (gnus-group-position-cursor))
3912
3913 (defun gnus-group-enter-server-mode ()
3914   "Jump to the server buffer."
3915   (interactive)
3916   (gnus-server-setup-buffer)
3917   (gnus-configure-windows 'server)
3918   (gnus-server-prepare))
3919
3920 (defun gnus-group-make-group (name method &optional address)
3921   "Add a new newsgroup.
3922 The user will be prompted for a NAME, for a select METHOD, and an
3923 ADDRESS."
3924   (interactive
3925    (cons 
3926     (read-string "Group name: ")
3927     (let ((method
3928            (completing-read 
3929             "Method: " (append gnus-valid-select-methods gnus-server-alist)
3930             nil t)))
3931       (if (assoc method gnus-valid-select-methods)
3932           (list method
3933                 (if (memq 'prompt-address
3934                           (assoc method gnus-valid-select-methods))
3935                     (read-string "Address: ")
3936                   ""))
3937         (list method nil)))))
3938   
3939   (let* ((meth (if address (list (intern method) address) method))
3940          (nname (gnus-group-prefixed-name name meth))
3941          info)
3942     (and (gnus-gethash nname gnus-newsrc-hashtb)
3943          (error "Group %s already exists" nname))
3944     (gnus-group-change-level 
3945      (setq info (list t nname gnus-level-default-subscribed nil nil meth))
3946      gnus-level-default-subscribed gnus-level-killed 
3947      (and (gnus-group-group-name)
3948           (gnus-gethash (gnus-group-group-name)
3949                         gnus-newsrc-hashtb))
3950      t)
3951     (gnus-sethash nname (cons 1 0) gnus-active-hashtb)
3952     (gnus-dribble-enter 
3953      (concat "(gnus-group-set-info '" (prin1-to-string (cdr info)) ")"))
3954     (gnus-group-insert-group-line-info nname)
3955
3956     (if (assoc method gnus-valid-select-methods)
3957         (require (intern method)))
3958     (and (gnus-check-backend-function 'request-create-group nname)
3959          (gnus-request-create-group nname))))
3960
3961 (defun gnus-group-edit-group (group &optional part)
3962   "Edit the group on the current line."
3963   (interactive (list (gnus-group-group-name)))
3964   (let ((done-func '(lambda () 
3965                       "Exit editing mode and update the information."
3966                       (interactive)
3967                       (gnus-group-edit-group-done 'part 'group)))
3968         (part (or part 'info))
3969         (winconf (current-window-configuration))
3970         info)
3971     (or group (error "No group on current line"))
3972     (or (setq info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
3973         (error "Killed group; can't be edited"))
3974     (set-buffer (get-buffer-create gnus-group-edit-buffer))
3975     (gnus-configure-windows 'edit-group)
3976     (gnus-add-current-to-buffer-list)
3977     (emacs-lisp-mode)
3978     ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3979     (use-local-map (copy-keymap emacs-lisp-mode-map))
3980     (local-set-key "\C-c\C-c" done-func)
3981     (make-local-variable 'gnus-prev-winconf)
3982     (setq gnus-prev-winconf winconf)
3983     ;; We modify the func to let it know what part it is editing.
3984     (setcar (cdr (nth 4 done-func)) (list 'quote part))
3985     (setcar (cdr (cdr (nth 4 done-func))) group)
3986     (erase-buffer)
3987     (insert
3988      (cond 
3989       ((eq part 'method)
3990        ";; Type `C-c C-c' after editing the select method.\n\n")
3991       ((eq part 'params)
3992        ";; Type `C-c C-c' after editing the group parameters.\n\n")
3993       ((eq part 'info)
3994        ";; Type `C-c C-c' after editing the group info.\n\n")))
3995     (let ((cinfo (gnus-copy-sequence info))
3996           marked)
3997       (if (not (setq marked (nth 3 cinfo)))
3998           ()
3999         (while marked
4000           (or (eq 'score (car (car marked)))
4001               (eq 'bookmark (car (car marked)))
4002               (eq 'killed (car (car marked)))
4003               (not (numberp (car (cdr (car marked)))))
4004               (setcdr (car marked) 
4005                       (gnus-compress-sequence (sort (cdr (car marked)) '<) t)))
4006           (setq marked (cdr marked))))
4007       (insert 
4008        (pp-to-string
4009         (cond ((eq part 'method)
4010                (or (nth 4 info) "native"))
4011               ((eq part 'params)
4012                (nth 5 info))
4013               (t
4014                cinfo)))
4015        "\n"))))
4016
4017 (defun gnus-group-edit-group-method (group)
4018   "Edit the select method of GROUP."
4019   (interactive (list (gnus-group-group-name)))
4020   (gnus-group-edit-group group 'method))
4021
4022 (defun gnus-group-edit-group-parameters (group)
4023   "Edit the group parameters of GROUP."
4024   (interactive (list (gnus-group-group-name)))
4025   (gnus-group-edit-group group 'params))
4026
4027 (defun gnus-group-edit-group-done (part group)
4028   "Get info from buffer, update variables and jump to the group buffer."
4029   (set-buffer (get-buffer-create gnus-group-edit-buffer))
4030   (goto-char (point-min))
4031   (let ((form (read (current-buffer)))
4032         (winconf gnus-prev-winconf))
4033     (if (eq part 'info) 
4034         (gnus-group-set-info form)
4035       (gnus-group-set-info form group part))
4036     (kill-buffer (current-buffer))
4037     (and winconf (set-window-configuration winconf))
4038     (set-buffer gnus-group-buffer)
4039     (gnus-group-update-group (gnus-group-group-name))
4040     (gnus-group-position-cursor)))
4041
4042 (defun gnus-group-make-help-group ()
4043   "Create the (ding) Gnus documentation group."
4044   (interactive)
4045   (let ((path load-path)
4046         name)
4047     (and (gnus-gethash (setq name (gnus-group-prefixed-name
4048                                    "gnus-help" '(nndoc "gnus-help")))
4049                        gnus-newsrc-hashtb)
4050          (error "Documentation group already exists"))
4051     (while (and path
4052                 (not (file-exists-p (concat (file-name-as-directory (car path))
4053                                             "doc.txt"))))
4054       (setq path (cdr path)))
4055     (or path (error "Couldn't find doc group"))
4056     (gnus-group-make-group 
4057      (gnus-group-real-name name)
4058      (list 'nndoc name
4059            (list 'nndoc-address (concat (file-name-as-directory (car path)) "doc.txt"))
4060            (list 'nndoc-article-type 'mbox))))
4061   (gnus-group-position-cursor))
4062
4063 (defun gnus-group-make-doc-group (file type)
4064   "Create a group that uses a single file as the source."
4065   (interactive 
4066    (list (read-file-name "File name: ") 
4067          (let ((err "")
4068                found char)
4069            (while (not found)
4070              (message "%sFile type (mbox, babyl, digest) [mbd]: " err)
4071              (setq found (cond ((= (setq char (read-char)) ?m) 'mbox)
4072                                ((= char ?b) 'babyl)
4073                                ((= char ?d) 'digest)
4074                                (t (setq err "%c unknown. " char)
4075                                   nil))))
4076            found)))
4077   (let* ((file (expand-file-name file))
4078          (name (gnus-generate-new-group-name
4079                 (gnus-group-prefixed-name
4080                  (file-name-nondirectory file) '(nndoc "")))))
4081     (gnus-group-make-group 
4082      (gnus-group-real-name name)
4083      (list 'nndoc name
4084            (list 'nndoc-address file)
4085            (list 'nndoc-article-type type)))))
4086
4087 (defun gnus-group-make-archive-group ()
4088   "Create the (ding) Gnus archive group."
4089   (interactive)
4090   (and (gnus-gethash (gnus-group-prefixed-name "ding.archives" '(nndir ""))
4091                      gnus-newsrc-hashtb)
4092        (error "Archive group already exists"))
4093   (gnus-group-make-group "ding.archives" "nndir" gnus-group-archive-directory)
4094   (gnus-group-position-cursor))
4095
4096 (defun gnus-group-make-directory-group (dir)
4097   "Create an nndir group.
4098 The user will be prompted for a directory. The contents of this
4099 directory will be used as a newsgroup. The directory should contain
4100 mail messages or news articles in files that have numeric names."
4101   (interactive
4102    (list (read-file-name "Create group from directory: ")))
4103   (or (file-exists-p dir) (error "No such directory"))
4104   (or (file-directory-p dir) (error "Not a directory"))
4105   (gnus-group-make-group dir "nndir" dir)
4106   (gnus-group-position-cursor))
4107
4108 (defun gnus-group-make-kiboze-group (group address scores)
4109   "Create an nnkiboze group.
4110 The user will be prompted for a name, a regexp to match groups, and
4111 score file entries for articles to include in the group."
4112   (interactive
4113    (list
4114     (read-string "nnkiboze group name: ")
4115     (read-string "Source groups (regexp): ")
4116     (let ((headers (mapcar (lambda (group) (list group))
4117                            '("subject" "from" "number" "date" "message-id"
4118                              "references" "chars" "lines" "xref")))
4119           scores header regexp regexps)
4120       (while (not (equal "" (setq header (completing-read 
4121                                           "Match on header: " headers nil t))))
4122         (setq regexps nil)
4123         (while (not (equal "" (setq regexp (read-string 
4124                                             (format "Match on %s (string): "
4125                                                     header)))))
4126           (setq regexps (cons (list regexp nil nil 'r) regexps)))
4127         (setq scores (cons (cons header regexps) scores)))
4128       scores)))
4129   (gnus-group-make-group group "nnkiboze" address)
4130   (save-excursion
4131     (gnus-set-work-buffer)
4132     (let (emacs-lisp-mode-hook)
4133       (pp scores (current-buffer)))
4134     (write-region (point-min) (point-max) 
4135                   (concat (or gnus-kill-files-directory "~/News")
4136                           "nnkiboze:" group "." gnus-score-file-suffix)))
4137   (gnus-group-position-cursor))
4138
4139 (defun gnus-group-add-to-virtual (n vgroup)
4140   "Add the current group to a virtual group."
4141   (interactive
4142    (list current-prefix-arg
4143          (completing-read "Add to virtual group: " gnus-newsrc-hashtb nil t
4144                           "nnvirtual:")))
4145   (or (eq (car (gnus-find-method-for-group vgroup)) 'nnvirtual)
4146       (error "%s is not an nnvirtual group" vgroup))
4147   (let* ((groups (gnus-group-process-prefix n))
4148          (method (nth 4 (nth 2 (gnus-gethash vgroup gnus-newsrc-hashtb)))))
4149     (setcar (cdr method)
4150             (concat 
4151              (nth 1 method) "\\|"
4152              (mapconcat 
4153               (lambda (s) 
4154                 (gnus-group-remove-mark s)
4155                 (concat "\\(^" (regexp-quote s) "$\\)"))
4156               groups "\\|"))))
4157   (gnus-group-position-cursor))
4158
4159 (defun gnus-group-make-empty-virtual (group)
4160   "Create a new, fresh, empty virtual group."
4161   (interactive "sCreate new, empty virtual group: ")
4162   (let* ((method (list 'nnvirtual "^$"))
4163          (pgroup (gnus-group-prefixed-name group method)))
4164     ;; Check whether it exists already.
4165     (and (gnus-gethash pgroup gnus-newsrc-hashtb)
4166          (error "Group %s already exists." pgroup))
4167     ;; Subscribe the new group after the group on the current line.
4168     (gnus-subscribe-group pgroup (gnus-group-group-name) method)
4169     (gnus-group-update-group pgroup)
4170     (forward-line -1)
4171     (gnus-group-position-cursor)))
4172
4173 (defun gnus-group-enter-directory (dir)
4174   "Enter an ephemeral nneething group."
4175   (interactive "DDirectory to read: ")
4176   (let* ((method (list 'nneething dir))
4177          (leaf (gnus-group-prefixed-name
4178                 (file-name-nondirectory (directory-file-name dir))
4179                 method))
4180          (name (gnus-generate-new-group-name leaf)))
4181     (let ((nneething-read-only t))
4182       (or (gnus-group-read-ephemeral-group 
4183            name method t
4184            (cons (current-buffer) (if (eq major-mode 'gnus-summary-mode)
4185                                       'summary 'group)))
4186           (error "Couldn't enter %s" dir)))))
4187
4188 ;; Group sorting commands
4189 ;; Suggested by Joe Hildebrand <hildjj@idaho.fuentez.com>.
4190
4191 (defun gnus-group-sort-groups ()
4192   "Sort the group buffer using `gnus-group-sort-function'."
4193   (interactive)
4194   (setq gnus-newsrc-alist 
4195         (sort (cdr gnus-newsrc-alist) gnus-group-sort-function))
4196   (gnus-make-hashtable-from-newsrc-alist)
4197   (gnus-group-list-groups (if gnus-have-all-newsgroups gnus-level-unsubscribed)
4198                           gnus-have-all-newsgroups))
4199
4200 (defun gnus-group-sort-by-alphabet (info1 info2)
4201   (string< (car info1) (car info2)))
4202
4203 (defun gnus-group-sort-by-unread (info1 info2)
4204   (let ((n1 (car (gnus-gethash (car info1) gnus-newsrc-hashtb)))
4205         (n2 (car (gnus-gethash (car info2) gnus-newsrc-hashtb))))
4206     (< (or (and (numberp n1) n1) 0)
4207        (or (and (numberp n2) n2) 0))))
4208
4209 (defun gnus-group-sort-by-level (info1 info2)
4210   (< (nth 1 info1) (nth 1 info2)))
4211
4212 ;; Group catching up.
4213
4214 (defun gnus-group-catchup-current (&optional n all)
4215   "Mark all articles not marked as unread in current newsgroup as read.
4216 If prefix argument N is numeric, the ARG next newsgroups will be
4217 caught up. If ALL is non-nil, marked articles will also be marked as
4218 read. Cross references (Xref: header) of articles are ignored.
4219 The difference between N and actual number of newsgroups that were
4220 caught up is returned."
4221   (interactive "P")
4222   (if (not (or (not gnus-interactive-catchup) ;Without confirmation?
4223                gnus-expert-user
4224                (gnus-y-or-n-p
4225                 (if all
4226                     "Do you really want to mark all articles as read? "
4227                   "Mark all unread articles as read? "))))
4228       n
4229     (let ((groups (gnus-group-process-prefix n))
4230           (ret 0))
4231       (while groups
4232         ;; Virtual groups have to be given special treatment. 
4233         (let ((method (gnus-find-method-for-group (car groups))))
4234           (if (eq 'nnvirtual (car method))
4235               (nnvirtual-catchup-group
4236                (gnus-group-real-name (car groups)) (nth 1 method) all)))
4237         (gnus-group-remove-mark (car groups))
4238         (if (prog1
4239                 (gnus-group-goto-group (car groups))
4240               (gnus-group-catchup (car groups) all))
4241             (gnus-group-update-group-line)
4242           (setq ret (1+ ret)))
4243         (setq groups (cdr groups)))
4244       (gnus-group-next-unread-group 1)
4245       ret)))
4246
4247 (defun gnus-group-catchup-current-all (&optional n)
4248   "Mark all articles in current newsgroup as read.
4249 Cross references (Xref: header) of articles are ignored."
4250   (interactive "P")
4251   (gnus-group-catchup-current n 'all))
4252
4253 (defun gnus-group-catchup (group &optional all)
4254   "Mark all articles in GROUP as read.
4255 If ALL is non-nil, all articles are marked as read.
4256 The return value is the number of articles that were marked as read,
4257 or nil if no action could be taken."
4258   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4259          (num (car entry))
4260          (marked (nth 3 (nth 2 entry))))
4261     (if (not (numberp (car entry)))
4262         (gnus-message 1 "Can't catch up; non-active group")
4263       ;; Do the updating only if the newsgroup isn't killed.
4264       (if (not entry)
4265           ()
4266         (gnus-update-read-articles 
4267          group (and (not all) (append (cdr (assq 'tick marked))
4268                                       (cdr (assq 'dormant marked))))
4269          nil (and (not all) (cdr (assq 'tick marked))))
4270         (and all marked
4271              (setcar (nthcdr 3 (nth 2 entry)) 
4272                      (delq (assq 'dormant marked) 
4273                            (nth 3 (nth 2 entry)))))))
4274     num))
4275
4276 (defun gnus-group-expire-articles (&optional n)
4277   "Expire all expirable articles in the current newsgroup."
4278   (interactive "P")
4279   (let ((groups (gnus-group-process-prefix n))
4280         group)
4281     (or groups (error "No groups to expire"))
4282     (while groups
4283       (setq group (car groups)
4284             groups (cdr groups))
4285       (gnus-group-remove-mark group)
4286       (if (not (gnus-check-backend-function 'request-expire-articles group))
4287           ()
4288         (let* ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
4289                (expirable (if (memq 'total-expire (nth 5 info))
4290                               (cons nil (gnus-list-of-read-articles group))
4291                             (assq 'expire (nth 3 info)))))
4292           (and expirable 
4293                (setcdr expirable
4294                        (gnus-request-expire-articles 
4295                         (cdr expirable) group))))))))
4296
4297 (defun gnus-group-expire-all-groups ()
4298   "Expire all expirable articles in all newsgroups."
4299   (interactive)
4300   (save-excursion
4301     (gnus-message 5 "Expiring...")
4302     (let ((gnus-group-marked (mapcar (lambda (info) (car info))
4303                                      (cdr gnus-newsrc-alist))))
4304       (gnus-group-expire-articles nil)))
4305   (gnus-group-position-cursor)
4306   (gnus-message 5 "Expiring...done"))
4307
4308 (defun gnus-group-set-current-level (n level)
4309   "Set the level of the next N groups to LEVEL."
4310   (interactive "P\nnLevel: ")
4311   (or (and (>= level 1) (<= level gnus-level-killed))
4312       (error "Illegal level: %d" level))
4313   (let ((groups (gnus-group-process-prefix n))
4314         group)
4315     (while groups
4316       (setq group (car groups)
4317             groups (cdr groups))
4318       (gnus-group-remove-mark group)
4319       (gnus-message 6 "Changed level of %s from %d to %d" 
4320                     group (gnus-group-group-level) level)
4321       (gnus-group-change-level group level
4322                                (gnus-group-group-level))
4323       (gnus-group-update-group-line)))
4324   (gnus-group-position-cursor))
4325
4326 (defun gnus-group-unsubscribe-current-group (&optional n)
4327   "Toggle subscription of the current group.
4328 If given numerical prefix, toggle the N next groups."
4329   (interactive "P")
4330   (let ((groups (gnus-group-process-prefix n))
4331         group)
4332     (while groups
4333       (setq group (car groups)
4334             groups (cdr groups))
4335       (gnus-group-remove-mark group)
4336       (gnus-group-unsubscribe-group
4337        group (if (<= (gnus-group-group-level) gnus-level-subscribed)
4338                  gnus-level-default-unsubscribed
4339                gnus-level-default-subscribed))
4340       (gnus-group-update-group-line))
4341     (gnus-group-next-group 1)))
4342
4343 (defun gnus-group-unsubscribe-group (group &optional level)
4344   "Toggle subscribe from/to unsubscribe GROUP.
4345 New newsgroup is added to .newsrc automatically."
4346   (interactive
4347    (list (completing-read
4348           "Group: " gnus-active-hashtb nil 
4349           (memq gnus-select-method gnus-have-read-active-file))))
4350   (let ((newsrc (gnus-gethash group gnus-newsrc-hashtb)))
4351     (cond (newsrc
4352            ;; Toggle subscription flag.
4353            (gnus-group-change-level 
4354             newsrc (if level level (if (<= (nth 1 (nth 2 newsrc)) 
4355                                            gnus-level-subscribed) 
4356                                        (1+ gnus-level-subscribed)
4357                                      gnus-level-default-subscribed)))
4358            (gnus-group-update-group group))
4359           ((and (stringp group)
4360                 (or (not gnus-have-read-active-file)
4361                     (gnus-gethash group gnus-active-hashtb)))
4362            ;; Add new newsgroup.
4363            (gnus-group-change-level 
4364             group 
4365             (if level level gnus-level-default-subscribed) 
4366             (or (and (member group gnus-zombie-list) 
4367                      gnus-level-zombie) 
4368                 gnus-level-killed)
4369             (and (gnus-group-group-name)
4370                  (gnus-gethash (gnus-group-group-name) gnus-newsrc-hashtb)))
4371            (gnus-group-update-group group))
4372           (t (error "No such newsgroup: %s" group)))
4373     (gnus-group-position-cursor)))
4374
4375 (defun gnus-group-transpose-groups (n)
4376   "Move the current newsgroup up N places.
4377 If given a negative prefix, move down instead. The difference between
4378 N and the number of steps taken is returned." 
4379   (interactive "p")
4380   (or (gnus-group-group-name)
4381       (error "No group on current line"))
4382   (gnus-group-kill-group 1)
4383   (prog1
4384       (forward-line (- n))
4385     (gnus-group-yank-group)
4386     (gnus-group-position-cursor)))
4387
4388 (defun gnus-group-kill-all-zombies ()
4389   "Kill all zombie newsgroups."
4390   (interactive)
4391   (setq gnus-killed-list (nconc gnus-zombie-list gnus-killed-list))
4392   (setq gnus-zombie-list nil)
4393   (funcall gnus-group-prepare-function gnus-level-subscribed nil nil)
4394   (goto-char (point-min))
4395   (gnus-group-position-cursor))
4396
4397 (defun gnus-group-kill-region (begin end)
4398   "Kill newsgroups in current region (excluding current point).
4399 The killed newsgroups can be yanked by using \\[gnus-group-yank-group]."
4400   (interactive "r")
4401   (let ((lines
4402          ;; Count lines.
4403          (save-excursion
4404            (count-lines
4405             (progn
4406               (goto-char begin)
4407               (beginning-of-line)
4408               (point))
4409             (progn
4410               (goto-char end)
4411               (beginning-of-line)
4412               (point))))))
4413     (goto-char begin)
4414     (beginning-of-line)                 ;Important when LINES < 1
4415     (gnus-group-kill-group lines)))
4416
4417 (defun gnus-group-kill-group (&optional n)
4418   "The the next N groups.
4419 The killed newsgroups can be yanked by using \\[gnus-group-yank-group].
4420 However, only groups that were alive can be yanked; already killed 
4421 groups or zombie groups can't be yanked.
4422 The return value is the name of the (last) group that was killed."
4423   (interactive "P")
4424   (let ((buffer-read-only nil)
4425         (groups (gnus-group-process-prefix n))
4426         group entry level)
4427     (while groups
4428       (setq group (car groups)
4429             groups (cdr groups))
4430       (gnus-group-remove-mark group)
4431       (setq level (gnus-group-group-level))
4432       (gnus-delete-line)
4433       (if (setq entry (gnus-gethash group gnus-newsrc-hashtb))
4434           (setq gnus-list-of-killed-groups 
4435                 (cons (cons (car entry) (nth 2 entry)) 
4436                       gnus-list-of-killed-groups)))
4437       (gnus-group-change-level 
4438        (if entry entry group) gnus-level-killed (if entry nil level)))
4439     (gnus-group-position-cursor)
4440     group))
4441
4442 (defun gnus-group-yank-group (&optional arg)
4443   "Yank the last newsgroups killed with \\[gnus-group-kill-group],
4444 inserting it before the current newsgroup.  The numeric ARG specifies
4445 how many newsgroups are to be yanked.  The name of the (last)
4446 newsgroup yanked is returned."
4447   (interactive "p")
4448   (if (not arg) (setq arg 1))
4449   (let (info group prev)
4450     (while (>= (setq arg (1- arg)) 0)
4451       (if (not (setq info (car gnus-list-of-killed-groups)))
4452           (error "No more newsgroups to yank"))
4453       (setq group (nth 2 info))
4454       ;; Find which newsgroup to insert this one before - search
4455       ;; backward until something suitable is found. If there are no
4456       ;; other newsgroups in this buffer, just make this newsgroup the
4457       ;; first newsgroup.
4458       (setq prev (gnus-group-group-name))
4459       (gnus-group-change-level 
4460        info (nth 2 info) gnus-level-killed 
4461        (and prev (gnus-gethash prev gnus-newsrc-hashtb))
4462        t)
4463       (gnus-group-insert-group-line-info (nth 1 info))
4464       (setq gnus-list-of-killed-groups 
4465             (cdr gnus-list-of-killed-groups)))
4466     (forward-line -1)
4467     (gnus-group-position-cursor)
4468     group))
4469       
4470 (defun gnus-group-list-all-groups (&optional arg)
4471   "List all newsgroups with level ARG or lower.
4472 Default is gnus-level-unsubscribed, which lists all subscribed and most
4473 unsubscribed groups."
4474   (interactive "P")
4475   (setq arg (or arg gnus-level-unsubscribed))
4476   (gnus-group-list-groups arg t))
4477
4478 (defun gnus-group-list-killed ()
4479   "List all killed newsgroups in the group buffer."
4480   (interactive)
4481   (if (not gnus-killed-list)
4482       (gnus-message 6 "No killed groups")
4483     (funcall gnus-group-prepare-function gnus-level-killed t gnus-level-killed)
4484     (goto-char (point-min)))
4485   (gnus-group-position-cursor))
4486
4487 (defun gnus-group-list-zombies ()
4488   "List all zombie newsgroups in the group buffer."
4489   (interactive)
4490   (if (not gnus-zombie-list)
4491       (gnus-message 6 "No zombie groups")
4492     (funcall gnus-group-prepare-function gnus-level-zombie t gnus-level-zombie)
4493     (goto-char (point-min)))
4494   (gnus-group-position-cursor))
4495
4496 (defun gnus-group-get-new-news (&optional arg)
4497   "Get newly arrived articles.
4498 If ARG is non-nil, it should be a number between one and nine to
4499 specify which levels you are interested in re-scanning."
4500   (interactive "P")
4501   (run-hooks 'gnus-get-new-news-hook)
4502   (if gnus-group-use-permanent-levels
4503       (setq arg
4504             (setq gnus-group-default-list-level 
4505                   (or arg gnus-group-default-list-level
4506                       gnus-level-subscribed))))
4507   (if (and gnus-read-active-file (not arg))
4508       (progn
4509         (gnus-read-active-file)
4510         (gnus-get-unread-articles (or arg (1+ gnus-level-subscribed))))
4511     (let ((gnus-read-active-file (not arg))
4512           (gnus-have-read-active-file 
4513            (and (not arg) gnus-have-read-active-file)))
4514       (gnus-get-unread-articles (or arg (1+ gnus-level-subscribed)))))
4515   (gnus-group-list-groups (or (and gnus-group-use-permanent-levels arg)
4516                               gnus-group-default-list-level
4517                               gnus-level-subscribed)
4518                           gnus-have-all-newsgroups))
4519
4520 (defun gnus-group-get-new-news-this-group (&optional n)
4521   "Check for newly arrived news in the current group (and the N-1 next groups).
4522 The difference between N and the number of newsgroup checked is returned.
4523 If N is negative, this group and the N-1 previous groups will be checked."
4524   (interactive "P")
4525   (let* ((groups (gnus-group-process-prefix n))
4526          (ret (if (numberp n) (- n (length groups)) 0))
4527          group)
4528     (while groups
4529       (setq group (car groups)
4530             groups (cdr groups))
4531       (gnus-group-remove-mark group)
4532       (or (gnus-get-new-news-in-group group)
4533           (progn 
4534             (ding) 
4535             (message "%s error: %s" group (gnus-status-message group))
4536             (sit-for 2))))
4537     ;; !!! I don't know why the buffer scrolls forward when updating
4538     ;; the first line in the group buffer, but it does. So we set the
4539     ;; window start forcibly.
4540 ;    (set-window-start (get-buffer-window (current-buffer)) w-p)
4541     (gnus-group-next-unread-group 1 t)
4542     (gnus-summary-position-cursor)
4543     ret))
4544
4545 (defun gnus-get-new-news-in-group (group)
4546   (and group 
4547        (gnus-activate-newsgroup group)
4548        (progn
4549          (gnus-get-unread-articles-in-group 
4550           (nth 2 (gnus-gethash group gnus-newsrc-hashtb))
4551           (gnus-gethash group gnus-active-hashtb))
4552          (gnus-group-update-group-line)
4553          t)))
4554
4555 (defun gnus-group-fetch-faq (group)
4556   "Fetch the FAQ for the current group."
4557   (interactive (list (gnus-group-real-name (gnus-group-group-name))))
4558   (or group (error "No group name given"))
4559   (let ((file (concat gnus-group-faq-directory group))) 
4560     (if (not (file-exists-p file))
4561         (error "No such file: %s" file)
4562       (find-file file))))
4563   
4564 (defun gnus-group-describe-group (force &optional group)
4565   "Display a description of the current newsgroup."
4566   (interactive (list current-prefix-arg (gnus-group-group-name)))
4567   (and force (setq gnus-description-hashtb nil))
4568   (let ((method (gnus-find-method-for-group group))
4569         desc)
4570     (or group (error "No group name given"))
4571     (and (or (and gnus-description-hashtb
4572                   ;; We check whether this group's method has been
4573                   ;; queried for a description file.  
4574                   (gnus-gethash 
4575                    (gnus-group-prefixed-name "" method) 
4576                    gnus-description-hashtb))
4577              (setq desc (gnus-group-get-description group))
4578              (gnus-read-descriptions-file method))
4579          (message
4580           (or desc (gnus-gethash group gnus-description-hashtb)
4581               "No description available")))))
4582
4583 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
4584 (defun gnus-group-describe-all-groups (&optional force)
4585   "Pop up a buffer with descriptions of all newsgroups."
4586   (interactive "P")
4587   (and force (setq gnus-description-hashtb nil))
4588   (if (not (or gnus-description-hashtb
4589                (gnus-read-all-descriptions-files)))
4590       (error "Couldn't request descriptions file"))
4591   (let ((buffer-read-only nil)
4592         b)
4593     (erase-buffer)
4594     (mapatoms
4595      (lambda (group)
4596        (setq b (point))
4597        (insert (format "      *: %-20s %s\n" (symbol-name group)
4598                        (symbol-value group)))
4599        (add-text-properties 
4600         b (1+ b) (list 'gnus-group group
4601                        'gnus-unread t 'gnus-marked nil
4602                        'gnus-level (1+ gnus-level-subscribed))))
4603      gnus-description-hashtb)
4604     (goto-char (point-min))
4605     (gnus-group-position-cursor)))
4606
4607 ;; Suggested by by Daniel Quinlan <quinlan@best.com>.
4608 (defun gnus-group-apropos (regexp &optional search-description)
4609   "List all newsgroups that have names that match a regexp."
4610   (interactive "sGnus apropos (regexp): ")
4611   (let ((prev "")
4612         (obuf (current-buffer))
4613         groups des)
4614     ;; Go through all newsgroups that are known to Gnus.
4615     (mapatoms 
4616      (lambda (group)
4617        (and (string-match regexp (symbol-name group))
4618             (setq groups (cons (symbol-name group) groups))))
4619      gnus-active-hashtb)
4620     ;; Go through all descriptions that are known to Gnus. 
4621     (if search-description
4622         (mapatoms 
4623          (lambda (group)
4624            (and (string-match regexp (symbol-value group))
4625                 (gnus-gethash (symbol-name group) gnus-active-hashtb)
4626                 (setq groups (cons (symbol-name group) groups))))
4627          gnus-description-hashtb))
4628     (if (not groups)
4629         (gnus-message 3 "No groups matched \"%s\"." regexp)
4630       ;; Print out all the groups.
4631       (save-excursion
4632         (pop-to-buffer "*Gnus Help*")
4633         (buffer-disable-undo (current-buffer))
4634         (erase-buffer)
4635         (setq groups (sort groups 'string<))
4636         (while groups
4637           ;; Groups may be entered twice into the list of groups.
4638           (if (not (string= (car groups) prev))
4639               (progn
4640                 (insert (setq prev (car groups)) "\n")
4641                 (if (and gnus-description-hashtb
4642                          (setq des (gnus-gethash (car groups) 
4643                                                  gnus-description-hashtb)))
4644                     (insert "  " des "\n"))))
4645           (setq groups (cdr groups)))
4646         (goto-char (point-min))))
4647     (pop-to-buffer obuf)))
4648
4649 (defun gnus-group-description-apropos (regexp)
4650   "List all newsgroups that have names or descriptions that match a regexp."
4651   (interactive "sGnus description apropos (regexp): ")
4652   (if (not (or gnus-description-hashtb
4653                (gnus-read-all-descriptions-files)))
4654       (error "Couldn't request descriptions file"))
4655   (gnus-group-apropos regexp t))
4656
4657 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
4658 (defun gnus-group-list-matching (level regexp &optional all lowest) 
4659   "List all groups with unread articles that match REGEXP.
4660 If the prefix LEVEL is non-nil, it should be a number that says which
4661 level to cut off listing groups. 
4662 If ALL, also list groups with no unread articles.
4663 If LOWEST, don't list groups with level lower than LOWEST."
4664   (interactive "P\nsList newsgroups matching: ")
4665   (gnus-group-prepare-flat (or level gnus-level-subscribed)
4666                            all (or lowest 1) regexp)
4667   (goto-char (point-min))
4668   (gnus-group-position-cursor))
4669
4670 (defun gnus-group-list-all-matching (level regexp &optional lowest) 
4671   "List all groups that match REGEXP.
4672 If the prefix LEVEL is non-nil, it should be a number that says which
4673 level to cut off listing groups. 
4674 If LOWEST, don't list groups with level lower than LOWEST."
4675   (interactive "P\nsList newsgroups matching: ")
4676   (gnus-group-list-matching (or level gnus-level-killed) regexp t lowest))
4677
4678 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
4679 (defun gnus-group-save-newsrc ()
4680   "Save the Gnus startup files."
4681   (interactive)
4682   (gnus-save-newsrc-file))
4683
4684 (defun gnus-group-restart (&optional arg)
4685   "Force Gnus to read the .newsrc file."
4686   (interactive "P")
4687   (gnus-save-newsrc-file)
4688   (gnus-setup-news 'force)
4689   (gnus-group-list-groups arg gnus-have-all-newsgroups))
4690
4691 (defun gnus-group-read-init-file ()
4692   "Read the Gnus elisp init file."
4693   (interactive)
4694   (gnus-read-init-file))
4695
4696 (defun gnus-group-check-bogus-groups (&optional silent)
4697   "Check bogus newsgroups.
4698 If given a prefix, don't ask for confirmation before removing a bogus
4699 group."
4700   (interactive "P")
4701   (gnus-check-bogus-newsgroups (and (not silent) (not gnus-expert-user)))
4702   (gnus-group-list-groups nil gnus-have-all-newsgroups))
4703
4704 (defun gnus-group-edit-global-kill (&optional article group)
4705   "Edit the global kill file.
4706 If GROUP, edit that local kill file instead."
4707   (interactive "P")
4708   (setq gnus-current-kill-article article)
4709   (gnus-kill-file-edit-file group)
4710   (gnus-message 6
4711    (substitute-command-keys
4712     "Editing a global kill file (Type \\[gnus-kill-file-exit] to exit)")))
4713
4714 (defun gnus-group-edit-local-kill (article group)
4715   "Edit a local kill file."
4716   (interactive (list nil (gnus-group-group-name)))
4717   (gnus-group-edit-global-kill article group))
4718
4719 (defun gnus-group-force-update ()
4720   "Update `.newsrc' file."
4721   (interactive)
4722   (gnus-save-newsrc-file))
4723
4724 (defun gnus-group-suspend ()
4725   "Suspend the current Gnus session.
4726 In fact, cleanup buffers except for group mode buffer.
4727 The hook gnus-suspend-gnus-hook is called before actually suspending."
4728   (interactive)
4729   (run-hooks 'gnus-suspend-gnus-hook)
4730   ;; Kill Gnus buffers except for group mode buffer.
4731   (let ((group-buf (get-buffer gnus-group-buffer)))
4732     ;; Do this on a separate list in case the user does a ^G before we finish
4733     (let ((gnus-buffer-list
4734            (delq group-buf (delq gnus-dribble-buffer
4735                                  (append gnus-buffer-list nil)))))
4736       (while gnus-buffer-list
4737         (gnus-kill-buffer (car gnus-buffer-list))
4738         (setq gnus-buffer-list (cdr gnus-buffer-list))))
4739     (if group-buf
4740         (progn
4741           (setq gnus-buffer-list (list group-buf))
4742           (bury-buffer group-buf)
4743           (delete-windows-on group-buf t)))))
4744
4745 (defun gnus-group-clear-dribble ()
4746   "Clear all information from the dribble buffer."
4747   (interactive)
4748   (gnus-dribble-clear))
4749
4750 (defun gnus-group-exit ()
4751   "Quit reading news after updating .newsrc.eld and .newsrc.
4752 The hook `gnus-exit-gnus-hook' is called before actually exiting."
4753   (interactive)
4754   (if (or noninteractive                ;For gnus-batch-kill
4755           (zerop (buffer-size))         ;No news is good news.
4756           (not (gnus-server-opened gnus-select-method)) ;NNTP connection closed
4757           (not gnus-interactive-exit)   ;Without confirmation
4758           gnus-expert-user
4759           (gnus-y-or-n-p "Are you sure you want to quit reading news? "))
4760       (progn
4761         (if gnus-use-full-window
4762             (delete-other-windows)
4763           (gnus-remove-some-windows))
4764         (run-hooks 'gnus-exit-gnus-hook)
4765         (gnus-offer-save-summaries)
4766         (gnus-save-newsrc-file)
4767         (gnus-close-backends)
4768         (gnus-clear-system))))
4769
4770 (defun gnus-close-backends ()
4771   ;; Send a close request to all backends that support such a request. 
4772   (let ((methods gnus-valid-select-methods)
4773         func)
4774     (while methods
4775       (if (fboundp (setq func (intern (concat (car (car methods))
4776                                               "-request-close"))))
4777           (funcall func))
4778       (setq methods (cdr methods)))))
4779
4780 (defun gnus-group-quit ()
4781   "Quit reading news without updating .newsrc.eld or .newsrc.
4782 The hook `gnus-exit-gnus-hook' is called before actually exiting."
4783   (interactive)
4784   (if (or noninteractive                ;For gnus-batch-kill
4785           (zerop (buffer-size))
4786           (not (gnus-server-opened gnus-select-method))
4787           gnus-expert-user
4788           (not gnus-current-startup-file)
4789           (gnus-yes-or-no-p
4790            (format "Quit reading news without saving %s? "
4791                    (file-name-nondirectory gnus-current-startup-file))))
4792       (progn
4793         (run-hooks 'gnus-exit-gnus-hook)
4794         (if gnus-use-full-window
4795             (delete-other-windows)
4796           (gnus-remove-some-windows))
4797         (gnus-dribble-save)
4798         (gnus-close-backends)
4799         (gnus-clear-system))))
4800
4801 (defun gnus-offer-save-summaries ()
4802   (let ((buffers (buffer-list)))
4803     (save-excursion
4804       (while buffers
4805         (and 
4806          ;; We look for buffers with "Summary" in the name.
4807          (string-match "Summary" (or (buffer-name (car buffers)) ""))
4808          (progn
4809            (set-buffer (car buffers))
4810            ;; We check that this is, indeed, a summary buffer.
4811            (eq major-mode 'gnus-summary-mode)) 
4812          ;; We ask the user whether she wants to save the info.
4813          (gnus-y-or-n-p
4814                (format "Update summary buffer %s? " (buffer-name)))
4815          ;; We do it by simply exiting.
4816          (gnus-summary-exit))
4817         (setq buffers (cdr buffers))))))
4818
4819 (defun gnus-group-describe-briefly ()
4820   "Give a one line description of the group mode commands."
4821   (interactive)
4822   (gnus-message 7 (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")))
4823
4824 (defun gnus-group-browse-foreign-server (method)
4825   "Browse a foreign news server.
4826 If called interactively, this function will ask for a select method
4827  (nntp, nnspool, etc.) and a server address (eg. nntp.some.where). 
4828 If not, METHOD should be a list where the first element is the method
4829 and the second element is the address."
4830   (interactive
4831    (list (let ((how (completing-read 
4832                      "Which backend: "
4833                      (append gnus-valid-select-methods gnus-server-alist)
4834                      nil t "nntp")))
4835            ;; We either got a backend name or a virtual server name.
4836            ;; If the first, we also need an address.
4837            (if (assoc how gnus-valid-select-methods)
4838                (list (intern how)
4839                      ;; Suggested by mapjph@bath.ac.uk.
4840                      (completing-read 
4841                       "Address: " 
4842                       (mapcar (lambda (server) (list server))
4843                               gnus-secondary-servers)))
4844              ;; We got a server name, so we find the method.
4845              (gnus-server-to-method how)))))
4846   (gnus-browse-foreign-server method))
4847
4848 \f
4849 ;;;
4850 ;;; Browse Server Mode
4851 ;;;
4852
4853 (defvar gnus-browse-mode-hook nil)
4854 (defvar gnus-browse-mode-map nil)
4855 (put 'gnus-browse-mode 'mode-class 'special)
4856
4857 (if gnus-browse-mode-map
4858     nil
4859   (setq gnus-browse-mode-map (make-keymap))
4860   (suppress-keymap gnus-browse-mode-map)
4861   (define-key gnus-browse-mode-map " " 'gnus-browse-read-group)
4862   (define-key gnus-browse-mode-map "=" 'gnus-browse-select-group)
4863   (define-key gnus-browse-mode-map "n" 'gnus-browse-next-group)
4864   (define-key gnus-browse-mode-map "p" 'gnus-browse-prev-group)
4865   (define-key gnus-browse-mode-map "\177" 'gnus-browse-prev-group)
4866   (define-key gnus-browse-mode-map "N" 'gnus-browse-next-group)
4867   (define-key gnus-browse-mode-map "P" 'gnus-browse-prev-group)
4868   (define-key gnus-browse-mode-map "\M-n" 'gnus-browse-next-group)
4869   (define-key gnus-browse-mode-map "\M-p" 'gnus-browse-prev-group)
4870   (define-key gnus-browse-mode-map "\r" 'gnus-browse-select-group)
4871   (define-key gnus-browse-mode-map "u" 'gnus-browse-unsubscribe-current-group)
4872   (define-key gnus-browse-mode-map "l" 'gnus-browse-exit)
4873   (define-key gnus-browse-mode-map "L" 'gnus-browse-exit)
4874   (define-key gnus-browse-mode-map "q" 'gnus-browse-exit)
4875   (define-key gnus-browse-mode-map "Q" 'gnus-browse-exit)
4876   (define-key gnus-browse-mode-map "\C-c\C-c" 'gnus-browse-exit)
4877   (define-key gnus-browse-mode-map "?" 'gnus-browse-describe-briefly)
4878   (define-key gnus-browse-mode-map "\C-c\C-i" 'gnus-info-find-node)
4879   )
4880
4881 (defvar gnus-browse-current-method nil)
4882 (defvar gnus-browse-return-buffer nil)
4883
4884 (defvar gnus-browse-buffer "*Gnus Browse Server*")
4885
4886 (defun gnus-browse-foreign-server (method &optional return-buffer)
4887   (setq gnus-browse-current-method method)
4888   (setq gnus-browse-return-buffer return-buffer)
4889   (let ((gnus-select-method method)
4890         groups group)
4891     (gnus-message 5 "Connecting to %s..." (nth 1 method))
4892     (or (gnus-server-opened method)
4893         (gnus-open-server method)
4894         (error "Unable to contact server: %s" (gnus-status-message method)))
4895     (or (gnus-request-list method)
4896         (error "Couldn't request list: %s" (gnus-status-message method)))
4897     (get-buffer-create gnus-browse-buffer)
4898     (gnus-add-current-to-buffer-list)
4899     (and gnus-carpal (gnus-carpal-setup-buffer 'browse))
4900     (gnus-configure-windows 'browse)
4901     (buffer-disable-undo (current-buffer))
4902     (let ((buffer-read-only nil))
4903       (erase-buffer))
4904     (gnus-browse-mode)
4905     (setq mode-line-buffer-identification
4906           (format
4907            "(ding) Browse Server {%s:%s}" (car method) (car (cdr method))))
4908     (save-excursion
4909       (set-buffer nntp-server-buffer)
4910       (let ((cur (current-buffer)))
4911         (goto-char (point-min))
4912         (or (string= gnus-ignored-newsgroups "")
4913             (delete-matching-lines gnus-ignored-newsgroups))
4914         (while (re-search-forward 
4915                 "\\(^[^ \t]+\\)[ \t]+[0-9]+[ \t]+[0-9]+" nil t)
4916           (goto-char (match-end 1))
4917           (setq groups (cons (cons (buffer-substring (match-beginning 1)
4918                                                      (match-end 1))
4919                                    (max 0 (- (1+ (read cur)) (read cur))))
4920                              groups)))))
4921     (setq groups (sort groups 
4922                        (lambda (l1 l2)
4923                          (string< (car l1) (car l2)))))
4924     (let ((buffer-read-only nil))
4925       (while groups
4926         (setq group (car groups))
4927         (insert 
4928          (format "K%7d: %s\n" (cdr group) (car group)))
4929         (setq groups (cdr groups))))
4930     (switch-to-buffer (current-buffer))
4931     (goto-char (point-min))
4932     (gnus-group-position-cursor)))
4933
4934 (defun gnus-browse-mode ()
4935   "Major mode for browsing a foreign server.
4936
4937 All normal editing commands are switched off.
4938
4939 \\<gnus-browse-mode-map>
4940 The only things you can do in this buffer is
4941
4942 1) `\\[gnus-browse-unsubscribe-current-group]' to subscribe to a group.
4943 The group will be inserted into the group buffer upon exit from this
4944 buffer.  
4945
4946 2) `\\[gnus-browse-read-group]' to read a group ephemerally.
4947
4948 3) `\\[gnus-browse-exit]' to return to the group buffer."
4949   (interactive)
4950   (kill-all-local-variables)
4951   (if gnus-visual (gnus-browse-make-menu-bar))
4952   (setq mode-line-modified "-- ")
4953   (make-local-variable 'mode-line-format)
4954   (setq mode-line-format (copy-sequence mode-line-format))
4955   (and (equal (nth 3 mode-line-format) "   ")
4956        (setcar (nthcdr 3 mode-line-format) ""))
4957   (setq major-mode 'gnus-browse-mode)
4958   (setq mode-name "Browse Server")
4959   (setq mode-line-process nil)
4960   (use-local-map gnus-browse-mode-map)
4961   (buffer-disable-undo (current-buffer))
4962   (setq truncate-lines t)
4963   (setq buffer-read-only t)
4964   (run-hooks 'gnus-browse-mode-hook))
4965
4966 (defun gnus-browse-read-group (&optional no-article)
4967   "Enter the group at the current line."
4968   (interactive)
4969   (let ((group (gnus-browse-group-name)))
4970     (or (gnus-group-read-ephemeral-group 
4971          group gnus-browse-current-method nil
4972          (cons (current-buffer) 'browse))
4973         (error "Couldn't enter %s" group))))
4974
4975 (defun gnus-browse-select-group ()
4976   "Select the current group."
4977   (interactive)
4978   (gnus-browse-read-group 'no))
4979
4980 (defun gnus-browse-next-group (n)
4981   "Go to the next group."
4982   (interactive "p")
4983   (prog1
4984       (forward-line n)
4985     (gnus-group-position-cursor)))
4986
4987 (defun gnus-browse-prev-group (n)
4988   "Go to the next group."
4989   (interactive "p")
4990   (gnus-browse-next-group (- n)))
4991
4992 (defun gnus-browse-unsubscribe-current-group (arg)
4993   "(Un)subscribe to the next ARG groups."
4994   (interactive "p")
4995   (and (eobp)
4996        (error "No group at current line."))
4997   (let ((ward (if (< arg 0) -1 1))
4998         (arg (abs arg)))
4999     (while (and (> arg 0)
5000                 (not (eobp))
5001                 (gnus-browse-unsubscribe-group)
5002                 (zerop (gnus-browse-next-group ward)))
5003       (setq arg (1- arg)))
5004     (gnus-group-position-cursor)
5005     (if (/= 0 arg) (gnus-message 7 "No more newsgroups"))
5006     arg))
5007
5008 (defun gnus-browse-group-name ()
5009   (save-excursion
5010     (beginning-of-line)
5011     (if (not (re-search-forward ": \\(.*\\)$" (gnus-point-at-eol) t))
5012         ()
5013       (gnus-group-prefixed-name 
5014        (buffer-substring (match-beginning 1) (match-end 1))
5015        gnus-browse-current-method))))
5016   
5017 (defun gnus-browse-unsubscribe-group ()
5018   (let ((sub nil)
5019         (buffer-read-only nil)
5020         group)
5021     (save-excursion
5022       (beginning-of-line)
5023       (if (= (following-char) ?K) (setq sub t))
5024       (setq group (gnus-browse-group-name))
5025       (beginning-of-line)
5026       (delete-char 1)
5027       (if sub
5028           (progn
5029             (gnus-group-change-level 
5030              (list t group gnus-level-default-subscribed
5031                    nil nil gnus-browse-current-method) 
5032              gnus-level-default-subscribed gnus-level-killed
5033              (and (car (nth 1 gnus-newsrc-alist))
5034                   (gnus-gethash (car (nth 1 gnus-newsrc-alist))
5035                                 gnus-newsrc-hashtb))
5036              t)
5037             (insert ? ))
5038         (gnus-group-change-level 
5039          group gnus-level-killed gnus-level-default-subscribed)
5040         (insert ?K)))
5041     t))
5042
5043 (defun gnus-browse-exit ()
5044   "Quit browsing and return to the group buffer."
5045   (interactive)
5046   (if (eq major-mode 'gnus-browse-mode)
5047       (kill-buffer (current-buffer)))
5048   (if gnus-browse-return-buffer
5049       (gnus-configure-windows 'server 'force)
5050     (gnus-configure-windows 'group 'force)
5051     (gnus-group-list-groups nil)))
5052
5053 (defun gnus-browse-describe-briefly ()
5054   "Give a one line description of the group mode commands."
5055   (interactive)
5056   (gnus-message 6
5057    (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")))
5058       
5059 \f
5060 ;;;
5061 ;;; Gnus summary mode
5062 ;;;
5063
5064 (defvar gnus-summary-mode-map nil)
5065 (defvar gnus-summary-mark-map nil)
5066 (defvar gnus-summary-mscore-map nil)
5067 (defvar gnus-summary-article-map nil)
5068 (defvar gnus-summary-thread-map nil)
5069 (defvar gnus-summary-goto-map nil)
5070 (defvar gnus-summary-exit-map nil)
5071 (defvar gnus-summary-various-map nil)
5072 (defvar gnus-summary-interest-map nil)
5073 (defvar gnus-summary-sort-map nil)
5074 (defvar gnus-summary-backend-map nil)
5075 (defvar gnus-summary-save-map nil)
5076 (defvar gnus-summary-wash-map nil)
5077 (defvar gnus-summary-help-map nil)
5078
5079 (put 'gnus-summary-mode 'mode-class 'special)
5080
5081 (if gnus-summary-mode-map
5082     nil
5083   (setq gnus-summary-mode-map (make-keymap))
5084   (suppress-keymap gnus-summary-mode-map)
5085
5086   ;; Non-orthogonal keys
5087
5088   (define-key gnus-summary-mode-map " " 'gnus-summary-next-page)
5089   (define-key gnus-summary-mode-map "\177" 'gnus-summary-prev-page)
5090   (define-key gnus-summary-mode-map "\r" 'gnus-summary-scroll-up)
5091   (define-key gnus-summary-mode-map "n" 'gnus-summary-next-unread-article)
5092   (define-key gnus-summary-mode-map "p" 'gnus-summary-prev-unread-article)
5093   (define-key gnus-summary-mode-map "N" 'gnus-summary-next-article)
5094   (define-key gnus-summary-mode-map "P" 'gnus-summary-prev-article)
5095   (define-key gnus-summary-mode-map "\M-\C-n" 'gnus-summary-next-same-subject)
5096   (define-key gnus-summary-mode-map "\M-\C-p" 'gnus-summary-prev-same-subject)
5097   (define-key gnus-summary-mode-map "\M-n" 'gnus-summary-next-unread-subject)
5098   (define-key gnus-summary-mode-map "\M-p" 'gnus-summary-prev-unread-subject)
5099   (define-key gnus-summary-mode-map "." 'gnus-summary-first-unread-article)
5100   (define-key gnus-summary-mode-map "," 'gnus-summary-best-unread-article)
5101   (define-key gnus-summary-mode-map "\M-s" 'gnus-summary-search-article-forward)
5102   (define-key gnus-summary-mode-map "\M-r" 'gnus-summary-search-article-backward)
5103   (define-key gnus-summary-mode-map "<" 'gnus-summary-beginning-of-article)
5104   (define-key gnus-summary-mode-map ">" 'gnus-summary-end-of-article)
5105   (define-key gnus-summary-mode-map "j" 'gnus-summary-goto-subject)
5106   (define-key gnus-summary-mode-map "^" 'gnus-summary-refer-parent-article)
5107   (define-key gnus-summary-mode-map "\M-^" 'gnus-summary-refer-article)
5108   (define-key gnus-summary-mode-map "u" 'gnus-summary-tick-article-forward)
5109   (define-key gnus-summary-mode-map "!" 'gnus-summary-tick-article-forward)
5110   (define-key gnus-summary-mode-map "U" 'gnus-summary-tick-article-backward)
5111   (define-key gnus-summary-mode-map "d" 'gnus-summary-mark-as-read-forward)
5112   (define-key gnus-summary-mode-map "D" 'gnus-summary-mark-as-read-backward)
5113   (define-key gnus-summary-mode-map "E" 'gnus-summary-mark-as-expirable)
5114   (define-key gnus-summary-mode-map "\M-u" 'gnus-summary-clear-mark-forward)
5115   (define-key gnus-summary-mode-map "\M-U" 'gnus-summary-clear-mark-backward)
5116   (define-key gnus-summary-mode-map "k" 'gnus-summary-kill-same-subject-and-select)
5117   (define-key gnus-summary-mode-map "\C-k" 'gnus-summary-kill-same-subject)
5118   (define-key gnus-summary-mode-map "\M-\C-k" 'gnus-summary-kill-thread)
5119   (define-key gnus-summary-mode-map "\M-\C-l" 'gnus-summary-lower-thread)
5120   (define-key gnus-summary-mode-map "e" 'gnus-summary-edit-article)
5121   (define-key gnus-summary-mode-map "#" 'gnus-summary-mark-as-processable)
5122   (define-key gnus-summary-mode-map "\M-#" 'gnus-summary-unmark-as-processable)
5123   (define-key gnus-summary-mode-map "\M-\C-t" 'gnus-summary-toggle-threads)
5124   (define-key gnus-summary-mode-map "\M-\C-s" 'gnus-summary-show-thread)
5125   (define-key gnus-summary-mode-map "\M-\C-h" 'gnus-summary-hide-thread)
5126   (define-key gnus-summary-mode-map "\M-\C-f" 'gnus-summary-next-thread)
5127   (define-key gnus-summary-mode-map "\M-\C-b" 'gnus-summary-prev-thread)
5128   (define-key gnus-summary-mode-map "\M-\C-u" 'gnus-summary-up-thread)
5129   (define-key gnus-summary-mode-map "\M-\C-d" 'gnus-summary-down-thread)
5130   (define-key gnus-summary-mode-map "&" 'gnus-summary-execute-command)
5131   (define-key gnus-summary-mode-map "c" 'gnus-summary-catchup-and-exit)
5132   (define-key gnus-summary-mode-map "\C-w" 'gnus-summary-mark-region-as-read)
5133   (define-key gnus-summary-mode-map "\C-t" 'gnus-summary-toggle-truncation)
5134   (define-key gnus-summary-mode-map "?" 'gnus-summary-mark-as-dormant)
5135   (define-key gnus-summary-mode-map "\C-c\M-\C-s" 'gnus-summary-show-all-expunged)
5136   (define-key gnus-summary-mode-map "\C-c\C-s\C-n" 'gnus-summary-sort-by-number)
5137   (define-key gnus-summary-mode-map "\C-c\C-s\C-a" 'gnus-summary-sort-by-author)
5138   (define-key gnus-summary-mode-map "\C-c\C-s\C-s" 'gnus-summary-sort-by-subject)
5139   (define-key gnus-summary-mode-map "\C-c\C-s\C-d" 'gnus-summary-sort-by-date)
5140   (define-key gnus-summary-mode-map "\C-c\C-s\C-i" 'gnus-summary-sort-by-score)
5141   (define-key gnus-summary-mode-map "=" 'gnus-summary-expand-window)
5142   (define-key gnus-summary-mode-map "\C-x\C-s" 'gnus-summary-reselect-current-group)
5143   (define-key gnus-summary-mode-map "\M-g" 'gnus-summary-rescan-group)
5144   (define-key gnus-summary-mode-map "w" 'gnus-summary-stop-page-breaking)
5145   (define-key gnus-summary-mode-map "\C-c\C-r" 'gnus-summary-caesar-message)
5146   (define-key gnus-summary-mode-map "\M-t" 'gnus-summary-toggle-mime)
5147   (define-key gnus-summary-mode-map "f" 'gnus-summary-followup)
5148   (define-key gnus-summary-mode-map "F" 'gnus-summary-followup-with-original)
5149   (define-key gnus-summary-mode-map "C" 'gnus-summary-cancel-article)
5150   (define-key gnus-summary-mode-map "r" 'gnus-summary-reply)
5151   (define-key gnus-summary-mode-map "R" 'gnus-summary-reply-with-original)
5152   (define-key gnus-summary-mode-map "\C-c\C-f" 'gnus-summary-mail-forward)
5153   (define-key gnus-summary-mode-map "o" 'gnus-summary-save-article)
5154   (define-key gnus-summary-mode-map "\C-o" 'gnus-summary-save-article-mail)
5155   (define-key gnus-summary-mode-map "|" 'gnus-summary-pipe-output)
5156   (define-key gnus-summary-mode-map "\M-k" 'gnus-summary-edit-local-kill)
5157   (define-key gnus-summary-mode-map "\M-K" 'gnus-summary-edit-global-kill)
5158   (define-key gnus-summary-mode-map "V" 'gnus-version)
5159   (define-key gnus-summary-mode-map "\C-c\C-d" 'gnus-summary-describe-group)
5160   (define-key gnus-summary-mode-map "q" 'gnus-summary-exit)
5161   (define-key gnus-summary-mode-map "Q" 'gnus-summary-exit-no-update)
5162   (define-key gnus-summary-mode-map "\C-c\C-i" 'gnus-info-find-node)
5163   (define-key gnus-summary-mode-map gnus-mouse-2 'gnus-mouse-pick-article)
5164   (define-key gnus-summary-mode-map "m" 'gnus-summary-mail-other-window)
5165   (define-key gnus-summary-mode-map "a" 'gnus-summary-post-news)
5166   (define-key gnus-summary-mode-map "x" 'gnus-summary-remove-lines-marked-as-read)
5167 ; (define-key gnus-summary-mode-map "X" 'gnus-summary-remove-lines-marked-with)
5168   (define-key gnus-summary-mode-map "s" 'gnus-summary-isearch-article)
5169   (define-key gnus-summary-mode-map "t" 'gnus-summary-toggle-header)
5170   (define-key gnus-summary-mode-map "g" 'gnus-summary-show-article)
5171 ;  (define-key gnus-summary-mode-map "?" 'gnus-summary-describe-briefly)
5172   (define-key gnus-summary-mode-map "l" 'gnus-summary-goto-last-article)
5173   (define-key gnus-summary-mode-map "\C-c\C-v\C-v" 'gnus-uu-decode-uu-view)
5174   (define-key gnus-summary-mode-map "\C-d" 'gnus-summary-enter-digest-group)
5175   (define-key gnus-summary-mode-map "v" 'gnus-summary-verbose-headers)
5176   (define-key gnus-summary-mode-map "\C-c\C-b" 'gnus-bug)
5177
5178
5179   ;; Sort of orthogonal keymap
5180   (define-prefix-command 'gnus-summary-mark-map)
5181   (define-key gnus-summary-mode-map "M" 'gnus-summary-mark-map)
5182   (define-key gnus-summary-mark-map "t" 'gnus-summary-tick-article-forward)
5183   (define-key gnus-summary-mark-map "!" 'gnus-summary-tick-article-forward)
5184   (define-key gnus-summary-mark-map "d" 'gnus-summary-mark-as-read-forward)
5185   (define-key gnus-summary-mark-map "r" 'gnus-summary-mark-as-read-forward)
5186   (define-key gnus-summary-mark-map "c" 'gnus-summary-clear-mark-forward)
5187   (define-key gnus-summary-mark-map " " 'gnus-summary-clear-mark-forward)
5188   (define-key gnus-summary-mark-map "e" 'gnus-summary-mark-as-expirable)
5189   (define-key gnus-summary-mark-map "x" 'gnus-summary-mark-as-expirable)
5190   (define-key gnus-summary-mark-map "?" 'gnus-summary-mark-as-dormant)
5191   (define-key gnus-summary-mark-map "b" 'gnus-summary-set-bookmark)
5192   (define-key gnus-summary-mark-map "B" 'gnus-summary-remove-bookmark)
5193   (define-key gnus-summary-mark-map "#" 'gnus-summary-mark-as-processable)
5194   (define-key gnus-summary-mark-map "\M-#" 'gnus-summary-unmark-as-processable)
5195   (define-key gnus-summary-mark-map "\M-r" 'gnus-summary-remove-lines-marked-as-read)
5196   (define-key gnus-summary-mark-map "\M-\C-r" 'gnus-summary-remove-lines-marked-with)
5197   (define-key gnus-summary-mark-map "D" 'gnus-summary-show-all-dormant)
5198   (define-key gnus-summary-mark-map "\M-D" 'gnus-summary-hide-all-dormant)
5199   (define-key gnus-summary-mark-map "S" 'gnus-summary-show-all-expunged)
5200   (define-key gnus-summary-mark-map "C" 'gnus-summary-catchup)
5201   (define-key gnus-summary-mark-map "H" 'gnus-summary-catchup-to-here)
5202   (define-key gnus-summary-mark-map "\C-c" 'gnus-summary-catchup-all)
5203   (define-key gnus-summary-mark-map "k" 'gnus-summary-kill-same-subject-and-select)
5204   (define-key gnus-summary-mark-map "K" 'gnus-summary-kill-same-subject)
5205
5206   (define-prefix-command 'gnus-summary-mscore-map)
5207   (define-key gnus-summary-mark-map "s" 'gnus-summary-mscore-map)
5208   (define-key gnus-summary-mscore-map "c" 'gnus-summary-clear-above)
5209   (define-key gnus-summary-mscore-map "u" 'gnus-summary-tick-above)
5210   (define-key gnus-summary-mscore-map "m" 'gnus-summary-mark-above)
5211   (define-key gnus-summary-mscore-map "k" 'gnus-summary-kill-below)
5212
5213   (define-key gnus-summary-mark-map "p" 'gnus-uu-mark-map)
5214   
5215   (define-key gnus-summary-mode-map "S" 'gnus-summary-send-map)
5216   
5217   (define-prefix-command 'gnus-summary-goto-map)
5218   (define-key gnus-summary-mode-map "G" 'gnus-summary-goto-map)
5219   (define-key gnus-summary-goto-map "n" 'gnus-summary-next-unread-article)
5220   (define-key gnus-summary-goto-map "p" 'gnus-summary-prev-unread-article)
5221   (define-key gnus-summary-goto-map "N" 'gnus-summary-next-article)
5222   (define-key gnus-summary-goto-map "P" 'gnus-summary-prev-article)
5223   (define-key gnus-summary-goto-map "\C-n" 'gnus-summary-next-same-subject)
5224   (define-key gnus-summary-goto-map "\C-p" 'gnus-summary-prev-same-subject)
5225   (define-key gnus-summary-goto-map "\M-n" 'gnus-summary-next-unread-subject)
5226   (define-key gnus-summary-goto-map "\M-p" 'gnus-summary-prev-unread-subject)
5227   (define-key gnus-summary-goto-map "f" 'gnus-summary-first-unread-article)
5228   (define-key gnus-summary-goto-map "b" 'gnus-summary-best-unread-article)
5229   (define-key gnus-summary-goto-map "g" 'gnus-summary-goto-subject)
5230   (define-key gnus-summary-goto-map "l" 'gnus-summary-goto-last-article)
5231   (define-key gnus-summary-goto-map "p" 'gnus-summary-pop-article)
5232
5233
5234   (define-prefix-command 'gnus-summary-thread-map)
5235   (define-key gnus-summary-mode-map "T" 'gnus-summary-thread-map)
5236   (define-key gnus-summary-thread-map "k" 'gnus-summary-kill-thread)
5237   (define-key gnus-summary-thread-map "l" 'gnus-summary-lower-thread)
5238   (define-key gnus-summary-thread-map "i" 'gnus-summary-raise-thread)
5239   (define-key gnus-summary-thread-map "T" 'gnus-summary-toggle-threads)
5240   (define-key gnus-summary-thread-map "s" 'gnus-summary-show-thread)
5241   (define-key gnus-summary-thread-map "S" 'gnus-summary-show-all-threads)
5242   (define-key gnus-summary-thread-map "h" 'gnus-summary-hide-thread)
5243   (define-key gnus-summary-thread-map "H" 'gnus-summary-hide-all-threads)
5244   (define-key gnus-summary-thread-map "n" 'gnus-summary-next-thread)
5245   (define-key gnus-summary-thread-map "p" 'gnus-summary-prev-thread)
5246   (define-key gnus-summary-thread-map "u" 'gnus-summary-up-thread)
5247   (define-key gnus-summary-thread-map "d" 'gnus-summary-down-thread)
5248   (define-key gnus-summary-thread-map "#" 'gnus-uu-mark-thread)
5249
5250   
5251   (define-prefix-command 'gnus-summary-exit-map)
5252   (define-key gnus-summary-mode-map "Z" 'gnus-summary-exit-map)
5253   (define-key gnus-summary-exit-map "c" 'gnus-summary-catchup-and-exit)
5254   (define-key gnus-summary-exit-map "C" 'gnus-summary-catchup-all-and-exit)
5255   (define-key gnus-summary-exit-map "E" 'gnus-summary-exit-no-update)
5256   (define-key gnus-summary-exit-map "Q" 'gnus-summary-exit)
5257   (define-key gnus-summary-exit-map "Z" 'gnus-summary-exit)
5258   (define-key gnus-summary-exit-map "n" 'gnus-summary-catchup-and-goto-next-group)
5259   (define-key gnus-summary-exit-map "R" 'gnus-summary-reselect-current-group)
5260   (define-key gnus-summary-exit-map "G" 'gnus-summary-rescan-group)
5261   (define-key gnus-summary-exit-map "N" 'gnus-summary-next-group)
5262   (define-key gnus-summary-exit-map "P" 'gnus-summary-prev-group)
5263
5264
5265   (define-prefix-command 'gnus-summary-article-map)
5266   (define-key gnus-summary-mode-map "A" 'gnus-summary-article-map)
5267   (define-key gnus-summary-article-map " " 'gnus-summary-next-page)
5268   (define-key gnus-summary-article-map "n" 'gnus-summary-next-page)
5269   (define-key gnus-summary-article-map "\177" 'gnus-summary-prev-page)
5270   (define-key gnus-summary-article-map "p" 'gnus-summary-prev-page)
5271   (define-key gnus-summary-article-map "\r" 'gnus-summary-scroll-up)
5272   (define-key gnus-summary-article-map "<" 'gnus-summary-beginning-of-article)
5273   (define-key gnus-summary-article-map ">" 'gnus-summary-end-of-article)
5274   (define-key gnus-summary-article-map "b" 'gnus-summary-beginning-of-article)
5275   (define-key gnus-summary-article-map "e" 'gnus-summary-end-of-article)
5276   (define-key gnus-summary-article-map "^" 'gnus-summary-refer-parent-article)
5277   (define-key gnus-summary-article-map "r" 'gnus-summary-refer-parent-article)
5278   (define-key gnus-summary-article-map "w" 'gnus-summary-stop-page-breaking)
5279   (define-key gnus-summary-article-map "c" 'gnus-summary-caesar-message)
5280   (define-key gnus-summary-article-map "g" 'gnus-summary-show-article)
5281   (define-key gnus-summary-article-map "t" 'gnus-summary-toggle-header)
5282   (define-key gnus-summary-article-map "m" 'gnus-summary-toggle-mime)
5283   (define-key gnus-summary-article-map "s" 'gnus-summary-isearch-article)
5284
5285
5286   (define-prefix-command 'gnus-summary-wash-map)
5287   (define-key gnus-summary-mode-map "W" 'gnus-summary-wash-map)
5288   (define-key gnus-summary-wash-map "h" 'gnus-article-hide-headers)
5289   (define-key gnus-summary-wash-map "s" 'gnus-article-hide-signature)
5290   (define-key gnus-summary-wash-map "c" 'gnus-article-hide-citation)
5291   (define-key gnus-summary-wash-map "\C-c" 'gnus-article-hide-citation-maybe)
5292   (define-key gnus-summary-wash-map "o" 'gnus-article-treat-overstrike)
5293   (define-key gnus-summary-wash-map "w" 'gnus-article-word-wrap)
5294   (define-key gnus-summary-wash-map "m" 'gnus-article-remove-cr)
5295   (define-key gnus-summary-wash-map "q" 'gnus-article-de-quoted-unreadable)
5296   (define-key gnus-summary-wash-map "f" 'gnus-article-display-x-face)
5297   (define-key gnus-summary-wash-map "t" 'gnus-article-date-ut)
5298   (define-key gnus-summary-wash-map "\C-t" 'gnus-article-date-local)
5299   (define-key gnus-summary-wash-map "T" 'gnus-article-date-lapsed)
5300
5301   (define-key gnus-summary-wash-map "A" 'gnus-article-highlight)
5302   (define-key gnus-summary-wash-map "a" 'gnus-article-hide)
5303   (define-key gnus-summary-wash-map "H" 'gnus-article-highlight-headers)
5304   (define-key gnus-summary-wash-map "C" 'gnus-article-highlight-citation)
5305   (define-key gnus-summary-wash-map "S" 'gnus-article-highlight-signature)
5306   (define-key gnus-summary-wash-map "b" 'gnus-article-add-buttons)
5307
5308
5309   (define-prefix-command 'gnus-summary-help-map)
5310   (define-key gnus-summary-mode-map "H" 'gnus-summary-help-map)
5311   (define-key gnus-summary-help-map "v" 'gnus-version)
5312   (define-key gnus-summary-help-map "f" 'gnus-summary-fetch-faq)
5313   (define-key gnus-summary-help-map "d" 'gnus-summary-describe-group)
5314   (define-key gnus-summary-help-map "h" 'gnus-summary-describe-briefly)
5315   (define-key gnus-summary-help-map "i" 'gnus-info-find-node)
5316
5317
5318   (define-prefix-command 'gnus-summary-backend-map)
5319   (define-key gnus-summary-mode-map "B" 'gnus-summary-backend-map)
5320   (define-key gnus-summary-backend-map "e" 'gnus-summary-expire-articles)
5321   (define-key gnus-summary-backend-map "\M-\C-e" 
5322     'gnus-summary-expire-articles-now)
5323   (define-key gnus-summary-backend-map "\177" 'gnus-summary-delete-article)
5324   (define-key gnus-summary-backend-map "m" 'gnus-summary-move-article)
5325   (define-key gnus-summary-backend-map "r" 'gnus-summary-respool-article)
5326   (define-key gnus-summary-backend-map "w" 'gnus-summary-edit-article)
5327   (define-key gnus-summary-backend-map "c" 'gnus-summary-copy-article)
5328   (define-key gnus-summary-backend-map "q" 'gnus-summary-fancy-query)
5329   (define-key gnus-summary-backend-map "i" 'gnus-summary-import-article)
5330
5331
5332   (define-prefix-command 'gnus-summary-save-map)
5333   (define-key gnus-summary-mode-map "O" 'gnus-summary-save-map)
5334   (define-key gnus-summary-save-map "o" 'gnus-summary-save-article)
5335   (define-key gnus-summary-save-map "m" 'gnus-summary-save-article-mail)
5336   (define-key gnus-summary-save-map "r" 'gnus-summary-save-article-rmail)
5337   (define-key gnus-summary-save-map "f" 'gnus-summary-save-article-file)
5338   (define-key gnus-summary-save-map "h" 'gnus-summary-save-article-folder)
5339   (define-key gnus-summary-save-map "v" 'gnus-summary-save-article-vm)
5340   (define-key gnus-summary-save-map "p" 'gnus-summary-pipe-output)
5341 ;  (define-key gnus-summary-save-map "s" 'gnus-soup-add-article)
5342
5343   (define-key gnus-summary-mode-map "X" 'gnus-uu-extract-map)
5344   
5345   (define-prefix-command 'gnus-summary-various-map)
5346   (define-key gnus-summary-mode-map "V" 'gnus-summary-various-map)
5347   (define-key gnus-summary-various-map "u" 'gnus-summary-universal-argument)
5348   (define-key gnus-summary-various-map "\C-s" 'gnus-summary-search-article-forward)
5349   (define-key gnus-summary-various-map "\C-r" 'gnus-summary-search-article-backward)
5350   (define-key gnus-summary-various-map "r" 'gnus-summary-refer-article)
5351   (define-key gnus-summary-various-map "&" 'gnus-summary-execute-command)
5352   (define-key gnus-summary-various-map "T" 'gnus-summary-toggle-truncation)
5353   (define-key gnus-summary-various-map "e" 'gnus-summary-expand-window)
5354   (define-key gnus-summary-various-map "D" 'gnus-summary-enter-digest-group)
5355   (define-key gnus-summary-various-map "k" 'gnus-summary-edit-local-kill)
5356   (define-key gnus-summary-various-map "K" 'gnus-summary-edit-global-kill)
5357
5358   (define-key gnus-summary-various-map "S" 'gnus-summary-score-map)
5359
5360   (define-prefix-command 'gnus-summary-sort-map)
5361   (define-key gnus-summary-various-map "s" 'gnus-summary-sort-map)
5362   (define-key gnus-summary-sort-map "n" 'gnus-summary-sort-by-number)
5363   (define-key gnus-summary-sort-map "a" 'gnus-summary-sort-by-author)
5364   (define-key gnus-summary-sort-map "s" 'gnus-summary-sort-by-subject)
5365   (define-key gnus-summary-sort-map "d" 'gnus-summary-sort-by-date)
5366   (define-key gnus-summary-sort-map "i" 'gnus-summary-sort-by-score)
5367
5368   (define-key gnus-summary-mode-map "I" 'gnus-summary-increase-score)
5369   (define-key gnus-summary-mode-map "L" 'gnus-summary-lower-score)
5370   )
5371
5372
5373 \f
5374
5375 (defun gnus-summary-mode (&optional group)
5376   "Major mode for reading articles.
5377
5378 All normal editing commands are switched off.
5379 \\<gnus-summary-mode-map>
5380 Each line in this buffer represents one article.  To read an
5381 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
5382 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]', 
5383 respectively.
5384
5385 You can also post articles and send mail from this buffer.  To 
5386 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author 
5387 of an article, type `\\[gnus-summary-reply]'.
5388
5389 There are approx. one gazillion commands you can execute in this 
5390 buffer; read the info pages for more information (`\\[gnus-info-find-node]'). 
5391
5392 The following commands are available:
5393
5394 \\{gnus-summary-mode-map}"
5395   (interactive)
5396   (if gnus-visual (gnus-summary-make-menu-bar))
5397   (kill-all-local-variables)
5398   (let ((locals gnus-summary-local-variables))
5399     (while locals
5400       (if (consp (car locals))
5401           (progn
5402             (make-local-variable (car (car locals)))
5403             (set (car (car locals)) (eval (cdr (car locals)))))
5404         (make-local-variable (car locals))
5405         (set (car locals) nil))
5406       (setq locals (cdr locals))))
5407   (gnus-make-thread-indent-array)
5408   (setq mode-line-modified "-- ")
5409   (make-local-variable 'mode-line-format)
5410   (setq mode-line-format (copy-sequence mode-line-format))
5411   (and (equal (nth 3 mode-line-format) "   ")
5412        (setcar (nthcdr 3 mode-line-format) ""))
5413   (setq major-mode 'gnus-summary-mode)
5414   (setq mode-name "Summary")
5415   (make-local-variable 'minor-mode-alist)
5416   (use-local-map gnus-summary-mode-map)
5417   (buffer-disable-undo (current-buffer))
5418   (setq buffer-read-only t)             ;Disable modification
5419   (setq truncate-lines t)
5420   (setq selective-display t)
5421   (setq selective-display-ellipses t)   ;Display `...'
5422   (setq buffer-display-table gnus-summary-display-table)
5423   (setq gnus-newsgroup-name group)
5424   (run-hooks 'gnus-summary-mode-hook))
5425
5426 (defun gnus-summary-make-display-table ()
5427   ;; Change the display table.  Odd characters have a tendency to mess
5428   ;; up nicely formatted displays - we make all possible glyphs
5429   ;; display only a single character.
5430
5431   ;; We start from the standard display table, if any.
5432   (setq gnus-summary-display-table 
5433         (or (copy-sequence standard-display-table)
5434             (make-display-table)))
5435   ;; Nix out all the control chars...
5436   (let ((i 32))
5437     (while (>= (setq i (1- i)) 0)
5438       (aset gnus-summary-display-table i [??])))
5439   ;; ... but not newline and cr, of course. (cr is necessary for the
5440   ;; selective display).  
5441   (aset gnus-summary-display-table ?\n nil)
5442   (aset gnus-summary-display-table ?\r nil)
5443   ;; We nix out any glyphs over 126 that are not set already.  
5444   (let ((i 256))
5445     (while (>= (setq i (1- i)) 127)
5446       ;; Only modify if the entry is nil.
5447       (or (aref gnus-summary-display-table i) 
5448           (aset gnus-summary-display-table i [??])))))
5449
5450 (defun gnus-summary-clear-local-variables ()
5451   (let ((locals gnus-summary-local-variables))
5452     (while locals
5453       (if (consp (car locals))
5454           (and (vectorp (car (car locals)))
5455                (set (car (car locals)) nil))
5456         (and (vectorp (car locals))
5457              (set (car locals) nil)))
5458       (setq locals (cdr locals)))))
5459
5460 ;; Some summary mode macros.
5461
5462 ;; Return a header specified by a NUMBER.
5463 (defun gnus-get-header-by-number (number)
5464   (save-excursion
5465     (set-buffer gnus-summary-buffer)
5466     (or gnus-newsgroup-headers-hashtb-by-number
5467         (gnus-make-headers-hashtable-by-number))
5468     (gnus-gethash (int-to-string number)
5469                   gnus-newsgroup-headers-hashtb-by-number)))
5470
5471 ;; Fast version of the function above.
5472 (defmacro gnus-get-header-by-num (number)
5473   (` (gnus-gethash (int-to-string (, number)) 
5474                    gnus-newsgroup-headers-hashtb-by-number)))
5475
5476 (defmacro gnus-summary-search-forward (&optional unread subject backward)
5477   "Search for article forward.
5478 If UNREAD is non-nil, only unread articles are selected.
5479 If SUBJECT is non-nil, the article which has the same subject will be
5480 searched for. 
5481 If BACKWARD is non-nil, the search will be performed backwards instead."
5482   (` (gnus-summary-search-subject (, backward) (, unread) (, subject))))
5483
5484 (defmacro gnus-summary-search-backward (&optional unread subject)
5485   "Search for article backward.
5486 If 1st optional argument UNREAD is non-nil, only unread article is selected.
5487 If 2nd optional argument SUBJECT is non-nil, the article which has
5488 the same subject will be searched for."
5489   (` (gnus-summary-search-forward (, unread) (, subject) t)))
5490
5491 (defmacro gnus-summary-article-number (&optional number-or-nil)
5492   "The article number of the article on the current line.
5493 If there isn's an article number here, then we return the current
5494 article number."
5495   (if number-or-nil
5496       '(get-text-property (gnus-point-at-bol) 'gnus-number)
5497     '(or (get-text-property (gnus-point-at-bol) 'gnus-number) 
5498          gnus-current-article)))
5499
5500 (defmacro gnus-summary-thread-level ()
5501   "The thread level of the article on the current line."
5502   '(or (get-text-property (gnus-point-at-bol) 'gnus-level)
5503        0))
5504
5505 (defmacro gnus-summary-article-mark ()
5506   "The mark on the current line."
5507   '(get-text-property (gnus-point-at-bol) 'gnus-mark))
5508
5509 (defun gnus-summary-subject-string ()
5510   "Return current subject string or nil if nothing."
5511   (let ((article (gnus-summary-article-number))
5512         header)
5513     (and article 
5514          (setq header (gnus-get-header-by-num article))
5515          (vectorp header)
5516          (header-subject header))))
5517
5518
5519 (defun gnus-mouse-pick-article (e)
5520   (interactive "e")
5521   (mouse-set-point e)
5522   (gnus-summary-next-page nil t))
5523
5524 (defun gnus-summary-setup-buffer (group)
5525   "Initialize summary buffer."
5526   (let ((buffer (concat "*Summary " group "*")))
5527     (if (get-buffer buffer)
5528         (progn
5529           (set-buffer buffer)
5530           (not gnus-newsgroup-begin))
5531       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
5532       (setq gnus-summary-buffer (set-buffer (get-buffer-create buffer)))
5533       (gnus-add-current-to-buffer-list)
5534       (gnus-summary-mode group)
5535       (and gnus-carpal (gnus-carpal-setup-buffer 'summary))
5536       (setq gnus-newsgroup-name group)
5537       t)))
5538
5539 (defun gnus-set-global-variables ()
5540   ;; Set the global equivalents of the summary buffer-local variables
5541   ;; to the latest values they had. These reflect the summary buffer
5542   ;; that was in action when the last article was fetched.
5543   (if (eq major-mode 'gnus-summary-mode) 
5544       (progn
5545         (setq gnus-summary-buffer (current-buffer))
5546         (let ((name gnus-newsgroup-name)
5547               (marked gnus-newsgroup-marked)
5548               (unread gnus-newsgroup-unreads)
5549               (headers gnus-current-headers)
5550               (score-file gnus-current-score-file))
5551           (save-excursion
5552             (set-buffer gnus-group-buffer)
5553             (setq gnus-newsgroup-name name)
5554             (setq gnus-newsgroup-marked marked)
5555             (setq gnus-newsgroup-unreads unread)
5556             (setq gnus-current-headers headers)
5557             (setq gnus-current-score-file score-file))))))
5558
5559 (defun gnus-summary-insert-dummy-line (sformat subject number)
5560   (if (not sformat) 
5561       (setq sformat gnus-summary-dummy-line-format-spec))
5562   (let (b)
5563     (beginning-of-line)
5564     (setq b (point))
5565     (insert (eval sformat))
5566     (add-text-properties
5567      b (1+ b)
5568      (list 'gnus-number number 
5569            'gnus-mark gnus-dummy-mark
5570            'gnus-level 0))))
5571
5572 (defvar gnus-thread-indent-array nil)
5573 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
5574 (defun gnus-make-thread-indent-array ()
5575   (let ((n 200))
5576     (if (and gnus-thread-indent-array
5577              (= gnus-thread-indent-level gnus-thread-indent-array-level))
5578         nil
5579       (setq gnus-thread-indent-array (make-vector 201 "")
5580             gnus-thread-indent-array-level gnus-thread-indent-level)
5581       (while (>= n 0)
5582         (aset gnus-thread-indent-array n
5583               (make-string (* n gnus-thread-indent-level) ? ))
5584         (setq n (1- n))))))
5585
5586 (defun gnus-summary-insert-line 
5587   (sformat header level current unread replied expirable subject-or-nil
5588            &optional dummy score)
5589   (or sformat (setq sformat gnus-summary-line-format-spec))
5590   (let* ((indentation (aref gnus-thread-indent-array level))
5591          (lines (header-lines header))
5592          (score (or score gnus-summary-default-score 0))
5593          (score-char
5594           (if (or (null gnus-summary-default-score)
5595                   (<= (abs (- score gnus-summary-default-score))
5596                       gnus-summary-zcore-fuzz)) ? 
5597             (if (< score gnus-summary-default-score)
5598                 gnus-score-below-mark gnus-score-over-mark)))
5599          (replied (if replied gnus-replied-mark ? ))
5600          (from (header-from header))
5601          (name (cond 
5602                 ((string-match "(.+)" from)
5603                  (substring from (1+ (match-beginning 0)) (1- (match-end 0))))
5604                 ((string-match "<[^>]+> *$" from)
5605                  (let ((beg (match-beginning 0)))
5606                    (or (and (string-match "^\"[^\"]*\"" from)
5607                             (substring from (1+ (match-beginning 0))
5608                                        (1- (match-end 0))))
5609                        (substring from 0 beg))))
5610                 (t from)))
5611          (subject (header-subject header))
5612          (number (header-number header))
5613          (opening-bracket (if dummy ?\< ?\[))
5614          (closing-bracket (if dummy ?\> ?\]))
5615          (buffer-read-only nil)
5616          (b (progn (beginning-of-line) (point))))
5617     (or (numberp lines) (setq lines 0))
5618     (insert (eval sformat))
5619     (add-text-properties
5620      b (1+ b) (list 'gnus-number number 
5621                     'gnus-mark (or unread gnus-unread-mark)
5622                     'gnus-level level))))
5623
5624 (defun gnus-summary-update-line (&optional dont-update)
5625   ;; Update summary line after change.
5626   (or (not gnus-summary-default-score)
5627       gnus-summary-inhibit-highlight
5628       (let ((gnus-summary-inhibit-highlight t)
5629             (article (gnus-summary-article-number)))
5630         (progn
5631           (or dont-update
5632               (if (and gnus-summary-mark-below
5633                        (< (gnus-summary-article-score)
5634                           gnus-summary-mark-below))
5635                   (and (not (memq article gnus-newsgroup-marked))
5636                        (not (memq article gnus-newsgroup-dormant))
5637                        (memq article gnus-newsgroup-unreads)
5638                        (gnus-summary-mark-article-as-read gnus-low-score-mark))
5639                 (and (eq (gnus-summary-article-mark) gnus-low-score-mark)
5640                      (gnus-summary-mark-article-as-unread gnus-unread-mark))))
5641           (and gnus-visual
5642                (run-hooks 'gnus-summary-update-hook))))))
5643
5644 (defun gnus-summary-update-lines (&optional beg end)
5645   ;; Mark article as read (or not) by taking into account scores.
5646   (let ((beg (or beg (point-min)))
5647         (end (or end (point-max))))
5648     (if (or (not gnus-summary-default-score)
5649             gnus-summary-inhibit-highlight)
5650         ()
5651       (let ((gnus-summary-inhibit-highlight t)
5652             article)
5653         (save-excursion
5654           (set-buffer gnus-summary-buffer)
5655           (goto-char beg)
5656           (beginning-of-line)
5657           (while (and (not (eobp)) (< (point) end))
5658             (if (and gnus-summary-mark-below
5659                      (< (or (cdr (assq 
5660                                   (setq article (get-text-property 
5661                                                  (point) 'gnus-number))
5662                                   gnus-newsgroup-scored))
5663                             gnus-summary-default-score 0)
5664                         gnus-summary-mark-below))
5665                 ;; We want to possibly mark it as read...
5666                 (and (not (memq article gnus-newsgroup-marked))
5667                      (not (memq article gnus-newsgroup-dormant))
5668                      (memq article gnus-newsgroup-unreads)
5669                      (gnus-summary-mark-article-as-read gnus-low-score-mark))
5670               ;; We want to possibly mark it as unread.
5671               (and (eq (get-text-property (point) 'gnus-mark)
5672                        gnus-low-score-mark)
5673                    (gnus-summary-mark-article-as-unread gnus-unread-mark)))
5674             ;; Do the visual highlights at the same time.
5675             (and gnus-visual (run-hooks 'gnus-summary-update-hook))
5676             (forward-line 1)))))))
5677
5678 (defvar gnus-tmp-gathered nil)
5679
5680 (defun gnus-summary-number-of-articles-in-thread (thread &optional char)
5681   ;; Sum up all elements (and sub-elements) in a list.
5682   (let* ((number 
5683           (if (and (consp thread) (cdr thread)
5684                    (not (memq (header-number (car (car (cdr thread))))
5685                               gnus-tmp-gathered))
5686                    )
5687               (apply 
5688                '+ 1 (mapcar 'gnus-summary-number-of-articles-in-thread 
5689                             (cdr thread)))
5690             1)))
5691     (if char 
5692         (if (> number 1) gnus-not-empty-thread-mark
5693           gnus-empty-thread-mark)
5694       number)))
5695
5696 (defun gnus-summary-read-group 
5697   (group &optional show-all no-article kill-buffer)
5698   "Start reading news in newsgroup GROUP.
5699 If SHOW-ALL is non-nil, already read articles are also listed.
5700 If NO-ARTICLE is non-nil, no article is selected initially."
5701   (gnus-message 5 "Retrieving newsgroup: %s..." group)
5702   (let* ((new-group (gnus-summary-setup-buffer group))
5703          (quit-config (nth 1 (assoc 'quit-config (gnus-find-method-for-group
5704                                                   group))))
5705          (did-select (and new-group (gnus-select-newsgroup group show-all))))
5706     (cond 
5707      ((not new-group)
5708       (gnus-set-global-variables)
5709       (gnus-kill-buffer kill-buffer)
5710       (gnus-configure-windows 'summary)
5711       (gnus-set-mode-line 'summary)
5712       (gnus-summary-position-cursor)
5713       (message "")
5714       t)
5715      ((null did-select) 
5716       (and (eq major-mode 'gnus-summary-mode)
5717            (not (equal (current-buffer) kill-buffer))
5718            (progn
5719              (kill-buffer (current-buffer))
5720              (if (not quit-config)
5721                  (progn
5722                    (set-buffer gnus-group-buffer)
5723                    (gnus-group-jump-to-group group)
5724                    (gnus-group-next-unread-group 1))
5725                (if (not (buffer-name (car quit-config)))
5726                    (gnus-configure-windows 'group 'force)
5727                  (set-buffer (car quit-config))
5728                  (and (eq major-mode 'gnus-summary-mode)
5729                       (gnus-set-global-variables))
5730                  (gnus-configure-windows (cdr quit-config))))))
5731       (message "Can't select group")
5732       nil)
5733      ((eq did-select 'quit)
5734       (and (eq major-mode 'gnus-summary-mode)
5735            (not (equal (current-buffer) kill-buffer))
5736            (kill-buffer (current-buffer)))
5737       (gnus-kill-buffer kill-buffer)
5738       (if (not quit-config)
5739           (progn
5740             (set-buffer gnus-group-buffer)
5741             (gnus-group-jump-to-group group)
5742             (gnus-group-next-unread-group 1)
5743             (gnus-configure-windows 'group 'force))
5744         (if (not (buffer-name (car quit-config)))
5745             (gnus-configure-windows 'group 'force)
5746           (set-buffer (car quit-config))
5747           (and (eq major-mode 'gnus-summary-mode)
5748                (gnus-set-global-variables))
5749           (gnus-configure-windows (cdr quit-config))))
5750       (signal 'quit nil))
5751      (t
5752       (gnus-set-global-variables)
5753       ;; Save the active value in effect when the group was entered.
5754       (setq gnus-newsgroup-active 
5755             (gnus-copy-sequence
5756              (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
5757       ;; You can change the subjects in this hook.
5758       (run-hooks 'gnus-select-group-hook)
5759       ;; Do score processing.
5760       (and gnus-use-scoring (gnus-possibly-score-headers))
5761       (gnus-update-format-specifications)
5762       ;; Generate the summary buffer.
5763       (gnus-summary-prepare)
5764       (if (zerop (buffer-size))
5765           (cond (gnus-newsgroup-dormant
5766                  (gnus-summary-show-all-dormant))
5767                 ((and gnus-newsgroup-scored show-all)
5768                  (gnus-summary-show-all-expunged))))
5769       ;; Function `gnus-apply-kill-file' must be called in this hook.
5770       (run-hooks 'gnus-apply-kill-hook)
5771       (if (zerop (buffer-size))
5772           (progn
5773             ;; This newsgroup is empty.
5774             (gnus-summary-catchup-and-exit nil t) ;Without confirmations.
5775             (gnus-message 6 "No unread news")
5776             (gnus-kill-buffer kill-buffer)
5777             nil)
5778         ;;(save-excursion
5779         ;;  (if kill-buffer
5780         ;;      (let ((gnus-summary-buffer kill-buffer))
5781         ;;      (gnus-configure-windows 'group))))
5782         ;; Hide conversation thread subtrees.  We cannot do this in
5783         ;; gnus-summary-prepare-hook since kill processing may not
5784         ;; work with hidden articles.
5785         (and gnus-show-threads
5786              gnus-thread-hide-subtree
5787              (gnus-summary-hide-all-threads))
5788         ;; Show first unread article if requested.
5789         (goto-char (point-min))
5790         (if (and (not no-article)
5791                  gnus-auto-select-first
5792                  (gnus-summary-first-unread-article))
5793             ()
5794           (gnus-configure-windows 'summary))
5795         (gnus-set-mode-line 'summary)
5796         (gnus-summary-position-cursor)
5797         ;; If in async mode, we send some info to the backend.
5798         (and gnus-newsgroup-async
5799              (setq gnus-newsgroup-threads (nreverse gnus-newsgroup-threads))
5800              (gnus-request-asynchronous 
5801               gnus-newsgroup-name
5802               (if (and gnus-asynchronous-article-function
5803                        (fboundp gnus-asynchronous-article-function))
5804                   (funcall gnus-asynchronous-article-function
5805                            gnus-newsgroup-threads)
5806                 gnus-newsgroup-threads)))
5807         (gnus-kill-buffer kill-buffer)
5808         (if (not (get-buffer-window gnus-group-buffer))
5809             ()
5810           ;; gotta use windows, because recenter does wierd stuff if
5811           ;; the current buffer ain't the displayed window.
5812           (let ((owin (selected-window))) 
5813             (select-window (get-buffer-window gnus-group-buffer))
5814             (and (gnus-group-goto-group group)
5815                  (recenter))
5816             (select-window owin))))
5817       t))))
5818
5819 (defun gnus-summary-prepare ()
5820   ;; Generate the summary buffer.
5821   (let ((buffer-read-only nil))
5822     (erase-buffer)
5823     (gnus-message 5 "Threading...")
5824     (gnus-summary-prepare-threads 
5825      (if gnus-show-threads
5826          (gnus-gather-threads 
5827           (gnus-sort-threads 
5828            (if (and gnus-summary-expunge-below
5829                     (not gnus-fetch-old-headers))
5830                (gnus-make-threads-and-expunge)
5831              (gnus-make-threads))))
5832        gnus-newsgroup-headers)
5833      'cull)
5834     (gnus-message 5 "Threading...done")
5835     (gnus-summary-update-lines)
5836     ;; Remove the final newline.
5837     ;;(goto-char (point-max))
5838     ;;(delete-char -1)
5839     ;; Call hooks for modifying summary buffer.
5840     ;; Suggested by sven@tde.LTH.Se (Sven Mattisson).
5841     (goto-char (point-min))
5842     (run-hooks 'gnus-summary-prepare-hook)))
5843
5844 (defun gnus-gather-threads (threads)
5845   "Gather threads that have lost their roots."
5846   (if (not gnus-summary-make-false-root)
5847       threads 
5848     (let ((hashtb (gnus-make-hashtable 1023))
5849           (prev threads)
5850           (result threads)
5851           subject hthread whole-subject)
5852       (while threads
5853         (setq whole-subject 
5854               (setq subject (header-subject (car (car threads)))))
5855         (if gnus-summary-gather-subject-limit
5856             (or (and (numberp gnus-summary-gather-subject-limit)
5857                      (> (length subject) gnus-summary-gather-subject-limit)
5858                      (setq subject
5859                            (substring subject 0 
5860                                       gnus-summary-gather-subject-limit)))
5861                 (and (eq 'fuzzy gnus-summary-gather-subject-limit)
5862                      (setq subject (gnus-simplify-subject-fuzzy subject))))
5863           (setq subject (gnus-simplify-subject-re subject)))
5864         (if (setq hthread 
5865                   (gnus-gethash subject hashtb))
5866             (progn
5867               (or (stringp (car (car hthread)))
5868                   (setcar hthread (list whole-subject (car hthread))))
5869               (setcdr (car hthread) (nconc (cdr (car hthread)) 
5870                                            (list (car threads))))
5871               (setcdr prev (cdr threads))
5872               (setq threads prev))
5873           (gnus-sethash subject threads hashtb))
5874         (setq prev threads)
5875         (setq threads (cdr threads)))
5876       result)))
5877
5878 (defun gnus-make-threads ()
5879   ;; This function takes the dependencies already made by 
5880   ;; `gnus-get-newsgroup-headers' and builds the trees. First we go
5881   ;; through the dependecies in the hash table and finds all the
5882   ;; roots. Roots do not refer back to any valid articles.
5883   (let (roots new-roots)
5884     (and gnus-fetch-old-headers (eq gnus-headers-retrieved-by 'nov)
5885          (gnus-build-old-threads))
5886     (mapatoms
5887      (lambda (refs)
5888        (if (not (car (symbol-value refs)))
5889            (setq roots (append (cdr (symbol-value refs)) roots))
5890          ;; Ok, these refer back to valid articles, but if
5891          ;; `gnus-thread-ignore-subject' is nil, we have to check that
5892          ;; the root has the same subject as its children. The children
5893          ;; that do not are made into roots and removed from the list
5894          ;; of children. 
5895          (or gnus-thread-ignore-subject
5896              (let* ((prev (symbol-value refs))
5897                     (subject (gnus-simplify-subject-re 
5898                               (header-subject (car prev))))
5899                     (headers (cdr prev)))
5900                (while headers
5901                  (if (not (string= subject
5902                                    (gnus-simplify-subject-re 
5903                                     (header-subject (car headers)))))
5904                      (progn
5905                        (setq new-roots (cons (car headers) new-roots))
5906                        (setcdr prev (cdr headers)))
5907                    (setq prev headers))
5908                  (setq headers (cdr headers)))))))
5909      gnus-newsgroup-dependencies)
5910
5911     ;; We enter the new roots into the dependencies structure to
5912     ;; ensure that any possible later thread-regeneration will be
5913     ;; possible. 
5914     (let ((r new-roots))
5915       (while r
5916         (gnus-sethash (concat (header-id (car r)) ".boo")
5917                       (list nil (car r)) gnus-newsgroup-dependencies)
5918         (setq r (cdr r))))
5919
5920     (setq roots (nconc new-roots roots))
5921     
5922     (mapcar 'gnus-trim-thread
5923             (apply 'append
5924                    (mapcar 'gnus-cut-thread
5925                            (mapcar 'gnus-make-sub-thread roots))))))
5926   
5927 (defun gnus-make-threads-and-expunge ()
5928   ;; This function takes the dependencies already made by 
5929   ;; `gnus-get-newsgroup-headers' and builds the trees. First we go
5930   ;; through the dependecies in the hash table and finds all the
5931   ;; roots. Roots do not refer back to any valid articles.
5932   (let ((default (or gnus-summary-default-score 0))
5933         (below gnus-summary-expunge-below)
5934         roots article new-roots)
5935     (and gnus-fetch-old-headers (eq gnus-headers-retrieved-by 'nov)
5936          (gnus-build-old-threads))
5937     (mapatoms
5938      (lambda (refs)
5939        (if (not (car (symbol-value refs)))
5940            ;; These articles do not refer back to any other articles -
5941            ;; they are roots.
5942            (let ((headers (cdr (symbol-value refs))))
5943              ;; We weed out the low-scored articles.
5944              (while headers
5945                (if (not (< (or (cdr (assq (header-number (car headers))
5946                                           gnus-newsgroup-scored)) default)
5947                            below))
5948                    ;; It is over.
5949                    (setq roots (cons (car headers) roots))
5950                  ;; It is below, so we mark it as read.
5951                  (setq gnus-newsgroup-unreads
5952                        (delq (header-number (car headers))
5953                              gnus-newsgroup-unreads)))
5954                (setq headers (cdr headers))))
5955          ;; Ok, these refer back to valid articles, but if
5956          ;; `gnus-thread-ignore-subject' is nil, we have to check that
5957          ;; the root has the same subject as its children. The children
5958          ;; that do not are made into roots and removed from the list
5959          ;; of children. 
5960          (or gnus-thread-ignore-subject
5961              (let* ((prev (symbol-value refs))
5962                     (subject (gnus-simplify-subject-re 
5963                               (header-subject (car prev))))
5964                     (headers (cdr prev)))
5965                (while headers
5966                  (if (not (string= subject
5967                                    (gnus-simplify-subject-re 
5968                                     (header-subject (car headers)))))
5969                      (progn
5970                        (if (not (< (or (cdr (assq (header-number (car headers))
5971                                                   gnus-newsgroup-scored))
5972                                        default) below))
5973                            (setq new-roots (cons (car headers) new-roots))
5974                          (setq gnus-newsgroup-unreads
5975                                (delq (header-number (car headers))
5976                                      gnus-newsgroup-unreads)))
5977                        (setcdr prev (cdr headers)))
5978                    (setq prev headers))
5979                  (setq headers (cdr headers)))))
5980          ;; If this article is expunged, some of the children might be
5981          ;; roots.  
5982          (if (< (or (cdr (assq (header-number (car (symbol-value refs)))
5983                                gnus-newsgroup-scored)) default)
5984                 below)
5985              (let* ((prev (symbol-value refs))
5986                     (headers (cdr prev)))
5987                (while headers
5988                  (setq article (header-number (car headers)))
5989                  (if (not (< (or (cdr (assq article gnus-newsgroup-scored))
5990                                  default) below))
5991                      (progn (setq new-roots (cons (car headers) new-roots))
5992                             (setq prev headers))
5993                    (setq gnus-newsgroup-unreads 
5994                          (delq article gnus-newsgroup-unreads))
5995                    (setcdr prev (cdr headers)))
5996                  (setq headers (cdr headers))))
5997            ;; It was not expunged, but we look at expunged children.
5998            (let* ((prev (symbol-value refs))
5999                   (headers (cdr prev))
6000                   article)
6001              (while headers
6002                (setq article (header-number (car headers)))
6003                (if (not (< (or (cdr (assq article gnus-newsgroup-scored))
6004                                default) below))
6005                    (setq prev headers)
6006                  (setq gnus-newsgroup-unreads 
6007                        (delq article gnus-newsgroup-unreads))
6008                  (setcdr prev (cdr headers)))
6009                (setq headers (cdr headers)))))))
6010      gnus-newsgroup-dependencies)
6011
6012     ;; We enter the new roots into the dependencies structure to
6013     ;; ensure that any possible later thread-regeneration will be
6014     ;; possible. 
6015     (let ((r new-roots))
6016       (while r
6017         (gnus-sethash (concat (header-id (car r)) ".boo")
6018                       (list nil (car r)) gnus-newsgroup-dependencies)
6019         (setq r (cdr r))))
6020
6021     (setq roots (nconc new-roots roots))
6022     
6023     (mapcar 'gnus-trim-thread
6024             (apply 'append
6025                    (mapcar 'gnus-cut-thread
6026                            (mapcar 'gnus-make-sub-thread roots))))))
6027   
6028 (defun gnus-cut-thread (thread)
6029   ;; Remove leaf dormant or ancient articles from THREAD.
6030   (let ((head (car thread))
6031         (tail (apply 'append (mapcar 'gnus-cut-thread (cdr thread)))))
6032     (if (and (null tail)
6033              (let ((number (header-number head)))
6034                (or (memq number gnus-newsgroup-ancient)
6035                    (memq number gnus-newsgroup-dormant)
6036                    (and gnus-summary-expunge-below
6037                         (eq gnus-fetch-old-headers 'some)
6038                         (< (or (cdr (assq number gnus-newsgroup-scored))
6039                                gnus-summary-default-score 0)
6040                            gnus-summary-expunge-below)
6041                         (progn
6042                           (setq gnus-newsgroup-unreads
6043                                 (delq number gnus-newsgroup-unreads))
6044                           t)))))
6045         nil
6046       (list (cons head tail)))))
6047
6048 (defun gnus-trim-thread (thread)
6049   ;; Remove root ancient articles with only one child from THREAD.
6050   (if (and (eq gnus-fetch-old-headers 'some)
6051            (memq (header-number (car thread)) gnus-newsgroup-ancient)
6052            (= (length thread) 2))
6053       (gnus-trim-thread (nth 1 thread))
6054     thread))
6055
6056 (defun gnus-make-sub-thread (root)
6057   ;; This function makes a sub-tree for a node in the tree.
6058   (let ((children (reverse (cdr (gnus-gethash (downcase (header-id root))
6059                                               gnus-newsgroup-dependencies)))))
6060     (cons root (mapcar 'gnus-make-sub-thread children))))
6061
6062 (defun gnus-build-old-threads ()
6063   ;; Look at all the articles that refer back to old articles, and
6064   ;; fetch the headers for the articles that aren't there. This will
6065   ;; build complete threads - if the roots haven't been expired by the
6066   ;; server, that is.
6067   (let (id heads)
6068     (mapatoms
6069      (lambda (refs)
6070        (if (not (car (symbol-value refs)))
6071            (progn
6072              (setq heads (cdr (symbol-value refs)))
6073              (while heads
6074                (if (not (memq (header-number (car heads))
6075                               gnus-newsgroup-dormant))
6076                    (progn
6077                      (setq id (symbol-name refs))
6078                      (while (and (setq id (gnus-build-get-header id))
6079                                  (not (car (gnus-gethash 
6080                                             id gnus-newsgroup-dependencies)))))
6081                      (setq heads nil))
6082                  (setq heads (cdr heads)))))))
6083      gnus-newsgroup-dependencies)))
6084
6085 (defun gnus-build-get-header (id)
6086   ;; Look through the buffer of NOV lines and find the header to
6087   ;; ID. Enter this line into the dependencies hash table, and return
6088   ;; the id of the parent article (if any).
6089   (let ((deps gnus-newsgroup-dependencies)
6090         found header)
6091     (prog1
6092         (save-excursion
6093           (set-buffer nntp-server-buffer)
6094           (goto-char (point-min))
6095           (while (and (not found) (search-forward id nil t))
6096             (beginning-of-line)
6097             (setq found (looking-at 
6098                          (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
6099                                  (regexp-quote id))))
6100             (or found (beginning-of-line 2)))
6101           (if found
6102               (let (ref)
6103                 (beginning-of-line)
6104                 (and
6105                  (setq header (gnus-nov-parse-line 
6106                                (read (current-buffer)) deps))
6107                  (setq ref (header-references header))
6108                  (string-match "\\(<[^>]+>\\) *$" ref)
6109                  (substring ref (match-beginning 1) (match-end 1))))))
6110       (and header
6111            (setq gnus-newsgroup-headers (cons header gnus-newsgroup-headers)
6112                  gnus-newsgroup-ancient (cons (header-number header)
6113                                               gnus-newsgroup-ancient))))))
6114
6115 ;; Re-build the thread containing ID.
6116 (defun gnus-rebuild-thread (id)
6117   (let ((dep gnus-newsgroup-dependencies)
6118         (buffer-read-only nil)
6119         parent headers refs thread art)
6120     (while (and id (setq headers
6121                          (car (setq art (gnus-gethash (downcase id) dep)))))
6122       (setq parent art)
6123       (setq id (and (setq refs (header-references headers))
6124                     (string-match "\\(<[^>]+>\\) *$" refs)
6125                     (substring refs (match-beginning 1) (match-end 1)))))
6126     (setq thread (gnus-make-sub-thread (car parent)))
6127     (gnus-rebuild-remove-articles thread)
6128     (let ((beg (point)))
6129       (gnus-summary-prepare-threads (list thread))
6130       (gnus-summary-update-lines beg (point)))))
6131
6132 ;; Delete all lines in the summary buffer that correspond to articles
6133 ;; in this thread.
6134 (defun gnus-rebuild-remove-articles (thread)
6135   (and (gnus-summary-goto-subject (header-number (car thread)))
6136        (gnus-delete-line))
6137   (mapcar (lambda (th) (gnus-rebuild-remove-articles th)) (cdr thread)))
6138
6139 (defun gnus-sort-threads (threads)
6140   ;; Sort threads as specified in `gnus-thread-sort-functions'.
6141   (let ((fun gnus-thread-sort-functions))
6142     (while fun
6143       (setq threads (sort threads (car fun))
6144             fun (cdr fun))))
6145   threads)
6146
6147 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
6148 (defmacro gnus-thread-header (thread)
6149   ;; Return header of first article in THREAD.
6150   ;; Note that THREAD must never, evr be anything else than a variable -
6151   ;; using some other form will lead to serious barfage.
6152   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
6153   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
6154   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" ; 
6155         (vector thread) 2))
6156
6157 (defun gnus-thread-sort-by-number (h1 h2)
6158   "Sort threads by root article number."
6159   (< (header-number (gnus-thread-header h1))
6160      (header-number (gnus-thread-header h2))))
6161
6162 (defun gnus-thread-sort-by-author (h1 h2)
6163   "Sort threads by root author."
6164   (string-lessp
6165    (let ((extract (funcall 
6166                    gnus-extract-address-components
6167                    (header-from (gnus-thread-header h1)))))
6168      (or (car extract) (cdr extract)))
6169    (let ((extract (funcall
6170                    gnus-extract-address-components 
6171                    (header-from (gnus-thread-header h2)))))
6172      (or (car extract) (cdr extract)))))
6173
6174 (defun gnus-thread-sort-by-subject (h1 h2)
6175   "Sort threads by root subject."
6176   (string-lessp
6177    (downcase (gnus-simplify-subject 
6178               (header-subject (gnus-thread-header h1))))
6179    (downcase (gnus-simplify-subject 
6180               (header-subject (gnus-thread-header h2))))))
6181
6182 (defun gnus-thread-sort-by-date (h1 h2)
6183   "Sort threads by root article date."
6184   (string-lessp
6185    (gnus-sortable-date (header-date (gnus-thread-header h1)))
6186    (gnus-sortable-date (header-date (gnus-thread-header h2)))))
6187
6188 (defun gnus-thread-sort-by-score (h1 h2)
6189   "Sort threads by root article score.
6190 Unscored articles will be counted as having a score of zero."
6191   (> (or (cdr (assq (header-number (gnus-thread-header h1))
6192                     gnus-newsgroup-scored))
6193          gnus-summary-default-score 0)
6194      (or (cdr (assq (header-number (gnus-thread-header h2))
6195                     gnus-newsgroup-scored))
6196          gnus-summary-default-score 0)))
6197
6198 (defun gnus-thread-sort-by-total-score (h1 h2)
6199   "Sort threads by the sum of all scores in the thread.
6200 Unscored articles will be counted as having a score of zero."
6201   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
6202
6203 (defun gnus-thread-total-score (thread)
6204   ;;  This function find the total score of THREAD.
6205   (if (consp thread)
6206       (if (stringp (car thread))
6207           (apply gnus-thread-score-function 0
6208                  (mapcar 'gnus-thread-total-score-1 (cdr thread)))
6209         (gnus-thread-total-score-1 thread))
6210     (gnus-thread-total-score-1 (list thread))))
6211
6212 (defun gnus-thread-total-score-1 (root)
6213   ;; This function find the total score of the thread below ROOT.
6214   (setq root (car root))
6215   (apply gnus-thread-score-function
6216          (or (cdr (assq (header-number root) gnus-newsgroup-scored))
6217              gnus-summary-default-score 0)
6218          (mapcar 'gnus-thread-total-score
6219                  (cdr (gnus-gethash (downcase (header-id root))
6220                                     gnus-newsgroup-dependencies)))))
6221
6222 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
6223 (defvar gnus-tmp-prev-subject "")
6224
6225 (defun gnus-summary-prepare-threads (threads &optional cull)
6226   "Prepare summary buffer from THREADS and indentation LEVEL.  
6227 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'  
6228 or a straight list of headers."
6229   (let ((level 0)
6230         thread header number subject stack state gnus-tmp-gathered)
6231     (if (vectorp (car threads))
6232         ;; If this is a straight (sic) list of headers, then a
6233         ;; threaded summary display isn't required, so we just create
6234         ;; an unthreaded one.
6235         (gnus-summary-prepare-unthreaded threads cull)
6236
6237       ;; Do the threaded display.
6238
6239       (while (or threads stack)
6240         
6241         (if threads
6242             ;; If there are some threads, we do them before the
6243             ;; threads on the stack.
6244             (setq thread threads
6245                   header (car (car thread)))
6246           ;; There were no current threads, so we pop something off
6247           ;; the stack. 
6248           (setq state (car stack)
6249                 level (car state)
6250                 thread (cdr state)
6251                 stack (cdr stack)
6252                 header (car (car thread))))
6253
6254         (if (stringp header)
6255             (progn
6256               ;; The header is a dummy root.
6257               (cond 
6258                ((eq gnus-summary-make-false-root 'adopt)
6259                 ;; We let the first article adopt the rest.
6260                 (let ((th (car (cdr (car thread)))))
6261                   (while (cdr th)
6262                     (setq th (cdr th)))
6263                   (setcdr th (cdr (cdr (car thread))))
6264                   (setq gnus-tmp-gathered 
6265                         (nconc (mapcar
6266                                 (lambda (h) (header-number (car h)))
6267                                 (cdr (cdr (car thread))))
6268                                gnus-tmp-gathered))
6269                   (setcdr (cdr (car thread)) nil))
6270                 (setq level -1))
6271                ((eq gnus-summary-make-false-root 'empty)
6272                 ;; We print adopted articles with empty subject fields.
6273                 (setq gnus-tmp-gathered 
6274                       (nconc (mapcar
6275                               (lambda (h) (header-number (car h)))
6276                               (cdr (cdr (car thread))))
6277                              gnus-tmp-gathered))
6278                 (setq level -1))
6279                ((eq gnus-summary-make-false-root 'dummy)
6280                 ;; We output a dummy root.
6281                 (gnus-summary-insert-dummy-line 
6282                  nil header (header-number (car (car (cdr (car thread)))))))
6283                (t
6284                 ;; We do not make a root for the gathered
6285                 ;; sub-threads at all.  
6286                 (setq level -1))))
6287       
6288           (setq number (header-number header)
6289                 subject (header-subject header))
6290
6291           ;; Do the async thing.
6292           (and gnus-newsgroup-async
6293                (setq gnus-newsgroup-threads
6294                      (cons (cons number (header-lines header)) 
6295                            gnus-newsgroup-threads)))
6296
6297           ;; We may have to root out some bad articles...
6298           (and cull
6299                (= level 0)
6300                (cond ((and (memq (setq number (header-number header))
6301                                  gnus-newsgroup-dormant)
6302                            (null threads))
6303                       (setq header nil))
6304                      ((and gnus-summary-expunge-below
6305                            (< (or (cdr (assq number gnus-newsgroup-scored))
6306                                   gnus-summary-default-score 0)
6307                               gnus-summary-expunge-below))
6308                       (setq header nil)
6309                       (setq gnus-newsgroup-unreads 
6310                             (delq number gnus-newsgroup-unreads)))))
6311
6312           (and
6313            header
6314            (progn
6315              (inline
6316                (gnus-summary-insert-line
6317                 nil header level nil 
6318                 (cond 
6319                  ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
6320                  ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
6321                  ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
6322                  ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
6323                  (t gnus-ancient-mark))
6324                 (memq number gnus-newsgroup-replied)
6325                 (memq number gnus-newsgroup-expirable)
6326                 (if (and (eq gnus-summary-make-false-root 'empty)
6327                          (memq number gnus-tmp-gathered))
6328                     gnus-summary-same-subject
6329                   (if (or (zerop level)
6330                           (and gnus-thread-ignore-subject
6331                                (not (string= 
6332                                      (gnus-simplify-subject-re
6333                                       gnus-tmp-prev-subject)
6334                                      (gnus-simplify-subject-re
6335                                       subject)))))
6336                       subject
6337                     gnus-summary-same-subject))
6338                 (and (eq gnus-summary-make-false-root 'adopt)
6339                      (memq number gnus-tmp-gathered))
6340                 (cdr (assq number gnus-newsgroup-scored)))
6341
6342                (setq gnus-tmp-prev-subject subject)))))
6343
6344         (if (nth 1 thread) 
6345             (setq stack (cons (cons (max 0 level) (nthcdr 1 thread)) stack)))
6346         (setq level (1+ level))
6347         (setq threads (cdr (car thread)))))))
6348
6349
6350 (defun gnus-summary-prepare-unthreaded (headers &optional cull)
6351   (let (header number)
6352
6353     ;; Do the async thing, if that is required.
6354     (if gnus-newsgroup-async
6355         (setq gnus-newsgroup-threads
6356               (mapcar (lambda (h) (cons (header-number h) (header-lines h)))
6357                       headers)))
6358
6359     (while headers
6360       (setq header (car headers)
6361             headers (cdr headers)
6362             number (header-number header))
6363
6364       ;; We may have to root out some bad articles...
6365       (cond 
6366        ((and cull
6367              (memq (setq number (header-number header))
6368                    gnus-newsgroup-dormant)))
6369        ((and cull gnus-summary-expunge-below
6370              (< (or (cdr (assq number gnus-newsgroup-scored))
6371                     gnus-summary-default-score 0)
6372                 gnus-summary-expunge-below))
6373         (setq gnus-newsgroup-unreads 
6374               (delq number gnus-newsgroup-unreads)))
6375        (t
6376         (gnus-summary-insert-line
6377          nil header 0 nil 
6378          (cond ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
6379                ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
6380                ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
6381                ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
6382                (t gnus-ancient-mark))
6383          (memq number gnus-newsgroup-replied)
6384          (memq number gnus-newsgroup-expirable)
6385          (header-subject header) nil
6386          (cdr (assq number gnus-newsgroup-scored))))))))
6387
6388 (defun gnus-select-newsgroup (group &optional read-all)
6389   "Select newsgroup GROUP.
6390 If READ-ALL is non-nil, all articles in the group are selected."
6391   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
6392          (info (nth 2 entry))
6393          articles)
6394     (gnus-check-news-server
6395      (setq gnus-current-select-method (gnus-find-method-for-group group)))
6396
6397     (or (gnus-server-opened gnus-current-select-method)
6398         (gnus-open-server gnus-current-select-method)
6399         (error "Couldn't open server"))
6400     
6401     (or (and (eq (car entry) t)
6402              (gnus-activate-newsgroup (car info)))
6403         (gnus-request-group group t)
6404         (progn
6405           (kill-buffer (current-buffer))
6406           (error "Couldn't request group %s: %s" 
6407                  group (gnus-status-message group))))
6408
6409     (setq gnus-newsgroup-name group)
6410     (setq gnus-newsgroup-unselected nil)
6411     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
6412
6413     (and gnus-asynchronous
6414          (gnus-check-backend-function 
6415           'request-asynchronous gnus-newsgroup-name)
6416          (setq gnus-newsgroup-async
6417                (gnus-request-asynchronous gnus-newsgroup-name)))
6418
6419     (setq articles (gnus-articles-to-read group read-all))
6420
6421     (cond 
6422      ((null articles) 
6423       (gnus-message 3 "Couldn't select newsgroup")
6424       'quit)
6425      ((eq articles 0) nil)
6426      (t
6427       ;; Init the dependencies hash table.
6428       (setq gnus-newsgroup-dependencies 
6429             (gnus-make-hashtable (length articles)))
6430       ;; Retrieve the headers and read them in.
6431       (gnus-message 5 "Fetching headers...")
6432       (setq gnus-newsgroup-headers 
6433             (if (eq 'nov (setq gnus-headers-retrieved-by
6434                                (gnus-retrieve-headers 
6435                                 (if (and gnus-fetch-old-headers 
6436                                          (not (eq 1 (car articles))))
6437                                     (cons 1 articles)
6438                                   articles)
6439                                 gnus-newsgroup-name)))
6440                 (progn
6441                   (gnus-get-newsgroup-headers-xover articles))
6442               ;; If we were to fetch old headers, but the backend didn't
6443               ;; support XOVER, then it is possible we fetched one article
6444               ;; that we shouldn't have. If that's the case, we pop it off the
6445               ;; list of headers.
6446               (if (not gnus-fetch-old-headers)
6447                   ()
6448                 (save-excursion
6449                   (set-buffer nntp-server-buffer)
6450                   (goto-char (point-min))
6451                   (and (looking-at "[0-9]+[ \t]+1[ \t]")
6452                        (delete-region 
6453                         (point) 
6454                         (search-forward "\n.\n" nil t)))))
6455               (gnus-get-newsgroup-headers)))
6456       (gnus-message 5 "Fetching headers...done")      
6457       ;; Remove canceled articles from the list of unread articles.
6458       (setq gnus-newsgroup-unreads
6459             (gnus-set-sorted-intersection 
6460              gnus-newsgroup-unreads
6461              (mapcar (lambda (headers) (header-number headers))
6462                      gnus-newsgroup-headers)))
6463       ;; Adjust and set lists of article marks.
6464       (and info
6465            (let (marked)
6466              (gnus-adjust-marked-articles info)
6467              (setq gnus-newsgroup-marked 
6468                    (copy-sequence
6469                     (cdr (assq 'tick (setq marked (nth 3 info))))))
6470              (setq gnus-newsgroup-replied 
6471                    (copy-sequence (cdr (assq 'reply marked))))
6472              (setq gnus-newsgroup-expirable
6473                    (copy-sequence (cdr (assq 'expire marked))))
6474              (setq gnus-newsgroup-killed
6475                    (copy-sequence (cdr (assq 'killed marked))))
6476              (setq gnus-newsgroup-bookmarks 
6477                    (copy-sequence (cdr (assq 'bookmark marked))))
6478              (setq gnus-newsgroup-dormant 
6479                    (copy-sequence (cdr (assq 'dormant marked))))
6480              (setq gnus-newsgroup-scored 
6481                    (copy-sequence (cdr (assq 'score marked))))
6482              (setq gnus-newsgroup-processable nil)))
6483       ;; Create the header hashtb.
6484       (or gnus-newsgroup-headers-hashtb-by-number
6485           (gnus-make-headers-hashtable-by-number))
6486       ;; Check whether auto-expire is to be done in this group.
6487       (setq gnus-newsgroup-auto-expire
6488             (or (and (stringp gnus-auto-expirable-newsgroups)
6489                      (string-match gnus-auto-expirable-newsgroups group))
6490                 (memq 'auto-expire (nth 5 info))))
6491       ;; First and last article in this newsgroup.
6492       (and gnus-newsgroup-headers
6493            (setq gnus-newsgroup-begin 
6494                  (header-number (car gnus-newsgroup-headers)))
6495            (setq gnus-newsgroup-end
6496                  (header-number (gnus-last-element gnus-newsgroup-headers))))
6497       (setq gnus-reffed-article-number -1)
6498       ;; GROUP is successfully selected.
6499       (or gnus-newsgroup-headers t)))))
6500
6501 (defun gnus-articles-to-read (group read-all)
6502   ;; Find out what articles the user wants to read.
6503   (let* ((articles
6504           ;; Select all articles if `read-all' is non-nil, or if all the
6505           ;; unread articles are dormant articles.
6506           (if (or (and read-all (not (numberp read-all)))
6507                   (= (length gnus-newsgroup-unreads) 
6508                      (length gnus-newsgroup-dormant)))
6509               (gnus-uncompress-range 
6510                (gnus-gethash group gnus-active-hashtb))
6511             gnus-newsgroup-unreads))
6512          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
6513          (scored (length scored-list))
6514          (number (length articles))
6515          (marked (+ (length gnus-newsgroup-marked)
6516                     (length gnus-newsgroup-dormant)))
6517          (select
6518           (cond 
6519            ((numberp read-all)
6520             read-all)
6521            (t
6522             (condition-case ()
6523                 (cond ((and (or (<= scored marked)
6524                                 (= scored number))
6525                             (numberp gnus-large-newsgroup)
6526                             (> number gnus-large-newsgroup))
6527                        (let ((input
6528                               (read-string
6529                                (format
6530                                 "How many articles from %s (default %d): "
6531                                 gnus-newsgroup-name number))))
6532                          (if (string-match "^[ \t]*$" input)
6533                              number input)))
6534                       ((and (> scored marked) (< scored number))
6535                        (let ((input
6536                               (read-string
6537                                (format 
6538                                 "%s %s (%d scored, %d total): "
6539                                 "How many articles from"
6540                                 group scored number))))
6541                          (if (string-match "^[ \t]*$" input)
6542                              number input)))
6543                       (t number))
6544               (quit nil))))))
6545     (setq select (if (stringp select) (string-to-number select) select))
6546     (if (or (null select) (zerop select))
6547         select
6548       (if (and (not (zerop scored)) (<= (abs select) scored))
6549           (progn
6550             (setq articles (sort scored-list '<))
6551             (setq number (length articles)))
6552         (setq articles (copy-sequence articles)))
6553
6554       (if (< (abs select) number)
6555           (if (< select 0) 
6556               ;; Select the N oldest articles.
6557               (setcdr (nthcdr (1- (abs select)) articles) nil)
6558             ;; Select the N most recent articles.
6559             (setq articles (nthcdr (- number select) articles))))
6560       (setq gnus-newsgroup-unselected
6561             (gnus-sorted-intersection
6562              gnus-newsgroup-unreads
6563              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
6564       articles)))
6565
6566 (defun gnus-killed-articles (killed articles)
6567   (let (out)
6568     (while articles
6569       (if (inline (gnus-member-of-range (car articles) killed))
6570           (setq out (cons (car articles) out)))
6571       (setq articles (cdr articles)))
6572     out))
6573
6574 (defun gnus-adjust-marked-articles (info &optional active)
6575   "Remove all marked articles that are no longer legal."
6576   (let ((marked-lists (nth 3 info))
6577         (active (or active (gnus-gethash (car info) gnus-active-hashtb)))
6578         m prev)
6579     ;; There are many types of marked articles.
6580     (while marked-lists
6581       (setq m (cdr (setq prev (car marked-lists))))
6582       (cond ((or (eq 'tick (car prev)) (eq 'dormant (car prev)))
6583              ;; Make sure that all ticked articles are a subset of the
6584              ;; unread/unselected articles.
6585              (while m
6586                (if (or (memq (car m) gnus-newsgroup-unreads)
6587                        (memq (car m) gnus-newsgroup-unselected))
6588                    (setq prev m)
6589                  (setcdr prev (cdr m)))
6590                (setq m (cdr m))))
6591             ((eq 'score (car prev))
6592              ;; Scored articles should be a subset of
6593              ;; unread/unselected articles. 
6594              (while m
6595                (if (or (memq (car (car m)) gnus-newsgroup-unreads)
6596                        (memq (car (car m)) gnus-newsgroup-unreads))
6597                    (setq prev m)
6598                  (setcdr prev (cdr m)))
6599                (setq m (cdr m))))
6600             ((eq 'bookmark (car prev))
6601              ;; Bookmarks should be a subset of active articles.
6602              (while m
6603                (if (< (car (car m)) (car active))
6604                    (setcdr prev (cdr m))
6605                  (setq prev m))
6606                (setq m (cdr m))))
6607             ((eq 'killed (car prev))
6608              ;; Articles that have been through the kill process are
6609              ;; to be a subset of active articles.
6610              (while (and m (< (or (and (numberp (car m)) (car m))
6611                                   (cdr (car m)))
6612                               (car active)))
6613                (setcdr prev (cdr m))
6614                (setq m (cdr m)))
6615              (if (and m (< (or (and (numberp (car m)) (car m))
6616                                (car (car m)))
6617                            (car active))) 
6618                  (setcar (if (numberp (car m)) m (car m)) (car active))))
6619             ((or (eq 'reply (car prev)) (eq 'expire (car prev)))
6620              ;; The replied and expirable articles have to be articles
6621              ;; that are active. 
6622              (while m
6623                (if (< (car m) (car active))
6624                    (setcdr prev (cdr m))
6625                  (setq prev m))
6626                (setq m (cdr m)))))
6627       (setq marked-lists (cdr marked-lists)))
6628     ;; Remove all lists that are empty.
6629     (setq marked-lists (nth 3 info))
6630     (if marked-lists
6631         (progn
6632           (while (= 1 (length (car marked-lists)))
6633             (setq marked-lists (cdr marked-lists)))
6634           (setq m (cdr (setq prev marked-lists)))
6635           (while m
6636             (if (= 1 (length (car m)))
6637                 (setcdr prev (cdr m))
6638               (setq prev m))
6639             (setq m (cdr m)))
6640           (setcar (nthcdr 3 info) marked-lists)))
6641     ;; Finally, if there are no marked lists at all left, and if there
6642     ;; are no elements after the lists in the info list, we just chop
6643     ;; the info list off before the marked lists.
6644     (and (null marked-lists) 
6645          (not (nthcdr 4 info))
6646          (setcdr (nthcdr 2 info) nil)))
6647   info)
6648
6649 (defun gnus-set-marked-articles 
6650   (info ticked replied expirable killed dormant bookmark score) 
6651   "Enter the various lists of marked articles into the newsgroup info list."
6652   (let (newmarked)
6653     (and ticked (setq newmarked (cons (cons 'tick ticked) nil)))
6654     (and replied (setq newmarked (cons (cons 'reply replied) newmarked)))
6655     (and expirable (setq newmarked (cons (cons 'expire expirable) 
6656                                          newmarked)))
6657     (and killed (setq newmarked (cons (cons 'killed killed) newmarked)))
6658     (and dormant (setq newmarked (cons (cons 'dormant dormant) newmarked)))
6659     (and bookmark (setq newmarked (cons (cons 'bookmark bookmark) 
6660                                         newmarked)))
6661     (and score (setq newmarked (cons (cons 'score score) newmarked)))
6662     (if (nthcdr 3 info)
6663         (progn
6664           (setcar (nthcdr 3 info) newmarked)
6665           (and (not newmarked)
6666                (not (nthcdr 4 info))
6667                (setcdr (nthcdr 2 info) nil)))
6668       (if newmarked
6669           (setcdr (nthcdr 2 info) (list newmarked))))))
6670
6671 (defun gnus-add-marked-articles (group type articles &optional info force)
6672   ;; Add ARTICLES of TYPE to the info of GROUP.
6673   ;; If INFO is non-nil, use that info. If FORCE is non-nil, don't
6674   ;; add, but replace marked articles of TYPE with ARTICLES.
6675   (let ((info (or info (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
6676         marked m)
6677     (or (not info)
6678         (and (not (setq marked (nthcdr 3 info)))
6679              (setcdr (nthcdr 2 info) (list (list (cons type articles)))))
6680         (and (not (setq m (assq type (car marked))))
6681              (setcar marked (cons (cons type articles) (car marked))))
6682         (if force
6683             (setcdr m articles)
6684           (nconc m articles)))))
6685          
6686 (defun gnus-set-mode-line (where)
6687   "This function sets the mode line of the article or summary buffers.
6688 If WHERE is `summary', the summary mode line format will be used."
6689   (if (memq where gnus-updated-mode-lines)
6690       (let (mode-string)
6691         (save-excursion
6692           (set-buffer gnus-summary-buffer)
6693           (let* ((mformat (if (eq where 'article) 
6694                               gnus-article-mode-line-format-spec
6695                             gnus-summary-mode-line-format-spec))
6696                  (group-name gnus-newsgroup-name)
6697                  (article-number (or gnus-current-article 0))
6698                  (unread (- (length gnus-newsgroup-unreads)
6699                             (length gnus-newsgroup-dormant)))
6700                  (unread-and-unticked 
6701                   (- unread (length gnus-newsgroup-marked)))
6702                  (unselected (length gnus-newsgroup-unselected))
6703                  (unread-and-unselected
6704                   (cond ((and (zerop unread-and-unticked)
6705                               (zerop unselected)) "")
6706                         ((zerop unselected) 
6707                          (format "{%d more}" unread-and-unticked))
6708                         (t (format "{%d(+%d) more}"
6709                                    unread-and-unticked unselected))))
6710                  (subject
6711                   (if gnus-current-headers
6712                       (header-subject gnus-current-headers) ""))
6713                  (max-len (and gnus-mode-non-string-length
6714                                (- (frame-width) gnus-mode-non-string-length)))
6715                  header) ;; passed as argument to any user-format-funcs
6716             (setq mode-string (eval mformat))
6717             (or (numberp max-len)
6718                 (setq max-len (length mode-string)))
6719             (if (< max-len 4) (setq max-len 4))
6720             (if (> (length mode-string) max-len)
6721                 ;; modified by MORIOKA Tomohiko <morioka@jaist.ac.jp>
6722                 ;;  function `substring' might cut on a middle
6723                 ;;  of multi-octet character.
6724                 (setq mode-string 
6725                       (concat (gnus-truncate-string mode-string (- max-len 3))
6726                               "...")))
6727             (setq mode-string (format (format "%%-%ds" max-len)
6728                                       mode-string))))
6729         (setq mode-line-buffer-identification mode-string)
6730         (set-buffer-modified-p t))))
6731
6732 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
6733   "Go through the HEADERS list and add all Xrefs to a hash table.
6734 The resulting hash table is returned, or nil if no Xrefs were found."
6735   (let* ((from-method (gnus-find-method-for-group from-newsgroup))
6736          (prefix (if (and 
6737                       (gnus-group-foreign-p from-newsgroup)
6738                       (not (memq 'virtual 
6739                                  (assoc (symbol-name (car from-method))
6740                                         gnus-valid-select-methods))))
6741                      (gnus-group-real-prefix from-newsgroup)))
6742          (xref-hashtb (make-vector 63 0))
6743          start group entry number xrefs header)
6744     (while headers
6745       (setq header (car headers))
6746       (if (and (setq xrefs (header-xref header))
6747                (not (memq (header-number header) unreads)))
6748           (progn
6749             (setq start 0)
6750             (while (string-match "\\([^ ]+\\):\\([0-9]+\\)" xrefs start)
6751               (setq start (match-end 0))
6752               (setq group (concat prefix (substring xrefs (match-beginning 1) 
6753                                                     (match-end 1))))
6754               (setq number 
6755                     (string-to-int (substring xrefs (match-beginning 2) 
6756                                               (match-end 2))))
6757               (if (setq entry (gnus-gethash group xref-hashtb))
6758                   (setcdr entry (cons number (cdr entry)))
6759                 (gnus-sethash group (cons number nil) xref-hashtb)))))
6760       (setq headers (cdr headers)))
6761     (if start xref-hashtb nil)))
6762
6763 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads expirable)
6764   "Look through all the headers and mark the Xrefs as read."
6765   (let ((virtual (memq 'virtual 
6766                        (assoc (symbol-name (car (gnus-find-method-for-group 
6767                                                  from-newsgroup)))
6768                               gnus-valid-select-methods)))
6769         name entry info xref-hashtb idlist method
6770         nth4)
6771     (save-excursion
6772       (set-buffer gnus-group-buffer)
6773       (if (setq xref-hashtb 
6774                 (gnus-create-xref-hashtb from-newsgroup headers unreads))
6775           (mapatoms 
6776            (lambda (group)
6777              (if (string= from-newsgroup (setq name (symbol-name group)))
6778                  ()
6779                (setq idlist (symbol-value group))
6780                ;; Dead groups are not updated.
6781                (if (and (prog1 
6782                             (setq entry (gnus-gethash name gnus-newsrc-hashtb)
6783                                   info (nth 2 entry))
6784                           (if (stringp (setq nth4 (nth 4 info)))
6785                               (setq nth4 (gnus-server-to-method nth4))))
6786                         ;; Only do the xrefs if the group has the same
6787                         ;; select method as the group we have just read.
6788                         (or (gnus-methods-equal-p 
6789                              nth4 (gnus-find-method-for-group from-newsgroup))
6790                             virtual
6791                             (equal nth4 
6792                                    (setq method (gnus-find-method-for-group 
6793                                                  from-newsgroup)))
6794                             (and (equal (car nth4) (car method))
6795                                  (equal (nth 1 nth4) (nth 1 method))))
6796                         gnus-use-cross-reference
6797                         (or (not (eq gnus-use-cross-reference t))
6798                             virtual
6799                             ;; Only do cross-references on subscribed
6800                             ;; groups, if that is what is wanted.  
6801                             (<= (nth 1 info) gnus-level-subscribed)))
6802                    (gnus-group-make-articles-read name idlist expirable))))
6803            xref-hashtb)))))
6804
6805 (defun gnus-group-make-articles-read (group articles expirable)
6806   (let* ((num 0)
6807          (entry (gnus-gethash group gnus-newsrc-hashtb))
6808          (info (nth 2 entry))
6809          (active (gnus-gethash group gnus-active-hashtb))
6810          exps expirable range)
6811     ;; First peel off all illegal article numbers.
6812     (if active
6813         (let ((ids articles)
6814               (ticked (cdr (assq 'tick (nth 3 info))))
6815               (dormant (cdr (assq 'dormant (nth 3 info))))
6816               id)
6817           (setq exps nil)
6818           (while ids
6819             (setq id (car ids))
6820             (if (or (> id (cdr active))
6821                     (< id (car active))
6822                     (memq id ticked)
6823                     (memq id dormant))
6824                 (setq articles (delq id articles)))
6825             (and (memq id expirable)
6826                  (setq exps (cons id exps)))
6827             (setq ids (cdr ids)))))
6828     ;; Update expirable articles.
6829     (gnus-add-marked-articles nil 'expirable exps info)
6830     (and active
6831          (null (nth 2 info))
6832          (> (car active) 1)
6833          (setcar (nthcdr 2 info) (cons 1 (1- (car active)))))
6834     (setcar (nthcdr 2 info)
6835             (setq range
6836                   (gnus-add-to-range 
6837                    (nth 2 info) 
6838                    (setq articles (sort articles '<)))))
6839     ;; Then we have to re-compute how many unread
6840     ;; articles there are in this group.
6841     (if active
6842         (progn
6843           (cond 
6844            ((not range)
6845             (setq num (- (1+ (cdr active)) (car active))))
6846            ((not (listp (cdr range)))
6847             (setq num (- (cdr active) (- (1+ (cdr range)) 
6848                                          (car range)))))
6849            (t
6850             (while range
6851               (if (numberp (car range))
6852                   (setq num (1+ num))
6853                 (setq num (+ num (- (1+ (cdr (car range)))
6854                                     (car (car range))))))
6855               (setq range (cdr range)))
6856             (setq num (- (cdr active) num))))
6857           ;; Update the number of unread articles.
6858           (setcar 
6859            entry 
6860            (max 0 (- num 
6861                      (length (cdr (assq 'tick (nth 3 info))))
6862                      (length 
6863                       (cdr (assq 'dormant (nth 3 info)))))))
6864           ;; Update the group buffer.
6865           (gnus-group-update-group group t)))))
6866
6867 (defun gnus-methods-equal-p (m1 m2)
6868   (let ((m1 (or m1 gnus-select-method))
6869         (m2 (or m2 gnus-select-method)))
6870     (or (equal m1 m2)
6871         (and (eq (car m1) (car m2))
6872              (or (not (memq 'address (assoc (symbol-name (car m1))
6873                                             gnus-valid-select-methods)))
6874                  (equal (nth 1 m1) (nth 1 m2)))))))
6875
6876 (defsubst gnus-header-value ()
6877   (buffer-substring (match-end 0) (gnus-point-at-eol)))
6878
6879 (defvar gnus-newsgroup-none-id 0)
6880
6881 (defun gnus-get-newsgroup-headers ()
6882   (setq gnus-article-internal-prepare-hook nil)
6883   (let ((cur nntp-server-buffer)
6884         (dependencies gnus-newsgroup-dependencies)
6885         headers id dep end ref)
6886     (save-excursion
6887       (set-buffer nntp-server-buffer)
6888       (goto-char (point-min))
6889       ;; Search to the beginning of the next header. Error messages
6890       ;; do not begin with 2 or 3.
6891       (while (re-search-forward "^[23][0-9]+ " nil t)
6892         (let ((header (make-vector 9 nil))
6893               (case-fold-search t)
6894               (p (point))
6895               in-reply-to)
6896           (setq id nil
6897                 ref nil)
6898           (header-set-number header (read cur))
6899           ;; This implementation of this function, with nine
6900           ;; search-forwards instead of the one re-search-forward and
6901           ;; a case (which basically was the old function) is actually
6902           ;; about twice as fast, even though it looks messier. You
6903           ;; can't have everything, I guess. Speed and elegance
6904           ;; doesn't always come hand in hand.
6905           (save-restriction
6906             (narrow-to-region (point) (or (save-excursion 
6907                                             (search-forward "\n.\n" nil t))
6908                                           (point)))
6909             (if (search-forward "\nfrom: " nil t)
6910                 (header-set-from header (gnus-header-value))
6911               (header-set-from header "(nobody)"))
6912             (goto-char p)
6913             (if (search-forward "\nsubject: " nil t)
6914                 (header-set-subject header (gnus-header-value))
6915               (header-set-subject header "(none)"))
6916             (goto-char p)
6917             (and (search-forward "\nxref: " nil t)
6918                  (header-set-xref header (gnus-header-value)))
6919             (goto-char p)
6920             (or (numberp (and (search-forward "\nlines: " nil t)
6921                               (header-set-lines header (read cur))))
6922                 (header-set-lines header 0))
6923             (goto-char p)
6924             (and (search-forward "\ndate: " nil t)
6925                  (header-set-date header (gnus-header-value)))
6926             (goto-char p)
6927             (if (search-forward "\nmessage-id: " nil t)
6928                 (header-set-id header (setq id (gnus-header-value)))
6929               ;; If there was no message-id, we just fake one to make
6930               ;; subsequent routines simpler.
6931               (header-set-id 
6932                header 
6933                (setq id (concat "none+" 
6934                                 (int-to-string 
6935                                  (setq gnus-newsgroup-none-id 
6936                                        (1+ gnus-newsgroup-none-id)))))))
6937             (goto-char p)
6938             (if (search-forward "\nreferences: " nil t)
6939                 (progn
6940                   (header-set-references header (gnus-header-value))
6941                   (setq end (match-end 0))
6942                   (save-excursion
6943                     (setq ref 
6944                           (downcase
6945                            (buffer-substring
6946                             (progn 
6947                               (end-of-line)
6948                               (search-backward ">" end t)
6949                               (1+ (point)))
6950                             (progn
6951                               (search-backward "<" end t)
6952                               (point)))))))
6953               ;; Get the references from the in-reply-to header if there
6954               ;; ware no references and the in-reply-to header looks
6955               ;; promising. 
6956               (if (and (search-forward "\nin-reply-to: " nil t)
6957                        (setq in-reply-to (gnus-header-value))
6958                        (string-match "<[^>]+>" in-reply-to))
6959                   (progn
6960                     (header-set-references 
6961                      header 
6962                      (setq ref (substring in-reply-to (match-beginning 0)
6963                                           (match-end 0))))
6964                     (setq ref (downcase ref)))
6965                 (setq ref "none")))
6966             ;; We do some threading while we read the headers. The
6967             ;; message-id and the last reference are both entered into
6968             ;; the same hash table. Some tippy-toeing around has to be
6969             ;; done in case an article has arrived before the article
6970             ;; which it refers to.
6971             (if (boundp (setq dep (intern (downcase id) dependencies)))
6972                 (if (car (symbol-value dep))
6973                     ;; An article with this Message-ID has already
6974                     ;; been seen, so we ignore this one, except we add
6975                     ;; any additional Xrefs (in case the two articles
6976                     ;; came from different servers.
6977                     (progn
6978                       (header-set-xref 
6979                        (car (symbol-value dep))
6980                        (concat (or (header-xref (car (symbol-value dep))) "")
6981                                (or (header-xref header) "")))
6982                       (setq header nil))
6983                   (setcar (symbol-value dep) header))
6984               (set dep (list header)))
6985             (if header
6986                 (progn
6987                   (if (boundp (setq dep (intern ref dependencies)))
6988                       (setcdr (symbol-value dep) 
6989                               (cons header (cdr (symbol-value dep))))
6990                     (set dep (list nil header)))
6991                   (setq headers (cons header headers))))
6992             (goto-char (point-max))))))
6993     (nreverse headers)))
6994
6995 ;; The following macros and functions were written by Felix Lee
6996 ;; <flee@cse.psu.edu>. 
6997
6998 (defmacro gnus-nov-read-integer ()
6999   '(prog1
7000        (if (= (following-char) ?\t)
7001            0
7002          (let ((num (condition-case nil (read buffer) (error nil))))
7003            (if (numberp num) num 0)))
7004      (or (eobp) (forward-char 1))))
7005
7006 (defmacro gnus-nov-skip-field ()
7007   '(search-forward "\t" eol 'move))
7008
7009 (defmacro gnus-nov-field ()
7010   '(buffer-substring (point) (if (gnus-nov-skip-field) (1- (point)) eol)))
7011
7012 ;; Goes through the xover lines and returns a list of vectors
7013 (defun gnus-get-newsgroup-headers-xover (sequence)
7014   "Parse the news overview data in the server buffer, and return a
7015 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
7016   ;; Get the Xref when the users reads the articles since most/some
7017   ;; NNTP servers do not include Xrefs when using XOVER.
7018   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
7019   (let ((cur nntp-server-buffer)
7020         (dependencies gnus-newsgroup-dependencies)
7021         number headers header)
7022     (save-excursion
7023       (set-buffer nntp-server-buffer)
7024       (goto-char (point-min))
7025       (while (and sequence (not (eobp)))
7026         (setq number (read cur))
7027         (while (and sequence (< (car sequence) number))
7028           (setq sequence (cdr sequence)))
7029         (and sequence 
7030              (eq number (car sequence))
7031              (progn
7032                (setq sequence (cdr sequence))
7033                (if (setq header 
7034                          (inline (gnus-nov-parse-line number dependencies)))
7035                    (setq headers (cons header headers)))))
7036         (forward-line 1))
7037       (setq headers (nreverse headers)))
7038     headers))
7039
7040 ;; This function has to be called with point after the article number
7041 ;; on the beginning of the line.
7042 (defun gnus-nov-parse-line (number dependencies)
7043   (let ((none 0)
7044         (eol (gnus-point-at-eol)) 
7045         (buffer (current-buffer))
7046         header ref id dep)
7047
7048     ;; overview: [num subject from date id refs chars lines misc]
7049     (narrow-to-region (point) eol)
7050     (forward-char)
7051
7052     (condition-case nil
7053         (setq header
7054               (vector 
7055                number                   ; number
7056                (gnus-nov-field)         ; subject
7057                (gnus-nov-field)         ; from
7058                (gnus-nov-field)         ; date
7059                (setq id (or (gnus-nov-field)
7060                             (concat "none+"
7061                                     (int-to-string 
7062                                      (setq none (1+ none)))))) ; id
7063                (progn
7064                  (save-excursion
7065                    (let ((beg (point)))
7066                      (search-forward "\t" eol)
7067                      (if (search-backward ">" beg t)
7068                          (setq ref 
7069                                (downcase 
7070                                 (buffer-substring 
7071                                  (1+ (point))
7072                                  (progn
7073                                    (search-backward "<" beg t)
7074                                    (point)))))
7075                        (setq ref nil))))
7076                  (gnus-nov-field))      ; refs
7077                (gnus-nov-read-integer)  ; chars
7078                (gnus-nov-read-integer)  ; lines
7079                (if (= (following-char) ?\n)
7080                    nil
7081                  (gnus-nov-field))      ; misc
7082                ))
7083       (error (progn 
7084                (ding)
7085                (message "Strange nov line.")
7086                (setq header nil)
7087                (goto-char eol))))
7088
7089     (widen)
7090
7091     ;; We build the thread tree.
7092     (and header
7093          (if (boundp (setq dep (intern (downcase id) dependencies)))
7094              (if (car (symbol-value dep))
7095                  ;; An article with this Message-ID has already been seen,
7096                  ;; so we ignore this one, except we add any additional
7097                  ;; Xrefs (in case the two articles came from different
7098                  ;; servers.
7099                  (progn
7100                    (header-set-xref 
7101                     (car (symbol-value dep))
7102                     (concat (or (header-xref (car (symbol-value dep))) "")
7103                             (or (header-xref header) "")))
7104                    (setq header nil))
7105                (setcar (symbol-value dep) header))
7106            (set dep (list header))))
7107     (if header
7108         (progn
7109           (if (boundp (setq dep (intern (or ref "none") 
7110                                         dependencies)))
7111               (setcdr (symbol-value dep) 
7112                       (cons header (cdr (symbol-value dep))))
7113             (set dep (list nil header)))))
7114     header))
7115
7116 (defun gnus-article-get-xrefs ()
7117   "Fill in the Xref value in `gnus-current-headers', if necessary.
7118 This is meant to be called in `gnus-article-internal-prepare-hook'."
7119   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
7120                                  gnus-current-headers)))
7121     (or (not gnus-use-cross-reference)
7122         (not headers)
7123         (and (header-xref headers)
7124              (not (string= (header-xref headers) "")))
7125         (let ((case-fold-search t)
7126               xref)
7127           (save-restriction
7128             (gnus-narrow-to-headers)
7129             (goto-char (point-min))
7130             (if (or (and (eq (downcase (following-char)) ?x)
7131                          (looking-at "Xref:"))
7132                     (search-forward "\nXref:" nil t))
7133                 (progn
7134                   (goto-char (1+ (match-end 0)))
7135                   (setq xref (buffer-substring (point) 
7136                                                (progn (end-of-line) (point))))
7137                   (header-set-xref headers xref))))))))
7138
7139 (defalias 'gnus-find-header-by-number 'gnus-get-header-by-number)
7140 (make-obsolete 'gnus-find-header-by-number 'gnus-get-header-by-number)
7141
7142 (defun gnus-make-headers-hashtable-by-number ()
7143   "Make hashtable for the variable gnus-newsgroup-headers by number."
7144   (save-excursion
7145     (set-buffer gnus-summary-buffer)
7146     (let ((headers gnus-newsgroup-headers)
7147           header)
7148       (setq gnus-newsgroup-headers-hashtb-by-number
7149             (gnus-make-hashtable (length headers)))
7150       (while headers
7151         (setq header (car headers))
7152         (gnus-sethash (int-to-string (header-number header))
7153                       header gnus-newsgroup-headers-hashtb-by-number)
7154         (setq headers (cdr headers))))))
7155
7156 (defun gnus-more-header-backward ()
7157   "Find new header backward."
7158   (let ((first (car (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
7159         (artnum gnus-newsgroup-begin)
7160         (header nil))
7161     (while (and (not header)
7162                 (> artnum first))
7163       (setq artnum (1- artnum))
7164       (setq header (gnus-read-header artnum)))
7165     header))
7166
7167 (defun gnus-more-header-forward (&optional backward)
7168   "Find new header forward.
7169 If BACKWARD, find new header backward instead."
7170   (if backward
7171       (gnus-more-header-backward)
7172     (let ((last (cdr (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
7173           (artnum gnus-newsgroup-end)
7174           (header nil))
7175       (while (and (not header)
7176                   (< artnum last))
7177         (setq artnum (1+ artnum))
7178         (setq header (gnus-read-header artnum)))
7179       header)))
7180
7181 (defun gnus-extend-newsgroup (header &optional backward)
7182   "Extend newsgroup selection with HEADER.
7183 Optional argument BACKWARD means extend toward backward."
7184   (if header
7185       (let ((artnum (header-number header)))
7186         (setq gnus-newsgroup-headers
7187               (if backward
7188                   (cons header gnus-newsgroup-headers)
7189                 (nconc gnus-newsgroup-headers (list header))))
7190         (setq gnus-newsgroup-unselected
7191               (delq artnum gnus-newsgroup-unselected))
7192         (setq gnus-newsgroup-begin (min gnus-newsgroup-begin artnum))
7193         (setq gnus-newsgroup-end (max gnus-newsgroup-end artnum)))))
7194
7195 (defun gnus-summary-work-articles (n)
7196   "Return a list of articles to be worked upon. The prefix argument,
7197 the list of process marked articles, and the current article will be
7198 taken into consideration."
7199   (let (articles)
7200     (if (and n (numberp n))
7201         (let ((backward (< n 0))
7202               (n (abs n)))
7203           (save-excursion
7204             (while (and (> n 0)
7205                         (setq articles (cons (gnus-summary-article-number) 
7206                                              articles))
7207                         (gnus-summary-search-forward nil nil backward))
7208               (setq n (1- n))))
7209           (sort articles (function <)))
7210       (or (reverse gnus-newsgroup-processable)
7211           (list (gnus-summary-article-number))))))
7212
7213 (defun gnus-summary-search-group (&optional backward use-level)
7214   "Search for next unread newsgroup.
7215 If optional argument BACKWARD is non-nil, search backward instead."
7216   (save-excursion
7217     (set-buffer gnus-group-buffer)
7218     (if (gnus-group-search-forward 
7219          backward nil (if use-level (gnus-group-group-level) nil))
7220         (gnus-group-group-name))))
7221
7222 (defun gnus-summary-best-group (&optional exclude-group)
7223   "Find the name of the best unread group.
7224 If EXCLUDE-GROUP, do not go to this group."
7225   (save-excursion
7226     (set-buffer gnus-group-buffer)
7227     (save-excursion
7228       (gnus-group-best-unread-group exclude-group))))
7229
7230 (defun gnus-subject-equal (s1 s2)
7231   (cond
7232    ((null gnus-summary-gather-subject-limit)
7233     (equal (gnus-simplify-subject-re s1)
7234            (gnus-simplify-subject-re s2)))
7235    ((eq gnus-summary-gather-subject-limit 'fuzzy)
7236     (equal (gnus-simplify-subject-fuzzy s1)
7237            (gnus-simplify-subject-fuzzy s2)))
7238    ((numberp gnus-summary-gather-subject-limit)
7239     (equal (gnus-limit-string s1 gnus-summary-gather-subject-limit)
7240            (gnus-limit-string s2 gnus-summary-gather-subject-limit)))
7241    (t
7242     (equal s1 s2))))
7243     
7244 (defun gnus-summary-search-subject (&optional backward unread subject)
7245   "Search for article forward.
7246 If BACKWARD is non-nil, search backward.
7247 If UNREAD is non-nil, only unread articles are selected.
7248 If SUBJECT is non-nil, the article which has the same subject will be
7249 searched for." 
7250   (let ((func (if backward 'previous-single-property-change
7251                 'next-single-property-change))
7252         (beg (point))
7253         (did t)
7254         pos psubject)
7255     (beginning-of-line)
7256     (and gnus-summary-check-current unread
7257          (eq (get-text-property (point) 'gnus-mark) gnus-unread-mark)
7258          (setq did nil))
7259     (if (not did)
7260         ()
7261       (forward-char (if backward (if (bobp) 0 -1) (if (eobp) 0 1)))
7262       (while
7263           (and 
7264            (setq pos (funcall func (point) 'gnus-number))
7265            (goto-char (if backward (1- pos) pos))
7266            (setq did
7267                  (not (and
7268                        (or (not unread)
7269                            (eq (get-text-property (point) 'gnus-mark)
7270                                gnus-unread-mark))
7271                        (or (not subject)
7272                            (and (setq psubject (inline (gnus-summary-subject-string)))
7273                                 (inline (gnus-subject-equal subject psubject)))))))
7274            (if backward (if (bobp) nil (forward-char -1) t)
7275              (if (eobp) nil (forward-char 1) t)))))
7276     (if did
7277         (progn (goto-char beg) nil)
7278       (prog1
7279           (get-text-property (point) 'gnus-number)
7280         (gnus-summary-position-cursor)))))
7281
7282 (defun gnus-summary-pseudo-article ()
7283   "The thread level of the article on the current line."
7284   (get-text-property (gnus-point-at-bol) 'gnus-pseudo))
7285
7286 (defalias 'gnus-summary-score 'gnus-summary-article-score)
7287 (make-obsolete 'gnus-summary-score 'gnus-summary-article-score)
7288 (defun gnus-summary-article-score ()
7289   "Return current article score."
7290   (or (cdr (assq (gnus-summary-article-number) gnus-newsgroup-scored))
7291       gnus-summary-default-score 0))
7292
7293 (defun gnus-summary-recenter ()
7294   "Center point in the summary window.
7295 If `gnus-auto-center-summary' is nil, or the article buffer isn't
7296 displayed, no centering will be performed." 
7297   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
7298   ;; Recenter only when requested. Suggested by popovich@park.cs.columbia.edu.
7299   (let* ((top (cond ((< (window-height) 4) 0)
7300                     ((< (window-height) 7) 1)
7301                     (t 2)))
7302          (height (1- (window-height)))
7303          (bottom (save-excursion (goto-char (point-max))
7304                                  (forward-line (- height))
7305                                  (point)))
7306          (window (get-buffer-window (current-buffer))))
7307     (and 
7308      ;; The user has to want it,
7309      gnus-auto-center-summary 
7310      ;; the article buffer must be displayed,
7311      (get-buffer-window gnus-article-buffer)
7312      ;; Set the window start to either `bottom', which is the biggest
7313      ;; possible valid number, or the second line from the top,
7314      ;; whichever is the least.
7315      (set-window-start
7316       window (min bottom (save-excursion (forward-line (- top)) (point)))))))
7317
7318 ;; Function written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
7319 (defun gnus-short-group-name (group &optional levels)
7320   "Collapse GROUP name LEVELS."
7321   (let* ((name "") (foreign "") (depth -1) (skip 1)
7322          (levels (or levels
7323                      (progn
7324                        (while (string-match "\\." group skip)
7325                          (setq skip (match-end 0)
7326                                depth (+ depth 1)))
7327                        depth))))
7328     (if (string-match ":" group)
7329         (setq foreign (substring group 0 (match-end 0))
7330               group (substring group (match-end 0))))
7331     (while group
7332       (if (and (string-match "\\." group) (> levels 0))
7333           (setq name (concat name (substring group 0 1))
7334                 group (substring group (match-end 0))
7335                 levels (- levels 1)
7336                 name (concat name "."))
7337         (setq name (concat foreign name group)
7338               group nil)))
7339     name))
7340
7341 (defun gnus-summary-jump-to-group (newsgroup)
7342   "Move point to NEWSGROUP in group mode buffer."
7343   ;; Keep update point of group mode buffer if visible.
7344   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
7345       (save-window-excursion
7346         ;; Take care of tree window mode.
7347         (if (get-buffer-window gnus-group-buffer)
7348             (pop-to-buffer gnus-group-buffer))
7349         (gnus-group-jump-to-group newsgroup))
7350     (save-excursion
7351       ;; Take care of tree window mode.
7352       (if (get-buffer-window gnus-group-buffer)
7353           (pop-to-buffer gnus-group-buffer)
7354         (set-buffer gnus-group-buffer))
7355       (gnus-group-jump-to-group newsgroup))))
7356
7357 ;; This function returns a list of article numbers based on the
7358 ;; difference between the ranges of read articles in this group and
7359 ;; the range of active articles.
7360 (defun gnus-list-of-unread-articles (group)
7361   (let* ((read (nth 2 (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
7362          (active (gnus-gethash group gnus-active-hashtb))
7363          (last (cdr active))
7364          first nlast unread)
7365     ;; If none are read, then all are unread. 
7366     (if (not read)
7367         (setq first (car active))
7368       ;; If the range of read articles is a single range, then the
7369       ;; first unread article is the article after the last read
7370       ;; article. Sounds logical, doesn't it?
7371       (if (not (listp (cdr read)))
7372           (setq first (1+ (cdr read)))
7373         ;; `read' is a list of ranges.
7374         (if (/= (setq nlast (or (and (numberp (car read)) (car read)) 
7375                                 (car (car read)))) 1)
7376             (setq first 1))
7377         (while read
7378           (if first 
7379               (while (< first nlast)
7380                 (setq unread (cons first unread))
7381                 (setq first (1+ first))))
7382           (setq first (1+ (if (atom (car read)) (car read) (cdr (car read)))))
7383           (setq nlast (if (atom (car (cdr read))) 
7384                           (car (cdr read))
7385                         (car (car (cdr read)))))
7386           (setq read (cdr read)))))
7387     ;; And add the last unread articles.
7388     (while (<= first last)
7389       (setq unread (cons first unread))
7390       (setq first (1+ first)))
7391     ;; Return the list of unread articles.
7392     (nreverse unread)))
7393
7394 (defun gnus-list-of-read-articles (group)
7395   (let ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
7396         (active (gnus-gethash group gnus-active-hashtb)))
7397     (and info active
7398          (gnus-sorted-complement 
7399           (gnus-uncompress-range active) 
7400           (gnus-list-of-unread-articles group)))))
7401
7402 ;; Various summary commands
7403
7404 (defun gnus-summary-universal-argument ()
7405   "Perform any operation on all articles marked with the process mark."
7406   (interactive)
7407   (gnus-set-global-variables)
7408   (let ((articles (reverse gnus-newsgroup-processable))
7409         func)
7410     (or articles (error "No articles marked"))
7411     (or (setq func (key-binding (read-key-sequence "C-c C-u")))
7412         (error "Undefined key"))
7413     (while articles
7414       (gnus-summary-goto-subject (car articles))
7415       (command-execute func)
7416       (gnus-summary-remove-process-mark (car articles))
7417       (setq articles (cdr articles)))))
7418
7419 (defun gnus-summary-toggle-truncation (&optional arg)
7420   "Toggle truncation of summary lines.
7421 With arg, turn line truncation on iff arg is positive."
7422   (interactive "P")
7423   (setq truncate-lines
7424         (if (null arg) (not truncate-lines)
7425           (> (prefix-numeric-value arg) 0)))
7426   (redraw-display))
7427
7428 (defun gnus-summary-reselect-current-group (&optional all)
7429   "Once exit and then reselect the current newsgroup.
7430 The prefix argument ALL means to select all articles."
7431   (interactive "P")
7432   (gnus-set-global-variables)
7433   (let ((current-subject (gnus-summary-article-number))
7434         (group gnus-newsgroup-name))
7435     (setq gnus-newsgroup-begin nil)
7436     (gnus-summary-exit t)
7437     ;; We have to adjust the point of group mode buffer because the
7438     ;; current point was moved to the next unread newsgroup by
7439     ;; exiting.
7440     (gnus-summary-jump-to-group group)
7441     (gnus-group-read-group all t)
7442     (gnus-summary-goto-subject current-subject)))
7443
7444 (defun gnus-summary-rescan-group (&optional all)
7445   "Exit the newsgroup, ask for new articles, and select the newsgroup."
7446   (interactive "P")
7447   (gnus-set-global-variables)
7448   ;; Fix by Ilja Weis <kult@uni-paderborn.de>.
7449   (let ((group gnus-newsgroup-name))
7450     (gnus-summary-exit)
7451     (gnus-summary-jump-to-group group)
7452     (save-excursion
7453       (set-buffer gnus-group-buffer)
7454       (gnus-group-get-new-news-this-group 1))
7455     (gnus-summary-jump-to-group group)
7456     (gnus-group-read-group all)))
7457
7458 (defun gnus-summary-update-info ()
7459   (let* ((group gnus-newsgroup-name))
7460     (if gnus-newsgroup-kill-headers
7461         (setq gnus-newsgroup-killed
7462               (gnus-compress-sequence
7463                (nconc
7464                 (gnus-set-sorted-intersection
7465                  (gnus-uncompress-range gnus-newsgroup-killed)
7466                  (setq gnus-newsgroup-unselected
7467                        (sort gnus-newsgroup-unselected '<)))
7468                 (setq gnus-newsgroup-unreads
7469                       (sort gnus-newsgroup-unreads '<))) t)))
7470     (or (listp (cdr gnus-newsgroup-killed))
7471         (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
7472     (let ((headers gnus-newsgroup-headers))
7473       (gnus-close-group group)
7474       (run-hooks 'gnus-exit-group-hook)
7475       (gnus-update-read-articles 
7476        group gnus-newsgroup-unreads gnus-newsgroup-unselected 
7477        gnus-newsgroup-marked
7478        t gnus-newsgroup-replied gnus-newsgroup-expirable
7479        gnus-newsgroup-killed gnus-newsgroup-dormant
7480        gnus-newsgroup-bookmarks 
7481        (and gnus-save-score gnus-newsgroup-scored))
7482       (and gnus-use-cross-reference
7483            (gnus-mark-xrefs-as-read 
7484             group headers gnus-newsgroup-unreads gnus-newsgroup-expirable))
7485       ;; Do adaptive scoring, and possibly save score files.
7486       (and gnus-newsgroup-adaptive
7487            (gnus-score-adaptive))
7488       (and gnus-use-scoring 
7489            (fboundp 'gnus-score-save)
7490            (funcall 'gnus-score-save))
7491       ;; Do not switch windows but change the buffer to work.
7492       (set-buffer gnus-group-buffer)
7493       (or (assoc 'quit-config (gnus-find-method-for-group gnus-newsgroup-name))
7494           (gnus-group-update-group group)))))
7495   
7496 (defun gnus-summary-exit (&optional temporary)
7497   "Exit reading current newsgroup, and then return to group selection mode.
7498 gnus-exit-group-hook is called with no arguments if that value is non-nil."
7499   (interactive)
7500   (gnus-set-global-variables)
7501   (gnus-kill-save-kill-buffer)
7502   (let* ((group gnus-newsgroup-name)
7503          (quit-config (nth 1 (assoc 'quit-config (gnus-find-method-for-group
7504                                                   gnus-newsgroup-name))))
7505          (mode major-mode)
7506          (buf (current-buffer)))
7507     (run-hooks 'gnus-summary-prepare-exit-hook)
7508     (gnus-summary-update-info) ; Make all changes in this group permanent.
7509     (set-buffer buf)
7510     (and gnus-use-cache (gnus-cache-possibly-remove-articles))
7511     ;; Make sure where I was, and go to next newsgroup.
7512     (set-buffer gnus-group-buffer)
7513     (or quit-config
7514         (progn
7515           (gnus-group-jump-to-group group)
7516           (gnus-group-next-unread-group 1)))
7517     (if temporary
7518         nil                             ;Nothing to do.
7519       ;; We set all buffer-local variables to nil. It is unclear why
7520       ;; this is needed, but if we don't, buffer-local variables are
7521       ;; not garbage-collected, it seems. This would the lead to en
7522       ;; ever-growing Emacs.
7523       (set-buffer buf)
7524       (gnus-summary-clear-local-variables)
7525       ;; We clear the global counterparts of the buffer-local
7526       ;; variables as well, just to be on the safe side.
7527       (gnus-configure-windows 'group 'force)
7528       (gnus-summary-clear-local-variables)
7529       ;; Return to group mode buffer. 
7530       (if (eq mode 'gnus-summary-mode)
7531           (gnus-kill-buffer buf))
7532       (if (get-buffer gnus-article-buffer)
7533           (bury-buffer gnus-article-buffer))
7534       (setq gnus-current-select-method gnus-select-method)
7535       (pop-to-buffer gnus-group-buffer)
7536       (if (not quit-config)
7537           (progn
7538             (gnus-group-jump-to-group group)
7539             (gnus-group-next-unread-group 1))
7540         (if (not (buffer-name (car quit-config)))
7541             (gnus-configure-windows 'group 'force)
7542           (set-buffer (car quit-config))
7543           (and (eq major-mode 'gnus-summary-mode)
7544                (gnus-set-global-variables))
7545           (gnus-configure-windows (cdr quit-config))))
7546       (run-hooks 'gnus-summary-exit-hook))))
7547
7548 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
7549 (defun gnus-summary-exit-no-update (&optional no-questions)
7550   "Quit reading current newsgroup without updating read article info."
7551   (interactive)
7552   (gnus-set-global-variables)
7553   (let* ((group gnus-newsgroup-name)
7554          (quit-config (nth 1 (assoc 'quit-config 
7555                                     (gnus-find-method-for-group group)))))
7556     (if (or no-questions
7557             gnus-expert-user
7558             (gnus-y-or-n-p "Do you really wanna quit reading this group? "))
7559         (progn
7560           (gnus-close-group group)
7561           (gnus-summary-clear-local-variables)
7562           (set-buffer gnus-group-buffer)
7563           (gnus-summary-clear-local-variables)
7564           ;; Return to group selection mode.
7565           (gnus-configure-windows 'group 'force)
7566           (if (get-buffer gnus-summary-buffer)
7567               (kill-buffer gnus-summary-buffer))
7568           (if (get-buffer gnus-article-buffer)
7569               (bury-buffer gnus-article-buffer))
7570           (if (equal (gnus-group-group-name) group)
7571               (gnus-group-next-unread-group 1))
7572           (if quit-config
7573               (progn
7574                 (if (not (buffer-name (car quit-config)))
7575                     (gnus-configure-windows 'group 'force)
7576                   (set-buffer (car quit-config))
7577                   (and (eq major-mode 'gnus-summary-mode)
7578                        (gnus-set-global-variables))
7579                   (gnus-configure-windows (cdr quit-config)))))))))
7580
7581 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
7582 (defun gnus-summary-fetch-faq (group)
7583   "Fetch the FAQ for the current group."
7584   (interactive (list gnus-newsgroup-name))
7585   (let ((gnus-faq-buffer 
7586          (find-file (concat gnus-group-faq-directory 
7587                             (gnus-group-real-name group)))))
7588   (and gnus-faq-buffer (gnus-configure-windows 'summary-faq))))
7589
7590 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
7591 (defun gnus-summary-describe-group (&optional force)
7592   "Describe the current newsgroup."
7593   (interactive "P")
7594   (gnus-group-describe-group force gnus-newsgroup-name))
7595
7596 (defun gnus-summary-describe-briefly ()
7597   "Describe summary mode commands briefly."
7598   (interactive)
7599   (gnus-message 6
7600     (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")))
7601
7602 ;; Walking around group mode buffer from summary mode.
7603
7604 (defun gnus-summary-next-group (&optional no-article target-group backward)
7605   "Exit current newsgroup and then select next unread newsgroup.
7606 If prefix argument NO-ARTICLE is non-nil, no article is selected
7607 initially. If NEXT-GROUP, go to this group. If BACKWARD, go to
7608 previous group instead."
7609   (interactive "P")
7610   (gnus-set-global-variables)
7611   (let ((current-group gnus-newsgroup-name)
7612         (current-buffer (current-buffer))
7613         entered)
7614     ;; First we semi-exit this group to update Xrefs and all variables.
7615     ;; We can't do a real exit, because the window conf must remain
7616     ;; the same in case the user is prompted for info, and we don't
7617     ;; want the window conf to change before that...
7618     (gnus-summary-exit t)
7619     (while (not entered)
7620       ;; Then we find what group we are supposed to enter.
7621       (set-buffer gnus-group-buffer)
7622       (gnus-group-jump-to-group current-group)
7623       (setq target-group 
7624             (or target-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       (if (not target-group)
7629           ;; There are no further groups, so we return to the group
7630           ;; buffer.
7631           (progn
7632             (gnus-message 5 "Returning to the group buffer")
7633             (setq entered t)
7634             (set-buffer current-buffer)
7635             (gnus-summary-exit))
7636         ;; We try to enter the target group.
7637         (gnus-group-jump-to-group target-group)
7638         (let ((unreads (gnus-group-group-unread)))
7639           (if (and (or (eq t unreads)
7640                        (and unreads (not (zerop unreads))))
7641                    (gnus-summary-read-group
7642                     target-group nil no-article current-buffer))
7643               (setq entered t)
7644             (setq current-group target-group
7645                   target-group nil)))))))
7646
7647 (defun gnus-summary-next-group-old (&optional no-article group backward)
7648   "Exit current newsgroup and then select next unread newsgroup.
7649 If prefix argument NO-ARTICLE is non-nil, no article is selected initially.
7650 If BACKWARD, go to previous group instead."
7651   (interactive "P")
7652   (gnus-set-global-variables)
7653   (let ((ingroup gnus-newsgroup-name)
7654         (sumbuf (current-buffer))
7655         num)
7656     (set-buffer gnus-group-buffer)
7657     (if (and group
7658              (or (and (numberp (setq num (car (gnus-gethash
7659                                                group gnus-newsrc-hashtb))))
7660                       (< num 1))
7661                  (null num)))
7662         (progn
7663           (gnus-group-jump-to-group group)
7664           (setq group nil))
7665       (gnus-group-jump-to-group ingroup))
7666     (gnus-summary-search-group backward)
7667     (let ((group (or group (gnus-summary-search-group backward))))
7668       (set-buffer sumbuf)
7669       (gnus-summary-exit t)             ;Update all information.
7670       (if (null group)
7671           (gnus-summary-exit-no-update t)
7672         (gnus-group-jump-to-group ingroup)
7673         (setq group (gnus-summary-search-group backward))
7674         (gnus-message 5 "Selecting %s..." group)
7675         (set-buffer gnus-group-buffer)
7676         ;; We are now in group mode buffer.
7677         ;; Make sure group mode buffer point is on GROUP.
7678         (gnus-group-jump-to-group group)
7679         (if (not (eq gnus-auto-select-next 'quietly))
7680             (progn
7681               (gnus-summary-read-group group nil no-article sumbuf)
7682               (and (string= gnus-newsgroup-name ingroup)
7683                    (bufferp sumbuf) (buffer-name sumbuf)
7684                    (progn
7685                      (set-buffer (setq gnus-summary-buffer sumbuf))
7686                      (gnus-summary-exit-no-update t))))
7687           (let ((prevgroup group))
7688             (gnus-group-jump-to-group ingroup)
7689             (setq group (gnus-summary-search-group backward))
7690             (gnus-summary-read-group group nil no-article sumbuf)
7691             (while (and (string= gnus-newsgroup-name ingroup)
7692                         (bufferp sumbuf) 
7693                         (buffer-name sumbuf)
7694                         (not (string= prevgroup (gnus-group-group-name))))
7695               (set-buffer gnus-group-buffer)
7696               (gnus-summary-read-group 
7697                (setq prevgroup (gnus-group-group-name)) 
7698                nil no-article sumbuf))
7699             (and (string= prevgroup (gnus-group-group-name))
7700                  ;; We have reached the final group in the group
7701                  ;; buffer.
7702                  (progn
7703                    (if (buffer-name sumbuf)
7704                        (progn
7705                          (set-buffer sumbuf)
7706                          (gnus-summary-exit)))))))))))
7707
7708 (defun gnus-summary-prev-group (&optional no-article)
7709   "Exit current newsgroup and then select previous unread newsgroup.
7710 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7711   (interactive "P")
7712   (gnus-summary-next-group no-article nil t))
7713
7714 ;; Walking around summary lines.
7715
7716 (defun gnus-summary-first-subject (&optional unread)
7717   "Go to the first unread subject.
7718 If UNREAD is non-nil, go to the first unread article.
7719 Returns nil if there are no unread articles."
7720   (interactive "P")
7721   (prog1
7722       (cond ((not unread)
7723              (goto-char (point-min)))
7724             ((gnus-goto-char 
7725               (text-property-any 
7726                (point-min) (point-max) 'gnus-mark gnus-unread-mark))
7727              t)
7728             (t 
7729              ;; There are no unread articles.
7730              (gnus-message 3 "No more unread articles")
7731              nil))
7732     (gnus-summary-position-cursor)))
7733
7734 (defun gnus-summary-next-subject (n &optional unread dont-display)
7735   "Go to next N'th summary line.
7736 If N is negative, go to the previous N'th subject line.
7737 If UNREAD is non-nil, only unread articles are selected.
7738 The difference between N and the actual number of steps taken is
7739 returned."
7740   (interactive "p")
7741   (let ((backward (< n 0))
7742         (n (abs n)))
7743     (while (and (> n 0)
7744                 (gnus-summary-search-forward unread nil backward))
7745       (setq n (1- n)))
7746     (if (/= 0 n) (gnus-message 7 "No more%s articles"
7747                                (if unread " unread" "")))
7748     (or dont-display
7749         (progn
7750           (gnus-summary-recenter)
7751           (gnus-summary-position-cursor)))
7752   n))
7753
7754 (defun gnus-summary-next-unread-subject (n)
7755   "Go to next N'th unread summary line."
7756   (interactive "p")
7757   (gnus-summary-next-subject n t))
7758
7759 (defun gnus-summary-prev-subject (n &optional unread)
7760   "Go to previous N'th summary line.
7761 If optional argument UNREAD is non-nil, only unread article is selected."
7762   (interactive "p")
7763   (gnus-summary-next-subject (- n) unread))
7764
7765 (defun gnus-summary-prev-unread-subject (n)
7766   "Go to previous N'th unread summary line."
7767   (interactive "p")
7768   (gnus-summary-next-subject (- n) t))
7769
7770 (defun gnus-summary-goto-subject (article)
7771   "Go the subject line of ARTICLE."
7772   (interactive
7773    (list
7774     (string-to-int
7775      (completing-read "Article number: "
7776                       (mapcar
7777                        (lambda (headers)
7778                          (list
7779                           (int-to-string (header-number headers))))
7780                        gnus-newsgroup-headers)
7781                       nil 'require-match))))
7782   (or article (error "No article number"))
7783   (let ((b (point)))
7784     (if (not (gnus-goto-char (text-property-any (point-min) (point-max)
7785                                                 'gnus-number article)))
7786         ()
7787       (gnus-summary-show-thread)
7788       ;; Skip dummy articles. 
7789       (if (eq (gnus-summary-article-mark) gnus-dummy-mark)
7790           (forward-line 1))
7791       (prog1
7792           (if (not (eobp))
7793               article
7794             (goto-char b)
7795             nil)
7796         (gnus-summary-position-cursor)))))
7797
7798 ;; Walking around summary lines with displaying articles.
7799
7800 (defun gnus-summary-expand-window (&optional arg)
7801   "Make the summary buffer take up the entire Emacs frame.
7802 Given a prefix, will force an `article' buffer configuration."
7803   (interactive "P")
7804   (gnus-set-global-variables)
7805   (if arg
7806       (gnus-configure-windows 'article 'force)
7807     (gnus-configure-windows 'summary 'force)))
7808
7809 (defun gnus-summary-display-article (article &optional all-header)
7810   "Display ARTICLE in article buffer."
7811   (gnus-set-global-variables)
7812   (if (null article)
7813       nil
7814     (prog1
7815         (gnus-article-prepare article all-header)
7816       (gnus-summary-show-thread)
7817       (if (eq (gnus-summary-article-mark) gnus-dummy-mark)
7818           (progn
7819             (forward-line 1)
7820             (gnus-summary-position-cursor)))
7821       (run-hooks 'gnus-select-article-hook)
7822       (gnus-summary-recenter)
7823       (gnus-summary-goto-subject article)
7824       ;; Successfully display article.
7825       (gnus-summary-update-line)
7826       (gnus-article-set-window-start 
7827        (cdr (assq article gnus-newsgroup-bookmarks)))
7828       t)))
7829
7830 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7831   "Select the current article.
7832 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7833 non-nil, the article will be re-fetched even if it already present in
7834 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7835 be displayed."
7836   (and (not pseudo) (gnus-summary-pseudo-article)
7837        (error "This is a pseudo-article."))
7838   (let ((article (or article (gnus-summary-article-number)))
7839         (all-headers (not (not all-headers))) ;Must be T or NIL.
7840         did) 
7841     (prog1
7842         (save-excursion
7843           (set-buffer gnus-summary-buffer)
7844           (if (or (null gnus-current-article)
7845                   (null gnus-article-current)
7846                   (null (get-buffer gnus-article-buffer))
7847                   (not (eq article (cdr gnus-article-current)))
7848                   (not (equal (car gnus-article-current) gnus-newsgroup-name))
7849                   force)
7850               ;; The requested article is different from the current article.
7851               (progn
7852                 (gnus-summary-display-article article all-headers)
7853                 (setq did article))
7854             (if (or all-headers gnus-show-all-headers) 
7855                 (gnus-article-show-all-headers))
7856             nil))
7857       (if did 
7858           (gnus-article-set-window-start 
7859            (cdr (assq article gnus-newsgroup-bookmarks)))))))
7860
7861 (defun gnus-summary-set-current-mark (&optional current-mark)
7862   "Obsolete function."
7863   nil)
7864
7865 (defun gnus-summary-next-article (&optional unread subject backward)
7866   "Select the next article.
7867 If UNREAD, only unread articles are selected.
7868 If SUBJECT, only articles with SUBJECT are selected.
7869 If BACKWARD, the previous article is selected instead of the next."
7870   (interactive "P")
7871   (gnus-set-global-variables)
7872   (let (header)
7873     (cond
7874      ;; Is there such an article?
7875      ((or (gnus-summary-display-article 
7876            (gnus-summary-search-forward unread subject backward))
7877           (eq (gnus-summary-article-mark) gnus-canceled-mark))
7878       (gnus-summary-position-cursor))
7879      ;; If not, we try the first unread, if that is wanted.
7880      ((and subject
7881            gnus-auto-select-same
7882            (gnus-summary-first-unread-article))
7883       (gnus-message 6 "Wrapped"))
7884      ;; Try to get next/previous article not displayed in this group.
7885      ((and gnus-auto-extend-newsgroup
7886            (not unread) (not subject)
7887            (setq header (gnus-more-header-forward backward)))
7888       (gnus-extend-newsgroup header backward)
7889       (let ((buffer-read-only nil))
7890         (goto-char (if backward (point-min) (point-max)))
7891         (gnus-summary-prepare-threads (list header)))
7892       (gnus-summary-goto-article (if backward gnus-newsgroup-begin
7893                                    gnus-newsgroup-end)))
7894      ;; Go to next/previous group.
7895      (t
7896       (or (assoc 'quit-config (gnus-find-method-for-group gnus-newsgroup-name))
7897           (gnus-summary-jump-to-group gnus-newsgroup-name))
7898       (let ((cmd (aref (this-command-keys) 0))
7899             (group 
7900              (if (eq gnus-keep-same-level 'best) 
7901                  (gnus-summary-best-group gnus-newsgroup-name)
7902                (gnus-summary-search-group backward gnus-keep-same-level))))
7903         ;; For some reason, the group window gets selected. We change
7904         ;; it back.  
7905         (select-window (get-buffer-window (current-buffer)))
7906         ;; Keep just the event type of CMD.
7907         (and (listp cmd) (setq cmd (car cmd)))
7908         ;; Select next unread newsgroup automagically.
7909         (cond 
7910          ((not gnus-auto-select-next)
7911           (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7912          ((eq gnus-auto-select-next 'quietly)
7913           ;; Select quietly.
7914           (if (assoc 'quit-config (gnus-find-method-for-group 
7915                                    gnus-newsgroup-name))
7916               (gnus-summary-exit)
7917             (gnus-message 7 "No more%s articles (%s)..."
7918                           (if unread " unread" "") 
7919                           (if group (concat "selecting " group)
7920                             "exiting"))
7921             (gnus-summary-next-group nil group backward)))
7922          (t
7923           (let ((keystrokes '(?\C-n ?\C-p))
7924                 key)
7925             (while (or (null key) (memq key keystrokes))
7926               (gnus-message 
7927                7 "No more%s articles%s" (if unread " unread" "")
7928                (if (and group (not (assoc 'quit-config
7929                                           (gnus-find-method-for-group 
7930                                            gnus-newsgroup-name))))
7931                    (format " (Type %s for %s [%s])"
7932                            (single-key-description cmd) group
7933                            (car (gnus-gethash group gnus-newsrc-hashtb)))
7934                  (format " (Type %s to exit %s)"
7935                          (single-key-description cmd)
7936                          gnus-newsgroup-name)))
7937               ;; Confirm auto selection.
7938               (let* ((event (read-event)))
7939                 (setq key (if (listp event) (car event) event))
7940                 (if (memq key keystrokes)
7941                     (let ((obuf (current-buffer)))
7942                       (switch-to-buffer gnus-group-buffer)
7943                       (gnus-group-jump-to-group group)
7944                       (execute-kbd-macro (char-to-string key))
7945                       (setq group (gnus-group-group-name))
7946                       (switch-to-buffer obuf)))))
7947             (if (equal key cmd)
7948                 (if (or (not group) (assoc 'quit-config
7949                                            (gnus-find-method-for-group
7950                                             gnus-newsgroup-name)))
7951                     (gnus-summary-exit)
7952                   (gnus-summary-next-group nil group backward))
7953               (setq unread-command-events (list key)))))))))))
7954
7955 (defun gnus-summary-next-unread-article ()
7956   "Select unread article after current one."
7957   (interactive)
7958   (gnus-summary-next-article t (and gnus-auto-select-same
7959                                     (gnus-summary-subject-string))))
7960
7961 (defun gnus-summary-prev-article (&optional unread subject)
7962   "Select the article after the current one.
7963 If UNREAD is non-nil, only unread articles are selected."
7964   (interactive "P")
7965   (gnus-summary-next-article unread subject t))
7966
7967 (defun gnus-summary-prev-unread-article ()
7968   "Select unred article before current one."
7969   (interactive)
7970   (gnus-summary-prev-article t (and gnus-auto-select-same
7971                                     (gnus-summary-subject-string))))
7972
7973 (defun gnus-summary-next-page (&optional lines circular)
7974   "Show next page of selected article.
7975 If end of article, select next article.
7976 Argument LINES specifies lines to be scrolled up.
7977 If CIRCULAR is non-nil, go to the start of the article instead of 
7978 instead of selecting the next article when reaching the end of the
7979 current article." 
7980   (interactive "P")
7981   (setq gnus-summary-buffer (current-buffer))
7982   (gnus-set-global-variables)
7983   (let ((article (gnus-summary-article-number))
7984         (endp nil))
7985     (gnus-configure-windows 'article)
7986     (if (or (null gnus-current-article)
7987             (null gnus-article-current)
7988             (/= article (cdr gnus-article-current))
7989             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7990         ;; Selected subject is different from current article's.
7991         (gnus-summary-display-article article)
7992       (gnus-eval-in-buffer-window
7993        gnus-article-buffer
7994        (setq endp (gnus-article-next-page lines)))
7995       (if endp
7996           (cond (circular
7997                  (gnus-summary-beginning-of-article))
7998                 (lines
7999                  (gnus-message 3 "End of message"))
8000                 ((null lines)
8001                  (gnus-summary-next-unread-article)))))
8002     (gnus-summary-recenter)
8003     (gnus-summary-position-cursor)))
8004
8005 (defun gnus-summary-prev-page (&optional lines)
8006   "Show previous page of selected article.
8007 Argument LINES specifies lines to be scrolled down."
8008   (interactive "P")
8009   (gnus-set-global-variables)
8010   (let ((article (gnus-summary-article-number)))
8011     (gnus-configure-windows 'article)
8012     (if (or (null gnus-current-article)
8013             (null gnus-article-current)
8014             (/= article (cdr gnus-article-current))
8015             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
8016         ;; Selected subject is different from current article's.
8017         (gnus-summary-display-article article)
8018       (gnus-summary-recenter)
8019       (gnus-eval-in-buffer-window gnus-article-buffer
8020         (gnus-article-prev-page lines))))
8021   (gnus-summary-position-cursor))
8022
8023 (defun gnus-summary-scroll-up (lines)
8024   "Scroll up (or down) one line current article.
8025 Argument LINES specifies lines to be scrolled up (or down if negative)."
8026   (interactive "p")
8027   (gnus-set-global-variables)
8028   (gnus-configure-windows 'article)
8029   (or (gnus-summary-select-article nil nil 'pseudo)
8030       (gnus-eval-in-buffer-window 
8031        gnus-article-buffer
8032        (cond ((> lines 0)
8033               (if (gnus-article-next-page lines)
8034                   (gnus-message 3 "End of message")))
8035              ((< lines 0)
8036               (gnus-article-prev-page (- lines))))))
8037   (gnus-summary-recenter)
8038   (gnus-summary-position-cursor))
8039
8040 (defun gnus-summary-next-same-subject ()
8041   "Select next article which has the same subject as current one."
8042   (interactive)
8043   (gnus-set-global-variables)
8044   (gnus-summary-next-article nil (gnus-summary-subject-string)))
8045
8046 (defun gnus-summary-prev-same-subject ()
8047   "Select previous article which has the same subject as current one."
8048   (interactive)
8049   (gnus-set-global-variables)
8050   (gnus-summary-prev-article nil (gnus-summary-subject-string)))
8051
8052 (defun gnus-summary-next-unread-same-subject ()
8053   "Select next unread article which has the same subject as current one."
8054   (interactive)
8055   (gnus-set-global-variables)
8056   (gnus-summary-next-article t (gnus-summary-subject-string)))
8057
8058 (defun gnus-summary-prev-unread-same-subject ()
8059   "Select previous unread article which has the same subject as current one."
8060   (interactive)
8061   (gnus-set-global-variables)
8062   (gnus-summary-prev-article t (gnus-summary-subject-string)))
8063
8064 (defun gnus-summary-first-unread-article ()
8065   "Select the first unread article. 
8066 Return nil if there are no unread articles."
8067   (interactive)
8068   (gnus-set-global-variables)
8069   (prog1
8070       (if (gnus-summary-first-subject t)
8071           (gnus-summary-display-article (gnus-summary-article-number)))
8072     (gnus-summary-position-cursor)))
8073
8074 (defun gnus-summary-best-unread-article ()
8075   "Select the unread article with the highest score."
8076   (interactive)
8077   (gnus-set-global-variables)
8078   (let ((scored gnus-newsgroup-scored)
8079         (best -1000000)
8080         article art)
8081     (while scored
8082       (or (> best (cdr (car scored)))
8083           (and (memq (setq art (car (car scored))) gnus-newsgroup-unreads)
8084                (not (memq art gnus-newsgroup-marked))
8085                (not (memq art gnus-newsgroup-dormant))
8086                (if (= best (cdr (car scored)))
8087                    (setq article (min art article))
8088                  (setq article art)
8089                  (setq best (cdr (car scored))))))
8090       (setq scored (cdr scored)))
8091     (if article 
8092         (gnus-summary-goto-article article)
8093       (gnus-summary-first-unread-article))
8094     (gnus-summary-position-cursor)))
8095
8096 (defun gnus-summary-goto-article (article &optional all-headers)
8097   "Fetch ARTICLE and display it if it exists.
8098 If ALL-HEADERS is non-nil, no header lines are hidden."
8099   (interactive
8100    (list
8101     (string-to-int
8102      (completing-read 
8103       "Article number: "
8104       (mapcar (lambda (headers) (list (int-to-string (header-number headers))))
8105               gnus-newsgroup-headers) 
8106       nil 'require-match))))
8107   (prog1
8108       (and (gnus-summary-goto-subject article)
8109            (gnus-summary-display-article article all-headers))
8110     (gnus-summary-position-cursor)))
8111
8112 (defun gnus-summary-goto-last-article ()
8113   "Go to the previously read article."
8114   (interactive)
8115   (prog1
8116       (and gnus-last-article
8117            (gnus-summary-goto-article gnus-last-article))
8118     (gnus-summary-position-cursor)))
8119
8120 (defun gnus-summary-pop-article (number)
8121   "Pop one article off the history and go to the previous.
8122 NUMBER articles will be popped off."
8123   (interactive "p")
8124   (let (to)
8125     (setq gnus-newsgroup-history
8126           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
8127     (if to
8128         (gnus-summary-goto-article (car to))
8129       (error "Article history empty")))
8130   (gnus-summary-position-cursor))
8131
8132 ;; Summary article oriented commands
8133
8134 (defun gnus-summary-refer-parent-article (n)
8135   "Refer parent article N times.
8136 The difference between N and the number of articles fetched is returned."
8137   (interactive "p")
8138   (gnus-set-global-variables)
8139   (while 
8140       (and 
8141        (> n 0)
8142        (let ((ref (header-references (gnus-get-header-by-num
8143                                       (gnus-summary-article-number)))))
8144          (if (and ref (not (equal ref ""))
8145                   (string-match "<[^<>]*>[ \t]*$" ref))
8146              (gnus-summary-refer-article 
8147               (substring ref (match-beginning 0) (match-end 0)))
8148            (gnus-message 1 "No references in article %d"
8149                          (gnus-summary-article-number))
8150            nil)))
8151     (setq n (1- n)))
8152   (gnus-summary-position-cursor)
8153   n)
8154     
8155 (defun gnus-summary-refer-article (message-id)
8156   "Refer article specified by MESSAGE-ID.
8157 NOTE: This command only works with newsgroups that use real or simulated NNTP."
8158   (interactive "sMessage-ID: ")
8159   (if (or (not (stringp message-id))
8160           (zerop (length message-id)))
8161       ()
8162     ;; Construct the correct Message-ID if necessary.
8163     ;; Suggested by tale@pawl.rpi.edu.
8164     (or (string-match "^<" message-id)
8165         (setq message-id (concat "<" message-id)))
8166     (or (string-match ">$" message-id)
8167         (setq message-id (concat message-id ">")))
8168     (let ((header (car (gnus-gethash (downcase message-id)
8169                                      gnus-newsgroup-dependencies))))
8170       (if header
8171           (or (gnus-summary-goto-article (header-number header))
8172               ;; The header has been read, but the article had been
8173               ;; expunged, so we insert it again.
8174               (progn
8175                 (gnus-summary-insert-line
8176                  nil header 0 nil gnus-read-mark nil nil
8177                  (header-subject header))
8178                 (forward-line -1)
8179                 (header-number header)))
8180         (let ((gnus-override-method gnus-refer-article-method)
8181               (gnus-ancient-mark gnus-read-mark)
8182               (tmp-point (window-start
8183                           (get-buffer-window gnus-article-buffer)))
8184               number tmp-buf)
8185           (and gnus-refer-article-method
8186                (or (gnus-server-opened gnus-refer-article-method)
8187                    (gnus-open-server gnus-refer-article-method)))
8188           ;; Save the old article buffer.
8189           (save-excursion
8190             (set-buffer gnus-article-buffer)
8191             (gnus-kill-buffer " *temp Article*")
8192             (setq tmp-buf (rename-buffer " *temp Article*")))
8193           (prog1
8194               (if (gnus-article-prepare 
8195                    message-id nil (gnus-read-header message-id))
8196                   (progn
8197                     (setq number (header-number gnus-current-headers))
8198                     (gnus-rebuild-thread message-id)
8199                     (gnus-summary-goto-subject number)
8200                     (gnus-summary-recenter)
8201                     (gnus-article-set-window-start 
8202                      (cdr (assq number gnus-newsgroup-bookmarks)))
8203                     message-id)
8204                 ;; We restore the old article buffer.
8205                 (save-excursion
8206                   (kill-buffer gnus-article-buffer)
8207                   (set-buffer tmp-buf)
8208                   (rename-buffer gnus-article-buffer)
8209                   (let ((buffer-read-only nil))
8210                     (and tmp-point
8211                          (set-window-start (get-buffer-window (current-buffer))
8212                                            tmp-point)))))))))))
8213
8214 (defun gnus-summary-enter-digest-group ()
8215   "Enter a digest group based on the current article."
8216   (interactive)
8217   (gnus-set-global-variables)
8218   (gnus-summary-select-article)
8219   ;; We do not want a narrowed article.
8220   (gnus-summary-stop-page-breaking)
8221   (let ((name (format "%s-%d" 
8222                       (gnus-group-prefixed-name 
8223                        gnus-newsgroup-name (list 'nndoc "")) 
8224                       gnus-current-article))
8225         (ogroup gnus-newsgroup-name)
8226         (buf (current-buffer)))
8227     (if (gnus-group-read-ephemeral-group 
8228          name (list 'nndoc name
8229                     (list 'nndoc-address (get-buffer gnus-article-buffer))
8230                     '(nndoc-article-type digest))
8231          t)
8232         (setcdr (nthcdr 4 (nth 2 (gnus-gethash name gnus-newsrc-hashtb)))
8233                 (list (list (cons 'to-group ogroup))))
8234       (switch-to-buffer buf)
8235       (gnus-set-global-variables)
8236       (gnus-configure-windows 'summary)
8237       (gnus-message 3 "Article not a digest?"))))
8238
8239 (defun gnus-summary-isearch-article ()
8240   "Do incremental search forward on current article."
8241   (interactive)
8242   (gnus-set-global-variables)
8243   (gnus-summary-select-article)
8244   (gnus-eval-in-buffer-window 
8245    gnus-article-buffer (isearch-forward)))
8246
8247 (defun gnus-summary-search-article-forward (regexp &optional backward)
8248   "Search for an article containing REGEXP forward.
8249 If BACKWARD, search backward instead."
8250   (interactive
8251    (list (read-string
8252           (format "Search article %s (regexp%s): "
8253                   (if current-prefix-arg "backward" "forward")
8254                   (if gnus-last-search-regexp
8255                       (concat ", default " gnus-last-search-regexp)
8256                     "")))
8257          current-prefix-arg))
8258   (gnus-set-global-variables)
8259   (if (string-equal regexp "")
8260       (setq regexp (or gnus-last-search-regexp ""))
8261     (setq gnus-last-search-regexp regexp))
8262   (if (gnus-summary-search-article regexp backward)
8263       (gnus-article-set-window-start 
8264        (cdr (assq (gnus-summary-article-number) gnus-newsgroup-bookmarks)))
8265     (error "Search failed: \"%s\"" regexp)))
8266
8267 (defun gnus-summary-search-article-backward (regexp)
8268   "Search for an article containing REGEXP backward."
8269   (interactive
8270    (list (read-string
8271           (format "Search article backward (regexp%s): "
8272                   (if gnus-last-search-regexp
8273                       (concat ", default " gnus-last-search-regexp)
8274                     "")))))
8275   (gnus-summary-search-article-forward regexp 'backward))
8276
8277 (defun gnus-summary-search-article (regexp &optional backward)
8278   "Search for an article containing REGEXP.
8279 Optional argument BACKWARD means do search for backward.
8280 gnus-select-article-hook is not called during the search."
8281   (let ((gnus-select-article-hook nil)  ;Disable hook.
8282         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8283         (re-search
8284          (if backward
8285              (function re-search-backward) (function re-search-forward)))
8286         (found nil)
8287         (last nil))
8288     ;; Hidden thread subtrees must be searched for ,too.
8289     (gnus-summary-show-all-threads)
8290     (if (eobp) (forward-line -1))
8291     ;; First of all, search current article.
8292     ;; We don't want to read article again from NNTP server nor reset
8293     ;; current point.
8294     (gnus-summary-select-article)
8295     (gnus-message 9 "Searching article: %d..." gnus-current-article)
8296     (setq last gnus-current-article)
8297     (gnus-eval-in-buffer-window gnus-article-buffer
8298       (save-restriction
8299         (widen)
8300         ;; Begin search from current point.
8301         (setq found (funcall re-search regexp nil t))))
8302     ;; Then search next articles.
8303     (while (and (not found)
8304                 (gnus-summary-display-article 
8305                  (gnus-summary-search-subject backward nil nil)))
8306       (gnus-message 9 "Searching article: %d..." gnus-current-article)
8307       (gnus-eval-in-buffer-window gnus-article-buffer
8308         (save-restriction
8309           (widen)
8310           (goto-char (if backward (point-max) (point-min)))
8311           (setq found (funcall re-search regexp nil t)))))
8312     (message "")
8313     ;; Adjust article pointer.
8314     (or (eq last gnus-current-article)
8315         (setq gnus-last-article last))
8316     ;; Return T if found such article.
8317     found))
8318
8319 (defun gnus-summary-execute-command (header regexp command &optional backward)
8320   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8321 If HEADER is an empty string (or nil), the match is done on the entire
8322 article. If BACKWARD (the prefix) is non-nil, search backward instead."
8323   (interactive
8324    (list (let ((completion-ignore-case t))
8325            (completing-read 
8326             "Header name: "
8327             (mapcar (lambda (string) (list string))
8328                     '("Number" "Subject" "From" "Lines" "Date"
8329                       "Message-ID" "Xref" "References"))
8330             nil 'require-match))
8331          (read-string "Regexp: ")
8332          (read-key-sequence "Command: ")
8333          current-prefix-arg))
8334   (gnus-set-global-variables)
8335   ;; Hidden thread subtrees must be searched as well.
8336   (gnus-summary-show-all-threads)
8337   ;; We don't want to change current point nor window configuration.
8338   (save-excursion
8339     (save-window-excursion
8340       (gnus-message 6 "Executing %s..." (key-description command))
8341       ;; We'd like to execute COMMAND interactively so as to give arguments.
8342       (gnus-execute header regexp
8343                     (` (lambda ()
8344                          (call-interactively '(, (key-binding command)))))
8345                     backward)
8346       (gnus-message 6 "Executing %s...done" (key-description command)))))
8347
8348 (defun gnus-summary-beginning-of-article ()
8349   "Scroll the article back to the beginning."
8350   (interactive)
8351   (gnus-set-global-variables)
8352   (gnus-summary-select-article)
8353   (gnus-configure-windows 'article)
8354   (gnus-eval-in-buffer-window
8355    gnus-article-buffer
8356    (widen)
8357    (goto-char (point-min))
8358    (and gnus-break-pages (gnus-narrow-to-page))))
8359
8360 (defun gnus-summary-end-of-article ()
8361   "Scroll to the end of the article."
8362   (interactive)
8363   (gnus-set-global-variables)
8364   (gnus-summary-select-article)
8365   (gnus-configure-windows 'article)
8366   (gnus-eval-in-buffer-window 
8367    gnus-article-buffer
8368    (widen)
8369    (goto-char (point-max))
8370    (and gnus-break-pages (gnus-narrow-to-page))))
8371
8372 (defun gnus-summary-show-article (&optional no-refetch)
8373   "Force re-fetching of the current article.
8374 If the prefix argument NO-REFETCH is non-nil, no actual refetch will
8375 be performed.  The current article will simply be redisplayed."
8376   (interactive "P")
8377   (gnus-set-global-variables)
8378   (if (not no-refetch)
8379       (gnus-summary-select-article gnus-have-all-headers t)
8380     (or gnus-current-article
8381         (error "There is no current article"))
8382     (gnus-summary-goto-subject gnus-current-article)
8383     (gnus-configure-windows 'article)
8384     (gnus-summary-position-cursor)))
8385
8386 (defun gnus-summary-verbose-headers (&optional arg)
8387   "Toggle permanent full header display.
8388 If ARG is a positive number, turn header display on.
8389 If ARG is a negative number, turn header display off."
8390   (interactive "P")
8391   (gnus-set-global-variables)
8392   (gnus-summary-toggle-header arg)
8393   (setq gnus-show-all-headers
8394         (cond ((or (not (numberp arg))
8395                    (zerop arg))
8396                (not gnus-show-all-headers))
8397               ((natnump arg)
8398                t))))
8399
8400 (defun gnus-summary-toggle-header (&optional arg)
8401   "Show the headers if they are hidden, or hide them if they are shown.
8402 If ARG is a positive number, show the entire header.
8403 If ARG is a negative number, hide the unwanted header lines."
8404   (interactive "P")
8405   (gnus-set-global-variables)
8406   (save-excursion
8407     (set-buffer gnus-article-buffer)
8408     (let ((buffer-read-only nil))
8409       (if (numberp arg) 
8410           (if (> arg 0) (remove-text-properties (point-min) (point-max) 
8411                                                 gnus-hidden-properties)
8412             (if (< arg 0) (run-hooks 'gnus-article-display-hook)))
8413         (if (text-property-any (point-min) (point-max) 'invisible t)
8414             (remove-text-properties 
8415              (point-min) (point-max) gnus-hidden-properties)
8416           ;; We hide the headers. This song and dance act below is
8417           ;; done because `gnus-have-all-headers' is buffer-local to
8418           ;; the summary buffer, and we only want to temporarily
8419           ;; change it in that buffer. Ugh.
8420           (let ((have gnus-have-all-headers))
8421             (save-excursion
8422               (set-buffer gnus-summary-buffer)
8423               (setq gnus-have-all-headers nil)
8424               (save-excursion
8425                 (set-buffer gnus-article-buffer)
8426                 (run-hooks 'gnus-article-display-hook))
8427               (setq gnus-have-all-headers have)))))
8428       (set-window-point (get-buffer-window (current-buffer)) (point-min)))))
8429
8430 (defun gnus-summary-show-all-headers ()
8431   "Make all header lines visible."
8432   (interactive)
8433   (gnus-set-global-variables)
8434   (gnus-article-show-all-headers))
8435
8436 (defun gnus-summary-toggle-mime (&optional arg)
8437   "Toggle MIME processing.
8438 If ARG is a positive number, turn MIME processing on."
8439   (interactive "P")
8440   (gnus-set-global-variables)
8441   (setq gnus-show-mime
8442         (if (null arg) (not gnus-show-mime)
8443           (> (prefix-numeric-value arg) 0)))
8444   (gnus-summary-select-article t 'force))
8445
8446 (defun gnus-summary-caesar-message (&optional arg)
8447   "Caesar rotate the current article by 13.
8448 The numerical prefix specifies how manu places to rotate each letter
8449 forward."
8450   (interactive "P")
8451   (gnus-set-global-variables)
8452   (gnus-summary-select-article)
8453   (let ((mail-header-separator ""))
8454     (gnus-eval-in-buffer-window 
8455      gnus-article-buffer
8456      (save-restriction
8457        (widen)
8458        (let ((last (point)))
8459          (news-caesar-buffer-body arg)
8460          (goto-char last)
8461          (recenter 0))))))
8462
8463 (defun gnus-summary-stop-page-breaking ()
8464   "Stop page breaking in the current article."
8465   (interactive)
8466   (gnus-set-global-variables)
8467   (gnus-summary-select-article)
8468   (gnus-eval-in-buffer-window gnus-article-buffer (widen)))
8469
8470 ;; Suggested by Brian Edmonds <bedmonds@prodigy.bc.ca>.
8471
8472 (defun gnus-summary-move-article (&optional n to-newsgroup select-method)
8473   "Move the current article to a different newsgroup.
8474 If N is a positive number, move the N next articles.
8475 If N is a negative number, move the N previous articles.
8476 If N is nil and any articles have been marked with the process mark,
8477 move those articles instead.
8478 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to. 
8479 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
8480 re-spool using this method.
8481 For this function to work, both the current newsgroup and the
8482 newsgroup that you want to move to have to support the `request-move'
8483 and `request-accept' functions. (Ie. mail newsgroups at present.)"
8484   (interactive "P")
8485   (gnus-set-global-variables)
8486   (or (gnus-check-backend-function 'request-move-article gnus-newsgroup-name)
8487       (error "The current newsgroup does not support article moving"))
8488   (let ((articles (gnus-summary-work-articles n))
8489         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
8490         art-group to-method sel-met)
8491     (if (and (not to-newsgroup) (not select-method))
8492         (setq to-newsgroup
8493               (completing-read 
8494                (format "Where do you want to move %s? %s"
8495                        (if (> (length articles) 1)
8496                            (format "these %d articles" (length articles))
8497                          "this article")
8498                        (if gnus-current-move-group
8499                            (format "(%s default) " gnus-current-move-group)
8500                          ""))
8501                gnus-active-hashtb nil nil prefix)))
8502     (if to-newsgroup
8503         (progn
8504           (if (or (string= to-newsgroup "") (string= to-newsgroup prefix))
8505               (setq to-newsgroup (or gnus-current-move-group "")))
8506           (or (gnus-gethash to-newsgroup gnus-active-hashtb)
8507               (gnus-activate-newsgroup to-newsgroup)
8508               (error "No such group: %s" to-newsgroup))
8509           (setq gnus-current-move-group to-newsgroup)))
8510     (setq to-method (if select-method (list select-method "")
8511                       (gnus-find-method-for-group to-newsgroup)))
8512     (or (gnus-check-backend-function 'request-accept-article (car to-method))
8513         (error "%s does not support article copying" (car to-method)))
8514     (or (gnus-server-opened to-method)
8515         (gnus-open-server to-method)
8516         (error "Can't open server %s" (car to-method)))
8517     (gnus-message 6 "Moving to %s: %s..." 
8518                   (or select-method to-newsgroup) articles)
8519     (while articles
8520       (if (setq art-group
8521                 (gnus-request-move-article 
8522                  (car articles)                   ; Article to move
8523                  gnus-newsgroup-name              ; From newsgrouo
8524                  (nth 1 (gnus-find-method-for-group 
8525                          gnus-newsgroup-name))    ; Server
8526                  (list 'gnus-request-accept-article 
8527                        (if select-method
8528                            (list 'quote select-method)
8529                          to-newsgroup)
8530                        (not (cdr articles)))     ; Accept form
8531                  (not (cdr articles))))          ; Only save nov last time
8532           (let* ((buffer-read-only nil)
8533                  (entry 
8534                   (or
8535                    (gnus-gethash (car art-group) gnus-newsrc-hashtb)
8536                    (gnus-gethash 
8537                     (gnus-group-prefixed-name 
8538                      (car art-group) 
8539                      (if select-method (list select-method "")
8540                        (gnus-find-method-for-group to-newsgroup)))
8541                     gnus-newsrc-hashtb)))
8542                  (info (nth 2 entry))
8543                  (article (car articles)))
8544             (gnus-summary-goto-subject article)
8545             (beginning-of-line)
8546             (delete-region (point)
8547                            (progn (forward-line 1) (point)))
8548             (if (not (memq article gnus-newsgroup-unreads))
8549                 (setcar (cdr (cdr info))
8550                         (gnus-add-to-range (nth 2 info) 
8551                                            (list (cdr art-group)))))
8552             ;; Copy any marks over to the new group.
8553             (let ((marks '((tick . gnus-newsgroup-marked)
8554                            (dormant . gnus-newsgroup-dormant)
8555                            (expire . gnus-newsgroup-expirable)
8556                            (bookmark . gnus-newsgroup-bookmarks)
8557                         ;   (score . gnus-newsgroup-scored)
8558                            (reply . gnus-newsgroup-replied)))
8559                   (to-article (cdr art-group)))
8560               (while marks
8561                 (if (memq article (symbol-value (cdr (car marks))))
8562                     (gnus-add-marked-articles 
8563                      (car info) (car (car marks)) (list to-article) info))
8564                 (setq marks (cdr marks))))
8565             (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8566             (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8567             (setq gnus-newsgroup-dormant
8568                   (delq article gnus-newsgroup-dormant)))
8569         (gnus-message 1 "Couldn't move article %s" (car articles)))
8570       (gnus-summary-remove-process-mark (car articles))
8571       (setq articles (cdr articles)))))
8572
8573 (defun gnus-summary-respool-article (&optional n respool-method)
8574   "Respool the current article.
8575 The article will be squeezed through the mail spooling process again,
8576 which means that it will be put in some mail newsgroup or other
8577 depending on `nnmail-split-methods'.
8578 If N is a positive number, respool the N next articles.
8579 If N is a negative number, respool the N previous articles.
8580 If N is nil and any articles have been marked with the process mark,
8581 respool those articles instead.
8582
8583 Respooling can be done both from mail groups and \"real\" newsgroups.
8584 In the former case, the articles in question will be moved from the
8585 current group into whatever groups they are destined to.  In the
8586 latter case, they will be copied into the relevant groups."
8587   (interactive "P")
8588   (gnus-set-global-variables)
8589   (let ((respool-methods (gnus-methods-using 'respool))
8590         (methname 
8591          (symbol-name (car (gnus-find-method-for-group gnus-newsgroup-name)))))
8592     (or respool-method
8593         (setq respool-method
8594               (completing-read
8595                "What method do you want to use when respooling? "
8596                respool-methods nil t methname)))
8597     (or (string= respool-method "")
8598         (if (assoc (symbol-name
8599                     (car (gnus-find-method-for-group gnus-newsgroup-name)))
8600                    respool-methods)
8601             (gnus-summary-move-article n nil (intern respool-method))
8602           (gnus-summary-copy-article n nil (intern respool-method))))))
8603
8604 ;; Suggested by gregj@unidata.com (Gregory J. Grubbs).
8605 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
8606   "Move the current article to a different newsgroup.
8607 If N is a positive number, move the N next articles.
8608 If N is a negative number, move the N previous articles.
8609 If N is nil and any articles have been marked with the process mark,
8610 move those articles instead.
8611 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to. 
8612 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
8613 re-spool using this method.
8614 For this function to work, the newsgroup that you want to move to have
8615 to support the `request-move' and `request-accept'
8616 functions. (Ie. mail newsgroups at present.)"
8617   (interactive "P")
8618   (gnus-set-global-variables)
8619   (let ((articles (gnus-summary-work-articles n))
8620         (copy-buf (get-buffer-create "*copy work*"))
8621         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
8622         art-group to-method)
8623     (buffer-disable-undo copy-buf)
8624     (if (and (not to-newsgroup) (not select-method))
8625         (setq to-newsgroup
8626               (completing-read 
8627                (format "Where do you want to copy %s? %s"
8628                        (if (> (length articles) 1)
8629                            (format "these %d articles" (length articles))
8630                          "this article")
8631                        (if gnus-current-move-group
8632                            (format "(%s default) " gnus-current-move-group)
8633                          ""))
8634                gnus-active-hashtb nil nil prefix)))
8635     (if to-newsgroup
8636         (progn
8637           (if (or (string= to-newsgroup "") (string= to-newsgroup prefix))
8638               (setq to-newsgroup (or gnus-current-move-group "")))
8639           (or (gnus-gethash to-newsgroup gnus-active-hashtb)
8640               (gnus-activate-newsgroup to-newsgroup)
8641               (error "No such group: %s" to-newsgroup))
8642           (setq gnus-current-move-group to-newsgroup)))
8643     (setq to-method (if select-method (list select-method "")
8644                       (gnus-find-method-for-group to-newsgroup)))
8645     (or (gnus-check-backend-function 'request-accept-article (car to-method))
8646         (error "%s does not support article copying" (car to-method)))
8647     (or (gnus-server-opened to-method)
8648         (gnus-open-server to-method)
8649         (error "Can't open server %s" (car to-method)))
8650     (while articles
8651       (gnus-message 6 "Copying to %s: %s..." 
8652                     (or select-method to-newsgroup) articles)
8653       (if (setq art-group
8654                 (save-excursion
8655                   (set-buffer copy-buf)
8656                   (gnus-request-article-this-buffer
8657                    (car articles) gnus-newsgroup-name)
8658                   (gnus-request-accept-article
8659                    (if select-method (quote select-method) to-newsgroup)
8660                    (not (cdr articles)))))
8661           (let* ((entry 
8662                   (or
8663                    (gnus-gethash (car art-group) gnus-newsrc-hashtb)
8664                    (gnus-gethash 
8665                     (gnus-group-prefixed-name 
8666                      (car art-group) 
8667                      (if select-method (list select-method "")
8668                        (gnus-find-method-for-group to-newsgroup)))
8669                     gnus-newsrc-hashtb)))
8670                  (info (nth 2 entry))
8671                  (article (car articles)))
8672             (if (not (memq article gnus-newsgroup-unreads))
8673                 (setcar (cdr (cdr info))
8674                         (gnus-add-to-range (nth 2 info) 
8675                                            (list (cdr art-group)))))
8676             ;; Copy any marks over to the new group.
8677             (let ((marks '((tick . gnus-newsgroup-marked)
8678                            (dormant . gnus-newsgroup-dormant)
8679                            (expire . gnus-newsgroup-expirable)
8680                            (bookmark . gnus-newsgroup-bookmarks)
8681                         ;   (score . gnus-newsgroup-scored)
8682                            (reply . gnus-newsgroup-replied)))
8683                   (to-article (cdr art-group)))
8684               (while marks
8685                 (if (memq article (symbol-value (cdr (car marks))))
8686                     (gnus-add-marked-articles 
8687                      (car info) (car (car marks)) (list to-article) info))
8688                 (setq marks (cdr marks)))))
8689         (gnus-message 1 "Couldn't copy article %s" (car articles)))
8690       (gnus-summary-remove-process-mark (car articles))
8691       (setq articles (cdr articles)))
8692     (kill-buffer copy-buf)))
8693
8694 (defun gnus-summary-import-article (file)
8695   "Import a random file into a mail newsgroup."
8696   (interactive "fImport file: ")
8697   (let ((group gnus-newsgroup-name)
8698         atts)
8699     (or (gnus-check-backend-function 'request-accept-article group)
8700         (error "%s does not support article importing" group))
8701     (or (file-readable-p file)
8702         (not (file-regular-p file))
8703         (error "Can't read %s" file))
8704     (save-excursion
8705       (set-buffer (get-buffer-create " *import file*"))
8706       (buffer-disable-undo (current-buffer))
8707       (erase-buffer)
8708       (insert-file-contents file)
8709       (goto-char (point-min))
8710       (if (nnheader-article-p)
8711           ()
8712         (setq atts (file-attributes file))
8713         (insert "From: " (read-string "From: ") "\n"
8714                 "Subject: " (read-string "Subject: ") "\n"
8715                 "Date: " (current-time-string (nth 5 atts)) "\n"
8716                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8717       (gnus-request-accept-article group t)
8718       (kill-buffer (current-buffer)))))
8719
8720 (defun gnus-summary-expire-articles ()
8721   "Expire all articles that are marked as expirable in the current group."
8722   (interactive)
8723   (if (not (gnus-check-backend-function 
8724             'request-expire-articles gnus-newsgroup-name))
8725       ()
8726     (let* ((info (nth 2 (gnus-gethash gnus-newsgroup-name 
8727                                       gnus-newsrc-hashtb)))
8728            (total (memq 'total-expire (nth 5 info)))
8729            (expirable (if total
8730                           (gnus-list-of-read-articles gnus-newsgroup-name)
8731                         (setq gnus-newsgroup-expirable
8732                               (sort gnus-newsgroup-expirable '<))))
8733            es)
8734       (if (not expirable)
8735           ()
8736         (gnus-message 6 "Expiring articles...")
8737         ;; The list of articles that weren't expired is returned.
8738         (setq es (gnus-request-expire-articles expirable gnus-newsgroup-name))
8739         (or total (setq gnus-newsgroup-expirable es))
8740         ;; We go through the old list of expirable, and mark all
8741         ;; really expired articles as non-existant.
8742         (or (eq es expirable) ; If nothing was expired, we don't mark.
8743             (let ((gnus-use-cache nil))
8744               (while expirable
8745                 (or (memq (car expirable) es)
8746                     (gnus-summary-mark-article
8747                      (car expirable) gnus-canceled-mark))
8748                 (setq expirable (cdr expirable)))))
8749         (gnus-message 6 "Expiring articles...done")))))
8750
8751 (defun gnus-summary-expire-articles-now ()
8752   "Expunge all expirable articles in the current group.
8753 This means that *all* articles that are marked as expirable will be
8754 deleted forever, right now."
8755   (interactive)
8756   (or gnus-expert-user
8757       (gnus-y-or-n-p
8758        "Are you really, really, really sure you want to expunge? ")
8759       (error "Phew!"))
8760   (let ((nnmail-expiry-wait -1)
8761         (nnmail-expiry-wait-function nil))
8762     (gnus-summary-expire-articles)))
8763
8764 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
8765 (defun gnus-summary-delete-article (&optional n)
8766   "Delete the N next (mail) articles.
8767 This command actually deletes articles. This is not a marking
8768 command. The article will disappear forever from you life, never to
8769 return. 
8770 If N is negative, delete backwards.
8771 If N is nil and articles have been marked with the process mark,
8772 delete these instead."
8773   (interactive "P")
8774   (or (gnus-check-backend-function 'request-expire-articles 
8775                                    gnus-newsgroup-name)
8776       (error "The current newsgroup does not support article deletion."))
8777   ;; Compute the list of articles to delete.
8778   (let ((articles (gnus-summary-work-articles n))
8779         not-deleted)
8780     (if (and gnus-novice-user
8781              (not (gnus-y-or-n-p 
8782                    (format "Do you really want to delete %s forever? "
8783                            (if (> (length articles) 1) "these articles"
8784                              "this article")))))
8785         ()
8786       ;; Delete the articles.
8787       (setq not-deleted (gnus-request-expire-articles 
8788                          articles gnus-newsgroup-name 'force))
8789       (while articles
8790         (gnus-summary-remove-process-mark (car articles))       
8791         ;; The backend might not have been able to delete the article
8792         ;; after all.  
8793         (or (memq (car articles) not-deleted)
8794             (gnus-summary-mark-article (car articles) gnus-canceled-mark))
8795         (setq articles (cdr articles))))
8796     (gnus-summary-position-cursor)
8797     not-deleted))
8798
8799 (defun gnus-summary-edit-article ()
8800   "Enter into a buffer and edit the current article.
8801 This will have permanent effect only in mail groups."
8802   (interactive)
8803   (or (gnus-check-backend-function 
8804        'request-replace-article gnus-newsgroup-name)
8805       (error "The current newsgroup does not support article editing."))
8806   (gnus-summary-select-article t)
8807   (gnus-configure-windows 'article)
8808   (select-window (get-buffer-window gnus-article-buffer))
8809   (gnus-message 6 "C-c C-c to end edits")
8810   (setq buffer-read-only nil)
8811   (text-mode)
8812   (use-local-map (copy-keymap (current-local-map)))
8813   (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
8814   (buffer-enable-undo)
8815   (widen)
8816   (goto-char (point-min))
8817   (search-forward "\n\n" nil t))
8818
8819 (defun gnus-summary-edit-article-done ()
8820   "Make edits to the current article permanent."
8821   (interactive)
8822   (let ((buf (buffer-substring-no-properties (point-min) (point-max))))
8823     (erase-buffer)
8824     (insert buf)
8825     (if (not (gnus-request-replace-article 
8826               (cdr gnus-article-current) (car gnus-article-current) 
8827               (current-buffer)))
8828         (error "Couldn't replace article.")
8829       (gnus-article-mode)
8830       (use-local-map gnus-article-mode-map)
8831       (setq buffer-read-only t)
8832       (buffer-disable-undo (current-buffer))
8833       (gnus-configure-windows 'summary))))
8834
8835 (defun gnus-summary-fancy-query ()
8836   "Query where the fancy respool algorithm would put this article."
8837   (interactive)
8838   (gnus-summary-select-article)
8839   (save-excursion
8840     (set-buffer gnus-article-buffer)
8841     (save-restriction
8842       (goto-char (point-min))
8843       (search-forward "\n\n")
8844       (narrow-to-region (point-min) (point))
8845       (pp-eval-expression (list 'quote (nnmail-split-fancy))))))
8846
8847 ;; Summary score commands.
8848
8849 ;; Suggested by boubaker@cenatls.cena.dgac.fr.
8850
8851 (defun gnus-summary-raise-score (n)
8852   "Raise the score of the current article by N."
8853   (interactive "p")
8854   (gnus-summary-set-score (+ (gnus-summary-article-score) n)))
8855
8856 (defun gnus-summary-set-score (n)
8857   "Set the score of the current article to N."
8858   (interactive "p")
8859   ;; Skip dummy header line.
8860   (save-excursion
8861     (gnus-summary-show-thread)
8862     (if (eq (gnus-summary-article-mark) gnus-dummy-mark)
8863         (forward-line 1))
8864     (let ((buffer-read-only nil))
8865       ;; Set score.
8866       (gnus-summary-update-mark
8867        (if (= n (or gnus-summary-default-score 0)) ? 
8868          (if (< n (or gnus-summary-default-score 0)) 
8869              gnus-score-below-mark gnus-score-over-mark)) 'score))
8870     (let* ((article (gnus-summary-article-number))
8871            (score (assq article gnus-newsgroup-scored)))
8872       (if score (setcdr score n)
8873         (setq gnus-newsgroup-scored 
8874               (cons (cons article n) gnus-newsgroup-scored))))
8875     (gnus-summary-update-line)))
8876
8877 (defun gnus-summary-current-score ()
8878   "Return the score of the current article."
8879   (interactive)
8880   (message "%s" (gnus-summary-article-score)))
8881
8882 ;; Summary marking commands.
8883
8884 (defun gnus-summary-raise-same-subject-and-select (score)
8885   "Raise articles which has the same subject with SCORE and select the next."
8886   (interactive "p")
8887   (let ((subject (gnus-summary-subject-string)))
8888     (gnus-summary-raise-score score)
8889     (while (gnus-summary-search-subject nil nil subject)
8890       (gnus-summary-raise-score score))
8891     (gnus-summary-next-article t)))
8892
8893 (defun gnus-summary-raise-same-subject (score)
8894   "Raise articles which has the same subject with SCORE."
8895   (interactive "p")
8896   (let ((subject (gnus-summary-subject-string)))
8897     (gnus-summary-raise-score score)
8898     (while (gnus-summary-search-subject nil nil subject)
8899       (gnus-summary-raise-score score))
8900     (gnus-summary-next-subject 1 t)))
8901
8902 (defun gnus-score-default (level)
8903   (if level (prefix-numeric-value level) 
8904     gnus-score-interactive-default-score))
8905
8906 (defun gnus-summary-raise-thread (&optional score)
8907   "Raise the score of the articles in the current thread with SCORE."
8908   (interactive "P")
8909   (setq score (1- (gnus-score-default score)))
8910   (let (e)
8911     (save-excursion
8912       (let ((level (gnus-summary-thread-level)))
8913         (gnus-summary-raise-score score)
8914         (while (and (zerop (gnus-summary-next-subject 1 nil t))
8915                     (> (gnus-summary-thread-level) level))
8916           (gnus-summary-raise-score score))
8917         (setq e (point))))
8918     (let ((gnus-summary-check-current t))
8919       (or (zerop (gnus-summary-next-subject 1 t))
8920           (goto-char e))))
8921   (gnus-summary-recenter)
8922   (gnus-summary-position-cursor)
8923   (gnus-set-mode-line 'summary))
8924
8925 (defun gnus-summary-lower-same-subject-and-select (score)
8926   "Raise articles which has the same subject with SCORE and select the next."
8927   (interactive "p")
8928   (gnus-summary-raise-same-subject-and-select (- score)))
8929
8930 (defun gnus-summary-lower-same-subject (score)
8931   "Raise articles which has the same subject with SCORE."
8932   (interactive "p")
8933   (gnus-summary-raise-same-subject (- score)))
8934
8935 (defun gnus-summary-lower-thread (&optional score)
8936   "Lower score of articles in the current thread with SCORE."
8937   (interactive "P")
8938   (gnus-summary-raise-thread (- (1- (gnus-score-default score)))))
8939
8940 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
8941   "Mark articles which has the same subject as read, and then select the next.
8942 If UNMARK is positive, remove any kind of mark.
8943 If UNMARK is negative, tick articles."
8944   (interactive "P")
8945   (if unmark
8946       (setq unmark (prefix-numeric-value unmark)))
8947   (let ((count
8948          (gnus-summary-mark-same-subject
8949           (gnus-summary-subject-string) unmark)))
8950     ;; Select next unread article. If auto-select-same mode, should
8951     ;; select the first unread article.
8952     (gnus-summary-next-article t (and gnus-auto-select-same
8953                                       (gnus-summary-subject-string)))
8954     (gnus-message 7 "%d articles are marked as %s"
8955                   count (if unmark "unread" "read"))))
8956
8957 (defun gnus-summary-kill-same-subject (&optional unmark)
8958   "Mark articles which has the same subject as read. 
8959 If UNMARK is positive, remove any kind of mark.
8960 If UNMARK is negative, tick articles."
8961   (interactive "P")
8962   (if unmark
8963       (setq unmark (prefix-numeric-value unmark)))
8964   (let ((count
8965          (gnus-summary-mark-same-subject
8966           (gnus-summary-subject-string) unmark)))
8967     ;; If marked as read, go to next unread subject.
8968     (if (null unmark)
8969         ;; Go to next unread subject.
8970         (gnus-summary-next-subject 1 t))
8971     (gnus-message 7 "%d articles are marked as %s"
8972                   count (if unmark "unread" "read"))))
8973
8974 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8975   "Mark articles with same SUBJECT as read, and return marked number.
8976 If optional argument UNMARK is positive, remove any kinds of marks.
8977 If optional argument UNMARK is negative, mark articles as unread instead."
8978   (let ((count 1))
8979     (save-excursion
8980       (cond 
8981        ((null unmark)                   ; Mark as read.
8982         (while (and 
8983                 (progn
8984                   (gnus-summary-mark-article-as-read gnus-killed-mark)
8985                   (gnus-summary-show-thread) t)
8986                 (gnus-summary-search-forward nil subject))
8987           (setq count (1+ count))))
8988        ((> unmark 0)                    ; Tick.
8989         (while (and
8990                 (progn
8991                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
8992                   (gnus-summary-show-thread) t)
8993                 (gnus-summary-search-forward nil subject))
8994           (setq count (1+ count))))
8995        (t                               ; Mark as unread.
8996         (while (and
8997                 (progn
8998                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
8999                   (gnus-summary-show-thread) t)
9000                 (gnus-summary-search-forward nil subject))
9001           (setq count (1+ count)))))
9002       ;; Return the number of marked articles.
9003       count)))
9004
9005 (defun gnus-summary-mark-as-processable (n &optional unmark)
9006   "Set the process mark on the next N articles.
9007 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9008 the process mark instead.  The difference between N and the actual
9009 number of articles marked is returned."
9010   (interactive "p")
9011   (let ((backward (< n 0))
9012         (n (abs n)))
9013   (while (and 
9014           (> n 0)
9015           (if unmark
9016               (gnus-summary-remove-process-mark (gnus-summary-article-number))
9017             (gnus-summary-set-process-mark (gnus-summary-article-number)))
9018           (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9019     (setq n (1- n)))
9020   (if (/= 0 n) (gnus-message 7 "No more articles"))
9021   (gnus-summary-recenter)
9022   (gnus-summary-position-cursor)
9023   n))
9024
9025 (defun gnus-summary-unmark-as-processable (n)
9026   "Remove the process mark from the next N articles.
9027 If N is negative, mark backward instead.  The difference between N and
9028 the actual number of articles marked is returned."
9029   (interactive "p")
9030   (gnus-summary-mark-as-processable n t))
9031
9032 (defun gnus-summary-unmark-all-processable ()
9033   "Remove the process mark from all articles."
9034   (interactive)
9035   (save-excursion
9036     (while gnus-newsgroup-processable
9037       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9038   (gnus-summary-position-cursor))
9039
9040 (defun gnus-summary-mark-as-expirable (n)
9041   "Mark N articles forward as expirable.
9042 If N is negative, mark backward instead. The difference between N and
9043 the actual number of articles marked is returned."
9044   (interactive "p")
9045   (gnus-summary-mark-forward n gnus-expirable-mark))
9046
9047 (defun gnus-summary-mark-article-as-replied (article)
9048   "Mark ARTICLE replied and update the summary line."
9049   (setq gnus-newsgroup-replied (cons article gnus-newsgroup-replied))
9050   (let ((buffer-read-only nil))
9051     (if (gnus-summary-goto-subject article)
9052         (progn
9053           (gnus-summary-update-mark gnus-replied-mark 'replied)
9054           t))))
9055
9056 (defun gnus-summary-set-bookmark (article)
9057   "Set a bookmark in current article."
9058   (interactive (list (gnus-summary-article-number)))
9059   (if (or (not (get-buffer gnus-article-buffer))
9060           (not gnus-current-article)
9061           (not gnus-article-current)
9062           (not (equal gnus-newsgroup-name (car gnus-article-current))))
9063       (error "No current article selected"))
9064   ;; Remove old bookmark, if one exists.
9065   (let ((old (assq article gnus-newsgroup-bookmarks)))
9066     (if old (setq gnus-newsgroup-bookmarks 
9067                   (delq old gnus-newsgroup-bookmarks))))
9068   ;; Set the new bookmark, which is on the form 
9069   ;; (article-number . line-number-in-body).
9070   (setq gnus-newsgroup-bookmarks 
9071         (cons 
9072          (cons article 
9073                (save-excursion
9074                  (set-buffer gnus-article-buffer)
9075                  (count-lines
9076                   (min (point)
9077                        (save-excursion
9078                          (goto-char (point-min))
9079                          (search-forward "\n\n" nil t)
9080                          (point)))
9081                   (point))))
9082          gnus-newsgroup-bookmarks))
9083   (gnus-message 6 "A bookmark has been added to the current article."))
9084
9085 (defun gnus-summary-remove-bookmark (article)
9086   "Remove the bookmark from the current article."
9087   (interactive (list (gnus-summary-article-number)))
9088   ;; Remove old bookmark, if one exists.
9089   (let ((old (assq article gnus-newsgroup-bookmarks)))
9090     (if old 
9091         (progn
9092           (setq gnus-newsgroup-bookmarks 
9093                 (delq old gnus-newsgroup-bookmarks))
9094           (gnus-message 6 "Removed bookmark."))
9095       (gnus-message 6 "No bookmark in current article."))))
9096
9097 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9098 (defun gnus-summary-mark-as-dormant (n)
9099   "Mark N articles forward as dormant.
9100 If N is negative, mark backward instead.  The difference between N and
9101 the actual number of articles marked is returned."
9102   (interactive "p")
9103   (gnus-summary-mark-forward n gnus-dormant-mark))
9104
9105 (defun gnus-summary-set-process-mark (article)
9106   "Set the process mark on ARTICLE and update the summary line."
9107   (setq gnus-newsgroup-processable (cons article gnus-newsgroup-processable))
9108   (let ((buffer-read-only nil))
9109     (if (gnus-summary-goto-subject article)
9110         (progn
9111           (gnus-summary-show-thread)
9112           (and (eq (gnus-summary-article-mark) gnus-dummy-mark)
9113                (forward-line 1))
9114           (gnus-summary-update-mark gnus-process-mark 'replied)
9115           t))))
9116
9117 (defun gnus-summary-remove-process-mark (article)
9118   "Remove the process mark from ARTICLE and update the summary line."
9119   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9120   (let ((buffer-read-only nil))
9121     (if (gnus-summary-goto-subject article)
9122         (progn
9123           (gnus-summary-show-thread)
9124           (and (eq (gnus-summary-article-mark) gnus-dummy-mark)
9125                (forward-line 1))
9126           (gnus-summary-update-mark ?  'replied)
9127           (if (memq article gnus-newsgroup-replied) 
9128               (gnus-summary-update-mark gnus-replied-mark 'replied))
9129           t))))
9130
9131 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9132   "Mark N articles as read forwards.
9133 If N is negative, mark backwards instead.
9134 Mark with MARK. If MARK is ? , ?! or ??, articles will be
9135 marked as unread. 
9136 The difference between N and the actual number of articles marked is
9137 returned."
9138   (interactive "p")
9139   (gnus-set-global-variables)
9140   (let ((backward (< n 0))
9141         (gnus-summary-goto-unread
9142          (and gnus-summary-goto-unread
9143               (not (memq mark (list gnus-unread-mark
9144                                     gnus-ticked-mark gnus-dormant-mark)))))
9145         (n (abs n))
9146         (mark (or mark gnus-del-mark)))
9147   (while (and (> n 0)
9148               (gnus-summary-mark-article nil mark no-expire)
9149               (zerop (gnus-summary-next-subject 
9150                       (if backward -1 1) gnus-summary-goto-unread t)))
9151     (setq n (1- n)))
9152   (if (/= 0 n) (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9153   (gnus-summary-recenter)
9154   (gnus-summary-position-cursor)
9155   (gnus-set-mode-line 'summary)
9156   n))
9157
9158 (defun gnus-summary-mark-article-as-read (mark)
9159   "Mark the current article quickly as read with MARK."
9160   (let ((article (gnus-summary-article-number)))
9161     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9162     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9163     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9164     ;; Possibly remove from cache, if that is used. 
9165     (and gnus-use-cache (gnus-cache-enter-remove-article article))
9166     (and gnus-newsgroup-auto-expire 
9167          (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
9168              (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
9169              (= mark gnus-read-mark))
9170          (progn
9171            (setq mark gnus-expirable-mark)
9172            (setq gnus-newsgroup-expirable 
9173                  (cons article gnus-newsgroup-expirable))))
9174     (while (eq (gnus-summary-article-mark) gnus-dummy-mark)
9175       (forward-line 1))
9176     ;; Fix the mark.
9177     (gnus-summary-update-mark mark 'unread)
9178     t))
9179
9180 (defun gnus-summary-mark-article-as-unread (mark)
9181   "Mark the current article quickly as unread with MARK."
9182   (let ((article (gnus-summary-article-number)))
9183     (or (memq article gnus-newsgroup-unreads)
9184         (setq gnus-newsgroup-unreads (cons article gnus-newsgroup-unreads)))
9185     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9186     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9187     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9188     (if (= mark gnus-ticked-mark)
9189         (setq gnus-newsgroup-marked (cons article gnus-newsgroup-marked)))
9190     (if (= mark gnus-dormant-mark)
9191         (setq gnus-newsgroup-dormant (cons article gnus-newsgroup-dormant)))
9192
9193     ;; See whether the article is to be put in the cache.
9194     (and gnus-use-cache
9195          (vectorp (gnus-get-header-by-num article))
9196          (save-excursion
9197            (gnus-cache-possibly-enter-article 
9198             gnus-newsgroup-name article 
9199             (gnus-get-header-by-num article)
9200             (= mark gnus-ticked-mark)
9201             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9202
9203     (while (eq (gnus-summary-article-mark) gnus-dummy-mark)
9204       (forward-line 1))
9205     ;; Fix the mark.
9206     (gnus-summary-update-mark mark 'unread)
9207     t))
9208
9209 (defun gnus-summary-mark-article (&optional article mark no-expire)
9210   "Mark ARTICLE with MARK.  MARK can be any character.
9211 Four MARK strings are reserved: `? ' (unread), `?!' (ticked), `??'
9212 (dormant) and `?E' (expirable).
9213 If MARK is nil, then the default character `?D' is used.
9214 If ARTICLE is nil, then the article on the current line will be
9215 marked." 
9216   (and (stringp mark)
9217        (setq mark (aref mark 0)))
9218   ;; If no mark is given, then we check auto-expiring.
9219   (and (not no-expire)
9220        gnus-newsgroup-auto-expire 
9221        (or (not mark)
9222            (and (numberp mark) 
9223                 (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
9224                     (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
9225                     (= mark gnus-read-mark))))
9226        (setq mark gnus-expirable-mark))
9227   (let* ((mark (or mark gnus-del-mark))
9228          (article (or article (gnus-summary-article-number))))
9229     (or article (error "No article on current line"))
9230     (if (or (= mark gnus-unread-mark) 
9231             (= mark gnus-ticked-mark) 
9232             (= mark gnus-dormant-mark))
9233         (gnus-mark-article-as-unread article mark)
9234       (gnus-mark-article-as-read article mark))
9235
9236     ;; See whether the article is to be put in the cache.
9237     (and gnus-use-cache
9238          (not (= mark gnus-canceled-mark))
9239          (vectorp (gnus-get-header-by-num article))
9240          (save-excursion
9241            (gnus-cache-possibly-enter-article 
9242             gnus-newsgroup-name article 
9243             (gnus-get-header-by-num article)
9244             (= mark gnus-ticked-mark)
9245             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9246
9247     (if (gnus-summary-goto-subject article)
9248         (let ((buffer-read-only nil))
9249           (gnus-summary-show-thread)
9250           (and (eq (gnus-summary-article-mark) gnus-dummy-mark)
9251                (forward-line 1))
9252           ;; Fix the mark.
9253           (gnus-summary-update-mark mark 'unread)
9254           t))))
9255
9256 (defun gnus-summary-update-mark (mark type)
9257   (beginning-of-line)
9258   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
9259         (buffer-read-only nil)
9260         plist)
9261     (if (not forward)
9262         ()
9263       (forward-char forward)
9264       (setq plist (text-properties-at (point)))
9265       (delete-char 1)
9266       (insert mark)
9267       (and plist (add-text-properties (1- (point)) (point) plist))
9268       (and (eq type 'unread)
9269            (add-text-properties (1- (point)) (point) (list 'gnus-mark mark)))
9270       (gnus-summary-update-line (eq mark gnus-unread-mark)))))
9271   
9272 (defun gnus-mark-article-as-read (article &optional mark)
9273   "Enter ARTICLE in the pertinent lists and remove it from others."
9274   ;; Make the article expirable.
9275   (let ((mark (or mark gnus-del-mark)))
9276     (if (= mark gnus-expirable-mark)
9277         (setq gnus-newsgroup-expirable (cons article gnus-newsgroup-expirable))
9278       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
9279     ;; Remove from unread and marked lists.
9280     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9281     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9282     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9283     ;; Possibly remove from cache, if that is used. 
9284     (and gnus-use-cache (gnus-cache-enter-remove-article article))))
9285
9286 (defun gnus-mark-article-as-unread (article &optional mark)
9287   "Enter ARTICLE in the pertinent lists and remove it from others."
9288   (let ((mark (or mark gnus-ticked-mark)))
9289     ;; Add to unread list.
9290     (or (memq article gnus-newsgroup-unreads)
9291         (setq gnus-newsgroup-unreads (cons article gnus-newsgroup-unreads)))
9292     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9293     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9294     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9295     (if (= mark gnus-ticked-mark)
9296         (setq gnus-newsgroup-marked (cons article gnus-newsgroup-marked)))
9297     (if (= mark gnus-dormant-mark)
9298         (setq gnus-newsgroup-dormant (cons article gnus-newsgroup-dormant)))))
9299
9300 (defalias 'gnus-summary-mark-as-unread-forward 
9301   'gnus-summary-tick-article-forward)
9302 (make-obsolete 'gnus-summary-mark-as-unread-forward 
9303                'gnus-summary-tick-article-forward)
9304 (defun gnus-summary-tick-article-forward (n)
9305   "Tick N articles forwards.
9306 If N is negative, tick backwards instead.
9307 The difference between N and the number of articles ticked is returned."
9308   (interactive "p")
9309   (gnus-summary-mark-forward n gnus-ticked-mark))
9310
9311 (defalias 'gnus-summary-mark-as-unread-backward 
9312   'gnus-summary-tick-article-backward)
9313 (make-obsolete 'gnus-summary-mark-as-unread-backward 
9314                'gnus-summary-tick-article-backward)
9315 (defun gnus-summary-tick-article-backward (n)
9316   "Tick N articles backwards.
9317 The difference between N and the number of articles ticked is returned."
9318   (interactive "p")
9319   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
9320
9321 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9322 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9323 (defun gnus-summary-tick-article (&optional article clear-mark)
9324   "Mark current article as unread.
9325 Optional 1st argument ARTICLE specifies article number to be marked as unread.
9326 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
9327   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
9328                                        gnus-ticked-mark)))
9329
9330 (defun gnus-summary-mark-as-read-forward (n)
9331   "Mark N articles as read forwards.
9332 If N is negative, mark backwards instead.
9333 The difference between N and the actual number of articles marked is
9334 returned."
9335   (interactive "p")
9336   (gnus-summary-mark-forward n gnus-del-mark t))
9337
9338 (defun gnus-summary-mark-as-read-backward (n)
9339   "Mark the N articles as read backwards.
9340 The difference between N and the actual number of articles marked is
9341 returned."
9342   (interactive "p")
9343   (gnus-summary-mark-forward (- n) gnus-del-mark t))
9344
9345 (defun gnus-summary-mark-as-read (&optional article mark)
9346   "Mark current article as read.
9347 ARTICLE specifies the article to be marked as read.
9348 MARK specifies a string to be inserted at the beginning of the line."
9349   (gnus-summary-mark-article article mark))
9350
9351 (defun gnus-summary-clear-mark-forward (n)
9352   "Clear marks from N articles forward.
9353 If N is negative, clear backward instead.
9354 The difference between N and the number of marks cleared is returned."
9355   (interactive "p")
9356   (gnus-summary-mark-forward n gnus-unread-mark))
9357
9358 (defun gnus-summary-clear-mark-backward (n)
9359   "Clear marks from N articles backward.
9360 The difference between N and the number of marks cleared is returned."
9361   (interactive "p")
9362   (gnus-summary-mark-forward (- n) gnus-unread-mark))
9363
9364 (defun gnus-summary-mark-unread-as-read ()
9365   "Intended to be used by `gnus-summary-mark-article-hook'."
9366   (and (memq gnus-current-article gnus-newsgroup-unreads)
9367        (or (memq gnus-current-article gnus-newsgroup-marked)
9368            (memq gnus-current-article gnus-newsgroup-dormant)
9369            (memq gnus-current-article gnus-newsgroup-expirable)
9370            (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
9371
9372 (defun gnus-summary-mark-region-as-read (point mark all)
9373   "Mark all unread articles between point and mark as read.
9374 If given a prefix, mark all articles between point and mark as read,
9375 even ticked and dormant ones."
9376   (interactive "r\nP")
9377   (save-excursion
9378     (goto-char point)
9379     (beginning-of-line)
9380     (while (and 
9381             (< (point) mark)
9382             (progn
9383               (and
9384                (or all
9385                    (and
9386                     (not (memq (gnus-summary-article-number)
9387                                gnus-newsgroup-marked))
9388                     (not (memq (gnus-summary-article-number)
9389                                gnus-newsgroup-dormant))))
9390                (gnus-summary-mark-article
9391                 (gnus-summary-article-number) gnus-del-mark))
9392               t)
9393             (zerop (forward-line 1))))))
9394
9395 ;; Fix by Per Abrahamsen <amanda@iesd.auc.dk>.
9396 (defalias 'gnus-summary-delete-marked-as-read 
9397   'gnus-summary-remove-lines-marked-as-read)
9398 (make-obsolete 'gnus-summary-delete-marked-as-read 
9399                'gnus-summary-remove-lines-marked-as-read)
9400 (defun gnus-summary-remove-lines-marked-as-read ()
9401   "Remove lines that are marked as read."
9402   (interactive)
9403   (gnus-summary-remove-lines-marked-with 
9404    (concat (mapconcat
9405             (lambda (char) (char-to-string (symbol-value char)))
9406             '(gnus-del-mark gnus-read-mark gnus-ancient-mark
9407               gnus-killed-mark gnus-kill-file-mark
9408               gnus-low-score-mark gnus-expirable-mark
9409               gnus-canceled-mark)
9410             ""))))
9411
9412 (defalias 'gnus-summary-delete-marked-with 
9413   'gnus-summary-remove-lines-marked-with)
9414 (make-obsolete 'gnus-summary-delete-marked-with 
9415                'gnus-summary-remove-lines-marked-with)
9416 ;; Rewrite by Daniel Quinlan <quinlan@best.com>.
9417 (defun gnus-summary-remove-lines-marked-with (marks)
9418   "Remove lines that are marked with MARKS (e.g. \"DK\")."
9419   (interactive "sMarks: ")
9420   ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
9421   (gnus-set-global-variables)
9422   (let ((buffer-read-only nil)
9423         (marks (concat "^[" marks "]")))
9424     (goto-char (point-min))
9425     (if gnus-newsgroup-adaptive
9426         (gnus-score-remove-lines-adaptive marks)
9427       (while (re-search-forward marks nil t)
9428         (gnus-delete-line)))
9429     ;; If we use dummy roots, we have to do an additional sweep over
9430     ;; the buffer.
9431     (if (not (eq gnus-summary-make-false-root 'dummy))
9432         ()
9433       (goto-char (point-min))
9434       (setq marks (concat "^[" (char-to-string gnus-dummy-mark) "]"))
9435       (while (re-search-forward marks nil t)
9436         (if (gnus-subject-equal
9437              (gnus-summary-subject-string)
9438              (progn
9439                (forward-line 1)
9440                (gnus-summary-subject-string)))
9441             ()
9442           (forward-line -1)
9443           (gnus-delete-line)))))
9444   (or (zerop (buffer-size))
9445       (if (eobp)
9446           (gnus-summary-prev-subject 1)
9447         (gnus-summary-position-cursor))))
9448
9449 (defun gnus-summary-expunge-below (&optional score)
9450   "Remove articles with score less than SCORE."
9451   (interactive "P")
9452   (gnus-set-global-variables)
9453   (setq score (if score
9454                   (prefix-numeric-value score)
9455                 (or gnus-summary-default-score 0)))
9456   (save-excursion
9457     (set-buffer gnus-summary-buffer)
9458     (goto-char (point-min))
9459     (let ((buffer-read-only nil)
9460           beg)
9461       (while (not (eobp))
9462         (if (< (gnus-summary-article-score) score)
9463             (progn
9464               (setq beg (point))
9465               (forward-line 1)
9466               (delete-region beg (point)))
9467           (forward-line 1)))
9468       ;; Adjust point.
9469       (or (zerop (buffer-size))
9470           (if (eobp)
9471               (gnus-summary-prev-subject 1)
9472             (gnus-summary-position-cursor))))))
9473
9474 (defun gnus-summary-mark-below (score mark)
9475   "Mark articles with score less than SCORE with MARK."
9476   (interactive "P\ncMark: ")
9477   (gnus-set-global-variables)
9478   (setq score (if score
9479                   (prefix-numeric-value score)
9480                 (or gnus-summary-default-score 0)))
9481   (save-excursion
9482     (set-buffer gnus-summary-buffer)
9483     (goto-char (point-min))
9484     (while (not (eobp))
9485       (and (< (gnus-summary-article-score) score)
9486            (gnus-summary-mark-article nil mark))
9487       (forward-line 1))))
9488
9489 (defun gnus-summary-kill-below (&optional score)
9490   "Mark articles with score below SCORE as read."
9491   (interactive "P")
9492   (gnus-set-global-variables)
9493   (gnus-summary-mark-below score gnus-killed-mark))
9494
9495 (defun gnus-summary-clear-above (&optional score)
9496   "Clear all marks from articles with score above SCORE."
9497   (interactive "P")
9498   (gnus-set-global-variables)
9499   (gnus-summary-mark-above score gnus-unread-mark))
9500
9501 (defun gnus-summary-tick-above (&optional score)
9502   "Tick all articles with score above SCORE."
9503   (interactive "P")
9504   (gnus-set-global-variables)
9505   (gnus-summary-mark-above score gnus-ticked-mark))
9506
9507 (defun gnus-summary-mark-above (score mark)
9508   "Mark articles with score over SCORE with MARK."
9509   (interactive "P\ncMark: ")
9510   (gnus-set-global-variables)
9511   (setq score (if score
9512                   (prefix-numeric-value score)
9513                 (or gnus-summary-default-score 0)))
9514   (save-excursion
9515     (set-buffer gnus-summary-buffer)
9516     (goto-char (point-min))
9517     (while (not (eobp))
9518       (if (> (gnus-summary-article-score) score)
9519           (progn
9520             (gnus-summary-mark-article nil mark)
9521             (forward-line 1))
9522         (forward-line 1)))))
9523
9524 ;; Suggested by Daniel Quinlan <quinlan@best.com>.  
9525 (defun gnus-summary-show-all-expunged ()
9526   "Display all the hidden articles that were expunged for low scores."
9527   (interactive)
9528   (gnus-set-global-variables)
9529   (let ((buffer-read-only nil))
9530     (let ((scored gnus-newsgroup-scored)
9531           headers h)
9532       (while scored
9533         (or (gnus-summary-goto-subject (car (car scored)))
9534             (and (setq h (gnus-get-header-by-num (car (car scored))))
9535                  (< (cdr (car scored)) gnus-summary-expunge-below)
9536                  (setq headers (cons h headers))))
9537         (setq scored (cdr scored)))
9538       (or headers (error "No expunged articles hidden."))
9539       (goto-char (point-min))
9540       (save-excursion 
9541         (gnus-summary-update-lines 
9542          (point)
9543          (progn
9544            (gnus-summary-prepare-unthreaded (nreverse headers))
9545            (point)))))
9546     (goto-char (point-min))
9547     (gnus-summary-position-cursor)))
9548
9549 (defun gnus-summary-show-all-dormant ()
9550   "Display all the hidden articles that are marked as dormant."
9551   (interactive)
9552   (gnus-set-global-variables)
9553   (let ((buffer-read-only nil))
9554     (let ((dormant gnus-newsgroup-dormant)
9555           headers h)
9556       (while dormant
9557         (or (gnus-summary-goto-subject (car dormant))
9558             (and (setq h (gnus-get-header-by-num (car dormant)))
9559                  (setq headers (cons h headers))))
9560         (setq dormant (cdr dormant)))
9561       (or headers (error "No dormant articles hidden."))
9562       (goto-char (point-min))
9563       (save-excursion 
9564         (gnus-summary-update-lines 
9565          (point)
9566          (progn
9567            (gnus-summary-prepare-unthreaded (nreverse headers))
9568            (point)))))
9569     (goto-char (point-min))
9570     (gnus-summary-position-cursor)))
9571
9572 (defun gnus-summary-hide-all-dormant ()
9573   "Hide all dormant articles."
9574   (interactive)
9575   (gnus-set-global-variables)
9576   (gnus-summary-remove-lines-marked-with (char-to-string gnus-dormant-mark))
9577   (gnus-summary-position-cursor))
9578
9579 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
9580   "Mark all articles not marked as unread in this newsgroup as read.
9581 If prefix argument ALL is non-nil, all articles are marked as read.
9582 If QUIETLY is non-nil, no questions will be asked.
9583 If TO-HERE is non-nil, it should be a point in the buffer. All
9584 articles before this point will be marked as read.
9585 The number of articles marked as read is returned."
9586   (interactive "P")
9587   (gnus-set-global-variables)
9588   (prog1
9589       (if (or quietly
9590               (not gnus-interactive-catchup) ;Without confirmation?
9591               gnus-expert-user
9592               (gnus-y-or-n-p
9593                (if all
9594                    "Mark absolutely all articles as read? "
9595                  "Mark all unread articles as read? ")))
9596           (if (and not-mark 
9597                    (not gnus-newsgroup-adaptive)
9598                    (not gnus-newsgroup-auto-expire))
9599               (progn
9600                 (and all (setq gnus-newsgroup-marked nil
9601                                gnus-newsgroup-dormant nil))
9602                 (setq gnus-newsgroup-unreads 
9603                       (append gnus-newsgroup-marked gnus-newsgroup-dormant)))
9604             ;; We actually mark all articles as canceled, which we
9605             ;; have to do when using auto-expiry or adaptive scoring. 
9606             (let ((unreads (length gnus-newsgroup-unreads)))
9607               (gnus-summary-show-all-threads)
9608               (if (gnus-summary-first-subject (not all))
9609                   (while (and (if to-here (< (point) to-here) t)
9610                               (gnus-summary-mark-article-as-read
9611                                gnus-catchup-mark)
9612                               (gnus-summary-search-subject nil (not all)))))
9613               (- unreads (length gnus-newsgroup-unreads))
9614               (or to-here
9615                   (setq gnus-newsgroup-unreads gnus-newsgroup-marked)))))
9616     (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
9617       (if (and (not to-here) (eq 'nnvirtual (car method)))
9618           (nnvirtual-catchup-group
9619            (gnus-group-real-name gnus-newsgroup-name) (nth 1 method) all)))
9620     (gnus-summary-position-cursor)))
9621
9622 (defun gnus-summary-catchup-to-here (&optional all)
9623   "Mark all unticked articles before the current one as read.
9624 If ALL is non-nil, also mark ticked and dormant articles as read."
9625   (interactive)
9626   (gnus-set-global-variables)
9627   (save-excursion
9628     (and (zerop (forward-line -1))
9629          (progn
9630            (end-of-line)
9631            (gnus-summary-catchup all t (point))
9632            (gnus-set-mode-line 'summary))))
9633   (gnus-summary-position-cursor))
9634
9635 (defun gnus-summary-catchup-all (&optional quietly)
9636   "Mark all articles in this newsgroup as read."
9637   (interactive)
9638   (gnus-set-global-variables)
9639   (gnus-summary-catchup t quietly))
9640
9641 (defun gnus-summary-catchup-and-exit (&optional all quietly)
9642   "Mark all articles not marked as unread in this newsgroup as read, then exit.
9643 If prefix argument ALL is non-nil, all articles are marked as read."
9644   (interactive "P")
9645   (gnus-set-global-variables)
9646   (gnus-summary-catchup all quietly nil 'fast)
9647   ;; Select next newsgroup or exit.
9648   (if (eq gnus-auto-select-next 'quietly)
9649       (gnus-summary-next-group nil)
9650     (gnus-summary-exit)))
9651
9652 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
9653   "Mark all articles in this newsgroup as read, and then exit."
9654   (interactive)
9655   (gnus-set-global-variables)
9656   (gnus-summary-catchup-and-exit t quietly))
9657
9658 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
9659 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
9660   "Mark all articles in this group as read and select the next group.
9661 If given a prefix, mark all articles, unread as well as ticked, as
9662 read." 
9663   (interactive "P")
9664   (gnus-set-global-variables)
9665   (gnus-summary-catchup all)
9666   (gnus-summary-next-group))
9667
9668 ;; Thread-based commands.
9669
9670 (defun gnus-summary-toggle-threads (&optional arg)
9671   "Toggle showing conversation threads.
9672 If ARG is positive number, turn showing conversation threads on."
9673   (interactive "P")
9674   (gnus-set-global-variables)
9675   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
9676     (setq gnus-show-threads
9677           (if (null arg) (not gnus-show-threads)
9678             (> (prefix-numeric-value arg) 0)))
9679     (gnus-summary-prepare)
9680     (gnus-summary-goto-subject current)
9681     (gnus-summary-position-cursor)))
9682
9683 (defun gnus-summary-show-all-threads ()
9684   "Show all threads."
9685   (interactive)
9686   (gnus-set-global-variables)
9687   (save-excursion
9688     (let ((buffer-read-only nil))
9689       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
9690   (gnus-summary-position-cursor))
9691
9692 (defun gnus-summary-show-thread ()
9693   "Show thread subtrees.
9694 Returns nil if no thread was there to be shown."
9695   (interactive)
9696   (gnus-set-global-variables)
9697   (let ((buffer-read-only nil)
9698         (orig (point))
9699         ;; first goto end then to beg, to have point at beg after let
9700         (end (progn (end-of-line) (point)))
9701         (beg (progn (beginning-of-line) (point))))
9702     (prog1
9703         ;; Any hidden lines here?
9704         (search-forward "\r" end t)
9705       (subst-char-in-region beg end ?\^M ?\n t)
9706       (goto-char orig)
9707       (gnus-summary-position-cursor))))
9708
9709 (defun gnus-summary-hide-all-threads ()
9710   "Hide all thread subtrees."
9711   (interactive)
9712   (gnus-set-global-variables)
9713   (save-excursion
9714     (goto-char (point-min))
9715     (gnus-summary-hide-thread)
9716     (while (and (not (eobp)) (zerop (forward-line 1)))
9717       (gnus-summary-hide-thread)))
9718   (gnus-summary-position-cursor))
9719
9720 (defun gnus-summary-hide-thread ()
9721   "Hide thread subtrees.
9722 Returns nil if no threads were there to be hidden."
9723   (interactive)
9724   (gnus-set-global-variables)
9725   (let ((buffer-read-only nil)
9726         (start (point))
9727         (level (gnus-summary-thread-level))
9728         (end (point)))
9729     ;; Go forward until either the buffer ends or the subthread
9730     ;; ends. 
9731     (if (eobp)
9732         ()
9733       (while (and (zerop (forward-line 1))
9734                   (> (gnus-summary-thread-level) level))
9735         (setq end (point)))
9736       (prog1
9737           (save-excursion
9738             (goto-char end)
9739             (search-backward "\n" start t))
9740         (subst-char-in-region start end ?\n ?\^M t)
9741         (forward-line -1)))))
9742
9743 (defun gnus-summary-go-to-next-thread (&optional previous)
9744   "Go to the same level (or less) next thread.
9745 If PREVIOUS is non-nil, go to previous thread instead.
9746 Return the article number moved to, or nil if moving was impossible."
9747   (let ((level (gnus-summary-thread-level))
9748         (article (gnus-summary-article-number)))
9749     (if previous 
9750         (while (and (zerop (forward-line -1))
9751                     (> (gnus-summary-thread-level) level)))
9752       (while (and (save-excursion
9753                     (forward-line 1)
9754                     (not (eobp)))
9755                   (zerop (forward-line 1))
9756                   (> (gnus-summary-thread-level) level))))
9757     (gnus-summary-recenter)
9758     (gnus-summary-position-cursor)
9759     (let ((oart (gnus-summary-article-number)))
9760       (and (/= oart article) oart))))
9761
9762 (defun gnus-summary-next-thread (n)
9763   "Go to the same level next N'th thread.
9764 If N is negative, search backward instead.
9765 Returns the difference between N and the number of skips actually
9766 done."
9767   (interactive "p")
9768   (gnus-set-global-variables)
9769   (let ((backward (< n 0))
9770         (n (abs n)))
9771   (while (and (> n 0)
9772               (gnus-summary-go-to-next-thread backward))
9773     (setq n (1- n)))
9774   (gnus-summary-position-cursor)
9775   (if (/= 0 n) (gnus-message 7 "No more threads"))
9776   n))
9777
9778 (defun gnus-summary-prev-thread (n)
9779   "Go to the same level previous N'th thread.
9780 Returns the difference between N and the number of skips actually
9781 done."
9782   (interactive "p")
9783   (gnus-set-global-variables)
9784   (gnus-summary-next-thread (- n)))
9785
9786 (defun gnus-summary-go-down-thread (&optional same)
9787   "Go down one level in the current thread.
9788 If SAME is non-nil, also move to articles of the same level."
9789   (let ((level (gnus-summary-thread-level))
9790         (start (point)))
9791     (if (and (zerop (forward-line 1))
9792              (> (gnus-summary-thread-level) level))
9793         t
9794       (goto-char start)
9795       nil)))
9796
9797 (defun gnus-summary-go-up-thread ()
9798   "Go up one level in the current thread."
9799   (let ((level (gnus-summary-thread-level))
9800         (start (point)))
9801     (while (and (zerop (forward-line -1))
9802                 (>= (gnus-summary-thread-level) level)))
9803     (if (>= (gnus-summary-thread-level) level)
9804         (progn
9805           (goto-char start)
9806           nil)
9807       t)))
9808
9809 (defun gnus-summary-down-thread (n)
9810   "Go down thread N steps.
9811 If N is negative, go up instead.
9812 Returns the difference between N and how many steps down that were
9813 taken."
9814   (interactive "p")
9815   (gnus-set-global-variables)
9816   (let ((up (< n 0))
9817         (n (abs n)))
9818   (while (and (> n 0)
9819               (if up (gnus-summary-go-up-thread)
9820                 (gnus-summary-go-down-thread)))
9821     (setq n (1- n)))
9822   (gnus-summary-position-cursor)
9823   (if (/= 0 n) (gnus-message 7 "Can't go further"))
9824   n))
9825
9826 (defun gnus-summary-up-thread (n)
9827   "Go up thread N steps.
9828 If N is negative, go up instead.
9829 Returns the difference between N and how many steps down that were
9830 taken."
9831   (interactive "p")
9832   (gnus-set-global-variables)
9833   (gnus-summary-down-thread (- n)))
9834
9835 (defun gnus-summary-kill-thread (&optional unmark)
9836   "Mark articles under current thread as read.
9837 If the prefix argument is positive, remove any kinds of marks.
9838 If the prefix argument is negative, tick articles instead."
9839   (interactive "P")
9840   (gnus-set-global-variables)
9841   (if unmark
9842       (setq unmark (prefix-numeric-value unmark)))
9843   (let ((killing t)
9844         (level (gnus-summary-thread-level)))
9845     (save-excursion
9846       (while killing
9847         ;; Mark the article...
9848         (cond ((null unmark) (gnus-summary-mark-article-as-read
9849                                gnus-killed-mark))
9850               ((> unmark 0) (gnus-summary-mark-article-as-unread 
9851                              gnus-unread-mark))
9852               (t (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
9853         ;; ...and go forward until either the buffer ends or the subtree
9854         ;; ends. 
9855         (if (not (and (zerop (forward-line 1))
9856                       (> (gnus-summary-thread-level) level)))
9857             (setq killing nil))))
9858     ;; Hide killed subtrees.
9859     (and (null unmark)
9860          gnus-thread-hide-killed
9861          (gnus-summary-hide-thread))
9862     ;; If marked as read, go to next unread subject.
9863     (if (null unmark)
9864         ;; Go to next unread subject.
9865         (gnus-summary-next-subject 1 t)))
9866   (gnus-set-mode-line 'summary))
9867
9868 ;; Summary sorting commands
9869
9870 (defun gnus-summary-sort-by-number (&optional reverse)
9871   "Sort summary buffer by article number.
9872 Argument REVERSE means reverse order."
9873   (interactive "P")
9874   (gnus-set-global-variables)
9875   (gnus-summary-sort 
9876    ;; `gnus-summary-article-number' is a macro, and `sort-subr' wants
9877    ;; a function, so we wrap it.
9878    (cons (lambda () (gnus-summary-article-number))
9879          'gnus-thread-sort-by-number) reverse))
9880
9881 (defun gnus-summary-sort-by-author (&optional reverse)
9882   "Sort summary buffer by author name alphabetically.
9883 If case-fold-search is non-nil, case of letters is ignored.
9884 Argument REVERSE means reverse order."
9885   (interactive "P")
9886   (gnus-set-global-variables)
9887   (gnus-summary-sort
9888    (cons
9889     (lambda ()
9890       (let ((extract (funcall
9891                       gnus-extract-address-components
9892                       (header-from (gnus-get-header-by-num
9893                                     (gnus-summary-article-number))))))
9894         (or (car extract) (cdr extract))))
9895     'gnus-thread-sort-by-author)
9896    reverse))
9897
9898 (defun gnus-summary-sort-by-subject (&optional reverse)
9899   "Sort summary buffer by subject alphabetically. `Re:'s are ignored.
9900 If case-fold-search is non-nil, case of letters is ignored.
9901 Argument REVERSE means reverse order."
9902   (interactive "P")
9903   (gnus-set-global-variables)
9904   (gnus-summary-sort
9905    (cons
9906     (lambda ()
9907       (downcase (gnus-simplify-subject (gnus-summary-subject-string))))
9908     'gnus-thread-sort-by-subject)
9909    reverse))
9910
9911 (defun gnus-summary-sort-by-date (&optional reverse)
9912   "Sort summary buffer by date.
9913 Argument REVERSE means reverse order."
9914   (interactive "P")
9915   (gnus-set-global-variables)
9916   (gnus-summary-sort
9917    (cons
9918     (lambda ()
9919       (gnus-sortable-date
9920        (header-date 
9921         (gnus-get-header-by-num (gnus-summary-article-number)))))
9922     'gnus-thread-sort-by-date)
9923    reverse))
9924
9925 (defun gnus-summary-sort-by-score (&optional reverse)
9926   "Sort summary buffer by score.
9927 Argument REVERSE means reverse order."
9928   (interactive "P")
9929   (gnus-set-global-variables)
9930   (gnus-summary-sort 
9931    (cons (lambda () (gnus-summary-article-score))
9932          'gnus-thread-sort-by-score)
9933    (not reverse)))
9934
9935 (defvar gnus-summary-already-sorted nil)
9936 (defun gnus-summary-sort (predicate reverse)
9937   ;; Sort summary buffer by PREDICATE.  REVERSE means reverse order. 
9938   (if gnus-summary-already-sorted
9939       ()
9940     (let (buffer-read-only)
9941       (if (not gnus-show-threads)
9942           ;; We do untreaded sorting...
9943           (progn
9944             (goto-char (point-min))
9945             (sort-subr reverse 'forward-line 'end-of-line (car predicate)))
9946         ;; ... or we do threaded sorting.
9947         (let ((gnus-thread-sort-functions (list (cdr predicate)))
9948               (gnus-summary-prepare-hook nil)
9949               (gnus-summary-already-sorted nil))
9950           ;; We do that by simply regenerating the threads.
9951           (gnus-summary-prepare)
9952           (and gnus-show-threads
9953                gnus-thread-hide-subtree
9954                (gnus-summary-hide-all-threads))
9955           ;; If in async mode, we send some info to the backend.
9956           (and gnus-newsgroup-async
9957                (setq gnus-newsgroup-threads (nreverse gnus-newsgroup-threads))
9958                (gnus-request-asynchronous 
9959                 gnus-newsgroup-name
9960                 (if (and gnus-asynchronous-article-function
9961                          (fboundp gnus-asynchronous-article-function))
9962                     (funcall gnus-asynchronous-article-function
9963                              gnus-newsgroup-threads)))))))))
9964
9965   
9966 (defun gnus-sortable-date (date)
9967   "Make sortable string by string-lessp from DATE.
9968 Timezone package is used."
9969   (let* ((date (timezone-fix-time date nil nil)) ;[Y M D H M S]
9970          (year (aref date 0))
9971          (month (aref date 1))
9972          (day (aref date 2)))
9973     (timezone-make-sortable-date 
9974      year month day 
9975      (timezone-make-time-string
9976       (aref date 3) (aref date 4) (aref date 5)))))
9977
9978
9979 ;; Summary saving commands.
9980
9981 (defun gnus-summary-save-article (&optional n)
9982   "Save the current article using the default saver function.
9983 If N is a positive number, save the N next articles.
9984 If N is a negative number, save the N previous articles.
9985 If N is nil and any articles have been marked with the process mark,
9986 save those articles instead.
9987 The variable `gnus-default-article-saver' specifies the saver function."
9988   (interactive "P")
9989   (gnus-set-global-variables)
9990   (let ((articles (gnus-summary-work-articles n)))
9991     (while articles
9992       (let ((header (gnus-get-header-by-num (car articles))))
9993         (if (vectorp header)
9994             (progn
9995               (save-window-excursion
9996                 (gnus-summary-select-article t nil nil (car articles)))
9997               (or gnus-save-all-headers
9998                   (gnus-article-hide-headers t))
9999               ;; Remove any X-Gnus lines.
10000               (save-excursion
10001                 (save-restriction
10002                   (set-buffer gnus-article-buffer)
10003                   (let ((buffer-read-only nil))
10004                     (goto-char (point-min))
10005                     (narrow-to-region (point) (or (search-forward "\n\n" nil t)
10006                                                   (point-max)))
10007                     (while (re-search-forward "^X-Gnus" nil t)
10008                       (beginning-of-line)
10009                       (delete-region (point)
10010                                      (progn (forward-line 1) (point))))
10011                     (widen))))
10012               (save-window-excursion
10013                 (if gnus-default-article-saver
10014                     (funcall gnus-default-article-saver)
10015                   (error "No default saver is defined."))))
10016           (if (assq 'name header)
10017               (gnus-copy-file (cdr (assq 'name header)))
10018             (gnus-message 1 "Article %d is unsaveable" (car articles)))))
10019       (gnus-summary-remove-process-mark (car articles))
10020       (setq articles (cdr articles)))
10021     (gnus-summary-position-cursor)
10022     n))
10023
10024 (defun gnus-summary-pipe-output (&optional arg)
10025   "Pipe the current article to a subprocess.
10026 If N is a positive number, pipe the N next articles.
10027 If N is a negative number, pipe the N previous articles.
10028 If N is nil and any articles have been marked with the process mark,
10029 pipe those articles instead."
10030   (interactive "P")
10031   (gnus-set-global-variables)
10032   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
10033     (gnus-summary-save-article arg)))
10034
10035 (defun gnus-summary-save-article-mail (&optional arg)
10036   "Append the current article to an mail file.
10037 If N is a positive number, save the N next articles.
10038 If N is a negative number, save the N previous articles.
10039 If N is nil and any articles have been marked with the process mark,
10040 save those articles instead."
10041   (interactive "P")
10042   (gnus-set-global-variables)
10043   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10044     (gnus-summary-save-article arg)))
10045
10046 (defun gnus-summary-save-article-rmail (&optional arg)
10047   "Append the current article to an rmail file.
10048 If N is a positive number, save the N next articles.
10049 If N is a negative number, save the N previous articles.
10050 If N is nil and any articles have been marked with the process mark,
10051 save those articles instead."
10052   (interactive "P")
10053   (gnus-set-global-variables)
10054   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10055     (gnus-summary-save-article arg)))
10056
10057 (defun gnus-summary-save-article-file (&optional arg)
10058   "Append the current article to a file.
10059 If N is a positive number, save the N next articles.
10060 If N is a negative number, save the N previous articles.
10061 If N is nil and any articles have been marked with the process mark,
10062 save those articles instead."
10063   (interactive "P")
10064   (gnus-set-global-variables)
10065   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10066     (gnus-summary-save-article arg)))
10067
10068 (defun gnus-read-save-file-name (prompt default-name)
10069   (let ((methods gnus-split-methods)
10070         split-name)
10071     (if (not gnus-split-methods)
10072         ()
10073       (save-excursion
10074         (set-buffer gnus-article-buffer)
10075         (gnus-narrow-to-headers)
10076         (while methods
10077           (goto-char (point-min))
10078           (and (condition-case () 
10079                    (re-search-forward (car (car methods)) nil t)
10080                  (error nil))
10081                (setq split-name (cons (nth 1 (car methods)) split-name)))
10082           (setq methods (cdr methods)))
10083         (widen)))
10084     (cond ((null split-name)
10085            (read-file-name
10086             (concat prompt " (default "
10087                     (file-name-nondirectory default-name) ") ")
10088             (file-name-directory default-name)
10089             default-name))
10090           ((= 1 (length split-name))
10091            (read-file-name
10092             (concat prompt " (default " (car split-name) ") ")
10093             gnus-article-save-directory
10094             (concat gnus-article-save-directory (car split-name))))
10095           (t
10096            (setq split-name (mapcar (lambda (el) (list el))
10097                                     (nreverse split-name)))
10098            (let ((result (completing-read 
10099                           (concat prompt " ")
10100                           split-name nil nil)))
10101              (concat gnus-article-save-directory
10102                      (if (string= result "")
10103                          (car (car split-name))
10104                        result)))))))
10105
10106 (defun gnus-summary-save-in-rmail (&optional filename)
10107   "Append this article to Rmail file.
10108 Optional argument FILENAME specifies file name.
10109 Directory to save to is default to `gnus-article-save-directory' which
10110 is initialized from the SAVEDIR environment variable."
10111   (interactive)
10112   (gnus-set-global-variables)
10113   (let ((default-name
10114           (funcall gnus-rmail-save-name gnus-newsgroup-name
10115                    gnus-current-headers gnus-newsgroup-last-rmail)))
10116     (or filename
10117         (setq filename (gnus-read-save-file-name 
10118                         "Save in rmail file:" default-name)))
10119     (gnus-make-directory (file-name-directory filename))
10120     (gnus-eval-in-buffer-window 
10121      gnus-article-buffer
10122      (save-excursion
10123        (save-restriction
10124          (widen)
10125          (gnus-output-to-rmail filename))))
10126     ;; Remember the directory name to save articles
10127     (setq gnus-newsgroup-last-rmail filename)))
10128
10129 (defun gnus-summary-save-in-mail (&optional filename)
10130   "Append this article to Unix mail file.
10131 Optional argument FILENAME specifies file name.
10132 Directory to save to is default to `gnus-article-save-directory' which
10133 is initialized from the SAVEDIR environment variable."
10134   (interactive)
10135   (gnus-set-global-variables)
10136   (let ((default-name
10137           (funcall gnus-mail-save-name gnus-newsgroup-name
10138                    gnus-current-headers gnus-newsgroup-last-mail)))
10139     (or filename
10140         (setq filename (gnus-read-save-file-name 
10141                         "Save in Unix mail file:" default-name)))
10142     (setq filename
10143           (expand-file-name filename
10144                             (and default-name
10145                                  (file-name-directory default-name))))
10146     (gnus-make-directory (file-name-directory filename))
10147     (gnus-eval-in-buffer-window 
10148      gnus-article-buffer
10149      (save-excursion
10150        (save-restriction
10151          (widen)
10152          (if (and (file-readable-p filename) (rmail-file-p filename))
10153              (gnus-output-to-rmail filename)
10154            (rmail-output filename 1 t t)))))
10155     ;; Remember the directory name to save articles.
10156     (setq gnus-newsgroup-last-mail filename)))
10157
10158 (defun gnus-summary-save-in-file (&optional filename)
10159   "Append this article to file.
10160 Optional argument FILENAME specifies file name.
10161 Directory to save to is default to `gnus-article-save-directory' which
10162 is initialized from the SAVEDIR environment variable."
10163   (interactive)
10164   (gnus-set-global-variables)
10165   (let ((default-name
10166           (funcall gnus-file-save-name gnus-newsgroup-name
10167                    gnus-current-headers gnus-newsgroup-last-file)))
10168     (or filename
10169         (setq filename (gnus-read-save-file-name 
10170                         "Save in file:" default-name)))
10171     (gnus-make-directory (file-name-directory filename))
10172     (gnus-eval-in-buffer-window 
10173      gnus-article-buffer
10174      (save-excursion
10175        (save-restriction
10176          (widen)
10177          (gnus-output-to-file filename))))
10178     ;; Remember the directory name to save articles.
10179     (setq gnus-newsgroup-last-file filename)))
10180
10181 (defun gnus-summary-save-in-pipe (&optional command)
10182   "Pipe this article to subprocess."
10183   (interactive)
10184   (gnus-set-global-variables)
10185   (let ((command (read-string "Shell command on article: "
10186                               gnus-last-shell-command)))
10187     (if (string-equal command "")
10188         (setq command gnus-last-shell-command))
10189     (gnus-eval-in-buffer-window 
10190      gnus-article-buffer
10191      (save-restriction
10192        (widen)
10193        (shell-command-on-region (point-min) (point-max) command nil)))
10194     (setq gnus-last-shell-command command)))
10195
10196 ;; Summary extract commands
10197
10198 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
10199   (let ((buffer-read-only nil)
10200         (article (gnus-summary-article-number))
10201         b)
10202     (or (gnus-summary-goto-subject article)
10203         (error (format "No such article: %d" article)))
10204     (or gnus-newsgroup-headers-hashtb-by-number
10205         (gnus-make-headers-hashtable-by-number))
10206     (gnus-summary-position-cursor)
10207     ;; If all commands are to be bunched up on one line, we collect
10208     ;; them here.  
10209     (if gnus-view-pseudos-separately
10210         ()
10211       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
10212             files action)
10213         (while ps
10214           (setq action (cdr (assq 'action (car ps))))
10215           (setq files (list (cdr (assq 'name (car ps)))))
10216           (while (and ps (cdr ps)
10217                       (string= (or action "1")
10218                                (or (cdr (assq 'action (car (cdr ps)))) "2")))
10219             (setq files (cons (cdr (assq 'name (car (cdr ps)))) files))
10220             (setcdr ps (cdr (cdr ps))))
10221           (if (not files)
10222               ()
10223             (if (not (string-match "%s" action))
10224                 (setq files (cons " " files)))
10225             (setq files (cons " " files))
10226             (and (assq 'execute (car ps))
10227                  (setcdr (assq 'execute (car ps))
10228                          (funcall (if (string-match "%s" action)
10229                                       'format 'concat)
10230                                   action 
10231                                   (mapconcat (lambda (f) f) files " ")))))
10232           (setq ps (cdr ps)))))
10233     (if (and gnus-view-pseudos (not not-view))
10234         (while pslist
10235           (and (assq 'execute (car pslist))
10236                (gnus-execute-command (cdr (assq 'execute (car pslist)))
10237                                      (eq gnus-view-pseudos 'not-confirm)))
10238           (setq pslist (cdr pslist)))
10239       (save-excursion
10240         (while pslist
10241           (gnus-summary-goto-subject (or (cdr (assq 'article (car pslist)))
10242                                          (gnus-summary-article-number)))
10243           (forward-line 1)
10244           (setq b (point))
10245           (insert "          " (file-name-nondirectory 
10246                                 (cdr (assq 'name (car pslist))))
10247                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
10248           (add-text-properties 
10249            b (1+ b) (list 'gnus-number gnus-reffed-article-number
10250                           'gnus-mark gnus-unread-mark 
10251                           'gnus-level 0
10252                           'gnus-pseudo (car pslist)))
10253           (forward-line -1)
10254           (gnus-sethash (int-to-string gnus-reffed-article-number)
10255                         (car pslist) gnus-newsgroup-headers-hashtb-by-number)
10256           (setq gnus-newsgroup-unreads
10257                 (cons gnus-reffed-article-number gnus-newsgroup-unreads))
10258           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
10259           (setq pslist (cdr pslist)))))))
10260
10261 (defun gnus-pseudos< (p1 p2)
10262   (let ((c1 (cdr (assq 'action p1)))
10263         (c2 (cdr (assq 'action p2))))
10264     (and c1 c2 (string< c1 c2))))
10265
10266 (defun gnus-request-pseudo-article (props)
10267   (cond ((assq 'execute props)
10268          (gnus-execute-command (cdr (assq 'execute props)))))
10269   (let ((gnus-current-article (gnus-summary-article-number)))
10270     (run-hooks 'gnus-mark-article-hook)))
10271
10272 (defun gnus-execute-command (command &optional automatic)
10273   (save-excursion
10274     (gnus-article-setup-buffer)
10275     (set-buffer gnus-article-buffer)
10276     (let ((command (if automatic command (read-string "Command: " command)))
10277           (buffer-read-only nil))
10278       (erase-buffer)
10279       (insert "$ " command "\n\n")
10280       (if gnus-view-pseudo-asynchronously
10281           (start-process "gnus-execute" nil "sh" "-c" command)
10282         (call-process "sh" nil t nil "-c" command)))))
10283
10284 (defun gnus-copy-file (file &optional to)
10285   "Copy FILE to TO."
10286   (interactive
10287    (list (read-file-name "Copy file: " default-directory)
10288          (read-file-name "Copy file to: " default-directory)))
10289   (gnus-set-global-variables)
10290   (or to (setq to (read-file-name "Copy file to: " default-directory)))
10291   (and (file-directory-p to) 
10292        (setq to (concat (file-name-as-directory to)
10293                         (file-name-nondirectory file))))
10294   (copy-file file to))
10295
10296 ;; Summary kill commands.
10297
10298 (defun gnus-summary-edit-global-kill (article)
10299   "Edit the \"global\" kill file."
10300   (interactive (list (gnus-summary-article-number)))
10301   (gnus-set-global-variables)
10302   (gnus-group-edit-global-kill article))
10303
10304 (defun gnus-summary-edit-local-kill ()
10305   "Edit a local kill file applied to the current newsgroup."
10306   (interactive)
10307   (gnus-set-global-variables)
10308   (setq gnus-current-headers 
10309         (gnus-gethash 
10310          (int-to-string (gnus-summary-article-number))
10311          gnus-newsgroup-headers-hashtb-by-number))
10312   (gnus-set-global-variables)
10313   (gnus-group-edit-local-kill 
10314    (gnus-summary-article-number) gnus-newsgroup-name))
10315
10316 \f
10317 ;;;
10318 ;;; Gnus article mode
10319 ;;;
10320
10321 (put 'gnus-article-mode 'mode-class 'special)
10322
10323 (if gnus-article-mode-map
10324     nil
10325   (setq gnus-article-mode-map (make-keymap))
10326   (suppress-keymap gnus-article-mode-map)
10327   (define-key gnus-article-mode-map " " 'gnus-article-next-page)
10328   (define-key gnus-article-mode-map "\177" 'gnus-article-prev-page)
10329   (define-key gnus-article-mode-map "\C-c^" 'gnus-article-refer-article)
10330   (define-key gnus-article-mode-map "h" 'gnus-article-show-summary)
10331   (define-key gnus-article-mode-map "s" 'gnus-article-show-summary)
10332   (define-key gnus-article-mode-map "\C-c\C-m" 'gnus-article-mail)
10333   (define-key gnus-article-mode-map "\C-c\C-M" 'gnus-article-mail-with-original)
10334   (define-key gnus-article-mode-map "?" 'gnus-article-describe-briefly)
10335   (define-key gnus-article-mode-map gnus-mouse-2 'gnus-article-push-button)
10336   (define-key gnus-article-mode-map "\r" 'gnus-article-press-button)
10337   (define-key gnus-article-mode-map "\t" 'gnus-article-next-button)
10338   (define-key gnus-article-mode-map "\C-c\C-b" 'gnus-bug)
10339   
10340   ;; Duplicate almost all summary keystrokes in the article mode map.
10341   (let ((commands 
10342          (list 
10343           " " "\177" "p" "N" "P" "\M-\C-n" "\M-\C-p"
10344           "\M-n" "\M-p" "." "," "\M-s" "\M-r" "<" ">" "j" "^" "\M-^"
10345           "u" "!" "U" "d" "D" "E" "\M-u" "\M-U" "k" "\C-k" "\M-\C-k"
10346           "\M-\C-l" "e" "#" "\M-#" "\M-\C-t" "\M-\C-s" "\M-\C-h"
10347           "\M-\C-f" "\M-\C-b" "\M-\C-u" "\M-\C-d" "&" "\C-w"
10348           "\C-t" "?" "\C-c\M-\C-s" "\C-c\C-s\C-n" "\C-c\C-s\C-a"
10349           "\C-c\C-s\C-s" "\C-c\C-s\C-d" "\C-c\C-s\C-i" "\C-x\C-s"
10350           "\M-g" "w" "\C-c\C-r" "\M-t" "C"
10351           "o" "\C-o" "|" "\M-k" "\M-K" "V" "\C-c\C-d"
10352           "\C-c\C-i" "x" "X" "t" "g" "?" "l"
10353           "\C-c\C-v\C-v" "\C-d" "v" 
10354 ;;        "Mt" "M!" "Md" "Mr"
10355 ;;        "Mc" "M " "Me" "Mx" "M?" "Mb" "MB" "M#" "M\M-#" "M\M-r"
10356 ;;        "M\M-\C-r" "MD" "M\M-D" "MS" "MC" "MH" "M\C-c" "Mk" "MK"
10357 ;;        "Ms" "Mc" "Mu" "Mm" "Mk" "Gn" "Gp" "GN" "GP" "G\C-n" "G\C-p"
10358 ;;        "G\M-n" "G\M-p" "Gf" "Gb" "Gg" "Gl" "Gp" "Tk" "Tl" "Ti" "TT"
10359 ;;        "Ts" "TS" "Th" "TH" "Tn" "Tp" "Tu" "Td" "T#" "A " "An" "A\177" "Ap"
10360 ;;        "A\r" "A<" "A>" "Ab" "Ae" "A^" "Ar" "Aw" "Ac" "Ag" "At" "Am"
10361 ;;        "As" "Wh" "Ws" "Wc" "Wo" "Ww" "Wd" "Wq" "Wf" "Wt" "W\C-t"
10362 ;;        "WT" "WA" "Wa" "WH" "WC" "WS" "Wb" "Hv" "Hf" "Hd" "Hh" "Hi"
10363 ;;        "Be" "B\177" "Bm" "Br" "Bw" "Bc" "Bq" "Bi" "Oo" "Om" "Or"
10364 ;;        "Of" "Oh" "Ov" "Op" "Vu" "V\C-s" "V\C-r" "Vr" "V&" "VT" "Ve"
10365 ;;        "VD" "Vk" "VK" "Vsn" "Vsa" "Vss" "Vsd" "Vsi"
10366           )))
10367     (while commands
10368       (define-key gnus-article-mode-map (car commands) 
10369         'gnus-article-summary-command)
10370       (setq commands (cdr commands))))
10371
10372   (let ((commands (list "q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
10373 ;;                      "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP" 
10374                          "=" "n")))
10375     (while commands
10376       (define-key gnus-article-mode-map (car commands) 
10377         'gnus-article-summary-command-nosave)
10378       (setq commands (cdr commands)))))
10379
10380
10381 (defun gnus-article-mode ()
10382   "Major mode for displaying an article.
10383
10384 All normal editing commands are switched off.
10385
10386 The following commands are available:
10387
10388 \\<gnus-article-mode-map>
10389 \\[gnus-article-next-page]\t Scroll the article one page forwards
10390 \\[gnus-article-prev-page]\t Scroll the article one page backwards
10391 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
10392 \\[gnus-article-show-summary]\t Display the summary buffer
10393 \\[gnus-article-mail]\t Send a reply to the address near point
10394 \\[gnus-article-mail-with-original]\t Send a reply to the address near point; include the original article
10395 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
10396 \\[gnus-info-find-node]\t Go to the Gnus info node"
10397   (interactive)
10398   (if gnus-visual (gnus-article-make-menu-bar))
10399   (kill-all-local-variables)
10400   (setq mode-line-modified "-- ")
10401   (make-local-variable 'mode-line-format)
10402   (setq mode-line-format (copy-sequence mode-line-format))
10403   (and (equal (nth 3 mode-line-format) "   ")
10404        (setcar (nthcdr 3 mode-line-format) ""))
10405   (setq mode-name "Article")
10406   (setq major-mode 'gnus-article-mode)
10407   (make-local-variable 'minor-mode-alist)
10408   (or (assq 'gnus-show-mime minor-mode-alist)
10409       (setq minor-mode-alist
10410             (cons (list 'gnus-show-mime " MIME") minor-mode-alist)))
10411   (use-local-map gnus-article-mode-map)
10412   (make-local-variable 'page-delimiter)
10413   (setq page-delimiter gnus-page-delimiter)
10414   (buffer-disable-undo (current-buffer))
10415   (setq buffer-read-only t)             ;Disable modification
10416   (run-hooks 'gnus-article-mode-hook))
10417
10418 (defun gnus-article-setup-buffer ()
10419   "Initialize article mode buffer."
10420   (if (get-buffer gnus-article-buffer)
10421       (save-excursion
10422         (set-buffer gnus-article-buffer)
10423         (buffer-disable-undo (current-buffer))
10424         (setq buffer-read-only t)
10425         (gnus-add-current-to-buffer-list)
10426         (or (eq major-mode 'gnus-article-mode)
10427             (gnus-article-mode)))
10428     (save-excursion
10429       (set-buffer (get-buffer-create gnus-article-buffer))
10430       (gnus-add-current-to-buffer-list)
10431       (gnus-article-mode))))
10432
10433 ;; Set article window start at LINE, where LINE is the number of lines
10434 ;; from the head of the article.
10435 (defun gnus-article-set-window-start (&optional line)
10436   (set-window-start 
10437    (get-buffer-window gnus-article-buffer)
10438    (save-excursion
10439      (set-buffer gnus-article-buffer)
10440      (goto-char (point-min))
10441      (if (not line)
10442          (point-min)
10443        (gnus-message 6 "Moved to bookmark")
10444        (search-forward "\n\n" nil t)
10445        (forward-line line)
10446        (point)))))
10447
10448 (defun gnus-request-article-this-buffer (article group)
10449   "Get an article and insert it into this buffer."
10450   (setq group (or group gnus-newsgroup-name))
10451
10452   ;; Open server if it has closed.
10453   (gnus-check-news-server (gnus-find-method-for-group group))
10454
10455   ;; Using `gnus-request-article' directly will insert the article into
10456   ;; `nntp-server-buffer' - so we'll save some time by not having to
10457   ;; copy it from the server buffer into the article buffer.
10458
10459   ;; We only request an article by message-id when we do not have the
10460   ;; headers for it, so we'll have to get those.
10461   (and (stringp article) 
10462        (let ((gnus-override-method gnus-refer-article-method))
10463          (gnus-read-header article)))
10464
10465   ;; If the article number is negative, that means that this article
10466   ;; doesn't belong in this newsgroup (possibly), so we find its
10467   ;; message-id and request it by id instead of number.
10468   (if (not (numberp article))
10469       ()
10470     (save-excursion
10471       (set-buffer gnus-summary-buffer)
10472       (let ((header (gnus-get-header-by-num article)))
10473         (if (< article 0)
10474             (if (vectorp header)
10475                 ;; It's a real article.
10476                 (setq article (header-id header))
10477               ;; It is an extracted pseudo-article.
10478               (setq article 'pseudo)
10479               (gnus-request-pseudo-article header)))
10480
10481         (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
10482           (if (not (eq (car method) 'nneething))
10483               ()
10484             (let ((dir (concat (file-name-as-directory (nth 1 method))
10485                                (header-subject header))))
10486               (if (file-directory-p dir)
10487                   (progn
10488                     (setq article 'nneething)
10489                     (gnus-group-enter-directory dir)))))))))
10490
10491   ;; Check the cache.
10492   (if (and gnus-use-cache
10493            (numberp article)
10494            (gnus-cache-request-article article group))
10495       'article
10496     ;; Get the article and into the article buffer.
10497     (if (or (stringp article) (numberp article))
10498         (progn
10499           (erase-buffer)
10500           (let ((gnus-override-method 
10501                  (and (stringp article) gnus-refer-article-method)))
10502             (and (gnus-request-article article group (current-buffer))
10503                  'article)))
10504       article)))
10505
10506 (defun gnus-read-header (id)
10507   "Read the headers of article ID and enter them into the Gnus system."
10508   (or gnus-newsgroup-headers-hashtb-by-number
10509       (gnus-make-headers-hashtable-by-number))
10510   (let (header)
10511     (if (not (setq header 
10512                    (car (if (let ((gnus-nov-is-evil t))
10513                               (gnus-retrieve-headers 
10514                                (list id) gnus-newsgroup-name))
10515                             (gnus-get-newsgroup-headers)))))
10516         nil
10517       (if (stringp id)
10518           (header-set-number header gnus-reffed-article-number))
10519       (setq gnus-newsgroup-headers (cons header gnus-newsgroup-headers))
10520       (gnus-sethash (int-to-string (header-number header)) header
10521                     gnus-newsgroup-headers-hashtb-by-number)
10522       (if (stringp id)
10523           (setq gnus-reffed-article-number (1- gnus-reffed-article-number)))
10524       (setq gnus-current-headers header)
10525       header)))
10526
10527 (defun gnus-article-prepare (article &optional all-headers header)
10528   "Prepare ARTICLE in article mode buffer.
10529 ARTICLE should either be an article number or a Message-ID.
10530 If ARTICLE is an id, HEADER should be the article headers.
10531 If ALL-HEADERS is non-nil, no headers are hidden."
10532   (save-excursion
10533     ;; Make sure we start in a summary buffer.
10534     (or (eq major-mode 'gnus-summary-mode)
10535         (set-buffer gnus-summary-buffer))
10536     (setq gnus-summary-buffer (current-buffer))
10537     ;; Make sure the connection to the server is alive.
10538     (or (gnus-server-opened (gnus-find-method-for-group gnus-newsgroup-name))
10539         (progn
10540           (gnus-check-news-server 
10541            (gnus-find-method-for-group gnus-newsgroup-name))
10542           (gnus-request-group gnus-newsgroup-name t)))
10543     (or gnus-newsgroup-headers-hashtb-by-number
10544         (gnus-make-headers-hashtable-by-number))
10545     (let* ((article (if header (header-number header) article))
10546            (summary-buffer (current-buffer))
10547            (internal-hook gnus-article-internal-prepare-hook)
10548            (group gnus-newsgroup-name)
10549            result)
10550       (save-excursion
10551         (gnus-article-setup-buffer)
10552         (set-buffer gnus-article-buffer)
10553         (if (not (setq result (let ((buffer-read-only nil))
10554                                 (gnus-request-article-this-buffer 
10555                                  article group))))
10556             ;; There is no such article.
10557             (save-excursion
10558               (if (not (numberp article))
10559                   ()
10560                 (setq gnus-article-current 
10561                       (cons gnus-newsgroup-name article))
10562                 (set-buffer gnus-summary-buffer)
10563                 (setq gnus-current-article article)
10564                 (gnus-summary-mark-article article gnus-canceled-mark))
10565               (gnus-message 1 "No such article (may be canceled)")
10566               (ding)
10567               nil)
10568           (if (or (eq result 'pseudo) (eq result 'nneething))
10569               (progn
10570                 (save-excursion
10571                   (set-buffer summary-buffer)
10572                   (setq gnus-last-article gnus-current-article
10573                         gnus-newsgroup-history (cons gnus-current-article
10574                                                      gnus-newsgroup-history)
10575                         gnus-current-article 0
10576                         gnus-current-headers nil
10577                         gnus-article-current nil)
10578                   (if (eq result 'nneething)
10579                       (gnus-configure-windows 'summary)
10580                     (gnus-configure-windows 'article))
10581                   (gnus-set-global-variables))
10582                 (gnus-set-mode-line 'article))
10583             ;; The result from the `request' was an actual article -
10584             ;; or at least some text that is now displayed in the
10585             ;; article buffer.
10586             (if (and (numberp article)
10587                      (not (eq article gnus-current-article)))
10588                 ;; Seems like a new article has been selected.
10589                 ;; `gnus-current-article' must be an article number.
10590                 (save-excursion
10591                   (set-buffer summary-buffer)
10592                   (setq gnus-last-article gnus-current-article
10593                         gnus-newsgroup-history (cons gnus-current-article
10594                                                      gnus-newsgroup-history)
10595                         gnus-current-article article
10596                         gnus-current-headers 
10597                         (gnus-get-header-by-num gnus-current-article)
10598                         gnus-article-current 
10599                         (cons gnus-newsgroup-name gnus-current-article))
10600                   (gnus-summary-show-thread)
10601                   (run-hooks 'gnus-mark-article-hook)
10602                   (gnus-set-mode-line 'summary)
10603                   (and gnus-visual 
10604                        (run-hooks 'gnus-visual-mark-article-hook))
10605                   ;; Set the global newsgroup variables here.
10606                   ;; Suggested by Jim Sisolak
10607                   ;; <sisolak@trans4.neep.wisc.edu>.
10608                   (gnus-set-global-variables)
10609                   (setq gnus-have-all-headers 
10610                         (or all-headers gnus-show-all-headers))
10611                   (and gnus-use-cache 
10612                        (vectorp (gnus-get-header-by-number article))
10613                        (gnus-cache-possibly-enter-article
10614                         group article
10615                         (gnus-get-header-by-number article)
10616                         (memq article gnus-newsgroup-marked)
10617                         (memq article gnus-newsgroup-dormant)
10618                         (memq article gnus-newsgroup-unreads)))))
10619             ;; Hooks for getting information from the article.
10620             ;; This hook must be called before being narrowed.
10621             (let (buffer-read-only)
10622               (run-hooks 'internal-hook)
10623               (run-hooks 'gnus-article-prepare-hook)
10624               ;; Decode MIME message.
10625               (if (and gnus-show-mime
10626                        (or (not gnus-strict-mime)
10627                            (gnus-fetch-field "Mime-Version")))
10628                   (funcall gnus-show-mime-method))
10629               ;; Perform the article display hooks.
10630               (run-hooks 'gnus-article-display-hook))
10631             ;; Do page break.
10632             (goto-char (point-min))
10633             (and gnus-break-pages (gnus-narrow-to-page))
10634             (gnus-set-mode-line 'article)
10635             (gnus-configure-windows 'article)
10636             (goto-char (point-min))
10637             t))))))
10638
10639 (defun gnus-article-show-all-headers ()
10640   "Show all article headers in article mode buffer."
10641   (save-excursion 
10642     (gnus-article-setup-buffer)
10643     (set-buffer gnus-article-buffer)
10644     (let ((buffer-read-only nil))
10645       (remove-text-properties (point-min) (point-max) 
10646                               gnus-hidden-properties))))
10647
10648 (defun gnus-article-hide-headers-if-wanted ()
10649   "Hide unwanted headers if `gnus-have-all-headers' is nil.
10650 Provided for backwards compatability."
10651   (or (save-excursion (set-buffer gnus-summary-buffer) gnus-have-all-headers)
10652       (gnus-article-hide-headers)))
10653
10654 (defun gnus-article-hide-headers (&optional delete)
10655   "Hide unwanted headers and possibly sort them as well."
10656   (interactive "P")
10657   (save-excursion
10658     (set-buffer gnus-article-buffer)
10659     (save-restriction
10660       (let ((sorted gnus-sorted-header-list)
10661             (buffer-read-only nil)
10662             want-list beg want-l)
10663         ;; First we narrow to just the headers.
10664         (widen)
10665         (goto-char (point-min))
10666         ;; Hide any "From " lines at the beginning of (mail) articles. 
10667         (while (looking-at "From ")
10668           (forward-line 1))
10669         (if (bobp) 
10670             (add-text-properties (point-min) (point) gnus-hidden-properties))
10671         ;; Then treat the rest of the header lines.
10672         (narrow-to-region 
10673          (point) 
10674          (progn (search-forward "\n\n" nil t) (forward-line -1) (point)))
10675         ;; Then we use the two regular expressions
10676         ;; `gnus-ignored-headers' and `gnus-visible-headers' to
10677         ;; select which header lines is to remain visible in the
10678         ;; article buffer.
10679         (goto-char (point-min))
10680         (while (re-search-forward "^[^ \t]*:" nil t)
10681           (beginning-of-line)
10682           ;; We add the headers we want to keep to a list and delete
10683           ;; them from the buffer.
10684           (if (or (and (stringp gnus-visible-headers)
10685                        (looking-at gnus-visible-headers))
10686                   (and (not (stringp gnus-visible-headers))
10687                        (stringp gnus-ignored-headers)
10688                        (not (looking-at gnus-ignored-headers))))
10689               (progn
10690                 (setq beg (point))
10691                 (forward-line 1)
10692                 ;; Be sure to get multi-line headers...
10693                 (re-search-forward "^[^ \t]*:" nil t)
10694                 (beginning-of-line)
10695                 (setq want-list 
10696                       (cons (buffer-substring beg (point)) want-list))
10697                 (delete-region beg (point))
10698                 (goto-char beg))
10699             (forward-line 1)))
10700         ;; Next we perform the sorting by looking at
10701         ;; `gnus-sorted-header-list'. 
10702         (goto-char (point-min))
10703         (while (and sorted want-list)
10704           (setq want-l want-list)
10705           (while (and want-l
10706                       (not (string-match (car sorted) (car want-l))))
10707             (setq want-l (cdr want-l)))
10708           (if want-l 
10709               (progn
10710                 (insert (car want-l))
10711                 (setq want-list (delq (car want-l) want-list))))
10712           (setq sorted (cdr sorted)))
10713         ;; Any headers that were not matched by the sorted list we
10714         ;; just tack on the end of the visible header list.
10715         (while want-list
10716           (insert (car want-list))
10717           (setq want-list (cdr want-list)))
10718         ;; And finally we make the unwanted headers invisible.
10719         (if delete
10720             (delete-region (point) (point-max))
10721           ;; Suggested by Sudish Joseph <joseph@cis.ohio-state.edu>.
10722           (add-text-properties (point) (point-max) gnus-hidden-properties))))))
10723
10724 ;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.
10725 (defun gnus-article-treat-overstrike ()
10726   "Translate overstrikes into bold text."
10727   (interactive)
10728   (save-excursion
10729     (set-buffer gnus-article-buffer)
10730     (let ((buffer-read-only nil))
10731       (while (search-forward "\b" nil t)
10732         (let ((next (following-char))
10733               (previous (char-after (- (point) 2))))
10734           (cond ((eq next previous)
10735                  (delete-region (- (point) 2) (point))
10736                  (put-text-property (point) (1+ (point))
10737                                     'face 'bold))
10738                 ((eq next ?_)
10739                  (delete-region (1- (point)) (1+ (point)))
10740                  (put-text-property (1- (point)) (point)
10741                                     'face 'underline))
10742                 ((eq previous ?_)
10743                  (delete-region (- (point) 2) (point))
10744                  (put-text-property (point) (1+ (point))
10745                                     'face 'underline))))))))
10746
10747 (defun gnus-article-word-wrap ()
10748   "Format too long lines."
10749   (interactive)
10750   (save-excursion
10751     (set-buffer gnus-article-buffer)
10752     (let ((buffer-read-only nil))
10753       (goto-char (point-min))
10754       (search-forward "\n\n" nil t)
10755       (end-of-line 1)
10756       (let ((paragraph-start "^[>|#:<;* ]*[ \t]*$")
10757             (adaptive-fill-regexp "[ \t]*\\([|#:<;>*]+ *\\)?")
10758             (adaptive-fill-mode t))
10759         (while (not (eobp))
10760           (and (>= (current-column) (min fill-column (window-width)))
10761                (/= (preceding-char) ?:)
10762                (fill-paragraph nil))
10763           (end-of-line 2))))))
10764
10765 (defun gnus-article-remove-cr ()
10766   "Remove carriage returns from an article."
10767   (interactive)
10768   (save-excursion
10769     (set-buffer gnus-article-buffer)
10770     (let ((buffer-read-only nil))
10771       (goto-char (point-min))
10772       (while (search-forward "\r" nil t)
10773         (replace-match "" t t)))))
10774
10775 (defun gnus-article-display-x-face (&optional force)
10776   "Look for an X-Face header and display it if present."
10777   (interactive (list 'force))
10778   (save-excursion
10779     (set-buffer gnus-article-buffer)
10780     (let ((inhibit-point-motion-hooks t)
10781           (case-fold-search nil))
10782       (save-restriction
10783         (goto-char (point-min))
10784         (search-forward "\n\n")
10785         (narrow-to-region (point-min) (point))
10786         (goto-char (point-min))
10787         (if (or (not gnus-article-x-face-command)
10788                 (and (not force)
10789                      (or (not gnus-article-x-face-too-ugly)
10790                          (string-match gnus-article-x-face-too-ugly
10791                                        (mail-fetch-field "from"))))
10792                 (progn
10793                   (goto-char (point-min))
10794                   (not (re-search-forward "^X-Face: " nil t))))
10795             nil
10796           (let ((beg (point))
10797                 (end (1- (re-search-forward "^\\($\\|[^ \t]\\)" nil t))))
10798             (if (symbolp gnus-article-x-face-command)
10799                 (and (or (fboundp gnus-article-x-face-command)
10800                          (error "%s is not a function"
10801                                 gnus-article-x-face-command))
10802                      (funcall gnus-article-x-face-command beg end))
10803               (call-process-region beg end "sh" nil 0 nil
10804                                    "-c" gnus-article-x-face-command))))))))
10805
10806 (defun gnus-article-de-quoted-unreadable (&optional force)
10807   "Do a naïve translation of a quoted-printable-encoded article.
10808 This is in no way, shape or form meant as a replacement for real MIME
10809 processing, but is simply a stop-gap measure until MIME support is
10810 written.
10811 If FORCE, decode the article whether it is marked as quoted-printable
10812 or not." 
10813   (interactive (list 'force))
10814   (save-excursion
10815     (set-buffer gnus-article-buffer)
10816     (let ((case-fold-search t)
10817           (buffer-read-only nil)
10818           (type (gnus-fetch-field "content-transfer-encoding")))
10819       (if (or force (and type (string-match "quoted-printable" type)))
10820           (progn
10821             (goto-char (point-min))
10822             (search-forward "\n\n" nil 'move)
10823             (gnus-mime-decode-quoted-printable (point) (point-max)))))))
10824
10825 (defun gnus-mime-decode-quoted-printable (from to)
10826   ;; Decode quoted-printable from region between FROM and TO.
10827   (save-excursion
10828     (goto-char from)
10829     (while (search-forward "=" to t)
10830       (cond ((eq (following-char) ?\n)
10831              (delete-char -1)
10832              (delete-char 1))
10833             ((looking-at "[0-9A-F][0-9A-F]")
10834              (delete-char -1)
10835              (insert (hexl-hex-string-to-integer
10836                       (buffer-substring (point) (+ 2 (point)))))
10837              (delete-char 2))
10838             ((gnus-message 3 "Malformed MIME quoted-printable message"))))))
10839
10840 (defvar gnus-article-time-units
10841   (list (cons 'year (* 365.25 24 60 60))
10842         (cons 'week (* 7 24 60 60))
10843         (cons 'day (* 24 60 60))
10844         (cons 'hour (* 60 60))
10845         (cons 'minute 60)
10846         (cons 'second 1)))
10847
10848 (defun gnus-article-date-ut (&optional type)
10849   "Convert DATE date to universal time in the current article.
10850 If TYPE is `local', convert to local time; if it is `lapsed', output
10851 how much time has lapsed since DATE."
10852   (interactive (list 'ut))
10853   (let ((date (header-date (or gnus-current-headers 
10854                                (gnus-get-header-by-number
10855                                 (gnus-summary-article-number))"")))
10856         (date-regexp "^Date: \\|^X-Sent: "))
10857     (if (or (not date)
10858             (string= date ""))
10859         ()
10860       (save-excursion
10861         (set-buffer gnus-article-buffer)
10862         (let ((buffer-read-only nil))
10863           (goto-char (point-min))
10864           (if (and (re-search-forward date-regexp nil t)
10865                    (progn 
10866                      (beginning-of-line)
10867                      (looking-at date-regexp)))
10868               (delete-region (gnus-point-at-bol)
10869                              (progn (end-of-line) (1+ (point))))
10870             (goto-char (point-min))
10871             (goto-char (- (search-forward "\n\n") 2)))
10872           (insert
10873            (cond 
10874             ((eq type 'local)
10875              (concat "Date: " (condition-case ()
10876                                   (timezone-make-date-arpa-standard date)
10877                                 (error date))
10878                      "\n"))
10879             ((eq type 'ut)
10880              (concat "Date: "
10881                      (condition-case ()
10882                          (timezone-make-date-arpa-standard date nil "UT")
10883                        (error date))
10884                      "\n"))
10885             ((eq type 'lapsed)
10886              ;; If the date is seriously mangled, the timezone
10887              ;; functions are liable to bug out, so we condition-case
10888              ;; the entire thing.  
10889              (let* ((real-sec (condition-case ()
10890                                   (- (gnus-seconds-since-epoch 
10891                                       (timezone-make-date-arpa-standard
10892                                        (current-time-string) 
10893                                        (current-time-zone) "UT"))
10894                                      (gnus-seconds-since-epoch 
10895                                       (timezone-make-date-arpa-standard 
10896                                        date nil "UT")))
10897                                 (error 0)))
10898                     (sec (abs real-sec))
10899                     num prev)
10900                (if (zerop sec)
10901                    "X-Sent: Now\n"
10902                  (concat
10903                   "X-Sent: "
10904                   (mapconcat 
10905                    (lambda (unit)
10906                      (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
10907                          ""
10908                        (setq sec (- sec (* num (cdr unit))))
10909                        (prog1
10910                            (concat (if prev ", " "") (int-to-string 
10911                                                       (floor num))
10912                                    " " (symbol-name (car unit))
10913                                    (if (> num 1) "s" ""))
10914                          (setq prev t))))
10915                    gnus-article-time-units "")
10916                   (if (> real-sec 0)
10917                       " ago\n"
10918                     " in the future\n")))))
10919             (t
10920              (error "Unknown conversion type: %s" type)))))))))
10921
10922 (defun gnus-article-date-local ()
10923   "Convert the current article date to the local timezone."
10924   (interactive)
10925   (gnus-article-date-ut 'local))
10926
10927 (defun gnus-article-date-lapsed ()
10928   "Convert the current article date to time lapsed since it was sent."
10929   (interactive)
10930   (gnus-article-date-ut 'lapsed))
10931
10932 (defun gnus-article-maybe-highlight ()
10933   (if gnus-visual (gnus-article-highlight)))
10934
10935 ;; Article savers.
10936
10937 (defun gnus-output-to-rmail (file-name)
10938   "Append the current article to an Rmail file named FILE-NAME."
10939   (require 'rmail)
10940   ;; Most of these codes are borrowed from rmailout.el.
10941   (setq file-name (expand-file-name file-name))
10942   (setq rmail-default-rmail-file file-name)
10943   (let ((artbuf (current-buffer))
10944         (tmpbuf (get-buffer-create " *Gnus-output*")))
10945     (save-excursion
10946       (or (get-file-buffer file-name)
10947           (file-exists-p file-name)
10948           (if (gnus-yes-or-no-p
10949                (concat "\"" file-name "\" does not exist, create it? "))
10950               (let ((file-buffer (create-file-buffer file-name)))
10951                 (save-excursion
10952                   (set-buffer file-buffer)
10953                   (rmail-insert-rmail-file-header)
10954                   (let ((require-final-newline nil))
10955                     (write-region (point-min) (point-max) file-name t 1)))
10956                 (kill-buffer file-buffer))
10957             (error "Output file does not exist")))
10958       (set-buffer tmpbuf)
10959       (buffer-disable-undo (current-buffer))
10960       (erase-buffer)
10961       (insert-buffer-substring artbuf)
10962       (gnus-convert-article-to-rmail)
10963       ;; Decide whether to append to a file or to an Emacs buffer.
10964       (let ((outbuf (get-file-buffer file-name)))
10965         (if (not outbuf)
10966             (append-to-file (point-min) (point-max) file-name)
10967           ;; File has been visited, in buffer OUTBUF.
10968           (set-buffer outbuf)
10969           (let ((buffer-read-only nil)
10970                 (msg (and (boundp 'rmail-current-message)
10971                           (symbol-value 'rmail-current-message))))
10972             ;; If MSG is non-nil, buffer is in RMAIL mode.
10973             (if msg
10974                 (progn (widen)
10975                        (narrow-to-region (point-max) (point-max))))
10976             (insert-buffer-substring tmpbuf)
10977             (if msg
10978                 (progn
10979                   (goto-char (point-min))
10980                   (widen)
10981                   (search-backward "\^_")
10982                   (narrow-to-region (point) (point-max))
10983                   (goto-char (1+ (point-min)))
10984                   (rmail-count-new-messages t)
10985                   (rmail-show-message msg)))))))
10986     (kill-buffer tmpbuf)))
10987
10988 (defun gnus-output-to-file (file-name)
10989   "Append the current article to a file named FILE-NAME."
10990   (setq file-name (expand-file-name file-name))
10991   (let ((artbuf (current-buffer))
10992         (tmpbuf (get-buffer-create " *Gnus-output*")))
10993     (save-excursion
10994       (set-buffer tmpbuf)
10995       (buffer-disable-undo (current-buffer))
10996       (erase-buffer)
10997       (insert-buffer-substring artbuf)
10998       ;; Append newline at end of the buffer as separator, and then
10999       ;; save it to file.
11000       (goto-char (point-max))
11001       (insert "\n")
11002       (append-to-file (point-min) (point-max) file-name))
11003     (kill-buffer tmpbuf)))
11004
11005 (defun gnus-convert-article-to-rmail ()
11006   "Convert article in current buffer to Rmail message format."
11007   (let ((buffer-read-only nil))
11008     ;; Convert article directly into Babyl format.
11009     ;; Suggested by Rob Austein <sra@lcs.mit.edu>
11010     (goto-char (point-min))
11011     (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
11012     (while (search-forward "\n\^_" nil t) ;single char
11013       (replace-match "\n^_" t t))               ;2 chars: "^" and "_"
11014     (goto-char (point-max))
11015     (insert "\^_")))
11016
11017 (defun gnus-narrow-to-page (&optional arg)
11018   "Make text outside current page invisible except for page delimiter.
11019 A numeric arg specifies to move forward or backward by that many pages,
11020 thus showing a page other than the one point was originally in."
11021   (interactive "P")
11022   (setq arg (if arg (prefix-numeric-value arg) 0))
11023   (save-excursion
11024     (forward-page -1)                   ;Beginning of current page.
11025     (widen)
11026     (if (> arg 0)
11027         (forward-page arg)
11028       (if (< arg 0)
11029           (forward-page (1- arg))))
11030     ;; Find the end of the page.
11031     (forward-page)
11032     ;; If we stopped due to end of buffer, stay there.
11033     ;; If we stopped after a page delimiter, put end of restriction
11034     ;; at the beginning of that line.
11035     ;; These are commented out.
11036     ;;    (if (save-excursion (beginning-of-line)
11037     ;;                  (looking-at page-delimiter))
11038     ;;  (beginning-of-line))
11039     (narrow-to-region (point)
11040                       (progn
11041                         ;; Find the top of the page.
11042                         (forward-page -1)
11043                         ;; If we found beginning of buffer, stay there.
11044                         ;; If extra text follows page delimiter on same line,
11045                         ;; include it.
11046                         ;; Otherwise, show text starting with following line.
11047                         (if (and (eolp) (not (bobp)))
11048                             (forward-line 1))
11049                         (point)))))
11050
11051 (defun gnus-gmt-to-local ()
11052   "Rewrite Date header described in GMT to local in current buffer.
11053 Intended to be used with gnus-article-prepare-hook."
11054   (save-excursion
11055     (save-restriction
11056       (widen)
11057       (goto-char (point-min))
11058       (narrow-to-region (point-min)
11059                         (progn (search-forward "\n\n" nil 'move) (point)))
11060       (goto-char (point-min))
11061       (if (re-search-forward "^Date:[ \t]\\(.*\\)$" nil t)
11062           (let ((buffer-read-only nil)
11063                 (date (buffer-substring-no-properties
11064                        (match-beginning 1) (match-end 1))))
11065             (delete-region (match-beginning 1) (match-end 1))
11066             (insert
11067              (timezone-make-date-arpa-standard 
11068               date nil (current-time-zone))))))))
11069
11070
11071 ;; Article mode commands
11072
11073 (defun gnus-article-next-page (&optional lines)
11074   "Show next page of current article.
11075 If end of article, return non-nil. Otherwise return nil.
11076 Argument LINES specifies lines to be scrolled up."
11077   (interactive "P")
11078   (move-to-window-line -1)
11079   ;; Fixed by enami@ptgd.sony.co.jp (enami tsugutomo)
11080   (if (save-excursion
11081         (end-of-line)
11082         (and (pos-visible-in-window-p)  ;Not continuation line.
11083              (eobp)))
11084       ;; Nothing in this page.
11085       (if (or (not gnus-break-pages)
11086               (save-excursion
11087                 (save-restriction
11088                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
11089           t                             ;Nothing more.
11090         (gnus-narrow-to-page 1)         ;Go to next page.
11091         nil)
11092     ;; More in this page.
11093     (condition-case ()
11094         (scroll-up lines)
11095       (end-of-buffer
11096        ;; Long lines may cause an end-of-buffer error.
11097        (goto-char (point-max))))
11098     nil))
11099
11100 (defun gnus-article-prev-page (&optional lines)
11101   "Show previous page of current article.
11102 Argument LINES specifies lines to be scrolled down."
11103   (interactive "P")
11104   (move-to-window-line 0)
11105   (if (and gnus-break-pages
11106            (bobp)
11107            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
11108       (progn
11109         (gnus-narrow-to-page -1) ;Go to previous page.
11110         (goto-char (point-max))
11111         (recenter -1))
11112     (scroll-down lines)))
11113
11114 (defun gnus-article-refer-article ()
11115   "Read article specified by message-id around point."
11116   (interactive)
11117   (search-forward ">" nil t)    ;Move point to end of "<....>".
11118   (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
11119       (let ((message-id
11120              (buffer-substring (match-beginning 1) (match-end 1))))
11121         (set-buffer gnus-summary-buffer)
11122         (gnus-summary-refer-article message-id))
11123     (error "No references around point")))
11124
11125 (defun gnus-article-show-summary ()
11126   "Reconfigure windows to show summary buffer."
11127   (interactive)
11128   (gnus-configure-windows 'article)
11129   (gnus-summary-goto-subject gnus-current-article))
11130
11131 (defun gnus-article-describe-briefly ()
11132   "Describe article mode commands briefly."
11133   (interactive)
11134   (gnus-message 6
11135    (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")))
11136
11137 (defun gnus-article-summary-command ()
11138   "Execute the last keystroke in the summary buffer."
11139   (interactive)
11140   (let ((obuf (current-buffer))
11141         (owin (current-window-configuration))
11142         func)
11143     (switch-to-buffer gnus-summary-buffer 'norecord)
11144     (setq func (lookup-key (current-local-map) (this-command-keys)))
11145     (call-interactively func)
11146     (set-buffer obuf)
11147     (set-window-configuration owin)
11148     (set-window-start (get-buffer-window (current-buffer)) (point))))
11149
11150 (defun gnus-article-summary-command-nosave ()
11151   "Execute the last keystroke in the summary buffer."
11152   (interactive)
11153   (let (func)
11154     (pop-to-buffer gnus-summary-buffer 'norecord)
11155     (setq func (lookup-key (current-local-map) (this-command-keys)))
11156     (call-interactively func)))
11157
11158 \f
11159 ;; Basic ideas by emv@math.lsa.umich.edu (Edward Vielmetti)
11160
11161 ;;;###autoload
11162 (defalias 'gnus-batch-kill 'gnus-batch-score)
11163 ;;;###autoload
11164 (defun gnus-batch-score ()
11165   "Run batched scoring.
11166 Usage: emacs -batch -l gnus -f gnus-batch-score <newsgroups> ...
11167 Newsgroups is a list of strings in Bnews format.  If you want to score
11168 the comp hierarchy, you'd say \"comp.all\". If you would not like to
11169 score the alt hierarchy, you'd say \"!alt.all\"."
11170   (interactive)
11171   (let* ((yes-and-no
11172           (gnus-newsrc-parse-options
11173            (apply (function concat)
11174                   (mapcar (lambda (g) (concat g " "))
11175                           command-line-args-left))))
11176          (gnus-expert-user t)
11177          (nnmail-spool-file nil)
11178          (gnus-use-dribble-file nil)
11179          (yes (car yes-and-no))
11180          (no (cdr yes-and-no))
11181          group newsrc entry
11182          ;; Disable verbose message.
11183          gnus-novice-user gnus-large-newsgroup)
11184     ;; Eat all arguments.
11185     (setq command-line-args-left nil)
11186     ;; Start Gnus.
11187     (gnus)
11188     ;; Apply kills to specified newsgroups in command line arguments.
11189     (setq newsrc (cdr gnus-newsrc-alist))
11190     (while newsrc
11191       (setq group (car (car newsrc)))
11192       (setq entry (gnus-gethash group gnus-newsrc-hashtb))
11193       (if (and (<= (nth 1 (car newsrc)) gnus-level-subscribed)
11194                (and (car entry)
11195                     (or (eq (car entry) t)
11196                         (not (zerop (car entry)))))
11197                (if yes (string-match yes group) t)
11198                (or (null no) (not (string-match no group))))
11199           (progn
11200             (gnus-summary-read-group group nil t)
11201             (and (eq (current-buffer) (get-buffer gnus-summary-buffer))
11202                  (gnus-summary-exit))))
11203       (setq newsrc (cdr newsrc)))
11204     ;; Exit Emacs.
11205     (switch-to-buffer gnus-group-buffer)
11206     (gnus-group-save-newsrc)))
11207
11208 (defun gnus-apply-kill-file ()
11209   "Apply a kill file to the current newsgroup.
11210 Returns the number of articles marked as read."
11211   (if (or (file-exists-p (gnus-newsgroup-kill-file nil))
11212           (file-exists-p (gnus-newsgroup-kill-file gnus-newsgroup-name)))
11213       (gnus-apply-kill-file-internal)
11214     0))
11215
11216 (defun gnus-kill-save-kill-buffer ()
11217   (save-excursion
11218     (let ((file (gnus-newsgroup-kill-file gnus-newsgroup-name)))
11219       (if (get-file-buffer file)
11220           (progn
11221             (set-buffer (get-file-buffer file))
11222             (and (buffer-modified-p) (save-buffer))
11223             (kill-buffer (current-buffer)))))))
11224
11225 (defvar gnus-kill-file-name "KILL"
11226   "Suffix of the kill files.")
11227
11228 (defun gnus-newsgroup-kill-file (newsgroup)
11229   "Return the name of a kill file name for NEWSGROUP.
11230 If NEWSGROUP is nil, return the global kill file name instead."
11231   (cond ((or (null newsgroup)
11232              (string-equal newsgroup ""))
11233          ;; The global KILL file is placed at top of the directory.
11234          (expand-file-name gnus-kill-file-name
11235                            (or gnus-kill-files-directory "~/News")))
11236         ((gnus-use-long-file-name 'not-kill)
11237          ;; Append ".KILL" to newsgroup name.
11238          (expand-file-name (concat newsgroup "." gnus-kill-file-name)
11239                            (or gnus-kill-files-directory "~/News")))
11240         (t
11241          ;; Place "KILL" under the hierarchical directory.
11242          (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
11243                                    "/" gnus-kill-file-name)
11244                            (or gnus-kill-files-directory "~/News")))))
11245
11246 \f
11247 ;;;
11248 ;;; Dribble file
11249 ;;;
11250
11251 (defvar gnus-dribble-ignore nil)
11252 (defvar gnus-dribble-eval-file nil)
11253
11254 (defun gnus-dribble-file-name ()
11255   (concat gnus-current-startup-file "-dribble"))
11256
11257 (defun gnus-dribble-open ()
11258   (save-excursion 
11259     (set-buffer 
11260      (setq gnus-dribble-buffer (find-file-noselect (gnus-dribble-file-name))))
11261     (buffer-disable-undo (current-buffer))
11262     (bury-buffer gnus-dribble-buffer)
11263     (auto-save-mode t)
11264     (goto-char (point-max))))
11265
11266 (defun gnus-dribble-enter (string)
11267   (if (and (not gnus-dribble-ignore)
11268            gnus-dribble-buffer
11269            (buffer-name gnus-dribble-buffer))
11270       (let ((obuf (current-buffer)))
11271         (set-buffer gnus-dribble-buffer)
11272         (insert string "\n")
11273         (set-window-point (get-buffer-window (current-buffer)) (point-max))
11274         (set-buffer obuf))))
11275
11276 (defun gnus-dribble-read-file ()
11277   (let ((dribble-file (gnus-dribble-file-name)))
11278     (save-excursion 
11279       (set-buffer (setq gnus-dribble-buffer 
11280                         (get-buffer-create 
11281                          (file-name-nondirectory dribble-file))))
11282       (gnus-add-current-to-buffer-list)
11283       (erase-buffer)
11284       (set-visited-file-name dribble-file)
11285       (buffer-disable-undo (current-buffer))
11286       (bury-buffer (current-buffer))
11287       (set-buffer-modified-p nil)
11288       (let ((auto (make-auto-save-file-name))
11289             (gnus-dribble-ignore t))
11290         (if (or (file-exists-p auto) (file-exists-p dribble-file))
11291             (progn
11292               (if (file-newer-than-file-p auto dribble-file)
11293                   (setq dribble-file auto))
11294               (insert-file-contents dribble-file)
11295               (if (not (zerop (buffer-size)))
11296                   (set-buffer-modified-p t))
11297               (if (gnus-y-or-n-p 
11298                    "Auto-save file exists. Do you want to read it? ")
11299                   (setq gnus-dribble-eval-file t))))))))
11300
11301 (defun gnus-dribble-eval-file ()
11302   (if (not gnus-dribble-eval-file)
11303       ()
11304     (setq gnus-dribble-eval-file nil)
11305     (save-excursion
11306       (set-buffer gnus-dribble-buffer)
11307       (eval-buffer (current-buffer)))))
11308
11309 (defun gnus-dribble-delete-file ()
11310   (if (file-exists-p (gnus-dribble-file-name))
11311       (delete-file (gnus-dribble-file-name)))
11312   (if gnus-dribble-buffer
11313       (save-excursion
11314         (set-buffer gnus-dribble-buffer)
11315         (let ((auto (make-auto-save-file-name)))
11316           (if (file-exists-p auto)
11317               (delete-file auto))
11318           (erase-buffer)
11319           (set-buffer-modified-p nil)))))
11320
11321 (defun gnus-dribble-save ()
11322   (if (and gnus-dribble-buffer
11323            (buffer-name gnus-dribble-buffer))
11324       (save-excursion
11325         (set-buffer gnus-dribble-buffer)
11326         (save-buffer))))
11327
11328 (defun gnus-dribble-clear ()
11329   (save-excursion
11330     (if (gnus-buffer-exists-p gnus-dribble-buffer)
11331         (progn
11332           (set-buffer gnus-dribble-buffer)
11333           (erase-buffer)
11334           (set-buffer-modified-p nil)
11335           (setq buffer-saved-size (buffer-size))))))
11336
11337 ;;;
11338 ;;; Server Communication
11339 ;;;
11340
11341 ;; All the Gnus backends have the same interface, and should return
11342 ;; data in a similar format. Below is an overview of what functions
11343 ;; these packages must supply and what results they should return.
11344 ;;
11345 ;; Variables:
11346 ;;
11347 ;; `nntp-server-buffer' - All data should be returned to Gnus in this
11348 ;; buffer. 
11349 ;;
11350 ;; Functions for the imaginary backend `choke':
11351 ;;
11352 ;; `choke-retrieve-headers ARTICLES &optional GROUP SERVER'
11353 ;; Should return all headers for all ARTICLES, or return NOV lines for
11354 ;; the same.
11355 ;;
11356 ;; `choke-request-group GROUP &optional SERVER DISCARD'
11357 ;; Switch to GROUP. If DISCARD is nil, active information on the group
11358 ;; must be returned.
11359 ;;
11360 ;; `choke-close-group GROUP &optional SERVER'
11361 ;; Close group. Most backends won't have to do anything with this
11362 ;; call, but it is an opportunity to clean up, if that is needed. It
11363 ;; is called when Gnus exits a group.
11364 ;;
11365 ;; `choke-request-article ARTICLE &optional GROUP SERVER'
11366 ;; Return ARTICLE, which is either an article number or
11367 ;; message-id. Note that not all backends can return articles based on
11368 ;; message-id. 
11369 ;;
11370 ;; `choke-request-list SERVER'
11371 ;; Return a list of all newsgroups on SERVER.
11372 ;;
11373 ;; `choke-request-list-newsgroups SERVER'
11374 ;; Return a list of descriptions of all newsgroups on SERVER.
11375 ;;
11376 ;; `choke-request-newgroups DATE &optional SERVER'
11377 ;; Return a list of all groups that have arrived after DATE on
11378 ;; SERVER. Note that the date doesn't have to be respected - Gnus will
11379 ;; always check whether the groups are old or not. Backends that do
11380 ;; not store date information may just return the entire list of
11381 ;; groups, although this might not be a good idea in general.
11382 ;;
11383 ;; `choke-request-post-buffer METHOD HEADER ARTICLE-BUFFER GROUP INFO'
11384 ;; Should return a buffer that is suitable for "posting". nnspool and
11385 ;; nntp return a `*post-buffer*', and nnmail return a `*mail*'
11386 ;; buffer. This function should fill out the appropriate headers. 
11387 ;;
11388 ;; `choke-request-post &optional SERVER'
11389 ;; Function that will be called from a buffer to be posted. 
11390 ;;
11391 ;; `choke-open-server SERVER &optional ARGUMENT'
11392 ;; Open a connection to SERVER.
11393 ;;
11394 ;; `choke-close-server &optional SERVER'
11395 ;; Close the connection to SERVER.
11396 ;;
11397 ;; `choke-server-opened &optional SERVER'
11398 ;; Whether the conenction to SERVER is opened or not.
11399 ;;
11400 ;; `choke-server-status &optional SERVER'
11401 ;; Should return a status string (not in the nntp buffer, but as the
11402 ;; result of the function).
11403 ;;
11404 ;; `choke-retrieve-groups GROUPS &optional SERVER'
11405 ;; Optional function for retrieving active file info on all groups in
11406 ;; GROUPS.  Two return formats are supported: The normal active file
11407 ;; format, and a list of GROUP lines.  This function should return (as
11408 ;; a function value) either `active' or `group', depending on what
11409 ;; format it returns.
11410 ;;
11411 ;; The following functions are optional and apply only to backends
11412 ;; that are able to control the contents of their groups totally
11413 ;; (ie. mail backends.)  Backends that aren't able to do that
11414 ;; shouldn't define these functions at all. Gnus will check for their
11415 ;; presence before attempting to call them.
11416 ;;
11417 ;; `choke-request-expire-articles ARTICLES &optional NEWSGROUP SERVER'
11418 ;; Should expire (according to some aging scheme) all ARTICLES. Most
11419 ;; backends will not be able to expire articles. Should return a list
11420 ;; of all articles that were not expired.
11421 ;;
11422 ;; `choke-request-move-article ARTICLE GROUP SERVER ACCEPT-FORM &optional LAST'
11423 ;; Should move ARTICLE from GROUP on SERVER by using ACCEPT-FORM.
11424 ;; Removes any information it has added to the article (extra headers,
11425 ;; whatever - make it as clean as possible), and then passes the
11426 ;; article on by evaling ACCEPT-FORM, which is normally a call to the
11427 ;; function described below. If the ACCEPT-FORM returns a non-nil
11428 ;; value, the article should then be deleted. If LAST is nil, that
11429 ;; means that there will be further calls to this function. This might
11430 ;; be taken as an advice not to save buffers/internal variables just
11431 ;; yet, but wait until the last call to speed things up.
11432 ;;
11433 ;; `choke-request-accept-article GROUP &optional LAST' 
11434 ;; The contents of the current buffer will be put into GROUP.  There
11435 ;; should, of course, be an article in the current buffer.  This
11436 ;; function is normally only called by the function described above,
11437 ;; and LAST works the same way as in that function.
11438 ;;
11439 ;; `choke-request-replace-article ARTICLE GROUP BUFFER'
11440 ;; Replace ARTICLE in GROUP with the contents of BUFFER.
11441 ;; This provides an easy interface for allowing editing of
11442 ;; articles. Note that even headers may be edited, so the backend has
11443 ;; to update any tables (nov buffers, etc) that it maintains after
11444 ;; replacing the article.
11445 ;;
11446 ;; `choke-request-create-group GROUP &optional SERVER'
11447 ;; Create GROUP on SERVER.  This might be a new, empty group, or it
11448 ;; might be a group that already exists, but hasn't been registered
11449 ;; yet. 
11450 ;;
11451 ;; All these functions must return nil if they couldn't service the
11452 ;; request. If the optional arguments are not supplied, some "current"
11453 ;; or "default" values should be used. In short, one should emulate an
11454 ;; NNTP server, in a way.
11455 ;;
11456 ;; If you want to write a new backend, you just have to supply the
11457 ;; functions listed above. In addition, you must enter the new backend
11458 ;; into the list of valid select methods:
11459 ;; (setq gnus-valid-select-methods 
11460 ;;       (cons '("choke" mail) gnus-valid-select-methods))
11461 ;; The first element in this list is the name of the backend. Other
11462 ;; elemnets may be `mail' (for mail groups),  `post' (for news
11463 ;; groups), `none' (neither), `respool' (for groups that can control
11464 ;; their contents). 
11465
11466 (defun gnus-start-news-server (&optional confirm)
11467   "Open a method for getting news.
11468 If CONFIRM is non-nil, the user will be asked for an NNTP server."
11469   (let (how)
11470     (if gnus-current-select-method
11471         ;; Stream is already opened.
11472         nil
11473       ;; Open NNTP server.
11474       (if (null gnus-nntp-service) (setq gnus-nntp-server nil))
11475       (if confirm
11476           (progn
11477             ;; Read server name with completion.
11478             (setq gnus-nntp-server
11479                   (completing-read "NNTP server: "
11480                                    (mapcar (lambda (server) (list server))
11481                                            (cons (list gnus-nntp-server)
11482                                                  gnus-secondary-servers))
11483                                    nil nil gnus-nntp-server))))
11484
11485       (if (and gnus-nntp-server 
11486                (stringp gnus-nntp-server)
11487                (not (string= gnus-nntp-server "")))
11488           (setq gnus-select-method
11489                 (cond ((or (string= gnus-nntp-server "")
11490                            (string= gnus-nntp-server "::"))
11491                        (list 'nnspool (system-name)))
11492                       ((string-match "^:" gnus-nntp-server)
11493                        (list 'nnmh gnus-nntp-server 
11494                              (list 'nnmh-directory 
11495                                    (file-name-as-directory
11496                                     (expand-file-name
11497                                      (concat "~/" (substring
11498                                                    gnus-nntp-server 1)))))
11499                              (list 'nnmh-get-new-mail nil)))
11500                       (t
11501                        (list 'nntp gnus-nntp-server)))))
11502
11503       (setq how (car gnus-select-method))
11504       (cond ((eq how 'nnspool)
11505              (require 'nnspool)
11506              (gnus-message 5 "Looking up local news spool..."))
11507             ((eq how 'nnmh)
11508              (require 'nnmh)
11509              (gnus-message 5 "Looking up mh spool..."))
11510             (t
11511              (require 'nntp)))
11512       (setq gnus-current-select-method gnus-select-method)
11513       (run-hooks 'gnus-open-server-hook)
11514       (or 
11515        ;; gnus-open-server-hook might have opened it
11516        (gnus-server-opened gnus-select-method)  
11517        (gnus-open-server gnus-select-method)
11518        (gnus-y-or-n-p
11519         (format
11520          "%s server on %s can't be opened. Continue? "
11521          (car gnus-select-method) (nth 1 gnus-select-method)))
11522        (progn
11523          (gnus-message 1 "Couldn't open server on %s" 
11524                        (nth 1 gnus-select-method))
11525          (ding)
11526          nil)))))
11527
11528 (defun gnus-check-news-server (&optional method)
11529   "If the news server is down, start it up again."
11530   (let ((method (if method method gnus-select-method)))
11531     (and (stringp method)
11532          (setq method (gnus-server-to-method method)))
11533     (if (gnus-server-opened method)
11534         ;; Stream is already opened.
11535         t
11536       ;; Open server.
11537       (gnus-message 5 "Opening server %s on %s..." (car method) (nth 1 method))
11538       (run-hooks 'gnus-open-server-hook)
11539       (or (gnus-server-opened method)
11540           (gnus-open-server method))
11541       (message ""))))
11542
11543 (defun gnus-nntp-message (&optional message)
11544   "Check the status of the NNTP server.
11545 If the status of the server is clear and MESSAGE is non-nil, MESSAGE
11546 is returned insted of the status string."
11547   (let ((status (gnus-status-message (gnus-find-method-for-group 
11548                                       gnus-newsgroup-name)))
11549         (message (or message "")))
11550     (if (and (stringp status) (> (length status) 0))
11551         status message)))
11552
11553 (defun gnus-get-function (method function)
11554   (and (stringp method)
11555        (setq method (gnus-server-to-method method)))
11556   (let ((func (intern (format "%s-%s" (car method) function))))
11557     (if (not (fboundp func)) 
11558         (progn
11559           (require (car method))
11560           (if (not (fboundp func)) 
11561               (error "No such function: %s" func))))
11562     func))
11563
11564 ;;; Interface functions to the backends.
11565
11566 (defun gnus-open-server (method)
11567   (funcall (gnus-get-function method 'open-server)
11568            (nth 1 method) (nthcdr 2 method)))
11569
11570 (defun gnus-close-server (method)
11571   (funcall (gnus-get-function method 'close-server) (nth 1 method)))
11572
11573 (defun gnus-request-list (method)
11574   (funcall (gnus-get-function method 'request-list) (nth 1 method)))
11575
11576 (defun gnus-request-list-newsgroups (method)
11577   (funcall (gnus-get-function method 'request-list-newsgroups) (nth 1 method)))
11578
11579 (defun gnus-request-newgroups (date method)
11580   (funcall (gnus-get-function method 'request-newgroups) 
11581            date (nth 1 method)))
11582
11583 (defun gnus-server-opened (method)
11584   (funcall (gnus-get-function method 'server-opened) (nth 1 method)))
11585
11586 (defun gnus-status-message (method)
11587   (let ((method (if (stringp method) (gnus-find-method-for-group method)
11588                   method)))
11589     (funcall (gnus-get-function method 'status-message) (nth 1 method))))
11590
11591 (defun gnus-request-group (group &optional dont-check)
11592   (let ((method (gnus-find-method-for-group group)))
11593 ;    (and t (message "%s GROUP %s" (car method) group))
11594     (funcall (gnus-get-function method 'request-group) 
11595              (gnus-group-real-name group) (nth 1 method) dont-check)))
11596
11597 (defun gnus-request-asynchronous (group &optional articles)
11598   (let ((method (gnus-find-method-for-group group)))
11599     (funcall (gnus-get-function method 'request-asynchronous) 
11600              (gnus-group-real-name group) (nth 1 method) articles)))
11601
11602 (defun gnus-list-active-group (group)
11603   (let ((method (gnus-find-method-for-group group))
11604         (func 'list-active-group))
11605     (and (gnus-check-backend-function func group)
11606          (funcall (gnus-get-function method func) 
11607                   (gnus-group-real-name group) (nth 1 method)))))
11608
11609 (defun gnus-request-group-description (group)
11610   (let ((method (gnus-find-method-for-group group))
11611         (func 'request-group-description))
11612     (and (gnus-check-backend-function func group)
11613          (funcall (gnus-get-function method func) 
11614                   (gnus-group-real-name group) (nth 1 method)))))
11615
11616 (defun gnus-close-group (group)
11617   (let ((method (gnus-find-method-for-group group)))
11618     (funcall (gnus-get-function method 'close-group) 
11619              (gnus-group-real-name group) (nth 1 method))))
11620
11621 (defun gnus-retrieve-headers (articles group)
11622   (let ((method (gnus-find-method-for-group group)))
11623     (if (and gnus-use-cache (numberp (car articles)))
11624         (gnus-cache-retrieve-headers articles group)
11625       (funcall (gnus-get-function method 'retrieve-headers) 
11626                articles (gnus-group-real-name group) (nth 1 method)))))
11627
11628 (defun gnus-retrieve-groups (groups method)
11629   (funcall (gnus-get-function method 'retrieve-groups) groups (nth 1 method)))
11630
11631 (defun gnus-request-article (article group &optional buffer)
11632   (let ((method (gnus-find-method-for-group group)))
11633     (funcall (gnus-get-function method 'request-article) 
11634              article (gnus-group-real-name group) (nth 1 method) buffer)))
11635
11636 (defun gnus-request-head (article group)
11637   (let ((method (gnus-find-method-for-group group)))
11638     (funcall (gnus-get-function method 'request-head) 
11639              article (gnus-group-real-name group) (nth 1 method))))
11640
11641 ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
11642 (defun gnus-request-post-buffer (post group subject header artbuf
11643                                       info follow-to respect-poster)
11644    (let* ((info (or info (and group (nth 2 (gnus-gethash 
11645                                             group gnus-newsrc-hashtb)))))
11646           (method
11647            (if (and gnus-post-method
11648                     ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
11649                     (memq 'post (assoc
11650                                  (format "%s" (car (gnus-find-method-for-group
11651                                                     gnus-newsgroup-name)))
11652                                         gnus-valid-select-methods)))
11653                gnus-post-method
11654              (gnus-find-method-for-group gnus-newsgroup-name))))
11655      (or (gnus-server-opened method)
11656          (gnus-open-server method)
11657          (error "Can't open server %s:%s" (car method) (nth 1 method)))
11658      (let ((mail-self-blind nil)
11659            (mail-archive-file-name nil))
11660        (funcall (gnus-get-function method 'request-post-buffer) 
11661                 post group subject header artbuf info follow-to
11662                 respect-poster))))
11663
11664 (defun gnus-request-post (method &optional force)
11665   (and (stringp method)
11666        (setq method (gnus-server-to-method method)))
11667   (and (not force) gnus-post-method
11668        (memq 'post (assoc (format "%s" (car method))
11669                           gnus-valid-select-methods))
11670        (setq method gnus-post-method))
11671   (funcall (gnus-get-function method 'request-post) 
11672            (nth 1 method)))
11673
11674 (defun gnus-request-expire-articles (articles group &optional force)
11675   (let ((method (gnus-find-method-for-group group)))
11676     (funcall (gnus-get-function method 'request-expire-articles) 
11677              articles (gnus-group-real-name group) (nth 1 method)
11678              force)))
11679
11680 (defun gnus-request-move-article 
11681   (article group server accept-function &optional last)
11682   (let ((method (gnus-find-method-for-group group)))
11683     (funcall (gnus-get-function method 'request-move-article) 
11684              article (gnus-group-real-name group) 
11685              (nth 1 method) accept-function last)))
11686
11687 (defun gnus-request-accept-article (group &optional last)
11688   (let ((func (if (symbolp group) group
11689                 (car (gnus-find-method-for-group group)))))
11690     (funcall (intern (format "%s-request-accept-article" func))
11691              (if (stringp group) (gnus-group-real-name group) group)
11692              last)))
11693
11694 (defun gnus-request-replace-article (article group buffer)
11695   (let ((func (car (gnus-find-method-for-group group))))
11696     (funcall (intern (format "%s-request-replace-article" func))
11697              article (gnus-group-real-name group) buffer)))
11698
11699 (defun gnus-request-create-group (group)
11700   (let ((method (gnus-find-method-for-group group)))
11701     (funcall (gnus-get-function method 'request-create-group) 
11702              (gnus-group-real-name group) (nth 1 method))))
11703
11704 (defun gnus-member-of-valid (symbol group)
11705   (memq symbol (assoc
11706                 (format "%s" (car (gnus-find-method-for-group group)))
11707                 gnus-valid-select-methods)))
11708
11709 (defsubst gnus-secondary-method-p (method)
11710   (member method gnus-secondary-select-methods))
11711
11712 (defun gnus-find-method-for-group (group &optional info)
11713   (or gnus-override-method
11714       (and (not group)
11715            gnus-select-method)
11716       (let ((info (or info (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
11717             method)
11718         (if (or (not info)
11719                 (not (setq method (nth 4 info))))
11720             (setq method gnus-select-method)
11721           (setq method
11722                 (cond ((stringp method)
11723                        (gnus-server-to-method method))
11724                       ((stringp (car method))
11725                        (gnus-server-extend-method group method))
11726                       (t
11727                        method))))
11728         (gnus-server-add-address method))))
11729
11730 (defun gnus-check-backend-function (func group)
11731   (let ((method (if (stringp group) (car (gnus-find-method-for-group group))
11732                  group)))
11733     (fboundp (intern (format "%s-%s" method func)))))
11734
11735 (defun gnus-methods-using (method)
11736   (let ((valids gnus-valid-select-methods)
11737         outs)
11738     (while valids
11739       (if (memq method (car valids)) 
11740           (setq outs (cons (car valids) outs)))
11741       (setq valids (cdr valids)))
11742     outs))
11743
11744 ;;; 
11745 ;;; Active & Newsrc File Handling
11746 ;;;
11747
11748 ;; Newsrc related functions.
11749 ;; Gnus internal format of gnus-newsrc-alist:
11750 ;; (("alt.general" 3 (1 . 1))
11751 ;;  ("alt.misc"    3 ((1 . 10) (12 . 15)))
11752 ;;  ("alt.test"    7 (1 . 99) (45 57 93)) ...)
11753 ;; The first item is the group name; the second is the subscription
11754 ;; level; the third is either a range of a list of ranges of read
11755 ;; articles, the optional fourth element is a list of marked articles,
11756 ;; the optional fifth element is the select method.
11757 ;;
11758 ;; Gnus internal format of gnus-newsrc-hashtb:
11759 ;; (95 ("alt.general" 3 (1 . 1)) ("alt.misc" 3 ((1 . 10) (12 . 15))) ...)
11760 ;; This is the entry for "alt.misc". The first element is the number
11761 ;; of unread articles in "alt.misc". The cdr of this entry is the
11762 ;; element *before* "alt.misc" in gnus-newsrc-alist, which makes is
11763 ;; trivial to remove or add new elements into gnus-newsrc-alist
11764 ;; without scanning the entire list. So, to get the actual information
11765 ;; of "alt.misc", you'd say something like 
11766 ;; (nth 2 (gnus-gethash "alt.misc" gnus-newsrc-hashtb))
11767 ;;
11768 ;; Gnus internal format of gnus-active-hashtb:
11769 ;; ((1 . 1))
11770 ;;  (5 . 10))
11771 ;;  (67 . 99)) ...)
11772 ;; The only element in each entry in this hash table is a range of
11773 ;; (possibly) available articles. (Articles in this range may have
11774 ;; been expired or canceled.)
11775 ;;
11776 ;; Gnus internal format of gnus-killed-list and gnus-zombie-list:
11777 ;; ("alt.misc" "alt.test" "alt.general" ...)
11778
11779 (defun gnus-setup-news (&optional rawfile level)
11780   "Setup news information.
11781 If RAWFILE is non-nil, the .newsrc file will also be read.
11782 If LEVEL is non-nil, the news will be set up at level LEVEL."
11783   (let ((init (not (and gnus-newsrc-alist gnus-active-hashtb (not rawfile)))))
11784     ;; Clear some variables to re-initialize news information.
11785     (if init (setq gnus-newsrc-alist nil 
11786                    gnus-active-hashtb nil))
11787
11788     ;; Read the newsrc file and create `gnus-newsrc-hashtb'.
11789     (if init (gnus-read-newsrc-file rawfile))
11790
11791     ;; Read the active file and create `gnus-active-hashtb'.
11792     ;; If `gnus-read-active-file' is nil, then we just create an empty
11793     ;; hash table. The partial filling out of the hash table will be
11794     ;; done in `gnus-get-unread-articles'.
11795     (if (and gnus-read-active-file 
11796              (not level))
11797         (gnus-read-active-file)
11798       (setq gnus-active-hashtb (make-vector 4095 0)))
11799
11800     ;; Possibly eval the dribble file.
11801     (and init gnus-use-dribble-file (gnus-dribble-eval-file))
11802
11803     ;; Find the number of unread articles in each non-dead group.
11804     (gnus-get-unread-articles (or level (1+ gnus-level-subscribed)))
11805     ;; Find new newsgroups and treat them.
11806     (if (and init gnus-check-new-newsgroups gnus-read-active-file (not level)
11807              (gnus-server-opened gnus-select-method))
11808         (gnus-find-new-newsgroups))
11809     (if (and init gnus-check-bogus-newsgroups 
11810              gnus-read-active-file (not level)
11811              (gnus-server-opened gnus-select-method))
11812         (gnus-check-bogus-newsgroups))))
11813
11814 (defun gnus-find-new-newsgroups ()
11815   "Search for new newsgroups and add them.
11816 Each new newsgroup will be treated with `gnus-subscribe-newsgroup-method.'
11817 The `-n' option line from .newsrc is respected."
11818   (interactive)
11819   (or (gnus-check-first-time-used)
11820       (if (or (consp gnus-check-new-newsgroups)
11821               (eq gnus-check-new-newsgroups 'ask-server))
11822           (gnus-ask-server-for-new-groups)
11823         (let ((groups 0)
11824               group new-newsgroups)
11825           (gnus-message 5 "Checking for new newsgroups...")
11826           (or gnus-have-read-active-file (gnus-read-active-file))
11827           (setq gnus-newsrc-last-checked-date (current-time-string))
11828           (if (not gnus-killed-hashtb) (gnus-make-hashtable-from-killed))
11829           ;; Go though every newsgroup in `gnus-active-hashtb' and compare
11830           ;; with `gnus-newsrc-hashtb' and `gnus-killed-hashtb'.
11831           (mapatoms
11832            (lambda (sym)
11833              (setq group (symbol-name sym))
11834              (if (or (gnus-gethash group gnus-killed-hashtb)
11835                      (gnus-gethash group gnus-newsrc-hashtb))
11836                  ()
11837                (let ((do-sub (gnus-matches-options-n group)))
11838                  (cond ((eq do-sub 'subscribe)
11839                         (setq groups (1+ groups))
11840                         (gnus-sethash group group gnus-killed-hashtb)
11841                         (funcall 
11842                          gnus-subscribe-options-newsgroup-method group))
11843                        ((eq do-sub 'ignore)
11844                         nil)
11845                        (t
11846                         (setq groups (1+ groups))
11847                         (gnus-sethash group group gnus-killed-hashtb)
11848                         (if gnus-subscribe-hierarchical-interactive
11849                             (setq new-newsgroups (cons group new-newsgroups))
11850                           (funcall gnus-subscribe-newsgroup-method group)))))))
11851            gnus-active-hashtb)
11852           (if new-newsgroups 
11853               (gnus-subscribe-hierarchical-interactive new-newsgroups))
11854           ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
11855           (if (> groups 0)
11856               (gnus-message 6 "%d new newsgroup%s arrived." 
11857                             groups (if (> groups 1) "s have" " has"))
11858             (gnus-message 6 "No new newsgroups."))))))
11859
11860 (defun gnus-matches-options-n (group)
11861   ;; Returns `subscribe' if the group is to be uncoditionally
11862   ;; subscribed, `ignore' if it is to be ignored, and nil if there is
11863   ;; no match for the group.
11864
11865   ;; First we check the two user variables.
11866   (cond
11867    ((and gnus-options-subscribe
11868          (string-match gnus-options-subscribe group))
11869     'subscribe)
11870    ((and gnus-options-not-subscribe
11871          (string-match gnus-options-not-subscribe group))
11872     'ignore)
11873    ;; Then we go through the list that was retrieved from the .newsrc
11874    ;; file.  This list has elements on the form 
11875    ;; `(REGEXP . {ignore,subscribe})'. The first match found (the list
11876    ;; is in the reverse order of the options line) is returned.
11877    (t
11878     (let ((regs gnus-newsrc-options-n))
11879       (while (and regs
11880                   (not (string-match (car (car regs)) group)))
11881         (setq regs (cdr regs)))
11882       (and regs (cdr (car regs)))))))
11883
11884 (defun gnus-ask-server-for-new-groups ()
11885   (let* ((date (or gnus-newsrc-last-checked-date (current-time-string)))
11886          (methods (cons gnus-select-method 
11887                         (append
11888                          (and (consp gnus-check-new-newsgroups)
11889                               gnus-check-new-newsgroups)
11890                          gnus-secondary-select-methods)))
11891          (groups 0)
11892          (new-date (current-time-string))
11893          hashtb group new-newsgroups got-new)
11894     ;; Go thorugh both primary and secondary select methods and
11895     ;; request new newsgroups.  
11896     (while methods
11897       (and (or (gnus-server-opened (car methods))
11898                (gnus-open-server (car methods)))
11899            (gnus-request-newgroups date (car methods))
11900            (save-excursion
11901              (setq got-new t)
11902              (set-buffer nntp-server-buffer)
11903              (or hashtb (setq hashtb (gnus-make-hashtable 
11904                                       (count-lines (point-min) (point-max)))))
11905              ;; Enter all the new groups in a hashtable.
11906              (gnus-active-to-gnus-format (car methods) hashtb 'ignore)))
11907       (setq methods (cdr methods)))
11908     (and got-new (setq gnus-newsrc-last-checked-date new-date))
11909     ;; Now all new groups from all select methods are in `hashtb'.
11910     (mapatoms
11911      (lambda (group-sym)
11912        (setq group (symbol-name group-sym))
11913        (if (or (gnus-gethash group gnus-newsrc-hashtb)
11914                (member group gnus-zombie-list)
11915                (member group gnus-killed-list))
11916            ;; The group is already known.
11917            ()
11918          (and (symbol-value group-sym)
11919               (gnus-sethash group (symbol-value group-sym) gnus-active-hashtb))
11920          (let ((do-sub (gnus-matches-options-n group)))
11921            (cond ((eq do-sub 'subscribe)
11922                   (setq groups (1+ groups))
11923                   (gnus-sethash group group gnus-killed-hashtb)
11924                   (funcall 
11925                    gnus-subscribe-options-newsgroup-method group))
11926                  ((eq do-sub 'ignore)
11927                   nil)
11928                  (t
11929                   (setq groups (1+ groups))
11930                   (gnus-sethash group group gnus-killed-hashtb)
11931                   (if gnus-subscribe-hierarchical-interactive
11932                       (setq new-newsgroups (cons group new-newsgroups))
11933                     (funcall gnus-subscribe-newsgroup-method group)))))))
11934      hashtb)
11935     (if new-newsgroups 
11936         (gnus-subscribe-hierarchical-interactive new-newsgroups))
11937     ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
11938     (if (> groups 0)
11939         (gnus-message 6 "%d new newsgroup%s arrived." 
11940                       groups (if (> groups 1) "s have" " has")))
11941     got-new))
11942
11943 (defun gnus-check-first-time-used ()
11944   (if (or (> (length gnus-newsrc-alist) 1)
11945           (file-exists-p gnus-startup-file)
11946           (file-exists-p (concat gnus-startup-file ".el"))
11947           (file-exists-p (concat gnus-startup-file ".eld")))
11948       nil
11949     (gnus-message 6 "First time user; subscribing you to default groups")
11950     (or gnus-have-read-active-file (gnus-read-active-file))
11951     (setq gnus-newsrc-last-checked-date (current-time-string))
11952     (let ((groups gnus-default-subscribed-newsgroups)
11953           group)
11954       (if (eq groups t)
11955           nil
11956         (setq groups (or groups gnus-backup-default-subscribed-newsgroups))
11957         (mapatoms
11958          (lambda (sym)
11959            (setq group (symbol-name sym))
11960            (let ((do-sub (gnus-matches-options-n group)))
11961              (cond ((eq do-sub 'subscribe)
11962                     (gnus-sethash group group gnus-killed-hashtb)
11963                     (funcall 
11964                      gnus-subscribe-options-newsgroup-method group))
11965                    ((eq do-sub 'ignore)
11966                     nil)
11967                    (t
11968                     (setq gnus-killed-list (cons group gnus-killed-list))))))
11969          gnus-active-hashtb)
11970         (while groups
11971           (if (gnus-gethash (car groups) gnus-active-hashtb)
11972               (gnus-group-change-level 
11973                (car groups) gnus-level-default-subscribed gnus-level-killed))
11974           (setq groups (cdr groups)))
11975         (gnus-group-make-help-group)
11976         (and gnus-novice-user
11977              (gnus-message 7 "`A k' to list killed groups"))))))
11978
11979 (defun gnus-subscribe-group (group previous &optional method)
11980   (gnus-group-change-level 
11981    (if method
11982        (list t group gnus-level-default-subscribed nil nil method)
11983      group) 
11984    gnus-level-default-subscribed gnus-level-killed previous t))
11985
11986 ;; `gnus-group-change-level' is the fundamental function for changing
11987 ;; subscription levels of newsgroups. This might mean just changing
11988 ;; from level 1 to 2, which is pretty trivial, from 2 to 6 or back
11989 ;; again, which subscribes/unsubscribes a group, which is equally
11990 ;; trivial. Changing from 1-7 to 8-9 means that you kill a group, and
11991 ;; from 8-9 to 1-7 means that you remove the group from the list of
11992 ;; killed (or zombie) groups and add them to the (kinda) subscribed
11993 ;; groups. And last but not least, moving from 8 to 9 and 9 to 8,
11994 ;; which is trivial.
11995 ;; ENTRY can either be a string (newsgroup name) or a list (if
11996 ;; FROMKILLED is t, it's a list on the format (NUM INFO-LIST),
11997 ;; otherwise it's a list in the format of the `gnus-newsrc-hashtb'
11998 ;; entries. 
11999 ;; LEVEL is the new level of the group, OLDLEVEL is the old level and
12000 ;; PREVIOUS is the group (in hashtb entry format) to insert this group
12001 ;; after. 
12002 (defun gnus-group-change-level (entry level &optional oldlevel
12003                                       previous fromkilled)
12004   (let (group info active num)
12005     ;; Glean what info we can from the arguments
12006     (if (consp entry)
12007         (if fromkilled (setq group (nth 1 entry))
12008           (setq group (car (nth 2 entry))))
12009       (setq group entry))
12010     (if (and (stringp entry)
12011              oldlevel 
12012              (< oldlevel gnus-level-zombie))
12013         (setq entry (gnus-gethash entry gnus-newsrc-hashtb)))
12014     (if (and (not oldlevel)
12015              (consp entry))
12016         (setq oldlevel (car (cdr (nth 2 entry)))))
12017     (if (stringp previous)
12018         (setq previous (gnus-gethash previous gnus-newsrc-hashtb)))
12019
12020     (gnus-dribble-enter
12021      (format "(gnus-group-change-level %S %S %S %S %S)" 
12022              group level oldlevel (car (nth 2 previous)) fromkilled))
12023     
12024     ;; Then we remove the newgroup from any old structures, if needed.
12025     ;; If the group was killed, we remove it from the killed or zombie
12026     ;; list. If not, and it is in fact going to be killed, we remove
12027     ;; it from the newsrc hash table and assoc.
12028     (cond ((>= oldlevel gnus-level-zombie)
12029            (if (= oldlevel gnus-level-zombie)
12030                (setq gnus-zombie-list (delete group gnus-zombie-list))
12031              (setq gnus-killed-list (delete group gnus-killed-list))))
12032           (t
12033            (if (and (>= level gnus-level-zombie)
12034                     entry)
12035                (progn
12036                  (gnus-sethash (car (nth 2 entry)) nil gnus-newsrc-hashtb)
12037                  (if (nth 3 entry)
12038                      (setcdr (gnus-gethash (car (nth 3 entry))
12039                                            gnus-newsrc-hashtb)
12040                              (cdr entry)))
12041                  (setcdr (cdr entry) (cdr (cdr (cdr entry))))))))
12042
12043     ;; Finally we enter (if needed) the list where it is supposed to
12044     ;; go, and change the subscription level. If it is to be killed,
12045     ;; we enter it into the killed or zombie list.
12046     (cond ((>= level gnus-level-zombie)
12047            ;; Remove from the hash table.
12048            (gnus-sethash group nil gnus-newsrc-hashtb)
12049            (or (gnus-group-foreign-p group)
12050                ;; We do not enter foreign groups into the list of dead
12051                ;; groups.  
12052                (if (= level gnus-level-zombie)
12053                    (setq gnus-zombie-list (cons group gnus-zombie-list))
12054                  (setq gnus-killed-list (cons group gnus-killed-list)))))
12055           (t
12056            ;; If the list is to be entered into the newsrc assoc, and
12057            ;; it was killed, we have to create an entry in the newsrc
12058            ;; hashtb format and fix the pointers in the newsrc assoc.
12059            (if (>= oldlevel gnus-level-zombie)
12060                (progn
12061                  (if (listp entry)
12062                      (progn
12063                        (setq info (cdr entry))
12064                        (setq num (car entry)))
12065                    (setq active (gnus-gethash group gnus-active-hashtb))
12066                    (setq num (if active (- (1+ (cdr active)) (car active)) t))
12067                    ;; Check whether the group is foreign. If so, the
12068                    ;; foreign select method has to be entered into the
12069                    ;; info. 
12070                    (let ((method (gnus-group-method-name group)))
12071                      (if (eq method gnus-select-method)
12072                          (setq info (list group level nil))
12073                        (setq info (list group level nil nil method)))))
12074                  (or previous 
12075                      (setq previous 
12076                            (let ((p gnus-newsrc-alist))
12077                              (while (cdr (cdr p))
12078                                (setq p (cdr p)))
12079                              p)))
12080                  (setq entry (cons info (cdr (cdr previous))))
12081                  (if (cdr previous)
12082                      (progn
12083                        (setcdr (cdr previous) entry)
12084                        (gnus-sethash group (cons num (cdr previous)) 
12085                                      gnus-newsrc-hashtb))
12086                    (setcdr previous entry)
12087                    (gnus-sethash group (cons num previous)
12088                                  gnus-newsrc-hashtb))
12089                  (if (cdr entry)
12090                      (setcdr (gnus-gethash (car (car (cdr entry)))
12091                                            gnus-newsrc-hashtb)
12092                              entry)))
12093              ;; It was alive, and it is going to stay alive, so we
12094              ;; just change the level and don't change any pointers or
12095              ;; hash table entries.
12096              (setcar (cdr (car (cdr (cdr entry)))) level))))))
12097
12098 (defun gnus-kill-newsgroup (newsgroup)
12099   "Obsolete function. Kills a newsgroup."
12100   (gnus-group-change-level
12101    (gnus-gethash newsgroup gnus-newsrc-hashtb) gnus-level-killed))
12102
12103 (defun gnus-check-bogus-newsgroups (&optional confirm)
12104   "Remove bogus newsgroups.
12105 If CONFIRM is non-nil, the user has to confirm the deletion of every
12106 newsgroup." 
12107   (let ((newsrc (cdr gnus-newsrc-alist))
12108         bogus group entry)
12109     (gnus-message 5 "Checking bogus newsgroups...")
12110     (or gnus-have-read-active-file (gnus-read-active-file))
12111     ;; Find all bogus newsgroup that are subscribed.
12112     (while newsrc
12113       (setq group (car (car newsrc)))
12114       (if (or (gnus-gethash group gnus-active-hashtb) ; Active
12115               (nth 4 (car newsrc))      ; Foreign
12116               (and confirm
12117                    (not (gnus-y-or-n-p
12118                          (format "Remove bogus newsgroup: %s " group)))))
12119           ;; Don't remove.
12120           ()
12121         ;; Found a bogus newsgroup.
12122         (setq bogus (cons group bogus)))
12123       (setq newsrc (cdr newsrc)))
12124     ;; Remove all bogus subscribed groups by first killing them, and
12125     ;; then removing them from the list of killed groups.
12126     (while bogus
12127       (and (setq entry (gnus-gethash (car bogus) gnus-newsrc-hashtb))
12128            (progn
12129              (gnus-group-change-level entry gnus-level-killed)
12130              (setq gnus-killed-list (delete (car bogus) gnus-killed-list))))
12131       (setq bogus (cdr bogus)))
12132     ;; Then we remove all bogus groups from the list of killed and
12133     ;; zombie groups. They are are removed without confirmation.
12134     (let ((dead-lists '(gnus-killed-list gnus-zombie-list))
12135           killed)
12136       (while dead-lists
12137         (setq killed (symbol-value (car dead-lists)))
12138         (while killed
12139           (setq group (car killed))
12140           (or (gnus-gethash group gnus-active-hashtb)
12141               ;; The group is bogus.
12142               (set (car dead-lists)
12143                    (delete group (symbol-value (car dead-lists)))))
12144           (setq killed (cdr killed)))
12145         (setq dead-lists (cdr dead-lists))))
12146     (gnus-message 5 "Checking bogus newsgroups...done")))
12147
12148 (defun gnus-check-duplicate-killed-groups ()
12149   "Remove duplicates from the list of killed groups."
12150   (interactive)
12151   (let ((killed gnus-killed-list))
12152     (while killed
12153       (gnus-message 9 "%d" (length killed))
12154       (setcdr killed (delete (car killed) (cdr killed)))
12155       (setq killed (cdr killed)))))
12156
12157 ;; Go though `gnus-newsrc-alist' and compare with `gnus-active-hashtb'
12158 ;; and compute how many unread articles there are in each group.
12159 (defun gnus-get-unread-articles (&optional level) 
12160   (let* ((newsrc (cdr gnus-newsrc-alist))
12161          (level (or level (1+ gnus-level-subscribed)))
12162          (foreign-level
12163           (min 
12164            (cond ((and gnus-activate-foreign-newsgroups 
12165                        (not (numberp gnus-activate-foreign-newsgroups)))
12166                   (1+ gnus-level-subscribed))
12167                  ((numberp gnus-activate-foreign-newsgroups)
12168                   gnus-activate-foreign-newsgroups)
12169                  (t 0))
12170            level))
12171          info group active virtuals method)
12172     (gnus-message 5 "Checking new news...")
12173
12174     (while newsrc
12175       (setq info (car newsrc)
12176             group (car info)
12177             active (gnus-gethash group gnus-active-hashtb))
12178
12179       ;; Check newsgroups. If the user doesn't want to check them, or
12180       ;; they can't be checked (for instance, if the news server can't
12181       ;; be reached) we just set the number of unread articles in this
12182       ;; newsgroup to t. This means that Gnus thinks that there are
12183       ;; unread articles, but it has no idea how many.
12184       (if (and (setq method (nth 4 info))
12185                (not (gnus-server-equal gnus-select-method
12186                                        (gnus-server-get-method nil method)))
12187                (not (gnus-secondary-method-p method)))
12188           ;; These groups are foreign. Check the level.
12189           (if (<= (nth 1 info) foreign-level)
12190               (if (eq (car (if (stringp method) 
12191                                (gnus-server-to-method method)
12192                              (nth 4 info))) 'nnvirtual)
12193                   ;; We have to activate the virtual groups after all
12194                   ;; the others, so we just pop them on a list for
12195                   ;; now. 
12196                   (setq virtuals (cons info virtuals))
12197                 (and (setq active (gnus-activate-newsgroup (car info)))
12198                      ;; Close the groups as we look at them!
12199                      (gnus-close-group group))))
12200                 
12201         ;; These groups are native or secondary. 
12202         (if (and (not gnus-read-active-file)
12203                  (<= (nth 1 info) level))
12204             (setq active (gnus-activate-newsgroup (car info)))))
12205       
12206       (if active
12207           (gnus-get-unread-articles-in-group info active)
12208         ;; The group couldn't be reached, so we nix out the number of
12209         ;; unread articles and stuff.
12210         (gnus-sethash group nil gnus-active-hashtb)
12211         (setcar (gnus-gethash group gnus-newsrc-hashtb) t))
12212
12213       (setq newsrc (cdr newsrc)))
12214
12215     ;; Activate the virtual groups. This has to be done after all the
12216     ;; other groups. 
12217     ;; !!! If one virtual group contains another virtual group, even
12218     ;; doing it this way might cause problems.
12219    (while virtuals
12220       (and (setq active (gnus-activate-newsgroup (car (car virtuals))))
12221            (gnus-get-unread-articles-in-group (car virtuals) active))
12222       (setq virtuals (cdr virtuals)))
12223
12224     (gnus-message 5 "Checking new news...done")))
12225
12226 ;; Create a hash table out of the newsrc alist. The `car's of the
12227 ;; alist elements are used as keys.
12228 (defun gnus-make-hashtable-from-newsrc-alist ()
12229   (let ((alist gnus-newsrc-alist)
12230         (ohashtb gnus-newsrc-hashtb)
12231         prev)
12232     (setq gnus-newsrc-hashtb (gnus-make-hashtable (length alist)))
12233     (setq alist 
12234           (setq prev (setq gnus-newsrc-alist 
12235                            (if (equal (car (car gnus-newsrc-alist))
12236                                       "dummy.group")
12237                                gnus-newsrc-alist
12238                              (cons (list "dummy.group" 0 nil) alist)))))
12239     (while alist
12240       (gnus-sethash (car (car alist)) 
12241                     (cons (and ohashtb (car (gnus-gethash 
12242                                              (car (car alist)) ohashtb))) 
12243                           prev) gnus-newsrc-hashtb)
12244       (setq prev alist
12245             alist (cdr alist)))))
12246
12247 (defun gnus-make-hashtable-from-killed ()
12248   "Create a hash table from the killed and zombie lists."
12249   (let ((lists '(gnus-killed-list gnus-zombie-list))
12250         list)
12251     (setq gnus-killed-hashtb 
12252           (gnus-make-hashtable 
12253            (+ (length gnus-killed-list) (length gnus-zombie-list))))
12254     (while lists
12255       (setq list (symbol-value (car lists)))
12256       (setq lists (cdr lists))
12257       (while list
12258         (gnus-sethash (car list) (car list) gnus-killed-hashtb)
12259         (setq list (cdr list))))))
12260
12261 (defun gnus-get-unread-articles-in-group (info active)
12262   (let* ((range (nth 2 info))
12263          (num 0)
12264          (marked (nth 3 info)))
12265     ;; If a cache is present, we may have to alter the active info.
12266     (and gnus-use-cache
12267          (gnus-cache-possibly-alter-active (car info) active))
12268     ;; Modify the list of read articles according to what articles 
12269     ;; are available; then tally the unread articles and add the
12270     ;; number to the group hash table entry.
12271     (cond 
12272      ((zerop (cdr active))
12273       (setq num 0))
12274      ((not range)
12275       (setq num (- (1+ (cdr active)) (car active))))
12276      ((not (listp (cdr range)))
12277       ;; Fix a single (num . num) range according to the
12278       ;; active hash table.
12279       ;; Fix by Carsten Bormann <cabo@Informatik.Uni-Bremen.DE>.
12280       (and (< (cdr range) (car active)) (setcdr range (1- (car active))))
12281       (and (> (cdr range) (cdr active)) (setcdr range (cdr active)))
12282       ;; Compute number of unread articles.
12283       (setq num (max 0 (- (cdr active) (- (1+ (cdr range)) (car range))))))
12284      (t
12285       ;; The read list is a list of ranges. Fix them according to
12286       ;; the active hash table.
12287       ;; First peel off any elements that are below the lower
12288       ;; active limit. 
12289       (while (and (cdr range) 
12290                   (>= (car active) 
12291                       (or (and (atom (car (cdr range))) (car (cdr range)))
12292                           (car (car (cdr range))))))
12293         (if (numberp (car range))
12294             (setcar range 
12295                     (cons (car range) 
12296                           (or (and (numberp (car (cdr range)))
12297                                    (car (cdr range))) 
12298                               (cdr (car (cdr range))))))
12299           (setcdr (car range) 
12300                   (or (and (numberp (nth 1 range)) (nth 1 range))
12301                       (cdr (car (cdr range))))))
12302         (setcdr range (cdr (cdr range))))
12303       ;; Adjust the first element to be the same as the lower limit. 
12304       (if (and (not (atom (car range))) 
12305                (< (cdr (car range)) (car active)))
12306           (setcdr (car range) (1- (car active))))
12307       ;; Then we want to peel off any elements that are higher
12308       ;; than the upper active limit.  
12309       (let ((srange range))
12310         ;; Go past all legal elements.
12311         (while (and (cdr srange) 
12312                     (<= (or (and (atom (car (cdr srange)))
12313                                  (car (cdr srange)))
12314                             (car (car (cdr srange)))) (cdr active)))
12315           (setq srange (cdr srange)))
12316         (if (cdr srange)
12317             ;; Nuke all remaining illegal elements.
12318             (setcdr srange nil))
12319
12320         ;; Adjust the final element.
12321         (if (and (not (atom (car srange)))
12322                  (> (cdr (car srange)) (cdr active)))
12323             (setcdr (car srange) (cdr active))))
12324       ;; Compute the number of unread articles.
12325       (while range
12326         (setq num (+ num (- (1+ (or (and (atom (car range)) (car range))
12327                                     (cdr (car range))))
12328                             (or (and (atom (car range)) (car range))
12329                                 (car (car range))))))
12330         (setq range (cdr range)))
12331       (setq num (max 0 (- (cdr active) num)))))
12332     (and info
12333          (progn
12334            (and (assq 'tick marked)
12335                 (inline (gnus-remove-illegal-marked-articles
12336                          (assq 'tick marked) (nth 2 info))))
12337            (and (assq 'dormant marked)
12338                 (inline (gnus-remove-illegal-marked-articles
12339                          (assq 'dormant marked) (nth 2 info))))
12340            (setcar
12341             (gnus-gethash (car info) gnus-newsrc-hashtb) 
12342             (setq num (max 0 (- num (length (cdr (assq 'tick marked)))
12343                                 (length (cdr (assq 'dormant marked)))))))))
12344     num))
12345
12346 (defun gnus-remove-illegal-marked-articles (marked ranges)
12347   (let ((m (cdr marked)))
12348     ;; Make sure that all ticked articles are a subset of the unread
12349     ;; articles. 
12350     (while m
12351       (if (gnus-member-of-range (car m) ranges)
12352           (setcdr marked (cdr m))
12353         (setq marked m))
12354       (setq m (cdr m)))))
12355
12356 (defun gnus-activate-newsgroup (group)
12357   (let ((method (gnus-find-method-for-group group))
12358         active)
12359     (and (or (gnus-server-opened method) (gnus-open-server method))
12360          (gnus-request-group group)
12361          (save-excursion
12362            (set-buffer nntp-server-buffer)
12363            (goto-char (point-min))
12364            (and (looking-at "[0-9]+ [0-9]+ \\([0-9]+\\) [0-9]+")
12365                 (progn
12366                   (goto-char (match-beginning 1))
12367                   (gnus-sethash 
12368                    group (setq active (cons (read (current-buffer))
12369                                             (read (current-buffer))))
12370                    gnus-active-hashtb))
12371                 active)))))
12372
12373 (defun gnus-update-read-articles 
12374   (group unread unselected ticked &optional domarks replied expirable killed
12375          dormant bookmark score)
12376   "Update the list of read and ticked articles in GROUP using the
12377 UNREAD and TICKED lists.
12378 Note: UNSELECTED has to be sorted over `<'.
12379 Returns whether the updating was successful."
12380   (let* ((active (or gnus-newsgroup-active 
12381                      (gnus-gethash group gnus-active-hashtb)))
12382          (entry (gnus-gethash group gnus-newsrc-hashtb))
12383          (info (nth 2 entry))
12384          (marked (nth 3 info))
12385          (prev 1)
12386          (unread (sort (copy-sequence unread) (function <)))
12387          read)
12388     (if (or (not info) (not active))
12389         ;; There is no info on this group if it was, in fact,
12390         ;; killed. Gnus stores no information on killed groups, so
12391         ;; there's nothing to be done. 
12392         ;; One could store the information somewhere temporarily,
12393         ;; perhaps... Hmmm... 
12394         ()
12395       ;; Remove any negative articles numbers.
12396       (while (and unread (< (car unread) 0))
12397         (setq unread (cdr unread)))
12398       ;; Remove any expired article numbers
12399       (while (and unread (< (car unread) (car active)))
12400         (setq unread (cdr unread)))
12401       (while (and ticked (< (car ticked) (car active)))
12402         (setq ticked (cdr ticked)))
12403       (while (and dormant (< (car dormant) (car active)))
12404         (setq dormant (cdr dormant)))
12405       (setq unread (sort (append unselected unread) '<))
12406       ;; Set the number of unread articles in gnus-newsrc-hashtb.
12407       (setcar entry (max 0 (- (length unread) (length ticked) 
12408                               (length dormant))))
12409       ;; Compute the ranges of read articles by looking at the list of
12410       ;; unread articles.  
12411       (while unread
12412         (if (/= (car unread) prev)
12413             (setq read (cons (if (= prev (1- (car unread))) prev
12414                                (cons prev (1- (car unread)))) read)))
12415         (setq prev (1+ (car unread)))
12416         (setq unread (cdr unread)))
12417       (if (<= prev (cdr active))
12418           (setq read (cons (cons prev (cdr active)) read)))
12419       ;; Enter this list into the group info.
12420       (setcar (cdr (cdr info)) 
12421               (if (> (length read) 1) (nreverse read) read))
12422       ;; Enter the list of ticked articles.
12423       (gnus-set-marked-articles 
12424        info ticked
12425        (if domarks replied (cdr (assq 'reply marked)))
12426        (if domarks expirable (cdr (assq 'expire marked)))
12427        (if domarks killed (cdr (assq 'killed marked)))
12428        (if domarks dormant (cdr (assq 'dormant marked)))
12429        (if domarks bookmark (cdr (assq 'bookmark marked)))
12430        (if domarks score (cdr (assq 'score marked))))
12431       t)))
12432
12433 (defun gnus-make-articles-unread (group articles)
12434   "Mark ARTICLES in GROUP as unread."
12435   (let* ((info (nth 2 (or (gnus-gethash group gnus-newsrc-hashtb)
12436                           (gnus-gethash (gnus-group-real-name group)
12437                                         gnus-newsrc-hashtb))))
12438          (ranges (nth 2 info))
12439          news)
12440     (while articles
12441       (and (gnus-member-of-range (car articles) ranges)
12442            (setq news (cons (car articles) news)))
12443       (setq articles (cdr articles)))
12444     (if (not news)
12445         ()
12446       (setcar (nthcdr 2 info)
12447               (gnus-remove-from-range (nth 2 info) (nreverse news)))
12448       (gnus-group-update-group group t))))
12449
12450 ;; Get the active file(s) from the backend(s).
12451 (defun gnus-read-active-file ()
12452   (gnus-group-set-mode-line)
12453   (let ((methods (if (or (gnus-server-opened gnus-select-method)
12454                          (gnus-open-server gnus-select-method))
12455                      ;; The native server is available.
12456                      (cons gnus-select-method gnus-secondary-select-methods)
12457                    ;; The native server is down, so we just do the
12458                    ;; secondary ones.   
12459                    gnus-secondary-select-methods))
12460         list-type)
12461     (setq gnus-have-read-active-file nil)
12462     (save-excursion
12463       (set-buffer nntp-server-buffer)
12464       (while methods
12465         (let* ((where (nth 1 (car methods)))
12466                (mesg (format "Reading active file%s via %s..."
12467                              (if (and where (not (zerop (length where))))
12468                                  (concat " from " where) "")
12469                              (car (car methods)))))
12470           (gnus-message 5 mesg)
12471           (gnus-check-news-server (car methods))
12472           (cond 
12473            ((and (eq gnus-read-active-file 'some)
12474                  (gnus-check-backend-function
12475                   'retrieve-groups (car (car methods))))
12476             (let ((newsrc (cdr gnus-newsrc-alist))
12477                   groups)
12478               (while newsrc
12479                 (and (gnus-server-equal 
12480                       (gnus-find-method-for-group
12481                        (car (car newsrc)) (car newsrc))
12482                       (gnus-server-get-method nil (car methods)))
12483                      (setq groups (cons (car (car newsrc)) groups)))
12484                 (setq newsrc (cdr newsrc)))
12485               (setq list-type (gnus-retrieve-groups groups (car methods)))
12486               (cond ((not list-type)
12487                      (gnus-message 
12488                       1 "Cannot read partial active file from %s server." 
12489                       (car (car methods)))
12490                      (ding)
12491                      (sit-for 2))
12492                     ((eq list-type 'active)
12493                      (gnus-active-to-gnus-format (car methods)))
12494                     (t
12495                      (gnus-groups-to-gnus-format (car methods))))))
12496            (t
12497             (if (not (gnus-request-list (car methods)))
12498                 (progn
12499                   (gnus-message 1 "Cannot read active file from %s server." 
12500                                 (car (car methods)))
12501                   (ding))
12502               (gnus-active-to-gnus-format (car methods))
12503               ;; We mark this active file as read.
12504               (setq gnus-have-read-active-file
12505                     (cons (car methods) gnus-have-read-active-file))
12506               (gnus-message 5 "%sdone" mesg)))))
12507         (setq methods (cdr methods))))))
12508
12509 ;; Read an active file and place the results in `gnus-active-hashtb'.
12510 (defun gnus-active-to-gnus-format (method &optional hashtb ignore-errors)
12511   (let ((cur (current-buffer))
12512         (hashtb (or hashtb 
12513                     (if (and gnus-active-hashtb 
12514                              (not (equal method gnus-select-method)))
12515                         gnus-active-hashtb
12516                       (setq gnus-active-hashtb
12517                             (if (equal method gnus-select-method)
12518                                 (gnus-make-hashtable 
12519                                  (count-lines (point-min) (point-max)))
12520                               (gnus-make-hashtable 4096))))))
12521         (flag-hashtb (gnus-make-hashtable 60)))
12522     ;; Delete unnecessary lines.
12523     (goto-char (point-min))
12524     (while (search-forward "\nto." nil t)
12525       (delete-region (1+ (match-beginning 0)) 
12526                      (progn (forward-line 1) (point))))
12527     (or (string= gnus-ignored-newsgroups "")
12528         (progn
12529           (goto-char (point-min))
12530           (delete-matching-lines gnus-ignored-newsgroups)))
12531     ;; If these are groups from a foreign select method, we insert the
12532     ;; group prefix in front of the group names. 
12533     (and method (not (eq method gnus-select-method))
12534          (let ((prefix (gnus-group-prefixed-name "" method)))
12535            (goto-char (point-min))
12536            (while (and (not (eobp))
12537                        (progn (insert prefix)
12538                               (zerop (forward-line 1)))))))
12539     (goto-char (point-min))
12540     ;; Store active file in hashtable.
12541     (goto-char (point-min))
12542     (if (string-match "%[oO]" gnus-group-line-format)
12543         ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
12544         ;; If we want information on moderated groups, we use this
12545         ;; loop...   
12546         (let* ((mod-hashtb (make-vector 7 0))
12547                (m (intern "m" mod-hashtb))
12548                group max min)
12549           (while (not (eobp))
12550             (condition-case nil
12551                 (progn
12552                   (narrow-to-region (point) (gnus-point-at-eol))
12553                   (setq group (let ((obarray hashtb)) (read cur)))
12554                   (if (and (numberp (setq max (read cur)))
12555                            (numberp (setq min (read cur)))
12556                            (progn 
12557                              (skip-chars-forward " ")
12558                              (not
12559                               (or (= (following-char) ?=)
12560                                   (= (following-char) ?x)
12561                                   (= (following-char) ?j)))))
12562                       (set group (cons min max))
12563                     (set group nil))
12564                   ;; Enter moderated groups into a list.
12565                   (if (eq (let ((obarray mod-hashtb)) (read cur)) m)
12566                       (setq gnus-moderated-list 
12567                             (cons (symbol-name group) gnus-moderated-list))))
12568               (error nil))
12569             (widen)
12570             (forward-line 1)))
12571       ;; And if we do not care about moderation, we use this loop,
12572       ;; which is faster.
12573       (let (group max min)
12574         (while (not (eobp))
12575           (condition-case ()
12576               (progn
12577                 (narrow-to-region (point) (gnus-point-at-eol))
12578                 ;; group gets set to a symbol interned in the hash table
12579                 ;; (what a hack!!) - jwz
12580                 (setq group (let ((obarray hashtb)) (read cur)))
12581                 (if (and (numberp (setq max (read cur)))
12582                          (numberp (setq min (read cur)))
12583                          (progn 
12584                            (skip-chars-forward " ")
12585                            (not
12586                             (or (= (following-char) ?=)
12587                                 (= (following-char) ?x)
12588                                 (= (following-char) ?j)))))
12589                     (set group (cons min max))
12590                   (set group nil)))
12591             (error 
12592              (progn 
12593                (if ignore-errors
12594                    (set group nil)
12595                  (ding) 
12596                  (gnus-message 3 "Warning - illegal active: %s"
12597                                (buffer-substring 
12598                                 (gnus-point-at-bol) (gnus-point-at-eol)))
12599                  nil))))
12600           (widen)
12601           (forward-line 1))))))
12602
12603 (defun gnus-groups-to-gnus-format (method &optional hashtb)
12604   ;; Parse a "groups" active file.
12605   (let ((cur (current-buffer))
12606         (hashtb (or hashtb 
12607                     (if method
12608                         gnus-active-hashtb
12609                       (setq gnus-active-hashtb
12610                             (gnus-make-hashtable 
12611                              (count-lines (point-min) (point-max)))))))
12612         (prefix (and method (not (eq method gnus-select-method))
12613                      (gnus-group-prefixed-name "" method))))
12614
12615     (goto-char (point-min))
12616     (condition-case ()
12617         ;; We split this into to separate loops, one with the prefix
12618         ;; and one without to speed the reading up somewhat.
12619         (if prefix
12620             (let (min max opoint)
12621               (while (not (eobp))
12622                 (read cur) (read cur)
12623                 (setq min (read cur)
12624                       max (read cur)
12625                       opoint (point))
12626                 (skip-chars-forward " \t")
12627                 (insert prefix)
12628                 (goto-char opoint)
12629                 (set (let ((obarray hashtb)) (read cur)) 
12630                      (cons min max))
12631                 (forward-line 1)))
12632           (let (min max)
12633             (while (not (eobp))
12634               (if (= (following-char) ?2)
12635                   (progn
12636                     (read cur) (read cur)
12637                     (setq min (read cur)
12638                           max (read cur))
12639                     (set (let ((obarray hashtb)) (read cur)) 
12640                          (cons min max))))
12641               (forward-line 1))))
12642       (error 
12643        (progn (ding) (gnus-message 3 "Possible error in active file."))))))
12644
12645 (defun gnus-read-newsrc-file (&optional force)
12646   "Read startup file.
12647 If FORCE is non-nil, the .newsrc file is read."
12648   (setq gnus-current-startup-file (gnus-make-newsrc-file gnus-startup-file))
12649   ;; Reset variables that might be defined in the .newsrc.eld file.
12650   (let ((variables gnus-variable-list))
12651     (while variables
12652       (set (car variables) nil)
12653       (setq variables (cdr variables))))
12654   (let* ((newsrc-file gnus-current-startup-file)
12655          (quick-file (concat newsrc-file ".el")))
12656     (save-excursion
12657       ;; We always load the .newsrc.eld file. If always contains
12658       ;; much information that can not be gotten from the .newsrc
12659       ;; file (ticked articles, killed groups, foreign methods, etc.)
12660       (gnus-read-newsrc-el-file quick-file)
12661  
12662       (if (or force
12663               (and (file-newer-than-file-p newsrc-file quick-file)
12664                    (file-newer-than-file-p newsrc-file 
12665                                            (concat quick-file "d")))
12666               (not gnus-newsrc-alist))
12667           ;; We read the .newsrc file. Note that if there if a
12668           ;; .newsrc.eld file exists, it has already been read, and
12669           ;; the `gnus-newsrc-hashtb' has been created. While reading
12670           ;; the .newsrc file, Gnus will only use the information it
12671           ;; can find there for changing the data already read -
12672           ;; ie. reading the .newsrc file will not trash the data
12673           ;; already read (except for read articles).
12674           (save-excursion
12675             (gnus-message 5 "Reading %s..." newsrc-file)
12676             (set-buffer (find-file-noselect newsrc-file))
12677             (buffer-disable-undo (current-buffer))
12678             (gnus-newsrc-to-gnus-format)
12679             (kill-buffer (current-buffer))
12680             (gnus-message 5 "Reading %s...done" newsrc-file))))))
12681
12682 (defun gnus-read-newsrc-el-file (file)
12683   (let ((ding-file (concat file "d")))
12684     ;; We always, always read the .eld file.
12685     (gnus-message 5 "Reading %s..." ding-file)
12686     (let (gnus-newsrc-assoc)
12687       (condition-case nil
12688           (load ding-file t t t)
12689         (error nil))
12690       (and gnus-newsrc-assoc (setq gnus-newsrc-alist gnus-newsrc-assoc)))
12691     (let ((inhibit-quit t))
12692       (gnus-uncompress-newsrc-alist))
12693     (gnus-make-hashtable-from-newsrc-alist)
12694     (if (not (file-newer-than-file-p file ding-file))
12695         ()
12696       ;; Old format quick file
12697       (gnus-message 5 "Reading %s..." file)
12698       ;; The .el file is newer than the .eld file, so we read that one
12699       ;; as well. 
12700       (gnus-read-old-newsrc-el-file file))))
12701
12702 ;; Parse the old-style quick startup file
12703 (defun gnus-read-old-newsrc-el-file (file)
12704   (let (newsrc killed marked group m)
12705     (prog1
12706         (let ((gnus-killed-assoc nil)
12707               gnus-marked-assoc gnus-newsrc-alist gnus-newsrc-assoc)
12708           (prog1
12709               (condition-case nil
12710                   (load file t t t)
12711                 (error nil))
12712             (setq newsrc gnus-newsrc-assoc
12713                   killed gnus-killed-assoc
12714                   marked gnus-marked-assoc)))
12715       (setq gnus-newsrc-alist nil)
12716       (while newsrc
12717         (setq group (car newsrc))
12718         (let ((info (nth 2 (gnus-gethash (car group) gnus-newsrc-hashtb))))
12719           (if info
12720               (progn
12721                 (setcar (nthcdr 2 info) (cdr (cdr group)))
12722                 (setcar (cdr info)
12723                         (if (nth 1 group) gnus-level-default-subscribed 
12724                           gnus-level-default-unsubscribed))
12725                 (setq gnus-newsrc-alist (cons info gnus-newsrc-alist)))
12726             (setq gnus-newsrc-alist
12727                   (cons 
12728                    (setq info
12729                          (list (car group)
12730                                (if (nth 1 group) gnus-level-default-subscribed
12731                                  gnus-level-default-unsubscribed) 
12732                                (cdr (cdr group))))
12733                    gnus-newsrc-alist)))
12734           (if (setq m (assoc (car group) marked))
12735             (setcdr (cdr (cdr info)) (cons (list (cons 'tick (cdr m))) nil))))
12736         (setq newsrc (cdr newsrc)))
12737       (setq newsrc killed)
12738       (while newsrc
12739         (setcar newsrc (car (car newsrc)))
12740         (setq newsrc (cdr newsrc)))
12741       (setq gnus-killed-list killed))
12742     ;; The .el file version of this variable does not begin with
12743     ;; "options", while the .eld version does, so we just add it if it
12744     ;; isn't there.
12745     (and
12746      gnus-newsrc-options 
12747      (progn
12748        (and (not (string-match "^ *options" gnus-newsrc-options))
12749             (setq gnus-newsrc-options (concat "options " gnus-newsrc-options)))
12750        (and (not (string-match "\n$" gnus-newsrc-options))
12751             (setq gnus-newsrc-options (concat gnus-newsrc-options "\n")))))
12752     (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist))
12753     (gnus-make-hashtable-from-newsrc-alist)))
12754       
12755 (defun gnus-make-newsrc-file (file)
12756   "Make server dependent file name by catenating FILE and server host name."
12757   (let* ((file (expand-file-name file nil))
12758          (real-file (concat file "-" (nth 1 gnus-select-method))))
12759     (if (or (file-exists-p real-file)
12760             (file-exists-p (concat real-file ".el"))
12761             (file-exists-p (concat real-file ".eld")))
12762         real-file file)))
12763
12764 (defun gnus-uncompress-newsrc-alist ()
12765   ;; Uncompress all lists of marked articles in the newsrc assoc.
12766   (let ((newsrc gnus-newsrc-alist)
12767         marked)
12768     (while newsrc
12769       (if (not (setq marked (nth 3 (car newsrc))))
12770           ()
12771         (while marked
12772           (or (eq 'score (car (car marked)))
12773               (eq 'bookmark (car (car marked)))
12774               (eq 'killed (car (car marked)))
12775               (setcdr (car marked) (gnus-uncompress-range (cdr (car marked)))))
12776           (setq marked (cdr marked))))
12777       (setq newsrc (cdr newsrc)))))
12778
12779 (defun gnus-compress-newsrc-alist ()
12780   ;; Compress all lists of marked articles in the newsrc assoc.
12781   (let ((newsrc gnus-newsrc-alist)
12782         marked)
12783     (while newsrc
12784       (if (not (setq marked (nth 3 (car newsrc))))
12785           ()
12786         (while marked
12787           (or (eq 'score (car (car marked)))
12788               (eq 'bookmark (car (car marked)))
12789               (eq 'killed (car (car marked)))
12790               (setcdr (car marked) 
12791                       (condition-case ()
12792                           (gnus-compress-sequence 
12793                            (sort (cdr (car marked)) '<) t)
12794                         (error (cdr (car marked))))))
12795           (setq marked (cdr marked))))
12796       (setq newsrc (cdr newsrc)))))
12797
12798 (defun gnus-newsrc-to-gnus-format ()
12799   (setq gnus-newsrc-options "")
12800   (setq gnus-newsrc-options-n nil)
12801
12802   (or gnus-active-hashtb
12803       (setq gnus-active-hashtb (make-vector 4095 0)))
12804   (let ((buf (current-buffer))
12805         (already-read (> (length gnus-newsrc-alist) 1))
12806         group subscribed options-symbol newsrc Options-symbol
12807         symbol reads num1)
12808     (goto-char (point-min))
12809     ;; We intern the symbol `options' in the active hashtb so that we
12810     ;; can `eq' against it later.
12811     (setq options-symbol (intern "options" gnus-active-hashtb))
12812     (setq Options-symbol (intern "Options" gnus-active-hashtb))
12813   
12814     (while (not (eobp))
12815       ;; We first read the first word on the line by narrowing and
12816       ;; then reading into `gnus-active-hashtb'.  Most groups will
12817       ;; already exist in that hashtb, so this will save some string
12818       ;; space.
12819       (narrow-to-region
12820        (point)
12821        (progn (skip-chars-forward "^ \t!:\n") (point)))
12822       (goto-char (point-min))
12823       (setq symbol 
12824             (and (/= (point-min) (point-max))
12825                  (let ((obarray gnus-active-hashtb)) (read buf))))
12826       (widen)
12827       ;; Now, the symbol we have read is either `options' or a group
12828       ;; name.  If it is an options line, we just add it to a string. 
12829       (cond 
12830        ((or (eq symbol options-symbol)
12831             (eq symbol Options-symbol))
12832         (setq gnus-newsrc-options
12833               ;; This concatting is quite inefficient, but since our
12834               ;; thorough studies show that approx 99.37% of all
12835               ;; .newsrc files only contain a single options line, we
12836               ;; don't give a damn, frankly, my dear.
12837               (concat gnus-newsrc-options
12838                       (buffer-substring 
12839                        (gnus-point-at-bol)
12840                        ;; Options may continue on the next line.
12841                        (or (and (re-search-forward "^[^ \t]" nil 'move)
12842                                 (progn (beginning-of-line) (point)))
12843                            (point))))))
12844        (symbol
12845         ;; It was a group name.
12846         (setq subscribed (= (following-char) ?:)
12847               group (symbol-name symbol)
12848               reads nil)
12849         (if (eolp)
12850             ;; If the line ends here, this is clearly a buggy line, so
12851             ;; we put point a the beginning of line and let the cond
12852             ;; below do the error handling.
12853             (beginning-of-line)
12854           ;; We skip to the beginning of the ranges.
12855           (skip-chars-forward "!: \t"))
12856         ;; We are now at the beginning of the list of read articles.
12857         ;; We read them range by range.
12858         (while
12859             (cond 
12860              ((looking-at "[0-9]+")
12861               ;; We narrow and read a number instead of buffer-substring/
12862               ;; string-to-int because it's faster. narrow/widen is
12863               ;; faster than save-restriction/narrow, and save-restriction
12864               ;; produces a garbage object.
12865               (setq num1 (progn
12866                            (narrow-to-region (match-beginning 0) (match-end 0))
12867                            (read buf)))
12868               (widen)
12869               ;; If the next character is a dash, then this is a range.
12870               (if (= (following-char) ?-)
12871                   (progn
12872                     ;; We read the upper bound of the range.
12873                     (forward-char 1)
12874                     (if (not (looking-at "[0-9]+"))
12875                         ;; This is a buggy line, by we pretend that
12876                         ;; it's kinda OK. Perhaps the user should be
12877                         ;; dinged? 
12878                         (setq reads (cons num1 reads))
12879                       (setq reads 
12880                             (cons 
12881                              (cons num1 (progn
12882                                           (narrow-to-region (match-beginning 0) 
12883                                                             (match-end 0))
12884                                           (read buf)))
12885                              reads))
12886                       (widen)))
12887                 ;; It was just a simple number, so we add it to the
12888                 ;; list of ranges.
12889                 (setq reads (cons num1 reads)))
12890               ;; If the next char in ?\n, then we have reached the end
12891               ;; of the line and return nil.
12892               (/= (following-char) ?\n))
12893              ((= (following-char) ?\n)
12894               ;; End of line, so we end.
12895               nil)
12896              (t
12897               ;; Not numbers and not eol, so this might be a buggy
12898               ;; line... 
12899               (or (eobp) ; If it was eob instead of ?\n, we allow it.
12900                   (progn
12901                     ;; The line was buggy.
12902                     (setq group nil)
12903                     (gnus-message 3 "Mangled line: %s" 
12904                                   (buffer-substring (gnus-point-at-bol) 
12905                                                     (gnus-point-at-eol)))
12906                     (ding)
12907                     (sit-for 1)))
12908               nil))
12909           ;; Skip past ", ". Spaces are illegal in these ranges, but
12910           ;; we allow them, because it's a common mistake to put a
12911           ;; space after the comma.
12912           (skip-chars-forward ", "))
12913
12914         ;; We have already read .newsrc.eld, so we gently update the
12915         ;; data in the hash table with the information we have just
12916         ;; read. 
12917         (if (not group)
12918             ()
12919           (let ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
12920                 level)
12921             (if info
12922                 ;; There is an entry for this file in the alist.
12923                 (progn
12924                   (setcar (nthcdr 2 info) (nreverse reads))
12925                   ;; We update the level very gently.  In fact, we
12926                   ;; only change it if there's been a status change
12927                   ;; from subscribed to unsubscribed, or vice versa.
12928                   (setq level (nth 1 info))
12929                   (cond ((and (<= level gnus-level-subscribed)
12930                               (not subscribed))
12931                          (setq level (if reads
12932                                          gnus-level-default-unsubscribed 
12933                                        (1+ gnus-level-default-unsubscribed))))
12934                         ((and (> level gnus-level-subscribed) subscribed)
12935                          (setq level gnus-level-default-subscribed)))
12936                   (setcar (cdr info) level))
12937               ;; This is a new group.
12938               (setq info (list group 
12939                                (if subscribed
12940                                    gnus-level-default-subscribed 
12941                                  (if reads
12942                                      (1+ gnus-level-subscribed)
12943                                    gnus-level-default-unsubscribed))
12944                                (nreverse reads))))
12945             (setq newsrc (cons info newsrc))))))
12946       (forward-line 1))
12947     
12948     (setq newsrc (nreverse newsrc))
12949
12950     (if (not already-read)
12951         ()
12952       ;; We now have two newsrc lists - `newsrc', which is what we
12953       ;; have read from .newsrc, and `gnus-newsrc-alist', which is
12954       ;; what we've read from .newsrc.eld. We have to merge these
12955       ;; lists. We do this by "attaching" any (foreign) groups in the
12956       ;; gnus-newsrc-alist to the (native) group that precedes them. 
12957       (let ((rc (cdr gnus-newsrc-alist))
12958             (prev gnus-newsrc-alist)
12959             entry mentry)
12960         (while rc
12961           (or (null (nth 4 (car rc))) ; It's a native group.
12962               (assoc (car (car rc)) newsrc) ; It's already in the alist.
12963               (if (setq entry (assoc (car (car prev)) newsrc))
12964                   (setcdr (setq mentry (memq entry newsrc))
12965                           (cons (car rc) (cdr mentry)))
12966                 (setq newsrc (cons (car rc) newsrc))))
12967           (setq prev rc
12968                 rc (cdr rc)))))
12969
12970     (setq gnus-newsrc-alist newsrc)
12971     ;; We make the newsrc hashtb.
12972     (gnus-make-hashtable-from-newsrc-alist)
12973
12974     ;; Finally, if we read some options lines, we parse them.
12975     (or (string= gnus-newsrc-options "")
12976         (gnus-newsrc-parse-options gnus-newsrc-options))))
12977
12978 ;; Parse options lines to find "options -n !all rec.all" and stuff.
12979 ;; The return value will be a list on the form
12980 ;; ((regexp1 . ignore)
12981 ;;  (regexp2 . subscribe)...)
12982 ;; When handling new newsgroups, groups that match a `ignore' regexp
12983 ;; will be ignored, and groups that match a `subscribe' regexp will be
12984 ;; subscribed. A line like
12985 ;; options -n !all rec.all
12986 ;; will lead to a list that looks like
12987 ;; (("^rec\\..+" . subscribe) 
12988 ;;  ("^.+" . ignore))
12989 ;; So all "rec.*" groups will be subscribed, while all the other
12990 ;; groups will be ignored. Note that "options -n !all rec.all" is very
12991 ;; different from "options -n rec.all !all". 
12992 (defun gnus-newsrc-parse-options (options)
12993   (let (out eol)
12994     (save-excursion
12995       (gnus-set-work-buffer)
12996       (insert (regexp-quote options))
12997       ;; First we treat all continuation lines.
12998       (goto-char (point-min))
12999       (while (re-search-forward "\n[ \t]+" nil t)
13000         (replace-match " " t t))
13001       ;; Then we transform all "all"s into ".+"s.
13002       (goto-char (point-min))
13003       (while (re-search-forward "\\ball\\b" nil t)
13004         (replace-match ".+" t t))
13005       (goto-char (point-min))
13006       ;; We remove all other options than the "-n" ones.
13007       (while (re-search-forward "[ \t]-[^n][^-]*" nil t)
13008         (replace-match " ")
13009         (forward-char -1))
13010       (goto-char (point-min))
13011
13012       ;; We are only interested in "options -n" lines - we
13013       ;; ignore the other option lines.
13014       (while (re-search-forward "[ \t]-n" nil t)
13015         (setq eol 
13016               (or (save-excursion
13017                     (and (re-search-forward "[ \t]-n" (gnus-point-at-eol) t)
13018                          (- (point) 2)))
13019                   (gnus-point-at-eol)))
13020         ;; Search for all "words"...
13021         (while (re-search-forward "[^ \t,\n]+" eol t)
13022           (if (= (char-after (match-beginning 0)) ?!)
13023               ;; If the word begins with a bang (!), this is a "not"
13024               ;; spec. We put this spec (minus the bang) and the
13025               ;; symbol `ignore' into the list.
13026               (setq out (cons (cons (concat 
13027                                      "^" (buffer-substring 
13028                                           (1+ (match-beginning 0))
13029                                           (match-end 0)))
13030                                     'ignore) out))
13031             ;; There was no bang, so this is a "yes" spec.
13032             (setq out (cons (cons (concat 
13033                                    "^" (buffer-substring (match-beginning 0)
13034                                                          (match-end 0)))
13035                                   'subscribe) out)))))
13036     
13037       (setq gnus-newsrc-options-n out))))
13038                
13039
13040 (defun gnus-save-newsrc-file ()
13041   "Save .newsrc file."
13042   ;; Note: We cannot save .newsrc file if all newsgroups are removed
13043   ;; from the variable gnus-newsrc-alist.
13044   (and (or gnus-newsrc-alist gnus-killed-list)
13045        gnus-current-startup-file
13046        (progn
13047          (run-hooks 'gnus-save-newsrc-hook)
13048          (save-excursion
13049            (if (or (not gnus-dribble-buffer)
13050                    (not (buffer-name gnus-dribble-buffer))
13051                    (zerop (save-excursion
13052                             (set-buffer gnus-dribble-buffer)
13053                             (buffer-size))))
13054                (gnus-message 4 "(No changes need to be saved)")
13055              (if gnus-save-newsrc-file
13056                  (progn
13057                    (gnus-message 5 "Saving %s..." gnus-current-startup-file)
13058                    ;; Make backup file of master newsrc.
13059                    (gnus-gnus-to-newsrc-format)
13060                    (gnus-message 5 "Saving %s...done"
13061                                  gnus-current-startup-file)))
13062              ;; Quickly loadable .newsrc.
13063              (set-buffer (get-buffer-create " *Gnus-newsrc*"))
13064              (set-visited-file-name (concat gnus-current-startup-file ".eld"))
13065              (gnus-add-current-to-buffer-list)
13066              (buffer-disable-undo (current-buffer))
13067              (erase-buffer)
13068              (gnus-message 5 "Saving %s.eld..." gnus-current-startup-file)
13069              (gnus-gnus-to-quick-newsrc-format)
13070              (save-buffer)
13071              (kill-buffer (current-buffer))
13072              (gnus-message 5 "Saving %s.eld...done" gnus-current-startup-file)
13073              (gnus-dribble-delete-file))))))
13074
13075 (defun gnus-gnus-to-quick-newsrc-format ()
13076   "Insert Gnus variables such as gnus-newsrc-alist in lisp format."
13077   (insert ";; (ding) Gnus startup file.\n")
13078   (insert ";; Never delete this file - touch .newsrc instead to force Gnus\n")
13079   (insert ";; to read .newsrc.\n")
13080   (let ((variables gnus-variable-list)
13081         (inhibit-quit t)
13082         (gnus-newsrc-alist (cdr gnus-newsrc-alist))
13083         variable)
13084     ;; insert lisp expressions.
13085     (gnus-compress-newsrc-alist)
13086     (while variables
13087       (setq variable (car variables))
13088       (and (boundp variable)
13089            (symbol-value variable)
13090            (or gnus-save-killed-list (not (eq variable 'gnus-killed-list)))
13091            (insert "(setq " (symbol-name variable) " '"
13092                    (prin1-to-string (symbol-value variable))
13093                    ")\n"))
13094       (setq variables (cdr variables)))
13095     (gnus-uncompress-newsrc-alist)))
13096
13097
13098 (defun gnus-gnus-to-newsrc-format ()
13099   ;; Generate and save the .newsrc file.
13100   (let ((newsrc (cdr gnus-newsrc-alist))
13101         info ranges range)
13102     (save-excursion
13103       (set-buffer (create-file-buffer gnus-current-startup-file))
13104       (set-visited-file-name gnus-current-startup-file)
13105       (buffer-disable-undo (current-buffer))
13106       (erase-buffer)
13107       ;; Write options.
13108       (if gnus-newsrc-options (insert gnus-newsrc-options))
13109       ;; Write subscribed and unsubscribed.
13110       (while newsrc
13111         (setq info (car newsrc))
13112         (if (not (nth 4 info))          ;Don't write foreign groups to .newsrc.
13113             (progn
13114               (insert (car info) (if (> (nth 1 info) gnus-level-subscribed)
13115                                      "!" ":"))
13116               (if (setq ranges (nth 2 info))
13117                   (progn
13118                     (insert " ")
13119                     (if (not (listp (cdr ranges)))
13120                         (if (= (car ranges) (cdr ranges))
13121                             (insert (int-to-string (car ranges)))
13122                           (insert (int-to-string (car ranges)) "-" 
13123                                   (int-to-string (cdr ranges))))
13124                       (while ranges
13125                         (setq range (car ranges)
13126                               ranges (cdr ranges))
13127                         (if (or (atom range) (= (car range) (cdr range)))
13128                             (insert (int-to-string 
13129                                      (or (and (atom range) range) 
13130                                          (car range))))
13131                           (insert (int-to-string (car range)) "-"
13132                                   (int-to-string (cdr range))))
13133                         (if ranges (insert ","))))))
13134               (insert "\n")))
13135         (setq newsrc (cdr newsrc)))
13136       (save-buffer)
13137       (kill-buffer (current-buffer)))))
13138
13139 (defun gnus-read-all-descriptions-files ()
13140   (let ((methods (cons gnus-select-method gnus-secondary-select-methods)))
13141     (while methods
13142       (gnus-read-descriptions-file (car methods))
13143       (setq methods (cdr methods)))
13144     t))
13145
13146 (defun gnus-read-descriptions-file (&optional method)
13147   (let ((method (or method gnus-select-method)))
13148     ;; We create the hashtable whether we manage to read the desc file
13149     ;; to avoid trying to re-read after a failed read.
13150     (or gnus-description-hashtb
13151         (setq gnus-description-hashtb 
13152               (gnus-make-hashtable (length gnus-active-hashtb))))
13153     ;; Mark this method's desc file as read.
13154     (gnus-sethash (gnus-group-prefixed-name "" method) "Has read"
13155                   gnus-description-hashtb)
13156
13157     (gnus-message 5 "Reading descriptions file via %s..." (car method))
13158     (cond 
13159      ((not (or (gnus-server-opened method)
13160                (gnus-open-server method)))
13161       (gnus-message 1 "Couldn't open server")
13162       nil)
13163      ((not (gnus-request-list-newsgroups method))
13164       (gnus-message 1 "Couldn't read newsgroups descriptions")
13165       nil)
13166      (t
13167       (let (group)
13168         (save-excursion
13169           (save-restriction
13170             (set-buffer nntp-server-buffer)
13171             (goto-char (point-min))
13172             (if (or (search-forward "\n.\n" nil t)
13173                     (goto-char (point-max)))
13174                 (progn
13175                   (beginning-of-line)
13176                   (narrow-to-region (point-min) (point))))
13177             (goto-char (point-min))
13178             (while (not (eobp))
13179               ;; If we get an error, we set group to 0, which is not a
13180               ;; symbol... 
13181               (setq group 
13182                     (condition-case ()
13183                         (let ((obarray gnus-description-hashtb))
13184                           ;; Group is set to a symbol interned in this
13185                           ;; hash table.
13186                           (read nntp-server-buffer))
13187                       (error 0)))
13188               (skip-chars-forward " \t")
13189               ;; ... which leads to this line being effectively ignored.
13190               (and (symbolp group)
13191                    (set group (buffer-substring 
13192                                (point) (progn (end-of-line) (point)))))
13193               (forward-line 1))))
13194         (gnus-message 5 "Reading descriptions file...done")
13195         t)))))
13196
13197 (defun gnus-group-get-description (group)
13198   ;; Get the description of a group by sending XGTITLE to the server.
13199   (and (gnus-request-group-description group)
13200        (save-excursion
13201          (set-buffer nntp-server-buffer)
13202          (goto-char (point-min))
13203          (and (looking-at "[^ \t]+[ \t]+\\(.*\\)")
13204               (buffer-substring (match-beginning 1) (match-end 1))))))
13205
13206 ;;;
13207 ;;; Server
13208 ;;;
13209
13210 (defvar gnus-server-mode-hook nil
13211   "Hook run in `gnus-server-mode' buffers.")
13212
13213 (defconst gnus-server-line-format "     {%(%h:%w%)}\n"
13214   "Format of server lines.
13215 It works along the same lines as a normal formatting string,
13216 with some simple extensions.")
13217
13218 (defvar gnus-server-mode-line-format "(ding) List of servers"
13219   "The format specification for the server mode line.")
13220
13221 (defconst gnus-server-line-format-alist
13222   (list (list ?h 'how ?s)
13223         (list ?n 'name ?s)
13224         (list ?w 'where ?s)
13225         ))
13226
13227 (defconst gnus-server-mode-line-format-alist 
13228   (list (list ?S 'news-server ?s)
13229         (list ?M 'news-method ?s)
13230         (list ?u 'user-defined ?s)))
13231
13232 (defvar gnus-server-line-format-spec nil)
13233 (defvar gnus-server-mode-line-format-spec nil)
13234 (defvar gnus-server-killed-servers nil)
13235
13236 (defvar gnus-server-mode-map nil)
13237 (put 'gnus-server-mode 'mode-class 'special)
13238
13239 (if gnus-server-mode-map
13240     nil
13241   (setq gnus-server-mode-map (make-sparse-keymap))
13242   (suppress-keymap gnus-server-mode-map)
13243   (define-key gnus-server-mode-map " " 'gnus-server-read-server)
13244   (define-key gnus-server-mode-map "\r" 'gnus-server-read-server)
13245   (define-key gnus-server-mode-map gnus-mouse-2 'gnus-server-pick-server)
13246   (define-key gnus-server-mode-map "q" 'gnus-server-exit)
13247   (define-key gnus-server-mode-map "l" 'gnus-server-list-servers)
13248   (define-key gnus-server-mode-map "k" 'gnus-server-kill-server)
13249   (define-key gnus-server-mode-map "y" 'gnus-server-yank-server)
13250   (define-key gnus-server-mode-map "c" 'gnus-server-copy-server)
13251   (define-key gnus-server-mode-map "a" 'gnus-server-add-server)
13252   (define-key gnus-server-mode-map "e" 'gnus-server-edit-server))
13253
13254 (defun gnus-server-mode ()
13255   "Major mode for listing and editing servers.
13256
13257 All normal editing commands are switched off.
13258 \\<gnus-server-mode-map>
13259
13260 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]'). 
13261
13262 The following commands are available:
13263
13264 \\{gnus-server-mode-map}"
13265   (interactive)
13266   (if gnus-visual (gnus-server-make-menu-bar))
13267   (kill-all-local-variables)
13268   (setq mode-line-modified "-- ")
13269   (make-local-variable 'mode-line-format)
13270   (setq mode-line-format (copy-sequence mode-line-format))
13271   (and (equal (nth 3 mode-line-format) "   ")
13272        (setcar (nthcdr 3 mode-line-format) ""))
13273   (setq major-mode 'gnus-server-mode)
13274   (setq mode-name "Server")
13275 ;  (gnus-group-set-mode-line)
13276   (setq mode-line-process nil)
13277   (use-local-map gnus-server-mode-map)
13278   (buffer-disable-undo (current-buffer))
13279   (setq truncate-lines t)
13280   (setq buffer-read-only t)
13281   (run-hooks 'gnus-server-mode-hook))
13282
13283 (defun gnus-server-insert-server-line (sformat name method)
13284   (let* ((sformat (or sformat gnus-server-line-format-spec))
13285          (how (car method))
13286          (where (nth 1 method))
13287          b)
13288     (beginning-of-line)
13289     (setq b (point))
13290     ;; Insert the text.
13291     (insert (eval sformat))
13292     (add-text-properties b (1+ b) (list 'gnus-server (intern name)))))
13293
13294 (defun gnus-server-setup-buffer ()
13295   (if (get-buffer gnus-server-buffer)
13296       ()
13297     (save-excursion
13298       (set-buffer (get-buffer-create gnus-server-buffer))
13299       (gnus-server-mode)
13300       (and gnus-carpal (gnus-carpal-setup-buffer 'server)))))
13301
13302 (defun gnus-server-prepare ()
13303   (setq gnus-server-mode-line-format-spec 
13304         (gnus-parse-format gnus-server-mode-line-format 
13305                            gnus-server-mode-line-format-alist))
13306   (setq gnus-server-line-format-spec 
13307         (gnus-parse-format gnus-server-line-format 
13308                            gnus-server-line-format-alist))
13309   (let ((alist gnus-server-alist)
13310         (buffer-read-only nil))
13311     (erase-buffer)
13312     (while alist
13313       (gnus-server-insert-server-line nil (car (car alist)) (cdr (car alist)))
13314       (setq alist (cdr alist))))
13315   (goto-char (point-min))
13316   (gnus-server-position-cursor))
13317
13318 (defun gnus-server-server-name ()
13319   (let ((server (get-text-property (gnus-point-at-bol) 'gnus-server)))
13320     (and server (symbol-name server))))
13321
13322 (defalias 'gnus-server-position-cursor 'gnus-goto-colon)
13323
13324 (defconst gnus-server-edit-buffer "*Gnus edit server*")
13325
13326 (defun gnus-server-update-server (server)
13327   (save-excursion
13328     (set-buffer gnus-server-buffer)
13329     (let ((buffer-read-only nil)
13330           (info (cdr (assoc server gnus-server-alist))))
13331       (gnus-dribble-enter 
13332        (concat "(gnus-server-set-info \"" server "\" '"
13333                (prin1-to-string info) ")"))
13334       ;; Buffer may be narrowed.
13335       (save-restriction
13336         (widen)
13337         (if (gnus-server-goto-server server)
13338             (delete-region (progn (beginning-of-line) (point))
13339                            (progn (forward-line 1) (point))))
13340         (let ((entry (assoc server gnus-server-alist)))
13341           (gnus-server-insert-server-line nil (car entry) (cdr entry))
13342           (gnus-server-position-cursor))))))
13343
13344 (defun gnus-server-set-info (server info)
13345   ;; Enter a select method into the virtual server alist.
13346   (gnus-dribble-enter 
13347    (concat "(gnus-server-set-info \"" server "\" '"
13348            (prin1-to-string info) ")"))
13349   (let* ((server (nth 1 info))
13350          (entry (assoc server gnus-server-alist)))
13351     (if entry (setcdr entry info)
13352       (setq gnus-server-alist
13353             (nconc gnus-server-alist (list (cons server info)))))))
13354
13355 (defun gnus-server-to-method (server)
13356   ;; Map virtual server names to select methods.
13357   (or (and (equal server "native") gnus-select-method)
13358       (cdr (assoc server gnus-server-alist))))
13359
13360 (defun gnus-server-extend-method (group method)
13361   ;; This function "extends" a virtual server.  If the server is
13362   ;; "hello", and the select method is ("hello" (my-var "something")) 
13363   ;; in the group "alt.alt", this will result in a new virtual server
13364   ;; called "helly+alt.alt".
13365   (let ((entry
13366          (gnus-copy-sequence 
13367           (if (equal (car method) "native") gnus-select-method
13368               (cdr (assoc (car method) gnus-server-alist))))))
13369     (setcar (cdr entry) (concat (nth 1 entry) "+" group))
13370     (nconc entry (cdr method))))
13371
13372 (defun gnus-server-get-method (group method)
13373   ;; Input either a server name, and extended server name, or a
13374   ;; select method, and return a select method. 
13375   (cond ((stringp method)
13376          (gnus-server-to-method method))
13377         ((stringp (car method))
13378          (gnus-server-extend-method group method))
13379         (t
13380          (gnus-server-add-address method))))
13381
13382 (defun gnus-server-add-address (method)
13383   (let ((method-name (symbol-name (car method))))
13384     (if (and (memq 'address (assoc method-name gnus-valid-select-methods))
13385              (not (assq (intern (concat method-name "-address")) method)))
13386         (append method (list (list (intern (concat method-name "-address"))
13387                                    (nth 1 method))))
13388       method)))
13389
13390 (defun gnus-server-equal (s1 s2)
13391   (or (equal s1 s2)
13392       (and (= (length s1) (length s2))
13393            (progn
13394              (while (and s1 (member (car s1) s2))
13395                (setq s1 (cdr s1)))
13396              (null s1)))))
13397
13398 ;;; Interactive server functions.
13399
13400 (defun gnus-server-kill-server (server)
13401   "Kill the server on the current line."
13402   (interactive (list (gnus-server-server-name)))
13403   (or (gnus-server-goto-server server)
13404       (if server (error "No such server: %s" server)
13405         (error "No server on the current line")))
13406   (let ((buffer-read-only nil))
13407     (delete-region (progn (beginning-of-line) (point))
13408                    (progn (forward-line 1) (point))))
13409   (setq gnus-server-killed-servers 
13410         (cons (assoc server gnus-server-alist) gnus-server-killed-servers))
13411   (setq gnus-server-alist (delq (car gnus-server-killed-servers)
13412                                 gnus-server-alist))
13413   (gnus-server-position-cursor))
13414
13415 (defun gnus-server-yank-server ()
13416   "Yank the previously killed server."
13417   (interactive)
13418   (or gnus-server-killed-servers
13419       (error "No killed servers to be yanked"))
13420   (let ((alist gnus-server-alist)
13421         (server (gnus-server-server-name))
13422         (killed (car gnus-server-killed-servers)))
13423     (if (not server) 
13424         (setq gnus-server-alist (nconc gnus-server-alist (list killed)))
13425       (if (string= server (car (car gnus-server-alist)))
13426           (setq gnus-server-alist (cons killed gnus-server-alist))
13427         (while (and (cdr alist)
13428                     (not (string= server (car (car (cdr alist))))))
13429           (setq alist (cdr alist)))
13430         (setcdr alist (cons killed (cdr alist)))))
13431     (gnus-server-update-server (car killed))
13432     (setq gnus-server-killed-servers (cdr gnus-server-killed-servers))
13433     (gnus-server-position-cursor)))
13434
13435 (defun gnus-server-exit ()
13436   "Return to the group buffer."
13437   (interactive)
13438   (kill-buffer (current-buffer))
13439   (switch-to-buffer gnus-group-buffer))
13440
13441 (defun gnus-server-list-servers ()
13442   "List all available servers."
13443   (interactive)
13444   (let ((cur (gnus-server-server-name)))
13445     (gnus-server-prepare)
13446     (if cur (gnus-server-goto-server cur)
13447       (goto-char (point-max))
13448       (forward-line -1))
13449     (gnus-server-position-cursor)))
13450
13451 (defun gnus-server-copy-server (from to)
13452   (interactive
13453    (list
13454     (or (gnus-server-server-name)
13455         (error "No server on the current line"))
13456     (read-string "Copy to: ")))
13457   (or from (error "No server on current line"))
13458   (or (and to (not (string= to ""))) (error "No name to copy to"))
13459   (and (assoc to gnus-server-alist) (error "%s already exists" to))
13460   (or (assoc from gnus-server-alist) 
13461       (error "%s: no such server" from))
13462   (let ((to-entry (gnus-copy-sequence (assoc from gnus-server-alist))))
13463     (setcar to-entry to)
13464     (setcar (nthcdr 2 to-entry) to)
13465     (setq gnus-server-killed-servers 
13466           (cons to-entry gnus-server-killed-servers))
13467     (gnus-server-yank-server)))
13468
13469 (defun gnus-server-add-server (how where)
13470   (interactive 
13471    (list (intern (completing-read "Server method: "
13472                                   gnus-valid-select-methods nil t))
13473          (read-string "Server name: ")))
13474   (setq gnus-server-killed-servers 
13475         (cons (list where how where) gnus-server-killed-servers))
13476   (gnus-server-yank-server))
13477
13478 (defun gnus-server-goto-server (server)
13479   "Jump to a server line."
13480   (interactive
13481    (list (completing-read "Goto server: " gnus-server-alist nil t)))
13482   (let ((to (text-property-any (point-min) (point-max) 
13483                                'gnus-server (intern server))))
13484     (and to
13485          (progn
13486            (goto-char to) 
13487            (gnus-server-position-cursor)))))
13488
13489 (defun gnus-server-edit-server (server)
13490   "Edit the server on the current line."
13491   (interactive (list (gnus-server-server-name)))
13492   (or server
13493       (error "No server on current line"))
13494   (let ((winconf (current-window-configuration)))
13495     (get-buffer-create gnus-server-edit-buffer)
13496     (gnus-configure-windows 'edit-server)
13497     (gnus-add-current-to-buffer-list)
13498     (emacs-lisp-mode)
13499     (make-local-variable 'gnus-prev-winconf)
13500     (setq gnus-prev-winconf winconf)
13501     (use-local-map (copy-keymap (current-local-map)))
13502     (let ((done-func '(lambda () 
13503                         "Exit editing mode and update the information."
13504                         (interactive)
13505                         (gnus-server-edit-server-done 'group))))
13506       (setcar (cdr (nth 4 done-func)) server)
13507       (local-set-key "\C-c\C-c" done-func))
13508     (erase-buffer)
13509     (insert ";; Type `C-c C-c' after you have edited the server.\n\n")
13510     (insert (pp-to-string (cdr (assoc server gnus-server-alist))))))
13511
13512 (defun gnus-server-edit-server-done (server)
13513   (interactive)
13514   (set-buffer (get-buffer-create gnus-server-edit-buffer))
13515   (goto-char (point-min))
13516   (let ((form (read (current-buffer)))
13517         (winconf gnus-prev-winconf))
13518     (gnus-server-set-info server form)
13519     (kill-buffer (current-buffer))
13520     (and winconf (set-window-configuration winconf))
13521     (set-buffer gnus-server-buffer)
13522     (gnus-server-update-server (gnus-server-server-name))
13523     (gnus-server-position-cursor)))
13524
13525 (defun gnus-server-read-server (server)
13526   "Browse a server."
13527   (interactive (list (gnus-server-server-name)))
13528   (gnus-browse-foreign-server (gnus-server-to-method server) (current-buffer)))
13529
13530 (defun gnus-mouse-pick-server (e)
13531   (interactive "e")
13532   (mouse-set-point e)
13533   (gnus-server-read-server (gnus-server-server-name)))
13534
13535 ;;;
13536 ;;; entry points into gnus-score.el
13537 ;;;
13538
13539 ;;; Finding score files. 
13540
13541 (defvar gnus-global-score-files nil
13542   "*List of global score files and directories.
13543 Set this variable if you want to use people's score files.  One entry
13544 for each score file or each score file directory.  Gnus will decide
13545 by itself what score files are applicable to which group.
13546
13547 Say you want to use the single score file
13548 \"/ftp.ifi.uio.no@ftp:/pub/larsi/ding/score/soc.motss.SCORE\" and all
13549 score files in the \"/ftp.some-where:/pub/score\" directory.
13550
13551  (setq gnus-global-score-files
13552        '(\"/ftp.ifi.uio.no:/pub/larsi/ding/score/soc.motss.SCORE\"
13553          \"/ftp.some-where:/pub/score\"))")
13554
13555 (defun gnus-score-score-files (group)
13556   "Return a list of all possible score files."
13557   ;; Search and set any global score files.
13558   (and gnus-global-score-files 
13559        (or gnus-internal-global-score-files
13560            (gnus-score-search-global-directories gnus-global-score-files)))
13561   ;; Fix the kill-file dir variable.
13562   (setq gnus-kill-files-directory 
13563         (file-name-as-directory
13564          (or gnus-kill-files-directory "~/News/")))
13565   ;; If we can't read it, there are no score files.
13566   (if (not (file-exists-p (expand-file-name gnus-kill-files-directory)))
13567       (setq gnus-score-file-list nil)
13568     (if (gnus-use-long-file-name 'not-score)
13569         ;; We want long file names.
13570         (if (or (not gnus-score-file-list)
13571                 (not (car gnus-score-file-list))
13572                 (gnus-file-newer-than gnus-kill-files-directory
13573                                       (car gnus-score-file-list)))
13574               (setq gnus-score-file-list 
13575                     (cons (nth 5 (file-attributes gnus-kill-files-directory))
13576                           (nreverse 
13577                            (directory-files 
13578                             gnus-kill-files-directory t 
13579                             (gnus-score-file-regexp))))))
13580       ;; We do not use long file names, so we have to do some
13581       ;; directory traversing.  
13582       (let ((mdir (length (expand-file-name gnus-kill-files-directory)))
13583             (suffixes (list gnus-score-file-suffix gnus-adaptive-file-suffix))
13584             dir files suffix)
13585         (while suffixes
13586           (setq dir (expand-file-name
13587                      (concat gnus-kill-files-directory
13588                              (gnus-replace-chars-in-string group ?. ?/))))
13589           (setq dir (gnus-replace-chars-in-string dir ?: ?/))
13590           (setq suffix (car suffixes)
13591                 suffixes (cdr suffixes))
13592           (if (file-exists-p (concat dir "/" suffix))
13593               (setq files (cons (concat dir "/" suffix) files)))
13594           (while (>= (1+ (length dir)) mdir)
13595             (and (file-exists-p (concat dir "/all/" suffix))
13596                  (setq files (cons (concat dir "/all/" suffix) files)))
13597             (string-match "/[^/]*$" dir)
13598             (setq dir (substring dir 0 (match-beginning 0)))))
13599         (setq gnus-score-file-list 
13600               (cons nil (nreverse files)))))
13601     (cdr gnus-score-file-list)))
13602
13603 (defun gnus-score-file-regexp ()
13604   (concat "\\(" gnus-score-file-suffix 
13605           "\\|" gnus-adaptive-file-suffix "\\)$"))
13606         
13607 (defun gnus-score-find-bnews (group)
13608   "Return a list of score files for GROUP.
13609 The score files are those files in the ~/News directory which matches
13610 GROUP using BNews sys file syntax."
13611   (let* ((sfiles (append (gnus-score-score-files group)
13612                          gnus-internal-global-score-files))
13613          (kill-dir (file-name-as-directory 
13614                     (expand-file-name gnus-kill-files-directory)))
13615          (klen (length kill-dir))
13616          ofiles not-match regexp)
13617     (save-excursion
13618       (set-buffer (get-buffer-create "*gnus score files*"))
13619       (buffer-disable-undo (current-buffer))
13620       ;; Go through all score file names and create regexp with them
13621       ;; as the source.  
13622       (while sfiles
13623         (erase-buffer)
13624         (insert (car sfiles))
13625         (goto-char (point-min))
13626         ;; First remove the suffix itself.
13627         (re-search-forward (concat "." (gnus-score-file-regexp)))
13628         (replace-match "" t t) 
13629         (goto-char (point-min))
13630         (if (looking-at (regexp-quote kill-dir))
13631             ;; If the file name was just "SCORE", `klen' is one character
13632             ;; too much.
13633             (delete-char (min (1- (point-max)) klen))
13634           (goto-char (point-max))
13635           (search-backward "/")
13636           (delete-region (1+ (point)) (point-min)))
13637         ;; If short file names were used, we have to translate slashes.
13638         (goto-char (point-min))
13639         (while (re-search-forward "[/:]" nil t)
13640           (replace-match "." t t))
13641         ;; Translate "all" to ".*".
13642         (while (search-forward "all" nil t)
13643           (replace-match ".*" t t))
13644         (goto-char (point-min))
13645         ;; Deal with "not."s.
13646         (if (looking-at "not.")
13647             (progn
13648               (setq not-match t)
13649               (setq regexp (buffer-substring 5 (point-max))))
13650           (setq regexp (buffer-substring 1 (point-max)))
13651           (setq not-match nil))
13652         ;; Finally - if this resulting regexp matches the group name,
13653         ;; we add this score file to the list of score files
13654         ;; applicable to this group.
13655         (if (or (and not-match
13656                      (not (string-match regexp group)))
13657                 (and (not not-match)
13658                      (string-match regexp group)))
13659             (setq ofiles (cons (car sfiles) ofiles)))
13660         (setq sfiles (cdr sfiles)))
13661       (kill-buffer (current-buffer))
13662       ;; Slight kludge here - the last score file returned should be
13663       ;; the local score file, whether it exists or not. This is so
13664       ;; that any score commands the user enters will go to the right
13665       ;; file, and not end up in some global score file.
13666       (let ((localscore
13667              (expand-file-name
13668               (if (gnus-use-long-file-name 'not-score)
13669                   (concat gnus-kill-files-directory group "." 
13670                           gnus-score-file-suffix)
13671                 (concat gnus-kill-files-directory
13672                         (gnus-replace-chars-in-string group ?. ?/)
13673                         "/" gnus-score-file-suffix)))))
13674         (and (member localscore ofiles)
13675              (delete localscore ofiles))
13676         (setq ofiles (cons localscore ofiles)))
13677       (nreverse ofiles))))
13678
13679 (defun gnus-score-find-single (group)
13680   "Return list containing the score file for GROUP."
13681   (list (gnus-score-file-name group gnus-adaptive-file-suffix)
13682         (gnus-score-file-name group)))
13683
13684 (defun gnus-score-find-hierarchical (group)
13685   "Return list of score files for GROUP.
13686 This includes the score file for the group and all its parents."
13687   (let ((all (copy-sequence '(nil)))
13688         (start 0))
13689     (while (string-match "\\." group (1+ start))
13690       (setq start (match-beginning 0))
13691       (setq all (cons (substring group 0 start) all)))
13692     (setq all (cons group all))
13693     (nconc
13694      (mapcar (lambda (newsgroup)
13695                (gnus-score-file-name newsgroup gnus-adaptive-file-suffix))
13696              (setq all (nreverse all)))
13697      (mapcar 'gnus-score-file-name all))))
13698
13699 (defvar gnus-score-file-alist-cache nil)
13700
13701 (defun gnus-score-find-alist (group)
13702   "Return list of score files for GROUP.
13703 The list is determined from the variable gnus-score-file-alist."
13704   (let ((alist gnus-score-file-multiple-match-alist)
13705         score-files)
13706     ;; if this group has been seen before, return the cached entry
13707     (if (setq score-files (assoc group gnus-score-file-alist-cache))
13708         (cdr score-files)       ; ensures caching of groups with no matches
13709       ;; handle the multiple match alist
13710       (while alist
13711         (and (string-match (car (car alist)) group)
13712              (setq score-files
13713                    (nconc score-files (cdr (car alist)))))
13714         (setq alist (cdr alist)))
13715       (setq alist gnus-score-file-single-match-alist)
13716       ;; handle the single match alist
13717       (catch 'done
13718         (while alist
13719           (and (string-match (car (car alist)) group)
13720                ;; progn used just in case ("regexp") has no files
13721                ;; and score-files is still nil. -sj
13722                ;; this can be construed as a "stop searching here" feature :>
13723                ;; and used to simplify regexps in the single-alist 
13724                (progn
13725                  (setq score-files
13726                        (append score-files (cdr (car alist))))
13727                  (throw 'done nil)))
13728           (setq alist (cdr alist))))
13729       ;; cache the score files
13730       (setq gnus-score-file-alist-cache
13731             (cons (cons group score-files) gnus-score-file-alist-cache))
13732       score-files)))
13733
13734
13735 (defun gnus-possibly-score-headers (&optional trace)
13736   (let ((func gnus-score-find-score-files-function)
13737         score-files)
13738     (and func (not (listp func))
13739          (setq func (list func)))
13740     ;; Go through all the functions for finding score files (or actual
13741     ;; scores) and add them to a list.
13742     (setq score-files (copy-sequence
13743                        (gnus-score-find-alist gnus-newsgroup-name)))
13744     (while func
13745       (and (symbolp (car func))
13746            (fboundp (car func))
13747            (setq score-files 
13748                  (nconc score-files (funcall (car func) gnus-newsgroup-name))))
13749       (setq func (cdr func)))
13750     (if score-files (gnus-score-headers score-files trace))))
13751
13752 (defun gnus-score-file-name (newsgroup &optional suffix)
13753   "Return the name of a score file for NEWSGROUP."
13754   (let ((suffix (or suffix gnus-score-file-suffix)))
13755     (cond  ((or (null newsgroup)
13756                 (string-equal newsgroup ""))
13757             ;; The global score file is placed at top of the directory.
13758             (expand-file-name 
13759              suffix (or gnus-kill-files-directory "~/News")))
13760            ((gnus-use-long-file-name 'not-score)
13761             ;; Append ".SCORE" to newsgroup name.
13762             (expand-file-name (concat newsgroup "." suffix)
13763                               (or gnus-kill-files-directory "~/News")))
13764            (t
13765             ;; Place "SCORE" under the hierarchical directory.
13766             (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
13767                                       "/" suffix)
13768                               (or gnus-kill-files-directory "~/News"))))))
13769
13770 (defun gnus-score-search-global-directories (files)
13771   "Scan all global score directories for score files."
13772   ;; Set the variable `gnus-internal-global-score-files' to all
13773   ;; available global score files.
13774   (interactive (list gnus-global-score-files))
13775   (let (out)
13776     (while files
13777       (if (string-match "/$" (car files))
13778           (setq out (nconc (directory-files 
13779                             (car files) t
13780                             (concat (gnus-score-file-regexp) "$"))))
13781         (setq out (cons (car files) out)))
13782       (setq files (cdr files)))
13783     (setq gnus-internal-global-score-files out)))
13784
13785 ;; Allow redefinition of Gnus functions.
13786
13787 (gnus-ems-redefine)
13788
13789 (provide 'gnus)
13790
13791 ;;; gnus.el ends here