09c1aa2abea4516850a66f5ce975b5c619546735
[gnus] / lisp / gnus.el
1 ;;; gnus.el --- a newsreader for GNU Emacs
2 ;; Copyright (C) 1987,88,89,90,93,94,95 Free Software Foundation, Inc.
3
4 ;; Author: Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
5 ;;      Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23
24 ;;; Commentary:
25
26 ;; Although (ding) Gnus looks suspiciously like GNUS, it isn't quite
27 ;; the same beast. Most internal structures have been changed. If you
28 ;; have written packages that depend on any of the hash tables,
29 ;; `gnus-newsrc-alist', `gnus-killed-assoc', marked lists, the .newsrc
30 ;; buffer, or internal knowledge of the `nntp-header-' macros, or
31 ;; dependence on the buffers having a certain format, your code will
32 ;; fail.
33
34 ;;; Code:
35
36 (require 'mail-utils)
37 (require 'timezone)
38 (require 'nnheader)
39
40 ;; Site dependent variables. These variables should be defined in
41 ;; paths.el.
42
43 (defvar gnus-default-nntp-server nil
44   "Specify a default NNTP server.
45 This variable should be defined in paths.el, and should never be set
46 by the user.
47 If you want to change servers, you should use `gnus-select-method'.
48 See the documentation to that variable.")
49
50 (defconst gnus-backup-default-subscribed-newsgroups 
51   '("news.announce.newusers" "news.groups.questions" "gnu.emacs.gnus")
52   "Default default new newsgroups the first time Gnus is run.
53 Should be set in paths.el, and shouldn't be touched by the user.")
54
55 (defvar gnus-local-domain nil
56   "Local domain name without a host name.
57 The DOMAINNAME environment variable is used instead if it is defined.
58 If the `system-name' function returns the full Internet name, there is
59 no need to set this variable.")
60
61 (defvar gnus-local-organization nil
62   "String with a description of what organization (if any) the user belongs to.
63 The ORGANIZATION environment variable is used instead if it is defined.
64 If this variable contains a function, this function will be called
65 with the current newsgroup name as the argument. The function should
66 return a string.
67
68 In any case, if the string (either in the variable, in the environment
69 variable, or returned by the function) is a file name, the contents of
70 this file will be used as the organization.")
71
72 (defvar gnus-use-generic-from nil
73   "If nil, the full host name will be the system name prepended to the domain name.
74 If this is a string, the full host name will be this string.
75 If this is non-nil, non-string, the domain name will be used as the
76 full host name.")
77
78 (defvar gnus-use-generic-path nil
79   "If nil, use the NNTP server name in the Path header.
80 If stringp, use this; if non-nil, use no host name (user name only).")
81
82
83 ;; Customization variables
84
85 (defvar gnus-select-method 
86   (list 'nntp (or (getenv "NNTPSERVER") 
87                   (if (and gnus-default-nntp-server
88                            (not (string= gnus-default-nntp-server "")))
89                       gnus-default-nntp-server)
90                   (system-name)))
91   "*Default method for selecting a newsgroup.
92 This variable should be a list, where the first element is how the
93 news is to be fetched, the second is the address. 
94
95 For instance, if you want to get your news via NNTP from
96 \"flab.flab.edu\", you could say:
97
98 (setq gnus-select-method '(nntp \"flab.flab.edu\"))
99
100 If you want to use your local spool, say:
101
102 (setq gnus-select-method (list 'nnspool (system-name)))
103
104 If you use this variable, you must set `gnus-nntp-server' to nil.
105
106 There is a lot more to know about select methods and virtual servers -
107 see the manual for details.")
108
109 ;; Added by Sudish Joseph <joseph@cis.ohio-state.edu>.
110 (defvar gnus-post-method nil
111   "*Preferred method for posting USENET news.
112 If this variable is nil, Gnus will use the current method to decide
113 which method to use when posting.  If it is non-nil, it will override
114 the current method.  This method will not be used in mail groups and
115 the like, only in \"real\" newsgroups.
116
117 The value must be a valid method as discussed in the documentation of
118 `gnus-select-method'.")
119
120 (defvar gnus-refer-article-method nil
121   "*Preferred method for fetching an article by Message-ID.
122 If you are reading news from the local spool (with nnspool), fetching
123 articles by Message-ID is painfully slow. By setting this method to an
124 nntp method, you might get acceptable results.
125
126 The value of this variable must be a valid select method as discussed
127 in the documentation of `gnus-select-method'")
128
129 (defvar gnus-secondary-select-methods nil
130   "*A list of secondary methods that will be used for reading news.
131 This is a list where each element is a complete select method (see
132 `gnus-select-method').  
133
134 If, for instance, you want to read your mail with the nnml backend,
135 you could set this variable:
136
137 (setq gnus-secondary-select-methods '((nnml \"\"))")
138
139 (defvar gnus-secondary-servers nil
140   "*List of NNTP servers that the user can choose between interactively.
141 To make Gnus query you for a server, you have to give `gnus' a
142 non-numeric prefix - `C-u M-x gnus', in short.")
143
144 (defvar gnus-nntp-server nil
145   "*The name of the host running the NNTP server.
146 This variable is semi-obsolete. Use the `gnus-select-method'
147 variable instead.")
148
149 (defvar gnus-nntp-service "nntp"
150   "*NNTP service name (\"nntp\" or 119).
151 This is an obsolete variable, which is scarcely used. If you use an
152 nntp server for your newsgroup and want to change the port number
153 used to 899, you would say something along these lines:
154
155  (setq gnus-select-method '(nntp \"my.nntp.server\" (nntp-port-number 899)))")
156
157 (defvar gnus-startup-file "~/.newsrc"
158   "*Your `.newsrc' file.
159 `.newsrc-SERVER' will be used instead if that exists.")
160
161 (defvar gnus-init-file "~/.gnus"
162   "*Your Gnus elisp startup file.
163 If a file with the .el or .elc suffixes exist, it will be read
164 instead.") 
165
166 (defvar gnus-group-faq-directory
167   "/anonymous@rtfm.mit.edu:/pub/usenet-by-group/"
168   "*Directory where the group FAQs are stored.
169 This will most commonly be on a remote machine, and the file will be
170 fetched by ange-ftp.")
171
172 (defvar gnus-group-archive-directory
173   "/ftp@ftp.hpc.uh.edu:/pub/emacs/ding-list/" 
174   "*The address of the (ding) archives.")
175
176 (defvar gnus-default-subscribed-newsgroups nil
177   "*This variable lists what newsgroups should be subscribed the first time Gnus is used.
178 It should be a list of strings.
179 If it is `t', Gnus will not do anything special the first time it is
180 started; it'll just use the normal newsgroups subscription methods.")
181
182 (defvar gnus-use-cross-reference t
183   "*Non-nil means that cross referenced articles will be marked as read.
184 If nil, ignore cross references.  If t, mark articles as read in
185 subscribed newsgroups. If neither t nor nil, mark as read in all
186 newsgroups.") 
187
188 (defvar gnus-use-dribble-file t
189   "*Non-nil means that Gnus will use a dribble file to store user updates.
190 If Emacs should crash without saving the .newsrc files, complete
191 information can be restored from the dribble file.")
192
193 (defvar gnus-asynchronous nil
194   "*If non-nil, Gnus will supply backends with data needed for async article fetching.")
195
196 (defvar gnus-asynchronous-article-function nil
197   "*Function for picking articles to pre-fetch, possibly.")
198
199 (defvar gnus-score-file-single-match-alist nil
200   "*Alist mapping regexps to lists of score files.
201 Each element of this alist should be of the form
202         (\"REGEXP\" [ \"SCORE-FILE-1\" ] [ \"SCORE-FILE-2\" ] ... )
203
204 If the name of a group is matched by REGEXP, the corresponding scorefiles
205 will be used for that group.
206 The first match found is used, subsequent matching entries are ignored (to
207 use multiple matches, see gnus-score-file-multiple-match-alist).
208
209 These score files are loaded in addition to any files returned by
210 gnus-score-find-score-files-function (which see).")
211
212 (defvar gnus-score-file-multiple-match-alist nil
213   "*Alist mapping regexps to lists of score files.
214 Each element of this alist should be of the form
215         (\"REGEXP\" [ \"SCORE-FILE-1\" ] [ \"SCORE-FILE-2\" ] ... )
216
217 If the name of a group is matched by REGEXP, the corresponding scorefiles
218 will be used for that group.
219 If multiple REGEXPs match a group, the score files corresponding to each
220 match will be used (for only one match to be used, see
221 gnus-score-file-single-match-alist).
222
223 These score files are loaded in addition to any files returned by
224 gnus-score-find-score-files-function (which see).")
225
226
227 (defvar gnus-score-file-suffix "SCORE"
228   "*Suffix of the score files.")
229
230 (defvar gnus-adaptive-file-suffix "ADAPT"
231   "*Suffix of the adaptive score files.")
232
233 (defvar gnus-score-find-score-files-function 'gnus-score-find-bnews
234   "*Function used to find score files.
235 The function will be called with the group name as the argument, and
236 should return a list of score files to apply to that group.  The score
237 files do not actually have to exist.
238
239 Predefined values are:
240
241 gnus-score-find-single: Only apply the group's own score file.
242 gnus-score-find-hierarchical: Also apply score files from parent groups.
243 gnus-score-find-bnews: Apply score files whose names matches.
244
245 See the documentation to these functions for more information.
246
247 This variable can also be a list of functions to be called.  Each
248 function should either return a list of score files, or a list of
249 score alists.")
250
251 (defvar gnus-score-interactive-default-score 1000
252   "*Scoring commands will raise/lower the score with this number as the default.")
253
254 (defvar gnus-large-newsgroup 200
255   "*The number of articles which indicates a large newsgroup.
256 If the number of articles in a newsgroup is greater than this value,
257 confirmation is required for selecting the newsgroup.")
258
259 ;; Suggested by Andrew Eskilsson <pi92ae@lelle.pt.hk-r.se>.
260 (defvar gnus-no-groups-message "No news is horrible news"
261   "*Message displayed by Gnus when no groups are available.")
262
263 (defvar gnus-use-long-file-name (not (memq system-type '(usg-unix-v xenix)))
264   "*Non-nil means that the default name of a file to save articles in is the group name.
265 If it's nil, the directory form of the group name is used instead.
266
267 If this variable is a list, and the list contains the element
268 `not-score', long file names will not be used for score files; if it
269 contains the element `not-save', long file names will not be used for
270 saving; and if it contains the element `not-kill', long file names
271 will not be used for kill files.")
272
273 (defvar gnus-article-save-directory (or (getenv "SAVEDIR") "~/News/")
274   "*Name of the directory articles will be saved in (default \"~/News\").
275 Initialized from the SAVEDIR environment variable.")
276
277 (defvar gnus-kill-files-directory (or (getenv "SAVEDIR") "~/News/")
278   "*Name of the directory where kill files will be stored (default \"~/News\").
279 Initialized from the SAVEDIR environment variable.")
280
281 (defvar gnus-default-article-saver 'gnus-summary-save-in-rmail
282   "*A function to save articles in your favorite format.
283 The function must be interactively callable (in other words, it must
284 be an Emacs command).
285
286 Gnus provides the following functions:
287
288 * gnus-summary-save-in-rmail (Rmail format)
289 * gnus-summary-save-in-mail (Unix mail format)
290 * gnus-summary-save-in-folder (MH folder)
291 * gnus-summary-save-in-file (article format).
292 * gnus-summary-save-in-vm (use VM's folder format).")
293
294 (defvar gnus-rmail-save-name (function gnus-plain-save-name)
295   "*A function generating a file name to save articles in Rmail format.
296 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
297
298 (defvar gnus-mail-save-name (function gnus-plain-save-name)
299   "*A function generating a file name to save articles in Unix mail format.
300 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
301
302 (defvar gnus-folder-save-name (function gnus-folder-save-name)
303   "*A function generating a file name to save articles in MH folder.
304 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER.")
305
306 (defvar gnus-file-save-name (function gnus-numeric-save-name)
307   "*A function generating a file name to save articles in article format.
308 The function is called with NEWSGROUP, HEADERS, and optional
309 LAST-FILE.")
310
311 (defvar gnus-split-methods nil
312   "*Variable used to suggest where articles are to be saved.
313 The syntax of this variable is the same as `nnmail-split-methods'.  
314
315 For instance, if you would like to save articles related to Gnus in
316 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
317 you could set this variable to something like:
318
319  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
320    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))")
321
322 (defvar gnus-save-score nil
323   "*If non-nil, save group scoring info.")
324
325 (defvar gnus-use-adaptive-scoring nil
326   "*If non-nil, use some adaptive scoring scheme.")
327
328 (defvar gnus-use-cache nil
329   "*If non-nil, Gnus will cache (some) articles locally.")
330
331 (defvar gnus-use-scoring t
332   "*If non-nil, enable scoring.")
333
334 (defvar gnus-fetch-old-headers nil
335   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
336 If an unread article in the group refers to an older, already read (or
337 just marked as read) article, the old article will not normally be
338 displayed in the Summary buffer.  If this variable is non-nil, Gnus
339 will attempt to grab the headers to the old articles, and thereby
340 build complete threads.  If it has the value `some', only enough
341 headers to connect otherwise loose threads will be displayed.
342
343 The server has to support XOVER for any of this to work.")
344
345 (defvar gnus-visual t
346   "*If non-nil, will do various highlighting.
347 If nil, no mouse highlights (or any other highlights) will be
348 performed.  This might speed up Gnus some when generating large group
349 and summary buffers.")
350
351 (defvar gnus-novice-user t
352   "*Non-nil means that you are a usenet novice.
353 If non-nil, verbose messages may be displayed and confirmations may be
354 required.")
355
356 (defvar gnus-expert-user nil
357   "*Non-nil means that you will never be asked for confirmation about anything.
358 And that means *anything*.")
359
360 (defvar gnus-verbose 7
361   "*Integer that says how verbose Gnus should be.
362 The higher the number, the more messages Gnus will flash to say what
363 it's doing.  At zero, Gnus will be totally mute; at five, Gnus will
364 display most important messages; and at ten, Gnus will keep on
365 jabbering all the time.")
366
367 (defvar gnus-keep-same-level nil
368   "*Non-nil means that the next newsgroup after the current will be on the same level.
369 When you type, for instance, `n' after reading the last article in the
370 current newsgroup, you will go to the next newsgroup. If this variable
371 is nil, the next newsgroup will be the next from the group
372 buffer. 
373 If this variable is non-nil, Gnus will either put you in the
374 next newsgroup with the same level, or, if no such newsgroup is
375 available, the next newsgroup with the lowest possible level higher
376 than the current level.
377 If this variable is `best', Gnus will make the next newsgroup the one
378 with the best level.")
379
380 (defvar gnus-summary-make-false-root 'adopt
381   "*nil means that Gnus won't gather loose threads.
382 If the root of a thread has expired or been read in a previous
383 session, the information necessary to build a complete thread has been
384 lost. Instead of having many small sub-threads from this original thread
385 scattered all over the summary buffer, Gnus can gather them. 
386
387 If non-nil, Gnus will try to gather all loose sub-threads from an
388 original thread into one large thread.
389
390 If this variable is non-nil, it should be one of `none', `adopt',
391 `dummy' or `empty'.
392
393 If this variable is `none', Gnus will not make a false root, but just
394 present the sub-threads after another.
395 If this variable is `dummy', Gnus will create a dummy root that will
396 have all the sub-threads as children.
397 If this variable is `adopt', Gnus will make one of the \"children\"
398 the parent and mark all the step-children as such.
399 If this variable is `empty', the \"children\" are printed with empty
400 subject fields.  (Or rather, they will be printed with a string
401 given by the `gnus-summary-same-subject' variable.)")
402
403 (defvar gnus-summary-gather-subject-limit nil
404   "*Maximum length of subject comparisons when gathering loose threads.
405 Use nil to compare full subjects.  Setting this variable to a low
406 number will help gather threads that have been corrupted by
407 newsreaders chopping off subject lines, but it might also mean that
408 unrelated articles that have subject that happen to begin with the
409 same few characters will be incorrectly gathered.
410
411 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
412 comparing subjects.")
413
414 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
415 (defvar gnus-summary-same-subject ""
416   "*String indicating that the current article has the same subject as the previous.
417 This variable will only be used if the value of
418 `gnus-summary-make-false-root' is `empty'.")
419
420 (defvar gnus-summary-goto-unread t
421   "*If non-nil, marking commands will go to the next unread article.")
422
423 (defvar gnus-group-goto-unread t
424   "*If non-nil, movement commands will go to the next unread and subscribed group.")
425
426 (defvar gnus-check-new-newsgroups t
427   "*Non-nil means that Gnus will add new newsgroups at startup.
428 If this variable is `ask-server', Gnus will ask the server for new
429 groups since the last time it checked. This means that the killed list
430 is no longer necessary, so you could set `gnus-save-killed-list' to
431 nil. 
432
433 A variant is to have this variable be a list of select methods. Gnus
434 will then use the `ask-server' method on all these select methods to
435 query for new groups from all those servers.
436
437 Eg.
438   (setq gnus-check-new-newsgroups 
439         '((nntp \"some.server\") (nntp \"other.server\")))
440
441 If this variable is nil, then you have to tell Gnus explicitly to
442 check for new newsgroups with \\<gnus-group-mode-map>\\[gnus-find-new-newsgroups].")
443
444 (defvar gnus-check-bogus-newsgroups nil
445   "*Non-nil means that Gnus will check and remove bogus newsgroup at startup.
446 If this variable is nil, then you have to tell Gnus explicitly to
447 check for bogus newsgroups with \\<gnus-group-mode-map>\\[gnus-group-check-bogus-groups].")
448
449 (defvar gnus-read-active-file t
450   "*Non-nil means that Gnus will read the entire active file at startup.
451 If this variable is nil, Gnus will only know about the groups in your
452 `.newsrc' file.
453
454 If this variable is `some', Gnus will try to only read the relevant
455 parts of the active file from the server.  Not all servers support
456 this, and it might be quite slow with other servers, but this should
457 generally be faster than both the t and nil value.
458
459 If you set this variable to nil or `some', you probably still want to
460 be told about new newsgroups that arrive.  To do that, set
461 `gnus-check-new-newsgroups' to `ask-server'.  This may not work
462 properly with all servers.")
463
464 (defvar gnus-level-subscribed 5
465   "*Groups with levels less than or equal to this variable are subscribed.")
466
467 (defvar gnus-level-unsubscribed 7
468   "*Groups with levels less than or equal to this variable are unsubscribed.
469 Groups with levels less than `gnus-level-subscribed', which should be
470 less than this variable, are subscribed.")
471
472 (defvar gnus-level-zombie 8
473   "*Groups with this level are zombie groups.")
474
475 (defvar gnus-level-killed 9
476   "*Groups with this level are killed.")
477
478 (defvar gnus-level-default-subscribed 3
479   "*New subscribed groups will be subscribed at this level.")
480
481 (defvar gnus-level-default-unsubscribed 6
482   "*New unsubscribed groups will be unsubscribed at this level.")
483
484 (defvar gnus-activate-foreign-newsgroups 4
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 If `gnus-visible-headers' is non-nil, this variable will be ignored.")
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 If this variable is non-nil, `gnus-ignored-headers' will be ignored.")
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-group-sort-by-alphabet', `gnus-group-sort-by-unread' and
775 `gnus-group-sort-by-level'")
776
777 ;; Mark variables suggested by Thomas Michanek
778 ;; <Thomas.Michanek@telelogic.se>. 
779 (defvar gnus-unread-mark ? 
780   "*Mark used for unread articles.")
781 (defvar gnus-ticked-mark ?!
782   "*Mark used for ticked articles.")
783 (defvar gnus-dormant-mark ??
784   "*Mark used for dormant articles.")
785 (defvar gnus-del-mark ?r
786   "*Mark used for del'd articles.")
787 (defvar gnus-read-mark ?R
788   "*Mark used for read articles.")
789 (defvar gnus-expirable-mark ?E
790   "*Mark used for expirable articles.")
791 (defvar gnus-killed-mark ?K
792   "*Mark used for killed articles.")
793 (defvar gnus-kill-file-mark ?X
794   "*Mark used for articles killed by kill files.")
795 (defvar gnus-low-score-mark ?Y
796   "*Mark used for articles with a low score.")
797 (defvar gnus-catchup-mark ?C
798   "*Mark used for articles that are caught up.")
799 (defvar gnus-replied-mark ?A
800   "*Mark used for articles that have been replied to.")
801 (defvar gnus-process-mark ?# 
802   "*Process mark.")
803 (defvar gnus-ancient-mark ?O
804   "*Mark used for ancient articles.")
805 (defvar gnus-canceled-mark ?G
806   "*Mark used for canceled articles.")
807 (defvar gnus-score-over-mark ?+
808   "*Score mark used for articles with high scores.")
809 (defvar gnus-score-below-mark ?-
810   "*Score mark used for articles with low scores.")
811 (defvar gnus-empty-thread-mark ? 
812   "*There is no thread under the article.")
813 (defvar gnus-not-empty-thread-mark ?=
814   "*There is a thread under the article.")
815 (defvar gnus-dummy-mark ?Z
816   "*This is a dummy article.")
817
818 (defvar gnus-view-pseudo-asynchronously nil
819   "*If non-nil, Gnus will view pseudo-articles asynchronously.")
820
821 (defvar gnus-view-pseudos nil
822   "*If `automatic', pseudo-articles will be viewed automatically.
823 If `not-confirm', pseudos will be viewed automatically, and the user
824 will not be asked to confirm the command.")
825
826 (defvar gnus-view-pseudos-separately t
827   "*If non-nil, one pseudo-article will be created for each file to be viewed.
828 If nil, all files that use the same viewing command will be given as a
829 list of parameters to that command.")
830
831 (defvar gnus-group-line-format "%M%S%p%5y: %(%g%)\n"
832   "*Format of group lines.
833 It works along the same lines as a normal formatting string,
834 with some simple extensions.
835
836 %M    Only marked articles (character, \"*\" or \" \")
837 %S    Whether the group is subscribed (character, \"U\", \"K\", \"Z\" or \" \")
838 %L    Level of subscribedness (integer)
839 %N    Number of unread articles (integer)
840 %I    Number of dormant articles (integer)
841 %i    Number of ticked and dormant (integer)
842 %T    Number of ticked articles (integer)
843 %R    Number of read articles (integer)
844 %t    Total number of articles (integer)
845 %y    Number of unread, unticked articles (integer)
846 %G    Group name (string)
847 %g    Qualified group name (string)
848 %D    Group description (string)
849 %s    Select method (string)
850 %o    Moderated group (char, \"m\")
851 %p    Process mark (char)
852 %O    Moderated group (string, \"(m)\" or \"\")
853 %n    Select from where (string)
854 %z    A string that look like `<%s:%n>' if a foreign select method is used
855 %u    User defined specifier. The next character in the format string should
856       be a letter.  Gnus will call the function gnus-user-format-function-X,
857       where X is the letter following %u. The function will be passed the
858       current header as argument. The function should return a string, which
859       will be inserted into the buffer just like information from any other
860       group specifier.
861
862 Text between %( and %) will be highlighted with `gnus-mouse-face' when
863 the mouse point move inside the area.  There can only be one such area.
864
865 Note that this format specification is not always respected. For
866 reasons of efficiency, when listing killed groups, this specification
867 is ignored altogether. If the spec is changed considerably, your
868 output may end up looking strange when listing both alive and killed
869 groups.
870
871 If you use %o or %O, reading the active file will be slower and quite
872 a bit of extra memory will be used. %D will also worsen performance.
873 Also note that if you change the format specification to include any
874 of these specs, you must probably re-start Gnus to see them go into
875 effect.") 
876
877 (defvar gnus-summary-line-format "%U%R%z%I%(%[%4L: %-20,20n%]%) %s\n"
878   "*The format specification of the lines in the summary buffer.
879
880 It works along the same lines as a normal formatting string,
881 with some simple extensions.
882
883 %N   Article number, left padded with spaces (string)
884 %S   Subject (string)
885 %s   Subject if it is at the root of a thread, and \"\" otherwise (string)
886 %n   Name of the poster (string)
887 %a   Extracted name of the poster (string)
888 %A   Extracted address of the poster (string)
889 %F   Contents of the From: header (string)
890 %x   Contents of the Xref: header (string)
891 %D   Date of the article (string)
892 %d   Date of the article (string) in DD-MMM format
893 %M   Message-id of the article (string)
894 %r   References of the article (string)
895 %c   Number of characters in the article (integer)
896 %L   Number of lines in the article (integer)
897 %I   Indentation based on thread level (a string of spaces)
898 %T   A string with two possible values: 80 spaces if the article
899      is on thread level two or larger and 0 spaces on level one
900 %R   \"R\" if this article has been replied to, \" \" otherwise (character)
901 %U   Status of this article (character, \"D\", \"K\", \"-\" or \" \")
902 %[   Opening bracket (character, \"[\" or \"<\")
903 %]   Closing bracket (character, \"]\" or \">\")
904 %>   Spaces of length thread-level (string)
905 %<   Spaces of length (- 20 thread-level) (string)
906 %i   Article score (number)
907 %z   Article zcore (character)
908 %t   Number of articles under the current thread (number).
909 %e   Whether the thread is empty or not (character).
910 %u   User defined specifier. The next character in the format string should
911      be a letter.  Gnus will call the function gnus-user-format-function-X,
912      where X is the letter following %u. The function will be passed the
913      current header as argument. The function should return a string, which
914      will be inserted into the summary just like information from any other
915      summary specifier.
916
917 Text between %( and %) will be highlighted with `gnus-mouse-face'
918 when the mouse point is placed inside the area.  There can only be one
919 such area.
920
921 The %U (status), %R (replied) and %z (zcore) specs have to be handled
922 with care. For reasons of efficiency, Gnus will compute what column
923 these characters will end up in, and \"hard-code\" that. This means that
924 it is illegal to have these specs after a variable-length spec. Well,
925 you might not be arrested, but your summary buffer will look strange,
926 which is bad enough.
927
928 The smart choice is to have these specs as for to the left as
929 possible. 
930
931 This restriction may disappear in later versions of Gnus.")
932
933 (defvar gnus-summary-dummy-line-format "*  :                          : %S\n"
934   "*The format specification for the dummy roots in the summary buffer.
935 It works along the same lines as a normal formatting string,
936 with some simple extensions.
937
938 %S  The subject")
939
940 (defvar gnus-summary-mode-line-format "(ding) %G/%A %Z"
941   "*The format specification for the summary mode line.")
942
943 (defvar gnus-article-mode-line-format "(ding) %G/%A %S"
944   "*The format specification for the article mode line.")
945
946 (defvar gnus-group-mode-line-format "(ding) List of groups   {%M:%S}  "
947   "*The format specification for the group mode line.")
948
949 (defvar gnus-valid-select-methods
950   '(("nntp" post address prompt-address)
951     ("nnspool" post)
952     ("nnvirtual" none virtual prompt-address) 
953     ("nnmbox" mail respool) 
954     ("nnml" mail respool)
955     ("nnmh" mail respool) 
956     ("nndir" none prompt-address address)
957     ("nneething" none prompt-address)
958     ("nndigest" none) 
959     ("nndoc" none prompt-address) 
960     ("nnbabyl" mail respool) 
961     ("nnkiboze" post virtual) 
962     ("nnsoup" post)
963     ("nnfolder" mail respool))
964   "An alist of valid select methods.
965 The first element of each list lists should be a string with the name
966 of the select method. The other elements may be be the category of
967 this method (ie. `post', `mail', `none' or whatever) or other
968 properties that this method has (like being respoolable).
969 If you implement a new select method, all you should have to change is
970 this variable. I think.")
971
972 (defvar gnus-updated-mode-lines '(group article summary)
973   "*List of buffers that should update their mode lines.
974 The list may contain the symbols `group', `article' and `summary'. If
975 the corresponding symbol is present, Gnus will keep that mode line
976 updated with information that may be pertinent. 
977 If this variable is nil, screen refresh may be quicker.")
978
979 ;; Added by Keinonen Kari <kk85613@cs.tut.fi>.
980 (defvar gnus-mode-non-string-length 21
981   "*Max length of mode-line non-string contents.
982 If this is nil, Gnus will take space as is needed, leaving the rest
983 of the modeline intact.")
984
985 (defvar gnus-mouse-face 'highlight
986   "*Face used for mouse highlighting in Gnus.
987 No mouse highlights will be done if `gnus-visual' is nil.")
988
989 (defvar gnus-summary-mark-below nil
990   "*Mark all articles with a score below this variable as read.
991 This variable is local to each summary buffer and usually set by the
992 score file.")  
993
994 (defvar gnus-thread-sort-functions '(gnus-thread-sort-by-number)
995   "*List of functions used for sorting threads in the summary buffer.
996 By default, threads are sorted by article number.
997
998 Each function takes two threads and return non-nil if the first thread
999 should be sorted before the other.  If you use more than one function,
1000 the primary sort function should be the last.
1001
1002 Ready-mady functions include `gnus-thread-sort-by-number',
1003 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
1004 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
1005 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').")
1006
1007 (defvar gnus-thread-score-function '+
1008   "*Function used for calculating the total score of a thread.
1009
1010 The function is called with the scores of the article and each
1011 subthread and should then return the score of the thread.
1012
1013 Some functions you can use are `+', `max', or `min'.")
1014
1015 (defvar gnus-options-subscribe nil
1016   "*All new groups matching this regexp will be subscribed unconditionally.
1017 Note that this variable deals only with new newsgroups.  This variable
1018 does not affect old newsgroups.")
1019
1020 (defvar gnus-options-not-subscribe nil
1021   "*All new groups matching this regexp will be ignored.
1022 Note that this variable deals only with new newsgroups.  This variable
1023 does not affect old (already subscribed) newsgroups.")
1024
1025 (defvar gnus-auto-expirable-newsgroups nil
1026   "*Groups in which to automatically mark read articles as expirable.
1027 If non-nil, this should be a regexp that should match all groups in
1028 which to perform auto-expiry.  This only makes sense for mail groups.")
1029
1030 (defvar gnus-hidden-properties '(invisible t intangible t)
1031   "Property list to use for hiding text.")
1032
1033 ;; Hooks.
1034
1035 (defvar gnus-group-mode-hook nil
1036   "*A hook for Gnus group mode.")
1037
1038 (defvar gnus-summary-mode-hook nil
1039   "*A hook for Gnus summary mode.
1040 This hook is run before any variables are set in the summary buffer.")
1041
1042 (defvar gnus-article-mode-hook nil
1043   "*A hook for Gnus article mode.")
1044
1045 (defun gnus-summary-prepare-exit-hook nil
1046   "*A hook called when preparing to exit from the summary buffer.
1047 It calls `gnus-summary-expire-articles' by default.")
1048 (add-hook 'gnus-summary-prepare-exit-hook 'gnus-summary-expire-articles)
1049
1050 (defun gnus-summary-exit-hook nil
1051   "*A hook called on exit from the summary buffer.")
1052
1053 (defvar gnus-open-server-hook nil
1054   "*A hook called just before opening connection to the news server.")
1055
1056 (defvar gnus-startup-hook nil
1057   "*A hook called at startup.
1058 This hook is called after Gnus is connected to the NNTP server.")
1059
1060 (defvar gnus-get-new-news-hook nil
1061   "*A hook run just before Gnus checks for new news.")
1062
1063 (defvar gnus-group-prepare-function 'gnus-group-prepare-flat
1064   "*A function that is called to generate the group buffer.
1065 The function is called with three arguments: The first is a number;
1066 all group with a level less or equal to that number should be listed,
1067 if the second is non-nil, empty groups should also be displayed. If
1068 the third is non-nil, it is a number. No groups with a level lower
1069 than this number should be displayed.
1070
1071 The only current function implemented is `gnus-group-prepare-flat'.")
1072
1073 (defvar gnus-group-prepare-hook nil
1074   "*A hook called after the group buffer has been generated.
1075 If you want to modify the group buffer, you can use this hook.")
1076
1077 (defvar gnus-summary-prepare-hook nil
1078   "*A hook called after the summary buffer has been generated.
1079 If you want to modify the summary buffer, you can use this hook.")
1080
1081 (defvar gnus-article-prepare-hook nil
1082   "*A hook called after an article has been prepared in the article buffer.
1083 If you want to run a special decoding program like nkf, use this hook.")
1084
1085 (defvar gnus-article-display-hook nil
1086   "*A hook called after the article is displayed in the article buffer.
1087 The hook is designed to change the contents of the article
1088 buffer. Typical functions that this hook may contain are
1089 `gnus-article-hide-headers' (hide selected headers),
1090 `gnus-article-maybe-highlight' (perform fancy article highlighting), 
1091 `gnus-article-hide-signature' (hide signature) and
1092 `gnus-article-treat-overstrike' (turn \"^H_\" into bold characters).")
1093 (add-hook 'gnus-article-display-hook 'gnus-article-hide-headers-if-wanted)
1094 (add-hook 'gnus-article-display-hook 'gnus-article-treat-overstrike)
1095 (add-hook 'gnus-article-display-hook 'gnus-article-maybe-highlight)
1096
1097 (defvar gnus-article-x-face-command
1098   "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | xv -quit -"
1099   "String or function to be executed to display an X-Face header.
1100 If it is a string, the command will be executed in a sub-shell
1101 asynchronously. The compressed face will be piped to this command.") 
1102
1103 (defvar gnus-article-x-face-too-ugly nil
1104   "Regexp matching posters whose face shouldn't be shown automatically.")
1105
1106 (defvar gnus-select-group-hook nil
1107   "*A hook called when a newsgroup is selected.
1108
1109 If you'd like to simplify subjects like the
1110 `gnus-summary-next-same-subject' command does, you can use the
1111 following hook:
1112
1113  (setq gnus-select-group-hook
1114       (list
1115         (lambda ()
1116           (mapcar (lambda (header)
1117                      (header-set-subject
1118                       header
1119                       (gnus-simplify-subject
1120                        (header-subject header) 're-only)))
1121                   gnus-newsgroup-headers))))")
1122
1123 (defvar gnus-select-article-hook
1124   '(gnus-summary-show-thread)
1125   "*A hook called when an article is selected.
1126 The default hook shows conversation thread subtrees of the selected
1127 article automatically using `gnus-summary-show-thread'.")
1128
1129 (defvar gnus-apply-kill-hook '(gnus-apply-kill-file)
1130   "*A hook called to apply kill files to a group.
1131 This hook is intended to apply a kill file to the selected newsgroup.
1132 The function `gnus-apply-kill-file' is called by default.
1133
1134 Since a general kill file is too heavy to use only for a few
1135 newsgroups, I recommend you to use a lighter hook function. For
1136 example, if you'd like to apply a kill file to articles which contains
1137 a string `rmgroup' in subject in newsgroup `control', you can use the
1138 following hook:
1139
1140 \(setq gnus-apply-kill-hook
1141       (list
1142         (lambda ()
1143           (cond ((string-match \"control\" gnus-newsgroup-name)
1144                  (gnus-kill \"Subject\" \"rmgroup\")
1145                  (gnus-expunge \"X\"))))))")
1146
1147 (defvar gnus-visual-mark-article-hook 
1148   (list 'gnus-highlight-selected-summary)
1149   "*Hook run after selecting an article in the summary buffer.
1150 It is meant to be used for highlighting the article in some way.  It
1151 is not run if `gnus-visual' is nil.")
1152
1153 (defvar gnus-prepare-article-hook (list 'gnus-inews-insert-signature)
1154   "*A hook called after preparing body, but before preparing header headers.
1155 The default hook (`gnus-inews-insert-signature') inserts a signature
1156 file specified by the variable `gnus-signature-file'.")
1157
1158 (defvar gnus-exit-group-hook nil
1159   "*A hook called when exiting (not quitting) summary mode.")
1160
1161 (defvar gnus-suspend-gnus-hook nil
1162   "*A hook called when suspending (not exiting) Gnus.")
1163
1164 (defvar gnus-exit-gnus-hook nil
1165   "*A hook called when exiting Gnus.")
1166
1167 (defvar gnus-save-newsrc-hook nil
1168   "*A hook called when saving the newsrc file.")
1169
1170 (defvar gnus-summary-update-hook 
1171   (list 'gnus-summary-highlight-line)
1172   "*A hook called when a summary line is changed.
1173 The hook will not be called if `gnus-visual' is nil.
1174
1175 The default function `gnus-summary-highlight-line' will
1176 highlight the line according to the `gnus-summary-highlight'
1177 variable.")
1178
1179 (defvar gnus-mark-article-hook (list 'gnus-summary-mark-unread-as-read)
1180   "*A hook called when an article is selected for the first time.
1181 The hook is intended to mark an article as read (or unread)
1182 automatically when it is selected.")
1183
1184 ;; Remove any hilit infestation.
1185 (add-hook 'gnus-startup-hook
1186           (lambda ()
1187             (remove-hook 'gnus-summary-prepare-hook
1188                          'hilit-rehighlight-buffer-quietly)
1189             (remove-hook 'gnus-summary-prepare-hook 'hilit-install-line-hooks)
1190             (setq gnus-mark-article-hook '(gnus-summary-mark-unread-as-read))
1191             (remove-hook 'gnus-article-prepare-hook
1192                          'hilit-rehighlight-buffer-quietly)))
1193
1194
1195 \f
1196 ;; Internal variables
1197
1198 ;; Avoid highlighting in kill files.
1199 (defvar gnus-summary-inhibit-highlight nil)
1200 (defvar gnus-newsgroup-selected-overlay nil)
1201
1202 (defvar gnus-article-mode-map nil)
1203 (defvar gnus-dribble-buffer nil)
1204 (defvar gnus-headers-retrieved-by nil)
1205 (defvar gnus-article-reply nil)
1206 (defvar gnus-override-method nil)
1207 (defvar gnus-article-check-size nil)
1208
1209 (defvar gnus-current-score-file nil)
1210 (defvar gnus-internal-global-score-files nil)
1211 (defvar gnus-score-file-list nil)
1212
1213
1214 (defvar gnus-current-move-group nil)
1215
1216 (defvar gnus-newsgroup-dependencies nil)
1217 (defvar gnus-newsgroup-threads nil)
1218 (defvar gnus-newsgroup-async nil)
1219 (defconst gnus-group-edit-buffer "*Gnus edit newsgroup*")
1220
1221 (defvar gnus-newsgroup-adaptive nil)
1222
1223 (defvar gnus-summary-display-table nil)
1224
1225 (defconst gnus-group-line-format-alist
1226   (list (list ?M 'marked ?c)
1227         (list ?S 'subscribed ?c)
1228         (list ?L 'level ?d)
1229         (list ?N 'number ?s)
1230         (list ?I 'number-of-dormant ?d)
1231         (list ?T 'number-of-ticked ?d)
1232         (list ?R 'number-of-read ?s)
1233         (list ?t 'number-total ?d)
1234         (list ?y 'number-of-unread-unticked ?s)
1235         (list ?i 'number-of-ticked-and-dormant ?d)
1236         (list ?g 'group ?s)
1237         (list ?G 'qualified-group ?s)
1238         (list ?D 'newsgroup-description ?s)
1239         (list ?o 'moderated ?c)
1240         (list ?O 'moderated-string ?s)
1241         (list ?p 'process-marked ?c)
1242         (list ?s 'news-server ?s)
1243         (list ?n 'news-method ?s)
1244         (list ?z 'news-method-string ?s)
1245         (list ?u 'user-defined ?s)))
1246
1247 (defconst gnus-summary-line-format-alist 
1248   (list (list ?N 'number ?d)
1249         (list ?S 'subject ?s)
1250         (list ?s 'subject-or-nil ?s)
1251         (list ?n 'name ?s)
1252         (list ?A '(car (cdr (funcall gnus-extract-address-components from)))
1253               ?s)
1254         (list ?a '(or (car (funcall gnus-extract-address-components from)) 
1255                       from) ?s)
1256         (list ?F 'from ?s)
1257         (list ?x (macroexpand '(header-xref header)) ?s)
1258         (list ?D (macroexpand '(header-date header)) ?s)
1259         (list ?d '(gnus-dd-mmm (header-date header)) ?s)
1260         (list ?M (macroexpand '(header-id header)) ?s)
1261         (list ?r (macroexpand '(header-references header)) ?s)
1262         (list ?c '(or (header-chars header) 0) ?d)
1263         (list ?L 'lines ?d)
1264         (list ?I 'indentation ?s)
1265         (list ?T '(if (= level 0) "" (make-string (frame-width) ? )) ?s)
1266         (list ?R 'replied ?c)
1267         (list ?\[ 'opening-bracket ?c)
1268         (list ?\] 'closing-bracket ?c)
1269         (list ?\> '(make-string level ? ) ?s)
1270         (list ?\< '(make-string (max 0 (- 20 level)) ? ) ?s)
1271         (list ?i 'score ?d)
1272         (list ?z 'score-char ?c)
1273         (list ?U 'unread ?c)
1274         (list ?t '(gnus-summary-number-of-articles-in-thread 
1275                    (and (boundp 'thread) (car thread)))
1276               ?d)
1277         (list ?e '(gnus-summary-number-of-articles-in-thread 
1278                    (and (boundp 'thread) (car thread)) t)
1279               ?c)
1280         (list ?u 'user-defined ?s))
1281   "An alist of format specifications that can appear in summary lines,
1282 and what variables they correspond with, along with the type of the
1283 variable (string, integer, character, etc).")
1284
1285 (defconst gnus-summary-dummy-line-format-alist
1286   (list (list ?S 'subject ?s)
1287         (list ?N 'number ?d)
1288         (list ?u 'user-defined ?s)))
1289
1290 (defconst gnus-summary-mode-line-format-alist 
1291   (list (list ?G 'group-name ?s)
1292         (list ?g '(gnus-short-group-name group-name) ?s)
1293         (list ?A 'article-number ?d)
1294         (list ?Z 'unread-and-unselected ?s)
1295         (list ?V 'gnus-version ?s)
1296         (list ?U 'unread ?d)
1297         (list ?S 'subject ?s)
1298         (list ?e 'unselected ?d)
1299         (list ?u 'user-defined ?s)
1300         (list ?s '(gnus-current-score-file-nondirectory) ?s)))
1301
1302 (defconst gnus-group-mode-line-format-alist 
1303   (list (list ?S 'news-server ?s)
1304         (list ?M 'news-method ?s)
1305         (list ?u 'user-defined ?s)))
1306
1307 (defvar gnus-have-read-active-file nil)
1308
1309 (defconst gnus-maintainer
1310   "gnus-bug@ifi.uio.no (The Gnus Bugfixing Girls + Boys)"
1311   "The mail address of the Gnus maintainers.")
1312
1313 (defconst gnus-version "(ding) Gnus v0.99.13"
1314   "Version number for this version of Gnus.")
1315
1316 (defvar gnus-info-nodes
1317   '((gnus-group-mode            "(gnus)The Group Buffer")
1318     (gnus-summary-mode          "(gnus)The Summary Buffer")
1319     (gnus-article-mode          "(gnus)The Article Buffer"))
1320   "Assoc list of major modes and related Info nodes.")
1321
1322 (defvar gnus-documentation-group-file "~/dgnus/lisp/doc.txt"
1323   "The location of the (ding) Gnus documentation group.")
1324
1325 (defvar gnus-group-buffer "*Group*")
1326 (defvar gnus-summary-buffer "*Summary*")
1327 (defvar gnus-article-buffer "*Article*")
1328 (defvar gnus-server-buffer "*Server*")
1329
1330 (defvar gnus-work-buffer " *gnus work*")
1331
1332 (defvar gnus-buffer-list nil
1333   "Gnus buffers that should be killed on exit.")
1334
1335 (defvar gnus-server-alist nil
1336   "List of available servers.")
1337
1338 (defvar gnus-variable-list
1339   '(gnus-newsrc-options gnus-newsrc-options-n
1340     gnus-newsrc-last-checked-date 
1341     gnus-newsrc-alist gnus-server-alist
1342     gnus-killed-list gnus-zombie-list)
1343   "Gnus variables saved in the quick startup file.")
1344
1345 (defvar gnus-overload-functions
1346   '((news-inews gnus-inews-news "rnewspost"))
1347   "Functions overloaded by gnus.
1348 It is a list of `(original overload &optional file)'.")
1349
1350 (defvar gnus-newsrc-options nil
1351   "Options line in the .newsrc file.")
1352
1353 (defvar gnus-newsrc-options-n nil
1354   "List of regexps representing groups to be subscribed/ignored unconditionally.") 
1355
1356 (defvar gnus-newsrc-last-checked-date nil
1357   "Date Gnus last asked server for new newsgroups.")
1358
1359 (defvar gnus-newsrc-alist nil
1360   "Assoc list of read articles.
1361 gnus-newsrc-hashtb should be kept so that both hold the same information.")
1362
1363 (defvar gnus-newsrc-hashtb nil
1364   "Hashtable of gnus-newsrc-alist.")
1365
1366 (defvar gnus-killed-list nil
1367   "List of killed newsgroups.")
1368
1369 (defvar gnus-killed-hashtb nil
1370   "Hash table equivalent of gnus-killed-list.")
1371
1372 (defvar gnus-zombie-list nil
1373   "List of almost dead newsgroups.")
1374
1375 (defvar gnus-description-hashtb nil
1376   "Descriptions of newsgroups.")
1377
1378 (defvar gnus-list-of-killed-groups nil
1379   "List of newsgroups that have recently been killed by the user.")
1380
1381 (defvar gnus-active-hashtb nil
1382   "Hashtable of active articles.")
1383
1384 (defvar gnus-moderated-list nil
1385   "List of moderated newsgroups.")
1386
1387 (defvar gnus-group-marked nil)
1388
1389 (defvar gnus-current-startup-file nil
1390   "Startup file for the current host.")
1391
1392 (defvar gnus-last-search-regexp nil
1393   "Default regexp for article search command.")
1394
1395 (defvar gnus-last-shell-command nil
1396   "Default shell command on article.")
1397
1398 (defvar gnus-current-select-method nil
1399   "The current method for selecting a newsgroup.")
1400
1401 (defvar gnus-group-list-mode nil)
1402
1403 (defvar gnus-article-internal-prepare-hook nil)
1404
1405 (defvar gnus-newsgroup-name nil)
1406 (defvar gnus-newsgroup-begin nil)
1407 (defvar gnus-newsgroup-end nil)
1408 (defvar gnus-newsgroup-last-rmail nil)
1409 (defvar gnus-newsgroup-last-mail nil)
1410 (defvar gnus-newsgroup-last-folder nil)
1411 (defvar gnus-newsgroup-last-file nil)
1412 (defvar gnus-newsgroup-auto-expire nil)
1413 (defvar gnus-newsgroup-active nil)
1414
1415 (defvar gnus-newsgroup-unreads nil
1416   "List of unread articles in the current newsgroup.")
1417
1418 (defvar gnus-newsgroup-unselected nil
1419   "List of unselected unread articles in the current newsgroup.")
1420
1421 (defvar gnus-newsgroup-reads nil
1422   "Alist of read articles and article marks in the current newsgroup.")
1423
1424 (defvar gnus-newsgroup-marked nil
1425   "List of ticked articles in the current newsgroup (a subset of unread art).")
1426
1427 (defvar gnus-newsgroup-killed nil
1428   "List of ranges of articles that have been through the scoring process.")
1429
1430 (defvar gnus-newsgroup-kill-headers nil)
1431
1432 (defvar gnus-newsgroup-replied nil
1433   "List of articles that have been replied to in the current newsgroup.")
1434
1435 (defvar gnus-newsgroup-expirable nil
1436   "List of articles in the current newsgroup that can be expired.")
1437
1438 (defvar gnus-newsgroup-processable nil
1439   "List of articles in the current newsgroup that can be processed.")
1440
1441 (defvar gnus-newsgroup-bookmarks nil
1442   "List of articles in the current newsgroup that have bookmarks.")
1443
1444 (defvar gnus-newsgroup-dormant nil
1445   "List of dormant articles in the current newsgroup.")
1446
1447 (defvar gnus-newsgroup-scored nil
1448   "List of scored articles in the current newsgroup.")
1449
1450 (defvar gnus-newsgroup-headers nil
1451   "List of article headers in the current newsgroup.")
1452 (defvar gnus-newsgroup-headers-hashtb-by-number nil)
1453
1454 (defvar gnus-newsgroup-ancient nil
1455   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1456
1457 (defvar gnus-current-article nil)
1458 (defvar gnus-article-current nil)
1459 (defvar gnus-current-headers nil)
1460 (defvar gnus-have-all-headers nil)
1461 (defvar gnus-last-article nil)
1462 (defvar gnus-newsgroup-history nil)
1463 (defvar gnus-current-kill-article nil)
1464
1465 ;; Save window configuration.
1466 (defvar gnus-prev-winconf nil)
1467
1468 ;; Format specs
1469 (defvar gnus-summary-line-format-spec nil)
1470 (defvar gnus-summary-dummy-line-format-spec nil)
1471 (defvar gnus-group-line-format-spec nil)
1472 (defvar gnus-summary-mode-line-format-spec nil)
1473 (defvar gnus-article-mode-line-format-spec nil)
1474 (defvar gnus-group-mode-line-format-spec nil)
1475 (defvar gnus-summary-mark-positions nil)
1476 (defvar gnus-group-mark-positions nil)
1477
1478 (defvar gnus-summary-expunge-below nil)
1479 (defvar gnus-reffed-article-number nil)
1480
1481 (defvar rmail-default-file (expand-file-name "~/XMBOX"))
1482 (defvar rmail-default-rmail-file (expand-file-name "~/XNEWS"))
1483
1484 (defvar gnus-cache-removeable-articles nil)
1485
1486 (defconst gnus-summary-local-variables 
1487   '(gnus-newsgroup-name 
1488     gnus-newsgroup-begin gnus-newsgroup-end 
1489     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail 
1490     gnus-newsgroup-last-folder gnus-newsgroup-last-file 
1491     gnus-newsgroup-auto-expire gnus-newsgroup-unreads 
1492     gnus-newsgroup-unselected gnus-newsgroup-marked
1493     gnus-newsgroup-reads
1494     gnus-newsgroup-replied gnus-newsgroup-expirable
1495     gnus-newsgroup-processable gnus-newsgroup-killed
1496     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1497     gnus-newsgroup-headers gnus-newsgroup-headers-hashtb-by-number
1498     gnus-current-article gnus-current-headers gnus-have-all-headers
1499     gnus-last-article gnus-article-internal-prepare-hook
1500     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1501     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1502     gnus-newsgroup-threads gnus-newsgroup-async
1503     gnus-score-alist gnus-current-score-file gnus-summary-expunge-below 
1504     gnus-summary-mark-below gnus-newsgroup-active gnus-scores-exclude-files
1505     gnus-newsgroup-history gnus-newsgroup-ancient
1506     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1507     gnus-cache-removeable-articles)
1508   "Variables that are buffer-local to the summary buffers.")
1509
1510 (defconst gnus-bug-message
1511   "Sending a bug report to the Gnus Towers.
1512 ========================================
1513
1514 The buffer below is a mail buffer.  When you press `C-c C-c', it will
1515 be sent to the Gnus Bug Exterminators. 
1516
1517 At the bottom of the buffer you'll see lots of variable settings.
1518 Please do not delete those.  They will tell the Bug People what your
1519 environment is, so that it will be easier to locate the bugs.
1520
1521 If you have found a bug that makes Emacs go \"beep\", set
1522 debug-on-error to t (`M-x set-variable RET debug-on-error RET t RET') 
1523 and include the backtrace in your bug report.
1524
1525 Please describe the bug in annoying, painstaking detail.
1526
1527 Thank you for your help in stamping out bugs.
1528 ")
1529
1530 ;;; End of variables.
1531
1532 ;; Define some autoload functions Gnus might use.
1533 (eval-and-compile
1534
1535   ;; Various 
1536   (autoload 'metamail-buffer "metamail")
1537   (autoload 'Info-goto-node "info")
1538   (autoload 'hexl-hex-string-to-integer "hexl")
1539   (autoload 'pp "pp")
1540   (autoload 'pp-to-string "pp")
1541   (autoload 'pp-eval-expression "pp")
1542   (autoload 'mail-extract-address-components "mail-extr")
1543
1544   (autoload 'nnmail-split-fancy "nnmail")
1545   (autoload 'nnvirtual-catchup-group "nnvirtual")
1546
1547   ;; timezone
1548   (autoload 'timezone-make-date-arpa-standard "timezone")
1549   (autoload 'timezone-fix-time "timezone")
1550   (autoload 'timezone-make-sortable-date "timezone")
1551   (autoload 'timezone-make-time-string "timezone")
1552
1553   ;; rmail & friends
1554   (autoload 'mail-position-on-field "sendmail")
1555   (autoload 'mail-setup "sendmail")
1556   (autoload 'rmail-output "rmailout")
1557   (autoload 'news-mail-other-window "rnewspost")
1558   (autoload 'news-reply-yank-original "rnewspost")
1559   (autoload 'news-caesar-buffer-body "rnewspost")
1560   (autoload 'rmail-insert-rmail-file-header "rmail")
1561   (autoload 'rmail-count-new-messages "rmail")
1562   (autoload 'rmail-show-message "rmail")
1563
1564   ;; gnus-soup
1565   (autoload 'gnus-group-brew-soup "gnus-soup" nil t)
1566   (autoload 'gnus-brew-soup "gnus-soup" nil t)
1567   (autoload 'gnus-soup-add-article "gnus-soup" nil t)
1568   (autoload 'gnus-soup-send-replies "gnus-soup" nil t)
1569   (autoload 'gnus-soup-save-areas "gnus-soup" nil t)
1570   (autoload 'gnus-soup-pack-packet "gnus-soup" nil t)
1571   (autoload 'nnsoup-pack-replies "nnsoup" nil t)
1572
1573   ;; gnus-mh
1574   (autoload 'gnus-mail-reply-using-mhe "gnus-mh")
1575   (autoload 'gnus-mail-forward-using-mhe "gnus-mh")
1576   (autoload 'gnus-mail-other-window-using-mhe "gnus-mh")
1577   (autoload 'gnus-summary-save-in-folder "gnus-mh")
1578   (autoload 'gnus-summary-save-article-folder "gnus-mh")
1579   (autoload 'gnus-Folder-save-name "gnus-mh")
1580   (autoload 'gnus-folder-save-name "gnus-mh")
1581
1582   ;; gnus-vis misc
1583   (autoload 'gnus-group-make-menu-bar "gnus-vis")
1584   (autoload 'gnus-summary-make-menu-bar "gnus-vis")
1585   (autoload 'gnus-server-make-menu-bar "gnus-vis")
1586   (autoload 'gnus-article-make-menu-bar "gnus-vis")
1587   (autoload 'gnus-browse-make-menu-bar "gnus-vis")
1588   (autoload 'gnus-highlight-selected-summary "gnus-vis")
1589   (autoload 'gnus-summary-highlight-line "gnus-vis")
1590   (autoload 'gnus-carpal-setup-buffer "gnus-vis")
1591
1592   ;; gnus-vis article
1593   (autoload 'gnus-article-push-button "gnus-vis" nil t)
1594   (autoload 'gnus-article-press-button "gnus-vis" nil t)
1595   (autoload 'gnus-article-highlight "gnus-vis" nil t)
1596   (autoload 'gnus-article-highlight-some "gnus-vis" nil t)
1597   (autoload 'gnus-article-hide "gnus-vis" nil t)
1598   (autoload 'gnus-article-hide-signature "gnus-vis" nil t)
1599   (autoload 'gnus-article-highlight-headers "gnus-vis" nil t)
1600   (autoload 'gnus-article-highlight-signature "gnus-vis" nil t)
1601   (autoload 'gnus-article-add-buttons "gnus-vis" nil t)
1602   (autoload 'gnus-article-next-button "gnus-vis" nil t)
1603   (autoload 'gnus-article-add-button "gnus-vis")
1604
1605   ;; gnus-cite
1606   (autoload 'gnus-article-highlight-citation "gnus-cite" nil t)
1607   (autoload 'gnus-article-hide-citation-maybe "gnus-cite" nil t)
1608   (autoload 'gnus-article-hide-citation "gnus-cite" nil t)
1609
1610   ;; gnus-kill
1611   (autoload 'gnus-kill "gnus-kill")
1612   (autoload 'gnus-apply-kill-file-internal "gnus-kill")
1613   (autoload 'gnus-kill-file-edit-file "gnus-kill")
1614   (autoload 'gnus-kill-file-raise-followups-to-author "gnus-kill")
1615   (autoload 'gnus-execute "gnus-kill")
1616   (autoload 'gnus-expunge "gnus-kill")
1617
1618   ;; gnus-cache
1619   (autoload 'gnus-cache-possibly-enter-article "gnus-cache")
1620   (autoload 'gnus-cache-save-buffers "gnus-cache")
1621   (autoload 'gnus-cache-possibly-remove-articles "gnus-cache")
1622   (autoload 'gnus-cache-request-article "gnus-cache")
1623   (autoload 'gnus-cache-retrieve-headers "gnus-cache")
1624   (autoload 'gnus-cache-possibly-alter-active "gnus-cache")
1625   (autoload 'gnus-jog-cache "gnus-cache" nil t)
1626   (autoload 'gnus-cache-enter-remove-article "gnus-cache")
1627
1628   ;; gnus-score
1629   (autoload 'gnus-summary-increase-score "gnus-score" nil t)
1630   (autoload 'gnus-summary-lower-score "gnus-score" nil t)
1631   (autoload 'gnus-summary-score-map "gnus-score" nil nil 'keymap)
1632   (autoload 'gnus-score-save "gnus-score")
1633   (autoload 'gnus-score-headers "gnus-score")
1634   (autoload 'gnus-current-score-file-nondirectory "gnus-score")
1635   (autoload 'gnus-score-adaptive "gnus-score")
1636   (autoload 'gnus-score-remove-lines-adaptive "gnus-score")
1637   (autoload 'gnus-score-find-trace "gnus-score")
1638
1639   ;; gnus-edit
1640   (autoload 'gnus-score-customize "gnus-edit" nil t)
1641
1642   ;; gnus-uu
1643   (autoload 'gnus-uu-extract-map "gnus-uu" nil nil 'keymap)
1644   (autoload 'gnus-uu-mark-map "gnus-uu" nil nil 'keymap)
1645   (autoload 'gnus-uu-digest-mail-forward "gnus-uu" nil t)
1646   (autoload 'gnus-uu-digest-post-forward "gnus-uu" nil t)
1647   (autoload 'gnus-uu-mark-series "gnus-uu" nil t)
1648   (autoload 'gnus-uu-mark-region "gnus-uu" nil t)
1649   (autoload 'gnus-uu-mark-by-regexp "gnus-uu" nil t)
1650   (autoload 'gnus-uu-mark-all "gnus-uu" nil t)
1651   (autoload 'gnus-uu-mark-sparse "gnus-uu" nil t)
1652   (autoload 'gnus-uu-mark-thread "gnus-uu" nil t)
1653   (autoload 'gnus-uu-decode-uu "gnus-uu" nil t)
1654   (autoload 'gnus-uu-decode-uu-and-save "gnus-uu" nil t)
1655   (autoload 'gnus-uu-decode-unshar "gnus-uu" nil t)
1656   (autoload 'gnus-uu-decode-unshar-and-save "gnus-uu" nil t)
1657   (autoload 'gnus-uu-decode-save "gnus-uu" nil t)
1658   (autoload 'gnus-uu-decode-binhex "gnus-uu" nil t)
1659   (autoload 'gnus-uu-decode-uu-view "gnus-uu" nil t)
1660   (autoload 'gnus-uu-decode-uu-and-save-view "gnus-uu" nil t)
1661   (autoload 'gnus-uu-decode-unshar-view "gnus-uu" nil t)
1662   (autoload 'gnus-uu-decode-unshar-and-save-view "gnus-uu" nil t)
1663   (autoload 'gnus-uu-decode-save-view "gnus-uu" nil t)
1664   (autoload 'gnus-uu-decode-binhex-view "gnus-uu" nil t)
1665
1666   ;; gnus-msg
1667   (autoload 'gnus-summary-send-map "gnus-msg" nil nil 'keymap)
1668   (autoload 'gnus-group-post-news "gnus-msg" nil t)
1669   (autoload 'gnus-group-mail "gnus-msg" nil t)
1670   (autoload 'gnus-summary-post-news "gnus-msg" nil t)
1671   (autoload 'gnus-summary-followup "gnus-msg" nil t)
1672   (autoload 'gnus-summary-followup-with-original "gnus-msg" nil t)
1673   (autoload 'gnus-summary-followup-and-reply "gnus-msg" nil t)
1674   (autoload 'gnus-summary-followup-and-reply-with-original "gnus-msg" nil t)
1675   (autoload 'gnus-summary-cancel-article "gnus-msg" nil t)
1676   (autoload 'gnus-summary-supersede-article "gnus-msg" nil t)
1677   (autoload 'gnus-post-news "gnus-msg" nil t)
1678   (autoload 'gnus-inews-news "gnus-msg" nil t)
1679   (autoload 'gnus-cancel-news "gnus-msg" nil t)
1680   (autoload 'gnus-summary-reply "gnus-msg" nil t)
1681   (autoload 'gnus-summary-reply-with-original "gnus-msg" nil t)
1682   (autoload 'gnus-summary-mail-forward "gnus-msg" nil t)
1683   (autoload 'gnus-summary-mail-other-window "gnus-msg" nil t)
1684   (autoload 'gnus-mail-reply-using-mail "gnus-msg")
1685   (autoload 'gnus-mail-yank-original "gnus-msg")
1686   (autoload 'gnus-mail-send-and-exit "gnus-msg")
1687   (autoload 'gnus-mail-forward-using-mail "gnus-msg")
1688   (autoload 'gnus-mail-other-window-using-mail "gnus-msg")
1689   (autoload 'gnus-article-mail-with-original "gnus-msg")
1690   (autoload 'gnus-article-mail "gnus-msg")
1691   (autoload 'gnus-bug "gnus-msg" nil t)
1692
1693   ;; gnus-vm
1694   (autoload 'gnus-summary-save-in-vm "gnus-vm" nil t)
1695   (autoload 'gnus-summary-save-article-vm "gnus-vm" nil t)
1696   (autoload 'gnus-mail-forward-using-vm "gnus-vm")
1697   (autoload 'gnus-mail-reply-using-vm "gnus-vm")
1698   (autoload 'gnus-mail-other-window-using-vm "gnus-vm" nil t)
1699   (autoload 'gnus-yank-article "gnus-vm" nil t)
1700
1701   )
1702
1703 \f
1704
1705 ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
1706 ;; If you want the cursor to go somewhere else, set these two
1707 ;; functions in some startup hook to whatever you want.
1708 (defalias 'gnus-summary-position-cursor 'gnus-goto-colon)
1709 (defalias 'gnus-group-position-cursor 'gnus-goto-colon)
1710
1711 ;;; Various macros and substs.
1712
1713 (defmacro gnus-eval-in-buffer-window (buffer &rest forms)
1714   "Pop to BUFFER, evaluate FORMS, and then returns to original window."
1715   (` (let ((GnusStartBufferWindow (selected-window)))
1716        (unwind-protect
1717            (progn
1718              (pop-to-buffer (, buffer))
1719              (,@ forms))
1720          (select-window GnusStartBufferWindow)))))
1721
1722 (defmacro gnus-gethash (string hashtable)
1723   "Get hash value of STRING in HASHTABLE."
1724   ;;(` (symbol-value (abbrev-symbol (, string) (, hashtable))))
1725   ;;(` (abbrev-expansion (, string) (, hashtable)))
1726   (` (symbol-value (intern-soft (, string) (, hashtable)))))
1727
1728 (defmacro gnus-sethash (string value hashtable)
1729   "Set hash value. Arguments are STRING, VALUE, and HASHTABLE."
1730   ;; We cannot use define-abbrev since it only accepts string as value.
1731   ;; (set (intern string hashtable) value))
1732   (` (set (intern (, string) (, hashtable)) (, value))))
1733
1734 (defsubst gnus-buffer-substring (beg end)
1735   (buffer-substring (match-beginning beg) (match-end end)))
1736
1737 ;; modified by MORIOKA Tomohiko <morioka@jaist.ac.jp>
1738 ;;   function `substring' might cut on a middle of multi-octet
1739 ;;   character.
1740 (defun gnus-truncate-string (str width)
1741   (substring str 0 width))
1742
1743 ;; Added by Geoffrey T. Dairiki <dairiki@u.washington.edu>. A safe way
1744 ;; to limit the length of a string. This function is necessary since
1745 ;; `(substr "abc" 0 30)' pukes with "Args out of range".
1746 (defsubst gnus-limit-string (str width)
1747   (if (> (length str) width)
1748       (substring str 0 width)
1749     str))
1750
1751 (defsubst gnus-simplify-subject-re (subject)
1752   "Remove \"Re:\" from subject lines."
1753   (let ((case-fold-search t))
1754     (if (string-match "^re: *" subject)
1755         (substring subject (match-end 0))
1756       subject)))
1757
1758 (defsubst gnus-goto-char (point)
1759   (and point (goto-char point)))
1760
1761 (defmacro gnus-buffer-exists-p (buffer)
1762   (` (and (, buffer)
1763           (funcall (if (stringp (, buffer)) 'get-buffer 'buffer-name)
1764                    (, buffer)))))
1765
1766 (defmacro gnus-kill-buffer (buffer)
1767   (` (if (gnus-buffer-exists-p (, buffer))
1768          (kill-buffer (, buffer)))))
1769
1770 (defsubst gnus-point-at-bol ()
1771   "Return point at the beginning of line."
1772   (let ((p (point)))
1773     (beginning-of-line)
1774     (prog1
1775         (point)
1776       (goto-char p))))
1777
1778 (defsubst gnus-point-at-eol ()
1779   "Return point at the beginning of line."
1780   (let ((p (point)))
1781     (end-of-line)
1782     (prog1
1783         (point)
1784       (goto-char p))))
1785
1786 ;; Delete the current line (and the next N lines.);
1787 (defmacro gnus-delete-line (&optional n)
1788   (` (delete-region (progn (beginning-of-line) (point))
1789                     (progn (forward-line (, (or n 1))) (point)))))
1790
1791 ;;; Load the compatability functions. 
1792
1793 (require 'gnus-ems)
1794
1795 \f
1796 ;;;
1797 ;;; Gnus Utility Functions
1798 ;;;
1799
1800 (defun gnus-extract-address-components (from)
1801   (let (name address)
1802     ;; First find the address - the thing with the @ in it.  This may
1803     ;; not be accurate in mail addresses, but does the trick most of
1804     ;; the time in news messages.
1805     (if (string-match "\\b[^@ \t<>]+[!@][^@ \t<>]+\\b" from)
1806         (setq address (substring from (match-beginning 0) (match-end 0))))
1807     ;; Then we check whether the "name <address>" format is used.
1808     (and address
1809          (string-match (concat "<" (regexp-quote address) ">") from)
1810          (and (setq name (substring from 0 (1- (match-beginning 0))))
1811               ;; Strip any quotes from the name.
1812               (string-match "\".*\"" name)
1813               (setq name (substring name 1 (1- (match-end 0))))))
1814     ;; If not, then "address (name)" is used.
1815     (or name
1816         (and (string-match "(.+)" from)
1817              (setq name (substring from (1+ (match-beginning 0)) 
1818                                    (1- (match-end 0)))))
1819         (and (string-match "()" from)
1820              (setq name address))
1821         ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>.
1822         ;; XOVER might not support folded From headers.
1823         (and (string-match "(.*" from)
1824              (setq name (substring from (1+ (match-beginning 0)) 
1825                                    (match-end 0)))))
1826     ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
1827     (list (or name from) (or address from))))
1828
1829 (defun gnus-fetch-field (field)
1830   "Return the value of the header FIELD of current article."
1831   (save-excursion
1832     (save-restriction
1833       (let ((case-fold-search t))
1834         (gnus-narrow-to-headers)
1835         (mail-fetch-field field)))))
1836
1837 (defun gnus-goto-colon ()
1838   (beginning-of-line)
1839   (search-forward ":" (gnus-point-at-eol) t))
1840
1841 (defun gnus-narrow-to-headers ()
1842   (widen)
1843   (save-excursion
1844     (narrow-to-region
1845      (goto-char (point-min))
1846      (if (search-forward "\n\n" nil t)
1847          (1- (point))
1848        (point-max)))))
1849
1850 (defvar gnus-old-specs nil)
1851
1852 (defun gnus-update-format-specifications ()
1853   (gnus-make-thread-indent-array)
1854
1855   (let ((formats '(summary summary-dummy group 
1856                            summary-mode group-mode article-mode))
1857         old-format new-format)
1858     (while formats
1859       (setq new-format (symbol-value
1860                         (intern (format "gnus-%s-line-format" (car formats)))))
1861       (or (and (setq old-format (cdr (assq (car formats) gnus-old-specs)))
1862                (equal old-format new-format))
1863           (set (intern (format "gnus-%s-line-format-spec" (car formats)))
1864                (gnus-parse-format
1865                 new-format
1866                 (symbol-value 
1867                  (intern (format "gnus-%s-line-format-alist"
1868                                  (if (eq (car formats) 'article-mode)
1869                                      'summary-mode (car formats))))))))
1870       (setq gnus-old-specs (cons (cons (car formats) new-format)
1871                                  (delq (car formats) gnus-old-specs)))
1872       (setq formats (cdr formats))))
1873       
1874   (gnus-update-group-mark-positions)
1875   (gnus-update-summary-mark-positions)
1876
1877   (if (and (string-match "%D" gnus-group-line-format)
1878            (not gnus-description-hashtb)
1879            gnus-read-active-file)
1880       (gnus-read-all-descriptions-files)))
1881
1882 (defun gnus-update-summary-mark-positions ()
1883   (save-excursion
1884     (let ((gnus-replied-mark 129)
1885           (gnus-score-below-mark 130)
1886           (gnus-score-over-mark 130)
1887           (thread nil)
1888           pos)
1889       (gnus-set-work-buffer)
1890       (gnus-summary-insert-line 
1891        nil [0 "" "" "" "" "" 0 0 ""]  0 nil 128 t nil "" nil 1)
1892       (goto-char (point-min))
1893       (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
1894                                          (- (point) 2)))))
1895       (goto-char (point-min))
1896       (setq pos (cons (cons 'replied (and (search-forward "\201" nil t)
1897                                           (- (point) 2))) pos))
1898       (goto-char (point-min))
1899       (setq pos (cons (cons 'score (and (search-forward "\202" nil t)
1900                                         (- (point) 2))) pos))
1901       (setq gnus-summary-mark-positions pos))))
1902
1903 (defun gnus-update-group-mark-positions ()
1904   (save-excursion
1905     (let ((gnus-process-mark 128)
1906           (gnus-group-marked '("dummy.group")))
1907       (gnus-set-work-buffer)
1908       (gnus-group-insert-group-line nil "dummy.group" 0 nil 0 nil)
1909       (goto-char (point-min))
1910       (setq gnus-group-mark-positions
1911             (list (cons 'process (and (search-forward "\200" nil t)
1912                                       (- (point) 2))))))))
1913
1914 (defun gnus-mouse-face-function (form)
1915   (` (let ((string (, form)))
1916        (put-text-property 0 (length string) 'mouse-face gnus-mouse-face string)
1917        string)))
1918
1919 (defun gnus-max-width-function (el max-width)
1920   (or (numberp max-width) (signal 'wrong-type-argument '(numberp max-width)))
1921   (` (let* ((val (eval (, el)))
1922             (valstr (if (numberp val)
1923                         (int-to-string val) val)))
1924        (if (> (length valstr) (, max-width))
1925            (substring valstr 0 (, max-width))
1926          valstr))))
1927
1928 (defun gnus-parse-format (format spec-alist)
1929   ;; This function parses the FORMAT string with the help of the
1930   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
1931   ;; string.  If the FORMAT string contains the specifiers %( and %)
1932   ;; the text between them will have the mouse-face text property.
1933   (if (string-match "\\`\\(.*\\)%(\\(.*\\)%)\\(.*\n?\\)\\'" format)
1934       (if (and gnus-visual gnus-mouse-face)
1935           (let ((pre (substring format (match-beginning 1) (match-end 1)))
1936                 (button (substring format (match-beginning 2) (match-end 2)))
1937                 (post (substring format (match-beginning 3) (match-end 3))))
1938             (list 'concat
1939                   (gnus-parse-simple-format pre spec-alist)
1940                   (gnus-mouse-face-function 
1941                    (gnus-parse-simple-format button spec-alist))
1942                   (gnus-parse-simple-format post spec-alist)))
1943         (gnus-parse-simple-format
1944          (concat (substring format (match-beginning 1) (match-end 1))
1945                  (substring format (match-beginning 2) (match-end 2))
1946                  (substring format (match-beginning 3) (match-end 3)))
1947          spec-alist))
1948     (gnus-parse-simple-format format spec-alist)))
1949
1950 (defun gnus-parse-simple-format (format spec-alist)
1951   ;; This function parses the FORMAT string with the help of the
1952   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
1953   ;; string. The list will consist of the symbol `format', a format
1954   ;; specification string, and a list of forms depending on the
1955   ;; SPEC-ALIST.
1956   (let ((max-width 0)
1957         spec flist fstring newspec elem beg)
1958     (save-excursion
1959       (gnus-set-work-buffer)
1960       (insert format)
1961       (goto-char (point-min))
1962       (while (re-search-forward "%[-0-9]*\\(,[0-9]*\\)*\\(.\\)\\(.\\)?" nil t)
1963         (setq spec (string-to-char (buffer-substring (match-beginning 2)
1964                                                      (match-end 2))))
1965         ;; First check if there are any specs that look anything like
1966         ;; "%12,12A", ie. with a "max width specification". These have
1967         ;; to be treated specially.
1968         (if (setq beg (match-beginning 1))
1969             (setq max-width 
1970                   (string-to-int 
1971                    (buffer-substring (1+ (match-beginning 1)) (match-end 1))))
1972           (setq max-width 0)
1973           (setq beg (match-beginning 2)))
1974         ;; Find the specification from `spec-alist'.
1975         (if (not (setq elem (cdr (assq spec spec-alist))))
1976             (setq elem '("*" ?s)))
1977         ;; Treat user defined format specifiers specially
1978         (and (eq (car elem) 'user-defined)
1979              (setq elem
1980                    (list 
1981                     (list (intern (concat "gnus-user-format-function-"
1982                                           (buffer-substring
1983                                            (match-beginning 3)
1984                                            (match-end 3))))
1985                           'header)
1986                     ?s))
1987              (delete-region (match-beginning 3) (match-end 3)))
1988         (if (not (zerop max-width))
1989             (let ((el (car elem)))
1990               (cond ((= (car (cdr elem)) ?c) 
1991                      (setq el (list 'char-to-string el)))
1992                     ((= (car (cdr elem)) ?d)
1993                      (numberp el) (setq el (list 'int-to-string el))))
1994               (setq flist (cons (gnus-max-width-function el max-width)
1995                                 flist))
1996               (setq newspec ?s))
1997           (setq flist (cons (car elem) flist))
1998           (setq newspec (car (cdr elem))))
1999         ;; Remove the old specification (and possibly a ",12" string).
2000         (delete-region beg (match-end 2))
2001         ;; Insert the new specification.
2002         (goto-char beg)
2003         (insert newspec))
2004       (setq fstring (buffer-substring 1 (point-max))))
2005     (cons 'format (cons fstring (nreverse flist)))))
2006
2007 ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
2008 (defun gnus-read-init-file ()
2009   (and gnus-init-file
2010        (or (and (file-exists-p gnus-init-file) 
2011                 ;; Don't try to load a directory.
2012                 (not (file-directory-p gnus-init-file)))
2013            (file-exists-p (concat gnus-init-file ".el"))
2014            (file-exists-p (concat gnus-init-file ".elc")))
2015        (load gnus-init-file nil t)))
2016
2017 (defun gnus-set-work-buffer ()
2018   (if (get-buffer gnus-work-buffer)
2019       (progn
2020         (set-buffer gnus-work-buffer)
2021         (erase-buffer))
2022     (set-buffer (get-buffer-create gnus-work-buffer))
2023     (kill-all-local-variables)
2024     (buffer-disable-undo (current-buffer))
2025     (gnus-add-current-to-buffer-list)))
2026
2027 ;; Article file names when saving.
2028
2029 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
2030   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2031 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group/num.
2032 Otherwise, it is like ~/News/news/group/num."
2033   (let ((default
2034           (expand-file-name
2035            (concat (if (gnus-use-long-file-name 'not-save)
2036                        (gnus-capitalize-newsgroup newsgroup)
2037                      (gnus-newsgroup-directory-form newsgroup))
2038                    "/" (int-to-string (header-number headers)))
2039            (or gnus-article-save-directory "~/News"))))
2040     (if (and last-file
2041              (string-equal (file-name-directory default)
2042                            (file-name-directory last-file))
2043              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2044         default
2045       (or last-file default))))
2046
2047 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
2048   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2049 If variable `gnus-use-long-file-name' is nil, it is ~/News/news.group/num.
2050 Otherwise, it is like ~/News/news/group/num."
2051   (let ((default
2052           (expand-file-name
2053            (concat (if (gnus-use-long-file-name 'not-save)
2054                        newsgroup
2055                      (gnus-newsgroup-directory-form newsgroup))
2056                    "/" (int-to-string (header-number headers)))
2057            (or gnus-article-save-directory "~/News"))))
2058     (if (and last-file
2059              (string-equal (file-name-directory default)
2060                            (file-name-directory last-file))
2061              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2062         default
2063       (or last-file default))))
2064
2065 (defun gnus-Plain-save-name (newsgroup headers &optional last-file)
2066   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2067 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group.
2068 Otherwise, it is like ~/News/news/group/news."
2069   (or last-file
2070       (expand-file-name
2071        (if (gnus-use-long-file-name 'not-save)
2072            (gnus-capitalize-newsgroup newsgroup)
2073          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2074        (or gnus-article-save-directory "~/News"))))
2075
2076 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
2077   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2078 If variable `gnus-use-long-file-name' is nil, it is ~/News/news.group.
2079 Otherwise, it is like ~/News/news/group/news."
2080   (or last-file
2081       (expand-file-name
2082        (if (gnus-use-long-file-name 'not-save)
2083            newsgroup
2084          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2085        (or gnus-article-save-directory "~/News"))))
2086
2087 ;; For subscribing new newsgroup
2088
2089 (defun gnus-subscribe-hierarchical-interactive (groups)
2090   (let ((groups (sort groups 'string<))
2091         prefixes prefix start ans group starts)
2092     (while groups
2093       (setq prefixes (list "^"))
2094       (while (and groups prefixes)
2095         (while (not (string-match (car prefixes) (car groups)))
2096           (setq prefixes (cdr prefixes)))
2097         (setq prefix (car prefixes))
2098         (setq start (1- (length prefix)))
2099         (if (and (string-match "[^\\.]\\." (car groups) start)
2100                  (cdr groups)
2101                  (setq prefix 
2102                        (concat "^" (substring (car groups) 0 (match-end 0))))
2103                  (string-match prefix (car (cdr groups))))
2104             (progn
2105               (setq prefixes (cons prefix prefixes))
2106               (message "Descend hierarchy %s? ([y]nsq): " 
2107                        (substring prefix 1 (1- (length prefix))))
2108               (setq ans (read-char))
2109               (cond ((= ans ?n)
2110                      (while (and groups 
2111                                  (string-match prefix 
2112                                                (setq group (car groups))))
2113                        (setq gnus-killed-list 
2114                              (cons group gnus-killed-list))
2115                        (gnus-sethash group group gnus-killed-hashtb)
2116                        (setq groups (cdr groups)))
2117                      (setq starts (cdr starts)))
2118                     ((= ans ?s)
2119                      (while (and groups 
2120                                  (string-match prefix 
2121                                                (setq group (car groups))))
2122                        (gnus-sethash group group gnus-killed-hashtb)
2123                        (gnus-subscribe-alphabetically (car groups))
2124                        (setq groups (cdr groups)))
2125                      (setq starts (cdr starts)))
2126                     ((= ans ?q)
2127                      (while groups
2128                        (setq group (car groups))
2129                        (setq gnus-killed-list (cons group gnus-killed-list))
2130                        (gnus-sethash group group gnus-killed-hashtb)
2131                        (setq groups (cdr groups))))
2132                     (t nil)))
2133           (message "Subscribe %s? ([n]yq)" (car groups))
2134           (setq ans (read-char))
2135           (setq group (car groups))
2136           (cond ((= ans ?y)
2137                  (gnus-subscribe-alphabetically (car groups))
2138                  (gnus-sethash group group gnus-killed-hashtb))
2139                 ((= ans ?q)
2140                  (while groups
2141                    (setq group (car groups))
2142                    (setq gnus-killed-list (cons group gnus-killed-list))
2143                    (gnus-sethash group group gnus-killed-hashtb)
2144                    (setq groups (cdr groups))))
2145                 (t 
2146                  (setq gnus-killed-list (cons group gnus-killed-list))
2147                  (gnus-sethash group group gnus-killed-hashtb)))
2148           (setq groups (cdr groups)))))))
2149
2150 (defun gnus-subscribe-randomly (newsgroup)
2151   "Subscribe new NEWSGROUP by making it the first newsgroup."
2152   (gnus-subscribe-newsgroup newsgroup))
2153
2154 (defun gnus-subscribe-alphabetically (newgroup)
2155   "Subscribe new NEWSGROUP and insert it in alphabetical order."
2156   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2157   (let ((groups (cdr gnus-newsrc-alist))
2158         before)
2159     (while (and (not before) groups)
2160       (if (string< newgroup (car (car groups)))
2161           (setq before (car (car groups)))
2162         (setq groups (cdr groups))))
2163     (gnus-subscribe-newsgroup newgroup before)))
2164
2165 (defun gnus-subscribe-hierarchically (newgroup)
2166   "Subscribe new NEWSGROUP and insert it in hierarchical newsgroup order."
2167   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2168   (save-excursion
2169     (set-buffer (find-file-noselect gnus-current-startup-file))
2170     (let ((groupkey newgroup)
2171           before)
2172       (while (and (not before) groupkey)
2173         (goto-char (point-min))
2174         (let ((groupkey-re
2175                (concat "^\\(" (regexp-quote groupkey) ".*\\)[!:]")))
2176           (while (and (re-search-forward groupkey-re nil t)
2177                       (progn
2178                         (setq before (buffer-substring
2179                                       (match-beginning 1) (match-end 1)))
2180                         (string< before newgroup)))))
2181         ;; Remove tail of newsgroup name (eg. a.b.c -> a.b)
2182         (setq groupkey
2183               (if (string-match "^\\(.*\\)\\.[^.]+$" groupkey)
2184                   (substring groupkey (match-beginning 1) (match-end 1)))))
2185       (gnus-subscribe-newsgroup newgroup before))))
2186
2187 (defun gnus-subscribe-interactively (newsgroup)
2188   "Subscribe new NEWSGROUP interactively.
2189 It is inserted in hierarchical newsgroup order if subscribed. If not,
2190 it is killed."
2191   (if (gnus-y-or-n-p (format "Subscribe new newsgroup: %s " newsgroup))
2192       (gnus-subscribe-hierarchically newsgroup)
2193     (setq gnus-killed-list (cons newsgroup gnus-killed-list))))
2194
2195 (defun gnus-subscribe-zombies (newsgroup)
2196   "Make new NEWSGROUP a zombie group."
2197   (setq gnus-zombie-list (cons newsgroup gnus-zombie-list)))
2198
2199 (defun gnus-subscribe-newsgroup (newsgroup &optional next)
2200   "Subscribe new NEWSGROUP.
2201 If NEXT is non-nil, it is inserted before NEXT. Otherwise it is made
2202 the first newsgroup."
2203   ;; We subscribe the group by changing its level to `subscribed'.
2204   (gnus-group-change-level 
2205    newsgroup gnus-level-default-subscribed
2206    gnus-level-killed (gnus-gethash (or next "dummy.group") gnus-newsrc-hashtb))
2207   (gnus-message 5 "Subscribe newsgroup: %s" newsgroup))
2208
2209 ;; For directories
2210
2211 (defun gnus-newsgroup-directory-form (newsgroup)
2212   "Make hierarchical directory name from NEWSGROUP name."
2213   (let ((newsgroup (gnus-newsgroup-saveable-name newsgroup))
2214         (len (length newsgroup))
2215         idx)
2216     ;; If this is a foreign group, we don't want to translate the
2217     ;; entire name.  
2218     (if (setq idx (string-match ":" newsgroup))
2219         (aset newsgroup idx ?/)
2220       (setq idx 0))
2221     ;; Replace all occurrences of `.' with `/'.
2222     (while (< idx len)
2223       (if (= (aref newsgroup idx) ?.)
2224           (aset newsgroup idx ?/))
2225       (setq idx (1+ idx)))
2226     newsgroup))
2227
2228 (defun gnus-newsgroup-saveable-name (group)
2229   ;; Replace any slashes in a group name (eg. an ange-ftp nndoc group)
2230   ;; with dots.
2231   (gnus-replace-chars-in-string group ?/ ?.))
2232
2233 (defun gnus-make-directory (dir)
2234   "Make DIRECTORY recursively."
2235   ;; Why don't we use `(make-directory dir 'parents)'? That's just one
2236   ;; of the many mysteries of the universe.
2237  (let* ((dir (expand-file-name dir default-directory))
2238          dirs err)
2239     (if (string-match "/$" dir)
2240         (setq dir (substring dir 0 (match-beginning 0))))
2241     ;; First go down the path until we find a directory that exists.
2242     (while (not (file-exists-p dir))
2243       (setq dirs (cons dir dirs))
2244       (string-match "/[^/]+$" dir)
2245       (setq dir (substring dir 0 (match-beginning 0))))
2246     ;; Then create all the subdirs.
2247     (while (and dirs (not err))
2248       (condition-case ()
2249           (make-directory (car dirs))
2250         (error (setq err t)))
2251       (setq dirs (cdr dirs)))
2252     ;; We return whether we were successful or not. 
2253     (not dirs)))
2254
2255 (defun gnus-capitalize-newsgroup (newsgroup)
2256   "Capitalize NEWSGROUP name."
2257   (and (not (zerop (length newsgroup)))
2258        (concat (char-to-string (upcase (aref newsgroup 0)))
2259                (substring newsgroup 1))))
2260
2261 ;; Var
2262
2263 (defun gnus-simplify-subject (subject &optional re-only)
2264   "Remove `Re:' and words in parentheses.
2265 If optional argument RE-ONLY is non-nil, strip `Re:' only."
2266   (let ((case-fold-search t))           ;Ignore case.
2267     ;; Remove `Re:' and `Re^N:'.
2268     (if (string-match "^re:[ \t]*" subject)
2269         (setq subject (substring subject (match-end 0))))
2270     ;; Remove words in parentheses from end.
2271     (or re-only
2272         (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
2273           (setq subject (substring subject 0 (match-beginning 0)))))
2274     ;; Return subject string.
2275     subject))
2276
2277 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
2278 ;; all whitespace.
2279 (defun gnus-simplify-subject-fuzzy (subject)
2280   (let ((case-fold-search t))
2281     (save-excursion
2282       (gnus-set-work-buffer)
2283       (insert subject)
2284       (inline (gnus-simplify-buffer-fuzzy))
2285       (buffer-string))))
2286
2287 (defun gnus-simplify-buffer-fuzzy ()
2288   (goto-char (point-min))
2289   ;; Fix by Stainless Steel Rat <ratinox@ccs.neu.edu>.
2290   (while (re-search-forward "^[ \t]*\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;][ \t]*"
2291                             nil t)
2292     (replace-match "" t t))
2293   (goto-char (point-min))
2294   (while (re-search-forward "[ \t\n]*([^()]*)[ \t\n]*$" nil t)
2295     (replace-match "" t t))
2296   (goto-char (point-min))
2297   (while (re-search-forward "[ \t]+" nil t)
2298     (replace-match " " t t))
2299   (goto-char (point-min))
2300   (while (re-search-forward "[ \t]+$" nil t)
2301     (replace-match "" t t))
2302   (goto-char (point-min))
2303   (while (re-search-forward "^[ \t]+" nil t)
2304     (replace-match "" t t))
2305   (if gnus-simplify-subject-fuzzy-regexp
2306       (while (re-search-forward gnus-simplify-subject-fuzzy-regexp nil t)
2307         (replace-match "" t t))))
2308
2309 ;; Add the current buffer to the list of buffers to be killed on exit. 
2310 (defun gnus-add-current-to-buffer-list ()
2311   (or (memq (current-buffer) gnus-buffer-list)
2312       (setq gnus-buffer-list (cons (current-buffer) gnus-buffer-list))))
2313
2314 (defun gnus-string> (s1 s2)
2315   (not (or (string< s1 s2)
2316            (string= s1 s2))))
2317
2318 ;; Functions accessing headers.
2319 ;; Functions are more convenient than macros in some cases.
2320
2321 (defun gnus-header-number (header)
2322   (header-number header))
2323
2324 (defun gnus-header-subject (header)
2325   (header-subject header))
2326
2327 (defun gnus-header-from (header)
2328   (header-from header))
2329
2330 (defun gnus-header-xref (header)
2331   (header-xref header))
2332
2333 (defun gnus-header-lines (header)
2334   (header-lines header))
2335
2336 (defun gnus-header-date (header)
2337   (header-date header))
2338
2339 (defun gnus-header-id (header)
2340   (header-id header))
2341
2342 (defun gnus-header-references (header)
2343   (header-references header))
2344
2345 ;;; General various misc type functions.
2346
2347 (defun gnus-clear-system ()
2348   "Clear all variables and buffers."
2349   ;; Clear Gnus variables.
2350   (let ((variables gnus-variable-list))
2351     (while variables
2352       (set (car variables) nil)
2353       (setq variables (cdr variables))))
2354   ;; Clear other internal variables.
2355   (setq gnus-list-of-killed-groups nil
2356         gnus-have-read-active-file nil
2357         gnus-newsrc-alist nil
2358         gnus-newsrc-hashtb nil
2359         gnus-killed-list nil
2360         gnus-zombie-list nil
2361         gnus-killed-hashtb nil
2362         gnus-active-hashtb nil
2363         gnus-moderated-list nil
2364         gnus-description-hashtb nil
2365         gnus-newsgroup-headers nil
2366         gnus-newsgroup-headers-hashtb-by-number nil
2367         gnus-newsgroup-name nil
2368         gnus-server-alist nil
2369         gnus-current-select-method nil)
2370   ;; Reset any score variables.
2371   (and (boundp 'gnus-score-cache)
2372        (set 'gnus-score-cache nil))
2373   (and (boundp 'gnus-internal-global-score-files)
2374        (set 'gnus-internal-global-score-files nil))
2375   ;; Kill the startup file.
2376   (and gnus-current-startup-file
2377        (get-file-buffer gnus-current-startup-file)
2378        (kill-buffer (get-file-buffer gnus-current-startup-file)))
2379   ;; Save any cache buffers.
2380   (and gnus-use-cache (gnus-cache-save-buffers))
2381   ;; Clear the dribble buffer.
2382   (gnus-dribble-clear)
2383   ;; Kill global KILL file buffer.
2384   (if (get-file-buffer (gnus-newsgroup-kill-file nil))
2385       (kill-buffer (get-file-buffer (gnus-newsgroup-kill-file nil))))
2386   (gnus-kill-buffer nntp-server-buffer)
2387   ;; Kill Gnus buffers.
2388   (while gnus-buffer-list
2389     (gnus-kill-buffer (car gnus-buffer-list))
2390     (setq gnus-buffer-list (cdr gnus-buffer-list))))
2391
2392 (defun gnus-windows-old-to-new (setting)
2393   (if (symbolp setting)
2394       (setq setting 
2395             (cond ((eq setting 'SelectArticle)
2396                    'article)
2397                   ((eq setting 'SelectSubject)
2398                    'summary)
2399                   ((eq setting 'SelectNewsgroup)
2400                    'group)
2401                   (t setting))))
2402   (if (or (listp setting)
2403           (not (and gnus-window-configuration
2404                     (memq setting '(group summary article)))))
2405       setting
2406     (let* ((setting (if (eq setting 'group) 
2407                         (if (assq 'newsgroup gnus-window-configuration)
2408                             'newsgroup
2409                           'newsgroups) setting))
2410            (elem (car (cdr (assq setting gnus-window-configuration))))
2411            (total (apply '+ elem))
2412            (types '(group summary article))
2413            (pbuf (if (eq setting 'newsgroups) 'group 'summary))
2414            (i 0)
2415            perc
2416            out)
2417       (while (< i 3)
2418         (or (zerop (nth i elem))
2419             (progn
2420               (setq perc  (/ (* 1.0 (nth 0 elem)) total))
2421               (setq out (cons (if (eq pbuf (nth i types))
2422                                   (vector (nth i types) perc 'point)
2423                                 (vector (nth i types) perc))
2424                               out))))
2425         (setq i (1+ i)))
2426       (list (nreverse out)))))
2427            
2428 (defun gnus-add-configuration (conf)
2429   (setq gnus-buffer-configuration 
2430         (cons conf (delq (assq (car conf) gnus-buffer-configuration)
2431                          gnus-buffer-configuration))))
2432
2433 (defun gnus-configure-windows (setting &optional force)
2434   (setq setting (gnus-windows-old-to-new setting))
2435   (let ((r (if (symbolp setting)
2436                (cdr (assq setting gnus-buffer-configuration))
2437              setting))
2438         (in-buf (current-buffer))
2439         rule val w height hor ohor heights sub jump-buffer
2440         rel total to-buf all-visible)
2441     (or r (error "No such setting: %s" setting))
2442
2443     (if (and (not force) (setq all-visible (gnus-all-windows-visible-p r)))
2444         ;; All the windows mentioned are already visibe, so we just
2445         ;; put point in the assigned buffer, and do not touch the
2446         ;; winconf. 
2447         (select-window (get-buffer-window all-visible))
2448
2449       ;; Either remove all windows or just remove all Gnus windows.
2450       (if gnus-use-full-window
2451           (delete-other-windows)
2452         (gnus-remove-some-windows)
2453         (switch-to-buffer nntp-server-buffer))
2454
2455       (while r
2456         (setq hor (car r)
2457               ohor nil)
2458
2459         ;; We have to do the (possible) horizontal splitting before the
2460         ;; vertical. 
2461         (if (and (listp (car hor)) 
2462                  (eq (car (car hor)) 'horizontal))
2463             (progn
2464               (split-window 
2465                nil
2466                (if (integerp (nth 1 (car hor)))
2467                    (nth 1 (car hor))
2468                  (- (frame-width) (floor (* (frame-width) (nth 1 (car hor))))))
2469                t)
2470               (setq hor (cdr hor))))
2471
2472         ;; Go through the rules and eval the elements that are to be
2473         ;; evaled.  
2474         (while hor
2475           (if (setq val (if (vectorp (car hor)) (car hor) (eval (car hor))))
2476               (progn
2477                 ;; Expand short buffer name.
2478                 (setq w (aref val 0))
2479                 (and (setq w (cdr (assq w gnus-window-to-buffer)))
2480                      (progn
2481                        (setq val (apply 'vector (mapcar 'identity val)))
2482                        (aset val 0 w)))
2483                 (setq ohor (cons val ohor))))
2484           (setq hor (cdr hor)))
2485         (setq rule (cons (nreverse ohor) rule))
2486         (setq r (cdr r)))
2487       (setq rule (nreverse rule))
2488
2489       ;; We tally the window sizes.
2490       (setq total (window-height))
2491       (while rule
2492         (setq hor (car rule))
2493         (if (and (listp (car hor)) (eq (car (car hor)) 'horizontal))
2494             (setq hor (cdr hor)))
2495         (setq sub 0)
2496         (while hor
2497           (setq rel (aref (car hor) 1)
2498                 heights (cons
2499                          (cond ((and (floatp rel) (= 1.0 rel))
2500                                 'x)
2501                                ((integerp rel)
2502                                 rel)
2503                                (t
2504                                 (max (floor (* total rel)) 4)))
2505                          heights)
2506                 sub (+ sub (if (numberp (car heights)) (car heights) 0))
2507                 hor (cdr hor)))
2508         (setq heights (nreverse heights)
2509               hor (car rule))
2510
2511         ;; We then go through these heighs and create windows for them.
2512         (while heights
2513           (setq height (car heights)
2514                 heights (cdr heights))
2515           (and (eq height 'x)
2516                (setq height (- total sub)))
2517           (and heights
2518                (split-window nil height))
2519           (setq to-buf (aref (car hor) 0))
2520           (switch-to-buffer 
2521            (cond ((not to-buf)
2522                   in-buf)
2523                  ((symbolp to-buf)
2524                   (symbol-value (aref (car hor) 0)))
2525                  (t
2526                   (aref (car hor) 0))))
2527           (and (> (length (car hor)) 2)
2528                (eq (aref (car hor) 2) 'point)
2529                (setq jump-buffer (current-buffer)))
2530           (other-window 1)
2531           (setq hor (cdr hor)))
2532       
2533         (setq rule (cdr rule)))
2534
2535       ;; Finally, we pop to the buffer that's supposed to have point. 
2536       (or jump-buffer (error "Missing `point' in spec for %s" setting))
2537
2538       (select-window (get-buffer-window jump-buffer))
2539       (set-buffer jump-buffer))))
2540
2541 (defun gnus-all-windows-visible-p (rule)
2542   (let (invisible hor jump-buffer val buffer)
2543     ;; Go through the rules and eval the elements that are to be
2544     ;; evaled.  
2545     (while (and rule (not invisible))
2546       (setq hor (car rule)
2547             rule (cdr rule))
2548       (while (and hor (not invisible))
2549         (if (setq val (if (vectorp (car hor)) 
2550                           (car hor)
2551                         (if (not (eq (car (car hor)) 'horizontal))
2552                             (eval (car hor)))))
2553             (progn
2554               ;; Expand short buffer name.
2555               (setq buffer (or (cdr (assq (aref val 0) gnus-window-to-buffer))
2556                                (aref val 0)))
2557               (setq buffer (if (symbolp buffer) (symbol-value buffer)
2558                              buffer))
2559               (and (> (length val) 2) (eq 'point (aref val 2))
2560                    (setq jump-buffer buffer))
2561               (setq invisible (not (and buffer (get-buffer-window buffer))))))
2562         (setq hor (cdr hor))))
2563     (and (not invisible) jump-buffer)))
2564
2565 (defun gnus-window-left-corner (&optional window)
2566   (nth 1 (window-edges window)))
2567
2568 (defun gnus-remove-some-windows ()
2569   (let ((buffers gnus-window-to-buffer)
2570         (lowest (frame-height))
2571         buf bufs lowest-buf)
2572     (save-excursion
2573       ;; Remove windows on all known Gnus buffers.
2574       (while buffers
2575         (setq buf (cdr (car buffers)))
2576         (if (symbolp buf)
2577             (setq buf (and (boundp buf) (symbol-value buf))))
2578         (and buf 
2579              (get-buffer-window buf)
2580              (progn
2581                (setq bufs (cons buf bufs))
2582                (pop-to-buffer buf)
2583                (if (< (gnus-window-left-corner) lowest)
2584                    (progn
2585                      (setq lowest (gnus-window-left-corner))
2586                      (setq lowest-buf buf)))))
2587         (setq buffers (cdr buffers)))
2588       ;; Remove windows on *all* summary buffers.
2589       (let (wins)
2590         (walk-windows
2591          (lambda (win)
2592            (let ((buf (window-buffer win)))
2593              (if (string-match  "^\\*Summary" (buffer-name buf))
2594              (progn
2595                (setq bufs (cons buf bufs))
2596                (pop-to-buffer buf)
2597                (if (< (gnus-window-left-corner) lowest)
2598                    (progn
2599                      (setq lowest-buf buf)
2600                      (setq lowest (gnus-window-left-corner))))))))))
2601       (and lowest-buf 
2602            (progn
2603              (pop-to-buffer lowest-buf)
2604              (switch-to-buffer nntp-server-buffer)))
2605       (while bufs
2606         (and (not (eq (car bufs) lowest-buf))
2607              (delete-windows-on (car bufs)))
2608         (setq bufs (cdr bufs))))))
2609                           
2610 (defun gnus-version ()
2611   "Version numbers of this version of Gnus."
2612   (interactive)
2613   (let ((methods gnus-valid-select-methods)
2614         (mess gnus-version)
2615         meth)
2616     ;; Go through all the legal select methods and add their version
2617     ;; numbers to the total version string. Only the backends that are
2618     ;; currently in use will have their message numbers taken into
2619     ;; consideration. 
2620     (while methods
2621       (setq meth (intern (concat (car (car methods)) "-version")))
2622       (and (boundp meth)
2623            (stringp (symbol-value meth))
2624            (setq mess (concat mess "; " (symbol-value meth))))
2625       (setq methods (cdr methods)))
2626     (gnus-message 2 mess)))
2627
2628 (defun gnus-info-find-node ()
2629   "Find Info documentation of Gnus."
2630   (interactive)
2631   ;; Enlarge info window if needed.
2632   (let ((mode major-mode))
2633     (gnus-configure-windows 'info)
2634     (Info-goto-node (car (cdr (assq mode gnus-info-nodes))))))
2635
2636 (defun gnus-overload-functions (&optional overloads)
2637   "Overload functions specified by optional argument OVERLOADS.
2638 If nothing is specified, use the variable gnus-overload-functions."
2639   (let ((defs nil)
2640         (overloads (or overloads gnus-overload-functions)))
2641     (while overloads
2642       (setq defs (car overloads))
2643       (setq overloads (cdr overloads))
2644       ;; Load file before overloading function if necessary.  Make
2645       ;; sure we cannot use `require' always.
2646       (and (not (fboundp (car defs)))
2647            (car (cdr (cdr defs)))
2648            (load (car (cdr (cdr defs))) nil 'nomessage))
2649       (fset (car defs) (car (cdr defs))))))
2650
2651 (defun gnus-replace-chars-in-string (string from to)
2652   "Replace characters in STRING from FROM to TO."
2653   (let ((string (substring string 0))   ;Copy string.
2654         (len (length string))
2655         (idx 0))
2656     ;; Replace all occurrences of FROM with TO.
2657     (while (< idx len)
2658       (if (= (aref string idx) from)
2659           (aset string idx to))
2660       (setq idx (1+ idx)))
2661     string))
2662
2663 (defun gnus-days-between (date1 date2)
2664   ;; Return the number of days between date1 and date2.
2665   (- (gnus-day-number date1) (gnus-day-number date2)))
2666
2667 (defun gnus-day-number (date)
2668   (let ((dat (mapcar (lambda (s) (and s (string-to-int s)) )
2669                      (timezone-parse-date date))))
2670     (timezone-absolute-from-gregorian 
2671      (nth 1 dat) (nth 2 dat) (car dat))))
2672
2673 ;; Returns a floating point number that says how many seconds have
2674 ;; lapsed between Jan 1 12:00:00 1970 and DATE.
2675 (defun gnus-seconds-since-epoch (date)
2676   (let* ((tdate (mapcar (lambda (ti) (and ti (string-to-int ti)))
2677                         (timezone-parse-date date)))
2678          (ttime (mapcar (lambda (ti) (and ti (string-to-int ti)))
2679                         (timezone-parse-time
2680                          (aref (timezone-parse-date date) 3))))
2681          (edate (mapcar (lambda (ti) (and ti (string-to-int ti)))
2682                         (timezone-parse-date "Jan 1 12:00:00 1970")))
2683          (tday (- (timezone-absolute-from-gregorian 
2684                    (nth 1 tdate) (nth 2 tdate) (nth 0 tdate))
2685                   (timezone-absolute-from-gregorian 
2686                    (nth 1 edate) (nth 2 edate) (nth 0 edate)))))
2687     (+ (nth 2 ttime)
2688        (* (nth 1 ttime) 60)
2689        (* 1.0 (nth 0 ttime) 60 60)
2690        (* 1.0 tday 60 60 24))))
2691
2692 (defun gnus-file-newer-than (file date)
2693   (let ((fdate (nth 5 (file-attributes file))))
2694     (or (> (car fdate) (car date))
2695         (and (= (car fdate) (car date))
2696              (> (nth 1 fdate) (nth 1 date))))))
2697
2698 ;; Two silly functions to ensure that all `y-or-n-p' questions clear
2699 ;; the echo area.
2700 (defun gnus-y-or-n-p (prompt)
2701   (prog1
2702       (y-or-n-p prompt)
2703     (message "")))
2704
2705 (defun gnus-yes-or-no-p (prompt)
2706   (prog1
2707       (yes-or-no-p prompt)
2708     (message "")))
2709
2710 ;; Check whether to use long file names.
2711 (defun gnus-use-long-file-name (symbol)
2712   ;; The variable has to be set...
2713   (and gnus-use-long-file-name
2714        ;; If it isn't a list, then we return t.
2715        (or (not (listp gnus-use-long-file-name))
2716            ;; If it is a list, and the list contains `symbol', we
2717            ;; return nil.  
2718            (not (memq symbol gnus-use-long-file-name)))))
2719
2720 ;; I suspect there's a better way, but I haven't taken the time to do
2721 ;; it yet. -erik selberg@cs.washington.edu
2722 (defun gnus-dd-mmm (messy-date)
2723   "Return a string like DD-MMM from a big messy string"
2724   (let ((datevec (timezone-parse-date messy-date)))
2725     (format "%2s-%s"
2726             (or (aref datevec 2) "??")
2727             (capitalize
2728              (or (car 
2729                   (nth (1- (string-to-number (aref datevec 1)))
2730                        timezone-months-assoc))
2731                  "???")))))
2732
2733 ;; Make a hash table (default and minimum size is 255).
2734 ;; Optional argument HASHSIZE specifies the table size.
2735 (defun gnus-make-hashtable (&optional hashsize)
2736   (make-vector (if hashsize (max (gnus-create-hash-size hashsize) 255) 255) 0))
2737
2738 ;; Make a number that is suitable for hashing; bigger than MIN and one
2739 ;; less than 2^x.
2740 (defun gnus-create-hash-size (min)
2741   (let ((i 1))
2742     (while (< i min)
2743       (setq i (* 2 i)))
2744     (1- i)))
2745
2746 ;; Show message if message has a lower level than `gnus-verbose'. 
2747 ;; Guide-line for numbers:
2748 ;; 1 - error messages, 3 - non-serious error messages, 5 - messages
2749 ;; for things that take a long time, 7 - not very important messages
2750 ;; on stuff, 9 - messages inside loops.
2751 (defun gnus-message (level &rest args)
2752   (if (<= level gnus-verbose)
2753       (apply 'message args)
2754     ;; We have to do this format thingie here even if the result isn't
2755     ;; shown - the return value has to be the same as the return value
2756     ;; from `message'.
2757     (apply 'format args)))
2758
2759 ;; Generate a unique new group name.
2760 (defun gnus-generate-new-group-name (leaf)
2761   (let ((name leaf)
2762         (num 0))
2763     (while (gnus-gethash name gnus-newsrc-hashtb)
2764       (setq name (concat leaf "<" (int-to-string (setq num (1+ num))) ">")))
2765     name))
2766
2767 (defun gnus-find-file-noselect (file &optional force)
2768   "Does vaguely the same as find-file-noselect. No hooks are run."
2769   (let (buf insert)
2770     (if (setq buf (get-file-buffer file))
2771         (setq insert force)
2772       (setq buf (create-file-buffer file))
2773       (setq insert t))
2774     (if (not insert)
2775         buf
2776       (save-excursion
2777         (set-buffer buf)
2778         (erase-buffer)
2779         (and (file-readable-p file)
2780              (insert-file-contents file))
2781         (set-visited-file-name file)
2782         (set-buffer-modified-p nil)
2783         (current-buffer)))))
2784
2785 ;;; List and range functions
2786
2787 (defun gnus-last-element (list)
2788   "Return last element of LIST."
2789   (while (cdr list)
2790     (setq list (cdr list)))
2791   (car list))
2792
2793 (defun gnus-copy-sequence (list)
2794   "Do a complete, total copy of a list."
2795   (if (and (consp list) (not (consp (cdr list))))
2796       (cons (car list) (cdr list))
2797     (mapcar (lambda (elem) (if (consp elem) 
2798                                (if (consp (cdr elem))
2799                                    (gnus-copy-sequence elem)
2800                                  (cons (car elem) (cdr elem)))
2801                              elem))
2802             list)))
2803
2804 (defun gnus-set-difference (list1 list2)
2805   "Return a list of elements of LIST1 that do not appear in LIST2."
2806   (let ((list1 (copy-sequence list1)))
2807     (while list2
2808       (setq list1 (delq (car list2) list1))
2809       (setq list2 (cdr list2)))
2810     list1))
2811
2812 (defun gnus-sorted-complement (list1 list2)
2813   "Return a list of elements of LIST1 that do not appear in LIST2.
2814 Both lists have to be sorted over <."
2815   (let (out)
2816     (if (or (null list1) (null list2))
2817         (or list1 list2)
2818       (while (and list1 list2)
2819         (cond ((= (car list1) (car list2))
2820                (setq list1 (cdr list1)
2821                      list2 (cdr list2)))
2822               ((< (car list1) (car list2))
2823                (setq out (cons (car list1) out))
2824                (setq list1 (cdr list1)))
2825               (t
2826                (setq out (cons (car list2) out))
2827                (setq list2 (cdr list2)))))
2828       (nconc (nreverse out) (or list1 list2)))))
2829
2830 (defun gnus-intersection (list1 list2)      
2831   (let ((result nil))
2832     (while list2
2833       (if (memq (car list2) list1)
2834           (setq result (cons (car list2) result)))
2835       (setq list2 (cdr list2)))
2836     result))
2837
2838 (defun gnus-sorted-intersection (list1 list2)
2839   ;; LIST1 and LIST2 have to be sorted over <.
2840   (let (out)
2841     (while (and list1 list2)
2842       (cond ((= (car list1) (car list2))
2843              (setq out (cons (car list1) out)
2844                    list1 (cdr list1)
2845                    list2 (cdr list2)))
2846             ((< (car list1) (car list2))
2847              (setq list1 (cdr list1)))
2848             (t
2849              (setq list2 (cdr list2)))))
2850     (nreverse out)))
2851
2852 (defun gnus-set-sorted-intersection (list1 list2)
2853   ;; LIST1 and LIST2 have to be sorted over <.
2854   ;; This function modifies LIST1.
2855   (let* ((top (cons nil list1))
2856          (prev top))
2857   (while (and list1 list2)
2858     (cond ((= (car list1) (car list2))
2859            (setq prev list1
2860                  list1 (cdr list1)
2861                  list2 (cdr list2)))
2862           ((< (car list1) (car list2))
2863            (setcdr prev (cdr list1))
2864            (setq list1 (cdr list1)))
2865           (t
2866            (setq list2 (cdr list2)))))
2867   (setcdr prev nil)
2868   (cdr top)))
2869
2870 (defun gnus-compress-sequence (numbers &optional always-list)
2871   "Convert list of numbers to a list of ranges or a single range.
2872 If ALWAYS-LIST is non-nil, this function will always release a list of
2873 ranges."
2874   (let* ((first (car numbers))
2875          (last (car numbers))
2876          result)
2877     (if (null numbers)
2878         nil
2879       (if (not (listp (cdr numbers)))
2880           numbers
2881         (while numbers
2882           (cond ((= last (car numbers)) nil) ;Omit duplicated number
2883                 ((= (1+ last) (car numbers)) ;Still in sequence
2884                  (setq last (car numbers)))
2885                 (t                      ;End of one sequence
2886                  (setq result 
2887                        (cons (if (= first last) first
2888                                (cons first last)) result))
2889                  (setq first (car numbers))
2890                  (setq last  (car numbers))))
2891           (setq numbers (cdr numbers)))
2892         (if (and (not always-list) (null result))
2893             (if (= first last) (list first) (cons first last))
2894           (nreverse (cons (if (= first last) first (cons first last))
2895                           result)))))))
2896
2897 (defalias 'gnus-uncompress-sequence 'gnus-uncompress-range)
2898 (defun gnus-uncompress-range (ranges)
2899   "Expand a list of ranges into a list of numbers.
2900 RANGES is either a single range on the form `(num . num)' or a list of
2901 these ranges."
2902   (let (first last result)
2903     (cond 
2904      ((null ranges)
2905       nil)
2906      ((not (listp (cdr ranges)))
2907       (setq first (car ranges))
2908       (setq last (cdr ranges))
2909       (while (<= first last)
2910         (setq result (cons first result))
2911         (setq first (1+ first)))
2912       (nreverse result))
2913      (t
2914       (while ranges
2915         (if (atom (car ranges))
2916             (if (numberp (car ranges))
2917                 (setq result (cons (car ranges) result)))
2918           (setq first (car (car ranges)))
2919           (setq last  (cdr (car ranges)))
2920           (while (<= first last)
2921             (setq result (cons first result))
2922             (setq first (1+ first))))
2923         (setq ranges (cdr ranges)))
2924       (nreverse result)))))
2925
2926 (defun gnus-add-to-range (ranges list)
2927   "Return a list of ranges that has all articles from both RANGES and LIST.
2928 Note: LIST has to be sorted over `<'."
2929   (if (not ranges)
2930       (gnus-compress-sequence list t)
2931     (setq list (copy-sequence list))
2932     (or (listp (cdr ranges))
2933         (setq ranges (list ranges)))
2934     (let ((out ranges)
2935           ilist lowest highest temp)
2936       (while (and ranges list)
2937         (setq ilist list)
2938         (setq lowest (or (and (atom (car ranges)) (car ranges))
2939                          (car (car ranges))))
2940         (while (and list (cdr list) (< (car (cdr list)) lowest))
2941           (setq list (cdr list)))
2942         (if (< (car ilist) lowest)
2943             (progn
2944               (setq temp list)
2945               (setq list (cdr list))
2946               (setcdr temp nil)
2947               (setq out (nconc (gnus-compress-sequence ilist t) out))))
2948         (setq highest (or (and (atom (car ranges)) (car ranges))
2949                           (cdr (car ranges))))
2950         (while (and list (<= (car list) highest))
2951           (setq list (cdr list)))
2952         (setq ranges (cdr ranges)))
2953       (if list
2954           (setq out (nconc (gnus-compress-sequence list t) out)))
2955       (setq out (sort out (lambda (r1 r2) 
2956                             (< (or (and (atom r1) r1) (car r1))
2957                                (or (and (atom r2) r2) (car r2))))))
2958       (setq ranges out)
2959       (while ranges
2960         (if (atom (car ranges))
2961             (if (cdr ranges)
2962                 (if (atom (car (cdr ranges)))
2963                     (if (= (1+ (car ranges)) (car (cdr ranges)))
2964                         (progn
2965                           (setcar ranges (cons (car ranges) 
2966                                                (car (cdr ranges))))
2967                           (setcdr ranges (cdr (cdr ranges)))))
2968                   (if (= (1+ (car ranges)) (car (car (cdr ranges))))
2969                       (progn
2970                         (setcar (car (cdr ranges)) (car ranges))
2971                         (setcar ranges (car (cdr ranges)))
2972                         (setcdr ranges (cdr (cdr ranges)))))))
2973           (if (cdr ranges)
2974               (if (atom (car (cdr ranges)))
2975                   (if (= (1+ (cdr (car ranges))) (car (cdr ranges)))
2976                       (progn
2977                         (setcdr (car ranges) (car (cdr ranges)))
2978                         (setcdr ranges (cdr (cdr ranges)))))
2979                 (if (= (1+ (cdr (car ranges))) (car (car (cdr ranges))))
2980                     (progn
2981                       (setcdr (car ranges) (cdr (car (cdr ranges))))
2982                       (setcdr ranges (cdr (cdr ranges))))))))
2983         (setq ranges (cdr ranges)))
2984       out)))
2985
2986 (defun gnus-remove-from-range (ranges list)
2987   "Return a list of ranges that has all articles from LIST removed from RANGES.
2988 Note: LIST has to be sorted over `<'."
2989   ;; !!! This function shouldn't look like this, but I've got a headache.
2990   (gnus-compress-sequence 
2991    (gnus-sorted-complement
2992     (gnus-uncompress-range ranges) list)))
2993
2994 (defun gnus-member-of-range (number ranges)
2995   (if (not (listp (cdr ranges)))
2996       (and (>= number (car ranges)) 
2997            (<= number (cdr ranges)))
2998     (let ((not-stop t))
2999       (while (and ranges 
3000                   (if (numberp (car ranges))
3001                       (>= number (car ranges))
3002                     (>= number (car (car ranges))))
3003                   not-stop)
3004         (if (if (numberp (car ranges))
3005                 (= number (car ranges))
3006               (and (>= number (car (car ranges)))
3007                    (<= number (cdr (car ranges)))))
3008             (setq not-stop nil))
3009         (setq ranges (cdr ranges)))
3010       (not not-stop))))
3011
3012 \f
3013 ;;;
3014 ;;; Gnus group mode
3015 ;;;
3016
3017 (defvar gnus-group-mode-map nil)
3018 (defvar gnus-group-group-map nil)
3019 (defvar gnus-group-mark-map nil)
3020 (defvar gnus-group-list-map nil)
3021 (defvar gnus-group-sub-map nil)
3022 (put 'gnus-group-mode 'mode-class 'special)
3023
3024 (if gnus-group-mode-map
3025     nil
3026   (setq gnus-group-mode-map (make-keymap))
3027   (suppress-keymap gnus-group-mode-map)
3028   (define-key gnus-group-mode-map " " 'gnus-group-read-group)
3029   (define-key gnus-group-mode-map "=" 'gnus-group-select-group)
3030   (define-key gnus-group-mode-map "\r" 'gnus-group-select-group)
3031   (define-key gnus-group-mode-map "j" 'gnus-group-jump-to-group)
3032   (define-key gnus-group-mode-map "n" 'gnus-group-next-unread-group)
3033   (define-key gnus-group-mode-map "p" 'gnus-group-prev-unread-group)
3034   (define-key gnus-group-mode-map "\177" 'gnus-group-prev-unread-group)
3035   (define-key gnus-group-mode-map "N" 'gnus-group-next-group)
3036   (define-key gnus-group-mode-map "P" 'gnus-group-prev-group)
3037   (define-key gnus-group-mode-map "\M-n" 'gnus-group-next-unread-group-same-level)
3038   (define-key gnus-group-mode-map "\M-p" 'gnus-group-prev-unread-group-same-level)
3039   (define-key gnus-group-mode-map "," 'gnus-group-best-unread-group)
3040   (define-key gnus-group-mode-map "." 'gnus-group-first-unread-group)
3041   (define-key gnus-group-mode-map "u" 'gnus-group-unsubscribe-current-group)
3042   (define-key gnus-group-mode-map "U" 'gnus-group-unsubscribe-group)
3043   (define-key gnus-group-mode-map "c" 'gnus-group-catchup-current)
3044   (define-key gnus-group-mode-map "C" 'gnus-group-catchup-current-all)
3045   (define-key gnus-group-mode-map "l" 'gnus-group-list-groups)
3046   (define-key gnus-group-mode-map "L" 'gnus-group-list-all-groups)
3047   (define-key gnus-group-mode-map "m" 'gnus-group-mail)
3048   (define-key gnus-group-mode-map "g" 'gnus-group-get-new-news)
3049   (define-key gnus-group-mode-map "\M-g" 'gnus-group-get-new-news-this-group)
3050   (define-key gnus-group-mode-map "R" 'gnus-group-restart)
3051   (define-key gnus-group-mode-map "r" 'gnus-group-read-init-file)
3052   (define-key gnus-group-mode-map "B" 'gnus-group-browse-foreign-server)
3053   (define-key gnus-group-mode-map "b" 'gnus-group-check-bogus-groups)
3054   (define-key gnus-group-mode-map "F" 'gnus-find-new-newsgroups)
3055   (define-key gnus-group-mode-map "\C-c\C-d" 'gnus-group-describe-group)
3056   (define-key gnus-group-mode-map "\M-d" 'gnus-group-describe-all-groups)
3057   (define-key gnus-group-mode-map "\C-c\C-a" 'gnus-group-apropos)
3058   (define-key gnus-group-mode-map "\C-c\M-C-a" 'gnus-group-description-apropos)
3059   (define-key gnus-group-mode-map "a" 'gnus-group-post-news)
3060   (define-key gnus-group-mode-map "\ek" 'gnus-group-edit-local-kill)
3061   (define-key gnus-group-mode-map "\eK" 'gnus-group-edit-global-kill)
3062   (define-key gnus-group-mode-map "\C-k" 'gnus-group-kill-group)
3063   (define-key gnus-group-mode-map "\C-y" 'gnus-group-yank-group)
3064   (define-key gnus-group-mode-map "\C-w" 'gnus-group-kill-region)
3065   (define-key gnus-group-mode-map "\C-x\C-t" 'gnus-group-transpose-groups)
3066   (define-key gnus-group-mode-map "\C-c\C-l" 'gnus-group-list-killed)
3067   (define-key gnus-group-mode-map "\C-c\C-x" 'gnus-group-expire-articles)
3068   (define-key gnus-group-mode-map "\C-c\M-\C-x" 'gnus-group-expire-all-groups)
3069   (define-key gnus-group-mode-map "V" 'gnus-version)
3070   (define-key gnus-group-mode-map "s" 'gnus-group-save-newsrc)
3071   (define-key gnus-group-mode-map "z" 'gnus-group-suspend)
3072   (define-key gnus-group-mode-map "Z" 'gnus-group-clear-dribble)
3073   (define-key gnus-group-mode-map "q" 'gnus-group-exit)
3074   (define-key gnus-group-mode-map "Q" 'gnus-group-quit)
3075   (define-key gnus-group-mode-map "\M-f" 'gnus-group-fetch-faq)
3076   (define-key gnus-group-mode-map "?" 'gnus-group-describe-briefly)
3077   (define-key gnus-group-mode-map "\C-c\C-i" 'gnus-info-find-node)
3078   (define-key gnus-group-mode-map "\M-e" 'gnus-group-edit-group-method)
3079   (define-key gnus-group-mode-map "^" 'gnus-group-enter-server-mode)
3080   (define-key gnus-group-mode-map gnus-mouse-2 'gnus-mouse-pick-group)
3081   (define-key gnus-group-mode-map "<" 'beginning-of-buffer)
3082   (define-key gnus-group-mode-map ">" 'end-of-buffer)
3083   (define-key gnus-group-mode-map "\C-c\C-b" 'gnus-bug)
3084   (define-key gnus-group-mode-map "\C-c\C-s" 'gnus-group-sort-groups)
3085
3086   (define-key gnus-group-mode-map "#" 'gnus-group-mark-group)
3087   (define-key gnus-group-mode-map "\M-#" 'gnus-group-unmark-group)
3088   (define-prefix-command 'gnus-group-mark-map)
3089   (define-key gnus-group-mode-map "M" 'gnus-group-mark-map)
3090   (define-key gnus-group-mark-map "m" 'gnus-group-mark-group)
3091   (define-key gnus-group-mark-map "u" 'gnus-group-unmark-group)
3092   (define-key gnus-group-mark-map "w" 'gnus-group-mark-region)
3093
3094   (define-prefix-command 'gnus-group-group-map)
3095   (define-key gnus-group-mode-map "G" 'gnus-group-group-map)
3096   (define-key gnus-group-group-map "d" 'gnus-group-make-directory-group)
3097   (define-key gnus-group-group-map "h" 'gnus-group-make-help-group)
3098   (define-key gnus-group-group-map "a" 'gnus-group-make-archive-group)
3099   (define-key gnus-group-group-map "k" 'gnus-group-make-kiboze-group)
3100   (define-key gnus-group-group-map "m" 'gnus-group-make-group)
3101   (define-key gnus-group-group-map "E" 'gnus-group-edit-group)
3102   (define-key gnus-group-group-map "e" 'gnus-group-edit-group-method)
3103   (define-key gnus-group-group-map "p" 'gnus-group-edit-group-parameters)
3104   (define-key gnus-group-group-map "v" 'gnus-group-add-to-virtual)
3105   (define-key gnus-group-group-map "V" 'gnus-group-make-empty-virtual)
3106   (define-key gnus-group-group-map "D" 'gnus-group-enter-directory)
3107   (define-key gnus-group-group-map "f" 'gnus-group-make-doc-group)
3108   (define-key gnus-group-group-map "sb" 'gnus-group-brew-soup)
3109   (define-key gnus-group-group-map "sw" 'gnus-soup-save-areas)
3110   (define-key gnus-group-group-map "ss" 'gnus-soup-send-replies)
3111   (define-key gnus-group-group-map "sp" 'gnus-soup-pack-packet)
3112   (define-key gnus-group-group-map "sr" 'nnsoup-pack-replies)
3113
3114   (define-prefix-command 'gnus-group-list-map)
3115   (define-key gnus-group-mode-map "A" 'gnus-group-list-map)
3116   (define-key gnus-group-list-map "k" 'gnus-group-list-killed)
3117   (define-key gnus-group-list-map "z" 'gnus-group-list-zombies)
3118   (define-key gnus-group-list-map "s" 'gnus-group-list-groups)
3119   (define-key gnus-group-list-map "u" 'gnus-group-list-all-groups)
3120   (define-key gnus-group-list-map "a" 'gnus-group-apropos)
3121   (define-key gnus-group-list-map "d" 'gnus-group-description-apropos)
3122   (define-key gnus-group-list-map "m" 'gnus-group-list-matching)
3123   (define-key gnus-group-list-map "M" 'gnus-group-list-all-matching)
3124
3125   (define-prefix-command 'gnus-group-sub-map)
3126   (define-key gnus-group-mode-map "S" 'gnus-group-sub-map)
3127   (define-key gnus-group-sub-map "l" 'gnus-group-set-current-level)
3128   (define-key gnus-group-sub-map "t" 'gnus-group-unsubscribe-current-group)
3129   (define-key gnus-group-sub-map "s" 'gnus-group-unsubscribe-group)
3130   (define-key gnus-group-sub-map "k" 'gnus-group-kill-group)
3131   (define-key gnus-group-sub-map "y" 'gnus-group-yank-group)
3132   (define-key gnus-group-sub-map "w" 'gnus-group-kill-region)
3133   (define-key gnus-group-sub-map "z" 'gnus-group-kill-all-zombies))
3134
3135 (defun gnus-group-mode ()
3136   "Major mode for reading news.
3137
3138 All normal editing commands are switched off.
3139 \\<gnus-group-mode-map>
3140 The group buffer lists (some of) the groups available.  For instance,
3141 `\\[gnus-group-list-groups]' will list all subscribed groups with unread articles, while `\\[gnus-group-list-zombies]'
3142 lists all zombie groups. 
3143
3144 Groups that are displayed can be entered with `\\[gnus-group-read-group]'.  To subscribe 
3145 to a group not displayed, type `\\[gnus-group-unsubscribe-group]'. 
3146
3147 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]'). 
3148
3149 The following commands are available:
3150
3151 \\{gnus-group-mode-map}"
3152   (interactive)
3153   (if gnus-visual (gnus-group-make-menu-bar))
3154   (kill-all-local-variables)
3155   (setq mode-line-modified "-- ")
3156   (make-local-variable 'mode-line-format)
3157   (setq mode-line-format (copy-sequence mode-line-format))
3158   (and (equal (nth 3 mode-line-format) "   ")
3159        (setcar (nthcdr 3 mode-line-format) ""))
3160   (setq major-mode 'gnus-group-mode)
3161   (setq mode-name "Group")
3162   (gnus-group-set-mode-line)
3163   (setq mode-line-process nil)
3164   (use-local-map gnus-group-mode-map)
3165   (buffer-disable-undo (current-buffer))
3166   (setq truncate-lines t)
3167   (setq buffer-read-only t)
3168   (run-hooks 'gnus-group-mode-hook))
3169
3170 (defun gnus-mouse-pick-group (e)
3171   (interactive "e")
3172   (mouse-set-point e)
3173   (gnus-group-read-group nil))
3174
3175 ;; Look at LEVEL and find out what the level is really supposed to be.
3176 ;; If LEVEL is non-nil, LEVEL will be returned, if not, what happens
3177 ;; will depend on whether `gnus-group-use-permanent-levels' is used.
3178 (defun gnus-group-default-level (&optional level number-or-nil)
3179   (cond  
3180    (gnus-group-use-permanent-levels
3181     (setq gnus-group-default-list-level 
3182           (or level gnus-group-default-list-level))
3183     (or gnus-group-default-list-level gnus-level-subscribed))
3184    (number-or-nil
3185     level)
3186    (t
3187     (or level gnus-group-default-list-level gnus-level-subscribed))))
3188   
3189
3190 ;;;###autoload
3191 (defun gnus-no-server (&optional arg)
3192   "Read network news.
3193 If ARG is a positive number, Gnus will use that as the
3194 startup level. If ARG is nil, Gnus will be started at level 2. 
3195 If ARG is non-nil and not a positive number, Gnus will
3196 prompt the user for the name of an NNTP server to use.
3197 As opposed to `gnus', this command will not connect to the local server."
3198   (interactive "P")
3199   (setq gnus-group-use-permanent-levels t)
3200   (gnus (or arg (1- gnus-level-default-subscribed)) t))
3201
3202 (defalias '\(ding\) 'gnus)
3203
3204 ;;;###autoload
3205 (defun gnus (&optional arg dont-connect)
3206   "Read network news.
3207 If ARG is non-nil and a positive number, Gnus will use that as the
3208 startup level. If ARG is non-nil and not a positive number, Gnus will
3209 prompt the user for the name of an NNTP server to use."
3210   (interactive "P")
3211   (if (get-buffer gnus-group-buffer)
3212       (progn
3213         (switch-to-buffer gnus-group-buffer)
3214         (gnus-group-get-new-news))
3215     (gnus-clear-system)
3216     (nnheader-init-server-buffer)
3217     (gnus-read-init-file)
3218
3219     (let ((level (and arg (numberp arg) (> arg 0) arg))
3220           did-connect)
3221       (unwind-protect
3222           (progn
3223             (gnus-group-setup-buffer)
3224             (or dont-connect 
3225                 (setq did-connect
3226                       (gnus-start-news-server (and arg (not level))))))
3227         (if (and (not dont-connect) 
3228                  (not did-connect))
3229             (gnus-group-quit)
3230           (run-hooks 'gnus-startup-hook)
3231           ;; NNTP server is successfully open. 
3232
3233           ;; Find the current startup file name.
3234           (setq gnus-current-startup-file 
3235                 (gnus-make-newsrc-file gnus-startup-file))
3236
3237           ;; Read the dribble file.
3238           (and gnus-use-dribble-file (gnus-dribble-read-file))
3239
3240           (gnus-summary-make-display-table)
3241           (let ((buffer-read-only nil))
3242             (erase-buffer)
3243             (if (not gnus-inhibit-startup-message)
3244                 (progn
3245                   (gnus-group-startup-message)
3246                   (sit-for 0))))
3247           (gnus-setup-news nil level)
3248           (gnus-group-list-groups level)
3249           (gnus-configure-windows 'group))))))
3250
3251 (defun gnus-unload ()
3252   "Unload all Gnus features."
3253   (interactive)
3254   (let ((history load-history)
3255         feature)
3256     (while history
3257       (and (string-match "^gnus" (car (car history)))
3258            (setq feature (cdr (assq 'provide (car history))))
3259            (unload-feature feature 'force))
3260       (setq history (cdr history)))))
3261
3262 (defun gnus-group-startup-message (&optional x y)
3263   "Insert startup message in current buffer."
3264   ;; Insert the message.
3265   (erase-buffer)
3266   (insert
3267    (format "
3268      %s
3269            A newsreader 
3270       for GNU Emacs
3271
3272         Based on GNUS 
3273              written by 
3274      Masanobu UMEDA
3275
3276        A Praxis Release
3277       larsi@ifi.uio.no
3278
3279            gnus-version))
3280   ;; And then hack it.
3281   ;; 18 is the longest line.
3282   (indent-rigidly (point-min) (point-max) 
3283                   (/ (max (- (window-width) (or x 28)) 0) 2))
3284   (goto-char (point-min))
3285   ;; +4 is fuzzy factor.
3286   (insert-char ?\n (/ (max (- (window-height) (or y 12)) 0) 2))
3287
3288   ;; Fontify some.
3289   (goto-char (point-min))
3290   (search-forward "Praxis")
3291   (put-text-property (match-beginning 0) (match-end 0) 'face 'bold)
3292   (goto-char (point-min)))
3293
3294 (defun gnus-group-setup-buffer ()
3295   (or (get-buffer gnus-group-buffer)
3296       (progn
3297         (switch-to-buffer gnus-group-buffer)
3298         (gnus-add-current-to-buffer-list)
3299         (gnus-group-mode)
3300         (and gnus-carpal (gnus-carpal-setup-buffer 'group)))))
3301
3302 (defun gnus-group-list-groups (&optional level unread)
3303   "List newsgroups with level LEVEL or lower that have unread articles.
3304 Default is all subscribed groups.
3305 If argument UNREAD is non-nil, groups with no unread articles are also
3306 listed." 
3307   (interactive (list (if current-prefix-arg
3308                          (prefix-numeric-value current-prefix-arg)
3309                        (or
3310                         (gnus-group-default-level nil t)
3311                         gnus-level-subscribed))))
3312   (or level
3313       (setq level (car gnus-group-list-mode)
3314             unread (cdr gnus-group-list-mode)))
3315   (setq level (gnus-group-default-level level))
3316   (gnus-group-setup-buffer)     ;May call from out of group buffer
3317   (let ((case-fold-search nil)
3318         (group (gnus-group-group-name)))
3319     (funcall gnus-group-prepare-function level unread nil)
3320     (if (zerop (buffer-size))
3321         (gnus-message 5 gnus-no-groups-message)
3322       (goto-char (point-min))
3323       (if (not group)
3324           ;; Go to the first group with unread articles.
3325           (gnus-group-search-forward nil nil nil t)
3326         ;; Find the right group to put point on. If the current group
3327         ;; has disapeared in the new listing, try to find the next
3328         ;; one. If no next one can be found, just leave point at the
3329         ;; first newsgroup in the buffer.
3330         (if (not (gnus-goto-char
3331                   (text-property-any (point-min) (point-max) 
3332                                      'gnus-group (intern group))))
3333             (let ((newsrc (nthcdr 3 (gnus-gethash group gnus-newsrc-hashtb))))
3334               (while (and newsrc
3335                           (not (gnus-goto-char 
3336                                 (text-property-any 
3337                                  (point-min) (point-max) 'gnus-group 
3338                                  (intern (car (car newsrc)))))))
3339                 (setq newsrc (cdr newsrc)))
3340               (or newsrc (progn (goto-char (point-max))
3341                                 (forward-line -1))))))
3342       ;; Adjust cursor point.
3343       (gnus-group-position-cursor))))
3344
3345 (defun gnus-group-prepare-flat (level &optional all lowest regexp) 
3346   "List all newsgroups with unread articles of level LEVEL or lower.
3347 If ALL is non-nil, list groups that have no unread articles.
3348 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher.
3349 If REGEXP, only list groups matching REGEXP."
3350   (set-buffer gnus-group-buffer)
3351   (let ((buffer-read-only nil)
3352         (newsrc (cdr gnus-newsrc-alist))
3353         (lowest (or lowest 1))
3354         info clevel unread group)
3355     (erase-buffer)
3356     (if (< lowest gnus-level-zombie)
3357         ;; List living groups.
3358         (while newsrc
3359           (setq info (car newsrc)
3360                 group (car info)
3361                 newsrc (cdr newsrc)
3362                 unread (car (gnus-gethash group gnus-newsrc-hashtb)))
3363           (and unread ; This group might be bogus
3364                (or (not regexp)
3365                    (string-match regexp group))
3366                (<= (setq clevel (car (cdr info))) level) 
3367                (>= clevel lowest)
3368                (or all            ; We list all groups?
3369                    (eq unread t)  ; We list unactivated groups
3370                    (> unread 0)   ; We list groups with unread articles
3371                    (cdr (assq 'tick (nth 3 info)))) ; And groups with tickeds
3372                (gnus-group-insert-group-line 
3373                 nil group (car (cdr info)) (nth 3 info) unread (nth 4 info)))))
3374
3375     ;; List dead groups.
3376     (and (>= level gnus-level-zombie) (<= lowest gnus-level-zombie)
3377          (gnus-group-prepare-flat-list-dead 
3378           (setq gnus-zombie-list (sort gnus-zombie-list 'string<)) 
3379           gnus-level-zombie ?Z
3380           regexp))
3381     (and (>= level gnus-level-killed) (<= lowest gnus-level-killed)
3382          (gnus-group-prepare-flat-list-dead 
3383           (setq gnus-killed-list (sort gnus-killed-list 'string<)) 
3384           gnus-level-killed ?K regexp))
3385
3386     (gnus-group-set-mode-line)
3387     (setq gnus-group-list-mode (cons level all))
3388     (run-hooks 'gnus-group-prepare-hook)))
3389
3390 (defun gnus-group-prepare-flat-list-dead (groups level mark regexp)
3391   ;; List zombies and killed lists somehwat faster, which was
3392   ;; suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>. It does
3393   ;; this by ignoring the group format specification altogether.
3394   (let (group beg)
3395     (while groups
3396       (setq group (car groups)
3397             groups (cdr groups))
3398       (if (or (not regexp)
3399               (string-match regexp group))
3400           (progn
3401             (setq beg (point))
3402             (insert (format " %c     *: %s\n" mark group))
3403             (add-text-properties 
3404              beg (1+ beg) 
3405              (list 'gnus-group (intern group)
3406                    'gnus-unread t
3407                    'gnus-level level)))))))
3408
3409 (defun gnus-group-real-name (group)
3410   "Find the real name of a foreign newsgroup."
3411   (if (string-match ":[^:]+$" group)
3412       (substring group (1+ (match-beginning 0)))
3413     group))
3414
3415 (defun gnus-group-prefixed-name (group method)
3416   "Return the whole name from GROUP and METHOD."
3417   (and (stringp method) (setq method (gnus-server-to-method method)))
3418   (concat (format "%s" (car method))
3419           (if (and 
3420                (assoc (format "%s" (car method)) (gnus-methods-using 'address))
3421                (not (string= (nth 1 method) "")))
3422               (concat "+" (nth 1 method)))
3423           ":" group))
3424
3425 (defun gnus-group-real-prefix (group)
3426   "Return the prefix of the current group name."
3427   (if (string-match "^[^:]+:" group)
3428       (substring group 0 (match-end 0))
3429     ""))
3430
3431 (defun gnus-group-method-name (group)
3432   "Return the method used for selecting GROUP."
3433   (let ((prefix (gnus-group-real-prefix group)))
3434     (if (equal prefix "")
3435         gnus-select-method
3436       (if (string-match "^[^\\+]+\\+" prefix)
3437           (list (intern (substring prefix 0 (1- (match-end 0))))
3438                 (substring prefix (match-end 0) (1- (length prefix))))
3439         (list (intern (substring prefix 0 (1- (length prefix)))) "")))))
3440
3441 (defun gnus-group-foreign-p (group)
3442   "Return nil if GROUP is native, non-nil if it is foreign."
3443   (string-match ":" group))
3444
3445 (defun gnus-group-set-info (info &optional method-only-group part)
3446   (let* ((entry (gnus-gethash
3447                  (or method-only-group (car info)) gnus-newsrc-hashtb))
3448          (part-info info)
3449          (info (if method-only-group (nth 2 entry) info)))
3450     (if (not method-only-group)
3451         ()
3452       (or entry
3453           (error "Trying to change non-existent group %s" method-only-group))
3454       ;; We have recevied parts of the actual group info - either the
3455       ;; select method or the group parameters.  We first check
3456       ;; whether we have to extend the info, and if so, do that.
3457       (let ((len (length info))
3458             (total (if (eq part 'method) 5 6)))
3459         (and (< len total)
3460              (setcdr (nthcdr (1- len) info)
3461                      (make-list (- total len) nil)))
3462         ;; Then we enter the new info.
3463         (setcar (nthcdr (1- total) info) part-info)))
3464     ;; We uncompress some lists of marked articles.
3465     (let (marked)
3466       (if (not (setq marked (nth 3 info)))
3467           ()
3468         (while marked
3469           (or (eq 'score (car (car marked)))
3470               (eq 'bookmark (car (car marked)))
3471               (eq 'killed (car (car marked)))
3472               (setcdr (car marked) 
3473                       (gnus-uncompress-range (cdr (car marked)))))
3474           (setq marked (cdr marked)))))
3475     (if entry
3476         ()
3477       ;; This is a new group, so we just create it.
3478       (save-excursion
3479         (set-buffer gnus-group-buffer)
3480         (if (nth 4 info)
3481             ;; It's a foreign group...
3482             (gnus-group-make-group 
3483              (gnus-group-real-name (car info))
3484              (prin1-to-string (car (nth 4 info)))
3485              (nth 1 (nth 4 info)))
3486           ;; It's a native group.
3487           (gnus-group-make-group
3488            (car info)
3489            (prin1-to-string (car gnus-select-method))
3490            (nth 1 gnus-select-method)))
3491         (gnus-message 6 "Note: New group created")
3492         (setq entry 
3493               (gnus-gethash (gnus-group-prefixed-name 
3494                              (gnus-group-real-name (car info))
3495                              (or (nth 4 info) gnus-select-method))
3496                             gnus-newsrc-hashtb))))
3497     ;; Whether it was a new group or not, we now have the entry, so we
3498     ;; can do the update.
3499     (if entry
3500         (progn
3501           (setcar (nthcdr 2 entry) info)
3502           (if (and (not (eq (car entry) t)) 
3503                    (gnus-gethash (car info) gnus-active-hashtb))
3504               (let ((marked (nth 3 info)))
3505                 (setcar entry 
3506                         (max 0 (- (length (gnus-list-of-unread-articles 
3507                                            (car info)))
3508                                   (length (cdr (assq 'tick marked)))
3509                                   (length (cdr (assq 'dormant marked)))))))))
3510       (error "No such group: %s" (car info)))))
3511
3512 (defun gnus-group-set-method-info (group select-method)
3513   (gnus-group-set-info select-method group 'method))
3514
3515 (defun gnus-group-set-params-info (group params)
3516   (gnus-group-set-info params group 'params))
3517
3518 (defun gnus-group-update-group-line ()
3519   "This function updates the current line in the newsgroup buffer and
3520 moves the point to the colon."
3521   (let* ((buffer-read-only nil)
3522          (group (gnus-group-group-name))
3523          (entry (and group (gnus-gethash group gnus-newsrc-hashtb))))
3524     (if entry
3525         (gnus-dribble-enter 
3526          (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
3527                  ")")))
3528     (beginning-of-line)
3529     (delete-region (point) (progn (forward-line 1) (point)))
3530     (gnus-group-insert-group-line-info group)
3531     (forward-line -1)
3532     (gnus-group-position-cursor)))
3533
3534 (defun gnus-group-insert-group-line-info (group)
3535   (let ((entry (gnus-gethash group gnus-newsrc-hashtb)) 
3536         active info)
3537     (if entry
3538         (progn
3539           (setq info (nth 2 entry))
3540           (gnus-group-insert-group-line 
3541            nil group (nth 1 info) (nth 3 info) (car entry) (nth 4 info)))
3542       (setq active (gnus-gethash group gnus-active-hashtb))
3543       (gnus-group-insert-group-line 
3544        nil group 
3545        (if (member group gnus-zombie-list) gnus-level-zombie gnus-level-killed)
3546        nil (if active (- (1+ (cdr active)) (car active)) 0) nil))))
3547
3548 (defun gnus-group-insert-group-line (gformat group level marked number method)
3549   (let* ((gformat (or gformat gnus-group-line-format-spec))
3550          (active (gnus-gethash group gnus-active-hashtb))
3551          (number-total (if active (1+ (- (cdr active) (car active))) 0))
3552          (number-of-dormant (length (cdr (assq 'dormant marked))))
3553          (number-of-ticked (length (cdr (assq 'tick marked))))
3554          (number-of-ticked-and-dormant
3555           (+ number-of-ticked number-of-dormant))
3556          (number-of-unread-unticked 
3557           (if (numberp number) (int-to-string (max 0 number))
3558             "*"))
3559          (number-of-read
3560           (if (numberp number)
3561               (max 0 (- number-total number))
3562             "*"))
3563          (subscribed (cond ((<= level gnus-level-subscribed) ? )
3564                            ((<= level gnus-level-unsubscribed) ?U)
3565                            ((= level gnus-level-zombie) ?Z)
3566                            (t ?K)))
3567          (qualified-group (gnus-group-real-name group))
3568          (newsgroup-description 
3569           (if gnus-description-hashtb
3570               (or (gnus-gethash group gnus-description-hashtb) "")
3571             ""))
3572          (moderated (if (member group gnus-moderated-list) ?m ? ))
3573          (moderated-string (if (eq moderated ?m) "(m)" ""))
3574          (method (gnus-server-get-method group method))
3575          (news-server (or (car (cdr method)) ""))
3576          (news-method (or (car method) ""))
3577          (news-method-string 
3578           (if method (format "(%s:%s)" (car method) (car (cdr method))) ""))
3579          (marked (if (and 
3580                       (numberp number) 
3581                       (zerop number)
3582                       (> number-of-ticked 0))
3583                      ?* ? ))
3584          (number (if (eq number t) "*" (+ number number-of-dormant 
3585                                           number-of-ticked)))
3586          (process-marked (if (member qualified-group gnus-group-marked)
3587                              gnus-process-mark ? ))
3588          (buffer-read-only nil)
3589          header ; passed as parameter to user-funcs.
3590          b)
3591     (beginning-of-line)
3592     (setq b (point))
3593     ;; Insert the text.
3594     (insert (eval gformat))
3595
3596     (add-text-properties 
3597      b (1+ b) (list 'gnus-group (intern group)
3598                     'gnus-unread (if (numberp number)
3599                                      (string-to-int number-of-unread-unticked)
3600                                    t)
3601                     'gnus-marked marked
3602                     'gnus-level level))))
3603
3604 (defun gnus-group-update-group (group &optional visible-only)
3605   "Update newsgroup info of GROUP.
3606 If VISIBLE-ONLY is non-nil, the group won't be displayed if it isn't already."
3607   (save-excursion
3608     (set-buffer gnus-group-buffer)
3609     (let ((buffer-read-only nil)
3610           visible)
3611       (let ((entry (gnus-gethash group gnus-newsrc-hashtb)))
3612         (if entry
3613             (gnus-dribble-enter 
3614              (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
3615                      ")"))))
3616       ;; Buffer may be narrowed.
3617       (save-restriction
3618         (widen)
3619         ;; Search a line to modify.  If the buffer is large, the search
3620         ;; takes long time.  In most cases, current point is on the line
3621         ;; we are looking for.  So, first of all, check current line. 
3622         (if (or (progn
3623                   (beginning-of-line)
3624                   (eq (get-text-property (point) 'gnus-group)
3625                       (intern group)))
3626                 (progn
3627                   (gnus-goto-char 
3628                    (text-property-any 
3629                     (point-min) (point-max) 'gnus-group (intern group)))))
3630             ;; GROUP is listed in current buffer. So, delete old line.
3631             (progn
3632               (setq visible t)
3633               (beginning-of-line)
3634               (delete-region (point) (progn (forward-line 1) (point))))
3635           ;; No such line in the buffer, find out where it's supposed to
3636           ;; go, and insert it there (or at the end of the buffer).
3637           ;; Fix by Per Abrahamsen <amanda@iesd.auc.dk>.
3638           (or visible-only
3639               (let ((entry 
3640                      (cdr (cdr (gnus-gethash group gnus-newsrc-hashtb)))))
3641                 (while (and entry
3642                             (car entry)
3643                             (not
3644                              (gnus-goto-char
3645                               (text-property-any
3646                                (point-min) (point-max) 
3647                                'gnus-group (intern (car (car entry)))))))
3648                   (setq entry (cdr entry)))
3649                 (or entry (goto-char (point-max)))))))
3650       (if (or visible (not visible-only))
3651           (gnus-group-insert-group-line-info group))
3652       (gnus-group-set-mode-line))))
3653
3654 (defun gnus-group-set-mode-line ()
3655   (if (memq 'group gnus-updated-mode-lines)
3656       (let* ((gformat (or gnus-group-mode-line-format-spec
3657                           (setq gnus-group-mode-line-format-spec
3658                                 (gnus-parse-format 
3659                                  gnus-group-mode-line-format 
3660                                  gnus-group-mode-line-format-alist))))
3661              (news-server (car (cdr gnus-select-method)))
3662              (news-method (car gnus-select-method))
3663              (max-len 60)
3664              (mode-string (eval gformat)))
3665         (setq mode-string (eval gformat))
3666         (if (> (length mode-string) max-len) 
3667             (setq mode-string (substring mode-string 0 (- max-len 4))))
3668         (setq mode-line-buffer-identification mode-string)
3669         (set-buffer-modified-p t))))
3670
3671 (defun gnus-group-group-name ()
3672   "Get the name of the newsgroup on the current line."
3673   (let ((group (get-text-property (gnus-point-at-bol) 'gnus-group)))
3674     (and group (symbol-name group))))
3675
3676 (defun gnus-group-group-level ()
3677   "Get the level of the newsgroup on the current line."
3678   (get-text-property (gnus-point-at-bol) 'gnus-level))
3679
3680 (defun gnus-group-group-unread ()
3681   "Get the number of unread articles of the newsgroup on the current line."
3682   (get-text-property (gnus-point-at-bol) 'gnus-unread))
3683
3684 (defun gnus-group-search-forward (&optional backward all level first-too)
3685   "Find the next newsgroup with unread articles.
3686 If BACKWARD is non-nil, find the previous newsgroup instead.
3687 If ALL is non-nil, just find any newsgroup.
3688 If LEVEL is non-nil, find group with level LEVEL, or higher if no such
3689 group exists.
3690 If FIRST-TOO, the current line is also eligible as a target."
3691   (let ((way (if backward -1 1))
3692         (low gnus-level-killed)
3693         (beg (point))
3694         pos found lev)
3695     (if (and backward (progn (beginning-of-line)) (bobp))
3696         nil
3697       (or first-too (forward-line way))
3698       (while (and 
3699               (not (eobp))
3700               (not (setq 
3701                     found 
3702                     (and (or all
3703                              (and
3704                               (let ((unread 
3705                                      (get-text-property (point) 'gnus-unread)))
3706                                 (or (eq unread t) (and unread (> unread 0))))
3707                               (setq lev (get-text-property (point)
3708                                                            'gnus-level))
3709                               (<= lev gnus-level-subscribed)))
3710                          (or (not level)
3711                              (and (setq lev (get-text-property (point)
3712                                                                'gnus-level))
3713                                   (or (= lev level)
3714                                       (and (< lev low)
3715                                            (< level lev)
3716                                            (progn
3717                                              (setq low lev)
3718                                              (setq pos (point))
3719                                              nil))))))))
3720               (zerop (forward-line way)))))
3721     (if found 
3722         (progn (gnus-group-position-cursor) t)
3723       (goto-char (or pos beg))
3724       (and pos t))))
3725
3726 ;;; Gnus group mode commands
3727
3728 ;; Group marking.
3729
3730 (defun gnus-group-mark-group (n &optional unmark no-advance)
3731   "Mark the current group."
3732   (interactive "p")
3733   (let ((buffer-read-only nil)
3734         group)
3735     (while 
3736         (and (> n 0) 
3737              (setq group (gnus-group-group-name))
3738              (progn
3739                (beginning-of-line)
3740                (forward-char 
3741                 (or (cdr (assq 'process gnus-group-mark-positions)) 2))
3742                (delete-char 1)
3743                (if unmark
3744                    (progn
3745                      (insert " ")
3746                      (setq gnus-group-marked (delete group gnus-group-marked)))
3747                  (insert "#")
3748                  (setq gnus-group-marked
3749                        (cons group (delete group gnus-group-marked))))
3750                t)
3751              (or no-advance (zerop (gnus-group-next-group 1))))
3752       (setq n (1- n)))
3753     (gnus-summary-position-cursor)
3754     n))
3755
3756 (defun gnus-group-unmark-group (n)
3757   "Remove the mark from the current group."
3758   (interactive "p")
3759   (gnus-group-mark-group n 'unmark))
3760
3761 (defun gnus-group-mark-region (unmark beg end)
3762   "Mark all groups between point and mark.
3763 If UNMARK, remove the mark instead."
3764   (interactive "P\nr")
3765   (let ((num (count-lines beg end)))
3766     (save-excursion
3767       (goto-char beg)
3768       (- num (gnus-group-mark-group num unmark)))))
3769
3770 (defun gnus-group-remove-mark (group)
3771   (and (gnus-group-goto-group group)
3772        (save-excursion
3773          (gnus-group-mark-group 1 'unmark t))))
3774
3775 ;; Return a list of groups to work on.  Take into consideration N (the
3776 ;; prefix) and the list of marked groups.
3777 (defun gnus-group-process-prefix (n)
3778   (cond (n
3779          (setq n (prefix-numeric-value n))
3780          ;; There is a prefix, so we return a list of the N next
3781          ;; groups. 
3782          (let ((way (if (< n 0) -1 1))
3783                (n (abs n))
3784                group groups)
3785            (save-excursion
3786              (while (and (> n 0)
3787                          (setq group (gnus-group-group-name)))
3788                (setq groups (cons group groups))
3789                (setq n (1- n))
3790                (forward-line way)))
3791            (nreverse groups)))
3792         (gnus-group-marked
3793          ;; No prefix, but a list of marked articles.
3794          (reverse gnus-group-marked))
3795         (t
3796          ;; Neither marked articles or a prefix, so we return the
3797          ;; current group.
3798          (let ((group (gnus-group-group-name)))
3799            (and group (list group))))))
3800
3801 ;; Selecting groups.
3802
3803 (defun gnus-group-read-group (&optional all no-article group)
3804   "Read news in this newsgroup.
3805 If the prefix argument ALL is non-nil, already read articles become
3806 readable. If the optional argument NO-ARTICLE is non-nil, no article
3807 will be auto-selected upon group entry."
3808   (interactive "P")
3809   (let ((group (or group (gnus-group-group-name)))
3810         number active marked entry)
3811     (or group (error "No group on current line"))
3812     (setq marked 
3813           (nth 3 (nth 2 (setq entry (gnus-gethash group gnus-newsrc-hashtb)))))
3814     ;; This group might be a dead group. In that case we have to get
3815     ;; the number of unread articles from `gnus-active-hashtb'.
3816     (if entry
3817         (setq number (car entry))
3818       (if (setq active (gnus-gethash group gnus-active-hashtb))
3819           (setq number (- (1+ (cdr active)) (car active)))))
3820     (gnus-summary-read-group 
3821      group (or all (and (numberp number) 
3822                         (zerop (+ number (length (cdr (assq 'tick marked)))
3823                                   (length (cdr (assq 'dormant marked)))))))
3824      no-article)))
3825
3826 (defun gnus-group-select-group (&optional all)
3827   "Select this newsgroup.
3828 No article is selected automatically.
3829 If argument ALL is non-nil, already read articles become readable."
3830   (interactive "P")
3831   (gnus-group-read-group all t))
3832
3833 (defun gnus-group-select-group-all ()
3834   "Select the current group and display all articles in it."
3835   (interactive)
3836   (gnus-group-select-group 'all))
3837
3838 ;; Enter a group that is not in the group buffer. Non-nil is returned
3839 ;; if selection was successful.
3840 (defun gnus-group-read-ephemeral-group 
3841   (group method &optional activate quit-config)
3842   (let ((group (if (gnus-group-foreign-p group) group
3843                  (gnus-group-prefixed-name group method))))
3844     (gnus-sethash 
3845      group
3846      (list t nil (list group gnus-level-default-subscribed nil nil 
3847                        (append method
3848                                (list
3849                                 (list 'quit-config 
3850                                       (if quit-config quit-config
3851                                         (cons (current-buffer) 'summary)))))))
3852      gnus-newsrc-hashtb)
3853     (set-buffer gnus-group-buffer)
3854     (or (gnus-check-server method)
3855         (error "Unable to contact server: %s" (gnus-status-message method)))
3856     (if activate (or (gnus-request-group group)
3857                      (error "Couldn't request group")))
3858     (condition-case ()
3859         (gnus-group-read-group t t group)
3860       (error nil)
3861       (quit nil))
3862     (not (equal major-mode 'gnus-group-mode))))
3863   
3864 (defun gnus-group-jump-to-group (group)
3865   "Jump to newsgroup GROUP."
3866   (interactive 
3867    (list (completing-read 
3868           "Group: " gnus-active-hashtb nil 
3869           (memq gnus-select-method gnus-have-read-active-file))))
3870
3871   (if (equal group "")
3872       (error "Empty group name"))
3873
3874   (let ((b (text-property-any 
3875             (point-min) (point-max) 'gnus-group (intern group))))
3876     (if b
3877         ;; Either go to the line in the group buffer...
3878         (goto-char b)
3879       ;; ... or insert the line.
3880       (or
3881        (gnus-gethash group gnus-active-hashtb)
3882        (gnus-activate-group group)
3883        (error "%s error: %s" group (gnus-status-message group)))
3884
3885       (gnus-group-update-group group)
3886       (goto-char (text-property-any 
3887                   (point-min) (point-max) 'gnus-group (intern group)))))
3888   ;; Adjust cursor point.
3889   (gnus-group-position-cursor))
3890
3891 (defun gnus-group-goto-group (group)
3892   "Goto to newsgroup GROUP."
3893   (let ((b (text-property-any (point-min) (point-max) 
3894                               'gnus-group (intern group))))
3895     (and b (goto-char b))))
3896
3897 (defun gnus-group-next-group (n)
3898   "Go to next N'th newsgroup.
3899 If N is negative, search backward instead.
3900 Returns the difference between N and the number of skips actually
3901 done."
3902   (interactive "p")
3903   (gnus-group-next-unread-group n t))
3904
3905 (defun gnus-group-next-unread-group (n &optional all level)
3906   "Go to next N'th unread newsgroup.
3907 If N is negative, search backward instead.
3908 If ALL is non-nil, choose any newsgroup, unread or not.
3909 If LEVEL is non-nil, choose the next group with level LEVEL, or, if no
3910 such group can be found, the next group with a level higher than
3911 LEVEL.
3912 Returns the difference between N and the number of skips actually
3913 made."
3914   (interactive "p")
3915   (let ((backward (< n 0))
3916         (n (abs n)))
3917     (while (and (> n 0)
3918                 (gnus-group-search-forward 
3919                  backward (or (not gnus-group-goto-unread) all) level))
3920       (setq n (1- n)))
3921     (if (/= 0 n) (gnus-message 7 "No more%s newsgroups%s" (if all "" " unread")
3922                                (if level " on this level or higher" "")))
3923     n))
3924
3925 (defun gnus-group-prev-group (n)
3926   "Go to previous N'th newsgroup.
3927 Returns the difference between N and the number of skips actually
3928 done."
3929   (interactive "p")
3930   (gnus-group-next-unread-group (- n) t))
3931
3932 (defun gnus-group-prev-unread-group (n)
3933   "Go to previous N'th unread newsgroup.
3934 Returns the difference between N and the number of skips actually
3935 done."  
3936   (interactive "p")
3937   (gnus-group-next-unread-group (- n)))
3938
3939 (defun gnus-group-next-unread-group-same-level (n)
3940   "Go to next N'th unread newsgroup on the same level.
3941 If N is negative, search backward instead.
3942 Returns the difference between N and the number of skips actually
3943 done."
3944   (interactive "p")
3945   (gnus-group-next-unread-group n t (gnus-group-group-level))
3946   (gnus-group-position-cursor))
3947
3948 (defun gnus-group-prev-unread-group-same-level (n)
3949   "Go to next N'th unread newsgroup on the same level.
3950 Returns the difference between N and the number of skips actually
3951 done."
3952   (interactive "p")
3953   (gnus-group-next-unread-group (- n) t (gnus-group-group-level))
3954   (gnus-group-position-cursor))
3955
3956 (defun gnus-group-best-unread-group (&optional exclude-group)
3957   "Go to the group with the highest level.
3958 If EXCLUDE-GROUP, do not go to that group."
3959   (interactive)
3960   (goto-char (point-min))
3961   (let ((best 100000)
3962         unread best-point)
3963     (while (setq unread (get-text-property (point) 'gnus-unread))
3964       (if (and (numberp unread) (> unread 0))
3965           (progn
3966             (if (and (< (get-text-property (point) 'gnus-level) best)
3967                      (or (not exclude-group)
3968                          (not (equal exclude-group (gnus-group-group-name)))))
3969                 (progn 
3970                   (setq best (get-text-property (point) 'gnus-level))
3971                   (setq best-point (point))))))
3972       (forward-line 1))
3973     (if best-point (goto-char best-point))
3974     (gnus-summary-position-cursor)
3975     (and best-point (gnus-group-group-name))))
3976
3977 (defun gnus-group-first-unread-group ()
3978   "Go to the first group with unread articles."
3979   (interactive)
3980   (prog1
3981       (let ((opoint (point))
3982             unread)
3983         (goto-char (point-min))
3984         (if (or (eq (setq unread (gnus-group-group-unread)) t) ; Not active.
3985                 (not (zerop unread)) ; Has unread articles.
3986                 (zerop (gnus-group-next-unread-group 1))) ; Next unread group.
3987             (point) ; Success.
3988           (goto-char opoint)
3989           nil)) ; Not success.
3990     (gnus-group-position-cursor)))
3991
3992 (defun gnus-group-enter-server-mode ()
3993   "Jump to the server buffer."
3994   (interactive)
3995   (gnus-server-setup-buffer)
3996   (gnus-configure-windows 'server)
3997   (gnus-server-prepare))
3998
3999 (defun gnus-group-make-group (name method &optional address)
4000   "Add a new newsgroup.
4001 The user will be prompted for a NAME, for a select METHOD, and an
4002 ADDRESS."
4003   (interactive
4004    (cons 
4005     (read-string "Group name: ")
4006     (let ((method
4007            (completing-read 
4008             "Method: " (append gnus-valid-select-methods gnus-server-alist)
4009             nil t)))
4010       (if (assoc method gnus-valid-select-methods)
4011           (list method
4012                 (if (memq 'prompt-address
4013                           (assoc method gnus-valid-select-methods))
4014                     (read-string "Address: ")
4015                   ""))
4016         (list method nil)))))
4017   
4018   (let* ((meth (if address (list (intern method) address) method))
4019          (nname (gnus-group-prefixed-name name meth))
4020          info)
4021     (and (gnus-gethash nname gnus-newsrc-hashtb)
4022          (error "Group %s already exists" nname))
4023     (gnus-group-change-level 
4024      (setq info (list t nname gnus-level-default-subscribed nil nil meth))
4025      gnus-level-default-subscribed gnus-level-killed 
4026      (and (gnus-group-group-name)
4027           (gnus-gethash (gnus-group-group-name)
4028                         gnus-newsrc-hashtb))
4029      t)
4030     (gnus-sethash nname (cons 1 0) gnus-active-hashtb)
4031     (gnus-dribble-enter 
4032      (concat "(gnus-group-set-info '" (prin1-to-string (cdr info)) ")"))
4033     (gnus-group-insert-group-line-info nname)
4034
4035     (if (assoc method gnus-valid-select-methods)
4036         (require (intern method)))
4037     (and (gnus-check-backend-function 'request-create-group nname)
4038          (gnus-request-create-group nname))))
4039
4040 (defun gnus-group-edit-group (group &optional part)
4041   "Edit the group on the current line."
4042   (interactive (list (gnus-group-group-name)))
4043   (let ((done-func '(lambda () 
4044                       "Exit editing mode and update the information."
4045                       (interactive)
4046                       (gnus-group-edit-group-done 'part 'group)))
4047         (part (or part 'info))
4048         (winconf (current-window-configuration))
4049         info)
4050     (or group (error "No group on current line"))
4051     (or (setq info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
4052         (error "Killed group; can't be edited"))
4053     (set-buffer (get-buffer-create gnus-group-edit-buffer))
4054     (gnus-configure-windows 'edit-group)
4055     (gnus-add-current-to-buffer-list)
4056     (emacs-lisp-mode)
4057     ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4058     (use-local-map (copy-keymap emacs-lisp-mode-map))
4059     (local-set-key "\C-c\C-c" done-func)
4060     (make-local-variable 'gnus-prev-winconf)
4061     (setq gnus-prev-winconf winconf)
4062     ;; We modify the func to let it know what part it is editing.
4063     (setcar (cdr (nth 4 done-func)) (list 'quote part))
4064     (setcar (cdr (cdr (nth 4 done-func))) group)
4065     (erase-buffer)
4066     (insert
4067      (cond 
4068       ((eq part 'method)
4069        ";; Type `C-c C-c' after editing the select method.\n\n")
4070       ((eq part 'params)
4071        ";; Type `C-c C-c' after editing the group parameters.\n\n")
4072       ((eq part 'info)
4073        ";; Type `C-c C-c' after editing the group info.\n\n")))
4074     (let ((cinfo (gnus-copy-sequence info))
4075           marked)
4076       (if (not (setq marked (nth 3 cinfo)))
4077           ()
4078         (while marked
4079           (or (eq 'score (car (car marked)))
4080               (eq 'bookmark (car (car marked)))
4081               (eq 'killed (car (car marked)))
4082               (not (numberp (car (cdr (car marked)))))
4083               (setcdr (car marked) 
4084                       (gnus-compress-sequence (sort (cdr (car marked)) '<) t)))
4085           (setq marked (cdr marked))))
4086       (insert 
4087        (pp-to-string
4088         (cond ((eq part 'method)
4089                (or (nth 4 info) "native"))
4090               ((eq part 'params)
4091                (nth 5 info))
4092               (t
4093                cinfo)))
4094        "\n"))))
4095
4096 (defun gnus-group-edit-group-method (group)
4097   "Edit the select method of GROUP."
4098   (interactive (list (gnus-group-group-name)))
4099   (gnus-group-edit-group group 'method))
4100
4101 (defun gnus-group-edit-group-parameters (group)
4102   "Edit the group parameters of GROUP."
4103   (interactive (list (gnus-group-group-name)))
4104   (gnus-group-edit-group group 'params))
4105
4106 (defun gnus-group-edit-group-done (part group)
4107   "Get info from buffer, update variables and jump to the group buffer."
4108   (set-buffer (get-buffer-create gnus-group-edit-buffer))
4109   (goto-char (point-min))
4110   (let ((form (read (current-buffer)))
4111         (winconf gnus-prev-winconf))
4112     (if (eq part 'info) 
4113         (gnus-group-set-info form)
4114       (gnus-group-set-info form group part))
4115     (kill-buffer (current-buffer))
4116     (and winconf (set-window-configuration winconf))
4117     (set-buffer gnus-group-buffer)
4118     (gnus-group-update-group (gnus-group-group-name))
4119     (gnus-group-position-cursor)))
4120
4121 (defun gnus-group-make-help-group ()
4122   "Create the (ding) Gnus documentation group."
4123   (interactive)
4124   (let ((path load-path)
4125         name)
4126     (and (gnus-gethash (setq name (gnus-group-prefixed-name
4127                                    "gnus-help" '(nndoc "gnus-help")))
4128                        gnus-newsrc-hashtb)
4129          (error "Documentation group already exists"))
4130     (while (and path
4131                 (not (file-exists-p (concat (file-name-as-directory (car path))
4132                                             "doc.txt"))))
4133       (setq path (cdr path)))
4134     (or path (error "Couldn't find doc group"))
4135     (gnus-group-make-group 
4136      (gnus-group-real-name name)
4137      (list 'nndoc name
4138            (list 'nndoc-address (concat (file-name-as-directory (car path)) "doc.txt"))
4139            (list 'nndoc-article-type 'mbox))))
4140   (gnus-group-position-cursor))
4141
4142 (defun gnus-group-make-doc-group (file type)
4143   "Create a group that uses a single file as the source."
4144   (interactive 
4145    (list (read-file-name "File name: ") 
4146          (let ((err "")
4147                found char)
4148            (while (not found)
4149              (message "%sFile type (mbox, babyl, digest) [mbd]: " err)
4150              (setq found (cond ((= (setq char (read-char)) ?m) 'mbox)
4151                                ((= char ?b) 'babyl)
4152                                ((= char ?d) 'digest)
4153                                (t (setq err (format "%c unknown. " char))
4154                                   nil))))
4155            found)))
4156   (let* ((file (expand-file-name file))
4157          (name (gnus-generate-new-group-name
4158                 (gnus-group-prefixed-name
4159                  (file-name-nondirectory file) '(nndoc "")))))
4160     (gnus-group-make-group 
4161      (gnus-group-real-name name)
4162      (list 'nndoc name
4163            (list 'nndoc-address file)
4164            (list 'nndoc-article-type type)))))
4165
4166 (defun gnus-group-make-archive-group ()
4167   "Create the (ding) Gnus archive group."
4168   (interactive)
4169   (and (gnus-gethash (gnus-group-prefixed-name "ding.archives" '(nndir ""))
4170                      gnus-newsrc-hashtb)
4171        (error "Archive group already exists"))
4172   (gnus-group-make-group "ding.archives" "nndir" gnus-group-archive-directory)
4173   (gnus-group-position-cursor))
4174
4175 (defun gnus-group-make-directory-group (dir)
4176   "Create an nndir group.
4177 The user will be prompted for a directory. The contents of this
4178 directory will be used as a newsgroup. The directory should contain
4179 mail messages or news articles in files that have numeric names."
4180   (interactive
4181    (list (read-file-name "Create group from directory: ")))
4182   (or (file-exists-p dir) (error "No such directory"))
4183   (or (file-directory-p dir) (error "Not a directory"))
4184   (gnus-group-make-group dir "nndir" dir)
4185   (gnus-group-position-cursor))
4186
4187 (defun gnus-group-make-kiboze-group (group address scores)
4188   "Create an nnkiboze group.
4189 The user will be prompted for a name, a regexp to match groups, and
4190 score file entries for articles to include in the group."
4191   (interactive
4192    (list
4193     (read-string "nnkiboze group name: ")
4194     (read-string "Source groups (regexp): ")
4195     (let ((headers (mapcar (lambda (group) (list group))
4196                            '("subject" "from" "number" "date" "message-id"
4197                              "references" "chars" "lines" "xref")))
4198           scores header regexp regexps)
4199       (while (not (equal "" (setq header (completing-read 
4200                                           "Match on header: " headers nil t))))
4201         (setq regexps nil)
4202         (while (not (equal "" (setq regexp (read-string 
4203                                             (format "Match on %s (string): "
4204                                                     header)))))
4205           (setq regexps (cons (list regexp nil nil 'r) regexps)))
4206         (setq scores (cons (cons header regexps) scores)))
4207       scores)))
4208   (gnus-group-make-group group "nnkiboze" address)
4209   (save-excursion
4210     (gnus-set-work-buffer)
4211     (let (emacs-lisp-mode-hook)
4212       (pp scores (current-buffer)))
4213     (write-region (point-min) (point-max) 
4214                   (concat (or gnus-kill-files-directory "~/News")
4215                           "nnkiboze:" group "." gnus-score-file-suffix)))
4216   (gnus-group-position-cursor))
4217
4218 (defun gnus-group-add-to-virtual (n vgroup)
4219   "Add the current group to a virtual group."
4220   (interactive
4221    (list current-prefix-arg
4222          (completing-read "Add to virtual group: " gnus-newsrc-hashtb nil t
4223                           "nnvirtual:")))
4224   (or (eq (car (gnus-find-method-for-group vgroup)) 'nnvirtual)
4225       (error "%s is not an nnvirtual group" vgroup))
4226   (let* ((groups (gnus-group-process-prefix n))
4227          (method (nth 4 (nth 2 (gnus-gethash vgroup gnus-newsrc-hashtb)))))
4228     (setcar (cdr method)
4229             (concat 
4230              (nth 1 method) "\\|"
4231              (mapconcat 
4232               (lambda (s) 
4233                 (gnus-group-remove-mark s)
4234                 (concat "\\(^" (regexp-quote s) "$\\)"))
4235               groups "\\|"))))
4236   (gnus-group-position-cursor))
4237
4238 (defun gnus-group-make-empty-virtual (group)
4239   "Create a new, fresh, empty virtual group."
4240   (interactive "sCreate new, empty virtual group: ")
4241   (let* ((method (list 'nnvirtual "^$"))
4242          (pgroup (gnus-group-prefixed-name group method)))
4243     ;; Check whether it exists already.
4244     (and (gnus-gethash pgroup gnus-newsrc-hashtb)
4245          (error "Group %s already exists." pgroup))
4246     ;; Subscribe the new group after the group on the current line.
4247     (gnus-subscribe-group pgroup (gnus-group-group-name) method)
4248     (gnus-group-update-group pgroup)
4249     (forward-line -1)
4250     (gnus-group-position-cursor)))
4251
4252 (defun gnus-group-enter-directory (dir)
4253   "Enter an ephemeral nneething group."
4254   (interactive "DDirectory to read: ")
4255   (let* ((method (list 'nneething dir))
4256          (leaf (gnus-group-prefixed-name
4257                 (file-name-nondirectory (directory-file-name dir))
4258                 method))
4259          (name (gnus-generate-new-group-name leaf)))
4260     (let ((nneething-read-only t))
4261       (or (gnus-group-read-ephemeral-group 
4262            name method t
4263            (cons (current-buffer) (if (eq major-mode 'gnus-summary-mode)
4264                                       'summary 'group)))
4265           (error "Couldn't enter %s" dir)))))
4266
4267 ;; Group sorting commands
4268 ;; Suggested by Joe Hildebrand <hildjj@idaho.fuentez.com>.
4269
4270 (defun gnus-group-sort-groups ()
4271   "Sort the group buffer using `gnus-group-sort-function'."
4272   (interactive)
4273   (setq gnus-newsrc-alist 
4274         (sort (cdr gnus-newsrc-alist) gnus-group-sort-function))
4275   (gnus-make-hashtable-from-newsrc-alist)
4276   (gnus-group-list-groups))
4277
4278 (defun gnus-group-sort-by-alphabet (info1 info2)
4279   (string< (car info1) (car info2)))
4280
4281 (defun gnus-group-sort-by-unread (info1 info2)
4282   (let ((n1 (car (gnus-gethash (car info1) gnus-newsrc-hashtb)))
4283         (n2 (car (gnus-gethash (car info2) gnus-newsrc-hashtb))))
4284     (< (or (and (numberp n1) n1) 0)
4285        (or (and (numberp n2) n2) 0))))
4286
4287 (defun gnus-group-sort-by-level (info1 info2)
4288   (< (nth 1 info1) (nth 1 info2)))
4289
4290 ;; Group catching up.
4291
4292 (defun gnus-group-catchup-current (&optional n all)
4293   "Mark all articles not marked as unread in current newsgroup as read.
4294 If prefix argument N is numeric, the ARG next newsgroups will be
4295 caught up. If ALL is non-nil, marked articles will also be marked as
4296 read. Cross references (Xref: header) of articles are ignored.
4297 The difference between N and actual number of newsgroups that were
4298 caught up is returned."
4299   (interactive "P")
4300   (if (not (or (not gnus-interactive-catchup) ;Without confirmation?
4301                gnus-expert-user
4302                (gnus-y-or-n-p
4303                 (if all
4304                     "Do you really want to mark all articles as read? "
4305                   "Mark all unread articles as read? "))))
4306       n
4307     (let ((groups (gnus-group-process-prefix n))
4308           (ret 0))
4309       (while groups
4310         ;; Virtual groups have to be given special treatment. 
4311         (let ((method (gnus-find-method-for-group (car groups))))
4312           (if (eq 'nnvirtual (car method))
4313               (nnvirtual-catchup-group
4314                (gnus-group-real-name (car groups)) (nth 1 method) all)))
4315         (gnus-group-remove-mark (car groups))
4316         (if (prog1
4317                 (gnus-group-goto-group (car groups))
4318               (gnus-group-catchup (car groups) all))
4319             (gnus-group-update-group-line)
4320           (setq ret (1+ ret)))
4321         (setq groups (cdr groups)))
4322       (gnus-group-next-unread-group 1)
4323       ret)))
4324
4325 (defun gnus-group-catchup-current-all (&optional n)
4326   "Mark all articles in current newsgroup as read.
4327 Cross references (Xref: header) of articles are ignored."
4328   (interactive "P")
4329   (gnus-group-catchup-current n 'all))
4330
4331 (defun gnus-group-catchup (group &optional all)
4332   "Mark all articles in GROUP as read.
4333 If ALL is non-nil, all articles are marked as read.
4334 The return value is the number of articles that were marked as read,
4335 or nil if no action could be taken."
4336   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4337          (num (car entry))
4338          (marked (nth 3 (nth 2 entry))))
4339     (if (not (numberp (car entry)))
4340         (gnus-message 1 "Can't catch up; non-active group")
4341       ;; Do the updating only if the newsgroup isn't killed.
4342       (if (not entry)
4343           ()
4344         (gnus-update-read-articles 
4345          group (and (not all) (append (cdr (assq 'tick marked))
4346                                       (cdr (assq 'dormant marked))))
4347          nil (and (not all) (cdr (assq 'tick marked))))
4348         (and all 
4349              (setq marked (nth 3 (nth 2 entry)))
4350              (setcar (nthcdr 3 (nth 2 entry)) 
4351                      (delq (assq 'dormant marked) 
4352                            (nth 3 (nth 2 entry)))))))
4353     num))
4354
4355 (defun gnus-group-expire-articles (&optional n)
4356   "Expire all expirable articles in the current newsgroup."
4357   (interactive "P")
4358   (let ((groups (gnus-group-process-prefix n))
4359         group)
4360     (or groups (error "No groups to expire"))
4361     (while groups
4362       (setq group (car groups)
4363             groups (cdr groups))
4364       (gnus-group-remove-mark group)
4365       (if (not (gnus-check-backend-function 'request-expire-articles group))
4366           ()
4367         (let* ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
4368                (expirable (if (memq 'total-expire (nth 5 info))
4369                               (cons nil (gnus-list-of-read-articles group))
4370                             (assq 'expire (nth 3 info)))))
4371           (and expirable 
4372                (setcdr expirable
4373                        (gnus-request-expire-articles 
4374                         (cdr expirable) group))))))))
4375
4376 (defun gnus-group-expire-all-groups ()
4377   "Expire all expirable articles in all newsgroups."
4378   (interactive)
4379   (save-excursion
4380     (gnus-message 5 "Expiring...")
4381     (let ((gnus-group-marked (mapcar (lambda (info) (car info))
4382                                      (cdr gnus-newsrc-alist))))
4383       (gnus-group-expire-articles nil)))
4384   (gnus-group-position-cursor)
4385   (gnus-message 5 "Expiring...done"))
4386
4387 (defun gnus-group-set-current-level (n level)
4388   "Set the level of the next N groups to LEVEL."
4389   (interactive "P\nnLevel: ")
4390   (or (and (>= level 1) (<= level gnus-level-killed))
4391       (error "Illegal level: %d" level))
4392   (let ((groups (gnus-group-process-prefix n))
4393         group)
4394     (while groups
4395       (setq group (car groups)
4396             groups (cdr groups))
4397       (gnus-group-remove-mark group)
4398       (gnus-message 6 "Changed level of %s from %d to %d" 
4399                     group (gnus-group-group-level) level)
4400       (gnus-group-change-level group level
4401                                (gnus-group-group-level))
4402       (gnus-group-update-group-line)))
4403   (gnus-group-position-cursor))
4404
4405 (defun gnus-group-unsubscribe-current-group (&optional n)
4406   "Toggle subscription of the current group.
4407 If given numerical prefix, toggle the N next groups."
4408   (interactive "P")
4409   (let ((groups (gnus-group-process-prefix n))
4410         group)
4411     (while groups
4412       (setq group (car groups)
4413             groups (cdr groups))
4414       (gnus-group-remove-mark group)
4415       (gnus-group-unsubscribe-group
4416        group (if (<= (gnus-group-group-level) gnus-level-subscribed)
4417                  gnus-level-default-unsubscribed
4418                gnus-level-default-subscribed))
4419       (gnus-group-update-group-line))
4420     (gnus-group-next-group 1)))
4421
4422 (defun gnus-group-unsubscribe-group (group &optional level)
4423   "Toggle subscribe from/to unsubscribe GROUP.
4424 New newsgroup is added to .newsrc automatically."
4425   (interactive
4426    (list (completing-read
4427           "Group: " gnus-active-hashtb nil 
4428           (memq gnus-select-method gnus-have-read-active-file))))
4429   (let ((newsrc (gnus-gethash group gnus-newsrc-hashtb)))
4430     (cond (newsrc
4431            ;; Toggle subscription flag.
4432            (gnus-group-change-level 
4433             newsrc (if level level (if (<= (nth 1 (nth 2 newsrc)) 
4434                                            gnus-level-subscribed) 
4435                                        (1+ gnus-level-subscribed)
4436                                      gnus-level-default-subscribed)))
4437            (gnus-group-update-group group))
4438           ((and (stringp group)
4439                 (or (not (memq gnus-select-method gnus-have-read-active-file))
4440                     (gnus-gethash group gnus-active-hashtb)))
4441            ;; Add new newsgroup.
4442            (gnus-group-change-level 
4443             group 
4444             (if level level gnus-level-default-subscribed) 
4445             (or (and (member group gnus-zombie-list) 
4446                      gnus-level-zombie) 
4447                 gnus-level-killed)
4448             (and (gnus-group-group-name)
4449                  (gnus-gethash (gnus-group-group-name) gnus-newsrc-hashtb)))
4450            (gnus-group-update-group group))
4451           (t (error "No such newsgroup: %s" group)))
4452     (gnus-group-position-cursor)))
4453
4454 (defun gnus-group-transpose-groups (n)
4455   "Move the current newsgroup up N places.
4456 If given a negative prefix, move down instead. The difference between
4457 N and the number of steps taken is returned." 
4458   (interactive "p")
4459   (or (gnus-group-group-name)
4460       (error "No group on current line"))
4461   (gnus-group-kill-group 1)
4462   (prog1
4463       (forward-line (- n))
4464     (gnus-group-yank-group)
4465     (gnus-group-position-cursor)))
4466
4467 (defun gnus-group-kill-all-zombies ()
4468   "Kill all zombie newsgroups."
4469   (interactive)
4470   (setq gnus-killed-list (nconc gnus-zombie-list gnus-killed-list))
4471   (setq gnus-zombie-list nil)
4472   (gnus-group-list-groups))
4473
4474 (defun gnus-group-kill-region (begin end)
4475   "Kill newsgroups in current region (excluding current point).
4476 The killed newsgroups can be yanked by using \\[gnus-group-yank-group]."
4477   (interactive "r")
4478   (let ((lines
4479          ;; Count lines.
4480          (save-excursion
4481            (count-lines
4482             (progn
4483               (goto-char begin)
4484               (beginning-of-line)
4485               (point))
4486             (progn
4487               (goto-char end)
4488               (beginning-of-line)
4489               (point))))))
4490     (goto-char begin)
4491     (beginning-of-line)                 ;Important when LINES < 1
4492     (gnus-group-kill-group lines)))
4493
4494 (defun gnus-group-kill-group (&optional n)
4495   "The the next N groups.
4496 The killed newsgroups can be yanked by using \\[gnus-group-yank-group].
4497 However, only groups that were alive can be yanked; already killed 
4498 groups or zombie groups can't be yanked.
4499 The return value is the name of the (last) group that was killed."
4500   (interactive "P")
4501   (let ((buffer-read-only nil)
4502         (groups (gnus-group-process-prefix n))
4503         group entry level)
4504     (while groups
4505       (setq group (car groups)
4506             groups (cdr groups))
4507       (gnus-group-remove-mark group)
4508       (setq level (gnus-group-group-level))
4509       (gnus-delete-line)
4510       (if (setq entry (gnus-gethash group gnus-newsrc-hashtb))
4511           (setq gnus-list-of-killed-groups 
4512                 (cons (cons (car entry) (nth 2 entry)) 
4513                       gnus-list-of-killed-groups)))
4514       (gnus-group-change-level 
4515        (if entry entry group) gnus-level-killed (if entry nil level)))
4516     (gnus-group-position-cursor)
4517     group))
4518
4519 (defun gnus-group-yank-group (&optional arg)
4520   "Yank the last newsgroups killed with \\[gnus-group-kill-group],
4521 inserting it before the current newsgroup.  The numeric ARG specifies
4522 how many newsgroups are to be yanked.  The name of the (last)
4523 newsgroup yanked is returned."
4524   (interactive "p")
4525   (if (not arg) (setq arg 1))
4526   (let (info group prev)
4527     (while (>= (setq arg (1- arg)) 0)
4528       (if (not (setq info (car gnus-list-of-killed-groups)))
4529           (error "No more newsgroups to yank"))
4530       (setq group (nth 2 info))
4531       ;; Find which newsgroup to insert this one before - search
4532       ;; backward until something suitable is found. If there are no
4533       ;; other newsgroups in this buffer, just make this newsgroup the
4534       ;; first newsgroup.
4535       (setq prev (gnus-group-group-name))
4536       (gnus-group-change-level 
4537        info (nth 2 info) gnus-level-killed 
4538        (and prev (gnus-gethash prev gnus-newsrc-hashtb))
4539        t)
4540       (gnus-group-insert-group-line-info (nth 1 info))
4541       (setq gnus-list-of-killed-groups 
4542             (cdr gnus-list-of-killed-groups)))
4543     (forward-line -1)
4544     (gnus-group-position-cursor)
4545     group))
4546       
4547 (defun gnus-group-list-all-groups (&optional arg)
4548   "List all newsgroups with level ARG or lower.
4549 Default is gnus-level-unsubscribed, which lists all subscribed and most
4550 unsubscribed groups."
4551   (interactive "P")
4552   (gnus-group-list-groups (or arg gnus-level-unsubscribed) t))
4553
4554 (defun gnus-group-list-killed ()
4555   "List all killed newsgroups in the group buffer."
4556   (interactive)
4557   (if (not gnus-killed-list)
4558       (gnus-message 6 "No killed groups")
4559     (let (gnus-group-list-mode)
4560       (funcall gnus-group-prepare-function 
4561                gnus-level-killed t gnus-level-killed))
4562     (goto-char (point-min)))
4563   (gnus-group-position-cursor))
4564
4565 (defun gnus-group-list-zombies ()
4566   "List all zombie newsgroups in the group buffer."
4567   (interactive)
4568   (if (not gnus-zombie-list)
4569       (gnus-message 6 "No zombie groups")
4570     (let (gnus-group-list-mode)
4571       (funcall gnus-group-prepare-function
4572                gnus-level-zombie t gnus-level-zombie))
4573     (goto-char (point-min)))
4574   (gnus-group-position-cursor))
4575
4576 (defun gnus-group-get-new-news (&optional arg)
4577   "Get newly arrived articles.
4578 If ARG is non-nil, it should be a number between one and nine to
4579 specify which levels you are interested in re-scanning."
4580   (interactive "P")
4581   (run-hooks 'gnus-get-new-news-hook)
4582   (setq arg (gnus-group-default-level arg t))
4583   (if (and gnus-read-active-file (not arg))
4584       (progn
4585         (gnus-read-active-file)
4586         (gnus-get-unread-articles (or arg (1+ gnus-level-subscribed))))
4587     (let ((gnus-read-active-file (not arg))
4588           (gnus-have-read-active-file 
4589            (and (not arg) gnus-have-read-active-file)))
4590       (gnus-get-unread-articles (or arg (1+ gnus-level-subscribed)))))
4591   (gnus-group-list-groups))
4592
4593 (defun gnus-group-get-new-news-this-group (&optional n)
4594   "Check for newly arrived news in the current group (and the N-1 next groups).
4595 The difference between N and the number of newsgroup checked is returned.
4596 If N is negative, this group and the N-1 previous groups will be checked."
4597   (interactive "P")
4598   (let* ((groups (gnus-group-process-prefix n))
4599          (ret (if (numberp n) (- n (length groups)) 0))
4600          group)
4601     (while groups
4602       (setq group (car groups)
4603             groups (cdr groups))
4604       (gnus-group-remove-mark group)
4605       (or (gnus-get-new-news-in-group group)
4606           (progn 
4607             (ding) 
4608             (message "%s error: %s" group (gnus-status-message group))
4609             (sit-for 2))))
4610     (gnus-group-next-unread-group 1 t)
4611     (gnus-summary-position-cursor)
4612     ret))
4613
4614 (defun gnus-get-new-news-in-group (group)
4615   (and group 
4616        (gnus-activate-group group)
4617        (progn
4618          (gnus-get-unread-articles-in-group 
4619           (nth 2 (gnus-gethash group gnus-newsrc-hashtb))
4620           (gnus-gethash group gnus-active-hashtb))
4621          (gnus-group-update-group-line)
4622          t)))
4623
4624 (defun gnus-group-fetch-faq (group)
4625   "Fetch the FAQ for the current group."
4626   (interactive (list (gnus-group-real-name (gnus-group-group-name))))
4627   (or group (error "No group name given"))
4628   (let ((file (concat gnus-group-faq-directory group))) 
4629     (if (not (file-exists-p file))
4630         (error "No such file: %s" file)
4631       (find-file file))))
4632   
4633 (defun gnus-group-describe-group (force &optional group)
4634   "Display a description of the current newsgroup."
4635   (interactive (list current-prefix-arg (gnus-group-group-name)))
4636   (and force (setq gnus-description-hashtb nil))
4637   (let ((method (gnus-find-method-for-group group))
4638         desc)
4639     (or group (error "No group name given"))
4640     (and (or (and gnus-description-hashtb
4641                   ;; We check whether this group's method has been
4642                   ;; queried for a description file.  
4643                   (gnus-gethash 
4644                    (gnus-group-prefixed-name "" method) 
4645                    gnus-description-hashtb))
4646              (setq desc (gnus-group-get-description group))
4647              (gnus-read-descriptions-file method))
4648          (message
4649           (or desc (gnus-gethash group gnus-description-hashtb)
4650               "No description available")))))
4651
4652 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
4653 (defun gnus-group-describe-all-groups (&optional force)
4654   "Pop up a buffer with descriptions of all newsgroups."
4655   (interactive "P")
4656   (and force (setq gnus-description-hashtb nil))
4657   (if (not (or gnus-description-hashtb
4658                (gnus-read-all-descriptions-files)))
4659       (error "Couldn't request descriptions file"))
4660   (let ((buffer-read-only nil)
4661         b)
4662     (erase-buffer)
4663     (mapatoms
4664      (lambda (group)
4665        (setq b (point))
4666        (insert (format "      *: %-20s %s\n" (symbol-name group)
4667                        (symbol-value group)))
4668        (add-text-properties 
4669         b (1+ b) (list 'gnus-group group
4670                        'gnus-unread t 'gnus-marked nil
4671                        'gnus-level (1+ gnus-level-subscribed))))
4672      gnus-description-hashtb)
4673     (goto-char (point-min))
4674     (gnus-group-position-cursor)))
4675
4676 ;; Suggested by by Daniel Quinlan <quinlan@best.com>.
4677 (defun gnus-group-apropos (regexp &optional search-description)
4678   "List all newsgroups that have names that match a regexp."
4679   (interactive "sGnus apropos (regexp): ")
4680   (let ((prev "")
4681         (obuf (current-buffer))
4682         groups des)
4683     ;; Go through all newsgroups that are known to Gnus.
4684     (mapatoms 
4685      (lambda (group)
4686        (and (symbol-name group)
4687             (string-match regexp (symbol-name group))
4688             (setq groups (cons (symbol-name group) groups))))
4689      gnus-active-hashtb)
4690     ;; Go through all descriptions that are known to Gnus. 
4691     (if search-description
4692         (mapatoms 
4693          (lambda (group)
4694            (and (string-match regexp (symbol-value group))
4695                 (gnus-gethash (symbol-name group) gnus-active-hashtb)
4696                 (setq groups (cons (symbol-name group) groups))))
4697          gnus-description-hashtb))
4698     (if (not groups)
4699         (gnus-message 3 "No groups matched \"%s\"." regexp)
4700       ;; Print out all the groups.
4701       (save-excursion
4702         (pop-to-buffer "*Gnus Help*")
4703         (buffer-disable-undo (current-buffer))
4704         (erase-buffer)
4705         (setq groups (sort groups 'string<))
4706         (while groups
4707           ;; Groups may be entered twice into the list of groups.
4708           (if (not (string= (car groups) prev))
4709               (progn
4710                 (insert (setq prev (car groups)) "\n")
4711                 (if (and gnus-description-hashtb
4712                          (setq des (gnus-gethash (car groups) 
4713                                                  gnus-description-hashtb)))
4714                     (insert "  " des "\n"))))
4715           (setq groups (cdr groups)))
4716         (goto-char (point-min))))
4717     (pop-to-buffer obuf)))
4718
4719 (defun gnus-group-description-apropos (regexp)
4720   "List all newsgroups that have names or descriptions that match a regexp."
4721   (interactive "sGnus description apropos (regexp): ")
4722   (if (not (or gnus-description-hashtb
4723                (gnus-read-all-descriptions-files)))
4724       (error "Couldn't request descriptions file"))
4725   (gnus-group-apropos regexp t))
4726
4727 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
4728 (defun gnus-group-list-matching (level regexp &optional all lowest) 
4729   "List all groups with unread articles that match REGEXP.
4730 If the prefix LEVEL is non-nil, it should be a number that says which
4731 level to cut off listing groups. 
4732 If ALL, also list groups with no unread articles.
4733 If LOWEST, don't list groups with level lower than LOWEST."
4734   (interactive "P\nsList newsgroups matching: ")
4735   (gnus-group-prepare-flat (or level gnus-level-subscribed)
4736                            all (or lowest 1) regexp)
4737   (goto-char (point-min))
4738   (gnus-group-position-cursor))
4739
4740 (defun gnus-group-list-all-matching (level regexp &optional lowest) 
4741   "List all groups that match REGEXP.
4742 If the prefix LEVEL is non-nil, it should be a number that says which
4743 level to cut off listing groups. 
4744 If LOWEST, don't list groups with level lower than LOWEST."
4745   (interactive "P\nsList newsgroups matching: ")
4746   (gnus-group-list-matching (or level gnus-level-killed) regexp t lowest))
4747
4748 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
4749 (defun gnus-group-save-newsrc ()
4750   "Save the Gnus startup files."
4751   (interactive)
4752   (gnus-save-newsrc-file))
4753
4754 (defun gnus-group-restart (&optional arg)
4755   "Force Gnus to read the .newsrc file."
4756   (interactive "P")
4757   (gnus-save-newsrc-file)
4758   (gnus-setup-news 'force)
4759   (gnus-group-list-groups arg))
4760
4761 (defun gnus-group-read-init-file ()
4762   "Read the Gnus elisp init file."
4763   (interactive)
4764   (gnus-read-init-file))
4765
4766 (defun gnus-group-check-bogus-groups (&optional silent)
4767   "Check bogus newsgroups.
4768 If given a prefix, don't ask for confirmation before removing a bogus
4769 group."
4770   (interactive "P")
4771   (gnus-check-bogus-newsgroups (and (not silent) (not gnus-expert-user)))
4772   (gnus-group-list-groups))
4773
4774 (defun gnus-group-edit-global-kill (&optional article group)
4775   "Edit the global kill file.
4776 If GROUP, edit that local kill file instead."
4777   (interactive "P")
4778   (setq gnus-current-kill-article article)
4779   (gnus-kill-file-edit-file group)
4780   (gnus-message 6
4781    (substitute-command-keys
4782     "Editing a global kill file (Type \\[gnus-kill-file-exit] to exit)")))
4783
4784 (defun gnus-group-edit-local-kill (article group)
4785   "Edit a local kill file."
4786   (interactive (list nil (gnus-group-group-name)))
4787   (gnus-group-edit-global-kill article group))
4788
4789 (defun gnus-group-force-update ()
4790   "Update `.newsrc' file."
4791   (interactive)
4792   (gnus-save-newsrc-file))
4793
4794 (defun gnus-group-suspend ()
4795   "Suspend the current Gnus session.
4796 In fact, cleanup buffers except for group mode buffer.
4797 The hook gnus-suspend-gnus-hook is called before actually suspending."
4798   (interactive)
4799   (run-hooks 'gnus-suspend-gnus-hook)
4800   ;; Kill Gnus buffers except for group mode buffer.
4801   (let ((group-buf (get-buffer gnus-group-buffer)))
4802     ;; Do this on a separate list in case the user does a ^G before we finish
4803     (let ((gnus-buffer-list
4804            (delq group-buf (delq gnus-dribble-buffer
4805                                  (append gnus-buffer-list nil)))))
4806       (while gnus-buffer-list
4807         (gnus-kill-buffer (car gnus-buffer-list))
4808         (setq gnus-buffer-list (cdr gnus-buffer-list))))
4809     (if group-buf
4810         (progn
4811           (setq gnus-buffer-list (list group-buf))
4812           (bury-buffer group-buf)
4813           (delete-windows-on group-buf t)))))
4814
4815 (defun gnus-group-clear-dribble ()
4816   "Clear all information from the dribble buffer."
4817   (interactive)
4818   (gnus-dribble-clear))
4819
4820 (defun gnus-group-exit ()
4821   "Quit reading news after updating .newsrc.eld and .newsrc.
4822 The hook `gnus-exit-gnus-hook' is called before actually exiting."
4823   (interactive)
4824   (if (or noninteractive                ;For gnus-batch-kill
4825           (zerop (buffer-size))         ;No news is good news.
4826           (not (gnus-server-opened gnus-select-method)) ;NNTP connection closed
4827           (not gnus-interactive-exit)   ;Without confirmation
4828           gnus-expert-user
4829           (gnus-y-or-n-p "Are you sure you want to quit reading news? "))
4830       (progn
4831         (run-hooks 'gnus-exit-gnus-hook)
4832         ;; Offer to save data from non-quitted summary buffers.
4833         (gnus-offer-save-summaries)
4834         ;; Save the newsrc file(s).
4835         (gnus-save-newsrc-file)
4836         ;; Kill-em-all.
4837         (gnus-close-backends)
4838         ;; Reset everything.
4839         (gnus-clear-system))))
4840
4841 (defun gnus-close-backends ()
4842   ;; Send a close request to all backends that support such a request. 
4843   (let ((methods gnus-valid-select-methods)
4844         func)
4845     (while methods
4846       (if (fboundp (setq func (intern (concat (car (car methods))
4847                                               "-request-close"))))
4848           (funcall func))
4849       (setq methods (cdr methods)))))
4850
4851 (defun gnus-group-quit ()
4852   "Quit reading news without updating .newsrc.eld or .newsrc.
4853 The hook `gnus-exit-gnus-hook' is called before actually exiting."
4854   (interactive)
4855   (if (or noninteractive                ;For gnus-batch-kill
4856           (zerop (buffer-size))
4857           (not (gnus-server-opened gnus-select-method))
4858           gnus-expert-user
4859           (not gnus-current-startup-file)
4860           (gnus-yes-or-no-p
4861            (format "Quit reading news without saving %s? "
4862                    (file-name-nondirectory gnus-current-startup-file))))
4863       (progn
4864         (run-hooks 'gnus-exit-gnus-hook)
4865         (if gnus-use-full-window
4866             (delete-other-windows)
4867           (gnus-remove-some-windows))
4868         (gnus-dribble-save)
4869         (gnus-close-backends)
4870         (gnus-clear-system))))
4871
4872 (defun gnus-offer-save-summaries ()
4873   (let ((buffers (buffer-list))
4874         answer)
4875     (save-excursion
4876       (while (and buffers (not (eq answer ?q)))
4877         (and 
4878          ;; We look for buffers with "Summary" in the name.
4879          (string-match "Summary" (or (buffer-name (car buffers)) ""))
4880          (progn
4881            (set-buffer (car buffers))
4882            ;; We check that this is, indeed, a summary buffer.
4883            (eq major-mode 'gnus-summary-mode)) 
4884          ;; We ask the user whether she wants to save the info.
4885          (or (eq answer ?!)
4886              (progn
4887                (setq answer nil)
4888                (while (not (memq answer '(?y ?n ?! ?q)))
4889                  (message (format "%sUpdate summary buffer %s? (y, n, !, q)"
4890                                   (if answer "Illegal char. " "")
4891                                   (buffer-name)))
4892                  (setq answer (read-char)))
4893                (or (eq answer ?y) (eq answer ?!))))
4894          ;; We do it by simply exiting.
4895          (gnus-summary-exit))
4896         (setq buffers (cdr buffers))))))
4897
4898 (defun gnus-group-describe-briefly ()
4899   "Give a one line description of the group mode commands."
4900   (interactive)
4901   (gnus-message 7 (substitute-command-keys "\\<gnus-group-mode-map>\\[gnus-group-read-group]:Select  \\[gnus-group-next-unread-group]:Forward  \\[gnus-group-prev-unread-group]:Backward  \\[gnus-group-exit]:Exit  \\[gnus-info-find-node]:Run Info  \\[gnus-group-describe-briefly]:This help")))
4902
4903 (defun gnus-group-browse-foreign-server (method)
4904   "Browse a foreign news server.
4905 If called interactively, this function will ask for a select method
4906  (nntp, nnspool, etc.) and a server address (eg. nntp.some.where). 
4907 If not, METHOD should be a list where the first element is the method
4908 and the second element is the address."
4909   (interactive
4910    (list (let ((how (completing-read 
4911                      "Which backend: "
4912                      (append gnus-valid-select-methods gnus-server-alist)
4913                      nil t "nntp")))
4914            ;; We either got a backend name or a virtual server name.
4915            ;; If the first, we also need an address.
4916            (if (assoc how gnus-valid-select-methods)
4917                (list (intern how)
4918                      ;; Suggested by mapjph@bath.ac.uk.
4919                      (completing-read 
4920                       "Address: " 
4921                       (mapcar (lambda (server) (list server))
4922                               gnus-secondary-servers)))
4923              ;; We got a server name, so we find the method.
4924              (gnus-server-to-method how)))))
4925   (gnus-browse-foreign-server method))
4926
4927 \f
4928 ;;;
4929 ;;; Browse Server Mode
4930 ;;;
4931
4932 (defvar gnus-browse-mode-hook nil)
4933 (defvar gnus-browse-mode-map nil)
4934 (put 'gnus-browse-mode 'mode-class 'special)
4935
4936 (if gnus-browse-mode-map
4937     nil
4938   (setq gnus-browse-mode-map (make-keymap))
4939   (suppress-keymap gnus-browse-mode-map)
4940   (define-key gnus-browse-mode-map " " 'gnus-browse-read-group)
4941   (define-key gnus-browse-mode-map "=" 'gnus-browse-select-group)
4942   (define-key gnus-browse-mode-map "n" 'gnus-browse-next-group)
4943   (define-key gnus-browse-mode-map "p" 'gnus-browse-prev-group)
4944   (define-key gnus-browse-mode-map "\177" 'gnus-browse-prev-group)
4945   (define-key gnus-browse-mode-map "N" 'gnus-browse-next-group)
4946   (define-key gnus-browse-mode-map "P" 'gnus-browse-prev-group)
4947   (define-key gnus-browse-mode-map "\M-n" 'gnus-browse-next-group)
4948   (define-key gnus-browse-mode-map "\M-p" 'gnus-browse-prev-group)
4949   (define-key gnus-browse-mode-map "\r" 'gnus-browse-select-group)
4950   (define-key gnus-browse-mode-map "u" 'gnus-browse-unsubscribe-current-group)
4951   (define-key gnus-browse-mode-map "l" 'gnus-browse-exit)
4952   (define-key gnus-browse-mode-map "L" 'gnus-browse-exit)
4953   (define-key gnus-browse-mode-map "q" 'gnus-browse-exit)
4954   (define-key gnus-browse-mode-map "Q" 'gnus-browse-exit)
4955   (define-key gnus-browse-mode-map "\C-c\C-c" 'gnus-browse-exit)
4956   (define-key gnus-browse-mode-map "?" 'gnus-browse-describe-briefly)
4957   (define-key gnus-browse-mode-map "\C-c\C-i" 'gnus-info-find-node)
4958   )
4959
4960 (defvar gnus-browse-current-method nil)
4961 (defvar gnus-browse-return-buffer nil)
4962
4963 (defvar gnus-browse-buffer "*Gnus Browse Server*")
4964
4965 (defun gnus-browse-foreign-server (method &optional return-buffer)
4966   (setq gnus-browse-current-method method)
4967   (setq gnus-browse-return-buffer return-buffer)
4968   (let ((gnus-select-method method)
4969         groups group)
4970     (gnus-message 5 "Connecting to %s..." (nth 1 method))
4971     (or (gnus-check-server method)
4972         (error "Unable to contact server: %s" (gnus-status-message method)))
4973     (or (gnus-request-list method)
4974         (error "Couldn't request list: %s" (gnus-status-message method)))
4975     (get-buffer-create gnus-browse-buffer)
4976     (gnus-add-current-to-buffer-list)
4977     (and gnus-carpal (gnus-carpal-setup-buffer 'browse))
4978     (gnus-configure-windows 'browse)
4979     (buffer-disable-undo (current-buffer))
4980     (let ((buffer-read-only nil))
4981       (erase-buffer))
4982     (gnus-browse-mode)
4983     (setq mode-line-buffer-identification
4984           (format
4985            "(ding) Browse Server {%s:%s}" (car method) (car (cdr method))))
4986     (save-excursion
4987       (set-buffer nntp-server-buffer)
4988       (let ((cur (current-buffer)))
4989         (goto-char (point-min))
4990         (or (string= gnus-ignored-newsgroups "")
4991             (delete-matching-lines gnus-ignored-newsgroups))
4992         (while (re-search-forward 
4993                 "\\(^[^ \t]+\\)[ \t]+[0-9]+[ \t]+[0-9]+" nil t)
4994           (goto-char (match-end 1))
4995           (setq groups (cons (cons (buffer-substring (match-beginning 1)
4996                                                      (match-end 1))
4997                                    (max 0 (- (1+ (read cur)) (read cur))))
4998                              groups)))))
4999     (setq groups (sort groups 
5000                        (lambda (l1 l2)
5001                          (string< (car l1) (car l2)))))
5002     (let ((buffer-read-only nil))
5003       (while groups
5004         (setq group (car groups))
5005         (insert 
5006          (format "K%7d: %s\n" (cdr group) (car group)))
5007         (setq groups (cdr groups))))
5008     (switch-to-buffer (current-buffer))
5009     (goto-char (point-min))
5010     (gnus-group-position-cursor)))
5011
5012 (defun gnus-browse-mode ()
5013   "Major mode for browsing a foreign server.
5014
5015 All normal editing commands are switched off.
5016
5017 \\<gnus-browse-mode-map>
5018 The only things you can do in this buffer is
5019
5020 1) `\\[gnus-browse-unsubscribe-current-group]' to subscribe to a group.
5021 The group will be inserted into the group buffer upon exit from this
5022 buffer.  
5023
5024 2) `\\[gnus-browse-read-group]' to read a group ephemerally.
5025
5026 3) `\\[gnus-browse-exit]' to return to the group buffer."
5027   (interactive)
5028   (kill-all-local-variables)
5029   (if gnus-visual (gnus-browse-make-menu-bar))
5030   (setq mode-line-modified "-- ")
5031   (make-local-variable 'mode-line-format)
5032   (setq mode-line-format (copy-sequence mode-line-format))
5033   (and (equal (nth 3 mode-line-format) "   ")
5034        (setcar (nthcdr 3 mode-line-format) ""))
5035   (setq major-mode 'gnus-browse-mode)
5036   (setq mode-name "Browse Server")
5037   (setq mode-line-process nil)
5038   (use-local-map gnus-browse-mode-map)
5039   (buffer-disable-undo (current-buffer))
5040   (setq truncate-lines t)
5041   (setq buffer-read-only t)
5042   (run-hooks 'gnus-browse-mode-hook))
5043
5044 (defun gnus-browse-read-group (&optional no-article)
5045   "Enter the group at the current line."
5046   (interactive)
5047   (let ((group (gnus-browse-group-name)))
5048     (or (gnus-group-read-ephemeral-group 
5049          group gnus-browse-current-method nil
5050          (cons (current-buffer) 'browse))
5051         (error "Couldn't enter %s" group))))
5052
5053 (defun gnus-browse-select-group ()
5054   "Select the current group."
5055   (interactive)
5056   (gnus-browse-read-group 'no))
5057
5058 (defun gnus-browse-next-group (n)
5059   "Go to the next group."
5060   (interactive "p")
5061   (prog1
5062       (forward-line n)
5063     (gnus-group-position-cursor)))
5064
5065 (defun gnus-browse-prev-group (n)
5066   "Go to the next group."
5067   (interactive "p")
5068   (gnus-browse-next-group (- n)))
5069
5070 (defun gnus-browse-unsubscribe-current-group (arg)
5071   "(Un)subscribe to the next ARG groups."
5072   (interactive "p")
5073   (and (eobp)
5074        (error "No group at current line."))
5075   (let ((ward (if (< arg 0) -1 1))
5076         (arg (abs arg)))
5077     (while (and (> arg 0)
5078                 (not (eobp))
5079                 (gnus-browse-unsubscribe-group)
5080                 (zerop (gnus-browse-next-group ward)))
5081       (setq arg (1- arg)))
5082     (gnus-group-position-cursor)
5083     (if (/= 0 arg) (gnus-message 7 "No more newsgroups"))
5084     arg))
5085
5086 (defun gnus-browse-group-name ()
5087   (save-excursion
5088     (beginning-of-line)
5089     (if (not (re-search-forward ": \\(.*\\)$" (gnus-point-at-eol) t))
5090         ()
5091       (gnus-group-prefixed-name 
5092        (buffer-substring (match-beginning 1) (match-end 1))
5093        gnus-browse-current-method))))
5094   
5095 (defun gnus-browse-unsubscribe-group ()
5096   (let ((sub nil)
5097         (buffer-read-only nil)
5098         group)
5099     (save-excursion
5100       (beginning-of-line)
5101       (if (= (following-char) ?K) (setq sub t))
5102       (setq group (gnus-browse-group-name))
5103       (beginning-of-line)
5104       (delete-char 1)
5105       (if sub
5106           (progn
5107             (gnus-group-change-level 
5108              (list t group gnus-level-default-subscribed
5109                    nil nil gnus-browse-current-method) 
5110              gnus-level-default-subscribed gnus-level-killed
5111              (and (car (nth 1 gnus-newsrc-alist))
5112                   (gnus-gethash (car (nth 1 gnus-newsrc-alist))
5113                                 gnus-newsrc-hashtb))
5114              t)
5115             (insert ? ))
5116         (gnus-group-change-level 
5117          group gnus-level-killed gnus-level-default-subscribed)
5118         (insert ?K)))
5119     t))
5120
5121 (defun gnus-browse-exit ()
5122   "Quit browsing and return to the group buffer."
5123   (interactive)
5124   (if (eq major-mode 'gnus-browse-mode)
5125       (kill-buffer (current-buffer)))
5126   (if gnus-browse-return-buffer
5127       (gnus-configure-windows 'server 'force)
5128     (gnus-configure-windows 'group 'force)
5129     (gnus-group-list-groups nil)))
5130
5131 (defun gnus-browse-describe-briefly ()
5132   "Give a one line description of the group mode commands."
5133   (interactive)
5134   (gnus-message 6
5135    (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")))
5136       
5137 \f
5138 ;;;
5139 ;;; Gnus summary mode
5140 ;;;
5141
5142 (defvar gnus-summary-mode-map nil)
5143 (defvar gnus-summary-mark-map nil)
5144 (defvar gnus-summary-mscore-map nil)
5145 (defvar gnus-summary-article-map nil)
5146 (defvar gnus-summary-thread-map nil)
5147 (defvar gnus-summary-goto-map nil)
5148 (defvar gnus-summary-exit-map nil)
5149 (defvar gnus-summary-interest-map nil)
5150 (defvar gnus-summary-sort-map nil)
5151 (defvar gnus-summary-backend-map nil)
5152 (defvar gnus-summary-save-map nil)
5153 (defvar gnus-summary-wash-map nil)
5154 (defvar gnus-summary-wash-hide-map nil)
5155 (defvar gnus-summary-wash-highlight-map nil)
5156 (defvar gnus-summary-wash-time-map nil)
5157 (defvar gnus-summary-help-map nil)
5158
5159 (put 'gnus-summary-mode 'mode-class 'special)
5160
5161 (if gnus-summary-mode-map
5162     nil
5163   (setq gnus-summary-mode-map (make-keymap))
5164   (suppress-keymap gnus-summary-mode-map)
5165
5166   ;; Non-orthogonal keys
5167
5168   (define-key gnus-summary-mode-map " " 'gnus-summary-next-page)
5169   (define-key gnus-summary-mode-map "\177" 'gnus-summary-prev-page)
5170   (define-key gnus-summary-mode-map "\r" 'gnus-summary-scroll-up)
5171   (define-key gnus-summary-mode-map "n" 'gnus-summary-next-unread-article)
5172   (define-key gnus-summary-mode-map "p" 'gnus-summary-prev-unread-article)
5173   (define-key gnus-summary-mode-map "N" 'gnus-summary-next-article)
5174   (define-key gnus-summary-mode-map "P" 'gnus-summary-prev-article)
5175   (define-key gnus-summary-mode-map "\M-\C-n" 'gnus-summary-next-same-subject)
5176   (define-key gnus-summary-mode-map "\M-\C-p" 'gnus-summary-prev-same-subject)
5177   (define-key gnus-summary-mode-map "\M-n" 'gnus-summary-next-unread-subject)
5178   (define-key gnus-summary-mode-map "\M-p" 'gnus-summary-prev-unread-subject)
5179   (define-key gnus-summary-mode-map "." 'gnus-summary-first-unread-article)
5180   (define-key gnus-summary-mode-map "," 'gnus-summary-best-unread-article)
5181   (define-key gnus-summary-mode-map "\M-s" 'gnus-summary-search-article-forward)
5182   (define-key gnus-summary-mode-map "\M-r" 'gnus-summary-search-article-backward)
5183   (define-key gnus-summary-mode-map "<" 'gnus-summary-beginning-of-article)
5184   (define-key gnus-summary-mode-map ">" 'gnus-summary-end-of-article)
5185   (define-key gnus-summary-mode-map "j" 'gnus-summary-goto-subject)
5186   (define-key gnus-summary-mode-map "^" 'gnus-summary-refer-parent-article)
5187   (define-key gnus-summary-mode-map "\M-^" 'gnus-summary-refer-article)
5188   (define-key gnus-summary-mode-map "u" 'gnus-summary-tick-article-forward)
5189   (define-key gnus-summary-mode-map "!" 'gnus-summary-tick-article-forward)
5190   (define-key gnus-summary-mode-map "U" 'gnus-summary-tick-article-backward)
5191   (define-key gnus-summary-mode-map "d" 'gnus-summary-mark-as-read-forward)
5192   (define-key gnus-summary-mode-map "D" 'gnus-summary-mark-as-read-backward)
5193   (define-key gnus-summary-mode-map "E" 'gnus-summary-mark-as-expirable)
5194   (define-key gnus-summary-mode-map "\M-u" 'gnus-summary-clear-mark-forward)
5195   (define-key gnus-summary-mode-map "\M-U" 'gnus-summary-clear-mark-backward)
5196   (define-key gnus-summary-mode-map "k" 'gnus-summary-kill-same-subject-and-select)
5197   (define-key gnus-summary-mode-map "\C-k" 'gnus-summary-kill-same-subject)
5198   (define-key gnus-summary-mode-map "\M-\C-k" 'gnus-summary-kill-thread)
5199   (define-key gnus-summary-mode-map "\M-\C-l" 'gnus-summary-lower-thread)
5200   (define-key gnus-summary-mode-map "e" 'gnus-summary-edit-article)
5201   (define-key gnus-summary-mode-map "#" 'gnus-summary-mark-as-processable)
5202   (define-key gnus-summary-mode-map "\M-#" 'gnus-summary-unmark-as-processable)
5203   (define-key gnus-summary-mode-map "\M-\C-t" 'gnus-summary-toggle-threads)
5204   (define-key gnus-summary-mode-map "\M-\C-s" 'gnus-summary-show-thread)
5205   (define-key gnus-summary-mode-map "\M-\C-h" 'gnus-summary-hide-thread)
5206   (define-key gnus-summary-mode-map "\M-\C-f" 'gnus-summary-next-thread)
5207   (define-key gnus-summary-mode-map "\M-\C-b" 'gnus-summary-prev-thread)
5208   (define-key gnus-summary-mode-map "\M-\C-u" 'gnus-summary-up-thread)
5209   (define-key gnus-summary-mode-map "\M-\C-d" 'gnus-summary-down-thread)
5210   (define-key gnus-summary-mode-map "&" 'gnus-summary-execute-command)
5211   (define-key gnus-summary-mode-map "c" 'gnus-summary-catchup-and-exit)
5212   (define-key gnus-summary-mode-map "\C-w" 'gnus-summary-mark-region-as-read)
5213   (define-key gnus-summary-mode-map "\C-t" 'gnus-summary-toggle-truncation)
5214   (define-key gnus-summary-mode-map "?" 'gnus-summary-mark-as-dormant)
5215   (define-key gnus-summary-mode-map "\C-c\M-\C-s" 'gnus-summary-show-all-expunged)
5216   (define-key gnus-summary-mode-map "\C-c\C-s\C-n" 'gnus-summary-sort-by-number)
5217   (define-key gnus-summary-mode-map "\C-c\C-s\C-a" 'gnus-summary-sort-by-author)
5218   (define-key gnus-summary-mode-map "\C-c\C-s\C-s" 'gnus-summary-sort-by-subject)
5219   (define-key gnus-summary-mode-map "\C-c\C-s\C-d" 'gnus-summary-sort-by-date)
5220   (define-key gnus-summary-mode-map "\C-c\C-s\C-i" 'gnus-summary-sort-by-score)
5221   (define-key gnus-summary-mode-map "=" 'gnus-summary-expand-window)
5222   (define-key gnus-summary-mode-map "\C-x\C-s" 'gnus-summary-reselect-current-group)
5223   (define-key gnus-summary-mode-map "\M-g" 'gnus-summary-rescan-group)
5224   (define-key gnus-summary-mode-map "w" 'gnus-summary-stop-page-breaking)
5225   (define-key gnus-summary-mode-map "\C-c\C-r" 'gnus-summary-caesar-message)
5226   (define-key gnus-summary-mode-map "\M-t" 'gnus-summary-toggle-mime)
5227   (define-key gnus-summary-mode-map "f" 'gnus-summary-followup)
5228   (define-key gnus-summary-mode-map "F" 'gnus-summary-followup-with-original)
5229   (define-key gnus-summary-mode-map "C" 'gnus-summary-cancel-article)
5230   (define-key gnus-summary-mode-map "r" 'gnus-summary-reply)
5231   (define-key gnus-summary-mode-map "R" 'gnus-summary-reply-with-original)
5232   (define-key gnus-summary-mode-map "\C-c\C-f" 'gnus-summary-mail-forward)
5233   (define-key gnus-summary-mode-map "o" 'gnus-summary-save-article)
5234   (define-key gnus-summary-mode-map "\C-o" 'gnus-summary-save-article-mail)
5235   (define-key gnus-summary-mode-map "|" 'gnus-summary-pipe-output)
5236   (define-key gnus-summary-mode-map "\M-k" 'gnus-summary-edit-local-kill)
5237   (define-key gnus-summary-mode-map "\M-K" 'gnus-summary-edit-global-kill)
5238   (define-key gnus-summary-mode-map "V" 'gnus-version)
5239   (define-key gnus-summary-mode-map "\C-c\C-d" 'gnus-summary-describe-group)
5240   (define-key gnus-summary-mode-map "q" 'gnus-summary-exit)
5241   (define-key gnus-summary-mode-map "Q" 'gnus-summary-exit-no-update)
5242   (define-key gnus-summary-mode-map "\C-c\C-i" 'gnus-info-find-node)
5243   (define-key gnus-summary-mode-map gnus-mouse-2 'gnus-mouse-pick-article)
5244   (define-key gnus-summary-mode-map "m" 'gnus-summary-mail-other-window)
5245   (define-key gnus-summary-mode-map "a" 'gnus-summary-post-news)
5246   (define-key gnus-summary-mode-map "x" 'gnus-summary-remove-lines-marked-as-read)
5247 ; (define-key gnus-summary-mode-map "X" 'gnus-summary-remove-lines-marked-with)
5248   (define-key gnus-summary-mode-map "s" 'gnus-summary-isearch-article)
5249   (define-key gnus-summary-mode-map "t" 'gnus-summary-toggle-header)
5250   (define-key gnus-summary-mode-map "g" 'gnus-summary-show-article)
5251 ;  (define-key gnus-summary-mode-map "?" 'gnus-summary-describe-briefly)
5252   (define-key gnus-summary-mode-map "l" 'gnus-summary-goto-last-article)
5253   (define-key gnus-summary-mode-map "\C-c\C-v\C-v" 'gnus-uu-decode-uu-view)
5254   (define-key gnus-summary-mode-map "\C-d" 'gnus-summary-enter-digest-group)
5255   (define-key gnus-summary-mode-map "v" 'gnus-summary-verbose-headers)
5256   (define-key gnus-summary-mode-map "\C-c\C-b" 'gnus-bug)
5257
5258
5259   ;; Sort of orthogonal keymap
5260   (define-prefix-command 'gnus-summary-mark-map)
5261   (define-key gnus-summary-mode-map "M" 'gnus-summary-mark-map)
5262   (define-key gnus-summary-mark-map "t" 'gnus-summary-tick-article-forward)
5263   (define-key gnus-summary-mark-map "!" 'gnus-summary-tick-article-forward)
5264   (define-key gnus-summary-mark-map "d" 'gnus-summary-mark-as-read-forward)
5265   (define-key gnus-summary-mark-map "r" 'gnus-summary-mark-as-read-forward)
5266   (define-key gnus-summary-mark-map "c" 'gnus-summary-clear-mark-forward)
5267   (define-key gnus-summary-mark-map " " 'gnus-summary-clear-mark-forward)
5268   (define-key gnus-summary-mark-map "e" 'gnus-summary-mark-as-expirable)
5269   (define-key gnus-summary-mark-map "x" 'gnus-summary-mark-as-expirable)
5270   (define-key gnus-summary-mark-map "?" 'gnus-summary-mark-as-dormant)
5271   (define-key gnus-summary-mark-map "b" 'gnus-summary-set-bookmark)
5272   (define-key gnus-summary-mark-map "B" 'gnus-summary-remove-bookmark)
5273   (define-key gnus-summary-mark-map "#" 'gnus-summary-mark-as-processable)
5274   (define-key gnus-summary-mark-map "\M-#" 'gnus-summary-unmark-as-processable)
5275   (define-key gnus-summary-mark-map "\M-r" 'gnus-summary-remove-lines-marked-as-read)
5276   (define-key gnus-summary-mark-map "\M-\C-r" 'gnus-summary-remove-lines-marked-with)
5277   (define-key gnus-summary-mark-map "D" 'gnus-summary-show-all-dormant)
5278   (define-key gnus-summary-mark-map "\M-D" 'gnus-summary-hide-all-dormant)
5279   (define-key gnus-summary-mark-map "S" 'gnus-summary-show-all-expunged)
5280   (define-key gnus-summary-mark-map "C" 'gnus-summary-catchup)
5281   (define-key gnus-summary-mark-map "H" 'gnus-summary-catchup-to-here)
5282   (define-key gnus-summary-mark-map "\C-c" 'gnus-summary-catchup-all)
5283   (define-key gnus-summary-mark-map "k" 'gnus-summary-kill-same-subject-and-select)
5284   (define-key gnus-summary-mark-map "K" 'gnus-summary-kill-same-subject)
5285
5286   (define-prefix-command 'gnus-summary-mscore-map)
5287   (define-key gnus-summary-mark-map "V" 'gnus-summary-mscore-map)
5288   (define-key gnus-summary-mscore-map "c" 'gnus-summary-clear-above)
5289   (define-key gnus-summary-mscore-map "u" 'gnus-summary-tick-above)
5290   (define-key gnus-summary-mscore-map "m" 'gnus-summary-mark-above)
5291   (define-key gnus-summary-mscore-map "k" 'gnus-summary-kill-below)
5292
5293   (define-key gnus-summary-mark-map "P" 'gnus-uu-mark-map)
5294   
5295   (define-key gnus-summary-mode-map "S" 'gnus-summary-send-map)
5296   
5297   (define-prefix-command 'gnus-summary-goto-map)
5298   (define-key gnus-summary-mode-map "G" 'gnus-summary-goto-map)
5299   (define-key gnus-summary-goto-map "n" 'gnus-summary-next-unread-article)
5300   (define-key gnus-summary-goto-map "p" 'gnus-summary-prev-unread-article)
5301   (define-key gnus-summary-goto-map "N" 'gnus-summary-next-article)
5302   (define-key gnus-summary-goto-map "P" 'gnus-summary-prev-article)
5303   (define-key gnus-summary-goto-map "\C-n" 'gnus-summary-next-same-subject)
5304   (define-key gnus-summary-goto-map "\C-p" 'gnus-summary-prev-same-subject)
5305   (define-key gnus-summary-goto-map "\M-n" 'gnus-summary-next-unread-subject)
5306   (define-key gnus-summary-goto-map "\M-p" 'gnus-summary-prev-unread-subject)
5307   (define-key gnus-summary-goto-map "f" 'gnus-summary-first-unread-article)
5308   (define-key gnus-summary-goto-map "b" 'gnus-summary-best-unread-article)
5309   (define-key gnus-summary-goto-map "g" 'gnus-summary-goto-subject)
5310   (define-key gnus-summary-goto-map "l" 'gnus-summary-goto-last-article)
5311   (define-key gnus-summary-goto-map "p" 'gnus-summary-pop-article)
5312
5313
5314   (define-prefix-command 'gnus-summary-thread-map)
5315   (define-key gnus-summary-mode-map "T" 'gnus-summary-thread-map)
5316   (define-key gnus-summary-thread-map "k" 'gnus-summary-kill-thread)
5317   (define-key gnus-summary-thread-map "l" 'gnus-summary-lower-thread)
5318   (define-key gnus-summary-thread-map "i" 'gnus-summary-raise-thread)
5319   (define-key gnus-summary-thread-map "T" 'gnus-summary-toggle-threads)
5320   (define-key gnus-summary-thread-map "s" 'gnus-summary-show-thread)
5321   (define-key gnus-summary-thread-map "S" 'gnus-summary-show-all-threads)
5322   (define-key gnus-summary-thread-map "h" 'gnus-summary-hide-thread)
5323   (define-key gnus-summary-thread-map "H" 'gnus-summary-hide-all-threads)
5324   (define-key gnus-summary-thread-map "n" 'gnus-summary-next-thread)
5325   (define-key gnus-summary-thread-map "p" 'gnus-summary-prev-thread)
5326   (define-key gnus-summary-thread-map "u" 'gnus-summary-up-thread)
5327   (define-key gnus-summary-thread-map "d" 'gnus-summary-down-thread)
5328   (define-key gnus-summary-thread-map "#" 'gnus-uu-mark-thread)
5329
5330   
5331   (define-prefix-command 'gnus-summary-exit-map)
5332   (define-key gnus-summary-mode-map "Z" 'gnus-summary-exit-map)
5333   (define-key gnus-summary-exit-map "c" 'gnus-summary-catchup-and-exit)
5334   (define-key gnus-summary-exit-map "C" 'gnus-summary-catchup-all-and-exit)
5335   (define-key gnus-summary-exit-map "E" 'gnus-summary-exit-no-update)
5336   (define-key gnus-summary-exit-map "Q" 'gnus-summary-exit)
5337   (define-key gnus-summary-exit-map "Z" 'gnus-summary-exit)
5338   (define-key gnus-summary-exit-map "n" 'gnus-summary-catchup-and-goto-next-group)
5339   (define-key gnus-summary-exit-map "R" 'gnus-summary-reselect-current-group)
5340   (define-key gnus-summary-exit-map "G" 'gnus-summary-rescan-group)
5341   (define-key gnus-summary-exit-map "N" 'gnus-summary-next-group)
5342   (define-key gnus-summary-exit-map "P" 'gnus-summary-prev-group)
5343
5344
5345   (define-prefix-command 'gnus-summary-article-map)
5346   (define-key gnus-summary-mode-map "A" 'gnus-summary-article-map)
5347   (define-key gnus-summary-article-map " " 'gnus-summary-next-page)
5348   (define-key gnus-summary-article-map "n" 'gnus-summary-next-page)
5349   (define-key gnus-summary-article-map "\177" 'gnus-summary-prev-page)
5350   (define-key gnus-summary-article-map "p" 'gnus-summary-prev-page)
5351   (define-key gnus-summary-article-map "\r" 'gnus-summary-scroll-up)
5352   (define-key gnus-summary-article-map "<" 'gnus-summary-beginning-of-article)
5353   (define-key gnus-summary-article-map ">" 'gnus-summary-end-of-article)
5354   (define-key gnus-summary-article-map "b" 'gnus-summary-beginning-of-article)
5355   (define-key gnus-summary-article-map "e" 'gnus-summary-end-of-article)
5356   (define-key gnus-summary-article-map "^" 'gnus-summary-refer-parent-article)
5357   (define-key gnus-summary-article-map "r" 'gnus-summary-refer-parent-article)
5358   (define-key gnus-summary-article-map "g" 'gnus-summary-show-article)
5359   (define-key gnus-summary-article-map "s" 'gnus-summary-isearch-article)
5360
5361
5362
5363   (define-prefix-command 'gnus-summary-wash-map)
5364   (define-key gnus-summary-mode-map "W" 'gnus-summary-wash-map)
5365
5366   (define-prefix-command 'gnus-summary-wash-hide-map)
5367   (define-key gnus-summary-wash-map "W" 'gnus-summary-wash-hide-map)
5368   (define-key gnus-summary-wash-hide-map "a" 'gnus-article-hide)
5369   (define-key gnus-summary-wash-hide-map "h" 'gnus-article-hide-headers)
5370   (define-key gnus-summary-wash-hide-map "s" 'gnus-article-hide-signature)
5371   (define-key gnus-summary-wash-hide-map "c" 'gnus-article-hide-citation)
5372   (define-key gnus-summary-wash-hide-map "\C-c" 'gnus-article-hide-citation-maybe)
5373
5374   (define-prefix-command 'gnus-summary-wash-highlight-map)
5375   (define-key gnus-summary-wash-map "H" 'gnus-summary-wash-highlight-map)
5376   (define-key gnus-summary-wash-highlight-map "a" 'gnus-article-highlight)
5377   (define-key gnus-summary-wash-highlight-map "h" 'gnus-article-highlight-headers)
5378   (define-key gnus-summary-wash-highlight-map "c" 'gnus-article-highlight-citation)
5379   (define-key gnus-summary-wash-highlight-map "s" 'gnus-article-highlight-signature)
5380
5381   (define-prefix-command 'gnus-summary-wash-time-map)
5382   (define-key gnus-summary-wash-map "T" 'gnus-summary-wash-time-map)
5383   (define-key gnus-summary-wash-time-map "z" 'gnus-article-date-ut)
5384   (define-key gnus-summary-wash-time-map "u" 'gnus-article-date-ut)
5385   (define-key gnus-summary-wash-time-map "l" 'gnus-article-date-local)
5386   (define-key gnus-summary-wash-time-map "e" 'gnus-article-date-lapsed)
5387
5388   (define-key gnus-summary-wash-map "b" 'gnus-article-add-buttons)
5389   (define-key gnus-summary-wash-map "o" 'gnus-article-treat-overstrike)
5390   (define-key gnus-summary-wash-map "w" 'gnus-article-word-wrap)
5391   (define-key gnus-summary-wash-map "c" 'gnus-article-remove-cr)
5392   (define-key gnus-summary-wash-map "q" 'gnus-article-de-quoted-unreadable)
5393   (define-key gnus-summary-wash-map "f" 'gnus-article-display-x-face)
5394   (define-key gnus-summary-wash-map "l" 'gnus-summary-stop-page-breaking)
5395   (define-key gnus-summary-wash-map "r" 'gnus-summary-caesar-message)
5396   (define-key gnus-summary-wash-map "t" 'gnus-summary-toggle-header)
5397   (define-key gnus-summary-wash-map "m" 'gnus-summary-toggle-mime)
5398
5399
5400   (define-prefix-command 'gnus-summary-help-map)
5401   (define-key gnus-summary-mode-map "H" 'gnus-summary-help-map)
5402   (define-key gnus-summary-help-map "v" 'gnus-version)
5403   (define-key gnus-summary-help-map "f" 'gnus-summary-fetch-faq)
5404   (define-key gnus-summary-help-map "d" 'gnus-summary-describe-group)
5405   (define-key gnus-summary-help-map "h" 'gnus-summary-describe-briefly)
5406   (define-key gnus-summary-help-map "i" 'gnus-info-find-node)
5407
5408
5409   (define-prefix-command 'gnus-summary-backend-map)
5410   (define-key gnus-summary-mode-map "B" 'gnus-summary-backend-map)
5411   (define-key gnus-summary-backend-map "e" 'gnus-summary-expire-articles)
5412   (define-key gnus-summary-backend-map "\M-\C-e" 
5413     'gnus-summary-expire-articles-now)
5414   (define-key gnus-summary-backend-map "\177" 'gnus-summary-delete-article)
5415   (define-key gnus-summary-backend-map "m" 'gnus-summary-move-article)
5416   (define-key gnus-summary-backend-map "r" 'gnus-summary-respool-article)
5417   (define-key gnus-summary-backend-map "w" 'gnus-summary-edit-article)
5418   (define-key gnus-summary-backend-map "c" 'gnus-summary-copy-article)
5419   (define-key gnus-summary-backend-map "q" 'gnus-summary-fancy-query)
5420   (define-key gnus-summary-backend-map "i" 'gnus-summary-import-article)
5421
5422
5423   (define-prefix-command 'gnus-summary-save-map)
5424   (define-key gnus-summary-mode-map "O" 'gnus-summary-save-map)
5425   (define-key gnus-summary-save-map "o" 'gnus-summary-save-article)
5426   (define-key gnus-summary-save-map "m" 'gnus-summary-save-article-mail)
5427   (define-key gnus-summary-save-map "r" 'gnus-summary-save-article-rmail)
5428   (define-key gnus-summary-save-map "f" 'gnus-summary-save-article-file)
5429   (define-key gnus-summary-save-map "h" 'gnus-summary-save-article-folder)
5430   (define-key gnus-summary-save-map "v" 'gnus-summary-save-article-vm)
5431   (define-key gnus-summary-save-map "p" 'gnus-summary-pipe-output)
5432 ;  (define-key gnus-summary-save-map "s" 'gnus-soup-add-article)
5433
5434   (define-key gnus-summary-mode-map "X" 'gnus-uu-extract-map)
5435
5436   (define-key gnus-summary-mode-map "\M-&" 'gnus-summary-universal-argument)
5437 ;  (define-key gnus-summary-various-map "\C-s" 'gnus-summary-search-article-forward)
5438 ;  (define-key gnus-summary-various-map "\C-r" 'gnus-summary-search-article-backward)
5439 ;  (define-key gnus-summary-various-map "r" 'gnus-summary-refer-article)
5440 ;  (define-key gnus-summary-various-map "&" 'gnus-summary-execute-command)
5441 ;  (define-key gnus-summary-various-map "T" 'gnus-summary-toggle-truncation)
5442 ;  (define-key gnus-summary-various-map "e" 'gnus-summary-expand-window)
5443   (define-key gnus-summary-article-map "D" 'gnus-summary-enter-digest-group)
5444 ;  (define-key gnus-summary-various-map "k" 'gnus-summary-edit-local-kill)
5445 ;  (define-key gnus-summary-various-map "K" 'gnus-summary-edit-global-kill)
5446
5447   (define-key gnus-summary-mode-map "V" 'gnus-summary-score-map)
5448
5449 ;  (define-prefix-command 'gnus-summary-sort-map)
5450 ;  (define-key gnus-summary-various-map "s" 'gnus-summary-sort-map)
5451 ;  (define-key gnus-summary-sort-map "n" 'gnus-summary-sort-by-number)
5452 ;  (define-key gnus-summary-sort-map "a" 'gnus-summary-sort-by-author)
5453 ;  (define-key gnus-summary-sort-map "s" 'gnus-summary-sort-by-subject)
5454 ;  (define-key gnus-summary-sort-map "d" 'gnus-summary-sort-by-date)
5455 ;  (define-key gnus-summary-sort-map "i" 'gnus-summary-sort-by-score)
5456
5457   (define-key gnus-summary-mode-map "I" 'gnus-summary-increase-score)
5458   (define-key gnus-summary-mode-map "L" 'gnus-summary-lower-score)
5459   )
5460
5461
5462 \f
5463
5464 (defun gnus-summary-mode (&optional group)
5465   "Major mode for reading articles.
5466
5467 All normal editing commands are switched off.
5468 \\<gnus-summary-mode-map>
5469 Each line in this buffer represents one article.  To read an
5470 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
5471 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]', 
5472 respectively.
5473
5474 You can also post articles and send mail from this buffer.  To 
5475 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author 
5476 of an article, type `\\[gnus-summary-reply]'.
5477
5478 There are approx. one gazillion commands you can execute in this 
5479 buffer; read the info pages for more information (`\\[gnus-info-find-node]'). 
5480
5481 The following commands are available:
5482
5483 \\{gnus-summary-mode-map}"
5484   (interactive)
5485   (if gnus-visual (gnus-summary-make-menu-bar))
5486   (kill-all-local-variables)
5487   (let ((locals gnus-summary-local-variables))
5488     (while locals
5489       (if (consp (car locals))
5490           (progn
5491             (make-local-variable (car (car locals)))
5492             (set (car (car locals)) (eval (cdr (car locals)))))
5493         (make-local-variable (car locals))
5494         (set (car locals) nil))
5495       (setq locals (cdr locals))))
5496   (gnus-make-thread-indent-array)
5497   (setq mode-line-modified "-- ")
5498   (make-local-variable 'mode-line-format)
5499   (setq mode-line-format (copy-sequence mode-line-format))
5500   (and (equal (nth 3 mode-line-format) "   ")
5501        (setcar (nthcdr 3 mode-line-format) ""))
5502   (setq major-mode 'gnus-summary-mode)
5503   (setq mode-name "Summary")
5504   (make-local-variable 'minor-mode-alist)
5505   (use-local-map gnus-summary-mode-map)
5506   (buffer-disable-undo (current-buffer))
5507   (setq buffer-read-only t)             ;Disable modification
5508   (setq truncate-lines t)
5509   (setq selective-display t)
5510   (setq selective-display-ellipses t)   ;Display `...'
5511   (setq buffer-display-table gnus-summary-display-table)
5512   (setq gnus-newsgroup-name group)
5513   (run-hooks 'gnus-summary-mode-hook))
5514
5515 (defun gnus-summary-make-display-table ()
5516   ;; Change the display table.  Odd characters have a tendency to mess
5517   ;; up nicely formatted displays - we make all possible glyphs
5518   ;; display only a single character.
5519
5520   ;; We start from the standard display table, if any.
5521   (setq gnus-summary-display-table 
5522         (or (copy-sequence standard-display-table)
5523             (make-display-table)))
5524   ;; Nix out all the control chars...
5525   (let ((i 32))
5526     (while (>= (setq i (1- i)) 0)
5527       (aset gnus-summary-display-table i [??])))
5528   ;; ... but not newline and cr, of course. (cr is necessary for the
5529   ;; selective display).  
5530   (aset gnus-summary-display-table ?\n nil)
5531   (aset gnus-summary-display-table ?\r nil)
5532   ;; We nix out any glyphs over 126 that are not set already.  
5533   (let ((i 256))
5534     (while (>= (setq i (1- i)) 127)
5535       ;; Only modify if the entry is nil.
5536       (or (aref gnus-summary-display-table i) 
5537           (aset gnus-summary-display-table i [??])))))
5538
5539 (defun gnus-summary-clear-local-variables ()
5540   (let ((locals gnus-summary-local-variables))
5541     (while locals
5542       (if (consp (car locals))
5543           (and (vectorp (car (car locals)))
5544                (set (car (car locals)) nil))
5545         (and (vectorp (car locals))
5546              (set (car locals) nil)))
5547       (setq locals (cdr locals)))))
5548
5549 ;; Some summary mode macros.
5550
5551 ;; Return a header specified by a NUMBER.
5552 (defun gnus-get-header-by-number (number)
5553   (save-excursion
5554     (set-buffer gnus-summary-buffer)
5555     (or gnus-newsgroup-headers-hashtb-by-number
5556         (gnus-make-headers-hashtable-by-number))
5557     (gnus-gethash (int-to-string number)
5558                   gnus-newsgroup-headers-hashtb-by-number)))
5559
5560 ;; Fast version of the function above.
5561 (defmacro gnus-get-header-by-num (number)
5562   (` (gnus-gethash (int-to-string (, number)) 
5563                    gnus-newsgroup-headers-hashtb-by-number)))
5564
5565 (defmacro gnus-summary-search-forward (&optional unread subject backward)
5566   "Search for article forward.
5567 If UNREAD is non-nil, only unread articles are selected.
5568 If SUBJECT is non-nil, the article which has the same subject will be
5569 searched for. 
5570 If BACKWARD is non-nil, the search will be performed backwards instead."
5571   (` (gnus-summary-search-subject (, backward) (, unread) (, subject))))
5572
5573 (defmacro gnus-summary-search-backward (&optional unread subject)
5574   "Search for article backward.
5575 If 1st optional argument UNREAD is non-nil, only unread article is selected.
5576 If 2nd optional argument SUBJECT is non-nil, the article which has
5577 the same subject will be searched for."
5578   (` (gnus-summary-search-forward (, unread) (, subject) t)))
5579
5580 (defmacro gnus-summary-article-number (&optional number-or-nil)
5581   "The article number of the article on the current line.
5582 If there isn's an article number here, then we return the current
5583 article number."
5584   (if number-or-nil
5585       '(get-text-property (gnus-point-at-bol) 'gnus-number)
5586     '(or (get-text-property (gnus-point-at-bol) 'gnus-number) 
5587          gnus-current-article)))
5588
5589 (defmacro gnus-summary-thread-level ()
5590   "The thread level of the article on the current line."
5591   '(or (get-text-property (gnus-point-at-bol) 'gnus-level)
5592        0))
5593
5594 (defmacro gnus-summary-article-mark ()
5595   "The mark on the current line."
5596   '(get-text-property (gnus-point-at-bol) 'gnus-mark))
5597
5598 (defun gnus-summary-subject-string ()
5599   "Return current subject string or nil if nothing."
5600   (let ((article (gnus-summary-article-number))
5601         header)
5602     (and article 
5603          (setq header (gnus-get-header-by-num article))
5604          (vectorp header)
5605          (header-subject header))))
5606
5607 ;; Various summary mode internalish functions.
5608
5609 (defun gnus-mouse-pick-article (e)
5610   (interactive "e")
5611   (mouse-set-point e)
5612   (gnus-summary-next-page nil t))
5613
5614 (defun gnus-summary-setup-buffer (group)
5615   "Initialize summary buffer."
5616   (let ((buffer (concat "*Summary " group "*")))
5617     (if (get-buffer buffer)
5618         (progn
5619           (set-buffer buffer)
5620           (not gnus-newsgroup-begin))
5621       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
5622       (setq gnus-summary-buffer (set-buffer (get-buffer-create buffer)))
5623       (gnus-add-current-to-buffer-list)
5624       (gnus-summary-mode group)
5625       (and gnus-carpal (gnus-carpal-setup-buffer 'summary))
5626       (setq gnus-newsgroup-name group)
5627       t)))
5628
5629 (defun gnus-set-global-variables ()
5630   ;; Set the global equivalents of the summary buffer-local variables
5631   ;; to the latest values they had. These reflect the summary buffer
5632   ;; that was in action when the last article was fetched.
5633   (if (eq major-mode 'gnus-summary-mode) 
5634       (progn
5635         (setq gnus-summary-buffer (current-buffer))
5636         (let ((name gnus-newsgroup-name)
5637               (marked gnus-newsgroup-marked)
5638               (unread gnus-newsgroup-unreads)
5639               (headers gnus-current-headers)
5640               (score-file gnus-current-score-file))
5641           (save-excursion
5642             (set-buffer gnus-group-buffer)
5643             (setq gnus-newsgroup-name name)
5644             (setq gnus-newsgroup-marked marked)
5645             (setq gnus-newsgroup-unreads unread)
5646             (setq gnus-current-headers headers)
5647             (setq gnus-current-score-file score-file))))))
5648
5649 (defun gnus-summary-insert-dummy-line (sformat subject number)
5650   (if (not sformat) 
5651       (setq sformat gnus-summary-dummy-line-format-spec))
5652   (let (b)
5653     (beginning-of-line)
5654     (setq b (point))
5655     (insert (eval sformat))
5656     (add-text-properties
5657      b (1+ b)
5658      (list 'gnus-number number 
5659            'gnus-mark gnus-dummy-mark
5660            'gnus-level 0))))
5661
5662 (defvar gnus-thread-indent-array nil)
5663 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
5664 (defun gnus-make-thread-indent-array ()
5665   (let ((n 200))
5666     (if (and gnus-thread-indent-array
5667              (= gnus-thread-indent-level gnus-thread-indent-array-level))
5668         nil
5669       (setq gnus-thread-indent-array (make-vector 201 "")
5670             gnus-thread-indent-array-level gnus-thread-indent-level)
5671       (while (>= n 0)
5672         (aset gnus-thread-indent-array n
5673               (make-string (* n gnus-thread-indent-level) ? ))
5674         (setq n (1- n))))))
5675
5676 (defun gnus-summary-insert-line 
5677   (sformat header level current unread replied expirable subject-or-nil
5678            &optional dummy score)
5679   (or sformat (setq sformat gnus-summary-line-format-spec))
5680   (let* ((indentation (aref gnus-thread-indent-array level))
5681          (lines (header-lines header))
5682          (score (or score gnus-summary-default-score 0))
5683          (score-char
5684           (if (or (null gnus-summary-default-score)
5685                   (<= (abs (- score gnus-summary-default-score))
5686                       gnus-summary-zcore-fuzz)) ? 
5687             (if (< score gnus-summary-default-score)
5688                 gnus-score-below-mark gnus-score-over-mark)))
5689          (replied (if replied gnus-replied-mark ? ))
5690          (from (header-from header))
5691          (name (cond 
5692                 ((string-match "(.+)" from)
5693                  (substring from (1+ (match-beginning 0)) (1- (match-end 0))))
5694                 ((string-match "<[^>]+> *$" from)
5695                  (let ((beg (match-beginning 0)))
5696                    (or (and (string-match "^\"[^\"]*\"" from)
5697                             (substring from (1+ (match-beginning 0))
5698                                        (1- (match-end 0))))
5699                        (substring from 0 beg))))
5700                 (t from)))
5701          (subject (header-subject header))
5702          (number (header-number header))
5703          (opening-bracket (if dummy ?\< ?\[))
5704          (closing-bracket (if dummy ?\> ?\]))
5705          (buffer-read-only nil)
5706          (b (progn (beginning-of-line) (point))))
5707     (or (numberp lines) (setq lines 0))
5708     (insert (eval sformat))
5709     (add-text-properties
5710      b (1+ b) (list 'gnus-number number 
5711                     'gnus-mark (or unread gnus-unread-mark)
5712                     'gnus-level level))))
5713
5714 (defun gnus-summary-update-line (&optional dont-update)
5715   ;; Update summary line after change.
5716   (or (not gnus-summary-default-score)
5717       gnus-summary-inhibit-highlight
5718       (let ((gnus-summary-inhibit-highlight t)
5719             (article (gnus-summary-article-number)))
5720         (progn
5721           (or dont-update
5722               (if (and gnus-summary-mark-below
5723                        (< (gnus-summary-article-score)
5724                           gnus-summary-mark-below))
5725                   (and (not (memq article gnus-newsgroup-marked))
5726                        (not (memq article gnus-newsgroup-dormant))
5727                        (memq article gnus-newsgroup-unreads)
5728                        (gnus-summary-mark-article-as-read gnus-low-score-mark))
5729                 (and (eq (gnus-summary-article-mark) gnus-low-score-mark)
5730                      (gnus-summary-mark-article-as-unread gnus-unread-mark))))
5731           (and gnus-visual
5732                (run-hooks 'gnus-summary-update-hook))))))
5733
5734 (defun gnus-summary-update-lines (&optional beg end)
5735   ;; Mark article as read (or not) by taking into account scores.
5736   (let ((beg (or beg (point-min)))
5737         (end (or end (point-max))))
5738     (if (or (not gnus-summary-default-score)
5739             gnus-summary-inhibit-highlight)
5740         ()
5741       (let ((gnus-summary-inhibit-highlight t)
5742             article)
5743         (save-excursion
5744           (set-buffer gnus-summary-buffer)
5745           (goto-char beg)
5746           (beginning-of-line)
5747           (while (and (not (eobp)) (< (point) end))
5748             (if (and gnus-summary-mark-below
5749                      (< (or (cdr (assq 
5750                                   (setq article (get-text-property 
5751                                                  (point) 'gnus-number))
5752                                   gnus-newsgroup-scored))
5753                             gnus-summary-default-score 0)
5754                         gnus-summary-mark-below))
5755                 ;; We want to possibly mark it as read...
5756                 (and (not (memq article gnus-newsgroup-marked))
5757                      (not (memq article gnus-newsgroup-dormant))
5758                      (memq article gnus-newsgroup-unreads)
5759                      (gnus-summary-mark-article-as-read gnus-low-score-mark))
5760               ;; We want to possibly mark it as unread.
5761               (and (eq (get-text-property (point) 'gnus-mark)
5762                        gnus-low-score-mark)
5763                    (gnus-summary-mark-article-as-unread gnus-unread-mark)))
5764             ;; Do the visual highlights at the same time.
5765             (and gnus-visual (run-hooks 'gnus-summary-update-hook))
5766             (forward-line 1)))))))
5767
5768 (defvar gnus-tmp-gathered nil)
5769
5770 (defun gnus-summary-number-of-articles-in-thread (thread &optional char)
5771   ;; Sum up all elements (and sub-elements) in a list.
5772   (let* ((number
5773           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
5774           (if (and (consp thread) (cdr thread))
5775               (apply
5776                '+ 1 (mapcar
5777                      (function
5778                       (lambda (arg)
5779                         (if (memq (header-number (car arg)) gnus-tmp-gathered)
5780                             0
5781                           (gnus-summary-number-of-articles-in-thread arg))))
5782                      (cdr thread)))
5783             1)))
5784     (if char 
5785         (if (> number 1) gnus-not-empty-thread-mark
5786           gnus-empty-thread-mark)
5787       number)))
5788
5789 (defun gnus-summary-read-group 
5790   (group &optional show-all no-article kill-buffer)
5791   "Start reading news in newsgroup GROUP.
5792 If SHOW-ALL is non-nil, already read articles are also listed.
5793 If NO-ARTICLE is non-nil, no article is selected initially."
5794   (gnus-message 5 "Retrieving newsgroup: %s..." group)
5795   (let* ((new-group (gnus-summary-setup-buffer group))
5796          (quit-config (nth 1 (assoc 'quit-config (gnus-find-method-for-group
5797                                                   group))))
5798          (did-select (and new-group (gnus-select-newsgroup group show-all))))
5799     (cond 
5800      ((not new-group)
5801       (gnus-set-global-variables)
5802       (gnus-kill-buffer kill-buffer)
5803       (gnus-configure-windows 'summary)
5804       (gnus-set-mode-line 'summary)
5805       (gnus-summary-position-cursor)
5806       (message "")
5807       t)
5808      ((null did-select) 
5809       (and (eq major-mode 'gnus-summary-mode)
5810            (not (equal (current-buffer) kill-buffer))
5811            (progn
5812              (kill-buffer (current-buffer))
5813              (if (not quit-config)
5814                  (progn
5815                    (set-buffer gnus-group-buffer)
5816                    (gnus-group-jump-to-group group)
5817                    (gnus-group-next-unread-group 1))
5818                (if (not (buffer-name (car quit-config)))
5819                    (gnus-configure-windows 'group 'force)
5820                  (set-buffer (car quit-config))
5821                  (and (eq major-mode 'gnus-summary-mode)
5822                       (gnus-set-global-variables))
5823                  (gnus-configure-windows (cdr quit-config))))))
5824       (message "Can't select group")
5825       nil)
5826      ((eq did-select 'quit)
5827       (and (eq major-mode 'gnus-summary-mode)
5828            (not (equal (current-buffer) kill-buffer))
5829            (kill-buffer (current-buffer)))
5830       (gnus-kill-buffer kill-buffer)
5831       (if (not quit-config)
5832           (progn
5833             (set-buffer gnus-group-buffer)
5834             (gnus-group-jump-to-group group)
5835             (gnus-group-next-unread-group 1)
5836             (gnus-configure-windows 'group 'force))
5837         (if (not (buffer-name (car quit-config)))
5838             (gnus-configure-windows 'group 'force)
5839           (set-buffer (car quit-config))
5840           (and (eq major-mode 'gnus-summary-mode)
5841                (gnus-set-global-variables))
5842           (gnus-configure-windows (cdr quit-config))))
5843       (signal 'quit nil))
5844      (t
5845       (gnus-set-global-variables)
5846       ;; Save the active value in effect when the group was entered.
5847       (setq gnus-newsgroup-active 
5848             (gnus-copy-sequence
5849              (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
5850       ;; You can change the subjects in this hook.
5851       (run-hooks 'gnus-select-group-hook)
5852       ;; Do score processing.
5853       (and gnus-use-scoring (gnus-possibly-score-headers))
5854       (gnus-update-format-specifications)
5855       ;; Generate the summary buffer.
5856       (gnus-summary-prepare)
5857       ;; Create the header hashtb.
5858       (gnus-make-headers-hashtable-by-number)
5859       (if (zerop (buffer-size))
5860           (cond (gnus-newsgroup-dormant
5861                  (gnus-summary-show-all-dormant))
5862                 ((and gnus-newsgroup-scored show-all)
5863                  (gnus-summary-show-all-expunged))))
5864       ;; Function `gnus-apply-kill-file' must be called in this hook.
5865       (run-hooks 'gnus-apply-kill-hook)
5866       (if (zerop (buffer-size))
5867           (progn
5868             ;; This newsgroup is empty.
5869             (gnus-summary-catchup-and-exit nil t) ;Without confirmations.
5870             (gnus-message 6 "No unread news")
5871             (gnus-kill-buffer kill-buffer)
5872             nil)
5873         ;;(save-excursion
5874         ;;  (if kill-buffer
5875         ;;      (let ((gnus-summary-buffer kill-buffer))
5876         ;;      (gnus-configure-windows 'group))))
5877         ;; Hide conversation thread subtrees.  We cannot do this in
5878         ;; gnus-summary-prepare-hook since kill processing may not
5879         ;; work with hidden articles.
5880         (and gnus-show-threads
5881              gnus-thread-hide-subtree
5882              (gnus-summary-hide-all-threads))
5883         ;; Show first unread article if requested.
5884         (goto-char (point-min))
5885         (if (and (not no-article)
5886                  gnus-auto-select-first
5887                  (gnus-summary-first-unread-article))
5888             ()
5889           (gnus-configure-windows 'summary))
5890         (gnus-set-mode-line 'summary)
5891         (gnus-summary-position-cursor)
5892         ;; If in async mode, we send some info to the backend.
5893         (and gnus-newsgroup-async
5894              (setq gnus-newsgroup-threads (nreverse gnus-newsgroup-threads))
5895              (gnus-request-asynchronous 
5896               gnus-newsgroup-name
5897               (if (and gnus-asynchronous-article-function
5898                        (fboundp gnus-asynchronous-article-function))
5899                   (funcall gnus-asynchronous-article-function
5900                            gnus-newsgroup-threads)
5901                 gnus-newsgroup-threads)))
5902         (gnus-kill-buffer kill-buffer)
5903         (if (not (get-buffer-window gnus-group-buffer))
5904             ()
5905           ;; gotta use windows, because recenter does wierd stuff if
5906           ;; the current buffer ain't the displayed window.
5907           (let ((owin (selected-window))) 
5908             (select-window (get-buffer-window gnus-group-buffer))
5909             (and (gnus-group-goto-group group)
5910                  (recenter))
5911             (select-window owin))))
5912       t))))
5913
5914 (defun gnus-summary-prepare ()
5915   ;; Generate the summary buffer.
5916   (let ((buffer-read-only nil))
5917     (erase-buffer)
5918     (gnus-summary-prepare-threads 
5919      (if gnus-show-threads
5920          (gnus-gather-threads 
5921           (gnus-sort-threads 
5922            (if (and gnus-summary-expunge-below
5923                     (not gnus-fetch-old-headers))
5924                (gnus-make-threads-and-expunge)
5925              (gnus-make-threads))))
5926        gnus-newsgroup-headers)
5927      'cull)
5928     (gnus-summary-update-lines)
5929     ;; Remove the final newline.
5930     ;;(goto-char (point-max))
5931     ;;(delete-char -1)
5932     ;; Call hooks for modifying summary buffer.
5933     ;; Suggested by sven@tde.LTH.Se (Sven Mattisson).
5934     (goto-char (point-min))
5935     (run-hooks 'gnus-summary-prepare-hook)))
5936
5937 (defun gnus-gather-threads (threads)
5938   "Gather threads that have lost their roots."
5939   (if (not gnus-summary-make-false-root)
5940       threads 
5941     (let ((hashtb (gnus-make-hashtable 1023))
5942           (prev threads)
5943           (result threads)
5944           subject hthread whole-subject)
5945       (while threads
5946         (setq whole-subject 
5947               (setq subject (header-subject (car (car threads)))))
5948         (if gnus-summary-gather-subject-limit
5949             (or (and (numberp gnus-summary-gather-subject-limit)
5950                      (> (length subject) gnus-summary-gather-subject-limit)
5951                      (setq subject
5952                            (substring subject 0 
5953                                       gnus-summary-gather-subject-limit)))
5954                 (and (eq 'fuzzy gnus-summary-gather-subject-limit)
5955                      (setq subject (gnus-simplify-subject-fuzzy subject))))
5956           (setq subject (gnus-simplify-subject-re subject)))
5957         (if (setq hthread 
5958                   (gnus-gethash subject hashtb))
5959             (progn
5960               (or (stringp (car (car hthread)))
5961                   (setcar hthread (list whole-subject (car hthread))))
5962               (setcdr (car hthread) (nconc (cdr (car hthread)) 
5963                                            (list (car threads))))
5964               (setcdr prev (cdr threads))
5965               (setq threads prev))
5966           (gnus-sethash subject threads hashtb))
5967         (setq prev threads)
5968         (setq threads (cdr threads)))
5969       result)))
5970
5971 (defun gnus-make-threads ()
5972   ;; This function takes the dependencies already made by 
5973   ;; `gnus-get-newsgroup-headers' and builds the trees. First we go
5974   ;; through the dependecies in the hash table and finds all the
5975   ;; roots. Roots do not refer back to any valid articles.
5976   (gnus-message 6 "Threading...")
5977   (let (roots new-roots)
5978     (and gnus-fetch-old-headers
5979          (eq gnus-headers-retrieved-by 'nov)
5980          (gnus-build-old-threads))
5981     (mapatoms
5982      (lambda (refs)
5983        (if (not (car (symbol-value refs)))
5984            (setq roots (append (cdr (symbol-value refs)) roots))
5985          ;; Ok, these refer back to valid articles, but if
5986          ;; `gnus-thread-ignore-subject' is nil, we have to check that
5987          ;; the root has the same subject as its children. The children
5988          ;; that do not are made into roots and removed from the list
5989          ;; of children. 
5990          (or gnus-thread-ignore-subject
5991              (let* ((prev (symbol-value refs))
5992                     (subject (gnus-simplify-subject-re 
5993                               (header-subject (car prev))))
5994                     (headers (cdr prev)))
5995                (while headers
5996                  (if (not (string= subject
5997                                    (gnus-simplify-subject-re 
5998                                     (header-subject (car headers)))))
5999                      (progn
6000                        (setq new-roots (cons (car headers) new-roots))
6001                        (setcdr prev (cdr headers)))
6002                    (setq prev headers))
6003                  (setq headers (cdr headers)))))))
6004      gnus-newsgroup-dependencies)
6005
6006     ;; We enter the new roots into the dependencies structure to
6007     ;; ensure that any possible later thread-regeneration will be
6008     ;; possible. 
6009     (let ((r new-roots))
6010       (while r
6011         (gnus-sethash (concat (header-id (car r)) ".boo")
6012                       (list nil (car r)) gnus-newsgroup-dependencies)
6013         (setq r (cdr r))))
6014
6015     (setq roots (nconc new-roots roots))
6016
6017     (prog1
6018         (mapcar 'gnus-trim-thread
6019                 (apply 'append
6020                        (mapcar 'gnus-cut-thread
6021                                (mapcar 'gnus-make-sub-thread roots))))
6022       (gnus-message 6 "Threading...done"))))
6023
6024   
6025 (defun gnus-make-threads-and-expunge ()
6026   ;; This function takes the dependencies already made by 
6027   ;; `gnus-get-newsgroup-headers' and builds the trees. First we go
6028   ;; through the dependecies in the hash table and finds all the
6029   ;; roots. Roots do not refer back to any valid articles.
6030   (gnus-message 6 "Threading...")
6031   (let ((default (or gnus-summary-default-score 0))
6032         (below gnus-summary-expunge-below)
6033         roots article new-roots)
6034     (and gnus-fetch-old-headers
6035          (eq gnus-headers-retrieved-by 'nov)
6036          (gnus-build-old-threads))
6037     (mapatoms
6038      (lambda (refs)
6039        (if (not (car (symbol-value refs)))
6040            ;; These articles do not refer back to any other articles -
6041            ;; they are roots.
6042            (let ((headers (cdr (symbol-value refs))))
6043              ;; We weed out the low-scored articles.
6044              (while headers
6045                (if (not (< (or (cdr (assq (header-number (car headers))
6046                                           gnus-newsgroup-scored)) default)
6047                            below))
6048                    ;; It is over.
6049                    (setq roots (cons (car headers) roots))
6050                  ;; It is below, so we mark it as read.
6051                  (setq gnus-newsgroup-unreads
6052                        (delq (header-number (car headers))
6053                              gnus-newsgroup-unreads))
6054                  (setq gnus-newsgroup-reads 
6055                        (cons (cons (header-number (car headers))
6056                                    gnus-low-score-mark) 
6057                              gnus-newsgroup-reads)))
6058                (setq headers (cdr headers))))
6059          ;; Ok, these refer back to valid articles, but if
6060          ;; `gnus-thread-ignore-subject' is nil, we have to check that
6061          ;; the root has the same subject as its children. The children
6062          ;; that do not are made into roots and removed from the list
6063          ;; of children. 
6064          (or gnus-thread-ignore-subject
6065              (let* ((prev (symbol-value refs))
6066                     (subject (gnus-simplify-subject-re 
6067                               (header-subject (car prev))))
6068                     (headers (cdr prev)))
6069                (while headers
6070                  (if (not (string= subject
6071                                    (gnus-simplify-subject-re 
6072                                     (header-subject (car headers)))))
6073                      (progn
6074                        (if (not (< (or (cdr (assq (header-number (car headers))
6075                                                   gnus-newsgroup-scored))
6076                                        default) below))
6077                            (setq new-roots (cons (car headers) new-roots))
6078                          (setq gnus-newsgroup-unreads
6079                                (delq (header-number (car headers))
6080                                      gnus-newsgroup-unreads))
6081                          (setq gnus-newsgroup-reads
6082                                (cons (cons (header-number (car headers)) 
6083                                            gnus-low-score-mark) 
6084                                      gnus-newsgroup-reads)))
6085                        (setcdr prev (cdr headers)))
6086                    (setq prev headers))
6087                  (setq headers (cdr headers)))))
6088          ;; If this article is expunged, some of the children might be
6089          ;; roots.  
6090          (if (< (or (cdr (assq (header-number (car (symbol-value refs)))
6091                                gnus-newsgroup-scored)) default)
6092                 below)
6093              (let* ((prev (symbol-value refs))
6094                     (headers (cdr prev)))
6095                (while headers
6096                  (setq article (header-number (car headers)))
6097                  (if (not (< (or (cdr (assq article gnus-newsgroup-scored))
6098                                  default) below))
6099                      (progn (setq new-roots (cons (car headers) new-roots))
6100                             (setq prev headers))
6101                    (setq gnus-newsgroup-unreads 
6102                          (delq article gnus-newsgroup-unreads))
6103                    (setq gnus-newsgroup-reads 
6104                          (cons (cons article gnus-low-score-mark) 
6105                                gnus-newsgroup-reads))
6106                    (setcdr prev (cdr headers)))
6107                  (setq headers (cdr headers))))
6108            ;; It was not expunged, but we look at expunged children.
6109            (let* ((prev (symbol-value refs))
6110                   (headers (cdr prev))
6111                   article)
6112              (while headers
6113                (setq article (header-number (car headers)))
6114                (if (not (< (or (cdr (assq article gnus-newsgroup-scored))
6115                                default) below))
6116                    (setq prev headers)
6117                  (setq gnus-newsgroup-unreads 
6118                        (delq article gnus-newsgroup-unreads))
6119                  (setq gnus-newsgroup-reads 
6120                        (cons (cons article gnus-low-score-mark)
6121                              gnus-newsgroup-reads))
6122                  (setcdr prev (cdr headers)))
6123                (setq headers (cdr headers)))))))
6124      gnus-newsgroup-dependencies)
6125
6126     ;; We enter the new roots into the dependencies structure to
6127     ;; ensure that any possible later thread-regeneration will be
6128     ;; possible. 
6129     (let ((r new-roots))
6130       (while r
6131         (gnus-sethash (concat (header-id (car r)) ".boo")
6132                       (list nil (car r)) gnus-newsgroup-dependencies)
6133         (setq r (cdr r))))
6134
6135     (setq roots (nconc new-roots roots))
6136
6137     (prog1
6138         (mapcar 'gnus-trim-thread
6139                 (apply 'append
6140                        (mapcar 'gnus-cut-thread
6141                                (mapcar 'gnus-make-sub-thread roots))))
6142       (gnus-message 6 "Threading...done"))))
6143
6144   
6145 (defun gnus-cut-thread (thread)
6146   ;; Remove leaf dormant or ancient articles from THREAD.
6147   (let ((head (car thread))
6148         (tail (apply 'append (mapcar 'gnus-cut-thread (cdr thread)))))
6149     (if (and (null tail)
6150              (let ((number (header-number head)))
6151                (or (memq number gnus-newsgroup-ancient)
6152                    (memq number gnus-newsgroup-dormant)
6153                    (and gnus-summary-expunge-below
6154                         (eq gnus-fetch-old-headers 'some)
6155                         (< (or (cdr (assq number gnus-newsgroup-scored))
6156                                gnus-summary-default-score 0)
6157                            gnus-summary-expunge-below)
6158                         (progn
6159                           (setq gnus-newsgroup-unreads
6160                                 (delq number gnus-newsgroup-unreads))
6161                           (setq gnus-newsgroup-reads
6162                                 (cons (cons number gnus-low-score-mark)
6163                                       gnus-newsgroup-reads))
6164                           t)))))
6165         nil
6166       (list (cons head tail)))))
6167
6168 (defun gnus-trim-thread (thread)
6169   ;; Remove root ancient articles with only one child from THREAD.
6170   (if (and (eq gnus-fetch-old-headers 'some)
6171            (memq (header-number (car thread)) gnus-newsgroup-ancient)
6172            (= (length thread) 2))
6173       (gnus-trim-thread (nth 1 thread))
6174     thread))
6175
6176 (defun gnus-make-sub-thread (root)
6177   ;; This function makes a sub-tree for a node in the tree.
6178   (let ((children (reverse (cdr (gnus-gethash (downcase (header-id root))
6179                                               gnus-newsgroup-dependencies)))))
6180     (cons root (mapcar 'gnus-make-sub-thread children))))
6181
6182 (defun gnus-build-old-threads ()
6183   ;; Look at all the articles that refer back to old articles, and
6184   ;; fetch the headers for the articles that aren't there. This will
6185   ;; build complete threads - if the roots haven't been expired by the
6186   ;; server, that is.
6187   (let (id heads)
6188     (mapatoms
6189      (lambda (refs)
6190        (if (not (car (symbol-value refs)))
6191            (progn
6192              (setq heads (cdr (symbol-value refs)))
6193              (while heads
6194                (if (not (memq (header-number (car heads))
6195                               gnus-newsgroup-dormant))
6196                    (progn
6197                      (setq id (symbol-name refs))
6198                      (while (and (setq id (gnus-build-get-header id))
6199                                  (not (car (gnus-gethash 
6200                                             id gnus-newsgroup-dependencies)))))
6201                      (setq heads nil))
6202                  (setq heads (cdr heads)))))))
6203      gnus-newsgroup-dependencies)))
6204
6205 (defun gnus-build-get-header (id)
6206   ;; Look through the buffer of NOV lines and find the header to
6207   ;; ID. Enter this line into the dependencies hash table, and return
6208   ;; the id of the parent article (if any).
6209   (let ((deps gnus-newsgroup-dependencies)
6210         found header)
6211     (prog1
6212         (save-excursion
6213           (set-buffer nntp-server-buffer)
6214           (goto-char (point-min))
6215           (while (and (not found) (search-forward id nil t))
6216             (beginning-of-line)
6217             (setq found (looking-at 
6218                          (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
6219                                  (regexp-quote id))))
6220             (or found (beginning-of-line 2)))
6221           (if found
6222               (let (ref)
6223                 (beginning-of-line)
6224                 (and
6225                  (setq header (gnus-nov-parse-line 
6226                                (read (current-buffer)) deps))
6227                  (setq ref (header-references header))
6228                  (string-match "\\(<[^>]+>\\) *$" ref)
6229                  (substring ref (match-beginning 1) (match-end 1))))))
6230       (and header
6231            (setq gnus-newsgroup-headers (cons header gnus-newsgroup-headers)
6232                  gnus-newsgroup-ancient (cons (header-number header)
6233                                               gnus-newsgroup-ancient))))))
6234
6235 ;; Re-build the thread containing ID.
6236 (defun gnus-rebuild-thread (id)
6237   (let ((dep gnus-newsgroup-dependencies)
6238         (buffer-read-only nil)
6239         parent headers refs thread art)
6240     (while (and id (setq headers
6241                          (car (setq art (gnus-gethash (downcase id) dep)))))
6242       (setq parent art)
6243       (setq id (and (setq refs (header-references headers))
6244                     (string-match "\\(<[^>]+>\\) *$" refs)
6245                     (substring refs (match-beginning 1) (match-end 1)))))
6246     (setq thread (gnus-make-sub-thread (car parent)))
6247     (gnus-rebuild-remove-articles thread)
6248     (let ((beg (point)))
6249       (gnus-summary-prepare-threads (list thread))
6250       (gnus-summary-update-lines beg (point)))))
6251
6252 ;; Delete all lines in the summary buffer that correspond to articles
6253 ;; in this thread.
6254 (defun gnus-rebuild-remove-articles (thread)
6255   (and (gnus-summary-goto-subject (header-number (car thread)))
6256        (gnus-delete-line))
6257   (mapcar (lambda (th) (gnus-rebuild-remove-articles th)) (cdr thread)))
6258
6259 (defun gnus-sort-threads (threads)
6260   ;; Sort threads as specified in `gnus-thread-sort-functions'.
6261   (let ((fun gnus-thread-sort-functions))
6262     (while fun
6263       (setq threads (sort threads (car fun))
6264             fun (cdr fun))))
6265   threads)
6266
6267 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
6268 (defmacro gnus-thread-header (thread)
6269   ;; Return header of first article in THREAD.
6270   ;; Note that THREAD must never, evr be anything else than a variable -
6271   ;; using some other form will lead to serious barfage.
6272   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
6273   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
6274   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" ; 
6275         (vector thread) 2))
6276
6277 (defun gnus-thread-sort-by-number (h1 h2)
6278   "Sort threads by root article number."
6279   (< (header-number (gnus-thread-header h1))
6280      (header-number (gnus-thread-header h2))))
6281
6282 (defun gnus-thread-sort-by-author (h1 h2)
6283   "Sort threads by root author."
6284   (string-lessp
6285    (let ((extract (funcall 
6286                    gnus-extract-address-components
6287                    (header-from (gnus-thread-header h1)))))
6288      (or (car extract) (cdr extract)))
6289    (let ((extract (funcall
6290                    gnus-extract-address-components 
6291                    (header-from (gnus-thread-header h2)))))
6292      (or (car extract) (cdr extract)))))
6293
6294 (defun gnus-thread-sort-by-subject (h1 h2)
6295   "Sort threads by root subject."
6296   (string-lessp
6297    (downcase (gnus-simplify-subject 
6298               (header-subject (gnus-thread-header h1))))
6299    (downcase (gnus-simplify-subject 
6300               (header-subject (gnus-thread-header h2))))))
6301
6302 (defun gnus-thread-sort-by-date (h1 h2)
6303   "Sort threads by root article date."
6304   (string-lessp
6305    (gnus-sortable-date (header-date (gnus-thread-header h1)))
6306    (gnus-sortable-date (header-date (gnus-thread-header h2)))))
6307
6308 (defun gnus-thread-sort-by-score (h1 h2)
6309   "Sort threads by root article score.
6310 Unscored articles will be counted as having a score of zero."
6311   (> (or (cdr (assq (header-number (gnus-thread-header h1))
6312                     gnus-newsgroup-scored))
6313          gnus-summary-default-score 0)
6314      (or (cdr (assq (header-number (gnus-thread-header h2))
6315                     gnus-newsgroup-scored))
6316          gnus-summary-default-score 0)))
6317
6318 (defun gnus-thread-sort-by-total-score (h1 h2)
6319   "Sort threads by the sum of all scores in the thread.
6320 Unscored articles will be counted as having a score of zero."
6321   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
6322
6323 (defun gnus-thread-total-score (thread)
6324   ;;  This function find the total score of THREAD.
6325   (if (consp thread)
6326       (if (stringp (car thread))
6327           (apply gnus-thread-score-function 0
6328                  (mapcar 'gnus-thread-total-score-1 (cdr thread)))
6329         (gnus-thread-total-score-1 thread))
6330     (gnus-thread-total-score-1 (list thread))))
6331
6332 (defun gnus-thread-total-score-1 (root)
6333   ;; This function find the total score of the thread below ROOT.
6334   (setq root (car root))
6335   (apply gnus-thread-score-function
6336          (or (cdr (assq (header-number root) gnus-newsgroup-scored))
6337              gnus-summary-default-score 0)
6338          (mapcar 'gnus-thread-total-score
6339                  (cdr (gnus-gethash (downcase (header-id root))
6340                                     gnus-newsgroup-dependencies)))))
6341
6342 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
6343 (defvar gnus-tmp-prev-subject "")
6344
6345 (defun gnus-summary-prepare-threads (threads &optional cull)
6346   "Prepare summary buffer from THREADS and indentation LEVEL.  
6347 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'  
6348 or a straight list of headers."
6349   (message "Generating summary...")
6350   (let ((level 0)
6351         thread header number subject stack state gnus-tmp-gathered)
6352     (if (vectorp (car threads))
6353         ;; If this is a straight (sic) list of headers, then a
6354         ;; threaded summary display isn't required, so we just create
6355         ;; an unthreaded one.
6356         (gnus-summary-prepare-unthreaded threads cull)
6357
6358       ;; Do the threaded display.
6359
6360       (while (or threads stack)
6361         
6362         (if threads
6363             ;; If there are some threads, we do them before the
6364             ;; threads on the stack.
6365             (setq thread threads
6366                   header (car (car thread)))
6367           ;; There were no current threads, so we pop something off
6368           ;; the stack. 
6369           (setq state (car stack)
6370                 level (car state)
6371                 thread (cdr state)
6372                 stack (cdr stack)
6373                 header (car (car thread))))
6374
6375         (if (stringp header)
6376             (progn
6377               ;; The header is a dummy root.
6378               (cond 
6379                ((eq gnus-summary-make-false-root 'adopt)
6380                 ;; We let the first article adopt the rest.
6381                 (let ((th (car (cdr (car thread)))))
6382                   (while (cdr th)
6383                     (setq th (cdr th)))
6384                   (setcdr th (cdr (cdr (car thread))))
6385                   (setq gnus-tmp-gathered 
6386                         (nconc (mapcar
6387                                 (lambda (h) (header-number (car h)))
6388                                 (cdr (cdr (car thread))))
6389                                gnus-tmp-gathered))
6390                   (setcdr (cdr (car thread)) nil))
6391                 (setq level -1))
6392                ((eq gnus-summary-make-false-root 'empty)
6393                 ;; We print adopted articles with empty subject fields.
6394                 (setq gnus-tmp-gathered 
6395                       (nconc (mapcar
6396                               (lambda (h) (header-number (car h)))
6397                               (cdr (cdr (car thread))))
6398                              gnus-tmp-gathered))
6399                 (setq level -1))
6400                ((eq gnus-summary-make-false-root 'dummy)
6401                 ;; We output a dummy root.
6402                 (gnus-summary-insert-dummy-line 
6403                  nil header (header-number (car (car (cdr (car thread)))))))
6404                (t
6405                 ;; We do not make a root for the gathered
6406                 ;; sub-threads at all.  
6407                 (setq level -1))))
6408       
6409           (setq number (header-number header)
6410                 subject (header-subject header))
6411
6412           ;; Do the async thing.
6413           (and gnus-newsgroup-async
6414                (setq gnus-newsgroup-threads
6415                      (cons (cons number (header-lines header)) 
6416                            gnus-newsgroup-threads)))
6417
6418           ;; We may have to root out some bad articles...
6419           (and cull
6420                (= level 0)
6421                (cond ((and (memq (setq number (header-number header))
6422                                  gnus-newsgroup-dormant)
6423                            (null thread))
6424                       (setq header nil))
6425                      ((and gnus-summary-expunge-below
6426                            (< (or (cdr (assq number gnus-newsgroup-scored))
6427                                   gnus-summary-default-score 0)
6428                               gnus-summary-expunge-below))
6429                       (setq header nil)
6430                       (setq gnus-newsgroup-unreads 
6431                             (delq number gnus-newsgroup-unreads))
6432                       (setq gnus-newsgroup-reads
6433                             (cons (cons number gnus-low-score-mark)
6434                                   gnus-newsgroup-reads)))))
6435           
6436           (and
6437            header
6438            (progn
6439              (inline
6440                (gnus-summary-insert-line
6441                 nil header level nil 
6442                 (cond 
6443                  ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
6444                  ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
6445                  ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
6446                  ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
6447                  (t (or (cdr (assq number gnus-newsgroup-reads))
6448                         gnus-ancient-mark)))
6449                 (memq number gnus-newsgroup-replied)
6450                 (memq number gnus-newsgroup-expirable)
6451                 (if (and (eq gnus-summary-make-false-root 'empty)
6452                          (memq number gnus-tmp-gathered))
6453                     gnus-summary-same-subject
6454                   (if (or (zerop level)
6455                           (and gnus-thread-ignore-subject
6456                                (not (string= 
6457                                      (gnus-simplify-subject-re
6458                                       gnus-tmp-prev-subject)
6459                                      (gnus-simplify-subject-re
6460                                       subject)))))
6461                       subject
6462                     gnus-summary-same-subject))
6463                 (and (eq gnus-summary-make-false-root 'adopt)
6464                      (memq number gnus-tmp-gathered))
6465                 (cdr (assq number gnus-newsgroup-scored)))
6466
6467                (setq gnus-tmp-prev-subject subject)))))
6468
6469         (if (nth 1 thread) 
6470             (setq stack (cons (cons (max 0 level) (nthcdr 1 thread)) stack)))
6471         (setq level (1+ level))
6472         (setq threads (cdr (car thread))))))
6473   (message "Generating summary...done"))
6474
6475
6476
6477 (defun gnus-summary-prepare-unthreaded (headers &optional cull)
6478   (let (header number)
6479
6480     ;; Do the async thing, if that is required.
6481     (if gnus-newsgroup-async
6482         (setq gnus-newsgroup-threads
6483               (mapcar (lambda (h) (cons (header-number h) (header-lines h)))
6484                       headers)))
6485
6486     (while headers
6487       (setq header (car headers)
6488             headers (cdr headers)
6489             number (header-number header))
6490
6491       ;; We may have to root out some bad articles...
6492       (cond 
6493        ((and cull
6494              (memq (setq number (header-number header))
6495                    gnus-newsgroup-dormant)))
6496        ((and cull gnus-summary-expunge-below
6497              (< (or (cdr (assq number gnus-newsgroup-scored))
6498                     gnus-summary-default-score 0)
6499                 gnus-summary-expunge-below))
6500         (setq gnus-newsgroup-unreads 
6501               (delq number gnus-newsgroup-unreads))
6502         (setq gnus-newsgroup-reads
6503               (cons (cons number gnus-low-score-mark)
6504                     gnus-newsgroup-reads)))
6505        (t
6506         (gnus-summary-insert-line
6507          nil header 0 nil 
6508          (cond ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
6509                ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
6510                ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
6511                ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
6512                (t (or (cdr (assq number gnus-newsgroup-reads))
6513                       gnus-ancient-mark)))
6514          (memq number gnus-newsgroup-replied)
6515          (memq number gnus-newsgroup-expirable)
6516          (header-subject header) nil
6517          (cdr (assq number gnus-newsgroup-scored))))))))
6518
6519 (defun gnus-select-newsgroup (group &optional read-all)
6520   "Select newsgroup GROUP.
6521 If READ-ALL is non-nil, all articles in the group are selected."
6522   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
6523          (info (nth 2 entry))
6524          articles)
6525     (gnus-check-server
6526      (setq gnus-current-select-method (gnus-find-method-for-group group)))
6527
6528     (or (gnus-check-server gnus-current-select-method)
6529         (error "Couldn't open server"))
6530     
6531     (or (and (null entry)
6532              (gnus-activate-group group))
6533         (and (eq (car entry) t)
6534              (gnus-activate-group (car info)))
6535         (gnus-request-group group t)
6536         (progn
6537           (kill-buffer (current-buffer))
6538           (error "Couldn't request group %s: %s" 
6539                  group (gnus-status-message group))))
6540
6541     (setq gnus-newsgroup-name group)
6542     (setq gnus-newsgroup-unselected nil)
6543     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
6544
6545     (and gnus-asynchronous
6546          (gnus-check-backend-function 
6547           'request-asynchronous gnus-newsgroup-name)
6548          (setq gnus-newsgroup-async
6549                (gnus-request-asynchronous gnus-newsgroup-name)))
6550
6551     (setq articles (gnus-articles-to-read group read-all))
6552
6553     (cond 
6554      ((null articles) 
6555       (gnus-message 3 "Couldn't select newsgroup")
6556       'quit)
6557      ((eq articles 0) nil)
6558      (t
6559       ;; Init the dependencies hash table.
6560       (setq gnus-newsgroup-dependencies 
6561             (gnus-make-hashtable (length articles)))
6562       ;; Retrieve the headers and read them in.
6563       (gnus-message 5 "Fetching headers...")
6564       (setq gnus-newsgroup-headers 
6565             (if (eq 'nov (setq gnus-headers-retrieved-by
6566                                ;; This is a naughty hack. To get the
6567                                ;; retrieval of old headers to work, we
6568                                ;; set `nntp-nov-gap' to nil (locally),
6569                                ;; and then just retrieve the headers.
6570                                ;; Mucho magic.
6571                                (if gnus-fetch-old-headers
6572                                    (let (nntp-nov-gap)
6573                                      (gnus-retrieve-headers 
6574                                       (if (not (eq 1 (car articles)))
6575                                           (cons 1 articles)
6576                                         articles)
6577                                       gnus-newsgroup-name))
6578                                  (gnus-retrieve-headers 
6579                                   articles gnus-newsgroup-name))))
6580                 (progn
6581                   (gnus-get-newsgroup-headers-xover articles))
6582               ;; If we were to fetch old headers, but the backend didn't
6583               ;; support XOVER, then it is possible we fetched one article
6584               ;; that we shouldn't have. If that's the case, we remove it.
6585               (if (or (not gnus-fetch-old-headers)
6586                       (eq 1 (car articles)))
6587                   ()
6588                 (save-excursion
6589                   (set-buffer nntp-server-buffer)
6590                   (goto-char (point-min))
6591                   (and 
6592                    (looking-at "[0-9]+[ \t]+1[ \t]") ; This is not a NOV line.
6593                    (delete-region       ; So we delete this head.
6594                     (point) 
6595                     (search-forward "\n.\n" nil t)))))
6596               (gnus-get-newsgroup-headers)))
6597       (gnus-message 5 "Fetching headers...done")      
6598       ;; Remove canceled articles from the list of unread articles.
6599       (setq gnus-newsgroup-unreads
6600             (gnus-set-sorted-intersection 
6601              gnus-newsgroup-unreads
6602              (mapcar (lambda (headers) (header-number headers))
6603                      gnus-newsgroup-headers)))
6604       ;; Adjust and set lists of article marks.
6605       (and info
6606            (let (marked)
6607              (gnus-adjust-marked-articles info)
6608              (setq gnus-newsgroup-marked 
6609                    (copy-sequence
6610                     (cdr (assq 'tick (setq marked (nth 3 info))))))
6611              (setq gnus-newsgroup-replied 
6612                    (copy-sequence (cdr (assq 'reply marked))))
6613              (setq gnus-newsgroup-expirable
6614                    (copy-sequence (cdr (assq 'expire marked))))
6615              (setq gnus-newsgroup-killed
6616                    (copy-sequence (cdr (assq 'killed marked))))
6617              (setq gnus-newsgroup-bookmarks 
6618                    (copy-sequence (cdr (assq 'bookmark marked))))
6619              (setq gnus-newsgroup-dormant 
6620                    (copy-sequence (cdr (assq 'dormant marked))))
6621              (setq gnus-newsgroup-scored 
6622                    (copy-sequence (cdr (assq 'score marked))))
6623              (setq gnus-newsgroup-processable nil)))
6624       ;; Check whether auto-expire is to be done in this group.
6625       (setq gnus-newsgroup-auto-expire
6626             (or (and (stringp gnus-auto-expirable-newsgroups)
6627                      (string-match gnus-auto-expirable-newsgroups group))
6628                 (memq 'auto-expire (nth 5 info))))
6629       ;; First and last article in this newsgroup.
6630       (and gnus-newsgroup-headers
6631            (setq gnus-newsgroup-begin 
6632                  (header-number (car gnus-newsgroup-headers)))
6633            (setq gnus-newsgroup-end
6634                  (header-number (gnus-last-element gnus-newsgroup-headers))))
6635       (setq gnus-reffed-article-number -1)
6636       ;; GROUP is successfully selected.
6637       (or gnus-newsgroup-headers t)))))
6638
6639 (defun gnus-articles-to-read (group read-all)
6640   ;; Find out what articles the user wants to read.
6641   (let* ((articles
6642           ;; Select all articles if `read-all' is non-nil, or if all the
6643           ;; unread articles are dormant articles.
6644           (if (or (and read-all (not (numberp read-all)))
6645                   (= (length gnus-newsgroup-unreads) 
6646                      (length gnus-newsgroup-dormant)))
6647               (gnus-uncompress-range 
6648                (gnus-gethash group gnus-active-hashtb))
6649             gnus-newsgroup-unreads))
6650          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
6651          (scored (length scored-list))
6652          (number (length articles))
6653          (marked (+ (length gnus-newsgroup-marked)
6654                     (length gnus-newsgroup-dormant)))
6655          (select
6656           (cond 
6657            ((numberp read-all)
6658             read-all)
6659            (t
6660             (condition-case ()
6661                 (cond ((and (or (<= scored marked)
6662                                 (= scored number))
6663                             (numberp gnus-large-newsgroup)
6664                             (> number gnus-large-newsgroup))
6665                        (let ((input
6666                               (read-string
6667                                (format
6668                                 "How many articles from %s (default %d): "
6669                                 gnus-newsgroup-name number))))
6670                          (if (string-match "^[ \t]*$" input)
6671                              number input)))
6672                       ((and (> scored marked) (< scored number))
6673                        (let ((input
6674                               (read-string
6675                                (format 
6676                                 "%s %s (%d scored, %d total): "
6677                                 "How many articles from"
6678                                 group scored number))))
6679                          (if (string-match "^[ \t]*$" input)
6680                              number input)))
6681                       (t number))
6682               (quit nil))))))
6683     (setq select (if (stringp select) (string-to-number select) select))
6684     (if (or (null select) (zerop select))
6685         select
6686       (if (and (not (zerop scored)) (<= (abs select) scored))
6687           (progn
6688             (setq articles (sort scored-list '<))
6689             (setq number (length articles)))
6690         (setq articles (copy-sequence articles)))
6691
6692       (if (< (abs select) number)
6693           (if (< select 0) 
6694               ;; Select the N oldest articles.
6695               (setcdr (nthcdr (1- (abs select)) articles) nil)
6696             ;; Select the N most recent articles.
6697             (setq articles (nthcdr (- number select) articles))))
6698       (setq gnus-newsgroup-unselected
6699             (gnus-sorted-intersection
6700              gnus-newsgroup-unreads
6701              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
6702       articles)))
6703
6704 (defun gnus-killed-articles (killed articles)
6705   (let (out)
6706     (while articles
6707       (if (inline (gnus-member-of-range (car articles) killed))
6708           (setq out (cons (car articles) out)))
6709       (setq articles (cdr articles)))
6710     out))
6711
6712 (defun gnus-adjust-marked-articles (info &optional active)
6713   "Remove all marked articles that are no longer legal."
6714   (let ((marked-lists (nth 3 info))
6715         (active (or active (gnus-gethash (car info) gnus-active-hashtb)))
6716         m prev)
6717     ;; There are many types of marked articles.
6718     (while marked-lists
6719       (setq m (cdr (setq prev (car marked-lists))))
6720       (cond ((or (eq 'tick (car prev)) (eq 'dormant (car prev)))
6721              ;; Make sure that all ticked articles are a subset of the
6722              ;; unread/unselected articles.
6723              (while m
6724                (if (or (memq (car m) gnus-newsgroup-unreads)
6725                        (memq (car m) gnus-newsgroup-unselected))
6726                    (setq prev m)
6727                  (setcdr prev (cdr m)))
6728                (setq m (cdr m))))
6729             ((eq 'score (car prev))
6730              ;; Scored articles should be a subset of
6731              ;; unread/unselected articles. 
6732              (while m
6733                (if (or (memq (car (car m)) gnus-newsgroup-unreads)
6734                        (memq (car (car m)) gnus-newsgroup-unreads))
6735                    (setq prev m)
6736                  (setcdr prev (cdr m)))
6737                (setq m (cdr m))))
6738             ((eq 'bookmark (car prev))
6739              ;; Bookmarks should be a subset of active articles.
6740              (while m
6741                (if (< (car (car m)) (car active))
6742                    (setcdr prev (cdr m))
6743                  (setq prev m))
6744                (setq m (cdr m))))
6745             ((eq 'killed (car prev))
6746              ;; Articles that have been through the kill process are
6747              ;; to be a subset of active articles.
6748              (while (and m (< (or (and (numberp (car m)) (car m))
6749                                   (cdr (car m)))
6750                               (car active)))
6751                (setcdr prev (cdr m))
6752                (setq m (cdr m)))
6753              (if (and m (< (or (and (numberp (car m)) (car m))
6754                                (car (car m)))
6755                            (car active))) 
6756                  (setcar (if (numberp (car m)) m (car m)) (car active))))
6757             ((or (eq 'reply (car prev)) (eq 'expire (car prev)))
6758              ;; The replied and expirable articles have to be articles
6759              ;; that are active. 
6760              (while m
6761                (if (< (car m) (car active))
6762                    (setcdr prev (cdr m))
6763                  (setq prev m))
6764                (setq m (cdr m)))))
6765       (setq marked-lists (cdr marked-lists)))
6766     ;; Remove all lists that are empty.
6767     (setq marked-lists (nth 3 info))
6768     (if marked-lists
6769         (progn
6770           (while (= 1 (length (car marked-lists)))
6771             (setq marked-lists (cdr marked-lists)))
6772           (setq m (cdr (setq prev marked-lists)))
6773           (while m
6774             (if (= 1 (length (car m)))
6775                 (setcdr prev (cdr m))
6776               (setq prev m))
6777             (setq m (cdr m)))
6778           (setcar (nthcdr 3 info) marked-lists)))
6779     ;; Finally, if there are no marked lists at all left, and if there
6780     ;; are no elements after the lists in the info list, we just chop
6781     ;; the info list off before the marked lists.
6782     (and (null marked-lists) 
6783          (not (nthcdr 4 info))
6784          (setcdr (nthcdr 2 info) nil)))
6785   info)
6786
6787 (defun gnus-set-marked-articles 
6788   (info ticked replied expirable killed dormant bookmark score) 
6789   "Enter the various lists of marked articles into the newsgroup info list."
6790   (let (newmarked)
6791     (and ticked (setq newmarked (cons (cons 'tick ticked) nil)))
6792     (and replied (setq newmarked (cons (cons 'reply replied) newmarked)))
6793     (and expirable (setq newmarked (cons (cons 'expire expirable) 
6794                                          newmarked)))
6795     (and killed (setq newmarked (cons (cons 'killed killed) newmarked)))
6796     (and dormant (setq newmarked (cons (cons 'dormant dormant) newmarked)))
6797     (and bookmark (setq newmarked (cons (cons 'bookmark bookmark) 
6798                                         newmarked)))
6799     (and score (setq newmarked (cons (cons 'score score) newmarked)))
6800     (if (nthcdr 3 info)
6801         (progn
6802           (setcar (nthcdr 3 info) newmarked)
6803           (and (not newmarked)
6804                (not (nthcdr 4 info))
6805                (setcdr (nthcdr 2 info) nil)))
6806       (if newmarked
6807           (setcdr (nthcdr 2 info) (list newmarked))))))
6808
6809 (defun gnus-add-marked-articles (group type articles &optional info force)
6810   ;; Add ARTICLES of TYPE to the info of GROUP.
6811   ;; If INFO is non-nil, use that info. If FORCE is non-nil, don't
6812   ;; add, but replace marked articles of TYPE with ARTICLES.
6813   (let ((info (or info (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
6814         marked m)
6815     (or (not info)
6816         (and (not (setq marked (nthcdr 3 info)))
6817              (setcdr (nthcdr 2 info) (list (list (cons type articles)))))
6818         (and (not (setq m (assq type (car marked))))
6819              (setcar marked (cons (cons type articles) (car marked))))
6820         (if force
6821             (setcdr m articles)
6822           (nconc m articles)))))
6823          
6824 (defun gnus-set-mode-line (where)
6825   "This function sets the mode line of the article or summary buffers.
6826 If WHERE is `summary', the summary mode line format will be used."
6827   (if (memq where gnus-updated-mode-lines)
6828       (let (mode-string)
6829         (save-excursion
6830           (set-buffer gnus-summary-buffer)
6831           (let* ((mformat (if (eq where 'article) 
6832                               gnus-article-mode-line-format-spec
6833                             gnus-summary-mode-line-format-spec))
6834                  (group-name gnus-newsgroup-name)
6835                  (article-number (or gnus-current-article 0))
6836                  (unread (- (length gnus-newsgroup-unreads)
6837                             (length gnus-newsgroup-dormant)))
6838                  (unread-and-unticked 
6839                   (- unread (length gnus-newsgroup-marked)))
6840                  (unselected (length gnus-newsgroup-unselected))
6841                  (unread-and-unselected
6842                   (cond ((and (zerop unread-and-unticked)
6843                               (zerop unselected)) "")
6844                         ((zerop unselected) 
6845                          (format "{%d more}" unread-and-unticked))
6846                         (t (format "{%d(+%d) more}"
6847                                    unread-and-unticked unselected))))
6848                  (subject
6849                   (if gnus-current-headers
6850                       (header-subject gnus-current-headers) ""))
6851                  (max-len (and gnus-mode-non-string-length
6852                                (- (frame-width) gnus-mode-non-string-length)))
6853                  header) ;; passed as argument to any user-format-funcs
6854             (setq mode-string (eval mformat))
6855             (or (numberp max-len)
6856                 (setq max-len (length mode-string)))
6857             (if (< max-len 4) (setq max-len 4))
6858             (if (> (length mode-string) max-len)
6859                 ;; modified by MORIOKA Tomohiko <morioka@jaist.ac.jp>
6860                 ;;  function `substring' might cut on a middle
6861                 ;;  of multi-octet character.
6862                 (setq mode-string 
6863                       (concat (gnus-truncate-string mode-string (- max-len 3))
6864                               "...")))
6865             (setq mode-string (format (format "%%-%ds" max-len)
6866                                       mode-string))))
6867         (setq mode-line-buffer-identification mode-string)
6868         (set-buffer-modified-p t))))
6869
6870 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
6871   "Go through the HEADERS list and add all Xrefs to a hash table.
6872 The resulting hash table is returned, or nil if no Xrefs were found."
6873   (let* ((from-method (gnus-find-method-for-group from-newsgroup))
6874          (prefix (if (and 
6875                       (gnus-group-foreign-p from-newsgroup)
6876                       (not (memq 'virtual 
6877                                  (assoc (symbol-name (car from-method))
6878                                         gnus-valid-select-methods))))
6879                      (gnus-group-real-prefix from-newsgroup)))
6880          (xref-hashtb (make-vector 63 0))
6881          start group entry number xrefs header)
6882     (while headers
6883       (setq header (car headers))
6884       (if (and (setq xrefs (header-xref header))
6885                (not (memq (header-number header) unreads)))
6886           (progn
6887             (setq start 0)
6888             (while (string-match "\\([^ ]+\\):\\([0-9]+\\)" xrefs start)
6889               (setq start (match-end 0))
6890               (setq group (concat prefix (substring xrefs (match-beginning 1) 
6891                                                     (match-end 1))))
6892               (setq number 
6893                     (string-to-int (substring xrefs (match-beginning 2) 
6894                                               (match-end 2))))
6895               (if (setq entry (gnus-gethash group xref-hashtb))
6896                   (setcdr entry (cons number (cdr entry)))
6897                 (gnus-sethash group (cons number nil) xref-hashtb)))))
6898       (setq headers (cdr headers)))
6899     (if start xref-hashtb nil)))
6900
6901 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads expirable)
6902   "Look through all the headers and mark the Xrefs as read."
6903   (let ((virtual (memq 'virtual 
6904                        (assoc (symbol-name (car (gnus-find-method-for-group 
6905                                                  from-newsgroup)))
6906                               gnus-valid-select-methods)))
6907         name entry info xref-hashtb idlist method
6908         nth4)
6909     (save-excursion
6910       (set-buffer gnus-group-buffer)
6911       (if (setq xref-hashtb 
6912                 (gnus-create-xref-hashtb from-newsgroup headers unreads))
6913           (mapatoms 
6914            (lambda (group)
6915              (if (string= from-newsgroup (setq name (symbol-name group)))
6916                  ()
6917                (setq idlist (symbol-value group))
6918                ;; Dead groups are not updated.
6919                (if (and (prog1 
6920                             (setq entry (gnus-gethash name gnus-newsrc-hashtb)
6921                                   info (nth 2 entry))
6922                           (if (stringp (setq nth4 (nth 4 info)))
6923                               (setq nth4 (gnus-server-to-method nth4))))
6924                         ;; Only do the xrefs if the group has the same
6925                         ;; select method as the group we have just read.
6926                         (or (gnus-methods-equal-p 
6927                              nth4 (gnus-find-method-for-group from-newsgroup))
6928                             virtual
6929                             (equal nth4 
6930                                    (setq method (gnus-find-method-for-group 
6931                                                  from-newsgroup)))
6932                             (and (equal (car nth4) (car method))
6933                                  (equal (nth 1 nth4) (nth 1 method))))
6934                         gnus-use-cross-reference
6935                         (or (not (eq gnus-use-cross-reference t))
6936                             virtual
6937                             ;; Only do cross-references on subscribed
6938                             ;; groups, if that is what is wanted.  
6939                             (<= (nth 1 info) gnus-level-subscribed)))
6940                    (gnus-group-make-articles-read name idlist expirable))))
6941            xref-hashtb)))))
6942
6943 (defun gnus-group-make-articles-read (group articles expirable)
6944   (let* ((num 0)
6945          (entry (gnus-gethash group gnus-newsrc-hashtb))
6946          (info (nth 2 entry))
6947          (active (gnus-gethash group gnus-active-hashtb))
6948          exps expirable range)
6949     ;; First peel off all illegal article numbers.
6950     (if active
6951         (let ((ids articles)
6952               (ticked (cdr (assq 'tick (nth 3 info))))
6953               (dormant (cdr (assq 'dormant (nth 3 info))))
6954               id)
6955           (setq exps nil)
6956           (while ids
6957             (setq id (car ids))
6958             (if (or (> id (cdr active))
6959                     (< id (car active))
6960                     (memq id ticked)
6961                     (memq id dormant))
6962                 (setq articles (delq id articles)))
6963             (and (memq id expirable)
6964                  (setq exps (cons id exps)))
6965             (setq ids (cdr ids)))))
6966     ;; Update expirable articles.
6967     (gnus-add-marked-articles nil 'expirable exps info)
6968     (and active
6969          (null (nth 2 info))
6970          (> (car active) 1)
6971          (setcar (nthcdr 2 info) (cons 1 (1- (car active)))))
6972     (setcar (nthcdr 2 info)
6973             (setq range
6974                   (gnus-add-to-range 
6975                    (nth 2 info) 
6976                    (setq articles (sort articles '<)))))
6977     ;; Then we have to re-compute how many unread
6978     ;; articles there are in this group.
6979     (if active
6980         (progn
6981           (cond 
6982            ((not range)
6983             (setq num (- (1+ (cdr active)) (car active))))
6984            ((not (listp (cdr range)))
6985             (setq num (- (cdr active) (- (1+ (cdr range)) 
6986                                          (car range)))))
6987            (t
6988             (while range
6989               (if (numberp (car range))
6990                   (setq num (1+ num))
6991                 (setq num (+ num (- (1+ (cdr (car range)))
6992                                     (car (car range))))))
6993               (setq range (cdr range)))
6994             (setq num (- (cdr active) num))))
6995           ;; Update the number of unread articles.
6996           (setcar 
6997            entry 
6998            (max 0 (- num 
6999                      (length (cdr (assq 'tick (nth 3 info))))
7000                      (length 
7001                       (cdr (assq 'dormant (nth 3 info)))))))
7002           ;; Update the group buffer.
7003           (gnus-group-update-group group t)))))
7004
7005 (defun gnus-methods-equal-p (m1 m2)
7006   (let ((m1 (or m1 gnus-select-method))
7007         (m2 (or m2 gnus-select-method)))
7008     (or (equal m1 m2)
7009         (and (eq (car m1) (car m2))
7010              (or (not (memq 'address (assoc (symbol-name (car m1))
7011                                             gnus-valid-select-methods)))
7012                  (equal (nth 1 m1) (nth 1 m2)))))))
7013
7014 (defsubst gnus-header-value ()
7015   (buffer-substring (match-end 0) (gnus-point-at-eol)))
7016
7017 (defvar gnus-newsgroup-none-id 0)
7018
7019 (defun gnus-get-newsgroup-headers ()
7020   (setq gnus-article-internal-prepare-hook nil)
7021   (let ((cur nntp-server-buffer)
7022         (dependencies gnus-newsgroup-dependencies)
7023         headers id dep end ref)
7024     (save-excursion
7025       (set-buffer nntp-server-buffer)
7026       (goto-char (point-min))
7027       ;; Search to the beginning of the next header. Error messages
7028       ;; do not begin with 2 or 3.
7029       (while (re-search-forward "^[23][0-9]+ " nil t)
7030         (let ((header (make-vector 9 nil))
7031               (case-fold-search t)
7032               (p (point))
7033               in-reply-to)
7034           (setq id nil
7035                 ref nil)
7036           (header-set-number header (read cur))
7037           ;; This implementation of this function, with nine
7038           ;; search-forwards instead of the one re-search-forward and
7039           ;; a case (which basically was the old function) is actually
7040           ;; about twice as fast, even though it looks messier. You
7041           ;; can't have everything, I guess. Speed and elegance
7042           ;; doesn't always come hand in hand.
7043           (save-restriction
7044             (narrow-to-region (point) (or (save-excursion 
7045                                             (search-forward "\n.\n" nil t))
7046                                           (point)))
7047             (if (search-forward "\nfrom: " nil t)
7048                 (header-set-from header (gnus-header-value))
7049               (header-set-from header "(nobody)"))
7050             (goto-char p)
7051             (if (search-forward "\nsubject: " nil t)
7052                 (header-set-subject header (gnus-header-value))
7053               (header-set-subject header "(none)"))
7054             (goto-char p)
7055             (and (search-forward "\nxref: " nil t)
7056                  (header-set-xref header (gnus-header-value)))
7057             (goto-char p)
7058             (or (numberp (and (search-forward "\nlines: " nil t)
7059                               (header-set-lines header (read cur))))
7060                 (header-set-lines header 0))
7061             (goto-char p)
7062             (and (search-forward "\ndate: " nil t)
7063                  (header-set-date header (gnus-header-value)))
7064             (goto-char p)
7065             (if (search-forward "\nmessage-id: " nil t)
7066                 (header-set-id header (setq id (gnus-header-value)))
7067               ;; If there was no message-id, we just fake one to make
7068               ;; subsequent routines simpler.
7069               (header-set-id 
7070                header 
7071                (setq id (concat "none+" 
7072                                 (int-to-string 
7073                                  (setq gnus-newsgroup-none-id 
7074                                        (1+ gnus-newsgroup-none-id)))))))
7075             (goto-char p)
7076             (if (search-forward "\nreferences: " nil t)
7077                 (progn
7078                   (header-set-references header (gnus-header-value))
7079                   (setq end (match-end 0))
7080                   (save-excursion
7081                     (setq ref 
7082                           (downcase
7083                            (buffer-substring
7084                             (progn 
7085                               (end-of-line)
7086                               (search-backward ">" end t)
7087                               (1+ (point)))
7088                             (progn
7089                               (search-backward "<" end t)
7090                               (point)))))))
7091               ;; Get the references from the in-reply-to header if there
7092               ;; ware no references and the in-reply-to header looks
7093               ;; promising. 
7094               (if (and (search-forward "\nin-reply-to: " nil t)
7095                        (setq in-reply-to (gnus-header-value))
7096                        (string-match "<[^>]+>" in-reply-to))
7097                   (progn
7098                     (header-set-references 
7099                      header 
7100                      (setq ref (substring in-reply-to (match-beginning 0)
7101                                           (match-end 0))))
7102                     (setq ref (downcase ref)))
7103                 (setq ref "none")))
7104             ;; We do some threading while we read the headers. The
7105             ;; message-id and the last reference are both entered into
7106             ;; the same hash table. Some tippy-toeing around has to be
7107             ;; done in case an article has arrived before the article
7108             ;; which it refers to.
7109             (if (boundp (setq dep (intern (downcase id) dependencies)))
7110                 (if (car (symbol-value dep))
7111                     ;; An article with this Message-ID has already
7112                     ;; been seen, so we ignore this one, except we add
7113                     ;; any additional Xrefs (in case the two articles
7114                     ;; came from different servers.
7115                     (progn
7116                       (header-set-xref 
7117                        (car (symbol-value dep))
7118                        (concat (or (header-xref (car (symbol-value dep))) "")
7119                                (or (header-xref header) "")))
7120                       (setq header nil))
7121                   (setcar (symbol-value dep) header))
7122               (set dep (list header)))
7123             (if header
7124                 (progn
7125                   (if (boundp (setq dep (intern ref dependencies)))
7126                       (setcdr (symbol-value dep) 
7127                               (cons header (cdr (symbol-value dep))))
7128                     (set dep (list nil header)))
7129                   (setq headers (cons header headers))))
7130             (goto-char (point-max))))))
7131     (nreverse headers)))
7132
7133 ;; The following macros and functions were written by Felix Lee
7134 ;; <flee@cse.psu.edu>. 
7135
7136 (defmacro gnus-nov-read-integer ()
7137   '(prog1
7138        (if (= (following-char) ?\t)
7139            0
7140          (let ((num (condition-case nil (read buffer) (error nil))))
7141            (if (numberp num) num 0)))
7142      (or (eobp) (forward-char 1))))
7143
7144 (defmacro gnus-nov-skip-field ()
7145   '(search-forward "\t" eol 'move))
7146
7147 (defmacro gnus-nov-field ()
7148   '(buffer-substring (point) (if (gnus-nov-skip-field) (1- (point)) eol)))
7149
7150 ;; Goes through the xover lines and returns a list of vectors
7151 (defun gnus-get-newsgroup-headers-xover (sequence)
7152   "Parse the news overview data in the server buffer, and return a
7153 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
7154   ;; Get the Xref when the users reads the articles since most/some
7155   ;; NNTP servers do not include Xrefs when using XOVER.
7156   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
7157   (let ((cur nntp-server-buffer)
7158         (dependencies gnus-newsgroup-dependencies)
7159         number headers header)
7160     (save-excursion
7161       (set-buffer nntp-server-buffer)
7162       (goto-char (point-min))
7163       (while (and sequence (not (eobp)))
7164         (setq number (read cur))
7165         (while (and sequence (< (car sequence) number))
7166           (setq sequence (cdr sequence)))
7167         (and sequence 
7168              (eq number (car sequence))
7169              (progn
7170                (setq sequence (cdr sequence))
7171                (if (setq header 
7172                          (inline (gnus-nov-parse-line number dependencies)))
7173                    (setq headers (cons header headers)))))
7174         (forward-line 1))
7175       (setq headers (nreverse headers)))
7176     headers))
7177
7178 ;; This function has to be called with point after the article number
7179 ;; on the beginning of the line.
7180 (defun gnus-nov-parse-line (number dependencies)
7181   (let ((none 0)
7182         (eol (gnus-point-at-eol)) 
7183         (buffer (current-buffer))
7184         header ref id dep)
7185
7186     ;; overview: [num subject from date id refs chars lines misc]
7187     (narrow-to-region (point) eol)
7188     (forward-char)
7189
7190     (condition-case nil
7191         (setq header
7192               (vector 
7193                number                   ; number
7194                (gnus-nov-field)         ; subject
7195                (gnus-nov-field)         ; from
7196                (gnus-nov-field)         ; date
7197                (setq id (or (gnus-nov-field)
7198                             (concat "none+"
7199                                     (int-to-string 
7200                                      (setq none (1+ none)))))) ; id
7201                (progn
7202                  (save-excursion
7203                    (let ((beg (point)))
7204                      (search-forward "\t" eol)
7205                      (if (search-backward ">" beg t)
7206                          (setq ref 
7207                                (downcase 
7208                                 (buffer-substring 
7209                                  (1+ (point))
7210                                  (progn
7211                                    (search-backward "<" beg t)
7212                                    (point)))))
7213                        (setq ref nil))))
7214                  (gnus-nov-field))      ; refs
7215                (gnus-nov-read-integer)  ; chars
7216                (gnus-nov-read-integer)  ; lines
7217                (if (= (following-char) ?\n)
7218                    nil
7219                  (gnus-nov-field))      ; misc
7220                ))
7221       (error (progn 
7222                (ding)
7223                (message "Strange nov line.")
7224                (setq header nil)
7225                (goto-char eol))))
7226
7227     (widen)
7228
7229     ;; We build the thread tree.
7230     (and header
7231          (if (boundp (setq dep (intern (downcase id) dependencies)))
7232              (if (car (symbol-value dep))
7233                  ;; An article with this Message-ID has already been seen,
7234                  ;; so we ignore this one, except we add any additional
7235                  ;; Xrefs (in case the two articles came from different
7236                  ;; servers.
7237                  (progn
7238                    (header-set-xref 
7239                     (car (symbol-value dep))
7240                     (concat (or (header-xref (car (symbol-value dep))) "")
7241                             (or (header-xref header) "")))
7242                    (setq header nil))
7243                (setcar (symbol-value dep) header))
7244            (set dep (list header))))
7245     (if header
7246         (progn
7247           (if (boundp (setq dep (intern (or ref "none") 
7248                                         dependencies)))
7249               (setcdr (symbol-value dep) 
7250                       (cons header (cdr (symbol-value dep))))
7251             (set dep (list nil header)))))
7252     header))
7253
7254 (defun gnus-article-get-xrefs ()
7255   "Fill in the Xref value in `gnus-current-headers', if necessary.
7256 This is meant to be called in `gnus-article-internal-prepare-hook'."
7257   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
7258                                  gnus-current-headers)))
7259     (or (not gnus-use-cross-reference)
7260         (not headers)
7261         (and (header-xref headers)
7262              (not (string= (header-xref headers) "")))
7263         (let ((case-fold-search t)
7264               xref)
7265           (save-restriction
7266             (gnus-narrow-to-headers)
7267             (goto-char (point-min))
7268             (if (or (and (eq (downcase (following-char)) ?x)
7269                          (looking-at "Xref:"))
7270                     (search-forward "\nXref:" nil t))
7271                 (progn
7272                   (goto-char (1+ (match-end 0)))
7273                   (setq xref (buffer-substring (point) 
7274                                                (progn (end-of-line) (point))))
7275                   (header-set-xref headers xref))))))))
7276
7277 (defalias 'gnus-find-header-by-number 'gnus-get-header-by-number)
7278 (make-obsolete 'gnus-find-header-by-number 'gnus-get-header-by-number)
7279
7280 (defun gnus-make-headers-hashtable-by-number ()
7281   "Make hashtable for the variable gnus-newsgroup-headers by number."
7282   (save-excursion
7283     (set-buffer gnus-summary-buffer)
7284     (let ((headers gnus-newsgroup-headers)
7285           header)
7286       (setq gnus-newsgroup-headers-hashtb-by-number
7287             (gnus-make-hashtable (length headers)))
7288       (while headers
7289         (setq header (car headers))
7290         (gnus-sethash (int-to-string (header-number header))
7291                       header gnus-newsgroup-headers-hashtb-by-number)
7292         (setq headers (cdr headers))))))
7293
7294 (defun gnus-more-header-backward ()
7295   "Find new header backward."
7296   (let ((first (car (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
7297         (artnum gnus-newsgroup-begin)
7298         (header nil))
7299     (while (and (not header)
7300                 (> artnum first))
7301       (setq artnum (1- artnum))
7302       (setq header (gnus-read-header artnum)))
7303     header))
7304
7305 (defun gnus-more-header-forward (&optional backward)
7306   "Find new header forward.
7307 If BACKWARD, find new header backward instead."
7308   (if backward
7309       (gnus-more-header-backward)
7310     (let ((last (cdr (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
7311           (artnum gnus-newsgroup-end)
7312           (header nil))
7313       (while (and (not header)
7314                   (< artnum last))
7315         (setq artnum (1+ artnum))
7316         (setq header (gnus-read-header artnum)))
7317       header)))
7318
7319 (defun gnus-extend-newsgroup (header &optional backward)
7320   "Extend newsgroup selection with HEADER.
7321 Optional argument BACKWARD means extend toward backward."
7322   (if header
7323       (let ((artnum (header-number header)))
7324         (setq gnus-newsgroup-headers
7325               (if backward
7326                   (cons header gnus-newsgroup-headers)
7327                 (nconc gnus-newsgroup-headers (list header))))
7328         (setq gnus-newsgroup-unselected
7329               (delq artnum gnus-newsgroup-unselected))
7330         (setq gnus-newsgroup-begin (min gnus-newsgroup-begin artnum))
7331         (setq gnus-newsgroup-end (max gnus-newsgroup-end artnum)))))
7332
7333 (defun gnus-summary-work-articles (n)
7334   "Return a list of articles to be worked upon. The prefix argument,
7335 the list of process marked articles, and the current article will be
7336 taken into consideration."
7337   (let (articles)
7338     (if (and n (numberp n))
7339         (let ((backward (< n 0))
7340               (n (abs n)))
7341           (save-excursion
7342             (while (and (> n 0)
7343                         (setq articles (cons (gnus-summary-article-number) 
7344                                              articles))
7345                         (gnus-summary-search-forward nil nil backward))
7346               (setq n (1- n))))
7347           (sort articles (function <)))
7348       (or (reverse gnus-newsgroup-processable)
7349           (list (gnus-summary-article-number))))))
7350
7351 (defun gnus-summary-search-group (&optional backward use-level)
7352   "Search for next unread newsgroup.
7353 If optional argument BACKWARD is non-nil, search backward instead."
7354   (save-excursion
7355     (set-buffer gnus-group-buffer)
7356     (if (gnus-group-search-forward 
7357          backward nil (if use-level (gnus-group-group-level) nil))
7358         (gnus-group-group-name))))
7359
7360 (defun gnus-summary-best-group (&optional exclude-group)
7361   "Find the name of the best unread group.
7362 If EXCLUDE-GROUP, do not go to this group."
7363   (save-excursion
7364     (set-buffer gnus-group-buffer)
7365     (save-excursion
7366       (gnus-group-best-unread-group exclude-group))))
7367
7368 (defun gnus-subject-equal (s1 s2)
7369   (cond
7370    ((null gnus-summary-gather-subject-limit)
7371     (equal (gnus-simplify-subject-re s1)
7372            (gnus-simplify-subject-re s2)))
7373    ((eq gnus-summary-gather-subject-limit 'fuzzy)
7374     (equal (gnus-simplify-subject-fuzzy s1)
7375            (gnus-simplify-subject-fuzzy s2)))
7376    ((numberp gnus-summary-gather-subject-limit)
7377     (equal (gnus-limit-string s1 gnus-summary-gather-subject-limit)
7378            (gnus-limit-string s2 gnus-summary-gather-subject-limit)))
7379    (t
7380     (equal s1 s2))))
7381     
7382 (defun gnus-summary-search-subject (&optional backward unread subject)
7383   "Search for article forward.
7384 If BACKWARD is non-nil, search backward.
7385 If UNREAD is non-nil, only unread articles are selected.
7386 If SUBJECT is non-nil, the article which has the same subject will be
7387 searched for." 
7388   (let ((func (if backward 'previous-single-property-change
7389                 'next-single-property-change))
7390         (beg (point))
7391         (did t)
7392         pos psubject)
7393     (beginning-of-line)
7394     (and gnus-summary-check-current unread
7395          (eq (get-text-property (point) 'gnus-mark) gnus-unread-mark)
7396          (setq did nil))
7397     (if (not did)
7398         ()
7399       (forward-char (if backward (if (bobp) 0 -1) (if (eobp) 0 1)))
7400       (while
7401           (and 
7402            (setq pos (funcall func (point) 'gnus-number))
7403            (goto-char (if backward (1- pos) pos))
7404            (setq did
7405                  (not (and
7406                        (or (not unread)
7407                            (eq (get-text-property (point) 'gnus-mark)
7408                                gnus-unread-mark))
7409                        (or (not subject)
7410                            (and (setq psubject (inline (gnus-summary-subject-string)))
7411                                 (inline (gnus-subject-equal subject psubject)))))))
7412            (if backward (if (bobp) nil (forward-char -1) t)
7413              (if (eobp) nil (forward-char 1) t)))))
7414     (if did
7415         (progn (goto-char beg) nil)
7416       (prog1
7417           (get-text-property (point) 'gnus-number)
7418         (gnus-summary-position-cursor)))))
7419
7420 (defun gnus-summary-pseudo-article ()
7421   "The thread level of the article on the current line."
7422   (get-text-property (gnus-point-at-bol) 'gnus-pseudo))
7423
7424 (defalias 'gnus-summary-score 'gnus-summary-article-score)
7425 (make-obsolete 'gnus-summary-score 'gnus-summary-article-score)
7426 (defun gnus-summary-article-score ()
7427   "Return current article score."
7428   (or (cdr (assq (gnus-summary-article-number) gnus-newsgroup-scored))
7429       gnus-summary-default-score 0))
7430
7431 (defun gnus-summary-recenter ()
7432   "Center point in the summary window.
7433 If `gnus-auto-center-summary' is nil, or the article buffer isn't
7434 displayed, no centering will be performed." 
7435   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
7436   ;; Recenter only when requested. Suggested by popovich@park.cs.columbia.edu.
7437   (let* ((top (cond ((< (window-height) 4) 0)
7438                     ((< (window-height) 7) 1)
7439                     (t 2)))
7440          (height (1- (window-height)))
7441          (bottom (save-excursion (goto-char (point-max))
7442                                  (forward-line (- height))
7443                                  (point)))
7444          (window (get-buffer-window (current-buffer))))
7445     (and 
7446      ;; The user has to want it,
7447      gnus-auto-center-summary 
7448      ;; the article buffer must be displayed,
7449      (get-buffer-window gnus-article-buffer)
7450      ;; Set the window start to either `bottom', which is the biggest
7451      ;; possible valid number, or the second line from the top,
7452      ;; whichever is the least.
7453      (set-window-start
7454       window (min bottom (save-excursion (forward-line (- top)) (point)))))))
7455
7456 ;; Function written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
7457 (defun gnus-short-group-name (group &optional levels)
7458   "Collapse GROUP name LEVELS."
7459   (let* ((name "") (foreign "") (depth -1) (skip 1)
7460          (levels (or levels
7461                      (progn
7462                        (while (string-match "\\." group skip)
7463                          (setq skip (match-end 0)
7464                                depth (+ depth 1)))
7465                        depth))))
7466     (if (string-match ":" group)
7467         (setq foreign (substring group 0 (match-end 0))
7468               group (substring group (match-end 0))))
7469     (while group
7470       (if (and (string-match "\\." group) (> levels 0))
7471           (setq name (concat name (substring group 0 1))
7472                 group (substring group (match-end 0))
7473                 levels (- levels 1)
7474                 name (concat name "."))
7475         (setq name (concat foreign name group)
7476               group nil)))
7477     name))
7478
7479 (defun gnus-summary-jump-to-group (newsgroup)
7480   "Move point to NEWSGROUP in group mode buffer."
7481   ;; Keep update point of group mode buffer if visible.
7482   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
7483       (save-window-excursion
7484         ;; Take care of tree window mode.
7485         (if (get-buffer-window gnus-group-buffer)
7486             (pop-to-buffer gnus-group-buffer))
7487         (gnus-group-jump-to-group newsgroup))
7488     (save-excursion
7489       ;; Take care of tree window mode.
7490       (if (get-buffer-window gnus-group-buffer)
7491           (pop-to-buffer gnus-group-buffer)
7492         (set-buffer gnus-group-buffer))
7493       (gnus-group-jump-to-group newsgroup))))
7494
7495 ;; This function returns a list of article numbers based on the
7496 ;; difference between the ranges of read articles in this group and
7497 ;; the range of active articles.
7498 (defun gnus-list-of-unread-articles (group)
7499   (let* ((read (nth 2 (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
7500          (active (gnus-gethash group gnus-active-hashtb))
7501          (last (cdr active))
7502          first nlast unread)
7503     ;; If none are read, then all are unread. 
7504     (if (not read)
7505         (setq first (car active))
7506       ;; If the range of read articles is a single range, then the
7507       ;; first unread article is the article after the last read
7508       ;; article. Sounds logical, doesn't it?
7509       (if (not (listp (cdr read)))
7510           (setq first (1+ (cdr read)))
7511         ;; `read' is a list of ranges.
7512         (if (/= (setq nlast (or (and (numberp (car read)) (car read)) 
7513                                 (car (car read)))) 1)
7514             (setq first 1))
7515         (while read
7516           (if first 
7517               (while (< first nlast)
7518                 (setq unread (cons first unread))
7519                 (setq first (1+ first))))
7520           (setq first (1+ (if (atom (car read)) (car read) (cdr (car read)))))
7521           (setq nlast (if (atom (car (cdr read))) 
7522                           (car (cdr read))
7523                         (car (car (cdr read)))))
7524           (setq read (cdr read)))))
7525     ;; And add the last unread articles.
7526     (while (<= first last)
7527       (setq unread (cons first unread))
7528       (setq first (1+ first)))
7529     ;; Return the list of unread articles.
7530     (nreverse unread)))
7531
7532 (defun gnus-list-of-read-articles (group)
7533   (let ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
7534         (active (gnus-gethash group gnus-active-hashtb)))
7535     (and info active
7536          (gnus-sorted-complement 
7537           (gnus-uncompress-range active) 
7538           (gnus-list-of-unread-articles group)))))
7539
7540 ;; Various summary commands
7541
7542 (defun gnus-summary-universal-argument ()
7543   "Perform any operation on all articles marked with the process mark."
7544   (interactive)
7545   (gnus-set-global-variables)
7546   (let ((articles (reverse gnus-newsgroup-processable))
7547         func)
7548     (or articles (error "No articles marked"))
7549     (or (setq func (key-binding (read-key-sequence "C-c C-u")))
7550         (error "Undefined key"))
7551     (while articles
7552       (gnus-summary-goto-subject (car articles))
7553       (command-execute func)
7554       (gnus-summary-remove-process-mark (car articles))
7555       (setq articles (cdr articles)))))
7556
7557 (defun gnus-summary-toggle-truncation (&optional arg)
7558   "Toggle truncation of summary lines.
7559 With arg, turn line truncation on iff arg is positive."
7560   (interactive "P")
7561   (setq truncate-lines
7562         (if (null arg) (not truncate-lines)
7563           (> (prefix-numeric-value arg) 0)))
7564   (redraw-display))
7565
7566 (defun gnus-summary-reselect-current-group (&optional all)
7567   "Once exit and then reselect the current newsgroup.
7568 The prefix argument ALL means to select all articles."
7569   (interactive "P")
7570   (gnus-set-global-variables)
7571   (let ((current-subject (gnus-summary-article-number))
7572         (group gnus-newsgroup-name))
7573     (setq gnus-newsgroup-begin nil)
7574     (gnus-summary-exit t)
7575     ;; We have to adjust the point of group mode buffer because the
7576     ;; current point was moved to the next unread newsgroup by
7577     ;; exiting.
7578     (gnus-summary-jump-to-group group)
7579     (gnus-group-read-group all t)
7580     (gnus-summary-goto-subject current-subject)))
7581
7582 (defun gnus-summary-rescan-group (&optional all)
7583   "Exit the newsgroup, ask for new articles, and select the newsgroup."
7584   (interactive "P")
7585   (gnus-set-global-variables)
7586   ;; Fix by Ilja Weis <kult@uni-paderborn.de>.
7587   (let ((group gnus-newsgroup-name))
7588     (gnus-summary-exit)
7589     (gnus-summary-jump-to-group group)
7590     (save-excursion
7591       (set-buffer gnus-group-buffer)
7592       (gnus-group-get-new-news-this-group 1))
7593     (gnus-summary-jump-to-group group)
7594     (gnus-group-read-group all)))
7595
7596 (defun gnus-summary-update-info ()
7597   (let* ((group gnus-newsgroup-name))
7598     (if gnus-newsgroup-kill-headers
7599         (setq gnus-newsgroup-killed
7600               (gnus-compress-sequence
7601                (nconc
7602                 (gnus-set-sorted-intersection
7603                  (gnus-uncompress-range gnus-newsgroup-killed)
7604                  (setq gnus-newsgroup-unselected
7605                        (sort gnus-newsgroup-unselected '<)))
7606                 (setq gnus-newsgroup-unreads
7607                       (sort gnus-newsgroup-unreads '<))) t)))
7608     (or (listp (cdr gnus-newsgroup-killed))
7609         (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
7610     (let ((headers gnus-newsgroup-headers))
7611       (gnus-close-group group)
7612       (run-hooks 'gnus-exit-group-hook)
7613       (gnus-update-read-articles 
7614        group gnus-newsgroup-unreads gnus-newsgroup-unselected 
7615        gnus-newsgroup-marked
7616        t gnus-newsgroup-replied gnus-newsgroup-expirable
7617        gnus-newsgroup-killed gnus-newsgroup-dormant
7618        gnus-newsgroup-bookmarks 
7619        (and gnus-save-score gnus-newsgroup-scored))
7620       (and gnus-use-cross-reference
7621            (gnus-mark-xrefs-as-read 
7622             group headers gnus-newsgroup-unreads gnus-newsgroup-expirable))
7623       ;; Do adaptive scoring, and possibly save score files.
7624       (and gnus-newsgroup-adaptive
7625            (gnus-score-adaptive))
7626       (and gnus-use-scoring 
7627            (fboundp 'gnus-score-save)
7628            (funcall 'gnus-score-save))
7629       ;; Do not switch windows but change the buffer to work.
7630       (set-buffer gnus-group-buffer)
7631       (or (assoc 'quit-config (gnus-find-method-for-group gnus-newsgroup-name))
7632           (gnus-group-update-group group)))))
7633   
7634 (defun gnus-summary-exit (&optional temporary)
7635   "Exit reading current newsgroup, and then return to group selection mode.
7636 gnus-exit-group-hook is called with no arguments if that value is non-nil."
7637   (interactive)
7638   (gnus-set-global-variables)
7639   (gnus-kill-save-kill-buffer)
7640   (let* ((group gnus-newsgroup-name)
7641          (quit-config (nth 1 (assoc 'quit-config (gnus-find-method-for-group
7642                                                   gnus-newsgroup-name))))
7643          (mode major-mode)
7644          (buf (current-buffer)))
7645     (run-hooks 'gnus-summary-prepare-exit-hook)
7646     (gnus-summary-update-info) ; Make all changes in this group permanent.
7647     (set-buffer buf)
7648     (and gnus-use-cache (gnus-cache-possibly-remove-articles))
7649     ;; Make sure where I was, and go to next newsgroup.
7650     (set-buffer gnus-group-buffer)
7651     (or quit-config
7652         (progn
7653           (gnus-group-jump-to-group group)
7654           (gnus-group-next-unread-group 1)))
7655     (if temporary
7656         nil                             ;Nothing to do.
7657       ;; We set all buffer-local variables to nil. It is unclear why
7658       ;; this is needed, but if we don't, buffer-local variables are
7659       ;; not garbage-collected, it seems. This would the lead to en
7660       ;; ever-growing Emacs.
7661       (set-buffer buf)
7662       (gnus-summary-clear-local-variables)
7663       ;; We clear the global counterparts of the buffer-local
7664       ;; variables as well, just to be on the safe side.
7665       (gnus-configure-windows 'group 'force)
7666       (gnus-summary-clear-local-variables)
7667       ;; Return to group mode buffer. 
7668       (if (eq mode 'gnus-summary-mode)
7669           (gnus-kill-buffer buf))
7670       (if (get-buffer gnus-article-buffer)
7671           (bury-buffer gnus-article-buffer))
7672       (setq gnus-current-select-method gnus-select-method)
7673       (pop-to-buffer gnus-group-buffer)
7674       (if (not quit-config)
7675           (progn
7676             (gnus-group-jump-to-group group)
7677             (gnus-group-next-unread-group 1))
7678         (if (not (buffer-name (car quit-config)))
7679             (gnus-configure-windows 'group 'force)
7680           (set-buffer (car quit-config))
7681           (and (eq major-mode 'gnus-summary-mode)
7682                (gnus-set-global-variables))
7683           (gnus-configure-windows (cdr quit-config))))
7684       (run-hooks 'gnus-summary-exit-hook))))
7685
7686 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
7687 (defun gnus-summary-exit-no-update (&optional no-questions)
7688   "Quit reading current newsgroup without updating read article info."
7689   (interactive)
7690   (gnus-set-global-variables)
7691   (let* ((group gnus-newsgroup-name)
7692          (quit-config (nth 1 (assoc 'quit-config 
7693                                     (gnus-find-method-for-group group)))))
7694     (if (or no-questions
7695             gnus-expert-user
7696             (gnus-y-or-n-p "Do you really wanna quit reading this group? "))
7697         (progn
7698           (gnus-close-group group)
7699           (gnus-summary-clear-local-variables)
7700           (set-buffer gnus-group-buffer)
7701           (gnus-summary-clear-local-variables)
7702           ;; Return to group selection mode.
7703           (gnus-configure-windows 'group 'force)
7704           (if (get-buffer gnus-summary-buffer)
7705               (kill-buffer gnus-summary-buffer))
7706           (if (get-buffer gnus-article-buffer)
7707               (bury-buffer gnus-article-buffer))
7708           (if (equal (gnus-group-group-name) group)
7709               (gnus-group-next-unread-group 1))
7710           (if quit-config
7711               (progn
7712                 (if (not (buffer-name (car quit-config)))
7713                     (gnus-configure-windows 'group 'force)
7714                   (set-buffer (car quit-config))
7715                   (and (eq major-mode 'gnus-summary-mode)
7716                        (gnus-set-global-variables))
7717                   (gnus-configure-windows (cdr quit-config)))))))))
7718
7719 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
7720 (defun gnus-summary-fetch-faq (group)
7721   "Fetch the FAQ for the current group."
7722   (interactive (list gnus-newsgroup-name))
7723   (let ((gnus-faq-buffer 
7724          (find-file (concat gnus-group-faq-directory 
7725                             (gnus-group-real-name group)))))
7726   (and gnus-faq-buffer (gnus-configure-windows 'summary-faq))))
7727
7728 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
7729 (defun gnus-summary-describe-group (&optional force)
7730   "Describe the current newsgroup."
7731   (interactive "P")
7732   (gnus-group-describe-group force gnus-newsgroup-name))
7733
7734 (defun gnus-summary-describe-briefly ()
7735   "Describe summary mode commands briefly."
7736   (interactive)
7737   (gnus-message 6
7738     (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")))
7739
7740 ;; Walking around group mode buffer from summary mode.
7741
7742 (defun gnus-summary-next-group (&optional no-article target-group backward)
7743   "Exit current newsgroup and then select next unread newsgroup.
7744 If prefix argument NO-ARTICLE is non-nil, no article is selected
7745 initially. If NEXT-GROUP, go to this group. If BACKWARD, go to
7746 previous group instead."
7747   (interactive "P")
7748   (gnus-set-global-variables)
7749   (let ((current-group gnus-newsgroup-name)
7750         (current-buffer (current-buffer))
7751         entered)
7752     ;; First we semi-exit this group to update Xrefs and all variables.
7753     ;; We can't do a real exit, because the window conf must remain
7754     ;; the same in case the user is prompted for info, and we don't
7755     ;; want the window conf to change before that...
7756     (gnus-summary-exit t)
7757     (while (not entered)
7758       ;; Then we find what group we are supposed to enter.
7759       (set-buffer gnus-group-buffer)
7760       (gnus-group-jump-to-group current-group)
7761       (setq target-group 
7762             (or target-group        
7763                 (if (eq gnus-keep-same-level 'best) 
7764                     (gnus-summary-best-group gnus-newsgroup-name)
7765                   (gnus-summary-search-group backward gnus-keep-same-level))))
7766       (if (not target-group)
7767           ;; There are no further groups, so we return to the group
7768           ;; buffer.
7769           (progn
7770             (gnus-message 5 "Returning to the group buffer")
7771             (setq entered t)
7772             (set-buffer current-buffer)
7773             (gnus-summary-exit))
7774         ;; We try to enter the target group.
7775         (gnus-group-jump-to-group target-group)
7776         (let ((unreads (gnus-group-group-unread)))
7777           (if (and (or (eq t unreads)
7778                        (and unreads (not (zerop unreads))))
7779                    (gnus-summary-read-group
7780                     target-group nil no-article current-buffer))
7781               (setq entered t)
7782             (setq current-group target-group
7783                   target-group nil)))))))
7784
7785 (defun gnus-summary-next-group-old (&optional no-article group backward)
7786   "Exit current newsgroup and then select next unread newsgroup.
7787 If prefix argument NO-ARTICLE is non-nil, no article is selected initially.
7788 If BACKWARD, go to previous group instead."
7789   (interactive "P")
7790   (gnus-set-global-variables)
7791   (let ((ingroup gnus-newsgroup-name)
7792         (sumbuf (current-buffer))
7793         num)
7794     (set-buffer gnus-group-buffer)
7795     (if (and group
7796              (or (and (numberp (setq num (car (gnus-gethash
7797                                                group gnus-newsrc-hashtb))))
7798                       (< num 1))
7799                  (null num)))
7800         (progn
7801           (gnus-group-jump-to-group group)
7802           (setq group nil))
7803       (gnus-group-jump-to-group ingroup))
7804     (gnus-summary-search-group backward)
7805     (let ((group (or group (gnus-summary-search-group backward))))
7806       (set-buffer sumbuf)
7807       (gnus-summary-exit t)             ;Update all information.
7808       (if (null group)
7809           (gnus-summary-exit-no-update t)
7810         (gnus-group-jump-to-group ingroup)
7811         (setq group (gnus-summary-search-group backward))
7812         (gnus-message 5 "Selecting %s..." group)
7813         (set-buffer gnus-group-buffer)
7814         ;; We are now in group mode buffer.
7815         ;; Make sure group mode buffer point is on GROUP.
7816         (gnus-group-jump-to-group group)
7817         (if (not (eq gnus-auto-select-next 'quietly))
7818             (progn
7819               (gnus-summary-read-group group nil no-article sumbuf)
7820               (and (string= gnus-newsgroup-name ingroup)
7821                    (bufferp sumbuf) (buffer-name sumbuf)
7822                    (progn
7823                      (set-buffer (setq gnus-summary-buffer sumbuf))
7824                      (gnus-summary-exit-no-update t))))
7825           (let ((prevgroup group))
7826             (gnus-group-jump-to-group ingroup)
7827             (setq group (gnus-summary-search-group backward))
7828             (gnus-summary-read-group group nil no-article sumbuf)
7829             (while (and (string= gnus-newsgroup-name ingroup)
7830                         (bufferp sumbuf) 
7831                         (buffer-name sumbuf)
7832                         (not (string= prevgroup (gnus-group-group-name))))
7833               (set-buffer gnus-group-buffer)
7834               (gnus-summary-read-group 
7835                (setq prevgroup (gnus-group-group-name)) 
7836                nil no-article sumbuf))
7837             (and (string= prevgroup (gnus-group-group-name))
7838                  ;; We have reached the final group in the group
7839                  ;; buffer.
7840                  (progn
7841                    (if (buffer-name sumbuf)
7842                        (progn
7843                          (set-buffer sumbuf)
7844                          (gnus-summary-exit)))))))))))
7845
7846 (defun gnus-summary-prev-group (&optional no-article)
7847   "Exit current newsgroup and then select previous unread newsgroup.
7848 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7849   (interactive "P")
7850   (gnus-summary-next-group no-article nil t))
7851
7852 ;; Walking around summary lines.
7853
7854 (defun gnus-summary-first-subject (&optional unread)
7855   "Go to the first unread subject.
7856 If UNREAD is non-nil, go to the first unread article.
7857 Returns nil if there are no unread articles."
7858   (interactive "P")
7859   (prog1
7860       (cond ((not unread)
7861              (goto-char (point-min)))
7862             ((gnus-goto-char 
7863               (text-property-any 
7864                (point-min) (point-max) 'gnus-mark gnus-unread-mark))
7865              t)
7866             (t 
7867              ;; There are no unread articles.
7868              (gnus-message 3 "No more unread articles")
7869              nil))
7870     (gnus-summary-position-cursor)))
7871
7872 (defun gnus-summary-next-subject (n &optional unread dont-display)
7873   "Go to next N'th summary line.
7874 If N is negative, go to the previous N'th subject line.
7875 If UNREAD is non-nil, only unread articles are selected.
7876 The difference between N and the actual number of steps taken is
7877 returned."
7878   (interactive "p")
7879   (let ((backward (< n 0))
7880         (n (abs n)))
7881     (while (and (> n 0)
7882                 (gnus-summary-search-forward unread nil backward))
7883       (setq n (1- n)))
7884     (if (/= 0 n) (gnus-message 7 "No more%s articles"
7885                                (if unread " unread" "")))
7886     (or dont-display
7887         (progn
7888           (gnus-summary-recenter)
7889           (gnus-summary-position-cursor)))
7890   n))
7891
7892 (defun gnus-summary-next-unread-subject (n)
7893   "Go to next N'th unread summary line."
7894   (interactive "p")
7895   (gnus-summary-next-subject n t))
7896
7897 (defun gnus-summary-prev-subject (n &optional unread)
7898   "Go to previous N'th summary line.
7899 If optional argument UNREAD is non-nil, only unread article is selected."
7900   (interactive "p")
7901   (gnus-summary-next-subject (- n) unread))
7902
7903 (defun gnus-summary-prev-unread-subject (n)
7904   "Go to previous N'th unread summary line."
7905   (interactive "p")
7906   (gnus-summary-next-subject (- n) t))
7907
7908 (defun gnus-summary-goto-subject (article)
7909   "Go the subject line of ARTICLE."
7910   (interactive
7911    (list
7912     (string-to-int
7913      (completing-read "Article number: "
7914                       (mapcar
7915                        (lambda (headers)
7916                          (list
7917                           (int-to-string (header-number headers))))
7918                        gnus-newsgroup-headers)
7919                       nil 'require-match))))
7920   (or article (error "No article number"))
7921   (let ((b (point)))
7922     (if (not (gnus-goto-char (text-property-any (point-min) (point-max)
7923                                                 'gnus-number article)))
7924         ()
7925       (gnus-summary-show-thread)
7926       ;; Skip dummy articles. 
7927       (if (eq (gnus-summary-article-mark) gnus-dummy-mark)
7928           (forward-line 1))
7929       (prog1
7930           (if (not (eobp))
7931               article
7932             (goto-char b)
7933             nil)
7934         (gnus-summary-position-cursor)))))
7935
7936 ;; Walking around summary lines with displaying articles.
7937
7938 (defun gnus-summary-expand-window (&optional arg)
7939   "Make the summary buffer take up the entire Emacs frame.
7940 Given a prefix, will force an `article' buffer configuration."
7941   (interactive "P")
7942   (gnus-set-global-variables)
7943   (if arg
7944       (gnus-configure-windows 'article 'force)
7945     (gnus-configure-windows 'summary 'force)))
7946
7947 (defun gnus-summary-display-article (article &optional all-header)
7948   "Display ARTICLE in article buffer."
7949   (gnus-set-global-variables)
7950   (if (null article)
7951       nil
7952     (prog1
7953         (gnus-article-prepare article all-header)
7954       (gnus-summary-show-thread)
7955       (if (eq (gnus-summary-article-mark) gnus-dummy-mark)
7956           (progn
7957             (forward-line 1)
7958             (gnus-summary-position-cursor)))
7959       (run-hooks 'gnus-select-article-hook)
7960       (gnus-summary-recenter)
7961       (gnus-summary-goto-subject article)
7962       ;; Successfully display article.
7963       (gnus-summary-update-line)
7964       (gnus-article-set-window-start 
7965        (cdr (assq article gnus-newsgroup-bookmarks)))
7966       t)))
7967
7968 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7969   "Select the current article.
7970 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7971 non-nil, the article will be re-fetched even if it already present in
7972 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7973 be displayed."
7974   (and (not pseudo) (gnus-summary-pseudo-article)
7975        (error "This is a pseudo-article."))
7976   (let ((article (or article (gnus-summary-article-number)))
7977         (all-headers (not (not all-headers))) ;Must be T or NIL.
7978         did) 
7979     (prog1
7980         (save-excursion
7981           (set-buffer gnus-summary-buffer)
7982           (if (or (null gnus-current-article)
7983                   (null gnus-article-current)
7984                   (null (get-buffer gnus-article-buffer))
7985                   (not (eq article (cdr gnus-article-current)))
7986                   (not (equal (car gnus-article-current) gnus-newsgroup-name))
7987                   force)
7988               ;; The requested article is different from the current article.
7989               (progn
7990                 (gnus-summary-display-article article all-headers)
7991                 (setq did article))
7992             (if (or all-headers gnus-show-all-headers) 
7993                 (gnus-article-show-all-headers))
7994             nil))
7995       (if did 
7996           (gnus-article-set-window-start 
7997            (cdr (assq article gnus-newsgroup-bookmarks)))))))
7998
7999 (defun gnus-summary-set-current-mark (&optional current-mark)
8000   "Obsolete function."
8001   nil)
8002
8003 (defun gnus-summary-next-article (&optional unread subject backward)
8004   "Select the next article.
8005 If UNREAD, only unread articles are selected.
8006 If SUBJECT, only articles with SUBJECT are selected.
8007 If BACKWARD, the previous article is selected instead of the next."
8008   (interactive "P")
8009   (gnus-set-global-variables)
8010   (let (header)
8011     (cond
8012      ;; Is there such an article?
8013      ((or (gnus-summary-display-article 
8014            (gnus-summary-search-forward unread subject backward))
8015           (eq (gnus-summary-article-mark) gnus-canceled-mark))
8016       (gnus-summary-position-cursor))
8017      ;; If not, we try the first unread, if that is wanted.
8018      ((and subject
8019            gnus-auto-select-same
8020            (gnus-summary-first-unread-article))
8021       (gnus-message 6 "Wrapped"))
8022      ;; Try to get next/previous article not displayed in this group.
8023      ((and gnus-auto-extend-newsgroup
8024            (not unread) (not subject)
8025            (setq header (gnus-more-header-forward backward)))
8026       (gnus-extend-newsgroup header backward)
8027       (let ((buffer-read-only nil))
8028         (goto-char (if backward (point-min) (point-max)))
8029         (gnus-summary-prepare-threads (list header)))
8030       (gnus-summary-goto-article (if backward gnus-newsgroup-begin
8031                                    gnus-newsgroup-end)))
8032      ;; Go to next/previous group.
8033      (t
8034       (or (assoc 'quit-config (gnus-find-method-for-group gnus-newsgroup-name))
8035           (gnus-summary-jump-to-group gnus-newsgroup-name))
8036       (let ((cmd (aref (this-command-keys) 0))
8037             (group 
8038              (if (eq gnus-keep-same-level 'best) 
8039                  (gnus-summary-best-group gnus-newsgroup-name)
8040                (gnus-summary-search-group backward gnus-keep-same-level))))
8041         ;; For some reason, the group window gets selected. We change
8042         ;; it back.  
8043         (select-window (get-buffer-window (current-buffer)))
8044         ;; Keep just the event type of CMD.
8045         (and (listp cmd) (setq cmd (car cmd)))
8046         ;; Select next unread newsgroup automagically.
8047         (cond 
8048          ((not gnus-auto-select-next)
8049           (gnus-message 7 "No more%s articles" (if unread " unread" "")))
8050          ((eq gnus-auto-select-next 'quietly)
8051           ;; Select quietly.
8052           (if (assoc 'quit-config (gnus-find-method-for-group 
8053                                    gnus-newsgroup-name))
8054               (gnus-summary-exit)
8055             (gnus-message 7 "No more%s articles (%s)..."
8056                           (if unread " unread" "") 
8057                           (if group (concat "selecting " group)
8058                             "exiting"))
8059             (gnus-summary-next-group nil group backward)))
8060          (t
8061           (let ((keystrokes '(?\C-n ?\C-p))
8062                 key)
8063             (while (or (null key) (memq key keystrokes))
8064               (gnus-message 
8065                7 "No more%s articles%s" (if unread " unread" "")
8066                (if (and group (not (assoc 'quit-config
8067                                           (gnus-find-method-for-group 
8068                                            gnus-newsgroup-name))))
8069                    (format " (Type %s for %s [%s])"
8070                            (single-key-description cmd) group
8071                            (car (gnus-gethash group gnus-newsrc-hashtb)))
8072                  (format " (Type %s to exit %s)"
8073                          (single-key-description cmd)
8074                          gnus-newsgroup-name)))
8075               ;; Confirm auto selection.
8076               (let* ((event (read-event)))
8077                 (setq key (if (listp event) (car event) event))
8078                 (if (memq key keystrokes)
8079                     (let ((obuf (current-buffer)))
8080                       (switch-to-buffer gnus-group-buffer)
8081                       (and group
8082                            (gnus-group-jump-to-group group))
8083                       (condition-case ()
8084                           (execute-kbd-macro (char-to-string key))
8085                         (error (ding) nil))
8086                       (setq group (gnus-group-group-name))
8087                       (switch-to-buffer obuf)))))
8088             (if (equal key cmd)
8089                 (if (or (not group) (assoc 'quit-config
8090                                            (gnus-find-method-for-group
8091                                             gnus-newsgroup-name)))
8092                     (gnus-summary-exit)
8093                   (gnus-summary-next-group nil group backward))
8094               (setq unread-command-events (list key)))))))))))
8095
8096 (defun gnus-summary-next-unread-article ()
8097   "Select unread article after current one."
8098   (interactive)
8099   (gnus-summary-next-article t (and gnus-auto-select-same
8100                                     (gnus-summary-subject-string))))
8101
8102 (defun gnus-summary-prev-article (&optional unread subject)
8103   "Select the article after the current one.
8104 If UNREAD is non-nil, only unread articles are selected."
8105   (interactive "P")
8106   (gnus-summary-next-article unread subject t))
8107
8108 (defun gnus-summary-prev-unread-article ()
8109   "Select unred article before current one."
8110   (interactive)
8111   (gnus-summary-prev-article t (and gnus-auto-select-same
8112                                     (gnus-summary-subject-string))))
8113
8114 (defun gnus-summary-next-page (&optional lines circular)
8115   "Show next page of selected article.
8116 If end of article, select next article.
8117 Argument LINES specifies lines to be scrolled up.
8118 If CIRCULAR is non-nil, go to the start of the article instead of 
8119 instead of selecting the next article when reaching the end of the
8120 current article." 
8121   (interactive "P")
8122   (setq gnus-summary-buffer (current-buffer))
8123   (gnus-set-global-variables)
8124   (let ((article (gnus-summary-article-number))
8125         (endp nil))
8126     (gnus-configure-windows 'article)
8127     (if (or (null gnus-current-article)
8128             (null gnus-article-current)
8129             (/= article (cdr gnus-article-current))
8130             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
8131         ;; Selected subject is different from current article's.
8132         (gnus-summary-display-article article)
8133       (gnus-eval-in-buffer-window
8134        gnus-article-buffer
8135        (setq endp (gnus-article-next-page lines)))
8136       (if endp
8137           (cond (circular
8138                  (gnus-summary-beginning-of-article))
8139                 (lines
8140                  (gnus-message 3 "End of message"))
8141                 ((null lines)
8142                  (gnus-summary-next-unread-article)))))
8143     (gnus-summary-recenter)
8144     (gnus-summary-position-cursor)))
8145
8146 (defun gnus-summary-prev-page (&optional lines)
8147   "Show previous page of selected article.
8148 Argument LINES specifies lines to be scrolled down."
8149   (interactive "P")
8150   (gnus-set-global-variables)
8151   (let ((article (gnus-summary-article-number)))
8152     (gnus-configure-windows 'article)
8153     (if (or (null gnus-current-article)
8154             (null gnus-article-current)
8155             (/= article (cdr gnus-article-current))
8156             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
8157         ;; Selected subject is different from current article's.
8158         (gnus-summary-display-article article)
8159       (gnus-summary-recenter)
8160       (gnus-eval-in-buffer-window gnus-article-buffer
8161         (gnus-article-prev-page lines))))
8162   (gnus-summary-position-cursor))
8163
8164 (defun gnus-summary-scroll-up (lines)
8165   "Scroll up (or down) one line current article.
8166 Argument LINES specifies lines to be scrolled up (or down if negative)."
8167   (interactive "p")
8168   (gnus-set-global-variables)
8169   (gnus-configure-windows 'article)
8170   (or (gnus-summary-select-article nil nil 'pseudo)
8171       (gnus-eval-in-buffer-window 
8172        gnus-article-buffer
8173        (cond ((> lines 0)
8174               (if (gnus-article-next-page lines)
8175                   (gnus-message 3 "End of message")))
8176              ((< lines 0)
8177               (gnus-article-prev-page (- lines))))))
8178   (gnus-summary-recenter)
8179   (gnus-summary-position-cursor))
8180
8181 (defun gnus-summary-next-same-subject ()
8182   "Select next article which has the same subject as current one."
8183   (interactive)
8184   (gnus-set-global-variables)
8185   (gnus-summary-next-article nil (gnus-summary-subject-string)))
8186
8187 (defun gnus-summary-prev-same-subject ()
8188   "Select previous article which has the same subject as current one."
8189   (interactive)
8190   (gnus-set-global-variables)
8191   (gnus-summary-prev-article nil (gnus-summary-subject-string)))
8192
8193 (defun gnus-summary-next-unread-same-subject ()
8194   "Select next unread article which has the same subject as current one."
8195   (interactive)
8196   (gnus-set-global-variables)
8197   (gnus-summary-next-article t (gnus-summary-subject-string)))
8198
8199 (defun gnus-summary-prev-unread-same-subject ()
8200   "Select previous unread article which has the same subject as current one."
8201   (interactive)
8202   (gnus-set-global-variables)
8203   (gnus-summary-prev-article t (gnus-summary-subject-string)))
8204
8205 (defun gnus-summary-first-unread-article ()
8206   "Select the first unread article. 
8207 Return nil if there are no unread articles."
8208   (interactive)
8209   (gnus-set-global-variables)
8210   (prog1
8211       (if (gnus-summary-first-subject t)
8212           (progn
8213             (gnus-summary-show-thread)
8214             (gnus-summary-first-subject t)
8215             (gnus-summary-display-article (gnus-summary-article-number))))
8216     (gnus-summary-position-cursor)))
8217
8218 (defun gnus-summary-best-unread-article ()
8219   "Select the unread article with the highest score."
8220   (interactive)
8221   (gnus-set-global-variables)
8222   (let ((scored gnus-newsgroup-scored)
8223         (best -1000000)
8224         article art)
8225     (while scored
8226       (or (> best (cdr (car scored)))
8227           (and (memq (setq art (car (car scored))) gnus-newsgroup-unreads)
8228                (not (memq art gnus-newsgroup-marked))
8229                (not (memq art gnus-newsgroup-dormant))
8230                (if (= best (cdr (car scored)))
8231                    (setq article (min art article))
8232                  (setq article art)
8233                  (setq best (cdr (car scored))))))
8234       (setq scored (cdr scored)))
8235     (cond
8236      ((or (not article) (null gnus-newsgroup-unreads))
8237       ;; We didn't find any scored articles, so we just jump to the
8238       ;; first article. 
8239       (gnus-summary-first-unread-article))
8240      ((> best gnus-summary-default-score)
8241       ;; We found one, and it's bigger than the default score, so we
8242       ;; select it.
8243       (gnus-summary-goto-article article))
8244      (t
8245       ;; We found an article, but it has a score lower than the
8246       ;; defaults, so we try to find an article with the default
8247       ;; score. 
8248       (goto-char (point-min))
8249       (while (and (or (not (= (gnus-summary-article-mark) gnus-unread-mark))
8250                       (not (eq (cdr (assq (gnus-summary-article-number)
8251                                           gnus-newsgroup-scored))
8252                                gnus-summary-default-score)))
8253                   (zerop (forward-line 1))
8254                   (not (eobp))))
8255       (if (= (gnus-summary-article-mark) gnus-unread-mark)
8256           ;; We jump to the article we have finally found.
8257           (gnus-summary-goto-article (gnus-summary-article-number))
8258         ;; Or there were no default-scored articles.
8259         (gnus-summary-goto-article article))))
8260     (gnus-summary-position-cursor)))
8261
8262 (defun gnus-summary-goto-article (article &optional all-headers)
8263   "Fetch ARTICLE and display it if it exists.
8264 If ALL-HEADERS is non-nil, no header lines are hidden."
8265   (interactive
8266    (list
8267     (string-to-int
8268      (completing-read 
8269       "Article number: "
8270       (mapcar (lambda (headers) (list (int-to-string (header-number headers))))
8271               gnus-newsgroup-headers) 
8272       nil 'require-match))))
8273   (prog1
8274       (and (gnus-summary-goto-subject article)
8275            (gnus-summary-display-article article all-headers))
8276     (gnus-summary-position-cursor)))
8277
8278 (defun gnus-summary-goto-last-article ()
8279   "Go to the previously read article."
8280   (interactive)
8281   (prog1
8282       (and gnus-last-article
8283            (gnus-summary-goto-article gnus-last-article))
8284     (gnus-summary-position-cursor)))
8285
8286 (defun gnus-summary-pop-article (number)
8287   "Pop one article off the history and go to the previous.
8288 NUMBER articles will be popped off."
8289   (interactive "p")
8290   (let (to)
8291     (setq gnus-newsgroup-history
8292           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
8293     (if to
8294         (gnus-summary-goto-article (car to))
8295       (error "Article history empty")))
8296   (gnus-summary-position-cursor))
8297
8298 ;; Summary article oriented commands
8299
8300 (defun gnus-summary-refer-parent-article (n)
8301   "Refer parent article N times.
8302 The difference between N and the number of articles fetched is returned."
8303   (interactive "p")
8304   (gnus-set-global-variables)
8305   (while 
8306       (and 
8307        (> n 0)
8308        (let ((ref (header-references (gnus-get-header-by-num
8309                                       (gnus-summary-article-number)))))
8310          (if (and ref (not (equal ref ""))
8311                   (string-match "<[^<>]*>[ \t]*$" ref))
8312              (gnus-summary-refer-article 
8313               (substring ref (match-beginning 0) (match-end 0)))
8314            (gnus-message 1 "No references in article %d"
8315                          (gnus-summary-article-number))
8316            nil)))
8317     (setq n (1- n)))
8318   (gnus-summary-position-cursor)
8319   n)
8320     
8321 (defun gnus-summary-refer-article (message-id)
8322   "Refer article specified by MESSAGE-ID.
8323 NOTE: This command only works with newsgroups that use real or simulated NNTP."
8324   (interactive "sMessage-ID: ")
8325   (if (or (not (stringp message-id))
8326           (zerop (length message-id)))
8327       ()
8328     ;; Construct the correct Message-ID if necessary.
8329     ;; Suggested by tale@pawl.rpi.edu.
8330     (or (string-match "^<" message-id)
8331         (setq message-id (concat "<" message-id)))
8332     (or (string-match ">$" message-id)
8333         (setq message-id (concat message-id ">")))
8334     (let ((header (car (gnus-gethash (downcase message-id)
8335                                      gnus-newsgroup-dependencies))))
8336       (if header
8337           (or (gnus-summary-goto-article (header-number header))
8338               ;; The header has been read, but the article had been
8339               ;; expunged, so we insert it again.
8340               (progn
8341                 (gnus-summary-insert-line
8342                  nil header 0 nil gnus-read-mark nil nil
8343                  (header-subject header))
8344                 (forward-line -1)
8345                 (header-number header)))
8346         (let ((gnus-override-method gnus-refer-article-method)
8347               (gnus-ancient-mark gnus-read-mark)
8348               (tmp-point (window-start
8349                           (get-buffer-window gnus-article-buffer)))
8350               number tmp-buf)
8351           (and gnus-refer-article-method
8352                (gnus-check-server gnus-refer-article-method))
8353           ;; Save the old article buffer.
8354           (save-excursion
8355             (set-buffer gnus-article-buffer)
8356             (gnus-kill-buffer " *temp Article*")
8357             (setq tmp-buf (rename-buffer " *temp Article*")))
8358           (prog1
8359               (if (gnus-article-prepare 
8360                    message-id nil (gnus-read-header message-id))
8361                   (progn
8362                     (setq number (header-number gnus-current-headers))
8363                     (gnus-rebuild-thread message-id)
8364                     (gnus-summary-goto-subject number)
8365                     (gnus-summary-recenter)
8366                     (gnus-article-set-window-start 
8367                      (cdr (assq number gnus-newsgroup-bookmarks)))
8368                     message-id)
8369                 ;; We restore the old article buffer.
8370                 (save-excursion
8371                   (kill-buffer gnus-article-buffer)
8372                   (set-buffer tmp-buf)
8373                   (rename-buffer gnus-article-buffer)
8374                   (let ((buffer-read-only nil))
8375                     (and tmp-point
8376                          (set-window-start (get-buffer-window (current-buffer))
8377                                            tmp-point)))))))))))
8378
8379 (defun gnus-summary-enter-digest-group ()
8380   "Enter a digest group based on the current article."
8381   (interactive)
8382   (gnus-set-global-variables)
8383   (gnus-summary-select-article)
8384   ;; We do not want a narrowed article.
8385   (gnus-summary-stop-page-breaking)
8386   (let ((name (format "%s-%d" 
8387                       (gnus-group-prefixed-name 
8388                        gnus-newsgroup-name (list 'nndoc "")) 
8389                       gnus-current-article))
8390         (ogroup gnus-newsgroup-name)
8391         (buf (current-buffer)))
8392     (if (gnus-group-read-ephemeral-group 
8393          name (list 'nndoc name
8394                     (list 'nndoc-address (get-buffer gnus-article-buffer))
8395                     '(nndoc-article-type digest))
8396          t)
8397         (setcdr (nthcdr 4 (nth 2 (gnus-gethash name gnus-newsrc-hashtb)))
8398                 (list (list (cons 'to-group ogroup))))
8399       (switch-to-buffer buf)
8400       (gnus-set-global-variables)
8401       (gnus-configure-windows 'summary)
8402       (gnus-message 3 "Article not a digest?"))))
8403
8404 (defun gnus-summary-isearch-article ()
8405   "Do incremental search forward on current article."
8406   (interactive)
8407   (gnus-set-global-variables)
8408   (gnus-summary-select-article)
8409   (gnus-eval-in-buffer-window 
8410    gnus-article-buffer (isearch-forward)))
8411
8412 (defun gnus-summary-search-article-forward (regexp &optional backward)
8413   "Search for an article containing REGEXP forward.
8414 If BACKWARD, search backward instead."
8415   (interactive
8416    (list (read-string
8417           (format "Search article %s (regexp%s): "
8418                   (if current-prefix-arg "backward" "forward")
8419                   (if gnus-last-search-regexp
8420                       (concat ", default " gnus-last-search-regexp)
8421                     "")))
8422          current-prefix-arg))
8423   (gnus-set-global-variables)
8424   (if (string-equal regexp "")
8425       (setq regexp (or gnus-last-search-regexp ""))
8426     (setq gnus-last-search-regexp regexp))
8427   (if (gnus-summary-search-article regexp backward)
8428       (gnus-article-set-window-start 
8429        (cdr (assq (gnus-summary-article-number) gnus-newsgroup-bookmarks)))
8430     (error "Search failed: \"%s\"" regexp)))
8431
8432 (defun gnus-summary-search-article-backward (regexp)
8433   "Search for an article containing REGEXP backward."
8434   (interactive
8435    (list (read-string
8436           (format "Search article backward (regexp%s): "
8437                   (if gnus-last-search-regexp
8438                       (concat ", default " gnus-last-search-regexp)
8439                     "")))))
8440   (gnus-summary-search-article-forward regexp 'backward))
8441
8442 (defun gnus-summary-search-article (regexp &optional backward)
8443   "Search for an article containing REGEXP.
8444 Optional argument BACKWARD means do search for backward.
8445 gnus-select-article-hook is not called during the search."
8446   (let ((gnus-select-article-hook nil)  ;Disable hook.
8447         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8448         (re-search
8449          (if backward
8450              (function re-search-backward) (function re-search-forward)))
8451         (found nil)
8452         (last nil))
8453     ;; Hidden thread subtrees must be searched for ,too.
8454     (gnus-summary-show-all-threads)
8455     (if (eobp) (forward-line -1))
8456     ;; First of all, search current article.
8457     ;; We don't want to read article again from NNTP server nor reset
8458     ;; current point.
8459     (gnus-summary-select-article)
8460     (gnus-message 9 "Searching article: %d..." gnus-current-article)
8461     (setq last gnus-current-article)
8462     (gnus-eval-in-buffer-window gnus-article-buffer
8463       (save-restriction
8464         (widen)
8465         ;; Begin search from current point.
8466         (setq found (funcall re-search regexp nil t))))
8467     ;; Then search next articles.
8468     (while (and (not found)
8469                 (gnus-summary-display-article 
8470                  (gnus-summary-search-subject backward nil nil)))
8471       (gnus-message 9 "Searching article: %d..." gnus-current-article)
8472       (gnus-eval-in-buffer-window gnus-article-buffer
8473         (save-restriction
8474           (widen)
8475           (goto-char (if backward (point-max) (point-min)))
8476           (setq found (funcall re-search regexp nil t)))))
8477     (message "")
8478     ;; Adjust article pointer.
8479     (or (eq last gnus-current-article)
8480         (setq gnus-last-article last))
8481     ;; Return T if found such article.
8482     found))
8483
8484 (defun gnus-summary-execute-command (header regexp command &optional backward)
8485   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8486 If HEADER is an empty string (or nil), the match is done on the entire
8487 article. If BACKWARD (the prefix) is non-nil, search backward instead."
8488   (interactive
8489    (list (let ((completion-ignore-case t))
8490            (completing-read 
8491             "Header name: "
8492             (mapcar (lambda (string) (list string))
8493                     '("Number" "Subject" "From" "Lines" "Date"
8494                       "Message-ID" "Xref" "References"))
8495             nil 'require-match))
8496          (read-string "Regexp: ")
8497          (read-key-sequence "Command: ")
8498          current-prefix-arg))
8499   (gnus-set-global-variables)
8500   ;; Hidden thread subtrees must be searched as well.
8501   (gnus-summary-show-all-threads)
8502   ;; We don't want to change current point nor window configuration.
8503   (save-excursion
8504     (save-window-excursion
8505       (gnus-message 6 "Executing %s..." (key-description command))
8506       ;; We'd like to execute COMMAND interactively so as to give arguments.
8507       (gnus-execute header regexp
8508                     (` (lambda ()
8509                          (call-interactively '(, (key-binding command)))))
8510                     backward)
8511       (gnus-message 6 "Executing %s...done" (key-description command)))))
8512
8513 (defun gnus-summary-beginning-of-article ()
8514   "Scroll the article back to the beginning."
8515   (interactive)
8516   (gnus-set-global-variables)
8517   (gnus-summary-select-article)
8518   (gnus-configure-windows 'article)
8519   (gnus-eval-in-buffer-window
8520    gnus-article-buffer
8521    (widen)
8522    (goto-char (point-min))
8523    (and gnus-break-pages (gnus-narrow-to-page))))
8524
8525 (defun gnus-summary-end-of-article ()
8526   "Scroll to the end of the article."
8527   (interactive)
8528   (gnus-set-global-variables)
8529   (gnus-summary-select-article)
8530   (gnus-configure-windows 'article)
8531   (gnus-eval-in-buffer-window 
8532    gnus-article-buffer
8533    (widen)
8534    (goto-char (point-max))
8535    (and gnus-break-pages (gnus-narrow-to-page))))
8536
8537 (defun gnus-summary-show-article ()
8538   "Force re-fetching of the current article."
8539   (interactive)
8540   (gnus-set-global-variables)
8541   (or gnus-current-article
8542       (error "There is no current article"))
8543   (gnus-summary-goto-subject gnus-current-article)
8544   (gnus-summary-select-article nil 'force)
8545   (gnus-configure-windows 'article)
8546   (gnus-summary-position-cursor))
8547
8548 (defun gnus-summary-verbose-headers (&optional arg)
8549   "Toggle permanent full header display.
8550 If ARG is a positive number, turn header display on.
8551 If ARG is a negative number, turn header display off."
8552   (interactive "P")
8553   (gnus-set-global-variables)
8554   (gnus-summary-toggle-header arg)
8555   (setq gnus-show-all-headers
8556         (cond ((or (not (numberp arg))
8557                    (zerop arg))
8558                (not gnus-show-all-headers))
8559               ((natnump arg)
8560                t))))
8561
8562 (defun gnus-summary-toggle-header (&optional arg)
8563   "Show the headers if they are hidden, or hide them if they are shown.
8564 If ARG is a positive number, show the entire header.
8565 If ARG is a negative number, hide the unwanted header lines."
8566   (interactive "P")
8567   (gnus-set-global-variables)
8568   (save-excursion
8569     (set-buffer gnus-article-buffer)
8570     (let ((buffer-read-only nil))
8571       (if (numberp arg) 
8572           (if (> arg 0) (remove-text-properties (point-min) (point-max) 
8573                                                 gnus-hidden-properties)
8574             (if (< arg 0) (run-hooks 'gnus-article-display-hook)))
8575         (if (text-property-any (point-min) (point-max) 'invisible t)
8576             (remove-text-properties 
8577              (point-min) (point-max) gnus-hidden-properties)
8578           ;; We hide the headers. This song and dance act below is
8579           ;; done because `gnus-have-all-headers' is buffer-local to
8580           ;; the summary buffer, and we only want to temporarily
8581           ;; change it in that buffer. Ugh.
8582           (let ((have gnus-have-all-headers))
8583             (save-excursion
8584               (set-buffer gnus-summary-buffer)
8585               (setq gnus-have-all-headers nil)
8586               (save-excursion
8587                 (set-buffer gnus-article-buffer)
8588                 (run-hooks 'gnus-article-display-hook))
8589               (setq gnus-have-all-headers have)))))
8590       (set-window-point (get-buffer-window (current-buffer)) (point-min)))))
8591
8592 (defun gnus-summary-show-all-headers ()
8593   "Make all header lines visible."
8594   (interactive)
8595   (gnus-set-global-variables)
8596   (gnus-article-show-all-headers))
8597
8598 (defun gnus-summary-toggle-mime (&optional arg)
8599   "Toggle MIME processing.
8600 If ARG is a positive number, turn MIME processing on."
8601   (interactive "P")
8602   (gnus-set-global-variables)
8603   (setq gnus-show-mime
8604         (if (null arg) (not gnus-show-mime)
8605           (> (prefix-numeric-value arg) 0)))
8606   (gnus-summary-select-article t 'force))
8607
8608 (defun gnus-summary-caesar-message (&optional arg)
8609   "Caesar rotate the current article by 13.
8610 The numerical prefix specifies how manu places to rotate each letter
8611 forward."
8612   (interactive "P")
8613   (gnus-set-global-variables)
8614   (gnus-summary-select-article)
8615   (let ((mail-header-separator ""))
8616     (gnus-eval-in-buffer-window 
8617      gnus-article-buffer
8618      (save-restriction
8619        (widen)
8620        (let ((start (window-start)))
8621          (news-caesar-buffer-body arg)
8622          (set-window-start (get-buffer-window (current-buffer)) start))))))
8623
8624 (defun gnus-summary-stop-page-breaking ()
8625   "Stop page breaking in the current article."
8626   (interactive)
8627   (gnus-set-global-variables)
8628   (gnus-summary-select-article)
8629   (gnus-eval-in-buffer-window gnus-article-buffer (widen)))
8630
8631 ;; Suggested by Brian Edmonds <bedmonds@prodigy.bc.ca>.
8632
8633 (defun gnus-summary-move-article (&optional n to-newsgroup select-method)
8634   "Move the current article to a different newsgroup.
8635 If N is a positive number, move the N next articles.
8636 If N is a negative number, move the N previous articles.
8637 If N is nil and any articles have been marked with the process mark,
8638 move those articles instead.
8639 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to. 
8640 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
8641 re-spool using this method.
8642 For this function to work, both the current newsgroup and the
8643 newsgroup that you want to move to have to support the `request-move'
8644 and `request-accept' functions. (Ie. mail newsgroups at present.)"
8645   (interactive "P")
8646   (gnus-set-global-variables)
8647   (or (gnus-check-backend-function 'request-move-article gnus-newsgroup-name)
8648       (error "The current newsgroup does not support article moving"))
8649   (let ((articles (gnus-summary-work-articles n))
8650         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
8651         art-group to-method sel-met)
8652     (if (and (not to-newsgroup) (not select-method))
8653         (setq to-newsgroup
8654               (completing-read 
8655                (format "Where do you want to move %s? %s"
8656                        (if (> (length articles) 1)
8657                            (format "these %d articles" (length articles))
8658                          "this article")
8659                        (if gnus-current-move-group
8660                            (format "(%s default) " gnus-current-move-group)
8661                          ""))
8662                gnus-active-hashtb nil nil prefix)))
8663     (if to-newsgroup
8664         (progn
8665           (if (or (string= to-newsgroup "") (string= to-newsgroup prefix))
8666               (setq to-newsgroup (or gnus-current-move-group "")))
8667           (or (gnus-gethash to-newsgroup gnus-active-hashtb)
8668               (gnus-activate-group to-newsgroup)
8669               (error "No such group: %s" to-newsgroup))
8670           (setq gnus-current-move-group to-newsgroup)))
8671     (setq to-method (if select-method (list select-method "")
8672                       (gnus-find-method-for-group to-newsgroup)))
8673     (or (gnus-check-backend-function 'request-accept-article (car to-method))
8674         (error "%s does not support article copying" (car to-method)))
8675     (or (gnus-check-server to-method)
8676         (error "Can't open server %s" (car to-method)))
8677     (gnus-message 6 "Moving to %s: %s..." 
8678                   (or select-method to-newsgroup) articles)
8679     (while articles
8680       (if (setq art-group
8681                 (gnus-request-move-article 
8682                  (car articles)                   ; Article to move
8683                  gnus-newsgroup-name              ; From newsgrouo
8684                  (nth 1 (gnus-find-method-for-group 
8685                          gnus-newsgroup-name))    ; Server
8686                  (list 'gnus-request-accept-article 
8687                        (if select-method
8688                            (list 'quote select-method)
8689                          to-newsgroup)
8690                        (not (cdr articles)))     ; Accept form
8691                  (not (cdr articles))))          ; Only save nov last time
8692           (let* ((buffer-read-only nil)
8693                  (entry 
8694                   (or
8695                    (gnus-gethash (car art-group) gnus-newsrc-hashtb)
8696                    (gnus-gethash 
8697                     (gnus-group-prefixed-name 
8698                      (car art-group) 
8699                      (if select-method (list select-method "")
8700                        (gnus-find-method-for-group to-newsgroup)))
8701                     gnus-newsrc-hashtb)))
8702                  (info (nth 2 entry))
8703                  (article (car articles)))
8704             (gnus-summary-goto-subject article)
8705             (beginning-of-line)
8706             (delete-region (point) (progn (forward-line 1) (point)))
8707             ;; Update the group that has been moved to.
8708             (if (not info)
8709                 ()                      ; This group does not exist yet.
8710               (if (not (memq article gnus-newsgroup-unreads))
8711                   (setcar (cdr (cdr info))
8712                           (gnus-add-to-range (nth 2 info) 
8713                                              (list (cdr art-group)))))
8714               ;; Copy any marks over to the new group.
8715               (let ((marks '((tick . gnus-newsgroup-marked)
8716                              (dormant . gnus-newsgroup-dormant)
8717                              (expire . gnus-newsgroup-expirable)
8718                              (bookmark . gnus-newsgroup-bookmarks)
8719                              (reply . gnus-newsgroup-replied)))
8720                     (to-article (cdr art-group)))
8721                 (while marks
8722                   (if (memq article (symbol-value (cdr (car marks))))
8723                       (gnus-add-marked-articles 
8724                        (car info) (car (car marks)) (list to-article) info))
8725                   (setq marks (cdr marks)))))
8726             ;; Update marks.
8727             (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8728             (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8729             (setq gnus-newsgroup-dormant
8730                   (delq article gnus-newsgroup-dormant))
8731             (setq gnus-newsgroup-reads
8732                   (cons (cons article gnus-canceled-mark)
8733                         gnus-newsgroup-reads)))
8734         (gnus-message 1 "Couldn't move article %s" (car articles)))
8735       (gnus-summary-remove-process-mark (car articles))
8736       (setq articles (cdr articles)))))
8737
8738 (defun gnus-summary-respool-article (&optional n respool-method)
8739   "Respool the current article.
8740 The article will be squeezed through the mail spooling process again,
8741 which means that it will be put in some mail newsgroup or other
8742 depending on `nnmail-split-methods'.
8743 If N is a positive number, respool the N next articles.
8744 If N is a negative number, respool the N previous articles.
8745 If N is nil and any articles have been marked with the process mark,
8746 respool those articles instead.
8747
8748 Respooling can be done both from mail groups and \"real\" newsgroups.
8749 In the former case, the articles in question will be moved from the
8750 current group into whatever groups they are destined to.  In the
8751 latter case, they will be copied into the relevant groups."
8752   (interactive "P")
8753   (gnus-set-global-variables)
8754   (let ((respool-methods (gnus-methods-using 'respool))
8755         (methname 
8756          (symbol-name (car (gnus-find-method-for-group gnus-newsgroup-name)))))
8757     (or respool-method
8758         (setq respool-method
8759               (completing-read
8760                "What method do you want to use when respooling? "
8761                respool-methods nil t methname)))
8762     (or (string= respool-method "")
8763         (if (assoc (symbol-name
8764                     (car (gnus-find-method-for-group gnus-newsgroup-name)))
8765                    respool-methods)
8766             (gnus-summary-move-article n nil (intern respool-method))
8767           (gnus-summary-copy-article n nil (intern respool-method))))))
8768
8769 ;; Suggested by gregj@unidata.com (Gregory J. Grubbs).
8770 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
8771   "Move the current article to a different newsgroup.
8772 If N is a positive number, move the N next articles.
8773 If N is a negative number, move the N previous articles.
8774 If N is nil and any articles have been marked with the process mark,
8775 move those articles instead.
8776 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to. 
8777 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
8778 re-spool using this method.
8779 For this function to work, the newsgroup that you want to move to have
8780 to support the `request-move' and `request-accept'
8781 functions. (Ie. mail newsgroups at present.)"
8782   (interactive "P")
8783   (gnus-set-global-variables)
8784   (let ((articles (gnus-summary-work-articles n))
8785         (copy-buf (get-buffer-create "*copy work*"))
8786         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
8787         art-group to-method)
8788     (buffer-disable-undo copy-buf)
8789     (if (and (not to-newsgroup) (not select-method))
8790         (setq to-newsgroup
8791               (completing-read 
8792                (format "Where do you want to copy %s? %s"
8793                        (if (> (length articles) 1)
8794                            (format "these %d articles" (length articles))
8795                          "this article")
8796                        (if gnus-current-move-group
8797                            (format "(%s default) " gnus-current-move-group)
8798                          ""))
8799                gnus-active-hashtb nil nil prefix)))
8800     (if to-newsgroup
8801         (progn
8802           (if (or (string= to-newsgroup "") (string= to-newsgroup prefix))
8803               (setq to-newsgroup (or gnus-current-move-group "")))
8804           (or (gnus-gethash to-newsgroup gnus-active-hashtb)
8805               (gnus-activate-group to-newsgroup)
8806               (error "No such group: %s" to-newsgroup))
8807           (setq gnus-current-move-group to-newsgroup)))
8808     (setq to-method (if select-method (list select-method "")
8809                       (gnus-find-method-for-group to-newsgroup)))
8810     (or (gnus-check-backend-function 'request-accept-article (car to-method))
8811         (error "%s does not support article copying" (car to-method)))
8812     (or (gnus-check-server to-method)
8813         (error "Can't open server %s" (car to-method)))
8814     (while articles
8815       (gnus-message 6 "Copying to %s: %s..." 
8816                     (or select-method to-newsgroup) articles)
8817       (if (setq art-group
8818                 (save-excursion
8819                   (set-buffer copy-buf)
8820                   (gnus-request-article-this-buffer
8821                    (car articles) gnus-newsgroup-name)
8822                   (gnus-request-accept-article
8823                    (if select-method (quote select-method) to-newsgroup)
8824                    (not (cdr articles)))))
8825           (let* ((entry 
8826                   (or
8827                    (gnus-gethash (car art-group) gnus-newsrc-hashtb)
8828                    (gnus-gethash 
8829                     (gnus-group-prefixed-name 
8830                      (car art-group) 
8831                      (if select-method (list select-method "")
8832                        (gnus-find-method-for-group to-newsgroup)))
8833                     gnus-newsrc-hashtb)))
8834                  (info (nth 2 entry))
8835                  (article (car articles)))
8836             ;; We copy the info over to the new group.
8837             (if (not info)
8838                 ()                      ; This group does not exist (yet).
8839               (if (not (memq article gnus-newsgroup-unreads))
8840                   (setcar (cdr (cdr info))
8841                           (gnus-add-to-range (nth 2 info) 
8842                                              (list (cdr art-group)))))
8843               ;; Copy any marks over to the new group.
8844               (let ((marks '((tick . gnus-newsgroup-marked)
8845                              (dormant . gnus-newsgroup-dormant)
8846                              (expire . gnus-newsgroup-expirable)
8847                              (bookmark . gnus-newsgroup-bookmarks)
8848                              (reply . gnus-newsgroup-replied)))
8849                     (to-article (cdr art-group)))
8850                 (while marks
8851                   (if (memq article (symbol-value (cdr (car marks))))
8852                       (gnus-add-marked-articles 
8853                        (car info) (car (car marks)) (list to-article) info))
8854                   (setq marks (cdr marks))))))
8855         (gnus-message 1 "Couldn't copy article %s" (car articles)))
8856       (gnus-summary-remove-process-mark (car articles))
8857       (setq articles (cdr articles)))
8858     (kill-buffer copy-buf)))
8859
8860 (defun gnus-summary-import-article (file)
8861   "Import a random file into a mail newsgroup."
8862   (interactive "fImport file: ")
8863   (let ((group gnus-newsgroup-name)
8864         atts)
8865     (or (gnus-check-backend-function 'request-accept-article group)
8866         (error "%s does not support article importing" group))
8867     (or (file-readable-p file)
8868         (not (file-regular-p file))
8869         (error "Can't read %s" file))
8870     (save-excursion
8871       (set-buffer (get-buffer-create " *import file*"))
8872       (buffer-disable-undo (current-buffer))
8873       (erase-buffer)
8874       (insert-file-contents file)
8875       (goto-char (point-min))
8876       (if (nnheader-article-p)
8877           ()
8878         (setq atts (file-attributes file))
8879         (insert "From: " (read-string "From: ") "\n"
8880                 "Subject: " (read-string "Subject: ") "\n"
8881                 "Date: " (current-time-string (nth 5 atts)) "\n"
8882                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8883       (gnus-request-accept-article group t)
8884       (kill-buffer (current-buffer)))))
8885
8886 (defun gnus-summary-expire-articles ()
8887   "Expire all articles that are marked as expirable in the current group."
8888   (interactive)
8889   (if (not (gnus-check-backend-function 
8890             'request-expire-articles gnus-newsgroup-name))
8891       ()
8892     (let* ((info (nth 2 (gnus-gethash gnus-newsgroup-name 
8893                                       gnus-newsrc-hashtb)))
8894            (total (memq 'total-expire (nth 5 info)))
8895            (expirable (if total
8896                           (gnus-list-of-read-articles gnus-newsgroup-name)
8897                         (setq gnus-newsgroup-expirable
8898                               (sort gnus-newsgroup-expirable '<))))
8899            es)
8900       (if (not expirable)
8901           ()
8902         (gnus-message 6 "Expiring articles...")
8903         ;; The list of articles that weren't expired is returned.
8904         (setq es (gnus-request-expire-articles expirable gnus-newsgroup-name))
8905         (or total (setq gnus-newsgroup-expirable es))
8906         ;; We go through the old list of expirable, and mark all
8907         ;; really expired articles as non-existant.
8908         (or (eq es expirable) ; If nothing was expired, we don't mark.
8909             (let ((gnus-use-cache nil))
8910               (while expirable
8911                 (or (memq (car expirable) es)
8912                     (gnus-summary-mark-article
8913                      (car expirable) gnus-canceled-mark))
8914                 (setq expirable (cdr expirable)))))
8915         (gnus-message 6 "Expiring articles...done")))))
8916
8917 (defun gnus-summary-expire-articles-now ()
8918   "Expunge all expirable articles in the current group.
8919 This means that *all* articles that are marked as expirable will be
8920 deleted forever, right now."
8921   (interactive)
8922   (or gnus-expert-user
8923       (gnus-y-or-n-p
8924        "Are you really, really, really sure you want to expunge? ")
8925       (error "Phew!"))
8926   (let ((nnmail-expiry-wait -1)
8927         (nnmail-expiry-wait-function nil))
8928     (gnus-summary-expire-articles)))
8929
8930 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
8931 (defun gnus-summary-delete-article (&optional n)
8932   "Delete the N next (mail) articles.
8933 This command actually deletes articles. This is not a marking
8934 command. The article will disappear forever from you life, never to
8935 return. 
8936 If N is negative, delete backwards.
8937 If N is nil and articles have been marked with the process mark,
8938 delete these instead."
8939   (interactive "P")
8940   (or (gnus-check-backend-function 'request-expire-articles 
8941                                    gnus-newsgroup-name)
8942       (error "The current newsgroup does not support article deletion."))
8943   ;; Compute the list of articles to delete.
8944   (let ((articles (gnus-summary-work-articles n))
8945         not-deleted)
8946     (if (and gnus-novice-user
8947              (not (gnus-y-or-n-p 
8948                    (format "Do you really want to delete %s forever? "
8949                            (if (> (length articles) 1) "these articles"
8950                              "this article")))))
8951         ()
8952       ;; Delete the articles.
8953       (setq not-deleted (gnus-request-expire-articles 
8954                          articles gnus-newsgroup-name 'force))
8955       (while articles
8956         (gnus-summary-remove-process-mark (car articles))       
8957         ;; The backend might not have been able to delete the article
8958         ;; after all.  
8959         (or (memq (car articles) not-deleted)
8960             (gnus-summary-mark-article (car articles) gnus-canceled-mark))
8961         (setq articles (cdr articles))))
8962     (gnus-summary-position-cursor)
8963     not-deleted))
8964
8965 (defun gnus-summary-edit-article ()
8966   "Enter into a buffer and edit the current article.
8967 This will have permanent effect only in mail groups."
8968   (interactive)
8969   (or (gnus-check-backend-function 
8970        'request-replace-article gnus-newsgroup-name)
8971       (error "The current newsgroup does not support article editing."))
8972   (gnus-summary-select-article t)
8973   (gnus-configure-windows 'article)
8974   (select-window (get-buffer-window gnus-article-buffer))
8975   (gnus-message 6 "C-c C-c to end edits")
8976   (setq buffer-read-only nil)
8977   (text-mode)
8978   (use-local-map (copy-keymap (current-local-map)))
8979   (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
8980   (buffer-enable-undo)
8981   (widen)
8982   (goto-char (point-min))
8983   (search-forward "\n\n" nil t))
8984
8985 (defun gnus-summary-edit-article-done ()
8986   "Make edits to the current article permanent."
8987   (interactive)
8988   (let ((buf (buffer-substring-no-properties (point-min) (point-max))))
8989     (erase-buffer)
8990     (insert buf)
8991     (if (not (gnus-request-replace-article 
8992               (cdr gnus-article-current) (car gnus-article-current) 
8993               (current-buffer)))
8994         (error "Couldn't replace article.")
8995       (gnus-article-mode)
8996       (use-local-map gnus-article-mode-map)
8997       (setq buffer-read-only t)
8998       (buffer-disable-undo (current-buffer))
8999       (gnus-configure-windows 'summary))
9000       (and gnus-visual (run-hooks 'gnus-visual-mark-article-hook))))
9001
9002 (defun gnus-summary-edit-article-postpone ()
9003   "Postpone changes to the current article."
9004   (interactive)
9005   (gnus-article-mode)
9006   (use-local-map gnus-article-mode-map)
9007   (setq buffer-read-only t)
9008   (buffer-disable-undo (current-buffer))
9009   (gnus-configure-windows 'summary)
9010   (and gnus-visual (run-hooks 'gnus-visual-mark-article-hook)))
9011
9012 (defun gnus-summary-fancy-query ()
9013   "Query where the fancy respool algorithm would put this article."
9014   (interactive)
9015   (gnus-summary-select-article)
9016   (save-excursion
9017     (set-buffer gnus-article-buffer)
9018     (save-restriction
9019       (goto-char (point-min))
9020       (search-forward "\n\n")
9021       (narrow-to-region (point-min) (point))
9022       (pp-eval-expression (list 'quote (nnmail-split-fancy))))))
9023
9024 ;; Summary score commands.
9025
9026 ;; Suggested by boubaker@cenatls.cena.dgac.fr.
9027
9028 (defun gnus-summary-raise-score (n)
9029   "Raise the score of the current article by N."
9030   (interactive "p")
9031   (gnus-summary-set-score (+ (gnus-summary-article-score) n)))
9032
9033 (defun gnus-summary-set-score (n)
9034   "Set the score of the current article to N."
9035   (interactive "p")
9036   ;; Skip dummy header line.
9037   (save-excursion
9038     (gnus-summary-show-thread)
9039     (if (eq (gnus-summary-article-mark) gnus-dummy-mark)
9040         (forward-line 1))
9041     (let ((buffer-read-only nil))
9042       ;; Set score.
9043       (gnus-summary-update-mark
9044        (if (= n (or gnus-summary-default-score 0)) ? 
9045          (if (< n (or gnus-summary-default-score 0)) 
9046              gnus-score-below-mark gnus-score-over-mark)) 'score))
9047     (let* ((article (gnus-summary-article-number))
9048            (score (assq article gnus-newsgroup-scored)))
9049       (if score (setcdr score n)
9050         (setq gnus-newsgroup-scored 
9051               (cons (cons article n) gnus-newsgroup-scored))))
9052     (gnus-summary-update-line)))
9053
9054 (defun gnus-summary-current-score ()
9055   "Return the score of the current article."
9056   (interactive)
9057   (message "%s" (gnus-summary-article-score)))
9058
9059 ;; Summary marking commands.
9060
9061 (defun gnus-summary-raise-same-subject-and-select (score)
9062   "Raise articles which has the same subject with SCORE and select the next."
9063   (interactive "p")
9064   (let ((subject (gnus-summary-subject-string)))
9065     (gnus-summary-raise-score score)
9066     (while (gnus-summary-search-subject nil nil subject)
9067       (gnus-summary-raise-score score))
9068     (gnus-summary-next-article t)))
9069
9070 (defun gnus-summary-raise-same-subject (score)
9071   "Raise articles which has the same subject with SCORE."
9072   (interactive "p")
9073   (let ((subject (gnus-summary-subject-string)))
9074     (gnus-summary-raise-score score)
9075     (while (gnus-summary-search-subject nil nil subject)
9076       (gnus-summary-raise-score score))
9077     (gnus-summary-next-subject 1 t)))
9078
9079 (defun gnus-score-default (level)
9080   (if level (prefix-numeric-value level) 
9081     gnus-score-interactive-default-score))
9082
9083 (defun gnus-summary-raise-thread (&optional score)
9084   "Raise the score of the articles in the current thread with SCORE."
9085   (interactive "P")
9086   (setq score (1- (gnus-score-default score)))
9087   (let (e)
9088     (save-excursion
9089       (let ((level (gnus-summary-thread-level)))
9090         (gnus-summary-raise-score score)
9091         (while (and (zerop (gnus-summary-next-subject 1 nil t))
9092                     (> (gnus-summary-thread-level) level))
9093           (gnus-summary-raise-score score))
9094         (setq e (point))))
9095     (let ((gnus-summary-check-current t))
9096       (or (zerop (gnus-summary-next-subject 1 t))
9097           (goto-char e))))
9098   (gnus-summary-recenter)
9099   (gnus-summary-position-cursor)
9100   (gnus-set-mode-line 'summary))
9101
9102 (defun gnus-summary-lower-same-subject-and-select (score)
9103   "Raise articles which has the same subject with SCORE and select the next."
9104   (interactive "p")
9105   (gnus-summary-raise-same-subject-and-select (- score)))
9106
9107 (defun gnus-summary-lower-same-subject (score)
9108   "Raise articles which has the same subject with SCORE."
9109   (interactive "p")
9110   (gnus-summary-raise-same-subject (- score)))
9111
9112 (defun gnus-summary-lower-thread (&optional score)
9113   "Lower score of articles in the current thread with SCORE."
9114   (interactive "P")
9115   (gnus-summary-raise-thread (- (1- (gnus-score-default score)))))
9116
9117 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9118   "Mark articles which has the same subject as read, and then select the next.
9119 If UNMARK is positive, remove any kind of mark.
9120 If UNMARK is negative, tick articles."
9121   (interactive "P")
9122   (if unmark
9123       (setq unmark (prefix-numeric-value unmark)))
9124   (let ((count
9125          (gnus-summary-mark-same-subject
9126           (gnus-summary-subject-string) unmark)))
9127     ;; Select next unread article. If auto-select-same mode, should
9128     ;; select the first unread article.
9129     (gnus-summary-next-article t (and gnus-auto-select-same
9130                                       (gnus-summary-subject-string)))
9131     (gnus-message 7 "%d article%s marked as %s"
9132                   count (if (= count 1) " is" "s are")
9133                   (if unmark "unread" "read"))))
9134
9135 (defun gnus-summary-kill-same-subject (&optional unmark)
9136   "Mark articles which has the same subject as read. 
9137 If UNMARK is positive, remove any kind of mark.
9138 If UNMARK is negative, tick articles."
9139   (interactive "P")
9140   (if unmark
9141       (setq unmark (prefix-numeric-value unmark)))
9142   (let ((count
9143          (gnus-summary-mark-same-subject
9144           (gnus-summary-subject-string) unmark)))
9145     ;; If marked as read, go to next unread subject.
9146     (if (null unmark)
9147         ;; Go to next unread subject.
9148         (gnus-summary-next-subject 1 t))
9149     (gnus-message 7 "%d articles are marked as %s"
9150                   count (if unmark "unread" "read"))))
9151
9152 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9153   "Mark articles with same SUBJECT as read, and return marked number.
9154 If optional argument UNMARK is positive, remove any kinds of marks.
9155 If optional argument UNMARK is negative, mark articles as unread instead."
9156   (let ((count 1))
9157     (save-excursion
9158       (cond 
9159        ((null unmark)                   ; Mark as read.
9160         (while (and 
9161                 (progn
9162                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9163                   (gnus-summary-show-thread) t)
9164                 (gnus-summary-search-forward nil subject))
9165           (setq count (1+ count))))
9166        ((> unmark 0)                    ; Tick.
9167         (while (and
9168                 (progn
9169                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9170                   (gnus-summary-show-thread) t)
9171                 (gnus-summary-search-forward nil subject))
9172           (setq count (1+ count))))
9173        (t                               ; Mark as unread.
9174         (while (and
9175                 (progn
9176                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9177                   (gnus-summary-show-thread) t)
9178                 (gnus-summary-search-forward nil subject))
9179           (setq count (1+ count)))))
9180       (gnus-set-mode-line 'summary)
9181       ;; Return the number of marked articles.
9182       count)))
9183
9184 (defun gnus-summary-mark-as-processable (n &optional unmark)
9185   "Set the process mark on the next N articles.
9186 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9187 the process mark instead.  The difference between N and the actual
9188 number of articles marked is returned."
9189   (interactive "p")
9190   (let ((backward (< n 0))
9191         (n (abs n)))
9192   (while (and 
9193           (> n 0)
9194           (if unmark
9195               (gnus-summary-remove-process-mark (gnus-summary-article-number))
9196             (gnus-summary-set-process-mark (gnus-summary-article-number)))
9197           (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9198     (setq n (1- n)))
9199   (if (/= 0 n) (gnus-message 7 "No more articles"))
9200   (gnus-summary-recenter)
9201   (gnus-summary-position-cursor)
9202   n))
9203
9204 (defun gnus-summary-unmark-as-processable (n)
9205   "Remove the process mark from the next N articles.
9206 If N is negative, mark backward instead.  The difference between N and
9207 the actual number of articles marked is returned."
9208   (interactive "p")
9209   (gnus-summary-mark-as-processable n t))
9210
9211 (defun gnus-summary-unmark-all-processable ()
9212   "Remove the process mark from all articles."
9213   (interactive)
9214   (save-excursion
9215     (while gnus-newsgroup-processable
9216       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9217   (gnus-summary-position-cursor))
9218
9219 (defun gnus-summary-mark-as-expirable (n)
9220   "Mark N articles forward as expirable.
9221 If N is negative, mark backward instead. The difference between N and
9222 the actual number of articles marked is returned."
9223   (interactive "p")
9224   (gnus-summary-mark-forward n gnus-expirable-mark))
9225
9226 (defun gnus-summary-mark-article-as-replied (article)
9227   "Mark ARTICLE replied and update the summary line."
9228   (setq gnus-newsgroup-replied (cons article gnus-newsgroup-replied))
9229   (let ((buffer-read-only nil))
9230     (if (gnus-summary-goto-subject article)
9231         (progn
9232           (gnus-summary-update-mark gnus-replied-mark 'replied)
9233           t))))
9234
9235 (defun gnus-summary-set-bookmark (article)
9236   "Set a bookmark in current article."
9237   (interactive (list (gnus-summary-article-number)))
9238   (if (or (not (get-buffer gnus-article-buffer))
9239           (not gnus-current-article)
9240           (not gnus-article-current)
9241           (not (equal gnus-newsgroup-name (car gnus-article-current))))
9242       (error "No current article selected"))
9243   ;; Remove old bookmark, if one exists.
9244   (let ((old (assq article gnus-newsgroup-bookmarks)))
9245     (if old (setq gnus-newsgroup-bookmarks 
9246                   (delq old gnus-newsgroup-bookmarks))))
9247   ;; Set the new bookmark, which is on the form 
9248   ;; (article-number . line-number-in-body).
9249   (setq gnus-newsgroup-bookmarks 
9250         (cons 
9251          (cons article 
9252                (save-excursion
9253                  (set-buffer gnus-article-buffer)
9254                  (count-lines
9255                   (min (point)
9256                        (save-excursion
9257                          (goto-char (point-min))
9258                          (search-forward "\n\n" nil t)
9259                          (point)))
9260                   (point))))
9261          gnus-newsgroup-bookmarks))
9262   (gnus-message 6 "A bookmark has been added to the current article."))
9263
9264 (defun gnus-summary-remove-bookmark (article)
9265   "Remove the bookmark from the current article."
9266   (interactive (list (gnus-summary-article-number)))
9267   ;; Remove old bookmark, if one exists.
9268   (let ((old (assq article gnus-newsgroup-bookmarks)))
9269     (if old 
9270         (progn
9271           (setq gnus-newsgroup-bookmarks 
9272                 (delq old gnus-newsgroup-bookmarks))
9273           (gnus-message 6 "Removed bookmark."))
9274       (gnus-message 6 "No bookmark in current article."))))
9275
9276 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9277 (defun gnus-summary-mark-as-dormant (n)
9278   "Mark N articles forward as dormant.
9279 If N is negative, mark backward instead.  The difference between N and
9280 the actual number of articles marked is returned."
9281   (interactive "p")
9282   (gnus-summary-mark-forward n gnus-dormant-mark))
9283
9284 (defun gnus-summary-set-process-mark (article)
9285   "Set the process mark on ARTICLE and update the summary line."
9286   (setq gnus-newsgroup-processable (cons article gnus-newsgroup-processable))
9287   (let ((buffer-read-only nil))
9288     (if (gnus-summary-goto-subject article)
9289         (progn
9290           (gnus-summary-show-thread)
9291           (and (eq (gnus-summary-article-mark) gnus-dummy-mark)
9292                (forward-line 1))
9293           (gnus-summary-update-mark gnus-process-mark 'replied)
9294           t))))
9295
9296 (defun gnus-summary-remove-process-mark (article)
9297   "Remove the process mark from ARTICLE and update the summary line."
9298   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9299   (let ((buffer-read-only nil))
9300     (if (gnus-summary-goto-subject article)
9301         (progn
9302           (gnus-summary-show-thread)
9303           (and (eq (gnus-summary-article-mark) gnus-dummy-mark)
9304                (forward-line 1))
9305           (gnus-summary-update-mark ?  'replied)
9306           (if (memq article gnus-newsgroup-replied) 
9307               (gnus-summary-update-mark gnus-replied-mark 'replied))
9308           t))))
9309
9310 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9311   "Mark N articles as read forwards.
9312 If N is negative, mark backwards instead.
9313 Mark with MARK. If MARK is ? , ?! or ??, articles will be
9314 marked as unread. 
9315 The difference between N and the actual number of articles marked is
9316 returned."
9317   (interactive "p")
9318   (gnus-set-global-variables)
9319   (let ((backward (< n 0))
9320         (gnus-summary-goto-unread
9321          (and gnus-summary-goto-unread
9322               (not (memq mark (list gnus-unread-mark
9323                                     gnus-ticked-mark gnus-dormant-mark)))))
9324         (n (abs n))
9325         (mark (or mark gnus-del-mark)))
9326   (while (and (> n 0)
9327               (gnus-summary-mark-article nil mark no-expire)
9328               (zerop (gnus-summary-next-subject 
9329                       (if backward -1 1) gnus-summary-goto-unread t)))
9330     (setq n (1- n)))
9331   (if (/= 0 n) (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9332   (gnus-summary-recenter)
9333   (gnus-summary-position-cursor)
9334   (gnus-set-mode-line 'summary)
9335   n))
9336
9337 (defun gnus-summary-mark-article-as-read (mark)
9338   "Mark the current article quickly as read with MARK."
9339   (let ((article (gnus-summary-article-number)))
9340     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9341     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9342     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9343     (setq gnus-newsgroup-reads
9344           (cons (cons article mark) gnus-newsgroup-reads))
9345     ;; Possibly remove from cache, if that is used. 
9346     (and gnus-use-cache (gnus-cache-enter-remove-article article))
9347     (and gnus-newsgroup-auto-expire 
9348          (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
9349              (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
9350              (= mark gnus-read-mark))
9351          (progn
9352            (setq mark gnus-expirable-mark)
9353            (setq gnus-newsgroup-expirable 
9354                  (cons article gnus-newsgroup-expirable))))
9355     (while (eq (gnus-summary-article-mark) gnus-dummy-mark)
9356       (forward-line 1))
9357     ;; Fix the mark.
9358     (gnus-summary-update-mark mark 'unread)
9359     t))
9360
9361 (defun gnus-summary-mark-article-as-unread (mark)
9362   "Mark the current article quickly as unread with MARK."
9363   (let ((article (gnus-summary-article-number)))
9364     (or (memq article gnus-newsgroup-unreads)
9365         (setq gnus-newsgroup-unreads (cons article gnus-newsgroup-unreads)))
9366     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9367     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9368     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9369     (setq gnus-newsgroup-reads
9370           (delq (assq article gnus-newsgroup-reads)
9371                 gnus-newsgroup-reads))
9372     (if (= mark gnus-ticked-mark)
9373         (setq gnus-newsgroup-marked (cons article gnus-newsgroup-marked)))
9374     (if (= mark gnus-dormant-mark)
9375         (setq gnus-newsgroup-dormant (cons article gnus-newsgroup-dormant)))
9376
9377     ;; See whether the article is to be put in the cache.
9378     (and gnus-use-cache
9379          (vectorp (gnus-get-header-by-num article))
9380          (save-excursion
9381            (gnus-cache-possibly-enter-article 
9382             gnus-newsgroup-name article 
9383             (gnus-get-header-by-num article)
9384             (= mark gnus-ticked-mark)
9385             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9386
9387     (while (eq (gnus-summary-article-mark) gnus-dummy-mark)
9388       (forward-line 1))
9389     ;; Fix the mark.
9390     (gnus-summary-update-mark mark 'unread)
9391     t))
9392
9393 (defun gnus-summary-mark-article (&optional article mark no-expire)
9394   "Mark ARTICLE with MARK.  MARK can be any character.
9395 Four MARK strings are reserved: `? ' (unread), `?!' (ticked), `??'
9396 (dormant) and `?E' (expirable).
9397 If MARK is nil, then the default character `?D' is used.
9398 If ARTICLE is nil, then the article on the current line will be
9399 marked." 
9400   (and (stringp mark)
9401        (setq mark (aref mark 0)))
9402   ;; If no mark is given, then we check auto-expiring.
9403   (and (not no-expire)
9404        gnus-newsgroup-auto-expire 
9405        (or (not mark)
9406            (and (numberp mark) 
9407                 (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
9408                     (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
9409                     (= mark gnus-read-mark))))
9410        (setq mark gnus-expirable-mark))
9411   (let* ((mark (or mark gnus-del-mark))
9412          (article (or article (gnus-summary-article-number))))
9413     (or article (error "No article on current line"))
9414     (if (or (= mark gnus-unread-mark) 
9415             (= mark gnus-ticked-mark) 
9416             (= mark gnus-dormant-mark))
9417         (gnus-mark-article-as-unread article mark)
9418       (gnus-mark-article-as-read article mark))
9419
9420     ;; See whether the article is to be put in the cache.
9421     (and gnus-use-cache
9422          (not (= mark gnus-canceled-mark))
9423          (vectorp (gnus-get-header-by-num article))
9424          (save-excursion
9425            (gnus-cache-possibly-enter-article 
9426             gnus-newsgroup-name article 
9427             (gnus-get-header-by-num article)
9428             (= mark gnus-ticked-mark)
9429             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9430
9431     (if (gnus-summary-goto-subject article)
9432         (let ((buffer-read-only nil))
9433           (gnus-summary-show-thread)
9434           (and (eq (gnus-summary-article-mark) gnus-dummy-mark)
9435                (forward-line 1))
9436           ;; Fix the mark.
9437           (gnus-summary-update-mark mark 'unread)
9438           t))))
9439
9440 (defun gnus-summary-update-mark (mark type)
9441   (beginning-of-line)
9442   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
9443         (buffer-read-only nil)
9444         plist)
9445     (if (not forward)
9446         ()
9447       (forward-char forward)
9448       (setq plist (text-properties-at (point)))
9449       (delete-char 1)
9450       (insert mark)
9451       (and plist (add-text-properties (1- (point)) (point) plist))
9452       (and (eq type 'unread)
9453            (add-text-properties (1- (point)) (point) (list 'gnus-mark mark)))
9454       (gnus-summary-update-line (eq mark gnus-unread-mark)))))
9455   
9456 (defun gnus-mark-article-as-read (article &optional mark)
9457   "Enter ARTICLE in the pertinent lists and remove it from others."
9458   ;; Make the article expirable.
9459   (let ((mark (or mark gnus-del-mark)))
9460     (if (= mark gnus-expirable-mark)
9461         (setq gnus-newsgroup-expirable (cons article gnus-newsgroup-expirable))
9462       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
9463     ;; Remove from unread and marked lists.
9464     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9465     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9466     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9467     (setq gnus-newsgroup-reads 
9468           (cons (cons article mark) gnus-newsgroup-reads))
9469     ;; Possibly remove from cache, if that is used. 
9470     (and gnus-use-cache (gnus-cache-enter-remove-article article))))
9471
9472 (defun gnus-mark-article-as-unread (article &optional mark)
9473   "Enter ARTICLE in the pertinent lists and remove it from others."
9474   (let ((mark (or mark gnus-ticked-mark)))
9475     ;; Add to unread list.
9476     (or (memq article gnus-newsgroup-unreads)
9477         (setq gnus-newsgroup-unreads (cons article gnus-newsgroup-unreads)))
9478     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9479     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9480     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9481     (setq gnus-newsgroup-reads
9482           (delq (assq article gnus-newsgroup-reads)
9483                 gnus-newsgroup-reads))
9484     (if (= mark gnus-ticked-mark)
9485         (setq gnus-newsgroup-marked (cons article gnus-newsgroup-marked)))
9486     (if (= mark gnus-dormant-mark)
9487         (setq gnus-newsgroup-dormant (cons article gnus-newsgroup-dormant)))))
9488
9489 (defalias 'gnus-summary-mark-as-unread-forward 
9490   'gnus-summary-tick-article-forward)
9491 (make-obsolete 'gnus-summary-mark-as-unread-forward 
9492                'gnus-summary-tick-article-forward)
9493 (defun gnus-summary-tick-article-forward (n)
9494   "Tick N articles forwards.
9495 If N is negative, tick backwards instead.
9496 The difference between N and the number of articles ticked is returned."
9497   (interactive "p")
9498   (gnus-summary-mark-forward n gnus-ticked-mark))
9499
9500 (defalias 'gnus-summary-mark-as-unread-backward 
9501   'gnus-summary-tick-article-backward)
9502 (make-obsolete 'gnus-summary-mark-as-unread-backward 
9503                'gnus-summary-tick-article-backward)
9504 (defun gnus-summary-tick-article-backward (n)
9505   "Tick N articles backwards.
9506 The difference between N and the number of articles ticked is returned."
9507   (interactive "p")
9508   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
9509
9510 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9511 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9512 (defun gnus-summary-tick-article (&optional article clear-mark)
9513   "Mark current article as unread.
9514 Optional 1st argument ARTICLE specifies article number to be marked as unread.
9515 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
9516   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
9517                                        gnus-ticked-mark)))
9518
9519 (defun gnus-summary-mark-as-read-forward (n)
9520   "Mark N articles as read forwards.
9521 If N is negative, mark backwards instead.
9522 The difference between N and the actual number of articles marked is
9523 returned."
9524   (interactive "p")
9525   (gnus-summary-mark-forward n gnus-del-mark t))
9526
9527 (defun gnus-summary-mark-as-read-backward (n)
9528   "Mark the N articles as read backwards.
9529 The difference between N and the actual number of articles marked is
9530 returned."
9531   (interactive "p")
9532   (gnus-summary-mark-forward (- n) gnus-del-mark t))
9533
9534 (defun gnus-summary-mark-as-read (&optional article mark)
9535   "Mark current article as read.
9536 ARTICLE specifies the article to be marked as read.
9537 MARK specifies a string to be inserted at the beginning of the line."
9538   (gnus-summary-mark-article article mark))
9539
9540 (defun gnus-summary-clear-mark-forward (n)
9541   "Clear marks from N articles forward.
9542 If N is negative, clear backward instead.
9543 The difference between N and the number of marks cleared is returned."
9544   (interactive "p")
9545   (gnus-summary-mark-forward n gnus-unread-mark))
9546
9547 (defun gnus-summary-clear-mark-backward (n)
9548   "Clear marks from N articles backward.
9549 The difference between N and the number of marks cleared is returned."
9550   (interactive "p")
9551   (gnus-summary-mark-forward (- n) gnus-unread-mark))
9552
9553 (defun gnus-summary-mark-unread-as-read ()
9554   "Intended to be used by `gnus-summary-mark-article-hook'."
9555   (or (memq gnus-current-article gnus-newsgroup-marked)
9556       (memq gnus-current-article gnus-newsgroup-dormant)
9557       (memq gnus-current-article gnus-newsgroup-expirable)
9558       (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
9559
9560 (defun gnus-summary-mark-region-as-read (point mark all)
9561   "Mark all unread articles between point and mark as read.
9562 If given a prefix, mark all articles between point and mark as read,
9563 even ticked and dormant ones."
9564   (interactive "r\nP")
9565   (save-excursion
9566     (goto-char point)
9567     (beginning-of-line)
9568     (while (and 
9569             (< (point) mark)
9570             (progn
9571               (and
9572                (or all
9573                    (and
9574                     (not (memq (gnus-summary-article-number)
9575                                gnus-newsgroup-marked))
9576                     (not (memq (gnus-summary-article-number)
9577                                gnus-newsgroup-dormant))))
9578                (gnus-summary-mark-article
9579                 (gnus-summary-article-number) gnus-del-mark))
9580               t)
9581             (zerop (forward-line 1))))))
9582
9583 ;; Fix by Per Abrahamsen <amanda@iesd.auc.dk>.
9584 (defalias 'gnus-summary-delete-marked-as-read 
9585   'gnus-summary-remove-lines-marked-as-read)
9586 (make-obsolete 'gnus-summary-delete-marked-as-read 
9587                'gnus-summary-remove-lines-marked-as-read)
9588 (defun gnus-summary-remove-lines-marked-as-read ()
9589   "Remove lines that are marked as read."
9590   (interactive)
9591   (gnus-summary-remove-lines-marked-with 
9592    (concat (mapconcat
9593             (lambda (char) (char-to-string (symbol-value char)))
9594             '(gnus-del-mark gnus-read-mark gnus-ancient-mark
9595               gnus-killed-mark gnus-kill-file-mark
9596               gnus-low-score-mark gnus-expirable-mark
9597               gnus-canceled-mark gnus-catchup-mark)
9598             ""))))
9599
9600 (defalias 'gnus-summary-delete-marked-with 
9601   'gnus-summary-remove-lines-marked-with)
9602 (make-obsolete 'gnus-summary-delete-marked-with 
9603                'gnus-summary-remove-lines-marked-with)
9604 ;; Rewrite by Daniel Quinlan <quinlan@best.com>.
9605 (defun gnus-summary-remove-lines-marked-with (marks)
9606   "Remove lines that are marked with MARKS (e.g. \"DK\")."
9607   (interactive "sMarks: ")
9608   ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
9609   (gnus-set-global-variables)
9610   (let ((buffer-read-only nil)
9611         (marks (concat "^[" marks "]")))
9612     (goto-char (point-min))
9613     (if gnus-newsgroup-adaptive
9614         (gnus-score-remove-lines-adaptive marks)
9615       (while (re-search-forward marks nil t)
9616         (gnus-delete-line)))
9617     ;; If we use dummy roots, we have to do an additional sweep over
9618     ;; the buffer.
9619     (if (not (eq gnus-summary-make-false-root 'dummy))
9620         ()
9621       (goto-char (point-min))
9622       (setq marks (concat "^[" (char-to-string gnus-dummy-mark) "]"))
9623       (while (re-search-forward marks nil t)
9624         (if (gnus-subject-equal
9625              (gnus-summary-subject-string)
9626              (progn
9627                (forward-line 1)
9628                (gnus-summary-subject-string)))
9629             ()
9630           (forward-line -1)
9631           (gnus-delete-line)))))
9632   (or (zerop (buffer-size))
9633       (if (eobp)
9634           (gnus-summary-prev-subject 1)
9635         (gnus-summary-position-cursor))))
9636
9637 (defun gnus-summary-expunge-below (&optional score)
9638   "Remove articles with score less than SCORE."
9639   (interactive "P")
9640   (gnus-set-global-variables)
9641   (setq score (if score
9642                   (prefix-numeric-value score)
9643                 (or gnus-summary-default-score 0)))
9644   (save-excursion
9645     (set-buffer gnus-summary-buffer)
9646     (goto-char (point-min))
9647     (let ((buffer-read-only nil)
9648           beg)
9649       (while (not (eobp))
9650         (if (< (gnus-summary-article-score) score)
9651             (progn
9652               (setq beg (point))
9653               (forward-line 1)
9654               (delete-region beg (point)))
9655           (forward-line 1)))
9656       ;; Adjust point.
9657       (or (zerop (buffer-size))
9658           (if (eobp)
9659               (gnus-summary-prev-subject 1)
9660             (gnus-summary-position-cursor))))))
9661
9662 (defun gnus-summary-mark-below (score mark)
9663   "Mark articles with score less than SCORE with MARK."
9664   (interactive "P\ncMark: ")
9665   (gnus-set-global-variables)
9666   (setq score (if score
9667                   (prefix-numeric-value score)
9668                 (or gnus-summary-default-score 0)))
9669   (save-excursion
9670     (set-buffer gnus-summary-buffer)
9671     (goto-char (point-min))
9672     (while (not (eobp))
9673       (and (< (gnus-summary-article-score) score)
9674            (gnus-summary-mark-article nil mark))
9675       (forward-line 1))))
9676
9677 (defun gnus-summary-kill-below (&optional score)
9678   "Mark articles with score below SCORE as read."
9679   (interactive "P")
9680   (gnus-set-global-variables)
9681   (gnus-summary-mark-below score gnus-killed-mark))
9682
9683 (defun gnus-summary-clear-above (&optional score)
9684   "Clear all marks from articles with score above SCORE."
9685   (interactive "P")
9686   (gnus-set-global-variables)
9687   (gnus-summary-mark-above score gnus-unread-mark))
9688
9689 (defun gnus-summary-tick-above (&optional score)
9690   "Tick all articles with score above SCORE."
9691   (interactive "P")
9692   (gnus-set-global-variables)
9693   (gnus-summary-mark-above score gnus-ticked-mark))
9694
9695 (defun gnus-summary-mark-above (score mark)
9696   "Mark articles with score over SCORE with MARK."
9697   (interactive "P\ncMark: ")
9698   (gnus-set-global-variables)
9699   (setq score (if score
9700                   (prefix-numeric-value score)
9701                 (or gnus-summary-default-score 0)))
9702   (save-excursion
9703     (set-buffer gnus-summary-buffer)
9704     (goto-char (point-min))
9705     (while (not (eobp))
9706       (if (> (gnus-summary-article-score) score)
9707           (progn
9708             (gnus-summary-mark-article nil mark)
9709             (forward-line 1))
9710         (forward-line 1)))))
9711
9712 ;; Suggested by Daniel Quinlan <quinlan@best.com>.  
9713 (defun gnus-summary-show-all-expunged ()
9714   "Display all the hidden articles that were expunged for low scores."
9715   (interactive)
9716   (gnus-set-global-variables)
9717   (let ((buffer-read-only nil))
9718     (let ((scored gnus-newsgroup-scored)
9719           headers h)
9720       (while scored
9721         (or (gnus-summary-goto-subject (car (car scored)))
9722             (and (setq h (gnus-get-header-by-num (car (car scored))))
9723                  (< (cdr (car scored)) gnus-summary-expunge-below)
9724                  (setq headers (cons h headers))))
9725         (setq scored (cdr scored)))
9726       (or headers (error "No expunged articles hidden."))
9727       (goto-char (point-min))
9728       (save-excursion 
9729         (gnus-summary-update-lines 
9730          (point)
9731          (progn
9732            (gnus-summary-prepare-unthreaded (nreverse headers))
9733            (point)))))
9734     (goto-char (point-min))
9735     (gnus-summary-position-cursor)))
9736
9737 (defun gnus-summary-show-all-dormant ()
9738   "Display all the hidden articles that are marked as dormant."
9739   (interactive)
9740   (gnus-set-global-variables)
9741   (let ((buffer-read-only nil))
9742     (let ((dormant gnus-newsgroup-dormant)
9743           headers h)
9744       (while dormant
9745         (or (gnus-summary-goto-subject (car dormant))
9746             (and (setq h (gnus-get-header-by-num (car dormant)))
9747                  (setq headers (cons h headers))))
9748         (setq dormant (cdr dormant)))
9749       (or headers (error "No dormant articles hidden."))
9750       (goto-char (point-min))
9751       (save-excursion 
9752         (gnus-summary-update-lines 
9753          (point)
9754          (progn
9755            (gnus-summary-prepare-unthreaded (nreverse headers))
9756            (point)))))
9757     (goto-char (point-min))
9758     (gnus-summary-position-cursor)))
9759
9760 (defun gnus-summary-hide-all-dormant ()
9761   "Hide all dormant articles."
9762   (interactive)
9763   (gnus-set-global-variables)
9764   (gnus-summary-remove-lines-marked-with (char-to-string gnus-dormant-mark))
9765   (gnus-summary-position-cursor))
9766
9767 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
9768   "Mark all articles not marked as unread in this newsgroup as read.
9769 If prefix argument ALL is non-nil, all articles are marked as read.
9770 If QUIETLY is non-nil, no questions will be asked.
9771 If TO-HERE is non-nil, it should be a point in the buffer. All
9772 articles before this point will be marked as read.
9773 The number of articles marked as read is returned."
9774   (interactive "P")
9775   (gnus-set-global-variables)
9776   (prog1
9777       (if (or quietly
9778               (not gnus-interactive-catchup) ;Without confirmation?
9779               gnus-expert-user
9780               (gnus-y-or-n-p
9781                (if all
9782                    "Mark absolutely all articles as read? "
9783                  "Mark all unread articles as read? ")))
9784           (if (and not-mark 
9785                    (not gnus-newsgroup-adaptive)
9786                    (not gnus-newsgroup-auto-expire))
9787               (progn
9788                 (and all (setq gnus-newsgroup-marked nil
9789                                gnus-newsgroup-dormant nil))
9790                 (setq gnus-newsgroup-unreads 
9791                       (append gnus-newsgroup-marked gnus-newsgroup-dormant)))
9792             ;; We actually mark all articles as canceled, which we
9793             ;; have to do when using auto-expiry or adaptive scoring. 
9794             (let ((unreads (length gnus-newsgroup-unreads)))
9795               (gnus-summary-show-all-threads)
9796               (if (gnus-summary-first-subject (not all))
9797                   (while (and 
9798                           (if to-here (< (point) to-here) t)
9799                           (gnus-summary-mark-article-as-read gnus-catchup-mark)
9800                           (gnus-summary-search-subject nil (not all)))))
9801               (- unreads (length gnus-newsgroup-unreads))
9802               (or to-here
9803                   (setq gnus-newsgroup-unreads gnus-newsgroup-marked)))))
9804     (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
9805       (if (and (not to-here) (eq 'nnvirtual (car method)))
9806           (nnvirtual-catchup-group
9807            (gnus-group-real-name gnus-newsgroup-name) (nth 1 method) all)))
9808     (gnus-summary-position-cursor)))
9809
9810 (defun gnus-summary-catchup-to-here (&optional all)
9811   "Mark all unticked articles before the current one as read.
9812 If ALL is non-nil, also mark ticked and dormant articles as read."
9813   (interactive)
9814   (gnus-set-global-variables)
9815   (save-excursion
9816     (and (zerop (forward-line -1))
9817          (progn
9818            (end-of-line)
9819            (gnus-summary-catchup all t (point))
9820            (gnus-set-mode-line 'summary))))
9821   (gnus-summary-position-cursor))
9822
9823 (defun gnus-summary-catchup-all (&optional quietly)
9824   "Mark all articles in this newsgroup as read."
9825   (interactive)
9826   (gnus-set-global-variables)
9827   (gnus-summary-catchup t quietly))
9828
9829 (defun gnus-summary-catchup-and-exit (&optional all quietly)
9830   "Mark all articles not marked as unread in this newsgroup as read, then exit.
9831 If prefix argument ALL is non-nil, all articles are marked as read."
9832   (interactive "P")
9833   (gnus-set-global-variables)
9834   (gnus-summary-catchup all quietly nil 'fast)
9835   ;; Select next newsgroup or exit.
9836   (if (eq gnus-auto-select-next 'quietly)
9837       (gnus-summary-next-group nil)
9838     (gnus-summary-exit)))
9839
9840 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
9841   "Mark all articles in this newsgroup as read, and then exit."
9842   (interactive)
9843   (gnus-set-global-variables)
9844   (gnus-summary-catchup-and-exit t quietly))
9845
9846 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
9847 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
9848   "Mark all articles in this group as read and select the next group.
9849 If given a prefix, mark all articles, unread as well as ticked, as
9850 read." 
9851   (interactive "P")
9852   (gnus-set-global-variables)
9853   (gnus-summary-catchup all)
9854   (gnus-summary-next-group))
9855
9856 ;; Thread-based commands.
9857
9858 (defun gnus-summary-toggle-threads (&optional arg)
9859   "Toggle showing conversation threads.
9860 If ARG is positive number, turn showing conversation threads on."
9861   (interactive "P")
9862   (gnus-set-global-variables)
9863   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
9864     (setq gnus-show-threads
9865           (if (null arg) (not gnus-show-threads)
9866             (> (prefix-numeric-value arg) 0)))
9867     (gnus-summary-prepare)
9868     (gnus-summary-goto-subject current)
9869     (gnus-summary-position-cursor)))
9870
9871 (defun gnus-summary-show-all-threads ()
9872   "Show all threads."
9873   (interactive)
9874   (gnus-set-global-variables)
9875   (save-excursion
9876     (let ((buffer-read-only nil))
9877       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
9878   (gnus-summary-position-cursor))
9879
9880 (defun gnus-summary-show-thread ()
9881   "Show thread subtrees.
9882 Returns nil if no thread was there to be shown."
9883   (interactive)
9884   (gnus-set-global-variables)
9885   (let ((buffer-read-only nil)
9886         (orig (point))
9887         ;; first goto end then to beg, to have point at beg after let
9888         (end (progn (end-of-line) (point)))
9889         (beg (progn (beginning-of-line) (point))))
9890     (prog1
9891         ;; Any hidden lines here?
9892         (search-forward "\r" end t)
9893       (subst-char-in-region beg end ?\^M ?\n t)
9894       (goto-char orig)
9895       (gnus-summary-position-cursor))))
9896
9897 (defun gnus-summary-hide-all-threads ()
9898   "Hide all thread subtrees."
9899   (interactive)
9900   (gnus-set-global-variables)
9901   (save-excursion
9902     (goto-char (point-min))
9903     (gnus-summary-hide-thread)
9904     (while (and (not (eobp)) (zerop (forward-line 1)))
9905       (gnus-summary-hide-thread)))
9906   (gnus-summary-position-cursor))
9907
9908 (defun gnus-summary-hide-thread ()
9909   "Hide thread subtrees.
9910 Returns nil if no threads were there to be hidden."
9911   (interactive)
9912   (gnus-set-global-variables)
9913   (let ((buffer-read-only nil)
9914         (start (point))
9915         (level (gnus-summary-thread-level))
9916         (end (point)))
9917     ;; Go forward until either the buffer ends or the subthread
9918     ;; ends. 
9919     (if (eobp)
9920         ()
9921       (while (and (zerop (forward-line 1))
9922                   (> (gnus-summary-thread-level) level))
9923         (setq end (point)))
9924       (prog1
9925           (save-excursion
9926             (goto-char end)
9927             (search-backward "\n" start t))
9928         (subst-char-in-region start end ?\n ?\^M t)
9929         (forward-line -1)))))
9930
9931 (defun gnus-summary-go-to-next-thread (&optional previous)
9932   "Go to the same level (or less) next thread.
9933 If PREVIOUS is non-nil, go to previous thread instead.
9934 Return the article number moved to, or nil if moving was impossible."
9935   (let ((level (gnus-summary-thread-level))
9936         (article (gnus-summary-article-number)))
9937     (if previous 
9938         (while (and (zerop (forward-line -1))
9939                     (> (gnus-summary-thread-level) level)))
9940       (while (and (save-excursion
9941                     (forward-line 1)
9942                     (not (eobp)))
9943                   (zerop (forward-line 1))
9944                   (> (gnus-summary-thread-level) level))))
9945     (gnus-summary-recenter)
9946     (gnus-summary-position-cursor)
9947     (let ((oart (gnus-summary-article-number)))
9948       (and (/= oart article) oart))))
9949
9950 (defun gnus-summary-next-thread (n)
9951   "Go to the same level next N'th thread.
9952 If N is negative, search backward instead.
9953 Returns the difference between N and the number of skips actually
9954 done."
9955   (interactive "p")
9956   (gnus-set-global-variables)
9957   (let ((backward (< n 0))
9958         (n (abs n)))
9959   (while (and (> n 0)
9960               (gnus-summary-go-to-next-thread backward))
9961     (setq n (1- n)))
9962   (gnus-summary-position-cursor)
9963   (if (/= 0 n) (gnus-message 7 "No more threads"))
9964   n))
9965
9966 (defun gnus-summary-prev-thread (n)
9967   "Go to the same level previous N'th thread.
9968 Returns the difference between N and the number of skips actually
9969 done."
9970   (interactive "p")
9971   (gnus-set-global-variables)
9972   (gnus-summary-next-thread (- n)))
9973
9974 (defun gnus-summary-go-down-thread (&optional same)
9975   "Go down one level in the current thread.
9976 If SAME is non-nil, also move to articles of the same level."
9977   (let ((level (gnus-summary-thread-level))
9978         (start (point)))
9979     (if (and (zerop (forward-line 1))
9980              (> (gnus-summary-thread-level) level))
9981         t
9982       (goto-char start)
9983       nil)))
9984
9985 (defun gnus-summary-go-up-thread ()
9986   "Go up one level in the current thread."
9987   (let ((level (gnus-summary-thread-level))
9988         (start (point)))
9989     (while (and (zerop (forward-line -1))
9990                 (>= (gnus-summary-thread-level) level)))
9991     (if (>= (gnus-summary-thread-level) level)
9992         (progn
9993           (goto-char start)
9994           nil)
9995       t)))
9996
9997 (defun gnus-summary-down-thread (n)
9998   "Go down thread N steps.
9999 If N is negative, go up instead.
10000 Returns the difference between N and how many steps down that were
10001 taken."
10002   (interactive "p")
10003   (gnus-set-global-variables)
10004   (let ((up (< n 0))
10005         (n (abs n)))
10006   (while (and (> n 0)
10007               (if up (gnus-summary-go-up-thread)
10008                 (gnus-summary-go-down-thread)))
10009     (setq n (1- n)))
10010   (gnus-summary-position-cursor)
10011   (if (/= 0 n) (gnus-message 7 "Can't go further"))
10012   n))
10013
10014 (defun gnus-summary-up-thread (n)
10015   "Go up thread N steps.
10016 If N is negative, go up instead.
10017 Returns the difference between N and how many steps down that were
10018 taken."
10019   (interactive "p")
10020   (gnus-set-global-variables)
10021   (gnus-summary-down-thread (- n)))
10022
10023 (defun gnus-summary-kill-thread (&optional unmark)
10024   "Mark articles under current thread as read.
10025 If the prefix argument is positive, remove any kinds of marks.
10026 If the prefix argument is negative, tick articles instead."
10027   (interactive "P")
10028   (gnus-set-global-variables)
10029   (if unmark
10030       (setq unmark (prefix-numeric-value unmark)))
10031   (let ((killing t)
10032         (level (gnus-summary-thread-level)))
10033     (save-excursion
10034       (while killing
10035         ;; Mark the article...
10036         (cond ((null unmark) (gnus-summary-mark-article-as-read
10037                                gnus-killed-mark))
10038               ((> unmark 0) (gnus-summary-mark-article-as-unread 
10039                              gnus-unread-mark))
10040               (t (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10041         ;; ...and go forward until either the buffer ends or the subtree
10042         ;; ends. 
10043         (if (not (and (zerop (forward-line 1))
10044                       (> (gnus-summary-thread-level) level)))
10045             (setq killing nil))))
10046     ;; Hide killed subtrees.
10047     (and (null unmark)
10048          gnus-thread-hide-killed
10049          (gnus-summary-hide-thread))
10050     ;; If marked as read, go to next unread subject.
10051     (if (null unmark)
10052         ;; Go to next unread subject.
10053         (gnus-summary-next-subject 1 t)))
10054   (gnus-set-mode-line 'summary))
10055
10056 ;; Summary sorting commands
10057
10058 (defun gnus-summary-sort-by-number (&optional reverse)
10059   "Sort summary buffer by article number.
10060 Argument REVERSE means reverse order."
10061   (interactive "P")
10062   (gnus-set-global-variables)
10063   (gnus-summary-sort 
10064    ;; `gnus-summary-article-number' is a macro, and `sort-subr' wants
10065    ;; a function, so we wrap it.
10066    (cons (lambda () (gnus-summary-article-number))
10067          'gnus-thread-sort-by-number) reverse))
10068
10069 (defun gnus-summary-sort-by-author (&optional reverse)
10070   "Sort summary buffer by author name alphabetically.
10071 If case-fold-search is non-nil, case of letters is ignored.
10072 Argument REVERSE means reverse order."
10073   (interactive "P")
10074   (gnus-set-global-variables)
10075   (gnus-summary-sort
10076    (cons
10077     (lambda ()
10078       (let* ((header (gnus-get-header-by-num (gnus-summary-article-number)))
10079              (extract (funcall
10080                        gnus-extract-address-components
10081                        (header-from header))))
10082         (concat (or (car extract) (cdr extract))
10083                 "\r" (header-subject header))))
10084     'gnus-thread-sort-by-author)
10085    reverse))
10086
10087 (defun gnus-summary-sort-by-subject (&optional reverse)
10088   "Sort summary buffer by subject alphabetically. `Re:'s are ignored.
10089 If case-fold-search is non-nil, case of letters is ignored.
10090 Argument REVERSE means reverse order."
10091   (interactive "P")
10092   (gnus-set-global-variables)
10093   (gnus-summary-sort
10094    (cons
10095     (lambda ()
10096       (let* ((header (gnus-get-header-by-num (gnus-summary-article-number)))
10097              (extract (funcall
10098                        gnus-extract-address-components
10099                        (header-from header))))
10100         (concat 
10101          (downcase (gnus-simplify-subject (gnus-summary-subject-string)))
10102          "\r" (or (car extract) (cdr extract)))))
10103     'gnus-thread-sort-by-subject)
10104    reverse))
10105
10106 (defun gnus-summary-sort-by-date (&optional reverse)
10107   "Sort summary buffer by date.
10108 Argument REVERSE means reverse order."
10109   (interactive "P")
10110   (gnus-set-global-variables)
10111   (gnus-summary-sort
10112    (cons
10113     (lambda ()
10114       (gnus-sortable-date
10115        (header-date 
10116         (gnus-get-header-by-num (gnus-summary-article-number)))))
10117     'gnus-thread-sort-by-date)
10118    reverse))
10119
10120 (defun gnus-summary-sort-by-score (&optional reverse)
10121   "Sort summary buffer by score.
10122 Argument REVERSE means reverse order."
10123   (interactive "P")
10124   (gnus-set-global-variables)
10125   (gnus-summary-sort 
10126    (cons (lambda () (gnus-summary-article-score))
10127          'gnus-thread-sort-by-score)
10128    (not reverse)))
10129
10130 (defvar gnus-summary-already-sorted nil)
10131 (defun gnus-summary-sort (predicate reverse)
10132   ;; Sort summary buffer by PREDICATE.  REVERSE means reverse order. 
10133   (if gnus-summary-already-sorted
10134       ()
10135     (let (buffer-read-only)
10136       (if (not gnus-show-threads)
10137           ;; We do untreaded sorting...
10138           (progn
10139             (goto-char (point-min))
10140             (sort-subr reverse 'forward-line 'end-of-line (car predicate)))
10141         ;; ... or we do threaded sorting.
10142         (let ((gnus-thread-sort-functions (list (cdr predicate)))
10143               (gnus-summary-prepare-hook nil)
10144               (gnus-summary-already-sorted nil))
10145           ;; We do that by simply regenerating the threads.
10146           (gnus-summary-prepare)
10147           (and gnus-show-threads
10148                gnus-thread-hide-subtree
10149                (gnus-summary-hide-all-threads))
10150           ;; If in async mode, we send some info to the backend.
10151           (and gnus-newsgroup-async
10152                (setq gnus-newsgroup-threads (nreverse gnus-newsgroup-threads))
10153                (gnus-request-asynchronous 
10154                 gnus-newsgroup-name
10155                 (if (and gnus-asynchronous-article-function
10156                          (fboundp gnus-asynchronous-article-function))
10157                     (funcall gnus-asynchronous-article-function
10158                              gnus-newsgroup-threads)))))))))
10159
10160   
10161 (defun gnus-sortable-date (date)
10162   "Make sortable string by string-lessp from DATE.
10163 Timezone package is used."
10164   (let* ((date (timezone-fix-time date nil nil)) ;[Y M D H M S]
10165          (year (aref date 0))
10166          (month (aref date 1))
10167          (day (aref date 2)))
10168     (timezone-make-sortable-date 
10169      year month day 
10170      (timezone-make-time-string
10171       (aref date 3) (aref date 4) (aref date 5)))))
10172
10173
10174 ;; Summary saving commands.
10175
10176 (defun gnus-summary-save-article (&optional n)
10177   "Save the current article using the default saver function.
10178 If N is a positive number, save the N next articles.
10179 If N is a negative number, save the N previous articles.
10180 If N is nil and any articles have been marked with the process mark,
10181 save those articles instead.
10182 The variable `gnus-default-article-saver' specifies the saver function."
10183   (interactive "P")
10184   (gnus-set-global-variables)
10185   (let ((articles (gnus-summary-work-articles n)))
10186     (while articles
10187       (let ((header (gnus-get-header-by-num (car articles))))
10188         (if (vectorp header)
10189             (progn
10190               (save-window-excursion
10191                 (gnus-summary-select-article t nil nil (car articles)))
10192               (or gnus-save-all-headers
10193                   (gnus-article-hide-headers t))
10194               ;; Remove any X-Gnus lines.
10195               (save-excursion
10196                 (save-restriction
10197                   (set-buffer gnus-article-buffer)
10198                   (let ((buffer-read-only nil))
10199                     (goto-char (point-min))
10200                     (narrow-to-region (point) (or (search-forward "\n\n" nil t)
10201                                                   (point-max)))
10202                     (while (re-search-forward "^X-Gnus" nil t)
10203                       (beginning-of-line)
10204                       (delete-region (point)
10205                                      (progn (forward-line 1) (point))))
10206                     (widen))))
10207               (save-window-excursion
10208                 (if gnus-default-article-saver
10209                     (funcall gnus-default-article-saver)
10210                   (error "No default saver is defined."))))
10211           (if (assq 'name header)
10212               (gnus-copy-file (cdr (assq 'name header)))
10213             (gnus-message 1 "Article %d is unsaveable" (car articles)))))
10214       (gnus-summary-remove-process-mark (car articles))
10215       (setq articles (cdr articles)))
10216     (gnus-summary-position-cursor)
10217     n))
10218
10219 (defun gnus-summary-pipe-output (&optional arg)
10220   "Pipe the current article to a subprocess.
10221 If N is a positive number, pipe the N next articles.
10222 If N is a negative number, pipe the N previous articles.
10223 If N is nil and any articles have been marked with the process mark,
10224 pipe those articles instead."
10225   (interactive "P")
10226   (gnus-set-global-variables)
10227   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
10228     (gnus-summary-save-article arg)))
10229
10230 (defun gnus-summary-save-article-mail (&optional arg)
10231   "Append the current article to an mail file.
10232 If N is a positive number, save the N next articles.
10233 If N is a negative number, save the N previous articles.
10234 If N is nil and any articles have been marked with the process mark,
10235 save those articles instead."
10236   (interactive "P")
10237   (gnus-set-global-variables)
10238   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10239     (gnus-summary-save-article arg)))
10240
10241 (defun gnus-summary-save-article-rmail (&optional arg)
10242   "Append the current article to an rmail file.
10243 If N is a positive number, save the N next articles.
10244 If N is a negative number, save the N previous articles.
10245 If N is nil and any articles have been marked with the process mark,
10246 save those articles instead."
10247   (interactive "P")
10248   (gnus-set-global-variables)
10249   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10250     (gnus-summary-save-article arg)))
10251
10252 (defun gnus-summary-save-article-file (&optional arg)
10253   "Append the current article to a file.
10254 If N is a positive number, save the N next articles.
10255 If N is a negative number, save the N previous articles.
10256 If N is nil and any articles have been marked with the process mark,
10257 save those articles instead."
10258   (interactive "P")
10259   (gnus-set-global-variables)
10260   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10261     (gnus-summary-save-article arg)))
10262
10263 (defun gnus-read-save-file-name (prompt default-name)
10264   (let ((methods gnus-split-methods)
10265         split-name)
10266     (if (not gnus-split-methods)
10267         ()
10268       (save-excursion
10269         (set-buffer gnus-article-buffer)
10270         (gnus-narrow-to-headers)
10271         (while methods
10272           (goto-char (point-min))
10273           (and (condition-case () 
10274                    (re-search-forward (car (car methods)) nil t)
10275                  (error nil))
10276                (setq split-name (cons (nth 1 (car methods)) split-name)))
10277           (setq methods (cdr methods)))
10278         (widen)))
10279     (cond ((null split-name)
10280            (read-file-name
10281             (concat prompt " (default "
10282                     (file-name-nondirectory default-name) ") ")
10283             (file-name-directory default-name)
10284             default-name))
10285           ((= 1 (length split-name))
10286            (read-file-name
10287             (concat prompt " (default " (car split-name) ") ")
10288             gnus-article-save-directory
10289             (concat gnus-article-save-directory (car split-name))))
10290           (t
10291            (setq split-name (mapcar (lambda (el) (list el))
10292                                     (nreverse split-name)))
10293            (let ((result (completing-read 
10294                           (concat prompt " ")
10295                           split-name nil nil)))
10296              (concat gnus-article-save-directory
10297                      (if (string= result "")
10298                          (car (car split-name))
10299                        result)))))))
10300
10301 (defun gnus-summary-save-in-rmail (&optional filename)
10302   "Append this article to Rmail file.
10303 Optional argument FILENAME specifies file name.
10304 Directory to save to is default to `gnus-article-save-directory' which
10305 is initialized from the SAVEDIR environment variable."
10306   (interactive)
10307   (gnus-set-global-variables)
10308   (let ((default-name
10309           (funcall gnus-rmail-save-name gnus-newsgroup-name
10310                    gnus-current-headers gnus-newsgroup-last-rmail)))
10311     (or filename
10312         (setq filename (gnus-read-save-file-name 
10313                         "Save in rmail file:" default-name)))
10314     (gnus-make-directory (file-name-directory filename))
10315     (gnus-eval-in-buffer-window 
10316      gnus-article-buffer
10317      (save-excursion
10318        (save-restriction
10319          (widen)
10320          (gnus-output-to-rmail filename))))
10321     ;; Remember the directory name to save articles
10322     (setq gnus-newsgroup-last-rmail filename)))
10323
10324 (defun gnus-summary-save-in-mail (&optional filename)
10325   "Append this article to Unix mail file.
10326 Optional argument FILENAME specifies file name.
10327 Directory to save to is default to `gnus-article-save-directory' which
10328 is initialized from the SAVEDIR environment variable."
10329   (interactive)
10330   (gnus-set-global-variables)
10331   (let ((default-name
10332           (funcall gnus-mail-save-name gnus-newsgroup-name
10333                    gnus-current-headers gnus-newsgroup-last-mail)))
10334     (or filename
10335         (setq filename (gnus-read-save-file-name 
10336                         "Save in Unix mail file:" default-name)))
10337     (setq filename
10338           (expand-file-name filename
10339                             (and default-name
10340                                  (file-name-directory default-name))))
10341     (gnus-make-directory (file-name-directory filename))
10342     (gnus-eval-in-buffer-window 
10343      gnus-article-buffer
10344      (save-excursion
10345        (save-restriction
10346          (widen)
10347          (if (and (file-readable-p filename) (rmail-file-p filename))
10348              (gnus-output-to-rmail filename)
10349            (rmail-output filename 1 t t)))))
10350     ;; Remember the directory name to save articles.
10351     (setq gnus-newsgroup-last-mail filename)))
10352
10353 (defun gnus-summary-save-in-file (&optional filename)
10354   "Append this article to file.
10355 Optional argument FILENAME specifies file name.
10356 Directory to save to is default to `gnus-article-save-directory' which
10357 is initialized from the SAVEDIR environment variable."
10358   (interactive)
10359   (gnus-set-global-variables)
10360   (let ((default-name
10361           (funcall gnus-file-save-name gnus-newsgroup-name
10362                    gnus-current-headers gnus-newsgroup-last-file)))
10363     (or filename
10364         (setq filename (gnus-read-save-file-name 
10365                         "Save in file:" default-name)))
10366     (gnus-make-directory (file-name-directory filename))
10367     (gnus-eval-in-buffer-window 
10368      gnus-article-buffer
10369      (save-excursion
10370        (save-restriction
10371          (widen)
10372          (gnus-output-to-file filename))))
10373     ;; Remember the directory name to save articles.
10374     (setq gnus-newsgroup-last-file filename)))
10375
10376 (defun gnus-summary-save-in-pipe (&optional command)
10377   "Pipe this article to subprocess."
10378   (interactive)
10379   (gnus-set-global-variables)
10380   (let ((command (read-string "Shell command on article: "
10381                               gnus-last-shell-command)))
10382     (if (string-equal command "")
10383         (setq command gnus-last-shell-command))
10384     (gnus-eval-in-buffer-window 
10385      gnus-article-buffer
10386      (save-restriction
10387        (widen)
10388        (shell-command-on-region (point-min) (point-max) command nil)))
10389     (setq gnus-last-shell-command command)))
10390
10391 ;; Summary extract commands
10392
10393 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
10394   (let ((buffer-read-only nil)
10395         (article (gnus-summary-article-number))
10396         b)
10397     (or (gnus-summary-goto-subject article)
10398         (error (format "No such article: %d" article)))
10399     (gnus-summary-position-cursor)
10400     ;; If all commands are to be bunched up on one line, we collect
10401     ;; them here.  
10402     (if gnus-view-pseudos-separately
10403         ()
10404       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
10405             files action)
10406         (while ps
10407           (setq action (cdr (assq 'action (car ps))))
10408           (setq files (list (cdr (assq 'name (car ps)))))
10409           (while (and ps (cdr ps)
10410                       (string= (or action "1")
10411                                (or (cdr (assq 'action (car (cdr ps)))) "2")))
10412             (setq files (cons (cdr (assq 'name (car (cdr ps)))) files))
10413             (setcdr ps (cdr (cdr ps))))
10414           (if (not files)
10415               ()
10416             (if (not (string-match "%s" action))
10417                 (setq files (cons " " files)))
10418             (setq files (cons " " files))
10419             (and (assq 'execute (car ps))
10420                  (setcdr (assq 'execute (car ps))
10421                          (funcall (if (string-match "%s" action)
10422                                       'format 'concat)
10423                                   action 
10424                                   (mapconcat (lambda (f) f) files " ")))))
10425           (setq ps (cdr ps)))))
10426     (if (and gnus-view-pseudos (not not-view))
10427         (while pslist
10428           (and (assq 'execute (car pslist))
10429                (gnus-execute-command (cdr (assq 'execute (car pslist)))
10430                                      (eq gnus-view-pseudos 'not-confirm)))
10431           (setq pslist (cdr pslist)))
10432       (save-excursion
10433         (while pslist
10434           (gnus-summary-goto-subject (or (cdr (assq 'article (car pslist)))
10435                                          (gnus-summary-article-number)))
10436           (forward-line 1)
10437           (setq b (point))
10438           (insert "          " (file-name-nondirectory 
10439                                 (cdr (assq 'name (car pslist))))
10440                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
10441           (add-text-properties 
10442            b (1+ b) (list 'gnus-number gnus-reffed-article-number
10443                           'gnus-mark gnus-unread-mark 
10444                           'gnus-level 0
10445                           'gnus-pseudo (car pslist)))
10446           (forward-line -1)
10447           (gnus-sethash (int-to-string gnus-reffed-article-number)
10448                         (car pslist) gnus-newsgroup-headers-hashtb-by-number)
10449           (setq gnus-newsgroup-unreads
10450                 (cons gnus-reffed-article-number gnus-newsgroup-unreads))
10451           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
10452           (setq pslist (cdr pslist)))))))
10453
10454 (defun gnus-pseudos< (p1 p2)
10455   (let ((c1 (cdr (assq 'action p1)))
10456         (c2 (cdr (assq 'action p2))))
10457     (and c1 c2 (string< c1 c2))))
10458
10459 (defun gnus-request-pseudo-article (props)
10460   (cond ((assq 'execute props)
10461          (gnus-execute-command (cdr (assq 'execute props)))))
10462   (let ((gnus-current-article (gnus-summary-article-number)))
10463     (run-hooks 'gnus-mark-article-hook)))
10464
10465 (defun gnus-execute-command (command &optional automatic)
10466   (save-excursion
10467     (gnus-article-setup-buffer)
10468     (set-buffer gnus-article-buffer)
10469     (let ((command (if automatic command (read-string "Command: " command)))
10470           (buffer-read-only nil))
10471       (erase-buffer)
10472       (insert "$ " command "\n\n")
10473       (if gnus-view-pseudo-asynchronously
10474           (start-process "gnus-execute" nil "sh" "-c" command)
10475         (call-process "sh" nil t nil "-c" command)))))
10476
10477 (defun gnus-copy-file (file &optional to)
10478   "Copy FILE to TO."
10479   (interactive
10480    (list (read-file-name "Copy file: " default-directory)
10481          (read-file-name "Copy file to: " default-directory)))
10482   (gnus-set-global-variables)
10483   (or to (setq to (read-file-name "Copy file to: " default-directory)))
10484   (and (file-directory-p to) 
10485        (setq to (concat (file-name-as-directory to)
10486                         (file-name-nondirectory file))))
10487   (copy-file file to))
10488
10489 ;; Summary kill commands.
10490
10491 (defun gnus-summary-edit-global-kill (article)
10492   "Edit the \"global\" kill file."
10493   (interactive (list (gnus-summary-article-number)))
10494   (gnus-set-global-variables)
10495   (gnus-group-edit-global-kill article))
10496
10497 (defun gnus-summary-edit-local-kill ()
10498   "Edit a local kill file applied to the current newsgroup."
10499   (interactive)
10500   (gnus-set-global-variables)
10501   (setq gnus-current-headers 
10502         (gnus-gethash 
10503          (int-to-string (gnus-summary-article-number))
10504          gnus-newsgroup-headers-hashtb-by-number))
10505   (gnus-set-global-variables)
10506   (gnus-group-edit-local-kill 
10507    (gnus-summary-article-number) gnus-newsgroup-name))
10508
10509 \f
10510 ;;;
10511 ;;; Gnus article mode
10512 ;;;
10513
10514 (put 'gnus-article-mode 'mode-class 'special)
10515
10516 (if gnus-article-mode-map
10517     nil
10518   (setq gnus-article-mode-map (make-keymap))
10519   (suppress-keymap gnus-article-mode-map)
10520   (define-key gnus-article-mode-map " " 'gnus-article-next-page)
10521   (define-key gnus-article-mode-map "\177" 'gnus-article-prev-page)
10522   (define-key gnus-article-mode-map "\C-c^" 'gnus-article-refer-article)
10523   (define-key gnus-article-mode-map "h" 'gnus-article-show-summary)
10524   (define-key gnus-article-mode-map "s" 'gnus-article-show-summary)
10525   (define-key gnus-article-mode-map "\C-c\C-m" 'gnus-article-mail)
10526   (define-key gnus-article-mode-map "\C-c\C-M" 'gnus-article-mail-with-original)
10527   (define-key gnus-article-mode-map "?" 'gnus-article-describe-briefly)
10528   (define-key gnus-article-mode-map gnus-mouse-2 'gnus-article-push-button)
10529   (define-key gnus-article-mode-map "\r" 'gnus-article-press-button)
10530   (define-key gnus-article-mode-map "\t" 'gnus-article-next-button)
10531   (define-key gnus-article-mode-map "\C-c\C-b" 'gnus-bug)
10532   
10533   ;; Duplicate almost all summary keystrokes in the article mode map.
10534   (let ((commands 
10535          (list 
10536           "p" "N" "P" "\M-\C-n" "\M-\C-p"
10537           "\M-n" "\M-p" "." "," "\M-s" "\M-r" "<" ">" "j"
10538           "u" "!" "U" "d" "D" "E" "\M-u" "\M-U" "k" "\C-k" "\M-\C-k"
10539           "\M-\C-l" "e" "#" "\M-#" "\M-\C-t" "\M-\C-s" "\M-\C-h"
10540           "\M-\C-f" "\M-\C-b" "\M-\C-u" "\M-\C-d" "&" "\C-w"
10541           "\C-t" "?" "\C-c\M-\C-s" "\C-c\C-s\C-n" "\C-c\C-s\C-a"
10542           "\C-c\C-s\C-s" "\C-c\C-s\C-d" "\C-c\C-s\C-i" "\C-x\C-s"
10543           "\M-g" "w" "\C-c\C-r" "\M-t" "C"
10544           "o" "\C-o" "|" "\M-k" "\M-K" "V" "\C-c\C-d"
10545           "\C-c\C-i" "x" "X" "t" "g" "?" "l"
10546           "\C-c\C-v\C-v" "\C-d" "v" 
10547 ;;        "Mt" "M!" "Md" "Mr"
10548 ;;        "Mc" "M " "Me" "Mx" "M?" "Mb" "MB" "M#" "M\M-#" "M\M-r"
10549 ;;        "M\M-\C-r" "MD" "M\M-D" "MS" "MC" "MH" "M\C-c" "Mk" "MK"
10550 ;;        "Ms" "Mc" "Mu" "Mm" "Mk" "Gn" "Gp" "GN" "GP" "G\C-n" "G\C-p"
10551 ;;        "G\M-n" "G\M-p" "Gf" "Gb" "Gg" "Gl" "Gp" "Tk" "Tl" "Ti" "TT"
10552 ;;        "Ts" "TS" "Th" "TH" "Tn" "Tp" "Tu" "Td" "T#" "A " "An" "A\177" "Ap"
10553 ;;        "A\r" "A<" "A>" "Ab" "Ae" "A^" "Ar" "Aw" "Ac" "Ag" "At" "Am"
10554 ;;        "As" "Wh" "Ws" "Wc" "Wo" "Ww" "Wd" "Wq" "Wf" "Wt" "W\C-t"
10555 ;;        "WT" "WA" "Wa" "WH" "WC" "WS" "Wb" "Hv" "Hf" "Hd" "Hh" "Hi"
10556 ;;        "Be" "B\177" "Bm" "Br" "Bw" "Bc" "Bq" "Bi" "Oo" "Om" "Or"
10557 ;;        "Of" "Oh" "Ov" "Op" "Vu" "V\C-s" "V\C-r" "Vr" "V&" "VT" "Ve"
10558 ;;        "VD" "Vk" "VK" "Vsn" "Vsa" "Vss" "Vsd" "Vsi"
10559           )))
10560     (while (and nil commands) ; disabled
10561       (define-key gnus-article-mode-map (car commands) 
10562         'gnus-article-summary-command)
10563       (setq commands (cdr commands))))
10564
10565   (let ((commands (list "q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
10566 ;;                      "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP" 
10567                          "=" "n"  "^" "\M-^")))
10568     (while (and nil commands) ; disabled
10569       (define-key gnus-article-mode-map (car commands) 
10570         'gnus-article-summary-command-nosave)
10571       (setq commands (cdr commands)))))
10572
10573
10574 (defun gnus-article-mode ()
10575   "Major mode for displaying an article.
10576
10577 All normal editing commands are switched off.
10578
10579 The following commands are available:
10580
10581 \\<gnus-article-mode-map>
10582 \\[gnus-article-next-page]\t Scroll the article one page forwards
10583 \\[gnus-article-prev-page]\t Scroll the article one page backwards
10584 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
10585 \\[gnus-article-show-summary]\t Display the summary buffer
10586 \\[gnus-article-mail]\t Send a reply to the address near point
10587 \\[gnus-article-mail-with-original]\t Send a reply to the address near point; include the original article
10588 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
10589 \\[gnus-info-find-node]\t Go to the Gnus info node"
10590   (interactive)
10591   (if gnus-visual (gnus-article-make-menu-bar))
10592   (kill-all-local-variables)
10593   (setq mode-line-modified "-- ")
10594   (make-local-variable 'mode-line-format)
10595   (setq mode-line-format (copy-sequence mode-line-format))
10596   (and (equal (nth 3 mode-line-format) "   ")
10597        (setcar (nthcdr 3 mode-line-format) ""))
10598   (setq mode-name "Article")
10599   (setq major-mode 'gnus-article-mode)
10600   (make-local-variable 'minor-mode-alist)
10601   (or (assq 'gnus-show-mime minor-mode-alist)
10602       (setq minor-mode-alist
10603             (cons (list 'gnus-show-mime " MIME") minor-mode-alist)))
10604   (use-local-map gnus-article-mode-map)
10605   (make-local-variable 'page-delimiter)
10606   (setq page-delimiter gnus-page-delimiter)
10607   (buffer-disable-undo (current-buffer))
10608   (setq buffer-read-only t)             ;Disable modification
10609   (run-hooks 'gnus-article-mode-hook))
10610
10611 (defun gnus-article-setup-buffer ()
10612   "Initialize article mode buffer."
10613   (if (get-buffer gnus-article-buffer)
10614       (save-excursion
10615         (set-buffer gnus-article-buffer)
10616         (buffer-disable-undo (current-buffer))
10617         (setq buffer-read-only t)
10618         (gnus-add-current-to-buffer-list)
10619         (or (eq major-mode 'gnus-article-mode)
10620             (gnus-article-mode)))
10621     (save-excursion
10622       (set-buffer (get-buffer-create gnus-article-buffer))
10623       (gnus-add-current-to-buffer-list)
10624       (gnus-article-mode))))
10625
10626 ;; Set article window start at LINE, where LINE is the number of lines
10627 ;; from the head of the article.
10628 (defun gnus-article-set-window-start (&optional line)
10629   (set-window-start 
10630    (get-buffer-window gnus-article-buffer)
10631    (save-excursion
10632      (set-buffer gnus-article-buffer)
10633      (goto-char (point-min))
10634      (if (not line)
10635          (point-min)
10636        (gnus-message 6 "Moved to bookmark")
10637        (search-forward "\n\n" nil t)
10638        (forward-line line)
10639        (point)))))
10640
10641 (defun gnus-request-article-this-buffer (article group)
10642   "Get an article and insert it into this buffer."
10643   (setq group (or group gnus-newsgroup-name))
10644
10645   ;; Open server if it has closed.
10646   (gnus-check-server (gnus-find-method-for-group group))
10647
10648   ;; Using `gnus-request-article' directly will insert the article into
10649   ;; `nntp-server-buffer' - so we'll save some time by not having to
10650   ;; copy it from the server buffer into the article buffer.
10651
10652   ;; We only request an article by message-id when we do not have the
10653   ;; headers for it, so we'll have to get those.
10654   (and (stringp article) 
10655        (let ((gnus-override-method gnus-refer-article-method))
10656          (gnus-read-header article)))
10657
10658   ;; If the article number is negative, that means that this article
10659   ;; doesn't belong in this newsgroup (possibly), so we find its
10660   ;; message-id and request it by id instead of number.
10661   (if (not (numberp article))
10662       ()
10663     (save-excursion
10664       (set-buffer gnus-summary-buffer)
10665       (let ((header (gnus-get-header-by-num article)))
10666         (if (< article 0)
10667             (if (vectorp header)
10668                 ;; It's a real article.
10669                 (setq article (header-id header))
10670               ;; It is an extracted pseudo-article.
10671               (setq article 'pseudo)
10672               (gnus-request-pseudo-article header)))
10673
10674         (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
10675           (if (not (eq (car method) 'nneething))
10676               ()
10677             (let ((dir (concat (file-name-as-directory (nth 1 method))
10678                                (header-subject header))))
10679               (if (file-directory-p dir)
10680                   (progn
10681                     (setq article 'nneething)
10682                     (gnus-group-enter-directory dir)))))))))
10683
10684   ;; Check the cache.
10685   (if (and gnus-use-cache
10686            (numberp article)
10687            (gnus-cache-request-article article group))
10688       'article
10689     ;; Get the article and into the article buffer.
10690     (if (or (stringp article) (numberp article))
10691         (progn
10692           (erase-buffer)
10693           (let ((gnus-override-method 
10694                  (and (stringp article) gnus-refer-article-method)))
10695             (and (gnus-request-article article group (current-buffer))
10696                  'article)))
10697       article)))
10698
10699 (defun gnus-read-header (id)
10700   "Read the headers of article ID and enter them into the Gnus system."
10701   (let (header)
10702     (if (not (setq header 
10703                    (car (if (let ((gnus-nov-is-evil t))
10704                               (gnus-retrieve-headers 
10705                                (list id) gnus-newsgroup-name))
10706                             (gnus-get-newsgroup-headers)))))
10707         nil
10708       (if (stringp id)
10709           (header-set-number header gnus-reffed-article-number))
10710       (setq gnus-newsgroup-headers (cons header gnus-newsgroup-headers))
10711       (gnus-sethash (int-to-string (header-number header)) header
10712                     gnus-newsgroup-headers-hashtb-by-number)
10713       (if (stringp id)
10714           (setq gnus-reffed-article-number (1- gnus-reffed-article-number)))
10715       (setq gnus-current-headers header)
10716       header)))
10717
10718 (defun gnus-article-prepare (article &optional all-headers header)
10719   "Prepare ARTICLE in article mode buffer.
10720 ARTICLE should either be an article number or a Message-ID.
10721 If ARTICLE is an id, HEADER should be the article headers.
10722 If ALL-HEADERS is non-nil, no headers are hidden."
10723   (save-excursion
10724     ;; Make sure we start in a summary buffer.
10725     (or (eq major-mode 'gnus-summary-mode)
10726         (set-buffer gnus-summary-buffer))
10727     (setq gnus-summary-buffer (current-buffer))
10728     ;; Make sure the connection to the server is alive.
10729     (or (gnus-server-opened (gnus-find-method-for-group gnus-newsgroup-name))
10730         (progn
10731           (gnus-check-server 
10732            (gnus-find-method-for-group gnus-newsgroup-name))
10733           (gnus-request-group gnus-newsgroup-name t)))
10734     (let* ((article (if header (header-number header) article))
10735            (summary-buffer (current-buffer))
10736            (internal-hook gnus-article-internal-prepare-hook)
10737            (group gnus-newsgroup-name)
10738            result)
10739       (save-excursion
10740         (gnus-article-setup-buffer)
10741         (set-buffer gnus-article-buffer)
10742         (if (not (setq result (let ((buffer-read-only nil))
10743                                 (gnus-request-article-this-buffer 
10744                                  article group))))
10745             ;; There is no such article.
10746             (save-excursion
10747               (if (not (numberp article))
10748                   ()
10749                 (setq gnus-article-current 
10750                       (cons gnus-newsgroup-name article))
10751                 (set-buffer gnus-summary-buffer)
10752                 (setq gnus-current-article article)
10753                 (gnus-summary-mark-article article gnus-canceled-mark))
10754               (gnus-message 1 "No such article (may be canceled)")
10755               (ding)
10756               nil)
10757           (if (or (eq result 'pseudo) (eq result 'nneething))
10758               (progn
10759                 (save-excursion
10760                   (set-buffer summary-buffer)
10761                   (setq gnus-last-article gnus-current-article
10762                         gnus-newsgroup-history (cons gnus-current-article
10763                                                      gnus-newsgroup-history)
10764                         gnus-current-article 0
10765                         gnus-current-headers nil
10766                         gnus-article-current nil)
10767                   (if (eq result 'nneething)
10768                       (gnus-configure-windows 'summary)
10769                     (gnus-configure-windows 'article))
10770                   (gnus-set-global-variables))
10771                 (gnus-set-mode-line 'article))
10772             ;; The result from the `request' was an actual article -
10773             ;; or at least some text that is now displayed in the
10774             ;; article buffer.
10775             (if (and (numberp article)
10776                      (not (eq article gnus-current-article)))
10777                 ;; Seems like a new article has been selected.
10778                 ;; `gnus-current-article' must be an article number.
10779                 (save-excursion
10780                   (set-buffer summary-buffer)
10781                   (setq gnus-last-article gnus-current-article
10782                         gnus-newsgroup-history (cons gnus-current-article
10783                                                      gnus-newsgroup-history)
10784                         gnus-current-article article
10785                         gnus-current-headers 
10786                         (gnus-get-header-by-num gnus-current-article)
10787                         gnus-article-current 
10788                         (cons gnus-newsgroup-name gnus-current-article))
10789                   (gnus-summary-show-thread)
10790                   (run-hooks 'gnus-mark-article-hook)
10791                   (gnus-set-mode-line 'summary)
10792                   (and gnus-visual 
10793                        (run-hooks 'gnus-visual-mark-article-hook))
10794                   ;; Set the global newsgroup variables here.
10795                   ;; Suggested by Jim Sisolak
10796                   ;; <sisolak@trans4.neep.wisc.edu>.
10797                   (gnus-set-global-variables)
10798                   (setq gnus-have-all-headers 
10799                         (or all-headers gnus-show-all-headers))
10800                   (and gnus-use-cache 
10801                        (vectorp (gnus-get-header-by-number article))
10802                        (gnus-cache-possibly-enter-article
10803                         group article
10804                         (gnus-get-header-by-number article)
10805                         (memq article gnus-newsgroup-marked)
10806                         (memq article gnus-newsgroup-dormant)
10807                         (memq article gnus-newsgroup-unreads)))))
10808             ;; Hooks for getting information from the article.
10809             ;; This hook must be called before being narrowed.
10810             (let (buffer-read-only)
10811               (run-hooks 'internal-hook)
10812               (run-hooks 'gnus-article-prepare-hook)
10813               ;; Decode MIME message.
10814               (if (and gnus-show-mime
10815                        (or (not gnus-strict-mime)
10816                            (gnus-fetch-field "Mime-Version")))
10817                   (funcall gnus-show-mime-method))
10818               ;; Perform the article display hooks.
10819               (run-hooks 'gnus-article-display-hook))
10820             ;; Do page break.
10821             (goto-char (point-min))
10822             (and gnus-break-pages (gnus-narrow-to-page))
10823             (gnus-set-mode-line 'article)
10824             (gnus-configure-windows 'article)
10825             (goto-char (point-min))
10826             t))))))
10827
10828 (defun gnus-article-show-all-headers ()
10829   "Show all article headers in article mode buffer."
10830   (save-excursion 
10831     (gnus-article-setup-buffer)
10832     (set-buffer gnus-article-buffer)
10833     (let ((buffer-read-only nil))
10834       (remove-text-properties (point-min) (point-max) 
10835                               gnus-hidden-properties))))
10836
10837 (defun gnus-article-hide-headers-if-wanted ()
10838   "Hide unwanted headers if `gnus-have-all-headers' is nil.
10839 Provided for backwards compatability."
10840   (or (save-excursion (set-buffer gnus-summary-buffer) gnus-have-all-headers)
10841       (gnus-article-hide-headers)))
10842
10843 (defun gnus-article-hide-headers (&optional delete)
10844   "Hide unwanted headers and possibly sort them as well."
10845   (interactive "P")
10846   (save-excursion
10847     (set-buffer gnus-article-buffer)
10848     (save-restriction
10849       (let ((sorted gnus-sorted-header-list)
10850             (buffer-read-only nil)
10851             want-list beg want-l)
10852         ;; First we narrow to just the headers.
10853         (widen)
10854         (goto-char (point-min))
10855         ;; Hide any "From " lines at the beginning of (mail) articles. 
10856         (while (looking-at "From ")
10857           (forward-line 1))
10858         (if (bobp) 
10859             (add-text-properties (point-min) (point) gnus-hidden-properties))
10860         ;; Then treat the rest of the header lines.
10861         (narrow-to-region 
10862          (point) 
10863          (progn (search-forward "\n\n" nil t) (forward-line -1) (point)))
10864         ;; Then we use the two regular expressions
10865         ;; `gnus-ignored-headers' and `gnus-visible-headers' to
10866         ;; select which header lines is to remain visible in the
10867         ;; article buffer.
10868         (goto-char (point-min))
10869         (while (re-search-forward "^[^ \t]*:" nil t)
10870           (beginning-of-line)
10871           ;; We add the headers we want to keep to a list and delete
10872           ;; them from the buffer.
10873           (if (or (and (stringp gnus-visible-headers)
10874                        (looking-at gnus-visible-headers))
10875                   (and (not (stringp gnus-visible-headers))
10876                        (stringp gnus-ignored-headers)
10877                        (not (looking-at gnus-ignored-headers))))
10878               (progn
10879                 (setq beg (point))
10880                 (forward-line 1)
10881                 ;; Be sure to get multi-line headers...
10882                 (re-search-forward "^[^ \t]*:" nil t)
10883                 (beginning-of-line)
10884                 (setq want-list 
10885                       (cons (buffer-substring beg (point)) want-list))
10886                 (delete-region beg (point))
10887                 (goto-char beg))
10888             (forward-line 1)))
10889         ;; Next we perform the sorting by looking at
10890         ;; `gnus-sorted-header-list'. 
10891         (goto-char (point-min))
10892         (while (and sorted want-list)
10893           (setq want-l want-list)
10894           (while (and want-l
10895                       (not (string-match (car sorted) (car want-l))))
10896             (setq want-l (cdr want-l)))
10897           (if want-l 
10898               (progn
10899                 (insert (car want-l))
10900                 (setq want-list (delq (car want-l) want-list))))
10901           (setq sorted (cdr sorted)))
10902         ;; Any headers that were not matched by the sorted list we
10903         ;; just tack on the end of the visible header list.
10904         (while want-list
10905           (insert (car want-list))
10906           (setq want-list (cdr want-list)))
10907         ;; And finally we make the unwanted headers invisible.
10908         (if delete
10909             (delete-region (point) (point-max))
10910           ;; Suggested by Sudish Joseph <joseph@cis.ohio-state.edu>.
10911           (add-text-properties (point) (point-max) gnus-hidden-properties))))))
10912
10913 ;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.
10914 (defun gnus-article-treat-overstrike ()
10915   "Translate overstrikes into bold text."
10916   (interactive)
10917   (save-excursion
10918     (set-buffer gnus-article-buffer)
10919     (let ((buffer-read-only nil))
10920       (while (search-forward "\b" nil t)
10921         (let ((next (following-char))
10922               (previous (char-after (- (point) 2))))
10923           (cond ((eq next previous)
10924                  (put-text-property (- (point) 2) (point)
10925                                     'invisible t)
10926                  (put-text-property (point) (1+ (point))
10927                                     'face 'bold))
10928                 ((eq next ?_)
10929                  (put-text-property (1- (point)) (1+ (point))
10930                                     'invisible t)
10931                  (put-text-property (1- (point)) (point)
10932                                     'face 'underline))
10933                 ((eq previous ?_)
10934                  (put-text-property (- (point) 2) (point)
10935                                     'invisible t)
10936                  (put-text-property (point) (1+ (point))
10937                                     'face 'underline))))))))
10938
10939 (defun gnus-article-word-wrap ()
10940   "Format too long lines."
10941   (interactive)
10942   (save-excursion
10943     (set-buffer gnus-article-buffer)
10944     (let ((buffer-read-only nil))
10945       (goto-char (point-min))
10946       (search-forward "\n\n" nil t)
10947       (end-of-line 1)
10948       (let ((paragraph-start "^[>|#:<;* ]*[ \t]*$")
10949             (adaptive-fill-regexp "[ \t]*\\([|#:<;>*]+ *\\)?")
10950             (adaptive-fill-mode t))
10951         (while (not (eobp))
10952           (and (>= (current-column) (min fill-column (window-width)))
10953                (/= (preceding-char) ?:)
10954                (fill-paragraph nil))
10955           (end-of-line 2))))))
10956
10957 (defun gnus-article-remove-cr ()
10958   "Remove carriage returns from an article."
10959   (interactive)
10960   (save-excursion
10961     (set-buffer gnus-article-buffer)
10962     (let ((buffer-read-only nil))
10963       (goto-char (point-min))
10964       (while (search-forward "\r" nil t)
10965         (replace-match "" t t)))))
10966
10967 (defun gnus-article-display-x-face (&optional force)
10968   "Look for an X-Face header and display it if present."
10969   (interactive (list 'force))
10970   (save-excursion
10971     (set-buffer gnus-article-buffer)
10972     (let ((inhibit-point-motion-hooks t)
10973           (case-fold-search nil))
10974       (save-restriction
10975         (goto-char (point-min))
10976         (search-forward "\n\n")
10977         (narrow-to-region (point-min) (point))
10978         (goto-char (point-min))
10979         (if (or (not gnus-article-x-face-command)
10980                 (and (not force)
10981                      (or (not gnus-article-x-face-too-ugly)
10982                          (string-match gnus-article-x-face-too-ugly
10983                                        (mail-fetch-field "from"))))
10984                 (progn
10985                   (goto-char (point-min))
10986                   (not (re-search-forward "^X-Face: " nil t))))
10987             nil
10988           (let ((beg (point))
10989                 (end (1- (re-search-forward "^\\($\\|[^ \t]\\)" nil t))))
10990             (if (symbolp gnus-article-x-face-command)
10991                 (and (or (fboundp gnus-article-x-face-command)
10992                          (error "%s is not a function"
10993                                 gnus-article-x-face-command))
10994                      (funcall gnus-article-x-face-command beg end))
10995               (call-process-region beg end "sh" nil 0 nil
10996                                    "-c" gnus-article-x-face-command))))))))
10997
10998 (defun gnus-article-de-quoted-unreadable (&optional force)
10999   "Do a naïve translation of a quoted-printable-encoded article.
11000 This is in no way, shape or form meant as a replacement for real MIME
11001 processing, but is simply a stop-gap measure until MIME support is
11002 written.
11003 If FORCE, decode the article whether it is marked as quoted-printable
11004 or not." 
11005   (interactive (list 'force))
11006   (save-excursion
11007     (set-buffer gnus-article-buffer)
11008     (let ((case-fold-search t)
11009           (buffer-read-only nil)
11010           (type (gnus-fetch-field "content-transfer-encoding")))
11011       (if (or force (and type (string-match "quoted-printable" type)))
11012           (progn
11013             (goto-char (point-min))
11014             (search-forward "\n\n" nil 'move)
11015             (gnus-mime-decode-quoted-printable (point) (point-max)))))))
11016
11017 (defun gnus-mime-decode-quoted-printable (from to)
11018   ;; Decode quoted-printable from region between FROM and TO.
11019   (save-excursion
11020     (goto-char from)
11021     (while (search-forward "=" to t)
11022       (cond ((eq (following-char) ?\n)
11023              (delete-char -1)
11024              (delete-char 1))
11025             ((looking-at "[0-9A-F][0-9A-F]")
11026              (delete-char -1)
11027              (insert (hexl-hex-string-to-integer
11028                       (buffer-substring (point) (+ 2 (point)))))
11029              (delete-char 2))
11030             ((looking-at "=")
11031              (delete-char 1))
11032             ((gnus-message 3 "Malformed MIME quoted-printable message"))))))
11033
11034 (defvar gnus-article-time-units
11035   (list (cons 'year (* 365.25 24 60 60))
11036         (cons 'week (* 7 24 60 60))
11037         (cons 'day (* 24 60 60))
11038         (cons 'hour (* 60 60))
11039         (cons 'minute 60)
11040         (cons 'second 1)))
11041
11042 (defun gnus-article-date-ut (&optional type)
11043   "Convert DATE date to universal time in the current article.
11044 If TYPE is `local', convert to local time; if it is `lapsed', output
11045 how much time has lapsed since DATE."
11046   (interactive (list 'ut))
11047   (let ((date (header-date (or gnus-current-headers 
11048                                (gnus-get-header-by-number
11049                                 (gnus-summary-article-number))"")))
11050         (date-regexp "^Date: \\|^X-Sent: "))
11051     (if (or (not date)
11052             (string= date ""))
11053         ()
11054       (save-excursion
11055         (set-buffer gnus-article-buffer)
11056         (let ((buffer-read-only nil))
11057           (goto-char (point-min))
11058           (if (and (re-search-forward date-regexp nil t)
11059                    (progn 
11060                      (beginning-of-line)
11061                      (looking-at date-regexp)))
11062               (delete-region (gnus-point-at-bol)
11063                              (progn (end-of-line) (1+ (point))))
11064             (goto-char (point-min))
11065             (goto-char (- (search-forward "\n\n") 2)))
11066           (insert
11067            (cond 
11068             ((eq type 'local)
11069              (concat "Date: " (condition-case ()
11070                                   (timezone-make-date-arpa-standard date)
11071                                 (error date))
11072                      "\n"))
11073             ((eq type 'ut)
11074              (concat "Date: "
11075                      (condition-case ()
11076                          (timezone-make-date-arpa-standard date nil "UT")
11077                        (error date))
11078                      "\n"))
11079             ((eq type 'lapsed)
11080              ;; If the date is seriously mangled, the timezone
11081              ;; functions are liable to bug out, so we condition-case
11082              ;; the entire thing.  
11083              (let* ((real-sec (condition-case ()
11084                                   (- (gnus-seconds-since-epoch 
11085                                       (timezone-make-date-arpa-standard
11086                                        (current-time-string) 
11087                                        (current-time-zone) "UT"))
11088                                      (gnus-seconds-since-epoch 
11089                                       (timezone-make-date-arpa-standard 
11090                                        date nil "UT")))
11091                                 (error 0)))
11092                     (sec (abs real-sec))
11093                     num prev)
11094                (if (zerop sec)
11095                    "X-Sent: Now\n"
11096                  (concat
11097                   "X-Sent: "
11098                   (mapconcat 
11099                    (lambda (unit)
11100                      (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
11101                          ""
11102                        (setq sec (- sec (* num (cdr unit))))
11103                        (prog1
11104                            (concat (if prev ", " "") (int-to-string 
11105                                                       (floor num))
11106                                    " " (symbol-name (car unit))
11107                                    (if (> num 1) "s" ""))
11108                          (setq prev t))))
11109                    gnus-article-time-units "")
11110                   (if (> real-sec 0)
11111                       " ago\n"
11112                     " in the future\n")))))
11113             (t
11114              (error "Unknown conversion type: %s" type)))))))))
11115
11116 (defun gnus-article-date-local ()
11117   "Convert the current article date to the local timezone."
11118   (interactive)
11119   (gnus-article-date-ut 'local))
11120
11121 (defun gnus-article-date-lapsed ()
11122   "Convert the current article date to time lapsed since it was sent."
11123   (interactive)
11124   (gnus-article-date-ut 'lapsed))
11125
11126 (defun gnus-article-maybe-highlight ()
11127   "Do some article highlighting if `gnus-visual' is non-nil."
11128   (if gnus-visual (gnus-article-highlight-some)))
11129
11130 ;; Article savers.
11131
11132 (defun gnus-output-to-rmail (file-name)
11133   "Append the current article to an Rmail file named FILE-NAME."
11134   (require 'rmail)
11135   ;; Most of these codes are borrowed from rmailout.el.
11136   (setq file-name (expand-file-name file-name))
11137   (setq rmail-default-rmail-file file-name)
11138   (let ((artbuf (current-buffer))
11139         (tmpbuf (get-buffer-create " *Gnus-output*")))
11140     (save-excursion
11141       (or (get-file-buffer file-name)
11142           (file-exists-p file-name)
11143           (if (gnus-yes-or-no-p
11144                (concat "\"" file-name "\" does not exist, create it? "))
11145               (let ((file-buffer (create-file-buffer file-name)))
11146                 (save-excursion
11147                   (set-buffer file-buffer)
11148                   (rmail-insert-rmail-file-header)
11149                   (let ((require-final-newline nil))
11150                     (write-region (point-min) (point-max) file-name t 1)))
11151                 (kill-buffer file-buffer))
11152             (error "Output file does not exist")))
11153       (set-buffer tmpbuf)
11154       (buffer-disable-undo (current-buffer))
11155       (erase-buffer)
11156       (insert-buffer-substring artbuf)
11157       (gnus-convert-article-to-rmail)
11158       ;; Decide whether to append to a file or to an Emacs buffer.
11159       (let ((outbuf (get-file-buffer file-name)))
11160         (if (not outbuf)
11161             (append-to-file (point-min) (point-max) file-name)
11162           ;; File has been visited, in buffer OUTBUF.
11163           (set-buffer outbuf)
11164           (let ((buffer-read-only nil)
11165                 (msg (and (boundp 'rmail-current-message)
11166                           (symbol-value 'rmail-current-message))))
11167             ;; If MSG is non-nil, buffer is in RMAIL mode.
11168             (if msg
11169                 (progn (widen)
11170                        (narrow-to-region (point-max) (point-max))))
11171             (insert-buffer-substring tmpbuf)
11172             (if msg
11173                 (progn
11174                   (goto-char (point-min))
11175                   (widen)
11176                   (search-backward "\^_")
11177                   (narrow-to-region (point) (point-max))
11178                   (goto-char (1+ (point-min)))
11179                   (rmail-count-new-messages t)
11180                   (rmail-show-message msg)))))))
11181     (kill-buffer tmpbuf)))
11182
11183 (defun gnus-output-to-file (file-name)
11184   "Append the current article to a file named FILE-NAME."
11185   (setq file-name (expand-file-name file-name))
11186   (let ((artbuf (current-buffer))
11187         (tmpbuf (get-buffer-create " *Gnus-output*")))
11188     (save-excursion
11189       (set-buffer tmpbuf)
11190       (buffer-disable-undo (current-buffer))
11191       (erase-buffer)
11192       (insert-buffer-substring artbuf)
11193       ;; Append newline at end of the buffer as separator, and then
11194       ;; save it to file.
11195       (goto-char (point-max))
11196       (insert "\n")
11197       (append-to-file (point-min) (point-max) file-name))
11198     (kill-buffer tmpbuf)))
11199
11200 (defun gnus-convert-article-to-rmail ()
11201   "Convert article in current buffer to Rmail message format."
11202   (let ((buffer-read-only nil))
11203     ;; Convert article directly into Babyl format.
11204     ;; Suggested by Rob Austein <sra@lcs.mit.edu>
11205     (goto-char (point-min))
11206     (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
11207     (while (search-forward "\n\^_" nil t) ;single char
11208       (replace-match "\n^_" t t))               ;2 chars: "^" and "_"
11209     (goto-char (point-max))
11210     (insert "\^_")))
11211
11212 (defun gnus-narrow-to-page (&optional arg)
11213   "Make text outside current page invisible except for page delimiter.
11214 A numeric arg specifies to move forward or backward by that many pages,
11215 thus showing a page other than the one point was originally in."
11216   (interactive "P")
11217   (setq arg (if arg (prefix-numeric-value arg) 0))
11218   (save-excursion
11219     (forward-page -1)                   ;Beginning of current page.
11220     (widen)
11221     (if (> arg 0)
11222         (forward-page arg)
11223       (if (< arg 0)
11224           (forward-page (1- arg))))
11225     ;; Find the end of the page.
11226     (forward-page)
11227     ;; If we stopped due to end of buffer, stay there.
11228     ;; If we stopped after a page delimiter, put end of restriction
11229     ;; at the beginning of that line.
11230     ;; These are commented out.
11231     ;;    (if (save-excursion (beginning-of-line)
11232     ;;                  (looking-at page-delimiter))
11233     ;;  (beginning-of-line))
11234     (narrow-to-region (point)
11235                       (progn
11236                         ;; Find the top of the page.
11237                         (forward-page -1)
11238                         ;; If we found beginning of buffer, stay there.
11239                         ;; If extra text follows page delimiter on same line,
11240                         ;; include it.
11241                         ;; Otherwise, show text starting with following line.
11242                         (if (and (eolp) (not (bobp)))
11243                             (forward-line 1))
11244                         (point)))))
11245
11246 (defun gnus-gmt-to-local ()
11247   "Rewrite Date header described in GMT to local in current buffer.
11248 Intended to be used with gnus-article-prepare-hook."
11249   (save-excursion
11250     (save-restriction
11251       (widen)
11252       (goto-char (point-min))
11253       (narrow-to-region (point-min)
11254                         (progn (search-forward "\n\n" nil 'move) (point)))
11255       (goto-char (point-min))
11256       (if (re-search-forward "^Date:[ \t]\\(.*\\)$" nil t)
11257           (let ((buffer-read-only nil)
11258                 (date (buffer-substring-no-properties
11259                        (match-beginning 1) (match-end 1))))
11260             (delete-region (match-beginning 1) (match-end 1))
11261             (insert
11262              (timezone-make-date-arpa-standard 
11263               date nil (current-time-zone))))))))
11264
11265
11266 ;; Article mode commands
11267
11268 (defun gnus-article-next-page (&optional lines)
11269   "Show next page of current article.
11270 If end of article, return non-nil. Otherwise return nil.
11271 Argument LINES specifies lines to be scrolled up."
11272   (interactive "P")
11273   (move-to-window-line -1)
11274   ;; Fixed by enami@ptgd.sony.co.jp (enami tsugutomo)
11275   (if (save-excursion
11276         (end-of-line)
11277         (and (pos-visible-in-window-p)  ;Not continuation line.
11278              (eobp)))
11279       ;; Nothing in this page.
11280       (if (or (not gnus-break-pages)
11281               (save-excursion
11282                 (save-restriction
11283                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
11284           t                             ;Nothing more.
11285         (gnus-narrow-to-page 1)         ;Go to next page.
11286         nil)
11287     ;; More in this page.
11288     (condition-case ()
11289         (scroll-up lines)
11290       (end-of-buffer
11291        ;; Long lines may cause an end-of-buffer error.
11292        (goto-char (point-max))))
11293     nil))
11294
11295 (defun gnus-article-prev-page (&optional lines)
11296   "Show previous page of current article.
11297 Argument LINES specifies lines to be scrolled down."
11298   (interactive "P")
11299   (move-to-window-line 0)
11300   (if (and gnus-break-pages
11301            (bobp)
11302            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
11303       (progn
11304         (gnus-narrow-to-page -1) ;Go to previous page.
11305         (goto-char (point-max))
11306         (recenter -1))
11307     (scroll-down lines)))
11308
11309 (defun gnus-article-refer-article ()
11310   "Read article specified by message-id around point."
11311   (interactive)
11312   (search-forward ">" nil t)    ;Move point to end of "<....>".
11313   (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
11314       (let ((message-id
11315              (buffer-substring (match-beginning 1) (match-end 1))))
11316         (set-buffer gnus-summary-buffer)
11317         (gnus-summary-refer-article message-id))
11318     (error "No references around point")))
11319
11320 (defun gnus-article-show-summary ()
11321   "Reconfigure windows to show summary buffer."
11322   (interactive)
11323   (gnus-configure-windows 'article)
11324   (gnus-summary-goto-subject gnus-current-article))
11325
11326 (defun gnus-article-describe-briefly ()
11327   "Describe article mode commands briefly."
11328   (interactive)
11329   (gnus-message 6
11330    (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")))
11331
11332 (defun gnus-article-summary-command ()
11333   "Execute the last keystroke in the summary buffer."
11334   (interactive)
11335   (let ((obuf (current-buffer))
11336         (owin (current-window-configuration))
11337         func)
11338     (switch-to-buffer gnus-summary-buffer 'norecord)
11339     (setq func (lookup-key (current-local-map) (this-command-keys)))
11340     (call-interactively func)
11341     (set-buffer obuf)
11342     (set-window-configuration owin)
11343     (set-window-point (get-buffer-window (current-buffer)) (point))))
11344
11345 (defun gnus-article-summary-command-nosave ()
11346   "Execute the last keystroke in the summary buffer."
11347   (interactive)
11348   (let (func)
11349     (pop-to-buffer gnus-summary-buffer 'norecord)
11350     (setq func (lookup-key (current-local-map) (this-command-keys)))
11351     (call-interactively func)))
11352
11353 \f
11354 ;; Basic ideas by emv@math.lsa.umich.edu (Edward Vielmetti)
11355
11356 ;;;###autoload
11357 (defalias 'gnus-batch-kill 'gnus-batch-score)
11358 ;;;###autoload
11359 (defun gnus-batch-score ()
11360   "Run batched scoring.
11361 Usage: emacs -batch -l gnus -f gnus-batch-score <newsgroups> ...
11362 Newsgroups is a list of strings in Bnews format.  If you want to score
11363 the comp hierarchy, you'd say \"comp.all\". If you would not like to
11364 score the alt hierarchy, you'd say \"!alt.all\"."
11365   (interactive)
11366   (let* ((yes-and-no
11367           (gnus-newsrc-parse-options
11368            (apply (function concat)
11369                   (mapcar (lambda (g) (concat g " "))
11370                           command-line-args-left))))
11371          (gnus-expert-user t)
11372          (nnmail-spool-file nil)
11373          (gnus-use-dribble-file nil)
11374          (yes (car yes-and-no))
11375          (no (cdr yes-and-no))
11376          group newsrc entry
11377          ;; Disable verbose message.
11378          gnus-novice-user gnus-large-newsgroup)
11379     ;; Eat all arguments.
11380     (setq command-line-args-left nil)
11381     ;; Start Gnus.
11382     (gnus)
11383     ;; Apply kills to specified newsgroups in command line arguments.
11384     (setq newsrc (cdr gnus-newsrc-alist))
11385     (while newsrc
11386       (setq group (car (car newsrc)))
11387       (setq entry (gnus-gethash group gnus-newsrc-hashtb))
11388       (if (and (<= (nth 1 (car newsrc)) gnus-level-subscribed)
11389                (and (car entry)
11390                     (or (eq (car entry) t)
11391                         (not (zerop (car entry)))))
11392                (if yes (string-match yes group) t)
11393                (or (null no) (not (string-match no group))))
11394           (progn
11395             (gnus-summary-read-group group nil t)
11396             (and (eq (current-buffer) (get-buffer gnus-summary-buffer))
11397                  (gnus-summary-exit))))
11398       (setq newsrc (cdr newsrc)))
11399     ;; Exit Emacs.
11400     (switch-to-buffer gnus-group-buffer)
11401     (gnus-group-save-newsrc)))
11402
11403 (defun gnus-apply-kill-file ()
11404   "Apply a kill file to the current newsgroup.
11405 Returns the number of articles marked as read."
11406   (if (or (file-exists-p (gnus-newsgroup-kill-file nil))
11407           (file-exists-p (gnus-newsgroup-kill-file gnus-newsgroup-name)))
11408       (gnus-apply-kill-file-internal)
11409     0))
11410
11411 (defun gnus-kill-save-kill-buffer ()
11412   (save-excursion
11413     (let ((file (gnus-newsgroup-kill-file gnus-newsgroup-name)))
11414       (if (get-file-buffer file)
11415           (progn
11416             (set-buffer (get-file-buffer file))
11417             (and (buffer-modified-p) (save-buffer))
11418             (kill-buffer (current-buffer)))))))
11419
11420 (defvar gnus-kill-file-name "KILL"
11421   "Suffix of the kill files.")
11422
11423 (defun gnus-newsgroup-kill-file (newsgroup)
11424   "Return the name of a kill file name for NEWSGROUP.
11425 If NEWSGROUP is nil, return the global kill file name instead."
11426   (cond ((or (null newsgroup)
11427              (string-equal newsgroup ""))
11428          ;; The global KILL file is placed at top of the directory.
11429          (expand-file-name gnus-kill-file-name
11430                            (or gnus-kill-files-directory "~/News")))
11431         ((gnus-use-long-file-name 'not-kill)
11432          ;; Append ".KILL" to newsgroup name.
11433          (expand-file-name (concat (gnus-newsgroup-saveable-name newsgroup)
11434                                    "." gnus-kill-file-name)
11435                            (or gnus-kill-files-directory "~/News")))
11436         (t
11437          ;; Place "KILL" under the hierarchical directory.
11438          (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
11439                                    "/" gnus-kill-file-name)
11440                            (or gnus-kill-files-directory "~/News")))))
11441
11442 \f
11443 ;;;
11444 ;;; Dribble file
11445 ;;;
11446
11447 (defvar gnus-dribble-ignore nil)
11448 (defvar gnus-dribble-eval-file nil)
11449
11450 (defun gnus-dribble-file-name ()
11451   (concat gnus-current-startup-file "-dribble"))
11452
11453 (defun gnus-dribble-enter (string)
11454   (if (and (not gnus-dribble-ignore)
11455            gnus-dribble-buffer
11456            (buffer-name gnus-dribble-buffer))
11457       (let ((obuf (current-buffer)))
11458         (set-buffer gnus-dribble-buffer)
11459         (insert string "\n")
11460         (set-window-point (get-buffer-window (current-buffer)) (point-max))
11461         (set-buffer obuf))))
11462
11463 (defun gnus-dribble-read-file ()
11464   (let ((dribble-file (gnus-dribble-file-name)))
11465     (save-excursion 
11466       (set-buffer (setq gnus-dribble-buffer 
11467                         (get-buffer-create 
11468                          (file-name-nondirectory dribble-file))))
11469       (gnus-add-current-to-buffer-list)
11470       (erase-buffer)
11471       (set-visited-file-name dribble-file)
11472       (buffer-disable-undo (current-buffer))
11473       (bury-buffer (current-buffer))
11474       (set-buffer-modified-p nil)
11475       (let ((auto (make-auto-save-file-name))
11476             (gnus-dribble-ignore t))
11477         (if (or (file-exists-p auto) (file-exists-p dribble-file))
11478             (progn
11479               (if (file-newer-than-file-p auto dribble-file)
11480                   (setq dribble-file auto))
11481               (insert-file-contents dribble-file)
11482               (if (not (zerop (buffer-size)))
11483                   (set-buffer-modified-p t))
11484               (if (gnus-y-or-n-p 
11485                    "Auto-save file exists. Do you want to read it? ")
11486                   (setq gnus-dribble-eval-file t))))))))
11487
11488 (defun gnus-dribble-eval-file ()
11489   (if (not gnus-dribble-eval-file)
11490       ()
11491     (setq gnus-dribble-eval-file nil)
11492     (save-excursion
11493       (let ((gnus-dribble-ignore t))
11494         (set-buffer gnus-dribble-buffer)
11495         (eval-buffer (current-buffer))))))
11496
11497 (defun gnus-dribble-delete-file ()
11498   (if (file-exists-p (gnus-dribble-file-name))
11499       (delete-file (gnus-dribble-file-name)))
11500   (if gnus-dribble-buffer
11501       (save-excursion
11502         (set-buffer gnus-dribble-buffer)
11503         (let ((auto (make-auto-save-file-name)))
11504           (if (file-exists-p auto)
11505               (delete-file auto))
11506           (erase-buffer)
11507           (set-buffer-modified-p nil)))))
11508
11509 (defun gnus-dribble-save ()
11510   (if (and gnus-dribble-buffer
11511            (buffer-name gnus-dribble-buffer))
11512       (save-excursion
11513         (set-buffer gnus-dribble-buffer)
11514         (save-buffer))))
11515
11516 (defun gnus-dribble-clear ()
11517   (save-excursion
11518     (if (gnus-buffer-exists-p gnus-dribble-buffer)
11519         (progn
11520           (set-buffer gnus-dribble-buffer)
11521           (erase-buffer)
11522           (set-buffer-modified-p nil)
11523           (setq buffer-saved-size (buffer-size))))))
11524
11525 ;;;
11526 ;;; Server Communication
11527 ;;;
11528
11529 ;; All the Gnus backends have the same interface, and should return
11530 ;; data in a similar format. Below is an overview of what functions
11531 ;; these packages must supply and what results they should return.
11532 ;;
11533 ;; Variables:
11534 ;;
11535 ;; `nntp-server-buffer' - All data should be returned to Gnus in this
11536 ;; buffer. 
11537 ;;
11538 ;; Functions for the imaginary backend `choke':
11539 ;;
11540 ;; `choke-retrieve-headers ARTICLES &optional GROUP SERVER'
11541 ;; Should return all headers for all ARTICLES, or return NOV lines for
11542 ;; the same.
11543 ;;
11544 ;; `choke-request-group GROUP &optional SERVER DISCARD'
11545 ;; Switch to GROUP. If DISCARD is nil, active information on the group
11546 ;; must be returned.
11547 ;;
11548 ;; `choke-close-group GROUP &optional SERVER'
11549 ;; Close group. Most backends won't have to do anything with this
11550 ;; call, but it is an opportunity to clean up, if that is needed. It
11551 ;; is called when Gnus exits a group.
11552 ;;
11553 ;; `choke-request-article ARTICLE &optional GROUP SERVER'
11554 ;; Return ARTICLE, which is either an article number or
11555 ;; message-id. Note that not all backends can return articles based on
11556 ;; message-id. 
11557 ;;
11558 ;; `choke-request-list SERVER'
11559 ;; Return a list of all newsgroups on SERVER.
11560 ;;
11561 ;; `choke-request-list-newsgroups SERVER'
11562 ;; Return a list of descriptions of all newsgroups on SERVER.
11563 ;;
11564 ;; `choke-request-newgroups DATE &optional SERVER'
11565 ;; Return a list of all groups that have arrived after DATE on
11566 ;; SERVER. Note that the date doesn't have to be respected - Gnus will
11567 ;; always check whether the groups are old or not. Backends that do
11568 ;; not store date information may just return the entire list of
11569 ;; groups, although this might not be a good idea in general.
11570 ;;
11571 ;; `choke-request-post-buffer METHOD HEADER ARTICLE-BUFFER GROUP INFO'
11572 ;; Should return a buffer that is suitable for "posting". nnspool and
11573 ;; nntp return a `*post-buffer*', and nnmail return a `*mail*'
11574 ;; buffer. This function should fill out the appropriate headers. 
11575 ;;
11576 ;; `choke-request-post &optional SERVER'
11577 ;; Function that will be called from a buffer to be posted. 
11578 ;;
11579 ;; `choke-open-server SERVER &optional ARGUMENT'
11580 ;; Open a connection to SERVER.
11581 ;;
11582 ;; `choke-close-server &optional SERVER'
11583 ;; Close the connection to SERVER.
11584 ;;
11585 ;; `choke-server-opened &optional SERVER'
11586 ;; Whether the conenction to SERVER is opened or not.
11587 ;;
11588 ;; `choke-server-status &optional SERVER'
11589 ;; Should return a status string (not in the nntp buffer, but as the
11590 ;; result of the function).
11591 ;;
11592 ;; `choke-retrieve-groups GROUPS &optional SERVER'
11593 ;; Optional function for retrieving active file info on all groups in
11594 ;; GROUPS.  Two return formats are supported: The normal active file
11595 ;; format, and a list of GROUP lines.  This function should return (as
11596 ;; a function value) either `active' or `group', depending on what
11597 ;; format it returns.
11598 ;;
11599 ;; The following functions are optional and apply only to backends
11600 ;; that are able to control the contents of their groups totally
11601 ;; (ie. mail backends.)  Backends that aren't able to do that
11602 ;; shouldn't define these functions at all. Gnus will check for their
11603 ;; presence before attempting to call them.
11604 ;;
11605 ;; `choke-request-expire-articles ARTICLES &optional NEWSGROUP SERVER'
11606 ;; Should expire (according to some aging scheme) all ARTICLES. Most
11607 ;; backends will not be able to expire articles. Should return a list
11608 ;; of all articles that were not expired.
11609 ;;
11610 ;; `choke-request-move-article ARTICLE GROUP SERVER ACCEPT-FORM &optional LAST'
11611 ;; Should move ARTICLE from GROUP on SERVER by using ACCEPT-FORM.
11612 ;; Removes any information it has added to the article (extra headers,
11613 ;; whatever - make it as clean as possible), and then passes the
11614 ;; article on by evaling ACCEPT-FORM, which is normally a call to the
11615 ;; function described below. If the ACCEPT-FORM returns a non-nil
11616 ;; value, the article should then be deleted. If LAST is nil, that
11617 ;; means that there will be further calls to this function. This might
11618 ;; be taken as an advice not to save buffers/internal variables just
11619 ;; yet, but wait until the last call to speed things up.
11620 ;;
11621 ;; `choke-request-accept-article GROUP &optional LAST' 
11622 ;; The contents of the current buffer will be put into GROUP.  There
11623 ;; should, of course, be an article in the current buffer.  This
11624 ;; function is normally only called by the function described above,
11625 ;; and LAST works the same way as in that function.
11626 ;;
11627 ;; `choke-request-replace-article ARTICLE GROUP BUFFER'
11628 ;; Replace ARTICLE in GROUP with the contents of BUFFER.
11629 ;; This provides an easy interface for allowing editing of
11630 ;; articles. Note that even headers may be edited, so the backend has
11631 ;; to update any tables (nov buffers, etc) that it maintains after
11632 ;; replacing the article.
11633 ;;
11634 ;; `choke-request-create-group GROUP &optional SERVER'
11635 ;; Create GROUP on SERVER.  This might be a new, empty group, or it
11636 ;; might be a group that already exists, but hasn't been registered
11637 ;; yet. 
11638 ;;
11639 ;; All these functions must return nil if they couldn't service the
11640 ;; request. If the optional arguments are not supplied, some "current"
11641 ;; or "default" values should be used. In short, one should emulate an
11642 ;; NNTP server, in a way.
11643 ;;
11644 ;; If you want to write a new backend, you just have to supply the
11645 ;; functions listed above. In addition, you must enter the new backend
11646 ;; into the list of valid select methods:
11647 ;; (setq gnus-valid-select-methods 
11648 ;;       (cons '("choke" mail) gnus-valid-select-methods))
11649 ;; The first element in this list is the name of the backend. Other
11650 ;; elemnets may be `mail' (for mail groups),  `post' (for news
11651 ;; groups), `none' (neither), `respool' (for groups that can control
11652 ;; their contents). 
11653
11654 (defun gnus-start-news-server (&optional confirm)
11655   "Open a method for getting news.
11656 If CONFIRM is non-nil, the user will be asked for an NNTP server."
11657   (let (how)
11658     (if gnus-current-select-method
11659         ;; Stream is already opened.
11660         nil
11661       ;; Open NNTP server.
11662       (if (null gnus-nntp-service) (setq gnus-nntp-server nil))
11663       (if confirm
11664           (progn
11665             ;; Read server name with completion.
11666             (setq gnus-nntp-server
11667                   (completing-read "NNTP server: "
11668                                    (mapcar (lambda (server) (list server))
11669                                            (cons (list gnus-nntp-server)
11670                                                  gnus-secondary-servers))
11671                                    nil nil gnus-nntp-server))))
11672
11673       (if (and gnus-nntp-server 
11674                (stringp gnus-nntp-server)
11675                (not (string= gnus-nntp-server "")))
11676           (setq gnus-select-method
11677                 (cond ((or (string= gnus-nntp-server "")
11678                            (string= gnus-nntp-server "::"))
11679                        (list 'nnspool (system-name)))
11680                       ((string-match "^:" gnus-nntp-server)
11681                        (list 'nnmh gnus-nntp-server 
11682                              (list 'nnmh-directory 
11683                                    (file-name-as-directory
11684                                     (expand-file-name
11685                                      (concat "~/" (substring
11686                                                    gnus-nntp-server 1)))))
11687                              (list 'nnmh-get-new-mail nil)))
11688                       (t
11689                        (list 'nntp gnus-nntp-server)))))
11690
11691       (setq how (car gnus-select-method))
11692       (cond ((eq how 'nnspool)
11693              (require 'nnspool)
11694              (gnus-message 5 "Looking up local news spool..."))
11695             ((eq how 'nnmh)
11696              (require 'nnmh)
11697              (gnus-message 5 "Looking up mh spool..."))
11698             (t
11699              (require 'nntp)))
11700       (setq gnus-current-select-method gnus-select-method)
11701       (run-hooks 'gnus-open-server-hook)
11702       (or 
11703        ;; gnus-open-server-hook might have opened it
11704        (gnus-server-opened gnus-select-method)  
11705        (gnus-open-server gnus-select-method)
11706        (gnus-y-or-n-p
11707         (format
11708          "%s server on %s can't be opened. Continue? "
11709          (car gnus-select-method) (nth 1 gnus-select-method)))
11710        (progn
11711          (gnus-message 1 "Couldn't open server on %s" 
11712                        (nth 1 gnus-select-method))
11713          (ding)
11714          nil)))))
11715
11716 (defun gnus-check-server (&optional method)
11717   "If the news server is down, start it up again."
11718   (let ((method (if method method gnus-select-method)))
11719     (and (stringp method)
11720          (setq method (gnus-server-to-method method)))
11721     (if (gnus-server-opened method)
11722         ;; Stream is already opened.
11723         t
11724       ;; Open server.
11725       (gnus-message 5 "Opening server %s on %s..." (car method) (nth 1 method))
11726       (run-hooks 'gnus-open-server-hook)
11727       (prog1
11728           (gnus-open-server method)
11729         (message "")))))
11730
11731 (defun gnus-nntp-message (&optional message)
11732   "Check the status of the NNTP server.
11733 If the status of the server is clear and MESSAGE is non-nil, MESSAGE
11734 is returned insted of the status string."
11735   (let ((status (gnus-status-message (gnus-find-method-for-group 
11736                                       gnus-newsgroup-name)))
11737         (message (or message "")))
11738     (if (and (stringp status) (> (length status) 0))
11739         status message)))
11740
11741 (defun gnus-get-function (method function)
11742   (and (stringp method)
11743        (setq method (gnus-server-to-method method)))
11744   (let ((func (intern (format "%s-%s" (car method) function))))
11745     (if (not (fboundp func)) 
11746         (progn
11747           (require (car method))
11748           (if (not (fboundp func)) 
11749               (error "No such function: %s" func))))
11750     func))
11751
11752 ;;; Interface functions to the backends.
11753
11754 (defun gnus-open-server (method)
11755   (funcall (gnus-get-function method 'open-server)
11756            (nth 1 method) (nthcdr 2 method)))
11757
11758 (defun gnus-close-server (method)
11759   (funcall (gnus-get-function method 'close-server) (nth 1 method)))
11760
11761 (defun gnus-request-list (method)
11762   (funcall (gnus-get-function method 'request-list) (nth 1 method)))
11763
11764 (defun gnus-request-list-newsgroups (method)
11765   (funcall (gnus-get-function method 'request-list-newsgroups) (nth 1 method)))
11766
11767 (defun gnus-request-newgroups (date method)
11768   (funcall (gnus-get-function method 'request-newgroups) 
11769            date (nth 1 method)))
11770
11771 (defun gnus-server-opened (method)
11772   (funcall (gnus-get-function method 'server-opened) (nth 1 method)))
11773
11774 (defun gnus-status-message (method)
11775   (let ((method (if (stringp method) (gnus-find-method-for-group method)
11776                   method)))
11777     (funcall (gnus-get-function method 'status-message) (nth 1 method))))
11778
11779 (defun gnus-request-group (group &optional dont-check)
11780   (let ((method (gnus-find-method-for-group group)))
11781 ;    (and t (message "%s GROUP %s" (car method) group))
11782     (funcall (gnus-get-function method 'request-group) 
11783              (gnus-group-real-name group) (nth 1 method) dont-check)))
11784
11785 (defun gnus-request-asynchronous (group &optional articles)
11786   (let ((method (gnus-find-method-for-group group)))
11787     (funcall (gnus-get-function method 'request-asynchronous) 
11788              (gnus-group-real-name group) (nth 1 method) articles)))
11789
11790 (defun gnus-list-active-group (group)
11791   (let ((method (gnus-find-method-for-group group))
11792         (func 'list-active-group))
11793     (and (gnus-check-backend-function func group)
11794          (funcall (gnus-get-function method func) 
11795                   (gnus-group-real-name group) (nth 1 method)))))
11796
11797 (defun gnus-request-group-description (group)
11798   (let ((method (gnus-find-method-for-group group))
11799         (func 'request-group-description))
11800     (and (gnus-check-backend-function func group)
11801          (funcall (gnus-get-function method func) 
11802                   (gnus-group-real-name group) (nth 1 method)))))
11803
11804 (defun gnus-close-group (group)
11805   (let ((method (gnus-find-method-for-group group)))
11806     (funcall (gnus-get-function method 'close-group) 
11807              (gnus-group-real-name group) (nth 1 method))))
11808
11809 (defun gnus-retrieve-headers (articles group)
11810   (let ((method (gnus-find-method-for-group group)))
11811     (if (and gnus-use-cache (numberp (car articles)))
11812         (gnus-cache-retrieve-headers articles group)
11813       (funcall (gnus-get-function method 'retrieve-headers) 
11814                articles (gnus-group-real-name group) (nth 1 method)))))
11815
11816 (defun gnus-retrieve-groups (groups method)
11817   (funcall (gnus-get-function method 'retrieve-groups) groups (nth 1 method)))
11818
11819 (defun gnus-request-article (article group &optional buffer)
11820   (let ((method (gnus-find-method-for-group group)))
11821     (funcall (gnus-get-function method 'request-article) 
11822              article (gnus-group-real-name group) (nth 1 method) buffer)))
11823
11824 (defun gnus-request-head (article group)
11825   (let ((method (gnus-find-method-for-group group)))
11826     (funcall (gnus-get-function method 'request-head) 
11827              article (gnus-group-real-name group) (nth 1 method))))
11828
11829 ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
11830 (defun gnus-request-post-buffer (post group subject header artbuf
11831                                       info follow-to respect-poster)
11832    (let* ((info (or info (and group (nth 2 (gnus-gethash 
11833                                             group gnus-newsrc-hashtb)))))
11834           (method
11835            (if (and gnus-post-method
11836                     ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
11837                     (memq 'post (assoc
11838                                  (format "%s" (car (gnus-find-method-for-group
11839                                                     gnus-newsgroup-name)))
11840                                         gnus-valid-select-methods)))
11841                gnus-post-method
11842              (gnus-find-method-for-group gnus-newsgroup-name))))
11843      (or (gnus-check-server method)
11844          (error "Can't open server %s:%s" (car method) (nth 1 method)))
11845      (let ((mail-self-blind nil)
11846            (mail-archive-file-name nil))
11847        (funcall (gnus-get-function method 'request-post-buffer) 
11848                 post group subject header artbuf info follow-to
11849                 respect-poster))))
11850
11851 (defun gnus-request-post (method &optional force)
11852   (and (stringp method)
11853        (setq method (gnus-server-to-method method)))
11854   (and (not force) gnus-post-method
11855        (memq 'post (assoc (format "%s" (car method))
11856                           gnus-valid-select-methods))
11857        (setq method gnus-post-method))
11858   (funcall (gnus-get-function method 'request-post) 
11859            (nth 1 method)))
11860
11861 (defun gnus-request-expire-articles (articles group &optional force)
11862   (let ((method (gnus-find-method-for-group group)))
11863     (funcall (gnus-get-function method 'request-expire-articles) 
11864              articles (gnus-group-real-name group) (nth 1 method)
11865              force)))
11866
11867 (defun gnus-request-move-article 
11868   (article group server accept-function &optional last)
11869   (let ((method (gnus-find-method-for-group group)))
11870     (funcall (gnus-get-function method 'request-move-article) 
11871              article (gnus-group-real-name group) 
11872              (nth 1 method) accept-function last)))
11873
11874 (defun gnus-request-accept-article (group &optional last)
11875   (let ((func (if (symbolp group) group
11876                 (car (gnus-find-method-for-group group)))))
11877     (funcall (intern (format "%s-request-accept-article" func))
11878              (if (stringp group) (gnus-group-real-name group) group)
11879              last)))
11880
11881 (defun gnus-request-replace-article (article group buffer)
11882   (let ((func (car (gnus-find-method-for-group group))))
11883     (funcall (intern (format "%s-request-replace-article" func))
11884              article (gnus-group-real-name group) buffer)))
11885
11886 (defun gnus-request-create-group (group)
11887   (let ((method (gnus-find-method-for-group group)))
11888     (funcall (gnus-get-function method 'request-create-group) 
11889              (gnus-group-real-name group) (nth 1 method))))
11890
11891 (defun gnus-member-of-valid (symbol group)
11892   (memq symbol (assoc
11893                 (format "%s" (car (gnus-find-method-for-group group)))
11894                 gnus-valid-select-methods)))
11895
11896 (defun gnus-secondary-method-p (method)
11897   (let ((methods gnus-secondary-select-methods)
11898         (gmethod (gnus-server-get-method nil method)))
11899     (while (and methods
11900                 (not (equal (gnus-server-get-method nil (car methods)) 
11901                             gmethod)))
11902       (setq methods (cdr methods)))
11903     methods))
11904
11905 (defun gnus-find-method-for-group (group &optional info)
11906   (or gnus-override-method
11907       (and (not group)
11908            gnus-select-method)
11909       (let ((info (or info (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
11910             method)
11911         (if (or (not info)
11912                 (not (setq method (nth 4 info))))
11913             (setq method gnus-select-method)
11914           (setq method
11915                 (cond ((stringp method)
11916                        (gnus-server-to-method method))
11917                       ((stringp (car method))
11918                        (gnus-server-extend-method group method))
11919                       (t
11920                        method))))
11921         (gnus-server-add-address method))))
11922
11923 (defun gnus-check-backend-function (func group)
11924   (let ((method (if (stringp group) (car (gnus-find-method-for-group group))
11925                  group)))
11926     (fboundp (intern (format "%s-%s" method func)))))
11927
11928 (defun gnus-methods-using (method)
11929   (let ((valids gnus-valid-select-methods)
11930         outs)
11931     (while valids
11932       (if (memq method (car valids)) 
11933           (setq outs (cons (car valids) outs)))
11934       (setq valids (cdr valids)))
11935     outs))
11936
11937 ;;; 
11938 ;;; Active & Newsrc File Handling
11939 ;;;
11940
11941 ;; Newsrc related functions.
11942 ;; Gnus internal format of gnus-newsrc-alist:
11943 ;; (("alt.general" 3 (1 . 1))
11944 ;;  ("alt.misc"    3 ((1 . 10) (12 . 15)))
11945 ;;  ("alt.test"    7 (1 . 99) (45 57 93)) ...)
11946 ;; The first item is the group name; the second is the subscription
11947 ;; level; the third is either a range of a list of ranges of read
11948 ;; articles, the optional fourth element is a list of marked articles,
11949 ;; the optional fifth element is the select method.
11950 ;;
11951 ;; Gnus internal format of gnus-newsrc-hashtb:
11952 ;; (95 ("alt.general" 3 (1 . 1)) ("alt.misc" 3 ((1 . 10) (12 . 15))) ...)
11953 ;; This is the entry for "alt.misc". The first element is the number
11954 ;; of unread articles in "alt.misc". The cdr of this entry is the
11955 ;; element *before* "alt.misc" in gnus-newsrc-alist, which makes is
11956 ;; trivial to remove or add new elements into gnus-newsrc-alist
11957 ;; without scanning the entire list. So, to get the actual information
11958 ;; of "alt.misc", you'd say something like 
11959 ;; (nth 2 (gnus-gethash "alt.misc" gnus-newsrc-hashtb))
11960 ;;
11961 ;; Gnus internal format of gnus-active-hashtb:
11962 ;; ((1 . 1))
11963 ;;  (5 . 10))
11964 ;;  (67 . 99)) ...)
11965 ;; The only element in each entry in this hash table is a range of
11966 ;; (possibly) available articles. (Articles in this range may have
11967 ;; been expired or canceled.)
11968 ;;
11969 ;; Gnus internal format of gnus-killed-list and gnus-zombie-list:
11970 ;; ("alt.misc" "alt.test" "alt.general" ...)
11971
11972 (defun gnus-setup-news (&optional rawfile level)
11973   "Setup news information.
11974 If RAWFILE is non-nil, the .newsrc file will also be read.
11975 If LEVEL is non-nil, the news will be set up at level LEVEL."
11976   (let ((init (not (and gnus-newsrc-alist gnus-active-hashtb (not rawfile)))))
11977     ;; Clear some variables to re-initialize news information.
11978     (if init (setq gnus-newsrc-alist nil 
11979                    gnus-active-hashtb nil))
11980
11981     ;; Read the newsrc file and create `gnus-newsrc-hashtb'.
11982     (if init (gnus-read-newsrc-file rawfile))
11983
11984     ;; If we don't read the complete active file, we fill in the
11985     ;; hashtb here. 
11986     (if (or (null gnus-read-active-file)
11987             (eq gnus-read-active-file 'some))
11988         (gnus-update-active-hashtb-from-killed))
11989
11990     ;; Read the active file and create `gnus-active-hashtb'.
11991     ;; If `gnus-read-active-file' is nil, then we just create an empty
11992     ;; hash table. The partial filling out of the hash table will be
11993     ;; done in `gnus-get-unread-articles'.
11994     (and gnus-read-active-file 
11995          (not level)
11996          (gnus-read-active-file))
11997
11998     ;; Possibly eval the dribble file.
11999     (and init gnus-use-dribble-file (gnus-dribble-eval-file))
12000
12001     (gnus-update-format-specifications)
12002
12003     ;; Find the number of unread articles in each non-dead group.
12004     (let ((gnus-read-active-file (and (not level) gnus-read-active-file)))
12005       (gnus-get-unread-articles (or level (1+ gnus-level-subscribed))))
12006     ;; Find new newsgroups and treat them.
12007     (if (and init gnus-check-new-newsgroups gnus-read-active-file (not level)
12008              (gnus-server-opened gnus-select-method))
12009         (gnus-find-new-newsgroups))
12010     (if (and init gnus-check-bogus-newsgroups 
12011              gnus-read-active-file (not level)
12012              (gnus-server-opened gnus-select-method))
12013         (gnus-check-bogus-newsgroups))))
12014
12015 (defun gnus-find-new-newsgroups ()
12016   "Search for new newsgroups and add them.
12017 Each new newsgroup will be treated with `gnus-subscribe-newsgroup-method.'
12018 The `-n' option line from .newsrc is respected."
12019   (interactive)
12020   (or (gnus-check-first-time-used)
12021       (if (or (consp gnus-check-new-newsgroups)
12022               (eq gnus-check-new-newsgroups 'ask-server))
12023           (gnus-ask-server-for-new-groups)
12024         (let ((groups 0)
12025               group new-newsgroups)
12026           (gnus-message 5 "Checking for new newsgroups...")
12027           (or gnus-have-read-active-file (gnus-read-active-file))
12028           (setq gnus-newsrc-last-checked-date (current-time-string))
12029           (if (not gnus-killed-hashtb) (gnus-make-hashtable-from-killed))
12030           ;; Go though every newsgroup in `gnus-active-hashtb' and compare
12031           ;; with `gnus-newsrc-hashtb' and `gnus-killed-hashtb'.
12032           (mapatoms
12033            (lambda (sym)
12034              (if (or (null (setq group (symbol-name sym)))
12035                      (null (symbol-value sym))
12036                      (gnus-gethash group gnus-killed-hashtb)
12037                      (gnus-gethash group gnus-newsrc-hashtb))
12038                  ()
12039                (let ((do-sub (gnus-matches-options-n group)))
12040                  (cond 
12041                   ((eq do-sub 'subscribe)
12042                    (setq groups (1+ groups))
12043                    (gnus-sethash group group gnus-killed-hashtb)
12044                    (funcall gnus-subscribe-options-newsgroup-method group))
12045                   ((eq do-sub 'ignore)
12046                    nil)
12047                   (t
12048                    (setq groups (1+ groups))
12049                    (gnus-sethash group group gnus-killed-hashtb)
12050                    (if gnus-subscribe-hierarchical-interactive
12051                        (setq new-newsgroups (cons group new-newsgroups))
12052                      (funcall gnus-subscribe-newsgroup-method group)))))))
12053            gnus-active-hashtb)
12054           (if new-newsgroups 
12055               (gnus-subscribe-hierarchical-interactive new-newsgroups))
12056           ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
12057           (if (> groups 0)
12058               (gnus-message 6 "%d new newsgroup%s arrived." 
12059                             groups (if (> groups 1) "s have" " has"))
12060             (gnus-message 6 "No new newsgroups."))))))
12061
12062 (defun gnus-matches-options-n (group)
12063   ;; Returns `subscribe' if the group is to be uncoditionally
12064   ;; subscribed, `ignore' if it is to be ignored, and nil if there is
12065   ;; no match for the group.
12066
12067   ;; First we check the two user variables.
12068   (cond
12069    ((and gnus-options-subscribe
12070          (string-match gnus-options-subscribe group))
12071     'subscribe)
12072    ((and gnus-options-not-subscribe
12073          (string-match gnus-options-not-subscribe group))
12074     'ignore)
12075    ;; Then we go through the list that was retrieved from the .newsrc
12076    ;; file.  This list has elements on the form 
12077    ;; `(REGEXP . {ignore,subscribe})'. The first match found (the list
12078    ;; is in the reverse order of the options line) is returned.
12079    (t
12080     (let ((regs gnus-newsrc-options-n))
12081       (while (and regs
12082                   (not (string-match (car (car regs)) group)))
12083         (setq regs (cdr regs)))
12084       (and regs (cdr (car regs)))))))
12085
12086 (defun gnus-ask-server-for-new-groups ()
12087   (let* ((date (or gnus-newsrc-last-checked-date (current-time-string)))
12088          (methods (cons gnus-select-method 
12089                         (append
12090                          (and (consp gnus-check-new-newsgroups)
12091                               gnus-check-new-newsgroups)
12092                          gnus-secondary-select-methods)))
12093          (groups 0)
12094          (new-date (current-time-string))
12095          hashtb group new-newsgroups got-new method)
12096     ;; Go thorugh both primary and secondary select methods and
12097     ;; request new newsgroups.  
12098     (while methods
12099       (setq method (gnus-server-get-method nil (car methods)))
12100       (and (gnus-check-server method)
12101            (gnus-request-newgroups date method)
12102            (save-excursion
12103              (setq got-new t)
12104              (set-buffer nntp-server-buffer)
12105              (or hashtb (setq hashtb (gnus-make-hashtable 
12106                                       (count-lines (point-min) (point-max)))))
12107              ;; Enter all the new groups in a hashtable.
12108              (gnus-active-to-gnus-format method hashtb 'ignore)))
12109       (setq methods (cdr methods)))
12110     (and got-new (setq gnus-newsrc-last-checked-date new-date))
12111     ;; Now all new groups from all select methods are in `hashtb'.
12112     (mapatoms
12113      (lambda (group-sym)
12114        (setq group (symbol-name group-sym))
12115        (if (or (gnus-gethash group gnus-newsrc-hashtb)
12116                (member group gnus-zombie-list)
12117                (member group gnus-killed-list))
12118            ;; The group is already known.
12119            ()
12120          (and (symbol-value group-sym)
12121               (gnus-sethash group (symbol-value group-sym) gnus-active-hashtb))
12122          (let ((do-sub (gnus-matches-options-n group)))
12123            (cond ((eq do-sub 'subscribe)
12124                   (setq groups (1+ groups))
12125                   (gnus-sethash group group gnus-killed-hashtb)
12126                   (funcall 
12127                    gnus-subscribe-options-newsgroup-method group))
12128                  ((eq do-sub 'ignore)
12129                   nil)
12130                  (t
12131                   (setq groups (1+ groups))
12132                   (gnus-sethash group group gnus-killed-hashtb)
12133                   (if gnus-subscribe-hierarchical-interactive
12134                       (setq new-newsgroups (cons group new-newsgroups))
12135                     (funcall gnus-subscribe-newsgroup-method group)))))))
12136      hashtb)
12137     (if new-newsgroups 
12138         (gnus-subscribe-hierarchical-interactive new-newsgroups))
12139     ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
12140     (if (> groups 0)
12141         (gnus-message 6 "%d new newsgroup%s arrived." 
12142                       groups (if (> groups 1) "s have" " has")))
12143     got-new))
12144
12145 (defun gnus-check-first-time-used ()
12146   (if (or (> (length gnus-newsrc-alist) 1)
12147           (file-exists-p gnus-startup-file)
12148           (file-exists-p (concat gnus-startup-file ".el"))
12149           (file-exists-p (concat gnus-startup-file ".eld")))
12150       nil
12151     (gnus-message 6 "First time user; subscribing you to default groups")
12152     (or gnus-have-read-active-file (gnus-read-active-file))
12153     (setq gnus-newsrc-last-checked-date (current-time-string))
12154     (let ((groups gnus-default-subscribed-newsgroups)
12155           group)
12156       (if (eq groups t)
12157           nil
12158         (setq groups (or groups gnus-backup-default-subscribed-newsgroups))
12159         (mapatoms
12160          (lambda (sym)
12161            (if (null (setq group (symbol-name sym)))
12162                ()
12163              (let ((do-sub (gnus-matches-options-n group)))
12164                (cond 
12165                 ((eq do-sub 'subscribe)
12166                  (gnus-sethash group group gnus-killed-hashtb)
12167                  (funcall gnus-subscribe-options-newsgroup-method group))
12168                 ((eq do-sub 'ignore)
12169                  nil)
12170                 (t
12171                  (setq gnus-killed-list (cons group gnus-killed-list)))))))
12172          gnus-active-hashtb)
12173         (while groups
12174           (if (gnus-gethash (car groups) gnus-active-hashtb)
12175               (gnus-group-change-level 
12176                (car groups) gnus-level-default-subscribed gnus-level-killed))
12177           (setq groups (cdr groups)))
12178         (gnus-group-make-help-group)
12179         (and gnus-novice-user
12180              (gnus-message 7 "`A k' to list killed groups"))))))
12181
12182 (defun gnus-subscribe-group (group previous &optional method)
12183   (gnus-group-change-level 
12184    (if method
12185        (list t group gnus-level-default-subscribed nil nil method)
12186      group) 
12187    gnus-level-default-subscribed gnus-level-killed previous t))
12188
12189 ;; `gnus-group-change-level' is the fundamental function for changing
12190 ;; subscription levels of newsgroups. This might mean just changing
12191 ;; from level 1 to 2, which is pretty trivial, from 2 to 6 or back
12192 ;; again, which subscribes/unsubscribes a group, which is equally
12193 ;; trivial. Changing from 1-7 to 8-9 means that you kill a group, and
12194 ;; from 8-9 to 1-7 means that you remove the group from the list of
12195 ;; killed (or zombie) groups and add them to the (kinda) subscribed
12196 ;; groups. And last but not least, moving from 8 to 9 and 9 to 8,
12197 ;; which is trivial.
12198 ;; ENTRY can either be a string (newsgroup name) or a list (if
12199 ;; FROMKILLED is t, it's a list on the format (NUM INFO-LIST),
12200 ;; otherwise it's a list in the format of the `gnus-newsrc-hashtb'
12201 ;; entries. 
12202 ;; LEVEL is the new level of the group, OLDLEVEL is the old level and
12203 ;; PREVIOUS is the group (in hashtb entry format) to insert this group
12204 ;; after. 
12205 (defun gnus-group-change-level (entry level &optional oldlevel
12206                                       previous fromkilled)
12207   (let (group info active num)
12208     ;; Glean what info we can from the arguments
12209     (if (consp entry)
12210         (if fromkilled (setq group (nth 1 entry))
12211           (setq group (car (nth 2 entry))))
12212       (setq group entry))
12213     (if (and (stringp entry)
12214              oldlevel 
12215              (< oldlevel gnus-level-zombie))
12216         (setq entry (gnus-gethash entry gnus-newsrc-hashtb)))
12217     (if (and (not oldlevel)
12218              (consp entry))
12219         (setq oldlevel (car (cdr (nth 2 entry)))))
12220     (if (stringp previous)
12221         (setq previous (gnus-gethash previous gnus-newsrc-hashtb)))
12222
12223     (if (and (>= oldlevel gnus-level-zombie)
12224              (gnus-gethash group gnus-newsrc-hashtb))
12225         ;; We are trying to subscribe a group that is already
12226         ;; subscribed. 
12227         () ; Do nothing. 
12228
12229       (gnus-dribble-enter
12230        (format "(gnus-group-change-level %S %S %S %S %S)" 
12231                group level oldlevel (car (nth 2 previous)) fromkilled))
12232     
12233       ;; Then we remove the newgroup from any old structures, if needed.
12234       ;; If the group was killed, we remove it from the killed or zombie
12235       ;; list. If not, and it is in fact going to be killed, we remove
12236       ;; it from the newsrc hash table and assoc.
12237       (cond ((>= oldlevel gnus-level-zombie)
12238              (if (= oldlevel gnus-level-zombie)
12239                  (setq gnus-zombie-list (delete group gnus-zombie-list))
12240                (setq gnus-killed-list (delete group gnus-killed-list))))
12241             (t
12242              (if (and (>= level gnus-level-zombie)
12243                       entry)
12244                  (progn
12245                    (gnus-sethash (car (nth 2 entry)) nil gnus-newsrc-hashtb)
12246                    (if (nth 3 entry)
12247                        (setcdr (gnus-gethash (car (nth 3 entry))
12248                                              gnus-newsrc-hashtb)
12249                                (cdr entry)))
12250                    (setcdr (cdr entry) (cdr (cdr (cdr entry))))))))
12251
12252       ;; Finally we enter (if needed) the list where it is supposed to
12253       ;; go, and change the subscription level. If it is to be killed,
12254       ;; we enter it into the killed or zombie list.
12255       (cond ((>= level gnus-level-zombie)
12256              ;; Remove from the hash table.
12257              (gnus-sethash group nil gnus-newsrc-hashtb)
12258              (or (gnus-group-foreign-p group)
12259                  ;; We do not enter foreign groups into the list of dead
12260                  ;; groups.  
12261                  (if (= level gnus-level-zombie)
12262                      (setq gnus-zombie-list (cons group gnus-zombie-list))
12263                    (setq gnus-killed-list (cons group gnus-killed-list)))))
12264             (t
12265              ;; If the list is to be entered into the newsrc assoc, and
12266              ;; it was killed, we have to create an entry in the newsrc
12267              ;; hashtb format and fix the pointers in the newsrc assoc.
12268              (if (>= oldlevel gnus-level-zombie)
12269                  (progn
12270                    (if (listp entry)
12271                        (progn
12272                          (setq info (cdr entry))
12273                          (setq num (car entry)))
12274                      (setq active (gnus-gethash group gnus-active-hashtb))
12275                      (setq num (if active (- (1+ (cdr active)) (car active)) t))
12276                      ;; Check whether the group is foreign. If so, the
12277                      ;; foreign select method has to be entered into the
12278                      ;; info. 
12279                      (let ((method (gnus-group-method-name group)))
12280                        (if (eq method gnus-select-method)
12281                            (setq info (list group level nil))
12282                          (setq info (list group level nil nil method)))))
12283                    (or previous 
12284                        (setq previous 
12285                              (let ((p gnus-newsrc-alist))
12286                                (while (cdr (cdr p))
12287                                  (setq p (cdr p)))
12288                                p)))
12289                    (setq entry (cons info (cdr (cdr previous))))
12290                    (if (cdr previous)
12291                        (progn
12292                          (setcdr (cdr previous) entry)
12293                          (gnus-sethash group (cons num (cdr previous)) 
12294                                        gnus-newsrc-hashtb))
12295                      (setcdr previous entry)
12296                      (gnus-sethash group (cons num previous)
12297                                    gnus-newsrc-hashtb))
12298                    (if (cdr entry)
12299                        (setcdr (gnus-gethash (car (car (cdr entry)))
12300                                              gnus-newsrc-hashtb)
12301                                entry)))
12302                ;; It was alive, and it is going to stay alive, so we
12303                ;; just change the level and don't change any pointers or
12304                ;; hash table entries.
12305                (setcar (cdr (car (cdr (cdr entry)))) level)))))))
12306
12307 (defun gnus-kill-newsgroup (newsgroup)
12308   "Obsolete function. Kills a newsgroup."
12309   (gnus-group-change-level
12310    (gnus-gethash newsgroup gnus-newsrc-hashtb) gnus-level-killed))
12311
12312 (defun gnus-check-bogus-newsgroups (&optional confirm)
12313   "Remove bogus newsgroups.
12314 If CONFIRM is non-nil, the user has to confirm the deletion of every
12315 newsgroup." 
12316   (let ((newsrc (cdr gnus-newsrc-alist))
12317         bogus group entry)
12318     (gnus-message 5 "Checking bogus newsgroups...")
12319     (or gnus-have-read-active-file (gnus-read-active-file))
12320     ;; Find all bogus newsgroup that are subscribed.
12321     (while newsrc
12322       (setq group (car (car newsrc)))
12323       (if (or (gnus-gethash group gnus-active-hashtb) ; Active
12324               (nth 4 (car newsrc))      ; Foreign
12325               (and confirm
12326                    (not (gnus-y-or-n-p
12327                          (format "Remove bogus newsgroup: %s " group)))))
12328           ;; Don't remove.
12329           ()
12330         ;; Found a bogus newsgroup.
12331         (setq bogus (cons group bogus)))
12332       (setq newsrc (cdr newsrc)))
12333     ;; Remove all bogus subscribed groups by first killing them, and
12334     ;; then removing them from the list of killed groups.
12335     (while bogus
12336       (and (setq entry (gnus-gethash (car bogus) gnus-newsrc-hashtb))
12337            (progn
12338              (gnus-group-change-level entry gnus-level-killed)
12339              (setq gnus-killed-list (delete (car bogus) gnus-killed-list))))
12340       (setq bogus (cdr bogus)))
12341     ;; Then we remove all bogus groups from the list of killed and
12342     ;; zombie groups. They are are removed without confirmation.
12343     (let ((dead-lists '(gnus-killed-list gnus-zombie-list))
12344           killed)
12345       (while dead-lists
12346         (setq killed (symbol-value (car dead-lists)))
12347         (while killed
12348           (setq group (car killed))
12349           (or (gnus-gethash group gnus-active-hashtb)
12350               ;; The group is bogus.
12351               (set (car dead-lists)
12352                    (delete group (symbol-value (car dead-lists)))))
12353           (setq killed (cdr killed)))
12354         (setq dead-lists (cdr dead-lists))))
12355     (gnus-message 5 "Checking bogus newsgroups...done")))
12356
12357 (defun gnus-check-duplicate-killed-groups ()
12358   "Remove duplicates from the list of killed groups."
12359   (interactive)
12360   (let ((killed gnus-killed-list))
12361     (while killed
12362       (gnus-message 9 "%d" (length killed))
12363       (setcdr killed (delete (car killed) (cdr killed)))
12364       (setq killed (cdr killed)))))
12365
12366 ;; Go though `gnus-newsrc-alist' and compare with `gnus-active-hashtb'
12367 ;; and compute how many unread articles there are in each group.
12368 (defun gnus-get-unread-articles (&optional level) 
12369   (let* ((newsrc (cdr gnus-newsrc-alist))
12370          (level (or level (1+ gnus-level-subscribed)))
12371          (foreign-level
12372           (min 
12373            (cond ((and gnus-activate-foreign-newsgroups 
12374                        (not (numberp gnus-activate-foreign-newsgroups)))
12375                   (1+ gnus-level-subscribed))
12376                  ((numberp gnus-activate-foreign-newsgroups)
12377                   gnus-activate-foreign-newsgroups)
12378                  (t 0))
12379            level))
12380          info group active virtuals method)
12381     (gnus-message 5 "Checking new news...")
12382
12383     (while newsrc
12384       (setq info (car newsrc)
12385             group (car info)
12386             active (gnus-gethash group gnus-active-hashtb))
12387
12388       ;; Check newsgroups. If the user doesn't want to check them, or
12389       ;; they can't be checked (for instance, if the news server can't
12390       ;; be reached) we just set the number of unread articles in this
12391       ;; newsgroup to t. This means that Gnus thinks that there are
12392       ;; unread articles, but it has no idea how many.
12393       (if (and (setq method (nth 4 info))
12394                (not (gnus-server-equal gnus-select-method
12395                                        (gnus-server-get-method nil method)))
12396                (not (gnus-secondary-method-p method)))
12397           ;; These groups are foreign. Check the level.
12398           (if (<= (nth 1 info) foreign-level)
12399               (if (eq (car (if (stringp method) 
12400                                (gnus-server-to-method method)
12401                              (nth 4 info))) 'nnvirtual)
12402                   ;; We have to activate the virtual groups after all
12403                   ;; the others, so we just pop them on a list for
12404                   ;; now. 
12405                   (setq virtuals (cons info virtuals))
12406                 (and (setq active (gnus-activate-group (car info)))
12407                      ;; Close the groups as we look at them!
12408                      (gnus-close-group group))))
12409
12410         (or gnus-read-active-file (gnus-check-server method))
12411         ;; These groups are native or secondary. 
12412         (if (and (not gnus-read-active-file)
12413                  (<= (nth 1 info) level))
12414             (setq active (gnus-activate-group (car info)))))
12415       
12416       (if active
12417           (gnus-get-unread-articles-in-group info active)
12418         ;; The group couldn't be reached, so we nix out the number of
12419         ;; unread articles and stuff.
12420         (gnus-sethash group nil gnus-active-hashtb)
12421         (setcar (gnus-gethash group gnus-newsrc-hashtb) t))
12422
12423       (setq newsrc (cdr newsrc)))
12424
12425     ;; Activate the virtual groups. This has to be done after all the
12426     ;; other groups. 
12427     ;; !!! If one virtual group contains another virtual group, even
12428     ;; doing it this way might cause problems.
12429     (while virtuals
12430       (and (setq active (gnus-activate-group (car (car virtuals))))
12431            (gnus-get-unread-articles-in-group (car virtuals) active))
12432       (setq virtuals (cdr virtuals)))
12433
12434     (gnus-message 5 "Checking new news...done")))
12435
12436 ;; Create a hash table out of the newsrc alist. The `car's of the
12437 ;; alist elements are used as keys.
12438 (defun gnus-make-hashtable-from-newsrc-alist ()
12439   (let ((alist gnus-newsrc-alist)
12440         (ohashtb gnus-newsrc-hashtb)
12441         prev)
12442     (setq gnus-newsrc-hashtb (gnus-make-hashtable (length alist)))
12443     (setq alist 
12444           (setq prev (setq gnus-newsrc-alist 
12445                            (if (equal (car (car gnus-newsrc-alist))
12446                                       "dummy.group")
12447                                gnus-newsrc-alist
12448                              (cons (list "dummy.group" 0 nil) alist)))))
12449     (while alist
12450       (gnus-sethash (car (car alist)) 
12451                     (cons (and ohashtb (car (gnus-gethash 
12452                                              (car (car alist)) ohashtb))) 
12453                           prev) gnus-newsrc-hashtb)
12454       (setq prev alist
12455             alist (cdr alist)))))
12456
12457 (defun gnus-make-hashtable-from-killed ()
12458   "Create a hash table from the killed and zombie lists."
12459   (let ((lists '(gnus-killed-list gnus-zombie-list))
12460         list)
12461     (setq gnus-killed-hashtb 
12462           (gnus-make-hashtable 
12463            (+ (length gnus-killed-list) (length gnus-zombie-list))))
12464     (while lists
12465       (setq list (symbol-value (car lists)))
12466       (setq lists (cdr lists))
12467       (while list
12468         (gnus-sethash (car list) (car list) gnus-killed-hashtb)
12469         (setq list (cdr list))))))
12470
12471 (defun gnus-get-unread-articles-in-group (info active)
12472   (let* ((range (nth 2 info))
12473          (num 0)
12474          (marked (nth 3 info)))
12475     ;; If a cache is present, we may have to alter the active info.
12476     (and gnus-use-cache
12477          (gnus-cache-possibly-alter-active (car info) active))
12478     ;; Modify the list of read articles according to what articles 
12479     ;; are available; then tally the unread articles and add the
12480     ;; number to the group hash table entry.
12481     (cond 
12482      ((zerop (cdr active))
12483       (setq num 0))
12484      ((not range)
12485       (setq num (- (1+ (cdr active)) (car active))))
12486      ((not (listp (cdr range)))
12487       ;; Fix a single (num . num) range according to the
12488       ;; active hash table.
12489       ;; Fix by Carsten Bormann <cabo@Informatik.Uni-Bremen.DE>.
12490       (and (< (cdr range) (car active)) (setcdr range (1- (car active))))
12491       (and (> (cdr range) (cdr active)) (setcdr range (cdr active)))
12492       ;; Compute number of unread articles.
12493       (setq num (max 0 (- (cdr active) (- (1+ (cdr range)) (car range))))))
12494      (t
12495       ;; The read list is a list of ranges. Fix them according to
12496       ;; the active hash table.
12497       ;; First peel off any elements that are below the lower
12498       ;; active limit. 
12499       (while (and (cdr range) 
12500                   (>= (car active) 
12501                       (or (and (atom (car (cdr range))) (car (cdr range)))
12502                           (car (car (cdr range))))))
12503         (if (numberp (car range))
12504             (setcar range 
12505                     (cons (car range) 
12506                           (or (and (numberp (car (cdr range)))
12507                                    (car (cdr range))) 
12508                               (cdr (car (cdr range))))))
12509           (setcdr (car range) 
12510                   (or (and (numberp (nth 1 range)) (nth 1 range))
12511                       (cdr (car (cdr range))))))
12512         (setcdr range (cdr (cdr range))))
12513       ;; Adjust the first element to be the same as the lower limit. 
12514       (if (and (not (atom (car range))) 
12515                (< (cdr (car range)) (car active)))
12516           (setcdr (car range) (1- (car active))))
12517       ;; Then we want to peel off any elements that are higher
12518       ;; than the upper active limit.  
12519       (let ((srange range))
12520         ;; Go past all legal elements.
12521         (while (and (cdr srange) 
12522                     (<= (or (and (atom (car (cdr srange)))
12523                                  (car (cdr srange)))
12524                             (car (car (cdr srange)))) (cdr active)))
12525           (setq srange (cdr srange)))
12526         (if (cdr srange)
12527             ;; Nuke all remaining illegal elements.
12528             (setcdr srange nil))
12529
12530         ;; Adjust the final element.
12531         (if (and (not (atom (car srange)))
12532                  (> (cdr (car srange)) (cdr active)))
12533             (setcdr (car srange) (cdr active))))
12534       ;; Compute the number of unread articles.
12535       (while range
12536         (setq num (+ num (- (1+ (or (and (atom (car range)) (car range))
12537                                     (cdr (car range))))
12538                             (or (and (atom (car range)) (car range))
12539                                 (car (car range))))))
12540         (setq range (cdr range)))
12541       (setq num (max 0 (- (cdr active) num)))))
12542     (and info
12543          (progn
12544            (and (assq 'tick marked)
12545                 (inline (gnus-remove-illegal-marked-articles
12546                          (assq 'tick marked) (nth 2 info))))
12547            (and (assq 'dormant marked)
12548                 (inline (gnus-remove-illegal-marked-articles
12549                          (assq 'dormant marked) (nth 2 info))))
12550            (setcar
12551             (gnus-gethash (car info) gnus-newsrc-hashtb) 
12552             (setq num (max 0 (- num (length (cdr (assq 'tick marked)))
12553                                 (length (cdr (assq 'dormant marked)))))))))
12554     num))
12555
12556 (defun gnus-remove-illegal-marked-articles (marked ranges)
12557   (let ((m (cdr marked)))
12558     ;; Make sure that all ticked articles are a subset of the unread
12559     ;; articles. 
12560     (while m
12561       (if (gnus-member-of-range (car m) ranges)
12562           (setcdr marked (cdr m))
12563         (setq marked m))
12564       (setq m (cdr m)))))
12565
12566 (defun gnus-activate-group (group)
12567   ;; Check whether a group has been activated or not.
12568   (let ((method (gnus-find-method-for-group group))
12569         active)
12570     (and (gnus-check-server method)
12571          ;; We escape all bugs and quits here to make it possible to
12572          ;; continue if a group is so out-there that it reports bugs
12573          ;; and stuff.
12574          (condition-case ()
12575              (gnus-request-group group)
12576            (error nil)
12577            (quit nil))
12578          (save-excursion
12579            (set-buffer nntp-server-buffer)
12580            (goto-char (point-min))
12581            ;; Parse the result we got from `gnus-request-group'.
12582            (and (looking-at "[0-9]+ [0-9]+ \\([0-9]+\\) [0-9]+")
12583                 (progn
12584                   (goto-char (match-beginning 1))
12585                   (gnus-sethash 
12586                    group (setq active (cons (read (current-buffer))
12587                                             (read (current-buffer))))
12588                    gnus-active-hashtb))
12589                 ;; Return the new active info.
12590                 active)))))
12591
12592 (defun gnus-update-read-articles 
12593   (group unread unselected ticked &optional domarks replied expirable killed
12594          dormant bookmark score)
12595   "Update the list of read and ticked articles in GROUP using the
12596 UNREAD and TICKED lists.
12597 Note: UNSELECTED has to be sorted over `<'.
12598 Returns whether the updating was successful."
12599   (let* ((active (or gnus-newsgroup-active 
12600                      (gnus-gethash group gnus-active-hashtb)))
12601          (entry (gnus-gethash group gnus-newsrc-hashtb))
12602          (info (nth 2 entry))
12603          (marked (nth 3 info))
12604          (prev 1)
12605          (unread (sort (copy-sequence unread) (function <)))
12606          read)
12607     (if (or (not info) (not active))
12608         ;; There is no info on this group if it was, in fact,
12609         ;; killed. Gnus stores no information on killed groups, so
12610         ;; there's nothing to be done. 
12611         ;; One could store the information somewhere temporarily,
12612         ;; perhaps... Hmmm... 
12613         ()
12614       ;; Remove any negative articles numbers.
12615       (while (and unread (< (car unread) 0))
12616         (setq unread (cdr unread)))
12617       ;; Remove any expired article numbers
12618       (while (and unread (< (car unread) (car active)))
12619         (setq unread (cdr unread)))
12620       (while (and ticked (< (car ticked) (car active)))
12621         (setq ticked (cdr ticked)))
12622       (while (and dormant (< (car dormant) (car active)))
12623         (setq dormant (cdr dormant)))
12624       (setq unread (sort (append unselected unread) '<))
12625       ;; Compute the ranges of read articles by looking at the list of
12626       ;; unread articles.  
12627       (while unread
12628         (if (/= (car unread) prev)
12629             (setq read (cons (if (= prev (1- (car unread))) prev
12630                                (cons prev (1- (car unread)))) read)))
12631         (setq prev (1+ (car unread)))
12632         (setq unread (cdr unread)))
12633       (if (<= prev (cdr active))
12634           (setq read (cons (cons prev (cdr active)) read)))
12635       ;; Enter this list into the group info.
12636       (setcar (cdr (cdr info)) 
12637               (if (> (length read) 1) (nreverse read) read))
12638       ;; Enter the list of ticked articles.
12639       (gnus-set-marked-articles 
12640        info ticked
12641        (if domarks replied (cdr (assq 'reply marked)))
12642        (if domarks expirable (cdr (assq 'expire marked)))
12643        (if domarks killed (cdr (assq 'killed marked)))
12644        (if domarks dormant (cdr (assq 'dormant marked)))
12645        (if domarks bookmark (cdr (assq 'bookmark marked)))
12646        (if domarks score (cdr (assq 'score marked))))
12647       ;; Set the number of unread articles in gnus-newsrc-hashtb.
12648       (gnus-get-unread-articles-in-group 
12649        info (gnus-gethash group gnus-active-hashtb))
12650       t)))
12651
12652 (defun gnus-make-articles-unread (group articles)
12653   "Mark ARTICLES in GROUP as unread."
12654   (let* ((info (nth 2 (or (gnus-gethash group gnus-newsrc-hashtb)
12655                           (gnus-gethash (gnus-group-real-name group)
12656                                         gnus-newsrc-hashtb))))
12657          (ranges (nth 2 info))
12658          news)
12659     (while articles
12660       (and (gnus-member-of-range (car articles) ranges)
12661            (setq news (cons (car articles) news)))
12662       (setq articles (cdr articles)))
12663     (if (not news)
12664         ()
12665       (setcar (nthcdr 2 info)
12666               (gnus-remove-from-range (nth 2 info) (nreverse news)))
12667       (gnus-group-update-group group t))))
12668
12669 ;; Enter all dead groups into the hashtb.
12670 (defun gnus-update-active-hashtb-from-killed ()
12671   (let ((hashtb (setq gnus-active-hashtb (make-vector 4095 0)))
12672         (lists (list gnus-killed-list gnus-zombie-list))
12673         killed)
12674     (while lists
12675       (setq killed (car lists))
12676       (while killed
12677         (gnus-sethash (car killed) nil hashtb)
12678         (setq killed (cdr killed)))
12679       (setq lists (cdr lists)))))
12680
12681 ;; Get the active file(s) from the backend(s).
12682 (defun gnus-read-active-file ()
12683   (gnus-group-set-mode-line)
12684   (let ((methods (if (gnus-check-server gnus-select-method)
12685                      ;; The native server is available.
12686                      (cons gnus-select-method gnus-secondary-select-methods)
12687                    ;; The native server is down, so we just do the
12688                    ;; secondary ones.   
12689                    gnus-secondary-select-methods))
12690         list-type)
12691     (setq gnus-have-read-active-file nil)
12692     (save-excursion
12693       (set-buffer nntp-server-buffer)
12694       (while methods
12695         (let* ((method (gnus-server-get-method nil (car methods)))
12696                (where (nth 1 method))
12697                (mesg (format "Reading active file%s via %s..."
12698                              (if (and where (not (zerop (length where))))
12699                                  (concat " from " where) "")
12700                              (car method))))
12701           (gnus-message 5 mesg)
12702           (gnus-check-server method)
12703           (cond 
12704            ((and (eq gnus-read-active-file 'some)
12705                  (gnus-check-backend-function 'retrieve-groups (car method)))
12706             (let ((newsrc (cdr gnus-newsrc-alist))
12707                   (gmethod (gnus-server-get-method nil method))
12708                   groups)
12709               (while newsrc
12710                 (and (gnus-server-equal 
12711                       (gnus-find-method-for-group 
12712                        (car (car newsrc)) (car newsrc))
12713                       gmethod)
12714                      (setq groups (cons (gnus-group-real-name 
12715                                          (car (car newsrc))) groups)))
12716                 (setq newsrc (cdr newsrc)))
12717               (gnus-check-server method)
12718               (setq list-type (gnus-retrieve-groups groups method))
12719               (cond ((not list-type)
12720                      (gnus-message 
12721                       1 "Cannot read partial active file from %s server." 
12722                       (car method))
12723                      (ding)
12724                      (sit-for 2))
12725                     ((eq list-type 'active)
12726                      (gnus-active-to-gnus-format method gnus-active-hashtb))
12727                     (t
12728                      (gnus-groups-to-gnus-format method gnus-active-hashtb)))))
12729            (t
12730             (if (not (gnus-request-list method))
12731                 (progn
12732                   (gnus-message 1 "Cannot read active file from %s server." 
12733                                 (car method))
12734                   (ding))
12735               (gnus-active-to-gnus-format method)
12736               ;; We mark this active file as read.
12737               (setq gnus-have-read-active-file
12738                     (cons method gnus-have-read-active-file))
12739               (gnus-message 5 "%sdone" mesg)))))
12740         (setq methods (cdr methods))))))
12741
12742 ;; Read an active file and place the results in `gnus-active-hashtb'.
12743 (defun gnus-active-to-gnus-format (method &optional hashtb ignore-errors)
12744   (let ((cur (current-buffer))
12745         (hashtb (or hashtb 
12746                     (if (and gnus-active-hashtb 
12747                              (not (equal method gnus-select-method)))
12748                         gnus-active-hashtb
12749                       (setq gnus-active-hashtb
12750                             (if (equal method gnus-select-method)
12751                                 (gnus-make-hashtable 
12752                                  (count-lines (point-min) (point-max)))
12753                               (gnus-make-hashtable 4096))))))
12754         (flag-hashtb (gnus-make-hashtable 60)))
12755     ;; Delete unnecessary lines.
12756     (goto-char (point-min))
12757     (while (search-forward "\nto." nil t)
12758       (delete-region (1+ (match-beginning 0)) 
12759                      (progn (forward-line 1) (point))))
12760     (or (string= gnus-ignored-newsgroups "")
12761         (progn
12762           (goto-char (point-min))
12763           (delete-matching-lines gnus-ignored-newsgroups)))
12764     ;; Make the group names readable as a lisp expression even if they
12765     ;; contain special characters.
12766     ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
12767     (goto-char (point-max))
12768     (while (re-search-backward "[][';?()#]" nil t)
12769       (insert ?\\))
12770     ;; If these are groups from a foreign select method, we insert the
12771     ;; group prefix in front of the group names. 
12772     (and method (not (gnus-server-equal
12773                       (gnus-server-get-method nil method)
12774                       (gnus-server-get-method nil gnus-select-method)))
12775          (let ((prefix (gnus-group-prefixed-name "" method)))
12776            (goto-char (point-min))
12777            (while (and (not (eobp))
12778                        (progn (insert prefix)
12779                               (zerop (forward-line 1)))))))
12780     ;; Store the active file in a hash table.
12781     (goto-char (point-min))
12782     (if (string-match "%[oO]" gnus-group-line-format)
12783         ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
12784         ;; If we want information on moderated groups, we use this
12785         ;; loop...   
12786         (let* ((mod-hashtb (make-vector 7 0))
12787                (m (intern "m" mod-hashtb))
12788                group max min)
12789           (while (not (eobp))
12790             (condition-case nil
12791                 (progn
12792                   (narrow-to-region (point) (gnus-point-at-eol))
12793                   (setq group (let ((obarray hashtb)) (read cur)))
12794                   (if (and (numberp (setq max (read cur)))
12795                            (numberp (setq min (read cur)))
12796                            (progn 
12797                              (skip-chars-forward " \t")
12798                              (not
12799                               (or (= (following-char) ?=)
12800                                   (= (following-char) ?x)
12801                                   (= (following-char) ?j)))))
12802                       (set group (cons min max))
12803                     (set group nil))
12804                   ;; Enter moderated groups into a list.
12805                   (if (eq (let ((obarray mod-hashtb)) (read cur)) m)
12806                       (setq gnus-moderated-list 
12807                             (cons (symbol-name group) gnus-moderated-list))))
12808               (error 
12809                (set group nil)))
12810             (widen)
12811             (forward-line 1)))
12812       ;; And if we do not care about moderation, we use this loop,
12813       ;; which is faster.
12814       (let (group max min)
12815         (while (not (eobp))
12816           (condition-case ()
12817               (progn
12818                 (narrow-to-region (point) (gnus-point-at-eol))
12819                 ;; group gets set to a symbol interned in the hash table
12820                 ;; (what a hack!!) - jwz
12821                 (setq group (let ((obarray hashtb)) (read cur)))
12822                 (if (and (numberp (setq max (read cur)))
12823                          (numberp (setq min (read cur)))
12824                          (progn 
12825                            (skip-chars-forward " \t")
12826                            (not
12827                             (or (= (following-char) ?=)
12828                                 (= (following-char) ?x)
12829                                 (= (following-char) ?j)))))
12830                     (set group (cons min max))
12831                   (set group nil)))
12832             (error 
12833              (progn 
12834                (set group nil)
12835                (if ignore-errors
12836                    ()
12837                  (gnus-message 3 "Warning - illegal active: %s"
12838                                (buffer-substring 
12839                                 (gnus-point-at-bol) (gnus-point-at-eol)))
12840                  nil))))
12841           (widen)
12842           (forward-line 1))))))
12843
12844 (defun gnus-groups-to-gnus-format (method &optional hashtb)
12845   ;; Parse a "groups" active file.
12846   (let ((cur (current-buffer))
12847         (hashtb (or hashtb 
12848                     (if (and method gnus-active-hashtb)
12849                         gnus-active-hashtb
12850                       (setq gnus-active-hashtb
12851                             (gnus-make-hashtable 
12852                              (count-lines (point-min) (point-max)))))))
12853         (prefix (and method (not (eq method gnus-select-method))
12854                      (gnus-group-prefixed-name "" method))))
12855
12856     (goto-char (point-min))
12857     ;; We split this into to separate loops, one with the prefix
12858     ;; and one without to speed the reading up somewhat.
12859     (if prefix
12860         (let (min max opoint group)
12861           (while (not (eobp))
12862             (condition-case ()
12863                 (progn
12864                   (read cur) (read cur)
12865                   (setq min (read cur)
12866                         max (read cur)
12867                         opoint (point))
12868                   (skip-chars-forward " \t")
12869                   (insert prefix)
12870                   (goto-char opoint)
12871                   (set (let ((obarray hashtb)) (read cur)) 
12872                        (cons min max)))
12873               (error (if group (set group nil))))
12874             (forward-line 1)))
12875       (let (min max group)
12876         (while (not (eobp))
12877           (condition-case ()
12878               (if (= (following-char) ?2)
12879                   (progn
12880                     (read cur) (read cur)
12881                     (setq min (read cur)
12882                           max (read cur))
12883                     (set (setq group (let ((obarray hashtb)) (read cur)))
12884                          (cons min max))))
12885             (error (if group (set group nil))))
12886           (forward-line 1))))))
12887
12888 (defun gnus-read-newsrc-file (&optional force)
12889   "Read startup file.
12890 If FORCE is non-nil, the .newsrc file is read."
12891   ;; Reset variables that might be defined in the .newsrc.eld file.
12892   (let ((variables gnus-variable-list))
12893     (while variables
12894       (set (car variables) nil)
12895       (setq variables (cdr variables))))
12896   (let* ((newsrc-file gnus-current-startup-file)
12897          (quick-file (concat newsrc-file ".el")))
12898     (save-excursion
12899       ;; We always load the .newsrc.eld file. If always contains
12900       ;; much information that can not be gotten from the .newsrc
12901       ;; file (ticked articles, killed groups, foreign methods, etc.)
12902       (gnus-read-newsrc-el-file quick-file)
12903  
12904       (if (or force
12905               (and (file-newer-than-file-p newsrc-file quick-file)
12906                    (file-newer-than-file-p newsrc-file 
12907                                            (concat quick-file "d")))
12908               (not gnus-newsrc-alist))
12909           ;; We read the .newsrc file. Note that if there if a
12910           ;; .newsrc.eld file exists, it has already been read, and
12911           ;; the `gnus-newsrc-hashtb' has been created. While reading
12912           ;; the .newsrc file, Gnus will only use the information it
12913           ;; can find there for changing the data already read -
12914           ;; ie. reading the .newsrc file will not trash the data
12915           ;; already read (except for read articles).
12916           (save-excursion
12917             (gnus-message 5 "Reading %s..." newsrc-file)
12918             (set-buffer (find-file-noselect newsrc-file))
12919             (buffer-disable-undo (current-buffer))
12920             (gnus-newsrc-to-gnus-format)
12921             (kill-buffer (current-buffer))
12922             (gnus-message 5 "Reading %s...done" newsrc-file))))))
12923
12924 (defun gnus-read-newsrc-el-file (file)
12925   (let ((ding-file (concat file "d")))
12926     ;; We always, always read the .eld file.
12927     (gnus-message 5 "Reading %s..." ding-file)
12928     (let (gnus-newsrc-assoc)
12929       (condition-case nil
12930           (load ding-file t t t)
12931         (error nil))
12932       (and gnus-newsrc-assoc (setq gnus-newsrc-alist gnus-newsrc-assoc)))
12933     (let ((inhibit-quit t))
12934       (gnus-uncompress-newsrc-alist))
12935     (gnus-make-hashtable-from-newsrc-alist)
12936     (if (not (file-newer-than-file-p file ding-file))
12937         ()
12938       ;; Old format quick file
12939       (gnus-message 5 "Reading %s..." file)
12940       ;; The .el file is newer than the .eld file, so we read that one
12941       ;; as well. 
12942       (gnus-read-old-newsrc-el-file file))))
12943
12944 ;; Parse the old-style quick startup file
12945 (defun gnus-read-old-newsrc-el-file (file)
12946   (let (newsrc killed marked group m)
12947     (prog1
12948         (let ((gnus-killed-assoc nil)
12949               gnus-marked-assoc gnus-newsrc-alist gnus-newsrc-assoc)
12950           (prog1
12951               (condition-case nil
12952                   (load file t t t)
12953                 (error nil))
12954             (setq newsrc gnus-newsrc-assoc
12955                   killed gnus-killed-assoc
12956                   marked gnus-marked-assoc)))
12957       (setq gnus-newsrc-alist nil)
12958       (while newsrc
12959         (setq group (car newsrc))
12960         (let ((info (nth 2 (gnus-gethash (car group) gnus-newsrc-hashtb))))
12961           (if info
12962               (progn
12963                 (setcar (nthcdr 2 info) (cdr (cdr group)))
12964                 (setcar (cdr info)
12965                         (if (nth 1 group) gnus-level-default-subscribed 
12966                           gnus-level-default-unsubscribed))
12967                 (setq gnus-newsrc-alist (cons info gnus-newsrc-alist)))
12968             (setq gnus-newsrc-alist
12969                   (cons 
12970                    (setq info
12971                          (list (car group)
12972                                (if (nth 1 group) gnus-level-default-subscribed
12973                                  gnus-level-default-unsubscribed) 
12974                                (cdr (cdr group))))
12975                    gnus-newsrc-alist)))
12976           (if (setq m (assoc (car group) marked))
12977             (setcdr (cdr (cdr info)) (cons (list (cons 'tick (cdr m))) nil))))
12978         (setq newsrc (cdr newsrc)))
12979       (setq newsrc killed)
12980       (while newsrc
12981         (setcar newsrc (car (car newsrc)))
12982         (setq newsrc (cdr newsrc)))
12983       (setq gnus-killed-list killed))
12984     ;; The .el file version of this variable does not begin with
12985     ;; "options", while the .eld version does, so we just add it if it
12986     ;; isn't there.
12987     (and
12988      gnus-newsrc-options 
12989      (progn
12990        (and (not (string-match "^ *options" gnus-newsrc-options))
12991             (setq gnus-newsrc-options (concat "options " gnus-newsrc-options)))
12992        (and (not (string-match "\n$" gnus-newsrc-options))
12993             (setq gnus-newsrc-options (concat gnus-newsrc-options "\n")))))
12994     (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist))
12995     (gnus-make-hashtable-from-newsrc-alist)))
12996       
12997 (defun gnus-make-newsrc-file (file)
12998   "Make server dependent file name by catenating FILE and server host name."
12999   (let* ((file (expand-file-name file nil))
13000          (real-file (concat file "-" (nth 1 gnus-select-method))))
13001     (if (or (file-exists-p real-file)
13002             (file-exists-p (concat real-file ".el"))
13003             (file-exists-p (concat real-file ".eld")))
13004         real-file file)))
13005
13006 (defun gnus-uncompress-newsrc-alist ()
13007   ;; Uncompress all lists of marked articles in the newsrc assoc.
13008   (let ((newsrc gnus-newsrc-alist)
13009         marked)
13010     (while newsrc
13011       (if (not (setq marked (nth 3 (car newsrc))))
13012           ()
13013         (while marked
13014           (or (eq 'score (car (car marked)))
13015               (eq 'bookmark (car (car marked)))
13016               (eq 'killed (car (car marked)))
13017               (setcdr (car marked) (gnus-uncompress-range (cdr (car marked)))))
13018           (setq marked (cdr marked))))
13019       (setq newsrc (cdr newsrc)))))
13020
13021 (defun gnus-compress-newsrc-alist ()
13022   ;; Compress all lists of marked articles in the newsrc assoc.
13023   (let ((newsrc gnus-newsrc-alist)
13024         marked)
13025     (while newsrc
13026       (if (not (setq marked (nth 3 (car newsrc))))
13027           ()
13028         (while marked
13029           (or (eq 'score (car (car marked)))
13030               (eq 'bookmark (car (car marked)))
13031               (eq 'killed (car (car marked)))
13032               (setcdr (car marked) 
13033                       (condition-case ()
13034                           (gnus-compress-sequence 
13035                            (sort (cdr (car marked)) '<) t)
13036                         (error (cdr (car marked))))))
13037           (setq marked (cdr marked))))
13038       (setq newsrc (cdr newsrc)))))
13039
13040 (defun gnus-newsrc-to-gnus-format ()
13041   (setq gnus-newsrc-options "")
13042   (setq gnus-newsrc-options-n nil)
13043
13044   (or gnus-active-hashtb
13045       (setq gnus-active-hashtb (make-vector 4095 0)))
13046   (let ((buf (current-buffer))
13047         (already-read (> (length gnus-newsrc-alist) 1))
13048         group subscribed options-symbol newsrc Options-symbol
13049         symbol reads num1)
13050     (goto-char (point-min))
13051     ;; We intern the symbol `options' in the active hashtb so that we
13052     ;; can `eq' against it later.
13053     (setq options-symbol (intern "options" gnus-active-hashtb))
13054     (setq Options-symbol (intern "Options" gnus-active-hashtb))
13055   
13056     (while (not (eobp))
13057       ;; We first read the first word on the line by narrowing and
13058       ;; then reading into `gnus-active-hashtb'.  Most groups will
13059       ;; already exist in that hashtb, so this will save some string
13060       ;; space.
13061       (narrow-to-region
13062        (point)
13063        (progn (skip-chars-forward "^ \t!:\n") (point)))
13064       (goto-char (point-min))
13065       (setq symbol 
13066             (and (/= (point-min) (point-max))
13067                  (let ((obarray gnus-active-hashtb)) (read buf))))
13068       (widen)
13069       ;; Now, the symbol we have read is either `options' or a group
13070       ;; name.  If it is an options line, we just add it to a string. 
13071       (cond 
13072        ((or (eq symbol options-symbol)
13073             (eq symbol Options-symbol))
13074         (setq gnus-newsrc-options
13075               ;; This concatting is quite inefficient, but since our
13076               ;; thorough studies show that approx 99.37% of all
13077               ;; .newsrc files only contain a single options line, we
13078               ;; don't give a damn, frankly, my dear.
13079               (concat gnus-newsrc-options
13080                       (buffer-substring 
13081                        (gnus-point-at-bol)
13082                        ;; Options may continue on the next line.
13083                        (or (and (re-search-forward "^[^ \t]" nil 'move)
13084                                 (progn (beginning-of-line) (point)))
13085                            (point))))))
13086        (symbol
13087         ;; It was a group name.
13088         (setq subscribed (= (following-char) ?:)
13089               group (symbol-name symbol)
13090               reads nil)
13091         (if (eolp)
13092             ;; If the line ends here, this is clearly a buggy line, so
13093             ;; we put point a the beginning of line and let the cond
13094             ;; below do the error handling.
13095             (beginning-of-line)
13096           ;; We skip to the beginning of the ranges.
13097           (skip-chars-forward "!: \t"))
13098         ;; We are now at the beginning of the list of read articles.
13099         ;; We read them range by range.
13100         (while
13101             (cond 
13102              ((looking-at "[0-9]+")
13103               ;; We narrow and read a number instead of buffer-substring/
13104               ;; string-to-int because it's faster. narrow/widen is
13105               ;; faster than save-restriction/narrow, and save-restriction
13106               ;; produces a garbage object.
13107               (setq num1 (progn
13108                            (narrow-to-region (match-beginning 0) (match-end 0))
13109                            (read buf)))
13110               (widen)
13111               ;; If the next character is a dash, then this is a range.
13112               (if (= (following-char) ?-)
13113                   (progn
13114                     ;; We read the upper bound of the range.
13115                     (forward-char 1)
13116                     (if (not (looking-at "[0-9]+"))
13117                         ;; This is a buggy line, by we pretend that
13118                         ;; it's kinda OK. Perhaps the user should be
13119                         ;; dinged? 
13120                         (setq reads (cons num1 reads))
13121                       (setq reads 
13122                             (cons 
13123                              (cons num1 (progn
13124                                           (narrow-to-region (match-beginning 0) 
13125                                                             (match-end 0))
13126                                           (read buf)))
13127                              reads))
13128                       (widen)))
13129                 ;; It was just a simple number, so we add it to the
13130                 ;; list of ranges.
13131                 (setq reads (cons num1 reads)))
13132               ;; If the next char in ?\n, then we have reached the end
13133               ;; of the line and return nil.
13134               (/= (following-char) ?\n))
13135              ((= (following-char) ?\n)
13136               ;; End of line, so we end.
13137               nil)
13138              (t
13139               ;; Not numbers and not eol, so this might be a buggy
13140               ;; line... 
13141               (or (eobp) ; If it was eob instead of ?\n, we allow it.
13142                   (progn
13143                     ;; The line was buggy.
13144                     (setq group nil)
13145                     (gnus-message 3 "Mangled line: %s" 
13146                                   (buffer-substring (gnus-point-at-bol) 
13147                                                     (gnus-point-at-eol)))
13148                     (ding)
13149                     (sit-for 1)))
13150               nil))
13151           ;; Skip past ", ". Spaces are illegal in these ranges, but
13152           ;; we allow them, because it's a common mistake to put a
13153           ;; space after the comma.
13154           (skip-chars-forward ", "))
13155
13156         ;; We have already read .newsrc.eld, so we gently update the
13157         ;; data in the hash table with the information we have just
13158         ;; read. 
13159         (if (not group)
13160             ()
13161           (let ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
13162                 level)
13163             (if info
13164                 ;; There is an entry for this file in the alist.
13165                 (progn
13166                   (setcar (nthcdr 2 info) (nreverse reads))
13167                   ;; We update the level very gently.  In fact, we
13168                   ;; only change it if there's been a status change
13169                   ;; from subscribed to unsubscribed, or vice versa.
13170                   (setq level (nth 1 info))
13171                   (cond ((and (<= level gnus-level-subscribed)
13172                               (not subscribed))
13173                          (setq level (if reads
13174                                          gnus-level-default-unsubscribed 
13175                                        (1+ gnus-level-default-unsubscribed))))
13176                         ((and (> level gnus-level-subscribed) subscribed)
13177                          (setq level gnus-level-default-subscribed)))
13178                   (setcar (cdr info) level))
13179               ;; This is a new group.
13180               (setq info (list group 
13181                                (if subscribed
13182                                    gnus-level-default-subscribed 
13183                                  (if reads
13184                                      (1+ gnus-level-subscribed)
13185                                    gnus-level-default-unsubscribed))
13186                                (nreverse reads))))
13187             (setq newsrc (cons info newsrc))))))
13188       (forward-line 1))
13189     
13190     (setq newsrc (nreverse newsrc))
13191
13192     (if (not already-read)
13193         ()
13194       ;; We now have two newsrc lists - `newsrc', which is what we
13195       ;; have read from .newsrc, and `gnus-newsrc-alist', which is
13196       ;; what we've read from .newsrc.eld. We have to merge these
13197       ;; lists. We do this by "attaching" any (foreign) groups in the
13198       ;; gnus-newsrc-alist to the (native) group that precedes them. 
13199       (let ((rc (cdr gnus-newsrc-alist))
13200             (prev gnus-newsrc-alist)
13201             entry mentry)
13202         (while rc
13203           (or (null (nth 4 (car rc))) ; It's a native group.
13204               (assoc (car (car rc)) newsrc) ; It's already in the alist.
13205               (if (setq entry (assoc (car (car prev)) newsrc))
13206                   (setcdr (setq mentry (memq entry newsrc))
13207                           (cons (car rc) (cdr mentry)))
13208                 (setq newsrc (cons (car rc) newsrc))))
13209           (setq prev rc
13210                 rc (cdr rc)))))
13211
13212     (setq gnus-newsrc-alist newsrc)
13213     ;; We make the newsrc hashtb.
13214     (gnus-make-hashtable-from-newsrc-alist)
13215
13216     ;; Finally, if we read some options lines, we parse them.
13217     (or (string= gnus-newsrc-options "")
13218         (gnus-newsrc-parse-options gnus-newsrc-options))))
13219
13220 ;; Parse options lines to find "options -n !all rec.all" and stuff.
13221 ;; The return value will be a list on the form
13222 ;; ((regexp1 . ignore)
13223 ;;  (regexp2 . subscribe)...)
13224 ;; When handling new newsgroups, groups that match a `ignore' regexp
13225 ;; will be ignored, and groups that match a `subscribe' regexp will be
13226 ;; subscribed. A line like
13227 ;; options -n !all rec.all
13228 ;; will lead to a list that looks like
13229 ;; (("^rec\\..+" . subscribe) 
13230 ;;  ("^.+" . ignore))
13231 ;; So all "rec.*" groups will be subscribed, while all the other
13232 ;; groups will be ignored. Note that "options -n !all rec.all" is very
13233 ;; different from "options -n rec.all !all". 
13234 (defun gnus-newsrc-parse-options (options)
13235   (let (out eol)
13236     (save-excursion
13237       (gnus-set-work-buffer)
13238       (insert (regexp-quote options))
13239       ;; First we treat all continuation lines.
13240       (goto-char (point-min))
13241       (while (re-search-forward "\n[ \t]+" nil t)
13242         (replace-match " " t t))
13243       ;; Then we transform all "all"s into ".+"s.
13244       (goto-char (point-min))
13245       (while (re-search-forward "\\ball\\b" nil t)
13246         (replace-match ".+" t t))
13247       (goto-char (point-min))
13248       ;; We remove all other options than the "-n" ones.
13249       (while (re-search-forward "[ \t]-[^n][^-]*" nil t)
13250         (replace-match " ")
13251         (forward-char -1))
13252       (goto-char (point-min))
13253
13254       ;; We are only interested in "options -n" lines - we
13255       ;; ignore the other option lines.
13256       (while (re-search-forward "[ \t]-n" nil t)
13257         (setq eol 
13258               (or (save-excursion
13259                     (and (re-search-forward "[ \t]-n" (gnus-point-at-eol) t)
13260                          (- (point) 2)))
13261                   (gnus-point-at-eol)))
13262         ;; Search for all "words"...
13263         (while (re-search-forward "[^ \t,\n]+" eol t)
13264           (if (= (char-after (match-beginning 0)) ?!)
13265               ;; If the word begins with a bang (!), this is a "not"
13266               ;; spec. We put this spec (minus the bang) and the
13267               ;; symbol `ignore' into the list.
13268               (setq out (cons (cons (concat 
13269                                      "^" (buffer-substring 
13270                                           (1+ (match-beginning 0))
13271                                           (match-end 0)))
13272                                     'ignore) out))
13273             ;; There was no bang, so this is a "yes" spec.
13274             (setq out (cons (cons (concat 
13275                                    "^" (buffer-substring (match-beginning 0)
13276                                                          (match-end 0)))
13277                                   'subscribe) out)))))
13278     
13279       (setq gnus-newsrc-options-n out))))
13280                
13281
13282 (defun gnus-save-newsrc-file ()
13283   "Save .newsrc file."
13284   ;; Note: We cannot save .newsrc file if all newsgroups are removed
13285   ;; from the variable gnus-newsrc-alist.
13286   (and (or gnus-newsrc-alist gnus-killed-list)
13287        gnus-current-startup-file
13288        (progn
13289          (run-hooks 'gnus-save-newsrc-hook)
13290          (save-excursion
13291            (if (and gnus-use-dribble-file
13292                     (or (not gnus-dribble-buffer)
13293                         (not (buffer-name gnus-dribble-buffer))
13294                         (zerop (save-excursion
13295                                  (set-buffer gnus-dribble-buffer)
13296                                  (buffer-size)))))
13297                (gnus-message 4 "(No changes need to be saved)")
13298              (if gnus-save-newsrc-file
13299                  (progn
13300                    (gnus-message 5 "Saving %s..." gnus-current-startup-file)
13301                    ;; Make backup file of master newsrc.
13302                    (gnus-gnus-to-newsrc-format)
13303                    (gnus-message 5 "Saving %s...done"
13304                                  gnus-current-startup-file)))
13305              ;; Quickly loadable .newsrc.
13306              (set-buffer (get-buffer-create " *Gnus-newsrc*"))
13307              (set-visited-file-name (concat gnus-current-startup-file ".eld"))
13308              (gnus-add-current-to-buffer-list)
13309              (buffer-disable-undo (current-buffer))
13310              (erase-buffer)
13311              (gnus-message 5 "Saving %s.eld..." gnus-current-startup-file)
13312              (gnus-gnus-to-quick-newsrc-format)
13313              (save-buffer)
13314              (kill-buffer (current-buffer))
13315              (gnus-message 5 "Saving %s.eld...done" gnus-current-startup-file)
13316              (gnus-dribble-delete-file))))))
13317
13318 (defun gnus-gnus-to-quick-newsrc-format ()
13319   "Insert Gnus variables such as gnus-newsrc-alist in lisp format."
13320   (insert ";; (ding) Gnus startup file.\n")
13321   (insert ";; Never delete this file - touch .newsrc instead to force Gnus\n")
13322   (insert ";; to read .newsrc.\n")
13323   (insert "(setq gnus-newsrc-file-version "
13324           (prin1-to-string gnus-version) ")\n")
13325   (let ((variables gnus-variable-list)
13326         (inhibit-quit t)
13327         (gnus-newsrc-alist (cdr gnus-newsrc-alist))
13328         variable)
13329     ;; insert lisp expressions.
13330     (gnus-compress-newsrc-alist)
13331     (while variables
13332       (setq variable (car variables))
13333       (and (boundp variable)
13334            (symbol-value variable)
13335            (or gnus-save-killed-list (not (eq variable 'gnus-killed-list)))
13336            (insert "(setq " (symbol-name variable) " '"
13337                    (prin1-to-string (symbol-value variable))
13338                    ")\n"))
13339       (setq variables (cdr variables)))
13340     (gnus-uncompress-newsrc-alist)))
13341
13342
13343 (defun gnus-gnus-to-newsrc-format ()
13344   ;; Generate and save the .newsrc file.
13345   (let ((newsrc (cdr gnus-newsrc-alist))
13346         info ranges range)
13347     (save-excursion
13348       (set-buffer (create-file-buffer gnus-current-startup-file))
13349       (set-visited-file-name gnus-current-startup-file)
13350       (buffer-disable-undo (current-buffer))
13351       (erase-buffer)
13352       ;; Write options.
13353       (if gnus-newsrc-options (insert gnus-newsrc-options))
13354       ;; Write subscribed and unsubscribed.
13355       (while newsrc
13356         (setq info (car newsrc))
13357         (if (not (nth 4 info))          ;Don't write foreign groups to .newsrc.
13358             (progn
13359               (insert (car info) (if (> (nth 1 info) gnus-level-subscribed)
13360                                      "!" ":"))
13361               (if (setq ranges (nth 2 info))
13362                   (progn
13363                     (insert " ")
13364                     (if (not (listp (cdr ranges)))
13365                         (if (= (car ranges) (cdr ranges))
13366                             (insert (int-to-string (car ranges)))
13367                           (insert (int-to-string (car ranges)) "-" 
13368                                   (int-to-string (cdr ranges))))
13369                       (while ranges
13370                         (setq range (car ranges)
13371                               ranges (cdr ranges))
13372                         (if (or (atom range) (= (car range) (cdr range)))
13373                             (insert (int-to-string 
13374                                      (or (and (atom range) range) 
13375                                          (car range))))
13376                           (insert (int-to-string (car range)) "-"
13377                                   (int-to-string (cdr range))))
13378                         (if ranges (insert ","))))))
13379               (insert "\n")))
13380         (setq newsrc (cdr newsrc)))
13381       (save-buffer)
13382       (kill-buffer (current-buffer)))))
13383
13384 (defun gnus-read-all-descriptions-files ()
13385   (let ((methods (cons gnus-select-method gnus-secondary-select-methods)))
13386     (while methods
13387       (gnus-read-descriptions-file (car methods))
13388       (setq methods (cdr methods)))
13389     t))
13390
13391 (defun gnus-read-descriptions-file (&optional method)
13392   (let ((method (or method gnus-select-method)))
13393     ;; We create the hashtable whether we manage to read the desc file
13394     ;; to avoid trying to re-read after a failed read.
13395     (or gnus-description-hashtb
13396         (setq gnus-description-hashtb 
13397               (gnus-make-hashtable (length gnus-active-hashtb))))
13398     ;; Mark this method's desc file as read.
13399     (gnus-sethash (gnus-group-prefixed-name "" method) "Has read"
13400                   gnus-description-hashtb)
13401
13402     (gnus-message 5 "Reading descriptions file via %s..." (car method))
13403     (cond 
13404      ((not (gnus-check-server method))
13405       (gnus-message 1 "Couldn't open server")
13406       nil)
13407      ((not (gnus-request-list-newsgroups method))
13408       (gnus-message 1 "Couldn't read newsgroups descriptions")
13409       nil)
13410      (t
13411       (let (group)
13412         (save-excursion
13413           (save-restriction
13414             (set-buffer nntp-server-buffer)
13415             (goto-char (point-min))
13416             (if (or (search-forward "\n.\n" nil t)
13417                     (goto-char (point-max)))
13418                 (progn
13419                   (beginning-of-line)
13420                   (narrow-to-region (point-min) (point))))
13421             (goto-char (point-min))
13422             (while (not (eobp))
13423               ;; If we get an error, we set group to 0, which is not a
13424               ;; symbol... 
13425               (setq group 
13426                     (condition-case ()
13427                         (let ((obarray gnus-description-hashtb))
13428                           ;; Group is set to a symbol interned in this
13429                           ;; hash table.
13430                           (read nntp-server-buffer))
13431                       (error 0)))
13432               (skip-chars-forward " \t")
13433               ;; ... which leads to this line being effectively ignored.
13434               (and (symbolp group)
13435                    (set group (buffer-substring 
13436                                (point) (progn (end-of-line) (point)))))
13437               (forward-line 1))))
13438         (gnus-message 5 "Reading descriptions file...done")
13439         t)))))
13440
13441 (defun gnus-group-get-description (group)
13442   ;; Get the description of a group by sending XGTITLE to the server.
13443   (and (gnus-request-group-description group)
13444        (save-excursion
13445          (set-buffer nntp-server-buffer)
13446          (goto-char (point-min))
13447          (and (looking-at "[^ \t]+[ \t]+\\(.*\\)")
13448               (buffer-substring (match-beginning 1) (match-end 1))))))
13449
13450 ;;;
13451 ;;; Server
13452 ;;;
13453
13454 (defvar gnus-server-mode-hook nil
13455   "Hook run in `gnus-server-mode' buffers.")
13456
13457 (defconst gnus-server-line-format "     {%(%h:%w%)}\n"
13458   "Format of server lines.
13459 It works along the same lines as a normal formatting string,
13460 with some simple extensions.")
13461
13462 (defvar gnus-server-mode-line-format "(ding) List of servers"
13463   "The format specification for the server mode line.")
13464
13465 (defconst gnus-server-line-format-alist
13466   (list (list ?h 'how ?s)
13467         (list ?n 'name ?s)
13468         (list ?w 'where ?s)
13469         ))
13470
13471 (defconst gnus-server-mode-line-format-alist 
13472   (list (list ?S 'news-server ?s)
13473         (list ?M 'news-method ?s)
13474         (list ?u 'user-defined ?s)))
13475
13476 (defvar gnus-server-line-format-spec nil)
13477 (defvar gnus-server-mode-line-format-spec nil)
13478 (defvar gnus-server-killed-servers nil)
13479
13480 (defvar gnus-server-mode-map nil)
13481 (put 'gnus-server-mode 'mode-class 'special)
13482
13483 (if gnus-server-mode-map
13484     nil
13485   (setq gnus-server-mode-map (make-sparse-keymap))
13486   (suppress-keymap gnus-server-mode-map)
13487   (define-key gnus-server-mode-map " " 'gnus-server-read-server)
13488   (define-key gnus-server-mode-map "\r" 'gnus-server-read-server)
13489   (define-key gnus-server-mode-map gnus-mouse-2 'gnus-server-pick-server)
13490   (define-key gnus-server-mode-map "q" 'gnus-server-exit)
13491   (define-key gnus-server-mode-map "l" 'gnus-server-list-servers)
13492   (define-key gnus-server-mode-map "k" 'gnus-server-kill-server)
13493   (define-key gnus-server-mode-map "y" 'gnus-server-yank-server)
13494   (define-key gnus-server-mode-map "c" 'gnus-server-copy-server)
13495   (define-key gnus-server-mode-map "a" 'gnus-server-add-server)
13496   (define-key gnus-server-mode-map "e" 'gnus-server-edit-server))
13497
13498 (defun gnus-server-mode ()
13499   "Major mode for listing and editing servers.
13500
13501 All normal editing commands are switched off.
13502 \\<gnus-server-mode-map>
13503
13504 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]'). 
13505
13506 The following commands are available:
13507
13508 \\{gnus-server-mode-map}"
13509   (interactive)
13510   (if gnus-visual (gnus-server-make-menu-bar))
13511   (kill-all-local-variables)
13512   (setq mode-line-modified "-- ")
13513   (make-local-variable 'mode-line-format)
13514   (setq mode-line-format (copy-sequence mode-line-format))
13515   (and (equal (nth 3 mode-line-format) "   ")
13516        (setcar (nthcdr 3 mode-line-format) ""))
13517   (setq major-mode 'gnus-server-mode)
13518   (setq mode-name "Server")
13519 ;  (gnus-group-set-mode-line)
13520   (setq mode-line-process nil)
13521   (use-local-map gnus-server-mode-map)
13522   (buffer-disable-undo (current-buffer))
13523   (setq truncate-lines t)
13524   (setq buffer-read-only t)
13525   (run-hooks 'gnus-server-mode-hook))
13526
13527 (defun gnus-server-insert-server-line (sformat name method)
13528   (let* ((sformat (or sformat gnus-server-line-format-spec))
13529          (how (car method))
13530          (where (nth 1 method))
13531          b)
13532     (beginning-of-line)
13533     (setq b (point))
13534     ;; Insert the text.
13535     (insert (eval sformat))
13536     (add-text-properties b (1+ b) (list 'gnus-server (intern name)))))
13537
13538 (defun gnus-server-setup-buffer ()
13539   (if (get-buffer gnus-server-buffer)
13540       ()
13541     (save-excursion
13542       (set-buffer (get-buffer-create gnus-server-buffer))
13543       (gnus-server-mode)
13544       (and gnus-carpal (gnus-carpal-setup-buffer 'server)))))
13545
13546 (defun gnus-server-prepare ()
13547   (setq gnus-server-mode-line-format-spec 
13548         (gnus-parse-format gnus-server-mode-line-format 
13549                            gnus-server-mode-line-format-alist))
13550   (setq gnus-server-line-format-spec 
13551         (gnus-parse-format gnus-server-line-format 
13552                            gnus-server-line-format-alist))
13553   (let ((alist gnus-server-alist)
13554         (buffer-read-only nil))
13555     (erase-buffer)
13556     (while alist
13557       (gnus-server-insert-server-line nil (car (car alist)) (cdr (car alist)))
13558       (setq alist (cdr alist))))
13559   (goto-char (point-min))
13560   (gnus-server-position-cursor))
13561
13562 (defun gnus-server-server-name ()
13563   (let ((server (get-text-property (gnus-point-at-bol) 'gnus-server)))
13564     (and server (symbol-name server))))
13565
13566 (defalias 'gnus-server-position-cursor 'gnus-goto-colon)
13567
13568 (defconst gnus-server-edit-buffer "*Gnus edit server*")
13569
13570 (defun gnus-server-update-server (server)
13571   (save-excursion
13572     (set-buffer gnus-server-buffer)
13573     (let ((buffer-read-only nil)
13574           (info (cdr (assoc server gnus-server-alist))))
13575       (gnus-dribble-enter 
13576        (concat "(gnus-server-set-info \"" server "\" '"
13577                (prin1-to-string info) ")"))
13578       ;; Buffer may be narrowed.
13579       (save-restriction
13580         (widen)
13581         (if (gnus-server-goto-server server)
13582             (delete-region (progn (beginning-of-line) (point))
13583                            (progn (forward-line 1) (point))))
13584         (let ((entry (assoc server gnus-server-alist)))
13585           (gnus-server-insert-server-line nil (car entry) (cdr entry))
13586           (gnus-server-position-cursor))))))
13587
13588 (defun gnus-server-set-info (server info)
13589   ;; Enter a select method into the virtual server alist.
13590   (gnus-dribble-enter 
13591    (concat "(gnus-server-set-info \"" server "\" '"
13592            (prin1-to-string info) ")"))
13593   (let* ((server (nth 1 info))
13594          (entry (assoc server gnus-server-alist)))
13595     (if entry (setcdr entry info)
13596       (setq gnus-server-alist
13597             (nconc gnus-server-alist (list (cons server info)))))))
13598
13599 (defun gnus-server-to-method (server)
13600   ;; Map virtual server names to select methods.
13601   (or (and (equal server "native") gnus-select-method)
13602       (cdr (assoc server gnus-server-alist))))
13603
13604 (defun gnus-server-extend-method (group method)
13605   ;; This function "extends" a virtual server.  If the server is
13606   ;; "hello", and the select method is ("hello" (my-var "something")) 
13607   ;; in the group "alt.alt", this will result in a new virtual server
13608   ;; called "helly+alt.alt".
13609   (let ((entry
13610          (gnus-copy-sequence 
13611           (if (equal (car method) "native") gnus-select-method
13612               (cdr (assoc (car method) gnus-server-alist))))))
13613     (setcar (cdr entry) (concat (nth 1 entry) "+" group))
13614     (nconc entry (cdr method))))
13615
13616 (defun gnus-server-get-method (group method)
13617   ;; Input either a server name, and extended server name, or a
13618   ;; select method, and return a select method. 
13619   (cond ((stringp method)
13620          (gnus-server-to-method method))
13621         ((and (stringp (car method)) group)
13622          (gnus-server-extend-method group method))
13623         (t
13624          (gnus-server-add-address method))))
13625
13626 (defun gnus-server-add-address (method)
13627   (let ((method-name (symbol-name (car method))))
13628     (if (and (memq 'address (assoc method-name gnus-valid-select-methods))
13629              (not (assq (intern (concat method-name "-address")) method)))
13630         (append method (list (list (intern (concat method-name "-address"))
13631                                    (nth 1 method))))
13632       method)))
13633
13634 (defun gnus-server-equal (s1 s2)
13635   (or (equal s1 s2)
13636       (and (= (length s1) (length s2))
13637            (progn
13638              (while (and s1 (member (car s1) s2))
13639                (setq s1 (cdr s1)))
13640              (null s1)))))
13641
13642 ;;; Interactive server functions.
13643
13644 (defun gnus-server-kill-server (server)
13645   "Kill the server on the current line."
13646   (interactive (list (gnus-server-server-name)))
13647   (or (gnus-server-goto-server server)
13648       (if server (error "No such server: %s" server)
13649         (error "No server on the current line")))
13650   (let ((buffer-read-only nil))
13651     (delete-region (progn (beginning-of-line) (point))
13652                    (progn (forward-line 1) (point))))
13653   (setq gnus-server-killed-servers 
13654         (cons (assoc server gnus-server-alist) gnus-server-killed-servers))
13655   (setq gnus-server-alist (delq (car gnus-server-killed-servers)
13656                                 gnus-server-alist))
13657   (gnus-server-position-cursor))
13658
13659 (defun gnus-server-yank-server ()
13660   "Yank the previously killed server."
13661   (interactive)
13662   (or gnus-server-killed-servers
13663       (error "No killed servers to be yanked"))
13664   (let ((alist gnus-server-alist)
13665         (server (gnus-server-server-name))
13666         (killed (car gnus-server-killed-servers)))
13667     (if (not server) 
13668         (setq gnus-server-alist (nconc gnus-server-alist (list killed)))
13669       (if (string= server (car (car gnus-server-alist)))
13670           (setq gnus-server-alist (cons killed gnus-server-alist))
13671         (while (and (cdr alist)
13672                     (not (string= server (car (car (cdr alist))))))
13673           (setq alist (cdr alist)))
13674         (setcdr alist (cons killed (cdr alist)))))
13675     (gnus-server-update-server (car killed))
13676     (setq gnus-server-killed-servers (cdr gnus-server-killed-servers))
13677     (gnus-server-position-cursor)))
13678
13679 (defun gnus-server-exit ()
13680   "Return to the group buffer."
13681   (interactive)
13682   (kill-buffer (current-buffer))
13683   (switch-to-buffer gnus-group-buffer))
13684
13685 (defun gnus-server-list-servers ()
13686   "List all available servers."
13687   (interactive)
13688   (let ((cur (gnus-server-server-name)))
13689     (gnus-server-prepare)
13690     (if cur (gnus-server-goto-server cur)
13691       (goto-char (point-max))
13692       (forward-line -1))
13693     (gnus-server-position-cursor)))
13694
13695 (defun gnus-server-copy-server (from to)
13696   (interactive
13697    (list
13698     (or (gnus-server-server-name)
13699         (error "No server on the current line"))
13700     (read-string "Copy to: ")))
13701   (or from (error "No server on current line"))
13702   (or (and to (not (string= to ""))) (error "No name to copy to"))
13703   (and (assoc to gnus-server-alist) (error "%s already exists" to))
13704   (or (assoc from gnus-server-alist) 
13705       (error "%s: no such server" from))
13706   (let ((to-entry (gnus-copy-sequence (assoc from gnus-server-alist))))
13707     (setcar to-entry to)
13708     (setcar (nthcdr 2 to-entry) to)
13709     (setq gnus-server-killed-servers 
13710           (cons to-entry gnus-server-killed-servers))
13711     (gnus-server-yank-server)))
13712
13713 (defun gnus-server-add-server (how where)
13714   (interactive 
13715    (list (intern (completing-read "Server method: "
13716                                   gnus-valid-select-methods nil t))
13717          (read-string "Server name: ")))
13718   (setq gnus-server-killed-servers 
13719         (cons (list where how where) gnus-server-killed-servers))
13720   (gnus-server-yank-server))
13721
13722 (defun gnus-server-goto-server (server)
13723   "Jump to a server line."
13724   (interactive
13725    (list (completing-read "Goto server: " gnus-server-alist nil t)))
13726   (let ((to (text-property-any (point-min) (point-max) 
13727                                'gnus-server (intern server))))
13728     (and to
13729          (progn
13730            (goto-char to) 
13731            (gnus-server-position-cursor)))))
13732
13733 (defun gnus-server-edit-server (server)
13734   "Edit the server on the current line."
13735   (interactive (list (gnus-server-server-name)))
13736   (or server
13737       (error "No server on current line"))
13738   (let ((winconf (current-window-configuration)))
13739     (get-buffer-create gnus-server-edit-buffer)
13740     (gnus-configure-windows 'edit-server)
13741     (gnus-add-current-to-buffer-list)
13742     (emacs-lisp-mode)
13743     (make-local-variable 'gnus-prev-winconf)
13744     (setq gnus-prev-winconf winconf)
13745     (use-local-map (copy-keymap (current-local-map)))
13746     (let ((done-func '(lambda () 
13747                         "Exit editing mode and update the information."
13748                         (interactive)
13749                         (gnus-server-edit-server-done 'group))))
13750       (setcar (cdr (nth 4 done-func)) server)
13751       (local-set-key "\C-c\C-c" done-func))
13752     (erase-buffer)
13753     (insert ";; Type `C-c C-c' after you have edited the server.\n\n")
13754     (insert (pp-to-string (cdr (assoc server gnus-server-alist))))))
13755
13756 (defun gnus-server-edit-server-done (server)
13757   (interactive)
13758   (set-buffer (get-buffer-create gnus-server-edit-buffer))
13759   (goto-char (point-min))
13760   (let ((form (read (current-buffer)))
13761         (winconf gnus-prev-winconf))
13762     (gnus-server-set-info server form)
13763     (kill-buffer (current-buffer))
13764     (and winconf (set-window-configuration winconf))
13765     (set-buffer gnus-server-buffer)
13766     (gnus-server-update-server (gnus-server-server-name))
13767     (gnus-server-position-cursor)))
13768
13769 (defun gnus-server-read-server (server)
13770   "Browse a server."
13771   (interactive (list (gnus-server-server-name)))
13772   (gnus-browse-foreign-server (gnus-server-to-method server) (current-buffer)))
13773
13774 (defun gnus-mouse-pick-server (e)
13775   (interactive "e")
13776   (mouse-set-point e)
13777   (gnus-server-read-server (gnus-server-server-name)))
13778
13779 ;;;
13780 ;;; entry points into gnus-score.el
13781 ;;;
13782
13783 ;;; Finding score files. 
13784
13785 (defvar gnus-global-score-files nil
13786   "*List of global score files and directories.
13787 Set this variable if you want to use people's score files.  One entry
13788 for each score file or each score file directory.  Gnus will decide
13789 by itself what score files are applicable to which group.
13790
13791 Say you want to use the single score file
13792 \"/ftp.ifi.uio.no@ftp:/pub/larsi/ding/score/soc.motss.SCORE\" and all
13793 score files in the \"/ftp.some-where:/pub/score\" directory.
13794
13795  (setq gnus-global-score-files
13796        '(\"/ftp.ifi.uio.no:/pub/larsi/ding/score/soc.motss.SCORE\"
13797          \"/ftp.some-where:/pub/score\"))")
13798
13799 (defun gnus-score-score-files (group)
13800   "Return a list of all possible score files."
13801   ;; Search and set any global score files.
13802   (and gnus-global-score-files 
13803        (or gnus-internal-global-score-files
13804            (gnus-score-search-global-directories gnus-global-score-files)))
13805   ;; Fix the kill-file dir variable.
13806   (setq gnus-kill-files-directory 
13807         (file-name-as-directory
13808          (or gnus-kill-files-directory "~/News/")))
13809   ;; If we can't read it, there are no score files.
13810   (if (not (file-exists-p (expand-file-name gnus-kill-files-directory)))
13811       (setq gnus-score-file-list nil)
13812     (if (gnus-use-long-file-name 'not-score)
13813         ;; We want long file names.
13814         (if (or (not gnus-score-file-list)
13815                 (not (car gnus-score-file-list))
13816                 (gnus-file-newer-than gnus-kill-files-directory
13817                                       (car gnus-score-file-list)))
13818               (setq gnus-score-file-list 
13819                     (cons (nth 5 (file-attributes gnus-kill-files-directory))
13820                           (nreverse 
13821                            (directory-files 
13822                             gnus-kill-files-directory t 
13823                             (gnus-score-file-regexp))))))
13824       ;; We do not use long file names, so we have to do some
13825       ;; directory traversing.  
13826       (let ((mdir (length (expand-file-name gnus-kill-files-directory)))
13827             (suffixes (list gnus-score-file-suffix gnus-adaptive-file-suffix))
13828             dir files suffix)
13829         (while suffixes
13830           (setq dir (expand-file-name
13831                      (concat gnus-kill-files-directory
13832                              (gnus-replace-chars-in-string group ?. ?/))))
13833           (setq dir (gnus-replace-chars-in-string dir ?: ?/))
13834           (setq suffix (car suffixes)
13835                 suffixes (cdr suffixes))
13836           (if (file-exists-p (concat dir "/" suffix))
13837               (setq files (cons (concat dir "/" suffix) files)))
13838           (while (>= (1+ (length dir)) mdir)
13839             (and (file-exists-p (concat dir "/all/" suffix))
13840                  (setq files (cons (concat dir "/all/" suffix) files)))
13841             (string-match "/[^/]*$" dir)
13842             (setq dir (substring dir 0 (match-beginning 0)))))
13843         (setq gnus-score-file-list 
13844               (cons nil (nreverse files)))))
13845     (cdr gnus-score-file-list)))
13846
13847 (defun gnus-score-file-regexp ()
13848   (concat "\\(" gnus-score-file-suffix 
13849           "\\|" gnus-adaptive-file-suffix "\\)$"))
13850         
13851 (defun gnus-score-find-bnews (group)
13852   "Return a list of score files for GROUP.
13853 The score files are those files in the ~/News directory which matches
13854 GROUP using BNews sys file syntax."
13855   (let* ((sfiles (append (gnus-score-score-files group)
13856                          gnus-internal-global-score-files))
13857          (kill-dir (file-name-as-directory 
13858                     (expand-file-name gnus-kill-files-directory)))
13859          (klen (length kill-dir))
13860          ofiles not-match regexp)
13861     (save-excursion
13862       (set-buffer (get-buffer-create "*gnus score files*"))
13863       (buffer-disable-undo (current-buffer))
13864       ;; Go through all score file names and create regexp with them
13865       ;; as the source.  
13866       (while sfiles
13867         (erase-buffer)
13868         (insert (car sfiles))
13869         (goto-char (point-min))
13870         ;; First remove the suffix itself.
13871         (re-search-forward (concat "." (gnus-score-file-regexp)))
13872         (replace-match "" t t) 
13873         (goto-char (point-min))
13874         (if (looking-at (regexp-quote kill-dir))
13875             ;; If the file name was just "SCORE", `klen' is one character
13876             ;; too much.
13877             (delete-char (min (1- (point-max)) klen))
13878           (goto-char (point-max))
13879           (search-backward "/")
13880           (delete-region (1+ (point)) (point-min)))
13881         ;; If short file names were used, we have to translate slashes.
13882         (goto-char (point-min))
13883         (while (re-search-forward "[/:]" nil t)
13884           (replace-match "." t t))
13885         ;; Translate "all" to ".*".
13886         (while (search-forward "all" nil t)
13887           (replace-match ".*" t t))
13888         (goto-char (point-min))
13889         ;; Deal with "not."s.
13890         (if (looking-at "not.")
13891             (progn
13892               (setq not-match t)
13893               (setq regexp (buffer-substring 5 (point-max))))
13894           (setq regexp (buffer-substring 1 (point-max)))
13895           (setq not-match nil))
13896         ;; Finally - if this resulting regexp matches the group name,
13897         ;; we add this score file to the list of score files
13898         ;; applicable to this group.
13899         (if (or (and not-match
13900                      (not (string-match regexp group)))
13901                 (and (not not-match)
13902                      (string-match regexp group)))
13903             (setq ofiles (cons (car sfiles) ofiles)))
13904         (setq sfiles (cdr sfiles)))
13905       (kill-buffer (current-buffer))
13906       ;; Slight kludge here - the last score file returned should be
13907       ;; the local score file, whether it exists or not. This is so
13908       ;; that any score commands the user enters will go to the right
13909       ;; file, and not end up in some global score file.
13910       (let ((localscore
13911              (expand-file-name
13912               (if (gnus-use-long-file-name 'not-score)
13913                   (concat gnus-kill-files-directory group "." 
13914                           gnus-score-file-suffix)
13915                 (concat gnus-kill-files-directory
13916                         (gnus-replace-chars-in-string group ?. ?/)
13917                         "/" gnus-score-file-suffix)))))
13918         (and (member localscore ofiles)
13919              (delete localscore ofiles))
13920         (setq ofiles (cons localscore ofiles)))
13921       (nreverse ofiles))))
13922
13923 (defun gnus-score-find-single (group)
13924   "Return list containing the score file for GROUP."
13925   (list (gnus-score-file-name group gnus-adaptive-file-suffix)
13926         (gnus-score-file-name group)))
13927
13928 (defun gnus-score-find-hierarchical (group)
13929   "Return list of score files for GROUP.
13930 This includes the score file for the group and all its parents."
13931   (let ((all (copy-sequence '(nil)))
13932         (start 0))
13933     (while (string-match "\\." group (1+ start))
13934       (setq start (match-beginning 0))
13935       (setq all (cons (substring group 0 start) all)))
13936     (setq all (cons group all))
13937     (nconc
13938      (mapcar (lambda (newsgroup)
13939                (gnus-score-file-name newsgroup gnus-adaptive-file-suffix))
13940              (setq all (nreverse all)))
13941      (mapcar 'gnus-score-file-name all))))
13942
13943 (defvar gnus-score-file-alist-cache nil)
13944
13945 (defun gnus-score-find-alist (group)
13946   "Return list of score files for GROUP.
13947 The list is determined from the variable gnus-score-file-alist."
13948   (let ((alist gnus-score-file-multiple-match-alist)
13949         score-files)
13950     ;; if this group has been seen before, return the cached entry
13951     (if (setq score-files (assoc group gnus-score-file-alist-cache))
13952         (cdr score-files)      ; ensures caching of groups with no matches
13953       ;; handle the multiple match alist
13954       (while alist
13955         (and (string-match (car (car alist)) group)
13956              (setq score-files
13957                    (nconc score-files (copy-sequence (cdr (car alist))))))
13958         (setq alist (cdr alist)))
13959       (setq alist gnus-score-file-single-match-alist)
13960       ;; handle the single match alist
13961       (while alist
13962         (and (string-match (car (car alist)) group)
13963              ;; progn used just in case ("regexp") has no files
13964              ;; and score-files is still nil. -sj
13965              ;; this can be construed as a "stop searching here" feature :>
13966              ;; and used to simplify regexps in the single-alist 
13967              (progn
13968                (setq score-files
13969                      (nconc score-files (copy-sequence (cdr (car alist)))))
13970                (setq alist nil)))
13971         (setq alist (cdr alist)))
13972       ;; cache the score files
13973       (setq gnus-score-file-alist-cache
13974             (cons (cons group score-files) gnus-score-file-alist-cache))
13975       score-files)))
13976
13977
13978 (defun gnus-possibly-score-headers (&optional trace)
13979   (let ((func gnus-score-find-score-files-function)
13980         score-files)
13981     (and func (not (listp func))
13982          (setq func (list func)))
13983     ;; Go through all the functions for finding score files (or actual
13984     ;; scores) and add them to a list.
13985     (setq score-files (gnus-score-find-alist gnus-newsgroup-name))
13986     (while func
13987       (and (symbolp (car func))
13988            (fboundp (car func))
13989            (setq score-files 
13990                  (nconc score-files (funcall (car func) gnus-newsgroup-name))))
13991       (setq func (cdr func)))
13992     (if score-files (gnus-score-headers score-files trace))))
13993
13994 (defun gnus-score-file-name (newsgroup &optional suffix)
13995   "Return the name of a score file for NEWSGROUP."
13996   (let ((suffix (or suffix gnus-score-file-suffix)))
13997     (cond 
13998      ((or (null newsgroup)
13999           (string-equal newsgroup ""))
14000       ;; The global score file is placed at top of the directory.
14001       (expand-file-name 
14002        suffix (or gnus-kill-files-directory "~/News")))
14003      ((gnus-use-long-file-name 'not-score)
14004       ;; Append ".SCORE" to newsgroup name.
14005       (expand-file-name (concat (gnus-newsgroup-saveable-name newsgroup)
14006                                 "." suffix)
14007                         (or gnus-kill-files-directory "~/News")))
14008      (t
14009       ;; Place "SCORE" under the hierarchical directory.
14010       (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
14011                                 "/" suffix)
14012                         (or gnus-kill-files-directory "~/News"))))))
14013
14014 (defun gnus-score-search-global-directories (files)
14015   "Scan all global score directories for score files."
14016   ;; Set the variable `gnus-internal-global-score-files' to all
14017   ;; available global score files.
14018   (interactive (list gnus-global-score-files))
14019   (let (out)
14020     (while files
14021       (if (string-match "/$" (car files))
14022           (setq out (nconc (directory-files 
14023                             (car files) t
14024                             (concat (gnus-score-file-regexp) "$"))))
14025         (setq out (cons (car files) out)))
14026       (setq files (cdr files)))
14027     (setq gnus-internal-global-score-files out)))
14028
14029 ;; Allow redefinition of Gnus functions.
14030
14031 (gnus-ems-redefine)
14032
14033 (provide 'gnus)
14034
14035 ;;; gnus.el ends here