*** 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@sina.tcamc.uh.edu:/pub/emacs/ding-list/" 
174   "*The address of the (ding) archives.")
175
176 (defvar gnus-default-subscribed-newsgroups nil
177   "*This variable lists what newsgroups should be subscribed the first time Gnus is used.
178 It should be a list of strings.
179 If it is `t', Gnus will not do anything special the first time it is
180 started; it'll just use the normal newsgroups subscription methods.")
181
182 (defvar gnus-use-cross-reference t
183   "*Non-nil means that cross referenced articles will be marked as read.
184 If nil, ignore cross references.  If t, mark articles as read in
185 subscribed newsgroups. If neither t nor nil, mark as read in all
186 newsgroups.") 
187
188 (defvar gnus-use-dribble-file t
189   "*Non-nil means that Gnus will use a dribble file to store user updates.
190 If Emacs should crash without saving the .newsrc files, complete
191 information can be restored from the dribble file.")
192
193 (defvar gnus-asynchronous nil
194   "*If non-nil, Gnus will supply backends with data needed for async article fetching.")
195
196 (defvar gnus-asynchronous-article-function nil
197   "*Function for picking articles to pre-fetch, possibly.")
198
199 (defvar gnus-score-file-single-match-alist nil
200   "*Alist mapping regexps to lists of score files.
201 Each element of this alist should be of the form
202         (\"REGEXP\" [ \"SCORE-FILE-1\" ] [ \"SCORE-FILE-2\" ] ... )
203
204 If the name of a group is matched by REGEXP, the corresponding scorefiles
205 will be used for that group.
206 The first match found is used, subsequent matching entries are ignored (to
207 use multiple matches, see gnus-score-file-multiple-match-alist).
208
209 These score files are loaded in addition to any files returned by
210 gnus-score-find-score-files-function (which see).")
211
212 (defvar gnus-score-file-multiple-match-alist nil
213   "*Alist mapping regexps to lists of score files.
214 Each element of this alist should be of the form
215         (\"REGEXP\" [ \"SCORE-FILE-1\" ] [ \"SCORE-FILE-2\" ] ... )
216
217 If the name of a group is matched by REGEXP, the corresponding scorefiles
218 will be used for that group.
219 If multiple REGEXPs match a group, the score files corresponding to each
220 match will be used (for only one match to be used, see
221 gnus-score-file-single-match-alist).
222
223 These score files are loaded in addition to any files returned by
224 gnus-score-find-score-files-function (which see).")
225
226
227 (defvar gnus-score-file-suffix "SCORE"
228   "*Suffix of the score files.")
229
230 (defvar gnus-adaptive-file-suffix "ADAPT"
231   "*Suffix of the adaptive score files.")
232
233 (defvar gnus-score-find-score-files-function 'gnus-score-find-bnews
234   "*Function used to find SCORE files.
235 The function will be called with the group name as the argument, and
236 should return a list of score files to apply to that group.  The score
237 files do not actually have to exist.
238
239 Predefined values are:
240
241 gnus-score-find-single: Only apply the group's own SCORE file.
242 gnus-score-find-hierarchical: Also apply SCORE files from parent groups.
243 gnus-score-find-bnews: Apply SCORE files whose names matches.
244
245 See the documentation to these functions for more information.
246
247 This variable can also be a list of functions to be called.  Each
248 function should either return a list of score files, or a list of
249 score alists.")
250
251 (defvar gnus-score-interactive-default-score 1000
252   "*Scoring commands will raise/lower the score with this number as the default.")
253
254 (defvar gnus-large-newsgroup 200
255   "*The number of articles which indicates a large newsgroup.
256 If the number of articles in a newsgroup is greater than this value,
257 confirmation is required for selecting the newsgroup.")
258
259 ;; Suggested by Andrew Eskilsson <pi92ae@lelle.pt.hk-r.se>.
260 (defvar gnus-no-groups-message "No news is horrible news"
261   "*Message displayed by Gnus when no groups are available.")
262
263 (defvar gnus-use-long-file-name (not (memq system-type '(usg-unix-v xenix)))
264   "*Non-nil means that the default name of a file to save articles in is the group name.
265 If it's nil, the directory form of the group name is used instead.
266
267 If this variable is a list, and the list contains the element
268 `not-score', long file names will not be used for score files; if it
269 contains the element `not-save', long file names will not be used for
270 saving; and if it contains the element `not-kill', long file names
271 will not be used for kill files.")
272
273 (defvar gnus-article-save-directory (or (getenv "SAVEDIR") "~/News/")
274   "*Name of the directory articles will be saved in (default \"~/News\").
275 Initialized from the SAVEDIR environment variable.")
276
277 (defvar gnus-kill-files-directory (or (getenv "SAVEDIR") "~/News/")
278   "*Name of the directory where kill files will be stored (default \"~/News\").
279 Initialized from the SAVEDIR environment variable.")
280
281 (defvar gnus-default-article-saver 'gnus-summary-save-in-rmail
282   "*A function to save articles in your favorite format.
283 The function must be interactively callable (in other words, it must
284 be an Emacs command).
285
286 Gnus provides the following functions:
287
288 * gnus-summary-save-in-rmail (Rmail format)
289 * gnus-summary-save-in-mail (Unix mail format)
290 * gnus-summary-save-in-folder (MH folder)
291 * gnus-summary-save-in-file (article format).
292 * gnus-summary-save-in-vm (use VM's folder format).")
293
294 (defvar gnus-rmail-save-name (function gnus-plain-save-name)
295   "*A function generating a file name to save articles in Rmail format.
296 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
297
298 (defvar gnus-mail-save-name (function gnus-plain-save-name)
299   "*A function generating a file name to save articles in Unix mail format.
300 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
301
302 (defvar gnus-folder-save-name (function gnus-folder-save-name)
303   "*A function generating a file name to save articles in MH folder.
304 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER.")
305
306 (defvar gnus-file-save-name (function gnus-numeric-save-name)
307   "*A function generating a file name to save articles in article format.
308 The function is called with NEWSGROUP, HEADERS, and optional
309 LAST-FILE.")
310
311 (defvar gnus-split-methods nil
312   "*Variable used to suggest where articles are to be saved.
313 The syntax of this variable is the same as `nnmail-split-methods'.  
314
315 For instance, if you would like to save articles related to Gnus in
316 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
317 you could set this variable to something like:
318
319  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
320    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))")
321
322 (defvar gnus-save-score nil
323   "*If non-nil, save group scoring info.")
324
325 (defvar gnus-use-adaptive-scoring nil
326   "*If non-nil, use some adaptive scoring scheme.")
327
328 (defvar gnus-use-cache nil
329   "*If non-nil, Gnus will cache (some) articles locally.")
330
331 (defvar gnus-use-scoring t
332   "*If non-nil, enable scoring.")
333
334 (defvar gnus-fetch-old-headers nil
335   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
336 If an unread article in the group refers to an older, already read (or
337 just marked as read) article, the old article will not normally be
338 displayed in the Summary buffer.  If this variable is non-nil, Gnus
339 will attempt to grab the headers to the old articles, and thereby
340 build complete threads.  If it has the value `some', only enough
341 headers to connect otherwise loose threads will be displayed.
342
343 The server has to support XOVER for any of this to work.")
344
345 (defvar gnus-visual t
346   "*If non-nil, will do various highlighting.
347 If nil, no mouse highlights (or any other highlights) will be
348 performed.  This might speed up Gnus some when generating large group
349 and summary buffers.")
350
351 (defvar gnus-novice-user t
352   "*Non-nil means that you are a usenet novice.
353 If non-nil, verbose messages may be displayed and confirmations may be
354 required.")
355
356 (defvar gnus-expert-user nil
357   "*Non-nil means that you will never be asked for confirmation about anything.
358 And that means *anything*.")
359
360 (defvar gnus-verbose 7
361   "*Integer that says how verbose Gnus should be.
362 The higher the number, the more messages Gnus will flash to say what
363 it's doing.  At zero, Gnus will be totally mute; at five, Gnus will
364 display most important messages; and at ten, Gnus will keep on
365 jabbering all the time.")
366
367 (defvar gnus-keep-same-level nil
368   "*Non-nil means that the next newsgroup after the current will be on the same level.
369 When you type, for instance, `n' after reading the last article in the
370 current newsgroup, you will go to the next newsgroup. If this variable
371 is nil, the next newsgroup will be the next from the group
372 buffer. 
373 If this variable is non-nil, Gnus will either put you in the
374 next newsgroup with the same level, or, if no such newsgroup is
375 available, the next newsgroup with the lowest possible level higher
376 than the current level.
377 If this variable is `best', Gnus will make the next newsgroup the one
378 with the best level.")
379
380 (defvar gnus-summary-make-false-root 'adopt
381   "*nil means that Gnus won't gather loose threads.
382 If the root of a thread has expired or been read in a previous
383 session, the information necessary to build a complete thread has been
384 lost. Instead of having many small sub-threads from this original thread
385 scattered all over the summary buffer, Gnus can gather them. 
386
387 If non-nil, Gnus will try to gather all loose sub-threads from an
388 original thread into one large thread.
389
390 If this variable is non-nil, it should be one of `none', `adopt',
391 `dummy' or `empty'.
392
393 If this variable is `none', Gnus will not make a false root, but just
394 present the sub-threads after another.
395 If this variable is `dummy', Gnus will create a dummy root that will
396 have all the sub-threads as children.
397 If this variable is `adopt', Gnus will make one of the \"children\"
398 the parent and mark all the step-children as such.
399 If this variable is `empty', the \"children\" are printed with empty
400 subject fields.  (Or rather, they will be printed with a string
401 given by the `gnus-summary-same-subject' variable.)")
402
403 (defvar gnus-summary-gather-subject-limit nil
404   "*Maximum length of subject comparisons when gathering loose threads.
405 Use nil to compare full subjects.  Setting this variable to a low
406 number will help gather threads that have been corrupted by
407 newsreaders chopping off subject lines, but it might also mean that
408 unrelated articles that have subject that happen to begin with the
409 same few characters will be incorrectly gathered.
410
411 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
412 comparing subjects.")
413
414 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
415 (defvar gnus-summary-same-subject ""
416   "*String indicating that the current article has the same subject as the previous.
417 This variable will only be used if the value of
418 `gnus-summary-make-false-root' is `empty'.")
419
420 (defvar gnus-summary-goto-unread nil
421   "*If non-nil, marking commands will go to the next unread article.")
422
423 (defvar gnus-group-goto-unread t
424   "*If non-nil, movement commands will go to the next unread and subscribed group.")
425
426 (defvar gnus-check-new-newsgroups t
427   "*Non-nil means that Gnus will add new newsgroups at startup.
428 If this variable is `ask-server', Gnus will ask the server for new
429 groups since the last time it checked. This means that the killed list
430 is no longer necessary, so you could set `gnus-save-killed-list' to
431 nil. 
432
433 A variant is to have this variable be a list of select methods. Gnus
434 will then use the `ask-server' method on all these select methods to
435 query for new groups from all those servers.
436
437 Eg.
438   (setq gnus-check-new-newsgroups 
439         '((nntp \"some.server\") (nntp \"other.server\")))
440
441 If this variable is nil, then you have to tell Gnus explicitly to
442 check for new newsgroups with \\<gnus-group-mode-map>\\[gnus-find-new-newsgroups].")
443
444 (defvar gnus-check-bogus-newsgroups nil
445   "*Non-nil means that Gnus will check and remove bogus newsgroup at startup.
446 If this variable is nil, then you have to tell Gnus explicitly to
447 check for bogus newsgroups with \\<gnus-group-mode-map>\\[gnus-group-check-bogus-groups].")
448
449 (defvar gnus-read-active-file t
450   "*Non-nil means that Gnus will read the entire active file at startup.
451 If this variable is nil, Gnus will only know about the groups in your
452 `.newsrc' file.
453
454 If this variable is `some', Gnus will try to only read the relevant
455 parts of the active file from the server.  Not all servers support
456 this, and it might be quite slow with other servers, but this should
457 generally be faster than both the t and nil value.
458
459 If you set this variable to nil or `some', you probably still want to
460 be told about new newsgroups that arrive.  To do that, set
461 `gnus-check-new-newsgroups' to `ask-server'.  This may not work
462 properly with all servers.")
463
464 (defvar gnus-level-subscribed 5
465   "*Groups with levels less than or equal to this variable are subscribed.")
466
467 (defvar gnus-level-unsubscribed 7
468   "*Groups with levels less than or equal to this variable are unsubscribed.
469 Groups with levels less than `gnus-level-subscribed', which should be
470 less than this variable, are subscribed.")
471
472 (defvar gnus-level-zombie 8
473   "*Groups with this level are zombie groups.")
474
475 (defvar gnus-level-killed 9
476   "*Groups with this level are killed.")
477
478 (defvar gnus-level-default-subscribed 3
479   "*New subscribed groups will be subscribed at this level.")
480
481 (defvar gnus-level-default-unsubscribed 6
482   "*New unsubscribed groups will be unsubscribed at this level.")
483
484 (defvar gnus-activate-foreign-newsgroups nil
485   "*If nil, Gnus will not check foreign newsgroups at startup.
486 If it is non-nil, it should be a number between one and nine. Foreign
487 newsgroups that have a level lower or equal to this number will be
488 activated on startup. For instance, if you want to active all
489 subscribed newsgroups, but not the rest, you'd set this variable to 
490 `gnus-level-subscribed'.
491
492 If you subscribe to lots of newsgroups from different servers, startup
493 might take a while. By setting this variable to nil, you'll save time,
494 but you won't be told how many unread articles there are in the
495 groups.")
496
497 (defvar gnus-save-newsrc-file t
498   "*Non-nil means that Gnus will save the `.newsrc' file.
499 Gnus always saves its own startup file, which is called
500 \".newsrc.eld\".  The file called \".newsrc\" is in a format that can
501 be readily understood by other newsreaders.  If you don't plan on
502 using other newsreaders, set this variable to nil to save some time on
503 exit.")
504
505 (defvar gnus-save-killed-list t
506   "*If non-nil, save the list of killed groups to the startup file.
507 This will save both time (when starting and quitting) and space (both
508 memory and disk), but it will also mean that Gnus has no record of
509 which groups are new and which are old, so the automatic new
510 newsgroups subscription methods become meaningless. You should always
511 set `gnus-check-new-newsgroups' to `ask-server' or nil if you set this
512 variable to nil.")
513
514 (defvar gnus-interactive-catchup t
515   "*If non-nil, require your confirmation when catching up a group.")
516
517 (defvar gnus-interactive-post t
518   "*If non-nil, group name will be asked for when posting.")
519
520 (defvar gnus-interactive-exit t
521   "*If non-nil, require your confirmation when exiting Gnus.")
522
523 (defvar gnus-kill-killed nil
524   "*If non-nil, Gnus will apply kill files to already killed articles.
525 If it is nil, Gnus will never apply kill files to articles that have
526 already been through the scoring process, which might very well save lots
527 of time.")
528
529 (defvar gnus-extract-address-components 'gnus-extract-address-components
530   "*Function for extracting address components from a From header.
531 Two pre-defined function exist: `gnus-extract-address-components',
532 which is the default, quite fast, and too simplistic solution, and
533 `mail-extract-address-components', which works much better, but is
534 slower.")
535
536 (defvar gnus-summary-default-score 0
537   "*Default article score level.
538 If this variable is nil, scoring will be disabled.")
539
540 (defvar gnus-summary-zcore-fuzz 0
541   "*Fuzziness factor for the zcore in the summary buffer.
542 Articles with scores closer than this to `gnus-summary-default-score'
543 will not be marked.")
544
545 (defvar gnus-simplify-subject-fuzzy-regexp nil
546   "*Regular expression that will be removed from subject strings if
547 fuzzy subject simplification is selected.")
548
549 (defvar gnus-group-default-list-level gnus-level-subscribed
550   "*Default listing level. 
551 Ignored if `gnus-group-use-permanent-levels' is 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 ?D
786   "*Mark used for del'd articles.")
787 (defvar gnus-read-mark ?d
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 ?R
800   "*Mark used for articles that have been replied to.")
801 (defvar gnus-process-mark ?# 
802   "*Process mark.")
803 (defvar gnus-ancient-mark ?A
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   Address of the poster (string)
888 %F   Contents of the From: header (string)
889 %x   Contents of the Xref: header (string)
890 %D   Date of the article (string)
891 %d   Date of the article (string) in DD-MMM format
892 %M   Message-id of the article (string)
893 %r   References of the article (string)
894 %c   Number of characters in the article (integer)
895 %L   Number of lines in the article (integer)
896 %I   Indentation based on thread level (a string of spaces)
897 %T   A string with two possible values: 80 spaces if the article
898      is on thread level two or larger and 0 spaces on level one
899 %R   \"R\" if this article has been replied to, \" \" otherwise (character)
900 %U   Status of this article (character, \"D\", \"K\", \"-\" or \" \")
901 %[   Opening bracket (character, \"[\" or \"<\")
902 %]   Closing bracket (character, \"]\" or \">\")
903 %>   Spaces of length thread-level (string)
904 %<   Spaces of length (- 20 thread-level) (string)
905 %i   Article score (number)
906 %z   Article zcore (character)
907 %t   Number of articles under the current thread (number).
908 %e   Whether the thread is empty or not (character).
909 %u   User defined specifier. The next character in the format string should
910      be a letter.  Gnus will call the function gnus-user-format-function-X,
911      where X is the letter following %u. The function will be passed the
912      current header as argument. The function should return a string, which
913      will be inserted into the summary just like information from any other
914      summary specifier.
915
916 Text between %( and %) will be highlighted with `gnus-mouse-face'
917 when the mouse point is placed inside the area.  There can only be one
918 such area.
919
920 The %U (status), %R (replied) and %z (zcore) specs have to be handled
921 with care. For reasons of efficiency, Gnus will compute what column
922 these characters will end up in, and \"hard-code\" that. This means that
923 it is illegal to have these specs after a variable-length spec. Well,
924 you might not be arrested, but your summary buffer will look strange,
925 which is bad enough.
926
927 The smart choice is to have these specs as for to the left as
928 possible. 
929
930 This restriction may disappear in later versions of Gnus.")
931
932 (defvar gnus-summary-dummy-line-format "*  :                          : %S\n"
933   "*The format specification for the dummy roots in the summary buffer.
934 It works along the same lines as a normal formatting string,
935 with some simple extensions.
936
937 %S  The subject")
938
939 (defvar gnus-summary-mode-line-format "(ding) %G/%A %Z"
940   "*The format specification for the summary mode line.")
941
942 (defvar gnus-article-mode-line-format "(ding) %G/%A %S"
943   "*The format specification for the article mode line.")
944
945 (defvar gnus-group-mode-line-format "(ding) List of groups   {%M:%S}  "
946   "*The format specification for the group mode line.")
947
948 (defvar gnus-valid-select-methods
949   '(("nntp" post address prompt-address)
950     ("nnspool" post)
951     ("nnvirtual" none virtual prompt-address) 
952     ("nnmbox" mail respool) 
953     ("nnml" mail respool)
954     ("nnmh" mail respool) 
955     ("nndir" none prompt-address address)
956     ("nneething" none prompt-address)
957     ("nndigest" none) 
958     ("nndoc" none prompt-address) 
959     ("nnbabyl" mail respool) 
960     ("nnkiboze" post virtual) 
961     ("nnsoup" post)
962     ("nnfolder" mail respool))
963   "An alist of valid select methods.
964 The first element of each list lists should be a string with the name
965 of the select method. The other elements may be be the category of
966 this method (ie. `post', `mail', `none' or whatever) or other
967 properties that this method has (like being respoolable).
968 If you implement a new select method, all you should have to change is
969 this variable. I think.")
970
971 (defvar gnus-updated-mode-lines '(group article summary)
972   "*List of buffers that should update their mode lines.
973 The list may contain the symbols `group', `article' and `summary'. If
974 the corresponding symbol is present, Gnus will keep that mode line
975 updated with information that may be pertinent. 
976 If this variable is nil, screen refresh may be quicker.")
977
978 ;; Added by Keinonen Kari <kk85613@cs.tut.fi>.
979 (defvar gnus-mode-non-string-length 21
980   "*Max length of mode-line non-string contents.
981 If this is nil, Gnus will take space as is needed, leaving the rest
982 of the modeline intact.")
983
984 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
985 (defvar gnus-display-type 
986   (condition-case nil
987       (let ((display-resource (x-get-resource ".displayType" "DisplayType")))
988         (cond (display-resource (intern (downcase display-resource)))
989               ((x-display-color-p) 'color)
990               ((x-display-grayscale-p) 'grayscale)
991               (t 'mono)))
992     (error 'mono))
993   "A symbol indicating the display Emacs is running under.
994 The symbol should be one of `color', `grayscale' or `mono'. If Emacs
995 guesses this display attribute wrongly, either set this variable in
996 your `~/.emacs' or set the resource `Emacs.displayType' in your
997 `~/.Xdefaults'. See also `gnus-background-mode'.")
998
999 (defvar gnus-background-mode 
1000   (condition-case nil
1001       (let ((bg-resource (x-get-resource ".backgroundMode"
1002                                          "BackgroundMode"))
1003             (params (frame-parameters)))
1004         (cond (bg-resource (intern (downcase bg-resource)))
1005               ((< (apply '+ (x-color-values
1006                              (cdr (assq 'background-color params))))
1007                   (/ (apply '+ (x-color-values "white")) 3))
1008                'dark)
1009               (t 'light)))
1010     (error 'light))
1011   "A symbol indicating the Emacs background brightness.
1012 The symbol should be one of `light' or `dark'.
1013 If Emacs guesses this frame attribute wrongly, either set this variable in
1014 your `~/.emacs' or set the resource `Emacs.backgroundMode' in your
1015 `~/.Xdefaults'.
1016 See also `gnus-display-type'.")
1017
1018 (defvar gnus-mouse-face 'highlight
1019   "*Face used for mouse highlighting in Gnus.
1020 No mouse highlights will be done if `gnus-visual' is nil.")
1021
1022 (defvar gnus-summary-mark-below nil
1023   "*Mark all articles with a score below this variable as read.
1024 This variable is local to each summary buffer and usually set by the
1025 score file.")  
1026
1027 (defvar gnus-thread-sort-functions '(gnus-thread-sort-by-number)
1028   "*List of functions used for sorting threads in the summary buffer.
1029 By default, threads are sorted by article number.
1030
1031 Each function takes two threads and return non-nil if the first thread
1032 should be sorted before the other.  If you use more than one function,
1033 the primary sort function should be the last.
1034
1035 Ready-mady functions include `gnus-thread-sort-by-number',
1036 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
1037 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
1038 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').")
1039
1040 (defvar gnus-thread-score-function '+
1041   "*Function used for calculating the total score of a thread.
1042
1043 The function is called with the scores of the article and each
1044 subthread and should then return the score of the thread.
1045
1046 Some functions you can use are `+', `max', or `min'.")
1047
1048 (defvar gnus-options-subscribe nil
1049   "*All new groups matching this regexp will be subscribed unconditionally.
1050 Note that this variable deals only with new newsgroups.  This variable
1051 does not affect old newsgroups.")
1052
1053 (defvar gnus-options-not-subscribe nil
1054   "*All new groups matching this regexp will be ignored.
1055 Note that this variable deals only with new newsgroups.  This variable
1056 does not affect old (already subscribed) newsgroups.")
1057
1058 (defvar gnus-auto-expirable-newsgroups nil
1059   "*Groups in which to automatically mark read articles as expirable.
1060 If non-nil, this should be a regexp that should match all groups in
1061 which to perform auto-expiry.  This only makes sense for mail groups.")
1062
1063 (defvar gnus-hidden-properties '(invisible t intangible t)
1064   "Property list to use for hiding text.")
1065
1066 ;; Hooks.
1067
1068 (defvar gnus-group-mode-hook nil
1069   "*A hook for Gnus group mode.")
1070
1071 (defvar gnus-summary-mode-hook nil
1072   "*A hook for Gnus summary mode.
1073 This hook is run before any variables are set in the summary buffer.")
1074
1075 (defvar gnus-article-mode-hook nil
1076   "*A hook for Gnus article mode.")
1077
1078 (defun gnus-summary-exit-hook nil
1079   "*A hook called on exit from the summary buffer.
1080 It calls `gnus-summary-expire-articles' by default.")
1081 (add-hook 'gnus-summary-exit-hook 'gnus-summary-expire-articles)
1082
1083 (defvar gnus-open-server-hook nil
1084   "*A hook called just before opening connection to the news server.")
1085
1086 (defvar gnus-startup-hook nil
1087   "*A hook called at startup.
1088 This hook is called after Gnus is connected to the NNTP server.")
1089
1090 (defvar gnus-get-new-news-hook nil
1091   "*A hook run just before Gnus checks for new news.")
1092
1093 (defvar gnus-group-prepare-function 'gnus-group-prepare-flat
1094   "*A function that is called to generate the group buffer.
1095 The function is called with three arguments: The first is a number;
1096 all group with a level less or equal to that number should be listed,
1097 if the second is non-nil, empty groups should also be displayed. If
1098 the third is non-nil, it is a number. No groups with a level lower
1099 than this number should be displayed.
1100
1101 The only current function implemented is `gnus-group-prepare-flat'.")
1102
1103 (defvar gnus-group-prepare-hook nil
1104   "*A hook called after the group buffer has been generated.
1105 If you want to modify the group buffer, you can use this hook.")
1106
1107 (defvar gnus-summary-prepare-hook nil
1108   "*A hook called after the summary buffer has been generated.
1109 If you want to modify the summary buffer, you can use this hook.")
1110
1111 (defvar gnus-article-prepare-hook nil
1112   "*A hook called after an article has been prepared in the article buffer.
1113 If you want to run a special decoding program like nkf, use this hook.")
1114
1115 (defvar gnus-article-display-hook nil
1116   "*A hook called after the article is displayed in the article buffer.
1117 The hook is designed to change the contents of the article
1118 buffer. Typical functions that this hook may contain are
1119 `gnus-article-hide-headers' (hide selected headers),
1120 `gnus-article-maybe-highlight' (perform fancy article highlighting), 
1121 `gnus-article-hide-signature' (hide signature) and
1122 `gnus-article-treat-overstrike' (turn \"^H_\" into bold characters).")
1123 (add-hook 'gnus-article-display-hook 'gnus-article-hide-headers-if-wanted)
1124 (add-hook 'gnus-article-display-hook 'gnus-article-treat-overstrike)
1125
1126 (defvar gnus-article-x-face-command
1127   "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | xv -quit -"
1128   "String or function to be executed to display an X-Face header.
1129 If it is a string, the command will be executed in a sub-shell
1130 asynchronously. The compressed face will be piped to this command.") 
1131
1132 (defvar gnus-article-x-face-too-ugly nil
1133   "Regexp matching posters whose face shouldn't be shown automatically.")
1134
1135 (defvar gnus-select-group-hook nil
1136   "*A hook called when a newsgroup is selected.
1137
1138 If you'd like to simplify subjects like the
1139 `gnus-summary-next-same-subject' command does, you can use the
1140 following hook:
1141
1142  (setq gnus-select-group-hook
1143       (list
1144         (lambda ()
1145           (mapcar (lambda (header)
1146                      (header-set-subject
1147                       header
1148                       (gnus-simplify-subject
1149                        (header-subject header) 're-only)))
1150                   gnus-newsgroup-headers))))")
1151
1152 (defvar gnus-select-article-hook
1153   '(gnus-summary-show-thread)
1154   "*A hook called when an article is selected.
1155 The default hook shows conversation thread subtrees of the selected
1156 article automatically using `gnus-summary-show-thread'.")
1157
1158 (defvar gnus-apply-kill-hook '(gnus-apply-kill-file)
1159   "*A hook called to apply kill files to a group.
1160 This hook is intended to apply a kill file to the selected newsgroup.
1161 The function `gnus-apply-kill-file' is called by default.
1162
1163 Since a general kill file is too heavy to use only for a few
1164 newsgroups, I recommend you to use a lighter hook function. For
1165 example, if you'd like to apply a kill file to articles which contains
1166 a string `rmgroup' in subject in newsgroup `control', you can use the
1167 following hook:
1168
1169 \(setq gnus-apply-kill-hook
1170       (list
1171         (lambda ()
1172           (cond ((string-match \"control\" gnus-newsgroup-name)
1173                  (gnus-kill \"Subject\" \"rmgroup\")
1174                  (gnus-expunge \"X\"))))))")
1175
1176 (defvar gnus-visual-mark-article-hook 
1177   (list 'gnus-highlight-selected-summary)
1178   "*Hook run after selecting an article in the summary buffer.
1179 It is meant to be used for highlighting the article in some way.  It
1180 is not run if `gnus-visual' is nil.")
1181
1182 (defvar gnus-prepare-article-hook (list 'gnus-inews-insert-signature)
1183   "*A hook called after preparing body, but before preparing header headers.
1184 The default hook (`gnus-inews-insert-signature') inserts a signature
1185 file specified by the variable `gnus-signature-file'.")
1186
1187 (defvar gnus-exit-group-hook nil
1188   "*A hook called when exiting (not quitting) summary mode.")
1189
1190 (defvar gnus-suspend-gnus-hook nil
1191   "*A hook called when suspending (not exiting) Gnus.")
1192
1193 (defvar gnus-exit-gnus-hook nil
1194   "*A hook called when exiting Gnus.")
1195
1196 (defvar gnus-save-newsrc-hook nil
1197   "*A hook called when saving the newsrc file.")
1198
1199 (defvar gnus-summary-update-hook 
1200   (list 'gnus-summary-highlight-line)
1201   "*A hook called when a summary line is changed.
1202 The hook will not be called if `gnus-visual' is nil.
1203
1204 The default function `gnus-summary-highlight-line' will
1205 highlight the line according to the `gnus-summary-highlight'
1206 variable.")
1207
1208 (defvar gnus-mark-article-hook (list 'gnus-summary-mark-unread-as-read)
1209   "*A hook called when an article is selected for the first time.
1210 The hook is intended to mark an article as read (or unread)
1211 automatically when it is selected.")
1212
1213 ;; Remove any hilit infestation.
1214 (add-hook 'gnus-startup-hook
1215           (lambda ()
1216             (remove-hook 'gnus-summary-prepare-hook
1217                          'hilit-rehighlight-buffer-quietly)
1218             (remove-hook 'gnus-summary-prepare-hook 'hilit-install-line-hooks)
1219             (setq gnus-mark-article-hook '(gnus-summary-mark-unread-as-read))
1220             (remove-hook 'gnus-article-prepare-hook
1221                          'hilit-rehighlight-buffer-quietly)))
1222
1223
1224 \f
1225 ;; Internal variables
1226
1227 ;; Avoid highlighting in kill files.
1228 (defvar gnus-summary-inhibit-highlight nil)
1229 (defvar gnus-newsgroup-selected-overlay nil)
1230
1231 (defvar gnus-article-mode-map nil)
1232 (defvar caesar-translate-table nil)
1233 (defvar gnus-dribble-buffer nil)
1234 (defvar gnus-headers-retrieved-by nil)
1235 (defvar gnus-article-reply nil)
1236 (defvar gnus-override-method nil)
1237 (defvar gnus-article-check-size nil)
1238
1239 (defvar gnus-current-score-file nil)
1240 (defvar gnus-internal-global-score-files nil)
1241 (defvar gnus-score-file-list nil)
1242
1243
1244 (defvar gnus-current-move-group nil)
1245
1246 (defvar gnus-newsgroup-dependencies nil)
1247 (defvar gnus-newsgroup-threads nil)
1248 (defvar gnus-newsgroup-async nil)
1249 (defconst gnus-group-edit-buffer "*Gnus edit newsgroup*")
1250
1251 (defvar gnus-newsgroup-adaptive nil)
1252
1253 (defvar gnus-summary-display-table nil)
1254
1255 (defconst gnus-group-line-format-alist
1256   (list (list ?M 'marked ?c)
1257         (list ?S 'subscribed ?c)
1258         (list ?L 'level ?d)
1259         (list ?N 'number ?s)
1260         (list ?I 'number-of-dormant ?d)
1261         (list ?T 'number-of-ticked ?d)
1262         (list ?R 'number-of-read ?s)
1263         (list ?t 'number-total ?d)
1264         (list ?y 'number-of-unread-unticked ?s)
1265         (list ?i 'number-of-ticked-and-dormant ?d)
1266         (list ?g 'group ?s)
1267         (list ?G 'qualified-group ?s)
1268         (list ?D 'newsgroup-description ?s)
1269         (list ?o 'moderated ?c)
1270         (list ?O 'moderated-string ?s)
1271         (list ?p 'process-marked ?c)
1272         (list ?s 'news-server ?s)
1273         (list ?n 'news-method ?s)
1274         (list ?z 'news-method-string ?s)
1275         (list ?u 'user-defined ?s)))
1276
1277 (defconst gnus-summary-line-format-alist 
1278   (list (list ?N 'number ?d)
1279         (list ?S 'subject ?s)
1280         (list ?s 'subject-or-nil ?s)
1281         (list ?n 'name ?s)
1282         (list ?A 'address ?s)
1283         (list ?F 'from ?s)
1284         (list ?x (macroexpand '(header-xref header)) ?s)
1285         (list ?D (macroexpand '(header-date header)) ?s)
1286         (list ?d '(gnus-dd-mmm (header-date header)) ?s)
1287         (list ?M (macroexpand '(header-id header)) ?s)
1288         (list ?r (macroexpand '(header-references header)) ?s)
1289         (list ?c '(or (header-chars header) 0) ?d)
1290         (list ?L 'lines ?d)
1291         (list ?I 'indentation ?s)
1292         (list ?T '(if (= level 0) "" (make-string (frame-width) ? )) ?s)
1293         (list ?R 'replied ?c)
1294         (list ?\[ 'opening-bracket ?c)
1295         (list ?\] 'closing-bracket ?c)
1296         (list ?\> '(make-string level ? ) ?s)
1297         (list ?\< '(make-string (max 0 (- 20 level)) ? ) ?s)
1298         (list ?i 'score ?d)
1299         (list ?z 'score-char ?c)
1300         (list ?U 'unread ?c)
1301         (list ?t '(gnus-summary-number-of-articles-in-thread 
1302                    (or (prog1 gnus-tmp-adopt-thread 
1303                          (setq gnus-tmp-adopt-thread nil))
1304                        (if (boundp 'thread) (symbol-value 'thread)
1305                          thread nil)))
1306                    ?d)
1307         (list ?e '(gnus-summary-number-of-articles-in-thread 
1308                    (or gnus-tmp-adopt-thread 
1309                        (if (boundp 'thread) (symbol-value 'thread)
1310                          thread nil)) t)
1311                    ?c)
1312         (list ?u 'user-defined ?s))
1313   "An alist of format specifications that can appear in summary lines,
1314 and what variables they correspond with, along with the type of the
1315 variable (string, integer, character, etc).")
1316
1317 (defconst gnus-summary-dummy-line-format-alist
1318   (list (list ?S 'subject ?s)
1319         (list ?N 'number ?d)
1320         (list ?u 'user-defined ?s)))
1321
1322 (defconst gnus-summary-mode-line-format-alist 
1323   (list (list ?G 'group-name ?s)
1324         (list ?g '(gnus-short-group-name group-name) ?s)
1325         (list ?A 'article-number ?d)
1326         (list ?Z 'unread-and-unselected ?s)
1327         (list ?V 'gnus-version ?s)
1328         (list ?U 'unread ?d)
1329         (list ?S 'subject ?s)
1330         (list ?e 'unselected ?d)
1331         (list ?u 'user-defined ?s)
1332         (list ?s '(gnus-current-score-file-nondirectory) ?s)))
1333
1334 (defconst gnus-group-mode-line-format-alist 
1335   (list (list ?S 'news-server ?s)
1336         (list ?M 'news-method ?s)
1337         (list ?u 'user-defined ?s)))
1338
1339 (defvar gnus-have-read-active-file nil)
1340
1341 (defconst gnus-maintainer "gnus-bug@ifi.uio.no (The Gnus Bugfixing Girls + Boys)"
1342   "The mail address of the Gnus maintainers.")
1343
1344 (defconst gnus-version "(ding) Gnus v0.93"
1345   "Version number for this version of Gnus.")
1346
1347 (defvar gnus-info-nodes
1348   '((gnus-group-mode            "(gnus)The Group Buffer")
1349     (gnus-summary-mode          "(gnus)The Summary Buffer")
1350     (gnus-article-mode          "(gnus)The Article Buffer"))
1351   "Assoc list of major modes and related Info nodes.")
1352
1353 (defvar gnus-documentation-group-file "~/dgnus/lisp/doc.txt"
1354   "The location of the (ding) Gnus documentation group.")
1355
1356 (defvar gnus-group-buffer "*Group*")
1357 (defvar gnus-summary-buffer "*Summary*")
1358 (defvar gnus-article-buffer "*Article*")
1359 (defvar gnus-server-buffer "*Server*")
1360
1361 (defvar gnus-work-buffer " *gnus work*")
1362
1363 (defvar gnus-buffer-list nil
1364   "Gnus buffers that should be killed on exit.")
1365
1366 (defvar gnus-server-alist nil
1367   "List of available servers.")
1368
1369 (defvar gnus-variable-list
1370   '(gnus-newsrc-options gnus-newsrc-options-n
1371     gnus-newsrc-last-checked-date 
1372     gnus-newsrc-alist gnus-server-alist
1373     gnus-killed-list gnus-zombie-list)
1374   "Gnus variables saved in the quick startup file.")
1375
1376 (defvar gnus-overload-functions
1377   '((news-inews gnus-inews-news "rnewspost")
1378     (caesar-region gnus-caesar-region "rnews"))
1379   "Functions overloaded by gnus.
1380 It is a list of `(original overload &optional file)'.")
1381
1382 (defvar gnus-newsrc-options nil
1383   "Options line in the .newsrc file.")
1384
1385 (defvar gnus-newsrc-options-n nil
1386   "List of regexps representing groups to be subscribed/ignored unconditionally.") 
1387
1388 (defvar gnus-newsrc-last-checked-date nil
1389   "Date Gnus last asked server for new newsgroups.")
1390
1391 (defvar gnus-newsrc-alist nil
1392   "Assoc list of read articles.
1393 gnus-newsrc-hashtb should be kept so that both hold the same information.")
1394
1395 (defvar gnus-newsrc-hashtb nil
1396   "Hashtable of gnus-newsrc-alist.")
1397
1398 (defvar gnus-killed-list nil
1399   "List of killed newsgroups.")
1400
1401 (defvar gnus-killed-hashtb nil
1402   "Hash table equivalent of gnus-killed-list.")
1403
1404 (defvar gnus-zombie-list nil
1405   "List of almost dead newsgroups.")
1406
1407 (defvar gnus-description-hashtb nil
1408   "Descriptions of newsgroups.")
1409
1410 (defvar gnus-list-of-killed-groups nil
1411   "List of newsgroups that have recently been killed by the user.")
1412
1413 (defvar gnus-active-hashtb nil
1414   "Hashtable of active articles.")
1415
1416 (defvar gnus-moderated-list nil
1417   "List of moderated newsgroups.")
1418
1419 (defvar gnus-group-marked nil)
1420
1421 (defvar gnus-current-startup-file nil
1422   "Startup file for the current host.")
1423
1424 (defvar gnus-last-search-regexp nil
1425   "Default regexp for article search command.")
1426
1427 (defvar gnus-last-shell-command nil
1428   "Default shell command on article.")
1429
1430 (defvar gnus-current-select-method nil
1431   "The current method for selecting a newsgroup.")
1432
1433 (defvar gnus-have-all-newsgroups nil)
1434
1435 (defvar gnus-article-internal-prepare-hook nil)
1436
1437 (defvar gnus-newsgroup-name nil)
1438 (defvar gnus-newsgroup-begin nil)
1439 (defvar gnus-newsgroup-end nil)
1440 (defvar gnus-newsgroup-last-rmail nil)
1441 (defvar gnus-newsgroup-last-mail nil)
1442 (defvar gnus-newsgroup-last-folder nil)
1443 (defvar gnus-newsgroup-last-file nil)
1444 (defvar gnus-newsgroup-auto-expire nil)
1445 (defvar gnus-newsgroup-active nil)
1446
1447 (defvar gnus-newsgroup-unreads nil
1448   "List of unread articles in the current newsgroup.")
1449
1450 (defvar gnus-newsgroup-unselected nil
1451   "List of unselected unread articles in the current newsgroup.")
1452
1453 (defvar gnus-newsgroup-marked nil
1454   "List of ticked articles in the current newsgroup (a subset of unread art).")
1455
1456 (defvar gnus-newsgroup-killed nil
1457   "List of ranges of articles that have been through the scoring process.")
1458
1459 (defvar gnus-newsgroup-kill-headers nil)
1460
1461 (defvar gnus-newsgroup-replied nil
1462   "List of articles that have been replied to in the current newsgroup.")
1463
1464 (defvar gnus-newsgroup-expirable nil
1465   "List of articles in the current newsgroup that can be expired.")
1466
1467 (defvar gnus-newsgroup-processable nil
1468   "List of articles in the current newsgroup that can be processed.")
1469
1470 (defvar gnus-newsgroup-bookmarks nil
1471   "List of articles in the current newsgroup that have bookmarks.")
1472
1473 (defvar gnus-newsgroup-dormant nil
1474   "List of dormant articles in the current newsgroup.")
1475
1476 (defvar gnus-newsgroup-scored nil
1477   "List of scored articles in the current newsgroup.")
1478
1479 (defvar gnus-newsgroup-headers nil
1480   "List of article headers in the current newsgroup.")
1481 (defvar gnus-newsgroup-headers-hashtb-by-number nil)
1482
1483 (defvar gnus-newsgroup-ancient nil
1484   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1485
1486 (defvar gnus-current-article nil)
1487 (defvar gnus-article-current nil)
1488 (defvar gnus-current-headers nil)
1489 (defvar gnus-have-all-headers nil)
1490 (defvar gnus-last-article nil)
1491 (defvar gnus-newsgroup-history nil)
1492 (defvar gnus-current-kill-article nil)
1493
1494 ;; Save window configuration.
1495 (defvar gnus-prev-winconf nil)
1496
1497 ;; Format specs
1498 (defvar gnus-summary-line-format-spec nil)
1499 (defvar gnus-summary-dummy-line-format-spec nil)
1500 (defvar gnus-group-line-format-spec nil)
1501 (defvar gnus-summary-mode-line-format-spec nil)
1502 (defvar gnus-article-mode-line-format-spec nil)
1503 (defvar gnus-group-mode-line-format-spec nil)
1504 (defvar gnus-summary-mark-positions nil)
1505
1506 (defvar gnus-summary-expunge-below nil)
1507 (defvar gnus-reffed-article-number nil)
1508
1509 (defvar rmail-default-file (expand-file-name "~/XMBOX"))
1510 (defvar rmail-default-rmail-file (expand-file-name "~/XNEWS"))
1511
1512 (defconst gnus-summary-local-variables 
1513   '(gnus-newsgroup-name 
1514     gnus-newsgroup-begin gnus-newsgroup-end 
1515     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail 
1516     gnus-newsgroup-last-folder gnus-newsgroup-last-file 
1517     gnus-newsgroup-auto-expire gnus-newsgroup-unreads 
1518     gnus-newsgroup-unselected gnus-newsgroup-marked
1519     gnus-newsgroup-replied gnus-newsgroup-expirable
1520     gnus-newsgroup-processable gnus-newsgroup-killed
1521     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1522     gnus-newsgroup-headers gnus-newsgroup-headers-hashtb-by-number
1523     gnus-current-article gnus-current-headers gnus-have-all-headers
1524     gnus-last-article gnus-article-internal-prepare-hook
1525     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1526     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1527     gnus-newsgroup-threads gnus-newsgroup-async
1528     gnus-score-alist gnus-current-score-file gnus-summary-expunge-below 
1529     gnus-summary-mark-below gnus-newsgroup-active gnus-scores-exclude-files
1530     gnus-newsgroup-history gnus-newsgroup-ancient
1531     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring))
1532   "Variables that are buffer-local to the summary buffers.")
1533
1534 (defconst gnus-bug-message
1535   "Sending a bug report to the Gnus Towers.
1536 ========================================
1537
1538 The buffer below is a mail buffer.  When you press `C-c C-c', it will
1539 be sent to the Gnus Bug Exterminators. 
1540
1541 At the bottom of the buffer you'll see lots of variable settings.
1542 Please do not delete those.  They will tell the Bug People what your
1543 environment is, so that it will be easier to locate the bugs.
1544
1545 If you have found a bug that makes Emacs go \"beep\", set
1546 debug-on-error to t (`M-x set-variable RET debug-on-error RET t RET') 
1547 and include the backtrace in your bug report.
1548
1549 Please describe the bug in annoying, painstaking detail.
1550
1551 Thank you for your help in stamping out bugs.
1552 ")
1553
1554 ;;; End of variables.
1555
1556 ;; Define some autoload functions Gnus might use.
1557 (eval-and-compile
1558
1559   ;; Various 
1560   (autoload 'metamail-buffer "metamail")
1561   (autoload 'Info-goto-node "info")
1562   (autoload 'hexl-hex-string-to-integer "hexl")
1563   (autoload 'pp "pp")
1564   (autoload 'pp-to-string "pp")
1565   (autoload 'pp-eval-expression "pp")
1566   (autoload 'mail-extract-address-components "mail-extr")
1567
1568   (autoload 'nnmail-split-fancy "nnmail")
1569   (autoload 'nnvirtual-catchup-group "nnvirtual")
1570
1571   ;; timezone
1572   (autoload 'timezone-make-date-arpa-standard "timezone")
1573   (autoload 'timezone-fix-time "timezone")
1574   (autoload 'timezone-make-sortable-date "timezone")
1575   (autoload 'timezone-make-time-string "timezone")
1576
1577   ;; rmail & friends
1578   (autoload 'mail-position-on-field "sendmail")
1579   (autoload 'mail-setup "sendmail")
1580   (autoload 'rmail-output "rmailout")
1581   (autoload 'news-mail-other-window "rnewspost")
1582   (autoload 'news-reply-yank-original "rnewspost")
1583   (autoload 'news-caesar-buffer-body "rnewspost")
1584   (autoload 'rmail-insert-rmail-file-header "rmail")
1585   (autoload 'rmail-count-new-messages "rmail")
1586   (autoload 'rmail-show-message "rmail")
1587
1588   ;; gnus-soup
1589   (autoload 'gnus-group-brew-soup "gnus-soup" nil t)
1590   (autoload 'gnus-brew-soup "gnus-soup" nil t)
1591   (autoload 'gnus-soup-add-article "gnus-soup" nil t)
1592   (autoload 'gnus-soup-send-replies "gnus-soup" nil t)
1593   (autoload 'gnus-soup-save-areas "gnus-soup" nil t)
1594   (autoload 'gnus-soup-pack-packet "gnus-soup" nil t)
1595   (autoload 'nnsoup-pack-replies "nnsoup" nil t)
1596
1597   ;; gnus-mh
1598   (autoload 'gnus-mail-reply-using-mhe "gnus-mh")
1599   (autoload 'gnus-mail-forward-using-mhe "gnus-mh")
1600   (autoload 'gnus-mail-other-window-using-mhe "gnus-mh")
1601   (autoload 'gnus-summary-save-in-folder "gnus-mh")
1602   (autoload 'gnus-summary-save-article-folder "gnus-mh")
1603   (autoload 'gnus-Folder-save-name "gnus-mh")
1604   (autoload 'gnus-folder-save-name "gnus-mh")
1605
1606   ;; gnus-vis misc
1607   (autoload 'gnus-group-make-menu-bar "gnus-vis")
1608   (autoload 'gnus-summary-make-menu-bar "gnus-vis")
1609   (autoload 'gnus-server-make-menu-bar "gnus-vis")
1610   (autoload 'gnus-article-make-menu-bar "gnus-vis")
1611   (autoload 'gnus-browse-make-menu-bar "gnus-vis")
1612   (autoload 'gnus-highlight-selected-summary "gnus-vis")
1613   (autoload 'gnus-summary-highlight-line "gnus-vis")
1614   (autoload 'gnus-carpal-setup-buffer "gnus-vis")
1615
1616   ;; gnus-vis article
1617   (autoload 'gnus-article-push-button "gnus-vis" nil t)
1618   (autoload 'gnus-article-press-button "gnus-vis" nil t)
1619   (autoload 'gnus-article-highlight "gnus-vis" nil t)
1620   (autoload 'gnus-article-hide "gnus-vis" nil t)
1621   (autoload 'gnus-article-hide-signature "gnus-vis" nil t)
1622   (autoload 'gnus-article-highlight-headers "gnus-vis" nil t)
1623   (autoload 'gnus-article-highlight-signature "gnus-vis" nil t)
1624   (autoload 'gnus-article-add-buttons "gnus-vis" nil t)
1625   (autoload 'gnus-article-next-button "gnus-vis" nil t)
1626   (autoload 'gnus-article-add-button "gnus-vis")
1627
1628   ;; gnus-cite
1629   (autoload 'gnus-article-highlight-citation "gnus-cite" nil t)
1630   (autoload 'gnus-article-hide-citation-maybe "gnus-cite" nil t)
1631   (autoload 'gnus-article-hide-citation "gnus-cite" nil t)
1632
1633   ;; gnus-kill
1634   (autoload 'gnus-kill "gnus-kill")
1635   (autoload 'gnus-apply-kill-file-internal "gnus-kill")
1636   (autoload 'gnus-kill-file-edit-file "gnus-kill")
1637   (autoload 'gnus-kill-file-raise-followups-to-author "gnus-kill")
1638   (autoload 'gnus-execute "gnus-kill")
1639   (autoload 'gnus-expunge "gnus-kill")
1640
1641   ;; gnus-cache
1642   (autoload 'gnus-cache-possibly-enter-article "gnus-cache")
1643   (autoload 'gnus-cache-save-buffers "gnus-cache")
1644   (autoload 'gnus-cache-possibly-remove-articles "gnus-cache")
1645   (autoload 'gnus-cache-request-article "gnus-cache")
1646   (autoload 'gnus-cache-retrieve-headers "gnus-cache")
1647   (autoload 'gnus-cache-possibly-alter-active "gnus-cache")
1648   (autoload 'gnus-jog-cache "gnus-cache" nil t)
1649   (autoload 'gnus-cache-enter-remove-article "gnus-cache")
1650
1651   ;; gnus-score
1652   (autoload 'gnus-summary-increase-score "gnus-score" nil t)
1653   (autoload 'gnus-summary-lower-score "gnus-score" nil t)
1654   (autoload 'gnus-summary-score-map "gnus-score" nil nil 'keymap)
1655   (autoload 'gnus-score-save "gnus-score")
1656   (autoload 'gnus-score-headers "gnus-score")
1657   (autoload 'gnus-current-score-file-nondirectory "gnus-score")
1658   (autoload 'gnus-score-adaptive "gnus-score")
1659   (autoload 'gnus-score-remove-lines-adaptive "gnus-score")
1660   (autoload 'gnus-score-find-trace "gnus-score")
1661
1662   ;; gnus-edit
1663   (autoload 'gnus-score-customize "gnus-edit" nil t)
1664
1665   ;; gnus-uu
1666   (autoload 'gnus-uu-extract-map "gnus-uu" nil nil 'keymap)
1667   (autoload 'gnus-uu-mark-map "gnus-uu" nil nil 'keymap)
1668   (autoload 'gnus-uu-digest-mail-forward "gnus-uu" nil t)
1669   (autoload 'gnus-uu-digest-post-forward "gnus-uu" nil t)
1670   (autoload 'gnus-uu-mark-series "gnus-uu" nil t)
1671   (autoload 'gnus-uu-mark-region "gnus-uu" nil t)
1672   (autoload 'gnus-uu-mark-by-regexp "gnus-uu" nil t)
1673   (autoload 'gnus-uu-mark-all "gnus-uu" nil t)
1674   (autoload 'gnus-uu-mark-sparse "gnus-uu" nil t)
1675   (autoload 'gnus-uu-mark-thread "gnus-uu" nil t)
1676   (autoload 'gnus-uu-decode-uu "gnus-uu" nil t)
1677   (autoload 'gnus-uu-decode-uu-and-save "gnus-uu" nil t)
1678   (autoload 'gnus-uu-decode-unshar "gnus-uu" nil t)
1679   (autoload 'gnus-uu-decode-unshar-and-save "gnus-uu" nil t)
1680   (autoload 'gnus-uu-decode-save "gnus-uu" nil t)
1681   (autoload 'gnus-uu-decode-binhex "gnus-uu" nil t)
1682   (autoload 'gnus-uu-decode-uu-view "gnus-uu" nil t)
1683   (autoload 'gnus-uu-decode-uu-and-save-view "gnus-uu" nil t)
1684   (autoload 'gnus-uu-decode-unshar-view "gnus-uu" nil t)
1685   (autoload 'gnus-uu-decode-unshar-and-save-view "gnus-uu" nil t)
1686   (autoload 'gnus-uu-decode-save-view "gnus-uu" nil t)
1687   (autoload 'gnus-uu-decode-binhex-view "gnus-uu" nil t)
1688
1689   ;; gnus-msg
1690   (autoload 'gnus-summary-send-map "gnus-msg" nil nil 'keymap)
1691   (autoload 'gnus-group-post-news "gnus-msg" nil t)
1692   (autoload 'gnus-group-mail "gnus-msg" nil t)
1693   (autoload 'gnus-summary-post-news "gnus-msg" nil t)
1694   (autoload 'gnus-summary-followup "gnus-msg" nil t)
1695   (autoload 'gnus-summary-followup-with-original "gnus-msg" nil t)
1696   (autoload 'gnus-summary-followup-and-reply "gnus-msg" nil t)
1697   (autoload 'gnus-summary-followup-and-reply-with-original "gnus-msg" nil t)
1698   (autoload 'gnus-summary-cancel-article "gnus-msg" nil t)
1699   (autoload 'gnus-summary-supersede-article "gnus-msg" nil t)
1700   (autoload 'gnus-post-news "gnus-msg" nil t)
1701   (autoload 'gnus-inews-news "gnus-msg" nil t)
1702   (autoload 'gnus-cancel-news "gnus-msg" nil t)
1703   (autoload 'gnus-summary-reply "gnus-msg" nil t)
1704   (autoload 'gnus-summary-reply-with-original "gnus-msg" nil t)
1705   (autoload 'gnus-summary-mail-forward "gnus-msg" nil t)
1706   (autoload 'gnus-summary-mail-other-window "gnus-msg" nil t)
1707   (autoload 'gnus-mail-reply-using-mail "gnus-msg")
1708   (autoload 'gnus-mail-yank-original "gnus-msg")
1709   (autoload 'gnus-mail-send-and-exit "gnus-msg")
1710   (autoload 'gnus-mail-forward-using-mail "gnus-msg")
1711   (autoload 'gnus-mail-other-window-using-mail "gnus-msg")
1712   (autoload 'gnus-article-mail-with-original "gnus-msg")
1713   (autoload 'gnus-article-mail "gnus-msg")
1714   (autoload 'gnus-bug "gnus-msg" nil t)
1715
1716   ;; gnus-vm
1717   (autoload 'gnus-summary-save-in-vm "gnus-vm" nil t)
1718   (autoload 'gnus-summary-save-article-vm "gnus-vm" nil t)
1719   (autoload 'gnus-mail-forward-using-vm "gnus-vm")
1720   (autoload 'gnus-mail-reply-using-vm "gnus-vm")
1721   (autoload 'gnus-mail-other-window-using-vm "gnus-vm" nil t)
1722   (autoload 'gnus-yank-article "gnus-vm" nil t)
1723
1724   )
1725
1726 \f
1727
1728 ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
1729 ;; If you want the cursor to go somewhere else, set these two
1730 ;; functions in some startup hook to whatever you want.
1731 (defalias 'gnus-summary-position-cursor 'gnus-goto-colon)
1732 (defalias 'gnus-group-position-cursor 'gnus-goto-colon)
1733
1734 ;;; Various macros and substs.
1735
1736 (defmacro gnus-eval-in-buffer-window (buffer &rest forms)
1737   "Pop to BUFFER, evaluate FORMS, and then returns to original window."
1738   (` (let ((GnusStartBufferWindow (selected-window)))
1739        (unwind-protect
1740            (progn
1741              (pop-to-buffer (, buffer))
1742              (,@ forms))
1743          (select-window GnusStartBufferWindow)))))
1744
1745 (defmacro gnus-gethash (string hashtable)
1746   "Get hash value of STRING in HASHTABLE."
1747   ;;(` (symbol-value (abbrev-symbol (, string) (, hashtable))))
1748   ;;(` (abbrev-expansion (, string) (, hashtable)))
1749   (` (symbol-value (intern-soft (, string) (, hashtable)))))
1750
1751 (defmacro gnus-sethash (string value hashtable)
1752   "Set hash value. Arguments are STRING, VALUE, and HASHTABLE."
1753   ;; We cannot use define-abbrev since it only accepts string as value.
1754   ;; (set (intern string hashtable) value))
1755   (` (set (intern (, string) (, hashtable)) (, value))))
1756
1757 (defsubst gnus-buffer-substring (beg end)
1758   (buffer-substring (match-beginning beg) (match-end end)))
1759
1760 ;; modified by MORIOKA Tomohiko <morioka@jaist.ac.jp>
1761 ;;   function `substring' might cut on a middle of multi-octet
1762 ;;   character.
1763
1764 (defun gnus-truncate-string (str width)
1765   (substring str 0 width))
1766
1767 ;; Added by Geoffrey T. Dairiki <dairiki@u.washington.edu>. A safe way
1768 ;; to limit the length of a string. This function is necessary since
1769 ;; `(substr "abc" 0 30)' pukes with "Args out of range".
1770 (defsubst gnus-limit-string (str width)
1771   (if (> (length str) width)
1772       (substring str 0 width)
1773     str))
1774
1775 (defsubst gnus-simplify-subject-re (subject)
1776   "Remove \"Re:\" from subject lines."
1777   (let ((case-fold-search t))
1778     (if (string-match "^re: *" subject)
1779         (substring subject (match-end 0))
1780       subject)))
1781
1782 (defsubst gnus-goto-char (point)
1783   (and point (goto-char point)))
1784
1785 (defmacro gnus-buffer-exists-p (buffer)
1786   (` (and (, buffer)
1787           (funcall (if (stringp (, buffer)) 'get-buffer 'buffer-name)
1788                    (, buffer)))))
1789
1790 (defmacro gnus-kill-buffer (buffer)
1791   (` (if (gnus-buffer-exists-p (, buffer))
1792          (kill-buffer (, buffer)))))
1793
1794 (defsubst gnus-point-at-bol ()
1795   "Return point at the beginning of line."
1796   (let ((p (point)))
1797     (beginning-of-line)
1798     (prog1
1799         (point)
1800       (goto-char p))))
1801
1802 (defsubst gnus-point-at-eol ()
1803   "Return point at the beginning of line."
1804   (let ((p (point)))
1805     (end-of-line)
1806     (prog1
1807         (point)
1808       (goto-char p))))
1809
1810 ;; Delete the current line (and the next N lines.);
1811 (defmacro gnus-delete-line (&optional n)
1812   (` (delete-region (progn (beginning-of-line) (point))
1813                     (progn (forward-line (, (or n 1))) (point)))))
1814
1815 ;;; Load the compatability functions. 
1816
1817 (require 'gnus-ems)
1818
1819 \f
1820 ;;;
1821 ;;; Gnus Utility Functions
1822 ;;;
1823
1824 (defun gnus-extract-address-components (from)
1825   (let (name address)
1826     ;; First find the address - the thing with the @ in it.  This may
1827     ;; not be accurate in mail addresses, but does the trick most of
1828     ;; the time in news messages.
1829     (if (string-match "\\b[^@ \t<>]+[!@][^@ \t<>]+\\b" from)
1830         (setq address (substring from (match-beginning 0) (match-end 0))))
1831     ;; Then we check whether the "name <address>" format is used.
1832     (and address
1833          (string-match (concat "<" (regexp-quote address) ">") from)
1834          (and (setq name (substring from 0 (1- (match-beginning 0))))
1835               ;; Strip any quotes from the name.
1836               (string-match "\".*\"" name)
1837               (setq name (substring name 1 (1- (match-end 0))))))
1838     ;; If not, then "address (name)" is used.
1839     (or name
1840         (and (string-match "(.+)" from)
1841              (setq name (substring from (1+ (match-beginning 0)) 
1842                                    (1- (match-end 0)))))
1843         (and (string-match "()" from)
1844              (setq name address))
1845         ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>.
1846         ;; XOVER might not support folded From headers.
1847         (and (string-match "(.*" from)
1848              (setq name (substring from (1+ (match-beginning 0)) 
1849                                    (match-end 0)))))
1850     ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
1851     (list (or name from) (or address from))))
1852
1853 (defun gnus-fetch-field (field)
1854   "Return the value of the header FIELD of current article."
1855   (save-excursion
1856     (save-restriction
1857       (let ((case-fold-search t))
1858         (gnus-narrow-to-headers)
1859         (mail-fetch-field field)))))
1860
1861 (defun gnus-goto-colon ()
1862   (beginning-of-line)
1863   (search-forward ":" (gnus-point-at-eol) t))
1864
1865 (defun gnus-narrow-to-headers ()
1866   (widen)
1867   (save-excursion
1868     (narrow-to-region
1869      (goto-char (point-min))
1870      (if (search-forward "\n\n" nil t)
1871          (1- (point))
1872        (point-max)))))
1873
1874 (defun gnus-update-format-specifications ()
1875   (gnus-make-thread-indent-array)
1876   (setq gnus-summary-line-format-spec 
1877         (gnus-parse-format
1878          gnus-summary-line-format gnus-summary-line-format-alist))
1879   (gnus-update-summary-mark-positions)
1880   (setq gnus-summary-dummy-line-format-spec 
1881         (gnus-parse-format gnus-summary-dummy-line-format 
1882                            gnus-summary-dummy-line-format-alist))
1883   (setq gnus-group-line-format-spec
1884         (gnus-parse-format 
1885          gnus-group-line-format 
1886          gnus-group-line-format-alist))
1887   (if (and (string-match "%D" gnus-group-line-format)
1888            (not gnus-description-hashtb)
1889            gnus-read-active-file)
1890       (gnus-read-all-descriptions-files))
1891   (setq gnus-summary-mode-line-format-spec 
1892         (gnus-parse-format gnus-summary-mode-line-format 
1893                            gnus-summary-mode-line-format-alist))
1894   (setq gnus-article-mode-line-format-spec 
1895         (gnus-parse-format gnus-article-mode-line-format 
1896                            gnus-summary-mode-line-format-alist))
1897   (setq gnus-group-mode-line-format-spec 
1898         (gnus-parse-format gnus-group-mode-line-format 
1899                            gnus-group-mode-line-format-alist)))
1900
1901 (defun gnus-update-summary-mark-positions ()
1902   (save-excursion
1903     (let ((gnus-replied-mark 129)
1904           (gnus-score-below-mark 130)
1905           (gnus-score-over-mark 130)
1906           (thread nil)
1907           pos)
1908       (gnus-set-work-buffer)
1909       (gnus-summary-insert-line 
1910        nil [0 "" "" "" "" "" 0 0 ""]  0 nil 128 t nil "" nil 1)
1911       (goto-char (point-min))
1912       (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
1913                                          (- (point) 2)))))
1914       (goto-char (point-min))
1915       (setq pos (cons (cons 'replied (and (search-forward "\201" nil t)
1916                                           (- (point) 2))) pos))
1917       (goto-char (point-min))
1918       (setq pos (cons (cons 'score (and (search-forward "\202" nil t)
1919                                         (- (point) 2))) pos))
1920       (setq gnus-summary-mark-positions pos))))
1921
1922 (defun gnus-format-max-width (form length)
1923   (let* ((val (eval form))
1924          (valstr (if (numberp val) (int-to-string val) val)))
1925     (gnus-limit-string valstr length)))
1926
1927 (defun gnus-set-mouse-face (string)
1928   ;; Set mouse face property on STRING.
1929   (put-text-property 0 (length string) 'mouse-face gnus-mouse-face string)
1930   string)
1931
1932 (defun gnus-parse-format (format spec-alist)
1933   ;; This function parses the FORMAT string with the help of the
1934   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
1935   ;; string.  If the FORMAT string contains the specifiers %( and %)
1936   ;; the text between them will have the mouse-face text property.
1937   (if (string-match "\\`\\(.*\\)%(\\(.*\\)%)\\(.*\n?\\)\\'" format)
1938       (if (and gnus-visual gnus-mouse-face)
1939           (let ((pre (substring format (match-beginning 1) (match-end 1)))
1940                 (button (substring format (match-beginning 2) (match-end 2)))
1941                 (post (substring format (match-beginning 3) (match-end 3))))
1942             (list 'concat
1943                   (gnus-parse-simple-format pre spec-alist)
1944                   (list 'gnus-set-mouse-face
1945                         (gnus-parse-simple-format button spec-alist))
1946                   (gnus-parse-simple-format post spec-alist)))
1947         (gnus-parse-simple-format
1948          (concat (substring format (match-beginning 1) (match-end 1))
1949                  (substring format (match-beginning 2) (match-end 2))
1950                  (substring format (match-beginning 3) (match-end 3)))
1951          spec-alist))
1952     (gnus-parse-simple-format format spec-alist)))
1953
1954 (defun gnus-parse-simple-format (format spec-alist)
1955   ;; This function parses the FORMAT string with the help of the
1956   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
1957   ;; string. The list will consist of the symbol `format', a format
1958   ;; specification string, and a list of forms depending on the
1959   ;; SPEC-ALIST.
1960   (let ((max-width 0)
1961         spec flist fstring newspec elem beg)
1962     (save-excursion
1963       (gnus-set-work-buffer)
1964       (insert format)
1965       (goto-char (point-min))
1966       (while (re-search-forward "%[-0-9]*\\(,[0-9]*\\)*\\(.\\)\\(.\\)?" nil t)
1967         (setq spec (string-to-char (buffer-substring (match-beginning 2)
1968                                                      (match-end 2))))
1969         ;; First check if there are any specs that look anything like
1970         ;; "%12,12A", ie. with a "max width specification". These have
1971         ;; to be treated specially.
1972         (if (setq beg (match-beginning 1))
1973             (setq max-width 
1974                   (string-to-int 
1975                    (buffer-substring (1+ (match-beginning 1)) (match-end 1))))
1976           (setq max-width 0)
1977           (setq beg (match-beginning 2)))
1978         ;; Find the specification from `spec-alist'.
1979         (if (not (setq elem (cdr (assq spec spec-alist))))
1980             (setq elem '("*" ?s)))
1981         ;; Treat user defined format specifiers specially
1982         (and (eq (car elem) 'user-defined)
1983              (setq elem
1984                    (list 
1985                     (list (intern (concat "gnus-user-format-function-"
1986                                           (buffer-substring
1987                                            (match-beginning 3)
1988                                            (match-end 3))))
1989                           'header)
1990                     ?s))
1991              (delete-region (match-beginning 3) (match-end 3)))
1992         (if (not (zerop max-width))
1993             (let ((el (car elem)))
1994               (cond ((= (car (cdr elem)) ?c) 
1995                      (setq el (list 'char-to-string el)))
1996                     ((= (car (cdr elem)) ?d)
1997                      (numberp el) (setq el (list 'int-to-string el))))
1998               (setq flist (cons (list 'gnus-format-max-width el max-width) 
1999                                 flist))
2000               (setq newspec ?s))
2001           (setq flist (cons (car elem) flist))
2002           (setq newspec (car (cdr elem))))
2003         ;; Remove the old specification (and possibly a ",12" string).
2004         (delete-region beg (match-end 2))
2005         ;; Insert the new specification.
2006         (goto-char beg)
2007         (insert newspec))
2008       (setq fstring (buffer-substring 1 (point-max))))
2009     (cons 'format (cons fstring (nreverse flist)))))
2010
2011 ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
2012 (defun gnus-read-init-file ()
2013   (and gnus-init-file
2014        (or (and (file-exists-p gnus-init-file) 
2015                 ;; Don't try to load a directory.
2016                 (not (file-directory-p gnus-init-file)))
2017            (file-exists-p (concat gnus-init-file ".el"))
2018            (file-exists-p (concat gnus-init-file ".elc")))
2019        (load gnus-init-file nil t)))
2020
2021 (defun gnus-set-work-buffer ()
2022   (if (get-buffer gnus-work-buffer)
2023       (progn
2024         (set-buffer gnus-work-buffer)
2025         (erase-buffer))
2026     (set-buffer (get-buffer-create gnus-work-buffer))
2027     (kill-all-local-variables)
2028     (buffer-disable-undo (current-buffer))
2029     (gnus-add-current-to-buffer-list)))
2030
2031 ;; Article file names when saving.
2032
2033 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
2034   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2035 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group/num.
2036 Otherwise, it is like ~/News/news/group/num."
2037   (let ((default
2038           (expand-file-name
2039            (concat (if (gnus-use-long-file-name 'not-save)
2040                        (gnus-capitalize-newsgroup newsgroup)
2041                      (gnus-newsgroup-directory-form newsgroup))
2042                    "/" (int-to-string (header-number headers)))
2043            (or gnus-article-save-directory "~/News"))))
2044     (if (and last-file
2045              (string-equal (file-name-directory default)
2046                            (file-name-directory last-file))
2047              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2048         default
2049       (or last-file default))))
2050
2051 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
2052   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2053 If variable `gnus-use-long-file-name' is nil, it is ~/News/news.group/num.
2054 Otherwise, it is like ~/News/news/group/num."
2055   (let ((default
2056           (expand-file-name
2057            (concat (if (gnus-use-long-file-name 'not-save)
2058                        newsgroup
2059                      (gnus-newsgroup-directory-form newsgroup))
2060                    "/" (int-to-string (header-number headers)))
2061            (or gnus-article-save-directory "~/News"))))
2062     (if (and last-file
2063              (string-equal (file-name-directory default)
2064                            (file-name-directory last-file))
2065              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2066         default
2067       (or last-file default))))
2068
2069 (defun gnus-Plain-save-name (newsgroup headers &optional last-file)
2070   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2071 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group.
2072 Otherwise, it is like ~/News/news/group/news."
2073   (or last-file
2074       (expand-file-name
2075        (if (gnus-use-long-file-name 'not-save)
2076            (gnus-capitalize-newsgroup newsgroup)
2077          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2078        (or gnus-article-save-directory "~/News"))))
2079
2080 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
2081   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2082 If variable `gnus-use-long-file-name' is nil, it is ~/News/news.group.
2083 Otherwise, it is like ~/News/news/group/news."
2084   (or last-file
2085       (expand-file-name
2086        (if (gnus-use-long-file-name 'not-save)
2087            newsgroup
2088          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2089        (or gnus-article-save-directory "~/News"))))
2090
2091 ;; For subscribing new newsgroup
2092
2093 (defun gnus-subscribe-hierarchical-interactive (groups)
2094   (let ((groups (sort groups 'string<))
2095         prefixes prefix start ans group starts)
2096     (while groups
2097       (setq prefixes (list "^"))
2098       (while (and groups prefixes)
2099         (while (not (string-match (car prefixes) (car groups)))
2100           (setq prefixes (cdr prefixes)))
2101         (setq prefix (car prefixes))
2102         (setq start (1- (length prefix)))
2103         (if (and (string-match "[^\\.]\\." (car groups) start)
2104                  (cdr groups)
2105                  (setq prefix 
2106                        (concat "^" (substring (car groups) 0 (match-end 0))))
2107                  (string-match prefix (car (cdr groups))))
2108             (progn
2109               (setq prefixes (cons prefix prefixes))
2110               (message "Descend hierarchy %s? ([y]nsq): " 
2111                        (substring prefix 1 (1- (length prefix))))
2112               (setq ans (read-char))
2113               (cond ((= ans ?n)
2114                      (while (and groups 
2115                                  (string-match prefix 
2116                                                (setq group (car groups))))
2117                        (setq gnus-killed-list 
2118                              (cons group gnus-killed-list))
2119                        (gnus-sethash group group gnus-killed-hashtb)
2120                        (setq groups (cdr groups)))
2121                      (setq starts (cdr starts)))
2122                     ((= ans ?s)
2123                      (while (and groups 
2124                                  (string-match prefix 
2125                                                (setq group (car groups))))
2126                        (gnus-sethash group group gnus-killed-hashtb)
2127                        (gnus-subscribe-alphabetically (car groups))
2128                        (setq groups (cdr groups)))
2129                      (setq starts (cdr starts)))
2130                     ((= ans ?q)
2131                      (while groups
2132                        (setq group (car groups))
2133                        (setq gnus-killed-list (cons group gnus-killed-list))
2134                        (gnus-sethash group group gnus-killed-hashtb)
2135                        (setq groups (cdr groups))))
2136                     (t nil)))
2137           (message "Subscribe %s? ([n]yq)" (car groups))
2138           (setq ans (read-char))
2139           (setq group (car groups))
2140           (cond ((= ans ?y)
2141                  (gnus-subscribe-alphabetically (car groups))
2142                  (gnus-sethash group group gnus-killed-hashtb))
2143                 ((= ans ?q)
2144                  (while groups
2145                    (setq group (car groups))
2146                    (setq gnus-killed-list (cons group gnus-killed-list))
2147                    (gnus-sethash group group gnus-killed-hashtb)
2148                    (setq groups (cdr groups))))
2149                 (t 
2150                  (setq gnus-killed-list (cons group gnus-killed-list))
2151                  (gnus-sethash group group gnus-killed-hashtb)))
2152           (setq groups (cdr groups)))))))
2153
2154 (defun gnus-subscribe-randomly (newsgroup)
2155   "Subscribe new NEWSGROUP by making it the first newsgroup."
2156   (gnus-subscribe-newsgroup newsgroup))
2157
2158 (defun gnus-subscribe-alphabetically (newgroup)
2159   "Subscribe new NEWSGROUP and insert it in alphabetical order."
2160   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2161   (let ((groups (cdr gnus-newsrc-alist))
2162         before)
2163     (while (and (not before) groups)
2164       (if (string< newgroup (car (car groups)))
2165           (setq before (car (car groups)))
2166         (setq groups (cdr groups))))
2167     (gnus-subscribe-newsgroup newgroup before)))
2168
2169 (defun gnus-subscribe-hierarchically (newgroup)
2170   "Subscribe new NEWSGROUP and insert it in hierarchical newsgroup order."
2171   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2172   (save-excursion
2173     (set-buffer (find-file-noselect gnus-current-startup-file))
2174     (let ((groupkey newgroup)
2175           before)
2176       (while (and (not before) groupkey)
2177         (goto-char (point-min))
2178         (let ((groupkey-re
2179                (concat "^\\(" (regexp-quote groupkey) ".*\\)[!:]")))
2180           (while (and (re-search-forward groupkey-re nil t)
2181                       (progn
2182                         (setq before (buffer-substring
2183                                       (match-beginning 1) (match-end 1)))
2184                         (string< before newgroup)))))
2185         ;; Remove tail of newsgroup name (eg. a.b.c -> a.b)
2186         (setq groupkey
2187               (if (string-match "^\\(.*\\)\\.[^.]+$" groupkey)
2188                   (substring groupkey (match-beginning 1) (match-end 1)))))
2189       (gnus-subscribe-newsgroup newgroup before))))
2190
2191 (defun gnus-subscribe-interactively (newsgroup)
2192   "Subscribe new NEWSGROUP interactively.
2193 It is inserted in hierarchical newsgroup order if subscribed. If not,
2194 it is killed."
2195   (if (gnus-y-or-n-p (format "Subscribe new newsgroup: %s " newsgroup))
2196       (gnus-subscribe-hierarchically newsgroup)
2197     (setq gnus-killed-list (cons newsgroup gnus-killed-list))))
2198
2199 (defun gnus-subscribe-zombies (newsgroup)
2200   "Make new NEWSGROUP a zombie group."
2201   (setq gnus-zombie-list (cons newsgroup gnus-zombie-list)))
2202
2203 (defun gnus-subscribe-newsgroup (newsgroup &optional next)
2204   "Subscribe new NEWSGROUP.
2205 If NEXT is non-nil, it is inserted before NEXT. Otherwise it is made
2206 the first newsgroup."
2207   ;; We subscribe the group by changing its level to `subscribed'.
2208   (gnus-group-change-level 
2209    newsgroup gnus-level-default-subscribed
2210    gnus-level-killed (gnus-gethash (or next "dummy.group") gnus-newsrc-hashtb))
2211   (gnus-message 5 "Subscribe newsgroup: %s" newsgroup))
2212
2213 ;; For directories
2214
2215 (defun gnus-newsgroup-directory-form (newsgroup)
2216   "Make hierarchical directory name from NEWSGROUP name."
2217   (let ((newsgroup (substring newsgroup 0)) ;Copy string.
2218         (len (length newsgroup))
2219         idx)
2220     ;; If this is a foreign group, we don't want to translate the
2221     ;; entire name.  
2222     (if (setq idx (string-match ":" newsgroup))
2223         (aset newsgroup idx ?/)
2224       (setq idx 0))
2225     ;; Replace all occurrences of `.' with `/'.
2226     (while (< idx len)
2227       (if (= (aref newsgroup idx) ?.)
2228           (aset newsgroup idx ?/))
2229       (setq idx (1+ idx)))
2230     newsgroup))
2231
2232 (defun gnus-make-directory (dir)
2233   "Make DIRECTORY recursively."
2234   (let* ((dir (expand-file-name dir default-directory))
2235          dirs)
2236     (if (string-match "/$" dir)
2237         (setq dir (substring dir 0 (match-beginning 0))))
2238     (while (not (file-exists-p dir))
2239       (setq dirs (cons dir dirs))
2240       (string-match "/[^/]+$" dir)
2241       (setq dir (substring dir 0 (match-beginning 0))))
2242     (while dirs
2243       (make-directory (car dirs))
2244       (setq dirs (cdr dirs)))))
2245
2246 (defun gnus-capitalize-newsgroup (newsgroup)
2247   "Capitalize NEWSGROUP name."
2248   (and (not (zerop (length newsgroup)))
2249        (concat (char-to-string (upcase (aref newsgroup 0)))
2250                (substring newsgroup 1))))
2251
2252 ;; Var
2253
2254 (defun gnus-simplify-subject (subject &optional re-only)
2255   "Remove `Re:' and words in parentheses.
2256 If optional argument RE-ONLY is non-nil, strip `Re:' only."
2257   (let ((case-fold-search t))           ;Ignore case.
2258     ;; Remove `Re:' and `Re^N:'.
2259     (if (string-match "^re:[ \t]*" subject)
2260         (setq subject (substring subject (match-end 0))))
2261     ;; Remove words in parentheses from end.
2262     (or re-only
2263         (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
2264           (setq subject (substring subject 0 (match-beginning 0)))))
2265     ;; Return subject string.
2266     subject))
2267
2268 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
2269 ;; all whitespace.
2270 (defun gnus-simplify-subject-fuzzy (subject)
2271   (let ((case-fold-search t))
2272     (save-excursion
2273       (gnus-set-work-buffer)
2274       (insert subject)
2275       (inline (gnus-simplify-buffer-fuzzy))
2276       (buffer-string))))
2277
2278 (defun gnus-simplify-buffer-fuzzy ()
2279   (goto-char (point-min))
2280   ;; Fix by Stainless Steel Rat <ratinox@ccs.neu.edu>.
2281   (while (re-search-forward "^[ \t]*\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;][ \t]*"
2282                             nil t)
2283     (replace-match "" t t))
2284   (goto-char (point-min))
2285   (while (re-search-forward "[ \t\n]*([^()]*)[ \t\n]*$" nil t)
2286     (replace-match "" t t))
2287   (goto-char (point-min))
2288   (while (re-search-forward "[ \t]+" nil t)
2289     (replace-match " " t t))
2290   (goto-char (point-min))
2291   (while (re-search-forward "[ \t]+$" nil t)
2292     (replace-match "" t t))
2293   (goto-char (point-min))
2294   (while (re-search-forward "^[ \t]+" nil t)
2295     (replace-match "" t t))
2296   (if gnus-simplify-subject-fuzzy-regexp
2297       (while (re-search-forward gnus-simplify-subject-fuzzy-regexp nil t)
2298         (replace-match "" t t)))
2299     )
2300
2301 ;; Add the current buffer to the list of buffers to be killed on exit. 
2302 (defun gnus-add-current-to-buffer-list ()
2303   (or (memq (current-buffer) gnus-buffer-list)
2304       (setq gnus-buffer-list (cons (current-buffer) gnus-buffer-list))))
2305
2306 (defun gnus-string> (s1 s2)
2307   (not (or (string< s1 s2)
2308            (string= s1 s2))))
2309
2310 ;; Functions accessing headers.
2311 ;; Functions are more convenient than macros in some cases.
2312
2313 (defun gnus-header-number (header)
2314   (header-number header))
2315
2316 (defun gnus-header-subject (header)
2317   (header-subject header))
2318
2319 (defun gnus-header-from (header)
2320   (header-from header))
2321
2322 (defun gnus-header-xref (header)
2323   (header-xref header))
2324
2325 (defun gnus-header-lines (header)
2326   (header-lines header))
2327
2328 (defun gnus-header-date (header)
2329   (header-date header))
2330
2331 (defun gnus-header-id (header)
2332   (header-id header))
2333
2334 (defun gnus-header-references (header)
2335   (header-references header))
2336
2337 ;;; General various misc type functions.
2338
2339 (defun gnus-clear-system ()
2340   "Clear all variables and buffers."
2341   ;; Clear Gnus variables.
2342   (let ((variables gnus-variable-list))
2343     (while variables
2344       (set (car variables) nil)
2345       (setq variables (cdr variables))))
2346   ;; Clear other internal variables.
2347   (setq gnus-list-of-killed-groups nil
2348         gnus-have-read-active-file nil
2349         gnus-newsrc-alist nil
2350         gnus-newsrc-hashtb nil
2351         gnus-killed-list nil
2352         gnus-zombie-list nil
2353         gnus-killed-hashtb nil
2354         gnus-active-hashtb nil
2355         gnus-moderated-list nil
2356         gnus-description-hashtb nil
2357         gnus-newsgroup-headers nil
2358         gnus-newsgroup-headers-hashtb-by-number nil
2359         gnus-newsgroup-name nil
2360         gnus-server-alist nil
2361         gnus-current-select-method nil)
2362   ;; Reset any score variables.
2363   (and (boundp 'gnus-score-cache)
2364        (set 'gnus-score-cache nil))
2365   (and (boundp 'gnus-internal-global-score-files)
2366        (set 'gnus-internal-global-score-files nil))
2367   ;; Kill the startup file.
2368   (and gnus-current-startup-file
2369        (get-file-buffer gnus-current-startup-file)
2370        (kill-buffer (get-file-buffer gnus-current-startup-file)))
2371   ;; Save any cache buffers.
2372   (and gnus-use-cache (gnus-cache-save-buffers))
2373   ;; Clear the dribble buffer.
2374   (gnus-dribble-clear)
2375   ;; Kill global KILL file buffer.
2376   (if (get-file-buffer (gnus-newsgroup-kill-file nil))
2377       (kill-buffer (get-file-buffer (gnus-newsgroup-kill-file nil))))
2378   (gnus-kill-buffer nntp-server-buffer)
2379   ;; Kill Gnus buffers.
2380   (while gnus-buffer-list
2381     (gnus-kill-buffer (car gnus-buffer-list))
2382     (setq gnus-buffer-list (cdr gnus-buffer-list))))
2383
2384 (defun gnus-windows-old-to-new (setting)
2385   (if (symbolp setting)
2386       (setq setting 
2387             (cond ((eq setting 'SelectArticle)
2388                    'article)
2389                   ((eq setting 'SelectSubject)
2390                    'summary)
2391                   ((eq setting 'SelectNewsgroup)
2392                    'group)
2393                   (t setting))))
2394   (if (or (listp setting)
2395           (not (and gnus-window-configuration
2396                     (memq setting '(group summary article)))))
2397       setting
2398     (let* ((setting (if (eq setting 'group) 
2399                         (if (assq 'newsgroup gnus-window-configuration)
2400                             'newsgroup
2401                           'newsgroups) setting))
2402            (elem (car (cdr (assq setting gnus-window-configuration))))
2403            (total (apply '+ elem))
2404            (types '(group summary article))
2405            (pbuf (if (eq setting 'newsgroups) 'group 'summary))
2406            (i 0)
2407            perc
2408            out)
2409       (while (< i 3)
2410         (or (zerop (nth i elem))
2411             (progn
2412               (setq perc  (/ (* 1.0 (nth 0 elem)) total))
2413               (setq out (cons (if (eq pbuf (nth i types))
2414                                   (vector (nth i types) perc 'point)
2415                                 (vector (nth i types) perc))
2416                               out))))
2417         (setq i (1+ i)))
2418       (list (nreverse out)))))
2419            
2420 (defun gnus-add-configuration (conf)
2421   (setq gnus-buffer-configuration 
2422         (cons conf (delq (assq (car conf) gnus-buffer-configuration)
2423                          gnus-buffer-configuration))))
2424
2425 (defun gnus-configure-windows (setting)
2426   (setq setting (gnus-windows-old-to-new setting))
2427   (let ((r (if (symbolp setting)
2428                   (cdr (assq setting gnus-buffer-configuration))
2429                 setting))
2430         (in-buf (current-buffer))
2431         rule val w height hor ohor heights sub jump-buffer
2432         rel total to-buf)
2433     (or r (error "No such setting: %s" setting))
2434
2435     ;; Either remove all windows or just remove all Gnus windows.
2436     (if gnus-use-full-window
2437         (delete-other-windows)
2438       (gnus-remove-some-windows)
2439       (switch-to-buffer nntp-server-buffer))
2440
2441     (while r
2442       (setq hor (car r)
2443             ohor nil)
2444
2445       ;; We have to do the (possible) horizontal splitting before the
2446       ;; vertical. 
2447       (if (and (listp (car hor)) 
2448                (eq (car (car hor)) 'horizontal))
2449           (progn
2450             (split-window 
2451              nil
2452              (if (integerp (nth 1 (car hor)))
2453                  (nth 1 (car hor))
2454                (- (frame-width) (floor (* (frame-width) (nth 1 (car hor))))))
2455              t)
2456             (setq hor (cdr hor))))
2457
2458       ;; Go through the rules and eval the elements that are to be
2459       ;; evaled.  
2460       (while hor
2461         (if (setq val (if (vectorp (car hor)) (car hor) (eval (car hor))))
2462             (progn
2463               ;; Expand short buffer name.
2464               (setq w (aref val 0))
2465               (and (setq w (cdr (assq w gnus-window-to-buffer)))
2466                    (progn
2467                      (setq val (apply 'vector (mapcar (lambda (v) v) val)))
2468                      (aset val 0 w)))
2469               (setq ohor (cons val ohor))))
2470         (setq hor (cdr hor)))
2471       (setq rule (cons (nreverse ohor) rule))
2472       (setq r (cdr r)))
2473     (setq rule (nreverse rule))
2474
2475     ;; We tally the window sizes.
2476     (setq total (window-height))
2477     (while rule
2478       (setq hor (car rule))
2479       (if (and (listp (car hor)) (eq (car (car hor)) 'horizontal))
2480           (setq hor (cdr hor)))
2481       (setq sub 0)
2482       (while hor
2483         (setq rel (aref (car hor) 1)
2484               heights (cons
2485                        (cond ((and (floatp rel) (= 1.0 rel))
2486                               'x)
2487                              ((integerp rel)
2488                               rel)
2489                              (t
2490                               (max (floor (* total rel)) 4)))
2491                        heights)
2492               sub (+ sub (if (numberp (car heights)) (car heights) 0))
2493               hor (cdr hor)))
2494       (setq heights (nreverse heights)
2495             hor (car rule))
2496
2497       ;; We then go through these heighs and create windows for them.
2498       (while heights
2499         (setq height (car heights)
2500               heights (cdr heights))
2501         (and (eq height 'x)
2502              (setq height (- total sub)))
2503         (and heights
2504              (split-window nil height))
2505         (setq to-buf (aref (car hor) 0))
2506         (switch-to-buffer 
2507          (cond ((not to-buf)
2508                 in-buf)
2509                ((symbolp to-buf)
2510                 (symbol-value (aref (car hor) 0)))
2511                (t
2512                 (aref (car hor) 0))))
2513         (and (> (length (car hor)) 2)
2514              (eq (aref (car hor) 2) 'point)
2515              (setq jump-buffer (current-buffer)))
2516         (other-window 1)
2517         (setq hor (cdr hor)))
2518       
2519       (setq rule (cdr rule)))
2520
2521     ;; Finally, we pop to the buffer that's supposed to have point. 
2522     (or jump-buffer (error "Missing `point' in spec for %s" setting))
2523
2524     (select-window (get-buffer-window jump-buffer))
2525     (set-buffer jump-buffer)))
2526       
2527 (defun gnus-remove-some-windows ()
2528   (let ((buffers gnus-window-to-buffer)
2529         (first t)
2530         buf)
2531     (save-excursion
2532       ;; Remove windows on all known Gnus buffers.
2533       (while buffers
2534         (setq buf (cdr (car buffers)))
2535         (if (symbolp buf)
2536             (setq buf (and (boundp buf) (symbol-value buf))))
2537         (and buf 
2538              (get-buffer-window buf)
2539              (progn
2540                (if first
2541                    (progn
2542                      (pop-to-buffer buf)
2543                      (switch-to-buffer nntp-server-buffer)
2544                      (setq first nil))
2545                  (delete-window (get-buffer-window buf)))))
2546         (setq buffers (cdr buffers)))
2547       ;; Remove windows on *all* summary buffers.
2548       (let ((buffers (buffer-list)))
2549         (while buffers
2550           (if (and (string-match 
2551                     "^\\*Summary" (or (buffer-name (car buffers)) ""))
2552                    (get-buffer-window (car buffers)))
2553               (delete-window (get-buffer-window (car buffers))))
2554           (setq buffers (cdr buffers)))))))
2555                           
2556 (defun gnus-version ()
2557   "Version numbers of this version of Gnus."
2558   (interactive)
2559   (let ((methods gnus-valid-select-methods)
2560         (mess gnus-version)
2561         meth)
2562     ;; Go through all the legal select methods and add their version
2563     ;; numbers to the total version string. Only the backends that are
2564     ;; currently in use will have their message numbers taken into
2565     ;; consideration. 
2566     (while methods
2567       (setq meth (intern (concat (car (car methods)) "-version")))
2568       (and (boundp meth)
2569            (stringp (symbol-value meth))
2570            (setq mess (concat mess "; " (symbol-value meth))))
2571       (setq methods (cdr methods)))
2572     (gnus-message 2 mess)))
2573
2574 (defun gnus-info-find-node ()
2575   "Find Info documentation of Gnus."
2576   (interactive)
2577   ;; Enlarge info window if needed.
2578   (let ((mode major-mode))
2579     (gnus-configure-windows 'info)
2580     (Info-goto-node (car (cdr (assq mode gnus-info-nodes))))))
2581
2582 (defun gnus-overload-functions (&optional overloads)
2583   "Overload functions specified by optional argument OVERLOADS.
2584 If nothing is specified, use the variable gnus-overload-functions."
2585   (let ((defs nil)
2586         (overloads (or overloads gnus-overload-functions)))
2587     (while overloads
2588       (setq defs (car overloads))
2589       (setq overloads (cdr overloads))
2590       ;; Load file before overloading function if necessary.  Make
2591       ;; sure we cannot use `require' always.
2592       (and (not (fboundp (car defs)))
2593            (car (cdr (cdr defs)))
2594            (load (car (cdr (cdr defs))) nil 'nomessage))
2595       (fset (car defs) (car (cdr defs))))))
2596
2597 (defun gnus-replace-chars-in-string (string from to)
2598   "Replace characters in STRING from FROM to TO."
2599   (let ((string (substring string 0))   ;Copy string.
2600         (len (length string))
2601         (idx 0))
2602     ;; Replace all occurrences of FROM with TO.
2603     (while (< idx len)
2604       (if (= (aref string idx) from)
2605           (aset string idx to))
2606       (setq idx (1+ idx)))
2607     string))
2608
2609 (defun gnus-days-between (date1 date2)
2610   ;; Return the number of days between date1 and date2.
2611   (- (gnus-day-number date1) (gnus-day-number date2)))
2612
2613 (defun gnus-day-number (date)
2614   (let ((dat (mapcar (lambda (s) (and s (string-to-int s)) )
2615                      (timezone-parse-date date))))
2616     (timezone-absolute-from-gregorian 
2617      (nth 1 dat) (nth 2 dat) (car dat))))
2618
2619 ;; Returns a floating point number that says how many seconds have
2620 ;; lapsed between Jan 1 12:00:00 1970 and DATE.
2621 (defun gnus-seconds-since-epoch (date)
2622   (let* ((tdate (mapcar (lambda (ti) (and ti (string-to-int ti)))
2623                         (timezone-parse-date date)))
2624          (ttime (mapcar (lambda (ti) (and ti (string-to-int ti)))
2625                         (timezone-parse-time
2626                          (aref (timezone-parse-date date) 3))))
2627          (edate (mapcar (lambda (ti) (and ti (string-to-int ti)))
2628                         (timezone-parse-date "Jan 1 12:00:00 1970")))
2629          (tday (- (timezone-absolute-from-gregorian 
2630                    (nth 1 tdate) (nth 2 tdate) (nth 0 tdate))
2631                   (timezone-absolute-from-gregorian 
2632                    (nth 1 edate) (nth 2 edate) (nth 0 edate)))))
2633     (+ (nth 2 ttime)
2634        (* (nth 1 ttime) 60)
2635        (* 1.0 (nth 0 ttime) 60 60)
2636        (* 1.0 tday 60 60 24))))
2637
2638 (defun gnus-file-newer-than (file date)
2639   (let ((fdate (nth 5 (file-attributes file))))
2640     (or (> (car fdate) (car date))
2641         (and (= (car fdate) (car date))
2642              (> (nth 1 fdate) (nth 1 date))))))
2643
2644 ;; Two silly functions to ensure that all `y-or-n-p' questions clear
2645 ;; the echo area.
2646 (defun gnus-y-or-n-p (prompt)
2647   (prog1
2648       (y-or-n-p prompt)
2649     (message "")))
2650
2651 (defun gnus-yes-or-no-p (prompt)
2652   (prog1
2653       (yes-or-no-p prompt)
2654     (message "")))
2655
2656 ;; Check whether to use long file names.
2657 (defun gnus-use-long-file-name (symbol)
2658   ;; The variable has to be set...
2659   (and gnus-use-long-file-name
2660        ;; If it isn't a list, then we return t.
2661        (or (not (listp gnus-use-long-file-name))
2662            ;; If it is a list, and the list contains `symbol', we
2663            ;; return nil.  
2664            (not (memq symbol gnus-use-long-file-name)))))
2665
2666 ;; I suspect there's a better way, but I haven't taken the time to do
2667 ;; it yet. -erik selberg@cs.washington.edu
2668 (defun gnus-dd-mmm (messy-date)
2669   "Return a string like DD-MMM from a big messy string"
2670   (let ((datevec (timezone-parse-date messy-date)))
2671     (format "%2s-%s"
2672             (or (aref datevec 2) "??")
2673             (capitalize
2674              (or (car 
2675                   (nth (1- (string-to-number (aref datevec 1)))
2676                        timezone-months-assoc))
2677                  "???")))))
2678
2679 ;; Make a hash table (default and minimum size is 255).
2680 ;; Optional argument HASHSIZE specifies the table size.
2681 (defun gnus-make-hashtable (&optional hashsize)
2682   (make-vector (if hashsize (max (gnus-create-hash-size hashsize) 255) 255) 0))
2683
2684 ;; Make a number that is suitable for hashing; bigger than MIN and one
2685 ;; less than 2^x.
2686 (defun gnus-create-hash-size (min)
2687   (let ((i 1))
2688     (while (< i min)
2689       (setq i (* 2 i)))
2690     (1- i)))
2691
2692 ;; Show message if message has a lower level than `gnus-verbose'. 
2693 ;; Guide-line for numbers:
2694 ;; 1 - error messages, 3 - non-serious error messages, 5 - messages
2695 ;; for things that take a long time, 7 - not very important messages
2696 ;; on stuff, 9 - messages inside loops.
2697 (defun gnus-message (level &rest args)
2698   (if (<= level gnus-verbose)
2699       (apply 'message args)
2700     ;; We have to do this format thingie here even if the result isn't
2701     ;; shown - the return value has to be the same as the return value
2702     ;; from `message'.
2703     (apply 'format args)))
2704
2705 ;; Generate a unique new group name.
2706 (defun gnus-generate-new-group-name (leaf)
2707   (let ((name leaf)
2708         (num 0))
2709     (while (gnus-gethash name gnus-newsrc-hashtb)
2710       (setq name (concat leaf "<" (int-to-string (setq num (1+ num))) ">")))
2711     name))
2712
2713 (defun gnus-find-file-noselect (file &optional force)
2714   "Does vaguely the same as find-file-noselect. No hooks are run."
2715   (let (buf insert)
2716     (if (setq buf (get-file-buffer file))
2717         (setq insert force)
2718       (setq buf (create-file-buffer file))
2719       (setq insert t))
2720     (if (not insert)
2721         buf
2722       (save-excursion
2723         (set-buffer buf)
2724         (erase-buffer)
2725         (and (file-readable-p file)
2726              (insert-file-contents file))
2727         (set-visited-file-name file)
2728         (set-buffer-modified-p nil)
2729         (current-buffer)))))
2730
2731 ;;; List and range functions
2732
2733 (defun gnus-last-element (list)
2734   "Return last element of LIST."
2735   (while (cdr list)
2736     (setq list (cdr list)))
2737   (car list))
2738
2739 (defun gnus-copy-sequence (list)
2740   "Do a complete, total copy of a list."
2741   (if (and (consp list) (not (consp (cdr list))))
2742       (cons (car list) (cdr list))
2743     (mapcar (lambda (elem) (if (consp elem) 
2744                                (if (consp (cdr elem))
2745                                    (gnus-copy-sequence elem)
2746                                  (cons (car elem) (cdr elem)))
2747                              elem))
2748             list)))
2749
2750 (defun gnus-set-difference (list1 list2)
2751   "Return a list of elements of LIST1 that do not appear in LIST2."
2752   (let ((list1 (copy-sequence list1)))
2753     (while list2
2754       (setq list1 (delq (car list2) list1))
2755       (setq list2 (cdr list2)))
2756     list1))
2757
2758 (defun gnus-sorted-complement (list1 list2)
2759   "Return a list of elements of LIST1 that do not appear in LIST2.
2760 Both lists have to be sorted over <."
2761   (let (out)
2762     (if (or (null list1) (null list2))
2763         (or list1 list2)
2764       (while (and list1 list2)
2765         (cond ((= (car list1) (car list2))
2766                (setq list1 (cdr list1)
2767                      list2 (cdr list2)))
2768               ((< (car list1) (car list2))
2769                (setq out (cons (car list1) out))
2770                (setq list1 (cdr list1)))
2771               (t
2772                (setq out (cons (car list2) out))
2773                (setq list2 (cdr list2)))))
2774       (nconc (nreverse out) (or list1 list2)))))
2775
2776 (defun gnus-intersection (list1 list2)      
2777   (let ((result nil))
2778     (while list2
2779       (if (memq (car list2) list1)
2780           (setq result (cons (car list2) result)))
2781       (setq list2 (cdr list2)))
2782     result))
2783
2784 (defun gnus-sorted-intersection (list1 list2)
2785   ;; LIST1 and LIST2 have to be sorted over <.
2786   (let (out)
2787     (while (and list1 list2)
2788       (cond ((= (car list1) (car list2))
2789              (setq out (cons (car list1) out)
2790                    list1 (cdr list1)
2791                    list2 (cdr list2)))
2792             ((< (car list1) (car list2))
2793              (setq list1 (cdr list1)))
2794             (t
2795              (setq list2 (cdr list2)))))
2796     (nreverse out)))
2797
2798 (defun gnus-set-sorted-intersection (list1 list2)
2799   ;; LIST1 and LIST2 have to be sorted over <.
2800   ;; This function modifies LIST1.
2801   (let* ((top (cons nil list1))
2802          (prev top))
2803   (while (and list1 list2)
2804     (cond ((= (car list1) (car list2))
2805            (setq prev list1
2806                  list1 (cdr list1)
2807                  list2 (cdr list2)))
2808           ((< (car list1) (car list2))
2809            (setcdr prev (cdr list1))
2810            (setq list1 (cdr list1)))
2811           (t
2812            (setq list2 (cdr list2)))))
2813   (setcdr prev nil)
2814   (cdr top)))
2815
2816 (defun gnus-compress-sequence (numbers &optional always-list)
2817   "Convert list of numbers to a list of ranges or a single range.
2818 If ALWAYS-LIST is non-nil, this function will always release a list of
2819 ranges."
2820   (let* ((first (car numbers))
2821          (last (car numbers))
2822          result)
2823     (if (null numbers)
2824         nil
2825       (if (not (listp (cdr numbers)))
2826           numbers
2827         (while numbers
2828           (cond ((= last (car numbers)) nil) ;Omit duplicated number
2829                 ((= (1+ last) (car numbers)) ;Still in sequence
2830                  (setq last (car numbers)))
2831                 (t                      ;End of one sequence
2832                  (setq result 
2833                        (cons (if (= first last) first
2834                                (cons first last)) result))
2835                  (setq first (car numbers))
2836                  (setq last  (car numbers))))
2837           (setq numbers (cdr numbers)))
2838         (if (and (not always-list) (null result))
2839             (if (= first last) (list first) (cons first last))
2840           (nreverse (cons (if (= first last) first (cons first last))
2841                           result)))))))
2842
2843 (defalias 'gnus-uncompress-sequence 'gnus-uncompress-range)
2844 (defun gnus-uncompress-range (ranges)
2845   "Expand a list of ranges into a list of numbers.
2846 RANGES is either a single range on the form `(num . num)' or a list of
2847 these ranges."
2848   (let (first last result)
2849     (cond 
2850      ((null ranges)
2851       nil)
2852      ((not (listp (cdr ranges)))
2853       (setq first (car ranges))
2854       (setq last (cdr ranges))
2855       (while (<= first last)
2856         (setq result (cons first result))
2857         (setq first (1+ first)))
2858       (nreverse result))
2859      (t
2860       (while ranges
2861         (if (atom (car ranges))
2862             (if (numberp (car ranges))
2863                 (setq result (cons (car ranges) result)))
2864           (setq first (car (car ranges)))
2865           (setq last  (cdr (car ranges)))
2866           (while (<= first last)
2867             (setq result (cons first result))
2868             (setq first (1+ first))))
2869         (setq ranges (cdr ranges)))
2870       (nreverse result)))))
2871
2872 (defun gnus-add-to-range (ranges list)
2873   "Return a list of ranges that has all articles from both RANGES and LIST.
2874 Note: LIST has to be sorted over `<'."
2875   (if (not ranges)
2876       (gnus-compress-sequence list t)
2877     (setq list (copy-sequence list))
2878     (or (listp (cdr ranges))
2879         (setq ranges (list ranges)))
2880     (let ((out ranges)
2881           ilist lowest highest temp)
2882       (while (and ranges list)
2883         (setq ilist list)
2884         (setq lowest (or (and (atom (car ranges)) (car ranges))
2885                          (car (car ranges))))
2886         (while (and list (cdr list) (< (car (cdr list)) lowest))
2887           (setq list (cdr list)))
2888         (if (< (car ilist) lowest)
2889             (progn
2890               (setq temp list)
2891               (setq list (cdr list))
2892               (setcdr temp nil)
2893               (setq out (nconc (gnus-compress-sequence ilist t) out))))
2894         (setq highest (or (and (atom (car ranges)) (car ranges))
2895                           (cdr (car ranges))))
2896         (while (and list (<= (car list) highest))
2897           (setq list (cdr list)))
2898         (setq ranges (cdr ranges)))
2899       (if list
2900           (setq out (nconc (gnus-compress-sequence list t) out)))
2901       (setq out (sort out (lambda (r1 r2) 
2902                             (< (or (and (atom r1) r1) (car r1))
2903                                (or (and (atom r2) r2) (car r2))))))
2904       (setq ranges out)
2905       (while ranges
2906         (if (atom (car ranges))
2907             (if (cdr ranges)
2908                 (if (atom (car (cdr ranges)))
2909                     (if (= (1+ (car ranges)) (car (cdr ranges)))
2910                         (progn
2911                           (setcar ranges (cons (car ranges) 
2912                                                (car (cdr ranges))))
2913                           (setcdr ranges (cdr (cdr ranges)))))
2914                   (if (= (1+ (car ranges)) (car (car (cdr ranges))))
2915                       (progn
2916                         (setcar (car (cdr ranges)) (car ranges))
2917                         (setcar ranges (car (cdr ranges)))
2918                         (setcdr ranges (cdr (cdr ranges)))))))
2919           (if (cdr ranges)
2920               (if (atom (car (cdr ranges)))
2921                   (if (= (1+ (cdr (car ranges))) (car (cdr ranges)))
2922                       (progn
2923                         (setcdr (car ranges) (car (cdr ranges)))
2924                         (setcdr ranges (cdr (cdr ranges)))))
2925                 (if (= (1+ (cdr (car ranges))) (car (car (cdr ranges))))
2926                     (progn
2927                       (setcdr (car ranges) (cdr (car (cdr ranges))))
2928                       (setcdr ranges (cdr (cdr ranges))))))))
2929         (setq ranges (cdr ranges)))
2930       out)))
2931
2932 (defun gnus-remove-from-range (ranges list)
2933   "Return a list of ranges that has all articles from LIST removed from RANGES.
2934 Note: LIST has to be sorted over `<'."
2935   ;; !!! This function shouldn't look like this, but I've got a headache.
2936   (gnus-compress-sequence 
2937    (gnus-sorted-complement
2938     (gnus-uncompress-range ranges) list)))
2939
2940 (defun gnus-member-of-range (number ranges)
2941   (if (not (listp (cdr ranges)))
2942       (and (>= number (car ranges)) 
2943            (<= number (cdr ranges)))
2944     (let ((not-stop t))
2945       (while (and ranges 
2946                   (if (numberp (car ranges))
2947                       (>= number (car ranges))
2948                     (>= number (car (car ranges))))
2949                   not-stop)
2950         (if (if (numberp (car ranges))
2951                 (= number (car ranges))
2952               (and (>= number (car (car ranges)))
2953                    (<= number (cdr (car ranges)))))
2954             (setq not-stop nil))
2955         (setq ranges (cdr ranges)))
2956       (not not-stop))))
2957
2958 \f
2959 ;;;
2960 ;;; Gnus group mode
2961 ;;;
2962
2963 (defvar gnus-group-mode-map nil)
2964 (defvar gnus-group-group-map nil)
2965 (defvar gnus-group-mark-map nil)
2966 (defvar gnus-group-list-map nil)
2967 (defvar gnus-group-sub-map nil)
2968 (put 'gnus-group-mode 'mode-class 'special)
2969
2970 (if gnus-group-mode-map
2971     nil
2972   (setq gnus-group-mode-map (make-keymap))
2973   (suppress-keymap gnus-group-mode-map)
2974   (define-key gnus-group-mode-map " " 'gnus-group-read-group)
2975   (define-key gnus-group-mode-map "=" 'gnus-group-select-group)
2976   (define-key gnus-group-mode-map "\r" 'gnus-group-select-group)
2977   (define-key gnus-group-mode-map "j" 'gnus-group-jump-to-group)
2978   (define-key gnus-group-mode-map "n" 'gnus-group-next-unread-group)
2979   (define-key gnus-group-mode-map "p" 'gnus-group-prev-unread-group)
2980   (define-key gnus-group-mode-map "\177" 'gnus-group-prev-unread-group)
2981   (define-key gnus-group-mode-map "N" 'gnus-group-next-group)
2982   (define-key gnus-group-mode-map "P" 'gnus-group-prev-group)
2983   (define-key gnus-group-mode-map "\M-n" 'gnus-group-next-unread-group-same-level)
2984   (define-key gnus-group-mode-map "\M-p" 'gnus-group-prev-unread-group-same-level)
2985   (define-key gnus-group-mode-map "," 'gnus-group-best-unread-group)
2986   (define-key gnus-group-mode-map "." 'gnus-group-first-unread-group)
2987   (define-key gnus-group-mode-map "u" 'gnus-group-unsubscribe-current-group)
2988   (define-key gnus-group-mode-map "U" 'gnus-group-unsubscribe-group)
2989   (define-key gnus-group-mode-map "c" 'gnus-group-catchup-current)
2990   (define-key gnus-group-mode-map "C" 'gnus-group-catchup-current-all)
2991   (define-key gnus-group-mode-map "l" 'gnus-group-list-groups)
2992   (define-key gnus-group-mode-map "L" 'gnus-group-list-all-groups)
2993   (define-key gnus-group-mode-map "m" 'gnus-group-mail)
2994   (define-key gnus-group-mode-map "g" 'gnus-group-get-new-news)
2995   (define-key gnus-group-mode-map "\M-g" 'gnus-group-get-new-news-this-group)
2996   (define-key gnus-group-mode-map "R" 'gnus-group-restart)
2997   (define-key gnus-group-mode-map "r" 'gnus-group-read-init-file)
2998   (define-key gnus-group-mode-map "B" 'gnus-group-browse-foreign-server)
2999   (define-key gnus-group-mode-map "b" 'gnus-group-check-bogus-groups)
3000   (define-key gnus-group-mode-map "F" 'gnus-find-new-newsgroups)
3001   (define-key gnus-group-mode-map "\C-c\C-d" 'gnus-group-describe-group)
3002   (define-key gnus-group-mode-map "\M-d" 'gnus-group-describe-all-groups)
3003   (define-key gnus-group-mode-map "\C-c\C-a" 'gnus-group-apropos)
3004   (define-key gnus-group-mode-map "\C-c\M-C-a" 'gnus-group-description-apropos)
3005   (define-key gnus-group-mode-map "a" 'gnus-group-post-news)
3006   (define-key gnus-group-mode-map "\ek" 'gnus-group-edit-local-kill)
3007   (define-key gnus-group-mode-map "\eK" 'gnus-group-edit-global-kill)
3008   (define-key gnus-group-mode-map "\C-k" 'gnus-group-kill-group)
3009   (define-key gnus-group-mode-map "\C-y" 'gnus-group-yank-group)
3010   (define-key gnus-group-mode-map "\C-w" 'gnus-group-kill-region)
3011   (define-key gnus-group-mode-map "\C-x\C-t" 'gnus-group-transpose-groups)
3012   (define-key gnus-group-mode-map "\C-c\C-l" 'gnus-group-list-killed)
3013   (define-key gnus-group-mode-map "\C-c\C-x" 'gnus-group-expire-articles)
3014   (define-key gnus-group-mode-map "\C-c\M-\C-x" 'gnus-group-expire-all-groups)
3015   (define-key gnus-group-mode-map "V" 'gnus-version)
3016   (define-key gnus-group-mode-map "s" 'gnus-group-save-newsrc)
3017   (define-key gnus-group-mode-map "z" 'gnus-group-suspend)
3018   (define-key gnus-group-mode-map "Z" 'gnus-group-clear-dribble)
3019   (define-key gnus-group-mode-map "q" 'gnus-group-exit)
3020   (define-key gnus-group-mode-map "Q" 'gnus-group-quit)
3021   (define-key gnus-group-mode-map "\M-f" 'gnus-group-fetch-faq)
3022   (define-key gnus-group-mode-map "?" 'gnus-group-describe-briefly)
3023   (define-key gnus-group-mode-map "\C-c\C-i" 'gnus-info-find-node)
3024   (define-key gnus-group-mode-map "\M-e" 'gnus-group-edit-group-method)
3025   (define-key gnus-group-mode-map "^" 'gnus-group-enter-server-mode)
3026   (define-key gnus-group-mode-map gnus-mouse-2 'gnus-mouse-pick-group)
3027   (define-key gnus-group-mode-map "<" 'beginning-of-buffer)
3028   (define-key gnus-group-mode-map ">" 'end-of-buffer)
3029   (define-key gnus-group-mode-map "\C-c\C-b" 'gnus-bug)
3030   (define-key gnus-group-mode-map "\C-c\C-s" 'gnus-group-sort-groups)
3031
3032   (define-key gnus-group-mode-map "#" 'gnus-group-mark-group)
3033   (define-key gnus-group-mode-map "\M-#" 'gnus-group-unmark-group)
3034   (define-prefix-command 'gnus-group-mark-map)
3035   (define-key gnus-group-mode-map "M" 'gnus-group-mark-map)
3036   (define-key gnus-group-mark-map "m" 'gnus-group-mark-group)
3037   (define-key gnus-group-mark-map "u" 'gnus-group-unmark-group)
3038   (define-key gnus-group-mark-map "w" 'gnus-group-mark-region)
3039
3040   (define-prefix-command 'gnus-group-group-map)
3041   (define-key gnus-group-mode-map "G" 'gnus-group-group-map)
3042   (define-key gnus-group-group-map "d" 'gnus-group-make-directory-group)
3043   (define-key gnus-group-group-map "h" 'gnus-group-make-help-group)
3044   (define-key gnus-group-group-map "a" 'gnus-group-make-archive-group)
3045   (define-key gnus-group-group-map "k" 'gnus-group-make-kiboze-group)
3046   (define-key gnus-group-group-map "m" 'gnus-group-make-group)
3047   (define-key gnus-group-group-map "E" 'gnus-group-edit-group)
3048   (define-key gnus-group-group-map "e" 'gnus-group-edit-group-method)
3049   (define-key gnus-group-group-map "p" 'gnus-group-edit-group-parameters)
3050   (define-key gnus-group-group-map "v" 'gnus-group-add-to-virtual)
3051   (define-key gnus-group-group-map "V" 'gnus-group-make-empty-virtual)
3052   (define-key gnus-group-group-map "D" 'gnus-group-enter-directory)
3053   (define-key gnus-group-group-map "f" 'gnus-group-make-doc-group)
3054   (define-key gnus-group-group-map "sb" 'gnus-group-brew-soup)
3055   (define-key gnus-group-group-map "sw" 'gnus-soup-save-areas)
3056   (define-key gnus-group-group-map "ss" 'gnus-soup-send-replies)
3057   (define-key gnus-group-group-map "sp" 'gnus-soup-pack-packet)
3058   (define-key gnus-group-group-map "sr" 'nnsoup-pack-replies)
3059
3060   (define-prefix-command 'gnus-group-list-map)
3061   (define-key gnus-group-mode-map "A" 'gnus-group-list-map)
3062   (define-key gnus-group-list-map "k" 'gnus-group-list-killed)
3063   (define-key gnus-group-list-map "z" 'gnus-group-list-zombies)
3064   (define-key gnus-group-list-map "s" 'gnus-group-list-groups)
3065   (define-key gnus-group-list-map "u" 'gnus-group-list-all-groups)
3066   (define-key gnus-group-list-map "a" 'gnus-group-apropos)
3067   (define-key gnus-group-list-map "d" 'gnus-group-description-apropos)
3068   (define-key gnus-group-list-map "m" 'gnus-group-list-matching)
3069   (define-key gnus-group-list-map "M" 'gnus-group-list-all-matching)
3070
3071   (define-prefix-command 'gnus-group-sub-map)
3072   (define-key gnus-group-mode-map "S" 'gnus-group-sub-map)
3073   (define-key gnus-group-sub-map "l" 'gnus-group-set-current-level)
3074   (define-key gnus-group-sub-map "t" 'gnus-group-unsubscribe-current-group)
3075   (define-key gnus-group-sub-map "s" 'gnus-group-unsubscribe-group)
3076   (define-key gnus-group-sub-map "k" 'gnus-group-kill-group)
3077   (define-key gnus-group-sub-map "y" 'gnus-group-yank-group)
3078   (define-key gnus-group-sub-map "w" 'gnus-group-kill-region)
3079   (define-key gnus-group-sub-map "z" 'gnus-group-kill-all-zombies))
3080
3081 (defun gnus-group-mode ()
3082   "Major mode for reading news.
3083
3084 All normal editing commands are switched off.
3085 \\<gnus-group-mode-map>
3086 The group buffer lists (some of) the groups available.  For instance,
3087 `\\[gnus-group-list-groups]' will list all subscribed groups with unread articles, while `\\[gnus-group-list-zombies]'
3088 lists all zombie groups. 
3089
3090 Groups that are displayed can be entered with `\\[gnus-group-read-group]'.  To subscribe 
3091 to a group not displayed, type `\\[gnus-group-unsubscribe-group]'. 
3092
3093 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]'). 
3094
3095 The following commands are available:
3096
3097 \\{gnus-group-mode-map}"
3098   (interactive)
3099   (if gnus-visual (gnus-group-make-menu-bar))
3100   (kill-all-local-variables)
3101   (setq mode-line-modified "-- ")
3102   (make-local-variable 'mode-line-format)
3103   (setq mode-line-format (copy-sequence mode-line-format))
3104   (and (equal (nth 3 mode-line-format) "   ")
3105        (setcar (nthcdr 3 mode-line-format) ""))
3106   (setq major-mode 'gnus-group-mode)
3107   (setq mode-name "Group")
3108   (gnus-group-set-mode-line)
3109   (setq mode-line-process nil)
3110   (use-local-map gnus-group-mode-map)
3111   (buffer-disable-undo (current-buffer))
3112   (setq truncate-lines t)
3113   (setq buffer-read-only t)
3114   (run-hooks 'gnus-group-mode-hook))
3115
3116 (defun gnus-mouse-pick-group (e)
3117   (interactive "e")
3118   (mouse-set-point e)
3119   (gnus-group-read-group nil))
3120
3121 ;;;###autoload
3122 (defun gnus-no-server (&optional arg)
3123   "Read network news.
3124 If ARG is a positive number, Gnus will use that as the
3125 startup level. If ARG is nil, Gnus will be started at level 2. 
3126 If ARG is non-nil and not a positive number, Gnus will
3127 prompt the user for the name of an NNTP server to use.
3128 As opposed to `gnus', this command will not connect to the local server."
3129   (interactive "P")
3130   (setq gnus-group-use-permanent-levels t)
3131   (gnus (or arg (1- gnus-level-default-subscribed)) t))
3132
3133 (defalias '\(ding\) 'gnus)
3134
3135 ;;;###autoload
3136 (defun gnus (&optional arg dont-connect)
3137   "Read network news.
3138 If ARG is non-nil and a positive number, Gnus will use that as the
3139 startup level. If ARG is non-nil and not a positive number, Gnus will
3140 prompt the user for the name of an NNTP server to use."
3141   (interactive "P")
3142   (if (get-buffer gnus-group-buffer)
3143       (progn
3144         (switch-to-buffer gnus-group-buffer)
3145         (gnus-group-get-new-news))
3146     (gnus-clear-system)
3147     (nnheader-init-server-buffer)
3148     (gnus-read-init-file)
3149     (let ((level (and arg (numberp arg) (> arg 0) arg))
3150           did-connect)
3151       (unwind-protect
3152           (progn
3153             (gnus-group-setup-buffer)
3154             (or dont-connect 
3155                 (setq did-connect
3156                       (gnus-start-news-server (and arg (not level))))))
3157         (if (and (not dont-connect) 
3158                  (not did-connect))
3159             (gnus-group-quit)
3160           (run-hooks 'gnus-startup-hook)
3161           ;; NNTP server is successfully open. 
3162           (gnus-update-format-specifications)
3163           (gnus-summary-make-display-table)
3164           (let ((buffer-read-only nil))
3165             (erase-buffer)
3166             (if (not gnus-inhibit-startup-message)
3167                 (progn
3168                   (gnus-group-startup-message)
3169                   (sit-for 0))))
3170           (gnus-setup-news nil level)
3171           (and gnus-use-dribble-file (gnus-dribble-open))
3172           (gnus-group-list-groups level)
3173           (gnus-configure-windows 'group))))))
3174
3175 (defun gnus-group-startup-message (&optional x y)
3176   "Insert startup message in current buffer."
3177   ;; Insert the message.
3178   (erase-buffer)
3179   (insert
3180    (format "
3181      %s
3182            A newsreader 
3183       for GNU Emacs
3184
3185         Based on GNUS 
3186              written by 
3187      Masanobu UMEDA
3188
3189        A Praxis Release
3190       larsi@ifi.uio.no
3191
3192            gnus-version))
3193   ;; And then hack it.
3194   ;; 18 is the longest line.
3195   (indent-rigidly (point-min) (point-max) 
3196                   (/ (max (- (window-width) (or x 28)) 0) 2))
3197   (goto-char (point-min))
3198   ;; +4 is fuzzy factor.
3199   (insert-char ?\n (/ (max (- (window-height) (or y 12)) 0) 2))
3200
3201   ;; Fontify some.
3202   (goto-char (point-min))
3203   (search-forward "Praxis")
3204   (put-text-property (match-beginning 0) (match-end 0) 'face 'bold)
3205   (goto-char (point-min)))
3206
3207 (defun gnus-group-setup-buffer ()
3208   (or (get-buffer gnus-group-buffer)
3209       (progn
3210         (switch-to-buffer gnus-group-buffer)
3211         (gnus-add-current-to-buffer-list)
3212         (gnus-group-mode)
3213         (and gnus-carpal (gnus-carpal-setup-buffer 'group)))))
3214
3215 (defun gnus-group-list-groups (level &optional unread)
3216   "List newsgroups with level LEVEL or lower that have unread articles.
3217 Default is all subscribed groups.
3218 If argument UNREAD is non-nil, groups with no unread articles are also listed."
3219   (interactive (list (and current-prefix-arg
3220                           (prefix-numeric-value current-prefix-arg))))
3221   (if gnus-group-use-permanent-levels
3222       (progn
3223         (setq gnus-group-default-list-level 
3224               (or level gnus-group-default-list-level))
3225         (setq level (or gnus-group-default-list-level gnus-level-subscribed)))
3226     (setq level (or level gnus-group-default-list-level 
3227                     gnus-level-subscribed)))
3228   (gnus-group-setup-buffer)     ;May call from out of group buffer
3229   (let ((case-fold-search nil)
3230         (group (gnus-group-group-name)))
3231     (funcall gnus-group-prepare-function level unread nil)
3232     (if (zerop (buffer-size))
3233         (gnus-message 5 gnus-no-groups-message)
3234       (goto-char (point-min))
3235       (if (not group)
3236           ;; Go to the first group with unread articles.
3237           (gnus-group-search-forward nil nil nil t)
3238         ;; Find the right group to put point on. If the current group
3239         ;; has disapeared in the new listing, try to find the next
3240         ;; one. If no next one can be found, just leave point at the
3241         ;; first newsgroup in the buffer.
3242         (if (not (gnus-goto-char
3243                   (text-property-any (point-min) (point-max) 
3244                                      'gnus-group (intern group))))
3245             (let ((newsrc (nthcdr 3 (gnus-gethash group gnus-newsrc-hashtb))))
3246               (while (and newsrc
3247                           (not (gnus-goto-char 
3248                                 (text-property-any 
3249                                  (point-min) (point-max) 'gnus-group 
3250                                  (intern (car (car newsrc)))))))
3251                 (setq newsrc (cdr newsrc)))
3252               (or newsrc (progn (goto-char (point-max))
3253                                 (forward-line -1))))))
3254       ;; Adjust cursor point.
3255       (gnus-group-position-cursor))))
3256
3257 (defun gnus-group-prepare-flat (level &optional all lowest regexp) 
3258   "List all newsgroups with unread articles of level LEVEL or lower.
3259 If ALL is non-nil, list groups that have no unread articles.
3260 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher.
3261 If REGEXP, only list groups matching REGEXP."
3262   (set-buffer gnus-group-buffer)
3263   (let ((buffer-read-only nil)
3264         (newsrc (cdr gnus-newsrc-alist))
3265         (lowest (or lowest 1))
3266         info clevel unread group)
3267     (erase-buffer)
3268     (if (< lowest gnus-level-zombie)
3269         ;; List living groups.
3270         (while newsrc
3271           (setq info (car newsrc)
3272                 group (car info)
3273                 newsrc (cdr newsrc)
3274                 unread (car (gnus-gethash group gnus-newsrc-hashtb)))
3275           (and unread ; This group might be bogus
3276                (or (not regexp)
3277                    (string-match regexp group))
3278                (<= (setq clevel (car (cdr info))) level) 
3279                (>= clevel lowest)
3280                (or all            ; We list all groups?
3281                    (eq unread t)  ; We list unactivated groups
3282                    (> unread 0)   ; We list groups with unread articles
3283                    (cdr (assq 'tick (nth 3 info)))) ; And groups with tickeds
3284                (gnus-group-insert-group-line 
3285                 nil group (car (cdr info)) (nth 3 info) unread (nth 4 info)))))
3286
3287     ;; List dead groups.
3288     (and (>= level gnus-level-zombie) (<= lowest gnus-level-zombie)
3289          (gnus-group-prepare-flat-list-dead 
3290           (setq gnus-zombie-list (sort gnus-zombie-list 'string<)) 
3291           gnus-level-zombie ?Z
3292           regexp))
3293     (and (>= level gnus-level-killed) (<= lowest gnus-level-killed)
3294          (gnus-group-prepare-flat-list-dead 
3295           (setq gnus-killed-list (sort gnus-killed-list 'string<)) 
3296           gnus-level-killed ?K regexp))
3297
3298     (gnus-group-set-mode-line)
3299     (setq gnus-have-all-newsgroups all)
3300     (run-hooks 'gnus-group-prepare-hook)))
3301
3302 (defun gnus-group-prepare-flat-list-dead (groups level mark regexp)
3303   ;; List zombies and killed lists somehwat faster, which was
3304   ;; suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>. It does
3305   ;; this by ignoring the group format specification altogether.
3306   (let (group beg)
3307     (while groups
3308       (setq group (car groups)
3309             groups (cdr groups))
3310       (if (or (not regexp)
3311               (string-match regexp group))
3312           (progn
3313             (setq beg (point))
3314             (insert (format " %c     *: %s\n" mark group))
3315             (add-text-properties 
3316              beg (1+ beg) 
3317              (list 'gnus-group (intern group)
3318                    'gnus-unread t
3319                    'gnus-level level)))))))
3320
3321 (defun gnus-group-real-name (group)
3322   "Find the real name of a foreign newsgroup."
3323   (if (string-match ":[^:]+$" group)
3324       (substring group (1+ (match-beginning 0)))
3325     group))
3326
3327 (defun gnus-group-prefixed-name (group method)
3328   "Return the whole name from GROUP and METHOD."
3329   (and (stringp method) (setq method (gnus-server-to-method method)))
3330   (concat (format "%s" (car method))
3331           (if (and 
3332                (assoc (format "%s" (car method)) (gnus-methods-using 'address))
3333                (not (string= (nth 1 method) "")))
3334               (concat "+" (nth 1 method)))
3335           ":" group))
3336
3337 (defun gnus-group-real-prefix (group)
3338   "Return the prefix of the current group name."
3339   (if (string-match "^[^:]+:" group)
3340       (substring group 0 (match-end 0))
3341     ""))
3342
3343 (defun gnus-group-method-name (group)
3344   "Return the method used for selecting GROUP."
3345   (let ((prefix (gnus-group-real-prefix group)))
3346     (if (equal prefix "")
3347         gnus-select-method
3348       (if (string-match "^[^\\+]+\\+" prefix)
3349           (list (intern (substring prefix 0 (1- (match-end 0))))
3350                 (substring prefix (match-end 0) (1- (length prefix))))
3351         (list (intern (substring prefix 0 (1- (length prefix)))) "")))))
3352
3353 (defun gnus-group-foreign-p (group)
3354   "Return nil if GROUP is native, non-nil if it is foreign."
3355   (string-match ":" group))
3356
3357 (defun gnus-group-set-info (info &optional method-only-group part)
3358   (let* ((entry (gnus-gethash
3359                  (or method-only-group (car info)) gnus-newsrc-hashtb))
3360          (part-info info)
3361          (info (if method-only-group (nth 2 entry) info)))
3362     (if (not method-only-group)
3363         ()
3364       (or entry
3365           (error "Trying to change non-existent group %s" method-only-group))
3366       ;; We have recevied parts of the actual group info - either the
3367       ;; select method or the group parameters.  We first check
3368       ;; whether we have to extend the info, and if so, do that.
3369       (let ((len (length info))
3370             (total (if (eq part 'method) 5 6)))
3371         (and (< len total)
3372              (setcdr (nthcdr (1- len) info)
3373                      (make-list (- total len) nil)))
3374         ;; Then we enter the new info.
3375         (setcar (nthcdr (1- total) info) part-info)))
3376     ;; We uncompress some lists of marked articles.
3377     (let (marked)
3378       (if (not (setq marked (nth 3 info)))
3379           ()
3380         (while marked
3381           (or (eq 'score (car (car marked)))
3382               (eq 'bookmark (car (car marked)))
3383               (eq 'killed (car (car marked)))
3384               (setcdr (car marked) 
3385                       (gnus-uncompress-range (cdr (car marked)))))
3386           (setq marked (cdr marked)))))
3387     (if entry
3388         ()
3389       ;; This is a new group, so we just create it.
3390       (save-excursion
3391         (set-buffer gnus-group-buffer)
3392         (if (nth 4 info)
3393             ;; It's a foreign group...
3394             (gnus-group-make-group 
3395              (gnus-group-real-name (car info))
3396              (prin1-to-string (car (nth 4 info)))
3397              (nth 1 (nth 4 info)))
3398           ;; It's a native group.
3399           (gnus-group-make-group
3400            (car info)
3401            (prin1-to-string (car gnus-select-method))
3402            (nth 1 gnus-select-method)))
3403         (gnus-message 6 "Note: New group created")
3404         (setq entry 
3405               (gnus-gethash (gnus-group-prefixed-name 
3406                              (gnus-group-real-name (car info))
3407                              (or (nth 4 info) gnus-select-method))
3408                             gnus-newsrc-hashtb))))
3409     ;; Whether it was a new group or not, we now have the entry, so we
3410     ;; can do the update.
3411     (if entry
3412         (progn
3413           (setcar (nthcdr 2 entry) info)
3414           (if (and (not (eq (car entry) t)) 
3415                    (gnus-gethash (car info) gnus-active-hashtb))
3416               (let ((marked (nth 3 info)))
3417                 (setcar entry 
3418                         (max 0 (- (length (gnus-list-of-unread-articles 
3419                                            (car info)))
3420                                   (length (cdr (assq 'tick marked)))
3421                                   (length (cdr (assq 'dormant marked)))))))))
3422       (error "No such group: %s" (car info)))))
3423
3424 (defun gnus-group-set-method-info (group select-method)
3425   (gnus-group-set-info select-method group 'method))
3426
3427 (defun gnus-group-set-params-info (group params)
3428   (gnus-group-set-info params group 'params))
3429
3430 (defun gnus-group-update-group-line ()
3431   "This function updates the current line in the newsgroup buffer and
3432 moves the point to the colon."
3433   (let* ((buffer-read-only nil)
3434          (group (gnus-group-group-name))
3435          (entry (and group (gnus-gethash group gnus-newsrc-hashtb))))
3436     (if entry
3437         (gnus-dribble-enter 
3438          (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
3439                  ")")))
3440     (beginning-of-line)
3441     (delete-region (point) (progn (forward-line 1) (point)))
3442     (gnus-group-insert-group-line-info group)
3443     (forward-line -1)
3444     (gnus-group-position-cursor)))
3445
3446 (defun gnus-group-insert-group-line-info (group)
3447   (let ((entry (gnus-gethash group gnus-newsrc-hashtb)) 
3448         active info)
3449     (if entry
3450         (progn
3451           (setq info (nth 2 entry))
3452           (gnus-group-insert-group-line 
3453            nil group (nth 1 info) (nth 3 info) (car entry) (nth 4 info)))
3454       (setq active (gnus-gethash group gnus-active-hashtb))
3455       (gnus-group-insert-group-line 
3456        nil group 
3457        (if (member group gnus-zombie-list) gnus-level-zombie gnus-level-killed)
3458        nil (if active (- (1+ (cdr active)) (car active)) 0) nil))))
3459
3460 (defun gnus-group-insert-group-line (gformat group level marked number method)
3461   (let* ((gformat (or gformat gnus-group-line-format-spec))
3462          (active (gnus-gethash group gnus-active-hashtb))
3463          (number-total (if active (1+ (- (cdr active) (car active))) 0))
3464          (number-of-dormant (length (cdr (assq 'dormant marked))))
3465          (number-of-ticked (length (cdr (assq 'tick marked))))
3466          (number-of-ticked-and-dormant
3467           (+ number-of-ticked number-of-dormant))
3468          (number-of-unread-unticked 
3469           (if (numberp number) (int-to-string (max 0 number))
3470             "*"))
3471          (number-of-read
3472           (if (numberp number)
3473               (max 0 (- number-total number))
3474             "*"))
3475          (subscribed (cond ((<= level gnus-level-subscribed) ? )
3476                            ((<= level gnus-level-unsubscribed) ?U)
3477                            ((= level gnus-level-zombie) ?Z)
3478                            (t ?K)))
3479          (qualified-group (gnus-group-real-name group))
3480          (newsgroup-description 
3481           (if gnus-description-hashtb
3482               (or (gnus-gethash group gnus-description-hashtb) "")
3483             ""))
3484          (moderated (if (member group gnus-moderated-list) ?m ? ))
3485          (moderated-string (if (eq moderated ?m) "(m)" ""))
3486          (method (gnus-server-get-method group method))
3487          (news-server (or (car (cdr method)) ""))
3488          (news-method (or (car method) ""))
3489          (news-method-string 
3490           (if method (format "(%s:%s)" (car method) (car (cdr method))) ""))
3491          (marked (if (and 
3492                       (numberp number) 
3493                       (zerop number)
3494                       (> number-of-ticked 0))
3495                      ?* ? ))
3496          (number (if (eq number t) "*" (+ number number-of-dormant 
3497                                           number-of-ticked)))
3498          (process-marked (if (member qualified-group gnus-group-marked)
3499                              gnus-process-mark ? ))
3500          (buffer-read-only nil)
3501          b)
3502     (beginning-of-line)
3503     (setq b (point))
3504     ;; Insert the text.
3505     (insert (eval gformat))
3506
3507     (add-text-properties 
3508      b (1+ b) (list 'gnus-group (intern group)
3509                     'gnus-unread (if (numberp number)
3510                                      (string-to-int number-of-unread-unticked)
3511                                    t)
3512                     'gnus-marked marked
3513                     'gnus-level level))))
3514
3515 (defun gnus-group-update-group (group &optional visible-only)
3516   "Update newsgroup info of GROUP.
3517 If VISIBLE-ONLY is non-nil, the group won't be displayed if it isn't already."
3518   (save-excursion
3519     (set-buffer gnus-group-buffer)
3520     (let ((buffer-read-only nil)
3521           visible)
3522       (let ((entry (gnus-gethash group gnus-newsrc-hashtb)))
3523         (if entry
3524             (gnus-dribble-enter 
3525              (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
3526                      ")"))))
3527       ;; Buffer may be narrowed.
3528       (save-restriction
3529         (widen)
3530         ;; Search a line to modify.  If the buffer is large, the search
3531         ;; takes long time.  In most cases, current point is on the line
3532         ;; we are looking for.  So, first of all, check current line. 
3533         (if (or (progn
3534                   (beginning-of-line)
3535                   (eq (get-text-property (point) 'gnus-group)
3536                       (intern group)))
3537                 (progn
3538                   (gnus-goto-char 
3539                    (text-property-any 
3540                     (point-min) (point-max) 'gnus-group (intern group)))))
3541             ;; GROUP is listed in current buffer. So, delete old line.
3542             (progn
3543               (setq visible t)
3544               (beginning-of-line)
3545               (delete-region (point) (progn (forward-line 1) (point))))
3546           ;; No such line in the buffer, find out where it's supposed to
3547           ;; go, and insert it there (or at the end of the buffer).
3548           ;; Fix by Per Abrahamsen <amanda@iesd.auc.dk>.
3549           (or visible-only
3550               (let ((entry 
3551                      (cdr (cdr (gnus-gethash group gnus-newsrc-hashtb)))))
3552                 (while (and entry
3553                             (car entry)
3554                             (not
3555                              (gnus-goto-char
3556                               (text-property-any
3557                                (point-min) (point-max) 
3558                                'gnus-group (intern (car (car entry)))))))
3559                   (setq entry (cdr entry)))
3560                 (or entry (goto-char (point-max)))))))
3561       (if (or visible (not visible-only))
3562           (gnus-group-insert-group-line-info group))
3563       (gnus-group-set-mode-line))))
3564
3565 (defun gnus-group-set-mode-line ()
3566   (if (memq 'group gnus-updated-mode-lines)
3567       (let* ((gformat (or gnus-group-mode-line-format-spec
3568                           (setq gnus-group-mode-line-format-spec
3569                                 (gnus-parse-format 
3570                                  gnus-group-mode-line-format 
3571                                  gnus-group-mode-line-format-alist))))
3572              (news-server (car (cdr gnus-select-method)))
3573              (news-method (car gnus-select-method))
3574              (max-len 60)
3575              (mode-string (eval gformat)))
3576         (setq mode-string (eval gformat))
3577         (if (> (length mode-string) max-len) 
3578             (setq mode-string (substring mode-string 0 (- max-len 4))))
3579         (setq mode-line-buffer-identification mode-string)
3580         (set-buffer-modified-p t))))
3581
3582 (defun gnus-group-group-name ()
3583   "Get the name of the newsgroup on the current line."
3584   (let ((group (get-text-property (gnus-point-at-bol) 'gnus-group)))
3585     (and group (symbol-name group))))
3586
3587 (defun gnus-group-group-level ()
3588   "Get the level of the newsgroup on the current line."
3589   (get-text-property (gnus-point-at-bol) 'gnus-level))
3590
3591 (defun gnus-group-group-unread ()
3592   "Get the number of unread articles of the newsgroup on the current line."
3593   (get-text-property (gnus-point-at-bol) 'gnus-unread))
3594
3595 (defun gnus-group-search-forward (&optional backward all level first-too)
3596   "Find the next newsgroup with unread articles.
3597 If BACKWARD is non-nil, find the previous newsgroup instead.
3598 If ALL is non-nil, just find any newsgroup.
3599 If LEVEL is non-nil, find group with level LEVEL, or higher if no such
3600 group exists.
3601 If FIRST-TOO, the current line is also eligible as a target."
3602   (let ((way (if backward -1 1))
3603         (low 10)
3604         (beg (point))
3605         pos found)
3606     (if (and backward (progn (beginning-of-line)) (bobp))
3607         nil
3608       (or first-too (forward-line way))
3609       (while (and 
3610               (not (eobp))
3611               (not (setq 
3612                     found 
3613                     (and (or all
3614                              (and
3615                               (let ((unread 
3616                                      (get-text-property (point) 'gnus-unread)))
3617                                 (or (eq unread t) (and unread (> unread 0))))
3618                               (let ((lev (get-text-property
3619                                           (point) 'gnus-level)))
3620                                 (and lev (<= (get-text-property 
3621                                               (point) 'gnus-level)
3622                                              gnus-level-subscribed)))))
3623                          (or (not level)
3624                              (let ((lev (get-text-property (point) 'gnus-level)))
3625                                (if (and lev (<= lev level))
3626                                    t
3627                                  (if (< lev low)
3628                                      (progn
3629                                        (setq low lev)
3630                                        (setq pos (point))))
3631                                  nil))))))
3632               (zerop (forward-line way)))))
3633     (if found 
3634         (progn (gnus-group-position-cursor) t)
3635       (if pos (goto-char pos) (goto-char beg))
3636       nil)))
3637
3638 ;;; Gnus group mode commands
3639
3640 ;; Group marking.
3641
3642 (defun gnus-group-mark-group (n &optional unmark no-advance)
3643   "Mark the current group."
3644   (interactive "p")
3645   (let ((buffer-read-only nil)
3646         group)
3647     (while 
3648         (and (> n 0) 
3649              (setq group (gnus-group-group-name))
3650              (progn
3651                (beginning-of-line)
3652                (forward-char 2)
3653                (delete-char 1)
3654                (if unmark
3655                    (progn
3656                      (insert " ")
3657                      (setq gnus-group-marked (delete group gnus-group-marked)))
3658                  (insert "#")
3659                  (setq gnus-group-marked
3660                        (cons group (delete group gnus-group-marked))))
3661                t)
3662              (or no-advance (zerop (gnus-group-next-group 1))))
3663       (setq n (1- n)))
3664     (gnus-summary-position-cursor)
3665     n))
3666
3667 (defun gnus-group-unmark-group (n)
3668   "Remove the mark from the current group."
3669   (interactive "p")
3670   (gnus-group-mark-group n 'unmark))
3671
3672 (defun gnus-group-mark-region (unmark beg end)
3673   "Mark all groups between point and mark.
3674 If UNMARK, remove the mark instead."
3675   (interactive "P\nr")
3676   (let ((num (count-lines beg end)))
3677     (save-excursion
3678       (goto-char beg)
3679       (- num (gnus-group-mark-group num unmark)))))
3680
3681 (defun gnus-group-remove-mark (group)
3682   (and (gnus-group-goto-group group)
3683        (save-excursion
3684          (gnus-group-mark-group 1 'unmark t))))
3685
3686 ;; Return a list of groups to work on.  Take into consideration N (the
3687 ;; prefix) and the list of marked groups.
3688 (defun gnus-group-process-prefix (n)
3689   (cond (n
3690          (setq n (prefix-numeric-value n))
3691          ;; There is a prefix, so we return a list of the N next
3692          ;; groups. 
3693          (let ((way (if (< n 0) -1 1))
3694                (n (abs n))
3695                group groups)
3696            (save-excursion
3697              (while (and (> n 0)
3698                          (setq group (gnus-group-group-name)))
3699                (setq groups (cons group groups))
3700                (setq n (1- n))
3701                (forward-line way)))
3702            (nreverse groups)))
3703         (gnus-group-marked
3704          ;; No prefix, but a list of marked articles.
3705          (reverse gnus-group-marked))
3706         (t
3707          ;; Neither marked articles or a prefix, so we return the
3708          ;; current group.
3709          (let ((group (gnus-group-group-name)))
3710            (and group (list group))))))
3711
3712 ;; Selecting groups.
3713
3714 (defun gnus-group-read-group (all &optional no-article group)
3715   "Read news in this newsgroup.
3716 If argument ALL is non-nil, already read articles become readable.
3717 If optional argument NO-ARTICLE is non-nil, no article body is displayed."
3718   (interactive "P")
3719   (let ((group (or group (gnus-group-group-name)))
3720         number active marked entry)
3721     (or group (error "No group on current line"))
3722     (setq marked 
3723           (nth 3 (nth 2 (setq entry (gnus-gethash group gnus-newsrc-hashtb)))))
3724     ;; This group might be a dead group. In that case we have to get
3725     ;; the number of unread articles from `gnus-active-hashtb'.
3726     (if entry
3727         (setq number (car entry))
3728       (if (setq active (gnus-gethash group gnus-active-hashtb))
3729           (setq number (- (1+ (cdr active)) (car active)))))
3730     (gnus-summary-read-group 
3731      group (or all (and (numberp number) 
3732                         (zerop (+ number (length (cdr (assq 'tick marked)))
3733                                   (length (cdr (assq 'dormant marked)))))))
3734      no-article)))
3735
3736 (defun gnus-group-select-group (all)
3737   "Select this newsgroup.
3738 No article is selected automatically.
3739 If argument ALL is non-nil, already read articles become readable."
3740   (interactive "P")
3741   (gnus-group-read-group all t))
3742
3743 ;; Enter a group that is not in the group buffer. Non-nil is returned
3744 ;; if selection was successful.
3745 (defun gnus-group-read-ephemeral-group 
3746   (group method &optional activate quit-config)
3747   (let ((group (if (gnus-group-foreign-p group) group
3748                  (gnus-group-prefixed-name group method))))
3749     (gnus-sethash 
3750      group
3751      (list t nil (list group gnus-level-default-subscribed nil nil 
3752                        (append method
3753                                (list
3754                                 (list 'quit-config 
3755                                       (if quit-config quit-config
3756                                         (cons (current-buffer) 'summary)))))))
3757      gnus-newsrc-hashtb)
3758     (set-buffer gnus-group-buffer)
3759     (or (gnus-server-opened method)
3760         (gnus-open-server method)
3761         (error "Unable to contact server: %s" (gnus-status-message method)))
3762     (if activate (gnus-request-group group))
3763     (condition-case ()
3764         (gnus-group-read-group t t group)
3765       (error nil)
3766       (quit nil))
3767     (not (equal major-mode 'gnus-group-mode))))
3768   
3769 (defun gnus-group-jump-to-group (group)
3770   "Jump to newsgroup GROUP."
3771   (interactive 
3772    (list (completing-read 
3773           "Group: " gnus-active-hashtb nil (not (not gnus-read-active-file)))))
3774
3775   (if (equal group "")
3776       (error "Empty group name"))
3777
3778   (let ((b (text-property-any 
3779             (point-min) (point-max) 'gnus-group (intern group))))
3780     (if b
3781         ;; Either go to the line in the group buffer...
3782         (goto-char b)
3783       ;; ... or insert the line.
3784       (or
3785        (gnus-gethash group gnus-active-hashtb)
3786        (gnus-activate-newsgroup group)
3787        (error "%s error: %s" group (gnus-status-message group)))
3788
3789       (gnus-group-update-group group)
3790       (goto-char (text-property-any 
3791                   (point-min) (point-max) 'gnus-group (intern group)))))
3792   ;; Adjust cursor point.
3793   (gnus-group-position-cursor))
3794
3795 (defun gnus-group-goto-group (group)
3796   "Goto to newsgroup GROUP."
3797   (let ((b (text-property-any (point-min) (point-max) 
3798                               'gnus-group (intern group))))
3799     (and b (goto-char b))))
3800
3801 (defun gnus-group-next-group (n)
3802   "Go to next N'th newsgroup.
3803 If N is negative, search backward instead.
3804 Returns the difference between N and the number of skips actually
3805 done."
3806   (interactive "p")
3807   (gnus-group-next-unread-group n t))
3808
3809 (defun gnus-group-next-unread-group (n &optional all level)
3810   "Go to next N'th unread newsgroup.
3811 If N is negative, search backward instead.
3812 If ALL is non-nil, choose any newsgroup, unread or not.
3813 If LEVEL is non-nil, choose the next group with level LEVEL, or, if no
3814 such group can be found, the next group with a level higher than
3815 LEVEL.
3816 Returns the difference between N and the number of skips actually
3817 made."
3818   (interactive "p")
3819   (let ((backward (< n 0))
3820         (n (abs n)))
3821     (while (and (> n 0)
3822                 (gnus-group-search-forward 
3823                  backward (or (not gnus-group-goto-unread) all) level))
3824       (setq n (1- n)))
3825     (if (/= 0 n) (gnus-message 7 "No more%s newsgroups%s" (if all "" " unread")
3826                                (if level " on this level or higher" "")))
3827     n))
3828
3829 (defun gnus-group-prev-group (n)
3830   "Go to previous N'th newsgroup.
3831 Returns the difference between N and the number of skips actually
3832 done."
3833   (interactive "p")
3834   (gnus-group-next-unread-group (- n) t))
3835
3836 (defun gnus-group-prev-unread-group (n)
3837   "Go to previous N'th unread newsgroup.
3838 Returns the difference between N and the number of skips actually
3839 done."  
3840   (interactive "p")
3841   (gnus-group-next-unread-group (- n)))
3842
3843 (defun gnus-group-next-unread-group-same-level (n)
3844   "Go to next N'th unread newsgroup on the same level.
3845 If N is negative, search backward instead.
3846 Returns the difference between N and the number of skips actually
3847 done."
3848   (interactive "p")
3849   (gnus-group-next-unread-group n t (gnus-group-group-level))
3850   (gnus-group-position-cursor))
3851
3852 (defun gnus-group-prev-unread-group-same-level (n)
3853   "Go to next N'th unread newsgroup on the same level.
3854 Returns the difference between N and the number of skips actually
3855 done."
3856   (interactive "p")
3857   (gnus-group-next-unread-group (- n) t (gnus-group-group-level))
3858   (gnus-group-position-cursor))
3859
3860 (defun gnus-group-best-unread-group (&optional exclude-group)
3861   "Go to the group with the highest level.
3862 If EXCLUDE-GROUP, do not go to that group."
3863   (interactive)
3864   (goto-char (point-min))
3865   (let ((best 100000)
3866         unread best-point)
3867     (while (setq unread (get-text-property (point) 'gnus-unread))
3868       (if (and (numberp unread) (> unread 0))
3869           (progn
3870             (if (and (< (get-text-property (point) 'gnus-level) best)
3871                      (or (not exclude-group)
3872                          (not (equal exclude-group (gnus-group-group-name)))))
3873                 (progn 
3874                   (setq best (get-text-property (point) 'gnus-level))
3875                   (setq best-point (point))))))
3876       (forward-line 1))
3877     (if best-point (goto-char best-point))
3878     (gnus-summary-position-cursor)
3879     (and best-point (gnus-group-group-name))))
3880
3881 (defun gnus-group-first-unread-group ()
3882   "Go to the first group with unread articles."
3883   (interactive)
3884   (goto-char (point-min))
3885   (or (not (zerop (or (get-text-property (point) 'gnus-unread) 0)))
3886       (gnus-group-next-unread-group 1))
3887   (gnus-group-position-cursor))
3888
3889 (defun gnus-group-enter-server-mode ()
3890   "Jump to the server buffer."
3891   (interactive)
3892   (gnus-server-setup-buffer)
3893   (gnus-configure-windows 'server)
3894   (gnus-server-prepare))
3895
3896 (defun gnus-group-make-group (name method &optional address)
3897   "Add a new newsgroup.
3898 The user will be prompted for a NAME, for a select METHOD, and an
3899 ADDRESS."
3900   (interactive
3901    (cons 
3902     (read-string "Group name: ")
3903     (let ((method
3904            (completing-read 
3905             "Method: " (append gnus-valid-select-methods gnus-server-alist)
3906             nil t)))
3907       (if (assoc method gnus-valid-select-methods)
3908           (list method
3909                 (if (memq 'prompt-address
3910                           (assoc method gnus-valid-select-methods))
3911                     (read-string "Address: ")
3912                   ""))
3913         (list method nil)))))
3914   
3915   (let* ((meth (if address (list (intern method) address) method))
3916          (nname (gnus-group-prefixed-name name meth))
3917          info)
3918     (and (gnus-gethash nname gnus-newsrc-hashtb)
3919          (error "Group %s already exists" nname))
3920     (gnus-group-change-level 
3921      (setq info (list t nname gnus-level-default-subscribed nil nil meth))
3922      gnus-level-default-subscribed gnus-level-killed 
3923      (and (gnus-group-group-name)
3924           (gnus-gethash (gnus-group-group-name)
3925                         gnus-newsrc-hashtb))
3926      t)
3927     (gnus-sethash nname (cons 1 0) gnus-active-hashtb)
3928     (gnus-dribble-enter 
3929      (concat "(gnus-group-set-info '" (prin1-to-string (cdr info)) ")"))
3930     (gnus-group-insert-group-line-info nname)
3931
3932     (require (intern method))
3933     (and (gnus-check-backend-function 'request-create-group nname)
3934          (gnus-request-create-group nname))))
3935
3936 (defun gnus-group-edit-group (group &optional part)
3937   "Edit the group on the current line."
3938   (interactive (list (gnus-group-group-name)))
3939   (let ((done-func '(lambda () 
3940                       "Exit editing mode and update the information."
3941                       (interactive)
3942                       (gnus-group-edit-group-done 'part 'group)))
3943         (part (or part 'info))
3944         (winconf (current-window-configuration))
3945         info)
3946     (or group (error "No group on current line"))
3947     (or (setq info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
3948         (error "Killed group; can't be edited"))
3949     (set-buffer (get-buffer-create gnus-group-edit-buffer))
3950     (gnus-configure-windows 'edit-group)
3951     (gnus-add-current-to-buffer-list)
3952     (emacs-lisp-mode)
3953     ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3954     (use-local-map (copy-keymap emacs-lisp-mode-map))
3955     (local-set-key "\C-c\C-c" done-func)
3956     (make-local-variable 'gnus-prev-winconf)
3957     (setq gnus-prev-winconf winconf)
3958     ;; We modify the func to let it know what part it is editing.
3959     (setcar (cdr (nth 4 done-func)) (list 'quote part))
3960     (setcar (cdr (cdr (nth 4 done-func))) group)
3961     (erase-buffer)
3962     (insert
3963      (cond 
3964       ((eq part 'method)
3965        ";; Type `C-c C-c' after editing the select method.\n\n")
3966       ((eq part 'params)
3967        ";; Type `C-c C-c' after editing the group parameters.\n\n")
3968       ((eq part 'info)
3969        ";; Type `C-c C-c' after editing the group info.\n\n")))
3970     (let ((cinfo (gnus-copy-sequence info))
3971           marked)
3972       (if (not (setq marked (nth 3 cinfo)))
3973           ()
3974         (while marked
3975           (or (eq 'score (car (car marked)))
3976               (eq 'bookmark (car (car marked)))
3977               (eq 'killed (car (car marked)))
3978               (not (numberp (car (cdr (car marked)))))
3979               (setcdr (car marked) 
3980                       (gnus-compress-sequence (sort (cdr (car marked)) '<) t)))
3981           (setq marked (cdr marked))))
3982       (insert 
3983        (pp-to-string
3984         (cond ((eq part 'method)
3985                (or (nth 4 info) "native"))
3986               ((eq part 'params)
3987                (nth 5 info))
3988               (t
3989                cinfo)))
3990        "\n"))))
3991
3992 (defun gnus-group-edit-group-method (group)
3993   "Edit the select method of GROUP."
3994   (interactive (list (gnus-group-group-name)))
3995   (gnus-group-edit-group group 'method))
3996
3997 (defun gnus-group-edit-group-parameters (group)
3998   "Edit the group parameters of GROUP."
3999   (interactive (list (gnus-group-group-name)))
4000   (gnus-group-edit-group group 'params))
4001
4002 (defun gnus-group-edit-group-done (part group)
4003   "Get info from buffer, update variables and jump to the group buffer."
4004   (set-buffer (get-buffer-create gnus-group-edit-buffer))
4005   (goto-char (point-min))
4006   (let ((form (read (current-buffer)))
4007         (winconf gnus-prev-winconf))
4008     (if (eq part 'info) 
4009         (gnus-group-set-info form)
4010       (gnus-group-set-info form group part))
4011     (kill-buffer (current-buffer))
4012     (and winconf (set-window-configuration winconf))
4013     (set-buffer gnus-group-buffer)
4014     (gnus-group-update-group (gnus-group-group-name))
4015     (gnus-group-position-cursor)))
4016
4017 (defun gnus-group-make-help-group ()
4018   "Create the (ding) Gnus documentation group."
4019   (interactive)
4020   (let ((path load-path)
4021         name)
4022     (and (gnus-gethash (setq name (gnus-group-prefixed-name
4023                                    "gnus-help" '(nndoc "gnus-help")))
4024                        gnus-newsrc-hashtb)
4025          (error "Documentation group already exists"))
4026     (while (and path
4027                 (not (file-exists-p (concat (file-name-as-directory (car path))
4028                                             "doc.txt"))))
4029       (setq path (cdr path)))
4030     (or path (error "Couldn't find doc group"))
4031     (gnus-group-make-group 
4032      (gnus-group-real-name name)
4033      (list 'nndoc name
4034            (list 'nndoc-address (concat (file-name-as-directory (car path)) "doc.txt"))
4035            (list 'nndoc-article-type 'mbox))))
4036   (gnus-group-position-cursor))
4037
4038 (defun gnus-group-make-doc-group (file type)
4039   "Create a group that uses a single file as the source."
4040   (interactive 
4041    (list (read-file-name "File name: ") 
4042          (let ((err "")
4043                found char)
4044            (while (not found)
4045              (message "%sFile type (mbox, babyl, digest) [mbd]: " err)
4046              (setq found (cond ((= (setq char (read-char)) ?m) 'mbox)
4047                                ((= char ?b) 'babyl)
4048                                ((= char ?d) 'digest)
4049                                (t (setq err "%c unknown. " char)
4050                                   nil))))
4051            found)))
4052   (let* ((file (expand-file-name file))
4053          (name (gnus-generate-new-group-name
4054                 (gnus-group-prefixed-name
4055                  (file-name-nondirectory file) '(nndoc "")))))
4056     (gnus-group-make-group 
4057      (gnus-group-real-name name)
4058      (list 'nndoc name
4059            (list 'nndoc-address file)
4060            (list 'nndoc-article-type type)))))
4061
4062 (defun gnus-group-make-archive-group ()
4063   "Create the (ding) Gnus archive group."
4064   (interactive)
4065   (and (gnus-gethash (gnus-group-prefixed-name "ding.archives" '(nndir ""))
4066                      gnus-newsrc-hashtb)
4067        (error "Archive group already exists"))
4068   (gnus-group-make-group "ding.archives" "nndir" gnus-group-archive-directory)
4069   (gnus-group-position-cursor))
4070
4071 (defun gnus-group-make-directory-group (dir)
4072   "Create an nndir group.
4073 The user will be prompted for a directory. The contents of this
4074 directory will be used as a newsgroup. The directory should contain
4075 mail messages or news articles in files that have numeric names."
4076   (interactive
4077    (list (read-file-name "Create group from directory: ")))
4078   (or (file-exists-p dir) (error "No such directory"))
4079   (or (file-directory-p dir) (error "Not a directory"))
4080   (gnus-group-make-group dir "nndir" dir)
4081   (gnus-group-position-cursor))
4082
4083 (defun gnus-group-make-kiboze-group (group address scores)
4084   "Create an nnkiboze group.
4085 The user will be prompted for a name, a regexp to match groups, and
4086 score file entries for articles to include in the group."
4087   (interactive
4088    (list
4089     (read-string "nnkiboze group name: ")
4090     (read-string "Source groups (regexp): ")
4091     (let ((headers (mapcar (lambda (group) (list group))
4092                            '("subject" "from" "number" "date" "message-id"
4093                              "references" "chars" "lines" "xref")))
4094           scores header regexp regexps)
4095       (while (not (equal "" (setq header (completing-read 
4096                                           "Match on header: " headers nil t))))
4097         (setq regexps nil)
4098         (while (not (equal "" (setq regexp (read-string 
4099                                             (format "Match on %s (string): "
4100                                                     header)))))
4101           (setq regexps (cons (list regexp nil nil 'r) regexps)))
4102         (setq scores (cons (cons header regexps) scores)))
4103       scores)))
4104   (gnus-group-make-group group "nnkiboze" address)
4105   (save-excursion
4106     (gnus-set-work-buffer)
4107     (let (emacs-lisp-mode-hook)
4108       (pp scores (current-buffer)))
4109     (write-region (point-min) (point-max) 
4110                   (concat (or gnus-kill-files-directory "~/News")
4111                           "nnkiboze:" group "." gnus-score-file-suffix)))
4112   (gnus-group-position-cursor))
4113
4114 (defun gnus-group-add-to-virtual (n vgroup)
4115   "Add the current group to a virtual group."
4116   (interactive
4117    (list current-prefix-arg
4118          (completing-read "Add to virtual group: " gnus-newsrc-hashtb nil t
4119                           "nnvirtual:")))
4120   (or (eq (car (gnus-find-method-for-group vgroup)) 'nnvirtual)
4121       (error "%s is not an nnvirtual group" vgroup))
4122   (let* ((groups (gnus-group-process-prefix n))
4123          (method (nth 4 (nth 2 (gnus-gethash vgroup gnus-newsrc-hashtb)))))
4124     (setcar (cdr method)
4125             (concat 
4126              (nth 1 method) "\\|"
4127              (mapconcat 
4128               (lambda (s) 
4129                 (gnus-group-remove-mark s)
4130                 (concat "\\(^" (regexp-quote s) "$\\)"))
4131               groups "\\|"))))
4132   (gnus-group-position-cursor))
4133
4134 (defun gnus-group-make-empty-virtual (group)
4135   "Create a new, fresh, empty virtual group."
4136   (interactive "sCreate new, empty virtual group: ")
4137   (let* ((method (list 'nnvirtual "^$"))
4138          (pgroup (gnus-group-prefixed-name group method)))
4139     ;; Check whether it exists already.
4140     (and (gnus-gethash pgroup gnus-newsrc-hashtb)
4141          (error "Group %s already exists." pgroup))
4142     ;; Subscribe the new group after the group on the current line.
4143     (gnus-subscribe-group pgroup (gnus-group-group-name) method)
4144     (gnus-group-update-group pgroup)
4145     (forward-line -1)
4146     (gnus-group-position-cursor)))
4147
4148 (defun gnus-group-enter-directory (dir)
4149   "Enter an ephemeral nneething group."
4150   (interactive "DDirectory to read: ")
4151   (let* ((method (list 'nneething dir))
4152          (leaf (gnus-group-prefixed-name
4153                 (file-name-nondirectory (directory-file-name dir))
4154                 method))
4155          (name (gnus-generate-new-group-name leaf)))
4156     (let ((nneething-read-only t))
4157       (or (gnus-group-read-ephemeral-group 
4158            name method t
4159            (cons (current-buffer) (if (eq major-mode 'gnus-summary-mode)
4160                                       'summary 'group)))
4161           (error "Couldn't enter %s" dir)))))
4162
4163 ;; Group sorting commands
4164 ;; Suggested by Joe Hildebrand <hildjj@idaho.fuentez.com>.
4165
4166 (defun gnus-group-sort-groups ()
4167   "Sort the group buffer using `gnus-group-sort-function'."
4168   (interactive)
4169   (setq gnus-newsrc-alist 
4170         (sort (cdr gnus-newsrc-alist) gnus-group-sort-function))
4171   (gnus-make-hashtable-from-newsrc-alist)
4172   (gnus-group-list-groups nil gnus-have-all-newsgroups))
4173
4174 (defun gnus-group-sort-by-alphabet (info1 info2)
4175   (string< (car info1) (car info2)))
4176
4177 (defun gnus-group-sort-by-unread (info1 info2)
4178   (let ((n1 (car (gnus-gethash (car info1) gnus-newsrc-hashtb)))
4179         (n2 (car (gnus-gethash (car info2) gnus-newsrc-hashtb))))
4180     (< (or (and (numberp n1) n1) 0)
4181        (or (and (numberp n2) n2) 0))))
4182
4183 (defun gnus-group-sort-by-level (info1 info2)
4184   (< (nth 1 info1) (nth 1 info2)))
4185
4186 ;; Group catching up.
4187
4188 (defun gnus-group-catchup-current (n &optional all)
4189   "Mark all articles not marked as unread in current newsgroup as read.
4190 If prefix argument N is numeric, the ARG next newsgroups will be
4191 caught up. If ALL is non-nil, marked articles will also be marked as
4192 read. Cross references (Xref: header) of articles are ignored.
4193 The difference between N and actual number of newsgroups that were
4194 caught up is returned."
4195   (interactive "P")
4196   (if (not (or (not gnus-interactive-catchup) ;Without confirmation?
4197                gnus-expert-user
4198                (gnus-y-or-n-p
4199                 (if all
4200                     "Do you really want to mark all articles as read? "
4201                   "Mark all unread articles as read? "))))
4202       n
4203     (let ((groups (gnus-group-process-prefix n))
4204           (ret 0))
4205       (while groups
4206         ;; Virtual groups have to be given special treatment. 
4207         (let ((method (gnus-find-method-for-group (car groups))))
4208           (if (eq 'nnvirtual (car method))
4209               (nnvirtual-catchup-group
4210                (gnus-group-real-name (car groups)) (nth 1 method) all)))
4211         (gnus-group-remove-mark (car groups))
4212         (if (prog1
4213                 (gnus-group-goto-group (car groups))
4214               (gnus-group-catchup (car groups) all))
4215             (gnus-group-update-group-line)
4216           (setq ret (1+ ret)))
4217         (setq groups (cdr groups)))
4218       (gnus-group-next-unread-group 1)
4219       ret)))
4220
4221 (defun gnus-group-catchup-current-all (n)
4222   "Mark all articles in current newsgroup as read.
4223 Cross references (Xref: header) of articles are ignored."
4224   (interactive "P")
4225   (gnus-group-catchup-current n 'all))
4226
4227 (defun gnus-group-catchup (group &optional all)
4228   "Mark all articles in GROUP as read.
4229 If ALL is non-nil, all articles are marked as read.
4230 The return value is the number of articles that were marked as read,
4231 or nil if no action could be taken."
4232   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4233          (num (car entry))
4234          (marked (nth 3 (nth 2 entry))))
4235     (if (not (numberp (car entry)))
4236         (gnus-message 1 "Can't catch up; non-active group")
4237       ;; Do the updating only if the newsgroup isn't killed.
4238       (if (not entry)
4239           ()
4240         (gnus-update-read-articles 
4241          group (and (not all) (append (cdr (assq 'tick marked))
4242                                       (cdr (assq 'dormant marked))))
4243          nil (and (not all) (cdr (assq 'tick marked))))
4244         (and all marked
4245              (setcar (nthcdr 3 (nth 2 entry)) 
4246                      (delq (assq 'dormant marked) 
4247                            (nth 3 (nth 2 entry)))))))
4248     num))
4249
4250 (defun gnus-group-expire-articles (n)
4251   "Expire all expirable articles in the current newsgroup."
4252   (interactive "P")
4253   (let ((groups (gnus-group-process-prefix n))
4254         group)
4255     (or groups (error "No groups to expire"))
4256     (while groups
4257       (setq group (car groups)
4258             groups (cdr groups))
4259       (gnus-group-remove-mark group)
4260       (if (not (gnus-check-backend-function 'request-expire-articles group))
4261           ()
4262         (let* ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
4263                (expirable (if (memq 'total-expire (nth 5 info))
4264                               (cons nil (gnus-list-of-read-articles group))
4265                             (assq 'expire (nth 3 info)))))
4266           (and expirable 
4267                (setcdr expirable
4268                        (gnus-request-expire-articles 
4269                         (cdr expirable) group))))))))
4270
4271 (defun gnus-group-expire-all-groups ()
4272   "Expire all expirable articles in all newsgroups."
4273   (interactive)
4274   (save-excursion
4275     (gnus-message 5 "Expiring...")
4276     (let ((gnus-group-marked (mapcar (lambda (info) (car info))
4277                                      (cdr gnus-newsrc-alist))))
4278       (gnus-group-expire-articles nil)))
4279   (gnus-group-position-cursor)
4280   (gnus-message 5 "Expiring...done"))
4281
4282 (defun gnus-group-set-current-level (n level)
4283   "Set the level of the next N groups to LEVEL."
4284   (interactive "P\nnLevel: ")
4285   (or (and (>= level 1) (<= level gnus-level-killed))
4286       (error "Illegal level: %d" level))
4287   (let ((groups (gnus-group-process-prefix n))
4288         group)
4289     (while groups
4290       (setq group (car groups)
4291             groups (cdr groups))
4292       (gnus-group-remove-mark group)
4293       (gnus-message 6 "Changed level of %s from %d to %d" 
4294                     group (gnus-group-group-level) level)
4295       (gnus-group-change-level group level
4296                                (gnus-group-group-level))
4297       (gnus-group-update-group-line)))
4298   (gnus-group-position-cursor))
4299
4300 (defun gnus-group-unsubscribe-current-group (n)
4301   "Toggle subscription of the current group.
4302 If given numerical prefix, toggle the N next groups."
4303   (interactive "P")
4304   (let ((groups (gnus-group-process-prefix n))
4305         group)
4306     (while groups
4307       (setq group (car groups)
4308             groups (cdr groups))
4309       (gnus-group-remove-mark group)
4310       (gnus-group-unsubscribe-group
4311        group (if (<= (gnus-group-group-level) gnus-level-subscribed)
4312                  gnus-level-default-unsubscribed
4313                gnus-level-default-subscribed))
4314       (gnus-group-update-group-line))
4315     (gnus-group-next-group 1)))
4316
4317 (defun gnus-group-unsubscribe-group (group &optional level)
4318   "Toggle subscribe from/to unsubscribe GROUP.
4319 New newsgroup is added to .newsrc automatically."
4320   (interactive
4321    (list (completing-read "Group: " gnus-active-hashtb nil 
4322                           gnus-have-read-active-file)))
4323   (let ((newsrc (gnus-gethash group gnus-newsrc-hashtb)))
4324     (cond (newsrc
4325            ;; Toggle subscription flag.
4326            (gnus-group-change-level 
4327             newsrc (if level level (if (<= (nth 1 (nth 2 newsrc)) 
4328                                            gnus-level-subscribed) 
4329                                        (1+ gnus-level-subscribed)
4330                                      gnus-level-default-subscribed)))
4331            (gnus-group-update-group group))
4332           ((and (stringp group)
4333                 (or (not gnus-have-read-active-file)
4334                     (gnus-gethash group gnus-active-hashtb)))
4335            ;; Add new newsgroup.
4336            (gnus-group-change-level 
4337             group 
4338             (if level level gnus-level-default-subscribed) 
4339             (or (and (member group gnus-zombie-list) 
4340                      gnus-level-zombie) 
4341                 gnus-level-killed)
4342             (and (gnus-group-group-name)
4343                  (gnus-gethash (gnus-group-group-name) gnus-newsrc-hashtb)))
4344            (gnus-group-update-group group))
4345           (t (error "No such newsgroup: %s" group)))
4346     (gnus-group-position-cursor)))
4347
4348 (defun gnus-group-transpose-groups (n)
4349   "Move the current newsgroup up N places.
4350 If given a negative prefix, move down instead. The difference between
4351 N and the number of steps taken is returned." 
4352   (interactive "p")
4353   (or (gnus-group-group-name)
4354       (error "No group on current line"))
4355   (gnus-group-kill-group 1)
4356   (prog1
4357       (forward-line (- n))
4358     (gnus-group-yank-group)
4359     (gnus-group-position-cursor)))
4360
4361 (defun gnus-group-kill-all-zombies ()
4362   "Kill all zombie newsgroups."
4363   (interactive)
4364   (setq gnus-killed-list (nconc gnus-zombie-list gnus-killed-list))
4365   (setq gnus-zombie-list nil)
4366   (funcall gnus-group-prepare-function gnus-level-subscribed nil nil)
4367   (goto-char (point-min))
4368   (gnus-group-position-cursor))
4369
4370 (defun gnus-group-kill-region (begin end)
4371   "Kill newsgroups in current region (excluding current point).
4372 The killed newsgroups can be yanked by using \\[gnus-group-yank-group]."
4373   (interactive "r")
4374   (let ((lines
4375          ;; Count lines.
4376          (save-excursion
4377            (count-lines
4378             (progn
4379               (goto-char begin)
4380               (beginning-of-line)
4381               (point))
4382             (progn
4383               (goto-char end)
4384               (beginning-of-line)
4385               (point))))))
4386     (goto-char begin)
4387     (beginning-of-line)                 ;Important when LINES < 1
4388     (gnus-group-kill-group lines)))
4389
4390 (defun gnus-group-kill-group (n)
4391   "The the next N groups.
4392 The killed newsgroups can be yanked by using \\[gnus-group-yank-group].
4393 However, only groups that were alive can be yanked; already killed 
4394 groups or zombie groups can't be yanked.
4395 The return value is the name of the (last) group that was killed."
4396   (interactive "P")
4397   (let ((buffer-read-only nil)
4398         (groups (gnus-group-process-prefix n))
4399         group entry level)
4400     (while groups
4401       (setq group (car groups)
4402             groups (cdr groups))
4403       (gnus-group-remove-mark group)
4404       (setq level (gnus-group-group-level))
4405       (gnus-delete-line)
4406       (if (setq entry (gnus-gethash group gnus-newsrc-hashtb))
4407           (setq gnus-list-of-killed-groups 
4408                 (cons (cons (car entry) (nth 2 entry)) 
4409                       gnus-list-of-killed-groups)))
4410       (gnus-group-change-level 
4411        (if entry entry group) gnus-level-killed (if entry nil level)))
4412     (gnus-group-position-cursor)
4413     group))
4414
4415 (defun gnus-group-yank-group (&optional arg)
4416   "Yank the last newsgroups killed with \\[gnus-group-kill-group],
4417 inserting it before the current newsgroup.  The numeric ARG specifies
4418 how many newsgroups are to be yanked.  The name of the (last)
4419 newsgroup yanked is returned."
4420   (interactive "p")
4421   (if (not arg) (setq arg 1))
4422   (let (info group prev)
4423     (while (>= (setq arg (1- arg)) 0)
4424       (if (not (setq info (car gnus-list-of-killed-groups)))
4425           (error "No more newsgroups to yank"))
4426       (setq group (nth 2 info))
4427       ;; Find which newsgroup to insert this one before - search
4428       ;; backward until something suitable is found. If there are no
4429       ;; other newsgroups in this buffer, just make this newsgroup the
4430       ;; first newsgroup.
4431       (setq prev (gnus-group-group-name))
4432       (gnus-group-change-level 
4433        info (nth 2 info) gnus-level-killed 
4434        (and prev (gnus-gethash prev gnus-newsrc-hashtb))
4435        t)
4436       (gnus-group-insert-group-line-info (nth 1 info))
4437       (setq gnus-list-of-killed-groups 
4438             (cdr gnus-list-of-killed-groups)))
4439     (forward-line -1)
4440     (gnus-group-position-cursor)
4441     group))
4442       
4443 (defun gnus-group-list-all-groups (arg)
4444   "List all newsgroups with level ARG or lower.
4445 Default is gnus-level-unsubscribed, which lists all subscribed and most
4446 unsubscribed groups."
4447   (interactive "P")
4448   (setq arg (or arg gnus-level-unsubscribed))
4449   (gnus-group-list-groups arg t))
4450
4451 (defun gnus-group-list-killed ()
4452   "List all killed newsgroups in the group buffer."
4453   (interactive)
4454   (if (not gnus-killed-list)
4455       (gnus-message 6 "No killed groups")
4456     (funcall gnus-group-prepare-function gnus-level-killed t gnus-level-killed)
4457     (goto-char (point-min)))
4458   (gnus-group-position-cursor))
4459
4460 (defun gnus-group-list-zombies ()
4461   "List all zombie newsgroups in the group buffer."
4462   (interactive)
4463   (if (not gnus-zombie-list)
4464       (gnus-message 6 "No zombie groups")
4465     (funcall gnus-group-prepare-function gnus-level-zombie t gnus-level-zombie)
4466     (goto-char (point-min)))
4467   (gnus-group-position-cursor))
4468
4469 (defun gnus-group-get-new-news (&optional arg)
4470   "Get newly arrived articles.
4471 If ARG is non-nil, it should be a number between one and nine to
4472 specify which levels you are interested in re-scanning."
4473   (interactive "P")
4474   (run-hooks 'gnus-get-new-news-hook)
4475   (let ((level arg))
4476     (if gnus-group-use-permanent-levels
4477         (if level
4478             (setq gnus-group-default-list-level level)
4479           (setq level (or gnus-group-default-list-level 
4480                           gnus-level-subscribed))))
4481     (if (and gnus-read-active-file (not level))
4482         (progn
4483           (gnus-read-active-file)
4484           (gnus-get-unread-articles (or level (1+ gnus-level-subscribed))))
4485       (let ((gnus-read-active-file nil)
4486             (gnus-have-read-active-file (not arg)))
4487         (gnus-get-unread-articles (or level (1+ gnus-level-subscribed)))))
4488     (gnus-group-list-groups (or (and gnus-group-use-permanent-levels level)
4489                                 gnus-group-default-list-level
4490                                 gnus-level-subscribed)
4491                             gnus-have-all-newsgroups)))
4492
4493 (defun gnus-group-get-new-news-this-group (n)
4494   "Check for newly arrived news in the current group (and the N-1 next groups).
4495 The difference between N and the number of newsgroup checked is returned.
4496 If N is negative, this group and the N-1 previous groups will be checked."
4497   (interactive "P")
4498   (let* ((groups (gnus-group-process-prefix n))
4499          (ret (if (numberp n) (- n (length groups)) 0))
4500          group)
4501     (while groups
4502       (setq group (car groups)
4503             groups (cdr groups))
4504       (gnus-group-remove-mark group)
4505       (or (gnus-get-new-news-in-group group)
4506           (progn 
4507             (ding) 
4508             (message "%s error: %s" group (gnus-status-message group))
4509             (sit-for 2))))
4510     ;; !!! I don't know why the buffer scrolls forward when updating
4511     ;; the first line in the group buffer, but it does. So we set the
4512     ;; window start forcibly.
4513 ;    (set-window-start (get-buffer-window (current-buffer)) w-p)
4514     (gnus-group-next-unread-group 1 t)
4515     (gnus-summary-position-cursor)
4516     ret))
4517
4518 (defun gnus-get-new-news-in-group (group)
4519   (and group 
4520        (gnus-activate-newsgroup group)
4521        (progn
4522          (gnus-get-unread-articles-in-group 
4523           (nth 2 (gnus-gethash group gnus-newsrc-hashtb))
4524           (gnus-gethash group gnus-active-hashtb))
4525          (gnus-group-update-group-line)
4526          t)))
4527
4528 (defun gnus-group-fetch-faq (group)
4529   "Fetch the FAQ for the current group."
4530   (interactive (list (gnus-group-real-name (gnus-group-group-name))))
4531   (or group (error "No group name given"))
4532   (let ((file (concat gnus-group-faq-directory group))) 
4533     (if (not (file-exists-p file))
4534         (error "No such file: %s" file)
4535       (find-file file))))
4536   
4537 (defun gnus-group-describe-group (force &optional group)
4538   "Display a description of the current newsgroup."
4539   (interactive (list current-prefix-arg (gnus-group-group-name)))
4540   (and force (setq gnus-description-hashtb nil))
4541   (let ((method (gnus-find-method-for-group group))
4542         desc)
4543     (or group (error "No group name given"))
4544     (and (or (and gnus-description-hashtb
4545                   ;; We check whether this group's method has been
4546                   ;; queried for a description file.  
4547                   (gnus-gethash 
4548                    (gnus-group-prefixed-name "" method) 
4549                    gnus-description-hashtb))
4550              (setq desc (gnus-group-get-description group))
4551              (gnus-read-descriptions-file method))
4552          (message
4553           (or desc (gnus-gethash group gnus-description-hashtb)
4554               "No description available")))))
4555
4556 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
4557 (defun gnus-group-describe-all-groups (force)
4558   "Pop up a buffer with descriptions of all newsgroups."
4559   (interactive "P")
4560   (and force (setq gnus-description-hashtb nil))
4561   (if (not (or gnus-description-hashtb
4562                (gnus-read-all-descriptions-files)))
4563       (error "Couldn't request descriptions file"))
4564   (let ((buffer-read-only nil)
4565         b)
4566     (erase-buffer)
4567     (mapatoms
4568      (lambda (group)
4569        (setq b (point))
4570        (insert (format "      *: %-20s %s\n" (symbol-name group)
4571                        (symbol-value group)))
4572        (add-text-properties 
4573         b (1+ b) (list 'gnus-group group
4574                        'gnus-unread t 'gnus-marked nil
4575                        'gnus-level (1+ gnus-level-subscribed))))
4576      gnus-description-hashtb)
4577     (goto-char (point-min))
4578     (gnus-group-position-cursor)))
4579
4580 ;; Suggested by by Daniel Quinlan <quinlan@best.com>.
4581 (defun gnus-group-apropos (regexp &optional search-description)
4582   "List all newsgroups that have names that match a regexp."
4583   (interactive "sGnus apropos (regexp): ")
4584   (let ((prev "")
4585         (obuf (current-buffer))
4586         groups des)
4587     ;; Go through all newsgroups that are known to Gnus.
4588     (mapatoms 
4589      (lambda (group)
4590        (and (string-match regexp (symbol-name group))
4591             (setq groups (cons (symbol-name group) groups))))
4592      gnus-active-hashtb)
4593     ;; Go through all descriptions that are known to Gnus. 
4594     (if search-description
4595         (mapatoms 
4596          (lambda (group)
4597            (and (string-match regexp (symbol-value group))
4598                 (gnus-gethash (symbol-name group) gnus-active-hashtb)
4599                 (setq groups (cons (symbol-name group) groups))))
4600          gnus-description-hashtb))
4601     (if (not groups)
4602         (gnus-message 3 "No groups matched \"%s\"." regexp)
4603       ;; Print out all the groups.
4604       (save-excursion
4605         (pop-to-buffer "*Gnus Help*")
4606         (buffer-disable-undo (current-buffer))
4607         (erase-buffer)
4608         (setq groups (sort groups 'string<))
4609         (while groups
4610           ;; Groups may be entered twice into the list of groups.
4611           (if (not (string= (car groups) prev))
4612               (progn
4613                 (insert (setq prev (car groups)) "\n")
4614                 (if (and gnus-description-hashtb
4615                          (setq des (gnus-gethash (car groups) 
4616                                                  gnus-description-hashtb)))
4617                     (insert "  " des "\n"))))
4618           (setq groups (cdr groups)))
4619         (goto-char (point-min))))
4620     (pop-to-buffer obuf)))
4621
4622 (defun gnus-group-description-apropos (regexp)
4623   "List all newsgroups that have names or descriptions that match a regexp."
4624   (interactive "sGnus description apropos (regexp): ")
4625   (if (not (or gnus-description-hashtb
4626                (gnus-read-all-descriptions-files)))
4627       (error "Couldn't request descriptions file"))
4628   (gnus-group-apropos regexp t))
4629
4630 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
4631 (defun gnus-group-list-matching (level regexp &optional all lowest) 
4632   "List all groups with unread articles that match REGEXP.
4633 If the prefix LEVEL is non-nil, it should be a number that says which
4634 level to cut off listing groups. 
4635 If ALL, also list groups with no unread articles.
4636 If LOWEST, don't list groups with level lower than LOWEST."
4637   (interactive "P\nsList newsgroups matching: ")
4638   (gnus-group-prepare-flat (or level gnus-level-subscribed)
4639                            all (or lowest 1) regexp)
4640   (goto-char (point-min))
4641   (gnus-group-position-cursor))
4642
4643 (defun gnus-group-list-all-matching (level regexp &optional lowest) 
4644   "List all groups that match REGEXP.
4645 If the prefix LEVEL is non-nil, it should be a number that says which
4646 level to cut off listing groups. 
4647 If LOWEST, don't list groups with level lower than LOWEST."
4648   (interactive "P\nsList newsgroups matching: ")
4649   (gnus-group-list-matching (or level gnus-level-killed) regexp t lowest))
4650
4651 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
4652 (defun gnus-group-save-newsrc ()
4653   "Save the Gnus startup files."
4654   (interactive)
4655   (gnus-save-newsrc-file))
4656
4657 (defun gnus-group-restart (&optional arg)
4658   "Force Gnus to read the .newsrc file."
4659   (interactive "P")
4660   (gnus-save-newsrc-file)
4661   (gnus-setup-news 'force)
4662   (gnus-group-list-groups arg gnus-have-all-newsgroups))
4663
4664 (defun gnus-group-read-init-file ()
4665   "Read the Gnus elisp init file."
4666   (interactive)
4667   (gnus-read-init-file))
4668
4669 (defun gnus-group-check-bogus-groups (silent)
4670   "Check bogus newsgroups.
4671 If given a prefix, don't ask for confirmation before removing a bogus
4672 group."
4673   (interactive "P")
4674   (gnus-check-bogus-newsgroups (and (not silent) (not gnus-expert-user)))
4675   (gnus-group-list-groups nil gnus-have-all-newsgroups))
4676
4677 (defun gnus-group-edit-global-kill (article &optional group)
4678   "Edit the global kill file.
4679 If GROUP, edit that local kill file instead."
4680   (interactive "P")
4681   (setq gnus-current-kill-article article)
4682   (gnus-kill-file-edit-file group)
4683   (gnus-message 6
4684    (substitute-command-keys
4685     "Editing a global kill file (Type \\[gnus-kill-file-exit] to exit)")))
4686
4687 (defun gnus-group-edit-local-kill (article group)
4688   "Edit a local kill file."
4689   (interactive (list nil (gnus-group-group-name)))
4690   (gnus-group-edit-global-kill article group))
4691
4692 (defun gnus-group-force-update ()
4693   "Update `.newsrc' file."
4694   (interactive)
4695   (gnus-save-newsrc-file))
4696
4697 (defun gnus-group-suspend ()
4698   "Suspend the current Gnus session.
4699 In fact, cleanup buffers except for group mode buffer.
4700 The hook gnus-suspend-gnus-hook is called before actually suspending."
4701   (interactive)
4702   (run-hooks 'gnus-suspend-gnus-hook)
4703   ;; Kill Gnus buffers except for group mode buffer.
4704   (let ((group-buf (get-buffer gnus-group-buffer)))
4705     ;; Do this on a separate list in case the user does a ^G before we finish
4706     (let ((gnus-buffer-list
4707            (delq group-buf (delq gnus-dribble-buffer
4708                                  (append gnus-buffer-list nil)))))
4709       (while gnus-buffer-list
4710         (gnus-kill-buffer (car gnus-buffer-list))
4711         (setq gnus-buffer-list (cdr gnus-buffer-list))))
4712     (if group-buf
4713         (progn
4714           (setq gnus-buffer-list (list group-buf))
4715           (bury-buffer group-buf)
4716           (delete-windows-on group-buf t)))))
4717
4718 (defun gnus-group-clear-dribble ()
4719   "Clear all information from the dribble buffer."
4720   (interactive)
4721   (gnus-dribble-clear))
4722
4723 (defun gnus-group-exit ()
4724   "Quit reading news after updating .newsrc.eld and .newsrc.
4725 The hook `gnus-exit-gnus-hook' is called before actually exiting."
4726   (interactive)
4727   (if (or noninteractive                ;For gnus-batch-kill
4728           (zerop (buffer-size))         ;No news is good news.
4729           (not (gnus-server-opened gnus-select-method)) ;NNTP connection closed
4730           (not gnus-interactive-exit)   ;Without confirmation
4731           gnus-expert-user
4732           (gnus-y-or-n-p "Are you sure you want to quit reading news? "))
4733       (progn
4734         (if gnus-use-full-window
4735             (delete-other-windows)
4736           (gnus-remove-some-windows))
4737         (run-hooks 'gnus-exit-gnus-hook)
4738         (gnus-offer-save-summaries)
4739         (gnus-save-newsrc-file)
4740         (gnus-close-backends)
4741         (gnus-clear-system))))
4742
4743 (defun gnus-close-backends ()
4744   ;; Send a close request to all backends that support such a request. 
4745   (let ((methods gnus-valid-select-methods)
4746         func)
4747     (while methods
4748       (if (fboundp (setq func (intern (concat (car (car methods))
4749                                               "-request-close"))))
4750           (funcall func))
4751       (setq methods (cdr methods)))))
4752
4753 (defun gnus-group-quit ()
4754   "Quit reading news without updating .newsrc.eld or .newsrc.
4755 The hook `gnus-exit-gnus-hook' is called before actually exiting."
4756   (interactive)
4757   (if (or noninteractive                ;For gnus-batch-kill
4758           (zerop (buffer-size))
4759           (not (gnus-server-opened gnus-select-method))
4760           gnus-expert-user
4761           (not gnus-current-startup-file)
4762           (gnus-yes-or-no-p
4763            (format "Quit reading news without saving %s? "
4764                    (file-name-nondirectory gnus-current-startup-file))))
4765       (progn
4766         (run-hooks 'gnus-exit-gnus-hook)
4767         (if gnus-use-full-window
4768             (delete-other-windows)
4769           (gnus-remove-some-windows))
4770         (gnus-dribble-save)
4771         (gnus-close-backends)
4772         (gnus-clear-system))))
4773
4774 (defun gnus-offer-save-summaries ()
4775   (let ((buffers (buffer-list)))
4776     (save-excursion
4777       (while buffers
4778         (and 
4779          ;; We look for buffers with "Summary" in the name.
4780          (string-match "Summary" (or (buffer-name (car buffers)) ""))
4781          (progn
4782            (set-buffer (car buffers))
4783            ;; We check that this is, indeed, a summary buffer.
4784            (eq major-mode 'gnus-summary-mode)) 
4785          ;; We ask the user whether she wants to save the info.
4786          (gnus-y-or-n-p
4787                (format "Update summary buffer %s? " (buffer-name)))
4788          ;; We do it by simply exiting.
4789          (gnus-summary-exit))
4790         (setq buffers (cdr buffers))))))
4791
4792 (defun gnus-group-describe-briefly ()
4793   "Give a one line description of the group mode commands."
4794   (interactive)
4795   (gnus-message 6
4796    (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")))
4797
4798 (defun gnus-group-browse-foreign-server (method)
4799   "Browse a foreign news server.
4800 If called interactively, this function will ask for a select method
4801  (nntp, nnspool, etc.) and a server address (eg. nntp.some.where). 
4802 If not, METHOD should be a list where the first element is the method
4803 and the second element is the address."
4804   (interactive
4805    (list (let ((how (completing-read 
4806                      "Which backend: "
4807                      (append gnus-valid-select-methods gnus-server-alist)
4808                      nil t "nntp")))
4809            ;; We either got a backend name or a virtual server name.
4810            ;; If the first, we also need an address.
4811            (if (assoc how gnus-valid-select-methods)
4812                (list (intern how)
4813                      ;; Suggested by mapjph@bath.ac.uk.
4814                      (completing-read 
4815                       "Address: " 
4816                       (mapcar (lambda (server) (list server))
4817                               gnus-secondary-servers)))
4818              ;; We got a server name, so we find the method.
4819              (gnus-server-to-method how)))))
4820   (gnus-browse-foreign-server method))
4821
4822 \f
4823 ;;;
4824 ;;; Browse Server Mode
4825 ;;;
4826
4827 (defvar gnus-browse-mode-hook nil)
4828 (defvar gnus-browse-mode-map nil)
4829 (put 'gnus-browse-mode 'mode-class 'special)
4830
4831 (if gnus-browse-mode-map
4832     nil
4833   (setq gnus-browse-mode-map (make-keymap))
4834   (suppress-keymap gnus-browse-mode-map)
4835   (define-key gnus-browse-mode-map " " 'gnus-browse-read-group)
4836   (define-key gnus-browse-mode-map "=" 'gnus-browse-select-group)
4837   (define-key gnus-browse-mode-map "n" 'gnus-browse-next-group)
4838   (define-key gnus-browse-mode-map "p" 'gnus-browse-prev-group)
4839   (define-key gnus-browse-mode-map "\177" 'gnus-browse-prev-group)
4840   (define-key gnus-browse-mode-map "N" 'gnus-browse-next-group)
4841   (define-key gnus-browse-mode-map "P" 'gnus-browse-prev-group)
4842   (define-key gnus-browse-mode-map "\M-n" 'gnus-browse-next-group)
4843   (define-key gnus-browse-mode-map "\M-p" 'gnus-browse-prev-group)
4844   (define-key gnus-browse-mode-map "\r" 'gnus-browse-select-group)
4845   (define-key gnus-browse-mode-map "u" 'gnus-browse-unsubscribe-current-group)
4846   (define-key gnus-browse-mode-map "l" 'gnus-browse-exit)
4847   (define-key gnus-browse-mode-map "L" 'gnus-browse-exit)
4848   (define-key gnus-browse-mode-map "q" 'gnus-browse-exit)
4849   (define-key gnus-browse-mode-map "Q" 'gnus-browse-exit)
4850   (define-key gnus-browse-mode-map "\C-c\C-c" 'gnus-browse-exit)
4851   (define-key gnus-browse-mode-map "?" 'gnus-browse-describe-briefly)
4852   (define-key gnus-browse-mode-map "\C-c\C-i" 'gnus-info-find-node)
4853   )
4854
4855 (defvar gnus-browse-current-method nil)
4856 (defvar gnus-browse-return-buffer nil)
4857
4858 (defvar gnus-browse-buffer "*Gnus Browse Server*")
4859
4860 (defun gnus-browse-foreign-server (method &optional return-buffer)
4861   (setq gnus-browse-current-method method)
4862   (setq gnus-browse-return-buffer return-buffer)
4863   (let ((gnus-select-method method)
4864         groups group)
4865     (gnus-message 5 "Connecting to %s..." (nth 1 method))
4866     (or (gnus-server-opened method)
4867         (gnus-open-server method)
4868         (error "Unable to contact server: %s" (gnus-status-message method)))
4869     (or (gnus-request-list method)
4870         (error "Couldn't request list: %s" (gnus-status-message method)))
4871     (get-buffer-create gnus-browse-buffer)
4872     (gnus-add-current-to-buffer-list)
4873     (and gnus-carpal (gnus-carpal-setup-buffer 'browse))
4874     (gnus-configure-windows 'browse)
4875     (buffer-disable-undo (current-buffer))
4876     (let ((buffer-read-only nil))
4877       (erase-buffer))
4878     (gnus-browse-mode)
4879     (setq mode-line-buffer-identification
4880           (format
4881            "(ding) Browse Server {%s:%s}" (car method) (car (cdr method))))
4882     (save-excursion
4883       (set-buffer nntp-server-buffer)
4884       (let ((cur (current-buffer)))
4885         (goto-char (point-min))
4886         (or (string= gnus-ignored-newsgroups "")
4887             (delete-matching-lines gnus-ignored-newsgroups))
4888         (while (re-search-forward 
4889                 "\\(^[^ \t]+\\)[ \t]+[0-9]+[ \t]+[0-9]+" nil t)
4890           (goto-char (match-end 1))
4891           (setq groups (cons (cons (buffer-substring (match-beginning 1)
4892                                                      (match-end 1))
4893                                    (max 0 (- (1+ (read cur)) (read cur))))
4894                              groups)))))
4895     (setq groups (sort groups 
4896                        (lambda (l1 l2)
4897                          (string< (car l1) (car l2)))))
4898     (let ((buffer-read-only nil))
4899       (while groups
4900         (setq group (car groups))
4901         (insert 
4902          (format "K%7d: %s\n" (cdr group) (car group)))
4903         (setq groups (cdr groups))))
4904     (switch-to-buffer (current-buffer))
4905     (goto-char (point-min))
4906     (gnus-group-position-cursor)))
4907
4908 (defun gnus-browse-mode ()
4909   "Major mode for browsing a foreign server.
4910
4911 All normal editing commands are switched off.
4912
4913 \\<gnus-browse-mode-map>
4914 The only things you can do in this buffer is
4915
4916 1) `\\[gnus-browse-unsubscribe-current-group]' to subscribe to a group.
4917 The group will be inserted into the group buffer upon exit from this
4918 buffer.  
4919
4920 2) `\\[gnus-browse-read-group]' to read a group ephemerally.
4921
4922 3) `\\[gnus-browse-exit]' to return to the group buffer."
4923   (interactive)
4924   (kill-all-local-variables)
4925   (if gnus-visual (gnus-browse-make-menu-bar))
4926   (setq mode-line-modified "-- ")
4927   (make-local-variable 'mode-line-format)
4928   (setq mode-line-format (copy-sequence mode-line-format))
4929   (and (equal (nth 3 mode-line-format) "   ")
4930        (setcar (nthcdr 3 mode-line-format) ""))
4931   (setq major-mode 'gnus-browse-mode)
4932   (setq mode-name "Browse Server")
4933   (setq mode-line-process nil)
4934   (use-local-map gnus-browse-mode-map)
4935   (buffer-disable-undo (current-buffer))
4936   (setq truncate-lines t)
4937   (setq buffer-read-only t)
4938   (run-hooks 'gnus-browse-mode-hook))
4939
4940 (defun gnus-browse-read-group (&optional no-article)
4941   "Enter the group at the current line."
4942   (interactive)
4943   (let ((group (gnus-browse-group-name)))
4944     (or (gnus-group-read-ephemeral-group 
4945          group gnus-browse-current-method nil
4946          (cons (current-buffer) 'browse))
4947         (error "Couldn't enter %s" group))))
4948
4949 (defun gnus-browse-select-group ()
4950   "Select the current group."
4951   (interactive)
4952   (gnus-browse-read-group 'no))
4953
4954 (defun gnus-browse-next-group (n)
4955   "Go to the next group."
4956   (interactive "p")
4957   (prog1
4958       (forward-line n)
4959     (gnus-group-position-cursor)))
4960
4961 (defun gnus-browse-prev-group (n)
4962   "Go to the next group."
4963   (interactive "p")
4964   (gnus-browse-next-group (- n)))
4965
4966 (defun gnus-browse-unsubscribe-current-group (arg)
4967   "(Un)subscribe to the next ARG groups."
4968   (interactive "p")
4969   (and (eobp)
4970        (error "No group at current line."))
4971   (let ((ward (if (< arg 0) -1 1))
4972         (arg (abs arg)))
4973     (while (and (> arg 0)
4974                 (not (eobp))
4975                 (gnus-browse-unsubscribe-group)
4976                 (zerop (gnus-browse-next-group ward)))
4977       (setq arg (1- arg)))
4978     (gnus-group-position-cursor)
4979     (if (/= 0 arg) (gnus-message 7 "No more newsgroups"))
4980     arg))
4981
4982 (defun gnus-browse-group-name ()
4983   (save-excursion
4984     (beginning-of-line)
4985     (if (not (re-search-forward ": \\(.*\\)$" (gnus-point-at-eol) t))
4986         ()
4987       (gnus-group-prefixed-name 
4988        (buffer-substring (match-beginning 1) (match-end 1))
4989        gnus-browse-current-method))))
4990   
4991 (defun gnus-browse-unsubscribe-group ()
4992   (let ((sub nil)
4993         (buffer-read-only nil)
4994         group)
4995     (save-excursion
4996       (beginning-of-line)
4997       (if (= (following-char) ?K) (setq sub t))
4998       (setq group (gnus-browse-group-name))
4999       (beginning-of-line)
5000       (delete-char 1)
5001       (if sub
5002           (progn
5003             (gnus-group-change-level 
5004              (list t group gnus-level-default-subscribed
5005                    nil nil gnus-browse-current-method) 
5006              gnus-level-default-subscribed gnus-level-killed
5007              (gnus-gethash (car (nth 1 gnus-newsrc-alist)) gnus-newsrc-hashtb)
5008              t)
5009             (insert ? ))
5010         (gnus-group-change-level 
5011          group gnus-level-killed gnus-level-default-subscribed)
5012         (insert ?K)))
5013     t))
5014
5015 (defun gnus-browse-exit ()
5016   "Quit browsing and return to the group buffer."
5017   (interactive)
5018   (if (eq major-mode 'gnus-browse-mode)
5019       (kill-buffer (current-buffer)))
5020   (if gnus-browse-return-buffer
5021       (gnus-configure-windows 'server)
5022     (gnus-configure-windows 'group)
5023     (gnus-group-list-groups nil)))
5024
5025 (defun gnus-browse-describe-briefly ()
5026   "Give a one line description of the group mode commands."
5027   (interactive)
5028   (gnus-message 6
5029    (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")))
5030       
5031 \f
5032 ;;;
5033 ;;; Gnus summary mode
5034 ;;;
5035
5036 (defvar gnus-summary-mode-map nil)
5037 (defvar gnus-summary-mark-map nil)
5038 (defvar gnus-summary-mscore-map nil)
5039 (defvar gnus-summary-article-map nil)
5040 (defvar gnus-summary-thread-map nil)
5041 (defvar gnus-summary-goto-map nil)
5042 (defvar gnus-summary-exit-map nil)
5043 (defvar gnus-summary-various-map nil)
5044 (defvar gnus-summary-interest-map nil)
5045 (defvar gnus-summary-sort-map nil)
5046 (defvar gnus-summary-backend-map nil)
5047 (defvar gnus-summary-save-map nil)
5048 (defvar gnus-summary-wash-map nil)
5049 (defvar gnus-summary-help-map nil)
5050
5051 (put 'gnus-summary-mode 'mode-class 'special)
5052
5053 (if gnus-summary-mode-map
5054     nil
5055   (setq gnus-summary-mode-map (make-keymap))
5056   (suppress-keymap gnus-summary-mode-map)
5057
5058   ;; Non-orthogonal keys
5059
5060   (define-key gnus-summary-mode-map " " 'gnus-summary-next-page)
5061   (define-key gnus-summary-mode-map "\177" 'gnus-summary-prev-page)
5062   (define-key gnus-summary-mode-map "\r" 'gnus-summary-scroll-up)
5063   (define-key gnus-summary-mode-map "n" 'gnus-summary-next-unread-article)
5064   (define-key gnus-summary-mode-map "p" 'gnus-summary-prev-unread-article)
5065   (define-key gnus-summary-mode-map "N" 'gnus-summary-next-article)
5066   (define-key gnus-summary-mode-map "P" 'gnus-summary-prev-article)
5067   (define-key gnus-summary-mode-map "\M-\C-n" 'gnus-summary-next-same-subject)
5068   (define-key gnus-summary-mode-map "\M-\C-p" 'gnus-summary-prev-same-subject)
5069   (define-key gnus-summary-mode-map "\M-n" 'gnus-summary-next-unread-subject)
5070   (define-key gnus-summary-mode-map "\M-p" 'gnus-summary-prev-unread-subject)
5071   (define-key gnus-summary-mode-map "." 'gnus-summary-first-unread-article)
5072   (define-key gnus-summary-mode-map "," 'gnus-summary-best-unread-article)
5073   (define-key gnus-summary-mode-map "\M-s" 'gnus-summary-search-article-forward)
5074   (define-key gnus-summary-mode-map "\M-r" 'gnus-summary-search-article-backward)
5075   (define-key gnus-summary-mode-map "<" 'gnus-summary-beginning-of-article)
5076   (define-key gnus-summary-mode-map ">" 'gnus-summary-end-of-article)
5077   (define-key gnus-summary-mode-map "j" 'gnus-summary-goto-subject)
5078   (define-key gnus-summary-mode-map "^" 'gnus-summary-refer-parent-article)
5079   (define-key gnus-summary-mode-map "\M-^" 'gnus-summary-refer-article)
5080   (define-key gnus-summary-mode-map "u" 'gnus-summary-tick-article-forward)
5081   (define-key gnus-summary-mode-map "!" 'gnus-summary-tick-article-forward)
5082   (define-key gnus-summary-mode-map "U" 'gnus-summary-tick-article-backward)
5083   (define-key gnus-summary-mode-map "d" 'gnus-summary-mark-as-read-forward)
5084   (define-key gnus-summary-mode-map "D" 'gnus-summary-mark-as-read-backward)
5085   (define-key gnus-summary-mode-map "E" 'gnus-summary-mark-as-expirable)
5086   (define-key gnus-summary-mode-map "\M-u" 'gnus-summary-clear-mark-forward)
5087   (define-key gnus-summary-mode-map "\M-U" 'gnus-summary-clear-mark-backward)
5088   (define-key gnus-summary-mode-map "k" 'gnus-summary-kill-same-subject-and-select)
5089   (define-key gnus-summary-mode-map "\C-k" 'gnus-summary-kill-same-subject)
5090   (define-key gnus-summary-mode-map "\M-\C-k" 'gnus-summary-kill-thread)
5091   (define-key gnus-summary-mode-map "\M-\C-l" 'gnus-summary-lower-thread)
5092   (define-key gnus-summary-mode-map "e" 'gnus-summary-edit-article)
5093   (define-key gnus-summary-mode-map "#" 'gnus-summary-mark-as-processable)
5094   (define-key gnus-summary-mode-map "\M-#" 'gnus-summary-unmark-as-processable)
5095   (define-key gnus-summary-mode-map "\M-\C-t" 'gnus-summary-toggle-threads)
5096   (define-key gnus-summary-mode-map "\M-\C-s" 'gnus-summary-show-thread)
5097   (define-key gnus-summary-mode-map "\M-\C-h" 'gnus-summary-hide-thread)
5098   (define-key gnus-summary-mode-map "\M-\C-f" 'gnus-summary-next-thread)
5099   (define-key gnus-summary-mode-map "\M-\C-b" 'gnus-summary-prev-thread)
5100   (define-key gnus-summary-mode-map "\M-\C-u" 'gnus-summary-up-thread)
5101   (define-key gnus-summary-mode-map "\M-\C-d" 'gnus-summary-down-thread)
5102   (define-key gnus-summary-mode-map "&" 'gnus-summary-execute-command)
5103   (define-key gnus-summary-mode-map "c" 'gnus-summary-catchup-and-exit)
5104   (define-key gnus-summary-mode-map "\C-w" 'gnus-summary-mark-region-as-read)
5105   (define-key gnus-summary-mode-map "\C-t" 'gnus-summary-toggle-truncation)
5106   (define-key gnus-summary-mode-map "?" 'gnus-summary-mark-as-dormant)
5107   (define-key gnus-summary-mode-map "\C-c\M-\C-s" 'gnus-summary-show-all-expunged)
5108   (define-key gnus-summary-mode-map "\C-c\C-s\C-n" 'gnus-summary-sort-by-number)
5109   (define-key gnus-summary-mode-map "\C-c\C-s\C-a" 'gnus-summary-sort-by-author)
5110   (define-key gnus-summary-mode-map "\C-c\C-s\C-s" 'gnus-summary-sort-by-subject)
5111   (define-key gnus-summary-mode-map "\C-c\C-s\C-d" 'gnus-summary-sort-by-date)
5112   (define-key gnus-summary-mode-map "\C-c\C-s\C-i" 'gnus-summary-sort-by-score)
5113   (define-key gnus-summary-mode-map "=" 'gnus-summary-expand-window)
5114   (define-key gnus-summary-mode-map "\C-x\C-s" 'gnus-summary-reselect-current-group)
5115   (define-key gnus-summary-mode-map "\M-g" 'gnus-summary-rescan-group)
5116   (define-key gnus-summary-mode-map "w" 'gnus-summary-stop-page-breaking)
5117   (define-key gnus-summary-mode-map "\C-c\C-r" 'gnus-summary-caesar-message)
5118   (define-key gnus-summary-mode-map "\M-t" 'gnus-summary-toggle-mime)
5119   (define-key gnus-summary-mode-map "f" 'gnus-summary-followup)
5120   (define-key gnus-summary-mode-map "F" 'gnus-summary-followup-with-original)
5121   (define-key gnus-summary-mode-map "C" 'gnus-summary-cancel-article)
5122   (define-key gnus-summary-mode-map "r" 'gnus-summary-reply)
5123   (define-key gnus-summary-mode-map "R" 'gnus-summary-reply-with-original)
5124   (define-key gnus-summary-mode-map "\C-c\C-f" 'gnus-summary-mail-forward)
5125   (define-key gnus-summary-mode-map "o" 'gnus-summary-save-article)
5126   (define-key gnus-summary-mode-map "\C-o" 'gnus-summary-save-article-mail)
5127   (define-key gnus-summary-mode-map "|" 'gnus-summary-pipe-output)
5128   (define-key gnus-summary-mode-map "\M-k" 'gnus-summary-edit-local-kill)
5129   (define-key gnus-summary-mode-map "\M-K" 'gnus-summary-edit-global-kill)
5130   (define-key gnus-summary-mode-map "V" 'gnus-version)
5131   (define-key gnus-summary-mode-map "\C-c\C-d" 'gnus-summary-describe-group)
5132   (define-key gnus-summary-mode-map "q" 'gnus-summary-exit)
5133   (define-key gnus-summary-mode-map "Q" 'gnus-summary-exit-no-update)
5134   (define-key gnus-summary-mode-map "\C-c\C-i" 'gnus-info-find-node)
5135   (define-key gnus-summary-mode-map gnus-mouse-2 'gnus-mouse-pick-article)
5136   (define-key gnus-summary-mode-map "m" 'gnus-summary-mail-other-window)
5137   (define-key gnus-summary-mode-map "a" 'gnus-summary-post-news)
5138   (define-key gnus-summary-mode-map "x" 'gnus-summary-remove-lines-marked-as-read)
5139 ; (define-key gnus-summary-mode-map "X" 'gnus-summary-remove-lines-marked-with)
5140   (define-key gnus-summary-mode-map "s" 'gnus-summary-isearch-article)
5141   (define-key gnus-summary-mode-map "t" 'gnus-summary-toggle-header)
5142   (define-key gnus-summary-mode-map "g" 'gnus-summary-show-article)
5143 ;  (define-key gnus-summary-mode-map "?" 'gnus-summary-describe-briefly)
5144   (define-key gnus-summary-mode-map "l" 'gnus-summary-goto-last-article)
5145   (define-key gnus-summary-mode-map "\C-c\C-v\C-v" 'gnus-uu-decode-uu-view)
5146   (define-key gnus-summary-mode-map "\C-d" 'gnus-summary-enter-digest-group)
5147   (define-key gnus-summary-mode-map "v" 'gnus-summary-verbose-headers)
5148   (define-key gnus-summary-mode-map "\C-c\C-b" 'gnus-bug)
5149
5150
5151   ;; Sort of orthogonal keymap
5152   (define-prefix-command 'gnus-summary-mark-map)
5153   (define-key gnus-summary-mode-map "M" 'gnus-summary-mark-map)
5154   (define-key gnus-summary-mark-map "t" 'gnus-summary-tick-article-forward)
5155   (define-key gnus-summary-mark-map "!" 'gnus-summary-tick-article-forward)
5156   (define-key gnus-summary-mark-map "d" 'gnus-summary-mark-as-read-forward)
5157   (define-key gnus-summary-mark-map "r" 'gnus-summary-mark-as-read-forward)
5158   (define-key gnus-summary-mark-map "c" 'gnus-summary-clear-mark-forward)
5159   (define-key gnus-summary-mark-map " " 'gnus-summary-clear-mark-forward)
5160   (define-key gnus-summary-mark-map "e" 'gnus-summary-mark-as-expirable)
5161   (define-key gnus-summary-mark-map "x" 'gnus-summary-mark-as-expirable)
5162   (define-key gnus-summary-mark-map "?" 'gnus-summary-mark-as-dormant)
5163   (define-key gnus-summary-mark-map "b" 'gnus-summary-set-bookmark)
5164   (define-key gnus-summary-mark-map "B" 'gnus-summary-remove-bookmark)
5165   (define-key gnus-summary-mark-map "#" 'gnus-summary-mark-as-processable)
5166   (define-key gnus-summary-mark-map "\M-#" 'gnus-summary-unmark-as-processable)
5167   (define-key gnus-summary-mark-map "\M-r" 'gnus-summary-remove-lines-marked-as-read)
5168   (define-key gnus-summary-mark-map "\M-\C-r" 'gnus-summary-remove-lines-marked-with)
5169   (define-key gnus-summary-mark-map "D" 'gnus-summary-show-all-dormant)
5170   (define-key gnus-summary-mark-map "\M-D" 'gnus-summary-hide-all-dormant)
5171   (define-key gnus-summary-mark-map "S" 'gnus-summary-show-all-expunged)
5172   (define-key gnus-summary-mark-map "C" 'gnus-summary-catchup)
5173   (define-key gnus-summary-mark-map "H" 'gnus-summary-catchup-to-here)
5174   (define-key gnus-summary-mark-map "\C-c" 'gnus-summary-catchup-all)
5175   (define-key gnus-summary-mark-map "k" 'gnus-summary-kill-same-subject-and-select)
5176   (define-key gnus-summary-mark-map "K" 'gnus-summary-kill-same-subject)
5177
5178   (define-prefix-command 'gnus-summary-mscore-map)
5179   (define-key gnus-summary-mark-map "s" 'gnus-summary-mscore-map)
5180   (define-key gnus-summary-mscore-map "c" 'gnus-summary-clear-above)
5181   (define-key gnus-summary-mscore-map "u" 'gnus-summary-tick-above)
5182   (define-key gnus-summary-mscore-map "m" 'gnus-summary-mark-above)
5183   (define-key gnus-summary-mscore-map "k" 'gnus-summary-kill-below)
5184
5185   (define-key gnus-summary-mark-map "p" 'gnus-uu-mark-map)
5186   
5187   (define-key gnus-summary-mode-map "S" 'gnus-summary-send-map)
5188   
5189   (define-prefix-command 'gnus-summary-goto-map)
5190   (define-key gnus-summary-mode-map "G" 'gnus-summary-goto-map)
5191   (define-key gnus-summary-goto-map "n" 'gnus-summary-next-unread-article)
5192   (define-key gnus-summary-goto-map "p" 'gnus-summary-prev-unread-article)
5193   (define-key gnus-summary-goto-map "N" 'gnus-summary-next-article)
5194   (define-key gnus-summary-goto-map "P" 'gnus-summary-prev-article)
5195   (define-key gnus-summary-goto-map "\C-n" 'gnus-summary-next-same-subject)
5196   (define-key gnus-summary-goto-map "\C-p" 'gnus-summary-prev-same-subject)
5197   (define-key gnus-summary-goto-map "\M-n" 'gnus-summary-next-unread-subject)
5198   (define-key gnus-summary-goto-map "\M-p" 'gnus-summary-prev-unread-subject)
5199   (define-key gnus-summary-goto-map "f" 'gnus-summary-first-unread-article)
5200   (define-key gnus-summary-goto-map "b" 'gnus-summary-best-unread-article)
5201   (define-key gnus-summary-goto-map "g" 'gnus-summary-goto-subject)
5202   (define-key gnus-summary-goto-map "l" 'gnus-summary-goto-last-article)
5203   (define-key gnus-summary-goto-map "p" 'gnus-summary-pop-article)
5204
5205
5206   (define-prefix-command 'gnus-summary-thread-map)
5207   (define-key gnus-summary-mode-map "T" 'gnus-summary-thread-map)
5208   (define-key gnus-summary-thread-map "k" 'gnus-summary-kill-thread)
5209   (define-key gnus-summary-thread-map "l" 'gnus-summary-lower-thread)
5210   (define-key gnus-summary-thread-map "i" 'gnus-summary-raise-thread)
5211   (define-key gnus-summary-thread-map "T" 'gnus-summary-toggle-threads)
5212   (define-key gnus-summary-thread-map "s" 'gnus-summary-show-thread)
5213   (define-key gnus-summary-thread-map "S" 'gnus-summary-show-all-threads)
5214   (define-key gnus-summary-thread-map "h" 'gnus-summary-hide-thread)
5215   (define-key gnus-summary-thread-map "H" 'gnus-summary-hide-all-threads)
5216   (define-key gnus-summary-thread-map "n" 'gnus-summary-next-thread)
5217   (define-key gnus-summary-thread-map "p" 'gnus-summary-prev-thread)
5218   (define-key gnus-summary-thread-map "u" 'gnus-summary-up-thread)
5219   (define-key gnus-summary-thread-map "d" 'gnus-summary-down-thread)
5220   (define-key gnus-summary-thread-map "#" 'gnus-uu-mark-thread)
5221
5222   
5223   (define-prefix-command 'gnus-summary-exit-map)
5224   (define-key gnus-summary-mode-map "Z" 'gnus-summary-exit-map)
5225   (define-key gnus-summary-exit-map "c" 'gnus-summary-catchup-and-exit)
5226   (define-key gnus-summary-exit-map "C" 'gnus-summary-catchup-all-and-exit)
5227   (define-key gnus-summary-exit-map "E" 'gnus-summary-exit-no-update)
5228   (define-key gnus-summary-exit-map "Q" 'gnus-summary-exit)
5229   (define-key gnus-summary-exit-map "Z" 'gnus-summary-exit)
5230   (define-key gnus-summary-exit-map "n" 'gnus-summary-catchup-and-goto-next-group)
5231   (define-key gnus-summary-exit-map "R" 'gnus-summary-reselect-current-group)
5232   (define-key gnus-summary-exit-map "G" 'gnus-summary-rescan-group)
5233   (define-key gnus-summary-exit-map "N" 'gnus-summary-next-group)
5234   (define-key gnus-summary-exit-map "P" 'gnus-summary-prev-group)
5235
5236
5237   (define-prefix-command 'gnus-summary-article-map)
5238   (define-key gnus-summary-mode-map "A" 'gnus-summary-article-map)
5239   (define-key gnus-summary-article-map " " 'gnus-summary-next-page)
5240   (define-key gnus-summary-article-map "n" 'gnus-summary-next-page)
5241   (define-key gnus-summary-article-map "\177" 'gnus-summary-prev-page)
5242   (define-key gnus-summary-article-map "p" 'gnus-summary-prev-page)
5243   (define-key gnus-summary-article-map "\r" 'gnus-summary-scroll-up)
5244   (define-key gnus-summary-article-map "<" 'gnus-summary-beginning-of-article)
5245   (define-key gnus-summary-article-map ">" 'gnus-summary-end-of-article)
5246   (define-key gnus-summary-article-map "b" 'gnus-summary-beginning-of-article)
5247   (define-key gnus-summary-article-map "e" 'gnus-summary-end-of-article)
5248   (define-key gnus-summary-article-map "^" 'gnus-summary-refer-parent-article)
5249   (define-key gnus-summary-article-map "r" 'gnus-summary-refer-parent-article)
5250   (define-key gnus-summary-article-map "w" 'gnus-summary-stop-page-breaking)
5251   (define-key gnus-summary-article-map "c" 'gnus-summary-caesar-message)
5252   (define-key gnus-summary-article-map "g" 'gnus-summary-show-article)
5253   (define-key gnus-summary-article-map "t" 'gnus-summary-toggle-header)
5254   (define-key gnus-summary-article-map "m" 'gnus-summary-toggle-mime)
5255   (define-key gnus-summary-article-map "s" 'gnus-summary-isearch-article)
5256
5257
5258   (define-prefix-command 'gnus-summary-wash-map)
5259   (define-key gnus-summary-mode-map "W" 'gnus-summary-wash-map)
5260   (define-key gnus-summary-wash-map "h" 'gnus-article-hide-headers)
5261   (define-key gnus-summary-wash-map "s" 'gnus-article-hide-signature)
5262   (define-key gnus-summary-wash-map "c" 'gnus-article-hide-citation)
5263   (define-key gnus-summary-wash-map "o" 'gnus-article-treat-overstrike)
5264   (define-key gnus-summary-wash-map "w" 'gnus-article-word-wrap)
5265   (define-key gnus-summary-wash-map "d" 'gnus-article-remove-cr)
5266   (define-key gnus-summary-wash-map "q" 'gnus-article-de-quoted-unreadable)
5267   (define-key gnus-summary-wash-map "f" 'gnus-article-display-x-face)
5268   (define-key gnus-summary-wash-map "t" 'gnus-article-date-ut)
5269   (define-key gnus-summary-wash-map "\C-t" 'gnus-article-date-local)
5270   (define-key gnus-summary-wash-map "T" 'gnus-article-date-lapsed)
5271
5272   (define-key gnus-summary-wash-map "A" 'gnus-article-highlight)
5273   (define-key gnus-summary-wash-map "a" 'gnus-article-hide)
5274   (define-key gnus-summary-wash-map "H" 'gnus-article-highlight-headers)
5275   (define-key gnus-summary-wash-map "C" 'gnus-article-highlight-citation)
5276   (define-key gnus-summary-wash-map "S" 'gnus-article-highlight-signature)
5277   (define-key gnus-summary-wash-map "b" 'gnus-article-add-buttons)
5278
5279
5280   (define-prefix-command 'gnus-summary-help-map)
5281   (define-key gnus-summary-mode-map "H" 'gnus-summary-help-map)
5282   (define-key gnus-summary-help-map "v" 'gnus-version)
5283   (define-key gnus-summary-help-map "f" 'gnus-summary-fetch-faq)
5284   (define-key gnus-summary-help-map "d" 'gnus-summary-describe-group)
5285   (define-key gnus-summary-help-map "h" 'gnus-summary-describe-briefly)
5286   (define-key gnus-summary-help-map "i" 'gnus-info-find-node)
5287
5288
5289   (define-prefix-command 'gnus-summary-backend-map)
5290   (define-key gnus-summary-mode-map "B" 'gnus-summary-backend-map)
5291   (define-key gnus-summary-backend-map "e" 'gnus-summary-expire-articles)
5292   (define-key gnus-summary-backend-map "\M-\C-e" 
5293     'gnus-summary-expire-articles-now)
5294   (define-key gnus-summary-backend-map "\177" 'gnus-summary-delete-article)
5295   (define-key gnus-summary-backend-map "m" 'gnus-summary-move-article)
5296   (define-key gnus-summary-backend-map "r" 'gnus-summary-respool-article)
5297   (define-key gnus-summary-backend-map "w" 'gnus-summary-edit-article)
5298   (define-key gnus-summary-backend-map "c" 'gnus-summary-copy-article)
5299   (define-key gnus-summary-backend-map "q" 'gnus-summary-fancy-query)
5300   (define-key gnus-summary-backend-map "i" 'gnus-summary-import-article)
5301
5302
5303   (define-prefix-command 'gnus-summary-save-map)
5304   (define-key gnus-summary-mode-map "O" 'gnus-summary-save-map)
5305   (define-key gnus-summary-save-map "o" 'gnus-summary-save-article)
5306   (define-key gnus-summary-save-map "m" 'gnus-summary-save-article-mail)
5307   (define-key gnus-summary-save-map "r" 'gnus-summary-save-article-rmail)
5308   (define-key gnus-summary-save-map "f" 'gnus-summary-save-article-file)
5309   (define-key gnus-summary-save-map "h" 'gnus-summary-save-article-folder)
5310   (define-key gnus-summary-save-map "v" 'gnus-summary-save-article-vm)
5311   (define-key gnus-summary-save-map "p" 'gnus-summary-pipe-output)
5312 ;  (define-key gnus-summary-save-map "s" 'gnus-soup-add-article)
5313
5314   (define-key gnus-summary-mode-map "X" 'gnus-uu-extract-map)
5315   
5316   (define-prefix-command 'gnus-summary-various-map)
5317   (define-key gnus-summary-mode-map "V" 'gnus-summary-various-map)
5318   (define-key gnus-summary-various-map "u" 'gnus-summary-universal-argument)
5319   (define-key gnus-summary-various-map "\C-s" 'gnus-summary-search-article-forward)
5320   (define-key gnus-summary-various-map "\C-r" 'gnus-summary-search-article-backward)
5321   (define-key gnus-summary-various-map "r" 'gnus-summary-refer-article)
5322   (define-key gnus-summary-various-map "&" 'gnus-summary-execute-command)
5323   (define-key gnus-summary-various-map "T" 'gnus-summary-toggle-truncation)
5324   (define-key gnus-summary-various-map "e" 'gnus-summary-expand-window)
5325   (define-key gnus-summary-various-map "D" 'gnus-summary-enter-digest-group)
5326   (define-key gnus-summary-various-map "k" 'gnus-summary-edit-local-kill)
5327   (define-key gnus-summary-various-map "K" 'gnus-summary-edit-global-kill)
5328
5329   (define-key gnus-summary-various-map "S" 'gnus-summary-score-map)
5330
5331   (define-prefix-command 'gnus-summary-sort-map)
5332   (define-key gnus-summary-various-map "s" 'gnus-summary-sort-map)
5333   (define-key gnus-summary-sort-map "n" 'gnus-summary-sort-by-number)
5334   (define-key gnus-summary-sort-map "a" 'gnus-summary-sort-by-author)
5335   (define-key gnus-summary-sort-map "s" 'gnus-summary-sort-by-subject)
5336   (define-key gnus-summary-sort-map "d" 'gnus-summary-sort-by-date)
5337   (define-key gnus-summary-sort-map "i" 'gnus-summary-sort-by-score)
5338
5339   (define-key gnus-summary-mode-map "I" 'gnus-summary-increase-score)
5340   (define-key gnus-summary-mode-map "L" 'gnus-summary-lower-score)
5341   )
5342
5343
5344 \f
5345
5346 (defun gnus-summary-mode (&optional group)
5347   "Major mode for reading articles.
5348
5349 All normal editing commands are switched off.
5350 \\<gnus-summary-mode-map>
5351 Each line in this buffer represents one article.  To read an
5352 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
5353 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]', 
5354 respectively.
5355
5356 You can also post articles and send mail from this buffer.  To 
5357 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author 
5358 of an article, type `\\[gnus-summary-reply]'.
5359
5360 There are approx. one gazillion commands you can execute in this 
5361 buffer; read the info pages for more information (`\\[gnus-info-find-node]'). 
5362
5363 The following commands are available:
5364
5365 \\{gnus-summary-mode-map}"
5366   (interactive)
5367   (if gnus-visual (gnus-summary-make-menu-bar))
5368   (kill-all-local-variables)
5369   (let ((locals gnus-summary-local-variables))
5370     (while locals
5371       (if (consp (car locals))
5372           (progn
5373             (make-local-variable (car (car locals)))
5374             (set (car (car locals)) (eval (cdr (car locals)))))
5375         (make-local-variable (car locals))
5376         (set (car locals) nil))
5377       (setq locals (cdr locals))))
5378   (gnus-make-thread-indent-array)
5379   (gnus-update-format-specifications)
5380   (setq mode-line-modified "-- ")
5381   (make-local-variable 'mode-line-format)
5382   (setq mode-line-format (copy-sequence mode-line-format))
5383   (and (equal (nth 3 mode-line-format) "   ")
5384        (setcar (nthcdr 3 mode-line-format) ""))
5385   (setq major-mode 'gnus-summary-mode)
5386   (setq mode-name "Summary")
5387   (make-local-variable 'minor-mode-alist)
5388   (use-local-map gnus-summary-mode-map)
5389   (buffer-disable-undo (current-buffer))
5390   (setq buffer-read-only t)             ;Disable modification
5391   (setq truncate-lines t)
5392   (setq selective-display t)
5393   (setq selective-display-ellipses t)   ;Display `...'
5394   (setq buffer-display-table gnus-summary-display-table)
5395   (setq gnus-newsgroup-name group)
5396   (run-hooks 'gnus-summary-mode-hook))
5397
5398 (defun gnus-summary-make-display-table ()
5399   ;; Change the display table.  Odd characters have a tendency to mess
5400   ;; up nicely formatted displays - we make all possible glyphs
5401   ;; display only a single character.
5402
5403   ;; We start from the standard display table, if any.
5404   (setq gnus-summary-display-table 
5405         (or (copy-sequence standard-display-table)
5406             (make-display-table)))
5407   ;; Nix out all the control chars...
5408   (let ((i 32))
5409     (while (>= (setq i (1- i)) 0)
5410       (aset gnus-summary-display-table i [??])))
5411   ;; ... but not newline and cr, of course. (cr is necessary for the
5412   ;; selective display).  
5413   (aset gnus-summary-display-table ?\n nil)
5414   (aset gnus-summary-display-table ?\r nil)
5415   ;; We nix out any glyphs over 126 that are not set already.  
5416   (let ((i 256))
5417     (while (>= (setq i (1- i)) 127)
5418       ;; Only modify if the entry is nil.
5419       (or (aref gnus-summary-display-table i) 
5420           (aset gnus-summary-display-table i [??])))))
5421
5422 (defun gnus-summary-clear-local-variables ()
5423   (let ((locals gnus-summary-local-variables))
5424     (while locals
5425       (if (consp (car locals))
5426           (and (vectorp (car (car locals)))
5427                (set (car (car locals)) nil))
5428         (and (vectorp (car locals))
5429              (set (car locals) nil)))
5430       (setq locals (cdr locals)))))
5431
5432 (defun gnus-mouse-pick-article (e)
5433   (interactive "e")
5434   (mouse-set-point e)
5435   (gnus-summary-next-page nil t))
5436
5437 (defun gnus-summary-setup-buffer (group)
5438   "Initialize summary buffer."
5439   (let ((buffer (concat "*Summary " group "*")))
5440     (if (get-buffer buffer)
5441         (progn
5442           (set-buffer buffer)
5443           (not gnus-newsgroup-begin))
5444       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
5445       (setq gnus-summary-buffer (set-buffer (get-buffer-create buffer)))
5446       (gnus-add-current-to-buffer-list)
5447       (gnus-summary-mode group)
5448       (and gnus-carpal (gnus-carpal-setup-buffer 'summary))
5449       (setq gnus-newsgroup-name group)
5450       t)))
5451
5452 (defun gnus-set-global-variables ()
5453   ;; Set the global equivalents of the summary buffer-local variables
5454   ;; to the latest values they had. These reflect the summary buffer
5455   ;; that was in action when the last article was fetched.
5456   (if (eq major-mode 'gnus-summary-mode) 
5457       (progn
5458         (setq gnus-summary-buffer (current-buffer))
5459         (let ((name gnus-newsgroup-name)
5460               (marked gnus-newsgroup-marked)
5461               (unread gnus-newsgroup-unreads)
5462               (headers gnus-current-headers)
5463               (score-file gnus-current-score-file))
5464           (save-excursion
5465             (set-buffer gnus-group-buffer)
5466             (setq gnus-newsgroup-name name)
5467             (setq gnus-newsgroup-marked marked)
5468             (setq gnus-newsgroup-unreads unread)
5469             (setq gnus-current-headers headers)
5470             (setq gnus-current-score-file score-file))))))
5471
5472 (defun gnus-summary-insert-dummy-line (sformat subject number)
5473   (if (not sformat) 
5474       (setq sformat gnus-summary-dummy-line-format-spec))
5475   (let (b)
5476     (beginning-of-line)
5477     (setq b (point))
5478     (insert (eval sformat))
5479     (add-text-properties
5480      b (1+ b)
5481      (list 'gnus-number number 
5482            'gnus-mark gnus-dummy-mark
5483            'gnus-level 0))))
5484
5485 (defvar gnus-thread-indent-array nil)
5486 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
5487 (defun gnus-make-thread-indent-array ()
5488   (let ((n 200))
5489     (if (and gnus-thread-indent-array
5490              (= gnus-thread-indent-level gnus-thread-indent-array-level))
5491         nil
5492       (setq gnus-thread-indent-array (make-vector 201 "")
5493             gnus-thread-indent-array-level gnus-thread-indent-level)
5494       (while (>= n 0)
5495         (aset gnus-thread-indent-array n
5496               (make-string (* n gnus-thread-indent-level) ? ))
5497         (setq n (1- n))))))
5498
5499 (defun gnus-summary-insert-line 
5500   (sformat header level current unread replied expirable subject-or-nil
5501            &optional dummy score)
5502   (or sformat (setq sformat gnus-summary-line-format-spec))
5503   (let* ((indentation (aref gnus-thread-indent-array level))
5504          (lines (header-lines header))
5505          (score (or score gnus-summary-default-score 0))
5506          (score-char
5507           (if (or (null gnus-summary-default-score)
5508                   (<= (abs (- score gnus-summary-default-score))
5509                       gnus-summary-zcore-fuzz)) ? 
5510             (if (< score gnus-summary-default-score)
5511                 gnus-score-below-mark gnus-score-over-mark)))
5512          (replied (if replied gnus-replied-mark ? ))
5513          (from (header-from header))
5514          (name-address (funcall gnus-extract-address-components from))
5515          (address (car (cdr name-address)))
5516          (name (or (car name-address) (car (cdr name-address))))
5517          (subject (header-subject header))
5518          (number (header-number header))
5519          (opening-bracket (if dummy ?\< ?\[))
5520          (closing-bracket (if dummy ?\> ?\]))
5521          (buffer-read-only nil)
5522          (b (progn (beginning-of-line) (point))))
5523     (or (numberp lines) (setq lines 0))
5524     (insert (eval sformat))
5525     (add-text-properties
5526      b (1+ b) (list 'gnus-number number 
5527                     'gnus-mark (or unread gnus-unread-mark)
5528                     'gnus-level level))))
5529
5530 (defun gnus-summary-update-line (&optional dont-update)
5531   ;; Update summary line after change.
5532   (or (not gnus-summary-default-score)
5533       gnus-summary-inhibit-highlight
5534       (let ((gnus-summary-inhibit-highlight t)
5535             (article (gnus-summary-article-number)))
5536         (progn
5537           (or dont-update
5538               (if (and gnus-summary-mark-below
5539                        (< (gnus-summary-article-score)
5540                           gnus-summary-mark-below))
5541                   (and (not (memq article gnus-newsgroup-marked))
5542                        (not (memq article gnus-newsgroup-dormant))
5543                        (memq article gnus-newsgroup-unreads)
5544                        (gnus-summary-mark-article nil gnus-low-score-mark))
5545                 (and (eq (gnus-summary-article-mark) gnus-low-score-mark)
5546                      (gnus-summary-mark-article nil gnus-unread-mark))))
5547           (and gnus-visual
5548                (run-hooks 'gnus-summary-update-hook))))))
5549
5550 (defun gnus-summary-update-lines (&optional beg end)
5551   ;; Rehighlight summary buffer according to `gnus-summary-highlight'.
5552   (let ((beg (or beg (point-min)))
5553         (end (or end (point-max))))
5554     (save-excursion
5555       (set-buffer gnus-summary-buffer)
5556       (goto-char beg)
5557       (while (and (not (eobp)) (< (point) end))
5558         (gnus-summary-update-line)
5559         (forward-line 1)))))
5560
5561 (defun gnus-summary-number-of-articles-in-thread (thread &optional char)
5562   ;; Sum up all elements (and sub-elements) in a list.
5563   (let ((number 
5564          (if (listp thread) 
5565              (apply 
5566               '+ (mapcar 'gnus-summary-number-of-articles-in-thread thread))
5567            1)))
5568     (if char 
5569         (if (> number 1) gnus-not-empty-thread-mark
5570           gnus-empty-thread-mark)
5571       number)))
5572
5573 (defun gnus-summary-read-group (group &optional show-all no-article kill-buffer)
5574   "Start reading news in newsgroup GROUP.
5575 If SHOW-ALL is non-nil, already read articles are also listed.
5576 If NO-ARTICLE is non-nil, no article is selected initially."
5577   (gnus-message 5 "Retrieving newsgroup: %s..." group)
5578   (let* ((new-group (gnus-summary-setup-buffer group))
5579          (quit-config (nth 1 (assoc 'quit-config (gnus-find-method-for-group
5580                                                   group))))
5581          (did-select (and new-group (gnus-select-newsgroup group show-all))))
5582     (cond 
5583      ((not new-group)
5584       (gnus-set-global-variables)
5585       (gnus-kill-buffer kill-buffer)
5586       (gnus-configure-windows 'summary)
5587       (gnus-set-mode-line 'summary)
5588       (gnus-summary-position-cursor)
5589       (message "")
5590       t)
5591      ((null did-select) 
5592       (and (eq major-mode 'gnus-summary-mode)
5593            (not (equal (current-buffer) kill-buffer))
5594            (progn
5595              (kill-buffer (current-buffer))
5596              (if (not quit-config)
5597                  (progn
5598                    (set-buffer gnus-group-buffer)
5599                    (gnus-group-jump-to-group group)
5600                    (gnus-group-next-unread-group 1))
5601                (if (not (buffer-name (car quit-config)))
5602                    (gnus-configure-windows 'group)
5603                  (set-buffer (car quit-config))
5604                  (and (eq major-mode 'gnus-summary-mode)
5605                       (gnus-set-global-variables))
5606                  (gnus-configure-windows (cdr quit-config))))))
5607       (message "Can't select group")
5608       nil)
5609      ((eq did-select 'quit)
5610       (and (eq major-mode 'gnus-summary-mode)
5611            (not (equal (current-buffer) kill-buffer))
5612            (kill-buffer (current-buffer)))
5613       (gnus-kill-buffer kill-buffer)
5614       (if (not quit-config)
5615           (progn
5616             (set-buffer gnus-group-buffer)
5617             (gnus-group-jump-to-group group)
5618             (gnus-group-next-unread-group 1))
5619         (if (not (buffer-name (car quit-config)))
5620             (gnus-configure-windows 'group)
5621           (set-buffer (car quit-config))
5622           (and (eq major-mode 'gnus-summary-mode)
5623                (gnus-set-global-variables))
5624           (gnus-configure-windows (cdr quit-config))))
5625       (signal 'quit nil))
5626      (t
5627       (gnus-set-global-variables)
5628       ;; Save the active value in effect when the group was entered.
5629       (setq gnus-newsgroup-active 
5630             (gnus-copy-sequence
5631              (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
5632       ;; You can change the subjects in this hook.
5633       (run-hooks 'gnus-select-group-hook)
5634       ;; Do score processing.
5635       (and gnus-use-scoring (gnus-possibly-score-headers))
5636       ;; Update the format specifiers.
5637       (gnus-update-format-specifications)
5638       ;; Generate the summary buffer.
5639       (gnus-summary-prepare)
5640       (if (zerop (buffer-size))
5641           (cond (gnus-newsgroup-dormant
5642                  (gnus-summary-show-all-dormant))
5643                 ((and gnus-newsgroup-scored show-all)
5644                  (gnus-summary-show-all-expunged))))
5645       ;; Function `gnus-apply-kill-file' must be called in this hook.
5646       (run-hooks 'gnus-apply-kill-hook)
5647       (if (zerop (buffer-size))
5648           (progn
5649             ;; This newsgroup is empty.
5650             (gnus-summary-catchup-and-exit nil t) ;Without confirmations.
5651             (gnus-message 6 "No unread news")
5652             (gnus-kill-buffer kill-buffer)
5653             nil)
5654         ;;(save-excursion
5655         ;;  (if kill-buffer
5656         ;;      (let ((gnus-summary-buffer kill-buffer))
5657         ;;      (gnus-configure-windows 'group))))
5658         ;; Hide conversation thread subtrees.  We cannot do this in
5659         ;; gnus-summary-prepare-hook since kill processing may not
5660         ;; work with hidden articles.
5661         (and gnus-show-threads
5662              gnus-thread-hide-subtree
5663              (gnus-summary-hide-all-threads))
5664         ;; Show first unread article if requested.
5665         (goto-char (point-min))
5666         (if (and (not no-article)
5667                  gnus-auto-select-first
5668                  (gnus-summary-first-unread-article))
5669             ()
5670           (gnus-configure-windows 'summary))
5671         (gnus-set-mode-line 'summary)
5672         (gnus-summary-position-cursor)
5673         ;; If in async mode, we send some info to the backend.
5674         (and gnus-newsgroup-async
5675              (setq gnus-newsgroup-threads (nreverse gnus-newsgroup-threads))
5676              (gnus-request-asynchronous 
5677               gnus-newsgroup-name
5678               (if (and gnus-asynchronous-article-function
5679                        (fboundp gnus-asynchronous-article-function))
5680                   (funcall gnus-asynchronous-article-function
5681                            gnus-newsgroup-threads)
5682                 gnus-newsgroup-threads)))
5683         (gnus-kill-buffer kill-buffer)
5684         (if (not (get-buffer-window gnus-group-buffer))
5685             ()
5686           ;; gotta use windows, because recenter does wierd stuff if
5687           ;; the current buffer ain't the displayed window.
5688           (let ((owin (selected-window))) 
5689             (select-window (get-buffer-window gnus-group-buffer))
5690             (and (gnus-group-goto-group group)
5691                  (recenter))
5692             (select-window owin))))
5693       t))))
5694
5695 (defun gnus-summary-prepare ()
5696   ;; Generate the summary buffer.
5697   (let ((buffer-read-only nil))
5698     (erase-buffer)
5699     (gnus-summary-prepare-threads 
5700      (if gnus-show-threads
5701          (gnus-gather-threads 
5702           (gnus-sort-threads 
5703            (if (and gnus-summary-expunge-below
5704                     (not gnus-fetch-old-headers))
5705                (gnus-make-threads-and-expunge)
5706              (gnus-make-threads))))
5707        gnus-newsgroup-headers)
5708      0 nil nil t)
5709     ;; Erase header retrieval message.
5710     (gnus-summary-update-lines)
5711     (message "")
5712     ;; Remove the final newline.
5713     ;;(goto-char (point-max))
5714     ;;(delete-char -1)
5715     ;; Call hooks for modifying summary buffer.
5716     ;; Suggested by sven@tde.LTH.Se (Sven Mattisson).
5717     (goto-char (point-min))
5718     (run-hooks 'gnus-summary-prepare-hook)))
5719
5720 (defun gnus-gather-threads (threads)
5721   "Gather threads that have lost their roots."
5722   (if (not gnus-summary-make-false-root)
5723       threads 
5724     (let ((hashtb (gnus-make-hashtable 1023))
5725           (prev threads)
5726           (result threads)
5727           subject hthread whole-subject)
5728       (while threads
5729         (setq whole-subject 
5730               (setq subject (header-subject (car (car threads)))))
5731         (if gnus-summary-gather-subject-limit
5732             (or (and (numberp gnus-summary-gather-subject-limit)
5733                      (> (length subject) gnus-summary-gather-subject-limit)
5734                      (setq subject
5735                            (substring subject 0 
5736                                       gnus-summary-gather-subject-limit)))
5737                 (and (eq 'fuzzy gnus-summary-gather-subject-limit)
5738                      (setq subject (gnus-simplify-subject-fuzzy subject))))
5739           (setq subject (gnus-simplify-subject-re subject)))
5740         (if (setq hthread 
5741                   (gnus-gethash subject hashtb))
5742             (progn
5743               (or (stringp (car (car hthread)))
5744                   (setcar hthread (list whole-subject (car hthread))))
5745               (setcdr (car hthread) (nconc (cdr (car hthread)) 
5746                                            (list (car threads))))
5747               (setcdr prev (cdr threads))
5748               (setq threads prev))
5749           (gnus-sethash subject threads hashtb))
5750         (setq prev threads)
5751         (setq threads (cdr threads)))
5752       result)))
5753
5754 (defun gnus-make-threads ()
5755   ;; This function takes the dependencies already made by 
5756   ;; `gnus-get-newsgroup-headers' and builds the trees. First we go
5757   ;; through the dependecies in the hash table and finds all the
5758   ;; roots. Roots do not refer back to any valid articles.
5759   (let (roots)
5760     (and gnus-fetch-old-headers (eq gnus-headers-retrieved-by 'nov)
5761          (gnus-build-old-threads))
5762     (mapatoms
5763      (lambda (refs)
5764        (if (not (car (symbol-value refs)))
5765            (setq roots (append (cdr (symbol-value refs)) roots))
5766          ;; Ok, these refer back to valid articles, but if
5767          ;; `gnus-thread-ignore-subject' is nil, we have to check that
5768          ;; the root has the same subject as its children. The children
5769          ;; that do not are made into roots and removed from the list
5770          ;; of children. 
5771          (or gnus-thread-ignore-subject
5772              (let* ((prev (symbol-value refs))
5773                     (subject (gnus-simplify-subject-re 
5774                               (header-subject (car prev))))
5775                     (headers (cdr prev)))
5776                (while headers
5777                  (if (not (string= subject
5778                                    (gnus-simplify-subject-re 
5779                                     (header-subject (car headers)))))
5780                      (progn
5781                        (setq roots (cons (car headers) roots))
5782                        (setcdr prev (cdr headers)))
5783                    (setq prev headers))
5784                  (setq headers (cdr headers)))))))
5785      gnus-newsgroup-dependencies)
5786     
5787     (mapcar 'gnus-trim-thread
5788             (apply 'append
5789                    (mapcar 'gnus-cut-thread
5790                            (mapcar 'gnus-make-sub-thread roots))))))
5791   
5792 (defun gnus-make-threads-and-expunge ()
5793   ;; This function takes the dependencies already made by 
5794   ;; `gnus-get-newsgroup-headers' and builds the trees. First we go
5795   ;; through the dependecies in the hash table and finds all the
5796   ;; roots. Roots do not refer back to any valid articles.
5797   (let ((default (or gnus-summary-default-score 0))
5798         (below gnus-summary-expunge-below)
5799         roots article)
5800     (and gnus-fetch-old-headers (eq gnus-headers-retrieved-by 'nov)
5801          (gnus-build-old-threads))
5802     (mapatoms
5803      (lambda (refs)
5804        (if (not (car (symbol-value refs)))
5805            ;; These articles do not refer back to any other articles -
5806            ;; they are roots.
5807            (let ((headers (cdr (symbol-value refs))))
5808              ;; We weed out the low-scored articles.
5809              (while headers
5810                (if (not (< (or (cdr (assq (header-number (car headers))
5811                                           gnus-newsgroup-scored)) default)
5812                            below))
5813                    ;; It is over.
5814                    (setq roots (cons (car headers) roots))
5815                  ;; It is below, so we mark it as read.
5816                  (setq gnus-newsgroup-unreads
5817                        (delq (header-number (car headers))
5818                              gnus-newsgroup-unreads)))
5819                (setq headers (cdr headers))))
5820          ;; Ok, these refer back to valid articles, but if
5821          ;; `gnus-thread-ignore-subject' is nil, we have to check that
5822          ;; the root has the same subject as its children. The children
5823          ;; that do not are made into roots and removed from the list
5824          ;; of children. 
5825          (or gnus-thread-ignore-subject
5826              (let* ((prev (symbol-value refs))
5827                     (subject (gnus-simplify-subject-re 
5828                               (header-subject (car prev))))
5829                     (headers (cdr prev)))
5830                (while headers
5831                  (if (not (string= subject
5832                                    (gnus-simplify-subject-re 
5833                                     (header-subject (car headers)))))
5834                      (progn
5835                        (if (not (< (or (cdr (assq (header-number (car headers))
5836                                                   gnus-newsgroup-scored))
5837                                        default) below))
5838                            (setq roots (cons (car headers) roots))
5839                          (setq gnus-newsgroup-unreads
5840                                (delq (header-number (car headers))
5841                                      gnus-newsgroup-unreads)))
5842                        (setcdr prev (cdr headers)))
5843                    (setq prev headers))
5844                  (setq headers (cdr headers)))))
5845          ;; If this article is expunged, some of the children might be
5846          ;; roots.  
5847          (if (< (or (cdr (assq (header-number (car (symbol-value refs)))
5848                                gnus-newsgroup-scored)) default)
5849                 below)
5850              (let* ((prev (symbol-value refs))
5851                     (headers (cdr prev)))
5852                (while headers
5853                  (setq article (header-number (car headers)))
5854                  (if (not (< (or (cdr (assq article gnus-newsgroup-scored))
5855                                  default) below))
5856                      (progn (setq roots (cons (car headers) roots))
5857                             (setq prev headers))
5858                    (setq gnus-newsgroup-unreads 
5859                          (delq article gnus-newsgroup-unreads))
5860                    (setcdr prev (cdr headers)))
5861                  (setq headers (cdr headers))))
5862            ;; It was not expunged, but we look at expunged children.
5863            (let* ((prev (symbol-value refs))
5864                   (headers (cdr prev))
5865                   article)
5866              (while headers
5867                (setq article (header-number (car headers)))
5868                (if (not (< (or (cdr (assq article gnus-newsgroup-scored))
5869                                default) below))
5870                    (setq prev headers)
5871                  (setq gnus-newsgroup-unreads 
5872                        (delq article gnus-newsgroup-unreads))
5873                  (setcdr prev (cdr headers)))
5874                (setq headers (cdr headers)))))))
5875      gnus-newsgroup-dependencies)
5876
5877     (mapcar 'gnus-trim-thread
5878             (apply 'append
5879                    (mapcar 'gnus-cut-thread
5880                            (mapcar 'gnus-make-sub-thread roots))))))
5881   
5882 (defun gnus-cut-thread (thread)
5883   ;; Remove leaf dormant or ancient articles from THREAD.
5884   (let ((head (car thread))
5885         (tail (apply 'append (mapcar 'gnus-cut-thread (cdr thread)))))
5886     (if (and (null tail)
5887              (let ((number (header-number head)))
5888                (or (memq number gnus-newsgroup-ancient)
5889                    (memq number gnus-newsgroup-dormant)
5890                    (and gnus-summary-expunge-below
5891                         (eq gnus-fetch-old-headers 'some)
5892                         (< (or (cdr (assq number gnus-newsgroup-scored))
5893                                gnus-summary-default-score 0)
5894                            gnus-summary-expunge-below)
5895                         (progn
5896                           (setq gnus-newsgroup-unreads
5897                                 (delq number gnus-newsgroup-unreads))
5898                           t)))))
5899         nil
5900       (list (cons head tail)))))
5901
5902 (defun gnus-trim-thread (thread)
5903   ;; Remove root ancient articles with only one child from THREAD.
5904   (if (and (eq gnus-fetch-old-headers 'some)
5905            (memq (header-number (car thread)) gnus-newsgroup-ancient)
5906            (= (length thread) 2))
5907       (gnus-trim-thread (nth 1 thread))
5908     thread))
5909
5910 (defun gnus-make-sub-thread (root)
5911   ;; This function makes a sub-tree for a node in the tree.
5912   (let ((children (reverse (cdr (gnus-gethash (downcase (header-id root))
5913                                               gnus-newsgroup-dependencies)))))
5914     (cons root (mapcar 'gnus-make-sub-thread children))))
5915
5916 (defun gnus-build-old-threads ()
5917   ;; Look at all the articles that refer back to old articles, and
5918   ;; fetch the headers for the articles that aren't there. This will
5919   ;; build complete threads - if the roots haven't been expired by the
5920   ;; server, that is.
5921   (let (id heads)
5922     (mapatoms
5923      (lambda (refs)
5924        (if (not (car (symbol-value refs)))
5925            (progn
5926              (setq heads (cdr (symbol-value refs)))
5927              (while heads
5928                (if (not (memq (header-number (car heads))
5929                               gnus-newsgroup-dormant))
5930                    (progn
5931                      (setq id (symbol-name refs))
5932                      (while (and (setq id (gnus-build-get-header id))
5933                                  (not (car (gnus-gethash 
5934                                             id gnus-newsgroup-dependencies)))))
5935                      (setq heads nil))
5936                  (setq heads (cdr heads)))))))
5937      gnus-newsgroup-dependencies)))
5938
5939 (defun gnus-build-get-header (id)
5940   ;; Look through the buffer of NOV lines and find the header to
5941   ;; ID. Enter this line into the dependencies hash table, and return
5942   ;; the id of the parent article (if any).
5943   (let ((deps gnus-newsgroup-dependencies)
5944         found header)
5945     (prog1
5946         (save-excursion
5947           (set-buffer nntp-server-buffer)
5948           (goto-char (point-min))
5949           (while (and (not found) (search-forward id nil t))
5950             (beginning-of-line)
5951             (setq found (looking-at 
5952                          (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
5953                                  (regexp-quote id))))
5954             (or found (beginning-of-line 2)))
5955           (if found
5956               (let (ref)
5957                 (beginning-of-line)
5958                 (and
5959                  (setq header (gnus-nov-parse-line 
5960                                (read (current-buffer)) deps))
5961                  (setq ref (header-references header))
5962                  (string-match "\\(<[^>]+>\\) *$" ref)
5963                  (substring ref (match-beginning 1) (match-end 1))))))
5964       (and header
5965            (setq gnus-newsgroup-headers (cons header gnus-newsgroup-headers)
5966                  gnus-newsgroup-ancient (cons (header-number header)
5967                                               gnus-newsgroup-ancient))))))
5968
5969 ;; Re-build the thread containing ID.
5970 (defun gnus-rebuild-thread (id)
5971   (let ((dep gnus-newsgroup-dependencies)
5972         (buffer-read-only nil)
5973         parent headers refs thread art)
5974     (while (and id (setq headers
5975                          (car (setq art (gnus-gethash (downcase id) dep)))))
5976       (setq parent art)
5977       (setq id (and (setq refs (header-references headers))
5978                     (string-match "\\(<[^>]+>\\) *$" refs)
5979                     (substring refs (match-beginning 1) (match-end 1)))))
5980     (setq thread (gnus-make-sub-thread (car parent)))
5981     (gnus-rebuild-remove-articles thread)
5982     (let ((beg (point)))
5983       (gnus-summary-prepare-threads (list thread) 0)
5984       (gnus-summary-update-lines beg (point)))))
5985
5986 ;; Delete all lines in the summary buffer that correspond to articles
5987 ;; in this thread.
5988 (defun gnus-rebuild-remove-articles (thread)
5989   (and (gnus-summary-goto-subject (header-number (car thread)))
5990        (gnus-delete-line))
5991   (mapcar (lambda (th) (gnus-rebuild-remove-articles th)) (cdr thread)))
5992
5993 (defun gnus-sort-threads (threads)
5994   ;; Sort threads as specified in `gnus-thread-sort-functions'.
5995   (let ((fun gnus-thread-sort-functions))
5996     (while fun
5997       (setq threads (sort threads (car fun))
5998             fun (cdr fun))))
5999   threads)
6000
6001 (defun gnus-thread-header (thread)
6002   ;; Return header of first article in THREAD.
6003   (if (consp thread)
6004       (if (stringp (car thread))
6005           (car (car (cdr thread)))
6006         (car thread))
6007     thread))
6008
6009 (defun gnus-thread-sort-by-number (h1 h2)
6010   "Sort threads by root article number."
6011   (let ((h1 (gnus-thread-header h1))
6012         (h2 (gnus-thread-header h2)))
6013     (< (header-number h1) (header-number h2))))
6014
6015 (defun gnus-thread-sort-by-author (h1 h2)
6016   "Sort threads by root author."
6017   (let ((h1 (gnus-thread-header h1))
6018         (h2 (gnus-thread-header h2)))
6019     (string-lessp
6020      (let ((extract (funcall 
6021                      gnus-extract-address-components (header-from h1))))
6022        (or (car extract) (cdr extract)))
6023      (let ((extract (funcall
6024                      gnus-extract-address-components (header-from h2))))
6025        (or (car extract) (cdr extract))))))
6026
6027 (defun gnus-thread-sort-by-subject (h1 h2)
6028   "Sort threads by root subject."
6029   (let ((h1 (gnus-thread-header h1))
6030         (h2 (gnus-thread-header h2)))
6031     (string-lessp
6032      (downcase (gnus-simplify-subject (header-subject h1)))
6033      (downcase (gnus-simplify-subject (header-subject h2))))))
6034
6035 (defun gnus-thread-sort-by-date (h1 h2)
6036   "Sort threads by root article date."
6037   (let ((h1 (gnus-thread-header h1))
6038         (h2 (gnus-thread-header h2)))
6039     (string-lessp
6040      (gnus-sortable-date (header-date h1))
6041      (gnus-sortable-date (header-date h2)))))
6042
6043 (defun gnus-thread-sort-by-score (h1 h2)
6044   "Sort threads by root article score.
6045 Unscored articles will be counted as having a score of zero."
6046   (let ((h1 (gnus-thread-header h1))
6047         (h2 (gnus-thread-header h2)))
6048     (let ((s1 (assq (header-number h1) gnus-newsgroup-scored))
6049           (s2 (assq (header-number h2) gnus-newsgroup-scored)))
6050       (> (or (cdr s1) gnus-summary-default-score 0)
6051          (or (cdr s2) gnus-summary-default-score 0)))))
6052
6053 (defun gnus-thread-sort-by-total-score (h1 h2)
6054   "Sort threads by the sum of all scores in the thread.
6055 Unscored articles will be counted as having a score of zero."
6056   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
6057
6058 (defun gnus-thread-total-score (thread)
6059   ;;  This function find the total score of THREAD.
6060   (if (consp thread)
6061       (if (stringp (car thread))
6062           (apply gnus-thread-score-function 0
6063                  (mapcar 'gnus-thread-total-score-1 (cdr thread)))
6064         (gnus-thread-total-score-1 thread))
6065     (gnus-thread-total-score-1 (list thread))))
6066
6067 (defun gnus-thread-total-score-1 (root)
6068   ;; This function find the total score of the thread below ROOT.
6069   (setq root (car root))
6070   (apply gnus-thread-score-function
6071          (or (cdr (assq (header-number root) gnus-newsgroup-scored))
6072              gnus-summary-default-score 0)
6073          (mapcar 'gnus-thread-total-score
6074                  (cdr (gnus-gethash (downcase (header-id root))
6075                                     gnus-newsgroup-dependencies)))))
6076
6077 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
6078 (defvar gnus-tmp-prev-subject "")
6079 (defvar gnus-tmp-adopt-thread nil)
6080
6081 ;; Basic ideas by Paul Dworkin <paul@media-lab.media.mit.edu>.
6082 (defun gnus-summary-prepare-threads 
6083   (threads level &optional not-child no-subject cull)
6084   "Prepare summary buffer from THREADS and indentation LEVEL.  
6085 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'  
6086 or a straight list of headers."
6087   (let (thread header number subject clevel)
6088     (while threads
6089       (setq thread (car threads)
6090             threads (cdr threads))
6091       ;; If `thread' is a cons, hierarchical threads are used.  If not,
6092       ;; `thread' is the header.
6093       (if (consp thread)
6094           (setq header (car thread))
6095         (setq header thread)
6096         (and cull
6097              (or (memq (setq number (header-number header))
6098                        gnus-newsgroup-dormant)
6099                  (and gnus-summary-expunge-below
6100                       (< (or (cdr (assq number gnus-newsgroup-scored))
6101                              gnus-summary-default-score 0)
6102                          gnus-summary-expunge-below)))
6103              (progn
6104                (setq header nil)
6105                (setq gnus-newsgroup-unreads 
6106                      (delq number gnus-newsgroup-unreads)))))
6107       (cond 
6108        ((stringp header)
6109         ;; The header is a dummy root.
6110         (cond ((eq gnus-summary-make-false-root 'adopt)
6111                ;; We let the first article adopt the rest.
6112                (let ((gnus-tmp-adopt-thread (list (cdr thread))))
6113                  (gnus-summary-prepare-threads (list (car (cdr thread))) 0))
6114                (setq thread (cdr (cdr thread)))
6115                (while thread
6116                  (gnus-summary-prepare-threads (list (car thread)) 1 t)
6117                  (setq thread (cdr thread))))
6118               ((eq gnus-summary-make-false-root 'dummy)
6119                ;; We output a dummy root.
6120                (gnus-summary-insert-dummy-line 
6121                 nil header (header-number (car (car (cdr thread)))))
6122                (setq clevel 1))
6123               ((eq gnus-summary-make-false-root 'empty)
6124                ;; We print the articles with empty subject fields. 
6125                (let ((gnus-tmp-adopt-thread (list (cdr thread))))
6126                  (gnus-summary-prepare-threads (list (car (cdr thread))) 0))
6127                (setq thread (cdr (cdr thread)))
6128                (while thread
6129                  (gnus-summary-prepare-threads 
6130                   (list (car thread)) 0 nil
6131                   (not (and (eq gnus-summary-gather-subject-limit 'fuzzy)
6132                             (not (string=  
6133                                   (gnus-simplify-subject-re 
6134                                    (header-subject (car (car thread))))
6135                                   (gnus-simplify-subject-re header))))))
6136                  (setq thread (cdr thread))))
6137               (t
6138                ;; We do not make a root for the gathered
6139                ;; sub-threads at all.  
6140                (setq clevel 0)))
6141         ;; Print the sub-threads.
6142         (and (consp thread) (cdr thread)
6143              (gnus-summary-prepare-threads (cdr thread) clevel)))
6144        ;; The header is a real article.
6145        (header
6146         (setq number (header-number header)
6147               subject (header-subject header))
6148         (and gnus-newsgroup-async
6149              (setq gnus-newsgroup-threads
6150                    (cons (cons (header-number header)
6151                                (header-lines header)) gnus-newsgroup-threads)))
6152         (gnus-summary-insert-line
6153          nil header level nil 
6154          (cond ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
6155                ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
6156                ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
6157                ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
6158                (t gnus-ancient-mark))
6159          (memq number gnus-newsgroup-replied)
6160          (memq number gnus-newsgroup-expirable)
6161          (if no-subject 
6162              gnus-summary-same-subject
6163            (if (or (zerop level)
6164                    (and gnus-thread-ignore-subject
6165                         (not (string= 
6166                               (gnus-simplify-subject-re gnus-tmp-prev-subject)
6167                               (gnus-simplify-subject-re subject)))))
6168                subject
6169              gnus-summary-same-subject))
6170          not-child
6171          (cdr (assq number gnus-newsgroup-scored)))
6172         (setq gnus-tmp-prev-subject subject)
6173         ;; Recursively print subthreads.
6174         (and (consp thread) (cdr thread)
6175              (gnus-summary-prepare-threads (cdr thread) (1+ level))))))))
6176
6177 (defun gnus-select-newsgroup (group &optional read-all)
6178   "Select newsgroup GROUP.
6179 If READ-ALL is non-nil, all articles in the group are selected."
6180   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
6181          (info (nth 2 entry))
6182          articles)
6183     (gnus-check-news-server
6184      (setq gnus-current-select-method (gnus-find-method-for-group group)))
6185
6186     (or (gnus-server-opened gnus-current-select-method)
6187         (gnus-open-server gnus-current-select-method)
6188         (error "Couldn't open server"))
6189     
6190     (or (and (eq (car entry) t)
6191              (gnus-activate-newsgroup (car info)))
6192         (gnus-request-group group t)
6193         (progn
6194           (kill-buffer (current-buffer))
6195           (error "Couldn't request group %s: %s" 
6196                  group (gnus-status-message group))))
6197
6198     (setq gnus-newsgroup-name group)
6199     (setq gnus-newsgroup-unselected nil)
6200     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
6201
6202     (and gnus-asynchronous
6203          (gnus-check-backend-function 
6204           'request-asynchronous gnus-newsgroup-name)
6205          (setq gnus-newsgroup-async
6206                (gnus-request-asynchronous gnus-newsgroup-name)))
6207
6208     (setq articles (gnus-articles-to-read group read-all))
6209
6210     (cond 
6211      ((null articles) 
6212       (gnus-message 3 "Couldn't select newsgroup")
6213       'quit)
6214      ((eq articles 0) nil)
6215      (t
6216       ;; Init the dependencies hash table.
6217       (setq gnus-newsgroup-dependencies 
6218             (gnus-make-hashtable (length articles)))
6219       ;; Retrieve the headers and read them in.
6220       (setq gnus-newsgroup-headers 
6221             (if (eq 'nov (setq gnus-headers-retrieved-by
6222                                (gnus-retrieve-headers 
6223                                 (if (and gnus-fetch-old-headers 
6224                                          (not (eq 1 (car articles))))
6225                                     (cons 1 articles)
6226                                   articles)
6227                                 gnus-newsgroup-name)))
6228                 (progn
6229                   (gnus-get-newsgroup-headers-xover articles))
6230               ;; If we were to fetch old headers, but the backend didn't
6231               ;; support XOVER, then it is possible we fetched one article
6232               ;; that we shouldn't have. If that's the case, we pop it off the
6233               ;; list of headers.
6234               (if (not gnus-fetch-old-headers)
6235                   ()
6236                 (save-excursion
6237                   (set-buffer nntp-server-buffer)
6238                   (goto-char (point-min))
6239                   (and (looking-at "[0-9]+[ \t]+1[ \t]")
6240                        (delete-region 
6241                         (point) 
6242                         (search-forward "\n.\n" nil t)))))
6243               (gnus-get-newsgroup-headers)))
6244       ;; Remove canceled articles from the list of unread articles.
6245       (setq gnus-newsgroup-unreads
6246             (gnus-set-sorted-intersection 
6247              gnus-newsgroup-unreads
6248              (mapcar (lambda (headers) (header-number headers))
6249                      gnus-newsgroup-headers)))
6250       ;; Adjust and set lists of article marks.
6251       (and info
6252            (let (marked)
6253              (gnus-adjust-marked-articles info)
6254              (setq gnus-newsgroup-marked 
6255                    (cdr (assq 'tick (setq marked (nth 3 info)))))
6256              (setq gnus-newsgroup-replied (cdr (assq 'reply marked)))
6257              (setq gnus-newsgroup-expirable (cdr (assq 'expire marked)))
6258              (setq gnus-newsgroup-killed (cdr (assq 'killed marked)))
6259              (setq gnus-newsgroup-bookmarks (cdr (assq 'bookmark marked)))
6260              (setq gnus-newsgroup-dormant (cdr (assq 'dormant marked)))
6261              (setq gnus-newsgroup-scored (cdr (assq 'score marked)))
6262              (setq gnus-newsgroup-processable nil)))
6263       ;; Check whether auto-expire is to be done in this group.
6264       (setq gnus-newsgroup-auto-expire
6265             (or (and (stringp gnus-auto-expirable-newsgroups)
6266                      (string-match gnus-auto-expirable-newsgroups group))
6267                 (memq 'auto-expire (nth 5 info))))
6268       ;; First and last article in this newsgroup.
6269       (and gnus-newsgroup-headers
6270            (setq gnus-newsgroup-begin 
6271                  (header-number (car gnus-newsgroup-headers)))
6272            (setq gnus-newsgroup-end
6273                  (header-number (gnus-last-element gnus-newsgroup-headers))))
6274       (setq gnus-reffed-article-number -1)
6275       ;; GROUP is successfully selected.
6276       (or gnus-newsgroup-headers t)))))
6277
6278 (defun gnus-articles-to-read (group read-all)
6279   ;; Find out what articles the user wants to read.
6280   (let* ((articles
6281           ;; Select all articles if `read-all' is non-nil, or if all the
6282           ;; unread articles are dormant articles.
6283           (if (or (and read-all (not (numberp read-all)))
6284                   (= (length gnus-newsgroup-unreads) 
6285                      (length gnus-newsgroup-dormant)))
6286               (gnus-uncompress-range 
6287                (gnus-gethash group gnus-active-hashtb))
6288             gnus-newsgroup-unreads))
6289          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
6290          (scored (length scored-list))
6291          (number (length articles))
6292          (marked (+ (length gnus-newsgroup-marked)
6293                     (length gnus-newsgroup-dormant)))
6294          (select
6295           (cond 
6296            ((numberp read-all)
6297             read-all)
6298            (t
6299             (condition-case ()
6300                 (cond ((and (or (<= scored marked)
6301                                 (= scored number))
6302                             (numberp gnus-large-newsgroup)
6303                             (> number gnus-large-newsgroup))
6304                        (let ((input
6305                               (read-string
6306                                (format
6307                                 "How many articles from %s (default %d): "
6308                                 gnus-newsgroup-name number))))
6309                          (if (string-match "^[ \t]*$" input)
6310                              number input)))
6311                       ((and (> scored marked) (< scored number))
6312                        (let ((input
6313                               (read-string
6314                                (format 
6315                                 "%s %s (%d scored, %d total): "
6316                                 "How many articles from"
6317                                 group scored number))))
6318                          (if (string-match "^[ \t]*$" input)
6319                              number input)))
6320                       (t number))
6321               (quit nil))))))
6322     (setq select (if (stringp select) (string-to-number select) select))
6323     (if (or (null select) (zerop select))
6324         select
6325       (if (and (not (zerop scored)) (<= (abs select) scored))
6326           (progn
6327             (setq articles (sort scored-list '<))
6328             (setq number (length articles)))
6329         (setq articles (copy-sequence articles)))
6330
6331       (if (< (abs select) number)
6332           (if (< select 0) 
6333               ;; Select the N oldest articles.
6334               (setcdr (nthcdr (1- (abs select)) articles) nil)
6335             ;; Select the N most recent articles.
6336             (setq articles (nthcdr (- number select) articles))))
6337       (setq gnus-newsgroup-unselected
6338             (gnus-sorted-intersection
6339              gnus-newsgroup-unreads
6340              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
6341       articles)))
6342
6343 (defun gnus-killed-articles (killed articles)
6344   (let (out)
6345     (while articles
6346       (if (inline (gnus-member-of-range (car articles) killed))
6347           (setq out (cons (car articles) out)))
6348       (setq articles (cdr articles)))
6349     out))
6350
6351 (defun gnus-adjust-marked-articles (info &optional active)
6352   "Remove all marked articles that are no longer legal."
6353   (let ((marked-lists (nth 3 info))
6354         (active (or active (gnus-gethash (car info) gnus-active-hashtb)))
6355         m prev)
6356     ;; There are many types of marked articles.
6357     (while marked-lists
6358       (setq m (cdr (setq prev (car marked-lists))))
6359       (cond ((or (eq 'tick (car prev)) (eq 'dormant (car prev)))
6360              ;; Make sure that all ticked articles are a subset of the
6361              ;; unread/unselected articles.
6362              (while m
6363                (if (or (memq (car m) gnus-newsgroup-unreads)
6364                        (memq (car m) gnus-newsgroup-unselected))
6365                    (setq prev m)
6366                  (setcdr prev (cdr m)))
6367                (setq m (cdr m))))
6368             ((eq 'score (car prev))
6369              ;; Scored articles should be a subset of
6370              ;; unread/unselected articles. 
6371              (while m
6372                (if (or (memq (car (car m)) gnus-newsgroup-unreads)
6373                        (memq (car (car m)) gnus-newsgroup-unreads))
6374                    (setq prev m)
6375                  (setcdr prev (cdr m)))
6376                (setq m (cdr m))))
6377             ((eq 'bookmark (car prev))
6378              ;; Bookmarks should be a subset of active articles.
6379              (while m
6380                (if (< (car (car m)) (car active))
6381                    (setcdr prev (cdr m))
6382                  (setq prev m))
6383                (setq m (cdr m))))
6384             ((eq 'killed (car prev))
6385              ;; Articles that have been through the kill process are
6386              ;; to be a subset of active articles.
6387              (while (and m (< (or (and (numberp (car m)) (car m))
6388                                   (cdr (car m)))
6389                               (car active)))
6390                (setcdr prev (cdr m))
6391                (setq m (cdr m)))
6392              (if (and m (< (or (and (numberp (car m)) (car m))
6393                                (car (car m)))
6394                            (car active))) 
6395                  (setcar (if (numberp (car m)) m (car m)) (car active))))
6396             ((or (eq 'reply (car prev)) (eq 'expire (car prev)))
6397              ;; The replied and expirable articles have to be articles
6398              ;; that are active. 
6399              (while m
6400                (if (< (car m) (car active))
6401                    (setcdr prev (cdr m))
6402                  (setq prev m))
6403                (setq m (cdr m)))))
6404       (setq marked-lists (cdr marked-lists)))
6405     ;; Remove all lists that are empty.
6406     (setq marked-lists (nth 3 info))
6407     (if marked-lists
6408         (progn
6409           (while (= 1 (length (car marked-lists)))
6410             (setq marked-lists (cdr marked-lists)))
6411           (setq m (cdr (setq prev marked-lists)))
6412           (while m
6413             (if (= 1 (length (car m)))
6414                 (setcdr prev (cdr m))
6415               (setq prev m))
6416             (setq m (cdr m)))
6417           (setcar (nthcdr 3 info) marked-lists)))
6418     ;; Finally, if there are no marked lists at all left, and if there
6419     ;; are no elements after the lists in the info list, we just chop
6420     ;; the info list off before the marked lists.
6421     (and (null marked-lists) 
6422          (not (nthcdr 4 info))
6423          (setcdr (nthcdr 2 info) nil)))
6424   info)
6425
6426 (defun gnus-set-marked-articles 
6427   (info ticked replied expirable killed dormant bookmark score) 
6428   "Enter the various lists of marked articles into the newsgroup info list."
6429   (let (newmarked)
6430     (and ticked (setq newmarked (cons (cons 'tick ticked) nil)))
6431     (and replied (setq newmarked (cons (cons 'reply replied) newmarked)))
6432     (and expirable (setq newmarked (cons (cons 'expire expirable) 
6433                                          newmarked)))
6434     (and killed (setq newmarked (cons (cons 'killed killed) newmarked)))
6435     (and dormant (setq newmarked (cons (cons 'dormant dormant) newmarked)))
6436     (and bookmark (setq newmarked (cons (cons 'bookmark bookmark) 
6437                                         newmarked)))
6438     (and score (setq newmarked (cons (cons 'score score) newmarked)))
6439     (if (nthcdr 3 info)
6440         (progn
6441           (setcar (nthcdr 3 info) newmarked)
6442           (and (not newmarked)
6443                (not (nthcdr 4 info))
6444                (setcdr (nthcdr 2 info) nil)))
6445       (if newmarked
6446           (setcdr (nthcdr 2 info) (list newmarked))))))
6447
6448 (defun gnus-add-marked-articles (group type articles &optional info force)
6449   ;; Add ARTICLES of TYPE to the info of GROUP.
6450   ;; If INFO is non-nil, use that info. If FORCE is non-nil, don't
6451   ;; add, but replace marked articles of TYPE with ARTICLES.
6452   (let ((info (or info (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
6453         marked m)
6454     (or (not info)
6455         (and (not (setq marked (nthcdr 3 info)))
6456              (setcdr (nthcdr 2 info) (list (list (cons type articles)))))
6457         (and (not (setq m (assq type (car marked))))
6458              (setcar marked (cons (cons type articles) (car marked))))
6459         (if force
6460             (setcdr m articles)
6461           (nconc m articles)))))
6462          
6463 (defun gnus-set-mode-line (where)
6464   "This function sets the mode line of the article or summary buffers.
6465 If WHERE is `summary', the summary mode line format will be used."
6466   (if (memq where gnus-updated-mode-lines)
6467       (let (mode-string)
6468         (save-excursion
6469           (set-buffer gnus-summary-buffer)
6470           (let* ((mformat (if (eq where 'article) 
6471                               gnus-article-mode-line-format-spec
6472                             gnus-summary-mode-line-format-spec))
6473                  (group-name gnus-newsgroup-name)
6474                  (article-number (or gnus-current-article 0))
6475                  (unread (- (length gnus-newsgroup-unreads)
6476                             (length gnus-newsgroup-dormant)))
6477                  (unread-and-unticked 
6478                   (- unread (length gnus-newsgroup-marked)))
6479                  (unselected (length gnus-newsgroup-unselected))
6480                  (unread-and-unselected
6481                   (cond ((and (zerop unread-and-unticked)
6482                               (zerop unselected)) "")
6483                         ((zerop unselected) 
6484                          (format "{%d more}" unread-and-unticked))
6485                         (t (format "{%d(+%d) more}"
6486                                    unread-and-unticked unselected))))
6487                  (subject
6488                   (if gnus-current-headers
6489                       (header-subject gnus-current-headers) ""))
6490                  (max-len (and gnus-mode-non-string-length
6491                                (- (frame-width) gnus-mode-non-string-length)))
6492                  header) ;; passed as argument to any user-format-funcs
6493             (setq mode-string (eval mformat))
6494             (or (numberp max-len)
6495                 (setq max-len (length mode-string)))
6496             (if (< max-len 4) (setq max-len 4))
6497             (if (> (length mode-string) max-len)
6498                 ;; modified by MORIOKA Tomohiko <morioka@jaist.ac.jp>
6499                 ;;  function `substring' might cut on a middle
6500                 ;;  of multi-octet character.
6501                 (setq mode-string 
6502                       (concat (gnus-truncate-string mode-string (- max-len 3))
6503                               "...")))
6504             (setq mode-string (format (format "%%-%ds" max-len)
6505                                       mode-string))))
6506         (setq mode-line-buffer-identification mode-string)
6507         (set-buffer-modified-p t))))
6508
6509 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
6510   "Go through the HEADERS list and add all Xrefs to a hash table.
6511 The resulting hash table is returned, or nil if no Xrefs were found."
6512   (let* ((from-method (gnus-find-method-for-group from-newsgroup))
6513          (prefix (if (and 
6514                       (gnus-group-foreign-p from-newsgroup)
6515                       (not (memq 'virtual 
6516                                  (assoc (symbol-name (car from-method))
6517                                         gnus-valid-select-methods))))
6518                      (gnus-group-real-prefix from-newsgroup)))
6519          (xref-hashtb (make-vector 63 0))
6520          start group entry number xrefs header)
6521     (while headers
6522       (setq header (car headers))
6523       (if (and (setq xrefs (header-xref header))
6524                (not (memq (header-number header) unreads)))
6525           (progn
6526             (setq start 0)
6527             (while (string-match "\\([^ ]+\\):\\([0-9]+\\)" xrefs start)
6528               (setq start (match-end 0))
6529               (setq group (concat prefix (substring xrefs (match-beginning 1) 
6530                                                     (match-end 1))))
6531               (setq number 
6532                     (string-to-int (substring xrefs (match-beginning 2) 
6533                                               (match-end 2))))
6534               (if (setq entry (gnus-gethash group xref-hashtb))
6535                   (setcdr entry (cons number (cdr entry)))
6536                 (gnus-sethash group (cons number nil) xref-hashtb)))))
6537       (setq headers (cdr headers)))
6538     (if start xref-hashtb nil)))
6539
6540 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads expirable)
6541   "Look through all the headers and mark the Xrefs as read."
6542   (let ((virtual (memq 'virtual 
6543                        (assoc (symbol-name (car (gnus-find-method-for-group 
6544                                                  from-newsgroup)))
6545                               gnus-valid-select-methods)))
6546         name entry info xref-hashtb idlist method
6547         nth4)
6548     (save-excursion
6549       (set-buffer gnus-group-buffer)
6550       (if (setq xref-hashtb 
6551                 (gnus-create-xref-hashtb from-newsgroup headers unreads))
6552           (mapatoms 
6553            (lambda (group)
6554              (if (string= from-newsgroup (setq name (symbol-name group)))
6555                  ()
6556                (setq idlist (symbol-value group))
6557                ;; Dead groups are not updated.
6558                (if (and (prog1 
6559                             (setq entry (gnus-gethash name gnus-newsrc-hashtb)
6560                                   info (nth 2 entry))
6561                           (if (stringp (setq nth4 (nth 4 info)))
6562                               (setq nth4 (gnus-server-to-method nth4))))
6563                         ;; Only do the xrefs if the group has the same
6564                         ;; select method as the group we have just read.
6565                         (or (gnus-methods-equal-p 
6566                              nth4 (gnus-find-method-for-group from-newsgroup))
6567                             virtual
6568                             (equal nth4 
6569                                    (setq method (gnus-find-method-for-group 
6570                                                  from-newsgroup)))
6571                             (and (equal (car nth4) (car method))
6572                                  (equal (nth 1 nth4) (nth 1 method))))
6573                         gnus-use-cross-reference
6574                         (or (not (eq gnus-use-cross-reference t))
6575                             virtual
6576                             ;; Only do cross-references on subscribed
6577                             ;; groups, if that is what is wanted.  
6578                             (<= (nth 1 info) gnus-level-subscribed)))
6579                    (gnus-group-make-articles-read name idlist expirable))))
6580            xref-hashtb)))))
6581
6582 (defun gnus-group-make-articles-read (group articles expirable)
6583   (let* ((num 0)
6584          (entry (gnus-gethash group gnus-newsrc-hashtb))
6585          (info (nth 2 entry))
6586          (active (gnus-gethash group gnus-active-hashtb))
6587          exps expirable range)
6588     ;; First peel off all illegal article numbers.
6589     (if active
6590         (let ((ids articles)
6591               (ticked (cdr (assq 'tick (nth 3 info))))
6592               (dormant (cdr (assq 'dormant (nth 3 info))))
6593               id)
6594           (setq exps nil)
6595           (while ids
6596             (setq id (car ids))
6597             (if (or (> id (cdr active))
6598                     (< id (car active))
6599                     (memq id ticked)
6600                     (memq id dormant))
6601                 (setq articles (delq id articles)))
6602             (and (memq id expirable)
6603                  (setq exps (cons id exps)))
6604             (setq ids (cdr ids)))))
6605     ;; Update expirable articles.
6606     (gnus-add-marked-articles nil 'expirable exps info)
6607     (and active
6608          (null (nth 2 info))
6609          (> (car active) 1)
6610          (setcar (nthcdr 2 info) (cons 1 (1- (car active)))))
6611     (setcar (nthcdr 2 info)
6612             (setq range
6613                   (gnus-add-to-range 
6614                    (nth 2 info) 
6615                    (setq articles (sort articles '<)))))
6616     ;; Then we have to re-compute how many unread
6617     ;; articles there are in this group.
6618     (if active
6619         (progn
6620           (cond 
6621            ((not range)
6622             (setq num (- (1+ (cdr active)) (car active))))
6623            ((not (listp (cdr range)))
6624             (setq num (- (cdr active) (- (1+ (cdr range)) 
6625                                          (car range)))))
6626            (t
6627             (while range
6628               (if (numberp (car range))
6629                   (setq num (1+ num))
6630                 (setq num (+ num (- (1+ (cdr (car range)))
6631                                     (car (car range))))))
6632               (setq range (cdr range)))
6633             (setq num (- (cdr active) num))))
6634           ;; Update the number of unread articles.
6635           (setcar 
6636            entry 
6637            (max 0 (- num 
6638                      (length (cdr (assq 'tick (nth 3 info))))
6639                      (length 
6640                       (cdr (assq 'dormant (nth 3 info)))))))
6641           ;; Update the group buffer.
6642           (gnus-group-update-group group t)))))
6643
6644 (defun gnus-methods-equal-p (m1 m2)
6645   (let ((m1 (or m1 gnus-select-method))
6646         (m2 (or m2 gnus-select-method)))
6647     (or (equal m1 m2)
6648         (and (eq (car m1) (car m2))
6649              (or (not (memq 'address (assoc (symbol-name (car m1))
6650                                             gnus-valid-select-methods)))
6651                  (equal (nth 1 m1) (nth 1 m2)))))))
6652
6653 (defsubst gnus-header-value ()
6654   (buffer-substring (match-end 0) (gnus-point-at-eol)))
6655
6656 (defvar gnus-newsgroup-none-id 0)
6657
6658 (defun gnus-get-newsgroup-headers ()
6659   (setq gnus-article-internal-prepare-hook nil)
6660   (let ((cur nntp-server-buffer)
6661         (dependencies gnus-newsgroup-dependencies)
6662         headers id dep end ref)
6663     (save-excursion
6664       (set-buffer nntp-server-buffer)
6665       (goto-char (point-min))
6666       ;; Search to the beginning of the next header. Error messages
6667       ;; do not begin with 2 or 3.
6668       (while (re-search-forward "^[23][0-9]+ " nil t)
6669         (let ((header (make-vector 9 nil))
6670               (case-fold-search t)
6671               (p (point))
6672               in-reply-to)
6673           (setq id nil
6674                 ref nil)
6675           (header-set-number header (read cur))
6676           ;; This implementation of this function, with nine
6677           ;; search-forwards instead of the one re-search-forward and
6678           ;; a case (which basically was the old function) is actually
6679           ;; about twice as fast, even though it looks messier. You
6680           ;; can't have everything, I guess. Speed and elegance
6681           ;; doesn't always come hand in hand.
6682           (save-restriction
6683             (narrow-to-region (point) (or (save-excursion 
6684                                             (search-forward "\n.\n" nil t))
6685                                           (point)))
6686             (if (search-forward "\nfrom: " nil t)
6687                 (header-set-from header (gnus-header-value))
6688               (header-set-from header "(nobody)"))
6689             (goto-char p)
6690             (if (search-forward "\nsubject: " nil t)
6691                 (header-set-subject header (gnus-header-value))
6692               (header-set-subject header "(none)"))
6693             (goto-char p)
6694             (and (search-forward "\nxref: " nil t)
6695                  (header-set-xref header (gnus-header-value)))
6696             (goto-char p)
6697             (or (numberp (and (search-forward "\nlines: " nil t)
6698                               (header-set-lines header (read cur))))
6699                 (header-set-lines header 0))
6700             (goto-char p)
6701             (and (search-forward "\ndate: " nil t)
6702                  (header-set-date header (gnus-header-value)))
6703             (goto-char p)
6704             (if (search-forward "\nmessage-id: " nil t)
6705                 (header-set-id header (setq id (gnus-header-value)))
6706               ;; If there was no message-id, we just fake one to make
6707               ;; subsequent routines simpler.
6708               (header-set-id 
6709                header 
6710                (setq id (concat "none+" 
6711                                 (int-to-string 
6712                                  (setq gnus-newsgroup-none-id 
6713                                        (1+ gnus-newsgroup-none-id)))))))
6714             (goto-char p)
6715             (if (search-forward "\nreferences: " nil t)
6716                 (progn
6717                   (header-set-references header (gnus-header-value))
6718                   (setq end (match-end 0))
6719                   (save-excursion
6720                     (setq ref 
6721                           (downcase
6722                            (buffer-substring
6723                             (progn 
6724                               (end-of-line)
6725                               (search-backward ">" end t)
6726                               (1+ (point)))
6727                             (progn
6728                               (search-backward "<" end t)
6729                               (point)))))))
6730               ;; Get the references from the in-reply-to header if there
6731               ;; ware no references and the in-reply-to header looks
6732               ;; promising. 
6733               (if (and (search-forward "\nin-reply-to: " nil t)
6734                        (setq in-reply-to (gnus-header-value))
6735                        (string-match "<[^>]+>" in-reply-to))
6736                   (progn
6737                     (header-set-references 
6738                      header 
6739                      (setq ref (substring in-reply-to (match-beginning 0)
6740                                           (match-end 0))))
6741                     (setq ref (downcase ref)))
6742                 (setq ref "none")))
6743             ;; We do some threading while we read the headers. The
6744             ;; message-id and the last reference are both entered into
6745             ;; the same hash table. Some tippy-toeing around has to be
6746             ;; done in case an article has arrived before the article
6747             ;; which it refers to.
6748             (if (boundp (setq dep (intern (downcase id) dependencies)))
6749                 (if (car (symbol-value dep))
6750                     ;; An article with this Message-ID has already
6751                     ;; been seen, so we ignore this one, except we add
6752                     ;; any additional Xrefs (in case the two articles
6753                     ;; came from different servers.
6754                     (progn
6755                       (header-set-xref 
6756                        (car (symbol-value dep))
6757                        (concat (or (header-xref (car (symbol-value dep))) "")
6758                                (or (header-xref header) "")))
6759                       (setq header nil))
6760                   (setcar (symbol-value dep) header))
6761               (set dep (list header)))
6762             (if header
6763                 (progn
6764                   (if (boundp (setq dep (intern ref dependencies)))
6765                       (setcdr (symbol-value dep) 
6766                               (cons header (cdr (symbol-value dep))))
6767                     (set dep (list nil header)))
6768                   (setq headers (cons header headers))))
6769             (goto-char (point-max))))))
6770     (nreverse headers)))
6771
6772 ;; The following macros and functions were written by Felix Lee
6773 ;; <flee@cse.psu.edu>. 
6774
6775 (defmacro gnus-nov-read-integer ()
6776   '(prog1
6777        (if (= (following-char) ?\t)
6778            0
6779          (let ((num (condition-case nil (read buffer) (error nil))))
6780            (if (numberp num) num 0)))
6781      (or (eobp) (forward-char 1))))
6782
6783 (defmacro gnus-nov-skip-field ()
6784   '(search-forward "\t" eol 'move))
6785
6786 (defmacro gnus-nov-field ()
6787   '(buffer-substring (point) (if (gnus-nov-skip-field) (1- (point)) eol)))
6788
6789 ;; Goes through the xover lines and returns a list of vectors
6790 (defun gnus-get-newsgroup-headers-xover (sequence)
6791   "Parse the news overview data in the server buffer, and return a
6792 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
6793   ;; Get the Xref when the users reads the articles since most/some
6794   ;; NNTP servers do not include Xrefs when using XOVER.
6795   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
6796   (let ((cur nntp-server-buffer)
6797         (dependencies gnus-newsgroup-dependencies)
6798         number headers header)
6799     (save-excursion
6800       (set-buffer nntp-server-buffer)
6801       (goto-char (point-min))
6802       (while (and sequence (not (eobp)))
6803         (setq number (read cur))
6804         (while (and sequence (< (car sequence) number))
6805           (setq sequence (cdr sequence)))
6806         (and sequence 
6807              (eq number (car sequence))
6808              (progn
6809                (setq sequence (cdr sequence))
6810                (if (setq header 
6811                          (inline (gnus-nov-parse-line number dependencies)))
6812                    (setq headers (cons header headers)))))
6813         (forward-line 1))
6814       (setq headers (nreverse headers)))
6815     headers))
6816
6817 ;; This function has to be called with point after the article number
6818 ;; on the beginning of the line.
6819 (defun gnus-nov-parse-line (number dependencies)
6820   (let ((none 0)
6821         (eol (gnus-point-at-eol)) 
6822         (buffer (current-buffer))
6823         header ref id dep)
6824
6825     ;; overview: [num subject from date id refs chars lines misc]
6826     (narrow-to-region (point) eol)
6827     (forward-char)
6828
6829     (condition-case nil
6830         (setq header
6831               (vector 
6832                number                   ; number
6833                (gnus-nov-field)         ; subject
6834                (gnus-nov-field)         ; from
6835                (gnus-nov-field)         ; date
6836                (setq id (or (gnus-nov-field)
6837                             (concat "none+"
6838                                     (int-to-string 
6839                                      (setq none (1+ none)))))) ; id
6840                (progn
6841                  (save-excursion
6842                    (let ((beg (point)))
6843                      (search-forward "\t" eol)
6844                      (if (search-backward ">" beg t)
6845                          (setq ref 
6846                                (downcase 
6847                                 (buffer-substring 
6848                                  (1+ (point))
6849                                  (progn
6850                                    (search-backward "<" beg t)
6851                                    (point)))))
6852                        (setq ref nil))))
6853                  (gnus-nov-field))      ; refs
6854                (gnus-nov-read-integer)  ; chars
6855                (gnus-nov-read-integer)  ; lines
6856                (if (= (following-char) ?\n)
6857                    nil
6858                  (gnus-nov-field))      ; misc
6859                ))
6860       (error (progn 
6861                (ding)
6862                (message "Strange nov line.")
6863                (setq header nil)
6864                (goto-char eol))))
6865
6866     (widen)
6867
6868     ;; We build the thread tree.
6869     (and header
6870          (if (boundp (setq dep (intern (downcase id) dependencies)))
6871              (if (car (symbol-value dep))
6872                  ;; An article with this Message-ID has already been seen,
6873                  ;; so we ignore this one, except we add any additional
6874                  ;; Xrefs (in case the two articles came from different
6875                  ;; servers.
6876                  (progn
6877                    (header-set-xref 
6878                     (car (symbol-value dep))
6879                     (concat (or (header-xref (car (symbol-value dep))) "")
6880                             (or (header-xref header) "")))
6881                    (setq header nil))
6882                (setcar (symbol-value dep) header))
6883            (set dep (list header))))
6884     (if header
6885         (progn
6886           (if (boundp (setq dep (intern (or ref "none") 
6887                                         dependencies)))
6888               (setcdr (symbol-value dep) 
6889                       (cons header (cdr (symbol-value dep))))
6890             (set dep (list nil header)))))
6891     header))
6892
6893 (defun gnus-article-get-xrefs ()
6894   "Fill in the Xref value in `gnus-current-headers', if necessary.
6895 This is meant to be called in `gnus-article-internal-prepare-hook'."
6896   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
6897                                  gnus-current-headers)))
6898     (or (not gnus-use-cross-reference)
6899         (not headers)
6900         (and (header-xref headers)
6901              (not (string= (header-xref headers) "")))
6902         (let ((case-fold-search t)
6903               xref)
6904           (save-restriction
6905             (gnus-narrow-to-headers)
6906             (goto-char (point-min))
6907             (if (or (and (eq (downcase (following-char)) ?x)
6908                          (looking-at "Xref:"))
6909                     (search-forward "\nXref:" nil t))
6910                 (progn
6911                   (goto-char (1+ (match-end 0)))
6912                   (setq xref (buffer-substring (point) 
6913                                                (progn (end-of-line) (point))))
6914                   (header-set-xref headers xref))))))))
6915
6916 (defalias 'gnus-find-header-by-number 'gnus-get-header-by-number)
6917 (make-obsolete 'gnus-find-header-by-number 'gnus-get-header-by-number)
6918
6919 ;; Return a header specified by a NUMBER.
6920 (defun gnus-get-header-by-number (number)
6921   (save-excursion
6922     (set-buffer gnus-summary-buffer)
6923     (or gnus-newsgroup-headers-hashtb-by-number
6924         (gnus-make-headers-hashtable-by-number))
6925     (gnus-gethash (int-to-string number)
6926                   gnus-newsgroup-headers-hashtb-by-number)))
6927
6928 (defun gnus-make-headers-hashtable-by-number ()
6929   "Make hashtable for the variable gnus-newsgroup-headers by number."
6930   (save-excursion
6931     (set-buffer gnus-summary-buffer)
6932     (let ((headers gnus-newsgroup-headers)
6933           header)
6934       (setq gnus-newsgroup-headers-hashtb-by-number
6935             (gnus-make-hashtable (length headers)))
6936       (while headers
6937         (setq header (car headers))
6938         (gnus-sethash (int-to-string (header-number header))
6939                       header gnus-newsgroup-headers-hashtb-by-number)
6940         (setq headers (cdr headers))))))
6941
6942 (defun gnus-more-header-backward ()
6943   "Find new header backward."
6944   (let ((first (car (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
6945         (artnum gnus-newsgroup-begin)
6946         (header nil))
6947     (while (and (not header)
6948                 (> artnum first))
6949       (setq artnum (1- artnum))
6950       (setq header (gnus-read-header artnum)))
6951     header))
6952
6953 (defun gnus-more-header-forward (&optional backward)
6954   "Find new header forward.
6955 If BACKWARD, find new header backward instead."
6956   (if backward
6957       (gnus-more-header-backward)
6958     (let ((last (cdr (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
6959           (artnum gnus-newsgroup-end)
6960           (header nil))
6961       (while (and (not header)
6962                   (< artnum last))
6963         (setq artnum (1+ artnum))
6964         (setq header (gnus-read-header artnum)))
6965       header)))
6966
6967 (defun gnus-extend-newsgroup (header &optional backward)
6968   "Extend newsgroup selection with HEADER.
6969 Optional argument BACKWARD means extend toward backward."
6970   (if header
6971       (let ((artnum (header-number header)))
6972         (setq gnus-newsgroup-headers
6973               (if backward
6974                   (cons header gnus-newsgroup-headers)
6975                 (nconc gnus-newsgroup-headers (list header))))
6976         (setq gnus-newsgroup-unselected
6977               (delq artnum gnus-newsgroup-unselected))
6978         (setq gnus-newsgroup-begin (min gnus-newsgroup-begin artnum))
6979         (setq gnus-newsgroup-end (max gnus-newsgroup-end artnum)))))
6980
6981 (defun gnus-summary-work-articles (n)
6982   "Return a list of articles to be worked upon. The prefix argument,
6983 the list of process marked articles, and the current article will be
6984 taken into consideration."
6985   (let (articles)
6986     (if (and n (numberp n))
6987         (let ((backward (< n 0))
6988               (n (abs n)))
6989           (save-excursion
6990             (while (and (> n 0)
6991                         (setq articles (cons (gnus-summary-article-number) 
6992                                              articles))
6993                         (gnus-summary-search-forward nil nil backward))
6994               (setq n (1- n))))
6995           (sort articles (function <)))
6996       (or (reverse gnus-newsgroup-processable)
6997           (list (gnus-summary-article-number))))))
6998
6999 (defun gnus-summary-search-group (&optional backward use-level)
7000   "Search for next unread newsgroup.
7001 If optional argument BACKWARD is non-nil, search backward instead."
7002   (save-excursion
7003     (set-buffer gnus-group-buffer)
7004     (if (gnus-group-search-forward 
7005          backward nil (if use-level (gnus-group-group-level) nil))
7006         (gnus-group-group-name))))
7007
7008 (defun gnus-summary-best-group (&optional exclude-group)
7009   "Find the name of the best unread group.
7010 If EXCLUDE-GROUP, do not go to this group."
7011   (save-excursion
7012     (set-buffer gnus-group-buffer)
7013     (save-excursion
7014       (gnus-group-best-unread-group exclude-group))))
7015
7016 (defun gnus-summary-search-subject (&optional backward unread subject)
7017   "Search for article forward.
7018 If BACKWARD is non-nil, search backward.
7019 If UNREAD is non-nil, only unread articles are selected.
7020 If SUBJECT is non-nil, the article which has the same subject will be
7021 searched for." 
7022   (let ((func (if backward 'previous-single-property-change
7023                 'next-single-property-change))
7024         (beg (point))
7025         (did t)
7026         pos psubject)
7027     (beginning-of-line)
7028     (and gnus-summary-check-current unread
7029          (eq (get-text-property (point) 'gnus-mark) gnus-unread-mark)
7030          (setq did nil))
7031     (if (not did)
7032         ()
7033       (forward-char (if backward (if (bobp) 0 -1) (if (eobp) 0 1)))
7034       (while
7035           (and 
7036            (setq pos (funcall func (point) 'gnus-number))
7037            (goto-char (if backward (1- pos) pos))
7038            (setq did
7039                  (not (and
7040                        (or (not unread)
7041                            (eq (get-text-property (point) 'gnus-mark)
7042                                gnus-unread-mark))
7043                        (or (not subject)
7044                            (and (setq psubject (gnus-summary-subject-string))
7045                                 (gnus-subject-equal subject psubject))))))
7046            (if backward (if (bobp) nil (forward-char -1) t)
7047              (if (eobp) nil (forward-char 1) t)))))
7048     (if did
7049         (progn (goto-char beg) nil)
7050       (prog1
7051           (get-text-property (point) 'gnus-number)
7052         (gnus-summary-position-cursor)))))
7053
7054 (defun gnus-subject-equal (s1 s2)
7055   (cond
7056    ((null gnus-summary-gather-subject-limit)
7057     (equal (gnus-simplify-subject-re s1)
7058            (gnus-simplify-subject-re s2)))
7059    ((eq gnus-summary-gather-subject-limit 'fuzzy)
7060     (equal (gnus-simplify-subject-fuzzy s1)
7061            (gnus-simplify-subject-fuzzy s2)))
7062    ((numberp gnus-summary-gather-subject-limit)
7063     (equal (gnus-limit-string s1 gnus-summary-gather-subject-limit)
7064            (gnus-limit-string s2 gnus-summary-gather-subject-limit)))
7065    (t
7066     (equal s1 s2))))
7067     
7068 (defun gnus-summary-search-forward (&optional unread subject backward)
7069   "Search for article forward.
7070 If UNREAD is non-nil, only unread articles are selected.
7071 If SUBJECT is non-nil, the article which has the same subject will be
7072 searched for. 
7073 If BACKWARD is non-nil, the search will be performed backwards instead."
7074   (gnus-summary-search-subject backward unread subject))
7075
7076 (defun gnus-summary-search-backward (&optional unread subject)
7077   "Search for article backward.
7078 If 1st optional argument UNREAD is non-nil, only unread article is selected.
7079 If 2nd optional argument SUBJECT is non-nil, the article which has
7080 the same subject will be searched for."
7081   (gnus-summary-search-forward unread subject t))
7082
7083 (defun gnus-summary-article-number (&optional number-or-nil)
7084   "The article number of the article on the current line.
7085 If there isn's an article number here, then we return the current
7086 article number."
7087   (let* ((number (get-text-property (gnus-point-at-bol) 'gnus-number)))
7088     (if number-or-nil number (or number gnus-current-article))))
7089
7090 (defun gnus-summary-thread-level ()
7091   "The thread level of the article on the current line."
7092   (or (get-text-property (gnus-point-at-bol) 'gnus-level)
7093       0))
7094
7095 (defun gnus-summary-pseudo-article ()
7096   "The thread level of the article on the current line."
7097   (get-text-property (gnus-point-at-bol) 'gnus-pseudo))
7098
7099 (defun gnus-summary-article-mark ()
7100   "The mark on the current line."
7101   (get-text-property (gnus-point-at-bol) 'gnus-mark))
7102
7103 (defun gnus-summary-subject-string ()
7104   "Return current subject string or nil if nothing."
7105   (let ((article (gnus-summary-article-number))
7106         header)
7107     (and article 
7108          (setq header (gnus-get-header-by-number article))
7109          (vectorp header)
7110          (header-subject header))))
7111
7112 (defalias 'gnus-summary-score 'gnus-summary-article-score)
7113 (make-obsolete 'gnus-summary-score 'gnus-summary-article-score)
7114 (defun gnus-summary-article-score ()
7115   "Return current article score."
7116   (or (cdr (assq (gnus-summary-article-number) gnus-newsgroup-scored))
7117       gnus-summary-default-score 0))
7118
7119 (defun gnus-summary-recenter ()
7120   "Center point in the summary window.
7121 If `gnus-auto-center-summary' is nil, or the article buffer isn't
7122 displayed, no centering will be performed." 
7123   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
7124   ;; Recenter only when requested. Suggested by popovich@park.cs.columbia.edu.
7125   (let* ((top (cond ((< (window-height) 4) 0)
7126                     ((< (window-height) 7) 1)
7127                     (t 2)))
7128          (height (1- (window-height)))
7129          (bottom (save-excursion (goto-char (point-max))
7130                                  (forward-line (- height))
7131                                  (point)))
7132          (window (get-buffer-window (current-buffer))))
7133     (and 
7134      ;; The user has to want it,
7135      gnus-auto-center-summary 
7136      ;; the article buffer must be displayed,
7137      (get-buffer-window gnus-article-buffer)
7138      ;; Set the window start to either `bottom', which is the biggest
7139      ;; possible valid number, or the second line from the top,
7140      ;; whichever is the least.
7141      (set-window-start
7142       window (min bottom (save-excursion (forward-line (- top)) (point)))))))
7143
7144 ;; Function written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
7145 (defun gnus-short-group-name (group &optional levels)
7146   "Collapse GROUP name LEVELS."
7147   (let* ((name "") (foreign "") (depth -1) (skip 1)
7148          (levels (or levels
7149                      (progn
7150                        (while (string-match "\\." group skip)
7151                          (setq skip (match-end 0)
7152                                depth (+ depth 1)))
7153                        depth))))
7154     (if (string-match ":" group)
7155         (setq foreign (substring group 0 (match-end 0))
7156               group (substring group (match-end 0))))
7157     (while group
7158       (if (and (string-match "\\." group) (> levels 0))
7159           (setq name (concat name (substring group 0 1))
7160                 group (substring group (match-end 0))
7161                 levels (- levels 1)
7162                 name (concat name "."))
7163         (setq name (concat foreign name group)
7164               group nil)))
7165     name))
7166
7167 (defun gnus-summary-jump-to-group (newsgroup)
7168   "Move point to NEWSGROUP in group mode buffer."
7169   ;; Keep update point of group mode buffer if visible.
7170   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
7171       (save-window-excursion
7172         ;; Take care of tree window mode.
7173         (if (get-buffer-window gnus-group-buffer)
7174             (pop-to-buffer gnus-group-buffer))
7175         (gnus-group-jump-to-group newsgroup))
7176     (save-excursion
7177       ;; Take care of tree window mode.
7178       (if (get-buffer-window gnus-group-buffer)
7179           (pop-to-buffer gnus-group-buffer)
7180         (set-buffer gnus-group-buffer))
7181       (gnus-group-jump-to-group newsgroup))))
7182
7183 ;; This function returns a list of article numbers based on the
7184 ;; difference between the ranges of read articles in this group and
7185 ;; the range of active articles.
7186 (defun gnus-list-of-unread-articles (group)
7187   (let* ((read (nth 2 (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
7188          (active (gnus-gethash group gnus-active-hashtb))
7189          (last (cdr active))
7190          first nlast unread)
7191     ;; If none are read, then all are unread. 
7192     (if (not read)
7193         (setq first (car active))
7194       ;; If the range of read articles is a single range, then the
7195       ;; first unread article is the article after the last read
7196       ;; article. Sounds logical, doesn't it?
7197       (if (not (listp (cdr read)))
7198           (setq first (1+ (cdr read)))
7199         ;; `read' is a list of ranges.
7200         (if (/= (setq nlast (or (and (numberp (car read)) (car read)) 
7201                                 (car (car read)))) 1)
7202             (setq first 1))
7203         (while read
7204           (if first 
7205               (while (< first nlast)
7206                 (setq unread (cons first unread))
7207                 (setq first (1+ first))))
7208           (setq first (1+ (if (atom (car read)) (car read) (cdr (car read)))))
7209           (setq nlast (if (atom (car (cdr read))) 
7210                           (car (cdr read))
7211                         (car (car (cdr read)))))
7212           (setq read (cdr read)))))
7213     ;; And add the last unread articles.
7214     (while (<= first last)
7215       (setq unread (cons first unread))
7216       (setq first (1+ first)))
7217     ;; Return the list of unread articles.
7218     (nreverse unread)))
7219
7220 (defun gnus-list-of-read-articles (group)
7221   (let ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
7222         (active (gnus-gethash group gnus-active-hashtb)))
7223     (and info active
7224          (gnus-sorted-complement 
7225           (gnus-uncompress-range active) 
7226           (gnus-list-of-unread-articles group)))))
7227
7228 ;; Various summary commands
7229
7230 (defun gnus-summary-universal-argument ()
7231   "Perform any operation on all articles marked with the process mark."
7232   (interactive)
7233   (gnus-set-global-variables)
7234   (let ((articles (reverse gnus-newsgroup-processable))
7235         func)
7236     (or articles (error "No articles marked"))
7237     (or (setq func (key-binding (read-key-sequence "C-c C-u")))
7238         (error "Undefined key"))
7239     (while articles
7240       (gnus-summary-goto-subject (car articles))
7241       (command-execute func)
7242       (gnus-summary-remove-process-mark (car articles))
7243       (setq articles (cdr articles)))))
7244
7245 (defun gnus-summary-toggle-truncation (arg)
7246   "Toggle truncation of summary lines.
7247 With arg, turn line truncation on iff arg is positive."
7248   (interactive "P")
7249   (setq truncate-lines
7250         (if (null arg) (not truncate-lines)
7251           (> (prefix-numeric-value arg) 0)))
7252   (redraw-display))
7253
7254 (defun gnus-summary-reselect-current-group (all)
7255   "Once exit and then reselect the current newsgroup.
7256 The prefix argument ALL means to select all articles."
7257   (interactive "P")
7258   (gnus-set-global-variables)
7259   (let ((current-subject (gnus-summary-article-number))
7260         (group gnus-newsgroup-name))
7261     (setq gnus-newsgroup-begin nil)
7262     (gnus-summary-exit t)
7263     ;; We have to adjust the point of group mode buffer because the
7264     ;; current point was moved to the next unread newsgroup by
7265     ;; exiting.
7266     (gnus-summary-jump-to-group group)
7267     (gnus-group-read-group all t)
7268     (gnus-summary-goto-subject current-subject)))
7269
7270 (defun gnus-summary-rescan-group (all)
7271   "Exit the newsgroup, ask for new articles, and select the newsgroup."
7272   (interactive "P")
7273   (gnus-set-global-variables)
7274   ;; Fix by Ilja Weis <kult@uni-paderborn.de>.
7275   (let ((group gnus-newsgroup-name))
7276     (gnus-summary-exit)
7277     (gnus-summary-jump-to-group group)
7278     (save-excursion
7279       (set-buffer gnus-group-buffer)
7280       (gnus-group-get-new-news-this-group 1))
7281     (gnus-summary-jump-to-group group)
7282     (gnus-group-read-group all)))
7283
7284 (defun gnus-summary-update-info ()
7285   (let* ((group gnus-newsgroup-name))
7286     (if gnus-newsgroup-kill-headers
7287         (setq gnus-newsgroup-killed
7288               (gnus-compress-sequence
7289                (nconc
7290                 (gnus-set-sorted-intersection
7291                  (gnus-uncompress-range gnus-newsgroup-killed)
7292                  (setq gnus-newsgroup-unselected
7293                        (sort gnus-newsgroup-unselected '<)))
7294                 (setq gnus-newsgroup-unreads
7295                       (sort gnus-newsgroup-unreads '<))) t)))
7296     (or (listp (cdr gnus-newsgroup-killed))
7297         (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
7298     (let ((headers gnus-newsgroup-headers))
7299       (gnus-close-group group)
7300       (run-hooks 'gnus-exit-group-hook)
7301       (gnus-update-read-articles 
7302        group gnus-newsgroup-unreads gnus-newsgroup-unselected 
7303        gnus-newsgroup-marked
7304        t gnus-newsgroup-replied gnus-newsgroup-expirable
7305        gnus-newsgroup-killed gnus-newsgroup-dormant
7306        gnus-newsgroup-bookmarks 
7307        (and gnus-save-score gnus-newsgroup-scored))
7308       (and gnus-use-cross-reference
7309            (gnus-mark-xrefs-as-read 
7310             group headers gnus-newsgroup-unreads gnus-newsgroup-expirable))
7311       ;; Do adaptive scoring, and possibly save score files.
7312       (and gnus-newsgroup-adaptive
7313            (gnus-score-adaptive))
7314       (and gnus-use-scoring 
7315            (fboundp 'gnus-score-save)
7316            (funcall 'gnus-score-save))
7317       ;; Do not switch windows but change the buffer to work.
7318       (set-buffer gnus-group-buffer)
7319       (or (assoc 'quit-config (gnus-find-method-for-group gnus-newsgroup-name))
7320           (gnus-group-update-group group)))))
7321   
7322 (defun gnus-summary-exit (&optional temporary)
7323   "Exit reading current newsgroup, and then return to group selection mode.
7324 gnus-exit-group-hook is called with no arguments if that value is non-nil."
7325   (interactive)
7326   (gnus-set-global-variables)
7327   (gnus-kill-save-kill-buffer)
7328   (let* ((group gnus-newsgroup-name)
7329          (quit-config (nth 1 (assoc 'quit-config (gnus-find-method-for-group
7330                                                   gnus-newsgroup-name))))
7331          (mode major-mode)
7332          (buf (current-buffer)))
7333     (gnus-summary-update-info) ; Make all changes in this group permanent.
7334     (set-buffer buf)
7335     (run-hooks 'gnus-summary-exit-hook)
7336     (and gnus-use-cache (gnus-cache-possibly-remove-articles))
7337     ;; Make sure where I was, and go to next newsgroup.
7338     (set-buffer gnus-group-buffer)
7339     (or quit-config
7340         (progn
7341           (gnus-group-jump-to-group group)
7342           (gnus-group-next-unread-group 1)))
7343     (if temporary
7344         nil                             ;Nothing to do.
7345       ;; We set all buffer-local variables to nil. It is unclear why
7346       ;; this is needed, but if we don't, buffer-local variables are
7347       ;; not garbage-collected, it seems. This would the lead to en
7348       ;; ever-growing Emacs.
7349       (set-buffer buf)
7350       (gnus-summary-clear-local-variables)
7351       ;; We clear the global counterparts of the buffer-local
7352       ;; variables as well, just to be on the safe side.
7353       (gnus-configure-windows 'group)
7354       (gnus-summary-clear-local-variables)
7355       ;; Return to group mode buffer. 
7356       (if (eq mode 'gnus-summary-mode)
7357           (gnus-kill-buffer buf))
7358       (if (get-buffer gnus-article-buffer)
7359           (bury-buffer gnus-article-buffer))
7360       (setq gnus-current-select-method gnus-select-method)
7361       (pop-to-buffer gnus-group-buffer)
7362       (if (not quit-config)
7363           (progn
7364             (gnus-group-jump-to-group group)
7365             (gnus-group-next-unread-group 1))
7366         (if (not (buffer-name (car quit-config)))
7367             (gnus-configure-windows 'group)
7368           (set-buffer (car quit-config))
7369           (and (eq major-mode 'gnus-summary-mode)
7370                (gnus-set-global-variables))
7371           (gnus-configure-windows (cdr quit-config)))))))
7372
7373 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
7374 (defun gnus-summary-exit-no-update (&optional no-questions)
7375   "Quit reading current newsgroup without updating read article info."
7376   (interactive)
7377   (gnus-set-global-variables)
7378   (let* ((group gnus-newsgroup-name)
7379          (quit-config (nth 1 (assoc 'quit-config 
7380                                     (gnus-find-method-for-group group)))))
7381     (if (or no-questions
7382             gnus-expert-user
7383             (gnus-y-or-n-p "Do you really wanna quit reading this group? "))
7384         (progn
7385           (and gnus-use-cache (gnus-cache-possibly-remove-articles))
7386           (gnus-close-group group)
7387           (gnus-summary-clear-local-variables)
7388           (set-buffer gnus-group-buffer)
7389           (gnus-summary-clear-local-variables)
7390           ;; Return to group selection mode.
7391           (gnus-configure-windows 'group)
7392           (if (get-buffer gnus-summary-buffer)
7393               (kill-buffer gnus-summary-buffer))
7394           (if (get-buffer gnus-article-buffer)
7395               (bury-buffer gnus-article-buffer))
7396           (if (equal (gnus-group-group-name) group)
7397               (gnus-group-next-unread-group 1))
7398           (if quit-config
7399               (progn
7400                 (if (not (buffer-name (car quit-config)))
7401                     (gnus-configure-windows 'group)
7402                   (set-buffer (car quit-config))
7403                   (and (eq major-mode 'gnus-summary-mode)
7404                        (gnus-set-global-variables))
7405                   (gnus-configure-windows (cdr quit-config)))))))))
7406
7407 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
7408 (defun gnus-summary-fetch-faq (group)
7409   "Fetch the FAQ for the current group."
7410   (interactive (list gnus-newsgroup-name))
7411   (let ((gnus-faq-buffer 
7412          (find-file (concat gnus-group-faq-directory 
7413                             (gnus-group-real-name group)))))
7414   (and gnus-faq-buffer (gnus-configure-windows 'summary-faq))))
7415
7416 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
7417 (defun gnus-summary-describe-group (force)
7418   "Describe the current newsgroup."
7419   (interactive "P")
7420   (gnus-group-describe-group force gnus-newsgroup-name))
7421
7422 (defun gnus-summary-describe-briefly ()
7423   "Describe summary mode commands briefly."
7424   (interactive)
7425   (gnus-message 6
7426     (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")))
7427
7428 ;; Walking around group mode buffer from summary mode.
7429
7430 (defun gnus-summary-next-group (&optional no-article target-group backward)
7431   "Exit current newsgroup and then select next unread newsgroup.
7432 If prefix argument NO-ARTICLE is non-nil, no article is selected
7433 initially. If NEXT-GROUP, go to this group. If BACKWARD, go to
7434 previous group instead."
7435   (interactive "P")
7436   (gnus-set-global-variables)
7437   (let ((current-group gnus-newsgroup-name)
7438         (current-buffer (current-buffer))
7439         entered)
7440     ;; First we semi-exit this group to update Xrefs and all variables.
7441     ;; We can't do a real exit, because the window conf must remain
7442     ;; the same in case the user is prompted for info, and we don't
7443     ;; want the window conf to change before that...
7444     (gnus-summary-exit t)
7445     (while (not entered)
7446       ;; Then we find what group we are supposed to enter.
7447       (set-buffer gnus-group-buffer)
7448       (gnus-group-jump-to-group current-group)
7449       (setq target-group 
7450             (or target-group        
7451                 (if (eq gnus-keep-same-level 'best) 
7452                     (gnus-summary-best-group gnus-newsgroup-name)
7453                   (gnus-summary-search-group backward gnus-keep-same-level))))
7454       (if (not target-group)
7455           ;; There are no further groups, so we return to the group
7456           ;; buffer.
7457           (progn
7458             (gnus-message 5 "Returning to the group buffer")
7459             (setq entered t)
7460             (set-buffer current-buffer)
7461             (gnus-summary-exit))
7462         ;; We try to enter the target group.
7463         (gnus-group-jump-to-group target-group)
7464         (if (and (not (zerop (gnus-group-group-unread)))
7465                  (gnus-summary-read-group
7466                   target-group nil no-article current-buffer))
7467             (setq entered t)
7468           (setq current-group target-group
7469                 target-group nil))))))
7470
7471 (defun gnus-summary-next-group-old (&optional no-article group backward)
7472   "Exit current newsgroup and then select next unread newsgroup.
7473 If prefix argument NO-ARTICLE is non-nil, no article is selected initially.
7474 If BACKWARD, go to previous group instead."
7475   (interactive "P")
7476   (gnus-set-global-variables)
7477   (let ((ingroup gnus-newsgroup-name)
7478         (sumbuf (current-buffer))
7479         num)
7480     (set-buffer gnus-group-buffer)
7481     (if (and group
7482              (or (and (numberp (setq num (car (gnus-gethash
7483                                                group gnus-newsrc-hashtb))))
7484                       (< num 1))
7485                  (null num)))
7486         (progn
7487           (gnus-group-jump-to-group group)
7488           (setq group nil))
7489       (gnus-group-jump-to-group ingroup))
7490     (gnus-summary-search-group backward)
7491     (let ((group (or group (gnus-summary-search-group backward))))
7492       (set-buffer sumbuf)
7493       (gnus-summary-exit t)             ;Update all information.
7494       (if (null group)
7495           (gnus-summary-exit-no-update t)
7496         (gnus-group-jump-to-group ingroup)
7497         (setq group (gnus-summary-search-group backward))
7498         (gnus-message 5 "Selecting %s..." group)
7499         (set-buffer gnus-group-buffer)
7500         ;; We are now in group mode buffer.
7501         ;; Make sure group mode buffer point is on GROUP.
7502         (gnus-group-jump-to-group group)
7503         (if (not (eq gnus-auto-select-next 'quietly))
7504             (progn
7505               (gnus-summary-read-group group nil no-article sumbuf)
7506               (and (string= gnus-newsgroup-name ingroup)
7507                    (bufferp sumbuf) (buffer-name sumbuf)
7508                    (progn
7509                      (set-buffer (setq gnus-summary-buffer sumbuf))
7510                      (gnus-summary-exit-no-update t))))
7511           (let ((prevgroup group))
7512             (gnus-group-jump-to-group ingroup)
7513             (setq group (gnus-summary-search-group backward))
7514             (gnus-summary-read-group group nil no-article sumbuf)
7515             (while (and (string= gnus-newsgroup-name ingroup)
7516                         (bufferp sumbuf) 
7517                         (buffer-name sumbuf)
7518                         (not (string= prevgroup (gnus-group-group-name))))
7519               (set-buffer gnus-group-buffer)
7520               (gnus-summary-read-group 
7521                (setq prevgroup (gnus-group-group-name)) 
7522                nil no-article sumbuf))
7523             (and (string= prevgroup (gnus-group-group-name))
7524                  ;; We have reached the final group in the group
7525                  ;; buffer.
7526                  (progn
7527                    (if (buffer-name sumbuf)
7528                        (progn
7529                          (set-buffer sumbuf)
7530                          (gnus-summary-exit)))))))))))
7531
7532 (defun gnus-summary-prev-group (no-article)
7533   "Exit current newsgroup and then select previous unread newsgroup.
7534 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7535   (interactive "P")
7536   (gnus-summary-next-group no-article nil t))
7537
7538 ;; Walking around summary lines.
7539
7540 (defun gnus-summary-first-subject (unread)
7541   "Go to the first unread subject.
7542 If UNREAD is non-nil, go to the first unread article.
7543 Returns nil if there are no unread articles."
7544   (interactive "P")
7545   (prog1
7546       (cond ((not unread)
7547              (goto-char (point-min)))
7548             ((gnus-goto-char 
7549               (text-property-any 
7550                (point-min) (point-max) 'gnus-mark gnus-unread-mark))
7551              t)
7552             (t 
7553              ;; There are no unread articles.
7554              (gnus-message 3 "No more unread articles")
7555              nil))
7556     (gnus-summary-position-cursor)))
7557
7558 (defun gnus-summary-next-subject (n &optional unread dont-display)
7559   "Go to next N'th summary line.
7560 If N is negative, go to the previous N'th subject line.
7561 If UNREAD is non-nil, only unread articles are selected.
7562 The difference between N and the actual number of steps taken is
7563 returned."
7564   (interactive "p")
7565   (let ((backward (< n 0))
7566         (n (abs n)))
7567     (while (and (> n 0)
7568                 (gnus-summary-search-forward unread nil backward))
7569       (setq n (1- n)))
7570     (if (/= 0 n) (gnus-message 7 "No more%s articles"
7571                                (if unread " unread" "")))
7572     (or dont-display
7573         (progn
7574           (gnus-summary-recenter)
7575           (gnus-summary-position-cursor)))
7576   n))
7577
7578 (defun gnus-summary-next-unread-subject (n)
7579   "Go to next N'th unread summary line."
7580   (interactive "p")
7581   (gnus-summary-next-subject n t))
7582
7583 (defun gnus-summary-prev-subject (n &optional unread)
7584   "Go to previous N'th summary line.
7585 If optional argument UNREAD is non-nil, only unread article is selected."
7586   (interactive "p")
7587   (gnus-summary-next-subject (- n) unread))
7588
7589 (defun gnus-summary-prev-unread-subject (n)
7590   "Go to previous N'th unread summary line."
7591   (interactive "p")
7592   (gnus-summary-next-subject (- n) t))
7593
7594 (defun gnus-summary-goto-subject (article)
7595   "Go the subject line of ARTICLE."
7596   (interactive
7597    (list
7598     (string-to-int
7599      (completing-read "Article number: "
7600                       (mapcar
7601                        (lambda (headers)
7602                          (list
7603                           (int-to-string (header-number headers))))
7604                        gnus-newsgroup-headers)
7605                       nil 'require-match))))
7606   (or article (error "No article number"))
7607   (let ((b (point)))
7608     (if (not (gnus-goto-char (text-property-any (point-min) (point-max)
7609                                                 'gnus-number article)))
7610         ()
7611       (gnus-summary-show-thread)
7612       ;; Skip dummy articles. 
7613       (if (eq (gnus-summary-article-mark) gnus-dummy-mark)
7614           (forward-line 1))
7615       (prog1
7616           (if (not (eobp))
7617               article
7618             (goto-char b)
7619             nil)
7620         (gnus-summary-position-cursor)))))
7621
7622 ;; Walking around summary lines with displaying articles.
7623
7624 (defun gnus-summary-expand-window ()
7625   "Make the summary buffer take up the entire Emacs frame."
7626   (interactive)
7627   (gnus-set-global-variables)
7628   (gnus-configure-windows 'summary))
7629
7630 (defun gnus-summary-display-article (article &optional all-header)
7631   "Display ARTICLE in article buffer."
7632   (gnus-set-global-variables)
7633   (if (null article)
7634       nil
7635     (prog1
7636         (gnus-article-prepare article all-header)
7637       (gnus-summary-show-thread)
7638       (if (eq (gnus-summary-article-mark) gnus-dummy-mark)
7639           (progn
7640             (forward-line 1)
7641             (gnus-summary-position-cursor)))
7642       (run-hooks 'gnus-select-article-hook)
7643       (gnus-summary-recenter)
7644       (gnus-summary-goto-subject article)
7645       ;; Successfully display article.
7646       (gnus-summary-update-line)
7647       (gnus-article-set-window-start 
7648        (cdr (assq article gnus-newsgroup-bookmarks)))
7649       t)))
7650
7651 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7652   "Select the current article.
7653 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7654 non-nil, the article will be re-fetched even if it already present in
7655 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7656 be displayed."
7657   (and (not pseudo) (gnus-summary-pseudo-article)
7658        (error "This is a pseudo-article."))
7659   (let ((article (or article (gnus-summary-article-number)))
7660         (all-headers (not (not all-headers))) ;Must be T or NIL.
7661         did) 
7662     (prog1
7663         (save-excursion
7664           (set-buffer gnus-summary-buffer)
7665           (if (or (null gnus-current-article)
7666                   (null gnus-article-current)
7667                   (null (get-buffer gnus-article-buffer))
7668                   (not (eq article (cdr gnus-article-current)))
7669                   (not (equal (car gnus-article-current) gnus-newsgroup-name))
7670                   force)
7671               ;; The requested article is different from the current article.
7672               (progn
7673                 (gnus-summary-display-article article all-headers)
7674                 (setq did article))
7675             (if all-headers (gnus-article-show-all-headers))
7676             nil))
7677       (if did 
7678           (gnus-article-set-window-start 
7679            (cdr (assq article gnus-newsgroup-bookmarks)))))))
7680
7681 (defun gnus-summary-set-current-mark (&optional current-mark)
7682   "Obsolete function."
7683   nil)
7684
7685 (defun gnus-summary-next-article (unread &optional subject backward)
7686   "Select the next article.
7687 If UNREAD, only unread articles are selected.
7688 If SUBJECT, only articles with SUBJECT are selected.
7689 If BACKWARD, the previous article is selected instead of the next."
7690   (interactive "P")
7691   (gnus-set-global-variables)
7692   (let (header)
7693     (cond
7694      ;; Is there such an article?
7695      ((gnus-summary-display-article 
7696        (gnus-summary-search-forward unread subject backward))
7697       (gnus-summary-position-cursor))
7698      ;; If not, we try the first unread, if that is wanted.
7699      ((and subject
7700            gnus-auto-select-same
7701            (gnus-summary-first-unread-article))
7702       (gnus-message 6 "Wrapped"))
7703      ;; Try to get next/previous article not displayed in this group.
7704      ((and gnus-auto-extend-newsgroup
7705            (not unread) (not subject)
7706            (setq header (gnus-more-header-forward backward)))
7707       (gnus-extend-newsgroup header backward)
7708       (let ((buffer-read-only nil))
7709         (goto-char (if backward (point-min) (point-max)))
7710         (gnus-summary-prepare-threads (list header) 0))
7711       (gnus-summary-goto-article (if backward gnus-newsgroup-begin
7712                                    gnus-newsgroup-end)))
7713      ;; Go to next/previous group.
7714      (t
7715       (or (assoc 'quit-config (gnus-find-method-for-group gnus-newsgroup-name))
7716           (gnus-summary-jump-to-group gnus-newsgroup-name))
7717       (let ((cmd (aref (this-command-keys) 0))
7718             (group 
7719              (if (eq gnus-keep-same-level 'best) 
7720                  (gnus-summary-best-group gnus-newsgroup-name)
7721                (gnus-summary-search-group backward gnus-keep-same-level))))
7722         ;; For some reason, the group window gets selected. We change
7723         ;; it back.  
7724         (select-window (get-buffer-window (current-buffer)))
7725         ;; Keep just the event type of CMD.
7726         (and (listp cmd) (setq cmd (car cmd)))
7727         ;; Select next unread newsgroup automagically.
7728         (cond 
7729          ((not gnus-auto-select-next)
7730           (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7731          ((eq gnus-auto-select-next 'quietly)
7732           ;; Select quietly.
7733           (if (assoc 'quit-config (gnus-find-method-for-group 
7734                                    gnus-newsgroup-name))
7735               (gnus-summary-exit)
7736             (gnus-message 7 "No more%s articles (%s)..."
7737                           (if unread " unread" "") 
7738                           (if group (concat "selecting " group)
7739                             "exiting"))
7740             (gnus-summary-next-group nil group backward)))
7741          (t
7742           (let ((keystrokes '(?\C-n ?\C-p))
7743                 key)
7744             (while (or (null key) (memq key keystrokes))
7745               (gnus-message 
7746                7 "No more%s articles%s" (if unread " unread" "")
7747                (if (and group (not (assoc 'quit-config
7748                                           (gnus-find-method-for-group 
7749                                            gnus-newsgroup-name))))
7750                    (format " (Type %s for %s [%s])"
7751                            (single-key-description cmd) group
7752                            (car (gnus-gethash group gnus-newsrc-hashtb)))
7753                  (format " (Type %s to exit %s)"
7754                          (single-key-description cmd)
7755                          gnus-newsgroup-name)))
7756               ;; Confirm auto selection.
7757               (let* ((event (read-event)))
7758                 (setq key (if (listp event) (car event) event))
7759                 (if (memq key keystrokes)
7760                     (let ((obuf (current-buffer)))
7761                       (switch-to-buffer gnus-group-buffer)
7762                       (gnus-group-jump-to-group group)
7763                       (execute-kbd-macro (char-to-string key))
7764                       (setq group (gnus-group-group-name))
7765                       (switch-to-buffer obuf)))))
7766             (if (equal key cmd)
7767                 (if (or (not group) (assoc 'quit-config
7768                                            (gnus-find-method-for-group
7769                                             gnus-newsgroup-name)))
7770                     (gnus-summary-exit)
7771                   (gnus-summary-next-group nil group backward))
7772               (setq unread-command-events (list key)))))))))))
7773
7774 (defun gnus-summary-next-unread-article ()
7775   "Select unread article after current one."
7776   (interactive)
7777   (gnus-summary-next-article t (and gnus-auto-select-same
7778                                     (gnus-summary-subject-string))))
7779
7780 (defun gnus-summary-prev-article (unread &optional subject)
7781   "Select the article after the current one.
7782 If UNREAD is non-nil, only unread articles are selected."
7783   (interactive "P")
7784   (gnus-summary-next-article unread subject t))
7785
7786 (defun gnus-summary-prev-unread-article ()
7787   "Select unred article before current one."
7788   (interactive)
7789   (gnus-summary-prev-article t (and gnus-auto-select-same
7790                                     (gnus-summary-subject-string))))
7791
7792 (defun gnus-summary-next-page (lines &optional circular)
7793   "Show next page of selected article.
7794 If end of article, select next article.
7795 Argument LINES specifies lines to be scrolled up.
7796 If CIRCULAR is non-nil, go to the start of the article instead of 
7797 instead of selecting the next article when reaching the end of the
7798 current article." 
7799   (interactive "P")
7800   (setq gnus-summary-buffer (current-buffer))
7801   (gnus-set-global-variables)
7802   (let ((article (gnus-summary-article-number))
7803         (endp nil))
7804     (gnus-configure-windows 'article)
7805     (if (or (null gnus-current-article)
7806             (null gnus-article-current)
7807             (/= article (cdr gnus-article-current))
7808             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7809         ;; Selected subject is different from current article's.
7810         (gnus-summary-display-article article)
7811       (gnus-eval-in-buffer-window
7812        gnus-article-buffer
7813        (setq endp (gnus-article-next-page lines)))
7814       (if endp
7815           (cond (circular
7816                  (gnus-summary-beginning-of-article))
7817                 (lines
7818                  (gnus-message 3 "End of message"))
7819                 ((null lines)
7820                  (gnus-summary-next-unread-article)))))
7821     (gnus-summary-recenter)
7822     (gnus-summary-position-cursor)))
7823
7824 (defun gnus-summary-prev-page (lines)
7825   "Show previous page of selected article.
7826 Argument LINES specifies lines to be scrolled down."
7827   (interactive "P")
7828   (gnus-set-global-variables)
7829   (let ((article (gnus-summary-article-number)))
7830     (gnus-configure-windows 'article)
7831     (if (or (null gnus-current-article)
7832             (null gnus-article-current)
7833             (/= article (cdr gnus-article-current))
7834             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7835         ;; Selected subject is different from current article's.
7836         (gnus-summary-display-article article)
7837       (gnus-summary-recenter)
7838       (gnus-eval-in-buffer-window gnus-article-buffer
7839         (gnus-article-prev-page lines))))
7840   (gnus-summary-position-cursor))
7841
7842 (defun gnus-summary-scroll-up (lines)
7843   "Scroll up (or down) one line current article.
7844 Argument LINES specifies lines to be scrolled up (or down if negative)."
7845   (interactive "p")
7846   (gnus-set-global-variables)
7847   (gnus-configure-windows 'article)
7848   (or (gnus-summary-select-article nil nil 'pseudo)
7849       (gnus-eval-in-buffer-window 
7850        gnus-article-buffer
7851        (cond ((> lines 0)
7852               (if (gnus-article-next-page lines)
7853                   (gnus-message 3 "End of message")))
7854              ((< lines 0)
7855               (gnus-article-prev-page (- lines))))))
7856   (gnus-summary-recenter)
7857   (gnus-summary-position-cursor))
7858
7859 (defun gnus-summary-next-same-subject ()
7860   "Select next article which has the same subject as current one."
7861   (interactive)
7862   (gnus-set-global-variables)
7863   (gnus-summary-next-article nil (gnus-summary-subject-string)))
7864
7865 (defun gnus-summary-prev-same-subject ()
7866   "Select previous article which has the same subject as current one."
7867   (interactive)
7868   (gnus-set-global-variables)
7869   (gnus-summary-prev-article nil (gnus-summary-subject-string)))
7870
7871 (defun gnus-summary-next-unread-same-subject ()
7872   "Select next unread article which has the same subject as current one."
7873   (interactive)
7874   (gnus-set-global-variables)
7875   (gnus-summary-next-article t (gnus-summary-subject-string)))
7876
7877 (defun gnus-summary-prev-unread-same-subject ()
7878   "Select previous unread article which has the same subject as current one."
7879   (interactive)
7880   (gnus-set-global-variables)
7881   (gnus-summary-prev-article t (gnus-summary-subject-string)))
7882
7883 (defun gnus-summary-first-unread-article ()
7884   "Select the first unread article. 
7885 Return nil if there are no unread articles."
7886   (interactive)
7887   (gnus-set-global-variables)
7888   (prog1
7889       (if (gnus-summary-first-subject t)
7890           (gnus-summary-display-article (gnus-summary-article-number)))
7891     (gnus-summary-position-cursor)))
7892
7893 (defun gnus-summary-best-unread-article ()
7894   "Select the unread article with the highest score."
7895   (interactive)
7896   (gnus-set-global-variables)
7897   (let ((scored gnus-newsgroup-scored)
7898         (best -1000000)
7899         article art)
7900     (while scored
7901       (or (> best (cdr (car scored)))
7902           (and (memq (setq art (car (car scored))) gnus-newsgroup-unreads)
7903                (not (memq art gnus-newsgroup-marked))
7904                (not (memq art gnus-newsgroup-dormant))
7905                (if (= best (cdr (car scored)))
7906                    (setq article (min art article))
7907                  (setq article art)
7908                  (setq best (cdr (car scored))))))
7909       (setq scored (cdr scored)))
7910     (if article 
7911         (gnus-summary-goto-article article)
7912       (gnus-summary-first-unread-article))
7913     (gnus-summary-position-cursor)))
7914
7915 (defun gnus-summary-goto-article (article &optional all-headers)
7916   "Fetch ARTICLE and display it if it exists.
7917 If ALL-HEADERS is non-nil, no header lines are hidden."
7918   (interactive
7919    (list
7920     (string-to-int
7921      (completing-read 
7922       "Article number: "
7923       (mapcar (lambda (headers) (list (int-to-string (header-number headers))))
7924               gnus-newsgroup-headers) 
7925       nil 'require-match))))
7926   (prog1
7927       (and (gnus-summary-goto-subject article)
7928            (gnus-summary-display-article article all-headers))
7929     (gnus-summary-position-cursor)))
7930
7931 (defun gnus-summary-goto-last-article ()
7932   "Go to the previously read article."
7933   (interactive)
7934   (prog1
7935       (and gnus-last-article
7936            (gnus-summary-goto-article gnus-last-article))
7937     (gnus-summary-position-cursor)))
7938
7939 (defun gnus-summary-pop-article (number)
7940   "Pop one article off the history and go to the previous.
7941 NUMBER articles will be popped off."
7942   (interactive "p")
7943   (let (to)
7944     (setq gnus-newsgroup-history
7945           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7946     (if to
7947         (gnus-summary-goto-article (car to))
7948       (error "Article history empty")))
7949   (gnus-summary-position-cursor))
7950
7951 ;; Summary article oriented commands
7952
7953 (defun gnus-summary-refer-parent-article (n)
7954   "Refer parent article N times.
7955 The difference between N and the number of articles fetched is returned."
7956   (interactive "p")
7957   (gnus-set-global-variables)
7958   (while 
7959       (and 
7960        (> n 0)
7961        (let ((ref (header-references (gnus-get-header-by-number
7962                                       (gnus-summary-article-number)))))
7963          (if (and ref (not (equal ref ""))
7964                   (string-match "<[^<>]*>[ \t]*$" ref))
7965              (gnus-summary-refer-article 
7966               (substring ref (match-beginning 0) (match-end 0)))
7967            (gnus-message 1 "No references in article %d"
7968                          (gnus-summary-article-number))
7969            nil)))
7970     (setq n (1- n)))
7971   (gnus-summary-position-cursor)
7972   n)
7973     
7974 (defun gnus-summary-refer-article (message-id)
7975   "Refer article specified by MESSAGE-ID.
7976 NOTE: This command only works with newsgroups that use real or simulated NNTP."
7977   (interactive "sMessage-ID: ")
7978   (if (or (not (stringp message-id))
7979           (zerop (length message-id)))
7980       ()
7981     ;; Construct the correct Message-ID if necessary.
7982     ;; Suggested by tale@pawl.rpi.edu.
7983     (or (string-match "^<" message-id)
7984         (setq message-id (concat "<" message-id)))
7985     (or (string-match ">$" message-id)
7986         (setq message-id (concat message-id ">")))
7987     (let ((header (car (gnus-gethash (downcase message-id)
7988                                      gnus-newsgroup-dependencies))))
7989       (if header
7990           (or (gnus-summary-goto-article (header-number header))
7991               ;; The header has been read, but the article had been
7992               ;; expunged, so we insert it again.
7993               (progn
7994                 (gnus-summary-insert-line
7995                  nil header 0 nil gnus-read-mark nil nil
7996                  (header-subject header))
7997                 (forward-line -1)
7998                 (header-number header)))
7999         (let ((gnus-override-method gnus-refer-article-method)
8000               (gnus-ancient-mark gnus-read-mark)
8001               (tmp-buf (get-buffer-create " *gnus refer"))
8002               (tmp-point (window-start
8003                           (get-buffer-window gnus-article-buffer)))
8004               number)
8005           (and gnus-refer-article-method
8006                (or (gnus-server-opened gnus-refer-article-method)
8007                    (gnus-open-server gnus-refer-article-method)))
8008           ;; Save the old article buffer.
8009           (save-excursion
8010             (set-buffer tmp-buf)
8011             (buffer-disable-undo (current-buffer))
8012             (insert-buffer-substring gnus-article-buffer))
8013           (prog1
8014               (if (gnus-article-prepare 
8015                    message-id nil (gnus-read-header message-id))
8016                   (progn
8017                     (setq number (header-number gnus-current-headers))
8018                     (gnus-rebuild-thread message-id)
8019                     (gnus-summary-goto-subject number)
8020                     (gnus-summary-recenter)
8021                     (gnus-article-set-window-start 
8022                      (cdr (assq number gnus-newsgroup-bookmarks)))
8023                     message-id)
8024                 ;; We restore the old article buffer.
8025                 (save-excursion
8026                   (set-buffer gnus-article-buffer)
8027                   (let ((buffer-read-only nil))
8028                     (insert-buffer-substring tmp-buf)
8029                     (and tmp-point
8030                          (set-window-start (get-buffer-window (current-buffer))
8031                                            tmp-point))))
8032                 nil)
8033             (kill-buffer tmp-buf)))))))
8034
8035 (defun gnus-summary-enter-digest-group ()
8036   "Enter a digest group based on the current article."
8037   (interactive)
8038   (gnus-set-global-variables)
8039   (gnus-summary-select-article)
8040   ;; We do not want a narrowed article.
8041   (gnus-summary-stop-page-breaking)
8042   (let ((name (format "%s-%d" 
8043                       (gnus-group-prefixed-name 
8044                        gnus-newsgroup-name (list 'nndoc "")) 
8045                       gnus-current-article))
8046         (ogroup gnus-newsgroup-name)
8047         (buf (current-buffer)))
8048     (if (gnus-group-read-ephemeral-group 
8049          name (list 'nndoc name
8050                     (list 'nndoc-address (get-buffer gnus-article-buffer))
8051                     '(nndoc-article-type digest))
8052          t)
8053         (setcdr (nthcdr 4 (nth 2 (gnus-gethash name gnus-newsrc-hashtb)))
8054                 (list (list (cons 'to-group ogroup))))
8055       (switch-to-buffer buf)
8056       (gnus-set-global-variables)
8057       (gnus-configure-windows 'summary)
8058       (gnus-message 3 "Article not a digest?"))))
8059
8060 (defun gnus-summary-isearch-article ()
8061   "Do incremental search forward on current article."
8062   (interactive)
8063   (gnus-set-global-variables)
8064   (gnus-summary-select-article)
8065   (gnus-eval-in-buffer-window 
8066    gnus-article-buffer (isearch-forward)))
8067
8068 (defun gnus-summary-search-article-forward (regexp &optional backward)
8069   "Search for an article containing REGEXP forward.
8070 If BACKWARD, search backward instead."
8071   (interactive
8072    (list (read-string
8073           (format "Search article %s (regexp%s): "
8074                   (if current-prefix-arg "backward" "forward")
8075                   (if gnus-last-search-regexp
8076                       (concat ", default " gnus-last-search-regexp)
8077                     "")))
8078          current-prefix-arg))
8079   (gnus-set-global-variables)
8080   (if (string-equal regexp "")
8081       (setq regexp (or gnus-last-search-regexp ""))
8082     (setq gnus-last-search-regexp regexp))
8083   (if (gnus-summary-search-article regexp backward)
8084       (gnus-article-set-window-start 
8085        (cdr (assq (gnus-summary-article-number) gnus-newsgroup-bookmarks)))
8086     (error "Search failed: \"%s\"" regexp)))
8087
8088 (defun gnus-summary-search-article-backward (regexp)
8089   "Search for an article containing REGEXP backward."
8090   (interactive
8091    (list (read-string
8092           (format "Search article backward (regexp%s): "
8093                   (if gnus-last-search-regexp
8094                       (concat ", default " gnus-last-search-regexp)
8095                     "")))))
8096   (gnus-summary-search-article-forward regexp 'backward))
8097
8098 (defun gnus-summary-search-article (regexp &optional backward)
8099   "Search for an article containing REGEXP.
8100 Optional argument BACKWARD means do search for backward.
8101 gnus-select-article-hook is not called during the search."
8102   (let ((gnus-select-article-hook nil)  ;Disable hook.
8103         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8104         (re-search
8105          (if backward
8106              (function re-search-backward) (function re-search-forward)))
8107         (found nil)
8108         (last nil))
8109     ;; Hidden thread subtrees must be searched for ,too.
8110     (gnus-summary-show-all-threads)
8111     (if (eobp) (forward-line -1))
8112     ;; First of all, search current article.
8113     ;; We don't want to read article again from NNTP server nor reset
8114     ;; current point.
8115     (gnus-summary-select-article)
8116     (gnus-message 9 "Searching article: %d..." gnus-current-article)
8117     (setq last gnus-current-article)
8118     (gnus-eval-in-buffer-window gnus-article-buffer
8119       (save-restriction
8120         (widen)
8121         ;; Begin search from current point.
8122         (setq found (funcall re-search regexp nil t))))
8123     ;; Then search next articles.
8124     (while (and (not found)
8125                 (gnus-summary-display-article 
8126                  (gnus-summary-search-subject backward nil nil)))
8127       (gnus-message 9 "Searching article: %d..." gnus-current-article)
8128       (gnus-eval-in-buffer-window gnus-article-buffer
8129         (save-restriction
8130           (widen)
8131           (goto-char (if backward (point-max) (point-min)))
8132           (setq found (funcall re-search regexp nil t)))))
8133     (message "")
8134     ;; Adjust article pointer.
8135     (or (eq last gnus-current-article)
8136         (setq gnus-last-article last))
8137     ;; Return T if found such article.
8138     found))
8139
8140 (defun gnus-summary-execute-command (header regexp command &optional backward)
8141   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8142 If HEADER is an empty string (or nil), the match is done on the entire
8143 article. If BACKWARD (the prefix) is non-nil, search backward instead."
8144   (interactive
8145    (list (let ((completion-ignore-case t))
8146            (completing-read 
8147             "Header name: "
8148             (mapcar (lambda (string) (list string))
8149                     '("Number" "Subject" "From" "Lines" "Date"
8150                       "Message-ID" "Xref" "References"))
8151             nil 'require-match))
8152          (read-string "Regexp: ")
8153          (read-key-sequence "Command: ")
8154          current-prefix-arg))
8155   (gnus-set-global-variables)
8156   ;; Hidden thread subtrees must be searched as well.
8157   (gnus-summary-show-all-threads)
8158   ;; We don't want to change current point nor window configuration.
8159   (save-excursion
8160     (save-window-excursion
8161       (gnus-message 6 "Executing %s..." (key-description command))
8162       ;; We'd like to execute COMMAND interactively so as to give arguments.
8163       (gnus-execute header regexp
8164                     (` (lambda ()
8165                          (call-interactively '(, (key-binding command)))))
8166                     backward)
8167       (gnus-message 6 "Executing %s...done" (key-description command)))))
8168
8169 (defun gnus-summary-beginning-of-article ()
8170   "Scroll the article back to the beginning."
8171   (interactive)
8172   (gnus-set-global-variables)
8173   (gnus-summary-select-article)
8174   (gnus-configure-windows 'article)
8175   (gnus-eval-in-buffer-window
8176    gnus-article-buffer
8177    (widen)
8178    (goto-char (point-min))
8179    (and gnus-break-pages (gnus-narrow-to-page))))
8180
8181 (defun gnus-summary-end-of-article ()
8182   "Scroll to the end of the article."
8183   (interactive)
8184   (gnus-set-global-variables)
8185   (gnus-summary-select-article)
8186   (gnus-configure-windows 'article)
8187   (gnus-eval-in-buffer-window 
8188    gnus-article-buffer
8189    (widen)
8190    (goto-char (point-max))
8191    (and gnus-break-pages (gnus-narrow-to-page))))
8192
8193 (defun gnus-summary-show-article (no-refetch)
8194   "Force re-fetching of the current article.
8195 If the prefix argument NO-REFETCH is non-nil, no actual refetch will
8196 be performed.  The current article will simply be redisplayed."
8197   (interactive "P")
8198   (gnus-set-global-variables)
8199   (if (not no-refetch)
8200       (gnus-summary-select-article gnus-have-all-headers t)
8201     (or gnus-current-article
8202         (error "There is no current article"))
8203     (gnus-summary-goto-subject gnus-current-article)
8204     (gnus-configure-windows 'article)
8205     (gnus-summary-position-cursor)))
8206
8207 (defun gnus-summary-verbose-headers (arg)
8208   "Toggle permanent full header display.
8209 If ARG is a positive number, turn header display on.
8210 If ARG is a negative number, turn header display off."
8211   (interactive "P")
8212   (gnus-set-global-variables)
8213   (gnus-summary-toggle-header arg)
8214   (setq gnus-have-all-headers
8215         (cond ((or (not (numberp arg))
8216                    (zerop arg))
8217                (not gnus-have-all-headers))
8218               ((natnump arg)
8219                t))))
8220
8221 (defun gnus-summary-toggle-header (arg)
8222   "Show the headers if they are hidden, or hide them if they are shown.
8223 If ARG is a positive number, show the entire header.
8224 If ARG is a negative number, hide the unwanted header lines."
8225   (interactive "P")
8226   (gnus-set-global-variables)
8227   (save-excursion
8228     (set-buffer gnus-article-buffer)
8229     (let ((buffer-read-only nil))
8230       (if (numberp arg) 
8231           (if (> arg 0) (remove-text-properties (point-min) (point-max) 
8232                                                 gnus-hidden-properties)
8233             (if (< arg 0) (run-hooks 'gnus-article-display-hook)))
8234         (if (text-property-any (point-min) (point-max) 'invisible t)
8235             (remove-text-properties (point-min) (point-max)
8236                                     gnus-hidden-properties)
8237           (let ((gnus-have-all-headers nil))
8238             (run-hooks 'gnus-article-display-hook))))
8239       (set-window-point (get-buffer-window (current-buffer)) (point-min)))))
8240
8241 (defun gnus-summary-show-all-headers ()
8242   "Make all header lines visible."
8243   (interactive)
8244   (gnus-set-global-variables)
8245   (gnus-article-show-all-headers))
8246
8247 (defun gnus-summary-toggle-mime (arg)
8248   "Toggle MIME processing.
8249 If ARG is a positive number, turn MIME processing on."
8250   (interactive "P")
8251   (gnus-set-global-variables)
8252   (setq gnus-show-mime
8253         (if (null arg) (not gnus-show-mime)
8254           (> (prefix-numeric-value arg) 0)))
8255   (gnus-summary-select-article t 'force))
8256
8257 (defun gnus-summary-caesar-message (rotnum)
8258   "Caesar rotates all letters of current message by 13/47 places.
8259 With prefix arg, specifies the number of places to rotate each letter forward.
8260 Caesar rotates Japanese letters by 47 places in any case."
8261   (interactive "P")
8262   (gnus-set-global-variables)
8263   (gnus-summary-select-article)
8264   (let ((mail-header-separator "")) ; !!! Is this necessary?
8265     (gnus-overload-functions)
8266     (gnus-eval-in-buffer-window 
8267      gnus-article-buffer
8268      (save-restriction
8269        (widen)
8270        ;; We don't want to jump to the beginning of the message.
8271        ;; `save-excursion' does not do its job.
8272        (move-to-window-line 0)
8273        (let ((last (point)))
8274          (news-caesar-buffer-body rotnum)
8275          (goto-char last)
8276          (recenter 0))))))
8277
8278 (defun gnus-summary-stop-page-breaking ()
8279   "Stop page breaking in the current article."
8280   (interactive)
8281   (gnus-set-global-variables)
8282   (gnus-summary-select-article)
8283   (gnus-eval-in-buffer-window gnus-article-buffer (widen)))
8284
8285 ;; Suggested by Brian Edmonds <bedmonds@prodigy.bc.ca>.
8286
8287 (defun gnus-summary-move-article (n &optional to-newsgroup select-method)
8288   "Move the current article to a different newsgroup.
8289 If N is a positive number, move the N next articles.
8290 If N is a negative number, move the N previous articles.
8291 If N is nil and any articles have been marked with the process mark,
8292 move those articles instead.
8293 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to. 
8294 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
8295 re-spool using this method.
8296 For this function to work, both the current newsgroup and the
8297 newsgroup that you want to move to have to support the `request-move'
8298 and `request-accept' functions. (Ie. mail newsgroups at present.)"
8299   (interactive "P")
8300   (gnus-set-global-variables)
8301   (or (gnus-check-backend-function 'request-move-article gnus-newsgroup-name)
8302       (error "The current newsgroup does not support article moving"))
8303   (let ((articles (gnus-summary-work-articles n))
8304         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
8305         art-group to-method)
8306     (if (and (not to-newsgroup) (not select-method))
8307         (setq to-newsgroup
8308               (completing-read 
8309                (format "Where do you want to move %s? %s"
8310                        (if (> (length articles) 1)
8311                            (format "these %d articles" (length articles))
8312                          "this article")
8313                        (if gnus-current-move-group
8314                            (format "(%s default) " gnus-current-move-group)
8315                          ""))
8316                gnus-active-hashtb nil nil prefix)))
8317     (if to-newsgroup
8318         (progn
8319           (if (or (string= to-newsgroup "") (string= to-newsgroup prefix))
8320               (setq to-newsgroup (or gnus-current-move-group "")))
8321           (or (gnus-gethash to-newsgroup gnus-active-hashtb)
8322               (gnus-activate-newsgroup to-newsgroup)
8323               (error "No such group: %s" to-newsgroup))
8324           (setq gnus-current-move-group to-newsgroup)))
8325     (setq to-method (or select-method (gnus-find-method-for-group
8326                                        to-newsgroup)))
8327     (or (gnus-check-backend-function 'request-accept-article (car to-method))
8328         (error "%s does not support article copying" (car to-method)))
8329     (or (gnus-server-opened to-method)
8330         (gnus-open-server to-method)
8331         (error "Can't open server %s" (car to-method)))
8332     (gnus-message 6 "Moving to %s: %s..." 
8333                   (or select-method to-newsgroup) articles)
8334     (while articles
8335       (if (setq art-group
8336                 (gnus-request-move-article 
8337                  (car articles)                   ; Article to move
8338                  gnus-newsgroup-name              ; From newsgrouo
8339                  (nth 1 (gnus-find-method-for-group 
8340                          gnus-newsgroup-name))    ; Server
8341                  (list 'gnus-request-accept-article 
8342                        (if select-method
8343                            (list 'quote select-method)
8344                          to-newsgroup)
8345                        (not (cdr articles)))     ; Accept form
8346                  (not (cdr articles))))          ; Only save nov last time
8347           (let* ((buffer-read-only nil)
8348                  (entry 
8349                   (or
8350                    (gnus-gethash (car art-group) gnus-newsrc-hashtb)
8351                    (gnus-gethash 
8352                     (gnus-group-prefixed-name 
8353                      (car art-group) 
8354                      (if select-method (list select-method "")
8355                        (gnus-find-method-for-group to-newsgroup)))
8356                     gnus-newsrc-hashtb)))
8357                  (info (nth 2 entry))
8358                  (article (car articles)))
8359             (gnus-summary-goto-subject article)
8360             (beginning-of-line)
8361             (delete-region (point)
8362                            (progn (forward-line 1) (point)))
8363             (if (not (memq article gnus-newsgroup-unreads))
8364                 (setcar (cdr (cdr info))
8365                         (gnus-add-to-range (nth 2 info) 
8366                                            (list (cdr art-group)))))
8367             ;; Copy any marks over to the new group.
8368             (let ((marks '((tick . gnus-newsgroup-marked)
8369                            (dormant . gnus-newsgroup-dormant)
8370                            (expire . gnus-newsgroup-expirable)
8371                            (bookmark . gnus-newsgroup-bookmarks)
8372                         ;   (score . gnus-newsgroup-scored)
8373                            (reply . gnus-newsgroup-replied)))
8374                   (to-article (cdr art-group)))
8375               (while marks
8376                 (if (memq article (symbol-value (cdr (car marks))))
8377                     (gnus-add-marked-articles 
8378                      (car info) (car (car marks)) (list to-article) info))
8379                 (setq marks (cdr marks))))
8380             (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8381             (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8382             (setq gnus-newsgroup-dormant
8383                   (delq article gnus-newsgroup-dormant)))
8384         (gnus-message 1 "Couldn't move article %s" (car articles)))
8385       (gnus-summary-remove-process-mark (car articles))
8386       (setq articles (cdr articles)))))
8387
8388 (defun gnus-summary-respool-article (n &optional respool-method)
8389   "Respool the current article.
8390 The article will be squeezed through the mail spooling process again,
8391 which means that it will be put in some mail newsgroup or other
8392 depending on `nnmail-split-methods'.
8393 If N is a positive number, respool the N next articles.
8394 If N is a negative number, respool the N previous articles.
8395 If N is nil and any articles have been marked with the process mark,
8396 respool those articles instead.
8397
8398 Respooling can be done both from mail groups and \"real\" newsgroups.
8399 In the former case, the articles in question will be moved from the
8400 current group into whatever groups they are destined to.  In the
8401 latter case, they will be copied into the relevant groups."
8402   (interactive "P")
8403   (gnus-set-global-variables)
8404   (let ((respool-methods (gnus-methods-using 'respool))
8405         (methname 
8406          (symbol-name (car (gnus-find-method-for-group gnus-newsgroup-name)))))
8407     (or respool-method
8408         (setq respool-method
8409               (completing-read
8410                "What method do you want to use when respooling? "
8411                respool-methods nil t methname)))
8412     (or (string= respool-method "")
8413         (if (assoc (symbol-name
8414                     (car (gnus-find-method-for-group gnus-newsgroup-name)))
8415                    respool-methods)
8416             (gnus-summary-move-article n nil (intern respool-method))
8417           (gnus-summary-copy-article n nil (intern respool-method))))))
8418
8419 ;; Suggested by gregj@unidata.com (Gregory J. Grubbs).
8420 (defun gnus-summary-copy-article (n &optional to-newsgroup select-method)
8421   "Move the current article to a different newsgroup.
8422 If N is a positive number, move the N next articles.
8423 If N is a negative number, move the N previous articles.
8424 If N is nil and any articles have been marked with the process mark,
8425 move those articles instead.
8426 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to. 
8427 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
8428 re-spool using this method.
8429 For this function to work, the newsgroup that you want to move to have
8430 to support the `request-move' and `request-accept'
8431 functions. (Ie. mail newsgroups at present.)"
8432   (interactive "P")
8433   (gnus-set-global-variables)
8434   (let ((articles (gnus-summary-work-articles n))
8435         (copy-buf (get-buffer-create "*copy work*"))
8436         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
8437         art-group to-method)
8438     (buffer-disable-undo copy-buf)
8439     (if (and (not to-newsgroup) (not select-method))
8440         (setq to-newsgroup
8441               (completing-read 
8442                (format "Where do you want to copy %s? %s"
8443                        (if (> (length articles) 1)
8444                            (format "these %d articles" (length articles))
8445                          "this article")
8446                        (if gnus-current-move-group
8447                            (format "(%s default) " gnus-current-move-group)
8448                          ""))
8449                gnus-active-hashtb nil nil prefix)))
8450     (if to-newsgroup
8451         (progn
8452           (if (or (string= to-newsgroup "") (string= to-newsgroup prefix))
8453               (setq to-newsgroup (or gnus-current-move-group "")))
8454           (or (gnus-gethash to-newsgroup gnus-active-hashtb)
8455               (gnus-activate-newsgroup to-newsgroup)
8456               (error "No such group: %s" to-newsgroup))
8457           (setq gnus-current-move-group to-newsgroup)))
8458     (setq to-method (or select-method (gnus-find-method-for-group
8459                                        to-newsgroup)))
8460     (or (gnus-check-backend-function 'request-accept-article (car to-method))
8461         (error "%s does not support article copying" (car to-method)))
8462     (or (gnus-server-opened to-method)
8463         (gnus-open-server to-method)
8464         (error "Can't open server %s" (car to-method)))
8465     (gnus-message 6 "Copying to %s: %s..." 
8466                   (or select-method to-newsgroup) articles)
8467     (while articles
8468       (if (setq art-group
8469                 (save-excursion
8470                   (set-buffer copy-buf)
8471                   (gnus-request-article-this-buffer
8472                    (car articles) gnus-newsgroup-name)
8473                   (gnus-request-accept-article
8474                    (if select-method (quote select-method) to-newsgroup)
8475                    (not (cdr articles)))))
8476           (let* ((entry 
8477                   (or
8478                    (gnus-gethash (car art-group) gnus-newsrc-hashtb)
8479                    (gnus-gethash 
8480                     (gnus-group-prefixed-name 
8481                      (car art-group) 
8482                      (if select-method (list select-method "")
8483                        (gnus-find-method-for-group to-newsgroup)))
8484                     gnus-newsrc-hashtb)))
8485                  (info (nth 2 entry))
8486                  (article (car articles)))
8487             (if (not (memq article gnus-newsgroup-unreads))
8488                 (setcar (cdr (cdr info))
8489                         (gnus-add-to-range (nth 2 info) 
8490                                            (list (cdr art-group)))))
8491             ;; Copy any marks over to the new group.
8492             (let ((marks '((tick . gnus-newsgroup-marked)
8493                            (dormant . gnus-newsgroup-dormant)
8494                            (expire . gnus-newsgroup-expirable)
8495                            (bookmark . gnus-newsgroup-bookmarks)
8496                         ;   (score . gnus-newsgroup-scored)
8497                            (reply . gnus-newsgroup-replied)))
8498                   (to-article (cdr art-group)))
8499               (while marks
8500                 (if (memq article (symbol-value (cdr (car marks))))
8501                     (gnus-add-marked-articles 
8502                      (car info) (car (car marks)) (list to-article) info))
8503                 (setq marks (cdr marks)))))
8504         (gnus-message 1 "Couldn't copy article %s" (car articles)))
8505       (gnus-summary-remove-process-mark (car articles))
8506       (setq articles (cdr articles)))
8507     (kill-buffer copy-buf)))
8508
8509 (defun gnus-summary-import-article (file)
8510   "Import a random file into a mail newsgroup."
8511   (interactive "fImport file: ")
8512   (let ((group gnus-newsgroup-name)
8513         atts)
8514     (or (gnus-check-backend-function 'request-accept-article group)
8515         (error "%s does not support article importing" group))
8516     (or (file-readable-p file)
8517         (not (file-regular-p file))
8518         (error "Can't read %s" file))
8519     (save-excursion
8520       (set-buffer (get-buffer-create " *import file*"))
8521       (buffer-disable-undo (current-buffer))
8522       (erase-buffer)
8523       (insert-file-contents file)
8524       (goto-char (point-min))
8525       (if (nnheader-article-p)
8526           ()
8527         (setq atts (file-attributes file))
8528         (insert "From: " (read-string "From: ") "\n"
8529                 "Subject: " (read-string "Subject: ") "\n"
8530                 "Date: " (current-time-string (nth 5 atts)) "\n"
8531                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8532       (gnus-request-accept-article group t)
8533       (kill-buffer (current-buffer)))))
8534
8535 (defun gnus-summary-expire-articles ()
8536   "Expire all articles that are marked as expirable in the current group."
8537   (interactive)
8538   (if (and gnus-newsgroup-expirable
8539            (gnus-check-backend-function 
8540             'request-expire-articles gnus-newsgroup-name))
8541       (let ((expirable gnus-newsgroup-expirable))
8542         ;; The list of articles that weren't expired is returned.
8543         (setq gnus-newsgroup-expirable 
8544               (gnus-request-expire-articles gnus-newsgroup-expirable
8545                                             gnus-newsgroup-name))
8546         ;; We go through the old list of expirable, and mark all
8547         ;; really expired articles as non-existent.
8548         (while expirable
8549           (or (memq (car expirable) gnus-newsgroup-expirable)
8550               (gnus-summary-mark-as-read (car expirable) gnus-canceled-mark))
8551           (setq expirable (cdr expirable))))))
8552
8553 (defun gnus-summary-expire-articles-now ()
8554   "Expunge all expirable articles in the current group.
8555 This means that *all* articles that are marked as expirable will be
8556 deleted forever, right now."
8557   (interactive)
8558   (or gnus-expert-user
8559       (gnus-y-or-n-p
8560        "Are you really, really, really sure you want to expunge? ")
8561       (error "Phew!"))
8562   (let ((nnmail-expiry-wait 0)
8563         (nnmail-expiry-wait-function nil))
8564     (gnus-summary-expire-articles)))
8565
8566 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
8567 (defun gnus-summary-delete-article (n)
8568   "Delete the N next (mail) articles.
8569 This command actually deletes articles. This is not a marking
8570 command. The article will disappear forever from you life, never to
8571 return. 
8572 If N is negative, delete backwards.
8573 If N is nil and articles have been marked with the process mark,
8574 delete these instead."
8575   (interactive "P")
8576   (or (gnus-check-backend-function 'request-expire-articles 
8577                                    gnus-newsgroup-name)
8578       (error "The current newsgroup does not support article deletion."))
8579   ;; Compute the list of articles to delete.
8580   (let ((articles (gnus-summary-work-articles n))
8581         not-deleted)
8582     (if (and gnus-novice-user
8583              (not (gnus-y-or-n-p 
8584                    (format "Do you really want to delete %s forever? "
8585                            (if (> (length articles) 1) "these articles"
8586                              "this article")))))
8587         ()
8588       ;; Delete the articles.
8589       (setq not-deleted (gnus-request-expire-articles 
8590                          articles gnus-newsgroup-name 'force))
8591       (while articles
8592         (gnus-summary-remove-process-mark (car articles))       
8593         ;; The backend might not have been able to delete the article
8594         ;; after all.  
8595         (or (memq (car articles) not-deleted)
8596             (gnus-summary-mark-as-read (car articles) gnus-canceled-mark))
8597         (setq articles (cdr articles))))
8598     (gnus-summary-position-cursor)
8599     not-deleted))
8600
8601 (defun gnus-summary-edit-article ()
8602   "Enter into a buffer and edit the current article.
8603 This will have permanent effect only in mail groups."
8604   (interactive)
8605   (or (gnus-check-backend-function 
8606        'request-replace-article gnus-newsgroup-name)
8607       (error "The current newsgroup does not support article editing."))
8608   (gnus-summary-select-article t)
8609   (gnus-configure-windows 'article)
8610   (select-window (get-buffer-window gnus-article-buffer))
8611   (gnus-message 6 "C-c C-c to end edits")
8612   (setq buffer-read-only nil)
8613   (text-mode)
8614   (use-local-map (copy-keymap (current-local-map)))
8615   (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
8616   (buffer-enable-undo)
8617   (goto-char (point-min))
8618   (search-forward "\n\n" nil t))
8619
8620 (defun gnus-summary-edit-article-done ()
8621   "Make edits to the current article permanent."
8622   (interactive)
8623   (if (not (gnus-request-replace-article 
8624             (cdr gnus-article-current) (car gnus-article-current) 
8625             (current-buffer)))
8626       (error "Couldn't replace article.")
8627     (gnus-article-mode)
8628     (use-local-map gnus-article-mode-map)
8629     (setq buffer-read-only t)
8630     (buffer-disable-undo (current-buffer))
8631     (pop-to-buffer gnus-summary-buffer)))      
8632
8633 (defun gnus-summary-fancy-query ()
8634   "Query where the fancy respool algorithm would put this article."
8635   (interactive)
8636   (gnus-summary-select-article)
8637   (save-excursion
8638     (set-buffer gnus-article-buffer)
8639     (save-restriction
8640       (goto-char (point-min))
8641       (search-forward "\n\n")
8642       (narrow-to-region (point-min) (point))
8643       (pp-eval-expression (list 'quote (nnmail-split-fancy))))))
8644
8645 ;; Summary score commands.
8646
8647 ;; Suggested by boubaker@cenatls.cena.dgac.fr.
8648
8649 (defun gnus-summary-raise-score (n)
8650   "Raise the score of the current article by N."
8651   (interactive "p")
8652   (gnus-summary-set-score (+ (gnus-summary-article-score) n)))
8653
8654 (defun gnus-summary-lower-score (n)
8655   "Lower the score of the current article by N."
8656   (interactive "p")
8657   (gnus-summary-raise-score (- n)))
8658
8659 (defun gnus-summary-set-score (n)
8660   "Set the score of the current article to N."
8661   (interactive "p")
8662   ;; Skip dummy header line.
8663   (save-excursion
8664     (gnus-summary-show-thread)
8665     (if (eq (gnus-summary-article-mark) gnus-dummy-mark)
8666         (forward-line 1))
8667     (let ((buffer-read-only nil))
8668       ;; Set score.
8669       (gnus-summary-update-mark
8670        (if (= n (or gnus-summary-default-score 0)) ? 
8671          (if (< n (or gnus-summary-default-score 0)) 
8672              gnus-score-below-mark gnus-score-over-mark)) 'score))
8673     (let* ((article (gnus-summary-article-number))
8674            (score (assq article gnus-newsgroup-scored)))
8675       (if score (setcdr score n)
8676         (setq gnus-newsgroup-scored 
8677               (cons (cons article n) gnus-newsgroup-scored))))
8678     (gnus-summary-update-line)))
8679
8680 (defun gnus-summary-current-score ()
8681   "Return the score of the current article."
8682   (interactive)
8683   (message "%s" (gnus-summary-article-score)))
8684
8685 ;; Summary marking commands.
8686
8687 (defun gnus-summary-raise-same-subject-and-select (score)
8688   "Raise articles which has the same subject with SCORE and select the next."
8689   (interactive "p")
8690   (let ((subject (gnus-summary-subject-string)))
8691     (gnus-summary-raise-score score)
8692     (while (gnus-summary-search-subject nil nil subject)
8693       (gnus-summary-raise-score score))
8694     (gnus-summary-next-article t)))
8695
8696 (defun gnus-summary-raise-same-subject (score)
8697   "Raise articles which has the same subject with SCORE."
8698   (interactive "p")
8699   (let ((subject (gnus-summary-subject-string)))
8700     (gnus-summary-raise-score score)
8701     (while (gnus-summary-search-subject nil nil subject)
8702       (gnus-summary-raise-score score))
8703     (gnus-summary-next-subject 1 t)))
8704
8705 (defun gnus-score-default (level)
8706   (if level (prefix-numeric-value level) 
8707     gnus-score-interactive-default-score))
8708
8709 (defun gnus-summary-raise-thread (score)
8710   "Raise the score of the articles in the current thread with SCORE."
8711   (interactive "P")
8712   (setq score (1- (gnus-score-default score)))
8713   (let (e)
8714     (save-excursion
8715       (let ((level (gnus-summary-thread-level)))
8716         (gnus-summary-raise-score score)
8717         (while (and (zerop (gnus-summary-next-subject 1 nil t))
8718                     (> (gnus-summary-thread-level) level))
8719           (gnus-summary-raise-score score))
8720         (setq e (point))))
8721     (let ((gnus-summary-check-current t))
8722       (or (zerop (gnus-summary-next-subject 1 t))
8723           (goto-char e))))
8724   (gnus-summary-recenter)
8725   (gnus-summary-position-cursor)
8726   (gnus-set-mode-line 'summary))
8727
8728 (defun gnus-summary-lower-same-subject-and-select (score)
8729   "Raise articles which has the same subject with SCORE and select the next."
8730   (interactive "p")
8731   (gnus-summary-raise-same-subject-and-select (- score)))
8732
8733 (defun gnus-summary-lower-same-subject (score)
8734   "Raise articles which has the same subject with SCORE."
8735   (interactive "p")
8736   (gnus-summary-raise-same-subject (- score)))
8737
8738 (defun gnus-summary-lower-thread (score)
8739   "Lower score of articles in the current thread with SCORE."
8740   (interactive "P")
8741   (gnus-summary-raise-thread (- (1- (gnus-score-default score)))))
8742
8743 (defun gnus-summary-kill-same-subject-and-select (unmark)
8744   "Mark articles which has the same subject as read, and then select the next.
8745 If UNMARK is positive, remove any kind of mark.
8746 If UNMARK is negative, tick articles."
8747   (interactive "P")
8748   (if unmark
8749       (setq unmark (prefix-numeric-value unmark)))
8750   (let ((count
8751          (gnus-summary-mark-same-subject
8752           (gnus-summary-subject-string) unmark)))
8753     ;; Select next unread article. If auto-select-same mode, should
8754     ;; select the first unread article.
8755     (gnus-summary-next-article t (and gnus-auto-select-same
8756                                       (gnus-summary-subject-string)))
8757     (gnus-message 7 "%d articles are marked as %s"
8758                   count (if unmark "unread" "read"))))
8759
8760 (defun gnus-summary-kill-same-subject (unmark)
8761   "Mark articles which has the same subject as read. 
8762 If UNMARK is positive, remove any kind of mark.
8763 If UNMARK is negative, tick articles."
8764   (interactive "P")
8765   (if unmark
8766       (setq unmark (prefix-numeric-value unmark)))
8767   (let ((count
8768          (gnus-summary-mark-same-subject
8769           (gnus-summary-subject-string) unmark)))
8770     ;; If marked as read, go to next unread subject.
8771     (if (null unmark)
8772         ;; Go to next unread subject.
8773         (gnus-summary-next-subject 1 t))
8774     (gnus-message 7 "%d articles are marked as %s"
8775                   count (if unmark "unread" "read"))))
8776
8777 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8778   "Mark articles with same SUBJECT as read, and return marked number.
8779 If optional argument UNMARK is positive, remove any kinds of marks.
8780 If optional argument UNMARK is negative, mark articles as unread instead."
8781   (let ((count 1))
8782     (save-excursion
8783       (cond ((null unmark)
8784              (gnus-summary-mark-as-read nil gnus-killed-mark))
8785             ((> unmark 0)
8786              (gnus-summary-tick-article nil t))
8787             (t
8788              (gnus-summary-tick-article)))
8789       (while (and subject
8790                   (gnus-summary-search-forward nil subject))
8791         (cond ((null unmark)
8792                (gnus-summary-mark-as-read nil gnus-killed-mark))
8793               ((> unmark 0)
8794                (gnus-summary-tick-article nil t))
8795               (t
8796                (gnus-summary-tick-article)))
8797         (setq count (1+ count))))
8798     ;; Hide killed thread subtrees.  Does not work properly always.
8799     ;;(and (null unmark)
8800     ;;     gnus-thread-hide-killed
8801     ;;     (gnus-summary-hide-thread))
8802     ;; Return number of articles marked as read.
8803     count))
8804
8805 (defun gnus-summary-mark-as-processable (n &optional unmark)
8806   "Set the process mark on the next N articles.
8807 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8808 the process mark instead.  The difference between N and the actual
8809 number of articles marked is returned."
8810   (interactive "p")
8811   (let ((backward (< n 0))
8812         (n (abs n)))
8813   (while (and 
8814           (> n 0)
8815           (if unmark
8816               (gnus-summary-remove-process-mark (gnus-summary-article-number))
8817             (gnus-summary-set-process-mark (gnus-summary-article-number)))
8818           (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8819     (setq n (1- n)))
8820   (if (/= 0 n) (gnus-message 7 "No more articles"))
8821   (gnus-summary-recenter)
8822   (gnus-summary-position-cursor)
8823   n))
8824
8825 (defun gnus-summary-unmark-as-processable (n)
8826   "Remove the process mark from the next N articles.
8827 If N is negative, mark backward instead.  The difference between N and
8828 the actual number of articles marked is returned."
8829   (interactive "p")
8830   (gnus-summary-mark-as-processable n t))
8831
8832 (defun gnus-summary-unmark-all-processable ()
8833   "Remove the process mark from all articles."
8834   (interactive)
8835   (save-excursion
8836     (while gnus-newsgroup-processable
8837       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8838   (gnus-summary-position-cursor))
8839
8840 (defun gnus-summary-mark-as-expirable (n)
8841   "Mark N articles forward as expirable.
8842 If N is negative, mark backward instead. The difference between N and
8843 the actual number of articles marked is returned."
8844   (interactive "p")
8845   (gnus-summary-mark-forward n gnus-expirable-mark))
8846
8847 (defun gnus-summary-mark-article-as-replied (article)
8848   "Mark ARTICLE replied and update the summary line."
8849   (setq gnus-newsgroup-replied (cons article gnus-newsgroup-replied))
8850   (let ((buffer-read-only nil))
8851     (if (gnus-summary-goto-subject article)
8852         (progn
8853           (gnus-summary-update-mark gnus-replied-mark 'replied)
8854           t))))
8855
8856 (defun gnus-summary-set-bookmark (article)
8857   "Set a bookmark in current article."
8858   (interactive (list (gnus-summary-article-number)))
8859   (if (or (not (get-buffer gnus-article-buffer))
8860           (not gnus-current-article)
8861           (not gnus-article-current)
8862           (not (equal gnus-newsgroup-name (car gnus-article-current))))
8863       (error "No current article selected"))
8864   ;; Remove old bookmark, if one exists.
8865   (let ((old (assq article gnus-newsgroup-bookmarks)))
8866     (if old (setq gnus-newsgroup-bookmarks 
8867                   (delq old gnus-newsgroup-bookmarks))))
8868   ;; Set the new bookmark, which is on the form 
8869   ;; (article-number . line-number-in-body).
8870   (setq gnus-newsgroup-bookmarks 
8871         (cons 
8872          (cons article 
8873                (save-excursion
8874                  (set-buffer gnus-article-buffer)
8875                  (count-lines
8876                   (min (point)
8877                        (save-excursion
8878                          (goto-char (point-min))
8879                          (search-forward "\n\n" nil t)
8880                          (point)))
8881                   (point))))
8882          gnus-newsgroup-bookmarks))
8883   (gnus-message 6 "A bookmark has been added to the current article."))
8884
8885 (defun gnus-summary-remove-bookmark (article)
8886   "Remove the bookmark from the current article."
8887   (interactive (list (gnus-summary-article-number)))
8888   ;; Remove old bookmark, if one exists.
8889   (let ((old (assq article gnus-newsgroup-bookmarks)))
8890     (if old 
8891         (progn
8892           (setq gnus-newsgroup-bookmarks 
8893                 (delq old gnus-newsgroup-bookmarks))
8894           (gnus-message 6 "Removed bookmark."))
8895       (gnus-message 6 "No bookmark in current article."))))
8896
8897 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8898 (defun gnus-summary-mark-as-dormant (n)
8899   "Mark N articles forward as dormant.
8900 If N is negative, mark backward instead.  The difference between N and
8901 the actual number of articles marked is returned."
8902   (interactive "p")
8903   (gnus-summary-mark-forward n gnus-dormant-mark))
8904
8905 (defun gnus-summary-set-process-mark (article)
8906   "Set the process mark on ARTICLE and update the summary line."
8907   (setq gnus-newsgroup-processable (cons article gnus-newsgroup-processable))
8908   (let ((buffer-read-only nil))
8909     (if (gnus-summary-goto-subject article)
8910         (progn
8911           (gnus-summary-show-thread)
8912           (and (eq (gnus-summary-article-mark) gnus-dummy-mark)
8913                (forward-line 1))
8914           (gnus-summary-update-mark gnus-process-mark 'replied)
8915           t))))
8916
8917 (defun gnus-summary-remove-process-mark (article)
8918   "Remove the process mark from ARTICLE and update the summary line."
8919   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8920   (let ((buffer-read-only nil))
8921     (if (gnus-summary-goto-subject article)
8922         (progn
8923           (gnus-summary-show-thread)
8924           (and (eq (gnus-summary-article-mark) gnus-dummy-mark)
8925                (forward-line 1))
8926           (gnus-summary-update-mark ?  'replied)
8927           (if (memq article gnus-newsgroup-replied) 
8928               (gnus-summary-update-mark gnus-replied-mark 'replied))
8929           t))))
8930
8931 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8932   "Mark N articles as read forwards.
8933 If N is negative, mark backwards instead.
8934 Mark with MARK. If MARK is ? , ?! or ??, articles will be
8935 marked as unread. 
8936 The difference between N and the actual number of articles marked is
8937 returned."
8938   (interactive "p")
8939   (gnus-set-global-variables)
8940   (let ((backward (< n 0))
8941         (n (abs n))
8942         (mark (or mark gnus-del-mark)))
8943   (while (and (> n 0)
8944               (gnus-summary-mark-article nil mark no-expire)
8945               (zerop (gnus-summary-next-subject 
8946                       (if backward -1 1) gnus-summary-goto-unread t)))
8947     (setq n (1- n)))
8948   (if (/= 0 n) (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8949   (gnus-summary-recenter)
8950   (gnus-summary-position-cursor)
8951   (gnus-set-mode-line 'summary)
8952   n))
8953
8954 (defun gnus-summary-mark-article (&optional article mark no-expire)
8955   "Mark ARTICLE with MARK.
8956 MARK can be any character.
8957 Five MARK strings are reserved: ?  (unread), 
8958 ?! (ticked), ?? (dormant), ?D (read), ?E (expirable).
8959 If MARK is nil, then the default character ?D is used.
8960 If ARTICLE is nil, then the article on the current line will be
8961 marked." 
8962   ;; If no mark is given, then we check auto-expiring.
8963   (and (not no-expire)
8964        gnus-newsgroup-auto-expire 
8965        (or (not mark)
8966            (and (numberp mark) 
8967                 (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
8968                     (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
8969                     (= mark gnus-read-mark))))
8970        (setq mark gnus-expirable-mark))
8971   (let* ((mark (or (and (stringp mark) (aref mark 0)) mark gnus-del-mark))
8972          (article (or article (gnus-summary-article-number))))
8973     (or article (error "No article on current line"))
8974     (if (or (= mark gnus-unread-mark) 
8975             (= mark gnus-ticked-mark) 
8976             (= mark gnus-dormant-mark))
8977         (gnus-mark-article-as-unread article mark)
8978       (gnus-mark-article-as-read article mark))
8979
8980     ;; See whether the article is to be put in the cache.
8981     (and gnus-use-cache
8982          (save-excursion
8983            (gnus-cache-possibly-enter-article 
8984             gnus-newsgroup-name article 
8985             (gnus-get-header-by-number article)
8986             (= mark gnus-ticked-mark)
8987             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8988
8989     (if (gnus-summary-goto-subject article)
8990         (let ((buffer-read-only nil))
8991           (gnus-summary-show-thread)
8992           (and (eq (gnus-summary-article-mark) gnus-dummy-mark)
8993                (forward-line 1))
8994           ;; Fix the mark.
8995           (gnus-summary-update-mark mark 'unread)
8996           t))))
8997
8998 (defun gnus-summary-update-mark (mark type)
8999   (beginning-of-line)
9000   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
9001         plist)
9002     (if (not forward)
9003         ()
9004       (forward-char forward)
9005       (setq plist (text-properties-at (point)))
9006       (delete-char 1)
9007       (insert mark)
9008       (and plist (add-text-properties (1- (point)) (point) plist))
9009       (and (eq type 'unread)
9010            (add-text-properties (1- (point)) (point) (list 'gnus-mark mark)))
9011       (gnus-summary-update-line (eq mark gnus-unread-mark)))))
9012   
9013 (defun gnus-mark-article-as-read (article &optional mark)
9014   "Enter ARTICLE in the pertinent lists and remove it from others."
9015   ;; Make the article expirable.
9016   (let ((mark (or (and (stringp mark) (aref mark 0)) mark gnus-del-mark)))
9017     (if (= mark gnus-expirable-mark)
9018         (setq gnus-newsgroup-expirable (cons article gnus-newsgroup-expirable))
9019       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
9020     ;; Remove from unread and marked lists.
9021     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9022     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9023     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9024     ;; Possibly remove from cache, if that is used. 
9025     (and gnus-use-cache (gnus-cache-enter-remove-article article))))
9026
9027 (defun gnus-mark-article-as-unread (article &optional mark)
9028   "Enter ARTICLE in the pertinent lists and remove it from others."
9029   (let ((mark (or (and (stringp mark) (aref mark 0)) mark gnus-ticked-mark)))
9030     ;; Add to unread list.
9031     (or (memq article gnus-newsgroup-unreads)
9032         (setq gnus-newsgroup-unreads (cons article gnus-newsgroup-unreads)))
9033     ;; If CLEAR-MARK is non-nil, the article must be removed from mark
9034     ;; lists.  Otherwise, it must be added to the list.
9035     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9036     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9037     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9038     (if (= mark gnus-ticked-mark)
9039         (setq gnus-newsgroup-marked (cons article gnus-newsgroup-marked)))
9040     (if (= mark gnus-dormant-mark)
9041         (setq gnus-newsgroup-dormant (cons article gnus-newsgroup-dormant)))))
9042
9043 (defalias 'gnus-summary-mark-as-unread-forward 
9044   'gnus-summary-tick-article-forward)
9045 (make-obsolete 'gnus-summary-mark-as-unread-forward 
9046                'gnus-summary-tick-article-forward)
9047 (defun gnus-summary-tick-article-forward (n)
9048   "Tick N articles forwards.
9049 If N is negative, tick backwards instead.
9050 The difference between N and the number of articles ticked is returned."
9051   (interactive "p")
9052   (gnus-summary-mark-forward n gnus-ticked-mark))
9053
9054 (defalias 'gnus-summary-mark-as-unread-backward 
9055   'gnus-summary-tick-article-backward)
9056 (make-obsolete 'gnus-summary-mark-as-unread-backward 
9057                'gnus-summary-tick-article-backward)
9058 (defun gnus-summary-tick-article-backward (n)
9059   "Tick N articles backwards.
9060 The difference between N and the number of articles ticked is returned."
9061   (interactive "p")
9062   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
9063
9064 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9065 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9066 (defun gnus-summary-tick-article (&optional article clear-mark)
9067   "Mark current article as unread.
9068 Optional 1st argument ARTICLE specifies article number to be marked as unread.
9069 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
9070   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
9071                                        gnus-ticked-mark)))
9072
9073 (defun gnus-summary-mark-as-read-forward (n)
9074   "Mark N articles as read forwards.
9075 If N is negative, mark backwards instead.
9076 The difference between N and the actual number of articles marked is
9077 returned."
9078   (interactive "p")
9079   (gnus-summary-mark-forward n gnus-del-mark t))
9080
9081 (defun gnus-summary-mark-as-read-backward (n)
9082   "Mark the N articles as read backwards.
9083 The difference between N and the actual number of articles marked is
9084 returned."
9085   (interactive "p")
9086   (gnus-summary-mark-forward (- n) gnus-del-mark t))
9087
9088 (defun gnus-summary-mark-as-read (&optional article mark)
9089   "Mark current article as read.
9090 ARTICLE specifies the article to be marked as read.
9091 MARK specifies a string to be inserted at the beginning of the line."
9092   (gnus-summary-mark-article article mark))
9093
9094 (defun gnus-summary-clear-mark-forward (n)
9095   "Clear marks from N articles forward.
9096 If N is negative, clear backward instead.
9097 The difference between N and the number of marks cleared is returned."
9098   (interactive "p")
9099   (gnus-summary-mark-forward n gnus-unread-mark))
9100
9101 (defun gnus-summary-clear-mark-backward (n)
9102   "Clear marks from N articles backward.
9103 The difference between N and the number of marks cleared is returned."
9104   (interactive "p")
9105   (gnus-summary-mark-forward (- n) gnus-unread-mark))
9106
9107 (defun gnus-summary-mark-unread-as-read ()
9108   "Intended to be used by `gnus-summary-mark-article-hook'."
9109   (or (memq gnus-current-article gnus-newsgroup-marked)
9110       (memq gnus-current-article gnus-newsgroup-dormant)
9111       (memq gnus-current-article gnus-newsgroup-expirable)
9112       (gnus-summary-mark-as-read gnus-current-article gnus-read-mark)))
9113
9114 (defun gnus-summary-mark-region-as-read (point mark all)
9115   "Mark all unread articles between point and mark as read.
9116 If given a prefix, mark all articles between point and mark as read,
9117 even ticked and dormant ones."
9118   (interactive "r\nP")
9119   (save-excursion
9120     (goto-char point)
9121     (beginning-of-line)
9122     (while (and 
9123             (< (point) mark)
9124             (progn
9125               (and
9126                (or all
9127                    (and
9128                     (not (memq (gnus-summary-article-number)
9129                                gnus-newsgroup-marked))
9130                     (not (memq (gnus-summary-article-number)
9131                                gnus-newsgroup-dormant))))
9132                (gnus-summary-mark-article
9133                 (gnus-summary-article-number) gnus-del-mark))
9134               t)
9135             (zerop (forward-line 1))))))
9136
9137 ;; Fix by Per Abrahamsen <amanda@iesd.auc.dk>.
9138 (defalias 'gnus-summary-delete-marked-as-read 
9139   'gnus-summary-remove-lines-marked-as-read)
9140 (make-obsolete 'gnus-summary-delete-marked-as-read 
9141                'gnus-summary-remove-lines-marked-as-read)
9142 (defun gnus-summary-remove-lines-marked-as-read ()
9143   "Remove lines that are marked as read."
9144   (interactive)
9145   (gnus-summary-remove-lines-marked-with 
9146    (concat (mapconcat
9147             (lambda (char) (char-to-string (symbol-value char)))
9148             '(gnus-del-mark gnus-read-mark gnus-ancient-mark
9149               gnus-killed-mark gnus-kill-file-mark
9150               gnus-low-score-mark gnus-expirable-mark
9151               gnus-canceled-mark)
9152             ""))))
9153
9154 (defalias 'gnus-summary-delete-marked-with 
9155   'gnus-summary-remove-lines-marked-with)
9156 (make-obsolete 'gnus-summary-delete-marked-with 
9157                'gnus-summary-remove-lines-marked-with)
9158 ;; Rewrite by Daniel Quinlan <quinlan@best.com>.
9159 (defun gnus-summary-remove-lines-marked-with (marks)
9160   "Remove lines that are marked with MARKS (e.g. \"DK\")."
9161   (interactive "sMarks: ")
9162   ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
9163   (gnus-set-global-variables)
9164   (let ((buffer-read-only nil)
9165         (marks (concat "^[" marks "]")))
9166     (goto-char (point-min))
9167     (if gnus-newsgroup-adaptive
9168         (gnus-score-remove-lines-adaptive marks)
9169       (while (re-search-forward marks nil t)
9170         (gnus-delete-line)))
9171     ;; If we use dummy roots, we have to do an additional sweep over
9172     ;; the buffer.
9173     (if (not (eq gnus-summary-make-false-root 'dummy))
9174         ()
9175       (goto-char (point-min))
9176       (setq marks (concat "^[" (char-to-string gnus-dummy-mark) "]"))
9177       (while (re-search-forward marks nil t)
9178         (if (gnus-subject-equal
9179              (gnus-summary-subject-string)
9180              (progn
9181                (forward-line 1)
9182                (gnus-summary-subject-string)))
9183             ()
9184           (forward-line -1)
9185           (gnus-delete-line)))))
9186   (or (zerop (buffer-size))
9187       (if (eobp)
9188           (gnus-summary-prev-subject 1)
9189         (gnus-summary-position-cursor))))
9190
9191 (defun gnus-summary-expunge-below (score)
9192   "Remove articles with score less than SCORE."
9193   (interactive "P")
9194   (gnus-set-global-variables)
9195   (setq score (if score
9196                   (prefix-numeric-value score)
9197                 (or gnus-summary-default-score 0)))
9198   (save-excursion
9199     (set-buffer gnus-summary-buffer)
9200     (goto-char (point-min))
9201     (let ((buffer-read-only nil)
9202           beg)
9203       (while (not (eobp))
9204         (if (< (gnus-summary-article-score) score)
9205             (progn
9206               (setq beg (point))
9207               (forward-line 1)
9208               (delete-region beg (point)))
9209           (forward-line 1)))
9210       ;; Adjust point.
9211       (or (zerop (buffer-size))
9212           (if (eobp)
9213               (gnus-summary-prev-subject 1)
9214             (gnus-summary-position-cursor))))))
9215
9216 (defun gnus-summary-mark-below (score mark)
9217   "Mark articles with score less than SCORE with MARK."
9218   (interactive "P\ncMark: ")
9219   (gnus-set-global-variables)
9220   (setq score (if score
9221                   (prefix-numeric-value score)
9222                 (or gnus-summary-default-score 0)))
9223   (save-excursion
9224     (set-buffer gnus-summary-buffer)
9225     (goto-char (point-min))
9226     (while (not (eobp))
9227       (and (< (gnus-summary-article-score) score)
9228            (gnus-summary-mark-article nil mark))
9229       (forward-line 1))))
9230
9231 (defun gnus-summary-kill-below (score)
9232   "Mark articles with score below SCORE as read."
9233   (interactive "P")
9234   (gnus-summary-mark-below score gnus-killed-mark))
9235
9236 (defun gnus-summary-clear-above (score)
9237   "Clear all marks from articles with score above SCORE."
9238   (interactive "P")
9239   (gnus-summary-mark-above score gnus-unread-mark))
9240
9241 (defun gnus-summary-tick-above (score)
9242   "Tick all articles with score above SCORE."
9243   (interactive "P")
9244   (gnus-summary-mark-above score gnus-ticked-mark))
9245
9246 (defun gnus-summary-mark-above (score mark)
9247   "Mark articles with score over SCORE with MARK."
9248   (interactive "P\ncMark: ")
9249   (setq score (if score
9250                   (prefix-numeric-value score)
9251                 (or gnus-summary-default-score 0)))
9252   (save-excursion
9253     (set-buffer gnus-summary-buffer)
9254     (goto-char (point-min))
9255     (while (not (eobp))
9256       (if (> (gnus-summary-article-score) score)
9257           (progn
9258             (gnus-summary-mark-article nil mark)
9259             (forward-line 1))
9260         (forward-line 1)))))
9261
9262 ;; Suggested by Daniel Quinlan <quinlan@best.com>.  
9263 (defun gnus-summary-show-all-expunged ()
9264   "Display all the hidden articles that were expunged for low scores."
9265   (interactive)
9266   (let ((buffer-read-only nil))
9267     (let ((scored gnus-newsgroup-scored)
9268           headers h)
9269       (while scored
9270         (or (gnus-summary-goto-subject (car (car scored)))
9271             (and (setq h (gnus-get-header-by-number (car (car scored))))
9272                  (< (cdr (car scored)) gnus-summary-expunge-below)
9273                  (setq headers (cons h headers))))
9274         (setq scored (cdr scored)))
9275       (or headers (error "No expunged articles hidden."))
9276       (goto-char (point-min))
9277       (save-excursion 
9278         (gnus-summary-update-lines 
9279          (point)
9280          (progn
9281            (gnus-summary-prepare-threads (nreverse headers) 0)
9282            (point)))))
9283     (goto-char (point-min))
9284     (gnus-summary-position-cursor)))
9285
9286 (defun gnus-summary-show-all-dormant ()
9287   "Display all the hidden articles that are marked as dormant."
9288   (interactive)
9289   (let ((buffer-read-only nil))
9290     (let ((dormant gnus-newsgroup-dormant)
9291           headers h)
9292       (while dormant
9293         (or (gnus-summary-goto-subject (car dormant))
9294             (and (setq h (gnus-get-header-by-number (car dormant)))
9295                  (setq headers (cons h headers))))
9296         (setq dormant (cdr dormant)))
9297       (or headers (error "No dormant articles hidden."))
9298       (goto-char (point-min))
9299       (save-excursion 
9300         (gnus-summary-update-lines 
9301          (point)
9302          (progn
9303            (gnus-summary-prepare-threads (nreverse headers) 0)
9304            (point)))))
9305     (goto-char (point-min))
9306     (gnus-summary-position-cursor)))
9307
9308 (defun gnus-summary-hide-all-dormant ()
9309   "Hide all dormant articles."
9310   (interactive)
9311   (gnus-summary-remove-lines-marked-with (char-to-string gnus-dormant-mark))
9312   (gnus-summary-position-cursor))
9313
9314 (defun gnus-summary-catchup (all &optional quietly to-here not-mark)
9315   "Mark all articles not marked as unread in this newsgroup as read.
9316 If prefix argument ALL is non-nil, all articles are marked as read.
9317 If QUIETLY is non-nil, no questions will be asked.
9318 If TO-HERE is non-nil, it should be a point in the buffer. All
9319 articles before this point will be marked as read.
9320 The number of articles marked as read is returned."
9321   (interactive "P")
9322   (prog1
9323       (if (or quietly
9324               (not gnus-interactive-catchup) ;Without confirmation?
9325               gnus-expert-user
9326               (gnus-y-or-n-p
9327                (if all
9328                    "Mark absolutely all articles as read? "
9329                  "Mark all unread articles as read? ")))
9330           (if (and not-mark 
9331                    (not gnus-newsgroup-adaptive)
9332                    (not gnus-newsgroup-auto-expire))
9333               (progn
9334                 (and all (setq gnus-newsgroup-marked nil
9335                                gnus-newsgroup-dormant nil))
9336                 (setq gnus-newsgroup-unreads 
9337                       (append gnus-newsgroup-marked gnus-newsgroup-dormant)))
9338             ;; We actually mark all articles as canceled, which we
9339             ;; have to do when using auto-expiry or adaptive scoring. 
9340             (let ((unreads (length gnus-newsgroup-unreads)))
9341               (if (gnus-summary-first-subject (not all))
9342                   (while (and (if to-here (< (point) to-here) t)
9343                               (gnus-summary-mark-as-read nil gnus-catchup-mark)
9344                               (gnus-summary-search-subject nil (not all)))))
9345               (- unreads (length gnus-newsgroup-unreads))
9346               (or to-here
9347                   (setq gnus-newsgroup-unreads gnus-newsgroup-marked)))))
9348     (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
9349       (if (and (not to-here) (eq 'nnvirtual (car method)))
9350           (nnvirtual-catchup-group
9351            (gnus-group-real-name gnus-newsgroup-name) (nth 1 method) all)))
9352     (gnus-summary-position-cursor)))
9353
9354 (defun gnus-summary-catchup-to-here (&optional all)
9355   "Mark all unticked articles before the current one as read.
9356 If ALL is non-nil, also mark ticked and dormant articles as read."
9357   (interactive)
9358   (save-excursion
9359     (end-of-line)
9360     (gnus-summary-catchup all t (point))
9361     (gnus-set-mode-line 'summary))
9362   (gnus-summary-position-cursor))
9363
9364 (defun gnus-summary-catchup-all (&optional quietly)
9365   "Mark all articles in this newsgroup as read."
9366   (interactive)
9367   (gnus-summary-catchup t quietly))
9368
9369 (defun gnus-summary-catchup-and-exit (all &optional quietly)
9370   "Mark all articles not marked as unread in this newsgroup as read, then exit.
9371 If prefix argument ALL is non-nil, all articles are marked as read."
9372   (interactive "P")
9373   (gnus-summary-catchup all quietly nil 'fast)
9374   ;; Select next newsgroup or exit.
9375   (if (eq gnus-auto-select-next 'quietly)
9376       (gnus-summary-next-group nil)
9377     (gnus-summary-exit)))
9378
9379 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
9380   "Mark all articles in this newsgroup as read, and then exit."
9381   (interactive)
9382   (gnus-summary-catchup-and-exit t quietly))
9383
9384 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
9385 (defun gnus-summary-catchup-and-goto-next-group (all)
9386   "Mark all articles in this group as read and select the next group.
9387 If given a prefix, mark all articles, unread as well as ticked, as
9388 read." 
9389   (interactive "P")
9390   (gnus-summary-catchup all)
9391   (gnus-summary-next-group))
9392
9393 ;; Thread-based commands.
9394
9395 (defun gnus-summary-toggle-threads (arg)
9396   "Toggle showing conversation threads.
9397 If ARG is positive number, turn showing conversation threads on."
9398   (interactive "P")
9399   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
9400     (setq gnus-show-threads
9401           (if (null arg) (not gnus-show-threads)
9402             (> (prefix-numeric-value arg) 0)))
9403     (gnus-summary-prepare)
9404     (gnus-summary-goto-subject current)
9405     (gnus-summary-position-cursor)))
9406
9407 (defun gnus-summary-show-all-threads ()
9408   "Show all threads."
9409   (interactive)
9410   (save-excursion
9411     (let ((buffer-read-only nil))
9412       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
9413   (gnus-summary-position-cursor))
9414
9415 (defun gnus-summary-show-thread ()
9416   "Show thread subtrees.
9417 Returns nil if no thread was there to be shown."
9418   (interactive)
9419   (prog1
9420       (save-excursion
9421         (let ((buffer-read-only nil)
9422               ;; first goto end then to beg, to have point at beg after let
9423               (end (progn (end-of-line) (point)))
9424               (beg (progn (beginning-of-line) (point))))
9425           (prog1
9426               ;; Any hidden lines here?
9427               (search-forward "\r" end t)
9428             (subst-char-in-region beg end ?\^M ?\n t))))
9429     (gnus-summary-position-cursor)))
9430
9431 (defun gnus-summary-hide-all-threads ()
9432   "Hide all thread subtrees."
9433   (interactive)
9434   (save-excursion
9435     (goto-char (point-min))
9436     (gnus-summary-hide-thread)
9437     (while (and (not (eobp)) (zerop (forward-line 1)))
9438       (gnus-summary-hide-thread)))
9439   (gnus-summary-position-cursor))
9440
9441 (defun gnus-summary-hide-thread ()
9442   "Hide thread subtrees.
9443 Returns nil if no threads were there to be hidden."
9444   (interactive)
9445   (let ((buffer-read-only nil)
9446         (start (point))
9447         (level (gnus-summary-thread-level))
9448         (end (point)))
9449     ;; Go forward until either the buffer ends or the subthread
9450     ;; ends. 
9451     (if (eobp)
9452         ()
9453       (while (and (zerop (forward-line 1))
9454                   (> (gnus-summary-thread-level) level))
9455         (setq end (point)))
9456       (prog1
9457           (save-excursion
9458             (goto-char end)
9459             (search-backward "\n" start t))
9460         (subst-char-in-region start end ?\n ?\^M t)
9461         (forward-line -1)))))
9462
9463 (defun gnus-summary-go-to-next-thread (&optional previous)
9464   "Go to the same level (or less) next thread.
9465 If PREVIOUS is non-nil, go to previous thread instead.
9466 Return the article number moved to, or nil if moving was impossible."
9467   (let ((level (gnus-summary-thread-level))
9468         (article (gnus-summary-article-number)))
9469     (if previous 
9470         (while (and (zerop (forward-line -1))
9471                     (> (gnus-summary-thread-level) level)))
9472       (while (and (save-excursion
9473                     (forward-line 1)
9474                     (not (eobp)))
9475                   (zerop (forward-line 1))
9476                   (> (gnus-summary-thread-level) level))))
9477     (gnus-summary-recenter)
9478     (gnus-summary-position-cursor)
9479     (let ((oart (gnus-summary-article-number)))
9480       (and (/= oart article) oart))))
9481
9482 (defun gnus-summary-next-thread (n)
9483   "Go to the same level next N'th thread.
9484 If N is negative, search backward instead.
9485 Returns the difference between N and the number of skips actually
9486 done."
9487   (interactive "p")
9488   (let ((backward (< n 0))
9489         (n (abs n)))
9490   (while (and (> n 0)
9491               (gnus-summary-go-to-next-thread backward))
9492     (setq n (1- n)))
9493   (gnus-summary-position-cursor)
9494   (if (/= 0 n) (gnus-message 7 "No more threads"))
9495   n))
9496
9497 (defun gnus-summary-prev-thread (n)
9498   "Go to the same level previous N'th thread.
9499 Returns the difference between N and the number of skips actually
9500 done."
9501   (interactive "p")
9502   (gnus-summary-next-thread (- n)))
9503
9504 (defun gnus-summary-go-down-thread (&optional same)
9505   "Go down one level in the current thread.
9506 If SAME is non-nil, also move to articles of the same level."
9507   (let ((level (gnus-summary-thread-level))
9508         (start (point)))
9509     (if (and (zerop (forward-line 1))
9510              (> (gnus-summary-thread-level) level))
9511         t
9512       (goto-char start)
9513       nil)))
9514
9515 (defun gnus-summary-go-up-thread ()
9516   "Go up one level in the current thread."
9517   (let ((level (gnus-summary-thread-level))
9518         (start (point)))
9519     (while (and (zerop (forward-line -1))
9520                 (>= (gnus-summary-thread-level) level)))
9521     (if (>= (gnus-summary-thread-level) level)
9522         (progn
9523           (goto-char start)
9524           nil)
9525       t)))
9526
9527 (defun gnus-summary-down-thread (n)
9528   "Go down thread N steps.
9529 If N is negative, go up instead.
9530 Returns the difference between N and how many steps down that were
9531 taken."
9532   (interactive "p")
9533   (let ((up (< n 0))
9534         (n (abs n)))
9535   (while (and (> n 0)
9536               (if up (gnus-summary-go-up-thread)
9537                 (gnus-summary-go-down-thread)))
9538     (setq n (1- n)))
9539   (gnus-summary-position-cursor)
9540   (if (/= 0 n) (gnus-message 7 "Can't go further"))
9541   n))
9542
9543 (defun gnus-summary-up-thread (n)
9544   "Go up thread N steps.
9545 If N is negative, go up instead.
9546 Returns the difference between N and how many steps down that were
9547 taken."
9548   (interactive "p")
9549   (gnus-summary-down-thread (- n)))
9550
9551 (defun gnus-summary-kill-thread (unmark)
9552   "Mark articles under current thread as read.
9553 If the prefix argument is positive, remove any kinds of marks.
9554 If the prefix argument is negative, tick articles instead."
9555   (interactive "P")
9556   (if unmark
9557       (setq unmark (prefix-numeric-value unmark)))
9558   (let ((killing t)
9559         (level (gnus-summary-thread-level)))
9560     (save-excursion
9561       (while killing
9562         ;; Mark the article...
9563         (cond ((null unmark) (gnus-summary-mark-as-read nil gnus-killed-mark))
9564               ((> unmark 0) (gnus-summary-tick-article nil t))
9565               (t (gnus-summary-tick-article)))
9566         ;; ...and go forward until either the buffer ends or the subtree
9567         ;; ends. 
9568         (if (not (and (zerop (forward-line 1))
9569                       (> (gnus-summary-thread-level) level)))
9570             (setq killing nil))))
9571     ;; Hide killed subtrees.
9572     (and (null unmark)
9573          gnus-thread-hide-killed
9574          (gnus-summary-hide-thread))
9575     ;; If marked as read, go to next unread subject.
9576     (if (null unmark)
9577         ;; Go to next unread subject.
9578         (gnus-summary-next-subject 1 t)))
9579   (gnus-set-mode-line 'summary))
9580
9581 ;; Summary sorting commands
9582
9583 (defun gnus-summary-sort-by-number (&optional reverse)
9584   "Sort summary buffer by article number.
9585 Argument REVERSE means reverse order."
9586   (interactive "P")
9587   (gnus-summary-sort 
9588    (cons 'gnus-summary-article-number 'gnus-thread-sort-by-number) reverse))
9589
9590 (defun gnus-summary-sort-by-author (&optional reverse)
9591   "Sort summary buffer by author name alphabetically.
9592 If case-fold-search is non-nil, case of letters is ignored.
9593 Argument REVERSE means reverse order."
9594   (interactive "P")
9595   (gnus-summary-sort
9596    (cons
9597     (lambda ()
9598       (let ((extract (funcall
9599                       gnus-extract-address-components
9600                       (header-from (gnus-get-header-by-number
9601                                     (gnus-summary-article-number))))))
9602         (or (car extract) (cdr extract))))
9603     'gnus-thread-sort-by-author)
9604    reverse))
9605
9606 (defun gnus-summary-sort-by-subject (&optional reverse)
9607   "Sort summary buffer by subject alphabetically. `Re:'s are ignored.
9608 If case-fold-search is non-nil, case of letters is ignored.
9609 Argument REVERSE means reverse order."
9610   (interactive "P")
9611   (gnus-summary-sort
9612    (cons
9613     (lambda ()
9614       (downcase (gnus-simplify-subject (gnus-summary-subject-string))))
9615     'gnus-thread-sort-by-subject)
9616    reverse))
9617
9618 (defun gnus-summary-sort-by-date (&optional reverse)
9619   "Sort summary buffer by date.
9620 Argument REVERSE means reverse order."
9621   (interactive "P")
9622   (gnus-summary-sort
9623    (cons
9624     (lambda ()
9625       (gnus-sortable-date
9626        (header-date 
9627         (gnus-get-header-by-number (gnus-summary-article-number)))))
9628     'gnus-thread-sort-by-date)
9629    reverse))
9630
9631 (defun gnus-summary-sort-by-score (&optional reverse)
9632   "Sort summary buffer by score.
9633 Argument REVERSE means reverse order."
9634   (interactive "P")
9635   (gnus-summary-sort 
9636    (cons 'gnus-summary-article-score 'gnus-thread-sort-by-score)
9637    (not reverse)))
9638
9639 (defvar gnus-summary-already-sorted nil)
9640 (defun gnus-summary-sort (predicate reverse)
9641   ;; Sort summary buffer by PREDICATE.  REVERSE means reverse order. 
9642   (if gnus-summary-already-sorted
9643       ()
9644     (let (buffer-read-only)
9645       (if (not gnus-show-threads)
9646           (progn
9647             (goto-char (point-min))
9648             (sort-subr reverse 'forward-line 'end-of-line (car predicate)))
9649         (let ((gnus-thread-sort-functions (list (cdr predicate)))
9650               (gnus-summary-prepare-hook nil)
9651               (gnus-summary-already-sorted nil))
9652           (gnus-summary-prepare)
9653           (and gnus-show-threads
9654                gnus-thread-hide-subtree
9655                (gnus-summary-hide-all-threads))
9656           ;; If in async mode, we send some info to the backend.
9657           (and gnus-newsgroup-async
9658                (setq gnus-newsgroup-threads (nreverse gnus-newsgroup-threads))
9659                (gnus-request-asynchronous 
9660                 gnus-newsgroup-name
9661                 (if (and gnus-asynchronous-article-function
9662                          (fboundp gnus-asynchronous-article-function))
9663                     (funcall gnus-asynchronous-article-function
9664                              gnus-newsgroup-threads)))))))))
9665
9666   
9667 (defun gnus-sortable-date (date)
9668   "Make sortable string by string-lessp from DATE.
9669 Timezone package is used."
9670   (let* ((date   (timezone-fix-time date nil nil)) ;[Y M D H M S]
9671          (year   (aref date 0))
9672          (month  (aref date 1))
9673          (day    (aref date 2)))
9674     (timezone-make-sortable-date 
9675      year month day 
9676      (timezone-make-time-string
9677       (aref date 3) (aref date 4) (aref date 5)))))
9678
9679
9680 ;; Summary saving commands.
9681
9682 (defun gnus-summary-save-article (n)
9683   "Save the current article using the default saver function.
9684 If N is a positive number, save the N next articles.
9685 If N is a negative number, save the N previous articles.
9686 If N is nil and any articles have been marked with the process mark,
9687 save those articles instead.
9688 The variable `gnus-default-article-saver' specifies the saver function."
9689   (interactive "P")
9690   (let ((articles (gnus-summary-work-articles n)))
9691     (while articles
9692       (let ((header (gnus-get-header-by-number (car articles))))
9693         (if (vectorp header)
9694             (progn
9695               (gnus-summary-select-article t nil nil (car articles))
9696               (or gnus-save-all-headers
9697                   (gnus-article-hide-headers t))
9698               ;; Remove any X-Gnus lines.
9699               (save-excursion
9700                 (save-restriction
9701                   (set-buffer gnus-article-buffer)
9702                   (let ((buffer-read-only nil))
9703                     (goto-char (point-min))
9704                     (narrow-to-region (point) (or (search-forward "\n\n" nil t)
9705                                                   (point-max)))
9706                     (while (re-search-forward "^X-Gnus" nil t)
9707                       (beginning-of-line)
9708                       (delete-region (point)
9709                                      (progn (forward-line 1) (point))))
9710                     (widen))))
9711               (save-excursion
9712                 (if gnus-default-article-saver
9713                     (funcall gnus-default-article-saver)
9714                   (error "No default saver is defined."))))
9715           (if (assq 'name header)
9716               (gnus-copy-file (cdr (assq 'name header)))
9717             (gnus-message 1 "Article %d is unsaveable" (car articles)))))
9718       (gnus-summary-remove-process-mark (car articles))
9719       (setq articles (cdr articles)))
9720     (gnus-summary-position-cursor)
9721     n))
9722
9723 (defun gnus-summary-pipe-output (arg)
9724   "Pipe the current article to a subprocess.
9725 If N is a positive number, pipe the N next articles.
9726 If N is a negative number, pipe the N previous articles.
9727 If N is nil and any articles have been marked with the process mark,
9728 pipe those articles instead."
9729   (interactive "P")
9730   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9731     (gnus-summary-save-article arg)))
9732
9733 (defun gnus-summary-save-article-mail (arg)
9734   "Append the current article to an mail file.
9735 If N is a positive number, save the N next articles.
9736 If N is a negative number, save the N previous articles.
9737 If N is nil and any articles have been marked with the process mark,
9738 save those articles instead."
9739   (interactive "P")
9740   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9741     (gnus-summary-save-article arg)))
9742
9743 (defun gnus-summary-save-article-rmail (arg)
9744   "Append the current article to an rmail file.
9745 If N is a positive number, save the N next articles.
9746 If N is a negative number, save the N previous articles.
9747 If N is nil and any articles have been marked with the process mark,
9748 save those articles instead."
9749   (interactive "P")
9750   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9751     (gnus-summary-save-article arg)))
9752
9753 (defun gnus-summary-save-article-file (arg)
9754   "Append the current article to a file.
9755 If N is a positive number, save the N next articles.
9756 If N is a negative number, save the N previous articles.
9757 If N is nil and any articles have been marked with the process mark,
9758 save those articles instead."
9759   (interactive "P")
9760   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9761     (gnus-summary-save-article arg)))
9762
9763 (defun gnus-read-save-file-name (prompt default-name)
9764   (let ((methods gnus-split-methods)
9765         split-name)
9766     (if (not gnus-split-methods)
9767         ()
9768       (save-excursion
9769         (set-buffer gnus-article-buffer)
9770         (gnus-narrow-to-headers)
9771         (while methods
9772           (goto-char (point-min))
9773           (and (condition-case () 
9774                    (re-search-forward (car (car methods)) nil t)
9775                  (error nil))
9776                (setq split-name (cons (nth 1 (car methods)) split-name)))
9777           (setq methods (cdr methods)))
9778         (widen)))
9779     (cond ((null split-name)
9780            (read-file-name
9781             (concat prompt " (default "
9782                     (file-name-nondirectory default-name) ") ")
9783             (file-name-directory default-name)
9784             default-name))
9785           ((= 1 (length split-name))
9786            (read-file-name
9787             (concat prompt " (default " (car split-name) ") ")
9788             gnus-article-save-directory
9789             (concat gnus-article-save-directory (car split-name))))
9790           (t
9791            (setq split-name (mapcar (lambda (el) (list el))
9792                                     (nreverse split-name)))
9793            (let ((result (completing-read 
9794                           (concat prompt " ")
9795                           split-name nil nil)))
9796              (concat gnus-article-save-directory
9797                      (if (string= result "")
9798                          (car (car split-name))
9799                        result)))))))
9800
9801 (defun gnus-summary-save-in-rmail (&optional filename)
9802   "Append this article to Rmail file.
9803 Optional argument FILENAME specifies file name.
9804 Directory to save to is default to `gnus-article-save-directory' which
9805 is initialized from the SAVEDIR environment variable."
9806   (interactive)
9807   (let ((default-name
9808           (funcall gnus-rmail-save-name gnus-newsgroup-name
9809                    gnus-current-headers gnus-newsgroup-last-rmail)))
9810     (or filename
9811         (setq filename (gnus-read-save-file-name 
9812                         "Save in rmail file:" default-name)))
9813     (gnus-make-directory (file-name-directory filename))
9814     (gnus-eval-in-buffer-window 
9815      gnus-article-buffer
9816      (save-excursion
9817        (save-restriction
9818          (widen)
9819          (gnus-output-to-rmail filename))))
9820     ;; Remember the directory name to save articles.
9821     (setq gnus-newsgroup-last-rmail filename)))
9822
9823 (defun gnus-summary-save-in-mail (&optional filename)
9824   "Append this article to Unix mail file.
9825 Optional argument FILENAME specifies file name.
9826 Directory to save to is default to `gnus-article-save-directory' which
9827 is initialized from the SAVEDIR environment variable."
9828   (interactive)
9829   (let ((default-name
9830           (funcall gnus-mail-save-name gnus-newsgroup-name
9831                    gnus-current-headers gnus-newsgroup-last-mail)))
9832     (or filename
9833         (setq filename (gnus-read-save-file-name 
9834                         "Save in Unix mail file:" default-name)))
9835     (setq filename
9836           (expand-file-name filename
9837                             (and default-name
9838                                  (file-name-directory default-name))))
9839     (gnus-make-directory (file-name-directory filename))
9840     (gnus-eval-in-buffer-window 
9841      gnus-article-buffer
9842      (save-excursion
9843        (save-restriction
9844          (widen)
9845          (if (and (file-readable-p filename) (rmail-file-p filename))
9846              (gnus-output-to-rmail filename)
9847            (rmail-output filename 1 t t)))))
9848     ;; Remember the directory name to save articles.
9849     (setq gnus-newsgroup-last-mail filename)))
9850
9851 (defun gnus-summary-save-in-file (&optional filename)
9852   "Append this article to file.
9853 Optional argument FILENAME specifies file name.
9854 Directory to save to is default to `gnus-article-save-directory' which
9855 is initialized from the SAVEDIR environment variable."
9856   (interactive)
9857   (let ((default-name
9858           (funcall gnus-file-save-name gnus-newsgroup-name
9859                    gnus-current-headers gnus-newsgroup-last-file)))
9860     (or filename
9861         (setq filename (gnus-read-save-file-name 
9862                         "Save in file:" default-name)))
9863     (gnus-make-directory (file-name-directory filename))
9864     (gnus-eval-in-buffer-window 
9865      gnus-article-buffer
9866      (save-excursion
9867        (save-restriction
9868          (widen)
9869          (gnus-output-to-file filename))))
9870     ;; Remember the directory name to save articles.
9871     (setq gnus-newsgroup-last-file filename)))
9872
9873 (defun gnus-summary-save-in-pipe (&optional command)
9874   "Pipe this article to subprocess."
9875   (interactive)
9876   (let ((command (read-string "Shell command on article: "
9877                               gnus-last-shell-command)))
9878     (if (string-equal command "")
9879         (setq command gnus-last-shell-command))
9880     (gnus-eval-in-buffer-window 
9881      gnus-article-buffer
9882      (save-restriction
9883        (widen)
9884        (shell-command-on-region (point-min) (point-max) command nil)))
9885     (setq gnus-last-shell-command command)))
9886
9887 ;; Summary extract commands
9888
9889 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
9890   (let ((buffer-read-only nil)
9891         (article (gnus-summary-article-number))
9892         b)
9893     (or (gnus-summary-goto-subject article)
9894         (error (format "No such article: %d" article)))
9895     (or gnus-newsgroup-headers-hashtb-by-number
9896         (gnus-make-headers-hashtable-by-number))
9897     (gnus-summary-position-cursor)
9898     ;; If all commands are to be bunched up on one line, we collect
9899     ;; them here.  
9900     (if gnus-view-pseudos-separately
9901         ()
9902       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9903             files action)
9904         (while ps
9905           (setq action (cdr (assq 'action (car ps))))
9906           (setq files (list (cdr (assq 'name (car ps)))))
9907           (while (and ps (cdr ps)
9908                       (string= (or action "1")
9909                                (or (cdr (assq 'action (car (cdr ps)))) "2")))
9910             (setq files (cons (cdr (assq 'name (car (cdr ps)))) files))
9911             (setcdr ps (cdr (cdr ps))))
9912           (if (not files)
9913               ()
9914             (if (not (string-match "%s" action))
9915                 (setq files (cons " " files)))
9916             (setq files (cons " " files))
9917             (and (assq 'execute (car ps))
9918                  (setcdr (assq 'execute (car ps))
9919                          (funcall (if (string-match "%s" action)
9920                                       'format 'concat)
9921                                   action 
9922                                   (mapconcat (lambda (f) f) files " ")))))
9923           (setq ps (cdr ps)))))
9924     (if (and gnus-view-pseudos (not not-view))
9925         (while pslist
9926           (and (assq 'execute (car pslist))
9927                (gnus-execute-command (cdr (assq 'execute (car pslist)))
9928                                      (eq gnus-view-pseudos 'not-confirm)))
9929           (setq pslist (cdr pslist)))
9930       (save-excursion
9931         (while pslist
9932           (gnus-summary-goto-subject (or (cdr (assq 'article (car pslist)))
9933                                          (gnus-summary-article-number)))
9934           (forward-line 1)
9935           (setq b (point))
9936           (insert "          " (file-name-nondirectory 
9937                                 (cdr (assq 'name (car pslist))))
9938                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9939           (add-text-properties 
9940            b (1+ b) (list 'gnus-number gnus-reffed-article-number
9941                           'gnus-mark gnus-unread-mark 
9942                           'gnus-level 0
9943                           'gnus-pseudo (car pslist)))
9944           (forward-line -1)
9945           (gnus-sethash (int-to-string gnus-reffed-article-number)
9946                         (car pslist) gnus-newsgroup-headers-hashtb-by-number)
9947           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9948           (setq pslist (cdr pslist)))))))
9949
9950 (defun gnus-pseudos< (p1 p2)
9951   (let ((c1 (cdr (assq 'action p1)))
9952         (c2 (cdr (assq 'action p2))))
9953     (and c1 c2 (string< c1 c2))))
9954
9955 (defun gnus-request-pseudo-article (props)
9956   (cond ((assq 'execute props)
9957          (gnus-execute-command (cdr (assq 'execute props)))))
9958   (let ((gnus-current-article (gnus-summary-article-number)))
9959     (run-hooks 'gnus-mark-article-hook)))
9960
9961 (defun gnus-execute-command (command &optional automatic)
9962   (save-excursion
9963     (gnus-article-setup-buffer)
9964     (set-buffer gnus-article-buffer)
9965     (let ((command (if automatic command (read-string "Command: " command)))
9966           (buffer-read-only nil))
9967       (erase-buffer)
9968       (insert "$ " command "\n\n")
9969       (if gnus-view-pseudo-asynchronously
9970           (start-process "gnus-execute" nil "sh" "-c" command)
9971         (call-process "sh" nil t nil "-c" command)))))
9972
9973 (defun gnus-copy-file (file &optional to)
9974   "Copy FILE to TO."
9975   (interactive
9976    (list (read-file-name "Copy file: " default-directory)
9977          (read-file-name "Copy file to: " default-directory)))
9978   (or to (setq to (read-file-name "Copy file to: " default-directory)))
9979   (and (file-directory-p to) 
9980        (setq to (concat (file-name-as-directory to)
9981                         (file-name-nondirectory file))))
9982   (copy-file file to))
9983
9984 ;; Summary kill commands.
9985
9986 (defun gnus-summary-edit-global-kill (article)
9987   "Edit the \"global\" kill file."
9988   (interactive (list (gnus-summary-article-number)))
9989   (gnus-group-edit-global-kill article))
9990
9991 (defun gnus-summary-edit-local-kill ()
9992   "Edit a local kill file applied to the current newsgroup."
9993   (interactive)
9994   (setq gnus-current-headers 
9995         (gnus-gethash 
9996          (int-to-string (gnus-summary-article-number))
9997          gnus-newsgroup-headers-hashtb-by-number))
9998   (gnus-set-global-variables)
9999   (gnus-group-edit-local-kill 
10000    (gnus-summary-article-number) gnus-newsgroup-name))
10001
10002 \f
10003 ;;;
10004 ;;; Gnus article mode
10005 ;;;
10006
10007 (put 'gnus-article-mode 'mode-class 'special)
10008
10009 (if gnus-article-mode-map
10010     nil
10011   (setq gnus-article-mode-map (make-keymap))
10012   (suppress-keymap gnus-article-mode-map)
10013   (define-key gnus-article-mode-map " " 'gnus-article-next-page)
10014   (define-key gnus-article-mode-map "\177" 'gnus-article-prev-page)
10015   (define-key gnus-article-mode-map "\C-c^" 'gnus-article-refer-article)
10016   (define-key gnus-article-mode-map "h" 'gnus-article-show-summary)
10017   (define-key gnus-article-mode-map "s" 'gnus-article-show-summary)
10018   (define-key gnus-article-mode-map "\C-c\C-m" 'gnus-article-mail)
10019   (define-key gnus-article-mode-map "\C-c\C-M" 'gnus-article-mail-with-original)
10020   (define-key gnus-article-mode-map "?" 'gnus-article-describe-briefly)
10021   (define-key gnus-article-mode-map gnus-mouse-2 'gnus-article-push-button)
10022   (define-key gnus-article-mode-map "\r" 'gnus-article-press-button)
10023   (define-key gnus-article-mode-map "\t" 'gnus-article-next-button)
10024   (define-key gnus-article-mode-map "\C-c\C-b" 'gnus-bug)
10025   
10026   ;; Duplicate almost all summary keystrokes in the article mode map.
10027   (let ((commands 
10028          (list 
10029           " " "\177" "\r" "n" "p" "N" "P" "\M-\C-n" "\M-\C-p"
10030           "\M-n" "\M-p" "." "," "\M-s" "\M-r" "<" ">" "j" "^" "\M-^"
10031           "u" "!" "U" "d" "D" "E" "\M-u" "\M-U" "k" "\C-k" "\M-\C-k"
10032           "\M-\C-l" "e" "#" "\M-#" "\M-\C-t" "\M-\C-s" "\M-\C-h"
10033           "\M-\C-f" "\M-\C-b" "\M-\C-u" "\M-\C-d" "&" "\C-w"
10034           "\C-t" "?" "\C-c\M-\C-s" "\C-c\C-s\C-n" "\C-c\C-s\C-a"
10035           "\C-c\C-s\C-s" "\C-c\C-s\C-d" "\C-c\C-s\C-i" "\C-x\C-s"
10036           "\M-g" "w" "\C-c\C-r" "\M-t" "C"
10037           "o" "\C-o" "|" "\M-k" "\M-K" "V" "\C-c\C-d"
10038           "\C-c\C-i" "x" "X" "s" "t" "g" "?" "l"
10039           "\C-c\C-v\C-v" "\C-d" "v" 
10040 ;;        "Mt" "M!" "Md" "Mr"
10041 ;;        "Mc" "M " "Me" "Mx" "M?" "Mb" "MB" "M#" "M\M-#" "M\M-r"
10042 ;;        "M\M-\C-r" "MD" "M\M-D" "MS" "MC" "MH" "M\C-c" "Mk" "MK"
10043 ;;        "Ms" "Mc" "Mu" "Mm" "Mk" "Gn" "Gp" "GN" "GP" "G\C-n" "G\C-p"
10044 ;;        "G\M-n" "G\M-p" "Gf" "Gb" "Gg" "Gl" "Gp" "Tk" "Tl" "Ti" "TT"
10045 ;;        "Ts" "TS" "Th" "TH" "Tn" "Tp" "Tu" "Td" "T#" "A " "An" "A\177" "Ap"
10046 ;;        "A\r" "A<" "A>" "Ab" "Ae" "A^" "Ar" "Aw" "Ac" "Ag" "At" "Am"
10047 ;;        "As" "Wh" "Ws" "Wc" "Wo" "Ww" "Wd" "Wq" "Wf" "Wt" "W\C-t"
10048 ;;        "WT" "WA" "Wa" "WH" "WC" "WS" "Wb" "Hv" "Hf" "Hd" "Hh" "Hi"
10049 ;;        "Be" "B\177" "Bm" "Br" "Bw" "Bc" "Bq" "Bi" "Oo" "Om" "Or"
10050 ;;        "Of" "Oh" "Ov" "Op" "Vu" "V\C-s" "V\C-r" "Vr" "V&" "VT" "Ve"
10051 ;;        "VD" "Vk" "VK" "Vsn" "Vsa" "Vss" "Vsd" "Vsi"
10052           )))
10053     (while commands
10054       (define-key gnus-article-mode-map (car commands) 
10055         'gnus-article-summary-command)
10056       (setq commands (cdr commands))))
10057
10058   (let ((commands (list "q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
10059 ;;                      "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP" 
10060                          "=")))
10061     (while commands
10062       (define-key gnus-article-mode-map (car commands) 
10063         'gnus-article-summary-command-nosave)
10064       (setq commands (cdr commands)))))
10065
10066
10067 (defun gnus-article-mode ()
10068   "Major mode for displaying an article.
10069
10070 All normal editing commands are switched off.
10071
10072 The following commands are available:
10073
10074 \\<gnus-article-mode-map>
10075 \\[gnus-article-next-page]\t Scroll the article one page forwards
10076 \\[gnus-article-prev-page]\t Scroll the article one page backwards
10077 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
10078 \\[gnus-article-show-summary]\t Display the summary buffer
10079 \\[gnus-article-mail]\t Send a reply to the address near point
10080 \\[gnus-article-mail-with-original]\t Send a reply to the address near point; include the original article
10081 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
10082 \\[gnus-info-find-node]\t Go to the Gnus info node"
10083   (interactive)
10084   (if gnus-visual (gnus-article-make-menu-bar))
10085   (kill-all-local-variables)
10086   (setq mode-line-modified "-- ")
10087   (make-local-variable 'mode-line-format)
10088   (setq mode-line-format (copy-sequence mode-line-format))
10089   (and (equal (nth 3 mode-line-format) "   ")
10090        (setcar (nthcdr 3 mode-line-format) ""))
10091   (setq mode-name "Article")
10092   (setq major-mode 'gnus-article-mode)
10093   (make-local-variable 'minor-mode-alist)
10094   (or (assq 'gnus-show-mime minor-mode-alist)
10095       (setq minor-mode-alist
10096             (cons (list 'gnus-show-mime " MIME") minor-mode-alist)))
10097   (use-local-map gnus-article-mode-map)
10098   (make-local-variable 'page-delimiter)
10099   (setq page-delimiter gnus-page-delimiter)
10100   (buffer-disable-undo (current-buffer))
10101   (setq buffer-read-only t)             ;Disable modification
10102   (run-hooks 'gnus-article-mode-hook))
10103
10104 (defun gnus-article-setup-buffer ()
10105   "Initialize article mode buffer."
10106   (if (get-buffer gnus-article-buffer)
10107       (save-excursion
10108         (set-buffer gnus-article-buffer)
10109         (buffer-disable-undo (current-buffer))
10110         (setq buffer-read-only t)
10111         (gnus-add-current-to-buffer-list)
10112         (or (eq major-mode 'gnus-article-mode)
10113             (gnus-article-mode)))
10114     (save-excursion
10115       (set-buffer (get-buffer-create gnus-article-buffer))
10116       (gnus-add-current-to-buffer-list)
10117       (gnus-article-mode))))
10118
10119 ;; Set article window start at LINE, where LINE is the number of lines
10120 ;; from the head of the article.
10121 (defun gnus-article-set-window-start (&optional line)
10122   (set-window-start 
10123    (get-buffer-window gnus-article-buffer)
10124    (save-excursion
10125      (set-buffer gnus-article-buffer)
10126      (goto-char (point-min))
10127      (if (not line)
10128          (point-min)
10129        (gnus-message 6 "Moved to bookmark")
10130        (search-forward "\n\n" nil t)
10131        (forward-line line)
10132        (point)))))
10133
10134 (defun gnus-request-article-this-buffer (article group)
10135   "Get an article and insert it into this buffer."
10136   (setq group (or group gnus-newsgroup-name))
10137
10138   ;; Open server if it has closed.
10139   (gnus-check-news-server (gnus-find-method-for-group group))
10140
10141   ;; Using `gnus-request-article' directly will insert the article into
10142   ;; `nntp-server-buffer' - so we'll save some time by not having to
10143   ;; copy it from the server buffer into the article buffer.
10144
10145   ;; We only request an article by message-id when we do not have the
10146   ;; headers for it, so we'll have to get those.
10147   (and (stringp article) 
10148        (let ((gnus-override-method gnus-refer-article-method))
10149          (gnus-read-header article)))
10150
10151   ;; If the article number is negative, that means that this article
10152   ;; doesn't belong in this newsgroup (possibly), so we find its
10153   ;; message-id and request it by id instead of number.
10154   (if (not (numberp article))
10155       ()
10156     (save-excursion
10157       (set-buffer gnus-summary-buffer)
10158       (let ((header (gnus-get-header-by-number article)))
10159         (if (< article 0)
10160             (if (vectorp header)
10161                 ;; It's a real article.
10162                 (setq article (header-id header))
10163               ;; It is an extracted pseudo-article.
10164               (setq article 'pseudo)
10165               (gnus-request-pseudo-article header)))
10166
10167         (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
10168           (if (not (eq (car method) 'nneething))
10169               ()
10170             (let ((dir (concat (file-name-as-directory (nth 1 method))
10171                                (header-subject header))))
10172               (if (file-directory-p dir)
10173                   (progn
10174                     (setq article 'nneething)
10175                     (gnus-group-enter-directory dir)))))))))
10176
10177   ;; Check the cache.
10178   (if (and gnus-use-cache
10179            (numberp article)
10180            (gnus-cache-request-article article group))
10181       'article
10182     ;; Get the article and into the article buffer.
10183     (if (or (stringp article) (numberp article))
10184         (progn
10185           (erase-buffer)
10186           (let ((gnus-override-method 
10187                  (and (stringp article) gnus-refer-article-method)))
10188             (and (gnus-request-article article group (current-buffer))
10189                  'article)))
10190       article)))
10191
10192 (defun gnus-read-header (id)
10193   "Read the headers of article ID and enter them into the Gnus system."
10194   (or gnus-newsgroup-headers-hashtb-by-number
10195       (gnus-make-headers-hashtable-by-number))
10196   (let (header)
10197     (if (not (setq header 
10198                    (car (if (let ((gnus-nov-is-evil t))
10199                               (gnus-retrieve-headers 
10200                                (list id) gnus-newsgroup-name))
10201                             (gnus-get-newsgroup-headers)))))
10202         nil
10203       (if (stringp id)
10204           (header-set-number header gnus-reffed-article-number))
10205       (setq gnus-newsgroup-headers (cons header gnus-newsgroup-headers))
10206       (gnus-sethash (int-to-string (header-number header)) header
10207                     gnus-newsgroup-headers-hashtb-by-number)
10208       (if (stringp id)
10209           (setq gnus-reffed-article-number (1- gnus-reffed-article-number)))
10210       (setq gnus-current-headers header)
10211       header)))
10212
10213 (defun gnus-article-prepare (article &optional all-headers header)
10214   "Prepare ARTICLE in article mode buffer.
10215 ARTICLE should either be an article number or a Message-ID.
10216 If ARTICLE is an id, HEADER should be the article headers.
10217 If ALL-HEADERS is non-nil, no headers are hidden."
10218   (save-excursion
10219     ;; Make sure we start in a summary buffer.
10220     (or (eq major-mode 'gnus-summary-mode)
10221         (set-buffer gnus-summary-buffer))
10222     (setq gnus-summary-buffer (current-buffer))
10223     ;; Make sure the connection to the server is alive.
10224     (or (gnus-server-opened (gnus-find-method-for-group gnus-newsgroup-name))
10225         (progn
10226           (gnus-check-news-server 
10227            (gnus-find-method-for-group gnus-newsgroup-name))
10228           (gnus-request-group gnus-newsgroup-name t)))
10229     (or gnus-newsgroup-headers-hashtb-by-number
10230         (gnus-make-headers-hashtable-by-number))
10231     (let* ((article (if header (header-number header) article))
10232            (summary-buffer (current-buffer))
10233            (internal-hook gnus-article-internal-prepare-hook)
10234            (group gnus-newsgroup-name)
10235            result)
10236       (save-excursion
10237         (gnus-article-setup-buffer)
10238         (set-buffer gnus-article-buffer)
10239         (if (not (setq result (let ((buffer-read-only nil))
10240                                 (gnus-request-article-this-buffer 
10241                                  article group))))
10242             ;; There is no such article.
10243             (save-excursion
10244               (if (not (numberp article))
10245                   ()
10246                 (setq gnus-article-current 
10247                       (cons gnus-newsgroup-name article))
10248                 (set-buffer gnus-summary-buffer)
10249                 (setq gnus-current-article article)
10250                 (gnus-summary-mark-as-read article gnus-canceled-mark))
10251               (gnus-message 1 "No such article (may be canceled)")
10252               (ding)
10253               nil)
10254           (if (or (eq result 'pseudo) (eq result 'nneething))
10255               (progn
10256                 (save-excursion
10257                   (set-buffer summary-buffer)
10258                   (setq gnus-last-article gnus-current-article
10259                         gnus-newsgroup-history (cons gnus-current-article
10260                                                      gnus-newsgroup-history)
10261                         gnus-current-article 0
10262                         gnus-current-headers nil
10263                         gnus-article-current nil)
10264                   (if (eq result 'nneething)
10265                       (gnus-configure-windows 'summary)
10266                     (gnus-configure-windows 'article))
10267                   (gnus-set-global-variables))
10268                 (gnus-set-mode-line 'article))
10269             ;; The result from the `request' was an actual article -
10270             ;; or at least some text that is now displayed in the
10271             ;; article buffer.
10272             (if (and (numberp article)
10273                      (not (eq article gnus-current-article)))
10274                 ;; Seems like a new article has been selected.
10275                 ;; `gnus-current-article' must be an article number.
10276                 (save-excursion
10277                   (set-buffer summary-buffer)
10278                   (setq gnus-last-article gnus-current-article
10279                         gnus-newsgroup-history (cons gnus-current-article
10280                                                      gnus-newsgroup-history)
10281                         gnus-current-article article
10282                         gnus-current-headers 
10283                         (gnus-get-header-by-number gnus-current-article)
10284                         gnus-article-current 
10285                         (cons gnus-newsgroup-name gnus-current-article))
10286                   (gnus-summary-show-thread)
10287                   (run-hooks 'gnus-mark-article-hook)
10288                   (gnus-set-mode-line 'summary)
10289                   (and gnus-visual 
10290                        (run-hooks 'gnus-visual-mark-article-hook))
10291                   ;; Set the global newsgroup variables here.
10292                   ;; Suggested by Jim Sisolak
10293                   ;; <sisolak@trans4.neep.wisc.edu>.
10294                   (gnus-set-global-variables)
10295                   (and gnus-use-cache 
10296                        (gnus-cache-possibly-enter-article
10297                         group article
10298                         (gnus-get-header-by-number article)
10299                         (memq article gnus-newsgroup-marked)
10300                         (memq article gnus-newsgroup-dormant)
10301                         (memq article gnus-newsgroup-unreads)))))
10302             ;; gnus-have-all-headers must be either T or NIL.
10303             (setq gnus-have-all-headers
10304                   (not (not (or all-headers gnus-show-all-headers))))
10305             ;; Hooks for getting information from the article.
10306             ;; This hook must be called before being narrowed.
10307             (let (buffer-read-only)
10308               (run-hooks 'internal-hook)
10309               (run-hooks 'gnus-article-prepare-hook)
10310               ;; Decode MIME message.
10311               (if (and gnus-show-mime
10312                        (or (not gnus-strict-mime)
10313                            (gnus-fetch-field "Mime-Version")))
10314                   (funcall gnus-show-mime-method))
10315               ;; Perform the article display hooks.
10316               (run-hooks 'gnus-article-display-hook))
10317             ;; Do page break.
10318             (goto-char (point-min))
10319             (and gnus-break-pages (gnus-narrow-to-page))
10320             (gnus-set-mode-line 'article)
10321             (gnus-configure-windows 'article)
10322             (goto-char (point-min))
10323             t))))))
10324
10325 (defun gnus-article-show-all-headers ()
10326   "Show all article headers in article mode buffer."
10327   (save-excursion 
10328     (gnus-article-setup-buffer)
10329     (set-buffer gnus-article-buffer)
10330     (let ((buffer-read-only nil))
10331       (remove-text-properties (point-min) (point-max) 
10332                               gnus-hidden-properties))))
10333
10334 (defun gnus-article-hide-headers-if-wanted ()
10335   "Hide unwanted headers if `gnus-have-all-headers' is nil.
10336 Provided for backwards compatability."
10337   (or (save-excursion (set-buffer gnus-summary-buffer) gnus-have-all-headers)
10338       (gnus-article-hide-headers)))
10339
10340 (defun gnus-article-hide-headers (&optional delete)
10341   "Hide unwanted headers and possibly sort them as well."
10342   (interactive "P")
10343   (save-excursion
10344     (set-buffer gnus-article-buffer)
10345     (save-restriction
10346       (let ((sorted gnus-sorted-header-list)
10347             (buffer-read-only nil)
10348             want-list beg want-l)
10349         ;; First we narrow to just the headers.
10350         (widen)
10351         (goto-char (point-min))
10352         ;; Hide any "From " lines at the beginning of (mail) articles. 
10353         (while (looking-at "From ")
10354           (forward-line 1))
10355         (if (bobp) 
10356             (add-text-properties (point-min) (point) gnus-hidden-properties))
10357         ;; Then treat the rest of the header lines.
10358         (narrow-to-region 
10359          (point) 
10360          (progn (search-forward "\n\n" nil t) (forward-line -1) (point)))
10361         ;; Then we use the two regular expressions
10362         ;; `gnus-ignored-headers' and `gnus-visible-headers' to
10363         ;; select which header lines is to remain visible in the
10364         ;; article buffer.
10365         (goto-char (point-min))
10366         (while (re-search-forward "^[^ \t]*:" nil t)
10367           (beginning-of-line)
10368           ;; We add the headers we want to keep to a list and delete
10369           ;; them from the buffer.
10370           (if (or (and (stringp gnus-visible-headers)
10371                        (looking-at gnus-visible-headers))
10372                   (and (not (stringp gnus-visible-headers))
10373                        (stringp gnus-ignored-headers)
10374                        (not (looking-at gnus-ignored-headers))))
10375               (progn
10376                 (setq beg (point))
10377                 (forward-line 1)
10378                 ;; Be sure to get multi-line headers...
10379                 (re-search-forward "^[^ \t]*:" nil t)
10380                 (beginning-of-line)
10381                 (setq want-list 
10382                       (cons (buffer-substring beg (point)) want-list))
10383                 (delete-region beg (point))
10384                 (goto-char beg))
10385             (forward-line 1)))
10386         ;; Next we perform the sorting by looking at
10387         ;; `gnus-sorted-header-list'. 
10388         (goto-char (point-min))
10389         (while (and sorted want-list)
10390           (setq want-l want-list)
10391           (while (and want-l
10392                       (not (string-match (car sorted) (car want-l))))
10393             (setq want-l (cdr want-l)))
10394           (if want-l 
10395               (progn
10396                 (insert (car want-l))
10397                 (setq want-list (delq (car want-l) want-list))))
10398           (setq sorted (cdr sorted)))
10399         ;; Any headers that were not matched by the sorted list we
10400         ;; just tack on the end of the visible header list.
10401         (while want-list
10402           (insert (car want-list))
10403           (setq want-list (cdr want-list)))
10404         ;; And finally we make the unwanted headers invisible.
10405         (if delete
10406             (delete-region (point) (point-max))
10407           ;; Suggested by Sudish Joseph <joseph@cis.ohio-state.edu>.
10408           (add-text-properties (point) (point-max) gnus-hidden-properties))))))
10409
10410 ;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.
10411 (defun gnus-article-treat-overstrike ()
10412   "Translate overstrikes into bold text."
10413   (interactive)
10414   (save-excursion
10415     (set-buffer gnus-article-buffer)
10416     (let ((buffer-read-only nil))
10417       (while (search-forward "\b" nil t)
10418         (let ((next (following-char))
10419               (previous (char-after (- (point) 2))))
10420           (cond ((eq next previous)
10421                  (delete-region (- (point) 2) (point))
10422                  (put-text-property (point) (1+ (point))
10423                                     'face 'bold))
10424                 ((eq next ?_)
10425                  (delete-region (1- (point)) (1+ (point)))
10426                  (put-text-property (1- (point)) (point)
10427                                     'face 'underline))
10428                 ((eq previous ?_)
10429                  (delete-region (- (point) 2) (point))
10430                  (put-text-property (point) (1+ (point))
10431                                     'face 'underline))))))))
10432
10433 (defun gnus-article-word-wrap ()
10434   "Format too long lines."
10435   (interactive)
10436   (save-excursion
10437     (set-buffer gnus-article-buffer)
10438     (let ((buffer-read-only nil))
10439       (goto-char (point-min))
10440       (search-forward "\n\n" nil t)
10441       (end-of-line 1)
10442       (let ((paragraph-start "^[>|#:<;* ]*[ \t]*$")
10443             (adaptive-fill-regexp "[ \t]*\\([|#:<;>*]+ *\\)?")
10444             (adaptive-fill-mode t))
10445         (while (not (eobp))
10446           (and (>= (current-column) (min fill-column (window-width)))
10447                (/= (preceding-char) ?:)
10448                (fill-paragraph nil))
10449           (end-of-line 2))))))
10450
10451 (defun gnus-article-remove-cr ()
10452   "Remove carriage returns from an article."
10453   (interactive)
10454   (save-excursion
10455     (set-buffer gnus-article-buffer)
10456     (let ((buffer-read-only nil))
10457       (goto-char (point-min))
10458       (while (search-forward "\r" nil t)
10459         (replace-match "" t t)))))
10460
10461 (defun gnus-article-display-x-face (&optional force)
10462   "Look for an X-Face header and display it if present."
10463   (interactive (list 'force))
10464   (save-excursion
10465     (set-buffer gnus-article-buffer)
10466     (let ((inhibit-point-motion-hooks t)
10467           (case-fold-search nil))
10468       (save-restriction
10469         (goto-char (point-min))
10470         (search-forward "\n\n")
10471         (narrow-to-region (point-min) (point))
10472         (goto-char (point-min))
10473         (if (or (not gnus-article-x-face-command)
10474                 (and (not force)
10475                      (or (not gnus-article-x-face-too-ugly)
10476                          (string-match gnus-article-x-face-too-ugly
10477                                        (mail-fetch-field "from"))))
10478                 (progn
10479                   (goto-char (point-min))
10480                   (not (re-search-forward "^X-Face: " nil t))))
10481             nil
10482           (let ((beg (point))
10483                 (end (1- (re-search-forward "^\\($\\|[^ \t]\\)" nil t))))
10484             (if (symbolp gnus-article-x-face-command)
10485                 (and (or (fboundp gnus-article-x-face-command)
10486                          (error "%s is not a function"
10487                                 gnus-article-x-face-command))
10488                      (funcall gnus-article-x-face-command beg end))
10489               (call-process-region beg end "sh" nil 0 nil
10490                                    "-c" gnus-article-x-face-command))))))))
10491
10492 (defun gnus-article-de-quoted-unreadable (&optional force)
10493   "Do a naïve translation of a quoted-printable-encoded article.
10494 This is in no way, shape or form meant as a replacement for real MIME
10495 processing, but is simply a stop-gap measure until MIME support is
10496 written.
10497 If FORCE, decode the article whether it is marked as quoted-printable
10498 or not." 
10499   (interactive (list 'force))
10500   (save-excursion
10501     (set-buffer gnus-article-buffer)
10502     (let ((case-fold-search t)
10503           (buffer-read-only nil)
10504           (type (gnus-fetch-field "content-transfer-encoding")))
10505       (if (or force (and type (string-match "quoted-printable" type)))
10506           (progn
10507             (goto-char (point-min))
10508             (search-forward "\n\n" nil 'move)
10509             (gnus-mime-decode-quoted-printable (point) (point-max)))))))
10510
10511 (defun gnus-mime-decode-quoted-printable (from to)
10512   ;; Decode quoted-printable from region between FROM and TO.
10513   (save-excursion
10514     (goto-char from)
10515     (while (search-forward "=" to t)
10516       (cond ((eq (following-char) ?\n)
10517              (delete-char -1)
10518              (delete-char 1))
10519             ((looking-at "[0-9A-F][0-9A-F]")
10520              (delete-char -1)
10521              (insert (hexl-hex-string-to-integer
10522                       (buffer-substring (point) (+ 2 (point)))))
10523              (delete-char 2))
10524             ((gnus-message 3 "Malformed MIME quoted-printable message"))))))
10525
10526 (defvar gnus-article-time-units
10527   (list (cons 'year (* 365.25 24 60 60))
10528         (cons 'week (* 7 24 60 60))
10529         (cons 'day (* 24 60 60))
10530         (cons 'hour (* 60 60))
10531         (cons 'minute 60)
10532         (cons 'second 1)))
10533
10534 (defun gnus-article-date-ut (&optional type)
10535   "Convert DATE date to universal time in the current article.
10536 If TYPE is `local', convert to local time; if it is `lapsed', output
10537 how much time has lapsed since DATE."
10538   (interactive (list 'ut))
10539   (let ((date (header-date (or gnus-current-headers 
10540                                (gnus-get-header-by-number
10541                                 (gnus-summary-article-number))"")))
10542         (date-regexp "^Date: \\|^X-Sent: "))
10543     (if (or (not date)
10544             (string= date ""))
10545         ()
10546       (save-excursion
10547         (set-buffer gnus-article-buffer)
10548         (let ((buffer-read-only nil))
10549           (goto-char (point-min))
10550           (if (and (re-search-forward date-regexp nil t)
10551                    (progn 
10552                      (beginning-of-line)
10553                      (looking-at date-regexp)))
10554               (delete-region (gnus-point-at-bol)
10555                              (progn (end-of-line) (1+ (point))))
10556             (goto-char (point-min))
10557             (goto-char (- (search-forward "\n\n") 2)))
10558           (insert
10559            (cond 
10560             ((eq type 'local)
10561              (concat "Date: " (condition-case ()
10562                                   (timezone-make-date-arpa-standard date)
10563                                 (error date))
10564                      "\n"))
10565             ((eq type 'ut)
10566              (concat "Date: "
10567                      (condition-case ()
10568                          (timezone-make-date-arpa-standard date nil "UT")
10569                        (error date))
10570                      "\n"))
10571             ((eq type 'lapsed)
10572              ;; If the date is seriously mangled, the timezone
10573              ;; functions are liable to bug out, so we condition-case
10574              ;; the entire thing.  
10575              (let* ((sec (condition-case ()
10576                              (max (- (gnus-seconds-since-epoch 
10577                                       (timezone-make-date-arpa-standard
10578                                        (current-time-string) 
10579                                        (current-time-zone) "UT"))
10580                                      (gnus-seconds-since-epoch 
10581                                       (timezone-make-date-arpa-standard 
10582                                        date nil "UT")))
10583                                   0)
10584                            (error 0)))
10585                     num prev)
10586                (concat
10587                 "X-Sent: "
10588                 (mapconcat 
10589                  (lambda (unit)
10590                    (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
10591                        ""
10592                      (setq sec (- sec (* num (cdr unit))))
10593                      (prog1
10594                          (concat (if prev ", " "") (int-to-string (floor num))
10595                                  " " (symbol-name (car unit))
10596                                  (if (> num 1) "s" ""))
10597                        (setq prev t))))
10598                  gnus-article-time-units "")
10599                 " ago\n")))
10600             (t
10601              (error "Unknown conversion type: %s" type)))))))))
10602
10603 (defun gnus-article-date-local ()
10604   "Convert the current article date to the local timezone."
10605   (interactive)
10606   (gnus-article-date-ut 'local))
10607
10608 (defun gnus-article-date-lapsed ()
10609   "Convert the current article date to time lapsed since it was sent."
10610   (interactive)
10611   (gnus-article-date-ut 'lapsed))
10612
10613 (defun gnus-article-maybe-highlight ()
10614   (if gnus-visual (gnus-article-highlight)))
10615
10616 ;; Article savers.
10617
10618 (defun gnus-output-to-rmail (file-name)
10619   "Append the current article to an Rmail file named FILE-NAME."
10620   (require 'rmail)
10621   ;; Most of these codes are borrowed from rmailout.el.
10622   (setq file-name (expand-file-name file-name))
10623   (setq rmail-default-rmail-file file-name)
10624   (let ((artbuf (current-buffer))
10625         (tmpbuf (get-buffer-create " *Gnus-output*")))
10626     (save-excursion
10627       (or (get-file-buffer file-name)
10628           (file-exists-p file-name)
10629           (if (gnus-yes-or-no-p
10630                (concat "\"" file-name "\" does not exist, create it? "))
10631               (let ((file-buffer (create-file-buffer file-name)))
10632                 (save-excursion
10633                   (set-buffer file-buffer)
10634                   (rmail-insert-rmail-file-header)
10635                   (let ((require-final-newline nil))
10636                     (write-region (point-min) (point-max) file-name t 1)))
10637                 (kill-buffer file-buffer))
10638             (error "Output file does not exist")))
10639       (set-buffer tmpbuf)
10640       (buffer-disable-undo (current-buffer))
10641       (erase-buffer)
10642       (insert-buffer-substring artbuf)
10643       (gnus-convert-article-to-rmail)
10644       ;; Decide whether to append to a file or to an Emacs buffer.
10645       (let ((outbuf (get-file-buffer file-name)))
10646         (if (not outbuf)
10647             (append-to-file (point-min) (point-max) file-name)
10648           ;; File has been visited, in buffer OUTBUF.
10649           (set-buffer outbuf)
10650           (let ((buffer-read-only nil)
10651                 (msg (and (boundp 'rmail-current-message)
10652                           (symbol-value 'rmail-current-message))))
10653             ;; If MSG is non-nil, buffer is in RMAIL mode.
10654             (if msg
10655                 (progn (widen)
10656                        (narrow-to-region (point-max) (point-max))))
10657             (insert-buffer-substring tmpbuf)
10658             (if msg
10659                 (progn
10660                   (goto-char (point-min))
10661                   (widen)
10662                   (search-backward "\^_")
10663                   (narrow-to-region (point) (point-max))
10664                   (goto-char (1+ (point-min)))
10665                   (rmail-count-new-messages t)
10666                   (rmail-show-message msg)))))))
10667     (kill-buffer tmpbuf)))
10668
10669 (defun gnus-output-to-file (file-name)
10670   "Append the current article to a file named FILE-NAME."
10671   (setq file-name (expand-file-name file-name))
10672   (let ((artbuf (current-buffer))
10673         (tmpbuf (get-buffer-create " *Gnus-output*")))
10674     (save-excursion
10675       (set-buffer tmpbuf)
10676       (buffer-disable-undo (current-buffer))
10677       (erase-buffer)
10678       (insert-buffer-substring artbuf)
10679       ;; Append newline at end of the buffer as separator, and then
10680       ;; save it to file.
10681       (goto-char (point-max))
10682       (insert "\n")
10683       (append-to-file (point-min) (point-max) file-name))
10684     (kill-buffer tmpbuf)))
10685
10686 (defun gnus-convert-article-to-rmail ()
10687   "Convert article in current buffer to Rmail message format."
10688   (let ((buffer-read-only nil))
10689     ;; Convert article directly into Babyl format.
10690     ;; Suggested by Rob Austein <sra@lcs.mit.edu>
10691     (goto-char (point-min))
10692     (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
10693     (while (search-forward "\n\^_" nil t) ;single char
10694       (replace-match "\n^_" t t))               ;2 chars: "^" and "_"
10695     (goto-char (point-max))
10696     (insert "\^_")))
10697
10698 (defun gnus-narrow-to-page (&optional arg)
10699   "Make text outside current page invisible except for page delimiter.
10700 A numeric arg specifies to move forward or backward by that many pages,
10701 thus showing a page other than the one point was originally in."
10702   (interactive "P")
10703   (setq arg (if arg (prefix-numeric-value arg) 0))
10704   (save-excursion
10705     (forward-page -1)                   ;Beginning of current page.
10706     (widen)
10707     (if (> arg 0)
10708         (forward-page arg)
10709       (if (< arg 0)
10710           (forward-page (1- arg))))
10711     ;; Find the end of the page.
10712     (forward-page)
10713     ;; If we stopped due to end of buffer, stay there.
10714     ;; If we stopped after a page delimiter, put end of restriction
10715     ;; at the beginning of that line.
10716     ;; These are commented out.
10717     ;;    (if (save-excursion (beginning-of-line)
10718     ;;                  (looking-at page-delimiter))
10719     ;;  (beginning-of-line))
10720     (narrow-to-region (point)
10721                       (progn
10722                         ;; Find the top of the page.
10723                         (forward-page -1)
10724                         ;; If we found beginning of buffer, stay there.
10725                         ;; If extra text follows page delimiter on same line,
10726                         ;; include it.
10727                         ;; Otherwise, show text starting with following line.
10728                         (if (and (eolp) (not (bobp)))
10729                             (forward-line 1))
10730                         (point)))))
10731
10732 (defun gnus-gmt-to-local ()
10733   "Rewrite Date header described in GMT to local in current buffer.
10734 Intended to be used with gnus-article-prepare-hook."
10735   (save-excursion
10736     (save-restriction
10737       (widen)
10738       (goto-char (point-min))
10739       (narrow-to-region (point-min)
10740                         (progn (search-forward "\n\n" nil 'move) (point)))
10741       (goto-char (point-min))
10742       (if (re-search-forward "^Date:[ \t]\\(.*\\)$" nil t)
10743           (let ((buffer-read-only nil)
10744                 (date (buffer-substring-no-properties
10745                        (match-beginning 1) (match-end 1))))
10746             (delete-region (match-beginning 1) (match-end 1))
10747             (insert
10748              (timezone-make-date-arpa-standard 
10749               date nil (current-time-zone))))))))
10750
10751
10752 ;; Article mode commands
10753
10754 (defun gnus-article-next-page (lines)
10755   "Show next page of current article.
10756 If end of article, return non-nil. Otherwise return nil.
10757 Argument LINES specifies lines to be scrolled up."
10758   (interactive "P")
10759   (move-to-window-line -1)
10760   ;; Fixed by enami@ptgd.sony.co.jp (enami tsugutomo)
10761   (if (save-excursion
10762         (end-of-line)
10763         (and (pos-visible-in-window-p)  ;Not continuation line.
10764              (eobp)))
10765       ;; Nothing in this page.
10766       (if (or (not gnus-break-pages)
10767               (save-excursion
10768                 (save-restriction
10769                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
10770           t                             ;Nothing more.
10771         (gnus-narrow-to-page 1)         ;Go to next page.
10772         nil)
10773     ;; More in this page.
10774     (condition-case ()
10775         (scroll-up lines)
10776       (end-of-buffer
10777        ;; Long lines may cause an end-of-buffer error.
10778        (goto-char (point-max))))
10779     nil))
10780
10781 (defun gnus-article-prev-page (lines)
10782   "Show previous page of current article.
10783 Argument LINES specifies lines to be scrolled down."
10784   (interactive "P")
10785   (move-to-window-line 0)
10786   (if (and gnus-break-pages
10787            (bobp)
10788            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
10789       (progn
10790         (gnus-narrow-to-page -1) ;Go to previous page.
10791         (goto-char (point-max))
10792         (recenter -1))
10793     (scroll-down lines)))
10794
10795 (defun gnus-article-refer-article ()
10796   "Read article specified by message-id around point."
10797   (interactive)
10798   (search-forward ">" nil t)    ;Move point to end of "<....>".
10799   (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
10800       (let ((message-id
10801              (buffer-substring (match-beginning 1) (match-end 1))))
10802         (set-buffer gnus-summary-buffer)
10803         (gnus-summary-refer-article message-id))
10804     (error "No references around point")))
10805
10806 (defun gnus-article-show-summary ()
10807   "Reconfigure windows to show summary buffer."
10808   (interactive)
10809   (gnus-configure-windows 'article)
10810   (gnus-summary-goto-subject gnus-current-article))
10811
10812 (defun gnus-article-describe-briefly ()
10813   "Describe article mode commands briefly."
10814   (interactive)
10815   (gnus-message 6
10816    (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")))
10817
10818 (defun gnus-article-summary-command ()
10819   "Execute the last keystroke in the summary buffer."
10820   (interactive)
10821   (let ((obuf (current-buffer))
10822         (owin (current-window-configuration))
10823         func)
10824     (switch-to-buffer gnus-summary-buffer 'norecord)
10825     (setq func (lookup-key (current-local-map) (this-command-keys)))
10826     (call-interactively func)
10827     (set-buffer obuf)
10828     (set-window-configuration owin)
10829     (set-window-start (get-buffer-window (current-buffer)) (point))))
10830
10831 (defun gnus-article-summary-command-nosave ()
10832   "Execute the last keystroke in the summary buffer."
10833   (interactive)
10834   (let (func)
10835     (switch-to-buffer gnus-summary-buffer 'norecord)
10836     (setq func (lookup-key (current-local-map) (this-command-keys)))
10837     (call-interactively func)))
10838
10839 ;; caesar-region written by phr@prep.ai.mit.edu  Nov 86
10840 ;; Modified by tower@prep Nov 86
10841 ;; Modified by umerin@flab.flab.Fujitsu.JUNET for ROT47.
10842
10843 (defun gnus-caesar-region (&optional n)
10844   "Caesar rotation of region by N, default 13, for decrypting netnews.
10845 ROT47 will be performed for Japanese text in any case."
10846   (interactive (if current-prefix-arg   ; Was there a prefix arg?
10847                    (list (prefix-numeric-value current-prefix-arg))
10848                  (list nil)))
10849   (cond ((not (numberp n)) (setq n 13))
10850         (t (setq n (mod n 26))))        ;canonicalize N
10851   (if (not (zerop n))           ; no action needed for a rot of 0
10852       (progn
10853         (if (or (not (boundp 'caesar-translate-table))
10854                 (not caesar-translate-table)
10855                 (/= (aref caesar-translate-table ?a) (+ ?a n)))
10856             (let ((i 0) 
10857                   (lower "abcdefghijklmnopqrstuvwxyz")
10858                   upper)
10859               (gnus-message 9 "Building caesar-translate-table...")
10860               (setq caesar-translate-table (make-vector 256 0))
10861               (while (< i 256)
10862                 (aset caesar-translate-table i i)
10863                 (setq i (1+ i)))
10864               (setq lower (concat lower lower)
10865                     upper (upcase lower)
10866                     i 0)
10867               (while (< i 26)
10868                 (aset caesar-translate-table (+ ?a i) (aref lower (+ i n)))
10869                 (aset caesar-translate-table (+ ?A i) (aref upper (+ i n)))
10870                 (setq i (1+ i)))
10871               ;; ROT47 for Japanese text.
10872               ;; Thanks to ichikawa@flab.fujitsu.junet.
10873               (setq i 161)
10874               (let ((t1 (logior ?O 128))
10875                     (t2 (logior ?! 128))
10876                     (t3 (logior ?~ 128)))
10877                 (while (< i 256)
10878                   (aset caesar-translate-table i
10879                         (let ((v (aref caesar-translate-table i)))
10880                           (if (<= v t1) (if (< v t2) v (+ v 47))
10881                             (if (<= v t3) (- v 47) v))))
10882                   (setq i (1+ i))))
10883               (gnus-message 9 "Building caesar-translate-table...done")))
10884         (let ((from (region-beginning))
10885               (to (region-end))
10886               (i 0) str len)
10887           (setq str (buffer-substring from to))
10888           (setq len (length str))
10889           (while (< i len)
10890             (aset str i (aref caesar-translate-table (aref str i)))
10891             (setq i (1+ i)))
10892           (goto-char from)
10893           (delete-region from to)
10894           (insert str)))))
10895
10896 \f
10897 ;; Basic ideas by emv@math.lsa.umich.edu (Edward Vielmetti)
10898
10899 ;;;###autoload
10900 (defalias 'gnus-batch-kill 'gnus-batch-score)
10901 ;;;###autoload
10902 (defun gnus-batch-score ()
10903   "Run batched scoring.
10904 Usage: emacs -batch -l gnus -f gnus-batch-score <newsgroups> ...
10905 Newsgroups is a list of strings in Bnews format.  If you want to score
10906 the comp hierarchy, you'd say \"comp.all\". If you would not like to
10907 score the alt hierarchy, you'd say \"!alt.all\"."
10908   (interactive)
10909   (let* ((yes-and-no
10910           (gnus-newsrc-parse-options
10911            (apply (function concat)
10912                   (mapcar (lambda (g) (concat g " "))
10913                           command-line-args-left))))
10914          (gnus-expert-user t)
10915          (nnmail-spool-file nil)
10916          (gnus-use-dribble-file nil)
10917          (yes (car yes-and-no))
10918          (no (cdr yes-and-no))
10919          group newsrc entry
10920          ;; Disable verbose message.
10921          gnus-novice-user gnus-large-newsgroup)
10922     ;; Eat all arguments.
10923     (setq command-line-args-left nil)
10924     ;; Start Gnus.
10925     (gnus)
10926     ;; Apply kills to specified newsgroups in command line arguments.
10927     (setq newsrc (cdr gnus-newsrc-alist))
10928     (while newsrc
10929       (setq group (car (car newsrc)))
10930       (setq entry (gnus-gethash group gnus-newsrc-hashtb))
10931       (if (and (<= (nth 1 (car newsrc)) gnus-level-subscribed)
10932                (and (car entry)
10933                     (or (eq (car entry) t)
10934                         (not (zerop (car entry)))))
10935                (if yes (string-match yes group) t)
10936                (or (null no) (not (string-match no group))))
10937           (progn
10938             (gnus-summary-read-group group nil t)
10939             (and (eq (current-buffer) (get-buffer gnus-summary-buffer))
10940                  (gnus-summary-exit))))
10941       (setq newsrc (cdr newsrc)))
10942     ;; Exit Emacs.
10943     (switch-to-buffer gnus-group-buffer)
10944     (gnus-group-save-newsrc)))
10945
10946 (defun gnus-apply-kill-file ()
10947   "Apply a kill file to the current newsgroup.
10948 Returns the number of articles marked as read."
10949   (if (or (file-exists-p (gnus-newsgroup-kill-file nil))
10950           (file-exists-p (gnus-newsgroup-kill-file gnus-newsgroup-name)))
10951       (gnus-apply-kill-file-internal)
10952     0))
10953
10954 (defun gnus-kill-save-kill-buffer ()
10955   (save-excursion
10956     (let ((file (gnus-newsgroup-kill-file gnus-newsgroup-name)))
10957       (if (get-file-buffer file)
10958           (progn
10959             (set-buffer (get-file-buffer file))
10960             (and (buffer-modified-p) (save-buffer))
10961             (kill-buffer (current-buffer)))))))
10962
10963 (defvar gnus-kill-file-name "KILL"
10964   "Suffix of the kill files.")
10965
10966 (defun gnus-newsgroup-kill-file (newsgroup)
10967   "Return the name of a kill file name for NEWSGROUP.
10968 If NEWSGROUP is nil, return the global kill file name instead."
10969   (cond ((or (null newsgroup)
10970              (string-equal newsgroup ""))
10971          ;; The global KILL file is placed at top of the directory.
10972          (expand-file-name gnus-kill-file-name
10973                            (or gnus-kill-files-directory "~/News")))
10974         ((gnus-use-long-file-name 'not-kill)
10975          ;; Append ".KILL" to newsgroup name.
10976          (expand-file-name (concat newsgroup "." gnus-kill-file-name)
10977                            (or gnus-kill-files-directory "~/News")))
10978         (t
10979          ;; Place "KILL" under the hierarchical directory.
10980          (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
10981                                    "/" gnus-kill-file-name)
10982                            (or gnus-kill-files-directory "~/News")))))
10983
10984 \f
10985 ;;;
10986 ;;; Dribble file
10987 ;;;
10988
10989 (defvar gnus-dribble-ignore nil)
10990
10991 (defun gnus-dribble-file-name ()
10992   (concat gnus-startup-file "-dribble"))
10993
10994 (defun gnus-dribble-open ()
10995   (save-excursion 
10996     (set-buffer 
10997      (setq gnus-dribble-buffer (find-file-noselect (gnus-dribble-file-name))))
10998     (buffer-disable-undo (current-buffer))
10999     (bury-buffer gnus-dribble-buffer)
11000     (auto-save-mode t)
11001     (goto-char (point-max))))
11002
11003 (defun gnus-dribble-enter (string)
11004   (if (and (not gnus-dribble-ignore)
11005            gnus-dribble-buffer
11006            (buffer-name gnus-dribble-buffer))
11007       (let ((obuf (current-buffer)))
11008         (set-buffer gnus-dribble-buffer)
11009         (insert string "\n")
11010         (set-window-point (get-buffer-window (current-buffer)) (point-max))
11011         (set-buffer obuf))))
11012
11013 (defun gnus-dribble-read-file ()
11014   (let ((dribble-file (gnus-dribble-file-name)))
11015     (save-excursion 
11016       (set-buffer (setq gnus-dribble-buffer 
11017                         (get-buffer-create 
11018                          (file-name-nondirectory dribble-file))))
11019       (gnus-add-current-to-buffer-list)
11020       (erase-buffer)
11021       (set-visited-file-name dribble-file)
11022       (buffer-disable-undo (current-buffer))
11023       (bury-buffer (current-buffer))
11024       (set-buffer-modified-p nil)
11025       (let ((auto (make-auto-save-file-name))
11026             (gnus-dribble-ignore t))
11027         (if (or (file-exists-p auto) (file-exists-p dribble-file))
11028             (progn
11029               (if (file-newer-than-file-p auto dribble-file)
11030                   (setq dribble-file auto))
11031               (insert-file-contents dribble-file)
11032               (if (not (zerop (buffer-size)))
11033                   (set-buffer-modified-p t))
11034               (if (gnus-y-or-n-p 
11035                    "Auto-save file exists. Do you want to read it? ")
11036                   (progn
11037                     (gnus-message 5 "Reading %s..." dribble-file) 
11038                     (eval-buffer (current-buffer))
11039                     (gnus-message 5 "Reading %s...done" dribble-file)))))))))
11040
11041 (defun gnus-dribble-delete-file ()
11042   (if (file-exists-p (gnus-dribble-file-name))
11043       (delete-file (gnus-dribble-file-name)))
11044   (if gnus-dribble-buffer
11045       (save-excursion
11046         (set-buffer gnus-dribble-buffer)
11047         (let ((auto (make-auto-save-file-name)))
11048           (if (file-exists-p auto)
11049               (delete-file auto))
11050           (erase-buffer)
11051           (set-buffer-modified-p nil)))))
11052
11053 (defun gnus-dribble-save ()
11054   (if (and gnus-dribble-buffer
11055            (buffer-name gnus-dribble-buffer))
11056       (save-excursion
11057         (set-buffer gnus-dribble-buffer)
11058         (save-buffer))))
11059
11060 (defun gnus-dribble-clear ()
11061   (save-excursion
11062     (if (gnus-buffer-exists-p gnus-dribble-buffer)
11063         (progn
11064           (set-buffer gnus-dribble-buffer)
11065           (erase-buffer)
11066           (set-buffer-modified-p nil)
11067           (setq buffer-saved-size (buffer-size))))))
11068
11069 ;;;
11070 ;;; Server Communication
11071 ;;;
11072
11073 ;; All the Gnus backends have the same interface, and should return
11074 ;; data in a similar format. Below is an overview of what functions
11075 ;; these packages must supply and what results they should return.
11076 ;;
11077 ;; Variables:
11078 ;;
11079 ;; `nntp-server-buffer' - All data should be returned to Gnus in this
11080 ;; buffer. 
11081 ;;
11082 ;; Functions for the imaginary backend `choke':
11083 ;;
11084 ;; `choke-retrieve-headers ARTICLES &optional GROUP SERVER'
11085 ;; Should return all headers for all ARTICLES, or return NOV lines for
11086 ;; the same.
11087 ;;
11088 ;; `choke-request-group GROUP &optional SERVER DISCARD'
11089 ;; Switch to GROUP. If DISCARD is nil, active information on the group
11090 ;; must be returned.
11091 ;;
11092 ;; `choke-close-group GROUP &optional SERVER'
11093 ;; Close group. Most backends won't have to do anything with this
11094 ;; call, but it is an opportunity to clean up, if that is needed. It
11095 ;; is called when Gnus exits a group.
11096 ;;
11097 ;; `choke-request-article ARTICLE &optional GROUP SERVER'
11098 ;; Return ARTICLE, which is either an article number or
11099 ;; message-id. Note that not all backends can return articles based on
11100 ;; message-id. 
11101 ;;
11102 ;; `choke-request-list SERVER'
11103 ;; Return a list of all newsgroups on SERVER.
11104 ;;
11105 ;; `choke-request-list-newsgroups SERVER'
11106 ;; Return a list of descriptions of all newsgroups on SERVER.
11107 ;;
11108 ;; `choke-request-newgroups DATE &optional SERVER'
11109 ;; Return a list of all groups that have arrived after DATE on
11110 ;; SERVER. Note that the date doesn't have to be respected - Gnus will
11111 ;; always check whether the groups are old or not. Backends that do
11112 ;; not store date information may just return the entire list of
11113 ;; groups, although this might not be a good idea in general.
11114 ;;
11115 ;; `choke-request-post-buffer METHOD HEADER ARTICLE-BUFFER GROUP INFO'
11116 ;; Should return a buffer that is suitable for "posting". nnspool and
11117 ;; nntp return a `*post-buffer*', and nnmail return a `*mail*'
11118 ;; buffer. This function should fill out the appropriate headers. 
11119 ;;
11120 ;; `choke-request-post &optional SERVER'
11121 ;; Function that will be called from a buffer to be posted. 
11122 ;;
11123 ;; `choke-open-server SERVER &optional ARGUMENT'
11124 ;; Open a connection to SERVER.
11125 ;;
11126 ;; `choke-close-server &optional SERVER'
11127 ;; Close the connection to SERVER.
11128 ;;
11129 ;; `choke-server-opened &optional SERVER'
11130 ;; Whether the conenction to SERVER is opened or not.
11131 ;;
11132 ;; `choke-server-status &optional SERVER'
11133 ;; Should return a status string (not in the nntp buffer, but as the
11134 ;; result of the function).
11135 ;;
11136 ;; `choke-retrieve-groups GROUPS &optional SERVER'
11137 ;; Optional function for retrieving active file info on all groups in
11138 ;; GROUPS.  Two return formats are supported: The normal active file
11139 ;; format, and a list of GROUP lines.  This function should return (as
11140 ;; a function value) either `active' or `group', depending on what
11141 ;; format it returns.
11142 ;;
11143 ;; The following functions are optional and apply only to backends
11144 ;; that are able to control the contents of their groups totally
11145 ;; (ie. mail backends.)  Backends that aren't able to do that
11146 ;; shouldn't define these functions at all. Gnus will check for their
11147 ;; presence before attempting to call them.
11148 ;;
11149 ;; `choke-request-expire-articles ARTICLES &optional NEWSGROUP SERVER'
11150 ;; Should expire (according to some aging scheme) all ARTICLES. Most
11151 ;; backends will not be able to expire articles. Should return a list
11152 ;; of all articles that were not expired.
11153 ;;
11154 ;; `choke-request-move-article ARTICLE GROUP SERVER ACCEPT-FORM &optional LAST'
11155 ;; Should move ARTICLE from GROUP on SERVER by using ACCEPT-FORM.
11156 ;; Removes any information it has added to the article (extra headers,
11157 ;; whatever - make it as clean as possible), and then passes the
11158 ;; article on by evaling ACCEPT-FORM, which is normally a call to the
11159 ;; function described below. If the ACCEPT-FORM returns a non-nil
11160 ;; value, the article should then be deleted. If LAST is nil, that
11161 ;; means that there will be further calls to this function. This might
11162 ;; be taken as an advice not to save buffers/internal variables just
11163 ;; yet, but wait until the last call to speed things up.
11164 ;;
11165 ;; `choke-request-accept-article GROUP &optional LAST' 
11166 ;; The contents of the current buffer will be put into GROUP.  There
11167 ;; should, of course, be an article in the current buffer.  This
11168 ;; function is normally only called by the function described above,
11169 ;; and LAST works the same way as in that function.
11170 ;;
11171 ;; `choke-request-replace-article ARTICLE GROUP BUFFER'
11172 ;; Replace ARTICLE in GROUP with the contents of BUFFER.
11173 ;; This provides an easy interface for allowing editing of
11174 ;; articles. Note that even headers may be edited, so the backend has
11175 ;; to update any tables (nov buffers, etc) that it maintains after
11176 ;; replacing the article.
11177 ;;
11178 ;; `choke-request-create-group GROUP &optional SERVER'
11179 ;; Create GROUP on SERVER.  This might be a new, empty group, or it
11180 ;; might be a group that already exists, but hasn't been registered
11181 ;; yet. 
11182 ;;
11183 ;; All these functions must return nil if they couldn't service the
11184 ;; request. If the optional arguments are not supplied, some "current"
11185 ;; or "default" values should be used. In short, one should emulate an
11186 ;; NNTP server, in a way.
11187 ;;
11188 ;; If you want to write a new backend, you just have to supply the
11189 ;; functions listed above. In addition, you must enter the new backend
11190 ;; into the list of valid select methods:
11191 ;; (setq gnus-valid-select-methods 
11192 ;;       (cons '("choke" mail) gnus-valid-select-methods))
11193 ;; The first element in this list is the name of the backend. Other
11194 ;; elemnets may be `mail' (for mail groups),  `post' (for news
11195 ;; groups), `none' (neither), `respool' (for groups that can control
11196 ;; their contents). 
11197
11198 (defun gnus-start-news-server (&optional confirm)
11199   "Open a method for getting news.
11200 If CONFIRM is non-nil, the user will be asked for an NNTP server."
11201   (let (how)
11202     (if gnus-current-select-method
11203         ;; Stream is already opened.
11204         nil
11205       ;; Open NNTP server.
11206       (if (null gnus-nntp-service) (setq gnus-nntp-server nil))
11207       (if confirm
11208           (progn
11209             ;; Read server name with completion.
11210             (setq gnus-nntp-server
11211                   (completing-read "NNTP server: "
11212                                    (mapcar (lambda (server) (list server))
11213                                            (cons (list gnus-nntp-server)
11214                                                  gnus-secondary-servers))
11215                                    nil nil gnus-nntp-server))))
11216
11217       (if (and gnus-nntp-server 
11218                (stringp gnus-nntp-server)
11219                (not (string= gnus-nntp-server "")))
11220           (setq gnus-select-method
11221                 (cond ((or (string= gnus-nntp-server "")
11222                            (string= gnus-nntp-server "::"))
11223                        (list 'nnspool (system-name)))
11224                       ((string-match "^:" gnus-nntp-server)
11225                        (list 'nnmh gnus-nntp-server 
11226                              (list 'nnmh-directory 
11227                                    (file-name-as-directory
11228                                     (expand-file-name
11229                                      (concat "~/" (substring
11230                                                    gnus-nntp-server 1)))))
11231                              (list 'nnmh-get-new-mail nil)))
11232                       (t
11233                        (list 'nntp gnus-nntp-server)))))
11234
11235       (setq how (car gnus-select-method))
11236       (cond ((eq how 'nnspool)
11237              (require 'nnspool)
11238              (gnus-message 5 "Looking up local news spool..."))
11239             ((eq how 'nnmh)
11240              (require 'nnmh)
11241              (gnus-message 5 "Looking up mh spool..."))
11242             (t
11243              (require 'nntp)))
11244       (setq gnus-current-select-method gnus-select-method)
11245       (run-hooks 'gnus-open-server-hook)
11246       (or 
11247        ;; gnus-open-server-hook might have opened it
11248        (gnus-server-opened gnus-select-method)  
11249        (gnus-open-server gnus-select-method)
11250        (gnus-y-or-n-p
11251         (format
11252          "%s server on %s can't be opened. Continue? "
11253          (car gnus-select-method) (nth 1 gnus-select-method)))
11254        (progn
11255          (gnus-message 1 "Couldn't open server on %s" 
11256                        (nth 1 gnus-select-method))
11257          (ding)
11258          nil)))))
11259
11260 (defun gnus-check-news-server (&optional method)
11261   "If the news server is down, start it up again."
11262   (let ((method (if method method gnus-select-method)))
11263     (and (stringp method)
11264          (setq method (gnus-server-to-method method)))
11265     (if (gnus-server-opened method)
11266         ;; Stream is already opened.
11267         t
11268       ;; Open server.
11269       (gnus-message 5 "Opening server %s on %s..." (car method) (nth 1 method))
11270       (run-hooks 'gnus-open-server-hook)
11271       (or (gnus-server-opened method)
11272           (gnus-open-server method))
11273       (message ""))))
11274
11275 (defun gnus-nntp-message (&optional message)
11276   "Check the status of the NNTP server.
11277 If the status of the server is clear and MESSAGE is non-nil, MESSAGE
11278 is returned insted of the status string."
11279   (let ((status (gnus-status-message (gnus-find-method-for-group 
11280                                       gnus-newsgroup-name)))
11281         (message (or message "")))
11282     (if (and (stringp status) (> (length status) 0))
11283         status message)))
11284
11285 (defun gnus-get-function (method function)
11286   (and (stringp method)
11287        (setq method (gnus-server-to-method method)))
11288   (let ((func (intern (format "%s-%s" (car method) function))))
11289     (if (not (fboundp func)) 
11290         (progn
11291           (require (car method))
11292           (if (not (fboundp func)) 
11293               (error "No such function: %s" func))))
11294     func))
11295
11296 ;;; Interface functions to the backends.
11297
11298 (defun gnus-open-server (method)
11299   (funcall (gnus-get-function method 'open-server)
11300            (nth 1 method) (nthcdr 2 method)))
11301
11302 (defun gnus-close-server (method)
11303   (funcall (gnus-get-function method 'close-server) (nth 1 method)))
11304
11305 (defun gnus-request-list (method)
11306   (funcall (gnus-get-function method 'request-list) (nth 1 method)))
11307
11308 (defun gnus-request-list-newsgroups (method)
11309   (funcall (gnus-get-function method 'request-list-newsgroups) (nth 1 method)))
11310
11311 (defun gnus-request-newgroups (date method)
11312   (funcall (gnus-get-function method 'request-newgroups) 
11313            date (nth 1 method)))
11314
11315 (defun gnus-server-opened (method)
11316   (funcall (gnus-get-function method 'server-opened) (nth 1 method)))
11317
11318 (defun gnus-status-message (method)
11319   (let ((method (if (stringp method) (gnus-find-method-for-group method)
11320                   method)))
11321     (funcall (gnus-get-function method 'status-message) (nth 1 method))))
11322
11323 (defun gnus-request-group (group &optional dont-check)
11324   (let ((method (gnus-find-method-for-group group)))
11325 ;    (and t (message "%s GROUP %s" (car method) group))
11326     (funcall (gnus-get-function method 'request-group) 
11327              (gnus-group-real-name group) (nth 1 method) dont-check)))
11328
11329 (defun gnus-request-asynchronous (group &optional articles)
11330   (let ((method (gnus-find-method-for-group group)))
11331     (funcall (gnus-get-function method 'request-asynchronous) 
11332              (gnus-group-real-name group) (nth 1 method) articles)))
11333
11334 (defun gnus-list-active-group (group)
11335   (let ((method (gnus-find-method-for-group group))
11336         (func 'list-active-group))
11337     (and (gnus-check-backend-function func group)
11338          (funcall (gnus-get-function method func) 
11339                   (gnus-group-real-name group) (nth 1 method)))))
11340
11341 (defun gnus-request-group-description (group)
11342   (let ((method (gnus-find-method-for-group group))
11343         (func 'request-group-description))
11344     (and (gnus-check-backend-function func group)
11345          (funcall (gnus-get-function method func) 
11346                   (gnus-group-real-name group) (nth 1 method)))))
11347
11348 (defun gnus-close-group (group)
11349   (let ((method (gnus-find-method-for-group group)))
11350     (funcall (gnus-get-function method 'close-group) 
11351              (gnus-group-real-name group) (nth 1 method))))
11352
11353 (defun gnus-retrieve-headers (articles group)
11354   (let ((method (gnus-find-method-for-group group)))
11355     (if (and gnus-use-cache (numberp (car articles)))
11356         (gnus-cache-retrieve-headers articles group)
11357       (funcall (gnus-get-function method 'retrieve-headers) 
11358                articles (gnus-group-real-name group) (nth 1 method)))))
11359
11360 (defun gnus-retrieve-groups (groups method)
11361   (funcall (gnus-get-function method 'retrieve-groups) groups (nth 1 method)))
11362
11363 (defun gnus-request-article (article group &optional buffer)
11364   (let ((method (gnus-find-method-for-group group)))
11365     (funcall (gnus-get-function method 'request-article) 
11366              article (gnus-group-real-name group) (nth 1 method) buffer)))
11367
11368 (defun gnus-request-head (article group)
11369   (let ((method (gnus-find-method-for-group group)))
11370     (funcall (gnus-get-function method 'request-head) 
11371              article (gnus-group-real-name group) (nth 1 method))))
11372
11373 ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
11374 (defun gnus-request-post-buffer (post group subject header artbuf
11375                                       info follow-to respect-poster)
11376    (let* ((info (or info (and group (nth 2 (gnus-gethash 
11377                                             group gnus-newsrc-hashtb)))))
11378           (method
11379            (if (and gnus-post-method
11380                     ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
11381                     (memq 'post (assoc
11382                                  (format "%s" (car (gnus-find-method-for-group
11383                                                     gnus-newsgroup-name)))
11384                                         gnus-valid-select-methods)))
11385                gnus-post-method
11386              (gnus-find-method-for-group gnus-newsgroup-name))))
11387      (or (gnus-server-opened method)
11388          (gnus-open-server method)
11389          (error "Can't open server %s:%s" (car method) (nth 1 method)))
11390      (let ((mail-self-blind nil)
11391            (mail-archive-file-name nil))
11392        (funcall (gnus-get-function method 'request-post-buffer) 
11393                 post group subject header artbuf info follow-to
11394                 respect-poster))))
11395
11396 (defun gnus-request-post (method &optional force)
11397   (and (stringp method)
11398        (setq method (gnus-server-to-method method)))
11399   (and (not force) gnus-post-method
11400        (memq 'post (assoc (format "%s" (car method))
11401                           gnus-valid-select-methods))
11402        (setq method gnus-post-method))
11403   (funcall (gnus-get-function method 'request-post) 
11404            (nth 1 method)))
11405
11406 (defun gnus-request-expire-articles (articles group &optional force)
11407   (let ((method (gnus-find-method-for-group group)))
11408     (funcall (gnus-get-function method 'request-expire-articles) 
11409              articles (gnus-group-real-name group) (nth 1 method)
11410              force)))
11411
11412 (defun gnus-request-move-article 
11413   (article group server accept-function &optional last)
11414   (let ((method (gnus-find-method-for-group group)))
11415     (funcall (gnus-get-function method 'request-move-article) 
11416              article (gnus-group-real-name group) 
11417              (nth 1 method) accept-function last)))
11418
11419 (defun gnus-request-accept-article (group &optional last)
11420   (let ((func (if (symbolp group) group
11421                 (car (gnus-find-method-for-group group)))))
11422     (funcall (intern (format "%s-request-accept-article" func))
11423              (if (stringp group) (gnus-group-real-name group) group)
11424              last)))
11425
11426 (defun gnus-request-replace-article (article group buffer)
11427   (let ((func (car (gnus-find-method-for-group group))))
11428     (funcall (intern (format "%s-request-replace-article" func))
11429              article (gnus-group-real-name group) buffer)))
11430
11431 (defun gnus-request-create-group (group)
11432   (let ((method (gnus-find-method-for-group group)))
11433     (funcall (gnus-get-function method 'request-create-group) 
11434              (gnus-group-real-name group) (nth 1 method))))
11435
11436 (defun gnus-member-of-valid (symbol group)
11437   (memq symbol (assoc
11438                 (format "%s" (car (gnus-find-method-for-group group)))
11439                 gnus-valid-select-methods)))
11440
11441 (defsubst gnus-secondary-method-p (method)
11442   (member method gnus-secondary-select-methods))
11443
11444 (defun gnus-find-method-for-group (group &optional info)
11445   (or gnus-override-method
11446       (and (not group)
11447            gnus-select-method)
11448       (let ((info (or info (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
11449             method)
11450         (if (or (not info)
11451                 (not (setq method (nth 4 info))))
11452             (setq method gnus-select-method)
11453           (setq method
11454                 (cond ((stringp method)
11455                        (gnus-server-to-method method))
11456                       ((stringp (car method))
11457                        (gnus-server-extend-method group method))
11458                       (t
11459                        method))))
11460         (gnus-server-add-address method))))
11461
11462 (defun gnus-check-backend-function (func group)
11463   (let ((method (if (stringp group) (car (gnus-find-method-for-group group))
11464                  group)))
11465     (fboundp (intern (format "%s-%s" method func)))))
11466
11467 (defun gnus-methods-using (method)
11468   (let ((valids gnus-valid-select-methods)
11469         outs)
11470     (while valids
11471       (if (memq method (car valids)) 
11472           (setq outs (cons (car valids) outs)))
11473       (setq valids (cdr valids)))
11474     outs))
11475
11476 ;;; 
11477 ;;; Active & Newsrc File Handling
11478 ;;;
11479
11480 ;; Newsrc related functions.
11481 ;; Gnus internal format of gnus-newsrc-alist:
11482 ;; (("alt.general" 3 (1 . 1))
11483 ;;  ("alt.misc"    3 ((1 . 10) (12 . 15)))
11484 ;;  ("alt.test"    7 (1 . 99) (45 57 93)) ...)
11485 ;; The first item is the group name; the second is the subscription
11486 ;; level; the third is either a range of a list of ranges of read
11487 ;; articles, the optional fourth element is a list of marked articles,
11488 ;; the optional fifth element is the select method.
11489 ;;
11490 ;; Gnus internal format of gnus-newsrc-hashtb:
11491 ;; (95 ("alt.general" 3 (1 . 1)) ("alt.misc" 3 ((1 . 10) (12 . 15))) ...)
11492 ;; This is the entry for "alt.misc". The first element is the number
11493 ;; of unread articles in "alt.misc". The cdr of this entry is the
11494 ;; element *before* "alt.misc" in gnus-newsrc-alist, which makes is
11495 ;; trivial to remove or add new elements into gnus-newsrc-alist
11496 ;; without scanning the entire list. So, to get the actual information
11497 ;; of "alt.misc", you'd say something like 
11498 ;; (nth 2 (gnus-gethash "alt.misc" gnus-newsrc-hashtb))
11499 ;;
11500 ;; Gnus internal format of gnus-active-hashtb:
11501 ;; ((1 . 1))
11502 ;;  (5 . 10))
11503 ;;  (67 . 99)) ...)
11504 ;; The only element in each entry in this hash table is a range of
11505 ;; (possibly) available articles. (Articles in this range may have
11506 ;; been expired or canceled.)
11507 ;;
11508 ;; Gnus internal format of gnus-killed-list and gnus-zombie-list:
11509 ;; ("alt.misc" "alt.test" "alt.general" ...)
11510
11511 (defun gnus-setup-news (&optional rawfile level)
11512   "Setup news information.
11513 If RAWFILE is non-nil, the .newsrc file will also be read.
11514 If LEVEL is non-nil, the news will be set up at level LEVEL."
11515   (let ((init (not (and gnus-newsrc-alist gnus-active-hashtb (not rawfile)))))
11516     ;; Clear some variables to re-initialize news information.
11517     (if init (setq gnus-newsrc-alist nil gnus-active-hashtb nil))
11518
11519     ;; Read the newsrc file and create `gnus-newsrc-hashtb'.
11520     (if init (gnus-read-newsrc-file rawfile))
11521
11522     ;; Read the active file and create `gnus-active-hashtb'.
11523     ;; If `gnus-read-active-file' is nil, then we just create an empty
11524     ;; hash table. The partial filling out of the hash table will be
11525     ;; done in `gnus-get-unread-articles'.
11526     (if (and gnus-read-active-file 
11527              (not level))
11528         (gnus-read-active-file)
11529       (setq gnus-active-hashtb (make-vector 4095 0)))
11530
11531     (and init gnus-use-dribble-file (gnus-dribble-read-file))
11532
11533     ;; Find the number of unread articles in each non-dead group.
11534     (gnus-get-unread-articles (or level (1+ gnus-level-subscribed)))
11535     ;; Find new newsgroups and treat them.
11536     (if (and init gnus-check-new-newsgroups gnus-read-active-file (not level)
11537              (gnus-server-opened gnus-select-method))
11538         (gnus-find-new-newsgroups))
11539     (if (and init gnus-check-bogus-newsgroups 
11540              gnus-read-active-file (not level)
11541              (gnus-server-opened gnus-select-method))
11542         (gnus-check-bogus-newsgroups))))
11543
11544 (defun gnus-find-new-newsgroups ()
11545   "Search for new newsgroups and add them.
11546 Each new newsgroup will be treated with `gnus-subscribe-newsgroup-method.'
11547 The `-n' option line from .newsrc is respected."
11548   (interactive)
11549   (or (gnus-check-first-time-used)
11550       (if (or (consp gnus-check-new-newsgroups)
11551               (eq gnus-check-new-newsgroups 'ask-server))
11552           (gnus-ask-server-for-new-groups)
11553         (let ((groups 0)
11554               group new-newsgroups)
11555           (or gnus-have-read-active-file (gnus-read-active-file))
11556           (setq gnus-newsrc-last-checked-date (current-time-string))
11557           (if (not gnus-killed-hashtb) (gnus-make-hashtable-from-killed))
11558           ;; Go though every newsgroup in `gnus-active-hashtb' and compare
11559           ;; with `gnus-newsrc-hashtb' and `gnus-killed-hashtb'.
11560           (mapatoms
11561            (lambda (sym)
11562              (setq group (symbol-name sym))
11563              (if (or (gnus-gethash group gnus-killed-hashtb)
11564                      (gnus-gethash group gnus-newsrc-hashtb))
11565                  ()
11566                (let ((do-sub (gnus-matches-options-n group)))
11567                  (cond ((eq do-sub 'subscribe)
11568                         (setq groups (1+ groups))
11569                         (gnus-sethash group group gnus-killed-hashtb)
11570                         (funcall 
11571                          gnus-subscribe-options-newsgroup-method group))
11572                        ((eq do-sub 'ignore)
11573                         nil)
11574                        (t
11575                         (setq groups (1+ groups))
11576                         (gnus-sethash group group gnus-killed-hashtb)
11577                         (if gnus-subscribe-hierarchical-interactive
11578                             (setq new-newsgroups (cons group new-newsgroups))
11579                           (funcall gnus-subscribe-newsgroup-method group)))))))
11580            gnus-active-hashtb)
11581           (if new-newsgroups 
11582               (gnus-subscribe-hierarchical-interactive new-newsgroups))
11583           ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
11584           (if (> groups 0)
11585               (gnus-message 6 "%d new newsgroup%s arrived." 
11586                             groups (if (> groups 1) "s have" " has")))))))
11587
11588 (defun gnus-matches-options-n (group)
11589   ;; Returns `subscribe' if the group is to be uncoditionally
11590   ;; subscribed, `ignore' if it is to be ignored, and nil if there is
11591   ;; no match for the group.
11592
11593   ;; First we check the two user variables.
11594   (cond
11595    ((and gnus-options-subscribe
11596          (string-match gnus-options-subscribe group))
11597     'subscribe)
11598    ((and gnus-options-not-subscribe
11599          (string-match gnus-options-not-subscribe group))
11600     'ignore)
11601    ;; Then we go through the list that was retrieved from the .newsrc
11602    ;; file.  This list has elements on the form 
11603    ;; `(REGEXP . {ignore,subscribe})'. The first match found (the list
11604    ;; is in the reverse order of the options line) is returned.
11605    (t
11606     (let ((regs gnus-newsrc-options-n))
11607       (while (and regs
11608                   (not (string-match (car (car regs)) group)))
11609         (setq regs (cdr regs)))
11610       (and regs (cdr (car regs)))))))
11611
11612 (defun gnus-ask-server-for-new-groups ()
11613   (let* ((date (or gnus-newsrc-last-checked-date (current-time-string)))
11614          (methods (cons gnus-select-method 
11615                         (append
11616                          (and (consp gnus-check-new-newsgroups)
11617                               gnus-check-new-newsgroups)
11618                          gnus-secondary-select-methods)))
11619          (groups 0)
11620          (new-date (current-time-string))
11621          hashtb group new-newsgroups got-new)
11622     ;; Go thorugh both primary and secondary select methods and
11623     ;; request new newsgroups.  
11624     (while methods
11625       (and (or (gnus-server-opened (car methods))
11626                (gnus-open-server (car methods)))
11627            (gnus-request-newgroups date (car methods))
11628            (save-excursion
11629              (setq got-new t)
11630              (set-buffer nntp-server-buffer)
11631              (or hashtb (setq hashtb (gnus-make-hashtable 
11632                                       (count-lines (point-min) (point-max)))))
11633              ;; Enter all the new groups in a hashtable.
11634              (gnus-active-to-gnus-format (car methods) hashtb 'ignore)))
11635       (setq methods (cdr methods)))
11636     (and got-new (setq gnus-newsrc-last-checked-date new-date))
11637     ;; Now all new groups from all select methods are in `hashtb'.
11638     (mapatoms
11639      (lambda (group-sym)
11640        (setq group (symbol-name group-sym))
11641        (if (or (gnus-gethash group gnus-newsrc-hashtb)
11642                (member group gnus-zombie-list)
11643                (member group gnus-killed-list))
11644            ;; The group is already known.
11645            ()
11646          (and (symbol-value group-sym)
11647               (gnus-sethash group (symbol-value group-sym) gnus-active-hashtb))
11648          (let ((do-sub (gnus-matches-options-n group)))
11649            (cond ((eq do-sub 'subscribe)
11650                   (setq groups (1+ groups))
11651                   (gnus-sethash group group gnus-killed-hashtb)
11652                   (funcall 
11653                    gnus-subscribe-options-newsgroup-method group))
11654                  ((eq do-sub 'ignore)
11655                   nil)
11656                  (t
11657                   (setq groups (1+ groups))
11658                   (gnus-sethash group group gnus-killed-hashtb)
11659                   (if gnus-subscribe-hierarchical-interactive
11660                       (setq new-newsgroups (cons group new-newsgroups))
11661                     (funcall gnus-subscribe-newsgroup-method group)))))))
11662      hashtb)
11663     (if new-newsgroups 
11664         (gnus-subscribe-hierarchical-interactive new-newsgroups))
11665     ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
11666     (if (> groups 0)
11667         (gnus-message 6 "%d new newsgroup%s arrived." 
11668                       groups (if (> groups 1) "s have" " has")))
11669     got-new))
11670
11671 (defun gnus-check-first-time-used ()
11672   (if (or (> (length gnus-newsrc-alist) 1)
11673           (file-exists-p gnus-startup-file)
11674           (file-exists-p (concat gnus-startup-file ".el"))
11675           (file-exists-p (concat gnus-startup-file ".eld")))
11676       nil
11677     (gnus-message 6 "First time user; subscribing you to default groups")
11678     (or gnus-have-read-active-file (gnus-read-active-file))
11679     (setq gnus-newsrc-last-checked-date (current-time-string))
11680     (let ((groups gnus-default-subscribed-newsgroups)
11681           group)
11682       (if (eq groups t)
11683           nil
11684         (setq groups (or groups gnus-backup-default-subscribed-newsgroups))
11685         (mapatoms
11686          (lambda (sym)
11687            (setq group (symbol-name sym))
11688            (let ((do-sub (gnus-matches-options-n group)))
11689              (cond ((eq do-sub 'subscribe)
11690                     (gnus-sethash group group gnus-killed-hashtb)
11691                     (funcall 
11692                      gnus-subscribe-options-newsgroup-method group))
11693                    ((eq do-sub 'ignore)
11694                     nil)
11695                    (t
11696                     (setq gnus-killed-list (cons group gnus-killed-list))))))
11697          gnus-active-hashtb)
11698         (while groups
11699           (if (gnus-gethash (car groups) gnus-active-hashtb)
11700               (gnus-group-change-level 
11701                (car groups) gnus-level-default-subscribed gnus-level-killed))
11702           (setq groups (cdr groups)))
11703         (gnus-group-make-help-group)
11704         (and gnus-novice-user
11705              (gnus-message 7 "`A k' to list killed groups"))))))
11706
11707 (defun gnus-subscribe-group (group previous &optional method)
11708   (gnus-group-change-level 
11709    (if method
11710        (list t group gnus-level-default-subscribed nil nil method)
11711      group) 
11712    gnus-level-default-subscribed gnus-level-killed previous t))
11713
11714 ;; `gnus-group-change-level' is the fundamental function for changing
11715 ;; subscription levels of newsgroups. This might mean just changing
11716 ;; from level 1 to 2, which is pretty trivial, from 2 to 6 or back
11717 ;; again, which subscribes/unsubscribes a group, which is equally
11718 ;; trivial. Changing from 1-7 to 8-9 means that you kill a group, and
11719 ;; from 8-9 to 1-7 means that you remove the group from the list of
11720 ;; killed (or zombie) groups and add them to the (kinda) subscribed
11721 ;; groups. And last but not least, moving from 8 to 9 and 9 to 8,
11722 ;; which is trivial.
11723 ;; ENTRY can either be a string (newsgroup name) or a list (if
11724 ;; FROMKILLED is t, it's a list on the format (NUM INFO-LIST),
11725 ;; otherwise it's a list in the format of the `gnus-newsrc-hashtb'
11726 ;; entries. 
11727 ;; LEVEL is the new level of the group, OLDLEVEL is the old level and
11728 ;; PREVIOUS is the group (in hashtb entry format) to insert this group
11729 ;; after. 
11730 (defun gnus-group-change-level (entry level &optional oldlevel
11731                                       previous fromkilled)
11732   (let (group info active num)
11733     ;; Glean what info we can from the arguments
11734     (if (consp entry)
11735         (if fromkilled (setq group (nth 1 entry))
11736           (setq group (car (nth 2 entry))))
11737       (setq group entry))
11738     (if (and (stringp entry)
11739              oldlevel 
11740              (< oldlevel gnus-level-zombie))
11741         (setq entry (gnus-gethash entry gnus-newsrc-hashtb)))
11742     (if (and (not oldlevel)
11743              (consp entry))
11744         (setq oldlevel (car (cdr (nth 2 entry)))))
11745     (if (stringp previous)
11746         (setq previous (gnus-gethash previous gnus-newsrc-hashtb)))
11747
11748     (gnus-dribble-enter
11749      (format "(gnus-group-change-level %S %S %S %S %S)" 
11750              group level oldlevel (car (nth 2 previous)) fromkilled))
11751     
11752     ;; Then we remove the newgroup from any old structures, if needed.
11753     ;; If the group was killed, we remove it from the killed or zombie
11754     ;; list. If not, and it is in fact going to be killed, we remove
11755     ;; it from the newsrc hash table and assoc.
11756     (cond ((>= oldlevel gnus-level-zombie)
11757            (if (= oldlevel gnus-level-zombie)
11758                (setq gnus-zombie-list (delete group gnus-zombie-list))
11759              (setq gnus-killed-list (delete group gnus-killed-list))))
11760           (t
11761            (if (>= level gnus-level-zombie)
11762                (progn
11763                  (gnus-sethash (car (nth 2 entry))
11764                                nil gnus-newsrc-hashtb)
11765                  (if (nth 3 entry)
11766                      (setcdr (gnus-gethash (car (nth 3 entry))
11767                                            gnus-newsrc-hashtb)
11768                              (cdr entry)))
11769                  (setcdr (cdr entry) (cdr (cdr (cdr entry))))))))
11770
11771     ;; Finally we enter (if needed) the list where it is supposed to
11772     ;; go, and change the subscription level. If it is to be killed,
11773     ;; we enter it into the killed or zombie list.
11774     (cond ((>= level gnus-level-zombie)
11775            ;; Remove from the hash table.
11776            (gnus-sethash group nil gnus-newsrc-hashtb)
11777            (or (gnus-group-foreign-p group)
11778                ;; We do not enter foreign groups into the list of dead
11779                ;; groups.  
11780                (if (= level gnus-level-zombie)
11781                    (setq gnus-zombie-list (cons group gnus-zombie-list))
11782                  (setq gnus-killed-list (cons group gnus-killed-list)))))
11783           (t
11784            ;; If the list is to be entered into the newsrc assoc, and
11785            ;; it was killed, we have to create an entry in the newsrc
11786            ;; hashtb format and fix the pointers in the newsrc assoc.
11787            (if (>= oldlevel gnus-level-zombie)
11788                (progn
11789                  (if (listp entry)
11790                      (progn
11791                        (setq info (cdr entry))
11792                        (setq num (car entry)))
11793                    (setq active (gnus-gethash group gnus-active-hashtb))
11794                    (setq num (if active (- (1+ (cdr active)) (car active)) t))
11795                    ;; Check whether the group is foreign. If so, the
11796                    ;; foreign select method has to be entered into the
11797                    ;; info. 
11798                    (let ((method (gnus-group-method-name group)))
11799                      (if (eq method gnus-select-method)
11800                          (setq info (list group level nil))
11801                        (setq info (list group level nil nil method)))))
11802                  (or previous 
11803                      (setq previous 
11804                            (let ((p gnus-newsrc-alist))
11805                              (while (cdr (cdr p))
11806                                (setq p (cdr p)))
11807                              p)))
11808                  (setq entry (cons info (cdr (cdr previous))))
11809                  (if (cdr previous)
11810                      (progn
11811                        (setcdr (cdr previous) entry)
11812                        (gnus-sethash group (cons num (cdr previous)) 
11813                                      gnus-newsrc-hashtb))
11814                    (setcdr previous entry)
11815                    (gnus-sethash group (cons num previous)
11816                                  gnus-newsrc-hashtb))
11817                  (if (cdr entry)
11818                      (setcdr (gnus-gethash (car (car (cdr entry)))
11819                                            gnus-newsrc-hashtb)
11820                              entry)))
11821              ;; It was alive, and it is going to stay alive, so we
11822              ;; just change the level and don't change any pointers or
11823              ;; hash table entries.
11824              (setcar (cdr (car (cdr (cdr entry)))) level))))))
11825
11826 (defun gnus-kill-newsgroup (newsgroup)
11827   "Obsolete function. Kills a newsgroup."
11828   (gnus-group-change-level
11829    (gnus-gethash newsgroup gnus-newsrc-hashtb) gnus-level-killed))
11830
11831 (defun gnus-check-bogus-newsgroups (&optional confirm)
11832   "Remove bogus newsgroups.
11833 If CONFIRM is non-nil, the user has to confirm the deletion of every
11834 newsgroup." 
11835   (let ((newsrc (cdr gnus-newsrc-alist))
11836         bogus group entry)
11837     (gnus-message 5 "Checking bogus newsgroups...")
11838     (or gnus-have-read-active-file (gnus-read-active-file))
11839     ;; Find all bogus newsgroup that are subscribed.
11840     (while newsrc
11841       (setq group (car (car newsrc)))
11842       (if (or (gnus-gethash group gnus-active-hashtb) ; Active
11843               (nth 4 (car newsrc))      ; Foreign
11844               (and confirm
11845                    (not (gnus-y-or-n-p
11846                          (format "Remove bogus newsgroup: %s " group)))))
11847           ;; Don't remove.
11848           ()
11849         ;; Found a bogus newsgroup.
11850         (setq bogus (cons group bogus)))
11851       (setq newsrc (cdr newsrc)))
11852     ;; Remove all bogus subscribed groups by first killing them, and
11853     ;; then removing them from the list of killed groups.
11854     (while bogus
11855       (and (setq entry (gnus-gethash (car bogus) gnus-newsrc-hashtb))
11856            (progn
11857              (gnus-group-change-level entry gnus-level-killed)
11858              (setq gnus-killed-list (delete (car bogus) gnus-killed-list))))
11859       (setq bogus (cdr bogus)))
11860     ;; Then we remove all bogus groups from the list of killed and
11861     ;; zombie groups. They are are removed without confirmation.
11862     (let ((dead-lists '(gnus-killed-list gnus-zombie-list))
11863           killed)
11864       (while dead-lists
11865         (setq killed (symbol-value (car dead-lists)))
11866         (while killed
11867           (setq group (car killed))
11868           (or (gnus-gethash group gnus-active-hashtb)
11869               ;; The group is bogus.
11870               (set (car dead-lists)
11871                    (delete group (symbol-value (car dead-lists)))))
11872           (setq killed (cdr killed)))
11873         (setq dead-lists (cdr dead-lists))))
11874     (gnus-message 5 "Checking bogus newsgroups...done")))
11875
11876 (defun gnus-check-duplicate-killed-groups ()
11877   "Remove duplicates from the list of killed groups."
11878   (interactive)
11879   (let ((killed gnus-killed-list))
11880     (while killed
11881       (gnus-message 9 "%d" (length killed))
11882       (setcdr killed (delete (car killed) (cdr killed)))
11883       (setq killed (cdr killed)))))
11884
11885 ;; Go though `gnus-newsrc-alist' and compare with `gnus-active-hashtb'
11886 ;; and compute how many unread articles there are in each group.
11887 (defun gnus-get-unread-articles (&optional level) 
11888   (let* ((newsrc (cdr gnus-newsrc-alist))
11889          (level (or level (1+ gnus-level-subscribed)))
11890          (foreign-level
11891           (min 
11892            (cond ((and gnus-activate-foreign-newsgroups 
11893                        (not (numberp gnus-activate-foreign-newsgroups)))
11894                   (1+ gnus-level-subscribed))
11895                  ((numberp gnus-activate-foreign-newsgroups)
11896                   gnus-activate-foreign-newsgroups)
11897                  (t 0))
11898            level))
11899          info group active virtuals method)
11900     (gnus-message 5 "Checking new news...")
11901
11902     (while newsrc
11903       (setq info (car newsrc)
11904             group (car info)
11905             active (gnus-gethash group gnus-active-hashtb))
11906
11907       ;; Check newsgroups. If the user doesn't want to check them, or
11908       ;; they can't be checked (for instance, if the news server can't
11909       ;; be reached) we just set the number of unread articles in this
11910       ;; newsgroup to t. This means that Gnus thinks that there are
11911       ;; unread articles, but it has no idea how many.
11912       (if (and (setq method (nth 4 info))
11913                (not (gnus-server-equal gnus-select-method
11914                                        (gnus-server-get-method nil method)))
11915                (not (gnus-secondary-method-p method)))
11916           ;; These groups are foreign. Check the level.
11917           (if (<= (nth 1 info) foreign-level)
11918               (if (eq (car (if (stringp method) 
11919                                (gnus-server-to-method method)
11920                              (nth 4 info))) 'nnvirtual)
11921                   ;; We have to activate the virtual groups after all
11922                   ;; the others, so we just pop them on a list for
11923                   ;; now. 
11924                   (setq virtuals (cons info virtuals))
11925                 (and (setq active (gnus-activate-newsgroup (car info)))
11926                      ;; Close the groups as we look at them!
11927                      (gnus-close-group group))))
11928                 
11929         ;; These groups are native or secondary. 
11930         (if (and (not gnus-have-read-active-file)
11931                  (<= (nth 1 info) level))
11932             (setq active (gnus-activate-newsgroup (car info)))))
11933       
11934       (if active
11935           (gnus-get-unread-articles-in-group info active)
11936         ;; The group couldn't be reached, so we nix out the number of
11937         ;; unread articles and stuff.
11938         (gnus-sethash group nil gnus-active-hashtb)
11939         (setcar (gnus-gethash group gnus-newsrc-hashtb) t))
11940
11941       (setq newsrc (cdr newsrc)))
11942
11943     ;; Activate the virtual groups. This has to be done after all the
11944     ;; other groups. 
11945     ;; !!! If one virtual group contains another virtual group, even
11946     ;; doing it this way might cause problems.
11947    (while virtuals
11948       (and (setq active (gnus-activate-newsgroup (car (car virtuals))))
11949            (gnus-get-unread-articles-in-group (car virtuals) active))
11950       (setq virtuals (cdr virtuals)))
11951
11952     (gnus-message 5 "Checking new news...done")))
11953
11954 ;; Create a hash table out of the newsrc alist. The `car's of the
11955 ;; alist elements are used as keys.
11956 (defun gnus-make-hashtable-from-newsrc-alist ()
11957   (let ((alist gnus-newsrc-alist)
11958         (ohashtb gnus-newsrc-hashtb)
11959         prev)
11960     (setq gnus-newsrc-hashtb (gnus-make-hashtable (length alist)))
11961     (setq alist 
11962           (setq prev (setq gnus-newsrc-alist 
11963                            (if (equal (car (car gnus-newsrc-alist))
11964                                       "dummy.group")
11965                                gnus-newsrc-alist
11966                              (cons (list "dummy.group" 0 nil) alist)))))
11967     (while alist
11968       (gnus-sethash (car (car alist)) 
11969                     (cons (and ohashtb (car (gnus-gethash 
11970                                              (car (car alist)) ohashtb))) 
11971                           prev) gnus-newsrc-hashtb)
11972       (setq prev alist
11973             alist (cdr alist)))))
11974
11975 (defun gnus-make-hashtable-from-killed ()
11976   "Create a hash table from the killed and zombie lists."
11977   (let ((lists '(gnus-killed-list gnus-zombie-list))
11978         list)
11979     (setq gnus-killed-hashtb 
11980           (gnus-make-hashtable 
11981            (+ (length gnus-killed-list) (length gnus-zombie-list))))
11982     (while lists
11983       (setq list (symbol-value (car lists)))
11984       (setq lists (cdr lists))
11985       (while list
11986         (gnus-sethash (car list) (car list) gnus-killed-hashtb)
11987         (setq list (cdr list))))))
11988
11989 (defun gnus-get-unread-articles-in-group (info active)
11990   (let* ((range (nth 2 info))
11991          (num 0)
11992          (marked (nth 3 info)))
11993     ;; If a cache is present, we may have to alter the active info.
11994     (and gnus-use-cache
11995          (gnus-cache-possibly-alter-active (car info) active))
11996     ;; Modify the list of read articles according to what articles 
11997     ;; are available; then tally the unread articles and add the
11998     ;; number to the group hash table entry.
11999     (cond ((zerop (cdr active))
12000            (setq num 0))
12001           ((not range)
12002            (setq num (- (1+ (cdr active)) (car active))))
12003           ((not (listp (cdr range)))
12004            ;; Fix a single (num . num) range according to the
12005            ;; active hash table.
12006            ;; Fix by Carsten Bormann <cabo@Informatik.Uni-Bremen.DE>.
12007            (and (< (cdr range) (car active)) (setcdr range (1- (car active))))
12008            (and (> (cdr range) (cdr active)) (setcdr range (cdr active)))
12009            ;; Compute number of unread articles.
12010            (setq num (max 0 (- (cdr active) 
12011                                (- (1+ (cdr range)) (car range))))))
12012           (t
12013            ;; The read list is a list of ranges. Fix them according to
12014            ;; the active hash table.
12015            ;; First peel off any elements that are below the lower
12016            ;; active limit. 
12017            (while (and (cdr range) 
12018                        (>= (car active) 
12019                            (or (and (atom (car (cdr range))) (car (cdr range)))
12020                                (car (car (cdr range))))))
12021              (if (numberp (car range))
12022                  (setcar range 
12023                          (cons (car range) 
12024                                (or (and (numberp (car (cdr range)))
12025                                         (car (cdr range))) 
12026                                    (cdr (car (cdr range))))))
12027                (setcdr (car range) 
12028                        (or (and (numberp (nth 1 range)) (nth 1 range))
12029                            (cdr (car (cdr range))))))
12030              (setcdr range (cdr (cdr range))))
12031            ;; Adjust the first element to be the same as the lower limit. 
12032            (if (and (not (atom (car range))) 
12033                     (< (cdr (car range)) (car active)))
12034                (setcdr (car range) (1- (car active))))
12035            ;; Then we want to peel off any elements that are higher
12036            ;; than the upper active limit.  
12037            (let ((srange range))
12038              ;; Go past all legal elements.
12039              (while (and (cdr srange) 
12040                          (<= (or (and (atom (car (cdr srange)))
12041                                       (car (cdr srange)))
12042                                  (car (car (cdr srange)))) (cdr active)))
12043                (setq srange (cdr srange)))
12044              (if (cdr srange)
12045                  ;; Nuke all remaining illegal elements.
12046                  (setcdr srange nil))
12047
12048              ;; Adjust the final element.
12049              (if (and (not (atom (car srange)))
12050                       (> (cdr (car srange)) (cdr active)))
12051                  (setcdr (car srange) (cdr active))))
12052            ;; Compute the number of unread articles.
12053            (while range
12054              (setq num (+ num (- (1+ (or (and (atom (car range)) (car range))
12055                                          (cdr (car range))))
12056                                  (or (and (atom (car range)) (car range))
12057                                      (car (car range))))))
12058              (setq range (cdr range)))
12059            (setq num (max 0 (- (cdr active) num)))))
12060     (and info
12061          (progn
12062            (and (assq 'tick marked)
12063                 (inline (gnus-remove-illegal-marked-articles
12064                          (assq 'tick marked) (nth 2 info))))
12065            (and (assq 'dormant marked)
12066                 (inline (gnus-remove-illegal-marked-articles
12067                          (assq 'dormant marked) (nth 2 info))))
12068            (setcar
12069             (gnus-gethash (car info) gnus-newsrc-hashtb) 
12070             (setq num (max 0 (- num (length (cdr (assq 'tick marked)))
12071                                 (length (cdr (assq 'dormant marked)))))))))
12072     num))
12073
12074 (defun gnus-remove-illegal-marked-articles (marked ranges)
12075   (let ((m (cdr marked)))
12076     ;; Make sure that all ticked articles are a subset of the unread
12077     ;; articles. 
12078     (while m
12079       (if (gnus-member-of-range (car m) ranges)
12080           (setcdr marked (cdr m))
12081         (setq marked m))
12082       (setq m (cdr m)))))
12083
12084 (defun gnus-activate-newsgroup (group)
12085   (let ((method (gnus-find-method-for-group group))
12086         active)
12087     (and (or (gnus-server-opened method) (gnus-open-server method))
12088          (gnus-request-group group)
12089          (save-excursion
12090            (set-buffer nntp-server-buffer)
12091            (goto-char (point-min))
12092            (and (looking-at "[0-9]+ [0-9]+ \\([0-9]+\\) [0-9]+")
12093                 (progn
12094                   (goto-char (match-beginning 1))
12095                   (gnus-sethash 
12096                    group (setq active (cons (read (current-buffer))
12097                                             (read (current-buffer))))
12098                    gnus-active-hashtb))
12099                 active)))))
12100
12101 (defun gnus-update-read-articles 
12102   (group unread unselected ticked &optional domarks replied expirable killed
12103          dormant bookmark score)
12104   "Update the list of read and ticked articles in GROUP using the
12105 UNREAD and TICKED lists.
12106 Note: UNSELECTED has to be sorted over `<'.
12107 Returns whether the updating was successful."
12108   (let* ((active (or gnus-newsgroup-active 
12109                      (gnus-gethash group gnus-active-hashtb)))
12110          (entry (gnus-gethash group gnus-newsrc-hashtb))
12111          (info (nth 2 entry))
12112          (marked (nth 3 info))
12113          (prev 1)
12114          (unread (sort (copy-sequence unread) (function <)))
12115          read)
12116     (if (or (not info) (not active))
12117         ;; There is no info on this group if it was, in fact,
12118         ;; killed. Gnus stores no information on killed groups, so
12119         ;; there's nothing to be done. 
12120         ;; One could store the information somewhere temporarily,
12121         ;; perhaps... Hmmm... 
12122         ()
12123       ;; Remove any negative articles numbers.
12124       (while (and unread (< (car unread) 0))
12125         (setq unread (cdr unread)))
12126       ;; Remove any expired article numbers
12127       (while (and unread (< (car unread) (car active)))
12128         (setq unread (cdr unread)))
12129       (while (and ticked (< (car ticked) (car active)))
12130         (setq ticked (cdr ticked)))
12131       (while (and dormant (< (car dormant) (car active)))
12132         (setq dormant (cdr dormant)))
12133       (setq unread (sort (append unselected unread) '<))
12134       ;; Set the number of unread articles in gnus-newsrc-hashtb.
12135       (setcar entry (max 0 (- (length unread) (length ticked) 
12136                               (length dormant))))
12137       ;; Compute the ranges of read articles by looking at the list of
12138       ;; unread articles.  
12139       (while unread
12140         (if (/= (car unread) prev)
12141             (setq read (cons (if (= prev (1- (car unread))) prev
12142                                (cons prev (1- (car unread)))) read)))
12143         (setq prev (1+ (car unread)))
12144         (setq unread (cdr unread)))
12145       (if (<= prev (cdr active))
12146           (setq read (cons (cons prev (cdr active)) read)))
12147       ;; Enter this list into the group info.
12148       (setcar (cdr (cdr info)) 
12149               (if (> (length read) 1) (nreverse read) read))
12150       ;; Enter the list of ticked articles.
12151       (gnus-set-marked-articles 
12152        info ticked
12153        (if domarks replied (cdr (assq 'reply marked)))
12154        (if domarks expirable (cdr (assq 'expire marked)))
12155        (if domarks killed (cdr (assq 'killed marked)))
12156        (if domarks dormant (cdr (assq 'dormant marked)))
12157        (if domarks bookmark (cdr (assq 'bookmark marked)))
12158        (if domarks score (cdr (assq 'score marked))))
12159       t)))
12160
12161 (defun gnus-make-articles-unread (group articles)
12162   "Mark ARTICLES in GROUP as unread."
12163   (let* ((info (nth 2 (or (gnus-gethash group gnus-newsrc-hashtb)
12164                           (gnus-gethash (gnus-group-real-name group)
12165                                         gnus-newsrc-hashtb))))
12166          (ranges (nth 2 info))
12167          news)
12168     (while articles
12169       (and (gnus-member-of-range (car articles) ranges)
12170            (setq news (cons (car articles) news)))
12171       (setq articles (cdr articles)))
12172     (if (not news)
12173         ()
12174       (setcar (nthcdr 2 info)
12175               (gnus-remove-from-range (nth 2 info) (nreverse news)))
12176       (gnus-group-update-group group t))))
12177
12178 ;; Get the active file(s) from the backend(s).
12179 (defun gnus-read-active-file ()
12180   (gnus-group-set-mode-line)
12181   (let ((methods (if (or (gnus-server-opened gnus-select-method)
12182                          (gnus-open-server gnus-select-method))
12183                      ;; The native server is available.
12184                      (cons gnus-select-method gnus-secondary-select-methods)
12185                    ;; The native server is down, so we just do the
12186                    ;; secondary ones.   
12187                    gnus-secondary-select-methods))
12188         list-type)
12189     (setq gnus-have-read-active-file nil)
12190     (save-excursion
12191       (set-buffer nntp-server-buffer)
12192       (while methods
12193         (let* ((where (nth 1 (car methods)))
12194                (mesg (format "Reading active file%s via %s..."
12195                              (if (and where (not (zerop (length where))))
12196                                  (concat " from " where) "")
12197                              (car (car methods)))))
12198           (gnus-message 5 mesg)
12199           (gnus-check-news-server (car methods))
12200           (cond 
12201            ((and (eq gnus-read-active-file 'some)
12202                  (gnus-check-backend-function
12203                   'retrieve-groups (car (car methods))))
12204             (let ((newsrc (cdr gnus-newsrc-alist))
12205                   groups)
12206               (while newsrc
12207                 (and (gnus-server-equal 
12208                       (gnus-find-method-for-group
12209                        (car (car newsrc)) (car newsrc))
12210                       (gnus-server-get-method nil (car methods)))
12211                      (setq groups (cons (car (car newsrc)) groups)))
12212                 (setq newsrc (cdr newsrc)))
12213               (setq list-type (gnus-retrieve-groups groups (car methods)))
12214               (cond ((not list-type)
12215                      (gnus-message 
12216                       1 "Cannot read partial active file from %s server." 
12217                       (car (car methods)))
12218                      (ding)
12219                      (sit-for 2))
12220                     ((eq list-type 'active)
12221                      (gnus-active-to-gnus-format (car methods)))
12222                     (t
12223                      (gnus-groups-to-gnus-format (car methods))))))
12224            (t
12225             (if (not (gnus-request-list (car methods)))
12226                 (progn
12227                   (gnus-message 1 "Cannot read active file from %s server." 
12228                                 (car (car methods)))
12229                   (ding))
12230               (gnus-active-to-gnus-format (car methods))
12231               (setq gnus-have-read-active-file t)
12232               (gnus-message 5 "%sdone" mesg)))))
12233         (setq methods (cdr methods))))))
12234
12235 ;; Read an active file and place the results in `gnus-active-hashtb'.
12236 (defun gnus-active-to-gnus-format (method &optional hashtb ignore-errors)
12237   (let ((cur (current-buffer))
12238         (hashtb (or hashtb 
12239                     (if (and gnus-active-hashtb 
12240                              (not (equal method gnus-select-method)))
12241                         gnus-active-hashtb
12242                       (setq gnus-active-hashtb
12243                             (if (equal method gnus-select-method)
12244                                 (gnus-make-hashtable 
12245                                  (count-lines (point-min) (point-max)))
12246                               (gnus-make-hashtable 4096)))))))
12247     ;; Delete unnecessary lines.
12248     (goto-char (point-min))
12249     (while (search-forward "\nto." nil t)
12250       (delete-region (1+ (match-beginning 0)) 
12251                      (progn (forward-line 1) (point))))
12252     (or (string= gnus-ignored-newsgroups "")
12253         (progn
12254           (goto-char (point-min))
12255           (delete-matching-lines gnus-ignored-newsgroups)))
12256     ;; If these are groups from a foreign select method, we insert the
12257     ;; group prefix in front of the group names. 
12258     (and method (not (eq method gnus-select-method))
12259          (let ((prefix (gnus-group-prefixed-name "" method)))
12260            (goto-char (point-min))
12261            (while (and (not (eobp))
12262                        (progn (insert prefix)
12263                               (zerop (forward-line 1)))))))
12264     (goto-char (point-min))
12265     ;; Store active file in hashtable.
12266     (goto-char (point-min))
12267     (if (string-match "%[oO]" gnus-group-line-format)
12268         ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
12269         ;; If we want information on moderated groups, we use this
12270         ;; loop...   
12271         (let* ((mod-hashtb (make-vector 7 0))
12272                (m (intern "m" mod-hashtb))
12273                group max min)
12274           (while (not (eobp))
12275             (condition-case nil
12276                 (progn
12277                   (narrow-to-region (point) (gnus-point-at-eol))
12278                   (setq group (let ((obarray hashtb)) (read cur)))
12279                   (if (and (numberp (setq max (read cur)))
12280                            (numberp (setq min (read cur))))
12281                       (set group (cons min max))
12282                     (set group nil))
12283                   ;; Enter moderated groups into a list.
12284                   (if (eq (let ((obarray mod-hashtb)) (read cur)) m)
12285                       (setq gnus-moderated-list 
12286                             (cons (symbol-name group) gnus-moderated-list))))
12287               (error nil))
12288             (widen)
12289             (forward-line 1)))
12290       ;; And if we do not care about moderation, we use this loop,
12291       ;; which is faster.
12292       (let (group max min)
12293         (while (not (eobp))
12294           (condition-case ()
12295               (progn
12296                 (narrow-to-region (point) (gnus-point-at-eol))
12297                 ;; group gets set to a symbol interned in the hash table
12298                 ;; (what a hack!!)
12299                 (setq group (let ((obarray hashtb)) (read cur)))
12300                 (if (and (numberp (setq max (read cur)))
12301                          (numberp (setq min (read cur))))
12302                     (set group (cons min max))
12303                   (set group nil)))
12304             (error 
12305              (progn 
12306                (if ignore-errors
12307                    (set group nil)
12308                  (ding) 
12309                  (gnus-message 3 "Warning - illegal active: %s"
12310                                (buffer-substring 
12311                                 (gnus-point-at-bol) (gnus-point-at-eol)))
12312                  nil))))
12313           (widen)
12314           (forward-line 1))))))
12315
12316 (defun gnus-groups-to-gnus-format (method &optional hashtb)
12317   ;; Parse a "groups" active file.
12318   (let ((cur (current-buffer))
12319         (hashtb (or hashtb 
12320                     (if method
12321                         gnus-active-hashtb
12322                       (setq gnus-active-hashtb
12323                             (gnus-make-hashtable 
12324                              (count-lines (point-min) (point-max)))))))
12325         (prefix (and method (not (eq method gnus-select-method))
12326                      (gnus-group-prefixed-name "" method))))
12327
12328     (goto-char (point-min))
12329     (condition-case ()
12330         ;; We split this into to separate loops, one with the prefix
12331         ;; and one without to speed the reading up somewhat.
12332         (if prefix
12333             (let (min max opoint)
12334               (while (not (eobp))
12335                 (read cur) (read cur)
12336                 (setq min (read cur)
12337                       max (read cur)
12338                       opoint (point))
12339                 (skip-chars-forward " \t")
12340                 (insert prefix)
12341                 (goto-char opoint)
12342                 (set (let ((obarray hashtb)) (read cur)) 
12343                      (cons min max))
12344                 (forward-line 1)))
12345           (let (min max)
12346             (while (not (eobp))
12347               (if (= (following-char) ?2)
12348                   (progn
12349                     (read cur) (read cur)
12350                     (setq min (read cur)
12351                           max (read cur))
12352                     (set (let ((obarray hashtb)) (read cur)) 
12353                          (cons min max))))
12354               (forward-line 1))))
12355       (error 
12356        (progn (ding) (gnus-message 3 "Possible error in active file."))))))
12357
12358 (defun gnus-read-newsrc-file (&optional force)
12359   "Read startup file.
12360 If FORCE is non-nil, the .newsrc file is read."
12361   (setq gnus-current-startup-file (gnus-make-newsrc-file gnus-startup-file))
12362   ;; Reset variables that might be defined in the .newsrc.eld file.
12363   (let ((variables gnus-variable-list))
12364     (while variables
12365       (set (car variables) nil)
12366       (setq variables (cdr variables))))
12367   (let* ((newsrc-file gnus-current-startup-file)
12368          (quick-file (concat newsrc-file ".el")))
12369     (save-excursion
12370       ;; We always load the .newsrc.eld file. If always contains
12371       ;; much information that can not be gotten from the .newsrc
12372       ;; file (ticked articles, killed groups, foreign methods, etc.)
12373       (gnus-read-newsrc-el-file quick-file)
12374  
12375       (if (or force
12376               (and (file-newer-than-file-p newsrc-file quick-file)
12377                    (file-newer-than-file-p newsrc-file 
12378                                            (concat quick-file "d")))
12379               (not gnus-newsrc-alist))
12380           ;; We read the .newsrc file. Note that if there if a
12381           ;; .newsrc.eld file exists, it has already been read, and
12382           ;; the `gnus-newsrc-hashtb' has been created. While reading
12383           ;; the .newsrc file, Gnus will only use the information it
12384           ;; can find there for changing the data already read -
12385           ;; ie. reading the .newsrc file will not trash the data
12386           ;; already read (except for read articles).
12387           (save-excursion
12388             (gnus-message 5 "Reading %s..." newsrc-file)
12389             (set-buffer (find-file-noselect newsrc-file))
12390             (buffer-disable-undo (current-buffer))
12391             (gnus-newsrc-to-gnus-format)
12392             (kill-buffer (current-buffer))
12393             (gnus-message 5 "Reading %s...done" newsrc-file))))))
12394
12395 (defun gnus-read-newsrc-el-file (file)
12396   (let ((ding-file (concat file "d")))
12397     ;; We always, always read the .eld file.
12398     (gnus-message 5 "Reading %s..." ding-file)
12399     (let (gnus-newsrc-assoc)
12400       (condition-case nil
12401           (load ding-file t t t)
12402         (error nil))
12403       (and gnus-newsrc-assoc (setq gnus-newsrc-alist gnus-newsrc-assoc)))
12404     (let ((inhibit-quit t))
12405       (gnus-uncompress-newsrc-alist))
12406     (gnus-make-hashtable-from-newsrc-alist)
12407     (if (not (file-newer-than-file-p file ding-file))
12408         ()
12409       ;; Old format quick file
12410       (gnus-message 5 "Reading %s..." file)
12411       ;; The .el file is newer than the .eld file, so we read that one
12412       ;; as well. 
12413       (gnus-read-old-newsrc-el-file file))))
12414
12415 ;; Parse the old-style quick startup file
12416 (defun gnus-read-old-newsrc-el-file (file)
12417   (let (newsrc killed marked group m)
12418     (prog1
12419         (let ((gnus-killed-assoc nil)
12420               gnus-marked-assoc gnus-newsrc-alist gnus-newsrc-assoc)
12421           (prog1
12422               (condition-case nil
12423                   (load file t t t)
12424                 (error nil))
12425             (setq newsrc gnus-newsrc-assoc
12426                   killed gnus-killed-assoc
12427                   marked gnus-marked-assoc)))
12428       (setq gnus-newsrc-alist nil)
12429       (while newsrc
12430         (setq group (car newsrc))
12431         (let ((info (nth 2 (gnus-gethash (car group) gnus-newsrc-hashtb))))
12432           (if info
12433               (progn
12434                 (setcar (nthcdr 2 info) (cdr (cdr group)))
12435                 (setcar (cdr info)
12436                         (if (nth 1 group) gnus-level-default-subscribed 
12437                           gnus-level-default-unsubscribed))
12438                 (setq gnus-newsrc-alist (cons info gnus-newsrc-alist)))
12439             (setq gnus-newsrc-alist
12440                   (cons 
12441                    (setq info
12442                          (list (car group)
12443                                (if (nth 1 group) gnus-level-default-subscribed
12444                                  gnus-level-default-unsubscribed) 
12445                                (cdr (cdr group))))
12446                    gnus-newsrc-alist)))
12447           (if (setq m (assoc (car group) marked))
12448             (setcdr (cdr (cdr info)) (cons (list (cons 'tick (cdr m))) nil))))
12449         (setq newsrc (cdr newsrc)))
12450       (setq newsrc killed)
12451       (while newsrc
12452         (setcar newsrc (car (car newsrc)))
12453         (setq newsrc (cdr newsrc)))
12454       (setq gnus-killed-list killed))
12455     (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist))
12456     (gnus-make-hashtable-from-newsrc-alist)))
12457       
12458 (defun gnus-make-newsrc-file (file)
12459   "Make server dependent file name by catenating FILE and server host name."
12460   (let* ((file (expand-file-name file nil))
12461          (real-file (concat file "-" (nth 1 gnus-select-method))))
12462     (if (file-exists-p real-file)
12463         real-file file)))
12464
12465 (defun gnus-uncompress-newsrc-alist ()
12466   ;; Uncompress all lists of marked articles in the newsrc assoc.
12467   (let ((newsrc gnus-newsrc-alist)
12468         marked)
12469     (while newsrc
12470       (if (not (setq marked (nth 3 (car newsrc))))
12471           ()
12472         (while marked
12473           (or (eq 'score (car (car marked)))
12474               (eq 'bookmark (car (car marked)))
12475               (eq 'killed (car (car marked)))
12476               (setcdr (car marked) (gnus-uncompress-range (cdr (car marked)))))
12477           (setq marked (cdr marked))))
12478       (setq newsrc (cdr newsrc)))))
12479
12480 (defun gnus-compress-newsrc-alist ()
12481   ;; Compress all lists of marked articles in the newsrc assoc.
12482   (let ((newsrc gnus-newsrc-alist)
12483         marked)
12484     (while newsrc
12485       (if (not (setq marked (nth 3 (car newsrc))))
12486           ()
12487         (while marked
12488           (or (eq 'score (car (car marked)))
12489               (eq 'bookmark (car (car marked)))
12490               (eq 'killed (car (car marked)))
12491               (setcdr (car marked) 
12492                       (condition-case ()
12493                           (gnus-compress-sequence 
12494                            (sort (cdr (car marked)) '<) t)
12495                         (error (cdr (car marked))))))
12496           (setq marked (cdr marked))))
12497       (setq newsrc (cdr newsrc)))))
12498
12499 (defun gnus-newsrc-to-gnus-format ()
12500   (setq gnus-newsrc-options "")
12501   (setq gnus-newsrc-options-n nil)
12502
12503   (or gnus-active-hashtb
12504       (setq gnus-active-hashtb (make-vector 4095 0)))
12505   (let ((buf (current-buffer))
12506         (already-read (> (length gnus-newsrc-alist) 1))
12507         group subscribed options-symbol newsrc
12508         symbol reads num1)
12509     (goto-char (point-min))
12510     ;; We intern the symbol `options' in the active hashtb so that we
12511     ;; can `eq' against it later.
12512     (setq options-symbol (intern "options" gnus-active-hashtb))
12513   
12514     (while (not (eobp))
12515       ;; We first read the first word on the line by narrowing and
12516       ;; then reading into `gnus-active-hashtb'.  Most groups will
12517       ;; already exist in that hashtb, so this will save some string
12518       ;; space.
12519       (narrow-to-region
12520        (point)
12521        (progn (skip-chars-forward "^ \t!:\n") (point)))
12522       (goto-char (point-min))
12523       (setq symbol 
12524             (and (/= (point-min) (point-max))
12525                  (let ((obarray gnus-active-hashtb)) (read buf))))
12526       (widen)
12527       ;; Now, the symbol we have read is either `options' or a group
12528       ;; name.  If it is an options line, we just add it to a string. 
12529       (cond 
12530        ((eq symbol options-symbol)
12531         (setq gnus-newsrc-options
12532               ;; This concatting is quite inefficient, but since our
12533               ;; thorough studies show that approx 99.37% of all
12534               ;; .newsrc files only contain a single options line, we
12535               ;; don't give a damn, frankly, my dear.
12536               (concat gnus-newsrc-options
12537                       (buffer-substring 
12538                        (gnus-point-at-bol)
12539                        ;; Options may continue on the next line.
12540                        (or (and (re-search-forward "^[^ \t]" nil 'move)
12541                                 (progn (beginning-of-line) (point)))
12542                            (point))))))
12543        (symbol
12544         ;; It was a group name.
12545         (setq subscribed (= (following-char) ?:)
12546               group (symbol-name symbol)
12547               reads nil)
12548         (if (eolp)
12549             ;; If the line ends here, this is clearly a buggy line, so
12550             ;; we put point a the beginning of line and let the cond
12551             ;; below do the error handling.
12552             (beginning-of-line)
12553           ;; We skip to the beginning of the ranges.
12554           (skip-chars-forward "!: \t"))
12555         ;; We are now at the beginning of the list of read articles.
12556         ;; We read them range by range.
12557         (while
12558             (cond 
12559              ((looking-at "[0-9]+")
12560               ;; We narrow and read a number instead of buffer-substring/
12561               ;; string-to-int because it's faster. narrow/widen is
12562               ;; faster than save-restriction/narrow, and save-restriction
12563               ;; produces a garbage object.
12564               (setq num1 (progn
12565                            (narrow-to-region (match-beginning 0) (match-end 0))
12566                            (read buf)))
12567               (widen)
12568               ;; If the next character is a dash, then this is a range.
12569               (if (= (following-char) ?-)
12570                   (progn
12571                     ;; We read the upper bound of the range.
12572                     (forward-char 1)
12573                     (if (not (looking-at "[0-9]+"))
12574                         ;; This is a buggy line, by we pretend that
12575                         ;; it's kinda OK. Perhaps the user should be
12576                         ;; dinged? 
12577                         (setq reads (cons num1 reads))
12578                       (setq reads 
12579                             (cons 
12580                              (cons num1 (progn
12581                                           (narrow-to-region (match-beginning 0) 
12582                                                             (match-end 0))
12583                                           (read buf)))
12584                              reads))
12585                       (widen)))
12586                 ;; It was just a simple number, so we add it to the
12587                 ;; list of ranges.
12588                 (setq reads (cons num1 reads)))
12589               ;; If the next char in ?\n, then we have reached the end
12590               ;; of the line and return nil.
12591               (/= (following-char) ?\n))
12592              ((= (following-char) ?\n)
12593               ;; End of line, so we end.
12594               nil)
12595              (t
12596               ;; Not numbers and not eol, so this might be a buggy
12597               ;; line... 
12598               (or (eobp) ; If it was eob instead of ?\n, we allow it.
12599                   (progn
12600                     ;; The line was buggy.
12601                     (setq group nil)
12602                     (gnus-message 3 "Mangled line: %s" 
12603                                   (buffer-substring (gnus-point-at-bol) 
12604                                                     (gnus-point-at-eol)))
12605                     (ding)
12606                     (sit-for 1)))
12607               nil))
12608           ;; Skip past ", ". Spaces are illegal in these ranges, but
12609           ;; we allow them, because it's a common mistake to put a
12610           ;; space after the comma.
12611           (skip-chars-forward ", "))
12612
12613         ;; We have already read .newsrc.eld, so we gently update the
12614         ;; data in the hash table with the information we have just
12615         ;; read. 
12616         (if (not group)
12617             ()
12618           (let ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
12619                 level)
12620             (if info
12621                 ;; There is an entry for this file in the alist.
12622                 (progn
12623                   (setcar (nthcdr 2 info) (nreverse reads))
12624                   ;; We update the level very gently.  In fact, we
12625                   ;; only change it if there's been a status change
12626                   ;; from subscribed to unsubscribed, or vice versa.
12627                   (setq level (nth 1 info))
12628                   (cond ((and (<= level gnus-level-subscribed)
12629                               (not subscribed))
12630                          (setq level (if reads
12631                                          gnus-level-default-unsubscribed 
12632                                        (1+ gnus-level-default-unsubscribed))))
12633                         ((and (> level gnus-level-subscribed) subscribed)
12634                          (setq level gnus-level-default-subscribed)))
12635                   (setcar (cdr info) level))
12636               ;; This is a new group.
12637               (setq info (list group 
12638                                (if subscribed
12639                                    gnus-level-default-subscribed 
12640                                  (if reads
12641                                      (1+ gnus-level-subscribed)
12642                                    gnus-level-default-unsubscribed))
12643                                (nreverse reads))))
12644             (setq newsrc (cons info newsrc))))))
12645       (forward-line 1))
12646     
12647     (setq newsrc (nreverse newsrc))
12648
12649     (if (not already-read)
12650         ()
12651       ;; We now have two newsrc lists - `newsrc', which is what we
12652       ;; have read from .newsrc, and `gnus-newsrc-alist', which is
12653       ;; what we've read from .newsrc.eld. We have to merge these
12654       ;; lists. We do this by "attaching" any (foreign) groups in the
12655       ;; gnus-newsrc-alist to the (native) group that precedes them. 
12656       (let ((rc (cdr gnus-newsrc-alist))
12657             (prev gnus-newsrc-alist)
12658             entry mentry)
12659         (while rc
12660           (or (null (nth 4 (car rc))) ; It's a native group.
12661               (assoc (car (car rc)) newsrc) ; It's already in the alist.
12662               (if (setq entry (assoc (car (car prev)) newsrc))
12663                   (setcdr (setq mentry (memq entry newsrc))
12664                           (cons (car rc) (cdr mentry)))
12665                 (setq newsrc (cons (car rc) newsrc))))
12666           (setq prev rc
12667                 rc (cdr rc)))))
12668
12669     (setq gnus-newsrc-alist newsrc)
12670     ;; We make the newsrc hashtb.
12671     (gnus-make-hashtable-from-newsrc-alist)
12672
12673     ;; Finally, if we read some options lines, we parse them.
12674     (or (string= gnus-newsrc-options "")
12675         (gnus-newsrc-parse-options gnus-newsrc-options))))
12676
12677 ;; Parse options lines to find "options -n !all rec.all" and stuff.
12678 ;; The return value will be a list on the form
12679 ;; ((regexp1 . ignore)
12680 ;;  (regexp2 . subscribe)...)
12681 ;; When handling new newsgroups, groups that match a `ignore' regexp
12682 ;; will be ignored, and groups that match a `subscribe' regexp will be
12683 ;; subscribed. A line like
12684 ;; options -n !all rec.all
12685 ;; will lead to a list that looks like
12686 ;; (("^rec\\..+" . subscribe) 
12687 ;;  ("^.+" . ignore))
12688 ;; So all "rec.*" groups will be subscribed, while all the other
12689 ;; groups will be ignored. Note that "options -n !all rec.all" is very
12690 ;; different from "options -n rec.all !all". 
12691 (defun gnus-newsrc-parse-options (options)
12692   (let (out eol)
12693     (save-excursion
12694       (gnus-set-work-buffer)
12695       (insert (regexp-quote options))
12696       ;; First we treat all continuation lines.
12697       (goto-char (point-min))
12698       (while (re-search-forward "\n[ \t]+" nil t)
12699         (replace-match " " t t))
12700       ;; Then we transform all "all"s into ".+"s.
12701       (goto-char (point-min))
12702       (while (re-search-forward "\\ball\\b" nil t)
12703         (replace-match ".+" t t))
12704       (goto-char (point-min))
12705       ;; We remove all other options than the "-n" ones.
12706       (while (re-search-forward "[ \t]-[^n][^-]*" nil t)
12707         (replace-match " ")
12708         (forward-char -1))
12709       (goto-char (point-min))
12710
12711       ;; We are only interested in "options -n" lines - we
12712       ;; ignore the other option lines.
12713       (while (re-search-forward "[ \t]-n" nil t)
12714         (setq eol 
12715               (or (save-excursion
12716                     (and (re-search-forward "[ \t]-n" (gnus-point-at-eol) t)
12717                          (- (point) 2)))
12718                   (gnus-point-at-eol)))
12719         ;; Search for all "words"...
12720         (while (re-search-forward "[^ \t,\n]+" eol t)
12721           (if (= (char-after (match-beginning 0)) ?!)
12722               ;; If the word begins with a bang (!), this is a "not"
12723               ;; spec. We put this spec (minus the bang) and the
12724               ;; symbol `ignore' into the list.
12725               (setq out (cons (cons (concat 
12726                                      "^" (buffer-substring 
12727                                           (1+ (match-beginning 0))
12728                                           (match-end 0)))
12729                                     'ignore) out))
12730             ;; There was no bang, so this is a "yes" spec.
12731             (setq out (cons (cons (concat 
12732                                    "^" (buffer-substring (match-beginning 0)
12733                                                          (match-end 0)))
12734                                   'subscribe) out)))))
12735     
12736       (setq gnus-newsrc-options-n out))))
12737                
12738
12739 (defun gnus-save-newsrc-file ()
12740   "Save .newsrc file."
12741   ;; Note: We cannot save .newsrc file if all newsgroups are removed
12742   ;; from the variable gnus-newsrc-alist.
12743   (and (or gnus-newsrc-alist gnus-killed-list)
12744        gnus-current-startup-file
12745        (let ((make-backup-files t)
12746              (version-control nil)
12747              (require-final-newline t)) ;Don't ask even if requested.
12748          ;; You can stop or change version control of backup file.
12749          ;; Suggested by jason@violet.berkeley.edu.
12750          (run-hooks 'gnus-save-newsrc-hook)
12751          (save-excursion
12752            (if (or (not gnus-dribble-buffer)
12753                    (not (buffer-name gnus-dribble-buffer))
12754                    (zerop (save-excursion
12755                             (set-buffer gnus-dribble-buffer)
12756                             (buffer-size))))
12757                (gnus-message 4 "(No changes need to be saved)")
12758              (if gnus-save-newsrc-file
12759                  (progn
12760                    (gnus-message 5 "Saving %s..." gnus-current-startup-file)
12761                    ;; Make backup file of master newsrc.
12762                    (gnus-gnus-to-newsrc-format)
12763                    (gnus-message 5 "Saving %s...done"
12764                                  gnus-current-startup-file)))
12765              ;; Quickly loadable .newsrc.
12766              (set-buffer (get-buffer-create " *Gnus-newsrc*"))
12767              (gnus-add-current-to-buffer-list)
12768              (buffer-disable-undo (current-buffer))
12769              (erase-buffer)
12770              (gnus-message 5 "Saving %s.eld..." gnus-current-startup-file)
12771              (gnus-gnus-to-quick-newsrc-format)
12772              (write-region 1 (point-max) 
12773                            (concat gnus-current-startup-file ".eld") 
12774                            nil 'nomesg)
12775              (kill-buffer (current-buffer))
12776              (gnus-message 5 "Saving %s.eld...done" gnus-current-startup-file)
12777              (gnus-dribble-delete-file))))))
12778
12779 (defun gnus-gnus-to-quick-newsrc-format ()
12780   "Insert Gnus variables such as gnus-newsrc-alist in lisp format."
12781   (insert ";; (ding) Gnus startup file.\n")
12782   (insert ";; Never delete this file - touch .newsrc instead to force Gnus\n")
12783   (insert ";; to read .newsrc.\n")
12784   (let ((variables gnus-variable-list)
12785         (inhibit-quit t)
12786         (gnus-newsrc-alist (cdr gnus-newsrc-alist))
12787         variable)
12788     ;; insert lisp expressions.
12789     (gnus-compress-newsrc-alist)
12790     (while variables
12791       (setq variable (car variables))
12792       (and (boundp variable)
12793            (symbol-value variable)
12794            (or gnus-save-killed-list (not (eq variable 'gnus-killed-list)))
12795            (insert "(setq " (symbol-name variable) " '"
12796                    (prin1-to-string (symbol-value variable))
12797                    ")\n"))
12798       (setq variables (cdr variables)))
12799     (gnus-uncompress-newsrc-alist)))
12800
12801
12802 (defun gnus-gnus-to-newsrc-format ()
12803   ;; Generate and save the .newsrc file.
12804   (let ((newsrc (cdr gnus-newsrc-alist))
12805         info ranges range)
12806     (save-excursion
12807       (set-buffer (create-file-buffer gnus-startup-file))
12808       (buffer-disable-undo (current-buffer))
12809       (erase-buffer)
12810       ;; Write options.
12811       (if gnus-newsrc-options (insert gnus-newsrc-options))
12812       ;; Write subscribed and unsubscribed.
12813       (while newsrc
12814         (setq info (car newsrc))
12815         (if (not (nth 4 info))          ;Don't write foreign groups to .newsrc.
12816             (progn
12817               (insert (car info) (if (> (nth 1 info) gnus-level-subscribed)
12818                                      "!" ":"))
12819               (if (setq ranges (nth 2 info))
12820                   (progn
12821                     (insert " ")
12822                     (if (not (listp (cdr ranges)))
12823                         (if (= (car ranges) (cdr ranges))
12824                             (insert (int-to-string (car ranges)))
12825                           (insert (int-to-string (car ranges)) "-" 
12826                                   (int-to-string (cdr ranges))))
12827                       (while ranges
12828                         (setq range (car ranges)
12829                               ranges (cdr ranges))
12830                         (if (or (atom range) (= (car range) (cdr range)))
12831                             (insert (int-to-string 
12832                                      (or (and (atom range) range) 
12833                                          (car range))))
12834                           (insert (int-to-string (car range)) "-"
12835                                   (int-to-string (cdr range))))
12836                         (if ranges (insert ","))))))
12837               (insert "\n")))
12838         (setq newsrc (cdr newsrc)))
12839       (write-region 1 (point-max) gnus-current-startup-file nil 'nomesg)
12840       (kill-buffer (current-buffer)))))
12841
12842 (defun gnus-read-all-descriptions-files ()
12843   (let ((methods (nconc (list gnus-select-method) 
12844                         gnus-secondary-select-methods)))
12845     (while methods
12846       (gnus-read-descriptions-file (car methods))
12847       (setq methods (cdr methods)))
12848     t))
12849
12850 (defun gnus-read-descriptions-file (&optional method)
12851   (let ((method (or method gnus-select-method)))
12852     ;; We create the hashtable whether we manage to read the desc file
12853     ;; to avoid trying to re-read after a failed read.
12854     (or gnus-description-hashtb
12855         (setq gnus-description-hashtb 
12856               (gnus-make-hashtable (length gnus-active-hashtb))))
12857     ;; Mark this method's desc file as read.
12858     (gnus-sethash (gnus-group-prefixed-name "" method) "Has read"
12859                   gnus-description-hashtb)
12860
12861     (gnus-message 5 "Reading descriptions file via %s..." (car method))
12862     (cond 
12863      ((not (or (gnus-server-opened method)
12864                (gnus-open-server method)))
12865       (gnus-message 1 "Couldn't open server")
12866       nil)
12867      ((not (gnus-request-list-newsgroups method))
12868       (gnus-message 1 "Couldn't read newsgroups descriptions")
12869       nil)
12870      (t
12871       (let (group)
12872         (save-excursion
12873           (save-restriction
12874             (set-buffer nntp-server-buffer)
12875             (goto-char (point-min))
12876             (if (or (search-forward "\n.\n" nil t)
12877                     (goto-char (point-max)))
12878                 (progn
12879                   (beginning-of-line)
12880                   (narrow-to-region (point-min) (point))))
12881             (goto-char (point-min))
12882             (while (not (eobp))
12883               ;; If we get an error, we set group to 0, which is not a
12884               ;; symbol... 
12885               (setq group 
12886                     (condition-case ()
12887                         (let ((obarray gnus-description-hashtb))
12888                           ;; Group is set to a symbol interned in this
12889                           ;; hash table.
12890                           (read nntp-server-buffer))
12891                       (error 0)))
12892               (skip-chars-forward " \t")
12893               ;; ... which leads to this line being effectively ignored.
12894               (and (symbolp group)
12895                    (set group (buffer-substring 
12896                                (point) (progn (end-of-line) (point)))))
12897               (forward-line 1))))
12898         (gnus-message 5 "Reading descriptions file...done")
12899         t)))))
12900
12901 (defun gnus-group-get-description (group)
12902   ;; Get the description of a group by sending XGTITLE to the server.
12903   (and (gnus-request-group-description group)
12904        (save-excursion
12905          (set-buffer nntp-server-buffer)
12906          (goto-char (point-min))
12907          (and (looking-at "[^ \t]+[ \t]+\\(.*\\)")
12908               (buffer-substring (match-beginning 1) (match-end 1))))))
12909
12910 ;;;
12911 ;;; Server
12912 ;;;
12913
12914 (defvar gnus-server-mode-hook nil
12915   "Hook run in `gnus-server-mode' buffers.")
12916
12917 (defconst gnus-server-line-format "     {%(%h:%w%)}\n"
12918   "Format of server lines.
12919 It works along the same lines as a normal formatting string,
12920 with some simple extensions.")
12921
12922 (defvar gnus-server-mode-line-format "(ding) List of servers"
12923   "The format specification for the server mode line.")
12924
12925 (defconst gnus-server-line-format-alist
12926   (list (list ?h 'how ?s)
12927         (list ?n 'name ?s)
12928         (list ?w 'where ?s)
12929         ))
12930
12931 (defconst gnus-server-mode-line-format-alist 
12932   (list (list ?S 'news-server ?s)
12933         (list ?M 'news-method ?s)
12934         (list ?u 'user-defined ?s)))
12935
12936 (defvar gnus-server-line-format-spec nil)
12937 (defvar gnus-server-mode-line-format-spec nil)
12938 (defvar gnus-server-killed-servers nil)
12939
12940 (defvar gnus-server-mode-map nil)
12941 (put 'gnus-server-mode 'mode-class 'special)
12942
12943 (if gnus-server-mode-map
12944     nil
12945   (setq gnus-server-mode-map (make-sparse-keymap))
12946   (suppress-keymap gnus-server-mode-map)
12947   (define-key gnus-server-mode-map " " 'gnus-server-read-server)
12948   (define-key gnus-server-mode-map "\r" 'gnus-server-read-server)
12949   (define-key gnus-server-mode-map gnus-mouse-2 'gnus-server-pick-server)
12950   (define-key gnus-server-mode-map "q" 'gnus-server-exit)
12951   (define-key gnus-server-mode-map "l" 'gnus-server-list-servers)
12952   (define-key gnus-server-mode-map "k" 'gnus-server-kill-server)
12953   (define-key gnus-server-mode-map "y" 'gnus-server-yank-server)
12954   (define-key gnus-server-mode-map "c" 'gnus-server-copy-server)
12955   (define-key gnus-server-mode-map "a" 'gnus-server-add-server)
12956   (define-key gnus-server-mode-map "e" 'gnus-server-edit-server))
12957
12958 (defun gnus-server-mode ()
12959   "Major mode for listing and editing servers.
12960
12961 All normal editing commands are switched off.
12962 \\<gnus-server-mode-map>
12963
12964 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]'). 
12965
12966 The following commands are available:
12967
12968 \\{gnus-server-mode-map}"
12969   (interactive)
12970   (if gnus-visual (gnus-server-make-menu-bar))
12971   (kill-all-local-variables)
12972   (setq mode-line-modified "-- ")
12973   (make-local-variable 'mode-line-format)
12974   (setq mode-line-format (copy-sequence mode-line-format))
12975   (and (equal (nth 3 mode-line-format) "   ")
12976        (setcar (nthcdr 3 mode-line-format) ""))
12977   (setq major-mode 'gnus-server-mode)
12978   (setq mode-name "Server")
12979 ;  (gnus-group-set-mode-line)
12980   (setq mode-line-process nil)
12981   (use-local-map gnus-server-mode-map)
12982   (buffer-disable-undo (current-buffer))
12983   (setq truncate-lines t)
12984   (setq buffer-read-only t)
12985   (run-hooks 'gnus-server-mode-hook))
12986
12987 (defun gnus-server-insert-server-line (sformat name method)
12988   (let* ((sformat (or sformat gnus-server-line-format-spec))
12989          (how (car method))
12990          (where (nth 1 method))
12991          b)
12992     (beginning-of-line)
12993     (setq b (point))
12994     ;; Insert the text.
12995     (insert (eval sformat))
12996     (add-text-properties b (1+ b) (list 'gnus-server (intern name)))))
12997
12998 (defun gnus-server-setup-buffer ()
12999   (if (get-buffer gnus-server-buffer)
13000       ()
13001     (save-excursion
13002       (set-buffer (get-buffer-create gnus-server-buffer))
13003       (gnus-server-mode)
13004       (and gnus-carpal (gnus-carpal-setup-buffer 'server)))))
13005
13006 (defun gnus-server-prepare ()
13007   (setq gnus-server-mode-line-format-spec 
13008         (gnus-parse-format gnus-server-mode-line-format 
13009                            gnus-server-mode-line-format-alist))
13010   (setq gnus-server-line-format-spec 
13011         (gnus-parse-format gnus-server-line-format 
13012                            gnus-server-line-format-alist))
13013   (let ((alist gnus-server-alist)
13014         (buffer-read-only nil))
13015     (erase-buffer)
13016     (while alist
13017       (gnus-server-insert-server-line nil (car (car alist)) (cdr (car alist)))
13018       (setq alist (cdr alist))))
13019   (goto-char (point-min))
13020   (gnus-server-position-cursor))
13021
13022 (defun gnus-server-server-name ()
13023   (let ((server (get-text-property (gnus-point-at-bol) 'gnus-server)))
13024     (and server (symbol-name server))))
13025
13026 (defalias 'gnus-server-position-cursor 'gnus-goto-colon)
13027
13028 (defconst gnus-server-edit-buffer "*Gnus edit server*")
13029
13030 (defun gnus-server-update-server (server)
13031   (save-excursion
13032     (set-buffer gnus-server-buffer)
13033     (let ((buffer-read-only nil)
13034           (info (cdr (assoc server gnus-server-alist))))
13035       (gnus-dribble-enter 
13036        (concat "(gnus-server-set-info \"" server "\" '"
13037                (prin1-to-string info) ")"))
13038       ;; Buffer may be narrowed.
13039       (save-restriction
13040         (widen)
13041         (if (gnus-server-goto-server server)
13042             (delete-region (progn (beginning-of-line) (point))
13043                            (progn (forward-line 1) (point))))
13044         (let ((entry (assoc server gnus-server-alist)))
13045           (gnus-server-insert-server-line nil (car entry) (cdr entry))
13046           (gnus-server-position-cursor))))))
13047
13048 (defun gnus-server-set-info (server info)
13049   ;; Enter a select method into the virtual server alist.
13050   (gnus-dribble-enter 
13051    (concat "(gnus-server-set-info \"" server "\" '"
13052            (prin1-to-string info) ")"))
13053   (let* ((server (nth 1 info))
13054          (entry (assoc server gnus-server-alist)))
13055     (if entry (setcdr entry info)
13056       (setq gnus-server-alist
13057             (nconc gnus-server-alist (list (cons server info)))))))
13058
13059 (defun gnus-server-to-method (server)
13060   ;; Map virtual server names to select methods.
13061   (or (and (equal server "native") gnus-select-method)
13062       (cdr (assoc server gnus-server-alist))))
13063
13064 (defun gnus-server-extend-method (group method)
13065   ;; This function "extends" a virtual server.  If the server is
13066   ;; "hello", and the select method is ("hello" (my-var "something")) 
13067   ;; in the group "alt.alt", this will result in a new virtual server
13068   ;; called "helly+alt.alt".
13069   (let ((entry
13070          (gnus-copy-sequence 
13071           (if (equal (car method) "native") gnus-select-method
13072               (cdr (assoc (car method) gnus-server-alist))))))
13073     (setcar (cdr entry) (concat (nth 1 entry) "+" group))
13074     (nconc entry (cdr method))))
13075
13076 (defun gnus-server-get-method (group method)
13077   ;; Input either a server name, and extended server name, or a
13078   ;; select method, and return a select method. 
13079   (cond ((stringp method)
13080          (gnus-server-to-method method))
13081         ((stringp (car method))
13082          (gnus-server-extend-method group method))
13083         (t
13084          (gnus-server-add-address method))))
13085
13086 (defun gnus-server-add-address (method)
13087   (let ((method-name (symbol-name (car method))))
13088     (if (and (memq 'address (assoc method-name gnus-valid-select-methods))
13089              (not (assq (intern (concat method-name "-address")) method)))
13090         (append method (list (list (intern (concat method-name "-address"))
13091                                    (nth 1 method))))
13092       method)))
13093
13094 (defun gnus-server-equal (s1 s2)
13095   (or (equal s1 s2)
13096       (and (= (length s1) (length s2))
13097            (progn
13098              (while (and s1 (member (car s1) s2))
13099                (setq s1 (cdr s1)))
13100              (null s1)))))
13101
13102 ;;; Interactive server functions.
13103
13104 (defun gnus-server-kill-server (server)
13105   "Kill the server on the current line."
13106   (interactive (list (gnus-server-server-name)))
13107   (or (gnus-server-goto-server server)
13108       (if server (error "No such server: %s" server)
13109         (error "No server on the current line")))
13110   (let ((buffer-read-only nil))
13111     (delete-region (progn (beginning-of-line) (point))
13112                    (progn (forward-line 1) (point))))
13113   (setq gnus-server-killed-servers 
13114         (cons (assoc server gnus-server-alist) gnus-server-killed-servers))
13115   (setq gnus-server-alist (delq (car gnus-server-killed-servers)
13116                                 gnus-server-alist))
13117   (gnus-server-position-cursor))
13118
13119 (defun gnus-server-yank-server ()
13120   "Yank the previously killed server."
13121   (interactive)
13122   (or gnus-server-killed-servers
13123       (error "No killed servers to be yanked"))
13124   (let ((alist gnus-server-alist)
13125         (server (gnus-server-server-name))
13126         (killed (car gnus-server-killed-servers)))
13127     (if (not server) 
13128         (setq gnus-server-alist (nconc gnus-server-alist (list killed)))
13129       (if (string= server (car (car gnus-server-alist)))
13130           (setq gnus-server-alist (cons killed gnus-server-alist))
13131         (while (and (cdr alist)
13132                     (not (string= server (car (car (cdr alist))))))
13133           (setq alist (cdr alist)))
13134         (setcdr alist (cons killed (cdr alist)))))
13135     (gnus-server-update-server (car killed))
13136     (setq gnus-server-killed-servers (cdr gnus-server-killed-servers))
13137     (gnus-server-position-cursor)))
13138
13139 (defun gnus-server-exit ()
13140   "Return to the group buffer."
13141   (interactive)
13142   (kill-buffer (current-buffer))
13143   (switch-to-buffer gnus-group-buffer))
13144
13145 (defun gnus-server-list-servers ()
13146   "List all available servers."
13147   (interactive)
13148   (let ((cur (gnus-server-server-name)))
13149     (gnus-server-prepare)
13150     (if cur (gnus-server-goto-server cur)
13151       (goto-char (point-max))
13152       (forward-line -1))
13153     (gnus-server-position-cursor)))
13154
13155 (defun gnus-server-copy-server (from to)
13156   (interactive
13157    (list
13158     (or (gnus-server-server-name)
13159         (error "No server on the current line"))
13160     (read-string "Copy to: ")))
13161   (or from (error "No server on current line"))
13162   (or (and to (not (string= to ""))) (error "No name to copy to"))
13163   (and (assoc to gnus-server-alist) (error "%s already exists" to))
13164   (or (assoc from gnus-server-alist) 
13165       (error "%s: no such server" from))
13166   (let ((to-entry (gnus-copy-sequence (assoc from gnus-server-alist))))
13167     (setcar to-entry to)
13168     (setcar (nthcdr 2 to-entry) to)
13169     (setq gnus-server-killed-servers 
13170           (cons to-entry gnus-server-killed-servers))
13171     (gnus-server-yank-server)))
13172
13173 (defun gnus-server-add-server (how where)
13174   (interactive 
13175    (list (intern (completing-read "Server method: "
13176                                   gnus-valid-select-methods nil t))
13177          (read-string "Server name: ")))
13178   (setq gnus-server-killed-servers 
13179         (cons (list where how where) gnus-server-killed-servers))
13180   (gnus-server-yank-server))
13181
13182 (defun gnus-server-goto-server (server)
13183   "Jump to a server line."
13184   (interactive
13185    (list (completing-read "Goto server: " gnus-server-alist nil t)))
13186   (let ((to (text-property-any (point-min) (point-max) 
13187                                'gnus-server (intern server))))
13188     (and to
13189          (progn
13190            (goto-char to) 
13191            (gnus-server-position-cursor)))))
13192
13193 (defun gnus-server-edit-server (server)
13194   "Edit the server on the current line."
13195   (interactive (list (gnus-server-server-name)))
13196   (or server
13197       (error "No server on current line"))
13198   (let ((winconf (current-window-configuration)))
13199     (get-buffer-create gnus-server-edit-buffer)
13200     (gnus-configure-windows 'edit-server)
13201     (gnus-add-current-to-buffer-list)
13202     (emacs-lisp-mode)
13203     (make-local-variable 'gnus-prev-winconf)
13204     (setq gnus-prev-winconf winconf)
13205     (use-local-map (copy-keymap (current-local-map)))
13206     (let ((done-func '(lambda () 
13207                         "Exit editing mode and update the information."
13208                         (interactive)
13209                         (gnus-server-edit-server-done 'group))))
13210       (setcar (cdr (nth 4 done-func)) server)
13211       (local-set-key "\C-c\C-c" done-func))
13212     (erase-buffer)
13213     (insert ";; Type `C-c C-c' after you have edited the server.\n\n")
13214     (insert (pp-to-string (cdr (assoc server gnus-server-alist))))))
13215
13216 (defun gnus-server-edit-server-done (server)
13217   (interactive)
13218   (set-buffer (get-buffer-create gnus-server-edit-buffer))
13219   (goto-char (point-min))
13220   (let ((form (read (current-buffer)))
13221         (winconf gnus-prev-winconf))
13222     (gnus-server-set-info server form)
13223     (kill-buffer (current-buffer))
13224     (and winconf (set-window-configuration winconf))
13225     (set-buffer gnus-server-buffer)
13226     (gnus-server-update-server (gnus-server-server-name))
13227     (gnus-server-position-cursor)))
13228
13229 (defun gnus-server-read-server (server)
13230   "Browse a server."
13231   (interactive (list (gnus-server-server-name)))
13232   (gnus-browse-foreign-server (gnus-server-to-method server) (current-buffer)))
13233
13234 (defun gnus-mouse-pick-server (e)
13235   (interactive "e")
13236   (mouse-set-point e)
13237   (gnus-server-read-server (gnus-server-server-name)))
13238
13239 ;;;
13240 ;;; entry points into gnus-score.el
13241 ;;;
13242
13243 ;;; Finding score files. 
13244
13245 (defvar gnus-global-score-files nil
13246   "*List of global score files and directories.
13247 Set this variable if you want to use people's score files.  One entry
13248 for each score file or each score file directory.  Gnus will decide
13249 by itself what score files are applicable to which group.
13250
13251 Say you want to use the single score file
13252 \"/ftp.ifi.uio.no@ftp:/pub/larsi/ding/score/soc.motss.SCORE\" and all
13253 score files in the \"/ftp.some-where:/pub/score\" directory.
13254
13255  (setq gnus-global-score-files
13256        '(\"/ftp.ifi.uio.no:/pub/larsi/ding/score/soc.motss.SCORE\"
13257          \"/ftp.some-where:/pub/score\"))")
13258
13259 (defun gnus-score-score-files (group)
13260   "Return a list of all possible score files."
13261   ;; Search and set any global score files.
13262   (and gnus-global-score-files 
13263        (or gnus-internal-global-score-files
13264            (gnus-score-search-global-directories gnus-global-score-files)))
13265   ;; Fix the kill-file dir variable.
13266   (setq gnus-kill-files-directory 
13267         (file-name-as-directory
13268          (or gnus-kill-files-directory "~/News/")))
13269   ;; If we can't read it, there are no score files.
13270   (if (not (file-exists-p (expand-file-name gnus-kill-files-directory)))
13271       (setq gnus-score-file-list nil)
13272     (if (gnus-use-long-file-name 'not-score)
13273         ;; We want long file names.
13274         (if (or (not gnus-score-file-list)
13275                 (not (car gnus-score-file-list))
13276                 (gnus-file-newer-than gnus-kill-files-directory
13277                                       (car gnus-score-file-list)))
13278               (setq gnus-score-file-list 
13279                     (cons (nth 5 (file-attributes gnus-kill-files-directory))
13280                           (nreverse 
13281                            (directory-files 
13282                             gnus-kill-files-directory t 
13283                             (gnus-score-file-regexp))))))
13284       ;; We do not use long file names, so we have to do some
13285       ;; directory traversing.  
13286       (let ((mdir (length (expand-file-name gnus-kill-files-directory)))
13287             (suffixes (list gnus-score-file-suffix gnus-adaptive-file-suffix))
13288             dir files suffix)
13289         (while suffixes
13290           (setq dir (expand-file-name
13291                      (concat gnus-kill-files-directory
13292                              (gnus-replace-chars-in-string group ?. ?/))))
13293           (setq dir (gnus-replace-chars-in-string dir ?: ?/))
13294           (setq suffix (car suffixes)
13295                 suffixes (cdr suffixes))
13296           (if (file-exists-p (concat dir "/" suffix))
13297               (setq files (cons (concat dir "/" suffix) files)))
13298           (while (>= (1+ (length dir)) mdir)
13299             (and (file-exists-p (concat dir "/all/" suffix))
13300                  (setq files (cons (concat dir "/all/" suffix) files)))
13301             (string-match "/[^/]*$" dir)
13302             (setq dir (substring dir 0 (match-beginning 0)))))
13303         (setq gnus-score-file-list 
13304               (cons nil (nreverse files)))))
13305     (cdr gnus-score-file-list)))
13306
13307 (defun gnus-score-file-regexp ()
13308   (concat "\\(" gnus-score-file-suffix 
13309           "\\|" gnus-adaptive-file-suffix "\\)$"))
13310         
13311 (defun gnus-score-find-bnews (group)
13312   "Return a list of score files for GROUP.
13313 The score files are those files in the ~/News directory which matches
13314 GROUP using BNews sys file syntax."
13315   (let* ((sfiles (append (gnus-score-score-files group)
13316                          gnus-internal-global-score-files))
13317          (kill-dir (file-name-as-directory 
13318                     (expand-file-name gnus-kill-files-directory)))
13319          (klen (length kill-dir))
13320          ofiles not-match regexp)
13321     (save-excursion
13322       (set-buffer (get-buffer-create "*gnus score files*"))
13323       (buffer-disable-undo (current-buffer))
13324       ;; Go through all score file names and create regexp with them
13325       ;; as the source.  
13326       (while sfiles
13327         (erase-buffer)
13328         (insert (car sfiles))
13329         (goto-char (point-min))
13330         ;; First remove the suffix itself.
13331         (re-search-forward (concat "." (gnus-score-file-regexp)))
13332         (replace-match "" t t) 
13333         (goto-char (point-min))
13334         (if (looking-at (regexp-quote kill-dir))
13335             ;; If the file name was just "SCORE", `klen' is one character
13336             ;; too much.
13337             (delete-char (min (1- (point-max)) klen))
13338           (goto-char (point-max))
13339           (search-backward "/")
13340           (delete-region (1+ (point)) (point-min)))
13341         ;; If short file names were used, we have to translate slashes.
13342         (goto-char (point-min))
13343         (while (re-search-forward "[/:]" nil t)
13344           (replace-match "." t t))
13345         ;; Translate "all" to ".*".
13346         (while (search-forward "all" nil t)
13347           (replace-match ".*" t t))
13348         (goto-char (point-min))
13349         ;; Deal with "not."s.
13350         (if (looking-at "not.")
13351             (progn
13352               (setq not-match t)
13353               (setq regexp (buffer-substring 5 (point-max))))
13354           (setq regexp (buffer-substring 1 (point-max)))
13355           (setq not-match nil))
13356         ;; Finally - if this resulting regexp matches the group name,
13357         ;; we add this score file to the list of score files
13358         ;; applicable to this group.
13359         (if (or (and not-match
13360                      (not (string-match regexp group)))
13361                 (and (not not-match)
13362                      (string-match regexp group)))
13363             (setq ofiles (cons (car sfiles) ofiles)))
13364         (setq sfiles (cdr sfiles)))
13365       (kill-buffer (current-buffer))
13366       ;; Slight kludge here - the last score file returned should be
13367       ;; the local score file, whether it exists or not. This is so
13368       ;; that any score commands the user enters will go to the right
13369       ;; file, and not end up in some global score file.
13370       (let ((localscore
13371              (expand-file-name
13372               (if (gnus-use-long-file-name 'not-score)
13373                   (concat gnus-kill-files-directory group "." 
13374                           gnus-score-file-suffix)
13375                 (concat gnus-kill-files-directory
13376                         (gnus-replace-chars-in-string group ?. ?/)
13377                         "/" gnus-score-file-suffix)))))
13378         (and (member localscore ofiles)
13379              (delete localscore ofiles))
13380         (setq ofiles (cons localscore ofiles)))
13381       (nreverse ofiles))))
13382
13383 (defun gnus-score-find-single (group)
13384   "Return list containing the score file for GROUP."
13385   (list (gnus-score-file-name group gnus-adaptive-file-suffix)
13386         (gnus-score-file-name group)))
13387
13388 (defun gnus-score-find-hierarchical (group)
13389   "Return list of score files for GROUP.
13390 This includes the score file for the group and all its parents."
13391   (let ((all (copy-sequence '(nil)))
13392         (start 0))
13393     (while (string-match "\\." group (1+ start))
13394       (setq start (match-beginning 0))
13395       (setq all (cons (substring group 0 start) all)))
13396     (setq all (cons group all))
13397     (nconc
13398      (mapcar (lambda (newsgroup)
13399                (gnus-score-file-name newsgroup gnus-adaptive-file-suffix))
13400              (setq all (nreverse all)))
13401      (mapcar 'gnus-score-file-name all))))
13402
13403 (defvar gnus-score-file-alist-cache nil)
13404
13405 (defun gnus-score-find-alist (group)
13406   "Return list of score files for GROUP.
13407 The list is determined from the variable gnus-score-file-alist."
13408   (let ((alist gnus-score-file-multiple-match-alist)
13409         score-files)
13410     ;; if this group has been seen before, return the cached entry
13411     (if (setq score-files (assoc group gnus-score-file-alist-cache))
13412         (cdr score-files)       ; ensures caching of groups with no matches
13413       ;; handle the multiple match alist
13414       (while alist
13415         (and (string-match (car (car alist)) group)
13416              (setq score-files
13417                    (nconc score-files (cdr (car alist)))))
13418         (setq alist (cdr alist)))
13419       (setq alist gnus-score-file-single-match-alist)
13420       ;; handle the single match alist
13421       (catch 'done
13422         (while alist
13423           (and (string-match (car (car alist)) group)
13424                ;; progn used just in case ("regexp") has no files
13425                ;; and score-files is still nil. -sj
13426                ;; this can be construed as a "stop searching here" feature :>
13427                ;; and used to simplify regexps in the single-alist 
13428                (progn
13429                  (setq score-files
13430                        (append score-files (cdr (car alist))))
13431                  (throw 'done nil)))
13432           (setq alist (cdr alist))))
13433       ;; cache the score files
13434       (setq gnus-score-file-alist-cache
13435             (cons (cons group score-files) gnus-score-file-alist-cache))
13436       score-files)))
13437
13438
13439 (defun gnus-possibly-score-headers (&optional trace)
13440   (let ((func gnus-score-find-score-files-function)
13441         score-files)
13442     (and func (not (listp func))
13443          (setq func (list func)))
13444     ;; Go through all the functions for finding score files (or actual
13445     ;; scores) and add them to a list.
13446     (setq score-files (copy-sequence
13447                        (gnus-score-find-alist gnus-newsgroup-name)))
13448     (while func
13449       (and (symbolp (car func))
13450            (fboundp (car func))
13451            (setq score-files 
13452                  (nconc score-files (funcall (car func) gnus-newsgroup-name))))
13453       (setq func (cdr func)))
13454     (if score-files (gnus-score-headers score-files trace))))
13455
13456 (defun gnus-score-file-name (newsgroup &optional suffix)
13457   "Return the name of a score file for NEWSGROUP."
13458   (let ((suffix (or suffix gnus-score-file-suffix)))
13459     (cond  ((or (null newsgroup)
13460                 (string-equal newsgroup ""))
13461             ;; The global score file is placed at top of the directory.
13462             (expand-file-name 
13463              suffix (or gnus-kill-files-directory "~/News")))
13464            ((gnus-use-long-file-name 'not-score)
13465             ;; Append ".SCORE" to newsgroup name.
13466             (expand-file-name (concat newsgroup "." suffix)
13467                               (or gnus-kill-files-directory "~/News")))
13468            (t
13469             ;; Place "SCORE" under the hierarchical directory.
13470             (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
13471                                       "/" suffix)
13472                               (or gnus-kill-files-directory "~/News"))))))
13473
13474 (defun gnus-score-search-global-directories (files)
13475   "Scan all global score directories for score files."
13476   ;; Set the variable `gnus-internal-global-score-files' to all
13477   ;; available global score files.
13478   (interactive (list gnus-global-score-files))
13479   (let (out)
13480     (while files
13481       (if (string-match "/$" (car files))
13482           (setq out (nconc (directory-files 
13483                             (car files) t
13484                             (concat (gnus-score-file-regexp) "$"))))
13485         (setq out (cons (car files) out)))
13486       (setq files (cdr files)))
13487     (setq gnus-internal-global-score-files out)))
13488
13489 ;; Allow redefinition of Gnus functions.
13490
13491 (gnus-ems-redefine)
13492
13493 (provide 'gnus)
13494
13495 ;;; gnus.el ends here