*** empty log message ***
[gnus] / lisp / gnus.el
1 ;;; gnus.el --- a newsreader for GNU Emacs
2 ;; Copyright (C) 1987,88,89,90,93,94,95 Free Software Foundation, Inc.
3
4 ;; Author: Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
5 ;;      Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23
24 ;;; Commentary:
25
26 ;; Although (ding) Gnus looks suspiciously like GNUS, it isn't quite
27 ;; the same beast. Most internal structures have been changed. If you
28 ;; have written packages that depend on any of the hash tables,
29 ;; `gnus-newsrc-alist', `gnus-killed-assoc', marked lists, the .newsrc
30 ;; buffer, or internal knowledge of the `nntp-header-' macros, or
31 ;; dependence on the buffers having a certain format, your code will
32 ;; fail.
33
34 ;;; Code:
35
36 (require 'mail-utils)
37 (require 'timezone)
38 (require 'nnheader)
39
40 ;; Site dependent variables. These variables should be defined in
41 ;; paths.el.
42
43 (defvar gnus-default-nntp-server nil
44   "Specify a default NNTP server.
45 This variable should be defined in paths.el, and should never be set
46 by the user.
47 If you want to change servers, you should use `gnus-select-method'.
48 See the documentation to that variable.")
49
50 (defconst gnus-backup-default-subscribed-newsgroups 
51   '("news.announce.newusers" "news.groups.questions" "gnu.emacs.gnus")
52   "Default default new newsgroups the first time Gnus is run.
53 Should be set in paths.el, and shouldn't be touched by the user.")
54
55 (defvar gnus-local-domain nil
56   "Local domain name without a host name.
57 The DOMAINNAME environment variable is used instead if it is defined.
58 If the `system-name' function returns the full Internet name, there is
59 no need to set this variable.")
60
61 (defvar gnus-local-organization nil
62   "String with a description of what organization (if any) the user belongs to.
63 The ORGANIZATION environment variable is used instead if it is defined.
64 If this variable contains a function, this function will be called
65 with the current newsgroup name as the argument. The function should
66 return a string.
67
68 In any case, if the string (either in the variable, in the environment
69 variable, or returned by the function) is a file name, the contents of
70 this file will be used as the organization.")
71
72 (defvar gnus-use-generic-from nil
73   "If nil, the full host name will be the system name prepended to the domain name.
74 If this is a string, the full host name will be this string.
75 If this is non-nil, non-string, the domain name will be used as the
76 full host name.")
77
78 (defvar gnus-use-generic-path nil
79   "If nil, use the NNTP server name in the Path header.
80 If stringp, use this; if non-nil, use no host name (user name only).")
81
82
83 ;; Customization variables
84
85 (defvar gnus-select-method 
86   (list 'nntp (or (getenv "NNTPSERVER") 
87                   (if (and gnus-default-nntp-server
88                            (not (string= gnus-default-nntp-server "")))
89                       gnus-default-nntp-server)
90                   (system-name)))
91   "*Default method for selecting a newsgroup.
92 This variable should be a list, where the first element is how the
93 news is to be fetched, the second is the address. 
94
95 For instance, if you want to get your news via NNTP from
96 \"flab.flab.edu\", you could say:
97
98 (setq gnus-select-method '(nntp \"flab.flab.edu\"))
99
100 If you want to use your local spool, say:
101
102 (setq gnus-select-method (list 'nnspool (system-name)))
103
104 If you use this variable, you must set `gnus-nntp-server' to nil.
105
106 There is a lot more to know about select methods and virtual servers -
107 see the manual for details.")
108
109 ;; Added by Sudish Joseph <joseph@cis.ohio-state.edu>.
110 (defvar gnus-post-method nil
111   "*Preferred method for posting USENET news.
112 If this variable is nil, Gnus will use the current method to decide
113 which method to use when posting.  If it is non-nil, it will override
114 the current method.  This method will not be used in mail groups and
115 the like, only in \"real\" newsgroups.
116
117 The value must be a valid method as discussed in the documentation of
118 `gnus-select-method'.")
119
120 (defvar gnus-refer-article-method nil
121   "*Preferred method for fetching an article by Message-ID.
122 If you are reading news from the local spool (with nnspool), fetching
123 articles by Message-ID is painfully slow. By setting this method to an
124 nntp method, you might get acceptable results.
125
126 The value of this variable must be a valid select method as discussed
127 in the documentation of `gnus-select-method'")
128
129 (defvar gnus-secondary-select-methods nil
130   "*A list of secondary methods that will be used for reading news.
131 This is a list where each element is a complete select method (see
132 `gnus-select-method').  
133
134 If, for instance, you want to read your mail with the nnml backend,
135 you could set this variable:
136
137 (setq gnus-secondary-select-methods '((nnml \"\"))")
138
139 (defvar gnus-secondary-servers nil
140   "*List of NNTP servers that the user can choose between interactively.
141 To make Gnus query you for a server, you have to give `gnus' a
142 non-numeric prefix - `C-u M-x gnus', in short.")
143
144 (defvar gnus-nntp-server nil
145   "*The name of the host running the NNTP server.
146 This variable is semi-obsolete. Use the `gnus-select-method'
147 variable instead.")
148
149 (defvar gnus-nntp-service "nntp"
150   "*NNTP service name (\"nntp\" or 119).
151 This is an obsolete variable, which is scarcely used. If you use an
152 nntp server for your newsgroup and want to change the port number
153 used to 899, you would say something along these lines:
154
155  (setq gnus-select-method '(nntp \"my.nntp.server\" (nntp-port-number 899)))")
156
157 (defvar gnus-startup-file "~/.newsrc"
158   "*Your `.newsrc' file.
159 `.newsrc-SERVER' will be used instead if that exists.")
160
161 (defvar gnus-init-file "~/.gnus"
162   "*Your Gnus elisp startup file.
163 If a file with the .el or .elc suffixes exist, it will be read
164 instead.") 
165
166 (defvar gnus-group-faq-directory
167   "/anonymous@rtfm.mit.edu:/pub/usenet-by-group/"
168   "*Directory where the group FAQs are stored.
169 This will most commonly be on a remote machine, and the file will be
170 fetched by ange-ftp.")
171
172 (defvar gnus-group-archive-directory
173   "/ftp@sina.tcamc.uh.edu:/pub/emacs/ding-list/" 
174   "*The address of the (ding) archives.")
175
176 (defvar gnus-default-subscribed-newsgroups nil
177   "*This variable lists what newsgroups should be subscribed the first time Gnus is used.
178 It should be a list of strings.
179 If it is `t', Gnus will not do anything special the first time it is
180 started; it'll just use the normal newsgroups subscription methods.")
181
182 (defvar gnus-use-cross-reference t
183   "*Non-nil means that cross referenced articles will be marked as read.
184 If nil, ignore cross references.  If t, mark articles as read in
185 subscribed newsgroups. If neither t nor nil, mark as read in all
186 newsgroups.") 
187
188 (defvar gnus-use-dribble-file t
189   "*Non-nil means that Gnus will use a dribble file to store user updates.
190 If Emacs should crash without saving the .newsrc files, complete
191 information can be restored from the dribble file.")
192
193 (defvar gnus-asynchronous nil
194   "*If non-nil, Gnus will supply backends with data needed for async article fetching.")
195
196 (defvar gnus-asynchronous-article-function nil
197   "*Function for picking articles to pre-fetch, possibly.")
198
199 (defvar gnus-score-file-single-match-alist nil
200   "*Alist mapping regexps to lists of score files.
201 Each element of this alist should be of the form
202         (\"REGEXP\" [ \"SCORE-FILE-1\" ] [ \"SCORE-FILE-2\" ] ... )
203
204 If the name of a group is matched by REGEXP, the corresponding scorefiles
205 will be used for that group.
206 The first match found is used, subsequent matching entries are ignored (to
207 use multiple matches, see gnus-score-file-multiple-match-alist).
208
209 These score files are loaded in addition to any files returned by
210 gnus-score-find-score-files-function (which see).")
211
212 (defvar gnus-score-file-multiple-match-alist nil
213   "*Alist mapping regexps to lists of score files.
214 Each element of this alist should be of the form
215         (\"REGEXP\" [ \"SCORE-FILE-1\" ] [ \"SCORE-FILE-2\" ] ... )
216
217 If the name of a group is matched by REGEXP, the corresponding scorefiles
218 will be used for that group.
219 If multiple REGEXPs match a group, the score files corresponding to each
220 match will be used (for only one match to be used, see
221 gnus-score-file-single-match-alist).
222
223 These score files are loaded in addition to any files returned by
224 gnus-score-find-score-files-function (which see).")
225
226
227 (defvar gnus-score-file-suffix "SCORE"
228   "*Suffix of the score files.")
229
230 (defvar gnus-adaptive-file-suffix "ADAPT"
231   "*Suffix of the adaptive score files.")
232
233 (defvar gnus-score-find-score-files-function 'gnus-score-find-bnews
234   "*Function used to find SCORE files.
235 The function will be called with the group name as the argument, and
236 should return a list of score files to apply to that group.  The score
237 files do not actually have to exist.
238
239 Predefined values are:
240
241 gnus-score-find-single: Only apply the group's own SCORE file.
242 gnus-score-find-hierarchical: Also apply SCORE files from parent groups.
243 gnus-score-find-bnews: Apply SCORE files whose names matches.
244
245 See the documentation to these functions for more information.
246
247 This variable can also be a list of functions to be called.  Each
248 function should either return a list of score files, or a list of
249 score alists.")
250
251 (defvar gnus-score-interactive-default-score 1000
252   "*Scoring commands will raise/lower the score with this number as the default.")
253
254 (defvar gnus-large-newsgroup 200
255   "*The number of articles which indicates a large newsgroup.
256 If the number of articles in a newsgroup is greater than this value,
257 confirmation is required for selecting the newsgroup.")
258
259 ;; Suggested by Andrew Eskilsson <pi92ae@lelle.pt.hk-r.se>.
260 (defvar gnus-no-groups-message "No news is horrible news"
261   "*Message displayed by Gnus when no groups are available.")
262
263 (defvar gnus-use-long-file-name (not (memq system-type '(usg-unix-v xenix)))
264   "*Non-nil means that the default name of a file to save articles in is the group name.
265 If it's nil, the directory form of the group name is used instead.
266
267 If this variable is a list, and the list contains the element
268 `not-score', long file names will not be used for score files; if it
269 contains the element `not-save', long file names will not be used for
270 saving; and if it contains the element `not-kill', long file names
271 will not be used for kill files.")
272
273 (defvar gnus-article-save-directory (or (getenv "SAVEDIR") "~/News/")
274   "*Name of the directory articles will be saved in (default \"~/News\").
275 Initialized from the SAVEDIR environment variable.")
276
277 (defvar gnus-kill-files-directory (or (getenv "SAVEDIR") "~/News/")
278   "*Name of the directory where kill files will be stored (default \"~/News\").
279 Initialized from the SAVEDIR environment variable.")
280
281 (defvar gnus-default-article-saver 'gnus-summary-save-in-rmail
282   "*A function to save articles in your favorite format.
283 The function must be interactively callable (in other words, it must
284 be an Emacs command).
285
286 Gnus provides the following functions:
287
288 * gnus-summary-save-in-rmail (Rmail format)
289 * gnus-summary-save-in-mail (Unix mail format)
290 * gnus-summary-save-in-folder (MH folder)
291 * gnus-summary-save-in-file (article format).
292 * gnus-summary-save-in-vm (use VM's folder format).")
293
294 (defvar gnus-rmail-save-name (function gnus-plain-save-name)
295   "*A function generating a file name to save articles in Rmail format.
296 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
297
298 (defvar gnus-mail-save-name (function gnus-plain-save-name)
299   "*A function generating a file name to save articles in Unix mail format.
300 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
301
302 (defvar gnus-folder-save-name (function gnus-folder-save-name)
303   "*A function generating a file name to save articles in MH folder.
304 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER.")
305
306 (defvar gnus-file-save-name (function gnus-numeric-save-name)
307   "*A function generating a file name to save articles in article format.
308 The function is called with NEWSGROUP, HEADERS, and optional
309 LAST-FILE.")
310
311 (defvar gnus-split-methods nil
312   "*Variable used to suggest where articles are to be saved.
313 The syntax of this variable is the same as `nnmail-split-methods'.  
314
315 For instance, if you would like to save articles related to Gnus in
316 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
317 you could set this variable to something like:
318
319  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
320    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))")
321
322 (defvar gnus-save-score nil
323   "*If non-nil, save group scoring info.")
324
325 (defvar gnus-use-adaptive-scoring nil
326   "*If non-nil, use some adaptive scoring scheme.")
327
328 (defvar gnus-use-cache nil
329   "*If non-nil, Gnus will cache (some) articles locally.")
330
331 (defvar gnus-use-scoring t
332   "*If non-nil, enable scoring.")
333
334 (defvar gnus-fetch-old-headers nil
335   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
336 If an unread article in the group refers to an older, already read (or
337 just marked as read) article, the old article will not normally be
338 displayed in the Summary buffer.  If this variable is non-nil, Gnus
339 will attempt to grab the headers to the old articles, and thereby
340 build complete threads.  If it has the value `some', only enough
341 headers to connect otherwise loose threads will be displayed.
342
343 The server has to support XOVER for any of this to work.")
344
345 (defvar gnus-visual t
346   "*If non-nil, will do various highlighting.
347 If nil, no mouse highlights (or any other highlights) will be
348 performed.  This might speed up Gnus some when generating large group
349 and summary buffers.")
350
351 (defvar gnus-novice-user t
352   "*Non-nil means that you are a usenet novice.
353 If non-nil, verbose messages may be displayed and confirmations may be
354 required.")
355
356 (defvar gnus-expert-user nil
357   "*Non-nil means that you will never be asked for confirmation about anything.
358 And that means *anything*.")
359
360 (defvar gnus-verbose 7
361   "*Integer that says how verbose Gnus should be.
362 The higher the number, the more messages Gnus will flash to say what
363 it's doing.  At zero, Gnus will be totally mute; at five, Gnus will
364 display most important messages; and at ten, Gnus will keep on
365 jabbering all the time.")
366
367 (defvar gnus-keep-same-level nil
368   "*Non-nil means that the next newsgroup after the current will be on the same level.
369 When you type, for instance, `n' after reading the last article in the
370 current newsgroup, you will go to the next newsgroup. If this variable
371 is nil, the next newsgroup will be the next from the group
372 buffer. 
373 If this variable is non-nil, Gnus will either put you in the
374 next newsgroup with the same level, or, if no such newsgroup is
375 available, the next newsgroup with the lowest possible level higher
376 than the current level.
377 If this variable is `best', Gnus will make the next newsgroup the one
378 with the best level.")
379
380 (defvar gnus-summary-make-false-root 'adopt
381   "*nil means that Gnus won't gather loose threads.
382 If the root of a thread has expired or been read in a previous
383 session, the information necessary to build a complete thread has been
384 lost. Instead of having many small sub-threads from this original thread
385 scattered all over the summary buffer, Gnus can gather them. 
386
387 If non-nil, Gnus will try to gather all loose sub-threads from an
388 original thread into one large thread.
389
390 If this variable is non-nil, it should be one of `none', `adopt',
391 `dummy' or `empty'.
392
393 If this variable is `none', Gnus will not make a false root, but just
394 present the sub-threads after another.
395 If this variable is `dummy', Gnus will create a dummy root that will
396 have all the sub-threads as children.
397 If this variable is `adopt', Gnus will make one of the \"children\"
398 the parent and mark all the step-children as such.
399 If this variable is `empty', the \"children\" are printed with empty
400 subject fields.  (Or rather, they will be printed with a string
401 given by the `gnus-summary-same-subject' variable.)")
402
403 (defvar gnus-summary-gather-subject-limit nil
404   "*Maximum length of subject comparisons when gathering loose threads.
405 Use nil to compare full subjects.  Setting this variable to a low
406 number will help gather threads that have been corrupted by
407 newsreaders chopping off subject lines, but it might also mean that
408 unrelated articles that have subject that happen to begin with the
409 same few characters will be incorrectly gathered.
410
411 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
412 comparing subjects.")
413
414 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
415 (defvar gnus-summary-same-subject ""
416   "*String indicating that the current article has the same subject as the previous.
417 This variable will only be used if the value of
418 `gnus-summary-make-false-root' is `empty'.")
419
420 (defvar gnus-summary-goto-unread nil
421   "*If non-nil, marking commands will go to the next unread article.")
422
423 (defvar gnus-group-goto-unread t
424   "*If non-nil, movement commands will go to the next unread and subscribed group.")
425
426 (defvar gnus-check-new-newsgroups t
427   "*Non-nil means that Gnus will add new newsgroups at startup.
428 If this variable is `ask-server', Gnus will ask the server for new
429 groups since the last time it checked. This means that the killed list
430 is no longer necessary, so you could set `gnus-save-killed-list' to
431 nil. 
432
433 A variant is to have this variable be a list of select methods. Gnus
434 will then use the `ask-server' method on all these select methods to
435 query for new groups from all those servers.
436
437 Eg.
438   (setq gnus-check-new-newsgroups 
439         '((nntp \"some.server\") (nntp \"other.server\")))
440
441 If this variable is nil, then you have to tell Gnus explicitly to
442 check for new newsgroups with \\<gnus-group-mode-map>\\[gnus-find-new-newsgroups].")
443
444 (defvar gnus-check-bogus-newsgroups nil
445   "*Non-nil means that Gnus will check and remove bogus newsgroup at startup.
446 If this variable is nil, then you have to tell Gnus explicitly to
447 check for bogus newsgroups with \\<gnus-group-mode-map>\\[gnus-group-check-bogus-groups].")
448
449 (defvar gnus-read-active-file t
450   "*Non-nil means that Gnus will read the entire active file at startup.
451 If this variable is nil, Gnus will only know about the groups in your
452 `.newsrc' file.
453
454 If this variable is `some', Gnus will try to only read the relevant
455 parts of the active file from the server.  Not all servers support
456 this, and it might be quite slow with other servers, but this should
457 generally be faster than both the t and nil value.
458
459 If you set this variable to nil or `some', you probably still want to
460 be told about new newsgroups that arrive.  To do that, set
461 `gnus-check-new-newsgroups' to `ask-server'.  This may not work
462 properly with all servers.")
463
464 (defvar gnus-level-subscribed 5
465   "*Groups with levels less than or equal to this variable are subscribed.")
466
467 (defvar gnus-level-unsubscribed 7
468   "*Groups with levels less than or equal to this variable are unsubscribed.
469 Groups with levels less than `gnus-level-subscribed', which should be
470 less than this variable, are subscribed.")
471
472 (defvar gnus-level-zombie 8
473   "*Groups with this level are zombie groups.")
474
475 (defvar gnus-level-killed 9
476   "*Groups with this level are killed.")
477
478 (defvar gnus-level-default-subscribed 3
479   "*New subscribed groups will be subscribed at this level.")
480
481 (defvar gnus-level-default-unsubscribed 6
482   "*New unsubscribed groups will be unsubscribed at this level.")
483
484 (defvar gnus-activate-foreign-newsgroups nil
485   "*If nil, Gnus will not check foreign newsgroups at startup.
486 If it is non-nil, it should be a number between one and nine. Foreign
487 newsgroups that have a level lower or equal to this number will be
488 activated on startup. For instance, if you want to active all
489 subscribed newsgroups, but not the rest, you'd set this variable to 
490 `gnus-level-subscribed'.
491
492 If you subscribe to lots of newsgroups from different servers, startup
493 might take a while. By setting this variable to nil, you'll save time,
494 but you won't be told how many unread articles there are in the
495 groups.")
496
497 (defvar gnus-save-newsrc-file t
498   "*Non-nil means that Gnus will save the `.newsrc' file.
499 Gnus always saves its own startup file, which is called
500 \".newsrc.eld\".  The file called \".newsrc\" is in a format that can
501 be readily understood by other newsreaders.  If you don't plan on
502 using other newsreaders, set this variable to nil to save some time on
503 exit.")
504
505 (defvar gnus-save-killed-list t
506   "*If non-nil, save the list of killed groups to the startup file.
507 This will save both time (when starting and quitting) and space (both
508 memory and disk), but it will also mean that Gnus has no record of
509 which groups are new and which are old, so the automatic new
510 newsgroups subscription methods become meaningless. You should always
511 set `gnus-check-new-newsgroups' to `ask-server' or nil if you set this
512 variable to nil.")
513
514 (defvar gnus-interactive-catchup t
515   "*If non-nil, require your confirmation when catching up a group.")
516
517 (defvar gnus-interactive-post t
518   "*If non-nil, group name will be asked for when posting.")
519
520 (defvar gnus-interactive-exit t
521   "*If non-nil, require your confirmation when exiting Gnus.")
522
523 (defvar gnus-kill-killed nil
524   "*If non-nil, Gnus will apply kill files to already killed articles.
525 If it is nil, Gnus will never apply kill files to articles that have
526 already been through the scoring process, which might very well save lots
527 of time.")
528
529 (defvar gnus-extract-address-components 'gnus-extract-address-components
530   "*Function for extracting address components from a From header.
531 Two pre-defined function exist: `gnus-extract-address-components',
532 which is the default, quite fast, and too simplistic solution, and
533 `mail-extract-address-components', which works much better, but is
534 slower.")
535
536 (defvar gnus-summary-default-score 0
537   "*Default article score level.
538 If this variable is nil, scoring will be disabled.")
539
540 (defvar gnus-summary-zcore-fuzz 0
541   "*Fuzziness factor for the zcore in the summary buffer.
542 Articles with scores closer than this to `gnus-summary-default-score'
543 will not be marked.")
544
545 (defvar gnus-simplify-subject-fuzzy-regexp nil
546   "*Regular expression that will be removed from subject strings if
547 fuzzy subject simplification is selected.")
548
549 (defvar gnus-group-default-list-level gnus-level-subscribed
550   "*Default listing level. 
551 Ignored if `gnus-group-use-permanent-levels' is nil.")
552
553 (defvar gnus-group-use-permanent-levels nil
554   "*If non-nil, once you set a level, Gnus will use this level.")
555
556 (defvar gnus-show-mime nil
557   "*If non-ni, do mime processing of articles.
558 The articles will simply be fed to the function given by
559 `gnus-show-mime-method'.")
560
561 (defvar gnus-strict-mime t
562   "*If nil, decode MIME header even if there is not Mime-Version field.")
563  
564 (defvar gnus-show-mime-method (function metamail-buffer)
565   "*Function to process a MIME message.
566 The function is called from the article buffer.")
567
568 (defvar gnus-show-threads t
569   "*If non-nil, display threads in summary mode.")
570
571 (defvar gnus-thread-hide-subtree nil
572   "*If non-nil, hide all threads initially.
573 If threads are hidden, you have to run the command
574 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
575 to expose hidden threads.")
576
577 (defvar gnus-thread-hide-killed t
578   "*If non-nil, hide killed threads automatically.")
579
580 (defvar gnus-thread-ignore-subject nil
581   "*If non-nil, ignore subjects and do all threading based on the Reference header.
582 If nil, which is the default, articles that have different subjects
583 from their parents will start separate threads.")
584
585 (defvar gnus-thread-indent-level 4
586   "*Number that says how much each sub-thread should be indented.")
587
588 (defvar gnus-ignored-newsgroups ""
589   "*A regexp to match uninteresting newsgroups in the active file.
590 Any lines in the active file matching this regular expression are
591 removed from the newsgroup list before anything else is done to it,
592 thus making them effectively non-existent.")
593
594 (defvar gnus-ignored-headers
595   "^Path:\\|^Posting-Version:\\|^Article-I.D.:\\|^Expires:\\|^Date-Received:\\|^References:\\|^Control:\\|^Xref:\\|^Lines:\\|^Posted:\\|^Relay-Version:\\|^Message-ID:\\|^Nf-ID:\\|^Nf-From:\\|^Approved:\\|^Sender:\\|^Received:\\|^Mail-from:"
596   "*All headers that match this regexp will be hidden.
597 Also see `gnus-visible-headers'.")
598
599 (defvar gnus-visible-headers "^From:\\|^Newsgroups:\\|^Subject:\\|^Date:\\|^Followup-To:\\|^Reply-To:\\|^Organization:\\|^Summary:\\|^Keywords:\\|^To:\\|^Cc:"
600   "*All headers that do not match this regexp will be hidden.
601 Also see `gnus-ignored-headers'.")
602
603 (defvar gnus-sorted-header-list
604   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:" "^To:" 
605     "^Cc:" "^Date:" "^Organization:")
606   "*This variable is a list of regular expressions.
607 If it is non-nil, headers that match the regular expressions will
608 be placed first in the article buffer in the sequence specified by
609 this list.")
610
611 (defvar gnus-show-all-headers nil
612   "*If non-nil, don't hide any headers.")
613
614 (defvar gnus-save-all-headers t
615   "*If non-nil, don't remove any headers before saving.")
616
617 (defvar gnus-inhibit-startup-message nil
618   "*If non-nil, the startup message will not be displayed.")
619
620 (defvar gnus-signature-separator "^-- *$"
621   "Regexp matching signature separator.")
622
623 (defvar gnus-auto-extend-newsgroup t
624   "*If non-nil, extend newsgroup forward and backward when requested.")
625
626 (defvar gnus-auto-select-first t
627   "*If non-nil, select the first unread article when entering a group.
628 If you want to prevent automatic selection of the first unread article
629 in some newsgroups, set the variable to nil in
630 `gnus-select-group-hook'.") 
631
632 (defvar gnus-auto-select-next t
633   "*If non-nil, offer to go to the next group from the end of the previous.
634 If the value is t and the next newsgroup is empty, Gnus will exit
635 summary mode and go back to group mode.  If the value is neither nil
636 nor t, Gnus will select the following unread newsgroup.  In
637 particular, if the value is the symbol `quietly', the next unread
638 newsgroup will be selected without any confirmations.")
639
640 (defvar gnus-auto-select-same nil
641   "*If non-nil, select the next article with the same subject.")
642
643 (defvar gnus-summary-check-current nil
644   "*If non-nil, consider the current article when moving.
645 The \"unread\" movement commands will stay on the same line if the
646 current article is unread.")
647
648 (defvar gnus-auto-center-summary t
649   "*If non-nil, always center the current summary buffer.")
650
651 (defvar gnus-break-pages t
652   "*If non-nil, do page breaking on articles.
653 The page delimiter is specified by the `gnus-page-delimiter'
654 variable.")
655
656 (defvar gnus-page-delimiter "^\^L"
657   "*Regexp describing what to use as article page delimiters.
658 The default value is \"^\^L\", which is a form linefeed at the
659 beginning of a line.")
660
661 (defvar gnus-use-full-window t
662   "*If non-nil, use the entire Emacs screen.")
663
664 (defvar gnus-window-configuration nil
665   "Obsolete variable.  See `gnus-buffer-configuration'.")
666
667 (defvar gnus-buffer-configuration
668   '((group ([group 1.0 point] 
669             (if gnus-carpal [group-carpal 4])))
670     (summary ([summary 1.0 point]
671               (if gnus-carpal [summary-carpal 4])))
672     (article ([summary 0.25 point] 
673               (if gnus-carpal [summary-carpal 4]) 
674               [article 1.0]))
675     (server ([server 1.0 point]
676              (if gnus-carpal [server-carpal 2])))
677     (browse ([browse 1.0 point]
678              (if gnus-carpal [browse-carpal 2])))
679     (group-mail ([mail 1.0 point]))
680     (summary-mail ([mail 1.0 point]))
681     (summary-reply ([article 0.5]
682                     [mail 1.0 point]))
683     (info ([nil 1.0 point]))
684     (summary-faq ([summary 0.25]
685                   [article 1.0 point]))
686     (edit-group ([group 0.5]
687                  [edit-group 1.0 point]))
688     (edit-server ([server 0.5]
689                   [edit-server 1.0 point]))
690     (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   "Mapping from short symbols to buffer names or buffer variables.")
732
733 (defvar gnus-carpal nil
734   "*If non-nil, display clickable icons.")
735
736 (defvar gnus-subscribe-newsgroup-method 'gnus-subscribe-zombies
737   "*Function called with a group name when new group is detected.
738 A few pre-made functions are supplied: `gnus-subscribe-randomly'
739 inserts new groups at the beginning of the list of groups;
740 `gnus-subscribe-alphabetically' inserts new groups in strict
741 alphabetic order; `gnus-subscribe-hierarchically' inserts new groups
742 in hierarchical newsgroup order; `gnus-subscribe-interactively' asks
743 for your decision.")
744
745 ;; Suggested by a bug report by Hallvard B Furuseth.
746 ;; <h.b.furuseth@usit.uio.no>. 
747 (defvar gnus-subscribe-options-newsgroup-method
748   (function gnus-subscribe-alphabetically)
749   "*This function is called to subscribe newsgroups mentioned on \"options -n\" lines.
750 If, for instance, you want to subscribe to all newsgroups in the
751 \"no\" and \"alt\" hierarchies, you'd put the following in your
752 .newsrc file:
753
754 options -n no.all alt.all
755
756 Gnus will the subscribe all new newsgroups in these hierarchies with
757 the subscription method in this variable.")
758
759 (defvar gnus-subscribe-hierarchical-interactive nil
760   "*If non-nil, Gnus will offer to subscribe hierarchically.
761 When a new hierarchy appears, Gnus will ask the user:
762
763 'alt.binaries': Do you want to subscribe to this hierarchy? ([d]ys):
764
765 If the user pressed `d', Gnus will descend the hierarchy, `y' will
766 subscribe to all newsgroups in the hierarchy and `s' will skip this
767 hierarchy in its entirety.")
768
769 (defvar gnus-group-sort-function 'gnus-group-sort-by-alphabet
770   "*Function used for sorting the group buffer.
771 This function will be called with group info entries as the arguments
772 for the groups to be sorted.  Pre-made functions include
773 `gnus-sort-by-alphabet', `gnus-sort-by-unread' and
774 `gnus-sort-by-level'")
775
776 ;; Mark variables suggested by Thomas Michanek
777 ;; <Thomas.Michanek@telelogic.se>. 
778 (defvar gnus-unread-mark ? 
779   "*Mark used for unread articles.")
780 (defvar gnus-ticked-mark ?!
781   "*Mark used for ticked articles.")
782 (defvar gnus-dormant-mark ??
783   "*Mark used for dormant articles.")
784 (defvar gnus-del-mark ?D
785   "*Mark used for del'd articles.")
786 (defvar gnus-read-mark ?d
787   "*Mark used for read articles.")
788 (defvar gnus-expirable-mark ?E
789   "*Mark used for expirable articles.")
790 (defvar gnus-killed-mark ?K
791   "*Mark used for killed articles.")
792 (defvar gnus-kill-file-mark ?X
793   "*Mark used for articles killed by kill files.")
794 (defvar gnus-low-score-mark ?Y
795   "*Mark used for articles with a low score.")
796 (defvar gnus-catchup-mark ?C
797   "*Mark used for articles that are caught up.")
798 (defvar gnus-replied-mark ?R
799   "*Mark used for articles that have been replied to.")
800 (defvar gnus-process-mark ?# 
801   "*Process mark.")
802 (defvar gnus-ancient-mark ?A
803   "*Mark used for ancient articles.")
804 (defvar gnus-canceled-mark ?G
805   "*Mark used for canceled articles.")
806 (defvar gnus-score-over-mark ?+
807   "*Score mark used for articles with high scores.")
808 (defvar gnus-score-below-mark ?-
809   "*Score mark used for articles with low scores.")
810 (defvar gnus-empty-thread-mark ? 
811   "*There is no thread under the article.")
812 (defvar gnus-not-empty-thread-mark ?=
813   "*There is a thread under the article.")
814 (defvar gnus-dummy-mark ?Z
815   "*This is a dummy article.")
816
817 (defvar gnus-view-pseudo-asynchronously nil
818   "*If non-nil, Gnus will view pseudo-articles asynchronously.")
819
820 (defvar gnus-view-pseudos nil
821   "*If `automatic', pseudo-articles will be viewed automatically.
822 If `not-confirm', pseudos will be viewed automatically, and the user
823 will not be asked to confirm the command.")
824
825 (defvar gnus-view-pseudos-separately t
826   "*If non-nil, one pseudo-article will be created for each file to be viewed.
827 If nil, all files that use the same viewing command will be given as a
828 list of parameters to that command.")
829
830 (defvar gnus-group-line-format "%M%S%p%5y: %(%g%)\n"
831   "*Format of group lines.
832 It works along the same lines as a normal formatting string,
833 with some simple extensions.
834
835 %M    Only marked articles (character, \"*\" or \" \")
836 %S    Whether the group is subscribed (character, \"U\", \"K\", \"Z\" or \" \")
837 %L    Level of subscribedness (integer)
838 %N    Number of unread articles (integer)
839 %I    Number of dormant articles (integer)
840 %i    Number of ticked and dormant (integer)
841 %T    Number of ticked articles (integer)
842 %R    Number of read articles (integer)
843 %t    Total number of articles (integer)
844 %y    Number of unread, unticked articles (integer)
845 %G    Group name (string)
846 %g    Qualified group name (string)
847 %D    Group description (string)
848 %s    Select method (string)
849 %o    Moderated group (char, \"m\")
850 %p    Process mark (char)
851 %O    Moderated group (string, \"(m)\" or \"\")
852 %n    Select from where (string)
853 %z    A string that look like `<%s:%n>' if a foreign select method is used
854 %u    User defined specifier. The next character in the format string should
855       be a letter.  Gnus will call the function gnus-user-format-function-X,
856       where X is the letter following %u. The function will be passed the
857       current header as argument. The function should return a string, which
858       will be inserted into the buffer just like information from any other
859       group specifier.
860
861 Text between %( and %) will be highlighted with `gnus-mouse-face' when
862 the mouse point move inside the area.  There can only be one such area.
863
864 Note that this format specification is not always respected. For
865 reasons of efficiency, when listing killed groups, this specification
866 is ignored altogether. If the spec is changed considerably, your
867 output may end up looking strange when listing both alive and killed
868 groups.
869
870 If you use %o or %O, reading the active file will be slower and quite
871 a bit of extra memory will be used. %D will also worsen performance.
872 Also note that if you change the format specification to include any
873 of these specs, you must probably re-start Gnus to see them go into
874 effect.") 
875
876 (defvar gnus-summary-line-format "%U%R%z%I%(%[%4L: %-20,20n%]%) %s\n"
877   "*The format specification of the lines in the summary buffer.
878
879 It works along the same lines as a normal formatting string,
880 with some simple extensions.
881
882 %N   Article number, left padded with spaces (string)
883 %S   Subject (string)
884 %s   Subject if it is at the root of a thread, and \"\" otherwise (string)
885 %n   Name of the poster (string)
886 %A   Address of the poster (string)
887 %F   Contents of the From: header (string)
888 %x   Contents of the Xref: header (string)
889 %D   Date of the article (string)
890 %d   Date of the article (string) in DD-MMM format
891 %M   Message-id of the article (string)
892 %r   References of the article (string)
893 %c   Number of characters in the article (integer)
894 %L   Number of lines in the article (integer)
895 %I   Indentation based on thread level (a string of spaces)
896 %T   A string with two possible values: 80 spaces if the article
897      is on thread level two or larger and 0 spaces on level one
898 %R   \"R\" if this article has been replied to, \" \" otherwise (character)
899 %U   Status of this article (character, \"D\", \"K\", \"-\" or \" \")
900 %[   Opening bracket (character, \"[\" or \"<\")
901 %]   Closing bracket (character, \"]\" or \">\")
902 %>   Spaces of length thread-level (string)
903 %<   Spaces of length (- 20 thread-level) (string)
904 %i   Article score (number)
905 %z   Article zcore (character)
906 %t   Number of articles under the current thread (number).
907 %e   Whether the thread is empty or not (character).
908 %u   User defined specifier. The next character in the format string should
909      be a letter.  Gnus will call the function gnus-user-format-function-X,
910      where X is the letter following %u. The function will be passed the
911      current header as argument. The function should return a string, which
912      will be inserted into the summary just like information from any other
913      summary specifier.
914
915 Text between %( and %) will be highlighted with `gnus-mouse-face'
916 when the mouse point is placed inside the area.  There can only be one
917 such area.
918
919 The %U (status), %R (replied) and %z (zcore) specs have to be handled
920 with care. For reasons of efficiency, Gnus will compute what column
921 these characters will end up in, and \"hard-code\" that. This means that
922 it is illegal to have these specs after a variable-length spec. Well,
923 you might not be arrested, but your summary buffer will look strange,
924 which is bad enough.
925
926 The smart choice is to have these specs as for to the left as
927 possible. 
928
929 This restriction may disappear in later versions of Gnus.")
930
931 (defvar gnus-summary-dummy-line-format "*  :                          : %S\n"
932   "*The format specification for the dummy roots in the summary buffer.
933 It works along the same lines as a normal formatting string,
934 with some simple extensions.
935
936 %S  The subject")
937
938 (defvar gnus-summary-mode-line-format "(ding) %G/%A %Z"
939   "*The format specification for the summary mode line.")
940
941 (defvar gnus-article-mode-line-format "(ding) %G/%A %S"
942   "*The format specification for the article mode line.")
943
944 (defvar gnus-group-mode-line-format "(ding) List of groups   {%M:%S}  "
945   "*The format specification for the group mode line.")
946
947 (defvar gnus-valid-select-methods
948   '(("nntp" post address prompt-address)
949     ("nnspool" post)
950     ("nnvirtual" none virtual prompt-address) 
951     ("nnmbox" mail respool) 
952     ("nnml" mail respool)
953     ("nnmh" mail respool) 
954     ("nndir" none prompt-address address)
955     ("nneething" none prompt-address)
956     ("nndigest" none) 
957     ("nndoc" none prompt-address) 
958     ("nnbabyl" mail respool) 
959     ("nnkiboze" post virtual) 
960     ("nnsoup" post)
961     ("nnfolder" mail respool))
962   "An alist of valid select methods.
963 The first element of each list lists should be a string with the name
964 of the select method. The other elements may be be the category of
965 this method (ie. `post', `mail', `none' or whatever) or other
966 properties that this method has (like being respoolable).
967 If you implement a new select method, all you should have to change is
968 this variable. I think.")
969
970 (defvar gnus-updated-mode-lines '(group article summary)
971   "*List of buffers that should update their mode lines.
972 The list may contain the symbols `group', `article' and `summary'. If
973 the corresponding symbol is present, Gnus will keep that mode line
974 updated with information that may be pertinent. 
975 If this variable is nil, screen refresh may be quicker.")
976
977 ;; Added by Keinonen Kari <kk85613@cs.tut.fi>.
978 (defvar gnus-mode-non-string-length 21
979   "*Max length of mode-line non-string contents.
980 If this is nil, Gnus will take space as is needed, leaving the rest
981 of the modeline intact.")
982
983 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
984 (defvar gnus-mouse-face 'highlight
985   "*Face used for mouse highlighting in Gnus.
986 No mouse highlights will be done if `gnus-visual' is nil.")
987
988 (defvar gnus-summary-mark-below nil
989   "*Mark all articles with a score below this variable as read.
990 This variable is local to each summary buffer and usually set by the
991 score file.")  
992
993 (defvar gnus-thread-sort-functions '(gnus-thread-sort-by-number)
994   "*List of functions used for sorting threads in the summary buffer.
995 By default, threads are sorted by article number.
996
997 Each function takes two threads and return non-nil if the first thread
998 should be sorted before the other.  If you use more than one function,
999 the primary sort function should be the last.
1000
1001 Ready-mady functions include `gnus-thread-sort-by-number',
1002 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
1003 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
1004 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').")
1005
1006 (defvar gnus-thread-score-function '+
1007   "*Function used for calculating the total score of a thread.
1008
1009 The function is called with the scores of the article and each
1010 subthread and should then return the score of the thread.
1011
1012 Some functions you can use are `+', `max', or `min'.")
1013
1014 (defvar gnus-options-subscribe nil
1015   "*All new groups matching this regexp will be subscribed unconditionally.
1016 Note that this variable deals only with new newsgroups.  This variable
1017 does not affect old newsgroups.")
1018
1019 (defvar gnus-options-not-subscribe nil
1020   "*All new groups matching this regexp will be ignored.
1021 Note that this variable deals only with new newsgroups.  This variable
1022 does not affect old (already subscribed) newsgroups.")
1023
1024 (defvar gnus-auto-expirable-newsgroups nil
1025   "*Groups in which to automatically mark read articles as expirable.
1026 If non-nil, this should be a regexp that should match all groups in
1027 which to perform auto-expiry.  This only makes sense for mail groups.")
1028
1029 (defvar gnus-hidden-properties '(invisible t intangible t)
1030   "Property list to use for hiding text.")
1031
1032 ;; Hooks.
1033
1034 (defvar gnus-group-mode-hook nil
1035   "*A hook for Gnus group mode.")
1036
1037 (defvar gnus-summary-mode-hook nil
1038   "*A hook for Gnus summary mode.
1039 This hook is run before any variables are set in the summary buffer.")
1040
1041 (defvar gnus-article-mode-hook nil
1042   "*A hook for Gnus article mode.")
1043
1044 (defvar gnus-open-server-hook nil
1045   "*A hook called just before opening connection to the news server.")
1046
1047 (defvar gnus-startup-hook nil
1048   "*A hook called at startup.
1049 This hook is called after Gnus is connected to the NNTP server.")
1050
1051 (defvar gnus-get-new-news-hook nil
1052   "*A hook run just before Gnus checks for new news.")
1053
1054 (defvar gnus-group-prepare-function 'gnus-group-prepare-flat
1055   "*A function that is called to generate the group buffer.
1056 The function is called with three arguments: The first is a number;
1057 all group with a level less or equal to that number should be listed,
1058 if the second is non-nil, empty groups should also be displayed. If
1059 the third is non-nil, it is a number. No groups with a level lower
1060 than this number should be displayed.
1061
1062 The only current function implemented is `gnus-group-prepare-flat'.")
1063
1064 (defvar gnus-group-prepare-hook nil
1065   "*A hook called after the group buffer has been generated.
1066 If you want to modify the group buffer, you can use this hook.")
1067
1068 (defvar gnus-summary-prepare-hook nil
1069   "*A hook called after the summary buffer has been generated.
1070 If you want to modify the summary buffer, you can use this hook.")
1071
1072 (defvar gnus-article-prepare-hook nil
1073   "*A hook called after an article has been prepared in the article buffer.
1074 If you want to run a special decoding program like nkf, use this hook.")
1075
1076 (defvar gnus-article-display-hook nil
1077   "*A hook called after the article is displayed in the article buffer.
1078 The hook is designed to change the contents of the article
1079 buffer. Typical functions that this hook may contain are
1080 `gnus-article-hide-headers' (hide selected headers),
1081 `gnus-article-maybe-highlight' (perform fancy article highlighting), 
1082 `gnus-article-hide-signature' (hide signature) and
1083 `gnus-article-treat-overstrike' (turn \"^H_\" into bold characters).")
1084 (add-hook 'gnus-article-display-hook 'gnus-article-hide-headers-if-wanted)
1085 (add-hook 'gnus-article-display-hook 'gnus-article-treat-overstrike)
1086
1087 (defvar gnus-article-x-face-command
1088   "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | xv -quit -"
1089   "String or function to be executed to display an X-Face header.
1090 If it is a string, the command will be executed in a sub-shell
1091 asynchronously. The compressed face will be piped to this command.") 
1092
1093 (defvar gnus-article-x-face-too-ugly nil
1094   "Regexp matching posters whose face shouldn't be shown automatically.")
1095
1096 (defvar gnus-select-group-hook nil
1097   "*A hook called when a newsgroup is selected.
1098
1099 If you'd like to simplify subjects like the
1100 `gnus-summary-next-same-subject' command does, you can use the
1101 following hook:
1102
1103  (setq gnus-select-group-hook
1104       (list
1105         (lambda ()
1106           (mapcar (lambda (header)
1107                      (header-set-subject
1108                       header
1109                       (gnus-simplify-subject
1110                        (header-subject header) 're-only)))
1111                   gnus-newsgroup-headers))))")
1112
1113 (defvar gnus-select-article-hook
1114   '(gnus-summary-show-thread)
1115   "*A hook called when an article is selected.
1116 The default hook shows conversation thread subtrees of the selected
1117 article automatically using `gnus-summary-show-thread'.")
1118
1119 (defvar gnus-apply-kill-hook '(gnus-apply-kill-file)
1120   "*A hook called to apply kill files to a group.
1121 This hook is intended to apply a kill file to the selected newsgroup.
1122 The function `gnus-apply-kill-file' is called by default.
1123
1124 Since a general kill file is too heavy to use only for a few
1125 newsgroups, I recommend you to use a lighter hook function. For
1126 example, if you'd like to apply a kill file to articles which contains
1127 a string `rmgroup' in subject in newsgroup `control', you can use the
1128 following hook:
1129
1130 \(setq gnus-apply-kill-hook
1131       (list
1132         (lambda ()
1133           (cond ((string-match \"control\" gnus-newsgroup-name)
1134                  (gnus-kill \"Subject\" \"rmgroup\")
1135                  (gnus-expunge \"X\"))))))")
1136
1137 (defvar gnus-visual-mark-article-hook 
1138   (list 'gnus-highlight-selected-summary)
1139   "*Hook run after selecting an article in the summary buffer.
1140 It is meant to be used for highlighting the article in some way.  It
1141 is not run if `gnus-visual' is nil.")
1142
1143 (defvar gnus-prepare-article-hook (list 'gnus-inews-insert-signature)
1144   "*A hook called after preparing body, but before preparing header headers.
1145 The default hook (`gnus-inews-insert-signature') inserts a signature
1146 file specified by the variable `gnus-signature-file'.")
1147
1148 (defvar gnus-exit-group-hook nil
1149   "*A hook called when exiting (not quitting) summary mode.")
1150
1151 (defvar gnus-suspend-gnus-hook nil
1152   "*A hook called when suspending (not exiting) Gnus.")
1153
1154 (defvar gnus-exit-gnus-hook nil
1155   "*A hook called when exiting Gnus.")
1156
1157 (defvar gnus-save-newsrc-hook nil
1158   "*A hook called when saving the newsrc file.")
1159
1160 (defvar gnus-summary-update-hook 
1161   (list 'gnus-summary-highlight-line)
1162   "*A hook called when a summary line is changed.
1163 The hook will not be called if `gnus-visual' is nil.
1164
1165 The default function `gnus-summary-highlight-line' will
1166 highlight the line according to the `gnus-summary-highlight'
1167 variable.")
1168
1169 (defvar gnus-mark-article-hook (list 'gnus-summary-mark-unread-as-read)
1170   "*A hook called when an article is selected for the first time.
1171 The hook is intended to mark an article as read (or unread)
1172 automatically when it is selected.")
1173
1174 \f
1175 ;; Internal variables
1176
1177 ;; Avoid highlighting in kill files.
1178 (defvar gnus-summary-inhibit-highlight nil)
1179 (defvar gnus-newsgroup-selected-overlay nil)
1180
1181 (defvar gnus-article-mode-map nil)
1182 (defvar caesar-translate-table nil)
1183 (defvar gnus-dribble-buffer nil)
1184 (defvar gnus-headers-retrieved-by nil)
1185 (defvar gnus-article-reply nil)
1186 (defvar gnus-override-method nil)
1187 (defvar gnus-article-check-size nil)
1188
1189 (defvar gnus-current-score-file nil)
1190 (defvar gnus-internal-global-score-files nil)
1191 (defvar gnus-score-file-list nil)
1192
1193
1194 (defvar gnus-current-move-group nil)
1195
1196 (defvar gnus-newsgroup-dependencies nil)
1197 (defvar gnus-newsgroup-threads nil)
1198 (defvar gnus-newsgroup-async nil)
1199 (defconst gnus-group-edit-buffer "*Gnus edit newsgroup*")
1200
1201 (defvar gnus-newsgroup-adaptive nil)
1202
1203 (defvar gnus-summary-display-table nil)
1204
1205 (defconst gnus-group-line-format-alist
1206   (list (list ?M 'marked ?c)
1207         (list ?S 'subscribed ?c)
1208         (list ?L 'level ?d)
1209         (list ?N 'number ?s)
1210         (list ?I 'number-of-dormant ?d)
1211         (list ?T 'number-of-ticked ?d)
1212         (list ?R 'number-of-read ?s)
1213         (list ?t 'number-total ?d)
1214         (list ?y 'number-of-unread-unticked ?s)
1215         (list ?i 'number-of-ticked-and-dormant ?d)
1216         (list ?g 'group ?s)
1217         (list ?G 'qualified-group ?s)
1218         (list ?D 'newsgroup-description ?s)
1219         (list ?o 'moderated ?c)
1220         (list ?O 'moderated-string ?s)
1221         (list ?p 'process-marked ?c)
1222         (list ?s 'news-server ?s)
1223         (list ?n 'news-method ?s)
1224         (list ?z 'news-method-string ?s)
1225         (list ?u 'user-defined ?s)))
1226
1227 (defconst gnus-summary-line-format-alist 
1228   (list (list ?N 'number ?d)
1229         (list ?S 'subject ?s)
1230         (list ?s 'subject-or-nil ?s)
1231         (list ?n 'name ?s)
1232         (list ?A 'address ?s)
1233         (list ?F 'from ?s)
1234         (list ?x (macroexpand '(header-xref header)) ?s)
1235         (list ?D (macroexpand '(header-date header)) ?s)
1236         (list ?d '(gnus-dd-mmm (header-date header)) ?s)
1237         (list ?M (macroexpand '(header-id header)) ?s)
1238         (list ?r (macroexpand '(header-references header)) ?s)
1239         (list ?c '(or (header-chars header) 0) ?d)
1240         (list ?L 'lines ?d)
1241         (list ?I 'indentation ?s)
1242         (list ?T '(if (= level 0) "" (make-string (frame-width) ? )) ?s)
1243         (list ?R 'replied ?c)
1244         (list ?\[ 'opening-bracket ?c)
1245         (list ?\] 'closing-bracket ?c)
1246         (list ?\> '(make-string level ? ) ?s)
1247         (list ?\< '(make-string (max 0 (- 20 level)) ? ) ?s)
1248         (list ?i 'score ?d)
1249         (list ?z 'score-char ?c)
1250         (list ?U 'unread ?c)
1251         (list ?t '(gnus-summary-number-of-articles-in-thread 
1252                    (or (prog1 gnus-tmp-adopt-thread 
1253                          (setq gnus-tmp-adopt-thread nil))
1254                        (if (boundp 'thread) (symbol-value 'thread)
1255                          thread nil)))
1256                    ?d)
1257         (list ?e '(gnus-summary-number-of-articles-in-thread 
1258                    (or gnus-tmp-adopt-thread 
1259                        (if (boundp 'thread) (symbol-value 'thread)
1260                          thread nil)) t)
1261                    ?c)
1262         (list ?u 'user-defined ?s))
1263   "An alist of format specifications that can appear in summary lines,
1264 and what variables they correspond with, along with the type of the
1265 variable (string, integer, character, etc).")
1266
1267 (defconst gnus-summary-dummy-line-format-alist
1268   (list (list ?S 'subject ?s)
1269         (list ?N 'number ?d)
1270         (list ?u 'user-defined ?s)))
1271
1272 (defconst gnus-summary-mode-line-format-alist 
1273   (list (list ?G 'group-name ?s)
1274         (list ?g '(gnus-short-group-name group-name) ?s)
1275         (list ?A 'article-number ?d)
1276         (list ?Z 'unread-and-unselected ?s)
1277         (list ?V 'gnus-version ?s)
1278         (list ?U 'unread ?d)
1279         (list ?S 'subject ?s)
1280         (list ?e 'unselected ?d)
1281         (list ?u 'user-defined ?s)
1282         (list ?s '(gnus-current-score-file-nondirectory) ?s)))
1283
1284 (defconst gnus-group-mode-line-format-alist 
1285   (list (list ?S 'news-server ?s)
1286         (list ?M 'news-method ?s)
1287         (list ?u 'user-defined ?s)))
1288
1289 (defvar gnus-have-read-active-file nil)
1290
1291 (defconst gnus-maintainer "gnus-bug@ifi.uio.no (The Gnus Bugfixing Girls & Boys)"
1292   "The mail address of the Gnus maintainers.")
1293
1294 (defconst gnus-version "(ding) Gnus v0.90"
1295   "Version number for this version of Gnus.")
1296
1297 (defvar gnus-info-nodes
1298   '((gnus-group-mode            "(gnus)The Group Buffer")
1299     (gnus-summary-mode          "(gnus)The Summary Buffer")
1300     (gnus-article-mode          "(gnus)The Article Buffer"))
1301   "Assoc list of major modes and related Info nodes.")
1302
1303 (defvar gnus-documentation-group-file "~/dgnus/lisp/doc.txt"
1304   "The location of the (ding) Gnus documentation group.")
1305
1306 (defvar gnus-group-buffer "*Group*")
1307 (defvar gnus-summary-buffer "*Summary*")
1308 (defvar gnus-article-buffer "*Article*")
1309 (defvar gnus-server-buffer "*Server*")
1310
1311 (defvar gnus-work-buffer " *gnus work*")
1312
1313 (defvar gnus-buffer-list nil
1314   "Gnus buffers that should be killed on exit.")
1315
1316 (defvar gnus-server-alist nil
1317   "List of available servers.")
1318
1319 (defvar gnus-variable-list
1320   '(gnus-newsrc-options gnus-newsrc-options-n
1321     gnus-newsrc-last-checked-date 
1322     gnus-newsrc-alist gnus-server-alist
1323     gnus-killed-list gnus-zombie-list)
1324   "Gnus variables saved in the quick startup file.")
1325
1326 (defvar gnus-overload-functions
1327   '((news-inews gnus-inews-news "rnewspost")
1328     (caesar-region gnus-caesar-region "rnews"))
1329   "Functions overloaded by gnus.
1330 It is a list of `(original overload &optional file)'.")
1331
1332 (defvar gnus-newsrc-options nil
1333   "Options line in the .newsrc file.")
1334
1335 (defvar gnus-newsrc-options-n nil
1336   "List of regexps representing groups to be subscribed/ignored unconditionally.") 
1337
1338 (defvar gnus-newsrc-last-checked-date nil
1339   "Date Gnus last asked server for new newsgroups.")
1340
1341 (defvar gnus-newsrc-alist nil
1342   "Assoc list of read articles.
1343 gnus-newsrc-hashtb should be kept so that both hold the same information.")
1344
1345 (defvar gnus-newsrc-hashtb nil
1346   "Hashtable of gnus-newsrc-alist.")
1347
1348 (defvar gnus-killed-list nil
1349   "List of killed newsgroups.")
1350
1351 (defvar gnus-killed-hashtb nil
1352   "Hash table equivalent of gnus-killed-list.")
1353
1354 (defvar gnus-zombie-list nil
1355   "List of almost dead newsgroups.")
1356
1357 (defvar gnus-description-hashtb nil
1358   "Descriptions of newsgroups.")
1359
1360 (defvar gnus-list-of-killed-groups nil
1361   "List of newsgroups that have recently been killed by the user.")
1362
1363 (defvar gnus-active-hashtb nil
1364   "Hashtable of active articles.")
1365
1366 (defvar gnus-moderated-list nil
1367   "List of moderated newsgroups.")
1368
1369 (defvar gnus-group-marked nil)
1370
1371 (defvar gnus-current-startup-file nil
1372   "Startup file for the current host.")
1373
1374 (defvar gnus-last-search-regexp nil
1375   "Default regexp for article search command.")
1376
1377 (defvar gnus-last-shell-command nil
1378   "Default shell command on article.")
1379
1380 (defvar gnus-current-select-method nil
1381   "The current method for selecting a newsgroup.")
1382
1383 (defvar gnus-have-all-newsgroups nil)
1384
1385 (defvar gnus-article-internal-prepare-hook nil)
1386
1387 (defvar gnus-newsgroup-name nil)
1388 (defvar gnus-newsgroup-begin nil)
1389 (defvar gnus-newsgroup-end nil)
1390 (defvar gnus-newsgroup-last-rmail nil)
1391 (defvar gnus-newsgroup-last-mail nil)
1392 (defvar gnus-newsgroup-last-folder nil)
1393 (defvar gnus-newsgroup-last-file nil)
1394 (defvar gnus-newsgroup-auto-expire nil)
1395 (defvar gnus-newsgroup-active nil)
1396
1397 (defvar gnus-newsgroup-unreads nil
1398   "List of unread articles in the current newsgroup.")
1399
1400 (defvar gnus-newsgroup-unselected nil
1401   "List of unselected unread articles in the current newsgroup.")
1402
1403 (defvar gnus-newsgroup-marked nil
1404   "List of ticked articles in the current newsgroup (a subset of unread art).")
1405
1406 (defvar gnus-newsgroup-killed nil
1407   "List of ranges of articles that have been through the scoring process.")
1408
1409 (defvar gnus-newsgroup-kill-headers nil)
1410
1411 (defvar gnus-newsgroup-replied nil
1412   "List of articles that have been replied to in the current newsgroup.")
1413
1414 (defvar gnus-newsgroup-expirable nil
1415   "List of articles in the current newsgroup that can be expired.")
1416
1417 (defvar gnus-newsgroup-processable nil
1418   "List of articles in the current newsgroup that can be processed.")
1419
1420 (defvar gnus-newsgroup-bookmarks nil
1421   "List of articles in the current newsgroup that have bookmarks.")
1422
1423 (defvar gnus-newsgroup-dormant nil
1424   "List of dormant articles in the current newsgroup.")
1425
1426 (defvar gnus-newsgroup-scored nil
1427   "List of scored articles in the current newsgroup.")
1428
1429 (defvar gnus-newsgroup-headers nil
1430   "List of article headers in the current newsgroup.")
1431 (defvar gnus-newsgroup-headers-hashtb-by-number nil)
1432
1433 (defvar gnus-newsgroup-ancient nil
1434   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1435
1436 (defvar gnus-current-article nil)
1437 (defvar gnus-article-current nil)
1438 (defvar gnus-current-headers nil)
1439 (defvar gnus-have-all-headers nil)
1440 (defvar gnus-last-article nil)
1441 (defvar gnus-newsgroup-history nil)
1442 (defvar gnus-current-kill-article nil)
1443
1444 ;; Save window configuration.
1445 (defvar gnus-prev-winconf nil)
1446
1447 ;; Format specs
1448 (defvar gnus-summary-line-format-spec nil)
1449 (defvar gnus-summary-dummy-line-format-spec nil)
1450 (defvar gnus-group-line-format-spec nil)
1451 (defvar gnus-summary-mode-line-format-spec nil)
1452 (defvar gnus-article-mode-line-format-spec nil)
1453 (defvar gnus-group-mode-line-format-spec nil)
1454 (defvar gnus-summary-mark-positions nil)
1455
1456 (defvar gnus-summary-expunge-below nil)
1457 (defvar gnus-reffed-article-number nil)
1458
1459 (defvar rmail-default-file (expand-file-name "~/XMBOX"))
1460 (defvar rmail-default-rmail-file (expand-file-name "~/XNEWS"))
1461
1462 (defconst gnus-summary-local-variables 
1463   '(gnus-newsgroup-name 
1464     gnus-newsgroup-begin gnus-newsgroup-end 
1465     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail 
1466     gnus-newsgroup-last-folder gnus-newsgroup-last-file 
1467     gnus-newsgroup-auto-expire gnus-newsgroup-unreads 
1468     gnus-newsgroup-unselected gnus-newsgroup-marked
1469     gnus-newsgroup-replied gnus-newsgroup-expirable
1470     gnus-newsgroup-processable gnus-newsgroup-killed
1471     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1472     gnus-newsgroup-headers gnus-newsgroup-headers-hashtb-by-number
1473     gnus-current-article gnus-current-headers gnus-have-all-headers
1474     gnus-last-article gnus-article-internal-prepare-hook
1475     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1476     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1477     gnus-newsgroup-threads gnus-newsgroup-async
1478     gnus-score-alist gnus-current-score-file gnus-summary-expunge-below 
1479     gnus-summary-mark-below gnus-newsgroup-active gnus-scores-exclude-files
1480     gnus-newsgroup-history gnus-newsgroup-ancient
1481     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring))
1482   "Variables that are buffer-local to the summary buffers.")
1483
1484 (defconst gnus-bug-message
1485   "Sending a bug report to the Gnus Towers.
1486 ========================================
1487
1488 The buffer below is a mail buffer.  When you press `C-c C-c', it will
1489 be sent to the Gnus Bug Exterminators. 
1490
1491 At the bottom of the buffer you'll see lots of variable settings.
1492 Please do not delete those.  They will tell the Bug People what your
1493 environment is, so that it will be easier to locate the bugs.
1494
1495 If you have found a bug that makes Emacs go \"beep\", set
1496 debug-on-error to t (`M-x set-variable RET debug-on-error RET t RET') 
1497 and include the backtrace in your bug report.
1498
1499 Please describe the bug in annoying, painstaking detail.
1500
1501 Thank you for your help in stamping out bugs.
1502 ")
1503
1504 ;;; End of variables.
1505
1506 ;; Define some autoload functions Gnus might use.
1507 (eval-and-compile
1508
1509   ;; Various 
1510   (autoload 'metamail-buffer "metamail")
1511   (autoload 'Info-goto-node "info")
1512   (autoload 'hexl-hex-string-to-integer "hexl")
1513   (autoload 'pp "pp")
1514   (autoload 'pp-to-string "pp")
1515   (autoload 'pp-eval-expression "pp")
1516   (autoload 'mail-extract-address-components "mail-extr")
1517
1518   (autoload 'nnmail-split-fancy "nnmail")
1519   (autoload 'nnvirtual-catchup-group "nnvirtual")
1520
1521   ;; timezone
1522   (autoload 'timezone-make-date-arpa-standard "timezone")
1523   (autoload 'timezone-fix-time "timezone")
1524   (autoload 'timezone-make-sortable-date "timezone")
1525   (autoload 'timezone-make-time-string "timezone")
1526
1527   ;; rmail & friends
1528   (autoload 'mail-position-on-field "sendmail")
1529   (autoload 'mail-setup "sendmail")
1530   (autoload 'rmail-output "rmailout")
1531   (autoload 'news-mail-other-window "rnewspost")
1532   (autoload 'news-reply-yank-original "rnewspost")
1533   (autoload 'news-caesar-buffer-body "rnewspost")
1534   (autoload 'rmail-insert-rmail-file-header "rmail")
1535   (autoload 'rmail-count-new-messages "rmail")
1536   (autoload 'rmail-show-message "rmail")
1537
1538   ;; gnus-soup
1539   (autoload 'gnus-group-brew-soup "gnus-soup" nil t)
1540   (autoload 'gnus-brew-soup "gnus-soup" nil t)
1541   (autoload 'gnus-soup-add-article "gnus-soup" nil t)
1542   (autoload 'gnus-soup-send-replies "gnus-soup" nil t)
1543   (autoload 'gnus-soup-save-areas "gnus-soup" nil t)
1544   (autoload 'gnus-soup-pack-packet "gnus-soup" nil t)
1545   (autoload 'nnsoup-pack-replies "nnsoup" nil t)
1546
1547   ;; gnus-mh
1548   (autoload 'gnus-mail-reply-using-mhe "gnus-mh")
1549   (autoload 'gnus-mail-forward-using-mhe "gnus-mh")
1550   (autoload 'gnus-mail-other-window-using-mhe "gnus-mh")
1551   (autoload 'gnus-summary-save-in-folder "gnus-mh")
1552   (autoload 'gnus-summary-save-article-folder "gnus-mh")
1553   (autoload 'gnus-Folder-save-name "gnus-mh")
1554   (autoload 'gnus-folder-save-name "gnus-mh")
1555
1556   ;; gnus-vis misc
1557   (autoload 'gnus-group-make-menu-bar "gnus-vis")
1558   (autoload 'gnus-summary-make-menu-bar "gnus-vis")
1559   (autoload 'gnus-server-make-menu-bar "gnus-vis")
1560   (autoload 'gnus-article-make-menu-bar "gnus-vis")
1561   (autoload 'gnus-browse-make-menu-bar "gnus-vis")
1562   (autoload 'gnus-highlight-selected-summary "gnus-vis")
1563   (autoload 'gnus-summary-highlight-line "gnus-vis")
1564   (autoload 'gnus-carpal-setup-buffer "gnus-vis")
1565
1566   ;; gnus-vis article
1567   (autoload 'gnus-article-push-button "gnus-vis" nil t)
1568   (autoload 'gnus-article-press-button "gnus-vis" nil t)
1569   (autoload 'gnus-article-highlight "gnus-vis" nil t)
1570   (autoload 'gnus-article-hide "gnus-vis" nil t)
1571   (autoload 'gnus-article-hide-signature "gnus-vis" nil t)
1572   (autoload 'gnus-article-highlight-headers "gnus-vis" nil t)
1573   (autoload 'gnus-article-highlight-signature "gnus-vis" nil t)
1574   (autoload 'gnus-article-add-buttons "gnus-vis" nil t)
1575   (autoload 'gnus-article-next-button "gnus-vis" nil t)
1576   (autoload 'gnus-article-add-button "gnus-vis")
1577
1578   ;; gnus-cite
1579   (autoload 'gnus-article-highlight-citation "gnus-cite" nil t)
1580   (autoload 'gnus-article-hide-citation-maybe "gnus-cite" nil t)
1581   (autoload 'gnus-article-hide-citation "gnus-cite" nil t)
1582
1583   ;; gnus-kill
1584   (autoload 'gnus-kill "gnus-kill")
1585   (autoload 'gnus-apply-kill-file-internal "gnus-kill")
1586   (autoload 'gnus-kill-file-edit-file "gnus-kill")
1587   (autoload 'gnus-kill-file-raise-followups-to-author "gnus-kill")
1588   (autoload 'gnus-execute "gnus-kill")
1589   (autoload 'gnus-expunge "gnus-kill")
1590
1591   ;; gnus-cache
1592   (autoload 'gnus-cache-possibly-enter-article "gnus-cache")
1593   (autoload 'gnus-cache-save-buffers "gnus-cache")
1594   (autoload 'gnus-cache-possibly-remove-articles "gnus-cache")
1595   (autoload 'gnus-cache-request-article "gnus-cache")
1596   (autoload 'gnus-cache-retrieve-headers "gnus-cache")
1597   (autoload 'gnus-cache-possibly-alter-active "gnus-cache")
1598   (autoload 'gnus-jog-cache "gnus-cache" nil t)
1599   (autoload 'gnus-cache-enter-remove-article "gnus-cache")
1600
1601   ;; gnus-score
1602   (autoload 'gnus-summary-increase-score "gnus-score" nil t)
1603   (autoload 'gnus-summary-lower-score "gnus-score" nil t)
1604   (autoload 'gnus-summary-score-map "gnus-score" nil nil 'keymap)
1605   (autoload 'gnus-score-save "gnus-score")
1606   (autoload 'gnus-score-headers "gnus-score")
1607   (autoload 'gnus-current-score-file-nondirectory "gnus-score")
1608   (autoload 'gnus-score-adaptive "gnus-score")
1609   (autoload 'gnus-score-remove-lines-adaptive "gnus-score")
1610   (autoload 'gnus-score-find-trace "gnus-score")
1611
1612   ;; gnus-edit
1613   (autoload 'gnus-score-customize "gnus-edit" nil t)
1614
1615   ;; gnus-uu
1616   (autoload 'gnus-uu-extract-map "gnus-uu" nil nil 'keymap)
1617   (autoload 'gnus-uu-mark-map "gnus-uu" nil nil 'keymap)
1618   (autoload 'gnus-uu-digest-mail-forward "gnus-uu" nil t)
1619   (autoload 'gnus-uu-digest-post-forward "gnus-uu" nil t)
1620   (autoload 'gnus-uu-mark-series "gnus-uu" nil t)
1621   (autoload 'gnus-uu-mark-region "gnus-uu" nil t)
1622   (autoload 'gnus-uu-mark-by-regexp "gnus-uu" nil t)
1623   (autoload 'gnus-uu-mark-all "gnus-uu" nil t)
1624   (autoload 'gnus-uu-mark-sparse "gnus-uu" nil t)
1625   (autoload 'gnus-uu-mark-thread "gnus-uu" nil t)
1626   (autoload 'gnus-uu-decode-uu "gnus-uu" nil t)
1627   (autoload 'gnus-uu-decode-uu-and-save "gnus-uu" nil t)
1628   (autoload 'gnus-uu-decode-unshar "gnus-uu" nil t)
1629   (autoload 'gnus-uu-decode-unshar-and-save "gnus-uu" nil t)
1630   (autoload 'gnus-uu-decode-save "gnus-uu" nil t)
1631   (autoload 'gnus-uu-decode-binhex "gnus-uu" nil t)
1632   (autoload 'gnus-uu-decode-uu-view "gnus-uu" nil t)
1633   (autoload 'gnus-uu-decode-uu-and-save-view "gnus-uu" nil t)
1634   (autoload 'gnus-uu-decode-unshar-view "gnus-uu" nil t)
1635   (autoload 'gnus-uu-decode-unshar-and-save-view "gnus-uu" nil t)
1636   (autoload 'gnus-uu-decode-save-view "gnus-uu" nil t)
1637   (autoload 'gnus-uu-decode-binhex-view "gnus-uu" nil t)
1638
1639   ;; gnus-msg
1640   (autoload 'gnus-summary-send-map "gnus-msg" nil nil 'keymap)
1641   (autoload 'gnus-group-post-news "gnus-msg" nil t)
1642   (autoload 'gnus-group-mail "gnus-msg" nil t)
1643   (autoload 'gnus-summary-post-news "gnus-msg" nil t)
1644   (autoload 'gnus-summary-followup "gnus-msg" nil t)
1645   (autoload 'gnus-summary-followup-with-original "gnus-msg" nil t)
1646   (autoload 'gnus-summary-followup-and-reply "gnus-msg" nil t)
1647   (autoload 'gnus-summary-followup-and-reply-with-original "gnus-msg" nil t)
1648   (autoload 'gnus-summary-cancel-article "gnus-msg" nil t)
1649   (autoload 'gnus-summary-supersede-article "gnus-msg" nil t)
1650   (autoload 'gnus-post-news "gnus-msg" nil t)
1651   (autoload 'gnus-inews-news "gnus-msg" nil t)
1652   (autoload 'gnus-cancel-news "gnus-msg" nil t)
1653   (autoload 'gnus-summary-reply "gnus-msg" nil t)
1654   (autoload 'gnus-summary-reply-with-original "gnus-msg" nil t)
1655   (autoload 'gnus-summary-mail-forward "gnus-msg" nil t)
1656   (autoload 'gnus-summary-mail-other-window "gnus-msg" nil t)
1657   (autoload 'gnus-mail-reply-using-mail "gnus-msg")
1658   (autoload 'gnus-mail-yank-original "gnus-msg")
1659   (autoload 'gnus-mail-send-and-exit "gnus-msg")
1660   (autoload 'gnus-mail-forward-using-mail "gnus-msg")
1661   (autoload 'gnus-mail-other-window-using-mail "gnus-msg")
1662   (autoload 'gnus-article-mail-with-original "gnus-msg")
1663   (autoload 'gnus-article-mail "gnus-msg")
1664   (autoload 'gnus-bug "gnus-msg" nil t)
1665
1666   ;; gnus-vm
1667   (autoload 'gnus-summary-save-in-vm "gnus-vm" nil t)
1668   (autoload 'gnus-summary-save-article-vm "gnus-vm" nil t)
1669   (autoload 'gnus-mail-forward-using-vm "gnus-vm")
1670   (autoload 'gnus-mail-reply-using-vm "gnus-vm")
1671   (autoload 'gnus-mail-other-window-using-vm "gnus-vm" nil t)
1672   (autoload 'gnus-yank-article "gnus-vm" nil t)
1673
1674   )
1675
1676 \f
1677
1678 ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
1679 ;; If you want the cursor to go somewhere else, set these two
1680 ;; functions in some startup hook to whatever you want.
1681 (defalias 'gnus-summary-position-cursor 'gnus-goto-colon)
1682 (defalias 'gnus-group-position-cursor 'gnus-goto-colon)
1683
1684 ;;; Various macros and substs.
1685
1686 (defmacro gnus-eval-in-buffer-window (buffer &rest forms)
1687   "Pop to BUFFER, evaluate FORMS, and then returns to original window."
1688   (` (let ((GnusStartBufferWindow (selected-window)))
1689        (unwind-protect
1690            (progn
1691              (pop-to-buffer (, buffer))
1692              (,@ forms))
1693          (select-window GnusStartBufferWindow)))))
1694
1695 (defmacro gnus-gethash (string hashtable)
1696   "Get hash value of STRING in HASHTABLE."
1697   ;;(` (symbol-value (abbrev-symbol (, string) (, hashtable))))
1698   ;;(` (abbrev-expansion (, string) (, hashtable)))
1699   (` (symbol-value (intern-soft (, string) (, hashtable)))))
1700
1701 (defmacro gnus-sethash (string value hashtable)
1702   "Set hash value. Arguments are STRING, VALUE, and HASHTABLE."
1703   ;; We cannot use define-abbrev since it only accepts string as value.
1704   ;; (set (intern string hashtable) value))
1705   (` (set (intern (, string) (, hashtable)) (, value))))
1706
1707 (defsubst gnus-buffer-substring (beg end)
1708   (buffer-substring (match-beginning beg) (match-end end)))
1709
1710 ;; modified by MORIOKA Tomohiko <morioka@jaist.ac.jp>
1711 ;;   function `substring' might cut on a middle of multi-octet
1712 ;;   character.
1713
1714 (defun gnus-truncate-string (str width)
1715   (substring str 0 width))
1716
1717 (defsubst gnus-simplify-subject-re (subject)
1718   "Remove \"Re:\" from subject lines."
1719   (let ((case-fold-search t))
1720     (if (string-match "^re: *" subject)
1721         (substring subject (match-end 0))
1722       subject)))
1723
1724 (defsubst gnus-goto-char (point)
1725   (and point (goto-char point)))
1726
1727 (defmacro gnus-buffer-exists-p (buffer)
1728   (` (and (, buffer)
1729           (funcall (if (stringp (, buffer)) 'get-buffer 'buffer-name)
1730                    (, buffer)))))
1731
1732 (defmacro gnus-kill-buffer (buffer)
1733   (` (if (gnus-buffer-exists-p (, buffer))
1734          (kill-buffer (, buffer)))))
1735
1736 (defsubst gnus-point-at-bol ()
1737   "Return point at the beginning of line."
1738   (let ((p (point)))
1739     (beginning-of-line)
1740     (prog1
1741         (point)
1742       (goto-char p))))
1743
1744 (defsubst gnus-point-at-eol ()
1745   "Return point at the beginning of line."
1746   (let ((p (point)))
1747     (end-of-line)
1748     (prog1
1749         (point)
1750       (goto-char p))))
1751
1752 ;; Delete the current line (and the next N lines.);
1753 (defmacro gnus-delete-line (&optional n)
1754   (` (delete-region (progn (beginning-of-line) (point))
1755                     (progn (forward-line (, (or n 1))) (point)))))
1756
1757 ;;; Load the compatability functions. 
1758
1759 (require 'gnus-ems)
1760
1761 \f
1762 ;;;
1763 ;;; Gnus Utility Functions
1764 ;;;
1765
1766 (defun gnus-extract-address-components (from)
1767   (let (name address)
1768     ;; First find the address - the thing with the @ in it.  This may
1769     ;; not be accurate in mail addresses, but does the trick most of
1770     ;; the time in news messages.
1771     (if (string-match "\\b[^@ \t<>]+[!@][^@ \t<>]+\\b" from)
1772         (setq address (substring from (match-beginning 0) (match-end 0))))
1773     ;; Then we check whether the "name <address>" format is used.
1774     (and address
1775          (string-match (concat "<" (regexp-quote address) ">") from)
1776          (and (setq name (substring from 0 (1- (match-beginning 0))))
1777               ;; Strip any quotes from the name.
1778               (string-match "\".*\"" name)
1779               (setq name (substring name 1 (1- (match-end 0))))))
1780     ;; If not, then "address (name)" is used.
1781     (or name
1782         (and (string-match "(.+)" from)
1783              (setq name (substring from (1+ (match-beginning 0)) 
1784                                    (1- (match-end 0)))))
1785         (and (string-match "()" from)
1786              (setq name address))
1787         ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>.
1788         ;; XOVER might not support folded From headers.
1789         (and (string-match "(.*" from)
1790              (setq name (substring from (1+ (match-beginning 0)) 
1791                                    (match-end 0)))))
1792     ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
1793     (list (or name from) (or address from))))
1794
1795 (defun gnus-fetch-field (field)
1796   "Return the value of the header FIELD of current article."
1797   (save-excursion
1798     (save-restriction
1799       (let ((case-fold-search t))
1800         (gnus-narrow-to-headers)
1801         (mail-fetch-field field)))))
1802
1803 (defun gnus-goto-colon ()
1804   (beginning-of-line)
1805   (search-forward ":" (gnus-point-at-eol) t))
1806
1807 (defun gnus-narrow-to-headers ()
1808   (widen)
1809   (save-excursion
1810     (narrow-to-region
1811      (goto-char (point-min))
1812      (if (search-forward "\n\n" nil t)
1813          (1- (point))
1814        (point-max)))))
1815
1816 (defun gnus-update-format-specifications ()
1817   (gnus-make-thread-indent-array)
1818   (setq gnus-summary-line-format-spec 
1819         (gnus-parse-format
1820          gnus-summary-line-format gnus-summary-line-format-alist))
1821   (gnus-update-summary-mark-positions)
1822   (setq gnus-summary-dummy-line-format-spec 
1823         (gnus-parse-format gnus-summary-dummy-line-format 
1824                            gnus-summary-dummy-line-format-alist))
1825   (setq gnus-group-line-format-spec
1826         (gnus-parse-format 
1827          gnus-group-line-format 
1828          gnus-group-line-format-alist))
1829   (if (and (string-match "%D" gnus-group-line-format)
1830            (not gnus-description-hashtb)
1831            gnus-read-active-file)
1832       (gnus-read-all-descriptions-files))
1833   (setq gnus-summary-mode-line-format-spec 
1834         (gnus-parse-format gnus-summary-mode-line-format 
1835                            gnus-summary-mode-line-format-alist))
1836   (setq gnus-article-mode-line-format-spec 
1837         (gnus-parse-format gnus-article-mode-line-format 
1838                            gnus-summary-mode-line-format-alist))
1839   (setq gnus-group-mode-line-format-spec 
1840         (gnus-parse-format gnus-group-mode-line-format 
1841                            gnus-group-mode-line-format-alist)))
1842
1843 (defun gnus-update-summary-mark-positions ()
1844   (save-excursion
1845     (let ((gnus-replied-mark 129)
1846           (gnus-score-below-mark 130)
1847           (gnus-score-over-mark 130)
1848           (thread nil)
1849           pos)
1850       (gnus-set-work-buffer)
1851       (gnus-summary-insert-line 
1852        nil [0 "" "" "" "" "" 0 0 ""]  0 nil 128 t nil "" nil 1)
1853       (goto-char (point-min))
1854       (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
1855                                          (- (point) 2)))))
1856       (goto-char (point-min))
1857       (setq pos (cons (cons 'replied (and (search-forward "\201" nil t)
1858                                           (- (point) 2))) pos))
1859       (goto-char (point-min))
1860       (setq pos (cons (cons 'score (and (search-forward "\202" nil t)
1861                                         (- (point) 2))) pos))
1862       (setq gnus-summary-mark-positions pos))))
1863
1864 (defun gnus-format-max-width (form length)
1865   (let* ((val (eval form))
1866          (valstr (if (numberp val) (int-to-string val) val)))
1867     (if (> (length valstr) length)
1868         (substring valstr 0 length)
1869       valstr)))
1870
1871 (defun gnus-set-mouse-face (string)
1872   ;; Set mouse face property on STRING.
1873   (put-text-property 0 (length string) 'mouse-face gnus-mouse-face string)
1874   string)
1875
1876 (defun gnus-parse-format (format spec-alist)
1877   ;; This function parses the FORMAT string with the help of the
1878   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
1879   ;; string.  If the FORMAT string contains the specifiers %( and %)
1880   ;; the text between them will have the mouse-face text property.
1881   (if (string-match "\\`\\(.*\\)%(\\(.*\\)%)\\(.*\n?\\)\\'" format)
1882       (if (and gnus-visual gnus-mouse-face)
1883           (let ((pre (substring format (match-beginning 1) (match-end 1)))
1884                 (button (substring format (match-beginning 2) (match-end 2)))
1885                 (post (substring format (match-beginning 3) (match-end 3))))
1886             (list 'concat
1887                   (gnus-parse-simple-format pre spec-alist)
1888                   (list 'gnus-set-mouse-face
1889                         (gnus-parse-simple-format button spec-alist))
1890                   (gnus-parse-simple-format post spec-alist)))
1891         (gnus-parse-simple-format
1892          (concat (substring format (match-beginning 1) (match-end 1))
1893                  (substring format (match-beginning 2) (match-end 2))
1894                  (substring format (match-beginning 3) (match-end 3)))
1895          spec-alist))
1896     (gnus-parse-simple-format format spec-alist)))
1897
1898 (defun gnus-parse-simple-format (format spec-alist)
1899   ;; This function parses the FORMAT string with the help of the
1900   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
1901   ;; string. The list will consist of the symbol `format', a format
1902   ;; specification string, and a list of forms depending on the
1903   ;; SPEC-ALIST.
1904   (let ((max-width 0)
1905         spec flist fstring b newspec max-width elem beg)
1906     (save-excursion
1907       (gnus-set-work-buffer)
1908       (insert format)
1909       (goto-char (point-min))
1910       (while (re-search-forward "%[-0-9]*\\(,[0-9]*\\)*\\(.\\)\\(.\\)?" nil t)
1911         (setq spec (string-to-char (buffer-substring (match-beginning 2)
1912                                                      (match-end 2))))
1913         ;; First check if there are any specs that look anything like
1914         ;; "%12,12A", ie. with a "max width specification". These have
1915         ;; to be treated specially.
1916         (if (setq beg (match-beginning 1))
1917             (setq max-width 
1918                   (string-to-int 
1919                    (buffer-substring (1+ (match-beginning 1)) (match-end 1))))
1920           (setq max-width 0)
1921           (setq beg (match-beginning 2)))
1922         ;; Find the specification from `spec-alist'.
1923         (if (not (setq elem (cdr (assq spec spec-alist))))
1924             (setq elem '("*" ?s)))
1925         ;; Treat user defined format specifiers specially
1926         (and (eq (car elem) 'user-defined)
1927              (setq elem
1928                    (list 
1929                     (list (intern (concat "gnus-user-format-function-"
1930                                           (buffer-substring
1931                                            (match-beginning 3)
1932                                            (match-end 3))))
1933                           'header)
1934                     ?s))
1935              (delete-region (match-beginning 3) (match-end 3)))
1936         (if (not (zerop max-width))
1937             (let ((el (car elem)))
1938               (cond ((= (car (cdr elem)) ?c) 
1939                      (setq el (list 'char-to-string el)))
1940                     ((= (car (cdr elem)) ?d)
1941                      (numberp el) (setq el (list 'int-to-string el))))
1942               (setq flist (cons (list 'gnus-format-max-width el max-width) 
1943                                 flist))
1944               (setq newspec ?s))
1945           (setq flist (cons (car elem) flist))
1946           (setq newspec (car (cdr elem))))
1947         ;; Remove the old specification (and possibly a ",12" string).
1948         (delete-region beg (match-end 2))
1949         ;; Insert the new specification.
1950         (goto-char beg)
1951         (insert newspec))
1952       (setq fstring (buffer-substring 1 (point-max))))
1953     (cons 'format (cons fstring (nreverse flist)))))
1954
1955 ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
1956 (defun gnus-read-init-file ()
1957   (and gnus-init-file
1958        (or (and (file-exists-p gnus-init-file) 
1959                 ;; Don't try to load a directory.
1960                 (not (file-directory-p gnus-init-file)))
1961            (file-exists-p (concat gnus-init-file ".el"))
1962            (file-exists-p (concat gnus-init-file ".elc")))
1963        (load gnus-init-file nil t)))
1964
1965 (defun gnus-set-work-buffer ()
1966   (if (get-buffer gnus-work-buffer)
1967       (progn
1968         (set-buffer gnus-work-buffer)
1969         (erase-buffer))
1970     (set-buffer (get-buffer-create gnus-work-buffer))
1971     (kill-all-local-variables)
1972     (buffer-disable-undo (current-buffer))
1973     (gnus-add-current-to-buffer-list)))
1974
1975 ;; Article file names when saving.
1976
1977 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
1978   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1979 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group/num.
1980 Otherwise, it is like ~/News/news/group/num."
1981   (let ((default
1982           (expand-file-name
1983            (concat (if (gnus-use-long-file-name 'not-save)
1984                        (gnus-capitalize-newsgroup newsgroup)
1985                      (gnus-newsgroup-directory-form newsgroup))
1986                    "/" (int-to-string (header-number headers)))
1987            (or gnus-article-save-directory "~/News"))))
1988     (if (and last-file
1989              (string-equal (file-name-directory default)
1990                            (file-name-directory last-file))
1991              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
1992         default
1993       (or last-file default))))
1994
1995 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
1996   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1997 If variable `gnus-use-long-file-name' is nil, it is ~/News/news.group/num.
1998 Otherwise, it is like ~/News/news/group/num."
1999   (let ((default
2000           (expand-file-name
2001            (concat (if (gnus-use-long-file-name 'not-save)
2002                        newsgroup
2003                      (gnus-newsgroup-directory-form newsgroup))
2004                    "/" (int-to-string (header-number headers)))
2005            (or gnus-article-save-directory "~/News"))))
2006     (if (and last-file
2007              (string-equal (file-name-directory default)
2008                            (file-name-directory last-file))
2009              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2010         default
2011       (or last-file default))))
2012
2013 (defun gnus-Plain-save-name (newsgroup headers &optional last-file)
2014   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2015 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group.
2016 Otherwise, it is like ~/News/news/group/news."
2017   (or last-file
2018       (expand-file-name
2019        (if (gnus-use-long-file-name 'not-save)
2020            (gnus-capitalize-newsgroup newsgroup)
2021          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2022        (or gnus-article-save-directory "~/News"))))
2023
2024 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
2025   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2026 If variable `gnus-use-long-file-name' is nil, it is ~/News/news.group.
2027 Otherwise, it is like ~/News/news/group/news."
2028   (or last-file
2029       (expand-file-name
2030        (if (gnus-use-long-file-name 'not-save)
2031            newsgroup
2032          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2033        (or gnus-article-save-directory "~/News"))))
2034
2035 ;; For subscribing new newsgroup
2036
2037 (defun gnus-subscribe-hierarchical-interactive (groups)
2038   (let ((groups (sort groups 'string<))
2039         prefixes prefix start rest ans group starts)
2040     (while groups
2041       (setq prefixes (list "^"))
2042       (while (and groups prefixes)
2043         (while (not (string-match (car prefixes) (car groups)))
2044           (setq prefixes (cdr prefixes)))
2045         (setq prefix (car prefixes))
2046         (setq start (1- (length prefix)))
2047         (if (and (string-match "[^\\.]\\." (car groups) start)
2048                  (cdr groups)
2049                  (setq prefix 
2050                        (concat "^" (substring (car groups) 0 (match-end 0))))
2051                  (string-match prefix (car (cdr groups))))
2052             (progn
2053               (setq prefixes (cons prefix prefixes))
2054               (message "Descend hierarchy %s? ([y]nsq): " 
2055                        (substring prefix 1 (1- (length prefix))))
2056               (setq ans (read-char))
2057               (cond ((= ans ?n)
2058                      (while (and groups 
2059                                  (string-match prefix 
2060                                                (setq group (car groups))))
2061                        (setq gnus-killed-list 
2062                              (cons group gnus-killed-list))
2063                        (gnus-sethash group group gnus-killed-hashtb)
2064                        (setq groups (cdr groups)))
2065                      (setq starts (cdr starts)))
2066                     ((= ans ?s)
2067                      (while (and groups 
2068                                  (string-match prefix 
2069                                                (setq group (car groups))))
2070                        (gnus-sethash group group gnus-killed-hashtb)
2071                        (gnus-subscribe-alphabetically (car groups))
2072                        (setq groups (cdr groups)))
2073                      (setq starts (cdr starts)))
2074                     ((= ans ?q)
2075                      (while groups
2076                        (setq group (car groups))
2077                        (setq gnus-killed-list (cons group gnus-killed-list))
2078                        (gnus-sethash group group gnus-killed-hashtb)
2079                        (setq groups (cdr groups))))
2080                     (t nil)))
2081           (message "Subscribe %s? ([n]yq)" (car groups))
2082           (setq ans (read-char))
2083           (setq group (car groups))
2084           (cond ((= ans ?y)
2085                  (gnus-subscribe-alphabetically (car groups))
2086                  (gnus-sethash group group gnus-killed-hashtb))
2087                 ((= ans ?q)
2088                  (while groups
2089                    (setq group (car groups))
2090                    (setq gnus-killed-list (cons group gnus-killed-list))
2091                    (gnus-sethash group group gnus-killed-hashtb)
2092                    (setq groups (cdr groups))))
2093                 (t 
2094                  (setq gnus-killed-list (cons group gnus-killed-list))
2095                  (gnus-sethash group group gnus-killed-hashtb)))
2096           (setq groups (cdr groups)))))))
2097
2098 (defun gnus-subscribe-randomly (newsgroup)
2099   "Subscribe new NEWSGROUP by making it the first newsgroup."
2100   (gnus-subscribe-newsgroup newsgroup))
2101
2102 (defun gnus-subscribe-alphabetically (newgroup)
2103   "Subscribe new NEWSGROUP and insert it in alphabetical order."
2104   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2105   (let ((groups (cdr gnus-newsrc-alist))
2106         before)
2107     (while (and (not before) groups)
2108       (if (string< newgroup (car (car groups)))
2109           (setq before (car (car groups)))
2110         (setq groups (cdr groups))))
2111     (gnus-subscribe-newsgroup newgroup before)))
2112
2113 (defun gnus-subscribe-hierarchically (newgroup)
2114   "Subscribe new NEWSGROUP and insert it in hierarchical newsgroup order."
2115   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2116   (save-excursion
2117     (set-buffer (find-file-noselect gnus-current-startup-file))
2118     (let ((groupkey newgroup)
2119           before)
2120       (while (and (not before) groupkey)
2121         (goto-char (point-min))
2122         (let ((groupkey-re
2123                (concat "^\\(" (regexp-quote groupkey) ".*\\)[!:]")))
2124           (while (and (re-search-forward groupkey-re nil t)
2125                       (progn
2126                         (setq before (buffer-substring
2127                                       (match-beginning 1) (match-end 1)))
2128                         (string< before newgroup)))))
2129         ;; Remove tail of newsgroup name (eg. a.b.c -> a.b)
2130         (setq groupkey
2131               (if (string-match "^\\(.*\\)\\.[^.]+$" groupkey)
2132                   (substring groupkey (match-beginning 1) (match-end 1)))))
2133       (gnus-subscribe-newsgroup newgroup before))))
2134
2135 (defun gnus-subscribe-interactively (newsgroup)
2136   "Subscribe new NEWSGROUP interactively.
2137 It is inserted in hierarchical newsgroup order if subscribed. If not,
2138 it is killed."
2139   (if (gnus-y-or-n-p (format "Subscribe new newsgroup: %s " newsgroup))
2140       (gnus-subscribe-hierarchically newsgroup)
2141     (setq gnus-killed-list (cons newsgroup gnus-killed-list))))
2142
2143 (defun gnus-subscribe-zombies (newsgroup)
2144   "Make new NEWSGROUP a zombie group."
2145   (setq gnus-zombie-list (cons newsgroup gnus-zombie-list)))
2146
2147 (defun gnus-subscribe-newsgroup (newsgroup &optional next)
2148   "Subscribe new NEWSGROUP.
2149 If NEXT is non-nil, it is inserted before NEXT. Otherwise it is made
2150 the first newsgroup."
2151   ;; We subscribe the group by changing its level to `subscribed'.
2152   (gnus-group-change-level 
2153    newsgroup gnus-level-default-subscribed
2154    gnus-level-killed (gnus-gethash (or next "dummy.group") gnus-newsrc-hashtb))
2155   (gnus-message 5 "Subscribe newsgroup: %s" newsgroup))
2156
2157 ;; For directories
2158
2159 (defun gnus-newsgroup-directory-form (newsgroup)
2160   "Make hierarchical directory name from NEWSGROUP name."
2161   (let ((newsgroup (substring newsgroup 0)) ;Copy string.
2162         (len (length newsgroup))
2163         idx)
2164     ;; If this is a foreign group, we don't want to translate the
2165     ;; entire name.  
2166     (if (setq idx (string-match ":" newsgroup))
2167         (aset newsgroup idx ?/)
2168       (setq idx 0))
2169     ;; Replace all occurrences of `.' with `/'.
2170     (while (< idx len)
2171       (if (= (aref newsgroup idx) ?.)
2172           (aset newsgroup idx ?/))
2173       (setq idx (1+ idx)))
2174     newsgroup))
2175
2176 (defun gnus-make-directory (dir)
2177   "Make DIRECTORY recursively."
2178   (let* ((dir (expand-file-name dir default-directory))
2179          dirs)
2180     (if (string-match "/$" dir)
2181         (setq dir (substring dir 0 (match-beginning 0))))
2182     (while (not (file-exists-p dir))
2183       (setq dirs (cons dir dirs))
2184       (string-match "/[^/]+$" dir)
2185       (setq dir (substring dir 0 (match-beginning 0))))
2186     (while dirs
2187       (make-directory (car dirs))
2188       (setq dirs (cdr dirs)))))
2189
2190 (defun gnus-capitalize-newsgroup (newsgroup)
2191   "Capitalize NEWSGROUP name."
2192   (and (not (zerop (length newsgroup)))
2193        (concat (char-to-string (upcase (aref newsgroup 0)))
2194                (substring newsgroup 1))))
2195
2196 ;; Var
2197
2198 (defun gnus-simplify-subject (subject &optional re-only)
2199   "Remove `Re:' and words in parentheses.
2200 If optional argument RE-ONLY is non-nil, strip `Re:' only."
2201   (let ((case-fold-search t))           ;Ignore case.
2202     ;; Remove `Re:' and `Re^N:'.
2203     (if (string-match "^re:[ \t]*" subject)
2204         (setq subject (substring subject (match-end 0))))
2205     ;; Remove words in parentheses from end.
2206     (or re-only
2207         (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
2208           (setq subject (substring subject 0 (match-beginning 0)))))
2209     ;; Return subject string.
2210     subject))
2211
2212 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
2213 ;; all whitespace.
2214 (defun gnus-simplify-subject-fuzzy (subject)
2215   (let ((case-fold-search t))
2216     (save-excursion
2217       (gnus-set-work-buffer)
2218       (insert subject)
2219       (inline (gnus-simplify-buffer-fuzzy))
2220       (buffer-string))))
2221
2222 (defun gnus-simplify-buffer-fuzzy ()
2223   (goto-char (point-min))
2224   ;; Fix by Stainless Steel Rat <ratinox@ccs.neu.edu>.
2225   (while (re-search-forward "^[ \t]*\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;][ \t]*"
2226                             nil t)
2227     (replace-match "" t t))
2228   (goto-char (point-min))
2229   (while (re-search-forward "[ \t\n]*([^()]*)[ \t\n]*$" nil t)
2230     (replace-match "" t t))
2231   (goto-char (point-min))
2232   (while (re-search-forward "[ \t]+" nil t)
2233     (replace-match " " t t))
2234   (goto-char (point-min))
2235   (while (re-search-forward "[ \t]+$" nil t)
2236     (replace-match "" t t))
2237   (goto-char (point-min))
2238   (while (re-search-forward "^[ \t]+" nil t)
2239     (replace-match "" t t))
2240   (if gnus-simplify-subject-fuzzy-regexp
2241       (while (re-search-forward gnus-simplify-subject-fuzzy-regexp nil t)
2242         (replace-match "" t t)))
2243     )
2244
2245 ;; Add the current buffer to the list of buffers to be killed on exit. 
2246 (defun gnus-add-current-to-buffer-list ()
2247   (or (memq (current-buffer) gnus-buffer-list)
2248       (setq gnus-buffer-list (cons (current-buffer) gnus-buffer-list))))
2249
2250 (defun gnus-string> (s1 s2)
2251   (not (or (string< s1 s2)
2252            (string= s1 s2))))
2253
2254 ;; Functions accessing headers.
2255 ;; Functions are more convenient than macros in some cases.
2256
2257 (defun gnus-header-number (header)
2258   (header-number header))
2259
2260 (defun gnus-header-subject (header)
2261   (header-subject header))
2262
2263 (defun gnus-header-from (header)
2264   (header-from header))
2265
2266 (defun gnus-header-xref (header)
2267   (header-xref header))
2268
2269 (defun gnus-header-lines (header)
2270   (header-lines header))
2271
2272 (defun gnus-header-date (header)
2273   (header-date header))
2274
2275 (defun gnus-header-id (header)
2276   (header-id header))
2277
2278 (defun gnus-header-references (header)
2279   (header-references header))
2280
2281 ;;; General various misc type functions.
2282
2283 (defun gnus-clear-system ()
2284   "Clear all variables and buffers."
2285   ;; Clear Gnus variables.
2286   (let ((variables gnus-variable-list))
2287     (while variables
2288       (set (car variables) nil)
2289       (setq variables (cdr variables))))
2290   ;; Clear other internal variables.
2291   (setq gnus-list-of-killed-groups nil
2292         gnus-have-read-active-file nil
2293         gnus-newsrc-alist nil
2294         gnus-newsrc-hashtb nil
2295         gnus-killed-list nil
2296         gnus-zombie-list nil
2297         gnus-killed-hashtb nil
2298         gnus-active-hashtb nil
2299         gnus-moderated-list nil
2300         gnus-description-hashtb nil
2301         gnus-newsgroup-headers nil
2302         gnus-newsgroup-headers-hashtb-by-number nil
2303         gnus-newsgroup-name nil
2304         gnus-server-alist nil
2305         gnus-current-select-method nil)
2306   ;; Reset any score variables.
2307   (and (boundp 'gnus-score-cache)
2308        (set 'gnus-score-cache nil))
2309   (and (boundp 'gnus-internal-global-score-files)
2310        (set 'gnus-internal-global-score-files nil))
2311   ;; Kill the startup file.
2312   (and gnus-current-startup-file
2313        (get-file-buffer gnus-current-startup-file)
2314        (kill-buffer (get-file-buffer gnus-current-startup-file)))
2315   ;; Save any cache buffers.
2316   (and gnus-use-cache (gnus-cache-save-buffers))
2317   ;; Clear the dribble buffer.
2318   (gnus-dribble-clear)
2319   ;; Kill global KILL file buffer.
2320   (if (get-file-buffer (gnus-newsgroup-kill-file nil))
2321       (kill-buffer (get-file-buffer (gnus-newsgroup-kill-file nil))))
2322   (gnus-kill-buffer nntp-server-buffer)
2323   ;; Kill Gnus buffers.
2324   (while gnus-buffer-list
2325     (gnus-kill-buffer (car gnus-buffer-list))
2326     (setq gnus-buffer-list (cdr gnus-buffer-list))))
2327
2328 (defun gnus-windows-old-to-new (setting)
2329   (if (symbolp setting)
2330       (setq setting 
2331             (cond ((eq setting 'SelectArticle)
2332                    'article)
2333                   ((eq setting 'SelectSubject)
2334                    'summary)
2335                   ((eq setting 'SelectNewsgroup)
2336                    'group)
2337                   (t setting))))
2338   (if (or (listp setting)
2339           (not (and gnus-window-configuration
2340                     (memq setting '(group summary article)))))
2341       setting
2342     (let* ((setting (if (eq setting 'group) 
2343                         (if (assq 'newsgroup gnus-window-configuration)
2344                             'newsgroup
2345                           'newsgroups) setting))
2346            (elem (car (cdr (assq setting gnus-window-configuration))))
2347            (total (apply '+ elem))
2348            (types '(group summary article))
2349            (pbuf (if (eq setting 'newsgroups) 'group 'summary))
2350            (i 0)
2351            perc
2352            out)
2353       (while (< i 3)
2354         (or (zerop (nth i elem))
2355             (progn
2356               (setq perc  (/ (* 1.0 (nth 0 elem)) total))
2357               (setq out (cons (if (eq pbuf (nth i types))
2358                                   (vector (nth i types) perc 'point)
2359                                 (vector (nth i types) perc))
2360                               out))))
2361         (setq i (1+ i)))
2362       (list (nreverse out)))))
2363            
2364 (defun gnus-add-configuration (conf)
2365   (setq gnus-buffer-configuration 
2366         (cons conf (delq (assq (car conf) gnus-buffer-configuration)
2367                          gnus-buffer-configuration))))
2368
2369 (defun gnus-configure-windows (setting)
2370   (setq setting (gnus-windows-old-to-new setting))
2371   (let ((r (if (symbolp setting)
2372                   (cdr (assq setting gnus-buffer-configuration))
2373                 setting))
2374         (in-buf (current-buffer))
2375         rule val window w height hor ohor heights sub jump-buffer
2376         rel total to-buf)
2377     (or r (error "No such setting: %s" setting))
2378
2379     ;; Either remove all windows or just remove all Gnus windows.
2380     (if gnus-use-full-window
2381         (delete-other-windows)
2382       (gnus-remove-some-windows)
2383       (switch-to-buffer nntp-server-buffer))
2384
2385     (while r
2386       (setq hor (car r)
2387             ohor nil)
2388
2389       ;; We have to do the (possible) horizontal splitting before the
2390       ;; vertical. 
2391       (if (and (listp (car hor)) 
2392                (eq (car (car hor)) 'horizontal))
2393           (progn
2394             (split-window 
2395              nil
2396              (if (integerp (nth 1 (car hor)))
2397                  (nth 1 (car hor))
2398                (- (frame-width) (floor (* (frame-width) (nth 1 (car hor))))))
2399              t)
2400             (setq hor (cdr hor))))
2401
2402       ;; Go through the rules and eval the elements that are to be
2403       ;; evaled.  
2404       (while hor
2405         (if (setq val (if (vectorp (car hor)) (car hor) (eval (car hor))))
2406             (progn
2407               ;; Expand short buffer name.
2408               (setq w (aref val 0))
2409               (and (setq w (cdr (assq w gnus-window-to-buffer)))
2410                    (progn
2411                      (setq val (apply 'vector (mapcar (lambda (v) v) val)))
2412                      (aset val 0 w)))
2413               (setq ohor (cons val ohor))))
2414         (setq hor (cdr hor)))
2415       (setq rule (cons (nreverse ohor) rule))
2416       (setq r (cdr r)))
2417     (setq rule (nreverse rule))
2418
2419     ;; We tally the window sizes.
2420     (setq total (window-height))
2421     (while rule
2422       (setq hor (car rule))
2423       (if (and (listp (car hor)) (eq (car (car hor)) 'horizontal))
2424           (setq hor (cdr hor)))
2425       (setq sub 0)
2426       (while hor
2427         (setq rel (aref (car hor) 1)
2428               heights (cons
2429                        (cond ((and (floatp rel) (= 1.0 rel))
2430                               'x)
2431                              ((integerp rel)
2432                               rel)
2433                              (t
2434                               (max (floor (* total rel)) 4)))
2435                        heights)
2436               sub (+ sub (if (numberp (car heights)) (car heights) 0))
2437               hor (cdr hor)))
2438       (setq heights (nreverse heights)
2439             hor (car rule))
2440
2441       ;; We then go through these heighs and create windows for them.
2442       (while heights
2443         (setq height (car heights)
2444               heights (cdr heights))
2445         (and (eq height 'x)
2446              (setq height (- total sub)))
2447         (and heights
2448              (split-window nil height))
2449         (setq to-buf (aref (car hor) 0))
2450         (switch-to-buffer 
2451          (cond ((not to-buf)
2452                 in-buf)
2453                ((symbolp to-buf)
2454                 (symbol-value (aref (car hor) 0)))
2455                (t
2456                 (aref (car hor) 0))))
2457         (and (> (length (car hor)) 2)
2458              (eq (aref (car hor) 2) 'point)
2459              (setq jump-buffer (current-buffer)))
2460         (other-window 1)
2461         (setq hor (cdr hor)))
2462       
2463       (setq rule (cdr rule)))
2464
2465     ;; Finally, we pop to the buffer that's supposed to have point. 
2466     (or jump-buffer (error "Missing `point' in spec for %s" setting))
2467
2468     (select-window (get-buffer-window jump-buffer))
2469     (set-buffer jump-buffer)))
2470       
2471 (defun gnus-remove-some-windows ()
2472   (let ((buffers gnus-window-to-buffer)
2473         (first t)
2474         buf)
2475     (save-excursion
2476       ;; Remove windows on all known Gnus buffers.
2477       (while buffers
2478         (setq buf (cdr (car buffers)))
2479         (if (symbolp buf)
2480             (setq buf (and (boundp buf) (symbol-value buf))))
2481         (and buf 
2482              (get-buffer-window buf)
2483              (progn
2484                (if first
2485                    (progn
2486                      (pop-to-buffer buf)
2487                      (switch-to-buffer nntp-server-buffer)
2488                      (setq first nil))
2489                  (delete-window (get-buffer-window buf)))))
2490         (setq buffers (cdr buffers)))
2491       ;; Remove windows on *all* summary buffers.
2492       (let ((buffers (buffer-list)))
2493         (while buffers
2494           (if (and (string-match 
2495                     "^\\*Summary" (or (buffer-name (car buffers)) ""))
2496                    (get-buffer-window (car buffers)))
2497               (delete-window (get-buffer-window (car buffers))))
2498           (setq buffers (cdr buffers)))))))
2499                           
2500 (defun gnus-version ()
2501   "Version numbers of this version of Gnus."
2502   (interactive)
2503   (let ((methods gnus-valid-select-methods)
2504         (mess gnus-version)
2505         meth)
2506     ;; Go through all the legal select methods and add their version
2507     ;; numbers to the total version string. Only the backends that are
2508     ;; currently in use will have their message numbers taken into
2509     ;; consideration. 
2510     (while methods
2511       (setq meth (intern (concat (car (car methods)) "-version")))
2512       (and (boundp meth)
2513            (stringp (symbol-value meth))
2514            (setq mess (concat mess "; " (symbol-value meth))))
2515       (setq methods (cdr methods)))
2516     (gnus-message 2 mess)))
2517
2518 (defun gnus-info-find-node ()
2519   "Find Info documentation of Gnus."
2520   (interactive)
2521   ;; Enlarge info window if needed.
2522   (let ((mode major-mode))
2523     (gnus-configure-windows 'info)
2524     (Info-goto-node (car (cdr (assq mode gnus-info-nodes))))))
2525
2526 (defun gnus-overload-functions (&optional overloads)
2527   "Overload functions specified by optional argument OVERLOADS.
2528 If nothing is specified, use the variable gnus-overload-functions."
2529   (let ((defs nil)
2530         (overloads (or overloads gnus-overload-functions)))
2531     (while overloads
2532       (setq defs (car overloads))
2533       (setq overloads (cdr overloads))
2534       ;; Load file before overloading function if necessary.  Make
2535       ;; sure we cannot use `require' always.
2536       (and (not (fboundp (car defs)))
2537            (car (cdr (cdr defs)))
2538            (load (car (cdr (cdr defs))) nil 'nomessage))
2539       (fset (car defs) (car (cdr defs))))))
2540
2541 (defun gnus-replace-chars-in-string (string from to)
2542   "Replace characters in STRING from FROM to TO."
2543   (let ((string (substring string 0))   ;Copy string.
2544         (len (length string))
2545         (idx 0))
2546     ;; Replace all occurrences of FROM with TO.
2547     (while (< idx len)
2548       (if (= (aref string idx) from)
2549           (aset string idx to))
2550       (setq idx (1+ idx)))
2551     string))
2552
2553 (defun gnus-days-between (date1 date2)
2554   ;; Return the number of days between date1 and date2.
2555   (- (gnus-day-number date1) (gnus-day-number date2)))
2556
2557 (defun gnus-day-number (date)
2558   (let ((dat (mapcar (lambda (s) (and s (string-to-int s)) )
2559                      (timezone-parse-date date))))
2560     (timezone-absolute-from-gregorian 
2561      (nth 1 dat) (nth 2 dat) (car dat))))
2562
2563 ;; Returns a floating point number that says how many seconds have
2564 ;; lapsed between Jan 1 12:00:00 1970 and DATE.
2565 (defun gnus-seconds-since-epoch (date)
2566   (let* ((tdate (mapcar (lambda (ti) (and ti (string-to-int ti)))
2567                         (timezone-parse-date date)))
2568          (ttime (mapcar (lambda (ti) (and ti (string-to-int ti)))
2569                         (timezone-parse-time
2570                          (aref (timezone-parse-date date) 3))))
2571          (edate (mapcar (lambda (ti) (and ti (string-to-int ti)))
2572                         (timezone-parse-date "Jan 1 12:00:00 1970")))
2573          (tday (- (timezone-absolute-from-gregorian 
2574                    (nth 1 tdate) (nth 2 tdate) (nth 0 tdate))
2575                   (timezone-absolute-from-gregorian 
2576                    (nth 1 edate) (nth 2 edate) (nth 0 edate)))))
2577     (+ (nth 2 ttime)
2578        (* (nth 1 ttime) 60)
2579        (* 1.0 (nth 0 ttime) 60 60)
2580        (* 1.0 tday 60 60 24))))
2581
2582 (defun gnus-file-newer-than (file date)
2583   (let ((fdate (nth 5 (file-attributes file))))
2584     (or (> (car fdate) (car date))
2585         (and (= (car fdate) (car date))
2586              (> (nth 1 fdate) (nth 1 date))))))
2587
2588 ;; Two silly functions to ensure that all `y-or-n-p' questions clear
2589 ;; the echo area.
2590 (defun gnus-y-or-n-p (prompt)
2591   (prog1
2592       (y-or-n-p prompt)
2593     (message "")))
2594
2595 (defun gnus-yes-or-no-p (prompt)
2596   (prog1
2597       (yes-or-no-p prompt)
2598     (message "")))
2599
2600 ;; Check whether to use long file names.
2601 (defun gnus-use-long-file-name (symbol)
2602   ;; The variable has to be set...
2603   (and gnus-use-long-file-name
2604        ;; If it isn't a list, then we return t.
2605        (or (not (listp gnus-use-long-file-name))
2606            ;; If it is a list, and the list contains `symbol', we
2607            ;; return nil.  
2608            (not (memq symbol gnus-use-long-file-name)))))
2609
2610 ;; I suspect there's a better way, but I haven't taken the time to do
2611 ;; it yet. -erik selberg@cs.washington.edu
2612 (defun gnus-dd-mmm (messy-date)
2613   "Return a string like DD-MMM from a big messy string"
2614   (let ((datevec (timezone-parse-date messy-date)))
2615     (format "%2s-%s"
2616             (or (aref datevec 2) "??")
2617             (capitalize
2618              (or (car 
2619                   (nth (1- (string-to-number (aref datevec 1)))
2620                        timezone-months-assoc))
2621                  "???")))))
2622
2623 ;; Make a hash table (default and minimum size is 255).
2624 ;; Optional argument HASHSIZE specifies the table size.
2625 (defun gnus-make-hashtable (&optional hashsize)
2626   (make-vector (if hashsize (max (gnus-create-hash-size hashsize) 255) 255) 0))
2627
2628 ;; Make a number that is suitable for hashing; bigger than MIN and one
2629 ;; less than 2^x.
2630 (defun gnus-create-hash-size (min)
2631   (let ((i 1))
2632     (while (< i min)
2633       (setq i (* 2 i)))
2634     (1- i)))
2635
2636 ;; Show message if message has a lower level than `gnus-verbose'. 
2637 ;; Guide-line for numbers:
2638 ;; 1 - error messages, 3 - non-serious error messages, 5 - messages
2639 ;; for things that take a long time, 7 - not very important messages
2640 ;; on stuff, 9 - messages inside loops.
2641 (defun gnus-message (level &rest args)
2642   (if (<= level gnus-verbose)
2643       (apply 'message args)
2644     ;; We have to do this format thingie here even if the result isn't
2645     ;; shown - the return value has to be the same as the return value
2646     ;; from `message'.
2647     (apply 'format args)))
2648
2649 ;; Generate a unique new group name.
2650 (defun gnus-generate-new-group-name (leaf)
2651   (let ((name leaf)
2652         (num 0))
2653     (while (gnus-gethash name gnus-newsrc-hashtb)
2654       (setq name (concat leaf "<" (int-to-string (setq num (1+ num))) ">")))
2655     name))
2656
2657 (defun gnus-find-file-noselect (file &optional force)
2658   "Does vaguely the same as find-file-noselect. No hooks are run."
2659   (let (buf insert)
2660     (if (setq buf (get-file-buffer file))
2661         (setq insert force)
2662       (setq buf (create-file-buffer file))
2663       (setq insert t))
2664     (if (not insert)
2665         buf
2666       (save-excursion
2667         (set-buffer buf)
2668         (erase-buffer)
2669         (and (file-readable-p file)
2670              (insert-file-contents file))
2671         (set-visited-file-name file)
2672         (set-buffer-modified-p nil)
2673         (current-buffer)))))
2674
2675 ;;; List and range functions
2676
2677 (defun gnus-last-element (list)
2678   "Return last element of LIST."
2679   (while (cdr list)
2680     (setq list (cdr list)))
2681   (car list))
2682
2683 (defun gnus-copy-sequence (list)
2684   "Do a complete, total copy of a list."
2685   (if (and (consp list) (not (consp (cdr list))))
2686       (cons (car list) (cdr list))
2687     (mapcar (lambda (elem) (if (consp elem) 
2688                                (if (consp (cdr elem))
2689                                    (gnus-copy-sequence elem)
2690                                  (cons (car elem) (cdr elem)))
2691                              elem))
2692             list)))
2693
2694 (defun gnus-set-difference (list1 list2)
2695   "Return a list of elements of LIST1 that do not appear in LIST2."
2696   (let ((list1 (copy-sequence list1)))
2697     (while list2
2698       (setq list1 (delq (car list2) list1))
2699       (setq list2 (cdr list2)))
2700     list1))
2701
2702 (defun gnus-sorted-complement (list1 list2)
2703   "Return a list of elements of LIST1 that do not appear in LIST2.
2704 Both lists have to be sorted over <."
2705   (let (out)
2706     (if (or (null list1) (null list2))
2707         (or list1 list2)
2708       (while (and list1 list2)
2709         (cond ((= (car list1) (car list2))
2710                (setq list1 (cdr list1)
2711                      list2 (cdr list2)))
2712               ((< (car list1) (car list2))
2713                (setq out (cons (car list1) out))
2714                (setq list1 (cdr list1)))
2715               (t
2716                (setq out (cons (car list2) out))
2717                (setq list2 (cdr list2)))))
2718       (nconc (nreverse out) (or list1 list2)))))
2719
2720 (defun gnus-intersection (list1 list2)      
2721   (let ((result nil))
2722     (while list2
2723       (if (memq (car list2) list1)
2724           (setq result (cons (car list2) result)))
2725       (setq list2 (cdr list2)))
2726     result))
2727
2728 (defun gnus-sorted-intersection (list1 list2)
2729   ;; LIST1 and LIST2 have to be sorted over <.
2730   (let (out)
2731     (while (and list1 list2)
2732       (cond ((= (car list1) (car list2))
2733              (setq out (cons (car list1) out)
2734                    list1 (cdr list1)
2735                    list2 (cdr list2)))
2736             ((< (car list1) (car list2))
2737              (setq list1 (cdr list1)))
2738             (t
2739              (setq list2 (cdr list2)))))
2740     (nreverse out)))
2741
2742 (defun gnus-set-sorted-intersection (list1 list2)
2743   ;; LIST1 and LIST2 have to be sorted over <.
2744   ;; This function modifies LIST1.
2745   (let* ((top (cons nil list1))
2746          (prev top))
2747   (while (and list1 list2)
2748     (cond ((= (car list1) (car list2))
2749            (setq prev list1
2750                  list1 (cdr list1)
2751                  list2 (cdr list2)))
2752           ((< (car list1) (car list2))
2753            (setcdr prev (cdr list1))
2754            (setq list1 (cdr list1)))
2755           (t
2756            (setq list2 (cdr list2)))))
2757   (setcdr prev nil)
2758   (cdr top)))
2759
2760 (defun gnus-compress-sequence (numbers &optional always-list)
2761   "Convert list of numbers to a list of ranges or a single range.
2762 If ALWAYS-LIST is non-nil, this function will always release a list of
2763 ranges."
2764   (let* ((first (car numbers))
2765          (last (car numbers))
2766          result)
2767     (if (null numbers)
2768         nil
2769       (if (not (listp (cdr numbers)))
2770           numbers
2771         (while numbers
2772           (cond ((= last (car numbers)) nil) ;Omit duplicated number
2773                 ((= (1+ last) (car numbers)) ;Still in sequence
2774                  (setq last (car numbers)))
2775                 (t                      ;End of one sequence
2776                  (setq result 
2777                        (cons (if (= first last) first
2778                                (cons first last)) result))
2779                  (setq first (car numbers))
2780                  (setq last  (car numbers))))
2781           (setq numbers (cdr numbers)))
2782         (if (and (not always-list) (null result))
2783             (if (= first last) (list first) (cons first last))
2784           (nreverse (cons (if (= first last) first (cons first last))
2785                           result)))))))
2786
2787 (defalias 'gnus-uncompress-sequence 'gnus-uncompress-range)
2788 (defun gnus-uncompress-range (ranges)
2789   "Expand a list of ranges into a list of numbers.
2790 RANGES is either a single range on the form `(num . num)' or a list of
2791 these ranges."
2792   (let (first last result)
2793     (cond 
2794      ((null ranges)
2795       nil)
2796      ((not (listp (cdr ranges)))
2797       (setq first (car ranges))
2798       (setq last (cdr ranges))
2799       (while (<= first last)
2800         (setq result (cons first result))
2801         (setq first (1+ first)))
2802       (nreverse result))
2803      (t
2804       (while ranges
2805         (if (atom (car ranges))
2806             (if (numberp (car ranges))
2807                 (setq result (cons (car ranges) result)))
2808           (setq first (car (car ranges)))
2809           (setq last  (cdr (car ranges)))
2810           (while (<= first last)
2811             (setq result (cons first result))
2812             (setq first (1+ first))))
2813         (setq ranges (cdr ranges)))
2814       (nreverse result)))))
2815
2816 (defun gnus-add-to-range (ranges list)
2817   "Return a list of ranges that has all articles from both RANGES and LIST.
2818 Note: LIST has to be sorted over `<'."
2819   (if (not ranges)
2820       (gnus-compress-sequence list t)
2821     (setq list (copy-sequence list))
2822     (or (listp (cdr ranges))
2823         (setq ranges (list ranges)))
2824     (let ((out ranges)
2825           ilist lowest highest temp)
2826       (while (and ranges list)
2827         (setq ilist list)
2828         (setq lowest (or (and (atom (car ranges)) (car ranges))
2829                          (car (car ranges))))
2830         (while (and list (cdr list) (< (car (cdr list)) lowest))
2831           (setq list (cdr list)))
2832         (if (< (car ilist) lowest)
2833             (progn
2834               (setq temp list)
2835               (setq list (cdr list))
2836               (setcdr temp nil)
2837               (setq out (nconc (gnus-compress-sequence ilist t) out))))
2838         (setq highest (or (and (atom (car ranges)) (car ranges))
2839                           (cdr (car ranges))))
2840         (while (and list (<= (car list) highest))
2841           (setq list (cdr list)))
2842         (setq ranges (cdr ranges)))
2843       (if list
2844           (setq out (nconc (gnus-compress-sequence list t) out)))
2845       (setq out (sort out (lambda (r1 r2) 
2846                             (< (or (and (atom r1) r1) (car r1))
2847                                (or (and (atom r2) r2) (car r2))))))
2848       (setq ranges out)
2849       (while ranges
2850         (if (atom (car ranges))
2851             (if (cdr ranges)
2852                 (if (atom (car (cdr ranges)))
2853                     (if (= (1+ (car ranges)) (car (cdr ranges)))
2854                         (progn
2855                           (setcar ranges (cons (car ranges) 
2856                                                (car (cdr ranges))))
2857                           (setcdr ranges (cdr (cdr ranges)))))
2858                   (if (= (1+ (car ranges)) (car (car (cdr ranges))))
2859                       (progn
2860                         (setcar (car (cdr ranges)) (car ranges))
2861                         (setcar ranges (car (cdr ranges)))
2862                         (setcdr ranges (cdr (cdr ranges)))))))
2863           (if (cdr ranges)
2864               (if (atom (car (cdr ranges)))
2865                   (if (= (1+ (cdr (car ranges))) (car (cdr ranges)))
2866                       (progn
2867                         (setcdr (car ranges) (car (cdr ranges)))
2868                         (setcdr ranges (cdr (cdr ranges)))))
2869                 (if (= (1+ (cdr (car ranges))) (car (car (cdr ranges))))
2870                     (progn
2871                       (setcdr (car ranges) (cdr (car (cdr ranges))))
2872                       (setcdr ranges (cdr (cdr ranges))))))))
2873         (setq ranges (cdr ranges)))
2874       out)))
2875
2876 (defun gnus-remove-from-range (ranges list)
2877   "Return a list of ranges that has all articles from LIST removed from RANGES.
2878 Note: LIST has to be sorted over `<'."
2879   ;; !!! This function shouldn't look like this, but I've got a headache.
2880   (gnus-compress-sequence 
2881    (gnus-sorted-complement
2882     (gnus-uncompress-range ranges) list)))
2883
2884 (defun gnus-member-of-range (number ranges)
2885   (if (not (listp (cdr ranges)))
2886       (and (>= number (car ranges)) 
2887            (<= number (cdr ranges)))
2888     (let ((not-stop t))
2889       (while (and ranges 
2890                   (if (numberp (car ranges))
2891                       (>= number (car ranges))
2892                     (>= number (car (car ranges))))
2893                   not-stop)
2894         (if (if (numberp (car ranges))
2895                 (= number (car ranges))
2896               (and (>= number (car (car ranges)))
2897                    (<= number (cdr (car ranges)))))
2898             (setq not-stop nil))
2899         (setq ranges (cdr ranges)))
2900       (not not-stop))))
2901
2902 \f
2903 ;;;
2904 ;;; Gnus group mode
2905 ;;;
2906
2907 (defvar gnus-group-mode-map nil)
2908 (defvar gnus-group-group-map nil)
2909 (defvar gnus-group-mark-map nil)
2910 (defvar gnus-group-list-map nil)
2911 (defvar gnus-group-sub-map nil)
2912 (put 'gnus-group-mode 'mode-class 'special)
2913
2914 (if gnus-group-mode-map
2915     nil
2916   (setq gnus-group-mode-map (make-keymap))
2917   (suppress-keymap gnus-group-mode-map)
2918   (define-key gnus-group-mode-map " " 'gnus-group-read-group)
2919   (define-key gnus-group-mode-map "=" 'gnus-group-select-group)
2920   (define-key gnus-group-mode-map "\r" 'gnus-group-select-group)
2921   (define-key gnus-group-mode-map "j" 'gnus-group-jump-to-group)
2922   (define-key gnus-group-mode-map "n" 'gnus-group-next-unread-group)
2923   (define-key gnus-group-mode-map "p" 'gnus-group-prev-unread-group)
2924   (define-key gnus-group-mode-map "\177" 'gnus-group-prev-unread-group)
2925   (define-key gnus-group-mode-map "N" 'gnus-group-next-group)
2926   (define-key gnus-group-mode-map "P" 'gnus-group-prev-group)
2927   (define-key gnus-group-mode-map "\M-n" 'gnus-group-next-unread-group-same-level)
2928   (define-key gnus-group-mode-map "\M-p" 'gnus-group-prev-unread-group-same-level)
2929   (define-key gnus-group-mode-map "," 'gnus-group-best-unread-group)
2930   (define-key gnus-group-mode-map "." 'gnus-group-first-unread-group)
2931   (define-key gnus-group-mode-map "u" 'gnus-group-unsubscribe-current-group)
2932   (define-key gnus-group-mode-map "U" 'gnus-group-unsubscribe-group)
2933   (define-key gnus-group-mode-map "c" 'gnus-group-catchup-current)
2934   (define-key gnus-group-mode-map "C" 'gnus-group-catchup-current-all)
2935   (define-key gnus-group-mode-map "l" 'gnus-group-list-groups)
2936   (define-key gnus-group-mode-map "L" 'gnus-group-list-all-groups)
2937   (define-key gnus-group-mode-map "m" 'gnus-group-mail)
2938   (define-key gnus-group-mode-map "g" 'gnus-group-get-new-news)
2939   (define-key gnus-group-mode-map "\M-g" 'gnus-group-get-new-news-this-group)
2940   (define-key gnus-group-mode-map "R" 'gnus-group-restart)
2941   (define-key gnus-group-mode-map "r" 'gnus-group-read-init-file)
2942   (define-key gnus-group-mode-map "B" 'gnus-group-browse-foreign-server)
2943   (define-key gnus-group-mode-map "b" 'gnus-group-check-bogus-groups)
2944   (define-key gnus-group-mode-map "F" 'gnus-find-new-newsgroups)
2945   (define-key gnus-group-mode-map "\C-c\C-d" 'gnus-group-describe-group)
2946   (define-key gnus-group-mode-map "\M-d" 'gnus-group-describe-all-groups)
2947   (define-key gnus-group-mode-map "\C-c\C-a" 'gnus-group-apropos)
2948   (define-key gnus-group-mode-map "\C-c\M-C-a" 'gnus-group-description-apropos)
2949   (define-key gnus-group-mode-map "a" 'gnus-group-post-news)
2950   (define-key gnus-group-mode-map "\ek" 'gnus-group-edit-local-kill)
2951   (define-key gnus-group-mode-map "\eK" 'gnus-group-edit-global-kill)
2952   (define-key gnus-group-mode-map "\C-k" 'gnus-group-kill-group)
2953   (define-key gnus-group-mode-map "\C-y" 'gnus-group-yank-group)
2954   (define-key gnus-group-mode-map "\C-w" 'gnus-group-kill-region)
2955   (define-key gnus-group-mode-map "\C-x\C-t" 'gnus-group-transpose-groups)
2956   (define-key gnus-group-mode-map "\C-c\C-l" 'gnus-group-list-killed)
2957   (define-key gnus-group-mode-map "\C-c\C-x" 'gnus-group-expire-articles)
2958   (define-key gnus-group-mode-map "\C-c\M-\C-x" 'gnus-group-expire-all-groups)
2959   (define-key gnus-group-mode-map "V" 'gnus-version)
2960   (define-key gnus-group-mode-map "s" 'gnus-group-save-newsrc)
2961   (define-key gnus-group-mode-map "z" 'gnus-group-suspend)
2962   (define-key gnus-group-mode-map "Z" 'gnus-group-clear-dribble)
2963   (define-key gnus-group-mode-map "q" 'gnus-group-exit)
2964   (define-key gnus-group-mode-map "Q" 'gnus-group-quit)
2965   (define-key gnus-group-mode-map "\M-f" 'gnus-group-fetch-faq)
2966   (define-key gnus-group-mode-map "?" 'gnus-group-describe-briefly)
2967   (define-key gnus-group-mode-map "\C-c\C-i" 'gnus-info-find-node)
2968   (define-key gnus-group-mode-map "\M-e" 'gnus-group-edit-group-method)
2969   (define-key gnus-group-mode-map "^" 'gnus-group-enter-server-mode)
2970   (define-key gnus-group-mode-map gnus-mouse-2 'gnus-mouse-pick-group)
2971   (define-key gnus-group-mode-map "<" 'beginning-of-buffer)
2972   (define-key gnus-group-mode-map ">" 'end-of-buffer)
2973   (define-key gnus-group-mode-map "\C-c\C-b" 'gnus-bug)
2974   (define-key gnus-group-mode-map "\C-c\C-s" 'gnus-group-sort-groups)
2975
2976   (define-key gnus-group-mode-map "#" 'gnus-group-mark-group)
2977   (define-key gnus-group-mode-map "\M-#" 'gnus-group-unmark-group)
2978   (define-prefix-command 'gnus-group-mark-map)
2979   (define-key gnus-group-mode-map "M" 'gnus-group-mark-map)
2980   (define-key gnus-group-mark-map "m" 'gnus-group-mark-group)
2981   (define-key gnus-group-mark-map "u" 'gnus-group-unmark-group)
2982   (define-key gnus-group-mark-map "w" 'gnus-group-mark-region)
2983
2984   (define-prefix-command 'gnus-group-group-map)
2985   (define-key gnus-group-mode-map "G" 'gnus-group-group-map)
2986   (define-key gnus-group-group-map "d" 'gnus-group-make-directory-group)
2987   (define-key gnus-group-group-map "h" 'gnus-group-make-help-group)
2988   (define-key gnus-group-group-map "a" 'gnus-group-make-archive-group)
2989   (define-key gnus-group-group-map "k" 'gnus-group-make-kiboze-group)
2990   (define-key gnus-group-group-map "m" 'gnus-group-make-group)
2991   (define-key gnus-group-group-map "E" 'gnus-group-edit-group)
2992   (define-key gnus-group-group-map "e" 'gnus-group-edit-group-method)
2993   (define-key gnus-group-group-map "p" 'gnus-group-edit-group-parameters)
2994   (define-key gnus-group-group-map "v" 'gnus-group-add-to-virtual)
2995   (define-key gnus-group-group-map "V" 'gnus-group-make-empty-virtual)
2996   (define-key gnus-group-group-map "D" 'gnus-group-enter-directory)
2997   (define-key gnus-group-group-map "f" 'gnus-group-make-doc-group)
2998   (define-key gnus-group-group-map "sb" 'gnus-group-brew-soup)
2999   (define-key gnus-group-group-map "sw" 'gnus-soup-save-areas)
3000   (define-key gnus-group-group-map "ss" 'gnus-soup-send-replies)
3001   (define-key gnus-group-group-map "sp" 'gnus-soup-pack-packet)
3002   (define-key gnus-group-group-map "sr" 'nnsoup-pack-replies)
3003
3004   (define-prefix-command 'gnus-group-list-map)
3005   (define-key gnus-group-mode-map "A" 'gnus-group-list-map)
3006   (define-key gnus-group-list-map "k" 'gnus-group-list-killed)
3007   (define-key gnus-group-list-map "z" 'gnus-group-list-zombies)
3008   (define-key gnus-group-list-map "s" 'gnus-group-list-groups)
3009   (define-key gnus-group-list-map "u" 'gnus-group-list-all-groups)
3010   (define-key gnus-group-list-map "a" 'gnus-group-apropos)
3011   (define-key gnus-group-list-map "d" 'gnus-group-description-apropos)
3012   (define-key gnus-group-list-map "m" 'gnus-group-list-matching)
3013   (define-key gnus-group-list-map "M" 'gnus-group-list-all-matching)
3014
3015   (define-prefix-command 'gnus-group-sub-map)
3016   (define-key gnus-group-mode-map "S" 'gnus-group-sub-map)
3017   (define-key gnus-group-sub-map "l" 'gnus-group-set-current-level)
3018   (define-key gnus-group-sub-map "t" 'gnus-group-unsubscribe-current-group)
3019   (define-key gnus-group-sub-map "s" 'gnus-group-unsubscribe-group)
3020   (define-key gnus-group-sub-map "k" 'gnus-group-kill-group)
3021   (define-key gnus-group-sub-map "y" 'gnus-group-yank-group)
3022   (define-key gnus-group-sub-map "w" 'gnus-group-kill-region)
3023   (define-key gnus-group-sub-map "z" 'gnus-group-kill-all-zombies))
3024
3025 (defun gnus-group-mode ()
3026   "Major mode for reading news.
3027
3028 All normal editing commands are switched off.
3029 \\<gnus-group-mode-map>
3030 The group buffer lists (some of) the groups available.  For instance,
3031 `\\[gnus-group-list-groups]' will list all subscribed groups with unread articles, while `\\[gnus-group-list-zombies]'
3032 lists all zombie groups. 
3033
3034 Groups that are displayed can be entered with `\\[gnus-group-read-group]'.  To subscribe 
3035 to a group not displayed, type `\\[gnus-group-unsubscribe-group]'. 
3036
3037 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]'). 
3038
3039 The following commands are available:
3040
3041 \\{gnus-group-mode-map}"
3042   (interactive)
3043   (if gnus-visual (gnus-group-make-menu-bar))
3044   (kill-all-local-variables)
3045   (setq mode-line-modified "-- ")
3046   (make-local-variable 'mode-line-format)
3047   (setq mode-line-format (copy-sequence mode-line-format))
3048   (and (equal (nth 3 mode-line-format) "   ")
3049        (setcar (nthcdr 3 mode-line-format) ""))
3050   (setq major-mode 'gnus-group-mode)
3051   (setq mode-name "Group")
3052   (gnus-group-set-mode-line)
3053   (setq mode-line-process nil)
3054   (use-local-map gnus-group-mode-map)
3055   (buffer-disable-undo (current-buffer))
3056   (setq truncate-lines t)
3057   (setq buffer-read-only t)
3058   (run-hooks 'gnus-group-mode-hook))
3059
3060 (defun gnus-mouse-pick-group (e)
3061   (interactive "e")
3062   (mouse-set-point e)
3063   (gnus-group-read-group nil))
3064
3065 ;;;###autoload
3066 (defun gnus-no-server (&optional arg)
3067   "Read network news.
3068 If ARG is a positive number, Gnus will use that as the
3069 startup level. If ARG is nil, Gnus will be started at level 2. 
3070 If ARG is non-nil and not a positive number, Gnus will
3071 prompt the user for the name of an NNTP server to use.
3072 As opposed to `gnus', this command will not connect to the local server."
3073   (interactive "P")
3074   (setq gnus-group-use-permanent-levels t)
3075   (gnus (or arg (1- gnus-level-default-subscribed)) t))
3076
3077 (defalias '\(ding\) 'gnus)
3078
3079 ;;;###autoload
3080 (defun gnus (&optional arg dont-connect)
3081   "Read network news.
3082 If ARG is non-nil and a positive number, Gnus will use that as the
3083 startup level. If ARG is non-nil and not a positive number, Gnus will
3084 prompt the user for the name of an NNTP server to use."
3085   (interactive "P")
3086   (if (get-buffer gnus-group-buffer)
3087       (progn
3088         (switch-to-buffer gnus-group-buffer)
3089         (gnus-group-get-new-news))
3090     (gnus-clear-system)
3091     (nnheader-init-server-buffer)
3092     (gnus-read-init-file)
3093     (let ((level (and arg (numberp arg) (> arg 0) arg))
3094           did-connect)
3095       (unwind-protect
3096           (progn
3097             (gnus-group-setup-buffer)
3098             (or dont-connect 
3099                 (setq did-connect
3100                       (gnus-start-news-server (and arg (not level))))))
3101         (if (and (not dont-connect) 
3102                  (not did-connect))
3103             (gnus-group-quit)
3104           (run-hooks 'gnus-startup-hook)
3105           ;; NNTP server is successfully open. 
3106           (gnus-update-format-specifications)
3107           (gnus-summary-make-display-table)
3108           (let ((buffer-read-only nil))
3109             (erase-buffer)
3110             (if (not gnus-inhibit-startup-message)
3111                 (progn
3112                   (gnus-group-startup-message)
3113                   (sit-for 0))))
3114           (gnus-setup-news nil level)
3115           (and gnus-use-dribble-file (gnus-dribble-open))
3116           (gnus-group-list-groups level)
3117           (gnus-configure-windows 'group))))))
3118
3119 (defun gnus-group-startup-message (&optional x y)
3120   "Insert startup message in current buffer."
3121   ;; Insert the message.
3122   (erase-buffer)
3123   (insert
3124    (format "
3125     %s
3126            A newsreader 
3127       for GNU Emacs
3128
3129         Based on GNUS 
3130              written by 
3131      Masanobu UMEDA
3132
3133     Lars Magne 
3134          Ingebrigtsen 
3135       larsi@ifi.uio.no
3136
3137            gnus-version))
3138   ;; And then hack it.
3139   ;; 18 is the longest line.
3140   (indent-rigidly (point-min) (point-max) 
3141                   (/ (max (- (window-width) (or x 28)) 0) 2))
3142   (goto-char (point-min))
3143   ;; +4 is fuzzy factor.
3144   (insert-char ?\n (/ (max (- (window-height) (or y 12)) 0) 2)))
3145
3146 (defun gnus-group-setup-buffer ()
3147   (or (get-buffer gnus-group-buffer)
3148       (progn
3149         (switch-to-buffer gnus-group-buffer)
3150         (gnus-add-current-to-buffer-list)
3151         (gnus-group-mode)
3152         (and gnus-carpal (gnus-carpal-setup-buffer 'group)))))
3153
3154 (defun gnus-group-list-groups (level &optional unread)
3155   "List newsgroups with level LEVEL or lower that have unread articles.
3156 Default is all subscribed groups.
3157 If argument UNREAD is non-nil, groups with no unread articles are also listed."
3158   (interactive (list (and current-prefix-arg
3159                           (prefix-numeric-value current-prefix-arg))))
3160   (if gnus-group-use-permanent-levels
3161       (progn
3162         (setq gnus-group-default-list-level 
3163               (or level gnus-group-default-list-level))
3164         (setq level (or gnus-group-default-list-level gnus-level-subscribed)))
3165     (setq level (or level gnus-group-default-list-level 
3166                     gnus-level-subscribed)))
3167   (gnus-group-setup-buffer)     ;May call from out of group buffer
3168   (let ((case-fold-search nil)
3169         (group (gnus-group-group-name)))
3170     (funcall gnus-group-prepare-function level unread nil)
3171     (if (zerop (buffer-size))
3172         (gnus-message 5 gnus-no-groups-message)
3173       (goto-char (point-min))
3174       (if (not group)
3175           ;; Go to the first group with unread articles.
3176           (gnus-group-search-forward nil nil nil t)
3177         ;; Find the right group to put point on. If the current group
3178         ;; has disapeared in the new listing, try to find the next
3179         ;; one. If no next one can be found, just leave point at the
3180         ;; first newsgroup in the buffer.
3181         (if (not (gnus-goto-char
3182                   (text-property-any (point-min) (point-max) 
3183                                      'gnus-group (intern group))))
3184             (let ((newsrc (nthcdr 3 (gnus-gethash group gnus-newsrc-hashtb))))
3185               (while (and newsrc
3186                           (not (gnus-goto-char 
3187                                 (text-property-any 
3188                                  (point-min) (point-max) 'gnus-group 
3189                                  (intern (car (car newsrc)))))))
3190                 (setq newsrc (cdr newsrc)))
3191               (or newsrc (progn (goto-char (point-max))
3192                                 (forward-line -1))))))
3193       ;; Adjust cursor point.
3194       (gnus-group-position-cursor))))
3195
3196 (defun gnus-group-prepare-flat (level &optional all lowest regexp) 
3197   "List all newsgroups with unread articles of level LEVEL or lower.
3198 If ALL is non-nil, list groups that have no unread articles.
3199 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher.
3200 If REGEXP, only list groups matching REGEXP."
3201   (set-buffer gnus-group-buffer)
3202   (let ((buffer-read-only nil)
3203         (newsrc (cdr gnus-newsrc-alist))
3204         (lowest (or lowest 1))
3205         info clevel unread group)
3206     (erase-buffer)
3207     (if (< lowest gnus-level-zombie)
3208         ;; List living groups.
3209         (while newsrc
3210           (setq info (car newsrc)
3211                 group (car info)
3212                 newsrc (cdr newsrc)
3213                 unread (car (gnus-gethash group gnus-newsrc-hashtb)))
3214           (and unread ; This group might be bogus
3215                (or (not regexp)
3216                    (string-match regexp group))
3217                (<= (setq clevel (car (cdr info))) level) 
3218                (>= clevel lowest)
3219                (or all            ; We list all groups?
3220                    (eq unread t)  ; We list unactivated groups
3221                    (> unread 0)   ; We list groups with unread articles
3222                    (cdr (assq 'tick (nth 3 info)))) ; And groups with tickeds
3223                (gnus-group-insert-group-line 
3224                 nil group (car (cdr info)) (nth 3 info) unread (nth 4 info)))))
3225
3226     ;; List dead groups.
3227     (and (>= level gnus-level-zombie) (<= lowest gnus-level-zombie)
3228          (gnus-group-prepare-flat-list-dead 
3229           (setq gnus-zombie-list (sort gnus-zombie-list 'string<)) 
3230           gnus-level-zombie ?Z
3231           regexp))
3232     (and (>= level gnus-level-killed) (<= lowest gnus-level-killed)
3233          (gnus-group-prepare-flat-list-dead 
3234           (setq gnus-killed-list (sort gnus-killed-list 'string<)) 
3235           gnus-level-killed ?K regexp))
3236
3237     (gnus-group-set-mode-line)
3238     (setq gnus-have-all-newsgroups all)
3239     (run-hooks 'gnus-group-prepare-hook)))
3240
3241 (defun gnus-group-prepare-flat-list-dead (groups level mark regexp)
3242   ;; List zombies and killed lists somehwat faster, which was
3243   ;; suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>. It does
3244   ;; this by ignoring the group format specification altogether.
3245   (let (group beg)
3246     (while groups
3247       (setq group (car groups)
3248             groups (cdr groups))
3249       (if (or (not regexp)
3250               (string-match regexp group))
3251           (progn
3252             (setq beg (point))
3253             (insert (format " %c     *: %s\n" mark group))
3254             (add-text-properties 
3255              beg (1+ beg) 
3256              (list 'gnus-group (intern group)
3257                    'gnus-unread t
3258                    'gnus-level level)))))))
3259
3260 (defun gnus-group-real-name (group)
3261   "Find the real name of a foreign newsgroup."
3262   (if (string-match ":[^:]+$" group)
3263       (substring group (1+ (match-beginning 0)))
3264     group))
3265
3266 (defun gnus-group-prefixed-name (group method)
3267   "Return the whole name from GROUP and METHOD."
3268   (and (stringp method) (setq method (gnus-server-to-method method)))
3269   (concat (format "%s" (car method))
3270           (if (and 
3271                (assoc (format "%s" (car method)) (gnus-methods-using 'address))
3272                (not (string= (nth 1 method) "")))
3273               (concat "+" (nth 1 method)))
3274           ":" group))
3275
3276 (defun gnus-group-real-prefix (group)
3277   "Return the prefix of the current group name."
3278   (if (string-match "^[^:]+:" group)
3279       (substring group 0 (match-end 0))
3280     ""))
3281
3282 (defun gnus-group-method-name (group)
3283   "Return the method used for selecting GROUP."
3284   (let ((prefix (gnus-group-real-prefix group)))
3285     (if (equal prefix "")
3286         gnus-select-method
3287       (if (string-match "^[^\\+]+\\+" prefix)
3288           (list (intern (substring prefix 0 (1- (match-end 0))))
3289                 (substring prefix (match-end 0) (1- (length prefix))))
3290         (list (intern (substring prefix 0 (1- (length prefix)))) "")))))
3291
3292 (defun gnus-group-foreign-p (group)
3293   "Return nil if GROUP is native, non-nil if it is foreign."
3294   (string-match ":" group))
3295
3296 (defun gnus-group-set-info (info &optional method-only-group part)
3297   (let* ((entry (gnus-gethash
3298                  (or method-only-group (car info)) gnus-newsrc-hashtb))
3299          (part-info info)
3300          (info (if method-only-group (nth 2 entry) info)))
3301     (if (not method-only-group)
3302         ()
3303       (or entry
3304           (error "Trying to change non-existent group %s" method-only-group))
3305       ;; We have recevied parts of the actual group info - either the
3306       ;; select method or the group parameters.  We first check
3307       ;; whether we have to extend the info, and if so, do that.
3308       (let ((len (length info))
3309             (total (if (eq part 'method) 5 6)))
3310         (and (< len total)
3311              (setcdr (nthcdr (1- len) info)
3312                      (make-list (- total len) nil)))
3313         ;; Then we enter the new info.
3314         (setcar (nthcdr (1- total) info) part-info)))
3315     ;; We uncompress some lists of marked articles.
3316     (let (marked)
3317       (if (not (setq marked (nth 3 info)))
3318           ()
3319         (while marked
3320           (or (eq 'score (car (car marked)))
3321               (eq 'bookmark (car (car marked)))
3322               (eq 'killed (car (car marked)))
3323               (setcdr (car marked) 
3324                       (gnus-uncompress-range (cdr (car marked)))))
3325           (setq marked (cdr marked)))))
3326     (if entry
3327         ()
3328       ;; This is a new group, so we just create it.
3329       (save-excursion
3330         (set-buffer gnus-group-buffer)
3331         (if (nth 4 info)
3332             ;; It's a foreign group...
3333             (gnus-group-make-group 
3334              (gnus-group-real-name (car info))
3335              (prin1-to-string (car (nth 4 info)))
3336              (nth 1 (nth 4 info)))
3337           ;; It's a native group.
3338           (gnus-group-make-group
3339            (car info)
3340            (prin1-to-string (car gnus-select-method))
3341            (nth 1 gnus-select-method)))
3342         (gnus-message 6 "Note: New group created")
3343         (setq entry 
3344               (gnus-gethash (gnus-group-prefixed-name 
3345                              (gnus-group-real-name (car info))
3346                              (or (nth 4 info) gnus-select-method))
3347                             gnus-newsrc-hashtb))))
3348     ;; Whether it was a new group or not, we now have the entry, so we
3349     ;; can do the update.
3350     (if entry
3351         (progn
3352           (setcar (nthcdr 2 entry) info)
3353           (if (and (not (eq (car entry) t)) 
3354                    (gnus-gethash (car info) gnus-active-hashtb))
3355               (let ((marked (nth 3 info)))
3356                 (setcar entry 
3357                         (max 0 (- (length (gnus-list-of-unread-articles 
3358                                            (car info)))
3359                                   (length (cdr (assq 'tick marked)))
3360                                   (length (cdr (assq 'dormant marked)))))))))
3361       (error "No such group: %s" (car info)))))
3362
3363 (defun gnus-group-set-method-info (group select-method)
3364   (gnus-group-set-info select-method group 'method))
3365
3366 (defun gnus-group-set-params-info (group params)
3367   (gnus-group-set-info params group 'params))
3368
3369 (defun gnus-group-update-group-line ()
3370   "This function updates the current line in the newsgroup buffer and
3371 moves the point to the colon."
3372   (let* ((buffer-read-only nil)
3373          (group (gnus-group-group-name))
3374          (entry (and group (gnus-gethash group gnus-newsrc-hashtb))))
3375     (if entry
3376         (gnus-dribble-enter 
3377          (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
3378                  ")")))
3379     (beginning-of-line)
3380     (delete-region (point) (progn (forward-line 1) (point)))
3381     (gnus-group-insert-group-line-info group)
3382     (forward-line -1)
3383     (gnus-group-position-cursor)))
3384
3385 (defun gnus-group-insert-group-line-info (group)
3386   (let ((entry (gnus-gethash group gnus-newsrc-hashtb)) 
3387         active info)
3388     (if entry
3389         (progn
3390           (setq info (nth 2 entry))
3391           (gnus-group-insert-group-line 
3392            nil group (nth 1 info) (nth 3 info) (car entry) (nth 4 info)))
3393       (setq active (gnus-gethash group gnus-active-hashtb))
3394       (gnus-group-insert-group-line 
3395        nil group (if (member group gnus-zombie-list) gnus-level-zombie
3396                    gnus-level-killed)
3397        nil (if active (- (1+ (cdr active)) (car active)) 0) nil))))
3398
3399 (defun gnus-group-insert-group-line (gformat group level marked number method)
3400   (let* ((gformat (or gformat gnus-group-line-format-spec))
3401          (active (gnus-gethash group gnus-active-hashtb))
3402          (number-total (if active (1+ (- (cdr active) (car active))) 0))
3403          (number-of-dormant (length (cdr (assq 'dormant marked))))
3404          (number-of-ticked (length (cdr (assq 'tick marked))))
3405          (number-of-ticked-and-dormant
3406           (+ number-of-ticked number-of-dormant))
3407          (number-of-unread-unticked 
3408           (if (numberp number) (int-to-string (max 0 number))
3409             "*"))
3410          (number-of-read
3411           (if (numberp number)
3412               (max 0 (- number-total number))
3413             "*"))
3414          (subscribed (cond ((<= level gnus-level-subscribed) ? )
3415                            ((<= level gnus-level-unsubscribed) ?U)
3416                            ((= level gnus-level-zombie) ?Z)
3417                            (t ?K)))
3418          (qualified-group (gnus-group-real-name group))
3419          (newsgroup-description 
3420           (if gnus-description-hashtb
3421               (or (gnus-gethash group gnus-description-hashtb) "")
3422             ""))
3423          (moderated (if (member group gnus-moderated-list) ?m ? ))
3424          (moderated-string (if (eq moderated ?m) "(m)" ""))
3425          (method (gnus-server-get-method group method))
3426          (news-server (or (car (cdr method)) ""))
3427          (news-method (or (car method) ""))
3428          (news-method-string 
3429           (if method (format "(%s:%s)" (car method) (car (cdr method))) ""))
3430          (marked (if (and 
3431                       (numberp number) 
3432                       (zerop number)
3433                       (> number-of-ticked 0))
3434                      ?* ? ))
3435          (number (if (eq number t) "*" (+ number number-of-dormant 
3436                                           number-of-ticked)))
3437          (process-marked (if (member qualified-group gnus-group-marked)
3438                              gnus-process-mark ? ))
3439          (buffer-read-only nil)
3440          b)
3441     (beginning-of-line)
3442     (setq b (point))
3443     ;; Insert the text.
3444     (insert (eval gformat))
3445
3446     (add-text-properties 
3447      b (1+ b) (list 'gnus-group (intern group)
3448                     'gnus-unread (if (numberp number)
3449                                      (string-to-int number-of-unread-unticked)
3450                                    t)
3451                     'gnus-marked marked
3452                     'gnus-level level))))
3453
3454 (defun gnus-group-update-group (group &optional visible-only)
3455   "Update newsgroup info of GROUP.
3456 If VISIBLE-ONLY is non-nil, the group won't be displayed if it isn't already."
3457   (save-excursion
3458     (set-buffer gnus-group-buffer)
3459     (let ((buffer-read-only nil)
3460           visible)
3461       (let ((entry (gnus-gethash group gnus-newsrc-hashtb)))
3462         (if entry
3463             (gnus-dribble-enter 
3464              (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
3465                      ")"))))
3466       ;; Buffer may be narrowed.
3467       (save-restriction
3468         (widen)
3469         ;; Search a line to modify.  If the buffer is large, the search
3470         ;; takes long time.  In most cases, current point is on the line
3471         ;; we are looking for.  So, first of all, check current line. 
3472         (if (or (progn
3473                   (beginning-of-line)
3474                   (eq (get-text-property (point) 'gnus-group)
3475                       (intern group)))
3476                 (progn
3477                   (gnus-goto-char 
3478                    (text-property-any 
3479                     (point-min) (point-max) 'gnus-group (intern group)))))
3480             ;; GROUP is listed in current buffer. So, delete old line.
3481             (progn
3482               (setq visible t)
3483               (beginning-of-line)
3484               (delete-region (point) (progn (forward-line 1) (point))))
3485           ;; No such line in the buffer, find out where it's supposed to
3486           ;; go, and insert it there (or at the end of the buffer).
3487           ;; Fix by Per Abrahamsen <amanda@iesd.auc.dk>.
3488           (or visible-only
3489               (let ((entry 
3490                      (cdr (cdr (gnus-gethash group gnus-newsrc-hashtb)))))
3491                 (while (and entry
3492                             (car entry)
3493                             (not
3494                              (gnus-goto-char
3495                               (text-property-any
3496                                (point-min) (point-max) 
3497                                'gnus-group (intern (car (car entry)))))))
3498                   (setq entry (cdr entry)))
3499                 (or entry (goto-char (point-max)))))))
3500       (if (or visible (not visible-only))
3501           (gnus-group-insert-group-line-info group))
3502       (gnus-group-set-mode-line))))
3503
3504 (defun gnus-group-set-mode-line ()
3505   (if (memq 'group gnus-updated-mode-lines)
3506       (let* ((gformat (or gnus-group-mode-line-format-spec
3507                           (setq gnus-group-mode-line-format-spec
3508                                 (gnus-parse-format 
3509                                  gnus-group-mode-line-format 
3510                                  gnus-group-mode-line-format-alist))))
3511              (news-server (car (cdr gnus-select-method)))
3512              (news-method (car gnus-select-method))
3513              (max-len 60)
3514              (mode-string (eval gformat)))
3515         (setq mode-string (eval gformat))
3516         (if (> (length mode-string) max-len) 
3517             (setq mode-string (substring mode-string 0 (- max-len 4))))
3518         (setq mode-line-buffer-identification mode-string)
3519         (set-buffer-modified-p t))))
3520
3521 (defun gnus-group-group-name ()
3522   "Get the name of the newsgroup on the current line."
3523   (let ((group (get-text-property (gnus-point-at-bol) 'gnus-group)))
3524     (and group (symbol-name group))))
3525
3526 (defun gnus-group-group-level ()
3527   "Get the level of the newsgroup on the current line."
3528   (get-text-property (gnus-point-at-bol) 'gnus-level))
3529
3530 (defun gnus-group-search-forward (&optional backward all level first-too)
3531   "Find the next newsgroup with unread articles.
3532 If BACKWARD is non-nil, find the previous newsgroup instead.
3533 If ALL is non-nil, just find any newsgroup.
3534 If LEVEL is non-nil, find group with level LEVEL, or higher if no such
3535 group exists.
3536 If FIRST-TOO, the current line is also eligible as a target."
3537   (let ((way (if backward -1 1))
3538         (low 10)
3539         (beg (point))
3540         pos found)
3541     (if (and backward (progn (beginning-of-line)) (bobp))
3542         nil
3543       (or first-too (forward-line way))
3544       (while (and 
3545               (not (eobp))
3546               (not (setq 
3547                     found 
3548                     (and (or all
3549                              (and
3550                               (let ((unread 
3551                                      (get-text-property (point) 'gnus-unread)))
3552                                 (or (eq unread t) (and unread (> unread 0))))
3553                               (let ((lev (get-text-property
3554                                           (point) 'gnus-level)))
3555                                 (and lev (<= (get-text-property 
3556                                               (point) 'gnus-level)
3557                                              gnus-level-subscribed)))))
3558                          (or (not level)
3559                              (let ((lev (get-text-property (point) 'gnus-level)))
3560                                (if (and lev (<= lev level))
3561                                    t
3562                                  (if (< lev low)
3563                                      (progn
3564                                        (setq low lev)
3565                                        (setq pos (point))))
3566                                  nil))))))
3567               (zerop (forward-line way)))))
3568     (if found 
3569         (progn (gnus-group-position-cursor) t)
3570       (if pos (goto-char pos) (goto-char beg))
3571       nil)))
3572
3573 ;;; Gnus group mode commands
3574
3575 ;; Group marking.
3576
3577 (defun gnus-group-mark-group (n &optional unmark)
3578   "Mark the current group."
3579   (interactive "p")
3580   (let ((buffer-read-only nil)
3581         group)
3582     (while 
3583         (and (> n 0) 
3584              (setq group (gnus-group-group-name))
3585              (progn
3586                (beginning-of-line)
3587                (forward-char 2)
3588                (delete-char 1)
3589                (if unmark
3590                    (progn
3591                      (insert " ")
3592                      (setq gnus-group-marked (delete group gnus-group-marked)))
3593                  (insert "#")
3594                  (setq gnus-group-marked
3595                        (cons group (delete group gnus-group-marked))))
3596                t)
3597              (zerop (gnus-group-next-group 1)))
3598       (setq n (1- n)))
3599     (gnus-summary-position-cursor)
3600     n))
3601
3602 (defun gnus-group-unmark-group (n)
3603   "Remove the mark from the current group."
3604   (interactive "p")
3605   (gnus-group-mark-group n 'unmark))
3606
3607 (defun gnus-group-mark-region (unmark beg end)
3608   "Mark all groups between point and mark.
3609 If UNMARK, remove the mark instead."
3610   (interactive "P\nr")
3611   (let ((num (count-lines beg end)))
3612     (save-excursion
3613       (goto-char beg)
3614       (- num (gnus-group-mark-group num unmark)))))
3615
3616 (defun gnus-group-remove-mark (group)
3617   (and (gnus-group-goto-group group)
3618        (save-excursion
3619          (gnus-group-mark-group 1 'unmark))))
3620
3621 ;; Return a list of groups to work on.  Take into consideration N (the
3622 ;; prefix) and the list of marked groups.
3623 (defun gnus-group-process-prefix (n)
3624   (cond (n
3625          (setq n (prefix-numeric-value n))
3626          ;; There is a prefix, so we return a list of the N next
3627          ;; groups. 
3628          (let ((way (if (< n 0) -1 1))
3629                (n (abs n))
3630                group groups)
3631            (save-excursion
3632              (while (and (> n 0)
3633                          (setq group (gnus-group-group-name)))
3634                (setq groups (cons group groups))
3635                (setq n (1- n))
3636                (forward-line way)))
3637            (nreverse groups)))
3638         (gnus-group-marked
3639          ;; No prefix, but a list of marked articles.
3640          (reverse gnus-group-marked))
3641         (t
3642          ;; Neither marked articles or a prefix, so we return the
3643          ;; current group.
3644          (let ((group (gnus-group-group-name)))
3645            (and group (list group))))))
3646
3647 ;; Selecting groups.
3648
3649 (defun gnus-group-read-group (all &optional no-article group)
3650   "Read news in this newsgroup.
3651 If argument ALL is non-nil, already read articles become readable.
3652 If optional argument NO-ARTICLE is non-nil, no article body is displayed."
3653   (interactive "P")
3654   (let ((group (or group (gnus-group-group-name)))
3655         number active marked entry)
3656     (or group (error "No group on current line"))
3657     (setq marked 
3658           (nth 3 (nth 2 (setq entry (gnus-gethash group gnus-newsrc-hashtb)))))
3659     ;; This group might be a dead group. In that case we have to get
3660     ;; the number of unread articles from `gnus-active-hashtb'.
3661     (if entry
3662         (setq number (car entry))
3663       (if (setq active (gnus-gethash group gnus-active-hashtb))
3664           (setq number (- (1+ (cdr active)) (car active)))))
3665     (gnus-summary-read-group 
3666      group (or all (and (numberp number) 
3667                         (zerop (+ number (length (cdr (assq 'tick marked)))
3668                                   (length (cdr (assq 'dormant marked)))))))
3669      no-article)))
3670
3671 (defun gnus-group-select-group (all)
3672   "Select this newsgroup.
3673 No article is selected automatically.
3674 If argument ALL is non-nil, already read articles become readable."
3675   (interactive "P")
3676   (gnus-group-read-group all t))
3677
3678 ;; Enter a group that is not in the group buffer. Non-nil is returned
3679 ;; if selection was successful.
3680 (defun gnus-group-read-ephemeral-group 
3681   (group method &optional activate quit-config)
3682   (let ((group (if (gnus-group-foreign-p group) group
3683                  (gnus-group-prefixed-name group method))))
3684     (gnus-sethash 
3685      group
3686      (list t nil (list group gnus-level-default-subscribed nil nil 
3687                        (append method
3688                                (list
3689                                 (list 'quit-config 
3690                                       (if quit-config quit-config
3691                                         (cons (current-buffer) 'summary)))))))
3692      gnus-newsrc-hashtb)
3693     (set-buffer gnus-group-buffer)
3694     (or (gnus-server-opened method)
3695         (gnus-open-server method)
3696         (error "Unable to contact server: %s" (gnus-status-message method)))
3697     (if activate (gnus-request-group group))
3698     (condition-case ()
3699         (gnus-group-read-group t t group)
3700       (error nil)
3701       (quit nil))
3702     (not (equal major-mode 'gnus-group-mode))))
3703   
3704 (defun gnus-group-jump-to-group (group)
3705   "Jump to newsgroup GROUP."
3706   (interactive 
3707    (list (completing-read 
3708           "Group: " gnus-active-hashtb nil (not (not gnus-read-active-file)))))
3709
3710   (if (equal group "")
3711       (error "Empty group name"))
3712
3713   (let ((b (text-property-any 
3714             (point-min) (point-max) 'gnus-group (intern group))))
3715     (if b
3716         ;; Either go to the line in the group buffer...
3717         (goto-char b)
3718       ;; ... or insert the line.
3719       (or
3720        (gnus-gethash group gnus-active-hashtb)
3721        (gnus-activate-newsgroup group)
3722        (error "%s error: %s" group (gnus-status-message group)))
3723
3724       (gnus-group-update-group group)
3725       (goto-char (text-property-any 
3726                   (point-min) (point-max) 'gnus-group (intern group)))))
3727   ;; Adjust cursor point.
3728   (gnus-group-position-cursor))
3729
3730 (defun gnus-group-goto-group (group)
3731   "Goto to newsgroup GROUP."
3732   (let ((b (text-property-any (point-min) (point-max) 
3733                               'gnus-group (intern group))))
3734     (and b (goto-char b))))
3735
3736 (defun gnus-group-next-group (n)
3737   "Go to next N'th newsgroup.
3738 If N is negative, search backward instead.
3739 Returns the difference between N and the number of skips actually
3740 done."
3741   (interactive "p")
3742   (gnus-group-next-unread-group n t))
3743
3744 (defun gnus-group-next-unread-group (n &optional all level)
3745   "Go to next N'th unread newsgroup.
3746 If N is negative, search backward instead.
3747 If ALL is non-nil, choose any newsgroup, unread or not.
3748 If LEVEL is non-nil, choose the next group with level LEVEL, or, if no
3749 such group can be found, the next group with a level higher than
3750 LEVEL.
3751 Returns the difference between N and the number of skips actually
3752 made."
3753   (interactive "p")
3754   (let ((backward (< n 0))
3755         (n (abs n)))
3756     (while (and (> n 0)
3757                 (gnus-group-search-forward 
3758                  backward (or (not gnus-group-goto-unread) all) level))
3759       (setq n (1- n)))
3760     (if (/= 0 n) (gnus-message 7 "No more%s newsgroups%s" (if all "" " unread")
3761                                (if level " on this level or higher" "")))
3762     n))
3763
3764 (defun gnus-group-prev-group (n)
3765   "Go to previous N'th newsgroup.
3766 Returns the difference between N and the number of skips actually
3767 done."
3768   (interactive "p")
3769   (gnus-group-next-unread-group (- n) t))
3770
3771 (defun gnus-group-prev-unread-group (n)
3772   "Go to previous N'th unread newsgroup.
3773 Returns the difference between N and the number of skips actually
3774 done."  
3775   (interactive "p")
3776   (gnus-group-next-unread-group (- n)))
3777
3778 (defun gnus-group-next-unread-group-same-level (n)
3779   "Go to next N'th unread newsgroup on the same level.
3780 If N is negative, search backward instead.
3781 Returns the difference between N and the number of skips actually
3782 done."
3783   (interactive "p")
3784   (gnus-group-next-unread-group n t (gnus-group-group-level))
3785   (gnus-group-position-cursor))
3786
3787 (defun gnus-group-prev-unread-group-same-level (n)
3788   "Go to next N'th unread newsgroup on the same level.
3789 Returns the difference between N and the number of skips actually
3790 done."
3791   (interactive "p")
3792   (gnus-group-next-unread-group (- n) t (gnus-group-group-level))
3793   (gnus-group-position-cursor))
3794
3795 (defun gnus-group-best-unread-group (&optional exclude-group)
3796   "Go to the group with the highest level.
3797 If EXCLUDE-GROUP, do not go to that group."
3798   (interactive)
3799   (goto-char (point-min))
3800   (let ((best 100000)
3801         unread best-point)
3802     (while (setq unread (get-text-property (point) 'gnus-unread))
3803       (if (and (numberp unread) (> unread 0))
3804           (progn
3805             (if (and (< (get-text-property (point) 'gnus-level) best)
3806                      (or (not exclude-group)
3807                          (not (equal exclude-group (gnus-group-group-name)))))
3808                 (progn 
3809                   (setq best (get-text-property (point) 'gnus-level))
3810                   (setq best-point (point))))))
3811       (forward-line 1))
3812     (if best-point (goto-char best-point))
3813     (gnus-summary-position-cursor)
3814     (and best-point (gnus-group-group-name))))
3815
3816 (defun gnus-group-first-unread-group ()
3817   "Go to the first group with unread articles."
3818   (interactive)
3819   (goto-char (point-min))
3820   (or (not (zerop (or (get-text-property (point) 'gnus-unread) 0)))
3821       (gnus-group-next-unread-group 1))
3822   (gnus-group-position-cursor))
3823
3824 (defun gnus-group-enter-server-mode ()
3825   "Jump to the server buffer."
3826   (interactive)
3827   (gnus-server-setup-buffer)
3828   (gnus-configure-windows 'server)
3829   (gnus-server-prepare))
3830
3831 (defun gnus-group-make-group (name method &optional address)
3832   "Add a new newsgroup.
3833 The user will be prompted for a NAME, for a select METHOD, and an
3834 ADDRESS."
3835   (interactive
3836    (cons 
3837     (read-string "Group name: ")
3838     (let ((method
3839            (completing-read 
3840             "Method: " (append gnus-valid-select-methods gnus-server-alist)
3841             nil t)))
3842       (if (assoc method gnus-valid-select-methods)
3843           (list method
3844                 (if (memq 'prompt-address
3845                           (assoc method gnus-valid-select-methods))
3846                     (read-string "Address: ")
3847                   ""))
3848         (list method nil)))))
3849   
3850   (let* ((meth (if address (list (intern method) address) method))
3851          (nname (gnus-group-prefixed-name name meth))
3852          info)
3853     (and (gnus-gethash nname gnus-newsrc-hashtb)
3854          (error "Group %s already exists" nname))
3855     (gnus-group-change-level 
3856      (setq info (list t nname gnus-level-default-subscribed nil nil meth))
3857      gnus-level-default-subscribed gnus-level-killed 
3858      (and (gnus-group-group-name)
3859           (gnus-gethash (gnus-group-group-name)
3860                         gnus-newsrc-hashtb))
3861      t)
3862     (gnus-sethash nname '(0 . 0) gnus-active-hashtb)
3863     (gnus-dribble-enter 
3864      (concat "(gnus-group-set-info '" (prin1-to-string (cdr info)) ")"))
3865     (gnus-group-insert-group-line-info nname)
3866
3867     (and (gnus-check-backend-function 'request-create-group nname)
3868          (gnus-request-create-group nname))))
3869
3870 (defun gnus-group-edit-group (group &optional part)
3871   "Edit the group on the current line."
3872   (interactive (list (gnus-group-group-name)))
3873   (let ((done-func '(lambda () 
3874                       "Exit editing mode and update the information."
3875                       (interactive)
3876                       (gnus-group-edit-group-done 'part 'group)))
3877         (part (or part 'info))
3878         (winconf (current-window-configuration))
3879         info)
3880     (or group (error "No group on current line"))
3881     (or (setq info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
3882         (error "Killed group; can't be edited"))
3883     (set-buffer (get-buffer-create gnus-group-edit-buffer))
3884     (gnus-configure-windows 'edit-group)
3885     (gnus-add-current-to-buffer-list)
3886     (emacs-lisp-mode)
3887     ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3888     (use-local-map (copy-keymap emacs-lisp-mode-map))
3889     (local-set-key "\C-c\C-c" done-func)
3890     (make-local-variable 'gnus-prev-winconf)
3891     (setq gnus-prev-winconf winconf)
3892     ;; We modify the func to let it know what part it is editing.
3893     (setcar (cdr (nth 4 done-func)) (list 'quote part))
3894     (setcar (cdr (cdr (nth 4 done-func))) group)
3895     (erase-buffer)
3896     (insert
3897      (cond 
3898       ((eq part 'method)
3899        ";; Type `C-c C-c' after editing the select method.\n\n")
3900       ((eq part 'params)
3901        ";; Type `C-c C-c' after editing the group parameters.\n\n")
3902       ((eq part 'info)
3903        ";; Type `C-c C-c' after editing the group info.\n\n")))
3904     (let ((cinfo (gnus-copy-sequence info))
3905           marked)
3906       (if (not (setq marked (nth 3 cinfo)))
3907           ()
3908         (while marked
3909           (or (eq 'score (car (car marked)))
3910               (eq 'bookmark (car (car marked)))
3911               (eq 'killed (car (car marked)))
3912               (not (numberp (car (cdr (car marked)))))
3913               (setcdr (car marked) 
3914                       (gnus-compress-sequence (sort (cdr (car marked)) '<) t)))
3915           (setq marked (cdr marked))))
3916       (insert 
3917        (pp-to-string
3918         (cond ((eq part 'method)
3919                (or (nth 4 info) "native"))
3920               ((eq part 'params)
3921                (nth 5 info))
3922               (t
3923                cinfo)))
3924        "\n"))))
3925
3926 (defun gnus-group-edit-group-method (group)
3927   "Edit the select method of GROUP."
3928   (interactive (list (gnus-group-group-name)))
3929   (gnus-group-edit-group group 'method))
3930
3931 (defun gnus-group-edit-group-parameters (group)
3932   "Edit the group parameters of GROUP."
3933   (interactive (list (gnus-group-group-name)))
3934   (gnus-group-edit-group group 'params))
3935
3936 (defun gnus-group-edit-group-done (part group)
3937   "Get info from buffer, update variables and jump to the group buffer."
3938   (set-buffer (get-buffer-create gnus-group-edit-buffer))
3939   (goto-char (point-min))
3940   (let ((form (read (current-buffer)))
3941         (winconf gnus-prev-winconf))
3942     (if (eq part 'info) 
3943         (gnus-group-set-info form)
3944       (gnus-group-set-info form group part))
3945     (kill-buffer (current-buffer))
3946     (and winconf (set-window-configuration winconf))
3947     (set-buffer gnus-group-buffer)
3948     (gnus-group-update-group (gnus-group-group-name))
3949     (gnus-group-position-cursor)))
3950
3951 (defun gnus-group-make-help-group ()
3952   "Create the (ding) Gnus documentation group."
3953   (interactive)
3954   (let ((path load-path))
3955     (and (gnus-gethash (setq name (gnus-group-prefixed-name
3956                                    "gnus-help" '(nndoc "gnus-help")))
3957                        gnus-newsrc-hashtb)
3958          (error "Documentation group already exists"))
3959     (while (and path
3960                 (not (file-exists-p (concat (file-name-as-directory (car path))
3961                                             "doc.txt"))))
3962       (setq path (cdr path)))
3963     (or path (error "Couldn't find doc group"))
3964     (gnus-group-make-group 
3965      (gnus-group-real-name name)
3966      (list 'nndoc name
3967            (list 'nndoc-address (concat (file-name-as-directory (car path)) "doc.txt"))
3968            (list 'nndoc-article-type 'mbox))))
3969   (gnus-group-position-cursor))
3970
3971 (defun gnus-group-make-doc-group (file type)
3972   "Create a group that uses a single file as the source."
3973   (interactive 
3974    (list (read-file-name "File name: ") 
3975          (let ((err "")
3976                found char)
3977            (while (not found)
3978              (message "%sFile type (mbox, babyl, digest) [mbd]: " err)
3979              (setq found (cond ((= (setq char (read-char)) ?m) 'mbox)
3980                                ((= char ?b) 'babyl)
3981                                ((= char ?d) 'digest)
3982                                (t (setq mess "%c unknown. " char)
3983                                   nil))))
3984            found)))
3985   (let* ((file (expand-file-name file))
3986          (name (gnus-generate-new-group-name
3987                 (gnus-group-prefixed-name
3988                  (file-name-nondirectory file) '(nndoc "")))))
3989     (gnus-group-make-group 
3990      (gnus-group-real-name name)
3991      (list 'nndoc name
3992            (list 'nndoc-address file)
3993            (list 'nndoc-article-type type)))))
3994
3995 (defun gnus-group-make-archive-group ()
3996   "Create the (ding) Gnus archive group."
3997   (interactive)
3998   (and (gnus-gethash (gnus-group-prefixed-name "ding.archives" '(nndir ""))
3999                      gnus-newsrc-hashtb)
4000        (error "Archive group already exists"))
4001   (gnus-group-make-group "ding.archives" "nndir" gnus-group-archive-directory)
4002   (gnus-group-position-cursor))
4003
4004 (defun gnus-group-make-directory-group (dir)
4005   "Create an nndir group.
4006 The user will be prompted for a directory. The contents of this
4007 directory will be used as a newsgroup. The directory should contain
4008 mail messages or news articles in files that have numeric names."
4009   (interactive
4010    (list (read-file-name "Create group from directory: ")))
4011   (or (file-exists-p dir) (error "No such directory"))
4012   (or (file-directory-p dir) (error "Not a directory"))
4013   (gnus-group-make-group dir "nndir" dir)
4014   (gnus-group-position-cursor))
4015
4016 (defun gnus-group-make-kiboze-group (group address scores)
4017   "Create an nnkiboze group.
4018 The user will be prompted for a name, a regexp to match groups, and
4019 score file entries for articles to include in the group."
4020   (interactive
4021    (list
4022     (read-string "nnkiboze group name: ")
4023     (read-string "Source groups (regexp): ")
4024     (let ((headers (mapcar (lambda (group) (list group))
4025                            '("subject" "from" "number" "date" "message-id"
4026                              "references" "chars" "lines" "xref")))
4027           scores header regexp regexps)
4028       (while (not (equal "" (setq header (completing-read 
4029                                           "Match on header: " headers nil t))))
4030         (setq regexps nil)
4031         (while (not (equal "" (setq regexp (read-string 
4032                                             (format "Match on %s (string): "
4033                                                     header)))))
4034           (setq regexps (cons (list regexp nil nil 'r) regexps)))
4035         (setq scores (cons (cons header regexps) scores)))
4036       scores)))
4037   (gnus-group-make-group group "nnkiboze" address)
4038   (save-excursion
4039     (gnus-set-work-buffer)
4040     (let (emacs-lisp-mode-hook)
4041       (pp scores (current-buffer)))
4042     (write-region (point-min) (point-max) 
4043                   (concat (or gnus-kill-files-directory "~/News")
4044                           "nnkiboze:" group "." gnus-score-file-suffix)))
4045   (gnus-group-position-cursor))
4046
4047 (defun gnus-group-add-to-virtual (n vgroup)
4048   "Add the current group to a virtual group."
4049   (interactive
4050    (list current-prefix-arg
4051          (completing-read "Add to virtual group: " gnus-newsrc-hashtb nil t
4052                           "nnvirtual:")))
4053   (or (eq (car (gnus-find-method-for-group vgroup)) 'nnvirtual)
4054       (error "%s is not an nnvirtual group" vgroup))
4055   (let* ((groups (gnus-group-process-prefix n))
4056          (method (nth 4 (nth 2 (gnus-gethash vgroup gnus-newsrc-hashtb)))))
4057     (setcar (cdr method)
4058             (concat 
4059              (nth 1 method) "\\|"
4060              (mapconcat 
4061               (lambda (s) 
4062                 (gnus-group-remove-mark s)
4063                 (concat "\\(^" (regexp-quote s) "$\\)"))
4064               groups "\\|"))))
4065   (gnus-group-position-cursor))
4066
4067 (defun gnus-group-make-empty-virtual (group)
4068   "Create a new, fresh, empty virtual group."
4069   (interactive "sCreate new, empty virtual group: ")
4070   (let* ((method (list 'nnvirtual "^$"))
4071          (pgroup (gnus-group-prefixed-name group method)))
4072     ;; Check whether it exists already.
4073     (and (gnus-gethash pgroup gnus-newsrc-hashtb)
4074          (error "Group %s already exists." pgroup))
4075     ;; Subscribe the new group after the group on the current line.
4076     (gnus-subscribe-group pgroup (gnus-group-group-name) method)
4077     (gnus-group-update-group pgroup)
4078     (forward-line -1)
4079     (gnus-group-position-cursor)))
4080
4081 (defun gnus-group-enter-directory (dir)
4082   "Enter an ephemeral nneething group."
4083   (interactive "DDirectory to read: ")
4084   (let* ((method (list 'nneething dir))
4085          (leaf (gnus-group-prefixed-name
4086                 (file-name-nondirectory (directory-file-name dir))
4087                 method))
4088          (name (gnus-generate-new-group-name leaf))
4089          (num 0))
4090     (let ((nneething-read-only t))
4091       (or (gnus-group-read-ephemeral-group 
4092            name method t
4093            (cons (current-buffer) (if (eq major-mode 'gnus-summary-mode)
4094                                       'summary 'group)))
4095           (error "Couldn't enter %s" dir)))))
4096
4097 ;; Group sorting commands
4098 ;; Suggested by Joe Hildebrand <hildjj@idaho.fuentez.com>.
4099
4100 (defun gnus-group-sort-groups ()
4101   "Sort the group buffer using `gnus-group-sort-function'."
4102   (interactive)
4103   (setq gnus-newsrc-alist 
4104         (sort (cdr gnus-newsrc-alist) gnus-group-sort-function))
4105   (gnus-make-hashtable-from-newsrc-alist)
4106   (gnus-group-list-groups nil gnus-have-all-newsgroups))
4107
4108 (defun gnus-group-sort-by-alphabet (info1 info2)
4109   (string< (car info1) (car info2)))
4110
4111 (defun gnus-group-sort-by-unread (info1 info2)
4112   (let ((n1 (car (gnus-gethash (car info1) gnus-newsrc-hashtb)))
4113         (n2 (car (gnus-gethash (car info2) gnus-newsrc-hashtb))))
4114     (< (or (and (numberp n1) n1) 0)
4115        (or (and (numberp n2) n2) 0))))
4116
4117 (defun gnus-group-sort-by-level (info1 info2)
4118   (< (nth 1 info1) (nth 1 info2)))
4119
4120 ;; Group catching up.
4121
4122 (defun gnus-group-catchup-current (n &optional all)
4123   "Mark all articles not marked as unread in current newsgroup as read.
4124 If prefix argument N is numeric, the ARG next newsgroups will be
4125 caught up. If ALL is non-nil, marked articles will also be marked as
4126 read. Cross references (Xref: header) of articles are ignored.
4127 The difference between N and actual number of newsgroups that were
4128 caught up is returned."
4129   (interactive "P")
4130   (if (not (or (not gnus-interactive-catchup) ;Without confirmation?
4131                gnus-expert-user
4132                (gnus-y-or-n-p
4133                 (if all
4134                     "Do you really want to mark all articles as read? "
4135                   "Mark all unread articles as read? "))))
4136       n
4137     (let ((groups (gnus-group-process-prefix n))
4138           (ret 0))
4139       (while groups
4140         ;; Virtual groups have to be given special treatment. 
4141         (let ((method (gnus-find-method-for-group (car groups))))
4142           (if (eq 'nnvirtual (car method))
4143               (nnvirtual-catchup-group
4144                (gnus-group-real-name (car groups)) (nth 1 method) all)))
4145         (gnus-group-remove-mark (car groups))
4146         (if (prog1
4147                 (gnus-group-goto-group (car groups))
4148               (gnus-group-catchup (car groups) all))
4149             (gnus-group-update-group-line)
4150           (setq ret (1+ ret)))
4151         (setq groups (cdr groups)))
4152       (gnus-group-next-unread-group 1)
4153       ret)))
4154
4155 (defun gnus-group-catchup-current-all (n)
4156   "Mark all articles in current newsgroup as read.
4157 Cross references (Xref: header) of articles are ignored."
4158   (interactive "P")
4159   (gnus-group-catchup-current n 'all))
4160
4161 (defun gnus-group-catchup (group &optional all)
4162   "Mark all articles in GROUP as read.
4163 If ALL is non-nil, all articles are marked as read.
4164 The return value is the number of articles that were marked as read,
4165 or nil if no action could be taken."
4166   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4167          (num (car entry))
4168          (marked (nth 3 (nth 2 entry)))
4169          ticked)
4170     (if (not (numberp (car entry)))
4171         (gnus-message 1 "Can't catch up; non-active group")
4172       ;; Do the updating only if the newsgroup isn't killed.
4173       (if (not entry)
4174           ()
4175         (gnus-update-read-articles 
4176          group (and (not all) (append (cdr (assq 'tick marked))
4177                                       (cdr (assq 'dormant marked))))
4178          nil (and (not all) (cdr (assq 'tick marked))))
4179         (and all marked
4180              (setcar (nthcdr 3 (nth 2 entry)) 
4181                      (delq (assq 'dormant marked) 
4182                            (nth 3 (nth 2 entry)))))))
4183     num))
4184
4185 (defun gnus-group-expire-articles (n)
4186   "Expire all expirable articles in the current newsgroup."
4187   (interactive "P")
4188   (let ((groups (gnus-group-process-prefix n))
4189         group)
4190     (or groups (error "No groups to expire"))
4191     (while groups
4192       (setq group (car groups)
4193             groups (cdr groups))
4194       (gnus-group-remove-mark group)
4195       (if (not (gnus-check-backend-function 'request-expire-articles group))
4196           ()
4197         (let* ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
4198                (expirable (if (memq 'total-expire (nth 5 info))
4199                               (cons nil (gnus-list-of-read-articles group))
4200                             (assq 'expire (nth 3 info)))))
4201           (and expirable 
4202                (setcdr expirable
4203                        (gnus-request-expire-articles 
4204                         (cdr expirable) group))))))))
4205
4206 (defun gnus-group-expire-all-groups ()
4207   "Expire all expirable articles in all newsgroups."
4208   (interactive)
4209   (gnus-message 5 "Expiring...")
4210   (let ((gnus-group-marked (mapcar (lambda (info) (car info))
4211                                    (cdr gnus-newsrc-alist))))
4212     (gnus-group-expire-articles nil))
4213   (gnus-message 5 "Expiring...done"))
4214
4215 (defun gnus-group-set-current-level (n level)
4216   "Set the level of the next N groups to LEVEL."
4217   (interactive "P\nnLevel: ")
4218   (or (and (>= level 1) (<= level gnus-level-killed))
4219       (error "Illegal level: %d" level))
4220   (let ((groups (gnus-group-process-prefix n))
4221         group)
4222     (while groups
4223       (setq group (car groups)
4224             groups (cdr groups))
4225       (gnus-group-remove-mark group)
4226       (gnus-message 6 "Changed level of %s from %d to %d" 
4227                     group (gnus-group-group-level) level)
4228       (gnus-group-change-level group level
4229                                (gnus-group-group-level))
4230       (gnus-group-update-group-line)))
4231   (gnus-group-position-cursor))
4232
4233 (defun gnus-group-unsubscribe-current-group (n)
4234   "Toggle subscription of the current group.
4235 If given numerical prefix, toggle the N next groups."
4236   (interactive "P")
4237   (let ((groups (gnus-group-process-prefix n))
4238         group)
4239     (while groups
4240       (setq group (car groups)
4241             groups (cdr groups))
4242       (gnus-group-remove-mark group)
4243       (gnus-group-unsubscribe-group
4244        group (if (<= (gnus-group-group-level) gnus-level-subscribed)
4245                  gnus-level-default-unsubscribed
4246                gnus-level-default-subscribed))
4247       (gnus-group-update-group-line))
4248     (gnus-group-next-group 1)))
4249
4250 (defun gnus-group-unsubscribe-group (group &optional level)
4251   "Toggle subscribe from/to unsubscribe GROUP.
4252 New newsgroup is added to .newsrc automatically."
4253   (interactive
4254    (list (completing-read "Group: " gnus-active-hashtb nil 
4255                           gnus-have-read-active-file)))
4256   (let ((newsrc (gnus-gethash group gnus-newsrc-hashtb)))
4257     (cond (newsrc
4258            ;; Toggle subscription flag.
4259            (gnus-group-change-level 
4260             newsrc (if level level (if (<= (nth 1 (nth 2 newsrc)) 
4261                                            gnus-level-subscribed) 
4262                                        (1+ gnus-level-subscribed)
4263                                      gnus-level-default-subscribed)))
4264            (gnus-group-update-group group))
4265           ((and (stringp group)
4266                 (or (not gnus-have-read-active-file)
4267                     (gnus-gethash group gnus-active-hashtb)))
4268            ;; Add new newsgroup.
4269            (gnus-group-change-level 
4270             group 
4271             (if level level gnus-level-default-subscribed) 
4272             (or (and (member group gnus-zombie-list) 
4273                      gnus-level-zombie) 
4274                 gnus-level-killed)
4275             (and (gnus-group-group-name)
4276                  (gnus-gethash (gnus-group-group-name) gnus-newsrc-hashtb)))
4277            (gnus-group-update-group group))
4278           (t (error "No such newsgroup: %s" group)))
4279     (gnus-group-position-cursor)))
4280
4281 (defun gnus-group-transpose-groups (n)
4282   "Move the current newsgroup up N places.
4283 If given a negative prefix, move down instead. The difference between
4284 N and the number of steps taken is returned." 
4285   (interactive "p")
4286   (or (gnus-group-group-name)
4287       (error "No group on current line"))
4288   (gnus-group-kill-group 1)
4289   (prog1
4290       (forward-line (- n))
4291     (gnus-group-yank-group)
4292     (gnus-group-position-cursor)))
4293
4294 (defun gnus-group-kill-all-zombies ()
4295   "Kill all zombie newsgroups."
4296   (interactive)
4297   (setq gnus-killed-list (nconc gnus-zombie-list gnus-killed-list))
4298   (setq gnus-zombie-list nil)
4299   (funcall gnus-group-prepare-function gnus-level-subscribed nil nil)
4300   (goto-char (point-min))
4301   (gnus-group-position-cursor))
4302
4303 (defun gnus-group-kill-region (begin end)
4304   "Kill newsgroups in current region (excluding current point).
4305 The killed newsgroups can be yanked by using \\[gnus-group-yank-group]."
4306   (interactive "r")
4307   (let ((lines
4308          ;; Count lines.
4309          (save-excursion
4310            (count-lines
4311             (progn
4312               (goto-char begin)
4313               (beginning-of-line)
4314               (point))
4315             (progn
4316               (goto-char end)
4317               (beginning-of-line)
4318               (point))))))
4319     (goto-char begin)
4320     (beginning-of-line)                 ;Important when LINES < 1
4321     (gnus-group-kill-group lines)))
4322
4323 (defun gnus-group-kill-group (n)
4324   "The the next N groups.
4325 The killed newsgroups can be yanked by using \\[gnus-group-yank-group].
4326 However, only groups that were alive can be yanked; already killed 
4327 groups or zombie groups can't be yanked.
4328 The return value is the name of the (last) group that was killed."
4329   (interactive "P")
4330   (let ((buffer-read-only nil)
4331         (groups (gnus-group-process-prefix n))
4332         group entry level)
4333     (while groups
4334       (setq group (car groups)
4335             groups (cdr groups))
4336       (gnus-group-remove-mark group)
4337       (setq level (gnus-group-group-level))
4338       (gnus-delete-line)
4339       (if (setq entry (gnus-gethash group gnus-newsrc-hashtb))
4340           (setq gnus-list-of-killed-groups 
4341                 (cons (cons (car entry) (nth 2 entry)) 
4342                       gnus-list-of-killed-groups)))
4343       (gnus-group-change-level 
4344        (if entry entry group) gnus-level-killed (if entry nil level)))
4345     (gnus-group-position-cursor)
4346     group))
4347
4348 (defun gnus-group-yank-group (&optional arg)
4349   "Yank the last newsgroups killed with \\[gnus-group-kill-group],
4350 inserting it before the current newsgroup.  The numeric ARG specifies
4351 how many newsgroups are to be yanked.  The name of the (last)
4352 newsgroup yanked is returned."
4353   (interactive "p")
4354   (if (not arg) (setq arg 1))
4355   (let (info group prev)
4356     (while (>= (setq arg (1- arg)) 0)
4357       (if (not (setq info (car gnus-list-of-killed-groups)))
4358           (error "No more newsgroups to yank"))
4359       (setq group (nth 2 info))
4360       ;; Find which newsgroup to insert this one before - search
4361       ;; backward until something suitable is found. If there are no
4362       ;; other newsgroups in this buffer, just make this newsgroup the
4363       ;; first newsgroup.
4364       (setq prev (gnus-group-group-name))
4365       (gnus-group-change-level 
4366        info (nth 2 info) gnus-level-killed 
4367        (and prev (gnus-gethash prev gnus-newsrc-hashtb))
4368        t)
4369       (gnus-group-insert-group-line-info (nth 1 info))
4370       (setq gnus-list-of-killed-groups 
4371             (cdr gnus-list-of-killed-groups)))
4372     (forward-line -1)
4373     (gnus-group-position-cursor)
4374     group))
4375       
4376 (defun gnus-group-list-all-groups (arg)
4377   "List all newsgroups with level ARG or lower.
4378 Default is gnus-level-unsubscribed, which lists all subscribed and most
4379 unsubscribed groups."
4380   (interactive "P")
4381   (setq arg (or arg gnus-level-unsubscribed))
4382   (gnus-group-list-groups arg t))
4383
4384 (defun gnus-group-list-killed ()
4385   "List all killed newsgroups in the group buffer."
4386   (interactive)
4387   (if (not gnus-killed-list)
4388       (gnus-message 6 "No killed groups")
4389     (funcall gnus-group-prepare-function gnus-level-killed t gnus-level-killed)
4390     (goto-char (point-min)))
4391   (gnus-group-position-cursor))
4392
4393 (defun gnus-group-list-zombies ()
4394   "List all zombie newsgroups in the group buffer."
4395   (interactive)
4396   (if (not gnus-zombie-list)
4397       (gnus-message 6 "No zombie groups")
4398     (funcall gnus-group-prepare-function gnus-level-zombie t gnus-level-zombie)
4399     (goto-char (point-min)))
4400   (gnus-group-position-cursor))
4401
4402 (defun gnus-group-get-new-news (&optional arg)
4403   "Get newly arrived articles.
4404 If ARG is non-nil, it should be a number between one and nine to
4405 specify which levels you are interested in re-scanning."
4406   (interactive "P")
4407   (run-hooks 'gnus-get-new-news-hook)
4408   (let ((level arg))
4409     (if gnus-group-use-permanent-levels
4410         (if level
4411             (setq gnus-group-default-list-level level)
4412           (setq level (or gnus-group-default-list-level 
4413                           gnus-level-subscribed))))
4414     (if (and gnus-read-active-file (not level))
4415         (progn
4416           (gnus-read-active-file)
4417           (gnus-get-unread-articles (or level (1+ gnus-level-subscribed))))
4418       (let ((gnus-read-active-file nil)
4419             (gnus-have-read-active-file (not arg)))
4420         (gnus-get-unread-articles (or level (1+ gnus-level-subscribed)))))
4421     (gnus-group-list-groups (or (and gnus-group-use-permanent-levels level)
4422                                 gnus-group-default-list-level
4423                                 gnus-level-subscribed)
4424                             gnus-have-all-newsgroups)))
4425
4426 (defun gnus-group-get-new-news-this-group (n)
4427   "Check for newly arrived news in the current group (and the N-1 next groups).
4428 The difference between N and the number of newsgroup checked is returned.
4429 If N is negative, this group and the N-1 previous groups will be checked."
4430   (interactive "P")
4431   (let* ((groups (gnus-group-process-prefix n))
4432          (ret (if (numberp n) (- n (length groups)) 0))
4433          (w-p (window-start))
4434          group)
4435     (while groups
4436       (setq group (car groups)
4437             groups (cdr groups))
4438       (gnus-group-remove-mark group)
4439       (or (gnus-get-new-news-in-group group)
4440           (progn 
4441             (ding) 
4442             (message "%s error: %s" group (gnus-status-message group))
4443             (sit-for 2))))
4444     ;; !!! I don't know why the buffer scrolls forward when updating
4445     ;; the first line in the group buffer, but it does. So we set the
4446     ;; window start forcibly.
4447 ;    (set-window-start (get-buffer-window (current-buffer)) w-p)
4448     (gnus-group-next-unread-group 1 t)
4449     (gnus-summary-position-cursor)
4450     ret))
4451
4452 (defun gnus-get-new-news-in-group (group)
4453   (and group 
4454        (gnus-activate-newsgroup group)
4455        (progn
4456          (gnus-get-unread-articles-in-group 
4457           (nth 2 (gnus-gethash group gnus-newsrc-hashtb))
4458           (gnus-gethash group gnus-active-hashtb))
4459          (gnus-group-update-group-line)
4460          t)))
4461
4462 (defun gnus-group-fetch-faq (group)
4463   "Fetch the FAQ for the current group."
4464   (interactive (list (gnus-group-real-name (gnus-group-group-name))))
4465   (or group (error "No group name given"))
4466   (let ((file (concat gnus-group-faq-directory group))) 
4467     (if (not (file-exists-p file))
4468         (error "No such file: %s" file)
4469       (find-file file))))
4470   
4471 (defun gnus-group-describe-group (force &optional group)
4472   "Display a description of the current newsgroup."
4473   (interactive (list current-prefix-arg (gnus-group-group-name)))
4474   (and force (setq gnus-description-hashtb nil))
4475   (let ((method (gnus-find-method-for-group group))
4476         desc)
4477     (or group (error "No group name given"))
4478     (and (or (and gnus-description-hashtb
4479                   ;; We check whether this group's method has been
4480                   ;; queried for a description file.  
4481                   (gnus-gethash 
4482                    (gnus-group-prefixed-name "" method) 
4483                    gnus-description-hashtb))
4484              (setq desc (gnus-group-get-description group))
4485              (gnus-read-descriptions-file method))
4486          (message
4487           (or desc (gnus-gethash group gnus-description-hashtb)
4488               "No description available")))))
4489
4490 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
4491 (defun gnus-group-describe-all-groups (force)
4492   "Pop up a buffer with descriptions of all newsgroups."
4493   (interactive "P")
4494   (and force (setq gnus-description-hashtb nil))
4495   (if (not (or gnus-description-hashtb
4496                (gnus-read-all-descriptions-files)))
4497       (error "Couldn't request descriptions file"))
4498   (let ((buffer-read-only nil)
4499         b)
4500     (erase-buffer)
4501     (mapatoms
4502      (lambda (group)
4503        (setq b (point))
4504        (insert (format "      *: %-20s %s\n" (symbol-name group)
4505                        (symbol-value group)))
4506        (add-text-properties 
4507         b (1+ b) (list 'gnus-group group
4508                        'gnus-unread t 'gnus-marked nil
4509                        'gnus-level (1+ gnus-level-subscribed))))
4510      gnus-description-hashtb)
4511     (goto-char (point-min))
4512     (gnus-group-position-cursor)))
4513
4514 ;; Suggested by by Daniel Quinlan <quinlan@best.com>.
4515 (defun gnus-group-apropos (regexp &optional search-description)
4516   "List all newsgroups that have names that match a regexp."
4517   (interactive "sGnus apropos (regexp): ")
4518   (let ((prev "")
4519         (obuf (current-buffer))
4520         groups des prev)
4521     ;; Go through all newsgroups that are known to Gnus.
4522     (mapatoms 
4523      (lambda (group)
4524        (and (string-match regexp (symbol-name group))
4525             (setq groups (cons (symbol-name group) groups))))
4526      gnus-active-hashtb)
4527     ;; Go through all descriptions that are known to Gnus. 
4528     (if search-description
4529         (mapatoms 
4530          (lambda (group)
4531            (and (string-match regexp (symbol-value group))
4532                 (gnus-gethash (symbol-name group) gnus-active-hashtb)
4533                 (setq groups (cons (symbol-name group) groups))))
4534          gnus-description-hashtb))
4535     (if (not groups)
4536         (gnus-message 3 "No groups matched \"%s\"." regexp)
4537       ;; Print out all the groups.
4538       (save-excursion
4539         (pop-to-buffer "*Gnus Help*")
4540         (buffer-disable-undo (current-buffer))
4541         (erase-buffer)
4542         (setq groups (sort groups 'string<))
4543         (while groups
4544           ;; Groups may be entered twice into the list of groups.
4545           (if (not (string= (car groups) prev))
4546               (progn
4547                 (insert (setq prev (car groups)) "\n")
4548                 (if (and gnus-description-hashtb
4549                          (setq des (gnus-gethash (car groups) 
4550                                                  gnus-description-hashtb)))
4551                     (insert "  " des "\n"))))
4552           (setq groups (cdr groups)))
4553         (goto-char (point-min))))
4554     (pop-to-buffer obuf)))
4555
4556 (defun gnus-group-description-apropos (regexp)
4557   "List all newsgroups that have names or descriptions that match a regexp."
4558   (interactive "sGnus description apropos (regexp): ")
4559   (if (not (or gnus-description-hashtb
4560                (gnus-read-all-descriptions-files)))
4561       (error "Couldn't request descriptions file"))
4562   (gnus-group-apropos regexp t))
4563
4564 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
4565 (defun gnus-group-list-matching (level regexp &optional all lowest) 
4566   "List all groups with unread articles that match REGEXP.
4567 If the prefix LEVEL is non-nil, it should be a number that says which
4568 level to cut off listing groups. 
4569 If ALL, also list groups with no unread articles.
4570 If LOWEST, don't list groups with level lower than LOWEST."
4571   (interactive "P\nsList newsgroups matching: ")
4572   (gnus-group-prepare-flat (or level gnus-level-subscribed)
4573                            all (or lowest 1) regexp)
4574   (goto-char (point-min))
4575   (gnus-group-position-cursor))
4576
4577 (defun gnus-group-list-all-matching (level regexp &optional lowest) 
4578   "List all groups that match REGEXP.
4579 If the prefix LEVEL is non-nil, it should be a number that says which
4580 level to cut off listing groups. 
4581 If LOWEST, don't list groups with level lower than LOWEST."
4582   (interactive "P\nsList newsgroups matching: ")
4583   (gnus-group-list-matching (or level gnus-level-killed) regexp t lowest))
4584
4585 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
4586 (defun gnus-group-save-newsrc ()
4587   "Save the Gnus startup files."
4588   (interactive)
4589   (gnus-save-newsrc-file))
4590
4591 (defun gnus-group-restart (&optional arg)
4592   "Force Gnus to read the .newsrc file."
4593   (interactive "P")
4594   (gnus-save-newsrc-file)
4595   (gnus-setup-news 'force)
4596   (gnus-group-list-groups arg gnus-have-all-newsgroups))
4597
4598 (defun gnus-group-read-init-file ()
4599   "Read the Gnus elisp init file."
4600   (interactive)
4601   (gnus-read-init-file))
4602
4603 (defun gnus-group-check-bogus-groups (silent)
4604   "Check bogus newsgroups.
4605 If given a prefix, don't ask for confirmation before removing a bogus
4606 group."
4607   (interactive "P")
4608   (gnus-check-bogus-newsgroups (and (not silent) (not gnus-expert-user)))
4609   (gnus-group-list-groups nil gnus-have-all-newsgroups))
4610
4611 (defun gnus-group-edit-global-kill (article &optional group)
4612   "Edit the global kill file.
4613 If GROUP, edit that local kill file instead."
4614   (interactive "P")
4615   (setq gnus-current-kill-article article)
4616   (gnus-kill-file-edit-file group)
4617   (gnus-message 6
4618    (substitute-command-keys
4619     "Editing a global kill file (Type \\[gnus-kill-file-exit] to exit)")))
4620
4621 (defun gnus-group-edit-local-kill (article group)
4622   "Edit a local kill file."
4623   (interactive (list nil (gnus-group-group-name)))
4624   (gnus-group-edit-global-kill article group))
4625
4626 (defun gnus-group-force-update ()
4627   "Update `.newsrc' file."
4628   (interactive)
4629   (gnus-save-newsrc-file))
4630
4631 (defun gnus-group-suspend ()
4632   "Suspend the current Gnus session.
4633 In fact, cleanup buffers except for group mode buffer.
4634 The hook gnus-suspend-gnus-hook is called before actually suspending."
4635   (interactive)
4636   (run-hooks 'gnus-suspend-gnus-hook)
4637   ;; Kill Gnus buffers except for group mode buffer.
4638   (let ((group-buf (get-buffer gnus-group-buffer)))
4639     ;; Do this on a separate list in case the user does a ^G before we finish
4640     (let ((gnus-buffer-list
4641            (delq group-buf (delq gnus-dribble-buffer
4642                                  (append gnus-buffer-list nil)))))
4643       (while gnus-buffer-list
4644         (gnus-kill-buffer (car gnus-buffer-list))
4645         (setq gnus-buffer-list (cdr gnus-buffer-list))))
4646     (if group-buf
4647         (progn
4648           (setq gnus-buffer-list (list group-buf))
4649           (bury-buffer group-buf)
4650           (delete-windows-on group-buf t)))))
4651
4652 (defun gnus-group-clear-dribble ()
4653   "Clear all information from the dribble buffer."
4654   (interactive)
4655   (gnus-dribble-clear))
4656
4657 (defun gnus-group-exit ()
4658   "Quit reading news after updating .newsrc.eld and .newsrc.
4659 The hook `gnus-exit-gnus-hook' is called before actually exiting."
4660   (interactive)
4661   (if (or noninteractive                ;For gnus-batch-kill
4662           (zerop (buffer-size))         ;No news is good news.
4663           (not (gnus-server-opened gnus-select-method)) ;NNTP connection closed
4664           (not gnus-interactive-exit)   ;Without confirmation
4665           gnus-expert-user
4666           (gnus-y-or-n-p "Are you sure you want to quit reading news? "))
4667       (progn
4668         (if gnus-use-full-window
4669             (delete-other-windows)
4670           (gnus-remove-some-windows))
4671         (run-hooks 'gnus-exit-gnus-hook)
4672         (gnus-offer-save-summaries)
4673         (gnus-save-newsrc-file)
4674         (gnus-close-backends)
4675         (gnus-clear-system))))
4676
4677 (defun gnus-close-backends ()
4678   ;; Send a close request to all backends that support such a request. 
4679   (let ((methods gnus-valid-select-methods)
4680         func)
4681     (while methods
4682       (if (fboundp (setq func (intern (concat (car (car methods))
4683                                               "-request-close"))))
4684           (funcall func))
4685       (setq methods (cdr methods)))))
4686
4687 (defun gnus-group-quit ()
4688   "Quit reading news without updating .newsrc.eld or .newsrc.
4689 The hook `gnus-exit-gnus-hook' is called before actually exiting."
4690   (interactive)
4691   (if (or noninteractive                ;For gnus-batch-kill
4692           (zerop (buffer-size))
4693           (not (gnus-server-opened gnus-select-method))
4694           gnus-expert-user
4695           (not gnus-current-startup-file)
4696           (gnus-yes-or-no-p
4697            (format "Quit reading news without saving %s? "
4698                    (file-name-nondirectory gnus-current-startup-file))))
4699       (progn
4700         (run-hooks 'gnus-exit-gnus-hook)
4701         (if gnus-use-full-window
4702             (delete-other-windows)
4703           (gnus-remove-some-windows))
4704         (gnus-dribble-save)
4705         (gnus-close-backends)
4706         (gnus-clear-system))))
4707
4708 (defun gnus-offer-save-summaries ()
4709   (let ((buffers (buffer-list)))
4710     (save-excursion
4711       (while buffers
4712         (and 
4713          ;; We look for buffers with "Summary" in the name.
4714          (string-match "Summary" (or (buffer-name (car buffers)) ""))
4715          (progn
4716            (set-buffer (car buffers))
4717            ;; We check that this is, indeed, a summary buffer.
4718            (eq major-mode 'gnus-summary-mode)) 
4719          ;; We ask the user whether she wants to save the info.
4720          (gnus-y-or-n-p
4721                (format "Update summary buffer %s? " (buffer-name)))
4722          ;; We do it by simply exiting.
4723          (gnus-summary-exit))
4724         (setq buffers (cdr buffers))))))
4725
4726 (defun gnus-group-describe-briefly ()
4727   "Give a one line description of the group mode commands."
4728   (interactive)
4729   (gnus-message 6
4730    (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")))
4731
4732 (defun gnus-group-browse-foreign-server (method)
4733   "Browse a foreign news server.
4734 If called interactively, this function will ask for a select method
4735  (nntp, nnspool, etc.) and a server address (eg. nntp.some.where). 
4736 If not, METHOD should be a list where the first element is the method
4737 and the second element is the address."
4738   (interactive
4739    (list (let ((how (completing-read 
4740                      "Which backend: "
4741                      (append gnus-valid-select-methods gnus-server-alist)
4742                      nil t "nntp")))
4743            ;; We either got a backend name or a virtual server name.
4744            ;; If the first, we also need an address.
4745            (if (assoc how gnus-valid-select-methods)
4746                (list (intern how)
4747                      ;; Suggested by mapjph@bath.ac.uk.
4748                      (completing-read 
4749                       "Address: " 
4750                       (mapcar (lambda (server) (list server))
4751                               gnus-secondary-servers)))
4752              ;; We got a server name, so we find the method.
4753              (gnus-server-to-method how)))))
4754   (gnus-browse-foreign-server method))
4755
4756 \f
4757 ;;;
4758 ;;; Browse Server Mode
4759 ;;;
4760
4761 (defvar gnus-browse-mode-hook nil)
4762 (defvar gnus-browse-mode-map nil)
4763 (put 'gnus-browse-mode 'mode-class 'special)
4764
4765 (if gnus-browse-mode-map
4766     nil
4767   (setq gnus-browse-mode-map (make-keymap))
4768   (suppress-keymap gnus-browse-mode-map)
4769   (define-key gnus-browse-mode-map " " 'gnus-browse-read-group)
4770   (define-key gnus-browse-mode-map "=" 'gnus-browse-select-group)
4771   (define-key gnus-browse-mode-map "n" 'gnus-browse-next-group)
4772   (define-key gnus-browse-mode-map "p" 'gnus-browse-prev-group)
4773   (define-key gnus-browse-mode-map "\177" 'gnus-browse-prev-group)
4774   (define-key gnus-browse-mode-map "N" 'gnus-browse-next-group)
4775   (define-key gnus-browse-mode-map "P" 'gnus-browse-prev-group)
4776   (define-key gnus-browse-mode-map "\M-n" 'gnus-browse-next-group)
4777   (define-key gnus-browse-mode-map "\M-p" 'gnus-browse-prev-group)
4778   (define-key gnus-browse-mode-map "\r" 'gnus-browse-select-group)
4779   (define-key gnus-browse-mode-map "u" 'gnus-browse-unsubscribe-current-group)
4780   (define-key gnus-browse-mode-map "l" 'gnus-browse-exit)
4781   (define-key gnus-browse-mode-map "L" 'gnus-browse-exit)
4782   (define-key gnus-browse-mode-map "q" 'gnus-browse-exit)
4783   (define-key gnus-browse-mode-map "Q" 'gnus-browse-exit)
4784   (define-key gnus-browse-mode-map "\C-c\C-c" 'gnus-browse-exit)
4785   (define-key gnus-browse-mode-map "?" 'gnus-browse-describe-briefly)
4786   (define-key gnus-browse-mode-map "\C-c\C-i" 'gnus-info-find-node)
4787   )
4788
4789 (defvar gnus-browse-current-method nil)
4790 (defvar gnus-browse-return-buffer nil)
4791
4792 (defvar gnus-browse-buffer "*Gnus Browse Server*")
4793
4794 (defun gnus-browse-foreign-server (method &optional return-buffer)
4795   (setq gnus-browse-current-method method)
4796   (setq gnus-browse-return-buffer return-buffer)
4797   (let ((gnus-select-method method)
4798         groups group)
4799     (gnus-message 5 "Connecting to %s..." (nth 1 method))
4800     (or (gnus-server-opened method)
4801         (gnus-open-server method)
4802         (error "Unable to contact server: %s" (gnus-status-message method)))
4803     (or (gnus-request-list method)
4804         (error "Couldn't request list: %s" (gnus-status-message method)))
4805     (get-buffer-create gnus-browse-buffer)
4806     (gnus-add-current-to-buffer-list)
4807     (and gnus-carpal (gnus-carpal-setup-buffer 'browse))
4808     (gnus-configure-windows 'browse)
4809     (buffer-disable-undo (current-buffer))
4810     (let ((buffer-read-only nil))
4811       (erase-buffer))
4812     (gnus-browse-mode)
4813     (setq mode-line-buffer-identification
4814           (format
4815            "(ding) Browse Server {%s:%s}" (car method) (car (cdr method))))
4816     (save-excursion
4817       (set-buffer nntp-server-buffer)
4818       (let ((cur (current-buffer)))
4819         (goto-char (point-min))
4820         (or (string= gnus-ignored-newsgroups "")
4821             (delete-matching-lines gnus-ignored-newsgroups))
4822         (while (re-search-forward 
4823                 "\\(^[^ \t]+\\)[ \t]+[0-9]+[ \t]+[0-9]+" nil t)
4824           (goto-char (match-end 1))
4825           (setq groups (cons (cons (buffer-substring (match-beginning 1)
4826                                                      (match-end 1))
4827                                    (max 0 (- (1+ (read cur)) (read cur))))
4828                              groups)))))
4829     (setq groups (sort groups 
4830                        (lambda (l1 l2)
4831                          (string< (car l1) (car l2)))))
4832     (let ((buffer-read-only nil))
4833       (while groups
4834         (setq group (car groups))
4835         (insert 
4836          (format "K%7d: %s\n" (cdr group) (car group)))
4837         (setq groups (cdr groups))))
4838     (switch-to-buffer (current-buffer))
4839     (goto-char (point-min))
4840     (gnus-group-position-cursor)))
4841
4842 (defun gnus-browse-mode ()
4843   "Major mode for browsing a foreign server.
4844
4845 All normal editing commands are switched off.
4846
4847 \\<gnus-browse-mode-map>
4848 The only things you can do in this buffer is
4849
4850 1) `\\[gnus-browse-unsubscribe-current-group]' to subscribe to a group.
4851 The group will be inserted into the group buffer upon exit from this
4852 buffer.  
4853
4854 2) `\\[gnus-browse-read-group]' to read a group ephemerally.
4855
4856 3) `\\[gnus-browse-exit]' to return to the group buffer."
4857   (interactive)
4858   (kill-all-local-variables)
4859   (if gnus-visual (gnus-browse-make-menu-bar))
4860   (setq mode-line-modified "-- ")
4861   (make-local-variable 'mode-line-format)
4862   (setq mode-line-format (copy-sequence mode-line-format))
4863   (and (equal (nth 3 mode-line-format) "   ")
4864        (setcar (nthcdr 3 mode-line-format) ""))
4865   (setq major-mode 'gnus-browse-mode)
4866   (setq mode-name "Browse Server")
4867   (setq mode-line-process nil)
4868   (use-local-map gnus-browse-mode-map)
4869   (buffer-disable-undo (current-buffer))
4870   (setq truncate-lines t)
4871   (setq buffer-read-only t)
4872   (run-hooks 'gnus-browse-mode-hook))
4873
4874 (defun gnus-browse-read-group (&optional no-article)
4875   "Enter the group at the current line."
4876   (interactive)
4877   (let ((group (gnus-browse-group-name)))
4878     (or (gnus-group-read-ephemeral-group 
4879          group gnus-browse-current-method nil
4880          (cons (current-buffer) 'browse))
4881         (error "Couldn't enter %s" group))))
4882
4883 (defun gnus-browse-select-group ()
4884   "Select the current group."
4885   (interactive)
4886   (gnus-browse-read-group 'no))
4887
4888 (defun gnus-browse-next-group (n)
4889   "Go to the next group."
4890   (interactive "p")
4891   (prog1
4892       (forward-line n)
4893     (gnus-group-position-cursor)))
4894
4895 (defun gnus-browse-prev-group (n)
4896   "Go to the next group."
4897   (interactive "p")
4898   (gnus-browse-next-group (- n)))
4899
4900 (defun gnus-browse-unsubscribe-current-group (arg)
4901   "(Un)subscribe to the next ARG groups."
4902   (interactive "p")
4903   (and (eobp)
4904        (error "No group at current line."))
4905   (let ((ward (if (< arg 0) -1 1))
4906         (arg (abs arg)))
4907     (while (and (> arg 0)
4908                 (not (eobp))
4909                 (gnus-browse-unsubscribe-group)
4910                 (zerop (gnus-browse-next-group ward)))
4911       (setq arg (1- arg)))
4912     (gnus-group-position-cursor)
4913     (if (/= 0 arg) (gnus-message 7 "No more newsgroups"))
4914     arg))
4915
4916 (defun gnus-browse-group-name ()
4917   (save-excursion
4918     (beginning-of-line)
4919     (if (not (re-search-forward ": \\(.*\\)$" (gnus-point-at-eol) t))
4920         ()
4921       (gnus-group-prefixed-name 
4922        (buffer-substring (match-beginning 1) (match-end 1))
4923        gnus-browse-current-method))))
4924   
4925 (defun gnus-browse-unsubscribe-group ()
4926   (let ((sub nil)
4927         (buffer-read-only nil)
4928         group)
4929     (save-excursion
4930       (beginning-of-line)
4931       (if (= (following-char) ?K) (setq sub t))
4932       (setq group (gnus-browse-group-name))
4933       (beginning-of-line)
4934       (delete-char 1)
4935       (if sub
4936           (progn
4937             (gnus-group-change-level 
4938              (list t group gnus-level-default-subscribed
4939                    nil nil gnus-browse-current-method) 
4940              gnus-level-default-subscribed gnus-level-killed
4941              (gnus-gethash (car (nth 1 gnus-newsrc-alist)) gnus-newsrc-hashtb)
4942              t)
4943             (insert ? ))
4944         (gnus-group-change-level 
4945          group gnus-level-killed gnus-level-default-subscribed)
4946         (insert ?K)))
4947     t))
4948
4949 (defun gnus-browse-exit ()
4950   "Quit browsing and return to the group buffer."
4951   (interactive)
4952   (if (eq major-mode 'gnus-browse-mode)
4953       (kill-buffer (current-buffer)))
4954   (if gnus-browse-return-buffer
4955       (gnus-configure-windows 'server)
4956     (gnus-configure-windows 'group)))
4957
4958 (defun gnus-browse-describe-briefly ()
4959   "Give a one line description of the group mode commands."
4960   (interactive)
4961   (gnus-message 6
4962    (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")))
4963       
4964 \f
4965 ;;;
4966 ;;; Gnus summary mode
4967 ;;;
4968
4969 (defvar gnus-summary-mode-map nil)
4970 (defvar gnus-summary-mark-map nil)
4971 (defvar gnus-summary-mscore-map nil)
4972 (defvar gnus-summary-article-map nil)
4973 (defvar gnus-summary-thread-map nil)
4974 (defvar gnus-summary-goto-map nil)
4975 (defvar gnus-summary-exit-map nil)
4976 (defvar gnus-summary-various-map nil)
4977 (defvar gnus-summary-interest-map nil)
4978 (defvar gnus-summary-sort-map nil)
4979 (defvar gnus-summary-backend-map nil)
4980 (defvar gnus-summary-save-map nil)
4981 (defvar gnus-summary-wash-map nil)
4982 (defvar gnus-summary-help-map nil)
4983
4984 (put 'gnus-summary-mode 'mode-class 'special)
4985
4986 (if gnus-summary-mode-map
4987     nil
4988   (setq gnus-summary-mode-map (make-keymap))
4989   (suppress-keymap gnus-summary-mode-map)
4990
4991   ;; Non-orthogonal keys
4992
4993   (define-key gnus-summary-mode-map " " 'gnus-summary-next-page)
4994   (define-key gnus-summary-mode-map "\177" 'gnus-summary-prev-page)
4995   (define-key gnus-summary-mode-map "\r" 'gnus-summary-scroll-up)
4996   (define-key gnus-summary-mode-map "n" 'gnus-summary-next-unread-article)
4997   (define-key gnus-summary-mode-map "p" 'gnus-summary-prev-unread-article)
4998   (define-key gnus-summary-mode-map "N" 'gnus-summary-next-article)
4999   (define-key gnus-summary-mode-map "P" 'gnus-summary-prev-article)
5000   (define-key gnus-summary-mode-map "\M-\C-n" 'gnus-summary-next-same-subject)
5001   (define-key gnus-summary-mode-map "\M-\C-p" 'gnus-summary-prev-same-subject)
5002   (define-key gnus-summary-mode-map "\M-n" 'gnus-summary-next-unread-subject)
5003   (define-key gnus-summary-mode-map "\M-p" 'gnus-summary-prev-unread-subject)
5004   (define-key gnus-summary-mode-map "." 'gnus-summary-first-unread-article)
5005   (define-key gnus-summary-mode-map "," 'gnus-summary-best-unread-article)
5006   (define-key gnus-summary-mode-map "\M-s" 'gnus-summary-search-article-forward)
5007   (define-key gnus-summary-mode-map "\M-r" 'gnus-summary-search-article-backward)
5008   (define-key gnus-summary-mode-map "<" 'gnus-summary-beginning-of-article)
5009   (define-key gnus-summary-mode-map ">" 'gnus-summary-end-of-article)
5010   (define-key gnus-summary-mode-map "j" 'gnus-summary-goto-subject)
5011   (define-key gnus-summary-mode-map "^" 'gnus-summary-refer-parent-article)
5012   (define-key gnus-summary-mode-map "\M-^" 'gnus-summary-refer-article)
5013   (define-key gnus-summary-mode-map "u" 'gnus-summary-tick-article-forward)
5014   (define-key gnus-summary-mode-map "!" 'gnus-summary-tick-article-forward)
5015   (define-key gnus-summary-mode-map "U" 'gnus-summary-tick-article-backward)
5016   (define-key gnus-summary-mode-map "d" 'gnus-summary-mark-as-read-forward)
5017   (define-key gnus-summary-mode-map "D" 'gnus-summary-mark-as-read-backward)
5018   (define-key gnus-summary-mode-map "E" 'gnus-summary-mark-as-expirable)
5019   (define-key gnus-summary-mode-map "\M-u" 'gnus-summary-clear-mark-forward)
5020   (define-key gnus-summary-mode-map "\M-U" 'gnus-summary-clear-mark-backward)
5021   (define-key gnus-summary-mode-map "k" 'gnus-summary-kill-same-subject-and-select)
5022   (define-key gnus-summary-mode-map "\C-k" 'gnus-summary-kill-same-subject)
5023   (define-key gnus-summary-mode-map "\M-\C-k" 'gnus-summary-kill-thread)
5024   (define-key gnus-summary-mode-map "\M-\C-l" 'gnus-summary-lower-thread)
5025   (define-key gnus-summary-mode-map "e" 'gnus-summary-edit-article)
5026   (define-key gnus-summary-mode-map "#" 'gnus-summary-mark-as-processable)
5027   (define-key gnus-summary-mode-map "\M-#" 'gnus-summary-unmark-as-processable)
5028   (define-key gnus-summary-mode-map "\M-\C-t" 'gnus-summary-toggle-threads)
5029   (define-key gnus-summary-mode-map "\M-\C-s" 'gnus-summary-show-thread)
5030   (define-key gnus-summary-mode-map "\M-\C-h" 'gnus-summary-hide-thread)
5031   (define-key gnus-summary-mode-map "\M-\C-f" 'gnus-summary-next-thread)
5032   (define-key gnus-summary-mode-map "\M-\C-b" 'gnus-summary-prev-thread)
5033   (define-key gnus-summary-mode-map "\M-\C-u" 'gnus-summary-up-thread)
5034   (define-key gnus-summary-mode-map "\M-\C-d" 'gnus-summary-down-thread)
5035   (define-key gnus-summary-mode-map "&" 'gnus-summary-execute-command)
5036   (define-key gnus-summary-mode-map "c" 'gnus-summary-catchup-and-exit)
5037   (define-key gnus-summary-mode-map "\C-w" 'gnus-summary-mark-region-as-read)
5038   (define-key gnus-summary-mode-map "\C-t" 'gnus-summary-toggle-truncation)
5039   (define-key gnus-summary-mode-map "?" 'gnus-summary-mark-as-dormant)
5040   (define-key gnus-summary-mode-map "\C-c\M-\C-s" 'gnus-summary-show-all-expunged)
5041   (define-key gnus-summary-mode-map "\C-c\C-s\C-n" 'gnus-summary-sort-by-number)
5042   (define-key gnus-summary-mode-map "\C-c\C-s\C-a" 'gnus-summary-sort-by-author)
5043   (define-key gnus-summary-mode-map "\C-c\C-s\C-s" 'gnus-summary-sort-by-subject)
5044   (define-key gnus-summary-mode-map "\C-c\C-s\C-d" 'gnus-summary-sort-by-date)
5045   (define-key gnus-summary-mode-map "\C-c\C-s\C-i" 'gnus-summary-sort-by-score)
5046   (define-key gnus-summary-mode-map "=" 'gnus-summary-expand-window)
5047   (define-key gnus-summary-mode-map "\C-x\C-s" 'gnus-summary-reselect-current-group)
5048   (define-key gnus-summary-mode-map "\M-g" 'gnus-summary-rescan-group)
5049   (define-key gnus-summary-mode-map "w" 'gnus-summary-stop-page-breaking)
5050   (define-key gnus-summary-mode-map "\C-c\C-r" 'gnus-summary-caesar-message)
5051   (define-key gnus-summary-mode-map "\M-t" 'gnus-summary-toggle-mime)
5052   (define-key gnus-summary-mode-map "f" 'gnus-summary-followup)
5053   (define-key gnus-summary-mode-map "F" 'gnus-summary-followup-with-original)
5054   (define-key gnus-summary-mode-map "C" 'gnus-summary-cancel-article)
5055   (define-key gnus-summary-mode-map "r" 'gnus-summary-reply)
5056   (define-key gnus-summary-mode-map "R" 'gnus-summary-reply-with-original)
5057   (define-key gnus-summary-mode-map "\C-c\C-f" 'gnus-summary-mail-forward)
5058   (define-key gnus-summary-mode-map "o" 'gnus-summary-save-article)
5059   (define-key gnus-summary-mode-map "\C-o" 'gnus-summary-save-article-mail)
5060   (define-key gnus-summary-mode-map "|" 'gnus-summary-pipe-output)
5061   (define-key gnus-summary-mode-map "\M-k" 'gnus-summary-edit-local-kill)
5062   (define-key gnus-summary-mode-map "\M-K" 'gnus-summary-edit-global-kill)
5063   (define-key gnus-summary-mode-map "V" 'gnus-version)
5064   (define-key gnus-summary-mode-map "\C-c\C-d" 'gnus-summary-describe-group)
5065   (define-key gnus-summary-mode-map "q" 'gnus-summary-exit)
5066   (define-key gnus-summary-mode-map "Q" 'gnus-summary-exit-no-update)
5067   (define-key gnus-summary-mode-map "\C-c\C-i" 'gnus-info-find-node)
5068   (define-key gnus-summary-mode-map gnus-mouse-2 'gnus-mouse-pick-article)
5069   (define-key gnus-summary-mode-map "m" 'gnus-summary-mail-other-window)
5070   (define-key gnus-summary-mode-map "a" 'gnus-summary-post-news)
5071   (define-key gnus-summary-mode-map "x" 'gnus-summary-remove-lines-marked-as-read)
5072 ; (define-key gnus-summary-mode-map "X" 'gnus-summary-remove-lines-marked-with)
5073   (define-key gnus-summary-mode-map "s" 'gnus-summary-isearch-article)
5074   (define-key gnus-summary-mode-map "t" 'gnus-summary-toggle-header)
5075   (define-key gnus-summary-mode-map "g" 'gnus-summary-show-article)
5076 ;  (define-key gnus-summary-mode-map "?" 'gnus-summary-describe-briefly)
5077   (define-key gnus-summary-mode-map "l" 'gnus-summary-goto-last-article)
5078   (define-key gnus-summary-mode-map "\C-c\C-v\C-v" 'gnus-uu-decode-uu-view)
5079   (define-key gnus-summary-mode-map "\C-d" 'gnus-summary-enter-digest-group)
5080   (define-key gnus-summary-mode-map "v" 'gnus-summary-verbose-headers)
5081   (define-key gnus-summary-mode-map "\C-c\C-b" 'gnus-bug)
5082
5083
5084   ;; Sort of orthogonal keymap
5085   (define-prefix-command 'gnus-summary-mark-map)
5086   (define-key gnus-summary-mode-map "M" 'gnus-summary-mark-map)
5087   (define-key gnus-summary-mark-map "t" 'gnus-summary-tick-article-forward)
5088   (define-key gnus-summary-mark-map "!" 'gnus-summary-tick-article-forward)
5089   (define-key gnus-summary-mark-map "d" 'gnus-summary-mark-as-read-forward)
5090   (define-key gnus-summary-mark-map "r" 'gnus-summary-mark-as-read-forward)
5091   (define-key gnus-summary-mark-map "c" 'gnus-summary-clear-mark-forward)
5092   (define-key gnus-summary-mark-map " " 'gnus-summary-clear-mark-forward)
5093   (define-key gnus-summary-mark-map "e" 'gnus-summary-mark-as-expirable)
5094   (define-key gnus-summary-mark-map "x" 'gnus-summary-mark-as-expirable)
5095   (define-key gnus-summary-mark-map "?" 'gnus-summary-mark-as-dormant)
5096   (define-key gnus-summary-mark-map "b" 'gnus-summary-set-bookmark)
5097   (define-key gnus-summary-mark-map "B" 'gnus-summary-remove-bookmark)
5098   (define-key gnus-summary-mark-map "#" 'gnus-summary-mark-as-processable)
5099   (define-key gnus-summary-mark-map "\M-#" 'gnus-summary-unmark-as-processable)
5100   (define-key gnus-summary-mark-map "\M-r" 'gnus-summary-remove-lines-marked-as-read)
5101   (define-key gnus-summary-mark-map "\M-\C-r" 'gnus-summary-remove-lines-marked-with)
5102   (define-key gnus-summary-mark-map "D" 'gnus-summary-show-all-dormant)
5103   (define-key gnus-summary-mark-map "\M-D" 'gnus-summary-hide-all-dormant)
5104   (define-key gnus-summary-mark-map "S" 'gnus-summary-show-all-expunged)
5105   (define-key gnus-summary-mark-map "C" 'gnus-summary-catchup)
5106   (define-key gnus-summary-mark-map "H" 'gnus-summary-catchup-to-here)
5107   (define-key gnus-summary-mark-map "\C-c" 'gnus-summary-catchup-all)
5108   (define-key gnus-summary-mark-map "k" 'gnus-summary-kill-same-subject-and-select)
5109   (define-key gnus-summary-mark-map "K" 'gnus-summary-kill-same-subject)
5110
5111   (define-prefix-command 'gnus-summary-mscore-map)
5112   (define-key gnus-summary-mark-map "s" 'gnus-summary-mscore-map)
5113   (define-key gnus-summary-mscore-map "c" 'gnus-summary-clear-above)
5114   (define-key gnus-summary-mscore-map "u" 'gnus-summary-tick-above)
5115   (define-key gnus-summary-mscore-map "m" 'gnus-summary-mark-above)
5116   (define-key gnus-summary-mscore-map "k" 'gnus-summary-kill-below)
5117
5118   (define-key gnus-summary-mark-map "p" 'gnus-uu-mark-map)
5119   
5120   (define-key gnus-summary-mode-map "S" 'gnus-summary-send-map)
5121   
5122   (define-prefix-command 'gnus-summary-goto-map)
5123   (define-key gnus-summary-mode-map "G" 'gnus-summary-goto-map)
5124   (define-key gnus-summary-goto-map "n" 'gnus-summary-next-unread-article)
5125   (define-key gnus-summary-goto-map "p" 'gnus-summary-prev-unread-article)
5126   (define-key gnus-summary-goto-map "N" 'gnus-summary-next-article)
5127   (define-key gnus-summary-goto-map "P" 'gnus-summary-prev-article)
5128   (define-key gnus-summary-goto-map "\C-n" 'gnus-summary-next-same-subject)
5129   (define-key gnus-summary-goto-map "\C-p" 'gnus-summary-prev-same-subject)
5130   (define-key gnus-summary-goto-map "\M-n" 'gnus-summary-next-unread-subject)
5131   (define-key gnus-summary-goto-map "\M-p" 'gnus-summary-prev-unread-subject)
5132   (define-key gnus-summary-goto-map "f" 'gnus-summary-first-unread-article)
5133   (define-key gnus-summary-goto-map "b" 'gnus-summary-best-unread-article)
5134   (define-key gnus-summary-goto-map "g" 'gnus-summary-goto-subject)
5135   (define-key gnus-summary-goto-map "l" 'gnus-summary-goto-last-article)
5136   (define-key gnus-summary-goto-map "p" 'gnus-summary-pop-article)
5137
5138
5139   (define-prefix-command 'gnus-summary-thread-map)
5140   (define-key gnus-summary-mode-map "T" 'gnus-summary-thread-map)
5141   (define-key gnus-summary-thread-map "k" 'gnus-summary-kill-thread)
5142   (define-key gnus-summary-thread-map "l" 'gnus-summary-lower-thread)
5143   (define-key gnus-summary-thread-map "i" 'gnus-summary-raise-thread)
5144   (define-key gnus-summary-thread-map "T" 'gnus-summary-toggle-threads)
5145   (define-key gnus-summary-thread-map "s" 'gnus-summary-show-thread)
5146   (define-key gnus-summary-thread-map "S" 'gnus-summary-show-all-threads)
5147   (define-key gnus-summary-thread-map "h" 'gnus-summary-hide-thread)
5148   (define-key gnus-summary-thread-map "H" 'gnus-summary-hide-all-threads)
5149   (define-key gnus-summary-thread-map "n" 'gnus-summary-next-thread)
5150   (define-key gnus-summary-thread-map "p" 'gnus-summary-prev-thread)
5151   (define-key gnus-summary-thread-map "u" 'gnus-summary-up-thread)
5152   (define-key gnus-summary-thread-map "d" 'gnus-summary-down-thread)
5153   (define-key gnus-summary-thread-map "#" 'gnus-uu-mark-thread)
5154
5155   
5156   (define-prefix-command 'gnus-summary-exit-map)
5157   (define-key gnus-summary-mode-map "Z" 'gnus-summary-exit-map)
5158   (define-key gnus-summary-exit-map "c" 'gnus-summary-catchup-and-exit)
5159   (define-key gnus-summary-exit-map "C" 'gnus-summary-catchup-all-and-exit)
5160   (define-key gnus-summary-exit-map "E" 'gnus-summary-exit-no-update)
5161   (define-key gnus-summary-exit-map "Q" 'gnus-summary-exit)
5162   (define-key gnus-summary-exit-map "Z" 'gnus-summary-exit)
5163   (define-key gnus-summary-exit-map "n" 'gnus-summary-catchup-and-goto-next-group)
5164   (define-key gnus-summary-exit-map "R" 'gnus-summary-reselect-current-group)
5165   (define-key gnus-summary-exit-map "G" 'gnus-summary-rescan-group)
5166   (define-key gnus-summary-exit-map "N" 'gnus-summary-next-group)
5167   (define-key gnus-summary-exit-map "P" 'gnus-summary-prev-group)
5168
5169
5170   (define-prefix-command 'gnus-summary-article-map)
5171   (define-key gnus-summary-mode-map "A" 'gnus-summary-article-map)
5172   (define-key gnus-summary-article-map " " 'gnus-summary-next-page)
5173   (define-key gnus-summary-article-map "n" 'gnus-summary-next-page)
5174   (define-key gnus-summary-article-map "\177" 'gnus-summary-prev-page)
5175   (define-key gnus-summary-article-map "p" 'gnus-summary-prev-page)
5176   (define-key gnus-summary-article-map "\r" 'gnus-summary-scroll-up)
5177   (define-key gnus-summary-article-map "<" 'gnus-summary-beginning-of-article)
5178   (define-key gnus-summary-article-map ">" 'gnus-summary-end-of-article)
5179   (define-key gnus-summary-article-map "b" 'gnus-summary-beginning-of-article)
5180   (define-key gnus-summary-article-map "e" 'gnus-summary-end-of-article)
5181   (define-key gnus-summary-article-map "^" 'gnus-summary-refer-parent-article)
5182   (define-key gnus-summary-article-map "r" 'gnus-summary-refer-parent-article)
5183   (define-key gnus-summary-article-map "w" 'gnus-summary-stop-page-breaking)
5184   (define-key gnus-summary-article-map "c" 'gnus-summary-caesar-message)
5185   (define-key gnus-summary-article-map "g" 'gnus-summary-show-article)
5186   (define-key gnus-summary-article-map "t" 'gnus-summary-toggle-header)
5187   (define-key gnus-summary-article-map "m" 'gnus-summary-toggle-mime)
5188   (define-key gnus-summary-article-map "s" 'gnus-summary-isearch-article)
5189
5190
5191   (define-prefix-command 'gnus-summary-wash-map)
5192   (define-key gnus-summary-mode-map "W" 'gnus-summary-wash-map)
5193   (define-key gnus-summary-wash-map "h" 'gnus-article-hide-headers)
5194   (define-key gnus-summary-wash-map "s" 'gnus-article-hide-signature)
5195   (define-key gnus-summary-wash-map "c" 'gnus-article-hide-citation)
5196   (define-key gnus-summary-wash-map "o" 'gnus-article-treat-overstrike)
5197   (define-key gnus-summary-wash-map "w" 'gnus-article-word-wrap)
5198   (define-key gnus-summary-wash-map "d" 'gnus-article-remove-cr)
5199   (define-key gnus-summary-wash-map "q" 'gnus-article-de-quoted-unreadable)
5200   (define-key gnus-summary-wash-map "f" 'gnus-article-display-x-face)
5201   (define-key gnus-summary-wash-map "t" 'gnus-article-date-ut)
5202   (define-key gnus-summary-wash-map "\C-t" 'gnus-article-date-local)
5203   (define-key gnus-summary-wash-map "T" 'gnus-article-date-lapsed)
5204
5205   (define-key gnus-summary-wash-map "A" 'gnus-article-highlight)
5206   (define-key gnus-summary-wash-map "a" 'gnus-article-hide)
5207   (define-key gnus-summary-wash-map "H" 'gnus-article-highlight-headers)
5208   (define-key gnus-summary-wash-map "C" 'gnus-article-highlight-citation)
5209   (define-key gnus-summary-wash-map "S" 'gnus-article-highlight-signature)
5210   (define-key gnus-summary-wash-map "b" 'gnus-article-add-buttons)
5211
5212
5213   (define-prefix-command 'gnus-summary-help-map)
5214   (define-key gnus-summary-mode-map "H" 'gnus-summary-help-map)
5215   (define-key gnus-summary-help-map "v" 'gnus-version)
5216   (define-key gnus-summary-help-map "f" 'gnus-summary-fetch-faq)
5217   (define-key gnus-summary-help-map "d" 'gnus-summary-describe-group)
5218   (define-key gnus-summary-help-map "h" 'gnus-summary-describe-briefly)
5219   (define-key gnus-summary-help-map "i" 'gnus-info-find-node)
5220
5221
5222   (define-prefix-command 'gnus-summary-backend-map)
5223   (define-key gnus-summary-mode-map "B" 'gnus-summary-backend-map)
5224   (define-key gnus-summary-backend-map "e" 'gnus-summary-expire-articles)
5225   (define-key gnus-summary-backend-map "\177" 'gnus-summary-delete-article)
5226   (define-key gnus-summary-backend-map "m" 'gnus-summary-move-article)
5227   (define-key gnus-summary-backend-map "r" 'gnus-summary-respool-article)
5228   (define-key gnus-summary-backend-map "w" 'gnus-summary-edit-article)
5229   (define-key gnus-summary-backend-map "c" 'gnus-summary-copy-article)
5230   (define-key gnus-summary-backend-map "q" 'gnus-summary-fancy-query)
5231   (define-key gnus-summary-backend-map "i" 'gnus-summary-import-article)
5232
5233
5234   (define-prefix-command 'gnus-summary-save-map)
5235   (define-key gnus-summary-mode-map "O" 'gnus-summary-save-map)
5236   (define-key gnus-summary-save-map "o" 'gnus-summary-save-article)
5237   (define-key gnus-summary-save-map "m" 'gnus-summary-save-article-mail)
5238   (define-key gnus-summary-save-map "r" 'gnus-summary-save-article-rmail)
5239   (define-key gnus-summary-save-map "f" 'gnus-summary-save-article-file)
5240   (define-key gnus-summary-save-map "h" 'gnus-summary-save-article-folder)
5241   (define-key gnus-summary-save-map "v" 'gnus-summary-save-article-vm)
5242   (define-key gnus-summary-save-map "p" 'gnus-summary-pipe-output)
5243 ;  (define-key gnus-summary-save-map "s" 'gnus-soup-add-article)
5244
5245   (define-key gnus-summary-mode-map "X" 'gnus-uu-extract-map)
5246   
5247   (define-prefix-command 'gnus-summary-various-map)
5248   (define-key gnus-summary-mode-map "V" 'gnus-summary-various-map)
5249   (define-key gnus-summary-various-map "u" 'gnus-summary-universal-argument)
5250   (define-key gnus-summary-various-map "\C-s" 'gnus-summary-search-article-forward)
5251   (define-key gnus-summary-various-map "\C-r" 'gnus-summary-search-article-backward)
5252   (define-key gnus-summary-various-map "r" 'gnus-summary-refer-article)
5253   (define-key gnus-summary-various-map "&" 'gnus-summary-execute-command)
5254   (define-key gnus-summary-various-map "T" 'gnus-summary-toggle-truncation)
5255   (define-key gnus-summary-various-map "e" 'gnus-summary-expand-window)
5256   (define-key gnus-summary-various-map "D" 'gnus-summary-enter-digest-group)
5257   (define-key gnus-summary-various-map "k" 'gnus-summary-edit-local-kill)
5258   (define-key gnus-summary-various-map "K" 'gnus-summary-edit-global-kill)
5259
5260   (define-key gnus-summary-various-map "S" 'gnus-summary-score-map)
5261
5262   (define-prefix-command 'gnus-summary-sort-map)
5263   (define-key gnus-summary-various-map "s" 'gnus-summary-sort-map)
5264   (define-key gnus-summary-sort-map "n" 'gnus-summary-sort-by-number)
5265   (define-key gnus-summary-sort-map "a" 'gnus-summary-sort-by-author)
5266   (define-key gnus-summary-sort-map "s" 'gnus-summary-sort-by-subject)
5267   (define-key gnus-summary-sort-map "d" 'gnus-summary-sort-by-date)
5268   (define-key gnus-summary-sort-map "i" 'gnus-summary-sort-by-score)
5269
5270   (define-key gnus-summary-mode-map "I" 'gnus-summary-increase-score)
5271   (define-key gnus-summary-mode-map "L" 'gnus-summary-lower-score)
5272   )
5273
5274
5275 \f
5276
5277 (defun gnus-summary-mode ()
5278   "Major mode for reading articles.
5279
5280 All normal editing commands are switched off.
5281 \\<gnus-summary-mode-map>
5282 Each line in this buffer represents one article.  To read an
5283 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
5284 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]', 
5285 respectively.
5286
5287 You can also post articles and send mail from this buffer.  To 
5288 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author 
5289 of an article, type `\\[gnus-summary-reply]'.
5290
5291 There are approx. one gazillion commands you can execute in this 
5292 buffer; read the info pages for more information (`\\[gnus-info-find-node]'). 
5293
5294 The following commands are available:
5295
5296 \\{gnus-summary-mode-map}"
5297   (interactive)
5298   (if gnus-visual (gnus-summary-make-menu-bar))
5299   (kill-all-local-variables)
5300   (let ((locals gnus-summary-local-variables))
5301     (while locals
5302       (if (consp (car locals))
5303           (progn
5304             (make-local-variable (car (car locals)))
5305             (set (car (car locals)) (eval (cdr (car locals)))))
5306         (make-local-variable (car locals))
5307         (set (car locals) nil))
5308       (setq locals (cdr locals))))
5309   (gnus-make-thread-indent-array)
5310   (gnus-update-format-specifications)
5311   (setq mode-line-modified "-- ")
5312   (make-local-variable 'mode-line-format)
5313   (setq mode-line-format (copy-sequence mode-line-format))
5314   (and (equal (nth 3 mode-line-format) "   ")
5315        (setcar (nthcdr 3 mode-line-format) ""))
5316   (setq major-mode 'gnus-summary-mode)
5317   (setq mode-name "Summary")
5318   (make-local-variable 'minor-mode-alist)
5319   (use-local-map gnus-summary-mode-map)
5320   (buffer-disable-undo (current-buffer))
5321   (setq buffer-read-only t)             ;Disable modification
5322   (setq truncate-lines t)
5323   (setq selective-display t)
5324   (setq selective-display-ellipses t)   ;Display `...'
5325   (setq buffer-display-table gnus-summary-display-table)
5326   (run-hooks 'gnus-summary-mode-hook))
5327
5328 (defun gnus-summary-make-display-table ()
5329   ;; Change the display table.  Odd characters have a tendency to mess
5330   ;; up nicely formatted displays - we make all possible glyphs
5331   ;; display only a single character.
5332
5333   ;; We start from the standard display table, if any.
5334   (setq gnus-summary-display-table 
5335         (or (copy-sequence standard-display-table)
5336             (make-display-table)))
5337   ;; Nix out all the control chars...
5338   (let ((i 32))
5339     (while (>= (setq i (1- i)) 0)
5340       (aset gnus-summary-display-table i [??])))
5341   ;; ... but not newline and cr, of course. (cr is necessary for the
5342   ;; selective display).  
5343   (aset gnus-summary-display-table ?\n nil)
5344   (aset gnus-summary-display-table ?\r nil)
5345   ;; We nix out any glyphs over 126 that are not set already.  
5346   (let ((i 256))
5347     (while (>= (setq i (1- i)) 127)
5348       ;; Only modify if the entry is nil.
5349       (or (aref gnus-summary-display-table i) 
5350           (aset gnus-summary-display-table i [??])))))
5351
5352 (defun gnus-summary-clear-local-variables ()
5353   (let ((locals gnus-summary-local-variables))
5354     (while locals
5355       (if (consp (car locals))
5356           (and (vectorp (car (car locals)))
5357                (set (car (car locals)) nil))
5358         (and (vectorp (car locals))
5359              (set (car locals) nil)))
5360       (setq locals (cdr locals)))))
5361
5362 (defun gnus-mouse-pick-article (e)
5363   (interactive "e")
5364   (mouse-set-point e)
5365   (gnus-summary-next-page nil t))
5366
5367 (defun gnus-summary-setup-buffer (group)
5368   "Initialize summary buffer."
5369   (let ((buffer (concat "*Summary " group "*")))
5370     (if (get-buffer buffer)
5371         (progn
5372           (set-buffer buffer)
5373           (not gnus-newsgroup-begin))
5374       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
5375       (setq gnus-summary-buffer (set-buffer (get-buffer-create buffer)))
5376       (gnus-add-current-to-buffer-list)
5377       (gnus-summary-mode)
5378       (and gnus-carpal (gnus-carpal-setup-buffer 'summary))
5379       (setq gnus-newsgroup-name group)
5380       t)))
5381
5382 (defun gnus-set-global-variables ()
5383   ;; Set the global equivalents of the summary buffer-local variables
5384   ;; to the latest values they had. These reflect the summary buffer
5385   ;; that was in action when the last article was fetched.
5386   (if (eq major-mode 'gnus-summary-mode) 
5387       (progn
5388         (setq gnus-summary-buffer (current-buffer))
5389         (let ((name gnus-newsgroup-name)
5390               (marked gnus-newsgroup-marked)
5391               (unread gnus-newsgroup-unreads)
5392               (headers gnus-current-headers)
5393               (score-file gnus-current-score-file))
5394           (save-excursion
5395             (set-buffer gnus-group-buffer)
5396             (setq gnus-newsgroup-name name)
5397             (setq gnus-newsgroup-marked marked)
5398             (setq gnus-newsgroup-unreads unread)
5399             (setq gnus-current-headers headers)
5400             (setq gnus-current-score-file score-file))))))
5401
5402 (defun gnus-summary-insert-dummy-line (sformat subject number)
5403   (if (not sformat) 
5404       (setq sformat gnus-summary-dummy-line-format-spec))
5405   (let (b)
5406     (beginning-of-line)
5407     (setq b (point))
5408     (insert (eval sformat))
5409     (add-text-properties
5410      b (1+ b)
5411      (list 'gnus-number number 
5412            'gnus-mark gnus-dummy-mark
5413            'gnus-level 0))))
5414
5415 (defvar gnus-thread-indent-array nil)
5416 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
5417 (defun gnus-make-thread-indent-array ()
5418   (let ((n 200))
5419     (if (and gnus-thread-indent-array
5420              (= gnus-thread-indent-level gnus-thread-indent-array-level))
5421         nil
5422       (setq gnus-thread-indent-array (make-vector 201 "")
5423             gnus-thread-indent-array-level gnus-thread-indent-level)
5424       (while (>= n 0)
5425         (aset gnus-thread-indent-array n
5426               (make-string (* n gnus-thread-indent-level) ? ))
5427         (setq n (1- n))))))
5428
5429 (defun gnus-summary-insert-line 
5430   (sformat header level current unread replied expirable subject-or-nil
5431            &optional dummy score)
5432   (or sformat (setq sformat gnus-summary-line-format-spec))
5433   (let* ((indentation (aref gnus-thread-indent-array level))
5434          (lines (header-lines header))
5435          (score (or score gnus-summary-default-score 0))
5436          (score-char
5437           (if (or (null gnus-summary-default-score)
5438                   (<= (abs (- score gnus-summary-default-score))
5439                       gnus-summary-zcore-fuzz)) ? 
5440             (if (< score gnus-summary-default-score)
5441                 gnus-score-below-mark gnus-score-over-mark)))
5442          (replied (if replied gnus-replied-mark ? ))
5443          (from (header-from header))
5444          (name-address (funcall gnus-extract-address-components from))
5445          (address (car (cdr name-address)))
5446          (name (or (car name-address) (car (cdr name-address))))
5447          (subject (header-subject header))
5448          (number (header-number header))
5449          (opening-bracket (if dummy ?\< ?\[))
5450          (closing-bracket (if dummy ?\> ?\]))
5451          (buffer-read-only nil)
5452          (b (progn (beginning-of-line) (point))))
5453     (or (numberp lines) (setq lines 0))
5454     (insert (eval sformat))
5455     (add-text-properties
5456      b (1+ b) (list 'gnus-number number 
5457                     'gnus-mark (or unread gnus-unread-mark)
5458                     'gnus-level level))))
5459
5460 (defun gnus-summary-update-line (&optional dont-update)
5461   ;; Update summary line after change.
5462   (or (not gnus-summary-default-score)
5463       gnus-summary-inhibit-highlight
5464       (let ((gnus-summary-inhibit-highlight t)
5465             (article (gnus-summary-article-number)))
5466         (progn
5467           (or dont-update
5468               (if (and gnus-summary-mark-below
5469                        (< (gnus-summary-article-score)
5470                           gnus-summary-mark-below))
5471                   (and (not (memq article gnus-newsgroup-marked))
5472                        (not (memq article gnus-newsgroup-dormant))
5473                        (memq article gnus-newsgroup-unreads)
5474                        (gnus-summary-mark-article nil gnus-low-score-mark))
5475                 (and (eq (gnus-summary-article-mark) gnus-low-score-mark)
5476                      (gnus-summary-mark-article nil gnus-unread-mark))))
5477           (and gnus-visual
5478                (run-hooks 'gnus-summary-update-hook))))))
5479
5480 (defun gnus-summary-update-lines (&optional beg end)
5481   ;; Rehighlight summary buffer according to `gnus-summary-highlight'.
5482   (let ((beg (or beg (point-min)))
5483         (end (or end (point-max))))
5484     (save-excursion
5485       (set-buffer gnus-summary-buffer)
5486       (goto-char beg)
5487       (while (and (not (eobp)) (< (point) end))
5488         (gnus-summary-update-line)
5489         (forward-line 1)))))
5490
5491 (defun gnus-summary-number-of-articles-in-thread (thread &optional char)
5492   ;; Sum up all elements (and sub-elements) in a list.
5493   (let ((number 
5494          (if (listp thread) 
5495              (apply 
5496               '+ (mapcar 'gnus-summary-number-of-articles-in-thread thread))
5497            1)))
5498     (if char 
5499         (if (> number 1) gnus-not-empty-thread-mark
5500           gnus-empty-thread-mark)
5501       number)))
5502
5503 (defun gnus-summary-read-group (group &optional show-all no-article kill-buffer)
5504   "Start reading news in newsgroup GROUP.
5505 If SHOW-ALL is non-nil, already read articles are also listed.
5506 If NO-ARTICLE is non-nil, no article is selected initially."
5507   (gnus-message 5 "Retrieving newsgroup: %s..." group)
5508   (let* ((new-group (gnus-summary-setup-buffer group))
5509          (did-select (and new-group (gnus-select-newsgroup group show-all)))
5510          (method (car (gnus-find-method-for-group group))))
5511     (cond 
5512      ((not new-group)
5513       (gnus-set-global-variables)
5514       (gnus-kill-buffer kill-buffer)
5515       (gnus-configure-windows 'summary)
5516       (gnus-set-mode-line 'summary)
5517       (gnus-summary-position-cursor)
5518       (message "")
5519       t)
5520      ((null did-select) 
5521       (and (eq major-mode 'gnus-summary-mode)
5522            (not (equal (current-buffer) kill-buffer))
5523            (progn
5524              (kill-buffer (current-buffer))
5525              (set-buffer gnus-group-buffer)
5526              (gnus-group-next-unread-group 1)))
5527       (message "Can't select group")
5528       nil)
5529      ((eq did-select 'quit)
5530       (and (eq major-mode 'gnus-summary-mode)
5531            (not (equal (current-buffer) kill-buffer))
5532            (kill-buffer (current-buffer)))
5533       (gnus-kill-buffer kill-buffer)
5534       (gnus-configure-windows 'group)
5535       (gnus-group-next-unread-group 1)
5536       (signal 'quit nil))
5537      (t
5538       (gnus-set-global-variables)
5539       ;; Save the active value in effect when the group was entered.
5540       (setq gnus-newsgroup-active 
5541             (gnus-copy-sequence
5542              (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
5543       ;; You can change the subjects in this hook.
5544       (run-hooks 'gnus-select-group-hook)
5545       ;; Do score processing.
5546       (and gnus-use-scoring (gnus-possibly-score-headers))
5547       ;; Update the format specifiers.
5548       (gnus-update-format-specifications)
5549       ;; Generate the summary buffer.
5550       (gnus-summary-prepare)
5551       (if (zerop (buffer-size))
5552           (cond (gnus-newsgroup-dormant
5553                  (gnus-summary-show-all-dormant))
5554                 ((and gnus-newsgroup-scored show-all)
5555                  (gnus-summary-show-all-expunged))))
5556       ;; Function `gnus-apply-kill-file' must be called in this hook.
5557       (run-hooks 'gnus-apply-kill-hook)
5558       (if (zerop (buffer-size))
5559           (progn
5560             ;; This newsgroup is empty.
5561             (gnus-summary-catchup-and-exit nil t) ;Without confirmations.
5562             (gnus-message 6 "No unread news")
5563             (gnus-kill-buffer kill-buffer)
5564             nil)
5565         ;;(save-excursion
5566         ;;  (if kill-buffer
5567         ;;      (let ((gnus-summary-buffer kill-buffer))
5568         ;;      (gnus-configure-windows 'group))))
5569         ;; Hide conversation thread subtrees.  We cannot do this in
5570         ;; gnus-summary-prepare-hook since kill processing may not
5571         ;; work with hidden articles.
5572         (and gnus-show-threads
5573              gnus-thread-hide-subtree
5574              (gnus-summary-hide-all-threads))
5575         ;; Show first unread article if requested.
5576         (goto-char (point-min))
5577         (if (and (not no-article)
5578                  gnus-auto-select-first
5579                  (gnus-summary-first-unread-article))
5580             ()
5581           (gnus-configure-windows 'summary))
5582         (gnus-set-mode-line 'summary)
5583         (gnus-summary-position-cursor)
5584         ;; If in async mode, we send some info to the backend.
5585         (and gnus-newsgroup-async
5586              (setq gnus-newsgroup-threads (nreverse gnus-newsgroup-threads))
5587              (gnus-request-asynchronous 
5588               gnus-newsgroup-name
5589               (if (and gnus-asynchronous-article-function
5590                        (fboundp gnus-asynchronous-article-function))
5591                   (funcall gnus-asynchronous-article-function
5592                            gnus-newsgroup-threads)
5593                 gnus-newsgroup-threads)))
5594         (gnus-kill-buffer kill-buffer)
5595         (if (not (get-buffer-window gnus-group-buffer))
5596             ()
5597           ;; gotta use windows, because recenter does wierd stuff if
5598           ;; the current buffer ain't the displayed window.
5599           (let ((owin (selected-window))) 
5600             (select-window (get-buffer-window gnus-group-buffer))
5601             (and (gnus-group-goto-group group)
5602                  (recenter))
5603             (select-window owin))))
5604       t))))
5605
5606 (defun gnus-summary-prepare ()
5607   ;; Generate the summary buffer.
5608   (let ((buffer-read-only nil))
5609     (erase-buffer)
5610     (gnus-summary-prepare-threads 
5611      (if gnus-show-threads
5612          (gnus-gather-threads 
5613           (gnus-sort-threads 
5614            (if (and gnus-summary-expunge-below
5615                     (not gnus-fetch-old-headers))
5616                (gnus-make-threads-and-expunge)
5617              (gnus-make-threads))))
5618        gnus-newsgroup-headers)
5619      0 nil nil t)
5620     ;; Erase header retrieval message.
5621     (gnus-summary-update-lines)
5622     (message "")
5623     ;; Remove the final newline.
5624     ;;(goto-char (point-max))
5625     ;;(delete-char -1)
5626     ;; Call hooks for modifying summary buffer.
5627     ;; Suggested by sven@tde.LTH.Se (Sven Mattisson).
5628     (goto-char (point-min))
5629     (run-hooks 'gnus-summary-prepare-hook)))
5630
5631 (defun gnus-subject-equal (s1 s2)
5632   (cond 
5633    ((numberp gnus-summary-gather-subject-limit)
5634     (string= (if (> (length s1) gnus-summary-gather-subject-limit)
5635                  (substring s1 0 gnus-summary-gather-subject-limit)
5636                s1)
5637              (if (> (length s2) gnus-summary-gather-subject-limit)
5638                  (substring s2 0 gnus-summary-gather-subject-limit)
5639                s2)))
5640    ((eq 'fuzzy gnus-summary-gather-subject-limit)
5641     (string= (gnus-simplify-subject-fuzzy s1)
5642              (gnus-simplify-subject-fuzzy s2)))
5643    (t
5644     (string= s1 s2))))
5645
5646 (defun gnus-gather-threads (threads)
5647   "Gather threads that have lost their roots."
5648   (if (not gnus-summary-make-false-root)
5649       threads 
5650     (let ((hashtb (gnus-make-hashtable 1023))
5651           (prev threads)
5652           (result threads)
5653           thread subject hthread whole-subject)
5654       (while threads
5655         (setq whole-subject 
5656               (setq subject (header-subject (car (car threads)))))
5657         (if gnus-summary-gather-subject-limit
5658             (or (and (numberp gnus-summary-gather-subject-limit)
5659                      (> (length subject) gnus-summary-gather-subject-limit)
5660                      (setq subject
5661                            (substring subject 0 
5662                                       gnus-summary-gather-subject-limit)))
5663                 (and (eq 'fuzzy gnus-summary-gather-subject-limit)
5664                      (setq subject (gnus-simplify-subject-fuzzy subject))))
5665           (setq subject (gnus-simplify-subject-re subject)))
5666         (if (setq hthread 
5667                   (gnus-gethash subject hashtb))
5668             (progn
5669               (or (stringp (car (car hthread)))
5670                   (setcar hthread (list whole-subject (car hthread))))
5671               (setcdr (car hthread) (nconc (cdr (car hthread)) 
5672                                            (list (car threads))))
5673               (setcdr prev (cdr threads))
5674               (setq threads prev))
5675           (gnus-sethash subject threads hashtb))
5676         (setq prev threads)
5677         (setq threads (cdr threads)))
5678       result)))
5679
5680 (defun gnus-make-threads ()
5681   ;; This function takes the dependencies already made by 
5682   ;; `gnus-get-newsgroup-headers' and builds the trees. First we go
5683   ;; through the dependecies in the hash table and finds all the
5684   ;; roots. Roots do not refer back to any valid articles.
5685   (let (roots)
5686     (and gnus-fetch-old-headers (eq gnus-headers-retrieved-by 'nov)
5687          (gnus-build-old-threads))
5688     (mapatoms
5689      (lambda (refs)
5690        (if (not (car (symbol-value refs)))
5691            (setq roots (append (cdr (symbol-value refs)) roots))
5692          ;; Ok, these refer back to valid articles, but if
5693          ;; `gnus-thread-ignore-subject' is nil, we have to check that
5694          ;; the root has the same subject as its children. The children
5695          ;; that do not are made into roots and removed from the list
5696          ;; of children. 
5697          (or gnus-thread-ignore-subject
5698              (let* ((prev (symbol-value refs))
5699                     (subject (gnus-simplify-subject-re 
5700                               (header-subject (car prev))))
5701                     (headers (cdr prev)))
5702                (while headers
5703                  (if (not (string= subject
5704                                    (gnus-simplify-subject-re 
5705                                     (header-subject (car headers)))))
5706                      (progn
5707                        (setq roots (cons (car headers) roots))
5708                        (setcdr prev (cdr headers)))
5709                    (setq prev headers))
5710                  (setq headers (cdr headers)))))))
5711      gnus-newsgroup-dependencies)
5712     
5713     (mapcar 'gnus-trim-thread
5714             (apply 'append
5715                    (mapcar 'gnus-cut-thread
5716                            (mapcar 'gnus-make-sub-thread roots))))))
5717   
5718 (defun gnus-make-threads-and-expunge ()
5719   ;; This function takes the dependencies already made by 
5720   ;; `gnus-get-newsgroup-headers' and builds the trees. First we go
5721   ;; through the dependecies in the hash table and finds all the
5722   ;; roots. Roots do not refer back to any valid articles.
5723   (let ((default (or gnus-summary-default-score 0))
5724         (below gnus-summary-expunge-below)
5725         roots article)
5726     (and gnus-fetch-old-headers (eq gnus-headers-retrieved-by 'nov)
5727          (gnus-build-old-threads))
5728     (mapatoms
5729      (lambda (refs)
5730        (if (not (car (symbol-value refs)))
5731            ;; These articles do not refer back to any other articles -
5732            ;; they are roots.
5733            (let ((headers (cdr (symbol-value refs))))
5734              ;; We weed out the low-scored articles.
5735              (while headers
5736                (if (not (< (or (cdr (assq (header-number (car headers))
5737                                           gnus-newsgroup-scored)) default)
5738                            below))
5739                    ;; It is over.
5740                    (setq roots (cons (car headers) roots))
5741                  ;; It is below, so we mark it as read.
5742                  (setq gnus-newsgroup-unreads
5743                        (delq (header-number (car headers))
5744                              gnus-newsgroup-unreads)))
5745                (setq headers (cdr headers))))
5746          ;; Ok, these refer back to valid articles, but if
5747          ;; `gnus-thread-ignore-subject' is nil, we have to check that
5748          ;; the root has the same subject as its children. The children
5749          ;; that do not are made into roots and removed from the list
5750          ;; of children. 
5751          (or gnus-thread-ignore-subject
5752              (let* ((prev (symbol-value refs))
5753                     (subject (gnus-simplify-subject-re 
5754                               (header-subject (car prev))))
5755                     (headers (cdr prev)))
5756                (while headers
5757                  (if (not (string= subject
5758                                    (gnus-simplify-subject-re 
5759                                     (header-subject (car headers)))))
5760                      (progn
5761                        (if (not (< (or (cdr (assq (header-number (car headers))
5762                                                   gnus-newsgroup-scored))
5763                                        default) below))
5764                            (setq roots (cons (car headers) roots))
5765                          (setq gnus-newsgroup-unreads
5766                                (delq (header-number (car headers))
5767                                      gnus-newsgroup-unreads)))
5768                        (setcdr prev (cdr headers)))
5769                    (setq prev headers))
5770                  (setq headers (cdr headers)))))
5771          ;; If this article is expunged, some of the children might be
5772          ;; roots.  
5773          (if (< (or (cdr (assq (header-number (car (symbol-value refs)))
5774                                gnus-newsgroup-scored)) default)
5775                 below)
5776              (let* ((prev (symbol-value refs))
5777                     (headers (cdr prev)))
5778                (while headers
5779                  (setq article (header-number (car headers)))
5780                  (if (not (< (or (cdr (assq article gnus-newsgroup-scored))
5781                                  default) below))
5782                      (progn (setq roots (cons (car headers) roots))
5783                             (setq prev headers))
5784                    (setq gnus-newsgroup-unreads 
5785                          (delq article gnus-newsgroup-unreads))
5786                    (setcdr prev (cdr headers)))
5787                  (setq headers (cdr headers))))
5788            ;; It was not expunged, but we look at expunged children.
5789            (let* ((prev (symbol-value refs))
5790                   (headers (cdr prev))
5791                   article id)
5792              (while headers
5793                (setq article (header-number (car headers)))
5794                (if (not (< (or (cdr (assq article gnus-newsgroup-scored))
5795                                default) below))
5796                    (setq prev headers)
5797                  (setq gnus-newsgroup-unreads 
5798                        (delq article gnus-newsgroup-unreads))
5799                  (setcdr prev (cdr headers)))
5800                (setq headers (cdr headers)))))))
5801      gnus-newsgroup-dependencies)
5802
5803     (mapcar 'gnus-trim-thread
5804             (apply 'append
5805                    (mapcar 'gnus-cut-thread
5806                            (mapcar 'gnus-make-sub-thread roots))))))
5807   
5808 (defun gnus-cut-thread (thread)
5809   ;; Remove leaf dormant or ancient articles from THREAD.
5810   (let ((head (car thread))
5811         (tail (apply 'append (mapcar 'gnus-cut-thread (cdr thread)))))
5812     (if (and (null tail)
5813              (let ((number (header-number head)))
5814                (or (memq number gnus-newsgroup-ancient)
5815                    (memq number gnus-newsgroup-dormant)
5816                    (and gnus-summary-expunge-below
5817                         (eq gnus-fetch-old-headers 'some)
5818                         (< (or (cdr (assq number gnus-newsgroup-scored))
5819                                gnus-summary-default-score 0)
5820                            gnus-summary-expunge-below)
5821                         (progn
5822                           (setq gnus-newsgroup-unreads
5823                                 (delq number gnus-newsgroup-unreads))
5824                           t)))))
5825         nil
5826       (list (cons head tail)))))
5827
5828 (defun gnus-trim-thread (thread)
5829   ;; Remove root ancient articles with only one child from THREAD.
5830   (if (and (eq gnus-fetch-old-headers 'some)
5831            (memq (header-number (car thread)) gnus-newsgroup-ancient)
5832            (= (length thread) 2))
5833       (gnus-trim-thread (nth 1 thread))
5834     thread))
5835
5836 (defun gnus-make-sub-thread (root)
5837   ;; This function makes a sub-tree for a node in the tree.
5838   (let ((children (reverse (cdr (gnus-gethash (downcase (header-id root))
5839                                               gnus-newsgroup-dependencies)))))
5840     (cons root (mapcar 'gnus-make-sub-thread children))))
5841
5842 (defun gnus-build-old-threads ()
5843   ;; Look at all the articles that refer back to old articles, and
5844   ;; fetch the headers for the articles that aren't there. This will
5845   ;; build complete threads - if the roots haven't been expired by the
5846   ;; server, that is.
5847   (let (id heads)
5848     (mapatoms
5849      (lambda (refs)
5850        (if (not (car (symbol-value refs)))
5851            (progn
5852              (setq heads (cdr (symbol-value refs)))
5853              (while heads
5854                (if (not (memq (header-number (car heads))
5855                               gnus-newsgroup-dormant))
5856                    (progn
5857                      (setq id (symbol-name refs))
5858                      (while (and (setq id (gnus-build-get-header id))
5859                                  (not (car (gnus-gethash 
5860                                             id gnus-newsgroup-dependencies)))))
5861                      (setq heads nil))
5862                  (setq heads (cdr heads)))))))
5863      gnus-newsgroup-dependencies)))
5864
5865 (defun gnus-build-get-header (id)
5866   ;; Look through the buffer of NOV lines and find the header to
5867   ;; ID. Enter this line into the dependencies hash table, and return
5868   ;; the id of the parent article (if any).
5869   (let ((deps gnus-newsgroup-dependencies)
5870         found header)
5871     (prog1
5872         (save-excursion
5873           (set-buffer nntp-server-buffer)
5874           (goto-char (point-min))
5875           (while (and (not found) (search-forward id nil t))
5876             (beginning-of-line)
5877             (setq found (looking-at 
5878                          (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
5879                                  (regexp-quote id))))
5880             (or found (beginning-of-line 2)))
5881           (if found
5882               (let (ref)
5883                 (beginning-of-line)
5884                 (and
5885                  (setq header (gnus-nov-parse-line 
5886                                (read (current-buffer)) deps))
5887                  (setq ref (header-references header))
5888                  (string-match "\\(<[^>]+>\\) *$" ref)
5889                  (substring ref (match-beginning 1) (match-end 1))))))
5890       (and header
5891            (setq gnus-newsgroup-headers (cons header gnus-newsgroup-headers)
5892                  gnus-newsgroup-ancient (cons (header-number header)
5893                                               gnus-newsgroup-ancient))))))
5894
5895 ;; Re-build the thread containing ID.
5896 (defun gnus-rebuild-thread (id)
5897   (let ((dep gnus-newsgroup-dependencies)
5898         (buffer-read-only nil)
5899         parent headers refs thread art)
5900     (while (and id (setq headers
5901                          (car (setq art (gnus-gethash (downcase id) dep)))))
5902       (setq parent art)
5903       (setq id (and (setq refs (header-references headers))
5904                     (string-match "\\(<[^>]+>\\) *$" refs)
5905                     (substring refs (match-beginning 1) (match-end 1)))))
5906     (setq thread (gnus-make-sub-thread (car parent)))
5907     (gnus-rebuild-remove-articles thread)
5908     (let ((beg (point)))
5909       (gnus-summary-prepare-threads (list thread) 0)
5910       (gnus-summary-update-lines beg (point)))))
5911
5912 ;; Delete all lines in the summary buffer that correspond to articles
5913 ;; in this thread.
5914 (defun gnus-rebuild-remove-articles (thread)
5915   (and (gnus-summary-goto-subject (header-number (car thread)))
5916        (gnus-delete-line))
5917   (mapcar (lambda (th) (gnus-rebuild-remove-articles th)) (cdr thread)))
5918
5919 (defun gnus-sort-threads (threads)
5920   ;; Sort threads as specified in `gnus-thread-sort-functions'.
5921   (let ((fun gnus-thread-sort-functions))
5922     (while fun
5923       (setq threads (sort threads (car fun))
5924             fun (cdr fun))))
5925   threads)
5926
5927 (defun gnus-thread-header (thread)
5928   ;; Return header of first article in THREAD.
5929   (if (consp thread)
5930       (if (stringp (car thread))
5931           (car (car (cdr thread)))
5932         (car thread))
5933     thread))
5934
5935 (defun gnus-thread-sort-by-number (h1 h2)
5936   "Sort threads by root article number."
5937   (let ((h1 (gnus-thread-header h1))
5938         (h2 (gnus-thread-header h2)))
5939     (< (header-number h1) (header-number h2))))
5940
5941 (defun gnus-thread-sort-by-author (h1 h2)
5942   "Sort threads by root author."
5943   (let ((h1 (gnus-thread-header h1))
5944         (h2 (gnus-thread-header h2)))
5945     (string-lessp
5946      (let ((extract (funcall 
5947                      gnus-extract-address-components (header-from h1))))
5948        (or (car extract) (cdr extract)))
5949      (let ((extract (funcall
5950                      gnus-extract-address-components (header-from h2))))
5951        (or (car extract) (cdr extract))))))
5952
5953 (defun gnus-thread-sort-by-subject (h1 h2)
5954   "Sort threads by root subject."
5955   (let ((h1 (gnus-thread-header h1))
5956         (h2 (gnus-thread-header h2)))
5957     (string-lessp
5958      (downcase (gnus-simplify-subject (header-subject h1)))
5959      (downcase (gnus-simplify-subject (header-subject h2))))))
5960
5961 (defun gnus-thread-sort-by-date (h1 h2)
5962   "Sort threads by root article date."
5963   (let ((h1 (gnus-thread-header h1))
5964         (h2 (gnus-thread-header h2)))
5965     (string-lessp
5966      (gnus-sortable-date (header-date h1))
5967      (gnus-sortable-date (header-date h2)))))
5968
5969 (defun gnus-thread-sort-by-score (h1 h2)
5970   "Sort threads by root article score.
5971 Unscored articles will be counted as having a score of zero."
5972   (let ((h1 (gnus-thread-header h1))
5973         (h2 (gnus-thread-header h2)))
5974     (let ((s1 (assq (header-number h1) gnus-newsgroup-scored))
5975           (s2 (assq (header-number h2) gnus-newsgroup-scored)))
5976       (> (or (cdr s1) gnus-summary-default-score 0)
5977          (or (cdr s2) gnus-summary-default-score 0)))))
5978
5979 (defun gnus-thread-sort-by-total-score (h1 h2)
5980   "Sort threads by the sum of all scores in the thread.
5981 Unscored articles will be counted as having a score of zero."
5982   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
5983
5984 (defun gnus-thread-total-score (thread)
5985   ;;  This function find the total score of THREAD.
5986   (if (consp thread)
5987       (if (stringp (car thread))
5988           (apply gnus-thread-score-function 0
5989                  (mapcar 'gnus-thread-total-score-1 (cdr thread)))
5990         (gnus-thread-total-score-1 thread))
5991     (gnus-thread-total-score-1 (list thread))))
5992
5993 (defun gnus-thread-total-score-1 (root)
5994   ;; This function find the total score of the thread below ROOT.
5995   (setq root (car root))
5996   (apply gnus-thread-score-function
5997          (or (cdr (assq (header-number root) gnus-newsgroup-scored))
5998              gnus-summary-default-score 0)
5999          (mapcar 'gnus-thread-total-score
6000                  (cdr (gnus-gethash (downcase (header-id root))
6001                                     gnus-newsgroup-dependencies)))))
6002
6003 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
6004 (defvar gnus-tmp-prev-subject "")
6005 (defvar gnus-tmp-adopt-thread nil)
6006
6007 ;; Basic ideas by Paul Dworkin <paul@media-lab.media.mit.edu>.
6008 (defun gnus-summary-prepare-threads 
6009   (threads level &optional not-child no-subject cull)
6010   "Prepare summary buffer from THREADS and indentation LEVEL.  
6011 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'  
6012 or a straight list of headers."
6013   (let (thread header number subject clevel)
6014     (while threads
6015       (setq thread (car threads)
6016             threads (cdr threads))
6017       ;; If `thread' is a cons, hierarchical threads are used.  If not,
6018       ;; `thread' is the header.
6019       (if (consp thread)
6020           (setq header (car thread))
6021         (setq header thread)
6022         (and cull
6023              (or (memq (setq number (header-number header))
6024                        gnus-newsgroup-dormant)
6025                  (and gnus-summary-expunge-below
6026                       (< (or (cdr (assq number gnus-newsgroup-scored))
6027                              gnus-summary-default-score 0)
6028                          gnus-summary-expunge-below)))
6029              (progn
6030                (setq header nil)
6031                (setq gnus-newsgroup-unreads 
6032                      (delq number gnus-newsgroup-unreads)))))
6033       (cond 
6034        ((stringp header)
6035         ;; The header is a dummy root.
6036         (cond ((eq gnus-summary-make-false-root 'adopt)
6037                ;; We let the first article adopt the rest.
6038                (let ((gnus-tmp-adopt-thread (list (cdr thread))))
6039                  (gnus-summary-prepare-threads (list (car (cdr thread))) 0))
6040                (setq thread (cdr (cdr thread)))
6041                (while thread
6042                  (gnus-summary-prepare-threads (list (car thread)) 1 t)
6043                  (setq thread (cdr thread))))
6044               ((eq gnus-summary-make-false-root 'dummy)
6045                ;; We output a dummy root.
6046                (gnus-summary-insert-dummy-line 
6047                 nil header (header-number (car (car (cdr thread)))))
6048                (setq clevel 1))
6049               ((eq gnus-summary-make-false-root 'empty)
6050                ;; We print the articles with empty subject fields. 
6051                (let ((gnus-tmp-adopt-thread (list (cdr thread))))
6052                  (gnus-summary-prepare-threads (list (car (cdr thread))) 0))
6053                (setq thread (cdr (cdr thread)))
6054                (while thread
6055                  (gnus-summary-prepare-threads 
6056                   (list (car thread)) 0 nil
6057                   (not (and (eq gnus-summary-gather-subject-limit 'fuzzy)
6058                             (not (string=  
6059                                   (gnus-simplify-subject-re 
6060                                    (header-subject (car (car thread))))
6061                                   (gnus-simplify-subject-re header))))))
6062                  (setq thread (cdr thread))))
6063               (t
6064                ;; We do not make a root for the gathered
6065                ;; sub-threads at all.  
6066                (setq clevel 0)))
6067         ;; Print the sub-threads.
6068         (and (consp thread) (cdr thread)
6069              (gnus-summary-prepare-threads (cdr thread) clevel)))
6070        ;; The header is a real article.
6071        (header
6072         (setq number (header-number header)
6073               subject (header-subject header))
6074         (and gnus-newsgroup-async
6075              (setq gnus-newsgroup-threads
6076                    (cons (cons (header-number header)
6077                                (header-lines header)) gnus-newsgroup-threads)))
6078         (gnus-summary-insert-line
6079          nil header level nil 
6080          (cond ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
6081                ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
6082                ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
6083                ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
6084                (t gnus-ancient-mark))
6085          (memq number gnus-newsgroup-replied)
6086          (memq number gnus-newsgroup-expirable)
6087          (if no-subject 
6088              gnus-summary-same-subject
6089            (if (or (zerop level)
6090                    (and gnus-thread-ignore-subject
6091                         (not (string= 
6092                               (gnus-simplify-subject-re gnus-tmp-prev-subject)
6093                               (gnus-simplify-subject-re subject)))))
6094                subject
6095              gnus-summary-same-subject))
6096          not-child
6097          (cdr (assq number gnus-newsgroup-scored)))
6098         (setq gnus-tmp-prev-subject subject)
6099         ;; Recursively print subthreads.
6100         (and (consp thread) (cdr thread)
6101              (gnus-summary-prepare-threads (cdr thread) (1+ level))))))))
6102
6103 (defun gnus-select-newsgroup (group &optional read-all)
6104   "Select newsgroup GROUP.
6105 If READ-ALL is non-nil, all articles in the group are selected."
6106   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
6107          (info (nth 2 entry))
6108          articles header-marks)
6109     (gnus-check-news-server
6110      (setq gnus-current-select-method (gnus-find-method-for-group group)))
6111
6112     (or (gnus-server-opened gnus-current-select-method)
6113         (gnus-open-server gnus-current-select-method)
6114         (error "Couldn't open server"))
6115     
6116     (or (and (eq (car entry) t)
6117              (gnus-activate-newsgroup (car info)))
6118         (gnus-request-group group t)
6119         (progn
6120           (kill-buffer (current-buffer))
6121           (error "Couldn't request group %s: %s" 
6122                  group (gnus-status-message group))))
6123
6124     (setq gnus-newsgroup-name group)
6125     (setq gnus-newsgroup-unselected nil)
6126     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
6127
6128     (and gnus-asynchronous
6129          (gnus-check-backend-function 
6130           'request-asynchronous gnus-newsgroup-name)
6131          (setq gnus-newsgroup-async
6132                (gnus-request-asynchronous gnus-newsgroup-name)))
6133
6134     (setq articles (gnus-articles-to-read group read-all))
6135
6136     (cond 
6137      ((null articles) 
6138       (gnus-message 3 "Couldn't select newsgroup")
6139       'quit)
6140      ((eq articles 0) nil)
6141      (t
6142       ;; Init the dependencies hash table.
6143       (setq gnus-newsgroup-dependencies 
6144             (gnus-make-hashtable (length articles)))
6145       ;; Retrieve the headers and read them in.
6146       (setq gnus-newsgroup-headers 
6147             (if (eq 'nov (setq gnus-headers-retrieved-by
6148                                (gnus-retrieve-headers 
6149                                 (if (and gnus-fetch-old-headers 
6150                                          (not (eq 1 (car articles))))
6151                                     (cons 1 articles)
6152                                   articles)
6153                                 gnus-newsgroup-name)))
6154                 (progn
6155                   (gnus-get-newsgroup-headers-xover articles))
6156               ;; If we were to fetch old headers, but the backend didn't
6157               ;; support XOVER, then it is possible we fetched one article
6158               ;; that we shouldn't have. If that's the case, we pop it off the
6159               ;; list of headers.
6160               (if (not gnus-fetch-old-headers)
6161                   ()
6162                 (save-excursion
6163                   (set-buffer nntp-server-buffer)
6164                   (goto-char (point-min))
6165                   (and (looking-at "[0-9]+[ \t]+1[ \t]")
6166                        (delete-region 
6167                         (point) 
6168                         (search-forward "\n.\n" nil t)))))
6169               (gnus-get-newsgroup-headers)))
6170       ;; Remove canceled articles from the list of unread articles.
6171       (setq gnus-newsgroup-unreads
6172             (gnus-set-sorted-intersection 
6173              gnus-newsgroup-unreads
6174              (mapcar (lambda (headers) (header-number headers))
6175                      gnus-newsgroup-headers)))
6176       ;; Adjust and set lists of article marks.
6177       (and info
6178            (let (marked)
6179              (gnus-adjust-marked-articles info)
6180              (setq gnus-newsgroup-marked 
6181                    (cdr (assq 'tick (setq marked (nth 3 info)))))
6182              (setq gnus-newsgroup-replied (cdr (assq 'reply marked)))
6183              (setq gnus-newsgroup-expirable (cdr (assq 'expire marked)))
6184              (setq gnus-newsgroup-killed (cdr (assq 'killed marked)))
6185              (setq gnus-newsgroup-bookmarks (cdr (assq 'bookmark marked)))
6186              (setq gnus-newsgroup-dormant (cdr (assq 'dormant marked)))
6187              (setq gnus-newsgroup-scored (cdr (assq 'score marked)))
6188              (setq gnus-newsgroup-processable nil)))
6189       ;; Check whether auto-expire is to be done in this group.
6190       (setq gnus-newsgroup-auto-expire
6191             (or (and (stringp gnus-auto-expirable-newsgroups)
6192                      (string-match gnus-auto-expirable-newsgroups group))
6193                 (memq 'auto-expire (nth 5 info))))
6194       ;; First and last article in this newsgroup.
6195       (and gnus-newsgroup-headers
6196            (setq gnus-newsgroup-begin 
6197                  (header-number (car gnus-newsgroup-headers)))
6198            (setq gnus-newsgroup-end
6199                  (header-number (gnus-last-element gnus-newsgroup-headers))))
6200       (setq gnus-reffed-article-number -1)
6201       ;; GROUP is successfully selected.
6202       (or gnus-newsgroup-headers t)))))
6203
6204 (defun gnus-articles-to-read (group read-all)
6205   ;; Find out what articles the user wants to read.
6206   (let* ((articles
6207           ;; Select all articles if `read-all' is non-nil, or if all the
6208           ;; unread articles are dormant articles.
6209           (if (or (and read-all (not (numberp read-all)))
6210                   (= (length gnus-newsgroup-unreads) 
6211                      (length gnus-newsgroup-dormant)))
6212               (gnus-uncompress-range 
6213                (gnus-gethash group gnus-active-hashtb))
6214             gnus-newsgroup-unreads))
6215          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
6216          (scored (length scored-list))
6217          (number (length articles))
6218          (marked (+ (length gnus-newsgroup-marked)
6219                     (length gnus-newsgroup-dormant)))
6220          (select
6221           (cond 
6222            ((numberp read-all)
6223             read-all)
6224            (t
6225             (condition-case ()
6226                 (cond ((and (or (<= scored marked)
6227                                 (= scored number))
6228                             (numberp gnus-large-newsgroup)
6229                             (> number gnus-large-newsgroup))
6230                        (let ((input
6231                               (read-string
6232                                (format
6233                                 "How many articles from %s (default %d): "
6234                                 gnus-newsgroup-name number))))
6235                          (if (string-match "^[ \t]*$" input)
6236                              number input)))
6237                       ((and (> scored marked) (< scored number))
6238                        (let ((input
6239                               (read-string
6240                                (format 
6241                                 "%s %s (%d scored, %d total): "
6242                                 "How many articles from"
6243                                 group scored number))))
6244                          (if (string-match "^[ \t]*$" input)
6245                              number input)))
6246                       (t number))
6247               (quit nil)))))
6248          total-articles)
6249     (setq select (if (stringp select) (string-to-number select) select))
6250     (if (or (null select) (zerop select))
6251         select
6252       (if (and (not (zerop scored)) (<= (abs select) scored))
6253           (progn
6254             (setq articles (sort scored-list '<))
6255             (setq number (length articles)))
6256         (setq articles (copy-sequence articles)))
6257
6258       (setq total-articles articles)
6259       
6260       (if (< (abs select) number)
6261           (if (< select 0) 
6262               ;; Select the N oldest articles.
6263               (setcdr (nthcdr (1- (abs select)) articles) nil)
6264             ;; Select the N most recent articles.
6265             (setq articles (nthcdr (- number select) articles))))
6266       (setq gnus-newsgroup-unselected
6267             (gnus-sorted-intersection
6268              gnus-newsgroup-unreads
6269              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
6270       articles)))
6271
6272 (defun gnus-killed-articles (killed articles)
6273   (let (out)
6274     (while articles
6275       (if (inline (gnus-member-of-range (car articles) killed))
6276           (setq out (cons (car articles) out)))
6277       (setq articles (cdr articles)))
6278     out))
6279
6280 (defun gnus-adjust-marked-articles (info &optional active)
6281   "Remove all marked articles that are no longer legal."
6282   (let ((marked-lists (nth 3 info))
6283         (active (or active (gnus-gethash (car info) gnus-active-hashtb)))
6284         marked m prev)
6285     ;; There are many types of marked articles.
6286     (while marked-lists
6287       (setq m (cdr (setq prev (car marked-lists))))
6288       (cond ((or (eq 'tick (car prev)) (eq 'dormant (car prev)))
6289              ;; Make sure that all ticked articles are a subset of the
6290              ;; unread/unselected articles.
6291              (while m
6292                (if (or (memq (car m) gnus-newsgroup-unreads)
6293                        (memq (car m) gnus-newsgroup-unselected))
6294                    (setq prev m)
6295                  (setcdr prev (cdr m)))
6296                (setq m (cdr m))))
6297             ((eq 'score (car prev))
6298              ;; Scored articles should be a subset of
6299              ;; unread/unselected articles. 
6300              (while m
6301                (if (or (memq (car (car m)) gnus-newsgroup-unreads)
6302                        (memq (car (car m)) gnus-newsgroup-unreads))
6303                    (setq prev m)
6304                  (setcdr prev (cdr m)))
6305                (setq m (cdr m))))
6306             ((eq 'bookmark (car prev))
6307              ;; Bookmarks should be a subset of active articles.
6308              (while m
6309                (if (< (car (car m)) (car active))
6310                    (setcdr prev (cdr m))
6311                  (setq prev m))
6312                (setq m (cdr m))))
6313             ((eq 'killed (car prev))
6314              ;; Articles that have been through the kill process are
6315              ;; to be a subset of active articles.
6316              (while (and m (< (or (and (numberp (car m)) (car m))
6317                                   (cdr (car m)))
6318                               (car active)))
6319                (setcdr prev (cdr m))
6320                (setq m (cdr m)))
6321              (if (and m (< (or (and (numberp (car m)) (car m))
6322                                (car (car m)))
6323                            (car active))) 
6324                  (setcar (if (numberp (car m)) m (car m)) (car active))))
6325             ((or (eq 'reply (car prev)) (eq 'expire (car prev)))
6326              ;; The replied and expirable articles have to be articles
6327              ;; that are active. 
6328              (while m
6329                (if (< (car m) (car active))
6330                    (setcdr prev (cdr m))
6331                  (setq prev m))
6332                (setq m (cdr m)))))
6333       (setq marked-lists (cdr marked-lists)))
6334     ;; Remove all lists that are empty.
6335     (setq marked-lists (nth 3 info))
6336     (if marked-lists
6337         (progn
6338           (while (= 1 (length (car marked-lists)))
6339             (setq marked-lists (cdr marked-lists)))
6340           (setq m (cdr (setq prev marked-lists)))
6341           (while m
6342             (if (= 1 (length (car m)))
6343                 (setcdr prev (cdr m))
6344               (setq prev m))
6345             (setq m (cdr m)))
6346           (setcar (nthcdr 3 info) marked-lists)))
6347     ;; Finally, if there are no marked lists at all left, and if there
6348     ;; are no elements after the lists in the info list, we just chop
6349     ;; the info list off before the marked lists.
6350     (and (null marked-lists) 
6351          (not (nthcdr 4 info))
6352          (setcdr (nthcdr 2 info) nil)))
6353   info)
6354
6355 (defun gnus-set-marked-articles 
6356   (info ticked replied expirable killed dormant bookmark score) 
6357   "Enter the various lists of marked articles into the newsgroup info list."
6358   (let (newmarked)
6359     (and ticked (setq newmarked (cons (cons 'tick ticked) nil)))
6360     (and replied (setq newmarked (cons (cons 'reply replied) newmarked)))
6361     (and expirable (setq newmarked (cons (cons 'expire expirable) 
6362                                          newmarked)))
6363     (and killed (setq newmarked (cons (cons 'killed killed) newmarked)))
6364     (and dormant (setq newmarked (cons (cons 'dormant dormant) newmarked)))
6365     (and bookmark (setq newmarked (cons (cons 'bookmark bookmark) 
6366                                         newmarked)))
6367     (and score (setq newmarked (cons (cons 'score score) newmarked)))
6368     (if (nthcdr 3 info)
6369         (progn
6370           (setcar (nthcdr 3 info) newmarked)
6371           (and (not newmarked)
6372                (not (nthcdr 4 info))
6373                (setcdr (nthcdr 2 info) nil)))
6374       (if newmarked
6375           (setcdr (nthcdr 2 info) (list newmarked))))))
6376
6377 (defun gnus-add-marked-articles (group type articles &optional info force)
6378   ;; Add ARTICLES of TYPE to the info of GROUP.
6379   ;; If INFO is non-nil, use that info. If FORCE is non-nil, don't
6380   ;; add, but replace marked articles of TYPE with ARTICLES.
6381   (let ((info (or info (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
6382         marked m)
6383     (or (not info)
6384         (and (not (setq marked (nthcdr 3 info)))
6385              (setcdr (nthcdr 2 info) (list (list (cons type articles)))))
6386         (and (not (setq m (assq type (car marked))))
6387              (setcar marked (cons (cons type articles) (car marked))))
6388         (if force
6389             (setcdr m articles)
6390           (nconc m articles)))))
6391          
6392 (defun gnus-set-mode-line (where)
6393   "This function sets the mode line of the article or summary buffers.
6394 If WHERE is `summary', the summary mode line format will be used."
6395   (if (memq where gnus-updated-mode-lines)
6396       (let (mode-string)
6397         (save-excursion
6398           (set-buffer gnus-summary-buffer)
6399           (let* ((mformat (if (eq where 'article) 
6400                               gnus-article-mode-line-format-spec
6401                             gnus-summary-mode-line-format-spec))
6402                  (group-name gnus-newsgroup-name)
6403                  (article-number (or gnus-current-article 0))
6404                  (unread (- (length gnus-newsgroup-unreads)
6405                             (length gnus-newsgroup-dormant)))
6406                  (unread-and-unticked 
6407                   (- unread (length gnus-newsgroup-marked)))
6408                  (unselected (length gnus-newsgroup-unselected))
6409                  (unread-and-unselected
6410                   (cond ((and (zerop unread-and-unticked)
6411                               (zerop unselected)) "")
6412                         ((zerop unselected) 
6413                          (format "{%d more}" unread-and-unticked))
6414                         (t (format "{%d(+%d) more}"
6415                                    unread-and-unticked unselected))))
6416                  (subject
6417                   (if gnus-current-headers
6418                       (header-subject gnus-current-headers) ""))
6419                  (max-len (and gnus-mode-non-string-length
6420                                (- (frame-width) gnus-mode-non-string-length)))
6421                  header) ;; passed as argument to any user-format-funcs
6422             (setq mode-string (eval mformat))
6423             (or (numberp max-len)
6424                 (setq max-len (length mode-string)))
6425             (if (< max-len 4) (setq max-len 4))
6426             (if (> (length mode-string) max-len)
6427                 ;; modified by MORIOKA Tomohiko <morioka@jaist.ac.jp>
6428                 ;;  function `substring' might cut on a middle
6429                 ;;  of multi-octet character.
6430                 (setq mode-string 
6431                       (concat (gnus-truncate-string mode-string (- max-len 3))
6432                               "...")))
6433             (setq mode-string (format (format "%%-%ds" max-len)
6434                                       mode-string))))
6435         (setq mode-line-buffer-identification mode-string)
6436         (set-buffer-modified-p t))))
6437
6438 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
6439   "Go through the HEADERS list and add all Xrefs to a hash table.
6440 The resulting hash table is returned, or nil if no Xrefs were found."
6441   (let* ((from-method (gnus-find-method-for-group from-newsgroup))
6442          (prefix (if (and 
6443                       (gnus-group-foreign-p from-newsgroup)
6444                       (not (memq 'virtual 
6445                                  (assoc (symbol-name (car from-method))
6446                                         gnus-valid-select-methods))))
6447                      (gnus-group-real-prefix from-newsgroup)))
6448          (xref-hashtb (make-vector 63 0))
6449          start group entry number xrefs header)
6450     (while headers
6451       (setq header (car headers))
6452       (if (and (setq xrefs (header-xref header))
6453                (not (memq (header-number header) unreads)))
6454           (progn
6455             (setq start 0)
6456             (while (string-match "\\([^ ]+\\):\\([0-9]+\\)" xrefs start)
6457               (setq start (match-end 0))
6458               (setq group (concat prefix (substring xrefs (match-beginning 1) 
6459                                                     (match-end 1))))
6460               (setq number 
6461                     (string-to-int (substring xrefs (match-beginning 2) 
6462                                               (match-end 2))))
6463               (if (setq entry (gnus-gethash group xref-hashtb))
6464                   (setcdr entry (cons number (cdr entry)))
6465                 (gnus-sethash group (cons number nil) xref-hashtb)))))
6466       (setq headers (cdr headers)))
6467     (if start xref-hashtb nil)))
6468
6469 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads expirable)
6470   "Look through all the headers and mark the Xrefs as read."
6471   (let ((virtual (memq 'virtual 
6472                        (assoc (symbol-name (car (gnus-find-method-for-group 
6473                                                  from-newsgroup)))
6474                               gnus-valid-select-methods)))
6475         name entry read info xref-hashtb idlist active num range exps method
6476         nth4)
6477     (save-excursion
6478       (set-buffer gnus-group-buffer)
6479       (if (setq xref-hashtb 
6480                 (gnus-create-xref-hashtb from-newsgroup headers unreads))
6481           (mapatoms 
6482            (lambda (group)
6483              (if (string= from-newsgroup (setq name (symbol-name group)))
6484                  ()
6485                (setq idlist (symbol-value group))
6486                ;; Dead groups are not updated.
6487                (if (and (prog1 
6488                             (setq entry (gnus-gethash name gnus-newsrc-hashtb)
6489                                   info (nth 2 entry))
6490                           (if (stringp (setq nth4 (nth 4 info)))
6491                               (setq nth4 (gnus-server-to-method nth4))))
6492                         ;; Only do the xrefs if the group has the same
6493                         ;; select method as the group we have just read.
6494                         (or (gnus-methods-equal-p 
6495                              nth4 (gnus-find-method-for-group from-newsgroup))
6496                             virtual
6497                             (equal nth4 
6498                                    (setq method (gnus-find-method-for-group 
6499                                                  from-newsgroup)))
6500                             (and (equal (car nth4) (car method))
6501                                  (equal (nth 1 nth4) (nth 1 method))))
6502                         gnus-use-cross-reference
6503                         (or (not (eq gnus-use-cross-reference t))
6504                             virtual
6505                             ;; Only do cross-references on subscribed
6506                             ;; groups, if that is what is wanted.  
6507                             (<= (nth 1 info) gnus-level-subscribed)))
6508                    (gnus-group-make-articles-read name idlist expirable))))
6509            xref-hashtb)))))
6510
6511 (defun gnus-group-make-articles-read (group articles expirable)
6512   (let* ((num 0)
6513          (entry (gnus-gethash group gnus-newsrc-hashtb))
6514          (info (nth 2 entry))
6515          (active (gnus-gethash name gnus-active-hashtb))
6516          exps expirable range)
6517     ;; First peel off all illegal article numbers.
6518     (if active
6519         (let ((ids articles)
6520               (ticked (cdr (assq 'tick (nth 3 info))))
6521               (dormant (cdr (assq 'dormant (nth 3 info))))
6522               id)
6523           (setq exps nil)
6524           (while ids
6525             (setq id (car ids))
6526             (if (or (> id (cdr active))
6527                     (< id (car active))
6528                     (memq id ticked)
6529                     (memq id dormant))
6530                 (setq articles (delq id articles)))
6531             (and (memq id expirable)
6532                  (setq exps (cons id exps)))
6533             (setq ids (cdr ids)))))
6534     ;; Update expirable articles.
6535     (gnus-add-marked-articles nil 'expirable exps info)
6536     (and active
6537          (null (nth 2 info))
6538          (> (car active) 1)
6539          (setcar (nthcdr 2 info) (cons 1 (1- (car active)))))
6540     (setcar (nthcdr 2 info)
6541             (setq range
6542                   (gnus-add-to-range 
6543                    (nth 2 info) 
6544                    (setq articles (sort articles '<)))))
6545     ;; Then we have to re-compute how many unread
6546     ;; articles there are in this group.
6547     (if active
6548         (progn
6549           (cond 
6550            ((not range)
6551             (setq num (- (1+ (cdr active)) (car active))))
6552            ((not (listp (cdr range)))
6553             (setq num (- (cdr active) (- (1+ (cdr range)) 
6554                                          (car range)))))
6555            (t
6556             (while range
6557               (if (numberp (car range))
6558                   (setq num (1+ num))
6559                 (setq num (+ num (- (1+ (cdr (car range)))
6560                                     (car (car range))))))
6561               (setq range (cdr range)))
6562             (setq num (- (cdr active) num))))
6563           ;; Update the number of unread articles.
6564           (setcar 
6565            entry 
6566            (max 0 (- num 
6567                      (length (cdr (assq 'tick (nth 3 info))))
6568                      (length 
6569                       (cdr (assq 'dormant (nth 3 info)))))))
6570           ;; Update the group buffer.
6571           (gnus-group-update-group name t)))))
6572
6573 (defun gnus-methods-equal-p (m1 m2)
6574   (let ((m1 (or m1 gnus-select-method))
6575         (m2 (or m2 gnus-select-method)))
6576     (or (equal m1 m2)
6577         (and (eq (car m1) (car m2))
6578              (or (not (memq 'address (assoc (symbol-name (car m1))
6579                                             gnus-valid-select-methods)))
6580                  (equal (nth 1 m1) (nth 1 m2)))))))
6581
6582 (defsubst gnus-header-value ()
6583   (buffer-substring (match-end 0) (gnus-point-at-eol)))
6584
6585 (defvar gnus-newsgroup-none-id 0)
6586
6587 (defun gnus-get-newsgroup-headers ()
6588   (setq gnus-article-internal-prepare-hook nil)
6589   (let ((cur nntp-server-buffer)
6590         (dependencies gnus-newsgroup-dependencies)
6591         headers char article id dep end)
6592     (save-excursion
6593       (set-buffer nntp-server-buffer)
6594       (goto-char (point-min))
6595       ;; Search to the beginning of the next header. Error messages
6596       ;; do not begin with 2 or 3.
6597       (while (re-search-forward "^[23][0-9]+ " nil t)
6598         (let ((header (make-vector 9 nil))
6599               (c (following-char))
6600               (case-fold-search t)
6601               (p (point))
6602               from subject in-reply-to references ref)
6603           (setq id nil
6604                 ref nil
6605                 references nil
6606                 subject nil
6607                 from nil)
6608           (header-set-number header (setq article (read cur)))
6609           ;; This implementation of this function, with nine
6610           ;; search-forwards instead of the one re-search-forward and
6611           ;; a case (which basically was the old function) is actually
6612           ;; about twice as fast, even though it looks messier. You
6613           ;; can't have everything, I guess. Speed and elegance
6614           ;; doesn't always come hand in hand.
6615           (save-restriction
6616             (narrow-to-region (point) (or (save-excursion 
6617                                             (search-forward "\n.\n" nil t))
6618                                           (point)))
6619             (if (search-forward "\nfrom: " nil t)
6620                 (header-set-from header (gnus-header-value))
6621               (header-set-from header "(nobody)"))
6622             (goto-char p)
6623             (if (search-forward "\nsubject: " nil t)
6624                 (header-set-subject header (gnus-header-value))
6625               (header-set-subject header "(none)"))
6626             (goto-char p)
6627             (and (search-forward "\nxref: " nil t)
6628                  (header-set-xref header (gnus-header-value)))
6629             (goto-char p)
6630             (or (numberp (and (search-forward "\nlines: " nil t)
6631                               (header-set-lines header (read cur))))
6632                 (header-set-lines header 0))
6633             (goto-char p)
6634             (and (search-forward "\ndate: " nil t)
6635                  (header-set-date header (gnus-header-value)))
6636             (goto-char p)
6637             (if (search-forward "\nmessage-id: " nil t)
6638                 (header-set-id header (setq id (gnus-header-value)))
6639               ;; If there was no message-id, we just fake one to make
6640               ;; subsequent routines simpler.
6641               (header-set-id 
6642                header 
6643                (setq id (concat "none+" 
6644                                 (int-to-string 
6645                                  (setq gnus-newsgroup-none-id 
6646                                        (1+ gnus-newsgroup-none-id)))))))
6647             (goto-char p)
6648             (if (search-forward "\nreferences: " nil t)
6649                 (progn
6650                   (header-set-references header (gnus-header-value))
6651                   (setq end (match-end 0))
6652                   (save-excursion
6653                     (setq ref 
6654                           (downcase
6655                            (buffer-substring
6656                             (progn 
6657                               (end-of-line)
6658                               (search-backward ">" end t)
6659                               (1+ (point)))
6660                             (progn
6661                               (search-backward "<" end t)
6662                               (point)))))))
6663               ;; Get the references from the in-reply-to header if there
6664               ;; ware no references and the in-reply-to header looks
6665               ;; promising. 
6666               (if (and (search-forward "\nin-reply-to: " nil t)
6667                        (setq in-reply-to (gnus-header-value))
6668                        (string-match "<[^>]+>" in-reply-to))
6669                   (progn
6670                     (header-set-references 
6671                      header 
6672                      (setq ref (substring in-reply-to (match-beginning 0)
6673                                           (match-end 0))))
6674                     (setq ref (downcase ref)))
6675                 (setq ref "none")))
6676             ;; We do some threading while we read the headers. The
6677             ;; message-id and the last reference are both entered into
6678             ;; the same hash table. Some tippy-toeing around has to be
6679             ;; done in case an article has arrived before the article
6680             ;; which it refers to.
6681             (if (boundp (setq dep (intern (downcase id) dependencies)))
6682                 (if (car (symbol-value dep))
6683                     ;; An article with this Message-ID has already
6684                     ;; been seen, so we ignore this one, except we add
6685                     ;; any additional Xrefs (in case the two articles
6686                     ;; came from different servers.
6687                     (progn
6688                       (header-set-xref 
6689                        (car (symbol-value dep))
6690                        (concat (or (header-xref (car (symbol-value dep))) "")
6691                                (or (header-xref header) "")))
6692                       (setq header nil))
6693                   (setcar (symbol-value dep) header))
6694               (set dep (list header)))
6695             (if header
6696                 (progn
6697                   (if (boundp (setq dep (intern ref dependencies)))
6698                       (setcdr (symbol-value dep) 
6699                               (cons header (cdr (symbol-value dep))))
6700                     (set dep (list nil header)))
6701                   (setq headers (cons header headers))))
6702             (goto-char (point-max))))))
6703     (nreverse headers)))
6704
6705 ;; The following macros and functions were written by Felix Lee
6706 ;; <flee@cse.psu.edu>. 
6707
6708 (defmacro gnus-nov-read-integer ()
6709   '(prog1
6710        (if (= (following-char) ?\t)
6711            0
6712          (let ((num (condition-case nil (read buffer) (error nil))))
6713            (if (numberp num) num 0)))
6714      (or (eobp) (forward-char 1))))
6715
6716 (defmacro gnus-nov-skip-field ()
6717   '(search-forward "\t" eol 'move))
6718
6719 (defmacro gnus-nov-field ()
6720   '(buffer-substring (point) (if (gnus-nov-skip-field) (1- (point)) eol)))
6721
6722 ;; Goes through the xover lines and returns a list of vectors
6723 (defun gnus-get-newsgroup-headers-xover (sequence)
6724   "Parse the news overview data in the server buffer, and return a
6725 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
6726   ;; Get the Xref when the users reads the articles since most/some
6727   ;; NNTP servers do not include Xrefs when using XOVER.
6728   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
6729   (let ((cur nntp-server-buffer)
6730         (dependencies gnus-newsgroup-dependencies)
6731         (none 0)
6732         number headers header)
6733     (save-excursion
6734       (set-buffer nntp-server-buffer)
6735       (goto-char (point-min))
6736       (while (and sequence (not (eobp)))
6737         (setq number (read cur))
6738         (while (and sequence (< (car sequence) number))
6739           (setq sequence (cdr sequence)))
6740         (and sequence 
6741              (eq number (car sequence))
6742              (progn
6743                (setq sequence (cdr sequence))
6744                (if (setq header 
6745                          (inline (gnus-nov-parse-line number dependencies)))
6746                    (setq headers (cons header headers)))))
6747         (forward-line 1))
6748       (setq headers (nreverse headers)))
6749     headers))
6750
6751 ;; This function has to be called with point after the article number
6752 ;; on the beginning of the line.
6753 (defun gnus-nov-parse-line (number dependencies)
6754   (let ((none 0)
6755         (eol (gnus-point-at-eol)) 
6756         (buffer (current-buffer))
6757         header ref id dep)
6758
6759     ;; overview: [num subject from date id refs chars lines misc]
6760     (narrow-to-region (point) eol)
6761     (forward-char)
6762
6763     (condition-case nil
6764         (setq header
6765               (vector 
6766                number                   ; number
6767                (gnus-nov-field)         ; subject
6768                (gnus-nov-field)         ; from
6769                (gnus-nov-field)         ; date
6770                (setq id (or (gnus-nov-field)
6771                             (concat "none+"
6772                                     (int-to-string 
6773                                      (setq none (1+ none)))))) ; id
6774                (progn
6775                  (save-excursion
6776                    (let ((beg (point)))
6777                      (search-forward "\t" eol)
6778                      (if (search-backward ">" beg t)
6779                          (setq ref 
6780                                (downcase 
6781                                 (buffer-substring 
6782                                  (1+ (point))
6783                                  (progn
6784                                    (search-backward "<" beg t)
6785                                    (point)))))
6786                        (setq ref nil))))
6787                  (gnus-nov-field))      ; refs
6788                (gnus-nov-read-integer)  ; chars
6789                (gnus-nov-read-integer)  ; lines
6790                (if (= (following-char) ?\n)
6791                    nil
6792                  (gnus-nov-field))      ; misc
6793                ))
6794       (error (progn 
6795                (ding)
6796                (message "Strange nov line.")
6797                (setq header nil)
6798                (goto-char eol))))
6799
6800     (widen)
6801
6802     ;; We build the thread tree.
6803     (and header
6804          (if (boundp (setq dep (intern (downcase id) dependencies)))
6805              (if (car (symbol-value dep))
6806                  ;; An article with this Message-ID has already been seen,
6807                  ;; so we ignore this one, except we add any additional
6808                  ;; Xrefs (in case the two articles came from different
6809                  ;; servers.
6810                  (progn
6811                    (header-set-xref 
6812                     (car (symbol-value dep))
6813                     (concat (or (header-xref (car (symbol-value dep))) "")
6814                             (or (header-xref header) "")))
6815                    (setq header nil))
6816                (setcar (symbol-value dep) header))
6817            (set dep (list header))))
6818     (if header
6819         (progn
6820           (if (boundp (setq dep (intern (or ref "none") 
6821                                         dependencies)))
6822               (setcdr (symbol-value dep) 
6823                       (cons header (cdr (symbol-value dep))))
6824             (set dep (list nil header)))))
6825     header))
6826
6827 (defun gnus-article-get-xrefs ()
6828   "Fill in the Xref value in `gnus-current-headers', if necessary.
6829 This is meant to be called in `gnus-article-internal-prepare-hook'."
6830   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
6831                                  gnus-current-headers)))
6832     (or (not gnus-use-cross-reference)
6833         (not headers)
6834         (and (header-xref headers)
6835              (not (string= (header-xref headers) "")))
6836         (let ((case-fold-search t)
6837               xref)
6838           (save-restriction
6839             (gnus-narrow-to-headers)
6840             (goto-char (point-min))
6841             (if (or (and (eq (downcase (following-char)) ?x)
6842                          (looking-at "Xref:"))
6843                     (search-forward "\nXref:" nil t))
6844                 (progn
6845                   (goto-char (1+ (match-end 0)))
6846                   (setq xref (buffer-substring (point) 
6847                                                (progn (end-of-line) (point))))
6848                   (header-set-xref headers xref))))))))
6849
6850 (defalias 'gnus-find-header-by-number 'gnus-get-header-by-number)
6851 (make-obsolete 'gnus-find-header-by-number 'gnus-get-header-by-number)
6852
6853 ;; Return a header specified by a NUMBER.
6854 (defun gnus-get-header-by-number (number)
6855   (save-excursion
6856     (set-buffer gnus-summary-buffer)
6857     (or gnus-newsgroup-headers-hashtb-by-number
6858         (gnus-make-headers-hashtable-by-number))
6859     (gnus-gethash (int-to-string number)
6860                   gnus-newsgroup-headers-hashtb-by-number)))
6861
6862 (defun gnus-make-headers-hashtable-by-number ()
6863   "Make hashtable for the variable gnus-newsgroup-headers by number."
6864   (save-excursion
6865     (set-buffer gnus-summary-buffer)
6866     (let ((headers gnus-newsgroup-headers)
6867           header)
6868       (setq gnus-newsgroup-headers-hashtb-by-number
6869             (gnus-make-hashtable (length headers)))
6870       (while headers
6871         (setq header (car headers))
6872         (gnus-sethash (int-to-string (header-number header))
6873                       header gnus-newsgroup-headers-hashtb-by-number)
6874         (setq headers (cdr headers))))))
6875
6876 (defun gnus-more-header-backward ()
6877   "Find new header backward."
6878   (let ((first (car (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
6879         (artnum gnus-newsgroup-begin)
6880         (header nil))
6881     (while (and (not header)
6882                 (> artnum first))
6883       (setq artnum (1- artnum))
6884       (setq header (gnus-read-header artnum)))
6885     header))
6886
6887 (defun gnus-more-header-forward (&optional backward)
6888   "Find new header forward.
6889 If BACKWARD, find new header backward instead."
6890   (if backward
6891       (gnus-more-header-backward)
6892     (let ((last (cdr (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
6893           (artnum gnus-newsgroup-end)
6894           (header nil))
6895       (while (and (not header)
6896                   (< artnum last))
6897         (setq artnum (1+ artnum))
6898         (setq header (gnus-read-header artnum)))
6899       header)))
6900
6901 (defun gnus-extend-newsgroup (header &optional backward)
6902   "Extend newsgroup selection with HEADER.
6903 Optional argument BACKWARD means extend toward backward."
6904   (if header
6905       (let ((artnum (header-number header)))
6906         (setq gnus-newsgroup-headers
6907               (if backward
6908                   (cons header gnus-newsgroup-headers)
6909                 (nconc gnus-newsgroup-headers (list header))))
6910         (setq gnus-newsgroup-unselected
6911               (delq artnum gnus-newsgroup-unselected))
6912         (setq gnus-newsgroup-begin (min gnus-newsgroup-begin artnum))
6913         (setq gnus-newsgroup-end (max gnus-newsgroup-end artnum)))))
6914
6915 (defun gnus-summary-work-articles (n)
6916   "Return a list of articles to be worked upon. The prefix argument,
6917 the list of process marked articles, and the current article will be
6918 taken into consideration."
6919   (let (articles)
6920     (if (and n (numberp n))
6921         (let ((backward (< n 0))
6922               (n (abs n)))
6923           (save-excursion
6924             (while (and (> n 0)
6925                         (setq articles (cons (gnus-summary-article-number) 
6926                                              articles))
6927                         (gnus-summary-search-forward nil nil backward))
6928               (setq n (1- n))))
6929           (sort articles (function <)))
6930       (or (reverse gnus-newsgroup-processable)
6931           (list (gnus-summary-article-number))))))
6932
6933 (defun gnus-summary-search-group (&optional backward use-level)
6934   "Search for next unread newsgroup.
6935 If optional argument BACKWARD is non-nil, search backward instead."
6936   (save-excursion
6937     (set-buffer gnus-group-buffer)
6938     (if (gnus-group-search-forward 
6939          backward nil (if use-level (gnus-group-group-level) nil))
6940         (gnus-group-group-name))))
6941
6942 (defun gnus-summary-best-group (&optional exclude-group)
6943   "Find the name of the best unread group.
6944 If EXCLUDE-GROUP, do not go to this group."
6945   (save-excursion
6946     (set-buffer gnus-group-buffer)
6947     (save-excursion
6948       (gnus-group-best-unread-group exclude-group))))
6949
6950 (defun gnus-summary-search-subject (&optional backward unread subject)
6951   "Search for article forward.
6952 If BACKWARD is non-nil, search backward.
6953 If UNREAD is non-nil, only unread articles are selected.
6954 If SUBJECT is non-nil, the article which has the same subject will be
6955 searched for." 
6956   (let ((func (if backward 'previous-single-property-change
6957                 'next-single-property-change))
6958         (beg (point))
6959         (did t)
6960         pos psubject)
6961     (beginning-of-line)
6962     (and gnus-summary-check-current unread
6963          (eq (get-text-property (point) 'gnus-mark) gnus-unread-mark)
6964          (setq did nil))
6965     (if (not did)
6966         ()
6967       (forward-char (if backward (if (bobp) 0 -1) (if (eobp) 0 1)))
6968       (while
6969           (and 
6970            (setq pos (funcall func (point) 'gnus-number))
6971            (goto-char (if backward (1- pos) pos))
6972            (setq did
6973                  (not (and
6974                        (or (not unread)
6975                            (eq (get-text-property (point) 'gnus-mark)
6976                                gnus-unread-mark))
6977                        (or (not subject)
6978                            (and (setq psubject (gnus-summary-subject-string))
6979                                 (gnus-subject-eq subject psubject))))))
6980            (if backward (if (bobp) nil (forward-char -1) t)
6981              (if (eobp) nil (forward-char 1) t)))))
6982     (if did
6983         (progn (goto-char beg) nil)
6984       (prog1
6985           (get-text-property (point) 'gnus-number)
6986         (gnus-summary-position-cursor)))))
6987
6988 (defun gnus-subject-eq (s1 s2)
6989   (cond
6990    ((null gnus-summary-gather-subject-limit)
6991     (equal (gnus-simplify-subject-re s1)
6992            (gnus-simplify-subject-re s2)))
6993    ((eq gnus-summary-gather-subject-limit 'fuzzy)
6994     (equal (gnus-simplify-subject-fuzzy s1)
6995            (gnus-simplify-subject-fuzzy s2)))
6996    ((numberp gnus-summary-gather-subject-limit)
6997     (equal (substring s1 gnus-summary-gather-subject-limit)
6998            (substring s2 gnus-summary-gather-subject-limit)))
6999    (t
7000     (equal s1 s2))))
7001     
7002 (defun gnus-summary-search-forward (&optional unread subject backward)
7003   "Search for article forward.
7004 If UNREAD is non-nil, only unread articles are selected.
7005 If SUBJECT is non-nil, the article which has the same subject will be
7006 searched for. 
7007 If BACKWARD is non-nil, the search will be performed backwards instead."
7008   (gnus-summary-search-subject backward unread subject))
7009
7010 (defun gnus-summary-search-backward (&optional unread subject)
7011   "Search for article backward.
7012 If 1st optional argument UNREAD is non-nil, only unread article is selected.
7013 If 2nd optional argument SUBJECT is non-nil, the article which has
7014 the same subject will be searched for."
7015   (gnus-summary-search-forward unread subject t))
7016
7017 (defun gnus-summary-article-number (&optional number-or-nil)
7018   "The article number of the article on the current line.
7019 If there isn's an article number here, then we return the current
7020 article number."
7021   (let* ((number (get-text-property (gnus-point-at-bol) 'gnus-number)))
7022     (if number-or-nil number (or number gnus-current-article))))
7023
7024 (defun gnus-summary-thread-level ()
7025   "The thread level of the article on the current line."
7026   (or (get-text-property (gnus-point-at-bol) 'gnus-level)
7027       0))
7028
7029 (defun gnus-summary-pseudo-article ()
7030   "The thread level of the article on the current line."
7031   (get-text-property (gnus-point-at-bol) 'gnus-pseudo))
7032
7033 (defun gnus-summary-article-mark ()
7034   "The mark on the current line."
7035   (get-text-property (gnus-point-at-bol) 'gnus-mark))
7036
7037 (defun gnus-summary-subject-string ()
7038   "Return current subject string or nil if nothing."
7039   (let ((article (gnus-summary-article-number))
7040         header)
7041     (and article 
7042          (setq header (gnus-get-header-by-number article))
7043          (vectorp header)
7044          (header-subject header))))
7045
7046 (defalias 'gnus-summary-score 'gnus-summary-article-score)
7047 (make-obsolete 'gnus-summary-score 'gnus-summary-article-score)
7048 (defun gnus-summary-article-score ()
7049   "Return current article score."
7050   (or (cdr (assq (gnus-summary-article-number) gnus-newsgroup-scored))
7051       gnus-summary-default-score 0))
7052
7053 (defun gnus-summary-recenter ()
7054   "Center point in the summary window.
7055 If `gnus-auto-center-summary' is nil, or the article buffer isn't
7056 displayed, no centering will be performed." 
7057   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
7058   ;; Recenter only when requested. Suggested by popovich@park.cs.columbia.edu.
7059   (let* ((top (cond ((< (window-height) 4) 0)
7060                     ((< (window-height) 7) 1)
7061                     (t 2)))
7062          (height (1- (window-height)))
7063          (bottom (save-excursion (goto-char (point-max))
7064                                  (forward-line (- height))
7065                                  (point)))
7066          (window (get-buffer-window (current-buffer))))
7067     (and 
7068      ;; The user has to want it,
7069      gnus-auto-center-summary 
7070      ;; the article buffer must be displayed,
7071      (get-buffer-window gnus-article-buffer)
7072      ;; Set the window start to either `bottom', which is the biggest
7073      ;; possible valid number, or the second line from the top,
7074      ;; whichever is the least.
7075      (set-window-start
7076       window (min bottom (save-excursion (forward-line (- top)) (point)))))))
7077
7078 ;; Function written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
7079 (defun gnus-short-group-name (group &optional levels)
7080   "Collapse GROUP name LEVELS."
7081   (let* ((name "") (foreign "") (depth -1) (skip 1)
7082          (levels (or levels
7083                      (progn
7084                        (while (string-match "\\." group skip)
7085                          (setq skip (match-end 0)
7086                                depth (+ depth 1)))
7087                        depth))))
7088     (if (string-match ":" group)
7089         (setq foreign (substring group 0 (match-end 0))
7090               group (substring group (match-end 0))))
7091     (while group
7092       (if (and (string-match "\\." group) (> levels 0))
7093           (setq name (concat name (substring group 0 1))
7094                 group (substring group (match-end 0))
7095                 levels (- levels 1)
7096                 name (concat name "."))
7097         (setq name (concat foreign name group)
7098               group nil)))
7099     name))
7100
7101 (defun gnus-summary-jump-to-group (newsgroup)
7102   "Move point to NEWSGROUP in group mode buffer."
7103   ;; Keep update point of group mode buffer if visible.
7104   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
7105       (save-window-excursion
7106         ;; Take care of tree window mode.
7107         (if (get-buffer-window gnus-group-buffer)
7108             (pop-to-buffer gnus-group-buffer))
7109         (gnus-group-jump-to-group newsgroup))
7110     (save-excursion
7111       ;; Take care of tree window mode.
7112       (if (get-buffer-window gnus-group-buffer)
7113           (pop-to-buffer gnus-group-buffer)
7114         (set-buffer gnus-group-buffer))
7115       (gnus-group-jump-to-group newsgroup))))
7116
7117 ;; This function returns a list of article numbers based on the
7118 ;; difference between the ranges of read articles in this group and
7119 ;; the range of active articles.
7120 (defun gnus-list-of-unread-articles (group)
7121   (let* ((read (nth 2 (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
7122          (active (gnus-gethash group gnus-active-hashtb))
7123          (last (cdr active))
7124          unread first nlast unread)
7125     ;; If none are read, then all are unread. 
7126     (if (not read)
7127         (setq first (car active))
7128       ;; If the range of read articles is a single range, then the
7129       ;; first unread article is the article after the last read
7130       ;; article. Sounds logical, doesn't it?
7131       (if (not (listp (cdr read)))
7132           (setq first (1+ (cdr read)))
7133         ;; `read' is a list of ranges.
7134         (if (/= (setq nlast (or (and (numberp (car read)) (car read)) 
7135                                 (car (car read)))) 1)
7136             (setq first 1))
7137         (while read
7138           (if first 
7139               (while (< first nlast)
7140                 (setq unread (cons first unread))
7141                 (setq first (1+ first))))
7142           (setq first (1+ (if (atom (car read)) (car read) (cdr (car read)))))
7143           (setq nlast (if (atom (car (cdr read))) 
7144                           (car (cdr read))
7145                         (car (car (cdr read)))))
7146           (setq read (cdr read)))))
7147     ;; And add the last unread articles.
7148     (while (<= first last)
7149       (setq unread (cons first unread))
7150       (setq first (1+ first)))
7151     ;; Return the list of unread articles.
7152     (nreverse unread)))
7153
7154 (defun gnus-list-of-read-articles (group)
7155   (let ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
7156         (active (gnus-gethash group gnus-active-hashtb)))
7157     (and info active
7158          (gnus-sorted-complement 
7159           (gnus-uncompress-range active) 
7160           (gnus-list-of-unread-articles group)))))
7161
7162 ;; Various summary commands
7163
7164 (defun gnus-summary-universal-argument ()
7165   "Perform any operation on all articles marked with the process mark."
7166   (interactive)
7167   (gnus-set-global-variables)
7168   (let ((articles (reverse gnus-newsgroup-processable))
7169         key func)
7170     (or articles (error "No articles marked"))
7171     (or (setq func (key-binding (read-key-sequence "C-c C-u")))
7172         (error "Undefined key"))
7173     (while articles
7174       (gnus-summary-goto-subject (car articles))
7175       (command-execute func)
7176       (gnus-summary-remove-process-mark (car articles))
7177       (setq articles (cdr articles)))))
7178
7179 (defun gnus-summary-toggle-truncation (arg)
7180   "Toggle truncation of summary lines.
7181 With arg, turn line truncation on iff arg is positive."
7182   (interactive "P")
7183   (setq truncate-lines
7184         (if (null arg) (not truncate-lines)
7185           (> (prefix-numeric-value arg) 0)))
7186   (redraw-display))
7187
7188 (defun gnus-summary-reselect-current-group (all)
7189   "Once exit and then reselect the current newsgroup.
7190 The prefix argument ALL means to select all articles."
7191   (interactive "P")
7192   (gnus-set-global-variables)
7193   (let ((current-subject (gnus-summary-article-number))
7194         (group gnus-newsgroup-name))
7195     (setq gnus-newsgroup-begin nil)
7196     (gnus-summary-exit t)
7197     ;; We have to adjust the point of group mode buffer because the
7198     ;; current point was moved to the next unread newsgroup by
7199     ;; exiting.
7200     (gnus-summary-jump-to-group group)
7201     (gnus-group-read-group all t)
7202     (gnus-summary-goto-subject current-subject)))
7203
7204 (defun gnus-summary-rescan-group (all)
7205   "Exit the newsgroup, ask for new articles, and select the newsgroup."
7206   (interactive "P")
7207   (gnus-set-global-variables)
7208   ;; Fix by Ilja Weis <kult@uni-paderborn.de>.
7209   (let ((group gnus-newsgroup-name))
7210     (gnus-summary-exit)
7211     (gnus-summary-jump-to-group group)
7212     (save-excursion
7213       (set-buffer gnus-group-buffer)
7214       (gnus-group-get-new-news-this-group 1))
7215     (gnus-summary-jump-to-group group)
7216     (gnus-group-read-group all)))
7217
7218 (defun gnus-summary-update-info ()
7219   (let* ((group gnus-newsgroup-name)
7220          (method (car (gnus-find-method-for-group group))))
7221     (if gnus-newsgroup-kill-headers
7222         (setq gnus-newsgroup-killed
7223               (gnus-compress-sequence
7224                (nconc
7225                 (gnus-set-sorted-intersection
7226                  (gnus-uncompress-range gnus-newsgroup-killed)
7227                  (setq gnus-newsgroup-unselected
7228                        (sort gnus-newsgroup-unselected '<)))
7229                 (setq gnus-newsgroup-unreads
7230                       (sort gnus-newsgroup-unreads '<))) t)))
7231     (or (listp (cdr gnus-newsgroup-killed))
7232         (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
7233     (let ((updated nil)
7234           (headers gnus-newsgroup-headers))
7235       (gnus-close-group group)
7236       (run-hooks 'gnus-exit-group-hook)
7237       (gnus-update-read-articles 
7238        group gnus-newsgroup-unreads gnus-newsgroup-unselected 
7239        gnus-newsgroup-marked
7240        t gnus-newsgroup-replied gnus-newsgroup-expirable
7241        gnus-newsgroup-killed gnus-newsgroup-dormant
7242        gnus-newsgroup-bookmarks 
7243        (and gnus-save-score gnus-newsgroup-scored))
7244       (and gnus-use-cross-reference
7245            (gnus-mark-xrefs-as-read 
7246             group headers gnus-newsgroup-unreads gnus-newsgroup-expirable))
7247       ;; Do adaptive scoring, and possibly save score files.
7248       (and gnus-newsgroup-adaptive
7249            (gnus-score-adaptive))
7250       (and gnus-use-scoring 
7251            (fboundp 'gnus-score-save)
7252            (funcall 'gnus-score-save))
7253       ;; Do not switch windows but change the buffer to work.
7254       (set-buffer gnus-group-buffer)
7255       (or (assoc 'quit-config (gnus-find-method-for-group gnus-newsgroup-name))
7256           (gnus-group-update-group group)))))
7257   
7258 (defun gnus-summary-exit (&optional temporary)
7259   "Exit reading current newsgroup, and then return to group selection mode.
7260 gnus-exit-group-hook is called with no arguments if that value is non-nil."
7261   (interactive)
7262   (gnus-set-global-variables)
7263   (gnus-kill-save-kill-buffer)
7264   (let* ((group gnus-newsgroup-name)
7265          (quit-config (nth 1 (assoc 'quit-config (gnus-find-method-for-group
7266                                                   gnus-newsgroup-name))))
7267          (mode major-mode)
7268          (method (car (gnus-find-method-for-group group)))
7269          (buf (current-buffer)))
7270     (gnus-summary-update-info) ; Make all changes in this group permanent.
7271     ;; Make sure where I was, and go to next newsgroup.
7272     (or quit-config
7273         (progn
7274           (gnus-group-jump-to-group group)
7275           (gnus-group-next-unread-group 1)))
7276     (if temporary
7277         nil                             ;Nothing to do.
7278       (and gnus-use-cache (gnus-cache-possibly-remove-articles))
7279       ;; We set all buffer-local variables to nil. It is unclear why
7280       ;; this is needed, but if we don't, buffer-local variables are
7281       ;; not garbage-collected, it seems. This would the lead to en
7282       ;; ever-growing Emacs.
7283       (set-buffer buf)
7284       (gnus-summary-clear-local-variables)
7285       ;; We clear the global counterparts of the buffer-local
7286       ;; variables as well, just to be on the safe side.
7287       (gnus-configure-windows 'group)
7288       (gnus-summary-clear-local-variables)
7289       ;; Return to group mode buffer. 
7290       (if (eq mode 'gnus-summary-mode)
7291           (gnus-kill-buffer buf))
7292       (if (get-buffer gnus-article-buffer)
7293           (bury-buffer gnus-article-buffer))
7294       (setq gnus-current-select-method gnus-select-method)
7295       (pop-to-buffer gnus-group-buffer)
7296       (if (not quit-config)
7297           (progn
7298             (gnus-group-jump-to-group group)
7299             (gnus-group-next-unread-group 1))
7300         (if (not (buffer-name (car quit-config)))
7301             (gnus-configure-windows 'group)
7302           (set-buffer (car quit-config))
7303           (and (eq major-mode 'gnus-summary-mode)
7304                (gnus-set-global-variables))
7305           (gnus-configure-windows (cdr quit-config)))))))
7306
7307 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
7308 (defun gnus-summary-exit-no-update (&optional no-questions)
7309   "Quit reading current newsgroup without updating read article info."
7310   (interactive)
7311   (let* ((group gnus-newsgroup-name)
7312          (quit-config (nth 1 (assoc 'quit-config 
7313                                     (gnus-find-method-for-group group)))))
7314     (if (or no-questions
7315             gnus-expert-user
7316             (gnus-y-or-n-p "Do you really wanna quit reading this group? "))
7317         (progn
7318           (and gnus-use-cache (gnus-cache-possibly-remove-articles))
7319           (gnus-close-group group)
7320           (gnus-summary-clear-local-variables)
7321           (set-buffer gnus-group-buffer)
7322           (gnus-summary-clear-local-variables)
7323           ;; Return to group selection mode.
7324           (gnus-configure-windows 'group)
7325           (if (get-buffer gnus-summary-buffer)
7326               (kill-buffer gnus-summary-buffer))
7327           (if (get-buffer gnus-article-buffer)
7328               (bury-buffer gnus-article-buffer))
7329           (if (equal (gnus-group-group-name) group)
7330               (gnus-group-next-unread-group 1))
7331           (if quit-config
7332               (progn
7333                 (if (not (buffer-name (car quit-config)))
7334                     (gnus-configure-windows 'group)
7335                   (set-buffer (car quit-config))
7336                   (and (eq major-mode 'gnus-summary-mode)
7337                        (gnus-set-global-variables))
7338                   (gnus-configure-windows (cdr quit-config)))))))))
7339
7340 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
7341 (defun gnus-summary-fetch-faq (group)
7342   "Fetch the FAQ for the current group."
7343   (interactive (list gnus-newsgroup-name))
7344   (gnus-configure-windows 'summary-faq)
7345   (find-file (concat gnus-group-faq-directory group)))
7346
7347 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
7348 (defun gnus-summary-describe-group (force)
7349   "Describe the current newsgroup."
7350   (interactive "P")
7351   (gnus-group-describe-group force gnus-newsgroup-name))
7352
7353 (defun gnus-summary-describe-briefly ()
7354   "Describe summary mode commands briefly."
7355   (interactive)
7356   (gnus-message 6
7357     (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")))
7358
7359 ;; Walking around group mode buffer from summary mode.
7360
7361 (defun gnus-summary-next-group (&optional no-article group backward)
7362   "Exit current newsgroup and then select next unread newsgroup.
7363 If prefix argument NO-ARTICLE is non-nil, no article is selected initially.
7364 If BACKWARD, go to previous group instead."
7365   (interactive "P")
7366   (gnus-set-global-variables)
7367   (let ((ingroup gnus-newsgroup-name)
7368         (sumbuf (current-buffer))
7369         num)
7370     (set-buffer gnus-group-buffer)
7371     (if (and group
7372              (or (and (numberp (setq num (car (gnus-gethash
7373                                                group gnus-newsrc-hashtb))))
7374                       (< num 1))
7375                  (null num)))
7376         (progn
7377           (gnus-group-jump-to-group group)
7378           (setq group nil))
7379       (gnus-group-jump-to-group ingroup))
7380     (gnus-summary-search-group backward)
7381     (let ((group (or group (gnus-summary-search-group backward))))
7382       (set-buffer sumbuf)
7383       (gnus-summary-exit t)             ;Update all information.
7384       (if (null group)
7385           (gnus-summary-exit-no-update t)
7386         (gnus-group-jump-to-group ingroup)
7387         (setq group (gnus-summary-search-group backward))
7388         (gnus-message 5 "Selecting %s..." group)
7389         (set-buffer gnus-group-buffer)
7390         ;; We are now in group mode buffer.
7391         ;; Make sure group mode buffer point is on GROUP.
7392         (gnus-group-jump-to-group group)
7393         (if (not (eq gnus-auto-select-next 'quietly))
7394             (progn
7395               (gnus-summary-read-group group nil no-article sumbuf)
7396               (and (string= gnus-newsgroup-name ingroup)
7397                    (bufferp sumbuf) (buffer-name sumbuf)
7398                    (progn
7399                      (set-buffer (setq gnus-summary-buffer sumbuf))
7400                      (gnus-summary-exit-no-update t))))
7401           (let ((prevgroup group))
7402             (gnus-group-jump-to-group ingroup)
7403             (setq group (gnus-summary-search-group backward))
7404             (gnus-summary-read-group group nil no-article sumbuf)
7405             (while (and (string= gnus-newsgroup-name ingroup)
7406                         (bufferp sumbuf) 
7407                         (buffer-name sumbuf)
7408                         (not (string= prevgroup (gnus-group-group-name))))
7409               (set-buffer gnus-group-buffer)
7410               (gnus-summary-read-group 
7411                (setq prevgroup (gnus-group-group-name)) 
7412                nil no-article sumbuf))
7413             (and (string= prevgroup (gnus-group-group-name))
7414                  ;; We have reached the final group in the group
7415                  ;; buffer.
7416                  (progn
7417                    (if (buffer-name sumbuf)
7418                        (progn
7419                          (set-buffer sumbuf)
7420                          (gnus-summary-exit)))))))))))
7421
7422 (defun gnus-summary-prev-group (no-article)
7423   "Exit current newsgroup and then select previous unread newsgroup.
7424 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7425   (interactive "P")
7426   (gnus-summary-next-group no-article nil t))
7427
7428 ;; Walking around summary lines.
7429
7430 (defun gnus-summary-first-subject (unread)
7431   "Go to the first unread subject.
7432 If UNREAD is non-nil, go to the first unread article.
7433 Returns nil if there are no unread articles."
7434   (interactive "P")
7435   (prog1
7436       (cond ((not unread)
7437              (goto-char (point-min)))
7438             ((gnus-goto-char 
7439               (text-property-any 
7440                (point-min) (point-max) 'gnus-mark gnus-unread-mark))
7441              t)
7442             (t 
7443              ;; There are no unread articles.
7444              (gnus-message 3 "No more unread articles")
7445              nil))
7446     (gnus-summary-position-cursor)))
7447
7448 (defun gnus-summary-next-subject (n &optional unread dont-display)
7449   "Go to next N'th summary line.
7450 If N is negative, go to the previous N'th subject line.
7451 If UNREAD is non-nil, only unread articles are selected.
7452 The difference between N and the actual number of steps taken is
7453 returned."
7454   (interactive "p")
7455   (let ((backward (< n 0))
7456         (n (abs n)))
7457     (while (and (> n 0)
7458                 (gnus-summary-search-forward unread nil backward))
7459       (setq n (1- n)))
7460     (if (/= 0 n) (gnus-message 7 "No more%s articles"
7461                                (if unread " unread" "")))
7462     (or dont-display
7463         (progn
7464           (gnus-summary-recenter)
7465           (gnus-summary-position-cursor)))
7466   n))
7467
7468 (defun gnus-summary-next-unread-subject (n)
7469   "Go to next N'th unread summary line."
7470   (interactive "p")
7471   (gnus-summary-next-subject n t))
7472
7473 (defun gnus-summary-prev-subject (n &optional unread)
7474   "Go to previous N'th summary line.
7475 If optional argument UNREAD is non-nil, only unread article is selected."
7476   (interactive "p")
7477   (gnus-summary-next-subject (- n) unread))
7478
7479 (defun gnus-summary-prev-unread-subject (n)
7480   "Go to previous N'th unread summary line."
7481   (interactive "p")
7482   (gnus-summary-next-subject (- n) t))
7483
7484 (defun gnus-summary-goto-subject (article)
7485   "Go the subject line of ARTICLE."
7486   (interactive
7487    (list
7488     (string-to-int
7489      (completing-read "Article number: "
7490                       (mapcar
7491                        (lambda (headers)
7492                          (list
7493                           (int-to-string (header-number headers))))
7494                        gnus-newsgroup-headers)
7495                       nil 'require-match))))
7496   (or article (error "No article number"))
7497   (let ((b (point)))
7498     (if (not (gnus-goto-char (text-property-any (point-min) (point-max)
7499                                                 'gnus-number article)))
7500         ()
7501       (gnus-summary-show-thread)
7502       ;; Skip dummy articles. 
7503       (if (eq (gnus-summary-article-mark) gnus-dummy-mark)
7504           (forward-line 1))
7505       (prog1
7506           (if (not (eobp))
7507               article
7508             (goto-char b)
7509             nil)
7510         (gnus-summary-position-cursor)))))
7511
7512 ;; Walking around summary lines with displaying articles.
7513
7514 (defun gnus-summary-expand-window ()
7515   "Make the summary buffer take up the entire Emacs frame."
7516   (interactive)
7517   (gnus-set-global-variables)
7518   (gnus-configure-windows 'summary))
7519
7520 (defun gnus-summary-display-article (article &optional all-header)
7521   "Display ARTICLE in article buffer."
7522   (gnus-set-global-variables)
7523   (if (null article)
7524       nil
7525     (prog1
7526         (gnus-article-prepare article all-header)
7527       (gnus-summary-show-thread)
7528       (if (eq (gnus-summary-article-mark) gnus-dummy-mark)
7529           (progn
7530             (forward-line 1)
7531             (gnus-summary-position-cursor)))
7532       (run-hooks 'gnus-select-article-hook)
7533       (gnus-summary-recenter)
7534       (gnus-summary-goto-subject article)
7535       ;; Successfully display article.
7536       (gnus-summary-update-line)
7537       (gnus-article-set-window-start 
7538        (cdr (assq article gnus-newsgroup-bookmarks)))
7539       t)))
7540
7541 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7542   "Select the current article.
7543 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7544 non-nil, the article will be re-fetched even if it already present in
7545 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7546 be displayed."
7547   (and (not pseudo) (gnus-summary-pseudo-article)
7548        (error "This is a pseudo-article."))
7549   (let ((article (or article (gnus-summary-article-number)))
7550         (all-headers (not (not all-headers))) ;Must be T or NIL.
7551         did) 
7552     (prog1
7553         (save-excursion
7554           (set-buffer gnus-summary-buffer)
7555           (if (or (null gnus-current-article)
7556                   (null gnus-article-current)
7557                   (not (eq article (cdr gnus-article-current)))
7558                   (not (equal (car gnus-article-current) gnus-newsgroup-name))
7559                   force)
7560               ;; The requested article is different from the current article.
7561               (progn
7562                 (gnus-summary-display-article article all-headers)
7563                 (setq did article))
7564             (if all-headers (gnus-article-show-all-headers))
7565             nil))
7566       (if did 
7567           (gnus-article-set-window-start 
7568            (cdr (assq article gnus-newsgroup-bookmarks)))))))
7569
7570 (defun gnus-summary-set-current-mark (&optional current-mark)
7571   "Obsolete function."
7572   nil)
7573
7574 (defun gnus-summary-next-article (unread &optional subject backward)
7575   "Select the next article.
7576 If UNREAD, only unread articles are selected.
7577 If SUBJECT, only articles with SUBJECT are selected.
7578 If BACKWARD, the previous article is selected instead of the next."
7579   (interactive "P")
7580   (gnus-set-global-variables)
7581   (let ((opoint (point))
7582         (method (car (gnus-find-method-for-group gnus-newsgroup-name)))
7583         header)
7584     (cond
7585      ;; Is there such an article?
7586      ((gnus-summary-display-article 
7587        (gnus-summary-search-forward unread subject backward))
7588       (gnus-summary-position-cursor))
7589      ;; If not, we try the first unread, if that is wanted.
7590      ((and subject
7591            gnus-auto-select-same
7592            (gnus-summary-first-unread-article))
7593       (gnus-message 6 "Wrapped"))
7594      ;; Try to get next/previous article not displayed in this group.
7595      ((and gnus-auto-extend-newsgroup
7596            (not unread) (not subject)
7597            (setq header (gnus-more-header-forward backward)))
7598       (gnus-extend-newsgroup header backward)
7599       (let ((buffer-read-only nil))
7600         (goto-char (if backward (point-min) (point-max)))
7601         (gnus-summary-prepare-threads (list header) 0))
7602       (gnus-summary-goto-article (if backward gnus-newsgroup-begin
7603                                    gnus-newsgroup-end)))
7604      ;; Go to next/previous group.
7605      (t
7606       (or (assoc 'quit-config (gnus-find-method-for-group gnus-newsgroup-name))
7607           (gnus-summary-jump-to-group gnus-newsgroup-name))
7608       (let ((cmd (aref (this-command-keys) 0))
7609             (group 
7610              (if (eq gnus-keep-same-level 'best) 
7611                  (gnus-summary-best-group gnus-newsgroup-name)
7612                (gnus-summary-search-group backward gnus-keep-same-level))))
7613         ;; For some reason, the group window gets selected. We change
7614         ;; it back.  
7615         (select-window (get-buffer-window (current-buffer)))
7616         ;; Keep just the event type of CMD.
7617         (and (listp cmd) (setq cmd (car cmd)))
7618         ;; Select next unread newsgroup automagically.
7619         (cond 
7620          ((not gnus-auto-select-next)
7621           (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7622          ((eq gnus-auto-select-next 'quietly)
7623           ;; Select quietly.
7624           (if (assoc 'quit-config (gnus-find-method-for-group 
7625                                    gnus-newsgroup-name))
7626               (gnus-summary-exit)
7627             (gnus-message 7 "No more%s articles (%s)..."
7628                           (if unread " unread" "") 
7629                           (if group (concat "selecting " group)
7630                             "exiting"))
7631             (gnus-summary-next-group nil group backward)))
7632          (t
7633           (let ((keystrokes '(?\C-n ?\C-p))
7634                 key)
7635             (while (or (null key) (memq key keystrokes))
7636               (gnus-message 
7637                7 "No more%s articles%s" (if unread " unread" "")
7638                (if (and group (not (assoc 'quit-config
7639                                           (gnus-find-method-for-group 
7640                                            gnus-newsgroup-name))))
7641                    (format " (Type %s for %s [%s])"
7642                            (single-key-description cmd) group
7643                            (car (gnus-gethash group gnus-newsrc-hashtb)))
7644                  (format " (Type %s to exit %s)"
7645                          (single-key-description cmd)
7646                          gnus-newsgroup-name)))
7647               ;; Confirm auto selection.
7648               (let* ((event (read-event)))
7649                 (setq key (if (listp event) (car event) event))
7650                 (if (memq key keystrokes)
7651                     (let ((obuf (current-buffer)))
7652                       (switch-to-buffer gnus-group-buffer)
7653                       (gnus-group-jump-to-group group)
7654                       (execute-kbd-macro (char-to-string key))
7655                       (setq group (gnus-group-group-name))
7656                       (switch-to-buffer obuf)))))
7657             (if (equal key cmd)
7658                 (if (or (not group) (assoc 'quit-config
7659                                            (gnus-find-method-for-group
7660                                             gnus-newsgroup-name)))
7661                     (gnus-summary-exit)
7662                   (gnus-summary-next-group nil group backward))
7663               (setq unread-command-events (list key)))))))))))
7664
7665 (defun gnus-summary-next-unread-article ()
7666   "Select unread article after current one."
7667   (interactive)
7668   (gnus-summary-next-article t (and gnus-auto-select-same
7669                                     (gnus-summary-subject-string))))
7670
7671 (defun gnus-summary-prev-article (unread &optional subject)
7672   "Select the article after the current one.
7673 If UNREAD is non-nil, only unread articles are selected."
7674   (interactive "P")
7675   (gnus-summary-next-article unread subject t))
7676
7677 (defun gnus-summary-prev-unread-article ()
7678   "Select unred article before current one."
7679   (interactive)
7680   (gnus-summary-prev-article t (and gnus-auto-select-same
7681                                     (gnus-summary-subject-string))))
7682
7683 (defun gnus-summary-next-page (lines &optional circular)
7684   "Show next page of selected article.
7685 If end of article, select next article.
7686 Argument LINES specifies lines to be scrolled up.
7687 If CIRCULAR is non-nil, go to the start of the article instead of 
7688 instead of selecting the next article when reaching the end of the
7689 current article." 
7690   (interactive "P")
7691   (setq gnus-summary-buffer (current-buffer))
7692   (gnus-set-global-variables)
7693   (let ((article (gnus-summary-article-number))
7694         (endp nil))
7695     (gnus-configure-windows 'article)
7696     (if (or (null gnus-current-article)
7697             (null gnus-article-current)
7698             (/= article (cdr gnus-article-current))
7699             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7700         ;; Selected subject is different from current article's.
7701         (gnus-summary-display-article article)
7702       (gnus-eval-in-buffer-window
7703        gnus-article-buffer
7704        (setq endp (gnus-article-next-page lines)))
7705       (if endp
7706           (cond (circular
7707                  (gnus-summary-beginning-of-article))
7708                 (lines
7709                  (gnus-message 3 "End of message"))
7710                 ((null lines)
7711                  (gnus-summary-next-unread-article)))))
7712     (gnus-summary-recenter)
7713     (gnus-summary-position-cursor)))
7714
7715 (defun gnus-summary-prev-page (lines)
7716   "Show previous page of selected article.
7717 Argument LINES specifies lines to be scrolled down."
7718   (interactive "P")
7719   (gnus-set-global-variables)
7720   (let ((article (gnus-summary-article-number)))
7721     (gnus-configure-windows 'article)
7722     (if (or (null gnus-current-article)
7723             (null gnus-article-current)
7724             (/= article (cdr gnus-article-current))
7725             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7726         ;; Selected subject is different from current article's.
7727         (gnus-summary-display-article article)
7728       (gnus-summary-recenter)
7729       (gnus-eval-in-buffer-window gnus-article-buffer
7730         (gnus-article-prev-page lines))))
7731   (gnus-summary-position-cursor))
7732
7733 (defun gnus-summary-scroll-up (lines)
7734   "Scroll up (or down) one line current article.
7735 Argument LINES specifies lines to be scrolled up (or down if negative)."
7736   (interactive "p")
7737   (gnus-set-global-variables)
7738   (gnus-configure-windows 'article)
7739   (or (gnus-summary-select-article nil nil 'pseudo)
7740       (gnus-eval-in-buffer-window 
7741        gnus-article-buffer
7742        (cond ((> lines 0)
7743               (if (gnus-article-next-page lines)
7744                   (gnus-message 3 "End of message")))
7745              ((< lines 0)
7746               (gnus-article-prev-page (- lines))))))
7747   (gnus-summary-recenter)
7748   (gnus-summary-position-cursor))
7749
7750 (defun gnus-summary-next-same-subject ()
7751   "Select next article which has the same subject as current one."
7752   (interactive)
7753   (gnus-set-global-variables)
7754   (gnus-summary-next-article nil (gnus-summary-subject-string)))
7755
7756 (defun gnus-summary-prev-same-subject ()
7757   "Select previous article which has the same subject as current one."
7758   (interactive)
7759   (gnus-set-global-variables)
7760   (gnus-summary-prev-article nil (gnus-summary-subject-string)))
7761
7762 (defun gnus-summary-next-unread-same-subject ()
7763   "Select next unread article which has the same subject as current one."
7764   (interactive)
7765   (gnus-set-global-variables)
7766   (gnus-summary-next-article t (gnus-summary-subject-string)))
7767
7768 (defun gnus-summary-prev-unread-same-subject ()
7769   "Select previous unread article which has the same subject as current one."
7770   (interactive)
7771   (gnus-set-global-variables)
7772   (gnus-summary-prev-article t (gnus-summary-subject-string)))
7773
7774 (defun gnus-summary-first-unread-article ()
7775   "Select the first unread article. 
7776 Return nil if there are no unread articles."
7777   (interactive)
7778   (gnus-set-global-variables)
7779   (prog1
7780       (if (gnus-summary-first-subject t)
7781           (gnus-summary-display-article (gnus-summary-article-number)))
7782     (gnus-summary-position-cursor)))
7783
7784 (defun gnus-summary-best-unread-article ()
7785   "Select the unread article with the highest score."
7786   (interactive)
7787   (gnus-set-global-variables)
7788   (let ((scored gnus-newsgroup-scored)
7789         (best -1000000)
7790         article art)
7791     (while scored
7792       (or (> best (cdr (car scored)))
7793           (and (memq (setq art (car (car scored))) gnus-newsgroup-unreads)
7794                (not (memq art gnus-newsgroup-marked))
7795                (not (memq art gnus-newsgroup-dormant))
7796                (if (= best (cdr (car scored)))
7797                    (setq article (min art article))
7798                  (setq article art)
7799                  (setq best (cdr (car scored))))))
7800       (setq scored (cdr scored)))
7801     (if article 
7802         (gnus-summary-goto-article article)
7803       (gnus-summary-first-unread-article))
7804     (gnus-summary-position-cursor)))
7805
7806 (defun gnus-summary-goto-article (article &optional all-headers)
7807   "Fetch ARTICLE and display it if it exists.
7808 If ALL-HEADERS is non-nil, no header lines are hidden."
7809   (interactive
7810    (list
7811     (string-to-int
7812      (completing-read 
7813       "Article number: "
7814       (mapcar (lambda (headers) (list (int-to-string (header-number headers))))
7815               gnus-newsgroup-headers) 
7816       nil 'require-match))))
7817   (prog1
7818       (and (gnus-summary-goto-subject article)
7819            (gnus-summary-display-article article all-headers))
7820     (gnus-summary-position-cursor)))
7821
7822 (defun gnus-summary-goto-last-article ()
7823   "Go to the previously read article."
7824   (interactive)
7825   (prog1
7826       (and gnus-last-article
7827            (gnus-summary-goto-article gnus-last-article))
7828     (gnus-summary-position-cursor)))
7829
7830 (defun gnus-summary-pop-article (number)
7831   "Pop one article off the history and go to the previous.
7832 NUMBER articles will be popped off."
7833   (interactive "p")
7834   (let (to)
7835     (setq gnus-newsgroup-history
7836           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7837     (if to
7838         (gnus-summary-goto-article (car to))
7839       (error "Article history empty")))
7840   (gnus-summary-position-cursor))
7841
7842 ;; Summary article oriented commands
7843
7844 (defun gnus-summary-refer-parent-article (n)
7845   "Refer parent article N times.
7846 The difference between N and the number of articles fetched is returned."
7847   (interactive "p")
7848   (gnus-set-global-variables)
7849   (while 
7850       (and 
7851        (> n 0)
7852        (let ((ref (header-references (gnus-get-header-by-number
7853                                       (gnus-summary-article-number)))))
7854          (if (and ref (not (equal ref ""))
7855                   (string-match "<[^<>]*>[ \t]*$" ref))
7856              (gnus-summary-refer-article 
7857               (substring ref (match-beginning 0) (match-end 0)))
7858            (gnus-message 1 "No references in article %d"
7859                          (gnus-summary-article-number))
7860            nil)))
7861     (setq n (1- n)))
7862   (gnus-summary-position-cursor)
7863   n)
7864     
7865 (defun gnus-summary-refer-article (message-id)
7866   "Refer article specified by MESSAGE-ID.
7867 NOTE: This command only works with newsgroups that use real or simulated NNTP."
7868   (interactive "sMessage-ID: ")
7869   (if (or (not (stringp message-id))
7870           (zerop (length message-id)))
7871       ()
7872     ;; Construct the correct Message-ID if necessary.
7873     ;; Suggested by tale@pawl.rpi.edu.
7874     (or (string-match "^<" message-id)
7875         (setq message-id (concat "<" message-id)))
7876     (or (string-match ">$" message-id)
7877         (setq message-id (concat message-id ">")))
7878     (let ((header (car (gnus-gethash (downcase message-id)
7879                                      gnus-newsgroup-dependencies))))
7880       (if header
7881           (or (gnus-summary-goto-article (header-number header))
7882               ;; The header has been read, but the article had been
7883               ;; expunged, so we insert it again.
7884               (progn
7885                 (gnus-summary-insert-line
7886                  nil header 0 nil gnus-read-mark nil nil
7887                  (header-subject header))
7888                 (forward-line -1)
7889                 (header-number header)))
7890         (let ((gnus-override-method gnus-refer-article-method)
7891               (gnus-ancient-mark gnus-read-mark)
7892               (tmp-buf (get-buffer-create " *gnus refer"))
7893               (tmp-point (window-start
7894                           (get-buffer-window gnus-article-buffer)))
7895               number)
7896           (and gnus-refer-article-method
7897                (or (gnus-server-opened gnus-refer-article-method)
7898                    (gnus-open-server gnus-refer-article-method)))
7899           ;; Save the old article buffer.
7900           (save-excursion
7901             (set-buffer tmp-buf)
7902             (buffer-disable-undo (current-buffer))
7903             (insert-buffer-substring gnus-article-buffer))
7904           (prog1
7905               (if (gnus-article-prepare 
7906                    message-id nil (gnus-read-header message-id))
7907                   (progn
7908                     (setq number (header-number gnus-current-headers))
7909                     (gnus-rebuild-thread message-id)
7910                     (gnus-summary-goto-subject number)
7911                     (gnus-summary-recenter)
7912                     (gnus-article-set-window-start 
7913                      (cdr (assq number gnus-newsgroup-bookmarks)))
7914                     message-id)
7915                 ;; We restore the old article buffer.
7916                 (save-excursion
7917                   (set-buffer gnus-article-buffer)
7918                   (let ((buffer-read-only nil))
7919                     (insert-buffer-substring tmp-buf)
7920                     (and tmp-point
7921                          (set-window-start (get-buffer-window (current-buffer))
7922                                            tmp-point))))
7923                 nil)
7924             (kill-buffer tmp-buf)))))))
7925
7926 (defun gnus-summary-enter-digest-group ()
7927   "Enter a digest group based on the current article."
7928   (interactive)
7929   (gnus-set-global-variables)
7930   (gnus-summary-select-article)
7931   ;; We do not want a narrowed article.
7932   (gnus-summary-stop-page-breaking)
7933   (let ((name (format "%s-%d" 
7934                       (gnus-group-prefixed-name 
7935                        gnus-newsgroup-name (list 'nndoc "")) 
7936                       gnus-current-article))
7937         (ogroup gnus-newsgroup-name)
7938         (buf (current-buffer)))
7939     (if (gnus-group-read-ephemeral-group 
7940          name (list 'nndoc name
7941                     (list 'nndoc-address (get-buffer gnus-article-buffer))
7942                     '(nndoc-article-type digest))
7943          t)
7944         (setcdr (nthcdr 4 (nth 2 (gnus-gethash name gnus-newsrc-hashtb)))
7945                 (list (list (cons 'to-group ogroup))))
7946       (switch-to-buffer buf)
7947       (gnus-set-global-variables)
7948       (gnus-configure-windows 'summary)
7949       (gnus-message 3 "Article not a digest?"))))
7950
7951 (defun gnus-summary-isearch-article ()
7952   "Do incremental search forward on current article."
7953   (interactive)
7954   (gnus-set-global-variables)
7955   (gnus-summary-select-article)
7956   (gnus-eval-in-buffer-window 
7957    gnus-article-buffer (isearch-forward)))
7958
7959 (defun gnus-summary-search-article-forward (regexp &optional backward)
7960   "Search for an article containing REGEXP forward.
7961 If BACKWARD, search backward instead."
7962   (interactive
7963    (list (read-string
7964           (format "Search article %s (regexp%s): "
7965                   (if current-prefix-arg "backward" "forward")
7966                   (if gnus-last-search-regexp
7967                       (concat ", default " gnus-last-search-regexp)
7968                     "")))
7969          current-prefix-arg))
7970   (gnus-set-global-variables)
7971   (if (string-equal regexp "")
7972       (setq regexp (or gnus-last-search-regexp ""))
7973     (setq gnus-last-search-regexp regexp))
7974   (if (gnus-summary-search-article regexp backward)
7975       (gnus-article-set-window-start 
7976        (cdr (assq (gnus-summary-article-number) gnus-newsgroup-bookmarks)))
7977     (error "Search failed: \"%s\"" regexp)))
7978
7979 (defun gnus-summary-search-article-backward (regexp)
7980   "Search for an article containing REGEXP backward."
7981   (interactive
7982    (list (read-string
7983           (format "Search article backward (regexp%s): "
7984                   (if gnus-last-search-regexp
7985                       (concat ", default " gnus-last-search-regexp)
7986                     "")))))
7987   (gnus-summary-search-article-forward regexp 'backward))
7988
7989 (defun gnus-summary-search-article (regexp &optional backward)
7990   "Search for an article containing REGEXP.
7991 Optional argument BACKWARD means do search for backward.
7992 gnus-select-article-hook is not called during the search."
7993   (let ((gnus-select-article-hook nil)  ;Disable hook.
7994         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7995         (re-search
7996          (if backward
7997              (function re-search-backward) (function re-search-forward)))
7998         (found nil)
7999         (last nil))
8000     ;; Hidden thread subtrees must be searched for ,too.
8001     (gnus-summary-show-all-threads)
8002     (if (eobp) (forward-line -1))
8003     ;; First of all, search current article.
8004     ;; We don't want to read article again from NNTP server nor reset
8005     ;; current point.
8006     (gnus-summary-select-article)
8007     (gnus-message 9 "Searching article: %d..." gnus-current-article)
8008     (setq last gnus-current-article)
8009     (gnus-eval-in-buffer-window gnus-article-buffer
8010       (save-restriction
8011         (widen)
8012         ;; Begin search from current point.
8013         (setq found (funcall re-search regexp nil t))))
8014     ;; Then search next articles.
8015     (while (and (not found)
8016                 (gnus-summary-display-article 
8017                  (gnus-summary-search-subject backward nil nil)))
8018       (gnus-message 9 "Searching article: %d..." gnus-current-article)
8019       (gnus-eval-in-buffer-window gnus-article-buffer
8020         (save-restriction
8021           (widen)
8022           (goto-char (if backward (point-max) (point-min)))
8023           (setq found (funcall re-search regexp nil t)))))
8024     (message "")
8025     ;; Adjust article pointer.
8026     (or (eq last gnus-current-article)
8027         (setq gnus-last-article last))
8028     ;; Return T if found such article.
8029     found))
8030
8031 (defun gnus-summary-execute-command (header regexp command &optional backward)
8032   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8033 If HEADER is an empty string (or nil), the match is done on the entire
8034 article. If BACKWARD (the prefix) is non-nil, search backward instead."
8035   (interactive
8036    (list (let ((completion-ignore-case t))
8037            (completing-read 
8038             "Header name: "
8039             (mapcar (lambda (string) (list string))
8040                     '("Number" "Subject" "From" "Lines" "Date"
8041                       "Message-ID" "Xref" "References"))
8042             nil 'require-match))
8043          (read-string "Regexp: ")
8044          (read-key-sequence "Command: ")
8045          current-prefix-arg))
8046   (gnus-set-global-variables)
8047   ;; Hidden thread subtrees must be searched as well.
8048   (gnus-summary-show-all-threads)
8049   ;; We don't want to change current point nor window configuration.
8050   (save-excursion
8051     (save-window-excursion
8052       (gnus-message 6 "Executing %s..." (key-description command))
8053       ;; We'd like to execute COMMAND interactively so as to give arguments.
8054       (gnus-execute header regexp
8055                     (` (lambda ()
8056                          (call-interactively '(, (key-binding command)))))
8057                     backward)
8058       (gnus-message 6 "Executing %s...done" (key-description command)))))
8059
8060 (defun gnus-summary-beginning-of-article ()
8061   "Scroll the article back to the beginning."
8062   (interactive)
8063   (gnus-set-global-variables)
8064   (gnus-summary-select-article)
8065   (gnus-configure-windows 'article)
8066   (gnus-eval-in-buffer-window
8067    gnus-article-buffer
8068    (widen)
8069    (goto-char (point-min))
8070    (and gnus-break-pages (gnus-narrow-to-page))))
8071
8072 (defun gnus-summary-end-of-article ()
8073   "Scroll to the end of the article."
8074   (interactive)
8075   (gnus-set-global-variables)
8076   (gnus-summary-select-article)
8077   (gnus-configure-windows 'article)
8078   (gnus-eval-in-buffer-window 
8079    gnus-article-buffer
8080    (widen)
8081    (goto-char (point-max))
8082    (and gnus-break-pages (gnus-narrow-to-page))))
8083
8084 (defun gnus-summary-show-article ()
8085   "Force re-fetching of the current article."
8086   (interactive)
8087   (gnus-set-global-variables)
8088   (gnus-summary-select-article gnus-have-all-headers t))
8089
8090 (defun gnus-summary-verbose-header (arg)
8091   "Toggle permanent full header display.
8092 If ARG is a positive number, turn header display on.
8093 If ARG is a negative number, turn header display off."
8094   (interactive "P")
8095   (gnus-set-global-variables)
8096   (gnus-summary-toggle-header arg)
8097   (setq gnus-have-all-headers
8098         (cond ((or (not (numberp arg))
8099                    (zerop arg))
8100                (not gnus-have-all-headers))
8101               ((natnump arg)
8102                t))))
8103
8104 (defun gnus-summary-toggle-header (arg)
8105   "Show the headers if they are hidden, or hide them if they are shown.
8106 If ARG is a positive number, show the entire header.
8107 If ARG is a negative number, hide the unwanted header lines."
8108   (interactive "P")
8109   (gnus-set-global-variables)
8110   (save-excursion
8111     (set-buffer gnus-article-buffer)
8112     (let ((buffer-read-only nil))
8113       (if (numberp arg) 
8114           (if (> arg 0) (remove-text-properties (point-min) (point-max) 
8115                                                 gnus-hidden-properties)
8116             (if (< arg 0) (run-hooks 'gnus-article-display-hook)))
8117         (if (text-property-any (point-min) (point-max) 'invisible t)
8118             (remove-text-properties (point-min) (point-max)
8119                                     gnus-hidden-properties)
8120           (let ((gnus-have-all-headers nil))
8121             (run-hooks 'gnus-article-display-hook))))
8122       (set-window-point (get-buffer-window (current-buffer)) (point-min)))))
8123
8124 (defun gnus-summary-show-all-headers ()
8125   "Make all header lines visible."
8126   (interactive)
8127   (gnus-set-global-variables)
8128   (gnus-article-show-all-headers))
8129
8130 (defun gnus-summary-toggle-mime (arg)
8131   "Toggle MIME processing.
8132 If ARG is a positive number, turn MIME processing on."
8133   (interactive "P")
8134   (gnus-set-global-variables)
8135   (setq gnus-show-mime
8136         (if (null arg) (not gnus-show-mime)
8137           (> (prefix-numeric-value arg) 0)))
8138   (gnus-summary-select-article t 'force))
8139
8140 (defun gnus-summary-caesar-message (rotnum)
8141   "Caesar rotates all letters of current message by 13/47 places.
8142 With prefix arg, specifies the number of places to rotate each letter forward.
8143 Caesar rotates Japanese letters by 47 places in any case."
8144   (interactive "P")
8145   (gnus-set-global-variables)
8146   (gnus-summary-select-article)
8147   (let ((mail-header-separator "")) ; !!! Is this necessary?
8148     (gnus-overload-functions)
8149     (gnus-eval-in-buffer-window 
8150      gnus-article-buffer
8151      (save-restriction
8152        (widen)
8153        ;; We don't want to jump to the beginning of the message.
8154        ;; `save-excursion' does not do its job.
8155        (move-to-window-line 0)
8156        (let ((last (point)))
8157          (news-caesar-buffer-body rotnum)
8158          (goto-char last)
8159          (recenter 0))))))
8160
8161 (defun gnus-summary-stop-page-breaking ()
8162   "Stop page breaking in the current article."
8163   (interactive)
8164   (gnus-set-global-variables)
8165   (gnus-summary-select-article)
8166   (gnus-eval-in-buffer-window gnus-article-buffer (widen)))
8167
8168 ;; Suggested by Brian Edmonds <bedmonds@prodigy.bc.ca>.
8169
8170 (defun gnus-summary-move-article (n &optional to-newsgroup select-method)
8171   "Move the current article to a different newsgroup.
8172 If N is a positive number, move the N next articles.
8173 If N is a negative number, move the N previous articles.
8174 If N is nil and any articles have been marked with the process mark,
8175 move those articles instead.
8176 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to. 
8177 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
8178 re-spool using this method.
8179 For this function to work, both the current newsgroup and the
8180 newsgroup that you want to move to have to support the `request-move'
8181 and `request-accept' functions. (Ie. mail newsgroups at present.)"
8182   (interactive "P")
8183   (gnus-set-global-variables)
8184   (or (gnus-check-backend-function 'request-move-article gnus-newsgroup-name)
8185       (error "The current newsgroup does not support article moving"))
8186   (let ((articles (gnus-summary-work-articles n))
8187         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
8188         art-group)
8189     (if (and (not to-newsgroup) (not select-method))
8190         (setq to-newsgroup
8191               (completing-read 
8192                (format "Where do you want to move %s? %s"
8193                        (if (> (length articles) 1)
8194                            (format "these %d articles" (length articles))
8195                          "this article")
8196                        (if gnus-current-move-group
8197                            (format "(%s default) " gnus-current-move-group)
8198                          ""))
8199                gnus-active-hashtb nil nil prefix)))
8200     (if to-newsgroup
8201         (progn
8202           (if (or (string= to-newsgroup "") (string= to-newsgroup prefix))
8203               (setq to-newsgroup (or gnus-current-move-group "")))
8204           (or (gnus-gethash to-newsgroup gnus-active-hashtb)
8205               (gnus-activate-newsgroup to-newsgroup)
8206               (error "No such group: %s" to-newsgroup))
8207           (setq gnus-current-move-group to-newsgroup)))
8208     (or (gnus-check-backend-function 'request-accept-article 
8209                                      (or select-method to-newsgroup))
8210         (error "%s does not support article moving" to-newsgroup))
8211     (gnus-message 6 "Moving to %s: %s..." 
8212                   (or select-method to-newsgroup) articles)
8213     (while articles
8214       (if (setq art-group
8215                 (gnus-request-move-article 
8216                  (car articles)                   ; Article to move
8217                  gnus-newsgroup-name              ; From newsgrouo
8218                  (nth 1 (gnus-find-method-for-group 
8219                          gnus-newsgroup-name))    ; Server
8220                  (list 'gnus-request-accept-article 
8221                        (if select-method
8222                            (list 'quote select-method)
8223                          to-newsgroup)
8224                        (not (cdr articles)))     ; Accept form
8225                  (not (cdr articles))))          ; Only save nov last time
8226           (let* ((buffer-read-only nil)
8227                  (entry 
8228                   (or
8229                    (gnus-gethash (car art-group) gnus-newsrc-hashtb)
8230                    (gnus-gethash 
8231                     (gnus-group-prefixed-name 
8232                      (car art-group) 
8233                      (if select-method (list select-method "")
8234                        (gnus-find-method-for-group to-newsgroup)))
8235                     gnus-newsrc-hashtb)))
8236                  (info (nth 2 entry))
8237                  (article (car articles))
8238                  (marked (nth 3 info)))
8239             (gnus-summary-goto-subject article)
8240             (beginning-of-line)
8241             (delete-region (point)
8242                            (progn (forward-line 1) (point)))
8243             (if (not (memq article gnus-newsgroup-unreads))
8244                 (setcar (cdr (cdr info))
8245                         (gnus-add-to-range (nth 2 info) 
8246                                            (list (cdr art-group)))))
8247             ;; Copy any marks over to the new group.
8248             (let ((marks '((tick . gnus-newsgroup-marked)
8249                            (dormant . gnus-newsgroup-dormant)
8250                            (expire . gnus-newsgroup-expirable)
8251                            (bookmark . gnus-newsgroup-bookmarks)
8252                         ;   (score . gnus-newsgroup-scored)
8253                            (reply . gnus-newsgroup-replied)))
8254                   (to-article (cdr art-group)))
8255               (while marks
8256                 (if (memq article (symbol-value (cdr (car marks))))
8257                     (gnus-add-marked-articles 
8258                      (car info) (car (car marks)) (list to-article) info))
8259                 (setq marks (cdr marks))))
8260             (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8261             (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8262             (setq gnus-newsgroup-dormant
8263                   (delq article gnus-newsgroup-dormant)))
8264         (gnus-message 1 "Couldn't move article %s" (car articles)))
8265       (gnus-summary-remove-process-mark (car articles))
8266       (setq articles (cdr articles)))))
8267
8268 (defun gnus-summary-respool-article (n &optional respool-method)
8269   "Respool the current article.
8270 The article will be squeezed through the mail spooling process again,
8271 which means that it will be put in some mail newsgroup or other
8272 depending on `nnmail-split-methods'.
8273 If N is a positive number, respool the N next articles.
8274 If N is a negative number, respool the N previous articles.
8275 If N is nil and any articles have been marked with the process mark,
8276 respool those articles instead.
8277
8278 Respooling can be done both from mail groups and \"real\" newsgroups.
8279 In the former case, the articles in question will be moved from the
8280 current group into whatever groups they are destined to.  In the
8281 latter case, they will be copied into the relevant groups."
8282   (interactive "P")
8283   (gnus-set-global-variables)
8284   (let ((respool-methods (gnus-methods-using 'respool))
8285         (methname 
8286          (symbol-name (car (gnus-find-method-for-group gnus-newsgroup-name)))))
8287     (or respool-method
8288         (setq respool-method
8289               (completing-read
8290                "What method do you want to use when respooling? "
8291                respool-methods nil t methname)))
8292     (or (string= respool-method "")
8293         (if (assoc (symbol-name
8294                     (car (gnus-find-method-for-group gnus-newsgroup-name)))
8295                    respool-methods)
8296             (gnus-summary-move-article n nil (intern respool-method))
8297           (gnus-summary-copy-article n nil (intern respool-method))))))
8298
8299 ;; Suggested by gregj@unidata.com (Gregory J. Grubbs).
8300 (defun gnus-summary-copy-article (n &optional to-newsgroup select-method)
8301   "Move the current article to a different newsgroup.
8302 If N is a positive number, move the N next articles.
8303 If N is a negative number, move the N previous articles.
8304 If N is nil and any articles have been marked with the process mark,
8305 move those articles instead.
8306 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to. 
8307 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
8308 re-spool using this method.
8309 For this function to work, the newsgroup that you want to move to have
8310 to support the `request-move' and `request-accept'
8311 functions. (Ie. mail newsgroups at present.)"
8312   (interactive "P")
8313   (gnus-set-global-variables)
8314   (let ((articles (gnus-summary-work-articles n))
8315         (copy-buf (get-buffer-create "*copy work*"))
8316         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
8317         art-group)
8318     (buffer-disable-undo copy-buf)
8319     (if (and (not to-newsgroup) (not select-method))
8320         (setq to-newsgroup
8321               (completing-read 
8322                (format "Where do you want to copy %s? %s"
8323                        (if (> (length articles) 1)
8324                            (format "these %d articles" (length articles))
8325                          "this article")
8326                        (if gnus-current-move-group
8327                            (format "(%s default) " gnus-current-move-group)
8328                          ""))
8329                gnus-active-hashtb nil nil prefix)))
8330     (if to-newsgroup
8331         (progn
8332           (if (or (string= to-newsgroup "") (string= to-newsgroup prefix))
8333               (setq to-newsgroup (or gnus-current-move-group "")))
8334           (or (gnus-gethash to-newsgroup gnus-active-hashtb)
8335               (gnus-activate-newsgroup to-newsgroup)
8336               (error "No such group: %s" to-newsgroup))
8337           (setq gnus-current-move-group to-newsgroup)))
8338     (or (gnus-check-backend-function 'request-accept-article 
8339                                      (or select-method to-newsgroup))
8340         (error "%s does not support article copying" to-newsgroup))
8341     (gnus-message 6 "Copying to %s: %s..." 
8342                   (or select-method to-newsgroup) articles)
8343     (while articles
8344       (if (setq art-group
8345                 (save-excursion
8346                   (set-buffer copy-buf)
8347                   (gnus-request-article-this-buffer
8348                    (car articles) gnus-newsgroup-name)
8349                   (gnus-request-accept-article
8350                    (if select-method (quote select-method) to-newsgroup)
8351                    (not (cdr articles)))))
8352           (let* ((entry 
8353                   (or
8354                    (gnus-gethash (car art-group) gnus-newsrc-hashtb)
8355                    (gnus-gethash 
8356                     (gnus-group-prefixed-name 
8357                      (car art-group) 
8358                      (if select-method (list select-method "")
8359                        (gnus-find-method-for-group to-newsgroup)))
8360                     gnus-newsrc-hashtb)))
8361                  (info (nth 2 entry))
8362                  (article (car articles))
8363                  (marked (nth 3 info)))
8364             (if (not (memq article gnus-newsgroup-unreads))
8365                 (setcar (cdr (cdr info))
8366                         (gnus-add-to-range (nth 2 info) 
8367                                            (list (cdr art-group)))))
8368             ;; Copy any marks over to the new group.
8369             (let ((marks '((tick . gnus-newsgroup-marked)
8370                            (dormant . gnus-newsgroup-dormant)
8371                            (expire . gnus-newsgroup-expirable)
8372                            (bookmark . gnus-newsgroup-bookmarks)
8373                         ;   (score . gnus-newsgroup-scored)
8374                            (reply . gnus-newsgroup-replied)))
8375                   (to-article (cdr art-group)))
8376               (while marks
8377                 (if (memq article (symbol-value (cdr (car marks))))
8378                     (gnus-add-marked-articles 
8379                      (car info) (car (car marks)) (list to-article) info))
8380                 (setq marks (cdr marks)))))
8381         (gnus-message 1 "Couldn't copy article %s" (car articles)))
8382       (gnus-summary-remove-process-mark (car articles))
8383       (setq articles (cdr articles)))
8384     (kill-buffer copy-buf)))
8385
8386 (defun gnus-summary-import-article (file)
8387   "Import a random file into a mail newsgroup."
8388   (interactive "fImport file: ")
8389   (let ((group gnus-newsgroup-name)
8390         atts)
8391     (or (gnus-check-backend-function 'request-accept-article group)
8392         (error "%s does not support article importing" group))
8393     (or (file-readable-p file)
8394         (not (file-regular-p file))
8395         (error "Can't read %s" file))
8396     (save-excursion
8397       (set-buffer (get-buffer-create " *import file*"))
8398       (buffer-disable-undo (current-buffer))
8399       (erase-buffer)
8400       (insert-file-contents file)
8401       (goto-char (point-min))
8402       (if (nnheader-article-p)
8403           ()
8404         (setq atts (file-attributes file))
8405         (insert "From: " (read-string "From: ") "\n"
8406                 "Subject: " (read-string "Subject: ") "\n"
8407                 "Date: " (current-time-string (nth 5 atts)) "\n"
8408                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8409       (gnus-request-accept-article group t)
8410       (kill-buffer (current-buffer)))))
8411
8412 (defun gnus-summary-expire-articles ()
8413   "Expire all articles that are marked as expirable in the current group."
8414   (interactive)
8415   (if (and gnus-newsgroup-expirable
8416            (gnus-check-backend-function 
8417             'request-expire-articles gnus-newsgroup-name))
8418       (let ((expirable gnus-newsgroup-expirable))
8419         ;; The list of articles that weren't expired is returned.
8420         (setq gnus-newsgroup-expirable 
8421               (gnus-request-expire-articles gnus-newsgroup-expirable
8422                                             gnus-newsgroup-name))
8423         ;; We go through the old list of expirable, and mark all
8424         ;; really expired articles as non-existent.
8425         (while expirable
8426           (or (memq (car expirable) gnus-newsgroup-expirable)
8427               (gnus-summary-mark-as-read (car expirable) gnus-canceled-mark))
8428           (setq expirable (cdr expirable))))))
8429
8430 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
8431 (defun gnus-summary-delete-article (n)
8432   "Delete the N next (mail) articles.
8433 This command actually deletes articles. This is not a marking
8434 command. The article will disappear forever from you life, never to
8435 return. 
8436 If N is negative, delete backwards.
8437 If N is nil and articles have been marked with the process mark,
8438 delete these instead."
8439   (interactive "P")
8440   (or (gnus-check-backend-function 'request-expire-articles 
8441                                    gnus-newsgroup-name)
8442       (error "The current newsgroup does not support article deletion."))
8443   ;; Compute the list of articles to delete.
8444   (let ((articles (gnus-summary-work-articles n))
8445         not-deleted)
8446     (if (and gnus-novice-user
8447              (not (gnus-y-or-n-p 
8448                    (format "Do you really want to delete %s forever? "
8449                            (if (> (length articles) 1) "these articles"
8450                              "this article")))))
8451         ()
8452       ;; Delete the articles.
8453       (setq not-deleted (gnus-request-expire-articles 
8454                          articles gnus-newsgroup-name 'force))
8455       (while articles
8456         (gnus-summary-remove-process-mark (car articles))       
8457         ;; The backend might not have been able to delete the article
8458         ;; after all.  
8459         (or (memq (car articles) not-deleted)
8460             (gnus-summary-mark-as-read (car articles) gnus-canceled-mark))
8461         (setq articles (cdr articles))))
8462     (gnus-summary-position-cursor)
8463     not-deleted))
8464
8465 (defun gnus-summary-edit-article ()
8466   "Enter into a buffer and edit the current article.
8467 This will have permanent effect only in mail groups."
8468   (interactive)
8469   (or (gnus-check-backend-function 
8470        'request-replace-article gnus-newsgroup-name)
8471       (error "The current newsgroup does not support article editing."))
8472   (gnus-summary-select-article t)
8473   (other-window 1)
8474   (gnus-message 6 "C-c C-c to end edits")
8475   (setq buffer-read-only nil)
8476   (text-mode)
8477   (use-local-map (copy-keymap (current-local-map)))
8478   (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
8479   (buffer-enable-undo)
8480   (goto-char (point-min))
8481   (search-forward "\n\n" nil t))
8482
8483 (defun gnus-summary-edit-article-done ()
8484   "Make edits to the current article permanent."
8485   (interactive)
8486   (if (not (gnus-request-replace-article 
8487             (cdr gnus-article-current) (car gnus-article-current) 
8488             (current-buffer)))
8489       (error "Couldn't replace article.")
8490     (gnus-article-mode)
8491     (use-local-map gnus-article-mode-map)
8492     (setq buffer-read-only t)
8493     (buffer-disable-undo (current-buffer))
8494     (pop-to-buffer gnus-summary-buffer)))      
8495
8496 (defun gnus-summary-fancy-query ()
8497   "Query where the fancy respool algorithm would put this article."
8498   (interactive)
8499   (gnus-summary-select-article)
8500   (save-excursion
8501     (set-buffer gnus-article-buffer)
8502     (save-restriction
8503       (goto-char (point-min))
8504       (search-forward "\n\n")
8505       (narrow-to-region (point-min) (point))
8506       (pp-eval-expression (list 'quote (nnmail-split-fancy))))))
8507
8508 ;; Summary score commands.
8509
8510 ;; Suggested by boubaker@cenatls.cena.dgac.fr.
8511
8512 (defun gnus-summary-raise-score (n)
8513   "Raise the score of the current article by N."
8514   (interactive "p")
8515   (gnus-summary-set-score (+ (gnus-summary-article-score) n)))
8516
8517 (defun gnus-summary-lower-score (n)
8518   "Lower the score of the current article by N."
8519   (interactive "p")
8520   (gnus-summary-raise-score (- n)))
8521
8522 (defun gnus-summary-set-score (n)
8523   "Set the score of the current article to N."
8524   (interactive "p")
8525   ;; Skip dummy header line.
8526   (save-excursion
8527     (gnus-summary-show-thread)
8528     (if (eq (gnus-summary-article-mark) gnus-dummy-mark)
8529         (forward-line 1))
8530     (let ((buffer-read-only nil))
8531       ;; Set score.
8532       (gnus-summary-update-mark
8533        (if (= n (or gnus-summary-default-score 0)) ? 
8534          (if (< n (or gnus-summary-default-score 0)) 
8535              gnus-score-below-mark gnus-score-over-mark)) 'score))
8536     (let* ((article (gnus-summary-article-number))
8537            (score (assq article gnus-newsgroup-scored)))
8538       (if score (setcdr score n)
8539         (setq gnus-newsgroup-scored 
8540               (cons (cons article n) gnus-newsgroup-scored))))
8541     (gnus-summary-update-line)))
8542
8543 (defun gnus-summary-current-score ()
8544   "Return the score of the current article."
8545   (interactive)
8546   (message "%s" (gnus-summary-article-score)))
8547
8548 ;; Summary marking commands.
8549
8550 (defun gnus-summary-raise-same-subject-and-select (score)
8551   "Raise articles which has the same subject with SCORE and select the next."
8552   (interactive "p")
8553   (let ((subject (gnus-summary-subject-string)))
8554     (gnus-summary-raise-score score)
8555     (while (gnus-summary-search-subject nil nil subject)
8556       (gnus-summary-raise-score score))
8557     (gnus-summary-next-article t)))
8558
8559 (defun gnus-summary-raise-same-subject (score)
8560   "Raise articles which has the same subject with SCORE."
8561   (interactive "p")
8562   (let ((subject (gnus-summary-subject-string)))
8563     (gnus-summary-raise-score score)
8564     (while (gnus-summary-search-subject nil nil subject)
8565       (gnus-summary-raise-score score))
8566     (gnus-summary-next-subject 1 t)))
8567
8568 (defun gnus-score-default (level)
8569   (if level (prefix-numeric-value level) 
8570     gnus-score-interactive-default-score))
8571
8572 (defun gnus-summary-raise-thread (score)
8573   "Raise the score of the articles in the current thread with SCORE."
8574   (interactive "P")
8575   (setq score (1- (gnus-score-default score)))
8576   (let (e)
8577     (save-excursion
8578       (let ((level (gnus-summary-thread-level)))
8579         (gnus-summary-raise-score score)
8580         (while (and (zerop (gnus-summary-next-subject 1 nil t))
8581                     (> (gnus-summary-thread-level) level))
8582           (gnus-summary-raise-score score))
8583         (setq e (point))))
8584     (let ((gnus-summary-check-current t))
8585       (or (zerop (gnus-summary-next-subject 1 t))
8586           (goto-char e))))
8587   (gnus-summary-recenter)
8588   (gnus-summary-position-cursor)
8589   (gnus-set-mode-line 'summary))
8590
8591 (defun gnus-summary-lower-same-subject-and-select (score)
8592   "Raise articles which has the same subject with SCORE and select the next."
8593   (interactive "p")
8594   (gnus-summary-raise-same-subject-and-select (- score)))
8595
8596 (defun gnus-summary-lower-same-subject (score)
8597   "Raise articles which has the same subject with SCORE."
8598   (interactive "p")
8599   (gnus-summary-raise-same-subject (- score)))
8600
8601 (defun gnus-summary-lower-thread (score)
8602   "Lower score of articles in the current thread with SCORE."
8603   (interactive "P")
8604   (gnus-summary-raise-thread (- (1- (gnus-score-default score)))))
8605
8606 (defun gnus-summary-kill-same-subject-and-select (unmark)
8607   "Mark articles which has the same subject as read, and then select the next.
8608 If UNMARK is positive, remove any kind of mark.
8609 If UNMARK is negative, tick articles."
8610   (interactive "P")
8611   (if unmark
8612       (setq unmark (prefix-numeric-value unmark)))
8613   (let ((count
8614          (gnus-summary-mark-same-subject
8615           (gnus-summary-subject-string) unmark)))
8616     ;; Select next unread article. If auto-select-same mode, should
8617     ;; select the first unread article.
8618     (gnus-summary-next-article t (and gnus-auto-select-same
8619                                       (gnus-summary-subject-string)))
8620     (gnus-message 7 "%d articles are marked as %s"
8621                   count (if unmark "unread" "read"))))
8622
8623 (defun gnus-summary-kill-same-subject (unmark)
8624   "Mark articles which has the same subject as read. 
8625 If UNMARK is positive, remove any kind of mark.
8626 If UNMARK is negative, tick articles."
8627   (interactive "P")
8628   (if unmark
8629       (setq unmark (prefix-numeric-value unmark)))
8630   (let ((count
8631          (gnus-summary-mark-same-subject
8632           (gnus-summary-subject-string) unmark)))
8633     ;; If marked as read, go to next unread subject.
8634     (if (null unmark)
8635         ;; Go to next unread subject.
8636         (gnus-summary-next-subject 1 t))
8637     (gnus-message 7 "%d articles are marked as %s"
8638                   count (if unmark "unread" "read"))))
8639
8640 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8641   "Mark articles with same SUBJECT as read, and return marked number.
8642 If optional argument UNMARK is positive, remove any kinds of marks.
8643 If optional argument UNMARK is negative, mark articles as unread instead."
8644   (let ((count 1))
8645     (save-excursion
8646       (cond ((null unmark)
8647              (gnus-summary-mark-as-read nil gnus-killed-mark))
8648             ((> unmark 0)
8649              (gnus-summary-tick-article nil t))
8650             (t
8651              (gnus-summary-tick-article)))
8652       (while (and subject
8653                   (gnus-summary-search-forward nil subject))
8654         (cond ((null unmark)
8655                (gnus-summary-mark-as-read nil gnus-killed-mark))
8656               ((> unmark 0)
8657                (gnus-summary-tick-article nil t))
8658               (t
8659                (gnus-summary-tick-article)))
8660         (setq count (1+ count))))
8661     ;; Hide killed thread subtrees.  Does not work properly always.
8662     ;;(and (null unmark)
8663     ;;     gnus-thread-hide-killed
8664     ;;     (gnus-summary-hide-thread))
8665     ;; Return number of articles marked as read.
8666     count))
8667
8668 (defun gnus-summary-mark-as-processable (n &optional unmark)
8669   "Set the process mark on the next N articles.
8670 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8671 the process mark instead.  The difference between N and the actual
8672 number of articles marked is returned."
8673   (interactive "p")
8674   (let ((backward (< n 0))
8675         (n (abs n)))
8676   (while (and 
8677           (> n 0)
8678           (if unmark
8679               (gnus-summary-remove-process-mark (gnus-summary-article-number))
8680             (gnus-summary-set-process-mark (gnus-summary-article-number)))
8681           (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8682     (setq n (1- n)))
8683   (if (/= 0 n) (gnus-message 7 "No more articles"))
8684   (gnus-summary-recenter)
8685   (gnus-summary-position-cursor)
8686   n))
8687
8688 (defun gnus-summary-unmark-as-processable (n)
8689   "Remove the process mark from the next N articles.
8690 If N is negative, mark backward instead.  The difference between N and
8691 the actual number of articles marked is returned."
8692   (interactive "p")
8693   (gnus-summary-mark-as-processable n t))
8694
8695 (defun gnus-summary-unmark-all-processable ()
8696   "Remove the process mark from all articles."
8697   (interactive)
8698   (save-excursion
8699     (while gnus-newsgroup-processable
8700       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8701   (gnus-summary-position-cursor))
8702
8703 (defun gnus-summary-mark-as-expirable (n)
8704   "Mark N articles forward as expirable.
8705 If N is negative, mark backward instead. The difference between N and
8706 the actual number of articles marked is returned."
8707   (interactive "p")
8708   (gnus-summary-mark-forward n gnus-expirable-mark))
8709
8710 (defun gnus-summary-mark-article-as-replied (article)
8711   "Mark ARTICLE replied and update the summary line."
8712   (setq gnus-newsgroup-replied (cons article gnus-newsgroup-replied))
8713   (let ((buffer-read-only nil))
8714     (if (gnus-summary-goto-subject article)
8715         (progn
8716           (gnus-summary-update-mark gnus-replied-mark 'replied)
8717           t))))
8718
8719 (defun gnus-summary-set-bookmark (article)
8720   "Set a bookmark in current article."
8721   (interactive (list (gnus-summary-article-number)))
8722   (if (or (not (get-buffer gnus-article-buffer))
8723           (not gnus-current-article)
8724           (not gnus-article-current)
8725           (not (equal gnus-newsgroup-name (car gnus-article-current))))
8726       (error "No current article selected"))
8727   ;; Remove old bookmark, if one exists.
8728   (let ((old (assq article gnus-newsgroup-bookmarks)))
8729     (if old (setq gnus-newsgroup-bookmarks 
8730                   (delq old gnus-newsgroup-bookmarks))))
8731   ;; Set the new bookmark, which is on the form 
8732   ;; (article-number . line-number-in-body).
8733   (setq gnus-newsgroup-bookmarks 
8734         (cons 
8735          (cons article 
8736                (save-excursion
8737                  (set-buffer gnus-article-buffer)
8738                  (count-lines
8739                   (min (point)
8740                        (save-excursion
8741                          (goto-char (point-min))
8742                          (search-forward "\n\n" nil t)
8743                          (point)))
8744                   (point))))
8745          gnus-newsgroup-bookmarks))
8746   (gnus-message 6 "A bookmark has been added to the current article."))
8747
8748 (defun gnus-summary-remove-bookmark (article)
8749   "Remove the bookmark from the current article."
8750   (interactive (list (gnus-summary-article-number)))
8751   ;; Remove old bookmark, if one exists.
8752   (let ((old (assq article gnus-newsgroup-bookmarks)))
8753     (if old 
8754         (progn
8755           (setq gnus-newsgroup-bookmarks 
8756                 (delq old gnus-newsgroup-bookmarks))
8757           (gnus-message 6 "Removed bookmark."))
8758       (gnus-message 6 "No bookmark in current article."))))
8759
8760 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8761 (defun gnus-summary-mark-as-dormant (n)
8762   "Mark N articles forward as dormant.
8763 If N is negative, mark backward instead.  The difference between N and
8764 the actual number of articles marked is returned."
8765   (interactive "p")
8766   (gnus-summary-mark-forward n gnus-dormant-mark))
8767
8768 (defun gnus-summary-set-process-mark (article)
8769   "Set the process mark on ARTICLE and update the summary line."
8770   (setq gnus-newsgroup-processable (cons article gnus-newsgroup-processable))
8771   (let ((buffer-read-only nil))
8772     (if (gnus-summary-goto-subject article)
8773         (progn
8774           (gnus-summary-show-thread)
8775           (and (eq (gnus-summary-article-mark) gnus-dummy-mark)
8776                (forward-line 1))
8777           (gnus-summary-update-mark gnus-process-mark 'replied)
8778           t))))
8779
8780 (defun gnus-summary-remove-process-mark (article)
8781   "Remove the process mark from ARTICLE and update the summary line."
8782   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8783   (let ((buffer-read-only nil))
8784     (if (gnus-summary-goto-subject article)
8785         (progn
8786           (gnus-summary-show-thread)
8787           (and (eq (gnus-summary-article-mark) gnus-dummy-mark)
8788                (forward-line 1))
8789           (gnus-summary-update-mark ?  'replied)
8790           (if (memq article gnus-newsgroup-replied) 
8791               (gnus-summary-update-mark gnus-replied-mark 'replied))
8792           t))))
8793
8794 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8795   "Mark N articles as read forwards.
8796 If N is negative, mark backwards instead.
8797 Mark with MARK. If MARK is ? , ?! or ??, articles will be
8798 marked as unread. 
8799 The difference between N and the actual number of articles marked is
8800 returned."
8801   (interactive "p")
8802   (gnus-set-global-variables)
8803   (let ((backward (< n 0))
8804         (n (abs n))
8805         (mark (or mark gnus-del-mark)))
8806   (while (and (> n 0)
8807               (gnus-summary-mark-article nil mark no-expire)
8808               (zerop (gnus-summary-next-subject 
8809                       (if backward -1 1) gnus-summary-goto-unread t)))
8810     (setq n (1- n)))
8811   (if (/= 0 n) (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8812   (gnus-summary-recenter)
8813   (gnus-summary-position-cursor)
8814   (gnus-set-mode-line 'summary)
8815   n))
8816
8817 (defun gnus-summary-mark-article (&optional article mark no-expire)
8818   "Mark ARTICLE with MARK.
8819 MARK can be any character.
8820 Five MARK strings are reserved: ?  (unread), 
8821 ?! (ticked), ?? (dormant), ?D (read), ?E (expirable).
8822 If MARK is nil, then the default character ?D is used.
8823 If ARTICLE is nil, then the article on the current line will be
8824 marked." 
8825   ;; If no mark is given, then we check auto-expiring.
8826   (and (not no-expire)
8827        gnus-newsgroup-auto-expire 
8828        (or (not mark)
8829            (and (numberp mark) 
8830                 (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
8831                     (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
8832                     (= mark gnus-read-mark) (= mark gnus-ancient-mark))))
8833        (setq mark gnus-expirable-mark))
8834   (let* ((mark (or (and (stringp mark) (aref mark 0)) mark gnus-del-mark))
8835          (article (or article (gnus-summary-article-number))))
8836     (or article (error "No article on current line"))
8837     (if (or (= mark gnus-unread-mark) 
8838             (= mark gnus-ticked-mark) 
8839             (= mark gnus-dormant-mark))
8840         (gnus-mark-article-as-unread article mark)
8841       (gnus-mark-article-as-read article mark))
8842
8843     ;; See whether the article is to be put in the cache.
8844     (and gnus-use-cache
8845          (save-excursion
8846            (gnus-cache-possibly-enter-article 
8847             gnus-newsgroup-name article 
8848             (gnus-get-header-by-number article)
8849             (= mark gnus-ticked-mark)
8850             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8851
8852     (if (gnus-summary-goto-subject article)
8853         (let ((buffer-read-only nil))
8854           (gnus-summary-show-thread)
8855           (and (eq (gnus-summary-article-mark) gnus-dummy-mark)
8856                (forward-line 1))
8857           ;; Fix the mark.
8858           (gnus-summary-update-mark mark 'unread)
8859           t))))
8860
8861 (defun gnus-summary-update-mark (mark type)
8862   (beginning-of-line)
8863   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8864         plist)
8865     (if (not forward)
8866         ()
8867       (forward-char forward)
8868       (setq plist (text-properties-at (point)))
8869       (delete-char 1)
8870       (insert mark)
8871       (and plist (add-text-properties (1- (point)) (point) plist))
8872       (and (eq type 'unread)
8873            (add-text-properties (1- (point)) (point) (list 'gnus-mark mark)))
8874       (gnus-summary-update-line (eq mark gnus-unread-mark)))))
8875   
8876 (defun gnus-mark-article-as-read (article &optional mark)
8877   "Enter ARTICLE in the pertinent lists and remove it from others."
8878   ;; Make the article expirable.
8879   (let ((mark (or (and (stringp mark) (aref mark 0)) mark gnus-del-mark)))
8880     (if (= mark gnus-expirable-mark)
8881         (setq gnus-newsgroup-expirable (cons article gnus-newsgroup-expirable))
8882       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8883     ;; Remove from unread and marked lists.
8884     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8885     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8886     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8887     ;; Possibly remove from cache, if that is used. 
8888     (and gnus-use-cache (gnus-cache-enter-remove-article article))))
8889
8890 (defun gnus-mark-article-as-unread (article &optional mark)
8891   "Enter ARTICLE in the pertinent lists and remove it from others."
8892   (let ((mark (or (and (stringp mark) (aref mark 0)) mark gnus-ticked-mark)))
8893     ;; Add to unread list.
8894     (or (memq article gnus-newsgroup-unreads)
8895         (setq gnus-newsgroup-unreads (cons article gnus-newsgroup-unreads)))
8896     ;; If CLEAR-MARK is non-nil, the article must be removed from mark
8897     ;; lists.  Otherwise, it must be added to the list.
8898     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8899     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8900     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8901     (if (= mark gnus-ticked-mark)
8902         (setq gnus-newsgroup-marked (cons article gnus-newsgroup-marked)))
8903     (if (= mark gnus-dormant-mark)
8904         (setq gnus-newsgroup-dormant (cons article gnus-newsgroup-dormant)))))
8905
8906 (defalias 'gnus-summary-mark-as-unread-forward 
8907   'gnus-summary-tick-article-forward)
8908 (make-obsolete 'gnus-summary-mark-as-unread-forward 
8909                'gnus-summary-tick-article-forward)
8910 (defun gnus-summary-tick-article-forward (n)
8911   "Tick N articles forwards.
8912 If N is negative, tick backwards instead.
8913 The difference between N and the number of articles ticked is returned."
8914   (interactive "p")
8915   (gnus-summary-mark-forward n gnus-ticked-mark))
8916
8917 (defalias 'gnus-summary-mark-as-unread-backward 
8918   'gnus-summary-tick-article-backward)
8919 (make-obsolete 'gnus-summary-mark-as-unread-backward 
8920                'gnus-summary-tick-article-backward)
8921 (defun gnus-summary-tick-article-backward (n)
8922   "Tick N articles backwards.
8923 The difference between N and the number of articles ticked is returned."
8924   (interactive "p")
8925   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8926
8927 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8928 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8929 (defun gnus-summary-tick-article (&optional article clear-mark)
8930   "Mark current article as unread.
8931 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8932 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8933   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8934                                        gnus-ticked-mark)))
8935
8936 (defun gnus-summary-mark-as-read-forward (n)
8937   "Mark N articles as read forwards.
8938 If N is negative, mark backwards instead.
8939 The difference between N and the actual number of articles marked is
8940 returned."
8941   (interactive "p")
8942   (gnus-summary-mark-forward n gnus-del-mark t))
8943
8944 (defun gnus-summary-mark-as-read-backward (n)
8945   "Mark the N articles as read backwards.
8946 The difference between N and the actual number of articles marked is
8947 returned."
8948   (interactive "p")
8949   (gnus-summary-mark-forward (- n) gnus-del-mark t))
8950
8951 (defun gnus-summary-mark-as-read (&optional article mark)
8952   "Mark current article as read.
8953 ARTICLE specifies the article to be marked as read.
8954 MARK specifies a string to be inserted at the beginning of the line."
8955   (gnus-summary-mark-article article mark))
8956
8957 (defun gnus-summary-clear-mark-forward (n)
8958   "Clear marks from N articles forward.
8959 If N is negative, clear backward instead.
8960 The difference between N and the number of marks cleared is returned."
8961   (interactive "p")
8962   (gnus-summary-mark-forward n gnus-unread-mark))
8963
8964 (defun gnus-summary-clear-mark-backward (n)
8965   "Clear marks from N articles backward.
8966 The difference between N and the number of marks cleared is returned."
8967   (interactive "p")
8968   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8969
8970 (defun gnus-summary-mark-unread-as-read ()
8971   "Intended to be used by `gnus-summary-mark-article-hook'."
8972   (or (memq gnus-current-article gnus-newsgroup-marked)
8973       (memq gnus-current-article gnus-newsgroup-dormant)
8974       (memq gnus-current-article gnus-newsgroup-expirable)
8975       (gnus-summary-mark-as-read gnus-current-article gnus-read-mark)))
8976
8977 (defun gnus-summary-mark-region-as-read (point mark all)
8978   "Mark all unread articles between point and mark as read.
8979 If given a prefix, mark all articles between point and mark as read,
8980 even ticked and dormant ones."
8981   (interactive "r\nP")
8982   (save-excursion
8983     (goto-char point)
8984     (beginning-of-line)
8985     (while (and 
8986             (< (point) mark)
8987             (progn
8988               (and
8989                (or all
8990                    (and
8991                     (not (memq (gnus-summary-article-number)
8992                                gnus-newsgroup-marked))
8993                     (not (memq (gnus-summary-article-number)
8994                                gnus-newsgroup-dormant))))
8995                (gnus-summary-mark-article
8996                 (gnus-summary-article-number) gnus-del-mark))
8997               t)
8998             (zerop (forward-line 1))))))
8999
9000 ;; Fix by Per Abrahamsen <amanda@iesd.auc.dk>.
9001 (defalias 'gnus-summary-delete-marked-as-read 
9002   'gnus-summary-remove-lines-marked-as-read)
9003 (make-obsolete 'gnus-summary-delete-marked-as-read 
9004                'gnus-summary-remove-lines-marked-as-read)
9005 (defun gnus-summary-remove-lines-marked-as-read ()
9006   "Remove lines that are marked as read."
9007   (interactive)
9008   (gnus-summary-remove-lines-marked-with 
9009    (concat (mapconcat
9010             (lambda (char) (char-to-string (symbol-value char)))
9011             '(gnus-del-mark gnus-read-mark gnus-ancient-mark
9012               gnus-killed-mark gnus-kill-file-mark
9013               gnus-low-score-mark gnus-expirable-mark)
9014             ""))))
9015
9016 (defalias 'gnus-summary-delete-marked-with 
9017   'gnus-summary-remove-lines-marked-with)
9018 (make-obsolete 'gnus-summary-delete-marked-with 
9019                'gnus-summary-remove-lines-marked-with)
9020 ;; Rewrite by Daniel Quinlan <quinlan@best.com>.
9021 (defun gnus-summary-remove-lines-marked-with (marks)
9022   "Remove lines that are marked with MARKS (e.g. \"DK\")."
9023   (interactive "sMarks: ")
9024   ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
9025   (gnus-set-global-variables)
9026   (let ((buffer-read-only nil)
9027         (marks (concat "^[" marks "]")))
9028     (goto-char (point-min))
9029     (if gnus-newsgroup-adaptive
9030         (gnus-score-remove-lines-adaptive marks)
9031       (while (re-search-forward marks nil t)
9032         (gnus-delete-line)))
9033     ;; If we use dummy roots, we have to do an additional sweep over
9034     ;; the buffer.
9035     (if (not (eq gnus-summary-make-false-root 'dummy))
9036         ()
9037       (goto-char (point-min))
9038       (setq marks (concat "^[" (char-to-string gnus-dummy-mark) "]"))
9039       (while (re-search-forward marks nil t)
9040         (if (gnus-subject-equal
9041              (gnus-summary-subject-string)
9042              (progn
9043                (forward-line 1)
9044                (gnus-summary-subject-string)))
9045             ()
9046           (forward-line -1)
9047           (gnus-delete-line)))))
9048   (or (zerop (buffer-size))
9049       (if (eobp)
9050           (gnus-summary-prev-subject 1)
9051         (gnus-summary-position-cursor))))
9052
9053 (defun gnus-summary-expunge-below (score)
9054   "Remove articles with score less than SCORE."
9055   (interactive "P")
9056   (gnus-set-global-variables)
9057   (setq score (if score
9058                   (prefix-numeric-value score)
9059                 (or gnus-summary-default-score 0)))
9060   (save-excursion
9061     (set-buffer gnus-summary-buffer)
9062     (goto-char (point-min))
9063     (let ((buffer-read-only nil)
9064           beg)
9065       (while (not (eobp))
9066         (if (< (gnus-summary-article-score) score)
9067             (progn
9068               (setq beg (point))
9069               (forward-line 1)
9070               (delete-region beg (point)))
9071           (forward-line 1)))
9072       ;; Adjust point.
9073       (or (zerop (buffer-size))
9074           (if (eobp)
9075               (gnus-summary-prev-subject 1)
9076             (gnus-summary-position-cursor))))))
9077
9078 (defun gnus-summary-mark-below (score mark)
9079   "Mark articles with score less than SCORE with MARK."
9080   (interactive "P\ncMark: ")
9081   (gnus-set-global-variables)
9082   (setq score (if score
9083                   (prefix-numeric-value score)
9084                 (or gnus-summary-default-score 0)))
9085   (save-excursion
9086     (set-buffer gnus-summary-buffer)
9087     (goto-char (point-min))
9088     (while (not (eobp))
9089       (and (< (gnus-summary-article-score) score)
9090            (gnus-summary-mark-article nil mark))
9091       (forward-line 1))))
9092
9093 (defun gnus-summary-kill-below (score)
9094   "Mark articles with score below SCORE as read."
9095   (interactive "P")
9096   (gnus-summary-mark-below score gnus-killed-mark))
9097
9098 (defun gnus-summary-clear-above (score)
9099   "Clear all marks from articles with score above SCORE."
9100   (interactive "P")
9101   (gnus-summary-mark-above score gnus-unread-mark))
9102
9103 (defun gnus-summary-tick-above (score)
9104   "Tick all articles with score above SCORE."
9105   (interactive "P")
9106   (gnus-summary-mark-above score gnus-ticked-mark))
9107
9108 (defun gnus-summary-mark-above (score mark)
9109   "Mark articles with score over SCORE with MARK."
9110   (interactive "P\ncMark: ")
9111   (setq score (if score
9112                   (prefix-numeric-value score)
9113                 (or gnus-summary-default-score 0)))
9114   (save-excursion
9115     (set-buffer gnus-summary-buffer)
9116     (goto-char (point-min))
9117     (while (not (eobp))
9118       (if (> (gnus-summary-article-score) score)
9119           (progn
9120             (gnus-summary-mark-article nil mark)
9121             (forward-line 1))
9122         (forward-line 1)))))
9123
9124 ;; Suggested by Daniel Quinlan <quinlan@best.com>.  
9125 (defun gnus-summary-show-all-expunged ()
9126   "Display all the hidden articles that were expunged for low scores."
9127   (interactive)
9128   (let ((buffer-read-only nil))
9129     (let ((scored gnus-newsgroup-scored)
9130           headers h)
9131       (while scored
9132         (or (gnus-summary-goto-subject (car (car scored)))
9133             (and (setq h (gnus-get-header-by-number (car (car scored))))
9134                  (< (cdr (car scored)) gnus-summary-expunge-below)
9135                  (setq headers (cons h headers))))
9136         (setq scored (cdr scored)))
9137       (or headers (error "No expunged articles hidden."))
9138       (goto-char (point-min))
9139       (save-excursion 
9140         (gnus-summary-update-lines 
9141          (point)
9142          (progn
9143            (gnus-summary-prepare-threads (nreverse headers) 0)
9144            (point)))))
9145     (goto-char (point-min))
9146     (gnus-summary-position-cursor)))
9147
9148 (defun gnus-summary-show-all-dormant ()
9149   "Display all the hidden articles that are marked as dormant."
9150   (interactive)
9151   (let ((buffer-read-only nil))
9152     (goto-char (point-min))
9153     (let ((dormant gnus-newsgroup-dormant)
9154           headers h)
9155       (while dormant
9156         (or (gnus-summary-goto-subject (car dormant))
9157             (and (setq h (gnus-get-header-by-number (car dormant)))
9158                  (setq headers (cons h headers))))
9159         (setq dormant (cdr dormant)))
9160       (or headers (error "No dormant articles hidden."))
9161       (save-excursion 
9162         (gnus-summary-update-lines 
9163          (point)
9164          (progn
9165            (gnus-summary-prepare-threads (nreverse headers) 0)
9166            (point)))))
9167     (goto-char (point-min))
9168     (gnus-summary-position-cursor)))
9169
9170 (defun gnus-summary-hide-all-dormant ()
9171   "Hide all dormant articles."
9172   (interactive)
9173   (gnus-summary-remove-lines-marked-with (char-to-string gnus-dormant-mark))
9174   (gnus-summary-position-cursor))
9175
9176 (defun gnus-summary-catchup (all &optional quietly to-here not-mark)
9177   "Mark all articles not marked as unread in this newsgroup as read.
9178 If prefix argument ALL is non-nil, all articles are marked as read.
9179 If QUIETLY is non-nil, no questions will be asked.
9180 If TO-HERE is non-nil, it should be a point in the buffer. All
9181 articles before this point will be marked as read.
9182 The number of articles marked as read is returned."
9183   (interactive "P")
9184   (prog1
9185       (if (or quietly
9186               (not gnus-interactive-catchup) ;Without confirmation?
9187               gnus-expert-user
9188               (gnus-y-or-n-p
9189                (if all
9190                    "Mark absolutely all articles as read? "
9191                  "Mark all unread articles as read? ")))
9192           (if (and not-mark 
9193                    (not gnus-newsgroup-adaptive)
9194                    (not gnus-newsgroup-auto-expire))
9195               (progn
9196                 (and all (setq gnus-newsgroup-marked nil
9197                                gnus-newsgroup-dormant nil))
9198                 (setq gnus-newsgroup-unreads 
9199                       (append gnus-newsgroup-marked gnus-newsgroup-dormant)))
9200             ;; We actually mark all articles as canceled, which we
9201             ;; have to do when using auto-expiry or adaptive scoring. 
9202             (let ((unreads (length gnus-newsgroup-unreads)))
9203               (if (gnus-summary-first-subject (not all))
9204                   (while (and (if to-here (< (point) to-here) t)
9205                               (gnus-summary-mark-as-read nil gnus-catchup-mark)
9206                               (gnus-summary-search-subject nil (not all)))))
9207               (- unreads (length gnus-newsgroup-unreads))
9208               (or to-here
9209                   (setq gnus-newsgroup-unreads gnus-newsgroup-marked)))))
9210     (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
9211       (if (and (not to-here) (eq 'nnvirtual (car method)))
9212           (nnvirtual-catchup-group
9213            (gnus-group-real-name gnus-newsgroup-name) (nth 1 method) all)))
9214     (gnus-summary-position-cursor)))
9215
9216 (defun gnus-summary-catchup-to-here (&optional all)
9217   "Mark all unticked articles before the current one as read.
9218 If ALL is non-nil, also mark ticked and dormant articles as read."
9219   (interactive)
9220   (save-excursion
9221     (end-of-line)
9222     (gnus-summary-catchup all t (point))
9223     (gnus-set-mode-line 'summary))
9224   (gnus-summary-position-cursor))
9225
9226 (defun gnus-summary-catchup-all (&optional quietly)
9227   "Mark all articles in this newsgroup as read."
9228   (interactive)
9229   (gnus-summary-catchup t quietly))
9230
9231 (defun gnus-summary-catchup-and-exit (all &optional quietly)
9232   "Mark all articles not marked as unread in this newsgroup as read, then exit.
9233 If prefix argument ALL is non-nil, all articles are marked as read."
9234   (interactive "P")
9235   (gnus-summary-catchup all quietly nil 'fast)
9236   ;; Select next newsgroup or exit.
9237   (if (eq gnus-auto-select-next 'quietly)
9238       (gnus-summary-next-group nil)
9239     (gnus-summary-exit)))
9240
9241 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
9242   "Mark all articles in this newsgroup as read, and then exit."
9243   (interactive)
9244   (gnus-summary-catchup-and-exit t quietly))
9245
9246 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
9247 (defun gnus-summary-catchup-and-goto-next-group (all)
9248   "Mark all articles in this group as read and select the next group.
9249 If given a prefix, mark all articles, unread as well as ticked, as
9250 read." 
9251   (interactive "P")
9252   (gnus-summary-catchup all)
9253   (gnus-summary-next-group))
9254
9255 ;; Thread-based commands.
9256
9257 (defun gnus-summary-toggle-threads (arg)
9258   "Toggle showing conversation threads.
9259 If ARG is positive number, turn showing conversation threads on."
9260   (interactive "P")
9261   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
9262     (setq gnus-show-threads
9263           (if (null arg) (not gnus-show-threads)
9264             (> (prefix-numeric-value arg) 0)))
9265     (gnus-summary-prepare)
9266     (gnus-summary-goto-subject current)
9267     (gnus-summary-position-cursor)))
9268
9269 (defun gnus-summary-show-all-threads ()
9270   "Show all threads."
9271   (interactive)
9272   (save-excursion
9273     (let ((buffer-read-only nil))
9274       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
9275   (gnus-summary-position-cursor))
9276
9277 (defun gnus-summary-show-thread ()
9278   "Show thread subtrees.
9279 Returns nil if no thread was there to be shown."
9280   (interactive)
9281   (prog1
9282       (save-excursion
9283         (let ((buffer-read-only nil)
9284               ;; first goto end then to beg, to have point at beg after let
9285               (end (progn (end-of-line) (point)))
9286               (beg (progn (beginning-of-line) (point))))
9287           (prog1
9288               ;; Any hidden lines here?
9289               (search-forward "\r" end t)
9290             (subst-char-in-region beg end ?\^M ?\n t))))
9291     (gnus-summary-position-cursor)))
9292
9293 (defun gnus-summary-hide-all-threads ()
9294   "Hide all thread subtrees."
9295   (interactive)
9296   (save-excursion
9297     (goto-char (point-min))
9298     (gnus-summary-hide-thread)
9299     (while (and (not (eobp)) (zerop (forward-line 1)))
9300       (gnus-summary-hide-thread)))
9301   (gnus-summary-position-cursor))
9302
9303 (defun gnus-summary-hide-thread ()
9304   "Hide thread subtrees.
9305 Returns nil if no threads were there to be hidden."
9306   (interactive)
9307   (let ((buffer-read-only nil)
9308         (start (point))
9309         (level (gnus-summary-thread-level))
9310         (end (point)))
9311     ;; Go forward until either the buffer ends or the subthread
9312     ;; ends. 
9313     (if (eobp)
9314         ()
9315       (while (and (zerop (forward-line 1))
9316                   (> (gnus-summary-thread-level) level))
9317         (setq end (point)))
9318       (prog1
9319           (save-excursion
9320             (goto-char end)
9321             (search-backward "\n" start t))
9322         (subst-char-in-region start end ?\n ?\^M t)
9323         (forward-line -1)))))
9324
9325 (defun gnus-summary-go-to-next-thread (&optional previous)
9326   "Go to the same level (or less) next thread.
9327 If PREVIOUS is non-nil, go to previous thread instead.
9328 Return the article number moved to, or nil if moving was impossible."
9329   (let ((level (gnus-summary-thread-level))
9330         (article (gnus-summary-article-number)))
9331     (if previous 
9332         (while (and (zerop (forward-line -1))
9333                     (> (gnus-summary-thread-level) level)))
9334       (while (and (save-excursion
9335                     (forward-line 1)
9336                     (not (eobp)))
9337                   (zerop (forward-line 1))
9338                   (> (gnus-summary-thread-level) level))))
9339     (gnus-summary-recenter)
9340     (gnus-summary-position-cursor)
9341     (let ((oart (gnus-summary-article-number)))
9342       (and (/= oart article) oart))))
9343
9344 (defun gnus-summary-next-thread (n)
9345   "Go to the same level next N'th thread.
9346 If N is negative, search backward instead.
9347 Returns the difference between N and the number of skips actually
9348 done."
9349   (interactive "p")
9350   (let ((backward (< n 0))
9351         (n (abs n)))
9352   (while (and (> n 0)
9353               (gnus-summary-go-to-next-thread backward))
9354     (setq n (1- n)))
9355   (gnus-summary-position-cursor)
9356   (if (/= 0 n) (gnus-message 7 "No more threads"))
9357   n))
9358
9359 (defun gnus-summary-prev-thread (n)
9360   "Go to the same level previous N'th thread.
9361 Returns the difference between N and the number of skips actually
9362 done."
9363   (interactive "p")
9364   (gnus-summary-next-thread (- n)))
9365
9366 (defun gnus-summary-go-down-thread (&optional same)
9367   "Go down one level in the current thread.
9368 If SAME is non-nil, also move to articles of the same level."
9369   (let ((level (gnus-summary-thread-level))
9370         (start (point)))
9371     (if (and (zerop (forward-line 1))
9372              (> (gnus-summary-thread-level) level))
9373         t
9374       (goto-char start)
9375       nil)))
9376
9377 (defun gnus-summary-go-up-thread ()
9378   "Go up one level in the current thread."
9379   (let ((level (gnus-summary-thread-level))
9380         (start (point)))
9381     (while (and (zerop (forward-line -1))
9382                 (>= (gnus-summary-thread-level) level)))
9383     (if (>= (gnus-summary-thread-level) level)
9384         (progn
9385           (goto-char start)
9386           nil)
9387       t)))
9388
9389 (defun gnus-summary-down-thread (n)
9390   "Go down thread N steps.
9391 If N is negative, go up instead.
9392 Returns the difference between N and how many steps down that were
9393 taken."
9394   (interactive "p")
9395   (let ((up (< n 0))
9396         (n (abs n)))
9397   (while (and (> n 0)
9398               (if up (gnus-summary-go-up-thread)
9399                 (gnus-summary-go-down-thread)))
9400     (setq n (1- n)))
9401   (gnus-summary-position-cursor)
9402   (if (/= 0 n) (gnus-message 7 "Can't go further"))
9403   n))
9404
9405 (defun gnus-summary-up-thread (n)
9406   "Go up thread N steps.
9407 If N is negative, go up instead.
9408 Returns the difference between N and how many steps down that were
9409 taken."
9410   (interactive "p")
9411   (gnus-summary-down-thread (- n)))
9412
9413 (defun gnus-summary-kill-thread (unmark)
9414   "Mark articles under current thread as read.
9415 If the prefix argument is positive, remove any kinds of marks.
9416 If the prefix argument is negative, tick articles instead."
9417   (interactive "P")
9418   (if unmark
9419       (setq unmark (prefix-numeric-value unmark)))
9420   (let ((killing t)
9421         (level (gnus-summary-thread-level)))
9422     (save-excursion
9423       (while killing
9424         ;; Mark the article...
9425         (cond ((null unmark) (gnus-summary-mark-as-read nil gnus-killed-mark))
9426               ((> unmark 0) (gnus-summary-tick-article nil t))
9427               (t (gnus-summary-tick-article)))
9428         ;; ...and go forward until either the buffer ends or the subtree
9429         ;; ends. 
9430         (if (not (and (zerop (forward-line 1))
9431                       (> (gnus-summary-thread-level) level)))
9432             (setq killing nil))))
9433     ;; Hide killed subtrees.
9434     (and (null unmark)
9435          gnus-thread-hide-killed
9436          (gnus-summary-hide-thread))
9437     ;; If marked as read, go to next unread subject.
9438     (if (null unmark)
9439         ;; Go to next unread subject.
9440         (gnus-summary-next-subject 1 t)))
9441   (gnus-set-mode-line 'summary))
9442
9443 ;; Summary sorting commands
9444
9445 (defun gnus-summary-sort-by-number (&optional reverse)
9446   "Sort summary buffer by article number.
9447 Argument REVERSE means reverse order."
9448   (interactive "P")
9449   (gnus-summary-sort 
9450    (cons 'gnus-summary-article-number 'gnus-thread-sort-by-number) reverse))
9451
9452 (defun gnus-summary-sort-by-author (&optional reverse)
9453   "Sort summary buffer by author name alphabetically.
9454 If case-fold-search is non-nil, case of letters is ignored.
9455 Argument REVERSE means reverse order."
9456   (interactive "P")
9457   (gnus-summary-sort
9458    (cons
9459     (lambda ()
9460       (let ((extract (funcall
9461                       gnus-extract-address-components
9462                       (header-from (gnus-get-header-by-number
9463                                     (gnus-summary-article-number))))))
9464         (or (car extract) (cdr extract))))
9465     'gnus-thread-sort-by-author)
9466    reverse))
9467
9468 (defun gnus-summary-sort-by-subject (&optional reverse)
9469   "Sort summary buffer by subject alphabetically. `Re:'s are ignored.
9470 If case-fold-search is non-nil, case of letters is ignored.
9471 Argument REVERSE means reverse order."
9472   (interactive "P")
9473   (gnus-summary-sort
9474    (cons
9475     (lambda ()
9476       (downcase (gnus-simplify-subject (gnus-summary-subject-string))))
9477     'gnus-thread-sort-by-subject)
9478    reverse))
9479
9480 (defun gnus-summary-sort-by-date (&optional reverse)
9481   "Sort summary buffer by date.
9482 Argument REVERSE means reverse order."
9483   (interactive "P")
9484   (gnus-summary-sort
9485    (cons
9486     (lambda ()
9487       (gnus-sortable-date
9488        (header-date 
9489         (gnus-get-header-by-number (gnus-summary-article-number)))))
9490     'gnus-thread-sort-by-date)
9491    reverse))
9492
9493 (defun gnus-summary-sort-by-score (&optional reverse)
9494   "Sort summary buffer by score.
9495 Argument REVERSE means reverse order."
9496   (interactive "P")
9497   (gnus-summary-sort 
9498    (cons 'gnus-summary-article-score 'gnus-thread-sort-by-score)
9499    (not reverse)))
9500
9501 (defvar gnus-summary-already-sorted nil)
9502 (defun gnus-summary-sort (predicate reverse)
9503   ;; Sort summary buffer by PREDICATE.  REVERSE means reverse order. 
9504   (if gnus-summary-already-sorted
9505       ()
9506     (let (buffer-read-only)
9507       (if (not gnus-show-threads)
9508           (progn
9509             (goto-char (point-min))
9510             (sort-subr reverse 'forward-line 'end-of-line (car predicate)))
9511         (let ((gnus-thread-sort-functions (list (cdr predicate)))
9512               (gnus-summary-prepare-hook nil)
9513               (gnus-summary-already-sorted nil))
9514           (gnus-summary-prepare)
9515           (and gnus-show-threads
9516                gnus-thread-hide-subtree
9517                (gnus-summary-hide-all-threads))
9518           ;; If in async mode, we send some info to the backend.
9519           (and gnus-newsgroup-async
9520                (setq gnus-newsgroup-threads (nreverse gnus-newsgroup-threads))
9521                (gnus-request-asynchronous 
9522                 gnus-newsgroup-name
9523                 (if (and gnus-asynchronous-article-function
9524                          (fboundp gnus-asynchronous-article-function))
9525                     (funcall gnus-asynchronous-article-function
9526                              gnus-newsgroup-threads)))))))))
9527
9528   
9529 (defun gnus-sortable-date (date)
9530   "Make sortable string by string-lessp from DATE.
9531 Timezone package is used."
9532   (let* ((date   (timezone-fix-time date nil nil)) ;[Y M D H M S]
9533          (year   (aref date 0))
9534          (month  (aref date 1))
9535          (day    (aref date 2)))
9536     (timezone-make-sortable-date 
9537      year month day 
9538      (timezone-make-time-string
9539       (aref date 3) (aref date 4) (aref date 5)))))
9540
9541
9542 ;; Summary saving commands.
9543
9544 (defun gnus-summary-save-article (n)
9545   "Save the current article using the default saver function.
9546 If N is a positive number, save the N next articles.
9547 If N is a negative number, save the N previous articles.
9548 If N is nil and any articles have been marked with the process mark,
9549 save those articles instead.
9550 The variable `gnus-default-article-saver' specifies the saver function."
9551   (interactive "P")
9552   (let ((articles (gnus-summary-work-articles n)))
9553     (while articles
9554       (let ((header (gnus-get-header-by-number (car articles))))
9555         (if (vectorp header)
9556             (progn
9557               (gnus-summary-select-article t nil nil (car articles))
9558               (or gnus-save-all-headers
9559                   (gnus-article-hide-headers t))
9560               ;; Remove any X-Gnus lines.
9561               (save-excursion
9562                 (save-restriction
9563                   (set-buffer gnus-article-buffer)
9564                   (let ((buffer-read-only nil))
9565                     (goto-char (point-min))
9566                     (narrow-to-region (point) (or (search-forward "\n\n" nil t)
9567                                                   (point-max)))
9568                     (while (re-search-forward "^X-Gnus" nil t)
9569                       (beginning-of-line)
9570                       (delete-region (point)
9571                                      (progn (forward-line 1) (point))))
9572                     (widen))))
9573               (save-excursion
9574                 (if gnus-default-article-saver
9575                     (funcall gnus-default-article-saver)
9576                   (error "No default saver is defined."))))
9577           (if (assq 'name header)
9578               (gnus-copy-file (cdr (assq 'name header)))
9579             (gnus-message 1 "Article %d is unsaveable" (car articles)))))
9580       (gnus-summary-remove-process-mark (car articles))
9581       (setq articles (cdr articles)))
9582     (gnus-summary-position-cursor)
9583     n))
9584
9585 (defun gnus-summary-pipe-output (arg)
9586   "Pipe the current article to a subprocess.
9587 If N is a positive number, pipe the N next articles.
9588 If N is a negative number, pipe the N previous articles.
9589 If N is nil and any articles have been marked with the process mark,
9590 pipe those articles instead."
9591   (interactive "P")
9592   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9593     (gnus-summary-save-article arg)))
9594
9595 (defun gnus-summary-save-article-mail (arg)
9596   "Append the current article to an mail file.
9597 If N is a positive number, save the N next articles.
9598 If N is a negative number, save the N previous articles.
9599 If N is nil and any articles have been marked with the process mark,
9600 save those articles instead."
9601   (interactive "P")
9602   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9603     (gnus-summary-save-article arg)))
9604
9605 (defun gnus-summary-save-article-rmail (arg)
9606   "Append the current article to an rmail file.
9607 If N is a positive number, save the N next articles.
9608 If N is a negative number, save the N previous articles.
9609 If N is nil and any articles have been marked with the process mark,
9610 save those articles instead."
9611   (interactive "P")
9612   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9613     (gnus-summary-save-article arg)))
9614
9615 (defun gnus-summary-save-article-file (arg)
9616   "Append the current article to a file.
9617 If N is a positive number, save the N next articles.
9618 If N is a negative number, save the N previous articles.
9619 If N is nil and any articles have been marked with the process mark,
9620 save those articles instead."
9621   (interactive "P")
9622   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9623     (gnus-summary-save-article arg)))
9624
9625 (defun gnus-read-save-file-name (prompt default-name)
9626   (let ((methods gnus-split-methods)
9627         split-name)
9628     (if (not gnus-split-methods)
9629         ()
9630       (save-excursion
9631         (set-buffer gnus-article-buffer)
9632         (gnus-narrow-to-headers)
9633         (while methods
9634           (goto-char (point-min))
9635           (and (condition-case () 
9636                    (re-search-forward (car (car methods)) nil t)
9637                  (error nil))
9638                (setq split-name (cons (nth 1 (car methods)) split-name)))
9639           (setq methods (cdr methods)))
9640         (widen)))
9641     (cond ((null split-name)
9642            (read-file-name
9643             (concat prompt " (default "
9644                     (file-name-nondirectory default-name) ") ")
9645             (file-name-directory default-name)
9646             default-name))
9647           ((= 1 (length split-name))
9648            (read-file-name
9649             (concat prompt " (default " (car split-name) ") ")
9650             gnus-article-save-directory
9651             (concat gnus-article-save-directory (car split-name))))
9652           (t
9653            (setq split-name (mapcar (lambda (el) (list el))
9654                                     (nreverse split-name)))
9655            (let ((result (completing-read 
9656                           (concat prompt " ")
9657                           split-name nil nil)))
9658              (concat gnus-article-save-directory
9659                      (if (string= result "")
9660                          (car (car split-name))
9661                        result)))))))
9662
9663 (defun gnus-summary-save-in-rmail (&optional filename)
9664   "Append this article to Rmail file.
9665 Optional argument FILENAME specifies file name.
9666 Directory to save to is default to `gnus-article-save-directory' which
9667 is initialized from the SAVEDIR environment variable."
9668   (interactive)
9669   (let ((default-name
9670           (funcall gnus-rmail-save-name gnus-newsgroup-name
9671                    gnus-current-headers gnus-newsgroup-last-rmail)))
9672     (or filename
9673         (setq filename (gnus-read-save-file-name 
9674                         "Save in rmail file:" default-name)))
9675     (gnus-make-directory (file-name-directory filename))
9676     (gnus-eval-in-buffer-window 
9677      gnus-article-buffer
9678      (save-excursion
9679        (save-restriction
9680          (widen)
9681          (gnus-output-to-rmail filename))))
9682     ;; Remember the directory name to save articles.
9683     (setq gnus-newsgroup-last-rmail filename)))
9684
9685 (defun gnus-summary-save-in-mail (&optional filename)
9686   "Append this article to Unix mail file.
9687 Optional argument FILENAME specifies file name.
9688 Directory to save to is default to `gnus-article-save-directory' which
9689 is initialized from the SAVEDIR environment variable."
9690   (interactive)
9691   (let ((default-name
9692           (funcall gnus-mail-save-name gnus-newsgroup-name
9693                    gnus-current-headers gnus-newsgroup-last-mail)))
9694     (or filename
9695         (setq filename (gnus-read-save-file-name 
9696                         "Save in Unix mail file:" default-name)))
9697     (setq filename
9698           (expand-file-name filename
9699                             (and default-name
9700                                  (file-name-directory default-name))))
9701     (gnus-make-directory (file-name-directory filename))
9702     (gnus-eval-in-buffer-window 
9703      gnus-article-buffer
9704      (save-excursion
9705        (save-restriction
9706          (widen)
9707          (if (and (file-readable-p filename) (rmail-file-p filename))
9708              (gnus-output-to-rmail filename)
9709            (rmail-output filename 1 t t)))))
9710     ;; Remember the directory name to save articles.
9711     (setq gnus-newsgroup-last-mail filename)))
9712
9713 (defun gnus-summary-save-in-file (&optional filename)
9714   "Append this article to file.
9715 Optional argument FILENAME specifies file name.
9716 Directory to save to is default to `gnus-article-save-directory' which
9717 is initialized from the SAVEDIR environment variable."
9718   (interactive)
9719   (let ((default-name
9720           (funcall gnus-file-save-name gnus-newsgroup-name
9721                    gnus-current-headers gnus-newsgroup-last-file)))
9722     (or filename
9723         (setq filename (gnus-read-save-file-name 
9724                         "Save in file:" default-name)))
9725     (gnus-make-directory (file-name-directory filename))
9726     (gnus-eval-in-buffer-window 
9727      gnus-article-buffer
9728      (save-excursion
9729        (save-restriction
9730          (widen)
9731          (gnus-output-to-file filename))))
9732     ;; Remember the directory name to save articles.
9733     (setq gnus-newsgroup-last-file filename)))
9734
9735 (defun gnus-summary-save-in-pipe (&optional command)
9736   "Pipe this article to subprocess."
9737   (interactive)
9738   (let ((command (read-string "Shell command on article: "
9739                               gnus-last-shell-command)))
9740     (if (string-equal command "")
9741         (setq command gnus-last-shell-command))
9742     (gnus-eval-in-buffer-window 
9743      gnus-article-buffer
9744      (save-restriction
9745        (widen)
9746        (shell-command-on-region (point-min) (point-max) command nil)))
9747     (setq gnus-last-shell-command command)))
9748
9749 ;; Summary extract commands
9750
9751 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
9752   (let ((buffer-read-only nil)
9753         (article (gnus-summary-article-number))
9754         b)
9755     (or (gnus-summary-goto-subject article)
9756         (error (format "No such article: %d" article)))
9757     (or gnus-newsgroup-headers-hashtb-by-number
9758         (gnus-make-headers-hashtable-by-number))
9759     (gnus-summary-position-cursor)
9760     ;; If all commands are to be bunched up on one line, we collect
9761     ;; them here.  
9762     (if gnus-view-pseudos-separately
9763         ()
9764       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9765             files action)
9766         (while ps
9767           (setq action (cdr (assq 'action (car ps))))
9768           (setq files (list (cdr (assq 'name (car ps)))))
9769           (while (and ps (cdr ps)
9770                       (string= (or action "1")
9771                                (or (cdr (assq 'action (car (cdr ps)))) "2")))
9772             (setq files (cons (cdr (assq 'name (car (cdr ps)))) files))
9773             (setcdr ps (cdr (cdr ps))))
9774           (if (not files)
9775               ()
9776             (if (not (string-match "%s" action))
9777                 (setq files (cons " " files)))
9778             (setq files (cons " " files))
9779             (and (assq 'execute (car ps))
9780                  (setcdr (assq 'execute (car ps))
9781                          (funcall (if (string-match "%s" action)
9782                                       'format 'concat)
9783                                   action 
9784                                   (mapconcat (lambda (f) f) files " ")))))
9785           (setq ps (cdr ps)))))
9786     (if (and gnus-view-pseudos (not not-view))
9787         (while pslist
9788           (and (assq 'execute (car pslist))
9789                (gnus-execute-command (cdr (assq 'execute (car pslist)))
9790                                      (eq gnus-view-pseudos 'not-confirm)))
9791           (setq pslist (cdr pslist)))
9792       (save-excursion
9793         (while pslist
9794           (gnus-summary-goto-subject (or (cdr (assq 'article (car pslist)))
9795                                          (gnus-summary-article-number)))
9796           (forward-line 1)
9797           (setq b (point))
9798           (insert "          " (file-name-nondirectory 
9799                                 (cdr (assq 'name (car pslist))))
9800                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9801           (add-text-properties 
9802            b (1+ b) (list 'gnus-number gnus-reffed-article-number
9803                           'gnus-mark gnus-unread-mark 
9804                           'gnus-level 0
9805                           'gnus-pseudo (car pslist)))
9806           (forward-line -1)
9807           (gnus-sethash (int-to-string gnus-reffed-article-number)
9808                         (car pslist) gnus-newsgroup-headers-hashtb-by-number)
9809           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9810           (setq pslist (cdr pslist)))))))
9811
9812 (defun gnus-pseudos< (p1 p2)
9813   (let ((c1 (cdr (assq 'action p1)))
9814         (c2 (cdr (assq 'action p2))))
9815     (and c1 c2 (string< c1 c2))))
9816
9817 (defun gnus-request-pseudo-article (props)
9818   (cond ((assq 'execute props)
9819          (gnus-execute-command (cdr (assq 'execute props)))))
9820   (let ((gnus-current-article (gnus-summary-article-number)))
9821     (run-hooks 'gnus-mark-article-hook)))
9822
9823 (defun gnus-execute-command (command &optional automatic)
9824   (save-excursion
9825     (gnus-article-setup-buffer)
9826     (set-buffer gnus-article-buffer)
9827     (let ((command (if automatic command (read-string "Command: " command)))
9828           (buffer-read-only nil))
9829       (erase-buffer)
9830       (insert "$ " command "\n\n")
9831       (if gnus-view-pseudo-asynchronously
9832           (start-process "gnus-execute" nil "sh" "-c" command)
9833         (call-process "sh" nil t nil "-c" command)))))
9834
9835 (defun gnus-copy-file (file &optional to)
9836   "Copy FILE to TO."
9837   (interactive
9838    (list (read-file-name "Copy file: " default-directory)
9839          (read-file-name "Copy file to: " default-directory)))
9840   (or to (setq to (read-file-name "Copy file to: " default-directory)))
9841   (and (file-directory-p to) 
9842        (setq to (concat (file-name-as-directory to)
9843                         (file-name-nondirectory file))))
9844   (copy-file file to))
9845
9846 ;; Summary kill commands.
9847
9848 (defun gnus-summary-edit-global-kill (article)
9849   "Edit the \"global\" kill file."
9850   (interactive (list (gnus-summary-article-number)))
9851   (gnus-group-edit-global-kill article))
9852
9853 (defun gnus-summary-edit-local-kill ()
9854   "Edit a local kill file applied to the current newsgroup."
9855   (interactive)
9856   (setq gnus-current-headers 
9857         (gnus-gethash 
9858          (int-to-string (gnus-summary-article-number))
9859          gnus-newsgroup-headers-hashtb-by-number))
9860   (gnus-set-global-variables)
9861   (gnus-group-edit-local-kill 
9862    (gnus-summary-article-number) gnus-newsgroup-name))
9863
9864 \f
9865 ;;;
9866 ;;; Gnus article mode
9867 ;;;
9868
9869 (put 'gnus-article-mode 'mode-class 'special)
9870
9871 (if gnus-article-mode-map
9872     nil
9873   (setq gnus-article-mode-map (make-keymap))
9874   (suppress-keymap gnus-article-mode-map)
9875   (define-key gnus-article-mode-map " " 'gnus-article-next-page)
9876   (define-key gnus-article-mode-map "\177" 'gnus-article-prev-page)
9877   (define-key gnus-article-mode-map "\C-c^" 'gnus-article-refer-article)
9878   (define-key gnus-article-mode-map "h" 'gnus-article-show-summary)
9879   (define-key gnus-article-mode-map "s" 'gnus-article-show-summary)
9880   (define-key gnus-article-mode-map "\C-c\C-m" 'gnus-article-mail)
9881   (define-key gnus-article-mode-map "\C-c\C-M" 'gnus-article-mail-with-original)
9882   (define-key gnus-article-mode-map "?" 'gnus-article-describe-briefly)
9883   (define-key gnus-article-mode-map gnus-mouse-2 'gnus-article-push-button)
9884   (define-key gnus-article-mode-map "\r" 'gnus-article-press-button)
9885   (define-key gnus-article-mode-map "\t" 'gnus-article-next-button)
9886   (define-key gnus-article-mode-map "\C-c\C-b" 'gnus-bug)
9887   
9888   ;; Duplicate almost all summary keystrokes in the article mode map.
9889   (let ((commands 
9890          (list 
9891           " " "\177" "\r" "n" "p" "N" "P" "\M-\C-n" "\M-\C-p"
9892           "\M-n" "\M-p" "." "," "\M-s" "\M-r" "<" ">" "j" "^" "\M-^"
9893           "u" "!" "U" "d" "D" "E" "\M-u" "\M-U" "k" "\C-k" "\M-\C-k"
9894           "\M-\C-l" "e" "#" "\M-#" "\M-\C-t" "\M-\C-s" "\M-\C-h"
9895           "\M-\C-f" "\M-\C-b" "\M-\C-u" "\M-\C-d" "&" "\C-w"
9896           "\C-t" "?" "\C-c\M-\C-s" "\C-c\C-s\C-n" "\C-c\C-s\C-a"
9897           "\C-c\C-s\C-s" "\C-c\C-s\C-d" "\C-c\C-s\C-i" "\C-x\C-s"
9898           "\M-g" "w" "\C-c\C-r" "\M-t" "C"
9899           "o" "\C-o" "|" "\M-k" "\M-K" "V" "\C-c\C-d"
9900           "\C-c\C-i" "x" "X" "s" "t" "g" "?" "l"
9901           "\C-c\C-v\C-v" "\C-d" "v" 
9902 ;;        "Mt" "M!" "Md" "Mr"
9903 ;;        "Mc" "M " "Me" "Mx" "M?" "Mb" "MB" "M#" "M\M-#" "M\M-r"
9904 ;;        "M\M-\C-r" "MD" "M\M-D" "MS" "MC" "MH" "M\C-c" "Mk" "MK"
9905 ;;        "Ms" "Mc" "Mu" "Mm" "Mk" "Gn" "Gp" "GN" "GP" "G\C-n" "G\C-p"
9906 ;;        "G\M-n" "G\M-p" "Gf" "Gb" "Gg" "Gl" "Gp" "Tk" "Tl" "Ti" "TT"
9907 ;;        "Ts" "TS" "Th" "TH" "Tn" "Tp" "Tu" "Td" "T#" "A " "An" "A\177" "Ap"
9908 ;;        "A\r" "A<" "A>" "Ab" "Ae" "A^" "Ar" "Aw" "Ac" "Ag" "At" "Am"
9909 ;;        "As" "Wh" "Ws" "Wc" "Wo" "Ww" "Wd" "Wq" "Wf" "Wt" "W\C-t"
9910 ;;        "WT" "WA" "Wa" "WH" "WC" "WS" "Wb" "Hv" "Hf" "Hd" "Hh" "Hi"
9911 ;;        "Be" "B\177" "Bm" "Br" "Bw" "Bc" "Bq" "Bi" "Oo" "Om" "Or"
9912 ;;        "Of" "Oh" "Ov" "Op" "Vu" "V\C-s" "V\C-r" "Vr" "V&" "VT" "Ve"
9913 ;;        "VD" "Vk" "VK" "Vsn" "Vsa" "Vss" "Vsd" "Vsi"
9914           )))
9915     (while commands
9916       (define-key gnus-article-mode-map (car commands) 
9917         'gnus-article-summary-command)
9918       (setq commands (cdr commands))))
9919
9920   (let ((commands (list "q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
9921 ;;                      "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP" 
9922                          "=")))
9923     (while commands
9924       (define-key gnus-article-mode-map (car commands) 
9925         'gnus-article-summary-command-nosave)
9926       (setq commands (cdr commands)))))
9927
9928
9929 (defun gnus-article-mode ()
9930   "Major mode for displaying an article.
9931
9932 All normal editing commands are switched off.
9933
9934 The following commands are available:
9935
9936 \\<gnus-article-mode-map>
9937 \\[gnus-article-next-page]\t Scroll the article one page forwards
9938 \\[gnus-article-prev-page]\t Scroll the article one page backwards
9939 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
9940 \\[gnus-article-show-summary]\t Display the summary buffer
9941 \\[gnus-article-mail]\t Send a reply to the address near point
9942 \\[gnus-article-mail-with-original]\t Send a reply to the address near point; include the original article
9943 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
9944 \\[gnus-info-find-node]\t Go to the Gnus info node"
9945   (interactive)
9946   (if gnus-visual (gnus-article-make-menu-bar))
9947   (kill-all-local-variables)
9948   (setq mode-line-modified "-- ")
9949   (make-local-variable 'mode-line-format)
9950   (setq mode-line-format (copy-sequence mode-line-format))
9951   (and (equal (nth 3 mode-line-format) "   ")
9952        (setcar (nthcdr 3 mode-line-format) ""))
9953   (setq mode-name "Article")
9954   (setq major-mode 'gnus-article-mode)
9955   (make-local-variable 'minor-mode-alist)
9956   (or (assq 'gnus-show-mime minor-mode-alist)
9957       (setq minor-mode-alist
9958             (cons (list 'gnus-show-mime " MIME") minor-mode-alist)))
9959   (use-local-map gnus-article-mode-map)
9960   (make-local-variable 'page-delimiter)
9961   (setq page-delimiter gnus-page-delimiter)
9962   (buffer-disable-undo (current-buffer))
9963   (setq buffer-read-only t)             ;Disable modification
9964   (run-hooks 'gnus-article-mode-hook))
9965
9966 (defun gnus-article-setup-buffer ()
9967   "Initialize article mode buffer."
9968   (if (get-buffer gnus-article-buffer)
9969       (save-excursion
9970         (set-buffer gnus-article-buffer)
9971         (buffer-disable-undo)
9972         (setq buffer-read-only t)
9973         (gnus-add-current-to-buffer-list)
9974         (or (eq major-mode 'gnus-article-mode)
9975             (gnus-article-mode)))
9976     (save-excursion
9977       (set-buffer (get-buffer-create gnus-article-buffer))
9978       (gnus-add-current-to-buffer-list)
9979       (gnus-article-mode))))
9980
9981 ;; Set article window start at LINE, where LINE is the number of lines
9982 ;; from the head of the article.
9983 (defun gnus-article-set-window-start (&optional line)
9984   (set-window-start 
9985    (get-buffer-window gnus-article-buffer)
9986    (save-excursion
9987      (set-buffer gnus-article-buffer)
9988      (goto-char (point-min))
9989      (if (not line)
9990          (point-min)
9991        (gnus-message 6 "Moved to bookmark")
9992        (search-forward "\n\n" nil t)
9993        (forward-line line)
9994        (point)))))
9995
9996 (defun gnus-request-article-this-buffer (article group)
9997   "Get an article and insert it into this buffer."
9998   (setq group (or group gnus-newsgroup-name))
9999
10000   ;; Open server if it has closed.
10001   (gnus-check-news-server (gnus-find-method-for-group group))
10002
10003   ;; Using `gnus-request-article' directly will insert the article into
10004   ;; `nntp-server-buffer' - so we'll save some time by not having to
10005   ;; copy it from the server buffer into the article buffer.
10006
10007   ;; We only request an article by message-id when we do not have the
10008   ;; headers for it, so we'll have to get those.
10009   (and (stringp article) 
10010        (let ((gnus-override-method gnus-refer-article-method))
10011          (gnus-read-header article)))
10012
10013   ;; If the article number is negative, that means that this article
10014   ;; doesn't belong in this newsgroup (possibly), so we find its
10015   ;; message-id and request it by id instead of number.
10016   (if (not (numberp article))
10017       ()
10018     (save-excursion
10019       (set-buffer gnus-summary-buffer)
10020       (let ((header (gnus-get-header-by-number article)))
10021         (if (< article 0)
10022             (if (vectorp header)
10023                 ;; It's a real article.
10024                 (setq article (header-id header))
10025               ;; It is an extracted pseudo-article.
10026               (setq article 'pseudo)
10027               (gnus-request-pseudo-article header)))
10028
10029         (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
10030           (if (not (eq (car method) 'nneething))
10031               ()
10032             (let ((dir (concat (file-name-as-directory (nth 1 method))
10033                                (header-subject header))))
10034               (if (file-directory-p dir)
10035                   (progn
10036                     (setq article 'nneething)
10037                     (gnus-group-enter-directory dir)))))))))
10038
10039   ;; Check the cache.
10040   (if (and gnus-use-cache
10041            (numberp article)
10042            (gnus-cache-request-article article group))
10043       'article
10044     ;; Get the article and into the article buffer.
10045     (if (or (stringp article) (numberp article))
10046         (progn
10047           (erase-buffer)
10048           (let ((gnus-override-method 
10049                  (and (stringp article) gnus-refer-article-method)))
10050             (and (gnus-request-article article group (current-buffer))
10051                  'article)))
10052       article)))
10053
10054 (defun gnus-read-header (id)
10055   "Read the headers of article ID and enter them into the Gnus system."
10056   (or gnus-newsgroup-headers-hashtb-by-number
10057       (gnus-make-headers-hashtable-by-number))
10058   (let (header)
10059     (if (not (setq header 
10060                    (car (if (let ((gnus-nov-is-evil t))
10061                               (gnus-retrieve-headers 
10062                                (list id) gnus-newsgroup-name))
10063                             (gnus-get-newsgroup-headers)))))
10064         nil
10065       (if (stringp id)
10066           (header-set-number header gnus-reffed-article-number))
10067       (setq gnus-newsgroup-headers (cons header gnus-newsgroup-headers))
10068       (gnus-sethash (int-to-string (header-number header)) header
10069                     gnus-newsgroup-headers-hashtb-by-number)
10070       (if (stringp id)
10071           (setq gnus-reffed-article-number (1- gnus-reffed-article-number)))
10072       (setq gnus-current-headers header)
10073       header)))
10074
10075 (defun gnus-article-prepare (article &optional all-headers header)
10076   "Prepare ARTICLE in article mode buffer.
10077 ARTICLE should either be an article number or a Message-ID.
10078 If ARTICLE is an id, HEADER should be the article headers.
10079 If ALL-HEADERS is non-nil, no headers are hidden."
10080   (save-excursion
10081     ;; Make sure we start in a summary buffer.
10082     (or (eq major-mode 'gnus-summary-mode)
10083         (set-buffer gnus-summary-buffer))
10084     (setq gnus-summary-buffer (current-buffer))
10085     ;; Make sure the connection to the server is alive.
10086     (or (gnus-server-opened (gnus-find-method-for-group gnus-newsgroup-name))
10087         (progn
10088           (gnus-check-news-server 
10089            (gnus-find-method-for-group gnus-newsgroup-name))
10090           (gnus-request-group gnus-newsgroup-name t)))
10091     (or gnus-newsgroup-headers-hashtb-by-number
10092         (gnus-make-headers-hashtable-by-number))
10093     (let* ((article (if header (header-number header) article))
10094            (summary-buffer (current-buffer))
10095            (internal-hook gnus-article-internal-prepare-hook)
10096            (group gnus-newsgroup-name)
10097            result)
10098       (save-excursion
10099         (gnus-article-setup-buffer)
10100         (set-buffer gnus-article-buffer)
10101         (if (not (setq result (let ((buffer-read-only nil))
10102                                 (gnus-request-article-this-buffer 
10103                                  article group))))
10104             ;; There is no such article.
10105             (save-excursion
10106               (if (not (numberp article))
10107                   ()
10108                 (setq gnus-article-current 
10109                       (cons gnus-newsgroup-name article))
10110                 (set-buffer gnus-summary-buffer)
10111                 (setq gnus-current-article article)
10112                 (gnus-summary-mark-as-read article gnus-canceled-mark))
10113               (gnus-message 1 "No such article (may be canceled)")
10114               (ding)
10115               nil)
10116           (if (or (eq result 'pseudo) (eq result 'nneething))
10117               (progn
10118                 (save-excursion
10119                   (set-buffer summary-buffer)
10120                   (setq gnus-last-article gnus-current-article
10121                         gnus-newsgroup-history (cons gnus-current-article
10122                                                      gnus-newsgroup-history)
10123                         gnus-current-article 0
10124                         gnus-current-headers nil
10125                         gnus-article-current nil)
10126                   (if (eq result 'nneething)
10127                       (gnus-configure-windows 'summary)
10128                     (gnus-configure-windows 'article))
10129                   (gnus-set-global-variables))
10130                 (gnus-set-mode-line 'article))
10131             ;; The result from the `request' was an actual article -
10132             ;; or at least some text that is now displayed in the
10133             ;; article buffer.
10134             (if (and (numberp article)
10135                      (not (eq article gnus-current-article)))
10136                 ;; Seems like a new article has been selected.
10137                 ;; `gnus-current-article' must be an article number.
10138                 (save-excursion
10139                   (set-buffer summary-buffer)
10140                   (setq gnus-last-article gnus-current-article
10141                         gnus-newsgroup-history (cons gnus-current-article
10142                                                      gnus-newsgroup-history)
10143                         gnus-current-article article
10144                         gnus-current-headers 
10145                         (gnus-get-header-by-number gnus-current-article)
10146                         gnus-article-current 
10147                         (cons gnus-newsgroup-name gnus-current-article))
10148                   (gnus-summary-show-thread)
10149                   (run-hooks 'gnus-mark-article-hook)
10150                   (gnus-set-mode-line 'summary)
10151                   (and gnus-visual 
10152                        (run-hooks 'gnus-visual-mark-article-hook))
10153                   ;; Set the global newsgroup variables here.
10154                   ;; Suggested by Jim Sisolak
10155                   ;; <sisolak@trans4.neep.wisc.edu>.
10156                   (gnus-set-global-variables)
10157                   (and gnus-use-cache 
10158                        (gnus-cache-possibly-enter-article
10159                         group article
10160                         (gnus-get-header-by-number article)
10161                         (memq article gnus-newsgroup-marked)
10162                         (memq article gnus-newsgroup-dormant)
10163                         (memq article gnus-newsgroup-unreads)))))
10164             ;; gnus-have-all-headers must be either T or NIL.
10165             (setq gnus-have-all-headers
10166                   (not (not (or all-headers gnus-show-all-headers))))
10167             ;; Hooks for getting information from the article.
10168             ;; This hook must be called before being narrowed.
10169             (let (buffer-read-only)
10170               (run-hooks 'internal-hook)
10171               (run-hooks 'gnus-article-prepare-hook)
10172               ;; Decode MIME message.
10173               (if (and gnus-show-mime
10174                        (or (not gnus-strict-mime)
10175                            (gnus-fetch-field "Mime-Version")))
10176                   (funcall gnus-show-mime-method))
10177               ;; Perform the article display hooks.
10178               (run-hooks 'gnus-article-display-hook))
10179             ;; Do page break.
10180             (goto-char (point-min))
10181             (and gnus-break-pages (gnus-narrow-to-page))
10182             (gnus-set-mode-line 'article)
10183             (gnus-configure-windows 'article)
10184             (goto-char (point-min))
10185             t))))))
10186
10187 (defun gnus-article-show-all-headers ()
10188   "Show all article headers in article mode buffer."
10189   (save-excursion 
10190     (gnus-article-setup-buffer)
10191     (set-buffer gnus-article-buffer)
10192     (let ((buffer-read-only nil))
10193       (remove-text-properties (point-min) (point-max) 
10194                               gnus-hidden-properties))))
10195
10196 (defun gnus-article-hide-headers-if-wanted ()
10197   "Hide unwanted headers if `gnus-have-all-headers' is nil.
10198 Provided for backwards compatability."
10199   (or (save-excursion (set-buffer gnus-summary-buffer) gnus-have-all-headers)
10200       (gnus-article-hide-headers)))
10201
10202 (defun gnus-article-hide-headers (&optional delete)
10203   "Hide unwanted headers and possibly sort them as well."
10204   (interactive "P")
10205   (save-excursion
10206     (set-buffer gnus-article-buffer)
10207     (save-restriction
10208       (let ((sorted gnus-sorted-header-list)
10209             (buffer-read-only nil)
10210             want want-list beg want-l)
10211         ;; First we narrow to just the headers.
10212         (widen)
10213         (goto-char (point-min))
10214         ;; Hide any "From " lines at the beginning of (mail) articles. 
10215         (while (looking-at "From ")
10216           (forward-line 1))
10217         (if (bobp) 
10218             (add-text-properties (point-min) (point) gnus-hidden-properties))
10219         ;; Then treat the rest of the header lines.
10220         (narrow-to-region 
10221          (point) 
10222          (progn (search-forward "\n\n" nil t) (forward-line -1) (point)))
10223         ;; Then we use the two regular expressions
10224         ;; `gnus-ignored-headers' and `gnus-visible-headers' to
10225         ;; select which header lines is to remain visible in the
10226         ;; article buffer.
10227         (goto-char (point-min))
10228         (while (re-search-forward "^[^ \t]*:" nil t)
10229           (beginning-of-line)
10230           ;; We add the headers we want to keep to a list and delete
10231           ;; them from the buffer.
10232           (if (or (and (stringp gnus-visible-headers)
10233                        (looking-at gnus-visible-headers))
10234                   (and (not (stringp gnus-visible-headers))
10235                        (stringp gnus-ignored-headers)
10236                        (not (looking-at gnus-ignored-headers))))
10237               (progn
10238                 (setq beg (point))
10239                 (forward-line 1)
10240                 ;; Be sure to get multi-line headers...
10241                 (re-search-forward "^[^ \t]*:" nil t)
10242                 (beginning-of-line)
10243                 (setq want-list 
10244                       (cons (buffer-substring beg (point)) want-list))
10245                 (delete-region beg (point))
10246                 (goto-char beg))
10247             (forward-line 1)))
10248         ;; Next we perform the sorting by looking at
10249         ;; `gnus-sorted-header-list'. 
10250         (goto-char (point-min))
10251         (while (and sorted want-list)
10252           (setq want-l want-list)
10253           (while (and want-l
10254                       (not (string-match (car sorted) (car want-l))))
10255             (setq want-l (cdr want-l)))
10256           (if want-l 
10257               (progn
10258                 (insert (car want-l))
10259                 (setq want-list (delq (car want-l) want-list))))
10260           (setq sorted (cdr sorted)))
10261         ;; Any headers that were not matched by the sorted list we
10262         ;; just tack on the end of the visible header list.
10263         (while want-list
10264           (insert (car want-list))
10265           (setq want-list (cdr want-list)))
10266         ;; And finally we make the unwanted headers invisible.
10267         (if delete
10268             (delete-region (point) (point-max))
10269           ;; Suggested by Sudish Joseph <joseph@cis.ohio-state.edu>.
10270           (add-text-properties (point) (point-max) gnus-hidden-properties))))))
10271
10272 ;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.
10273 (defun gnus-article-treat-overstrike ()
10274   "Translate overstrikes into bold text."
10275   (interactive)
10276   (save-excursion
10277     (set-buffer gnus-article-buffer)
10278     (let ((buffer-read-only nil))
10279       (while (search-forward "\b" nil t)
10280         (let ((next (following-char))
10281               (previous (char-after (- (point) 2))))
10282           (cond ((eq next previous)
10283                  (delete-region (- (point) 2) (point))
10284                  (put-text-property (point) (1+ (point))
10285                                     'face 'bold))
10286                 ((eq next ?_)
10287                  (delete-region (1- (point)) (1+ (point)))
10288                  (put-text-property (1- (point)) (point)
10289                                     'face 'underline))
10290                 ((eq previous ?_)
10291                  (delete-region (- (point) 2) (point))
10292                  (put-text-property (point) (1+ (point))
10293                                     'face 'underline))))))))
10294
10295 (defun gnus-article-word-wrap ()
10296   "Format too long lines."
10297   (interactive)
10298   (save-excursion
10299     (set-buffer gnus-article-buffer)
10300     (let ((buffer-read-only nil))
10301       (goto-char (point-min))
10302       (search-forward "\n\n" nil t)
10303       (end-of-line 1)
10304       (let ((paragraph-start "^[>|#:<;* ]*[ \t]*$")
10305             (adaptive-fill-regexp "[ \t]*\\([|#:<;>*]+ *\\)?")
10306             (adaptive-fill-mode t))
10307         (while (not (eobp))
10308           (and (>= (current-column) (min fill-column (window-width)))
10309                (/= (preceding-char) ?:)
10310                (fill-paragraph nil))
10311           (end-of-line 2))))))
10312
10313 (defun gnus-article-remove-cr ()
10314   "Remove carriage returns from an article."
10315   (interactive)
10316   (save-excursion
10317     (set-buffer gnus-article-buffer)
10318     (let ((buffer-read-only nil))
10319       (goto-char (point-min))
10320       (while (search-forward "\r" nil t)
10321         (replace-match "" t t)))))
10322
10323 (defun gnus-article-display-x-face (&optional force)
10324   "Look for an X-Face header and display it if present."
10325   (interactive (list 'force))
10326   (save-excursion
10327     (set-buffer gnus-article-buffer)
10328     (let ((inhibit-point-motion-hooks t)
10329           (case-fold-search nil))
10330       (save-restriction
10331         (goto-char (point-min))
10332         (search-forward "\n\n")
10333         (narrow-to-region (point-min) (point))
10334         (goto-char (point-min))
10335         (if (or (not gnus-article-x-face-command)
10336                 (and (not force)
10337                      (or (not gnus-article-x-face-too-ugly)
10338                          (string-match gnus-article-x-face-too-ugly
10339                                        (mail-fetch-field "from"))))
10340                 (progn
10341                   (goto-char (point-min))
10342                   (not (re-search-forward "^X-Face: " nil t))))
10343             nil
10344           (let ((beg (point))
10345                 (end (1- (re-search-forward "^\\($\\|[^ \t]\\)" nil t))))
10346             (if (symbolp gnus-article-x-face-command)
10347                 (and (or (fboundp gnus-article-x-face-command)
10348                          (error "%s is not a function"
10349                                 gnus-article-x-face-command))
10350                      (funcall gnus-article-x-face-command beg end))
10351               (call-process-region beg end "sh" nil 0 nil
10352                                    "-c" gnus-article-x-face-command))))))))
10353
10354 (defun gnus-article-de-quoted-unreadable (&optional force)
10355   "Do a naïve translation of a quoted-printable-encoded article.
10356 This is in no way, shape or form meant as a replacement for real MIME
10357 processing, but is simply a stop-gap measure until MIME support is
10358 written.
10359 If FORCE, decode the article whether it is marked as quoted-printable
10360 or not." 
10361   (interactive (list 'force))
10362   (save-excursion
10363     (set-buffer gnus-article-buffer)
10364     (let ((case-fold-search t)
10365           (buffer-read-only nil)
10366           (type (gnus-fetch-field "content-transfer-encoding")))
10367       (if (or force (and type (string-match "quoted-printable" type)))
10368           (progn
10369             (goto-char (point-min))
10370             (search-forward "\n\n" nil 'move)
10371             (gnus-mime-decode-quoted-printable (point) (point-max)))))))
10372
10373 (defun gnus-mime-decode-quoted-printable (from to)
10374   ;; Decode quoted-printable from region between FROM and TO.
10375   (save-excursion
10376     (goto-char from)
10377     (while (search-forward "=" to t)
10378       (cond ((eq (following-char) ?\n)
10379              (delete-char -1)
10380              (delete-char 1))
10381             ((looking-at "[0-9A-F][0-9A-F]")
10382              (delete-char -1)
10383              (insert (hexl-hex-string-to-integer
10384                       (buffer-substring (point) (+ 2 (point)))))
10385              (delete-char 2))
10386             ((gnus-message 3 "Malformed MIME quoted-printable message"))))))
10387
10388 (defvar gnus-article-time-units
10389   (list (cons 'year (* 365.25 24 60 60))
10390         (cons 'week (* 7 24 60 60))
10391         (cons 'day (* 24 60 60))
10392         (cons 'hour (* 60 60))
10393         (cons 'minute 60)
10394         (cons 'second 1)))
10395
10396 (defun gnus-article-date-ut (&optional type)
10397   "Convert DATE date to universal time in the current article.
10398 If TYPE is `local', convert to local time; if it is `lapsed', output
10399 how much time has lapsed since DATE."
10400   (interactive (list 'ut))
10401   (let ((date (header-date (or gnus-current-headers 
10402                                (gnus-get-header-by-number
10403                                 (gnus-summary-article-number))"")))
10404         (date-regexp "^Date: \\|^X-Sent: "))
10405     (if (or (not date)
10406             (string= date ""))
10407         ()
10408       (save-excursion
10409         (set-buffer gnus-article-buffer)
10410         (let ((buffer-read-only nil))
10411           (goto-char (point-min))
10412           (if (and (re-search-forward date-regexp nil t)
10413                    (progn 
10414                      (beginning-of-line)
10415                      (looking-at date-regexp)))
10416               (delete-region (gnus-point-at-bol)
10417                              (progn (end-of-line) (1+ (point))))
10418             (goto-char (point-min))
10419             (goto-char (- (search-forward "\n\n") 2)))
10420           (insert
10421            (cond 
10422             ((eq type 'local)
10423              (concat "Date: " (condition-case ()
10424                                   (timezone-make-date-arpa-standard date)
10425                                 (error date))
10426                      "\n"))
10427             ((eq type 'ut)
10428              (concat "Date: "
10429                      (condition-case ()
10430                          (timezone-make-date-arpa-standard date nil "UT")
10431                        (error date))
10432                      "\n"))
10433             ((eq type 'lapsed)
10434              ;; If the date is seriously mangled, the timezone
10435              ;; functions are liable to bug out, so we condition-case
10436              ;; the entire thing.  
10437              (let* ((sec (condition-case ()
10438                              (max (- (gnus-seconds-since-epoch 
10439                                       (timezone-make-date-arpa-standard
10440                                        (current-time-string) 
10441                                        (current-time-zone) "UT"))
10442                                      (gnus-seconds-since-epoch 
10443                                       (timezone-make-date-arpa-standard 
10444                                        date nil "UT")))
10445                                   0)
10446                            (error 0)))
10447                     num prev)
10448                (concat
10449                 "X-Sent: "
10450                 (mapconcat 
10451                  (lambda (unit)
10452                    (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
10453                        ""
10454                      (setq sec (- sec (* num (cdr unit))))
10455                      (prog1
10456                          (concat (if prev ", " "") (int-to-string (floor num))
10457                                  " " (symbol-name (car unit))
10458                                  (if (> num 1) "s" ""))
10459                        (setq prev t))))
10460                  gnus-article-time-units "")
10461                 " ago\n")))
10462             (t
10463              (error "Unknown conversion type: %s" type)))))))))
10464
10465 (defun gnus-article-date-local ()
10466   "Convert the current article date to the local timezone."
10467   (interactive)
10468   (gnus-article-date-ut 'local))
10469
10470 (defun gnus-article-date-lapsed ()
10471   "Convert the current article date to time lapsed since it was sent."
10472   (interactive)
10473   (gnus-article-date-ut 'lapsed))
10474
10475 (defun gnus-article-maybe-highlight ()
10476   (if gnus-visual (gnus-article-highlight)))
10477
10478 ;; Article savers.
10479
10480 (defun gnus-output-to-rmail (file-name)
10481   "Append the current article to an Rmail file named FILE-NAME."
10482   (require 'rmail)
10483   ;; Most of these codes are borrowed from rmailout.el.
10484   (setq file-name (expand-file-name file-name))
10485   (setq rmail-default-rmail-file file-name)
10486   (let ((artbuf (current-buffer))
10487         (tmpbuf (get-buffer-create " *Gnus-output*")))
10488     (save-excursion
10489       (or (get-file-buffer file-name)
10490           (file-exists-p file-name)
10491           (if (gnus-yes-or-no-p
10492                (concat "\"" file-name "\" does not exist, create it? "))
10493               (let ((file-buffer (create-file-buffer file-name)))
10494                 (save-excursion
10495                   (set-buffer file-buffer)
10496                   (rmail-insert-rmail-file-header)
10497                   (let ((require-final-newline nil))
10498                     (write-region (point-min) (point-max) file-name t 1)))
10499                 (kill-buffer file-buffer))
10500             (error "Output file does not exist")))
10501       (set-buffer tmpbuf)
10502       (buffer-disable-undo (current-buffer))
10503       (erase-buffer)
10504       (insert-buffer-substring artbuf)
10505       (gnus-convert-article-to-rmail)
10506       ;; Decide whether to append to a file or to an Emacs buffer.
10507       (let ((outbuf (get-file-buffer file-name)))
10508         (if (not outbuf)
10509             (append-to-file (point-min) (point-max) file-name)
10510           ;; File has been visited, in buffer OUTBUF.
10511           (set-buffer outbuf)
10512           (let ((buffer-read-only nil)
10513                 (msg (and (boundp 'rmail-current-message)
10514                           (symbol-value 'rmail-current-message))))
10515             ;; If MSG is non-nil, buffer is in RMAIL mode.
10516             (if msg
10517                 (progn (widen)
10518                        (narrow-to-region (point-max) (point-max))))
10519             (insert-buffer-substring tmpbuf)
10520             (if msg
10521                 (progn
10522                   (goto-char (point-min))
10523                   (widen)
10524                   (search-backward "\^_")
10525                   (narrow-to-region (point) (point-max))
10526                   (goto-char (1+ (point-min)))
10527                   (rmail-count-new-messages t)
10528                   (rmail-show-message msg)))))))
10529     (kill-buffer tmpbuf)))
10530
10531 (defun gnus-output-to-file (file-name)
10532   "Append the current article to a file named FILE-NAME."
10533   (setq file-name (expand-file-name file-name))
10534   (let ((artbuf (current-buffer))
10535         (tmpbuf (get-buffer-create " *Gnus-output*")))
10536     (save-excursion
10537       (set-buffer tmpbuf)
10538       (buffer-disable-undo (current-buffer))
10539       (erase-buffer)
10540       (insert-buffer-substring artbuf)
10541       ;; Append newline at end of the buffer as separator, and then
10542       ;; save it to file.
10543       (goto-char (point-max))
10544       (insert "\n")
10545       (append-to-file (point-min) (point-max) file-name))
10546     (kill-buffer tmpbuf)))
10547
10548 (defun gnus-convert-article-to-rmail ()
10549   "Convert article in current buffer to Rmail message format."
10550   (let ((buffer-read-only nil))
10551     ;; Convert article directly into Babyl format.
10552     ;; Suggested by Rob Austein <sra@lcs.mit.edu>
10553     (goto-char (point-min))
10554     (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
10555     (while (search-forward "\n\^_" nil t) ;single char
10556       (replace-match "\n^_" t t))               ;2 chars: "^" and "_"
10557     (goto-char (point-max))
10558     (insert "\^_")))
10559
10560 (defun gnus-narrow-to-page (&optional arg)
10561   "Make text outside current page invisible except for page delimiter.
10562 A numeric arg specifies to move forward or backward by that many pages,
10563 thus showing a page other than the one point was originally in."
10564   (interactive "P")
10565   (setq arg (if arg (prefix-numeric-value arg) 0))
10566   (save-excursion
10567     (forward-page -1)                   ;Beginning of current page.
10568     (widen)
10569     (if (> arg 0)
10570         (forward-page arg)
10571       (if (< arg 0)
10572           (forward-page (1- arg))))
10573     ;; Find the end of the page.
10574     (forward-page)
10575     ;; If we stopped due to end of buffer, stay there.
10576     ;; If we stopped after a page delimiter, put end of restriction
10577     ;; at the beginning of that line.
10578     ;; These are commented out.
10579     ;;    (if (save-excursion (beginning-of-line)
10580     ;;                  (looking-at page-delimiter))
10581     ;;  (beginning-of-line))
10582     (narrow-to-region (point)
10583                       (progn
10584                         ;; Find the top of the page.
10585                         (forward-page -1)
10586                         ;; If we found beginning of buffer, stay there.
10587                         ;; If extra text follows page delimiter on same line,
10588                         ;; include it.
10589                         ;; Otherwise, show text starting with following line.
10590                         (if (and (eolp) (not (bobp)))
10591                             (forward-line 1))
10592                         (point)))))
10593
10594 (defun gnus-gmt-to-local ()
10595   "Rewrite Date header described in GMT to local in current buffer.
10596 Intended to be used with gnus-article-prepare-hook."
10597   (save-excursion
10598     (save-restriction
10599       (widen)
10600       (goto-char (point-min))
10601       (narrow-to-region (point-min)
10602                         (progn (search-forward "\n\n" nil 'move) (point)))
10603       (goto-char (point-min))
10604       (if (re-search-forward "^Date:[ \t]\\(.*\\)$" nil t)
10605           (let ((buffer-read-only nil)
10606                 (date (buffer-substring-no-properties
10607                        (match-beginning 1) (match-end 1))))
10608             (delete-region (match-beginning 1) (match-end 1))
10609             (insert
10610              (timezone-make-date-arpa-standard 
10611               date nil (current-time-zone))))))))
10612
10613
10614 ;; Article mode commands
10615
10616 (defun gnus-article-next-page (lines)
10617   "Show next page of current article.
10618 If end of article, return non-nil. Otherwise return nil.
10619 Argument LINES specifies lines to be scrolled up."
10620   (interactive "P")
10621   (move-to-window-line -1)
10622   ;; Fixed by enami@ptgd.sony.co.jp (enami tsugutomo)
10623   (if (save-excursion
10624         (end-of-line)
10625         (and (pos-visible-in-window-p)  ;Not continuation line.
10626              (eobp)))
10627       ;; Nothing in this page.
10628       (if (or (not gnus-break-pages)
10629               (save-excursion
10630                 (save-restriction
10631                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
10632           t                             ;Nothing more.
10633         (gnus-narrow-to-page 1)         ;Go to next page.
10634         nil)
10635     ;; More in this page.
10636     (condition-case ()
10637         (scroll-up lines)
10638       (end-of-buffer
10639        ;; Long lines may cause an end-of-buffer error.
10640        (goto-char (point-max))))
10641     nil))
10642
10643 (defun gnus-article-prev-page (lines)
10644   "Show previous page of current article.
10645 Argument LINES specifies lines to be scrolled down."
10646   (interactive "P")
10647   (move-to-window-line 0)
10648   (if (and gnus-break-pages
10649            (bobp)
10650            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
10651       (progn
10652         (gnus-narrow-to-page -1) ;Go to previous page.
10653         (goto-char (point-max))
10654         (recenter -1))
10655     (scroll-down lines)))
10656
10657 (defun gnus-article-refer-article ()
10658   "Read article specified by message-id around point."
10659   (interactive)
10660   (search-forward ">" nil t)    ;Move point to end of "<....>".
10661   (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
10662       (let ((message-id
10663              (buffer-substring (match-beginning 1) (match-end 1))))
10664         (set-buffer gnus-summary-buffer)
10665         (gnus-summary-refer-article message-id))
10666     (error "No references around point")))
10667
10668 (defun gnus-article-show-summary ()
10669   "Reconfigure windows to show summary buffer."
10670   (interactive)
10671   (gnus-configure-windows 'article)
10672   (gnus-summary-goto-subject gnus-current-article))
10673
10674 (defun gnus-article-describe-briefly ()
10675   "Describe article mode commands briefly."
10676   (interactive)
10677   (gnus-message 6
10678    (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")))
10679
10680 (defun gnus-article-summary-command ()
10681   "Execute the last keystroke in the summary buffer."
10682   (interactive)
10683   (let ((obuf (current-buffer))
10684         (owin (current-window-configuration))
10685         func)
10686     (switch-to-buffer gnus-summary-buffer 'norecord)
10687     (setq func (lookup-key (current-local-map) (this-command-keys)))
10688     (call-interactively func)
10689     (set-buffer obuf)
10690     (let ((npoint (point)))
10691       (set-window-configuration owin)
10692       (set-window-start (get-buffer-window (current-buffer)) (point)))))
10693
10694 (defun gnus-article-summary-command-nosave ()
10695   "Execute the last keystroke in the summary buffer."
10696   (interactive)
10697   (let ((obuf (current-buffer))
10698         (owin (current-window-configuration))
10699         func)
10700     (switch-to-buffer gnus-summary-buffer 'norecord)
10701     (setq func (lookup-key (current-local-map) (this-command-keys)))
10702     (call-interactively func)))
10703
10704 ;; caesar-region written by phr@prep.ai.mit.edu  Nov 86
10705 ;; Modified by tower@prep Nov 86
10706 ;; Modified by umerin@flab.flab.Fujitsu.JUNET for ROT47.
10707
10708 (defun gnus-caesar-region (&optional n)
10709   "Caesar rotation of region by N, default 13, for decrypting netnews.
10710 ROT47 will be performed for Japanese text in any case."
10711   (interactive (if current-prefix-arg   ; Was there a prefix arg?
10712                    (list (prefix-numeric-value current-prefix-arg))
10713                  (list nil)))
10714   (cond ((not (numberp n)) (setq n 13))
10715         (t (setq n (mod n 26))))        ;canonicalize N
10716   (if (not (zerop n))           ; no action needed for a rot of 0
10717       (progn
10718         (if (or (not (boundp 'caesar-translate-table))
10719                 (not caesar-translate-table)
10720                 (/= (aref caesar-translate-table ?a) (+ ?a n)))
10721             (let ((i 0) 
10722                   (lower "abcdefghijklmnopqrstuvwxyz")
10723                   upper)
10724               (gnus-message 9 "Building caesar-translate-table...")
10725               (setq caesar-translate-table (make-vector 256 0))
10726               (while (< i 256)
10727                 (aset caesar-translate-table i i)
10728                 (setq i (1+ i)))
10729               (setq lower (concat lower lower)
10730                     upper (upcase lower)
10731                     i 0)
10732               (while (< i 26)
10733                 (aset caesar-translate-table (+ ?a i) (aref lower (+ i n)))
10734                 (aset caesar-translate-table (+ ?A i) (aref upper (+ i n)))
10735                 (setq i (1+ i)))
10736               ;; ROT47 for Japanese text.
10737               ;; Thanks to ichikawa@flab.fujitsu.junet.
10738               (setq i 161)
10739               (let ((t1 (logior ?O 128))
10740                     (t2 (logior ?! 128))
10741                     (t3 (logior ?~ 128)))
10742                 (while (< i 256)
10743                   (aset caesar-translate-table i
10744                         (let ((v (aref caesar-translate-table i)))
10745                           (if (<= v t1) (if (< v t2) v (+ v 47))
10746                             (if (<= v t3) (- v 47) v))))
10747                   (setq i (1+ i))))
10748               (gnus-message 9 "Building caesar-translate-table...done")))
10749         (let ((from (region-beginning))
10750               (to (region-end))
10751               (i 0) str len)
10752           (setq str (buffer-substring from to))
10753           (setq len (length str))
10754           (while (< i len)
10755             (aset str i (aref caesar-translate-table (aref str i)))
10756             (setq i (1+ i)))
10757           (goto-char from)
10758           (delete-region from to)
10759           (insert str)))))
10760
10761 \f
10762 ;; Basic ideas by emv@math.lsa.umich.edu (Edward Vielmetti)
10763
10764 ;;;###autoload
10765 (defalias 'gnus-batch-kill 'gnus-batch-score)
10766 ;;;###autoload
10767 (defun gnus-batch-score ()
10768   "Run batched scoring.
10769 Usage: emacs -batch -l gnus -f gnus-batch-score <newsgroups> ...
10770 Newsgroups is a list of strings in Bnews format.  If you want to score
10771 the comp hierarchy, you'd say \"comp.all\". If you would not like to
10772 score the alt hierarchy, you'd say \"!alt.all\"."
10773   (interactive)
10774   (let* ((yes-and-no
10775           (gnus-newsrc-parse-options
10776            (apply (function concat)
10777                   (mapcar (lambda (g) (concat g " "))
10778                           command-line-args-left))))
10779          (gnus-expert-user t)
10780          (nnmail-spool-file nil)
10781          (gnus-use-dribble-file nil)
10782          (yes (car yes-and-no))
10783          (no (cdr yes-and-no))
10784          group subscribed newsrc entry
10785          ;; Disable verbose message.
10786          gnus-novice-user gnus-large-newsgroup)
10787     ;; Eat all arguments.
10788     (setq command-line-args-left nil)
10789     ;; Start Gnus.
10790     (gnus)
10791     ;; Apply kills to specified newsgroups in command line arguments.
10792     (setq newsrc (cdr gnus-newsrc-alist))
10793     (while newsrc
10794       (setq group (car (car newsrc)))
10795       (setq entry (gnus-gethash group gnus-newsrc-hashtb))
10796       (if (and (<= (nth 1 (car newsrc)) gnus-level-subscribed)
10797                (and (car entry)
10798                     (or (eq (car entry) t)
10799                         (not (zerop (car entry)))))
10800                (if yes (string-match yes group) t)
10801                (or (null no) (not (string-match no group))))
10802           (progn
10803             (gnus-summary-read-group group nil t)
10804             (and (eq (current-buffer) (get-buffer gnus-summary-buffer))
10805                  (gnus-summary-exit))))
10806       (setq newsrc (cdr newsrc)))
10807     ;; Exit Emacs.
10808     (switch-to-buffer gnus-group-buffer)
10809     (gnus-group-save-newsrc)))
10810
10811 (defun gnus-apply-kill-file ()
10812   "Apply a kill file to the current newsgroup.
10813 Returns the number of articles marked as read."
10814   (if (or (file-exists-p (gnus-newsgroup-kill-file nil))
10815           (file-exists-p (gnus-newsgroup-kill-file gnus-newsgroup-name)))
10816       (gnus-apply-kill-file-internal)
10817     0))
10818
10819 (defun gnus-kill-save-kill-buffer ()
10820   (save-excursion
10821     (let ((file (gnus-newsgroup-kill-file gnus-newsgroup-name)))
10822       (if (get-file-buffer file)
10823           (progn
10824             (set-buffer (get-file-buffer file))
10825             (and (buffer-modified-p) (save-buffer))
10826             (kill-buffer (current-buffer)))))))
10827
10828 (defvar gnus-kill-file-name "KILL"
10829   "Suffix of the kill files.")
10830
10831 (defun gnus-newsgroup-kill-file (newsgroup)
10832   "Return the name of a kill file name for NEWSGROUP.
10833 If NEWSGROUP is nil, return the global kill file name instead."
10834   (cond ((or (null newsgroup)
10835              (string-equal newsgroup ""))
10836          ;; The global KILL file is placed at top of the directory.
10837          (expand-file-name gnus-kill-file-name
10838                            (or gnus-kill-files-directory "~/News")))
10839         ((gnus-use-long-file-name 'not-kill)
10840          ;; Append ".KILL" to newsgroup name.
10841          (expand-file-name (concat newsgroup "." gnus-kill-file-name)
10842                            (or gnus-kill-files-directory "~/News")))
10843         (t
10844          ;; Place "KILL" under the hierarchical directory.
10845          (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
10846                                    "/" gnus-kill-file-name)
10847                            (or gnus-kill-files-directory "~/News")))))
10848
10849 \f
10850 ;;;
10851 ;;; Dribble file
10852 ;;;
10853
10854 (defvar gnus-dribble-ignore nil)
10855
10856 (defun gnus-dribble-file-name ()
10857   (concat gnus-startup-file "-dribble"))
10858
10859 (defun gnus-dribble-open ()
10860   (save-excursion 
10861     (set-buffer 
10862      (setq gnus-dribble-buffer (find-file-noselect (gnus-dribble-file-name))))
10863     (buffer-disable-undo (current-buffer))
10864     (bury-buffer gnus-dribble-buffer)
10865     (auto-save-mode t)
10866     (goto-char (point-max))))
10867
10868 (defun gnus-dribble-enter (string)
10869   (if (and (not gnus-dribble-ignore)
10870            gnus-dribble-buffer
10871            (buffer-name gnus-dribble-buffer))
10872       (let ((obuf (current-buffer)))
10873         (set-buffer gnus-dribble-buffer)
10874         (insert string "\n")
10875         (set-window-point (get-buffer-window (current-buffer)) (point-max))
10876         (set-buffer obuf))))
10877
10878 (defun gnus-dribble-read-file ()
10879   (let ((dribble-file (gnus-dribble-file-name)))
10880     (save-excursion 
10881       (set-buffer (setq gnus-dribble-buffer 
10882                         (get-buffer-create 
10883                          (file-name-nondirectory dribble-file))))
10884       (gnus-add-current-to-buffer-list)
10885       (erase-buffer)
10886       (set-visited-file-name dribble-file)
10887       (buffer-disable-undo (current-buffer))
10888       (bury-buffer (current-buffer))
10889       (set-buffer-modified-p nil)
10890       (let ((auto (make-auto-save-file-name))
10891             (gnus-dribble-ignore t))
10892         (if (or (file-exists-p auto) (file-exists-p dribble-file))
10893             (progn
10894               (if (file-newer-than-file-p auto dribble-file)
10895                   (setq dribble-file auto))
10896               (insert-file-contents dribble-file)
10897               (if (not (zerop (buffer-size)))
10898                   (set-buffer-modified-p t))
10899               (if (gnus-y-or-n-p 
10900                    "Auto-save file exists. Do you want to read it? ")
10901                   (progn
10902                     (gnus-message 5 "Reading %s..." dribble-file) 
10903                     (eval-current-buffer)
10904                     (gnus-message 5 "Reading %s...done" dribble-file)))))))))
10905
10906 (defun gnus-dribble-delete-file ()
10907   (if (file-exists-p (gnus-dribble-file-name))
10908       (delete-file (gnus-dribble-file-name)))
10909   (if gnus-dribble-buffer
10910       (save-excursion
10911         (set-buffer gnus-dribble-buffer)
10912         (let ((auto (make-auto-save-file-name)))
10913           (if (file-exists-p auto)
10914               (delete-file auto))
10915           (erase-buffer)
10916           (set-buffer-modified-p nil)))))
10917
10918 (defun gnus-dribble-save ()
10919   (if (and gnus-dribble-buffer
10920            (buffer-name gnus-dribble-buffer))
10921       (save-excursion
10922         (set-buffer gnus-dribble-buffer)
10923         (save-buffer))))
10924
10925 (defun gnus-dribble-clear ()
10926   (save-excursion
10927     (if (gnus-buffer-exists-p gnus-dribble-buffer)
10928         (progn
10929           (set-buffer gnus-dribble-buffer)
10930           (erase-buffer)
10931           (set-buffer-modified-p nil)
10932           (setq buffer-saved-size (buffer-size))))))
10933
10934 ;;;
10935 ;;; Server Communication
10936 ;;;
10937
10938 ;; All the Gnus backends have the same interface, and should return
10939 ;; data in a similar format. Below is an overview of what functions
10940 ;; these packages must supply and what results they should return.
10941 ;;
10942 ;; Variables:
10943 ;;
10944 ;; `nntp-server-buffer' - All data should be returned to Gnus in this
10945 ;; buffer. 
10946 ;;
10947 ;; Functions for the imaginary backend `choke':
10948 ;;
10949 ;; `choke-retrieve-headers ARTICLES &optional GROUP SERVER'
10950 ;; Should return all headers for all ARTICLES, or return NOV lines for
10951 ;; the same.
10952 ;;
10953 ;; `choke-request-group GROUP &optional SERVER DISCARD'
10954 ;; Switch to GROUP. If DISCARD is nil, active information on the group
10955 ;; must be returned.
10956 ;;
10957 ;; `choke-close-group GROUP &optional SERVER'
10958 ;; Close group. Most backends won't have to do anything with this
10959 ;; call, but it is an opportunity to clean up, if that is needed. It
10960 ;; is called when Gnus exits a group.
10961 ;;
10962 ;; `choke-request-article ARTICLE &optional GROUP SERVER'
10963 ;; Return ARTICLE, which is either an article number or
10964 ;; message-id. Note that not all backends can return articles based on
10965 ;; message-id. 
10966 ;;
10967 ;; `choke-request-list SERVER'
10968 ;; Return a list of all newsgroups on SERVER.
10969 ;;
10970 ;; `choke-request-list-newsgroups SERVER'
10971 ;; Return a list of descriptions of all newsgroups on SERVER.
10972 ;;
10973 ;; `choke-request-newgroups DATE &optional SERVER'
10974 ;; Return a list of all groups that have arrived after DATE on
10975 ;; SERVER. Note that the date doesn't have to be respected - Gnus will
10976 ;; always check whether the groups are old or not. Backends that do
10977 ;; not store date information may just return the entire list of
10978 ;; groups, although this might not be a good idea in general.
10979 ;;
10980 ;; `choke-request-post-buffer METHOD HEADER ARTICLE-BUFFER GROUP INFO'
10981 ;; Should return a buffer that is suitable for "posting". nnspool and
10982 ;; nntp return a `*post-buffer*', and nnmail return a `*mail*'
10983 ;; buffer. This function should fill out the appropriate headers. 
10984 ;;
10985 ;; `choke-request-post &optional SERVER'
10986 ;; Function that will be called from a buffer to be posted. 
10987 ;;
10988 ;; `choke-open-server SERVER &optional ARGUMENT'
10989 ;; Open a connection to SERVER.
10990 ;;
10991 ;; `choke-close-server &optional SERVER'
10992 ;; Close the connection to SERVER.
10993 ;;
10994 ;; `choke-server-opened &optional SERVER'
10995 ;; Whether the conenction to SERVER is opened or not.
10996 ;;
10997 ;; `choke-server-status &optional SERVER'
10998 ;; Should return a status string (not in the nntp buffer, but as the
10999 ;; result of the function).
11000 ;;
11001 ;; `choke-retrieve-groups GROUPS &optional SERVER'
11002 ;; Optional function for retrieving active file info on all groups in
11003 ;; GROUPS.  Two return formats are supported: The normal active file
11004 ;; format, and a list of GROUP lines.  This function should return (as
11005 ;; a function value) either `active' or `group', depending on what
11006 ;; format it returns.
11007 ;;
11008 ;; The following functions are optional and apply only to backends
11009 ;; that are able to control the contents of their groups totally
11010 ;; (ie. mail backends.)  Backends that aren't able to do that
11011 ;; shouldn't define these functions at all. Gnus will check for their
11012 ;; presence before attempting to call them.
11013 ;;
11014 ;; `choke-request-expire-articles ARTICLES &optional NEWSGROUP SERVER'
11015 ;; Should expire (according to some aging scheme) all ARTICLES. Most
11016 ;; backends will not be able to expire articles. Should return a list
11017 ;; of all articles that were not expired.
11018 ;;
11019 ;; `choke-request-move-article ARTICLE GROUP SERVER ACCEPT-FORM &optional LAST'
11020 ;; Should move ARTICLE from GROUP on SERVER by using ACCEPT-FORM.
11021 ;; Removes any information it has added to the article (extra headers,
11022 ;; whatever - make it as clean as possible), and then passes the
11023 ;; article on by evaling ACCEPT-FORM, which is normally a call to the
11024 ;; function described below. If the ACCEPT-FORM returns a non-nil
11025 ;; value, the article should then be deleted. If LAST is nil, that
11026 ;; means that there will be further calls to this function. This might
11027 ;; be taken as an advice not to save buffers/internal variables just
11028 ;; yet, but wait until the last call to speed things up.
11029 ;;
11030 ;; `choke-request-accept-article GROUP &optional LAST' 
11031 ;; The contents of the current buffer will be put into GROUP.  There
11032 ;; should, of course, be an article in the current buffer.  This
11033 ;; function is normally only called by the function described above,
11034 ;; and LAST works the same way as in that function.
11035 ;;
11036 ;; `choke-request-replace-article ARTICLE GROUP BUFFER'
11037 ;; Replace ARTICLE in GROUP with the contents of BUFFER.
11038 ;; This provides an easy interface for allowing editing of
11039 ;; articles. Note that even headers may be edited, so the backend has
11040 ;; to update any tables (nov buffers, etc) that it maintains after
11041 ;; replacing the article.
11042 ;;
11043 ;; `choke-request-create-group GROUP &optional SERVER'
11044 ;; Create GROUP on SERVER.  This might be a new, empty group, or it
11045 ;; might be a group that already exists, but hasn't been registered
11046 ;; yet. 
11047 ;;
11048 ;; All these functions must return nil if they couldn't service the
11049 ;; request. If the optional arguments are not supplied, some "current"
11050 ;; or "default" values should be used. In short, one should emulate an
11051 ;; NNTP server, in a way.
11052 ;;
11053 ;; If you want to write a new backend, you just have to supply the
11054 ;; functions listed above. In addition, you must enter the new backend
11055 ;; into the list of valid select methods:
11056 ;; (setq gnus-valid-select-methods 
11057 ;;       (cons '("choke" mail) gnus-valid-select-methods))
11058 ;; The first element in this list is the name of the backend. Other
11059 ;; elemnets may be `mail' (for mail groups),  `post' (for news
11060 ;; groups), `none' (neither), `respool' (for groups that can control
11061 ;; their contents). 
11062
11063 (defun gnus-start-news-server (&optional confirm)
11064   "Open a method for getting news.
11065 If CONFIRM is non-nil, the user will be asked for an NNTP server."
11066   (let (how where)
11067     (if gnus-current-select-method
11068         ;; Stream is already opened.
11069         nil
11070       ;; Open NNTP server.
11071       (if (null gnus-nntp-service) (setq gnus-nntp-server nil))
11072       (if confirm
11073           (progn
11074             ;; Read server name with completion.
11075             (setq gnus-nntp-server
11076                   (completing-read "NNTP server: "
11077                                    (mapcar (lambda (server) (list server))
11078                                            (cons (list gnus-nntp-server)
11079                                                  gnus-secondary-servers))
11080                                    nil nil gnus-nntp-server))))
11081
11082       (if (and gnus-nntp-server 
11083                (stringp gnus-nntp-server)
11084                (not (string= gnus-nntp-server "")))
11085           (setq gnus-select-method
11086                 (cond ((or (string= gnus-nntp-server "")
11087                            (string= gnus-nntp-server "::"))
11088                        (list 'nnspool (system-name)))
11089                       ((string-match "^:" gnus-nntp-server)
11090                        (list 'nnmh gnus-nntp-server 
11091                              (list 'nnmh-directory 
11092                                    (file-name-as-directory
11093                                     (expand-file-name
11094                                      (concat "~/" (substring
11095                                                    gnus-nntp-server 1)))))
11096                              (list 'nnmh-get-new-mail nil)))
11097                       (t
11098                        (list 'nntp gnus-nntp-server)))))
11099
11100       (setq how (car gnus-select-method))
11101       (setq where (car (cdr gnus-select-method)))
11102       (cond ((eq how 'nnspool)
11103              (require 'nnspool)
11104              (gnus-message 5 "Looking up local news spool..."))
11105             ((eq how 'nnmh)
11106              (require 'nnmh)
11107              (gnus-message 5 "Looking up mh spool..."))
11108             (t
11109              (require 'nntp)))
11110       (setq gnus-current-select-method gnus-select-method)
11111       (run-hooks 'gnus-open-server-hook)
11112       (or 
11113        ;; gnus-open-server-hook might have opened it
11114        (gnus-server-opened gnus-select-method)  
11115        (gnus-open-server gnus-select-method)
11116        (gnus-y-or-n-p
11117         (format
11118          "%s server on %s can't be opened. Continue? "
11119          (car gnus-select-method) (nth 1 gnus-select-method)))
11120        (progn
11121          (gnus-message 1 "Couldn't open server on %s" 
11122                        (nth 1 gnus-select-method))
11123          (ding)
11124          nil)))))
11125
11126 (defun gnus-check-news-server (&optional method)
11127   "If the news server is down, start it up again."
11128   (let ((method (if method method gnus-select-method)))
11129     (and (stringp method)
11130          (setq method (gnus-server-to-method method)))
11131     (if (gnus-server-opened method)
11132         ;; Stream is already opened.
11133         t
11134       ;; Open server.
11135       (gnus-message 5 "Opening server %s on %s..." (car method) (nth 1 method))
11136       (run-hooks 'gnus-open-server-hook)
11137       (or (gnus-server-opened method)
11138           (gnus-open-server method))
11139       (message ""))))
11140
11141 (defun gnus-nntp-message (&optional message)
11142   "Check the status of the NNTP server.
11143 If the status of the server is clear and MESSAGE is non-nil, MESSAGE
11144 is returned insted of the status string."
11145   (let ((status (gnus-status-message (gnus-find-method-for-group 
11146                                       gnus-newsgroup-name)))
11147         (message (or message "")))
11148     (if (and (stringp status) (> (length status) 0))
11149         status message)))
11150
11151 (defun gnus-get-function (method function)
11152   (and (stringp method)
11153        (setq method (gnus-server-to-method method)))
11154   (let ((func (intern (format "%s-%s" (car method) function))))
11155     (if (not (fboundp func)) 
11156         (progn
11157           (require (car method))
11158           (if (not (fboundp func)) 
11159               (error "No such function: %s" func))))
11160     func))
11161
11162 ;;; Interface functions to the backends.
11163
11164 (defun gnus-open-server (method)
11165   (funcall (gnus-get-function method 'open-server)
11166            (nth 1 method) (nthcdr 2 method)))
11167
11168 (defun gnus-close-server (method)
11169   (funcall (gnus-get-function method 'close-server) (nth 1 method)))
11170
11171 (defun gnus-request-list (method)
11172   (funcall (gnus-get-function method 'request-list) (nth 1 method)))
11173
11174 (defun gnus-request-list-newsgroups (method)
11175   (funcall (gnus-get-function method 'request-list-newsgroups) (nth 1 method)))
11176
11177 (defun gnus-request-newgroups (date method)
11178   (funcall (gnus-get-function method 'request-newgroups) 
11179            date (nth 1 method)))
11180
11181 (defun gnus-server-opened (method)
11182   (funcall (gnus-get-function method 'server-opened) (nth 1 method)))
11183
11184 (defun gnus-status-message (method)
11185   (let ((method (if (stringp method) (gnus-find-method-for-group method)
11186                   method)))
11187     (funcall (gnus-get-function method 'status-message) (nth 1 method))))
11188
11189 (defun gnus-request-group (group &optional dont-check)
11190   (let ((method (gnus-find-method-for-group group)))
11191 ;    (and t (message "%s GROUP %s" (car method) group))
11192     (funcall (gnus-get-function method 'request-group) 
11193              (gnus-group-real-name group) (nth 1 method) dont-check)))
11194
11195 (defun gnus-request-asynchronous (group &optional articles)
11196   (let ((method (gnus-find-method-for-group group)))
11197     (funcall (gnus-get-function method 'request-asynchronous) 
11198              (gnus-group-real-name group) (nth 1 method) articles)))
11199
11200 (defun gnus-list-active-group (group)
11201   (let ((method (gnus-find-method-for-group group))
11202         (func 'list-active-group))
11203     (and (gnus-check-backend-function func group)
11204          (funcall (gnus-get-function method func) 
11205                   (gnus-group-real-name group) (nth 1 method)))))
11206
11207 (defun gnus-request-group-description (group)
11208   (let ((method (gnus-find-method-for-group group))
11209         (func 'request-group-description))
11210     (and (gnus-check-backend-function func group)
11211          (funcall (gnus-get-function method func) 
11212                   (gnus-group-real-name group) (nth 1 method)))))
11213
11214 (defun gnus-close-group (group)
11215   (let ((method (gnus-find-method-for-group group)))
11216     (funcall (gnus-get-function method 'close-group) 
11217              (gnus-group-real-name group) (nth 1 method))))
11218
11219 (defun gnus-retrieve-headers (articles group)
11220   (let ((method (gnus-find-method-for-group group)))
11221     (if (and gnus-use-cache (numberp (car articles)))
11222         (gnus-cache-retrieve-headers articles group)
11223       (funcall (gnus-get-function method 'retrieve-headers) 
11224                articles (gnus-group-real-name group) (nth 1 method)))))
11225
11226 (defun gnus-retrieve-groups (groups method)
11227   (funcall (gnus-get-function method 'retrieve-groups) groups (nth 1 method)))
11228
11229 (defun gnus-request-article (article group &optional buffer)
11230   (let ((method (gnus-find-method-for-group group)))
11231     (funcall (gnus-get-function method 'request-article) 
11232              article (gnus-group-real-name group) (nth 1 method) buffer)))
11233
11234 (defun gnus-request-head (article group)
11235   (let ((method (gnus-find-method-for-group group)))
11236     (funcall (gnus-get-function method 'request-head) 
11237              article (gnus-group-real-name group) (nth 1 method))))
11238
11239 ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
11240 (defun gnus-request-post-buffer (post group subject header artbuf
11241                                       info follow-to respect-poster)
11242    (let* ((info (or info (and group (nth 2 (gnus-gethash 
11243                                             group gnus-newsrc-hashtb)))))
11244           (method
11245            (if (and gnus-post-method
11246                     ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
11247                     (memq 'post (assoc
11248                                  (format "%s" (car (gnus-find-method-for-group
11249                                                     gnus-newsgroup-name)))
11250                                         gnus-valid-select-methods)))
11251                gnus-post-method
11252              (gnus-find-method-for-group gnus-newsgroup-name))))
11253      (or (gnus-server-opened method)
11254          (gnus-open-server method)
11255          (error "Can't open server %s:%s" (car method) (nth 1 method)))
11256      (let ((mail-self-blind nil)
11257            (mail-archive-file-name nil))
11258        (funcall (gnus-get-function method 'request-post-buffer) 
11259                 post group subject header artbuf info follow-to
11260                 respect-poster))))
11261
11262 (defun gnus-request-post (method &optional force)
11263   (and (stringp method)
11264        (setq method (gnus-server-to-method method)))
11265   (and (not force) gnus-post-method
11266        (memq 'post (assoc (format "%s" (car method))
11267                           gnus-valid-select-methods))
11268        (setq method gnus-post-method))
11269   (funcall (gnus-get-function method 'request-post) 
11270            (nth 1 method)))
11271
11272 (defun gnus-request-expire-articles (articles group &optional force)
11273   (let ((method (gnus-find-method-for-group group)))
11274     (funcall (gnus-get-function method 'request-expire-articles) 
11275              articles (gnus-group-real-name group) (nth 1 method)
11276              force)))
11277
11278 (defun gnus-request-move-article 
11279   (article group server accept-function &optional last)
11280   (let ((method (gnus-find-method-for-group group)))
11281     (funcall (gnus-get-function method 'request-move-article) 
11282              article (gnus-group-real-name group) 
11283              (nth 1 method) accept-function last)))
11284
11285 (defun gnus-request-accept-article (group &optional last)
11286   (let ((func (if (symbolp group) group
11287                 (car (gnus-find-method-for-group group)))))
11288     (funcall (intern (format "%s-request-accept-article" func))
11289              (if (stringp group) (gnus-group-real-name group) group)
11290              last)))
11291
11292 (defun gnus-request-replace-article (article group buffer)
11293   (let ((func (car (gnus-find-method-for-group group))))
11294     (funcall (intern (format "%s-request-replace-article" func))
11295              article (gnus-group-real-name group) buffer)))
11296
11297 (defun gnus-request-create-group (group)
11298   (let ((method (gnus-find-method-for-group group)))
11299     (funcall (gnus-get-function method 'request-create-group) 
11300              (gnus-group-real-name group) (nth 1 method))))
11301
11302 (defun gnus-member-of-valid (symbol group)
11303   (memq symbol (assoc
11304                 (format "%s" (car (gnus-find-method-for-group group)))
11305                 gnus-valid-select-methods)))
11306
11307 (defsubst gnus-secondary-method-p (method)
11308   (member method gnus-secondary-select-methods))
11309
11310 (defun gnus-find-method-for-group (group &optional info)
11311   (or gnus-override-method
11312       (and (not group)
11313            gnus-select-method)
11314       (let ((info (or info (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
11315             method)
11316         (if (or (not info)
11317                 (not (setq method (nth 4 info))))
11318             (setq method gnus-select-method)
11319           (setq method
11320                 (cond ((stringp method)
11321                        (gnus-server-to-method method))
11322                       ((stringp (car method))
11323                        (gnus-server-extend-method group method))
11324                       (t
11325                        method))))
11326         (gnus-server-add-address method))))
11327
11328 (defun gnus-check-backend-function (func group)
11329   (let ((method (if (stringp group) (car (gnus-find-method-for-group group))
11330                  group)))
11331     (fboundp (intern (format "%s-%s" method func)))))
11332
11333 (defun gnus-methods-using (method)
11334   (let ((valids gnus-valid-select-methods)
11335         outs)
11336     (while valids
11337       (if (memq method (car valids)) 
11338           (setq outs (cons (car valids) outs)))
11339       (setq valids (cdr valids)))
11340     outs))
11341
11342 ;;; 
11343 ;;; Active & Newsrc File Handling
11344 ;;;
11345
11346 ;; Newsrc related functions.
11347 ;; Gnus internal format of gnus-newsrc-alist:
11348 ;; (("alt.general" 3 (1 . 1))
11349 ;;  ("alt.misc"    3 ((1 . 10) (12 . 15)))
11350 ;;  ("alt.test"    7 (1 . 99) (45 57 93)) ...)
11351 ;; The first item is the group name; the second is the subscription
11352 ;; level; the third is either a range of a list of ranges of read
11353 ;; articles, the optional fourth element is a list of marked articles,
11354 ;; the optional fifth element is the select method.
11355 ;;
11356 ;; Gnus internal format of gnus-newsrc-hashtb:
11357 ;; (95 ("alt.general" 3 (1 . 1)) ("alt.misc" 3 ((1 . 10) (12 . 15))) ...)
11358 ;; This is the entry for "alt.misc". The first element is the number
11359 ;; of unread articles in "alt.misc". The cdr of this entry is the
11360 ;; element *before* "alt.misc" in gnus-newsrc-alist, which makes is
11361 ;; trivial to remove or add new elements into gnus-newsrc-alist
11362 ;; without scanning the entire list. So, to get the actual information
11363 ;; of "alt.misc", you'd say something like 
11364 ;; (nth 2 (gnus-gethash "alt.misc" gnus-newsrc-hashtb))
11365 ;;
11366 ;; Gnus internal format of gnus-active-hashtb:
11367 ;; ((1 . 1))
11368 ;;  (5 . 10))
11369 ;;  (67 . 99)) ...)
11370 ;; The only element in each entry in this hash table is a range of
11371 ;; (possibly) available articles. (Articles in this range may have
11372 ;; been expired or canceled.)
11373 ;;
11374 ;; Gnus internal format of gnus-killed-list and gnus-zombie-list:
11375 ;; ("alt.misc" "alt.test" "alt.general" ...)
11376
11377 (defun gnus-setup-news (&optional rawfile level)
11378   "Setup news information.
11379 If RAWFILE is non-nil, the .newsrc file will also be read.
11380 If LEVEL is non-nil, the news will be set up at level LEVEL."
11381   (let ((init (not (and gnus-newsrc-alist gnus-active-hashtb (not rawfile)))))
11382     ;; Clear some variables to re-initialize news information.
11383     (if init (setq gnus-newsrc-alist nil gnus-active-hashtb nil))
11384
11385     ;; Read the newsrc file and create `gnus-newsrc-hashtb'.
11386     (if init (gnus-read-newsrc-file rawfile))
11387
11388     ;; Read the active file and create `gnus-active-hashtb'.
11389     ;; If `gnus-read-active-file' is nil, then we just create an empty
11390     ;; hash table. The partial filling out of the hash table will be
11391     ;; done in `gnus-get-unread-articles'.
11392     (if (and gnus-read-active-file 
11393              (not level))
11394         (gnus-read-active-file)
11395       (setq gnus-active-hashtb (make-vector 4095 0)))
11396
11397     (and init gnus-use-dribble-file (gnus-dribble-read-file))
11398
11399     ;; Find the number of unread articles in each non-dead group.
11400     (gnus-get-unread-articles (or level (1+ gnus-level-subscribed)))
11401     ;; Find new newsgroups and treat them.
11402     (if (and init gnus-check-new-newsgroups gnus-read-active-file (not level)
11403              (gnus-server-opened gnus-select-method))
11404         (gnus-find-new-newsgroups))
11405     (if (and init gnus-check-bogus-newsgroups 
11406              gnus-read-active-file (not level)
11407              (gnus-server-opened gnus-select-method))
11408         (gnus-check-bogus-newsgroups))))
11409
11410 (defun gnus-find-new-newsgroups ()
11411   "Search for new newsgroups and add them.
11412 Each new newsgroup will be treated with `gnus-subscribe-newsgroup-method.'
11413 The `-n' option line from .newsrc is respected."
11414   (interactive)
11415   (or (gnus-check-first-time-used)
11416       (if (or (consp gnus-check-new-newsgroups)
11417               (eq gnus-check-new-newsgroups 'ask-server))
11418           (gnus-ask-server-for-new-groups)
11419         (let ((groups 0)
11420               group new-newsgroups)
11421           (or gnus-have-read-active-file (gnus-read-active-file))
11422           (setq gnus-newsrc-last-checked-date (current-time-string))
11423           (if (not gnus-killed-hashtb) (gnus-make-hashtable-from-killed))
11424           ;; Go though every newsgroup in `gnus-active-hashtb' and compare
11425           ;; with `gnus-newsrc-hashtb' and `gnus-killed-hashtb'.
11426           (mapatoms
11427            (lambda (sym)
11428              (setq group (symbol-name sym))
11429              (if (or (gnus-gethash group gnus-killed-hashtb)
11430                      (gnus-gethash group gnus-newsrc-hashtb))
11431                  ()
11432                (let ((do-sub (gnus-matches-options-n group)))
11433                  (cond ((eq do-sub 'subscribe)
11434                         (setq groups (1+ groups))
11435                         (gnus-sethash group group gnus-killed-hashtb)
11436                         (funcall 
11437                          gnus-subscribe-options-newsgroup-method group))
11438                        ((eq do-sub 'ignore)
11439                         nil)
11440                        (t
11441                         (setq groups (1+ groups))
11442                         (gnus-sethash group group gnus-killed-hashtb)
11443                         (if gnus-subscribe-hierarchical-interactive
11444                             (setq new-newsgroups (cons group new-newsgroups))
11445                           (funcall gnus-subscribe-newsgroup-method group)))))))
11446            gnus-active-hashtb)
11447           (if new-newsgroups 
11448               (gnus-subscribe-hierarchical-interactive new-newsgroups))
11449           ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
11450           (if (> groups 0)
11451               (gnus-message 6 "%d new newsgroup%s arrived." 
11452                             groups (if (> groups 1) "s have" " has")))))))
11453
11454 (defun gnus-matches-options-n (group)
11455   ;; Returns `subscribe' if the group is to be uncoditionally
11456   ;; subscribed, `ignore' if it is to be ignored, and nil if there is
11457   ;; no match for the group.
11458
11459   ;; First we check the two user variables.
11460   (cond
11461    ((and gnus-options-subscribe
11462          (string-match gnus-options-subscribe group))
11463     'subscribe)
11464    ((and gnus-options-not-subscribe
11465          (string-match gnus-options-not-subscribe group))
11466     'ignore)
11467    ;; Then we go through the list that was retrieved from the .newsrc
11468    ;; file.  This list has elements on the form 
11469    ;; `(REGEXP . {ignore,subscribe})'. The first match found (the list
11470    ;; is in the reverse order of the options line) is returned.
11471    (t
11472     (let ((regs gnus-newsrc-options-n))
11473       (while (and regs
11474                   (not (string-match (car (car regs)) group)))
11475         (setq regs (cdr regs)))
11476       (and regs (cdr (car regs)))))))
11477
11478 (defun gnus-ask-server-for-new-groups ()
11479   (let* ((date (or gnus-newsrc-last-checked-date (current-time-string)))
11480          (methods (cons gnus-select-method 
11481                         (append
11482                          (and (consp gnus-check-new-newsgroups)
11483                               gnus-check-new-newsgroups)
11484                          gnus-secondary-select-methods)))
11485          (groups 0)
11486          (new-date (current-time-string))
11487          hashtb group new-newsgroups got-new)
11488     ;; Go thorugh both primary and secondary select methods and
11489     ;; request new newsgroups.  
11490     (while methods
11491       (and (or (gnus-server-opened (car methods))
11492                (gnus-open-server (car methods)))
11493            (gnus-request-newgroups date (car methods))
11494            (save-excursion
11495              (setq got-new t)
11496              (set-buffer nntp-server-buffer)
11497              (or hashtb (setq hashtb (gnus-make-hashtable 
11498                                       (count-lines (point-min) (point-max)))))
11499              ;; Enter all the new groups in a hashtable.
11500              (gnus-active-to-gnus-format (car methods) hashtb 'ignore)))
11501       (setq methods (cdr methods)))
11502     (and got-new (setq gnus-newsrc-last-checked-date new-date))
11503     ;; Now all new groups from all select methods are in `hashtb'.
11504     (mapatoms
11505      (lambda (group-sym)
11506        (setq group (symbol-name group-sym))
11507        (if (or (gnus-gethash group gnus-newsrc-hashtb)
11508                (member group gnus-zombie-list)
11509                (member group gnus-killed-list))
11510            ;; The group is already known.
11511            ()
11512          (and (symbol-value group-sym)
11513               (gnus-sethash group (symbol-value group-sym) gnus-active-hashtb))
11514          (let ((do-sub (gnus-matches-options-n group)))
11515            (cond ((eq do-sub 'subscribe)
11516                   (setq groups (1+ groups))
11517                   (gnus-sethash group group gnus-killed-hashtb)
11518                   (funcall 
11519                    gnus-subscribe-options-newsgroup-method group))
11520                  ((eq do-sub 'ignore)
11521                   nil)
11522                  (t
11523                   (setq groups (1+ groups))
11524                   (gnus-sethash group group gnus-killed-hashtb)
11525                   (if gnus-subscribe-hierarchical-interactive
11526                       (setq new-newsgroups (cons group new-newsgroups))
11527                     (funcall gnus-subscribe-newsgroup-method group)))))))
11528      hashtb)
11529     (if new-newsgroups 
11530         (gnus-subscribe-hierarchical-interactive new-newsgroups))
11531     ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
11532     (if (> groups 0)
11533         (gnus-message 6 "%d new newsgroup%s arrived." 
11534                       groups (if (> groups 1) "s have" " has")))
11535     got-new))
11536
11537 (defun gnus-check-first-time-used ()
11538   (if (or (> (length gnus-newsrc-alist) 1)
11539           (file-exists-p gnus-startup-file)
11540           (file-exists-p (concat gnus-startup-file ".el"))
11541           (file-exists-p (concat gnus-startup-file ".eld")))
11542       nil
11543     (gnus-message 6 "First time user; subscribing you to default groups")
11544     (or gnus-have-read-active-file (gnus-read-active-file))
11545     (setq gnus-newsrc-last-checked-date (current-time-string))
11546     (let ((groups gnus-default-subscribed-newsgroups)
11547           group)
11548       (if (eq groups t)
11549           nil
11550         (setq groups (or groups gnus-backup-default-subscribed-newsgroups))
11551         (mapatoms
11552          (lambda (sym)
11553            (setq group (symbol-name sym))
11554            (let ((do-sub (gnus-matches-options-n group)))
11555              (cond ((eq do-sub 'subscribe)
11556                     (gnus-sethash group group gnus-killed-hashtb)
11557                     (funcall 
11558                      gnus-subscribe-options-newsgroup-method group))
11559                    ((eq do-sub 'ignore)
11560                     nil)
11561                    (t
11562                     (setq gnus-killed-list (cons group gnus-killed-list))))))
11563          gnus-active-hashtb)
11564         (while groups
11565           (if (gnus-gethash (car groups) gnus-active-hashtb)
11566               (gnus-group-change-level 
11567                (car groups) gnus-level-default-subscribed gnus-level-killed))
11568           (setq groups (cdr groups)))
11569         (gnus-group-make-help-group)
11570         (and gnus-novice-user
11571              (gnus-message 7 "`A k' to list killed groups"))))))
11572
11573 (defun gnus-subscribe-group (group previous &optional method)
11574   (gnus-group-change-level 
11575    (if method
11576        (list t group gnus-level-default-subscribed nil nil method)
11577      group) 
11578    gnus-level-default-subscribed gnus-level-killed previous t))
11579
11580 ;; `gnus-group-change-level' is the fundamental function for changing
11581 ;; subscription levels of newsgroups. This might mean just changing
11582 ;; from level 1 to 2, which is pretty trivial, from 2 to 6 or back
11583 ;; again, which subscribes/unsubscribes a group, which is equally
11584 ;; trivial. Changing from 1-7 to 8-9 means that you kill a group, and
11585 ;; from 8-9 to 1-7 means that you remove the group from the list of
11586 ;; killed (or zombie) groups and add them to the (kinda) subscribed
11587 ;; groups. And last but not least, moving from 8 to 9 and 9 to 8,
11588 ;; which is trivial.
11589 ;; ENTRY can either be a string (newsgroup name) or a list (if
11590 ;; FROMKILLED is t, it's a list on the format (NUM INFO-LIST),
11591 ;; otherwise it's a list in the format of the `gnus-newsrc-hashtb'
11592 ;; entries. 
11593 ;; LEVEL is the new level of the group, OLDLEVEL is the old level and
11594 ;; PREVIOUS is the group (in hashtb entry format) to insert this group
11595 ;; after. 
11596 (defun gnus-group-change-level (entry level &optional oldlevel
11597                                       previous fromkilled)
11598   (let ((pinfo entry)
11599         group info active num)
11600     ;; Glean what info we can from the arguments
11601     (if (consp entry)
11602         (if fromkilled (setq group (nth 1 entry))
11603           (setq group (car (nth 2 entry))))
11604       (setq group entry))
11605     (if (and (stringp entry)
11606              oldlevel 
11607              (< oldlevel gnus-level-zombie))
11608         (setq entry (gnus-gethash entry gnus-newsrc-hashtb)))
11609     (if (and (not oldlevel)
11610              (consp entry))
11611         (setq oldlevel (car (cdr (nth 2 entry)))))
11612     (if (stringp previous)
11613         (setq previous (gnus-gethash previous gnus-newsrc-hashtb)))
11614
11615     (gnus-dribble-enter
11616      (format "(gnus-group-change-level %S %S %S %S %S)" 
11617              group level oldlevel (car (nth 2 previous)) fromkilled))
11618     
11619     ;; Then we remove the newgroup from any old structures, if needed.
11620     ;; If the group was killed, we remove it from the killed or zombie
11621     ;; list. If not, and it is in fact going to be killed, we remove
11622     ;; it from the newsrc hash table and assoc.
11623     (cond ((>= oldlevel gnus-level-zombie)
11624            (if (= oldlevel gnus-level-zombie)
11625                (setq gnus-zombie-list (delete group gnus-zombie-list))
11626              (setq gnus-killed-list (delete group gnus-killed-list))))
11627           (t
11628            (if (>= level gnus-level-zombie)
11629                (progn
11630                  (gnus-sethash (car (nth 2 entry))
11631                                nil gnus-newsrc-hashtb)
11632                  (if (nth 3 entry)
11633                      (setcdr (gnus-gethash (car (nth 3 entry))
11634                                            gnus-newsrc-hashtb)
11635                              (cdr entry)))
11636                  (setcdr (cdr entry) (cdr (cdr (cdr entry))))))))
11637
11638     ;; Finally we enter (if needed) the list where it is supposed to
11639     ;; go, and change the subscription level. If it is to be killed,
11640     ;; we enter it into the killed or zombie list.
11641     (cond ((>= level gnus-level-zombie)
11642            ;; Remove from the hash table.
11643            (gnus-sethash group nil gnus-newsrc-hashtb)
11644            (or (gnus-group-foreign-p group)
11645                ;; We do not enter foreign groups into the list of dead
11646                ;; groups.  
11647                (if (= level gnus-level-zombie)
11648                    (setq gnus-zombie-list (cons group gnus-zombie-list))
11649                  (setq gnus-killed-list (cons group gnus-killed-list)))))
11650           (t
11651            ;; If the list is to be entered into the newsrc assoc, and
11652            ;; it was killed, we have to create an entry in the newsrc
11653            ;; hashtb format and fix the pointers in the newsrc assoc.
11654            (if (>= oldlevel gnus-level-zombie)
11655                (progn
11656                  (if (listp entry)
11657                      (progn
11658                        (setq info (cdr entry))
11659                        (setq num (car entry)))
11660                    (setq active (gnus-gethash group gnus-active-hashtb))
11661                    (setq num (if active (- (1+ (cdr active)) (car active)) t))
11662                    ;; Check whether the group is foreign. If so, the
11663                    ;; foreign select method has to be entered into the
11664                    ;; info. 
11665                    (let ((method (gnus-group-method-name group)))
11666                      (if (eq method gnus-select-method)
11667                          (setq info (list group level nil))
11668                        (setq info (list group level nil nil method)))))
11669                  (or previous 
11670                      (setq previous 
11671                            (let ((p gnus-newsrc-alist))
11672                              (while (cdr (cdr p))
11673                                (setq p (cdr p)))
11674                              p)))
11675                  (setq entry (cons info (cdr (cdr previous))))
11676                  (if (cdr previous)
11677                      (progn
11678                        (setcdr (cdr previous) entry)
11679                        (gnus-sethash group (cons num (cdr previous)) 
11680                                      gnus-newsrc-hashtb))
11681                    (setcdr previous entry)
11682                    (gnus-sethash group (cons num previous)
11683                                  gnus-newsrc-hashtb))
11684                  (if (cdr entry)
11685                      (setcdr (gnus-gethash (car (car (cdr entry)))
11686                                            gnus-newsrc-hashtb)
11687                              entry)))
11688              ;; It was alive, and it is going to stay alive, so we
11689              ;; just change the level and don't change any pointers or
11690              ;; hash table entries.
11691              (setcar (cdr (car (cdr (cdr entry)))) level))))))
11692
11693 (defun gnus-kill-newsgroup (newsgroup)
11694   "Obsolete function. Kills a newsgroup."
11695   (gnus-group-change-level
11696    (gnus-gethash newsgroup gnus-newsrc-hashtb) gnus-level-killed))
11697
11698 (defun gnus-check-bogus-newsgroups (&optional confirm)
11699   "Remove bogus newsgroups.
11700 If CONFIRM is non-nil, the user has to confirm the deletion of every
11701 newsgroup." 
11702   (let ((newsrc (cdr gnus-newsrc-alist))
11703         bogus group entry)
11704     (gnus-message 5 "Checking bogus newsgroups...")
11705     (or gnus-have-read-active-file (gnus-read-active-file))
11706     ;; Find all bogus newsgroup that are subscribed.
11707     (while newsrc
11708       (setq group (car (car newsrc)))
11709       (if (or (gnus-gethash group gnus-active-hashtb) ; Active
11710               (nth 4 (car newsrc))      ; Foreign
11711               (and confirm
11712                    (not (gnus-y-or-n-p
11713                          (format "Remove bogus newsgroup: %s " group)))))
11714           ;; Don't remove.
11715           ()
11716         ;; Found a bogus newsgroup.
11717         (setq bogus (cons group bogus)))
11718       (setq newsrc (cdr newsrc)))
11719     ;; Remove all bogus subscribed groups by first killing them, and
11720     ;; then removing them from the list of killed groups.
11721     (while bogus
11722       (and (setq entry (gnus-gethash (car bogus) gnus-newsrc-hashtb))
11723            (progn
11724              (gnus-group-change-level entry gnus-level-killed)
11725              (setq gnus-killed-list (delete (car bogus) gnus-killed-list))))
11726       (setq bogus (cdr bogus)))
11727     ;; Then we remove all bogus groups from the list of killed and
11728     ;; zombie groups. They are are removed without confirmation.
11729     (let ((dead-lists '(gnus-killed-list gnus-zombie-list))
11730           killed)
11731       (while dead-lists
11732         (setq killed (symbol-value (car dead-lists)))
11733         (while killed
11734           (setq group (car killed))
11735           (or (gnus-gethash group gnus-active-hashtb)
11736               ;; The group is bogus.
11737               (set (car dead-lists)
11738                    (delete group (symbol-value (car dead-lists)))))
11739           (setq killed (cdr killed)))
11740         (setq dead-lists (cdr dead-lists))))
11741     (gnus-message 5 "Checking bogus newsgroups...done")))
11742
11743 (defun gnus-check-duplicate-killed-groups ()
11744   "Remove duplicates from the list of killed groups."
11745   (interactive)
11746   (let ((killed gnus-killed-list))
11747     (while killed
11748       (gnus-message 9 "%d" (length killed))
11749       (setcdr killed (delete (car killed) (cdr killed)))
11750       (setq killed (cdr killed)))))
11751
11752 ;; Go though `gnus-newsrc-alist' and compare with `gnus-active-hashtb'
11753 ;; and compute how many unread articles there are in each group.
11754 (defun gnus-get-unread-articles (&optional level) 
11755   (let* ((newsrc (cdr gnus-newsrc-alist))
11756          (conditional level)
11757          (level (or level (1+ gnus-level-subscribed)))
11758          (foreign-level
11759           (min 
11760            (cond ((and gnus-activate-foreign-newsgroups 
11761                        (not (numberp gnus-activate-foreign-newsgroups)))
11762                   (1+ gnus-level-subscribed))
11763                  ((numberp gnus-activate-foreign-newsgroups)
11764                   gnus-activate-foreign-newsgroups)
11765                  (t 0))
11766            level))
11767          info group active virtuals method)
11768     (gnus-message 5 "Checking new news...")
11769
11770     (while newsrc
11771       (setq info (car newsrc)
11772             group (car info)
11773             active (gnus-gethash group gnus-active-hashtb))
11774
11775       ;; Check newsgroups. If the user doesn't want to check them, or
11776       ;; they can't be checked (for instance, if the news server can't
11777       ;; be reached) we just set the number of unread articles in this
11778       ;; newsgroup to t. This means that Gnus thinks that there are
11779       ;; unread articles, but it has no idea how many.
11780       (if (and (setq method (nth 4 info))
11781                (not (gnus-server-equal gnus-select-method
11782                                        (gnus-server-get-method nil method)))
11783                (not (gnus-secondary-method-p method)))
11784           ;; These groups are foreign. Check the level.
11785           (if (<= (nth 1 info) foreign-level)
11786               (if (eq (car (if (stringp method) 
11787                                (gnus-server-to-method method)
11788                              (nth 4 info))) 'nnvirtual)
11789                   ;; We have to activate the virtual groups after all
11790                   ;; the others, so we just pop them on a list for
11791                   ;; now. 
11792                   (setq virtuals (cons info virtuals))
11793                 (and (setq active (gnus-activate-newsgroup (car info)))
11794                      ;; Close the groups as we look at them!
11795                      (gnus-close-group group))))
11796                 
11797         ;; These groups are native or secondary. 
11798         (if (and (not gnus-have-read-active-file)
11799                  (<= (nth 1 info) level))
11800             (setq active (gnus-activate-newsgroup (car info)))))
11801       
11802       (if active
11803           (gnus-get-unread-articles-in-group info active)
11804         ;; The group couldn't be reached, so we nix out the number of
11805         ;; unread articles and stuff.
11806         (gnus-sethash group nil gnus-active-hashtb)
11807         (setcar (gnus-gethash group gnus-newsrc-hashtb) t))
11808
11809       (setq newsrc (cdr newsrc)))
11810
11811     ;; Activate the virtual groups. This has to be done after all the
11812     ;; other groups. 
11813     ;; !!! If one virtual group contains another virtual group, even
11814     ;; doing it this way might cause problems.
11815    (while virtuals
11816       (and (setq active (gnus-activate-newsgroup (car (car virtuals))))
11817            (gnus-get-unread-articles-in-group (car virtuals) active))
11818       (setq virtuals (cdr virtuals)))
11819
11820     (gnus-message 5 "Checking new news...done")))
11821
11822 ;; Create a hash table out of the newsrc alist. The `car's of the
11823 ;; alist elements are used as keys.
11824 (defun gnus-make-hashtable-from-newsrc-alist ()
11825   (let ((alist gnus-newsrc-alist)
11826         (ohashtb gnus-newsrc-hashtb)
11827         prev)
11828     (setq gnus-newsrc-hashtb (gnus-make-hashtable (length alist)))
11829     (setq alist 
11830           (setq prev (setq gnus-newsrc-alist 
11831                            (if (equal (car (car gnus-newsrc-alist))
11832                                       "dummy.group")
11833                                gnus-newsrc-alist
11834                              (cons (list "dummy.group" 0 nil) alist)))))
11835     (while alist
11836       (gnus-sethash (car (car alist)) 
11837                     (cons (and ohashtb (car (gnus-gethash 
11838                                              (car (car alist)) ohashtb))) 
11839                           prev) gnus-newsrc-hashtb)
11840       (setq prev alist
11841             alist (cdr alist)))))
11842
11843 (defun gnus-make-hashtable-from-killed ()
11844   "Create a hash table from the killed and zombie lists."
11845   (let ((lists '(gnus-killed-list gnus-zombie-list))
11846         list)
11847     (setq gnus-killed-hashtb 
11848           (gnus-make-hashtable 
11849            (+ (length gnus-killed-list) (length gnus-zombie-list))))
11850     (while lists
11851       (setq list (symbol-value (car lists)))
11852       (setq lists (cdr lists))
11853       (while list
11854         (gnus-sethash (car list) (car list) gnus-killed-hashtb)
11855         (setq list (cdr list))))))
11856
11857 (defun gnus-get-unread-articles-in-group (info active)
11858   (let* ((range (nth 2 info))
11859          (num 0)
11860          (marked (nth 3 info))
11861          srange lowest group highest)
11862     ;; If a cache is present, we may have to alter the active info.
11863     (and gnus-use-cache
11864          (gnus-cache-possibly-alter-active (car info) active))
11865     ;; Modify the list of read articles according to what articles 
11866     ;; are available; then tally the unread articles and add the
11867     ;; number to the group hash table entry.
11868     (cond ((zerop (cdr active))
11869            (setq num 0))
11870           ((not range)
11871            (setq num (- (1+ (cdr active)) (car active))))
11872           ((not (listp (cdr range)))
11873            ;; Fix a single (num . num) range according to the
11874            ;; active hash table.
11875            ;; Fix by Carsten Bormann <cabo@Informatik.Uni-Bremen.DE>.
11876            (and (< (cdr range) (car active)) (setcdr range (1- (car active))))
11877            (and (> (cdr range) (cdr active)) (setcdr range (cdr active)))
11878            ;; Compute number of unread articles.
11879            (setq num (max 0 (- (cdr active) 
11880                                (- (1+ (cdr range)) (car range))))))
11881           (t
11882            ;; The read list is a list of ranges. Fix them according to
11883            ;; the active hash table.
11884            ;; First peel off any elements that are below the lower
11885            ;; active limit. 
11886            (while (and (cdr range) 
11887                        (>= (car active) 
11888                            (or (and (atom (car (cdr range))) (car (cdr range)))
11889                                (car (car (cdr range))))))
11890              (if (numberp (car range))
11891                  (setcar range 
11892                          (cons (car range) 
11893                                (or (and (numberp (car (cdr range)))
11894                                         (car (cdr range))) 
11895                                    (cdr (car (cdr range))))))
11896                (setcdr (car range) 
11897                        (or (and (numberp (nth 1 range)) (nth 1 range))
11898                            (cdr (car (cdr range))))))
11899              (setcdr range (cdr (cdr range))))
11900            ;; Adjust the first element to be the same as the lower limit. 
11901            (if (and (not (atom (car range))) 
11902                     (< (cdr (car range)) (car active)))
11903                (setcdr (car range) (1- (car active))))
11904            ;; Then we want to peel off any elements that are higher
11905            ;; than the upper active limit.  
11906            (let ((srange range))
11907              ;; Go past all legal elements.
11908              (while (and (cdr srange) 
11909                          (<= (or (and (atom (car (cdr srange)))
11910                                       (car (cdr srange)))
11911                                  (car (car (cdr srange)))) (cdr active)))
11912                (setq srange (cdr srange)))
11913              (if (cdr srange)
11914                  ;; Nuke all remaining illegal elements.
11915                  (setcdr srange nil))
11916
11917              ;; Adjust the final element.
11918              (if (and (not (atom (car srange)))
11919                       (> (cdr (car srange)) (cdr active)))
11920                  (setcdr (car srange) (cdr active))))
11921            ;; Compute the number of unread articles.
11922            (while range
11923              (setq num (+ num (- (1+ (or (and (atom (car range)) (car range))
11924                                          (cdr (car range))))
11925                                  (or (and (atom (car range)) (car range))
11926                                      (car (car range))))))
11927              (setq range (cdr range)))
11928            (setq num (max 0 (- (cdr active) num)))))
11929     (and info
11930          (progn
11931            (and (assq 'tick marked)
11932                 (inline (gnus-remove-illegal-marked-articles
11933                          (assq 'tick marked) (nth 2 info))))
11934            (and (assq 'dormant marked)
11935                 (inline (gnus-remove-illegal-marked-articles
11936                          (assq 'dormant marked) (nth 2 info))))
11937            (setcar
11938             (gnus-gethash (car info) gnus-newsrc-hashtb) 
11939             (setq num (max 0 (- num (length (cdr (assq 'tick marked)))
11940                                 (length (cdr (assq 'dormant marked)))))))))
11941     num))
11942
11943 (defun gnus-remove-illegal-marked-articles (marked ranges)
11944   (let ((m (cdr marked)))
11945     ;; Make sure that all ticked articles are a subset of the unread
11946     ;; articles. 
11947     (while m
11948       (if (gnus-member-of-range (car m) ranges)
11949           (setcdr marked (cdr m))
11950         (setq marked m))
11951       (setq m (cdr m)))))
11952
11953 (defun gnus-activate-newsgroup (group)
11954   (let ((method (gnus-find-method-for-group group))
11955         active)
11956     (and (or (gnus-server-opened method) (gnus-open-server method))
11957          (gnus-request-group group)
11958          (save-excursion
11959            (set-buffer nntp-server-buffer)
11960            (goto-char (point-min))
11961            (and (looking-at "[0-9]+ [0-9]+ \\([0-9]+\\) [0-9]+")
11962                 (progn
11963                   (goto-char (match-beginning 1))
11964                   (gnus-sethash 
11965                    group (setq active (cons (read (current-buffer))
11966                                             (read (current-buffer))))
11967                    gnus-active-hashtb))
11968                 active)))))
11969
11970 (defun gnus-update-read-articles 
11971   (group unread unselected ticked &optional domarks replied expirable killed
11972          dormant bookmark score)
11973   "Update the list of read and ticked articles in GROUP using the
11974 UNREAD and TICKED lists.
11975 Note: UNSELECTED has to be sorted over `<'.
11976 Returns whether the updating was successful."
11977   (let* ((active (or gnus-newsgroup-active 
11978                      (gnus-gethash group gnus-active-hashtb)))
11979          (entry (gnus-gethash group gnus-newsrc-hashtb))
11980          (number (car entry))
11981          (info (nth 2 entry))
11982          (marked (nth 3 info))
11983          (prev 1)
11984          (unread (sort (copy-sequence unread) (function <)))
11985          last read)
11986     (if (or (not info) (not active))
11987         ;; There is no info on this group if it was, in fact,
11988         ;; killed. Gnus stores no information on killed groups, so
11989         ;; there's nothing to be done. 
11990         ;; One could store the information somewhere temporarily,
11991         ;; perhaps... Hmmm... 
11992         ()
11993       ;; Remove any negative articles numbers.
11994       (while (and unread (< (car unread) 0))
11995         (setq unread (cdr unread)))
11996       ;; Remove any expired article numbers
11997       (while (and unread (< (car unread) (car active)))
11998         (setq unread (cdr unread)))
11999       (while (and ticked (< (car ticked) (car active)))
12000         (setq ticked (cdr ticked)))
12001       (while (and dormant (< (car dormant) (car active)))
12002         (setq dormant (cdr dormant)))
12003       (setq unread (sort (append unselected unread) '<))
12004       ;; Set the number of unread articles in gnus-newsrc-hashtb.
12005       (setcar entry (max 0 (- (length unread) (length ticked) 
12006                               (length dormant))))
12007       ;; Compute the ranges of read articles by looking at the list of
12008       ;; unread articles.  
12009       (while unread
12010         (if (/= (car unread) prev)
12011             (setq read (cons (if (= prev (1- (car unread))) prev
12012                                (cons prev (1- (car unread)))) read)))
12013         (setq prev (1+ (car unread)))
12014         (setq unread (cdr unread)))
12015       (if (<= prev (cdr active))
12016           (setq read (cons (cons prev (cdr active)) read)))
12017       ;; Enter this list into the group info.
12018       (setcar (cdr (cdr info)) 
12019               (if (> (length read) 1) (nreverse read) read))
12020       ;; Enter the list of ticked articles.
12021       (gnus-set-marked-articles 
12022        info ticked
12023        (if domarks replied (cdr (assq 'reply marked)))
12024        (if domarks expirable (cdr (assq 'expire marked)))
12025        (if domarks killed (cdr (assq 'killed marked)))
12026        (if domarks dormant (cdr (assq 'dormant marked)))
12027        (if domarks bookmark (cdr (assq 'bookmark marked)))
12028        (if domarks score (cdr (assq 'score marked))))
12029       t)))
12030
12031 (defun gnus-make-articles-unread (group articles)
12032   "Mark ARTICLES in GROUP as unread."
12033   (let* ((info (nth 2 (or (gnus-gethash group gnus-newsrc-hashtb)
12034                           (gnus-gethash (gnus-group-real-name group)
12035                                         gnus-newsrc-hashtb))))
12036          (ranges (nth 2 info))
12037          news)
12038     (while articles
12039       (and (gnus-member-of-range (car articles) ranges)
12040            (setq news (cons (car articles) news)))
12041       (setq articles (cdr articles)))
12042     (if (not news)
12043         ()
12044       (setcar (nthcdr 2 info)
12045               (gnus-remove-from-range (nth 2 info) (nreverse news)))
12046       (gnus-group-update-group group t))))
12047
12048 ;; Get the active file(s) from the backend(s).
12049 (defun gnus-read-active-file ()
12050   (gnus-group-set-mode-line)
12051   (let ((methods (if (gnus-server-opened gnus-select-method)
12052                      ;; The native server is available.
12053                      (cons gnus-select-method gnus-secondary-select-methods)
12054                    ;; The native server is down, so we just do the
12055                    ;; secondary ones.   
12056                    gnus-secondary-select-methods))
12057         list-type)
12058     (setq gnus-have-read-active-file nil)
12059     (save-excursion
12060       (set-buffer nntp-server-buffer)
12061       (while methods
12062         (let* ((where (nth 1 (car methods)))
12063                (mesg (format "Reading active file%s via %s..."
12064                              (if (and where (not (zerop (length where))))
12065                                  (concat " from " where) "")
12066                              (car (car methods)))))
12067           (gnus-message 5 mesg)
12068           (gnus-check-news-server (car methods))
12069           (cond 
12070            ((and (eq gnus-read-active-file 'some)
12071                  (gnus-check-backend-function
12072                   'retrieve-groups (car (car methods))))
12073             (let ((newsrc (cdr gnus-newsrc-alist))
12074                   groups)
12075               (while newsrc
12076                 (and (gnus-server-equal 
12077                       (gnus-find-method-for-group
12078                        (car (car newsrc)) (car newsrc))
12079                       (gnus-server-get-method nil (car methods)))
12080                      (setq groups (cons (car (car newsrc)) groups)))
12081                 (setq newsrc (cdr newsrc)))
12082               (setq list-type (gnus-retrieve-groups groups (car methods)))
12083               (cond ((not list-type)
12084                      (gnus-message 
12085                       1 "Cannot read partial active file from %s server." 
12086                       (car (car methods)))
12087                      (ding)
12088                      (sit-for 2))
12089                     ((eq list-type 'active)
12090                      (gnus-active-to-gnus-format (car methods)))
12091                     (t
12092                      (gnus-groups-to-gnus-format (car methods))))))
12093            (t
12094             (if (not (gnus-request-list (car methods)))
12095                 (progn
12096                   (gnus-message 1 "Cannot read active file from %s server." 
12097                                 (car (car methods)))
12098                   (ding))
12099               (gnus-active-to-gnus-format (car methods))
12100               (setq gnus-have-read-active-file t)
12101               (gnus-message 5 "%sdone" mesg)))))
12102         (setq methods (cdr methods))))))
12103
12104 ;; Read an active file and place the results in `gnus-active-hashtb'.
12105 (defun gnus-active-to-gnus-format (method &optional hashtb ignore-errors)
12106   (let ((cur (current-buffer))
12107         (hashtb (or hashtb 
12108                     (if (and gnus-active-hashtb 
12109                              (not (equal method gnus-select-method)))
12110                         gnus-active-hashtb
12111                       (setq gnus-active-hashtb
12112                             (if (equal method gnus-select-method)
12113                                 (gnus-make-hashtable 
12114                                  (count-lines (point-min) (point-max)))
12115                               (gnus-make-hashtable 4096)))))))
12116     ;; Delete unnecessary lines.
12117     (goto-char (point-min))
12118     (while (search-forward "\nto." nil t)
12119       (delete-region (1+ (match-beginning 0)) 
12120                      (progn (forward-line 1) (point))))
12121     (or (string= gnus-ignored-newsgroups "")
12122         (progn
12123           (goto-char (point-min))
12124           (delete-matching-lines gnus-ignored-newsgroups)))
12125     ;; If these are groups from a foreign select method, we insert the
12126     ;; group prefix in front of the group names. 
12127     (and method (not (eq method gnus-select-method))
12128          (let ((prefix (gnus-group-prefixed-name "" method)))
12129            (goto-char (point-min))
12130            (while (and (not (eobp))
12131                        (progn (insert prefix)
12132                               (zerop (forward-line 1)))))))
12133     (goto-char (point-min))
12134     ;; Store active file in hashtable.
12135     (goto-char (point-min))
12136     (if (string-match "%[oO]" gnus-group-line-format)
12137         ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
12138         ;; If we want information on moderated groups, we use this
12139         ;; loop...   
12140         (let* ((mod-hashtb (make-vector 7 0))
12141                (m (intern "m" mod-hashtb))
12142                group max mod min)
12143           (while (not (eobp))
12144             (condition-case nil
12145                 (progn
12146                   (narrow-to-region (point) (gnus-point-at-eol))
12147                   (setq group (let ((obarray hashtb)) (read cur)))
12148                   (if (and (numberp (setq max (read cur)))
12149                            (numberp (setq min (read cur))))
12150                       (set group (cons min max))
12151                     (set group nil))
12152                   ;; Enter moderated groups into a list.
12153                   (if (eq (let ((obarray mod-hashtb)) (read cur)) m)
12154                       (setq gnus-moderated-list 
12155                             (cons (symbol-name group) gnus-moderated-list))))
12156               (error nil))
12157             (widen)
12158             (forward-line 1)))
12159       ;; And if we do not care about moderation, we use this loop,
12160       ;; which is faster.
12161       (let (group max min)
12162         (while (not (eobp))
12163           (condition-case ()
12164               (progn
12165                 (narrow-to-region (point) (gnus-point-at-eol))
12166                 ;; group gets set to a symbol interned in the hash table
12167                 ;; (what a hack!!)
12168                 (setq group (let ((obarray hashtb)) (read cur)))
12169                 (if (and (numberp (setq max (read cur)))
12170                          (numberp (setq min (read cur))))
12171                     (set group (cons min max))
12172                   (set group nil)))
12173             (error 
12174              (progn 
12175                (if ignore-errors
12176                    (set group nil)
12177                  (ding) 
12178                  (gnus-message 3 "Warning - illegal active: %s"
12179                                (buffer-substring 
12180                                 (gnus-point-at-bol) (gnus-point-at-eol)))
12181                  nil))))
12182           (widen)
12183           (forward-line 1))))))
12184
12185 (defun gnus-groups-to-gnus-format (method &optional hashtb)
12186   ;; Parse a "groups" active file.
12187   (let ((cur (current-buffer))
12188         (hashtb (or hashtb 
12189                     (if method
12190                         gnus-active-hashtb
12191                       (setq gnus-active-hashtb
12192                             (gnus-make-hashtable 
12193                              (count-lines (point-min) (point-max)))))))
12194         (prefix (and method (not (eq method gnus-select-method))
12195                      (gnus-group-prefixed-name "" method))))
12196
12197     (goto-char (point-min))
12198     (condition-case ()
12199         ;; We split this into to separate loops, one with the prefix
12200         ;; and one without to speed the reading up somewhat.
12201         (if prefix
12202             (let (min max opoint)
12203               (while (not (eobp))
12204                 (read cur) (read cur)
12205                 (setq min (read cur)
12206                       max (read cur)
12207                       opoint (point))
12208                 (skip-chars-forward " \t")
12209                 (insert prefix)
12210                 (goto-char opoint)
12211                 (set (let ((obarray hashtb)) (read cur)) 
12212                      (cons min max))
12213                 (forward-line 1)))
12214           (let (min max opoint)
12215             (while (not (eobp))
12216               (if (= (following-char) ?2)
12217                   (progn
12218                     (read cur) (read cur)
12219                     (setq min (read cur)
12220                           max (read cur))
12221                     (set (let ((obarray hashtb)) (read cur)) 
12222                          (cons min max))))
12223               (forward-line 1))))
12224       (error 
12225        (progn (ding) (gnus-message 3 "Possible error in active file."))))))
12226
12227 (defun gnus-read-newsrc-file (&optional force)
12228   "Read startup file.
12229 If FORCE is non-nil, the .newsrc file is read."
12230   (setq gnus-current-startup-file (gnus-make-newsrc-file gnus-startup-file))
12231   ;; Reset variables that might be defined in the .newsrc.eld file.
12232   (let ((variables gnus-variable-list))
12233     (while variables
12234       (set (car variables) nil)
12235       (setq variables (cdr variables))))
12236   (let* ((newsrc-file gnus-current-startup-file)
12237          (quick-file (concat newsrc-file ".el")))
12238     (save-excursion
12239       ;; We always load the .newsrc.eld file. If always contains
12240       ;; much information that can not be gotten from the .newsrc
12241       ;; file (ticked articles, killed groups, foreign methods, etc.)
12242       (gnus-read-newsrc-el-file quick-file)
12243  
12244       (if (or force
12245               (and (file-newer-than-file-p newsrc-file quick-file)
12246                    (file-newer-than-file-p newsrc-file 
12247                                            (concat quick-file "d")))
12248               (not gnus-newsrc-alist))
12249           ;; We read the .newsrc file. Note that if there if a
12250           ;; .newsrc.eld file exists, it has already been read, and
12251           ;; the `gnus-newsrc-hashtb' has been created. While reading
12252           ;; the .newsrc file, Gnus will only use the information it
12253           ;; can find there for changing the data already read -
12254           ;; ie. reading the .newsrc file will not trash the data
12255           ;; already read (except for read articles).
12256           (save-excursion
12257             (gnus-message 5 "Reading %s..." newsrc-file)
12258             (set-buffer (find-file-noselect newsrc-file))
12259             (buffer-disable-undo (current-buffer))
12260             (gnus-newsrc-to-gnus-format)
12261             (kill-buffer (current-buffer))
12262             (gnus-message 5 "Reading %s...done" newsrc-file))))))
12263
12264 (defun gnus-read-newsrc-el-file (file)
12265   (let ((ding-file (concat file "d")))
12266     ;; We always, always read the .eld file.
12267     (gnus-message 5 "Reading %s..." ding-file)
12268     (let (gnus-newsrc-assoc)
12269       (condition-case nil
12270           (load ding-file t t t)
12271         (error nil))
12272       (and gnus-newsrc-assoc (setq gnus-newsrc-alist gnus-newsrc-assoc)))
12273     (let ((inhibit-quit t))
12274       (gnus-uncompress-newsrc-alist))
12275     (gnus-make-hashtable-from-newsrc-alist)
12276     (if (not (file-newer-than-file-p file ding-file))
12277         ()
12278       ;; Old format quick file
12279       (gnus-message 5 "Reading %s..." file)
12280       ;; The .el file is newer than the .eld file, so we read that one
12281       ;; as well. 
12282       (gnus-read-old-newsrc-el-file file))))
12283
12284 ;; Parse the old-style quick startup file
12285 (defun gnus-read-old-newsrc-el-file (file)
12286   (let (newsrc killed marked group g m len info)
12287     (prog1
12288         (let ((gnus-killed-assoc nil)
12289               gnus-marked-assoc gnus-newsrc-alist gnus-newsrc-assoc)
12290           (prog1
12291               (condition-case nil
12292                   (load file t t t)
12293                 (error nil))
12294             (setq newsrc gnus-newsrc-assoc
12295                   killed gnus-killed-assoc
12296                   marked gnus-marked-assoc)))
12297       (setq gnus-newsrc-alist nil)
12298       (while newsrc
12299         (setq group (car newsrc))
12300         (let ((info (nth 2 (gnus-gethash (car group) gnus-newsrc-hashtb))))
12301           (if info
12302               (progn
12303                 (setcar (nthcdr 2 info) (cdr (cdr group)))
12304                 (setcar (cdr info)
12305                         (if (nth 1 group) gnus-level-default-subscribed 
12306                           gnus-level-default-unsubscribed))
12307                 (setq gnus-newsrc-alist (cons info gnus-newsrc-alist)))
12308             (setq gnus-newsrc-alist
12309                   (cons 
12310                    (setq info
12311                          (list (car group)
12312                                (if (nth 1 group) gnus-level-default-subscribed
12313                                  gnus-level-default-unsubscribed) 
12314                                (cdr (cdr group))))
12315                    gnus-newsrc-alist)))
12316           (if (setq m (assoc (car group) marked))
12317             (setcdr (cdr (cdr info)) (cons (list (cons 'tick (cdr m))) nil))))
12318         (setq newsrc (cdr newsrc)))
12319       (setq newsrc killed)
12320       (while newsrc
12321         (setcar newsrc (car (car newsrc)))
12322         (setq newsrc (cdr newsrc)))
12323       (setq gnus-killed-list killed))
12324     (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist))
12325     (gnus-make-hashtable-from-newsrc-alist)))
12326       
12327 (defun gnus-make-newsrc-file (file)
12328   "Make server dependent file name by catenating FILE and server host name."
12329   (let* ((file (expand-file-name file nil))
12330          (real-file (concat file "-" (nth 1 gnus-select-method))))
12331     (if (file-exists-p real-file)
12332         real-file file)))
12333
12334 (defun gnus-uncompress-newsrc-alist ()
12335   ;; Uncompress all lists of marked articles in the newsrc assoc.
12336   (let ((newsrc gnus-newsrc-alist)
12337         marked)
12338     (while newsrc
12339       (if (not (setq marked (nth 3 (car newsrc))))
12340           ()
12341         (while marked
12342           (or (eq 'score (car (car marked)))
12343               (eq 'bookmark (car (car marked)))
12344               (eq 'killed (car (car marked)))
12345               (setcdr (car marked) (gnus-uncompress-range (cdr (car marked)))))
12346           (setq marked (cdr marked))))
12347       (setq newsrc (cdr newsrc)))))
12348
12349 (defun gnus-compress-newsrc-alist ()
12350   ;; Compress all lists of marked articles in the newsrc assoc.
12351   (let ((newsrc gnus-newsrc-alist)
12352         marked)
12353     (while newsrc
12354       (if (not (setq marked (nth 3 (car newsrc))))
12355           ()
12356         (while marked
12357           (or (eq 'score (car (car marked)))
12358               (eq 'bookmark (car (car marked)))
12359               (eq 'killed (car (car marked)))
12360               (setcdr (car marked) 
12361                       (condition-case ()
12362                           (gnus-compress-sequence 
12363                            (sort (cdr (car marked)) '<) t)
12364                         (error (cdr (car marked))))))
12365           (setq marked (cdr marked))))
12366       (setq newsrc (cdr newsrc)))))
12367
12368 (defun gnus-newsrc-to-gnus-format ()
12369   (setq gnus-newsrc-options "")
12370   (setq gnus-newsrc-options-n nil)
12371
12372   (or gnus-active-hashtb
12373       (setq gnus-active-hashtb (make-vector 4095 0)))
12374   (let ((buf (current-buffer))
12375         (already-read (> (length gnus-newsrc-alist) 1))
12376         group level subscribed info options-symbol newsrc
12377         symbol reads num1)
12378     (goto-char (point-min))
12379     ;; We intern the symbol `options' in the active hashtb so that we
12380     ;; can `eq' against it later.
12381     (setq options-symbol (intern "options" gnus-active-hashtb))
12382   
12383     (while (not (eobp))
12384       ;; We first read the first word on the line by narrowing and
12385       ;; then reading into `gnus-active-hashtb'.  Most groups will
12386       ;; already exist in that hashtb, so this will save some string
12387       ;; space.
12388       (narrow-to-region
12389        (point)
12390        (progn (skip-chars-forward "^ \t!:\n") (point)))
12391       (goto-char (point-min))
12392       (setq symbol 
12393             (and (/= (point-min) (point-max))
12394                  (let ((obarray gnus-active-hashtb)) (read buf))))
12395       (widen)
12396       ;; Now, the symbol we have read is either `options' or a group
12397       ;; name.  If it is an options line, we just add it to a string. 
12398       (cond 
12399        ((eq symbol options-symbol)
12400         (setq gnus-newsrc-options
12401               ;; This concatting is quite inefficient, but since our
12402               ;; thorough studies show that approx 99.37% of all
12403               ;; .newsrc files only contain a single options line, we
12404               ;; don't give a damn, frankly, my dear.
12405               (concat gnus-newsrc-options
12406                       (buffer-substring 
12407                        (gnus-point-at-bol)
12408                        ;; Options may continue on the next line.
12409                        (or (and (re-search-forward "^[^ \t]" nil 'move)
12410                                 (progn (beginning-of-line) (point)))
12411                            (point))))))
12412        (symbol
12413         ;; It was a group name.
12414         (setq subscribed (= (following-char) ?:)
12415               group (symbol-name symbol)
12416               reads nil)
12417         (if (eolp)
12418             ;; If the line ends here, this is clearly a buggy line, so
12419             ;; we put point a the beginning of line and let the cond
12420             ;; below do the error handling.
12421             (beginning-of-line)
12422           ;; We skip to the beginning of the ranges.
12423           (skip-chars-forward "!: \t"))
12424         ;; We are now at the beginning of the list of read articles.
12425         ;; We read them range by range.
12426         (while
12427             (cond 
12428              ((looking-at "[0-9]+")
12429               ;; We narrow and read a number instead of buffer-substring/
12430               ;; string-to-int because it's faster. narrow/widen is
12431               ;; faster than save-restriction/narrow, and save-restriction
12432               ;; produces a garbage object.
12433               (setq num1 (progn
12434                            (narrow-to-region (match-beginning 0) (match-end 0))
12435                            (read buf)))
12436               (widen)
12437               ;; If the next character is a dash, then this is a range.
12438               (if (= (following-char) ?-)
12439                   (progn
12440                     ;; We read the upper bound of the range.
12441                     (forward-char 1)
12442                     (if (not (looking-at "[0-9]+"))
12443                         ;; This is a buggy line, by we pretend that
12444                         ;; it's kinda OK. Perhaps the user should be
12445                         ;; dinged? 
12446                         (setq reads (cons num1 reads))
12447                       (setq reads 
12448                             (cons 
12449                              (cons num1 (progn
12450                                           (narrow-to-region (match-beginning 0) 
12451                                                             (match-end 0))
12452                                           (read buf)))
12453                              reads))
12454                       (widen)))
12455                 ;; It was just a simple number, so we add it to the
12456                 ;; list of ranges.
12457                 (setq reads (cons num1 reads)))
12458               ;; If the next char in ?\n, then we have reached the end
12459               ;; of the line and return nil.
12460               (/= (following-char) ?\n))
12461              ((= (following-char) ?\n)
12462               ;; End of line, so we end.
12463               nil)
12464              (t
12465               ;; Not numbers and not eol, so this might be a buggy
12466               ;; line... 
12467               (or (eobp) ; If it was eob instead of ?\n, we allow it.
12468                   (progn
12469                     ;; The line was buggy.
12470                     (setq group nil)
12471                     (gnus-message 3 "Mangled line: %s" 
12472                                   (buffer-substring (gnus-point-at-bol) 
12473                                                     (gnus-point-at-eol)))
12474                     (ding)
12475                     (sit-for 1)))
12476               nil))
12477           ;; Skip past ", ". Spaces are illegal in these ranges, but
12478           ;; we allow them, because it's a common mistake to put a
12479           ;; space after the comma.
12480           (skip-chars-forward ", "))
12481
12482         ;; We have already read .newsrc.eld, so we gently update the
12483         ;; data in the hash table with the information we have just
12484         ;; read. 
12485         (if (not group)
12486             ()
12487           (let ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
12488                 level)
12489             (if info
12490                 ;; There is an entry for this file in the alist.
12491                 (progn
12492                   (setcar (nthcdr 2 info) (nreverse reads))
12493                   ;; We update the level very gently.  In fact, we
12494                   ;; only change it if there's been a status change
12495                   ;; from subscribed to unsubscribed, or vice versa.
12496                   (setq level (nth 1 info))
12497                   (cond ((and (<= level gnus-level-subscribed)
12498                               (not subscribed))
12499                          (setq level (if reads
12500                                          gnus-level-default-unsubscribed 
12501                                        (1+ gnus-level-default-unsubscribed))))
12502                         ((and (> level gnus-level-subscribed) subscribed)
12503                          (setq level gnus-level-default-subscribed)))
12504                   (setcar (cdr info) level))
12505               ;; This is a new group.
12506               (setq info (list group 
12507                                (if subscribed
12508                                    gnus-level-default-subscribed 
12509                                  (if reads
12510                                      (1+ gnus-level-subscribed)
12511                                    gnus-level-default-unsubscribed))
12512                                (nreverse reads))))
12513             (setq newsrc (cons info newsrc))))))
12514       (forward-line 1))
12515     
12516     (setq newsrc (nreverse newsrc))
12517
12518     (if (not already-read)
12519         ()
12520       ;; We now have two newsrc lists - `newsrc', which is what we
12521       ;; have read from .newsrc, and `gnus-newsrc-alist', which is
12522       ;; what we've read from .newsrc.eld. We have to merge these
12523       ;; lists. We do this by "attaching" any (foreign) groups in the
12524       ;; gnus-newsrc-alist to the (native) group that precedes them. 
12525       (let ((rc (cdr gnus-newsrc-alist))
12526             (prev gnus-newsrc-alist)
12527             entry mentry)
12528         (while rc
12529           (or (null (nth 4 (car rc))) ; It's a native group.
12530               (assoc (car (car rc)) newsrc) ; It's already in the alist.
12531               (if (setq entry (assoc (car (car prev)) newsrc))
12532                   (setcdr (setq mentry (memq entry newsrc))
12533                           (cons (car rc) (cdr mentry)))
12534                 (setq newsrc (cons (car rc) newsrc))))
12535           (setq prev rc
12536                 rc (cdr rc)))))
12537
12538     (setq gnus-newsrc-alist newsrc)
12539     ;; We make the newsrc hashtb.
12540     (gnus-make-hashtable-from-newsrc-alist)
12541
12542     ;; Finally, if we read some options lines, we parse them.
12543     (or (string= gnus-newsrc-options "")
12544         (gnus-newsrc-parse-options gnus-newsrc-options))))
12545
12546 ;; Parse options lines to find "options -n !all rec.all" and stuff.
12547 ;; The return value will be a list on the form
12548 ;; ((regexp1 . ignore)
12549 ;;  (regexp2 . subscribe)...)
12550 ;; When handling new newsgroups, groups that match a `ignore' regexp
12551 ;; will be ignored, and groups that match a `subscribe' regexp will be
12552 ;; subscribed. A line like
12553 ;; options -n !all rec.all
12554 ;; will lead to a list that looks like
12555 ;; (("^rec\\..+" . subscribe) 
12556 ;;  ("^.+" . ignore))
12557 ;; So all "rec.*" groups will be subscribed, while all the other
12558 ;; groups will be ignored. Note that "options -n !all rec.all" is very
12559 ;; different from "options -n rec.all !all". 
12560 (defun gnus-newsrc-parse-options (options)
12561   (let (out eol)
12562     (save-excursion
12563       (gnus-set-work-buffer)
12564       (insert (regexp-quote options))
12565       ;; First we treat all continuation lines.
12566       (goto-char (point-min))
12567       (while (re-search-forward "\n[ \t]+" nil t)
12568         (replace-match " " t t))
12569       ;; Then we transform all "all"s into ".+"s.
12570       (goto-char (point-min))
12571       (while (re-search-forward "\\ball\\b" nil t)
12572         (replace-match ".+" t t))
12573       (goto-char (point-min))
12574       ;; We remove all other options than the "-n" ones.
12575       (while (re-search-forward "[ \t]-[^n][^-]*" nil t)
12576         (replace-match " ")
12577         (forward-char -1))
12578       (goto-char (point-min))
12579
12580       ;; We are only interested in "options -n" lines - we
12581       ;; ignore the other option lines.
12582       (while (re-search-forward "[ \t]-n" nil t)
12583         (setq eol 
12584               (or (save-excursion
12585                     (and (re-search-forward "[ \t]-n" (gnus-point-at-eol) t)
12586                          (- (point) 2)))
12587                   (gnus-point-at-eol)))
12588         ;; Search for all "words"...
12589         (while (re-search-forward "[^ \t,\n]+" eol t)
12590           (if (= (char-after (match-beginning 0)) ?!)
12591               ;; If the word begins with a bang (!), this is a "not"
12592               ;; spec. We put this spec (minus the bang) and the
12593               ;; symbol `ignore' into the list.
12594               (setq out (cons (cons (concat 
12595                                      "^" (buffer-substring 
12596                                           (1+ (match-beginning 0))
12597                                           (match-end 0)))
12598                                     'ignore) out))
12599             ;; There was no bang, so this is a "yes" spec.
12600             (setq out (cons (cons (concat 
12601                                    "^" (buffer-substring (match-beginning 0)
12602                                                          (match-end 0)))
12603                                   'subscribe) out)))))
12604     
12605       (setq gnus-newsrc-options-n out))))
12606                
12607
12608 (defun gnus-save-newsrc-file ()
12609   "Save .newsrc file."
12610   ;; Note: We cannot save .newsrc file if all newsgroups are removed
12611   ;; from the variable gnus-newsrc-alist.
12612   (and (or gnus-newsrc-alist gnus-killed-list)
12613        gnus-current-startup-file
12614        (let ((make-backup-files t)
12615              (version-control nil)
12616              (require-final-newline t)) ;Don't ask even if requested.
12617          ;; You can stop or change version control of backup file.
12618          ;; Suggested by jason@violet.berkeley.edu.
12619          (run-hooks 'gnus-save-newsrc-hook)
12620          (save-excursion
12621            (if (or (not gnus-dribble-buffer)
12622                    (not (buffer-name gnus-dribble-buffer))
12623                    (zerop (save-excursion
12624                             (set-buffer gnus-dribble-buffer)
12625                             (buffer-size))))
12626                (gnus-message 4 "(No changes need to be saved)")
12627              (if gnus-save-newsrc-file
12628                  (progn
12629                    (gnus-message 5 "Saving %s..." gnus-current-startup-file)
12630                    ;; Make backup file of master newsrc.
12631                    (gnus-gnus-to-newsrc-format)
12632                    (gnus-message 5 "Saving %s...done"
12633                                  gnus-current-startup-file)))
12634              ;; Quickly loadable .newsrc.
12635              (set-buffer (get-buffer-create " *Gnus-newsrc*"))
12636              (gnus-add-current-to-buffer-list)
12637              (buffer-disable-undo (current-buffer))
12638              (erase-buffer)
12639              (gnus-message 5 "Saving %s.eld..." gnus-current-startup-file)
12640              (gnus-gnus-to-quick-newsrc-format)
12641              (write-region 1 (point-max) 
12642                            (concat gnus-current-startup-file ".eld") 
12643                            nil 'nomesg)
12644              (kill-buffer (current-buffer))
12645              (gnus-message 5 "Saving %s.eld...done" gnus-current-startup-file)
12646              (gnus-dribble-delete-file))))))
12647
12648 (defun gnus-gnus-to-quick-newsrc-format ()
12649   "Insert Gnus variables such as gnus-newsrc-alist in lisp format."
12650   (insert ";; (ding) Gnus startup file.\n")
12651   (insert ";; Never delete this file - touch .newsrc instead to force Gnus\n")
12652   (insert ";; to read .newsrc.\n")
12653   (let ((variables gnus-variable-list)
12654         (inhibit-quit t)
12655         (gnus-newsrc-alist (cdr gnus-newsrc-alist))
12656         variable)
12657     ;; insert lisp expressions.
12658     (gnus-compress-newsrc-alist)
12659     (while variables
12660       (setq variable (car variables))
12661       (and (boundp variable)
12662            (symbol-value variable)
12663            (or gnus-save-killed-list (not (eq variable 'gnus-killed-list)))
12664            (insert "(setq " (symbol-name variable) " '"
12665                    (prin1-to-string (symbol-value variable))
12666                    ")\n"))
12667       (setq variables (cdr variables)))
12668     (gnus-uncompress-newsrc-alist)))
12669
12670
12671 (defun gnus-gnus-to-newsrc-format ()
12672   ;; Generate and save the .newsrc file.
12673   (let ((newsrc (cdr gnus-newsrc-alist))
12674         info ranges range)
12675     (save-excursion
12676       (set-buffer (create-file-buffer gnus-startup-file))
12677       (buffer-disable-undo (current-buffer))
12678       (erase-buffer)
12679       ;; Write options.
12680       (if gnus-newsrc-options (insert gnus-newsrc-options))
12681       ;; Write subscribed and unsubscribed.
12682       (while newsrc
12683         (setq info (car newsrc))
12684         (if (not (nth 4 info))          ;Don't write foreign groups to .newsrc.
12685             (progn
12686               (insert (car info) (if (> (nth 1 info) gnus-level-subscribed)
12687                                      "!" ":"))
12688               (if (setq ranges (nth 2 info))
12689                   (progn
12690                     (insert " ")
12691                     (if (not (listp (cdr ranges)))
12692                         (if (= (car ranges) (cdr ranges))
12693                             (insert (int-to-string (car ranges)))
12694                           (insert (int-to-string (car ranges)) "-" 
12695                                   (int-to-string (cdr ranges))))
12696                       (while ranges
12697                         (setq range (car ranges)
12698                               ranges (cdr ranges))
12699                         (if (or (atom range) (= (car range) (cdr range)))
12700                             (insert (int-to-string 
12701                                      (or (and (atom range) range) 
12702                                          (car range))))
12703                           (insert (int-to-string (car range)) "-"
12704                                   (int-to-string (cdr range))))
12705                         (if ranges (insert ","))))))
12706               (insert "\n")))
12707         (setq newsrc (cdr newsrc)))
12708       (write-region 1 (point-max) gnus-current-startup-file nil 'nomesg)
12709       (kill-buffer (current-buffer)))))
12710
12711 (defun gnus-read-all-descriptions-files ()
12712   (let ((methods (nconc (list gnus-select-method) 
12713                         gnus-secondary-select-methods)))
12714     (while methods
12715       (gnus-read-descriptions-file (car methods))
12716       (setq methods (cdr methods)))
12717     t))
12718
12719 (defun gnus-read-descriptions-file (&optional method)
12720   (let ((method (or method gnus-select-method)))
12721     ;; We create the hashtable whether we manage to read the desc file
12722     ;; to avoid trying to re-read after a failed read.
12723     (or gnus-description-hashtb
12724         (setq gnus-description-hashtb 
12725               (gnus-make-hashtable (length gnus-active-hashtb))))
12726     ;; Mark this method's desc file as read.
12727     (gnus-sethash (gnus-group-prefixed-name "" method) "Has read"
12728                   gnus-description-hashtb)
12729
12730     (gnus-message 5 "Reading descriptions file via %s..." (car method))
12731     (cond 
12732      ((not (or (gnus-server-opened method)
12733                (gnus-open-server method)))
12734       (gnus-message 1 "Couldn't open server")
12735       nil)
12736      ((not (gnus-request-list-newsgroups method))
12737       (gnus-message 1 "Couldn't read newsgroups descriptions")
12738       nil)
12739      (t
12740       (let (group)
12741         (save-excursion
12742           (save-restriction
12743             (set-buffer nntp-server-buffer)
12744             (goto-char (point-min))
12745             (if (or (search-forward "\n.\n" nil t)
12746                     (goto-char (point-max)))
12747                 (progn
12748                   (beginning-of-line)
12749                   (narrow-to-region (point-min) (point))))
12750             (goto-char (point-min))
12751             (while (not (eobp))
12752               ;; If we get an error, we set group to 0, which is not a
12753               ;; symbol... 
12754               (setq group 
12755                     (condition-case ()
12756                         (let ((obarray gnus-description-hashtb))
12757                           ;; Group is set to a symbol interned in this
12758                           ;; hash table.
12759                           (read nntp-server-buffer))
12760                       (error 0)))
12761               (skip-chars-forward " \t")
12762               ;; ... which leads to this line being effectively ignored.
12763               (and (symbolp group)
12764                    (set group (buffer-substring 
12765                                (point) (progn (end-of-line) (point)))))
12766               (forward-line 1))))
12767         (gnus-message 5 "Reading descriptions file...done")
12768         t)))))
12769
12770 (defun gnus-group-get-description (group)
12771   ;; Get the description of a group by sending XGTITLE to the server.
12772   (and (gnus-request-group-description group)
12773        (save-excursion
12774          (set-buffer nntp-server-buffer)
12775          (goto-char (point-min))
12776          (and (looking-at "[^ \t]+[ \t]+\\(.*\\)")
12777               (buffer-substring (match-beginning 1) (match-end 1))))))
12778
12779 ;;;
12780 ;;; Server
12781 ;;;
12782
12783 (defvar gnus-server-mode-hook nil
12784   "Hook run in `gnus-server-mode' buffers.")
12785
12786 (defconst gnus-server-line-format "     {%(%h:%w%)}\n"
12787   "Format of server lines.
12788 It works along the same lines as a normal formatting string,
12789 with some simple extensions.")
12790
12791 (defvar gnus-server-mode-line-format "(ding) List of servers"
12792   "The format specification for the server mode line.")
12793
12794 (defconst gnus-server-line-format-alist
12795   (list (list ?h 'how ?s)
12796         (list ?n 'name ?s)
12797         (list ?w 'where ?s)
12798         ))
12799
12800 (defconst gnus-server-mode-line-format-alist 
12801   (list (list ?S 'news-server ?s)
12802         (list ?M 'news-method ?s)
12803         (list ?u 'user-defined ?s)))
12804
12805 (defvar gnus-server-line-format-spec nil)
12806 (defvar gnus-server-mode-line-format-spec nil)
12807 (defvar gnus-server-killed-servers nil)
12808
12809 (defvar gnus-server-mode-map nil)
12810 (put 'gnus-server-mode 'mode-class 'special)
12811
12812 (if gnus-server-mode-map
12813     nil
12814   (setq gnus-server-mode-map (make-sparse-keymap))
12815   (suppress-keymap gnus-server-mode-map)
12816   (define-key gnus-server-mode-map " " 'gnus-server-read-server)
12817   (define-key gnus-server-mode-map "\r" 'gnus-server-read-server)
12818   (define-key gnus-server-mode-map gnus-mouse-2 'gnus-server-pick-server)
12819   (define-key gnus-server-mode-map "q" 'gnus-server-exit)
12820   (define-key gnus-server-mode-map "l" 'gnus-server-list-servers)
12821   (define-key gnus-server-mode-map "k" 'gnus-server-kill-server)
12822   (define-key gnus-server-mode-map "y" 'gnus-server-yank-server)
12823   (define-key gnus-server-mode-map "c" 'gnus-server-copy-server)
12824   (define-key gnus-server-mode-map "a" 'gnus-server-add-server)
12825   (define-key gnus-server-mode-map "e" 'gnus-server-edit-server))
12826
12827 (defun gnus-server-mode ()
12828   "Major mode for listing and editing servers.
12829
12830 All normal editing commands are switched off.
12831 \\<gnus-server-mode-map>
12832
12833 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]'). 
12834
12835 The following commands are available:
12836
12837 \\{gnus-server-mode-map}"
12838   (interactive)
12839   (if gnus-visual (gnus-server-make-menu-bar))
12840   (kill-all-local-variables)
12841   (setq mode-line-modified "-- ")
12842   (make-local-variable 'mode-line-format)
12843   (setq mode-line-format (copy-sequence mode-line-format))
12844   (and (equal (nth 3 mode-line-format) "   ")
12845        (setcar (nthcdr 3 mode-line-format) ""))
12846   (setq major-mode 'gnus-server-mode)
12847   (setq mode-name "Server")
12848 ;  (gnus-group-set-mode-line)
12849   (setq mode-line-process nil)
12850   (use-local-map gnus-server-mode-map)
12851   (buffer-disable-undo (current-buffer))
12852   (setq truncate-lines t)
12853   (setq buffer-read-only t)
12854   (run-hooks 'gnus-server-mode-hook))
12855
12856 (defun gnus-server-insert-server-line (sformat name method)
12857   (let* ((sformat (or sformat gnus-server-line-format-spec))
12858          (how (car method))
12859          (where (nth 1 method))
12860          b)
12861     (beginning-of-line)
12862     (setq b (point))
12863     ;; Insert the text.
12864     (insert (eval sformat))
12865     (add-text-properties 
12866      b (1+ b) (list 'gnus-server (intern name)))))
12867
12868 (defun gnus-server-setup-buffer ()
12869   (if (get-buffer gnus-server-buffer)
12870       ()
12871     (save-excursion
12872       (set-buffer (get-buffer-create gnus-server-buffer))
12873       (gnus-server-mode)
12874       (and gnus-carpal (gnus-carpal-setup-buffer 'server)))))
12875
12876 (defun gnus-server-prepare ()
12877   (setq gnus-server-mode-line-format-spec 
12878         (gnus-parse-format gnus-server-mode-line-format 
12879                            gnus-server-mode-line-format-alist))
12880   (setq gnus-server-line-format-spec 
12881         (gnus-parse-format gnus-server-line-format 
12882                            gnus-server-line-format-alist))
12883   (let ((alist gnus-server-alist)
12884         (buffer-read-only nil))
12885     (erase-buffer)
12886     (while alist
12887       (gnus-server-insert-server-line nil (car (car alist)) (cdr (car alist)))
12888       (setq alist (cdr alist))))
12889   (goto-char (point-min))
12890   (gnus-server-position-cursor))
12891
12892 (defun gnus-server-server-name ()
12893   (let ((server (get-text-property (gnus-point-at-bol) 'gnus-server)))
12894     (and server (symbol-name server))))
12895
12896 (defalias 'gnus-server-position-cursor 'gnus-goto-colon)
12897
12898 (defconst gnus-server-edit-buffer "*Gnus edit server*")
12899
12900 (defun gnus-server-update-server (server)
12901   (save-excursion
12902     (set-buffer gnus-server-buffer)
12903     (let ((buffer-read-only nil)
12904           (info (cdr (assoc server gnus-server-alist))))
12905       (gnus-dribble-enter 
12906        (concat "(gnus-server-set-info \"" server "\" '"
12907                (prin1-to-string info) ")"))
12908       ;; Buffer may be narrowed.
12909       (save-restriction
12910         (widen)
12911         (if (gnus-server-goto-server server)
12912             (delete-region (progn (beginning-of-line) (point))
12913                            (progn (forward-line 1) (point))))
12914         (let ((entry (assoc server gnus-server-alist)))
12915           (gnus-server-insert-server-line nil (car entry) (cdr entry))
12916           (gnus-server-position-cursor))))))
12917
12918 (defun gnus-server-set-info (server info)
12919   ;; Enter a select method into the virtual server alist.
12920   (gnus-dribble-enter 
12921    (concat "(gnus-server-set-info \"" server "\" '"
12922            (prin1-to-string info) ")"))
12923   (let* ((server (nth 1 info))
12924          (entry (assoc server gnus-server-alist)))
12925     (if entry (setcdr entry info)
12926       (setq gnus-server-alist
12927             (nconc gnus-server-alist (list (cons server info)))))))
12928
12929 (defun gnus-server-to-method (server)
12930   ;; Map virtual server names to select methods.
12931   (or (and (equal server "native") gnus-select-method)
12932       (cdr (assoc server gnus-server-alist))))
12933
12934 (defun gnus-server-extend-method (group method)
12935   ;; This function "extends" a virtual server.  If the server is
12936   ;; "hello", and the select method is ("hello" (my-var "something")) 
12937   ;; in the group "alt.alt", this will result in a new virtual server
12938   ;; called "helly+alt.alt".
12939   (let ((entry
12940          (gnus-copy-sequence 
12941           (if (equal (car method) "native") gnus-select-method
12942               (cdr (assoc (car method) gnus-server-alist))))))
12943     (setcar (cdr entry) (concat (nth 1 entry) "+" group))
12944     (nconc entry (cdr method))))
12945
12946 (defun gnus-server-get-method (group method)
12947   ;; Input either a server name, and extended server name, or a
12948   ;; select method, and return a select method. 
12949   (cond ((stringp method)
12950          (gnus-server-to-method method))
12951         ((stringp (car method))
12952          (gnus-server-extend-method group method))
12953         (t
12954          (gnus-server-add-address method))))
12955
12956 (defun gnus-server-add-address (method)
12957   (let ((method-name (symbol-name (car method))))
12958     (if (and (memq 'address (assoc method-name gnus-valid-select-methods))
12959              (not (assq (intern (concat method-name "-address")) method)))
12960         (append method (list (list (intern (concat method-name "-address"))
12961                                    (nth 1 method))))
12962       method)))
12963
12964 (defun gnus-server-equal (s1 s2)
12965   (or (equal s1 s2)
12966       (and (= (length s1) (length s2))
12967            (progn
12968              (while (and s1 (member (car s1) s2))
12969                (setq s1 (cdr s1)))
12970              (null s1)))))
12971
12972 ;;; Interactive server functions.
12973
12974 (defun gnus-server-kill-server (server)
12975   "Kill the server on the current line."
12976   (interactive (list (gnus-server-server-name)))
12977   (or (gnus-server-goto-server server)
12978       (if server (error "No such server: %s" server)
12979         (error "No server on the current line")))
12980   (let ((buffer-read-only nil))
12981     (delete-region (progn (beginning-of-line) (point))
12982                    (progn (forward-line 1) (point))))
12983   (setq gnus-server-killed-servers 
12984         (cons (assoc server gnus-server-alist) gnus-server-killed-servers))
12985   (setq gnus-server-alist (delq (car gnus-server-killed-servers)
12986                                 gnus-server-alist))
12987   (gnus-server-position-cursor))
12988
12989 (defun gnus-server-yank-server ()
12990   "Yank the previously killed server."
12991   (interactive)
12992   (or gnus-server-killed-servers
12993       (error "No killed servers to be yanked"))
12994   (let ((alist gnus-server-alist)
12995         (server (gnus-server-server-name))
12996         (killed (car gnus-server-killed-servers)))
12997     (if (not server) 
12998         (setq gnus-server-alist (nconc gnus-server-alist (list killed)))
12999       (if (string= server (car (car gnus-server-alist)))
13000           (setq gnus-server-alist (cons killed gnus-server-alist))
13001         (while (and (cdr alist)
13002                     (not (string= server (car (car (cdr alist))))))
13003           (setq alist (cdr alist)))
13004         (setcdr alist (cons killed (cdr alist)))))
13005     (gnus-server-update-server (car killed))
13006     (setq gnus-server-killed-servers (cdr gnus-server-killed-servers))
13007     (gnus-server-position-cursor)))
13008
13009 (defun gnus-server-exit ()
13010   "Return to the group buffer."
13011   (interactive)
13012   (kill-buffer (current-buffer))
13013   (switch-to-buffer gnus-group-buffer))
13014
13015 (defun gnus-server-list-servers ()
13016   "List all available servers."
13017   (interactive)
13018   (let ((cur (gnus-server-server-name)))
13019     (gnus-server-prepare)
13020     (if cur (gnus-server-goto-server cur)
13021       (goto-char (point-max))
13022       (forward-line -1))
13023     (gnus-server-position-cursor)))
13024
13025 (defun gnus-server-copy-server (from to)
13026   (interactive
13027    (list
13028     (or (gnus-server-server-name)
13029         (error "No server on the current line"))
13030     (read-string "Copy to: ")))
13031   (or from (error "No server on current line"))
13032   (or (and to (not (string= to ""))) (error "No name to copy to"))
13033   (and (assoc to gnus-server-alist) (error "%s already exists" to))
13034   (or (assoc from gnus-server-alist) 
13035       (error "%s: no such server" from))
13036   (let ((to-entry (gnus-copy-sequence (assoc from gnus-server-alist))))
13037     (setcar to-entry to)
13038     (setcar (nthcdr 2 to-entry) to)
13039     (setq gnus-server-killed-servers 
13040           (cons to-entry gnus-server-killed-servers))
13041     (gnus-server-yank-server)))
13042
13043 (defun gnus-server-add-server (how where)
13044   (interactive 
13045    (list (intern (completing-read "Server method: "
13046                                   gnus-valid-select-methods nil t))
13047          (read-string "Server name: ")))
13048   (setq gnus-server-killed-servers 
13049         (cons (list where how where) gnus-server-killed-servers))
13050   (gnus-server-yank-server))
13051
13052 (defun gnus-server-goto-server (server)
13053   "Jump to a server line."
13054   (interactive
13055    (list (completing-read "Goto server: " gnus-server-alist nil t)))
13056   (let ((to (text-property-any (point-min) (point-max) 
13057                                'gnus-server (intern server))))
13058     (and to
13059          (progn
13060            (goto-char to) 
13061            (gnus-server-position-cursor)))))
13062
13063 (defun gnus-server-edit-server (server)
13064   "Edit the server on the current line."
13065   (interactive (list (gnus-server-server-name)))
13066   (or server
13067       (error "No server on current line"))
13068   (let ((winconf (current-window-configuration)))
13069     (get-buffer-create gnus-server-edit-buffer)
13070     (gnus-configure-windows 'edit-server)
13071     (gnus-add-current-to-buffer-list)
13072     (emacs-lisp-mode)
13073     (make-local-variable 'gnus-prev-winconf)
13074     (setq gnus-prev-winconf winconf)
13075     (use-local-map (copy-keymap (current-local-map)))
13076     (let ((done-func '(lambda () 
13077                         "Exit editing mode and update the information."
13078                         (interactive)
13079                         (gnus-server-edit-server-done 'group))))
13080       (setcar (cdr (nth 4 done-func)) server)
13081       (local-set-key "\C-c\C-c" done-func))
13082     (erase-buffer)
13083     (insert ";; Type `C-c C-c' after you have edited the server.\n\n")
13084     (insert (pp-to-string (cdr (assoc server gnus-server-alist))))))
13085
13086 (defun gnus-server-edit-server-done (server)
13087   (interactive)
13088   (set-buffer (get-buffer-create gnus-server-edit-buffer))
13089   (goto-char (point-min))
13090   (let ((form (read (current-buffer)))
13091         (winconf gnus-prev-winconf))
13092     (gnus-server-set-info server form)
13093     (kill-buffer (current-buffer))
13094     (and winconf (set-window-configuration winconf))
13095     (set-buffer gnus-server-buffer)
13096     (gnus-server-update-server (gnus-server-server-name))
13097     (gnus-server-position-cursor)))
13098
13099 (defun gnus-server-read-server (server)
13100   "Browse a server."
13101   (interactive (list (gnus-server-server-name)))
13102   (gnus-browse-foreign-server (gnus-server-to-method server) (current-buffer)))
13103
13104 (defun gnus-mouse-pick-server (e)
13105   (interactive "e")
13106   (mouse-set-point e)
13107   (gnus-server-read-server (gnus-server-server-name)))
13108
13109 ;;;
13110 ;;; entry points into gnus-score.el
13111 ;;;
13112
13113 ;;; Finding score files. 
13114
13115 (defvar gnus-global-score-files nil
13116   "*List of global score files and directories.
13117 Set this variable if you want to use people's score files.  One entry
13118 for each score file or each score file directory.  Gnus will decide
13119 by itself what score files are applicable to which group.
13120
13121 Say you want to use the single score file
13122 \"/ftp.ifi.uio.no@ftp:/pub/larsi/ding/score/soc.motss.SCORE\" and all
13123 score files in the \"/ftp.some-where:/pub/score\" directory.
13124
13125  (setq gnus-global-score-files
13126        '(\"/ftp.ifi.uio.no:/pub/larsi/ding/score/soc.motss.SCORE\"
13127          \"/ftp.some-where:/pub/score\"))")
13128
13129 (defun gnus-score-score-files (group)
13130   "Return a list of all possible score files."
13131   ;; Search and set any global score files.
13132   (and gnus-global-score-files 
13133        (or gnus-internal-global-score-files
13134            (gnus-score-search-global-directories gnus-global-score-files)))
13135   ;; Fix the kill-file dir variable.
13136   (setq gnus-kill-files-directory 
13137         (file-name-as-directory
13138          (or gnus-kill-files-directory "~/News/")))
13139   ;; If we can't read it, there are no score files.
13140   (if (not (file-exists-p (expand-file-name gnus-kill-files-directory)))
13141       (setq gnus-score-file-list nil)
13142     (if (gnus-use-long-file-name 'not-score)
13143         ;; We want long file names.
13144         (if (or (not gnus-score-file-list)
13145                 (not (car gnus-score-file-list))
13146                 (gnus-file-newer-than gnus-kill-files-directory
13147                                       (car gnus-score-file-list)))
13148               (setq gnus-score-file-list 
13149                     (cons (nth 5 (file-attributes gnus-kill-files-directory))
13150                           (nreverse 
13151                            (directory-files 
13152                             gnus-kill-files-directory t 
13153                             (gnus-score-file-regexp))))))
13154       ;; We do not use long file names, so we have to do some
13155       ;; directory traversing.  
13156       (let ((mdir (length (expand-file-name gnus-kill-files-directory)))
13157             (suffixes (list gnus-score-file-suffix gnus-adaptive-file-suffix))
13158             dir files suffix)
13159         (while suffixes
13160           (setq dir (expand-file-name
13161                      (concat gnus-kill-files-directory
13162                              (gnus-replace-chars-in-string group ?. ?/))))
13163           (setq suffix (car suffixes)
13164                 suffixes (cdr suffixes))
13165           (if (file-exists-p (concat dir "/" suffix))
13166               (setq files (cons (concat dir "/" suffix) files)))
13167           (while (>= (1+ (length dir)) mdir)
13168             (and (file-exists-p (concat dir "/all/" suffix))
13169                  (setq files (cons (concat dir "/all/" suffix) files)))
13170             (string-match "/[^/]*$" dir)
13171             (setq dir (substring dir 0 (match-beginning 0)))))
13172         (setq gnus-score-file-list 
13173               (cons nil (nreverse files)))))
13174     (cdr gnus-score-file-list)))
13175
13176 (defun gnus-score-file-regexp ()
13177   (concat "\\(" gnus-score-file-suffix 
13178           "\\|" gnus-adaptive-file-suffix "\\)$"))
13179         
13180 (defun gnus-score-find-bnews (group)
13181   "Return a list of score files for GROUP.
13182 The score files are those files in the ~/News directory which matches
13183 GROUP using BNews sys file syntax."
13184   (let* ((sfiles (append (gnus-score-score-files group)
13185                          gnus-internal-global-score-files))
13186          (kill-dir (file-name-as-directory 
13187                     (expand-file-name gnus-kill-files-directory)))
13188          (klen (length kill-dir))
13189          ofiles not-match regexp)
13190     (save-excursion
13191       (set-buffer (get-buffer-create "*gnus score files*"))
13192       (buffer-disable-undo (current-buffer))
13193       ;; Go through all score file names and create regexp with them
13194       ;; as the source.  
13195       (while sfiles
13196         (erase-buffer)
13197         (insert (car sfiles))
13198         (goto-char (point-min))
13199         ;; First remove the suffix itself.
13200         (re-search-forward (concat "." (gnus-score-file-regexp)))
13201         (replace-match "" t t) 
13202         (goto-char (point-min))
13203         (if (looking-at (regexp-quote kill-dir))
13204             ;; If the file name was just "SCORE", `klen' is one character
13205             ;; too much.
13206             (delete-char (min (1- (point-max)) klen))
13207           (goto-char (point-max))
13208           (search-backward "/")
13209           (delete-region (1+ (point)) (point-min)))
13210         ;; If short file names were used, we have to translate slashes.
13211         (goto-char (point-min))
13212         (while (search-forward "/" nil t)
13213           (replace-match "." t t))
13214         ;; Translate "all" to ".*".
13215         (while (search-forward "all" nil t)
13216           (replace-match ".*" t t))
13217         (goto-char (point-min))
13218         ;; Deal with "not."s.
13219         (if (looking-at "not.")
13220             (progn
13221               (setq not-match t)
13222               (setq regexp (buffer-substring 5 (point-max))))
13223           (setq regexp (buffer-substring 1 (point-max)))
13224           (setq not-match nil))
13225         ;; Finally - if this resulting regexp matches the group name,
13226         ;; we add this score file to the list of score files
13227         ;; applicable to this group.
13228         (if (or (and not-match
13229                      (not (string-match regexp group)))
13230                 (and (not not-match)
13231                      (string-match regexp group)))
13232             (setq ofiles (cons (car sfiles) ofiles)))
13233         (setq sfiles (cdr sfiles)))
13234       (kill-buffer (current-buffer))
13235       ;; Slight kludge here - the last score file returned should be
13236       ;; the local score file, whether it exists or not. This is so
13237       ;; that any score commands the user enters will go to the right
13238       ;; file, and not end up in some global score file.
13239       (let ((localscore
13240              (expand-file-name
13241               (if (gnus-use-long-file-name 'not-score)
13242                   (concat gnus-kill-files-directory group "." 
13243                           gnus-score-file-suffix)
13244                 (concat gnus-kill-files-directory
13245                         (gnus-replace-chars-in-string group ?. ?/)
13246                         "/" gnus-score-file-suffix)))))
13247         (and (member localscore ofiles)
13248              (delete localscore ofiles))
13249         (setq ofiles (cons localscore ofiles)))
13250       (nreverse ofiles))))
13251
13252 (defun gnus-score-find-single (group)
13253   "Return list containing the score file for GROUP."
13254   (list (gnus-score-file-name group gnus-adaptive-file-suffix)
13255         (gnus-score-file-name group)))
13256
13257 (defun gnus-score-find-hierarchical (group)
13258   "Return list of score files for GROUP.
13259 This includes the score file for the group and all its parents."
13260   (let ((all (copy-sequence '(nil)))
13261         (start 0))
13262     (while (string-match "\\." group (1+ start))
13263       (setq start (match-beginning 0))
13264       (setq all (cons (substring group 0 start) all)))
13265     (setq all (cons group all))
13266     (nconc
13267      (mapcar (lambda (newsgroup)
13268                (gnus-score-file-name newsgroup gnus-adaptive-file-suffix))
13269              (setq all (nreverse all)))
13270      (mapcar 'gnus-score-file-name all))))
13271
13272 (defvar gnus-score-file-alist-cache nil)
13273
13274 (defun gnus-score-find-alist (group)
13275   "Return list of score files for GROUP.
13276 The list is determined from the variable gnus-score-file-alist."
13277   (let ((alist gnus-score-file-multiple-match-alist)
13278         score-files)
13279     ;; if this group has been seen before, return the cached entry
13280     (if (setq score-files (assoc group gnus-score-file-alist-cache))
13281         (cdr score-files)       ; ensures caching of groups with no matches
13282       ;; handle the multiple match alist
13283       (while alist
13284         (and (string-match (car (car alist)) group)
13285              (setq score-files
13286                    (nconc score-files (cdr (car alist)))))
13287         (setq alist (cdr alist)))
13288       (setq alist gnus-score-file-single-match-alist)
13289       ;; handle the single match alist
13290       (catch 'done
13291         (while alist
13292           (and (string-match (car (car alist)) group)
13293                ;; progn used just in case ("regexp") has no files
13294                ;; and score-files is still nil. -sj
13295                ;; this can be construed as a "stop searching here" feature :>
13296                ;; and used to simplify regexps in the single-alist 
13297                (progn
13298                  (setq score-files
13299                        (append score-files (cdr (car alist))))
13300                  (throw 'done nil)))
13301           (setq alist (cdr alist))))
13302       ;; cache the score files
13303       (setq gnus-score-file-alist-cache
13304             (cons (cons group score-files) gnus-score-file-alist-cache))
13305       score-files)))
13306
13307
13308 (defun gnus-possibly-score-headers (&optional trace)
13309   (let ((func gnus-score-find-score-files-function)
13310         score-files scores)
13311     (and func (not (listp func))
13312          (setq func (list func)))
13313     ;; Go through all the functions for finding score files (or actual
13314     ;; scores) and add them to a list.
13315     (setq score-files (copy-sequence
13316                        (gnus-score-find-alist gnus-newsgroup-name)))
13317     (while func
13318       (and (symbolp (car func))
13319            (fboundp (car func))
13320            (setq score-files 
13321                  (nconc score-files (funcall (car func) gnus-newsgroup-name))))
13322       (setq func (cdr func)))
13323     (if score-files (gnus-score-headers score-files trace))))
13324
13325 (defun gnus-score-file-name (newsgroup &optional suffix)
13326   "Return the name of a score file for NEWSGROUP."
13327   (let ((suffix (or suffix gnus-score-file-suffix)))
13328     (cond  ((or (null newsgroup)
13329                 (string-equal newsgroup ""))
13330             ;; The global score file is placed at top of the directory.
13331             (expand-file-name 
13332              suffix (or gnus-kill-files-directory "~/News")))
13333            ((gnus-use-long-file-name 'not-score)
13334             ;; Append ".SCORE" to newsgroup name.
13335             (expand-file-name (concat newsgroup "." suffix)
13336                               (or gnus-kill-files-directory "~/News")))
13337            (t
13338             ;; Place "SCORE" under the hierarchical directory.
13339             (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
13340                                       "/" suffix)
13341                               (or gnus-kill-files-directory "~/News"))))))
13342
13343 (defun gnus-score-search-global-directories (files)
13344   "Scan all global score directories for score files."
13345   ;; Set the variable `gnus-internal-global-score-files' to all
13346   ;; available global score files.
13347   (interactive (list gnus-global-score-files))
13348   (let (out)
13349     (while files
13350       (if (string-match "/$" (car files))
13351           (setq out (nconc (directory-files 
13352                             (car files) t
13353                             (concat (gnus-score-file-regexp) "$"))))
13354         (setq out (cons (car files) out)))
13355       (setq files (cdr files)))
13356     (setq gnus-internal-global-score-files out)))
13357
13358 ;; Allow redefinition of Gnus functions.
13359
13360 (gnus-ems-redefine)
13361
13362 (provide 'gnus)
13363
13364 ;;; gnus.el ends here