*** empty log message ***
[gnus] / lisp / gnus.el
1 ;;; gnus.el --- a newsreader for GNU Emacs
2 ;; Copyright (C) 1987,88,89,90,93,94,95 Free Software Foundation, Inc.
3
4 ;; Author: Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
5 ;;      Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23
24 ;;; Commentary:
25
26 ;; Although (ding) Gnus looks suspiciously like GNUS, it isn't quite
27 ;; the same beast. Most internal structures have been changed. If you
28 ;; have written packages that depend on any of the hash tables,
29 ;; `gnus-newsrc-alist', `gnus-killed-assoc', marked lists, the .newsrc
30 ;; buffer, or internal knowledge of the `nntp-header-' macros, or
31 ;; dependence on the buffers having a certain format, your code will
32 ;; fail.
33
34 ;;; Code:
35
36 (require 'mail-utils)
37 (require 'timezone)
38 (require 'nnheader)
39
40 ;; Site dependent variables. These variables should be defined in
41 ;; paths.el.
42
43 (defvar gnus-default-nntp-server nil
44   "Specify a default NNTP server.
45 This variable should be defined in paths.el, and should never be set
46 by the user.
47 If you want to change servers, you should use `gnus-select-method'.
48 See the documentation to that variable.")
49
50 (defconst gnus-backup-default-subscribed-newsgroups 
51   '("news.announce.newusers" "news.groups.questions" "gnu.emacs.gnus")
52   "Default default new newsgroups the first time Gnus is run.
53 Should be set in paths.el, and shouldn't be touched by the user.")
54
55 (defvar gnus-local-domain nil
56   "Local domain name without a host name.
57 The DOMAINNAME environment variable is used instead if it is defined.
58 If the `system-name' function returns the full Internet name, there is
59 no need to set this variable.")
60
61 (defvar gnus-local-organization nil
62   "String with a description of what organization (if any) the user belongs to.
63 The ORGANIZATION environment variable is used instead if it is defined.
64 If this variable contains a function, this function will be called
65 with the current newsgroup name as the argument. The function should
66 return a string.
67
68 In any case, if the string (either in the variable, in the environment
69 variable, or returned by the function) is a file name, the contents of
70 this file will be used as the organization.")
71
72 (defvar gnus-use-generic-from nil
73   "If nil, the full host name will be the system name prepended to the domain name.
74 If this is a string, the full host name will be this string.
75 If this is non-nil, non-string, the domain name will be used as the
76 full host name.")
77
78 (defvar gnus-use-generic-path nil
79   "If nil, use the NNTP server name in the Path header.
80 If stringp, use this; if non-nil, use no host name (user name only).")
81
82
83 ;; Customization variables
84
85 (defvar gnus-select-method 
86   (list 'nntp (or (getenv "NNTPSERVER") 
87                   (if (and gnus-default-nntp-server
88                            (not (string= gnus-default-nntp-server "")))
89                       gnus-default-nntp-server)
90                   (system-name)))
91   "*Default method for selecting a newsgroup.
92 This variable should be a list, where the first element is how the
93 news is to be fetched, the second is the address. 
94
95 For instance, if you want to get your news via NNTP from
96 \"flab.flab.edu\", you could say:
97
98 (setq gnus-select-method '(nntp \"flab.flab.edu\"))
99
100 If you want to use your local spool, say:
101
102 (setq gnus-select-method (list 'nnspool (system-name)))
103
104 If you use this variable, you must set `gnus-nntp-server' to nil.
105
106 There is a lot more to know about select methods and virtual servers -
107 see the manual for details.")
108
109 ;; Added by Sudish Joseph <joseph@cis.ohio-state.edu>.
110 (defvar gnus-post-method nil
111   "*Preferred method for posting USENET news.
112 If this variable is nil, Gnus will use the current method to decide
113 which method to use when posting.  If it is non-nil, it will override
114 the current method.  This method will not be used in mail groups and
115 the like, only in \"real\" newsgroups.
116
117 The value must be a valid method as discussed in the documentation of
118 `gnus-select-method'.")
119
120 (defvar gnus-refer-article-method nil
121   "*Preferred method for fetching an article by Message-ID.
122 If you are reading news from the local spool (with nnspool), fetching
123 articles by Message-ID is painfully slow. By setting this method to an
124 nntp method, you might get acceptable results.
125
126 The value of this variable must be a valid select method as discussed
127 in the documentation of `gnus-select-method'")
128
129 (defvar gnus-secondary-select-methods nil
130   "*A list of secondary methods that will be used for reading news.
131 This is a list where each element is a complete select method (see
132 `gnus-select-method').  
133
134 If, for instance, you want to read your mail with the nnml backend,
135 you could set this variable:
136
137 (setq gnus-secondary-select-methods '((nnml \"\"))")
138
139 (defvar gnus-secondary-servers nil
140   "*List of NNTP servers that the user can choose between interactively.
141 To make Gnus query you for a server, you have to give `gnus' a
142 non-numeric prefix - `C-u M-x gnus', in short.")
143
144 (defvar gnus-nntp-server nil
145   "*The name of the host running the NNTP server.
146 This variable is semi-obsolete. Use the `gnus-select-method'
147 variable instead.")
148
149 (defvar gnus-nntp-service "nntp"
150   "*NNTP service name (\"nntp\" or 119).
151 This is an obsolete variable, which is scarcely used. If you use an
152 nntp server for your newsgroup and want to change the port number
153 used to 899, you would say something along these lines:
154
155  (setq gnus-select-method '(nntp \"my.nntp.server\" (nntp-port-number 899)))")
156
157 (defvar gnus-startup-file "~/.newsrc"
158   "*Your `.newsrc' file.
159 `.newsrc-SERVER' will be used instead if that exists.")
160
161 (defvar gnus-init-file "~/.gnus"
162   "*Your Gnus elisp startup file.
163 If a file with the .el or .elc suffixes exist, it will be read
164 instead.") 
165
166 (defvar gnus-group-faq-directory
167   "/anonymous@rtfm.mit.edu:/pub/usenet-by-group/"
168   "*Directory where the group FAQs are stored.
169 This will most commonly be on a remote machine, and the file will be
170 fetched by ange-ftp.")
171
172 (defvar gnus-group-archive-directory
173   "/ftp@ftp.hpc.uh.edu:/pub/emacs/ding-list/" 
174   "*The address of the (ding) archives.")
175
176 (defvar gnus-default-subscribed-newsgroups nil
177   "*This variable lists what newsgroups should be subscribed the first time Gnus is used.
178 It should be a list of strings.
179 If it is `t', Gnus will not do anything special the first time it is
180 started; it'll just use the normal newsgroups subscription methods.")
181
182 (defvar gnus-use-cross-reference t
183   "*Non-nil means that cross referenced articles will be marked as read.
184 If nil, ignore cross references.  If t, mark articles as read in
185 subscribed newsgroups. If neither t nor nil, mark as read in all
186 newsgroups.") 
187
188 (defvar gnus-use-dribble-file t
189   "*Non-nil means that Gnus will use a dribble file to store user updates.
190 If Emacs should crash without saving the .newsrc files, complete
191 information can be restored from the dribble file.")
192
193 (defvar gnus-asynchronous nil
194   "*If non-nil, Gnus will supply backends with data needed for async article fetching.")
195
196 (defvar gnus-asynchronous-article-function nil
197   "*Function for picking articles to pre-fetch, possibly.")
198
199 (defvar gnus-score-file-single-match-alist nil
200   "*Alist mapping regexps to lists of score files.
201 Each element of this alist should be of the form
202         (\"REGEXP\" [ \"SCORE-FILE-1\" ] [ \"SCORE-FILE-2\" ] ... )
203
204 If the name of a group is matched by REGEXP, the corresponding scorefiles
205 will be used for that group.
206 The first match found is used, subsequent matching entries are ignored (to
207 use multiple matches, see gnus-score-file-multiple-match-alist).
208
209 These score files are loaded in addition to any files returned by
210 gnus-score-find-score-files-function (which see).")
211
212 (defvar gnus-score-file-multiple-match-alist nil
213   "*Alist mapping regexps to lists of score files.
214 Each element of this alist should be of the form
215         (\"REGEXP\" [ \"SCORE-FILE-1\" ] [ \"SCORE-FILE-2\" ] ... )
216
217 If the name of a group is matched by REGEXP, the corresponding scorefiles
218 will be used for that group.
219 If multiple REGEXPs match a group, the score files corresponding to each
220 match will be used (for only one match to be used, see
221 gnus-score-file-single-match-alist).
222
223 These score files are loaded in addition to any files returned by
224 gnus-score-find-score-files-function (which see).")
225
226
227 (defvar gnus-score-file-suffix "SCORE"
228   "*Suffix of the score files.")
229
230 (defvar gnus-adaptive-file-suffix "ADAPT"
231   "*Suffix of the adaptive score files.")
232
233 (defvar gnus-score-find-score-files-function 'gnus-score-find-bnews
234   "*Function used to find SCORE files.
235 The function will be called with the group name as the argument, and
236 should return a list of score files to apply to that group.  The score
237 files do not actually have to exist.
238
239 Predefined values are:
240
241 gnus-score-find-single: Only apply the group's own SCORE file.
242 gnus-score-find-hierarchical: Also apply SCORE files from parent groups.
243 gnus-score-find-bnews: Apply SCORE files whose names matches.
244
245 See the documentation to these functions for more information.
246
247 This variable can also be a list of functions to be called.  Each
248 function should either return a list of score files, or a list of
249 score alists.")
250
251 (defvar gnus-score-interactive-default-score 1000
252   "*Scoring commands will raise/lower the score with this number as the default.")
253
254 (defvar gnus-large-newsgroup 200
255   "*The number of articles which indicates a large newsgroup.
256 If the number of articles in a newsgroup is greater than this value,
257 confirmation is required for selecting the newsgroup.")
258
259 ;; Suggested by Andrew Eskilsson <pi92ae@lelle.pt.hk-r.se>.
260 (defvar gnus-no-groups-message "No news is horrible news"
261   "*Message displayed by Gnus when no groups are available.")
262
263 (defvar gnus-use-long-file-name (not (memq system-type '(usg-unix-v xenix)))
264   "*Non-nil means that the default name of a file to save articles in is the group name.
265 If it's nil, the directory form of the group name is used instead.
266
267 If this variable is a list, and the list contains the element
268 `not-score', long file names will not be used for score files; if it
269 contains the element `not-save', long file names will not be used for
270 saving; and if it contains the element `not-kill', long file names
271 will not be used for kill files.")
272
273 (defvar gnus-article-save-directory (or (getenv "SAVEDIR") "~/News/")
274   "*Name of the directory articles will be saved in (default \"~/News\").
275 Initialized from the SAVEDIR environment variable.")
276
277 (defvar gnus-kill-files-directory (or (getenv "SAVEDIR") "~/News/")
278   "*Name of the directory where kill files will be stored (default \"~/News\").
279 Initialized from the SAVEDIR environment variable.")
280
281 (defvar gnus-default-article-saver 'gnus-summary-save-in-rmail
282   "*A function to save articles in your favorite format.
283 The function must be interactively callable (in other words, it must
284 be an Emacs command).
285
286 Gnus provides the following functions:
287
288 * gnus-summary-save-in-rmail (Rmail format)
289 * gnus-summary-save-in-mail (Unix mail format)
290 * gnus-summary-save-in-folder (MH folder)
291 * gnus-summary-save-in-file (article format).
292 * gnus-summary-save-in-vm (use VM's folder format).")
293
294 (defvar gnus-rmail-save-name (function gnus-plain-save-name)
295   "*A function generating a file name to save articles in Rmail format.
296 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
297
298 (defvar gnus-mail-save-name (function gnus-plain-save-name)
299   "*A function generating a file name to save articles in Unix mail format.
300 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
301
302 (defvar gnus-folder-save-name (function gnus-folder-save-name)
303   "*A function generating a file name to save articles in MH folder.
304 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER.")
305
306 (defvar gnus-file-save-name (function gnus-numeric-save-name)
307   "*A function generating a file name to save articles in article format.
308 The function is called with NEWSGROUP, HEADERS, and optional
309 LAST-FILE.")
310
311 (defvar gnus-split-methods nil
312   "*Variable used to suggest where articles are to be saved.
313 The syntax of this variable is the same as `nnmail-split-methods'.  
314
315 For instance, if you would like to save articles related to Gnus in
316 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
317 you could set this variable to something like:
318
319  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
320    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))")
321
322 (defvar gnus-save-score nil
323   "*If non-nil, save group scoring info.")
324
325 (defvar gnus-use-adaptive-scoring nil
326   "*If non-nil, use some adaptive scoring scheme.")
327
328 (defvar gnus-use-cache nil
329   "*If non-nil, Gnus will cache (some) articles locally.")
330
331 (defvar gnus-use-scoring t
332   "*If non-nil, enable scoring.")
333
334 (defvar gnus-fetch-old-headers nil
335   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
336 If an unread article in the group refers to an older, already read (or
337 just marked as read) article, the old article will not normally be
338 displayed in the Summary buffer.  If this variable is non-nil, Gnus
339 will attempt to grab the headers to the old articles, and thereby
340 build complete threads.  If it has the value `some', only enough
341 headers to connect otherwise loose threads will be displayed.
342
343 The server has to support XOVER for any of this to work.")
344
345 (defvar gnus-visual t
346   "*If non-nil, will do various highlighting.
347 If nil, no mouse highlights (or any other highlights) will be
348 performed.  This might speed up Gnus some when generating large group
349 and summary buffers.")
350
351 (defvar gnus-novice-user t
352   "*Non-nil means that you are a usenet novice.
353 If non-nil, verbose messages may be displayed and confirmations may be
354 required.")
355
356 (defvar gnus-expert-user nil
357   "*Non-nil means that you will never be asked for confirmation about anything.
358 And that means *anything*.")
359
360 (defvar gnus-verbose 7
361   "*Integer that says how verbose Gnus should be.
362 The higher the number, the more messages Gnus will flash to say what
363 it's doing.  At zero, Gnus will be totally mute; at five, Gnus will
364 display most important messages; and at ten, Gnus will keep on
365 jabbering all the time.")
366
367 (defvar gnus-keep-same-level nil
368   "*Non-nil means that the next newsgroup after the current will be on the same level.
369 When you type, for instance, `n' after reading the last article in the
370 current newsgroup, you will go to the next newsgroup. If this variable
371 is nil, the next newsgroup will be the next from the group
372 buffer. 
373 If this variable is non-nil, Gnus will either put you in the
374 next newsgroup with the same level, or, if no such newsgroup is
375 available, the next newsgroup with the lowest possible level higher
376 than the current level.
377 If this variable is `best', Gnus will make the next newsgroup the one
378 with the best level.")
379
380 (defvar gnus-summary-make-false-root 'adopt
381   "*nil means that Gnus won't gather loose threads.
382 If the root of a thread has expired or been read in a previous
383 session, the information necessary to build a complete thread has been
384 lost. Instead of having many small sub-threads from this original thread
385 scattered all over the summary buffer, Gnus can gather them. 
386
387 If non-nil, Gnus will try to gather all loose sub-threads from an
388 original thread into one large thread.
389
390 If this variable is non-nil, it should be one of `none', `adopt',
391 `dummy' or `empty'.
392
393 If this variable is `none', Gnus will not make a false root, but just
394 present the sub-threads after another.
395 If this variable is `dummy', Gnus will create a dummy root that will
396 have all the sub-threads as children.
397 If this variable is `adopt', Gnus will make one of the \"children\"
398 the parent and mark all the step-children as such.
399 If this variable is `empty', the \"children\" are printed with empty
400 subject fields.  (Or rather, they will be printed with a string
401 given by the `gnus-summary-same-subject' variable.)")
402
403 (defvar gnus-summary-gather-subject-limit nil
404   "*Maximum length of subject comparisons when gathering loose threads.
405 Use nil to compare full subjects.  Setting this variable to a low
406 number will help gather threads that have been corrupted by
407 newsreaders chopping off subject lines, but it might also mean that
408 unrelated articles that have subject that happen to begin with the
409 same few characters will be incorrectly gathered.
410
411 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
412 comparing subjects.")
413
414 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
415 (defvar gnus-summary-same-subject ""
416   "*String indicating that the current article has the same subject as the previous.
417 This variable will only be used if the value of
418 `gnus-summary-make-false-root' is `empty'.")
419
420 (defvar gnus-summary-goto-unread t
421   "*If non-nil, marking commands will go to the next unread article.")
422
423 (defvar gnus-group-goto-unread t
424   "*If non-nil, movement commands will go to the next unread and subscribed group.")
425
426 (defvar gnus-check-new-newsgroups t
427   "*Non-nil means that Gnus will add new newsgroups at startup.
428 If this variable is `ask-server', Gnus will ask the server for new
429 groups since the last time it checked. This means that the killed list
430 is no longer necessary, so you could set `gnus-save-killed-list' to
431 nil. 
432
433 A variant is to have this variable be a list of select methods. Gnus
434 will then use the `ask-server' method on all these select methods to
435 query for new groups from all those servers.
436
437 Eg.
438   (setq gnus-check-new-newsgroups 
439         '((nntp \"some.server\") (nntp \"other.server\")))
440
441 If this variable is nil, then you have to tell Gnus explicitly to
442 check for new newsgroups with \\<gnus-group-mode-map>\\[gnus-find-new-newsgroups].")
443
444 (defvar gnus-check-bogus-newsgroups nil
445   "*Non-nil means that Gnus will check and remove bogus newsgroup at startup.
446 If this variable is nil, then you have to tell Gnus explicitly to
447 check for bogus newsgroups with \\<gnus-group-mode-map>\\[gnus-group-check-bogus-groups].")
448
449 (defvar gnus-read-active-file t
450   "*Non-nil means that Gnus will read the entire active file at startup.
451 If this variable is nil, Gnus will only know about the groups in your
452 `.newsrc' file.
453
454 If this variable is `some', Gnus will try to only read the relevant
455 parts of the active file from the server.  Not all servers support
456 this, and it might be quite slow with other servers, but this should
457 generally be faster than both the t and nil value.
458
459 If you set this variable to nil or `some', you probably still want to
460 be told about new newsgroups that arrive.  To do that, set
461 `gnus-check-new-newsgroups' to `ask-server'.  This may not work
462 properly with all servers.")
463
464 (defvar gnus-level-subscribed 5
465   "*Groups with levels less than or equal to this variable are subscribed.")
466
467 (defvar gnus-level-unsubscribed 7
468   "*Groups with levels less than or equal to this variable are unsubscribed.
469 Groups with levels less than `gnus-level-subscribed', which should be
470 less than this variable, are subscribed.")
471
472 (defvar gnus-level-zombie 8
473   "*Groups with this level are zombie groups.")
474
475 (defvar gnus-level-killed 9
476   "*Groups with this level are killed.")
477
478 (defvar gnus-level-default-subscribed 3
479   "*New subscribed groups will be subscribed at this level.")
480
481 (defvar gnus-level-default-unsubscribed 6
482   "*New unsubscribed groups will be unsubscribed at this level.")
483
484 (defvar gnus-activate-foreign-newsgroups nil
485   "*If nil, Gnus will not check foreign newsgroups at startup.
486 If it is non-nil, it should be a number between one and nine. Foreign
487 newsgroups that have a level lower or equal to this number will be
488 activated on startup. For instance, if you want to active all
489 subscribed newsgroups, but not the rest, you'd set this variable to 
490 `gnus-level-subscribed'.
491
492 If you subscribe to lots of newsgroups from different servers, startup
493 might take a while. By setting this variable to nil, you'll save time,
494 but you won't be told how many unread articles there are in the
495 groups.")
496
497 (defvar gnus-save-newsrc-file t
498   "*Non-nil means that Gnus will save the `.newsrc' file.
499 Gnus always saves its own startup file, which is called
500 \".newsrc.eld\".  The file called \".newsrc\" is in a format that can
501 be readily understood by other newsreaders.  If you don't plan on
502 using other newsreaders, set this variable to nil to save some time on
503 exit.")
504
505 (defvar gnus-save-killed-list t
506   "*If non-nil, save the list of killed groups to the startup file.
507 This will save both time (when starting and quitting) and space (both
508 memory and disk), but it will also mean that Gnus has no record of
509 which groups are new and which are old, so the automatic new
510 newsgroups subscription methods become meaningless. You should always
511 set `gnus-check-new-newsgroups' to `ask-server' or nil if you set this
512 variable to nil.")
513
514 (defvar gnus-interactive-catchup t
515   "*If non-nil, require your confirmation when catching up a group.")
516
517 (defvar gnus-interactive-post t
518   "*If non-nil, group name will be asked for when posting.")
519
520 (defvar gnus-interactive-exit t
521   "*If non-nil, require your confirmation when exiting Gnus.")
522
523 (defvar gnus-kill-killed nil
524   "*If non-nil, Gnus will apply kill files to already killed articles.
525 If it is nil, Gnus will never apply kill files to articles that have
526 already been through the scoring process, which might very well save lots
527 of time.")
528
529 (defvar gnus-extract-address-components 'gnus-extract-address-components
530   "*Function for extracting address components from a From header.
531 Two pre-defined function exist: `gnus-extract-address-components',
532 which is the default, quite fast, and too simplistic solution, and
533 `mail-extract-address-components', which works much better, but is
534 slower.")
535
536 (defvar gnus-summary-default-score 0
537   "*Default article score level.
538 If this variable is nil, scoring will be disabled.")
539
540 (defvar gnus-summary-zcore-fuzz 0
541   "*Fuzziness factor for the zcore in the summary buffer.
542 Articles with scores closer than this to `gnus-summary-default-score'
543 will not be marked.")
544
545 (defvar gnus-simplify-subject-fuzzy-regexp nil
546   "*Regular expression that will be removed from subject strings if
547 fuzzy subject simplification is selected.")
548
549 (defvar gnus-group-default-list-level gnus-level-subscribed
550   "*Default listing level. 
551 Ignored if `gnus-group-use-permanent-levels' is non-nil.")
552
553 (defvar gnus-group-use-permanent-levels nil
554   "*If non-nil, once you set a level, Gnus will use this level.")
555
556 (defvar gnus-show-mime nil
557   "*If non-nil, do mime processing of articles.
558 The articles will simply be fed to the function given by
559 `gnus-show-mime-method'.")
560
561 (defvar gnus-strict-mime t
562   "*If nil, decode MIME header even if there is not Mime-Version field.")
563  
564 (defvar gnus-show-mime-method (function metamail-buffer)
565   "*Function to process a MIME message.
566 The function is called from the article buffer.")
567
568 (defvar gnus-show-threads t
569   "*If non-nil, display threads in summary mode.")
570
571 (defvar gnus-thread-hide-subtree nil
572   "*If non-nil, hide all threads initially.
573 If threads are hidden, you have to run the command
574 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
575 to expose hidden threads.")
576
577 (defvar gnus-thread-hide-killed t
578   "*If non-nil, hide killed threads automatically.")
579
580 (defvar gnus-thread-ignore-subject nil
581   "*If non-nil, ignore subjects and do all threading based on the Reference header.
582 If nil, which is the default, articles that have different subjects
583 from their parents will start separate threads.")
584
585 (defvar gnus-thread-indent-level 4
586   "*Number that says how much each sub-thread should be indented.")
587
588 (defvar gnus-ignored-newsgroups ""
589   "*A regexp to match uninteresting newsgroups in the active file.
590 Any lines in the active file matching this regular expression are
591 removed from the newsgroup list before anything else is done to it,
592 thus making them effectively non-existent.")
593
594 (defvar gnus-ignored-headers
595   "^Path:\\|^Posting-Version:\\|^Article-I.D.:\\|^Expires:\\|^Date-Received:\\|^References:\\|^Control:\\|^Xref:\\|^Lines:\\|^Posted:\\|^Relay-Version:\\|^Message-ID:\\|^Nf-ID:\\|^Nf-From:\\|^Approved:\\|^Sender:\\|^Received:\\|^Mail-from:"
596   "*All headers that match this regexp will be hidden.
597 Also see `gnus-visible-headers'.")
598
599 (defvar gnus-visible-headers "^From:\\|^Newsgroups:\\|^Subject:\\|^Date:\\|^Followup-To:\\|^Reply-To:\\|^Organization:\\|^Summary:\\|^Keywords:\\|^To:\\|^Cc:"
600   "*All headers that do not match this regexp will be hidden.
601 Also see `gnus-ignored-headers'.")
602
603 (defvar gnus-sorted-header-list
604   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:" "^To:" 
605     "^Cc:" "^Date:" "^Organization:")
606   "*This variable is a list of regular expressions.
607 If it is non-nil, headers that match the regular expressions will
608 be placed first in the article buffer in the sequence specified by
609 this list.")
610
611 (defvar gnus-show-all-headers nil
612   "*If non-nil, don't hide any headers.")
613
614 (defvar gnus-save-all-headers t
615   "*If non-nil, don't remove any headers before saving.")
616
617 (defvar gnus-inhibit-startup-message nil
618   "*If non-nil, the startup message will not be displayed.")
619
620 (defvar gnus-signature-separator "^-- *$"
621   "Regexp matching signature separator.")
622
623 (defvar gnus-auto-extend-newsgroup t
624   "*If non-nil, extend newsgroup forward and backward when requested.")
625
626 (defvar gnus-auto-select-first t
627   "*If non-nil, select the first unread article when entering a group.
628 If you want to prevent automatic selection of the first unread article
629 in some newsgroups, set the variable to nil in
630 `gnus-select-group-hook'.") 
631
632 (defvar gnus-auto-select-next t
633   "*If non-nil, offer to go to the next group from the end of the previous.
634 If the value is t and the next newsgroup is empty, Gnus will exit
635 summary mode and go back to group mode.  If the value is neither nil
636 nor t, Gnus will select the following unread newsgroup.  In
637 particular, if the value is the symbol `quietly', the next unread
638 newsgroup will be selected without any confirmations.")
639
640 (defvar gnus-auto-select-same nil
641   "*If non-nil, select the next article with the same subject.")
642
643 (defvar gnus-summary-check-current nil
644   "*If non-nil, consider the current article when moving.
645 The \"unread\" movement commands will stay on the same line if the
646 current article is unread.")
647
648 (defvar gnus-auto-center-summary t
649   "*If non-nil, always center the current summary buffer.")
650
651 (defvar gnus-break-pages t
652   "*If non-nil, do page breaking on articles.
653 The page delimiter is specified by the `gnus-page-delimiter'
654 variable.")
655
656 (defvar gnus-page-delimiter "^\^L"
657   "*Regexp describing what to use as article page delimiters.
658 The default value is \"^\^L\", which is a form linefeed at the
659 beginning of a line.")
660
661 (defvar gnus-use-full-window t
662   "*If non-nil, use the entire Emacs screen.")
663
664 (defvar gnus-window-configuration nil
665   "Obsolete variable.  See `gnus-buffer-configuration'.")
666
667 (defvar gnus-buffer-configuration
668   '((group ([group 1.0 point] 
669             (if gnus-carpal [group-carpal 4])))
670     (summary ([summary 1.0 point]
671               (if gnus-carpal [summary-carpal 4])))
672     (article ([summary 0.25 point] 
673               (if gnus-carpal [summary-carpal 4]) 
674               [article 1.0]))
675     (server ([server 1.0 point]
676              (if gnus-carpal [server-carpal 2])))
677     (browse ([browse 1.0 point]
678              (if gnus-carpal [browse-carpal 2])))
679     (group-mail ([mail 1.0 point]))
680     (summary-mail ([mail 1.0 point]))
681     (summary-reply ([article 0.5]
682                     [mail 1.0 point]))
683     (info ([nil 1.0 point]))
684     (summary-faq ([summary 0.25]
685                   [faq 1.0 point]))
686     (edit-group ([group 0.5]
687                  [edit-group 1.0 point]))
688     (edit-server ([server 0.5]
689                   [edit-server 1.0 point]))
690     (edit-score ([summary 0.25]
691                  [edit-score 1.0 point]))
692     (post ([post 1.0 point]))
693     (reply ([article 0.5]
694             [mail 1.0 point]))
695     (mail-forward ([mail 1.0 point]))
696     (post-forward ([post 1.0 point]))
697     (reply-yank ([mail 1.0 point]))
698     (followup ([article 0.5]
699                [post 1.0 point]))
700     (followup-yank ([post 1.0 point])))
701   "Window configuration for all possible Gnus buffers.
702 This variable is a list of lists.  Each of these lists has a NAME and
703 a RULE.  The NAMEs are commonsense names like `group', which names a
704 rule used when displaying the group buffer; `summary', which names a
705 rule for what happens when you enter a group and do not display an
706 article buffer; and so on.  See the value of this variable for a
707 complete list of NAMEs.
708
709 Each RULE is a list of vectors.  The first element in this vector is
710 the name of the buffer to be displayed; the second element is the
711 percentage of the screen this buffer is to occupy (a number in the
712 0.0-0.99 range); the optional third element is `point', which should
713 be present to denote which buffer point is to go to after making this
714 buffer configuration.")
715
716 (defvar gnus-window-to-buffer
717   '((group . gnus-group-buffer)
718     (summary . gnus-summary-buffer)
719     (article . gnus-article-buffer)
720     (server . gnus-server-buffer)
721     (browse . "*Gnus Browse Server*")
722     (edit-group . gnus-group-edit-buffer)
723     (edit-server . gnus-server-edit-buffer)
724     (group-carpal . gnus-carpal-group-buffer)
725     (summary-carpal . gnus-carpal-summary-buffer)
726     (server-carpal . gnus-carpal-server-buffer)
727     (browse-carpal . gnus-carpal-browse-buffer)
728     (edit-score . gnus-score-edit-buffer)
729     (mail . gnus-mail-buffer)
730     (post . gnus-post-news-buffer)
731     (faq . gnus-faq-buffer))
732   "Mapping from short symbols to buffer names or buffer variables.")
733
734 (defvar gnus-carpal nil
735   "*If non-nil, display clickable icons.")
736
737 (defvar gnus-subscribe-newsgroup-method 'gnus-subscribe-zombies
738   "*Function called with a group name when new group is detected.
739 A few pre-made functions are supplied: `gnus-subscribe-randomly'
740 inserts new groups at the beginning of the list of groups;
741 `gnus-subscribe-alphabetically' inserts new groups in strict
742 alphabetic order; `gnus-subscribe-hierarchically' inserts new groups
743 in hierarchical newsgroup order; `gnus-subscribe-interactively' asks
744 for your decision.")
745
746 ;; Suggested by a bug report by Hallvard B Furuseth.
747 ;; <h.b.furuseth@usit.uio.no>. 
748 (defvar gnus-subscribe-options-newsgroup-method
749   (function gnus-subscribe-alphabetically)
750   "*This function is called to subscribe newsgroups mentioned on \"options -n\" lines.
751 If, for instance, you want to subscribe to all newsgroups in the
752 \"no\" and \"alt\" hierarchies, you'd put the following in your
753 .newsrc file:
754
755 options -n no.all alt.all
756
757 Gnus will the subscribe all new newsgroups in these hierarchies with
758 the subscription method in this variable.")
759
760 (defvar gnus-subscribe-hierarchical-interactive nil
761   "*If non-nil, Gnus will offer to subscribe hierarchically.
762 When a new hierarchy appears, Gnus will ask the user:
763
764 'alt.binaries': Do you want to subscribe to this hierarchy? ([d]ys):
765
766 If the user pressed `d', Gnus will descend the hierarchy, `y' will
767 subscribe to all newsgroups in the hierarchy and `s' will skip this
768 hierarchy in its entirety.")
769
770 (defvar gnus-group-sort-function 'gnus-group-sort-by-alphabet
771   "*Function used for sorting the group buffer.
772 This function will be called with group info entries as the arguments
773 for the groups to be sorted.  Pre-made functions include
774 `gnus-sort-by-alphabet', `gnus-sort-by-unread' and
775 `gnus-sort-by-level'")
776
777 ;; Mark variables suggested by Thomas Michanek
778 ;; <Thomas.Michanek@telelogic.se>. 
779 (defvar gnus-unread-mark ? 
780   "*Mark used for unread articles.")
781 (defvar gnus-ticked-mark ?!
782   "*Mark used for ticked articles.")
783 (defvar gnus-dormant-mark ??
784   "*Mark used for dormant articles.")
785 (defvar gnus-del-mark ?r
786   "*Mark used for del'd articles.")
787 (defvar gnus-read-mark ?R
788   "*Mark used for read articles.")
789 (defvar gnus-expirable-mark ?E
790   "*Mark used for expirable articles.")
791 (defvar gnus-killed-mark ?K
792   "*Mark used for killed articles.")
793 (defvar gnus-kill-file-mark ?X
794   "*Mark used for articles killed by kill files.")
795 (defvar gnus-low-score-mark ?Y
796   "*Mark used for articles with a low score.")
797 (defvar gnus-catchup-mark ?C
798   "*Mark used for articles that are caught up.")
799 (defvar gnus-replied-mark ?A
800   "*Mark used for articles that have been replied to.")
801 (defvar gnus-process-mark ?# 
802   "*Process mark.")
803 (defvar gnus-ancient-mark ?O
804   "*Mark used for ancient articles.")
805 (defvar gnus-canceled-mark ?G
806   "*Mark used for canceled articles.")
807 (defvar gnus-score-over-mark ?+
808   "*Score mark used for articles with high scores.")
809 (defvar gnus-score-below-mark ?-
810   "*Score mark used for articles with low scores.")
811 (defvar gnus-empty-thread-mark ? 
812   "*There is no thread under the article.")
813 (defvar gnus-not-empty-thread-mark ?=
814   "*There is a thread under the article.")
815 (defvar gnus-dummy-mark ?Z
816   "*This is a dummy article.")
817
818 (defvar gnus-view-pseudo-asynchronously nil
819   "*If non-nil, Gnus will view pseudo-articles asynchronously.")
820
821 (defvar gnus-view-pseudos nil
822   "*If `automatic', pseudo-articles will be viewed automatically.
823 If `not-confirm', pseudos will be viewed automatically, and the user
824 will not be asked to confirm the command.")
825
826 (defvar gnus-view-pseudos-separately t
827   "*If non-nil, one pseudo-article will be created for each file to be viewed.
828 If nil, all files that use the same viewing command will be given as a
829 list of parameters to that command.")
830
831 (defvar gnus-group-line-format "%M%S%p%5y: %(%g%)\n"
832   "*Format of group lines.
833 It works along the same lines as a normal formatting string,
834 with some simple extensions.
835
836 %M    Only marked articles (character, \"*\" or \" \")
837 %S    Whether the group is subscribed (character, \"U\", \"K\", \"Z\" or \" \")
838 %L    Level of subscribedness (integer)
839 %N    Number of unread articles (integer)
840 %I    Number of dormant articles (integer)
841 %i    Number of ticked and dormant (integer)
842 %T    Number of ticked articles (integer)
843 %R    Number of read articles (integer)
844 %t    Total number of articles (integer)
845 %y    Number of unread, unticked articles (integer)
846 %G    Group name (string)
847 %g    Qualified group name (string)
848 %D    Group description (string)
849 %s    Select method (string)
850 %o    Moderated group (char, \"m\")
851 %p    Process mark (char)
852 %O    Moderated group (string, \"(m)\" or \"\")
853 %n    Select from where (string)
854 %z    A string that look like `<%s:%n>' if a foreign select method is used
855 %u    User defined specifier. The next character in the format string should
856       be a letter.  Gnus will call the function gnus-user-format-function-X,
857       where X is the letter following %u. The function will be passed the
858       current header as argument. The function should return a string, which
859       will be inserted into the buffer just like information from any other
860       group specifier.
861
862 Text between %( and %) will be highlighted with `gnus-mouse-face' when
863 the mouse point move inside the area.  There can only be one such area.
864
865 Note that this format specification is not always respected. For
866 reasons of efficiency, when listing killed groups, this specification
867 is ignored altogether. If the spec is changed considerably, your
868 output may end up looking strange when listing both alive and killed
869 groups.
870
871 If you use %o or %O, reading the active file will be slower and quite
872 a bit of extra memory will be used. %D will also worsen performance.
873 Also note that if you change the format specification to include any
874 of these specs, you must probably re-start Gnus to see them go into
875 effect.") 
876
877 (defvar gnus-summary-line-format "%U%R%z%I%(%[%4L: %-20,20n%]%) %s\n"
878   "*The format specification of the lines in the summary buffer.
879
880 It works along the same lines as a normal formatting string,
881 with some simple extensions.
882
883 %N   Article number, left padded with spaces (string)
884 %S   Subject (string)
885 %s   Subject if it is at the root of a thread, and \"\" otherwise (string)
886 %n   Name of the poster (string)
887 %a   Extracted name of the poster (string)
888 %A   Extracted address of the poster (string)
889 %F   Contents of the From: header (string)
890 %x   Contents of the Xref: header (string)
891 %D   Date of the article (string)
892 %d   Date of the article (string) in DD-MMM format
893 %M   Message-id of the article (string)
894 %r   References of the article (string)
895 %c   Number of characters in the article (integer)
896 %L   Number of lines in the article (integer)
897 %I   Indentation based on thread level (a string of spaces)
898 %T   A string with two possible values: 80 spaces if the article
899      is on thread level two or larger and 0 spaces on level one
900 %R   \"R\" if this article has been replied to, \" \" otherwise (character)
901 %U   Status of this article (character, \"D\", \"K\", \"-\" or \" \")
902 %[   Opening bracket (character, \"[\" or \"<\")
903 %]   Closing bracket (character, \"]\" or \">\")
904 %>   Spaces of length thread-level (string)
905 %<   Spaces of length (- 20 thread-level) (string)
906 %i   Article score (number)
907 %z   Article zcore (character)
908 %t   Number of articles under the current thread (number).
909 %e   Whether the thread is empty or not (character).
910 %u   User defined specifier. The next character in the format string should
911      be a letter.  Gnus will call the function gnus-user-format-function-X,
912      where X is the letter following %u. The function will be passed the
913      current header as argument. The function should return a string, which
914      will be inserted into the summary just like information from any other
915      summary specifier.
916
917 Text between %( and %) will be highlighted with `gnus-mouse-face'
918 when the mouse point is placed inside the area.  There can only be one
919 such area.
920
921 The %U (status), %R (replied) and %z (zcore) specs have to be handled
922 with care. For reasons of efficiency, Gnus will compute what column
923 these characters will end up in, and \"hard-code\" that. This means that
924 it is illegal to have these specs after a variable-length spec. Well,
925 you might not be arrested, but your summary buffer will look strange,
926 which is bad enough.
927
928 The smart choice is to have these specs as for to the left as
929 possible. 
930
931 This restriction may disappear in later versions of Gnus.")
932
933 (defvar gnus-summary-dummy-line-format "*  :                          : %S\n"
934   "*The format specification for the dummy roots in the summary buffer.
935 It works along the same lines as a normal formatting string,
936 with some simple extensions.
937
938 %S  The subject")
939
940 (defvar gnus-summary-mode-line-format "(ding) %G/%A %Z"
941   "*The format specification for the summary mode line.")
942
943 (defvar gnus-article-mode-line-format "(ding) %G/%A %S"
944   "*The format specification for the article mode line.")
945
946 (defvar gnus-group-mode-line-format "(ding) List of groups   {%M:%S}  "
947   "*The format specification for the group mode line.")
948
949 (defvar gnus-valid-select-methods
950   '(("nntp" post address prompt-address)
951     ("nnspool" post)
952     ("nnvirtual" none virtual prompt-address) 
953     ("nnmbox" mail respool) 
954     ("nnml" mail respool)
955     ("nnmh" mail respool) 
956     ("nndir" none prompt-address address)
957     ("nneething" none prompt-address)
958     ("nndigest" none) 
959     ("nndoc" none prompt-address) 
960     ("nnbabyl" mail respool) 
961     ("nnkiboze" post virtual) 
962     ("nnsoup" post)
963     ("nnfolder" mail respool))
964   "An alist of valid select methods.
965 The first element of each list lists should be a string with the name
966 of the select method. The other elements may be be the category of
967 this method (ie. `post', `mail', `none' or whatever) or other
968 properties that this method has (like being respoolable).
969 If you implement a new select method, all you should have to change is
970 this variable. I think.")
971
972 (defvar gnus-updated-mode-lines '(group article summary)
973   "*List of buffers that should update their mode lines.
974 The list may contain the symbols `group', `article' and `summary'. If
975 the corresponding symbol is present, Gnus will keep that mode line
976 updated with information that may be pertinent. 
977 If this variable is nil, screen refresh may be quicker.")
978
979 ;; Added by Keinonen Kari <kk85613@cs.tut.fi>.
980 (defvar gnus-mode-non-string-length 21
981   "*Max length of mode-line non-string contents.
982 If this is nil, Gnus will take space as is needed, leaving the rest
983 of the modeline intact.")
984
985 (defvar gnus-mouse-face 'highlight
986   "*Face used for mouse highlighting in Gnus.
987 No mouse highlights will be done if `gnus-visual' is nil.")
988
989 (defvar gnus-summary-mark-below nil
990   "*Mark all articles with a score below this variable as read.
991 This variable is local to each summary buffer and usually set by the
992 score file.")  
993
994 (defvar gnus-thread-sort-functions '(gnus-thread-sort-by-number)
995   "*List of functions used for sorting threads in the summary buffer.
996 By default, threads are sorted by article number.
997
998 Each function takes two threads and return non-nil if the first thread
999 should be sorted before the other.  If you use more than one function,
1000 the primary sort function should be the last.
1001
1002 Ready-mady functions include `gnus-thread-sort-by-number',
1003 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
1004 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
1005 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').")
1006
1007 (defvar gnus-thread-score-function '+
1008   "*Function used for calculating the total score of a thread.
1009
1010 The function is called with the scores of the article and each
1011 subthread and should then return the score of the thread.
1012
1013 Some functions you can use are `+', `max', or `min'.")
1014
1015 (defvar gnus-options-subscribe nil
1016   "*All new groups matching this regexp will be subscribed unconditionally.
1017 Note that this variable deals only with new newsgroups.  This variable
1018 does not affect old newsgroups.")
1019
1020 (defvar gnus-options-not-subscribe nil
1021   "*All new groups matching this regexp will be ignored.
1022 Note that this variable deals only with new newsgroups.  This variable
1023 does not affect old (already subscribed) newsgroups.")
1024
1025 (defvar gnus-auto-expirable-newsgroups nil
1026   "*Groups in which to automatically mark read articles as expirable.
1027 If non-nil, this should be a regexp that should match all groups in
1028 which to perform auto-expiry.  This only makes sense for mail groups.")
1029
1030 (defvar gnus-hidden-properties '(invisible t intangible t)
1031   "Property list to use for hiding text.")
1032
1033 ;; Hooks.
1034
1035 (defvar gnus-group-mode-hook nil
1036   "*A hook for Gnus group mode.")
1037
1038 (defvar gnus-summary-mode-hook nil
1039   "*A hook for Gnus summary mode.
1040 This hook is run before any variables are set in the summary buffer.")
1041
1042 (defvar gnus-article-mode-hook nil
1043   "*A hook for Gnus article mode.")
1044
1045 (defun gnus-summary-prepare-exit-hook nil
1046   "*A hook called when preparing to exit from the summary buffer.
1047 It calls `gnus-summary-expire-articles' by default.")
1048 (add-hook 'gnus-summary-prepare-exit-hook 'gnus-summary-expire-articles)
1049
1050 (defun gnus-summary-exit-hook nil
1051   "*A hook called on exit from the summary buffer.")
1052
1053 (defvar gnus-open-server-hook nil
1054   "*A hook called just before opening connection to the news server.")
1055
1056 (defvar gnus-startup-hook nil
1057   "*A hook called at startup.
1058 This hook is called after Gnus is connected to the NNTP server.")
1059
1060 (defvar gnus-get-new-news-hook nil
1061   "*A hook run just before Gnus checks for new news.")
1062
1063 (defvar gnus-group-prepare-function 'gnus-group-prepare-flat
1064   "*A function that is called to generate the group buffer.
1065 The function is called with three arguments: The first is a number;
1066 all group with a level less or equal to that number should be listed,
1067 if the second is non-nil, empty groups should also be displayed. If
1068 the third is non-nil, it is a number. No groups with a level lower
1069 than this number should be displayed.
1070
1071 The only current function implemented is `gnus-group-prepare-flat'.")
1072
1073 (defvar gnus-group-prepare-hook nil
1074   "*A hook called after the group buffer has been generated.
1075 If you want to modify the group buffer, you can use this hook.")
1076
1077 (defvar gnus-summary-prepare-hook nil
1078   "*A hook called after the summary buffer has been generated.
1079 If you want to modify the summary buffer, you can use this hook.")
1080
1081 (defvar gnus-article-prepare-hook nil
1082   "*A hook called after an article has been prepared in the article buffer.
1083 If you want to run a special decoding program like nkf, use this hook.")
1084
1085 (defvar gnus-article-display-hook nil
1086   "*A hook called after the article is displayed in the article buffer.
1087 The hook is designed to change the contents of the article
1088 buffer. Typical functions that this hook may contain are
1089 `gnus-article-hide-headers' (hide selected headers),
1090 `gnus-article-maybe-highlight' (perform fancy article highlighting), 
1091 `gnus-article-hide-signature' (hide signature) and
1092 `gnus-article-treat-overstrike' (turn \"^H_\" into bold characters).")
1093 (add-hook 'gnus-article-display-hook 'gnus-article-hide-headers-if-wanted)
1094 (add-hook 'gnus-article-display-hook 'gnus-article-treat-overstrike)
1095
1096 (defvar gnus-article-x-face-command
1097   "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | xv -quit -"
1098   "String or function to be executed to display an X-Face header.
1099 If it is a string, the command will be executed in a sub-shell
1100 asynchronously. The compressed face will be piped to this command.") 
1101
1102 (defvar gnus-article-x-face-too-ugly nil
1103   "Regexp matching posters whose face shouldn't be shown automatically.")
1104
1105 (defvar gnus-select-group-hook nil
1106   "*A hook called when a newsgroup is selected.
1107
1108 If you'd like to simplify subjects like the
1109 `gnus-summary-next-same-subject' command does, you can use the
1110 following hook:
1111
1112  (setq gnus-select-group-hook
1113       (list
1114         (lambda ()
1115           (mapcar (lambda (header)
1116                      (header-set-subject
1117                       header
1118                       (gnus-simplify-subject
1119                        (header-subject header) 're-only)))
1120                   gnus-newsgroup-headers))))")
1121
1122 (defvar gnus-select-article-hook
1123   '(gnus-summary-show-thread)
1124   "*A hook called when an article is selected.
1125 The default hook shows conversation thread subtrees of the selected
1126 article automatically using `gnus-summary-show-thread'.")
1127
1128 (defvar gnus-apply-kill-hook '(gnus-apply-kill-file)
1129   "*A hook called to apply kill files to a group.
1130 This hook is intended to apply a kill file to the selected newsgroup.
1131 The function `gnus-apply-kill-file' is called by default.
1132
1133 Since a general kill file is too heavy to use only for a few
1134 newsgroups, I recommend you to use a lighter hook function. For
1135 example, if you'd like to apply a kill file to articles which contains
1136 a string `rmgroup' in subject in newsgroup `control', you can use the
1137 following hook:
1138
1139 \(setq gnus-apply-kill-hook
1140       (list
1141         (lambda ()
1142           (cond ((string-match \"control\" gnus-newsgroup-name)
1143                  (gnus-kill \"Subject\" \"rmgroup\")
1144                  (gnus-expunge \"X\"))))))")
1145
1146 (defvar gnus-visual-mark-article-hook 
1147   (list 'gnus-highlight-selected-summary)
1148   "*Hook run after selecting an article in the summary buffer.
1149 It is meant to be used for highlighting the article in some way.  It
1150 is not run if `gnus-visual' is nil.")
1151
1152 (defvar gnus-prepare-article-hook (list 'gnus-inews-insert-signature)
1153   "*A hook called after preparing body, but before preparing header headers.
1154 The default hook (`gnus-inews-insert-signature') inserts a signature
1155 file specified by the variable `gnus-signature-file'.")
1156
1157 (defvar gnus-exit-group-hook nil
1158   "*A hook called when exiting (not quitting) summary mode.")
1159
1160 (defvar gnus-suspend-gnus-hook nil
1161   "*A hook called when suspending (not exiting) Gnus.")
1162
1163 (defvar gnus-exit-gnus-hook nil
1164   "*A hook called when exiting Gnus.")
1165
1166 (defvar gnus-save-newsrc-hook nil
1167   "*A hook called when saving the newsrc file.")
1168
1169 (defvar gnus-summary-update-hook 
1170   (list 'gnus-summary-highlight-line)
1171   "*A hook called when a summary line is changed.
1172 The hook will not be called if `gnus-visual' is nil.
1173
1174 The default function `gnus-summary-highlight-line' will
1175 highlight the line according to the `gnus-summary-highlight'
1176 variable.")
1177
1178 (defvar gnus-mark-article-hook (list 'gnus-summary-mark-unread-as-read)
1179   "*A hook called when an article is selected for the first time.
1180 The hook is intended to mark an article as read (or unread)
1181 automatically when it is selected.")
1182
1183 ;; Remove any hilit infestation.
1184 (add-hook 'gnus-startup-hook
1185           (lambda ()
1186             (remove-hook 'gnus-summary-prepare-hook
1187                          'hilit-rehighlight-buffer-quietly)
1188             (remove-hook 'gnus-summary-prepare-hook 'hilit-install-line-hooks)
1189             (setq gnus-mark-article-hook '(gnus-summary-mark-unread-as-read))
1190             (remove-hook 'gnus-article-prepare-hook
1191                          'hilit-rehighlight-buffer-quietly)))
1192
1193
1194 \f
1195 ;; Internal variables
1196
1197 ;; Avoid highlighting in kill files.
1198 (defvar gnus-summary-inhibit-highlight nil)
1199 (defvar gnus-newsgroup-selected-overlay nil)
1200
1201 (defvar gnus-article-mode-map nil)
1202 (defvar gnus-dribble-buffer nil)
1203 (defvar gnus-headers-retrieved-by nil)
1204 (defvar gnus-article-reply nil)
1205 (defvar gnus-override-method nil)
1206 (defvar gnus-article-check-size nil)
1207
1208 (defvar gnus-current-score-file nil)
1209 (defvar gnus-internal-global-score-files nil)
1210 (defvar gnus-score-file-list nil)
1211
1212
1213 (defvar gnus-current-move-group nil)
1214
1215 (defvar gnus-newsgroup-dependencies nil)
1216 (defvar gnus-newsgroup-threads nil)
1217 (defvar gnus-newsgroup-async nil)
1218 (defconst gnus-group-edit-buffer "*Gnus edit newsgroup*")
1219
1220 (defvar gnus-newsgroup-adaptive nil)
1221
1222 (defvar gnus-summary-display-table nil)
1223
1224 (defconst gnus-group-line-format-alist
1225   (list (list ?M 'marked ?c)
1226         (list ?S 'subscribed ?c)
1227         (list ?L 'level ?d)
1228         (list ?N 'number ?s)
1229         (list ?I 'number-of-dormant ?d)
1230         (list ?T 'number-of-ticked ?d)
1231         (list ?R 'number-of-read ?s)
1232         (list ?t 'number-total ?d)
1233         (list ?y 'number-of-unread-unticked ?s)
1234         (list ?i 'number-of-ticked-and-dormant ?d)
1235         (list ?g 'group ?s)
1236         (list ?G 'qualified-group ?s)
1237         (list ?D 'newsgroup-description ?s)
1238         (list ?o 'moderated ?c)
1239         (list ?O 'moderated-string ?s)
1240         (list ?p 'process-marked ?c)
1241         (list ?s 'news-server ?s)
1242         (list ?n 'news-method ?s)
1243         (list ?z 'news-method-string ?s)
1244         (list ?u 'user-defined ?s)))
1245
1246 (defconst gnus-summary-line-format-alist 
1247   (list (list ?N 'number ?d)
1248         (list ?S 'subject ?s)
1249         (list ?s 'subject-or-nil ?s)
1250         (list ?n 'name ?s)
1251         (list ?A '(car (cdr (funcall gnus-extract-address-components from)))
1252               ?s)
1253         (list ?a '(or (car (funcall gnus-extract-address-components from)) 
1254                       from) ?s)
1255         (list ?F 'from ?s)
1256         (list ?x (macroexpand '(header-xref header)) ?s)
1257         (list ?D (macroexpand '(header-date header)) ?s)
1258         (list ?d '(gnus-dd-mmm (header-date header)) ?s)
1259         (list ?M (macroexpand '(header-id header)) ?s)
1260         (list ?r (macroexpand '(header-references header)) ?s)
1261         (list ?c '(or (header-chars header) 0) ?d)
1262         (list ?L 'lines ?d)
1263         (list ?I 'indentation ?s)
1264         (list ?T '(if (= level 0) "" (make-string (frame-width) ? )) ?s)
1265         (list ?R 'replied ?c)
1266         (list ?\[ 'opening-bracket ?c)
1267         (list ?\] 'closing-bracket ?c)
1268         (list ?\> '(make-string level ? ) ?s)
1269         (list ?\< '(make-string (max 0 (- 20 level)) ? ) ?s)
1270         (list ?i 'score ?d)
1271         (list ?z 'score-char ?c)
1272         (list ?U 'unread ?c)
1273         (list ?t '(gnus-summary-number-of-articles-in-thread 
1274                    (and (boundp 'thread) (car thread)))
1275               ?d)
1276         (list ?e '(gnus-summary-number-of-articles-in-thread 
1277                    (and (boundp 'thread) (car thread)) t)
1278               ?c)
1279         (list ?u 'user-defined ?s))
1280   "An alist of format specifications that can appear in summary lines,
1281 and what variables they correspond with, along with the type of the
1282 variable (string, integer, character, etc).")
1283
1284 (defconst gnus-summary-dummy-line-format-alist
1285   (list (list ?S 'subject ?s)
1286         (list ?N 'number ?d)
1287         (list ?u 'user-defined ?s)))
1288
1289 (defconst gnus-summary-mode-line-format-alist 
1290   (list (list ?G 'group-name ?s)
1291         (list ?g '(gnus-short-group-name group-name) ?s)
1292         (list ?A 'article-number ?d)
1293         (list ?Z 'unread-and-unselected ?s)
1294         (list ?V 'gnus-version ?s)
1295         (list ?U 'unread ?d)
1296         (list ?S 'subject ?s)
1297         (list ?e 'unselected ?d)
1298         (list ?u 'user-defined ?s)
1299         (list ?s '(gnus-current-score-file-nondirectory) ?s)))
1300
1301 (defconst gnus-group-mode-line-format-alist 
1302   (list (list ?S 'news-server ?s)
1303         (list ?M 'news-method ?s)
1304         (list ?u 'user-defined ?s)))
1305
1306 (defvar gnus-have-read-active-file nil)
1307
1308 (defconst gnus-maintainer "gnus-bug@ifi.uio.no (The Gnus Bugfixing Girls + Boys)"
1309   "The mail address of the Gnus maintainers.")
1310
1311 (defconst gnus-version "(ding) Gnus v0.99.4"
1312   "Version number for this version of Gnus.")
1313
1314 (defvar gnus-info-nodes
1315   '((gnus-group-mode            "(gnus)The Group Buffer")
1316     (gnus-summary-mode          "(gnus)The Summary Buffer")
1317     (gnus-article-mode          "(gnus)The Article Buffer"))
1318   "Assoc list of major modes and related Info nodes.")
1319
1320 (defvar gnus-documentation-group-file "~/dgnus/lisp/doc.txt"
1321   "The location of the (ding) Gnus documentation group.")
1322
1323 (defvar gnus-group-buffer "*Group*")
1324 (defvar gnus-summary-buffer "*Summary*")
1325 (defvar gnus-article-buffer "*Article*")
1326 (defvar gnus-server-buffer "*Server*")
1327
1328 (defvar gnus-work-buffer " *gnus work*")
1329
1330 (defvar gnus-buffer-list nil
1331   "Gnus buffers that should be killed on exit.")
1332
1333 (defvar gnus-server-alist nil
1334   "List of available servers.")
1335
1336 (defvar gnus-variable-list
1337   '(gnus-newsrc-options gnus-newsrc-options-n
1338     gnus-newsrc-last-checked-date 
1339     gnus-newsrc-alist gnus-server-alist
1340     gnus-killed-list gnus-zombie-list)
1341   "Gnus variables saved in the quick startup file.")
1342
1343 (defvar gnus-overload-functions
1344   '((news-inews gnus-inews-news "rnewspost"))
1345   "Functions overloaded by gnus.
1346 It is a list of `(original overload &optional file)'.")
1347
1348 (defvar gnus-newsrc-options nil
1349   "Options line in the .newsrc file.")
1350
1351 (defvar gnus-newsrc-options-n nil
1352   "List of regexps representing groups to be subscribed/ignored unconditionally.") 
1353
1354 (defvar gnus-newsrc-last-checked-date nil
1355   "Date Gnus last asked server for new newsgroups.")
1356
1357 (defvar gnus-newsrc-alist nil
1358   "Assoc list of read articles.
1359 gnus-newsrc-hashtb should be kept so that both hold the same information.")
1360
1361 (defvar gnus-newsrc-hashtb nil
1362   "Hashtable of gnus-newsrc-alist.")
1363
1364 (defvar gnus-killed-list nil
1365   "List of killed newsgroups.")
1366
1367 (defvar gnus-killed-hashtb nil
1368   "Hash table equivalent of gnus-killed-list.")
1369
1370 (defvar gnus-zombie-list nil
1371   "List of almost dead newsgroups.")
1372
1373 (defvar gnus-description-hashtb nil
1374   "Descriptions of newsgroups.")
1375
1376 (defvar gnus-list-of-killed-groups nil
1377   "List of newsgroups that have recently been killed by the user.")
1378
1379 (defvar gnus-active-hashtb nil
1380   "Hashtable of active articles.")
1381
1382 (defvar gnus-moderated-list nil
1383   "List of moderated newsgroups.")
1384
1385 (defvar gnus-group-marked nil)
1386
1387 (defvar gnus-current-startup-file nil
1388   "Startup file for the current host.")
1389
1390 (defvar gnus-last-search-regexp nil
1391   "Default regexp for article search command.")
1392
1393 (defvar gnus-last-shell-command nil
1394   "Default shell command on article.")
1395
1396 (defvar gnus-current-select-method nil
1397   "The current method for selecting a newsgroup.")
1398
1399 (defvar gnus-have-all-newsgroups nil)
1400
1401 (defvar gnus-article-internal-prepare-hook nil)
1402
1403 (defvar gnus-newsgroup-name nil)
1404 (defvar gnus-newsgroup-begin nil)
1405 (defvar gnus-newsgroup-end nil)
1406 (defvar gnus-newsgroup-last-rmail nil)
1407 (defvar gnus-newsgroup-last-mail nil)
1408 (defvar gnus-newsgroup-last-folder nil)
1409 (defvar gnus-newsgroup-last-file nil)
1410 (defvar gnus-newsgroup-auto-expire nil)
1411 (defvar gnus-newsgroup-active nil)
1412
1413 (defvar gnus-newsgroup-unreads nil
1414   "List of unread articles in the current newsgroup.")
1415
1416 (defvar gnus-newsgroup-unselected nil
1417   "List of unselected unread articles in the current newsgroup.")
1418
1419 (defvar gnus-newsgroup-marked nil
1420   "List of ticked articles in the current newsgroup (a subset of unread art).")
1421
1422 (defvar gnus-newsgroup-killed nil
1423   "List of ranges of articles that have been through the scoring process.")
1424
1425 (defvar gnus-newsgroup-kill-headers nil)
1426
1427 (defvar gnus-newsgroup-replied nil
1428   "List of articles that have been replied to in the current newsgroup.")
1429
1430 (defvar gnus-newsgroup-expirable nil
1431   "List of articles in the current newsgroup that can be expired.")
1432
1433 (defvar gnus-newsgroup-processable nil
1434   "List of articles in the current newsgroup that can be processed.")
1435
1436 (defvar gnus-newsgroup-bookmarks nil
1437   "List of articles in the current newsgroup that have bookmarks.")
1438
1439 (defvar gnus-newsgroup-dormant nil
1440   "List of dormant articles in the current newsgroup.")
1441
1442 (defvar gnus-newsgroup-scored nil
1443   "List of scored articles in the current newsgroup.")
1444
1445 (defvar gnus-newsgroup-headers nil
1446   "List of article headers in the current newsgroup.")
1447 (defvar gnus-newsgroup-headers-hashtb-by-number nil)
1448
1449 (defvar gnus-newsgroup-ancient nil
1450   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1451
1452 (defvar gnus-current-article nil)
1453 (defvar gnus-article-current nil)
1454 (defvar gnus-current-headers nil)
1455 (defvar gnus-have-all-headers nil)
1456 (defvar gnus-last-article nil)
1457 (defvar gnus-newsgroup-history nil)
1458 (defvar gnus-current-kill-article nil)
1459
1460 ;; Save window configuration.
1461 (defvar gnus-prev-winconf nil)
1462
1463 ;; Format specs
1464 (defvar gnus-summary-line-format-spec nil)
1465 (defvar gnus-summary-dummy-line-format-spec nil)
1466 (defvar gnus-group-line-format-spec nil)
1467 (defvar gnus-summary-mode-line-format-spec nil)
1468 (defvar gnus-article-mode-line-format-spec nil)
1469 (defvar gnus-group-mode-line-format-spec nil)
1470 (defvar gnus-summary-mark-positions nil)
1471
1472 (defvar gnus-summary-expunge-below nil)
1473 (defvar gnus-reffed-article-number nil)
1474
1475 (defvar rmail-default-file (expand-file-name "~/XMBOX"))
1476 (defvar rmail-default-rmail-file (expand-file-name "~/XNEWS"))
1477
1478 (defvar gnus-cache-removeable-articles nil)
1479
1480 (defconst gnus-summary-local-variables 
1481   '(gnus-newsgroup-name 
1482     gnus-newsgroup-begin gnus-newsgroup-end 
1483     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail 
1484     gnus-newsgroup-last-folder gnus-newsgroup-last-file 
1485     gnus-newsgroup-auto-expire gnus-newsgroup-unreads 
1486     gnus-newsgroup-unselected gnus-newsgroup-marked
1487     gnus-newsgroup-replied gnus-newsgroup-expirable
1488     gnus-newsgroup-processable gnus-newsgroup-killed
1489     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1490     gnus-newsgroup-headers gnus-newsgroup-headers-hashtb-by-number
1491     gnus-current-article gnus-current-headers gnus-have-all-headers
1492     gnus-last-article gnus-article-internal-prepare-hook
1493     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1494     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1495     gnus-newsgroup-threads gnus-newsgroup-async
1496     gnus-score-alist gnus-current-score-file gnus-summary-expunge-below 
1497     gnus-summary-mark-below gnus-newsgroup-active gnus-scores-exclude-files
1498     gnus-newsgroup-history gnus-newsgroup-ancient
1499     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1500     gnus-cache-removeable-articles)
1501   "Variables that are buffer-local to the summary buffers.")
1502
1503 (defconst gnus-bug-message
1504   "Sending a bug report to the Gnus Towers.
1505 ========================================
1506
1507 The buffer below is a mail buffer.  When you press `C-c C-c', it will
1508 be sent to the Gnus Bug Exterminators. 
1509
1510 At the bottom of the buffer you'll see lots of variable settings.
1511 Please do not delete those.  They will tell the Bug People what your
1512 environment is, so that it will be easier to locate the bugs.
1513
1514 If you have found a bug that makes Emacs go \"beep\", set
1515 debug-on-error to t (`M-x set-variable RET debug-on-error RET t RET') 
1516 and include the backtrace in your bug report.
1517
1518 Please describe the bug in annoying, painstaking detail.
1519
1520 Thank you for your help in stamping out bugs.
1521 ")
1522
1523 ;;; End of variables.
1524
1525 ;; Define some autoload functions Gnus might use.
1526 (eval-and-compile
1527
1528   ;; Various 
1529   (autoload 'metamail-buffer "metamail")
1530   (autoload 'Info-goto-node "info")
1531   (autoload 'hexl-hex-string-to-integer "hexl")
1532   (autoload 'pp "pp")
1533   (autoload 'pp-to-string "pp")
1534   (autoload 'pp-eval-expression "pp")
1535   (autoload 'mail-extract-address-components "mail-extr")
1536
1537   (autoload 'nnmail-split-fancy "nnmail")
1538   (autoload 'nnvirtual-catchup-group "nnvirtual")
1539
1540   ;; timezone
1541   (autoload 'timezone-make-date-arpa-standard "timezone")
1542   (autoload 'timezone-fix-time "timezone")
1543   (autoload 'timezone-make-sortable-date "timezone")
1544   (autoload 'timezone-make-time-string "timezone")
1545
1546   ;; rmail & friends
1547   (autoload 'mail-position-on-field "sendmail")
1548   (autoload 'mail-setup "sendmail")
1549   (autoload 'rmail-output "rmailout")
1550   (autoload 'news-mail-other-window "rnewspost")
1551   (autoload 'news-reply-yank-original "rnewspost")
1552   (autoload 'news-caesar-buffer-body "rnewspost")
1553   (autoload 'rmail-insert-rmail-file-header "rmail")
1554   (autoload 'rmail-count-new-messages "rmail")
1555   (autoload 'rmail-show-message "rmail")
1556
1557   ;; gnus-soup
1558   (autoload 'gnus-group-brew-soup "gnus-soup" nil t)
1559   (autoload 'gnus-brew-soup "gnus-soup" nil t)
1560   (autoload 'gnus-soup-add-article "gnus-soup" nil t)
1561   (autoload 'gnus-soup-send-replies "gnus-soup" nil t)
1562   (autoload 'gnus-soup-save-areas "gnus-soup" nil t)
1563   (autoload 'gnus-soup-pack-packet "gnus-soup" nil t)
1564   (autoload 'nnsoup-pack-replies "nnsoup" nil t)
1565
1566   ;; gnus-mh
1567   (autoload 'gnus-mail-reply-using-mhe "gnus-mh")
1568   (autoload 'gnus-mail-forward-using-mhe "gnus-mh")
1569   (autoload 'gnus-mail-other-window-using-mhe "gnus-mh")
1570   (autoload 'gnus-summary-save-in-folder "gnus-mh")
1571   (autoload 'gnus-summary-save-article-folder "gnus-mh")
1572   (autoload 'gnus-Folder-save-name "gnus-mh")
1573   (autoload 'gnus-folder-save-name "gnus-mh")
1574
1575   ;; gnus-vis misc
1576   (autoload 'gnus-group-make-menu-bar "gnus-vis")
1577   (autoload 'gnus-summary-make-menu-bar "gnus-vis")
1578   (autoload 'gnus-server-make-menu-bar "gnus-vis")
1579   (autoload 'gnus-article-make-menu-bar "gnus-vis")
1580   (autoload 'gnus-browse-make-menu-bar "gnus-vis")
1581   (autoload 'gnus-highlight-selected-summary "gnus-vis")
1582   (autoload 'gnus-summary-highlight-line "gnus-vis")
1583   (autoload 'gnus-carpal-setup-buffer "gnus-vis")
1584
1585   ;; gnus-vis article
1586   (autoload 'gnus-article-push-button "gnus-vis" nil t)
1587   (autoload 'gnus-article-press-button "gnus-vis" nil t)
1588   (autoload 'gnus-article-highlight "gnus-vis" nil t)
1589   (autoload 'gnus-article-hide "gnus-vis" nil t)
1590   (autoload 'gnus-article-hide-signature "gnus-vis" nil t)
1591   (autoload 'gnus-article-highlight-headers "gnus-vis" nil t)
1592   (autoload 'gnus-article-highlight-signature "gnus-vis" nil t)
1593   (autoload 'gnus-article-add-buttons "gnus-vis" nil t)
1594   (autoload 'gnus-article-next-button "gnus-vis" nil t)
1595   (autoload 'gnus-article-add-button "gnus-vis")
1596
1597   ;; gnus-cite
1598   (autoload 'gnus-article-highlight-citation "gnus-cite" nil t)
1599   (autoload 'gnus-article-hide-citation-maybe "gnus-cite" nil t)
1600   (autoload 'gnus-article-hide-citation "gnus-cite" nil t)
1601
1602   ;; gnus-kill
1603   (autoload 'gnus-kill "gnus-kill")
1604   (autoload 'gnus-apply-kill-file-internal "gnus-kill")
1605   (autoload 'gnus-kill-file-edit-file "gnus-kill")
1606   (autoload 'gnus-kill-file-raise-followups-to-author "gnus-kill")
1607   (autoload 'gnus-execute "gnus-kill")
1608   (autoload 'gnus-expunge "gnus-kill")
1609
1610   ;; gnus-cache
1611   (autoload 'gnus-cache-possibly-enter-article "gnus-cache")
1612   (autoload 'gnus-cache-save-buffers "gnus-cache")
1613   (autoload 'gnus-cache-possibly-remove-articles "gnus-cache")
1614   (autoload 'gnus-cache-request-article "gnus-cache")
1615   (autoload 'gnus-cache-retrieve-headers "gnus-cache")
1616   (autoload 'gnus-cache-possibly-alter-active "gnus-cache")
1617   (autoload 'gnus-jog-cache "gnus-cache" nil t)
1618   (autoload 'gnus-cache-enter-remove-article "gnus-cache")
1619
1620   ;; gnus-score
1621   (autoload 'gnus-summary-increase-score "gnus-score" nil t)
1622   (autoload 'gnus-summary-lower-score "gnus-score" nil t)
1623   (autoload 'gnus-summary-score-map "gnus-score" nil nil 'keymap)
1624   (autoload 'gnus-score-save "gnus-score")
1625   (autoload 'gnus-score-headers "gnus-score")
1626   (autoload 'gnus-current-score-file-nondirectory "gnus-score")
1627   (autoload 'gnus-score-adaptive "gnus-score")
1628   (autoload 'gnus-score-remove-lines-adaptive "gnus-score")
1629   (autoload 'gnus-score-find-trace "gnus-score")
1630
1631   ;; gnus-edit
1632   (autoload 'gnus-score-customize "gnus-edit" nil t)
1633
1634   ;; gnus-uu
1635   (autoload 'gnus-uu-extract-map "gnus-uu" nil nil 'keymap)
1636   (autoload 'gnus-uu-mark-map "gnus-uu" nil nil 'keymap)
1637   (autoload 'gnus-uu-digest-mail-forward "gnus-uu" nil t)
1638   (autoload 'gnus-uu-digest-post-forward "gnus-uu" nil t)
1639   (autoload 'gnus-uu-mark-series "gnus-uu" nil t)
1640   (autoload 'gnus-uu-mark-region "gnus-uu" nil t)
1641   (autoload 'gnus-uu-mark-by-regexp "gnus-uu" nil t)
1642   (autoload 'gnus-uu-mark-all "gnus-uu" nil t)
1643   (autoload 'gnus-uu-mark-sparse "gnus-uu" nil t)
1644   (autoload 'gnus-uu-mark-thread "gnus-uu" nil t)
1645   (autoload 'gnus-uu-decode-uu "gnus-uu" nil t)
1646   (autoload 'gnus-uu-decode-uu-and-save "gnus-uu" nil t)
1647   (autoload 'gnus-uu-decode-unshar "gnus-uu" nil t)
1648   (autoload 'gnus-uu-decode-unshar-and-save "gnus-uu" nil t)
1649   (autoload 'gnus-uu-decode-save "gnus-uu" nil t)
1650   (autoload 'gnus-uu-decode-binhex "gnus-uu" nil t)
1651   (autoload 'gnus-uu-decode-uu-view "gnus-uu" nil t)
1652   (autoload 'gnus-uu-decode-uu-and-save-view "gnus-uu" nil t)
1653   (autoload 'gnus-uu-decode-unshar-view "gnus-uu" nil t)
1654   (autoload 'gnus-uu-decode-unshar-and-save-view "gnus-uu" nil t)
1655   (autoload 'gnus-uu-decode-save-view "gnus-uu" nil t)
1656   (autoload 'gnus-uu-decode-binhex-view "gnus-uu" nil t)
1657
1658   ;; gnus-msg
1659   (autoload 'gnus-summary-send-map "gnus-msg" nil nil 'keymap)
1660   (autoload 'gnus-group-post-news "gnus-msg" nil t)
1661   (autoload 'gnus-group-mail "gnus-msg" nil t)
1662   (autoload 'gnus-summary-post-news "gnus-msg" nil t)
1663   (autoload 'gnus-summary-followup "gnus-msg" nil t)
1664   (autoload 'gnus-summary-followup-with-original "gnus-msg" nil t)
1665   (autoload 'gnus-summary-followup-and-reply "gnus-msg" nil t)
1666   (autoload 'gnus-summary-followup-and-reply-with-original "gnus-msg" nil t)
1667   (autoload 'gnus-summary-cancel-article "gnus-msg" nil t)
1668   (autoload 'gnus-summary-supersede-article "gnus-msg" nil t)
1669   (autoload 'gnus-post-news "gnus-msg" nil t)
1670   (autoload 'gnus-inews-news "gnus-msg" nil t)
1671   (autoload 'gnus-cancel-news "gnus-msg" nil t)
1672   (autoload 'gnus-summary-reply "gnus-msg" nil t)
1673   (autoload 'gnus-summary-reply-with-original "gnus-msg" nil t)
1674   (autoload 'gnus-summary-mail-forward "gnus-msg" nil t)
1675   (autoload 'gnus-summary-mail-other-window "gnus-msg" nil t)
1676   (autoload 'gnus-mail-reply-using-mail "gnus-msg")
1677   (autoload 'gnus-mail-yank-original "gnus-msg")
1678   (autoload 'gnus-mail-send-and-exit "gnus-msg")
1679   (autoload 'gnus-mail-forward-using-mail "gnus-msg")
1680   (autoload 'gnus-mail-other-window-using-mail "gnus-msg")
1681   (autoload 'gnus-article-mail-with-original "gnus-msg")
1682   (autoload 'gnus-article-mail "gnus-msg")
1683   (autoload 'gnus-bug "gnus-msg" nil t)
1684
1685   ;; gnus-vm
1686   (autoload 'gnus-summary-save-in-vm "gnus-vm" nil t)
1687   (autoload 'gnus-summary-save-article-vm "gnus-vm" nil t)
1688   (autoload 'gnus-mail-forward-using-vm "gnus-vm")
1689   (autoload 'gnus-mail-reply-using-vm "gnus-vm")
1690   (autoload 'gnus-mail-other-window-using-vm "gnus-vm" nil t)
1691   (autoload 'gnus-yank-article "gnus-vm" nil t)
1692
1693   )
1694
1695 \f
1696
1697 ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
1698 ;; If you want the cursor to go somewhere else, set these two
1699 ;; functions in some startup hook to whatever you want.
1700 (defalias 'gnus-summary-position-cursor 'gnus-goto-colon)
1701 (defalias 'gnus-group-position-cursor 'gnus-goto-colon)
1702
1703 ;;; Various macros and substs.
1704
1705 (defmacro gnus-eval-in-buffer-window (buffer &rest forms)
1706   "Pop to BUFFER, evaluate FORMS, and then returns to original window."
1707   (` (let ((GnusStartBufferWindow (selected-window)))
1708        (unwind-protect
1709            (progn
1710              (pop-to-buffer (, buffer))
1711              (,@ forms))
1712          (select-window GnusStartBufferWindow)))))
1713
1714 (defmacro gnus-gethash (string hashtable)
1715   "Get hash value of STRING in HASHTABLE."
1716   ;;(` (symbol-value (abbrev-symbol (, string) (, hashtable))))
1717   ;;(` (abbrev-expansion (, string) (, hashtable)))
1718   (` (symbol-value (intern-soft (, string) (, hashtable)))))
1719
1720 (defmacro gnus-sethash (string value hashtable)
1721   "Set hash value. Arguments are STRING, VALUE, and HASHTABLE."
1722   ;; We cannot use define-abbrev since it only accepts string as value.
1723   ;; (set (intern string hashtable) value))
1724   (` (set (intern (, string) (, hashtable)) (, value))))
1725
1726 (defsubst gnus-buffer-substring (beg end)
1727   (buffer-substring (match-beginning beg) (match-end end)))
1728
1729 ;; modified by MORIOKA Tomohiko <morioka@jaist.ac.jp>
1730 ;;   function `substring' might cut on a middle of multi-octet
1731 ;;   character.
1732 (defun gnus-truncate-string (str width)
1733   (substring str 0 width))
1734
1735 ;; Added by Geoffrey T. Dairiki <dairiki@u.washington.edu>. A safe way
1736 ;; to limit the length of a string. This function is necessary since
1737 ;; `(substr "abc" 0 30)' pukes with "Args out of range".
1738 (defsubst gnus-limit-string (str width)
1739   (if (> (length str) width)
1740       (substring str 0 width)
1741     str))
1742
1743 (defsubst gnus-simplify-subject-re (subject)
1744   "Remove \"Re:\" from subject lines."
1745   (let ((case-fold-search t))
1746     (if (string-match "^re: *" subject)
1747         (substring subject (match-end 0))
1748       subject)))
1749
1750 (defsubst gnus-goto-char (point)
1751   (and point (goto-char point)))
1752
1753 (defmacro gnus-buffer-exists-p (buffer)
1754   (` (and (, buffer)
1755           (funcall (if (stringp (, buffer)) 'get-buffer 'buffer-name)
1756                    (, buffer)))))
1757
1758 (defmacro gnus-kill-buffer (buffer)
1759   (` (if (gnus-buffer-exists-p (, buffer))
1760          (kill-buffer (, buffer)))))
1761
1762 (defsubst gnus-point-at-bol ()
1763   "Return point at the beginning of line."
1764   (let ((p (point)))
1765     (beginning-of-line)
1766     (prog1
1767         (point)
1768       (goto-char p))))
1769
1770 (defsubst gnus-point-at-eol ()
1771   "Return point at the beginning of line."
1772   (let ((p (point)))
1773     (end-of-line)
1774     (prog1
1775         (point)
1776       (goto-char p))))
1777
1778 ;; Delete the current line (and the next N lines.);
1779 (defmacro gnus-delete-line (&optional n)
1780   (` (delete-region (progn (beginning-of-line) (point))
1781                     (progn (forward-line (, (or n 1))) (point)))))
1782
1783 ;;; Load the compatability functions. 
1784
1785 (require 'gnus-ems)
1786
1787 \f
1788 ;;;
1789 ;;; Gnus Utility Functions
1790 ;;;
1791
1792 (defun gnus-extract-address-components (from)
1793   (let (name address)
1794     ;; First find the address - the thing with the @ in it.  This may
1795     ;; not be accurate in mail addresses, but does the trick most of
1796     ;; the time in news messages.
1797     (if (string-match "\\b[^@ \t<>]+[!@][^@ \t<>]+\\b" from)
1798         (setq address (substring from (match-beginning 0) (match-end 0))))
1799     ;; Then we check whether the "name <address>" format is used.
1800     (and address
1801          (string-match (concat "<" (regexp-quote address) ">") from)
1802          (and (setq name (substring from 0 (1- (match-beginning 0))))
1803               ;; Strip any quotes from the name.
1804               (string-match "\".*\"" name)
1805               (setq name (substring name 1 (1- (match-end 0))))))
1806     ;; If not, then "address (name)" is used.
1807     (or name
1808         (and (string-match "(.+)" from)
1809              (setq name (substring from (1+ (match-beginning 0)) 
1810                                    (1- (match-end 0)))))
1811         (and (string-match "()" from)
1812              (setq name address))
1813         ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>.
1814         ;; XOVER might not support folded From headers.
1815         (and (string-match "(.*" from)
1816              (setq name (substring from (1+ (match-beginning 0)) 
1817                                    (match-end 0)))))
1818     ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
1819     (list (or name from) (or address from))))
1820
1821 (defun gnus-fetch-field (field)
1822   "Return the value of the header FIELD of current article."
1823   (save-excursion
1824     (save-restriction
1825       (let ((case-fold-search t))
1826         (gnus-narrow-to-headers)
1827         (mail-fetch-field field)))))
1828
1829 (defun gnus-goto-colon ()
1830   (beginning-of-line)
1831   (search-forward ":" (gnus-point-at-eol) t))
1832
1833 (defun gnus-narrow-to-headers ()
1834   (widen)
1835   (save-excursion
1836     (narrow-to-region
1837      (goto-char (point-min))
1838      (if (search-forward "\n\n" nil t)
1839          (1- (point))
1840        (point-max)))))
1841
1842 (defvar gnus-old-specs nil)
1843
1844 (defun gnus-update-format-specifications ()
1845   (gnus-make-thread-indent-array)
1846
1847   (let ((formats '(summary summary-dummy group 
1848                            summary-mode group-mode article-mode))
1849         old-format new-format)
1850     (while formats
1851       (setq new-format (symbol-value
1852                         (intern (format "gnus-%s-line-format" (car formats)))))
1853       (or (and (setq old-format (cdr (assq (car formats) gnus-old-specs)))
1854                (equal old-format new-format))
1855           (set (intern (format "gnus-%s-line-format-spec" (car formats)))
1856                (gnus-parse-format
1857                 new-format
1858                 (symbol-value 
1859                  (intern (format "gnus-%s-line-format-alist"
1860                                  (if (eq (car formats) 'article-mode)
1861                                      'summary-mode (car formats))))))))
1862       (setq gnus-old-specs (cons (cons (car formats) new-format)
1863                                  (delq (car formats) gnus-old-specs)))
1864       (setq formats (cdr formats))))
1865       
1866   (gnus-update-summary-mark-positions)
1867
1868   (if (and (string-match "%D" gnus-group-line-format)
1869            (not gnus-description-hashtb)
1870            gnus-read-active-file)
1871       (gnus-read-all-descriptions-files)))
1872
1873 (defun gnus-update-summary-mark-positions ()
1874   (save-excursion
1875     (let ((gnus-replied-mark 129)
1876           (gnus-score-below-mark 130)
1877           (gnus-score-over-mark 130)
1878           (thread nil)
1879           pos)
1880       (gnus-set-work-buffer)
1881       (gnus-summary-insert-line 
1882        nil [0 "" "" "" "" "" 0 0 ""]  0 nil 128 t nil "" nil 1)
1883       (goto-char (point-min))
1884       (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
1885                                          (- (point) 2)))))
1886       (goto-char (point-min))
1887       (setq pos (cons (cons 'replied (and (search-forward "\201" nil t)
1888                                           (- (point) 2))) pos))
1889       (goto-char (point-min))
1890       (setq pos (cons (cons 'score (and (search-forward "\202" nil t)
1891                                         (- (point) 2))) pos))
1892       (setq gnus-summary-mark-positions pos))))
1893
1894 (defun gnus-mouse-face-function (form)
1895   (` (let ((string (, form)))
1896        (put-text-property 0 (length string) 'mouse-face gnus-mouse-face string)
1897        string)))
1898
1899 (defun gnus-max-width-function (el max-width)
1900   (or (numberp max-width) (signal 'wrong-type-argument '(numberp max-width)))
1901   (` (let* ((val (eval (, el)))
1902             (valstr (if (numberp val)
1903                         (int-to-string val) val)))
1904        (if (> (length valstr) (, max-width))
1905            (substring valstr 0 (, max-width))
1906          valstr))))
1907
1908 (defun gnus-parse-format (format spec-alist)
1909   ;; This function parses the FORMAT string with the help of the
1910   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
1911   ;; string.  If the FORMAT string contains the specifiers %( and %)
1912   ;; the text between them will have the mouse-face text property.
1913   (if (string-match "\\`\\(.*\\)%(\\(.*\\)%)\\(.*\n?\\)\\'" format)
1914       (if (and gnus-visual gnus-mouse-face)
1915           (let ((pre (substring format (match-beginning 1) (match-end 1)))
1916                 (button (substring format (match-beginning 2) (match-end 2)))
1917                 (post (substring format (match-beginning 3) (match-end 3))))
1918             (list 'concat
1919                   (gnus-parse-simple-format pre spec-alist)
1920                   (gnus-mouse-face-function 
1921                    (gnus-parse-simple-format button spec-alist))
1922                   (gnus-parse-simple-format post spec-alist)))
1923         (gnus-parse-simple-format
1924          (concat (substring format (match-beginning 1) (match-end 1))
1925                  (substring format (match-beginning 2) (match-end 2))
1926                  (substring format (match-beginning 3) (match-end 3)))
1927          spec-alist))
1928     (gnus-parse-simple-format format spec-alist)))
1929
1930 (defun gnus-parse-simple-format (format spec-alist)
1931   ;; This function parses the FORMAT string with the help of the
1932   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
1933   ;; string. The list will consist of the symbol `format', a format
1934   ;; specification string, and a list of forms depending on the
1935   ;; SPEC-ALIST.
1936   (let ((max-width 0)
1937         spec flist fstring newspec elem beg)
1938     (save-excursion
1939       (gnus-set-work-buffer)
1940       (insert format)
1941       (goto-char (point-min))
1942       (while (re-search-forward "%[-0-9]*\\(,[0-9]*\\)*\\(.\\)\\(.\\)?" nil t)
1943         (setq spec (string-to-char (buffer-substring (match-beginning 2)
1944                                                      (match-end 2))))
1945         ;; First check if there are any specs that look anything like
1946         ;; "%12,12A", ie. with a "max width specification". These have
1947         ;; to be treated specially.
1948         (if (setq beg (match-beginning 1))
1949             (setq max-width 
1950                   (string-to-int 
1951                    (buffer-substring (1+ (match-beginning 1)) (match-end 1))))
1952           (setq max-width 0)
1953           (setq beg (match-beginning 2)))
1954         ;; Find the specification from `spec-alist'.
1955         (if (not (setq elem (cdr (assq spec spec-alist))))
1956             (setq elem '("*" ?s)))
1957         ;; Treat user defined format specifiers specially
1958         (and (eq (car elem) 'user-defined)
1959              (setq elem
1960                    (list 
1961                     (list (intern (concat "gnus-user-format-function-"
1962                                           (buffer-substring
1963                                            (match-beginning 3)
1964                                            (match-end 3))))
1965                           'header)
1966                     ?s))
1967              (delete-region (match-beginning 3) (match-end 3)))
1968         (if (not (zerop max-width))
1969             (let ((el (car elem)))
1970               (cond ((= (car (cdr elem)) ?c) 
1971                      (setq el (list 'char-to-string el)))
1972                     ((= (car (cdr elem)) ?d)
1973                      (numberp el) (setq el (list 'int-to-string el))))
1974               (setq flist (cons (gnus-max-width-function el max-width)
1975                                 flist))
1976               (setq newspec ?s))
1977           (setq flist (cons (car elem) flist))
1978           (setq newspec (car (cdr elem))))
1979         ;; Remove the old specification (and possibly a ",12" string).
1980         (delete-region beg (match-end 2))
1981         ;; Insert the new specification.
1982         (goto-char beg)
1983         (insert newspec))
1984       (setq fstring (buffer-substring 1 (point-max))))
1985     (cons 'format (cons fstring (nreverse flist)))))
1986
1987 ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
1988 (defun gnus-read-init-file ()
1989   (and gnus-init-file
1990        (or (and (file-exists-p gnus-init-file) 
1991                 ;; Don't try to load a directory.
1992                 (not (file-directory-p gnus-init-file)))
1993            (file-exists-p (concat gnus-init-file ".el"))
1994            (file-exists-p (concat gnus-init-file ".elc")))
1995        (load gnus-init-file nil t)))
1996
1997 (defun gnus-set-work-buffer ()
1998   (if (get-buffer gnus-work-buffer)
1999       (progn
2000         (set-buffer gnus-work-buffer)
2001         (erase-buffer))
2002     (set-buffer (get-buffer-create gnus-work-buffer))
2003     (kill-all-local-variables)
2004     (buffer-disable-undo (current-buffer))
2005     (gnus-add-current-to-buffer-list)))
2006
2007 ;; Article file names when saving.
2008
2009 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
2010   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2011 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group/num.
2012 Otherwise, it is like ~/News/news/group/num."
2013   (let ((default
2014           (expand-file-name
2015            (concat (if (gnus-use-long-file-name 'not-save)
2016                        (gnus-capitalize-newsgroup newsgroup)
2017                      (gnus-newsgroup-directory-form newsgroup))
2018                    "/" (int-to-string (header-number headers)))
2019            (or gnus-article-save-directory "~/News"))))
2020     (if (and last-file
2021              (string-equal (file-name-directory default)
2022                            (file-name-directory last-file))
2023              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2024         default
2025       (or last-file default))))
2026
2027 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
2028   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2029 If variable `gnus-use-long-file-name' is nil, it is ~/News/news.group/num.
2030 Otherwise, it is like ~/News/news/group/num."
2031   (let ((default
2032           (expand-file-name
2033            (concat (if (gnus-use-long-file-name 'not-save)
2034                        newsgroup
2035                      (gnus-newsgroup-directory-form newsgroup))
2036                    "/" (int-to-string (header-number headers)))
2037            (or gnus-article-save-directory "~/News"))))
2038     (if (and last-file
2039              (string-equal (file-name-directory default)
2040                            (file-name-directory last-file))
2041              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2042         default
2043       (or last-file default))))
2044
2045 (defun gnus-Plain-save-name (newsgroup headers &optional last-file)
2046   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2047 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group.
2048 Otherwise, it is like ~/News/news/group/news."
2049   (or last-file
2050       (expand-file-name
2051        (if (gnus-use-long-file-name 'not-save)
2052            (gnus-capitalize-newsgroup newsgroup)
2053          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2054        (or gnus-article-save-directory "~/News"))))
2055
2056 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
2057   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2058 If variable `gnus-use-long-file-name' is nil, it is ~/News/news.group.
2059 Otherwise, it is like ~/News/news/group/news."
2060   (or last-file
2061       (expand-file-name
2062        (if (gnus-use-long-file-name 'not-save)
2063            newsgroup
2064          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2065        (or gnus-article-save-directory "~/News"))))
2066
2067 ;; For subscribing new newsgroup
2068
2069 (defun gnus-subscribe-hierarchical-interactive (groups)
2070   (let ((groups (sort groups 'string<))
2071         prefixes prefix start ans group starts)
2072     (while groups
2073       (setq prefixes (list "^"))
2074       (while (and groups prefixes)
2075         (while (not (string-match (car prefixes) (car groups)))
2076           (setq prefixes (cdr prefixes)))
2077         (setq prefix (car prefixes))
2078         (setq start (1- (length prefix)))
2079         (if (and (string-match "[^\\.]\\." (car groups) start)
2080                  (cdr groups)
2081                  (setq prefix 
2082                        (concat "^" (substring (car groups) 0 (match-end 0))))
2083                  (string-match prefix (car (cdr groups))))
2084             (progn
2085               (setq prefixes (cons prefix prefixes))
2086               (message "Descend hierarchy %s? ([y]nsq): " 
2087                        (substring prefix 1 (1- (length prefix))))
2088               (setq ans (read-char))
2089               (cond ((= ans ?n)
2090                      (while (and groups 
2091                                  (string-match prefix 
2092                                                (setq group (car groups))))
2093                        (setq gnus-killed-list 
2094                              (cons group gnus-killed-list))
2095                        (gnus-sethash group group gnus-killed-hashtb)
2096                        (setq groups (cdr groups)))
2097                      (setq starts (cdr starts)))
2098                     ((= ans ?s)
2099                      (while (and groups 
2100                                  (string-match prefix 
2101                                                (setq group (car groups))))
2102                        (gnus-sethash group group gnus-killed-hashtb)
2103                        (gnus-subscribe-alphabetically (car groups))
2104                        (setq groups (cdr groups)))
2105                      (setq starts (cdr starts)))
2106                     ((= ans ?q)
2107                      (while groups
2108                        (setq group (car groups))
2109                        (setq gnus-killed-list (cons group gnus-killed-list))
2110                        (gnus-sethash group group gnus-killed-hashtb)
2111                        (setq groups (cdr groups))))
2112                     (t nil)))
2113           (message "Subscribe %s? ([n]yq)" (car groups))
2114           (setq ans (read-char))
2115           (setq group (car groups))
2116           (cond ((= ans ?y)
2117                  (gnus-subscribe-alphabetically (car groups))
2118                  (gnus-sethash group group gnus-killed-hashtb))
2119                 ((= ans ?q)
2120                  (while groups
2121                    (setq group (car groups))
2122                    (setq gnus-killed-list (cons group gnus-killed-list))
2123                    (gnus-sethash group group gnus-killed-hashtb)
2124                    (setq groups (cdr groups))))
2125                 (t 
2126                  (setq gnus-killed-list (cons group gnus-killed-list))
2127                  (gnus-sethash group group gnus-killed-hashtb)))
2128           (setq groups (cdr groups)))))))
2129
2130 (defun gnus-subscribe-randomly (newsgroup)
2131   "Subscribe new NEWSGROUP by making it the first newsgroup."
2132   (gnus-subscribe-newsgroup newsgroup))
2133
2134 (defun gnus-subscribe-alphabetically (newgroup)
2135   "Subscribe new NEWSGROUP and insert it in alphabetical order."
2136   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2137   (let ((groups (cdr gnus-newsrc-alist))
2138         before)
2139     (while (and (not before) groups)
2140       (if (string< newgroup (car (car groups)))
2141           (setq before (car (car groups)))
2142         (setq groups (cdr groups))))
2143     (gnus-subscribe-newsgroup newgroup before)))
2144
2145 (defun gnus-subscribe-hierarchically (newgroup)
2146   "Subscribe new NEWSGROUP and insert it in hierarchical newsgroup order."
2147   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2148   (save-excursion
2149     (set-buffer (find-file-noselect gnus-current-startup-file))
2150     (let ((groupkey newgroup)
2151           before)
2152       (while (and (not before) groupkey)
2153         (goto-char (point-min))
2154         (let ((groupkey-re
2155                (concat "^\\(" (regexp-quote groupkey) ".*\\)[!:]")))
2156           (while (and (re-search-forward groupkey-re nil t)
2157                       (progn
2158                         (setq before (buffer-substring
2159                                       (match-beginning 1) (match-end 1)))
2160                         (string< before newgroup)))))
2161         ;; Remove tail of newsgroup name (eg. a.b.c -> a.b)
2162         (setq groupkey
2163               (if (string-match "^\\(.*\\)\\.[^.]+$" groupkey)
2164                   (substring groupkey (match-beginning 1) (match-end 1)))))
2165       (gnus-subscribe-newsgroup newgroup before))))
2166
2167 (defun gnus-subscribe-interactively (newsgroup)
2168   "Subscribe new NEWSGROUP interactively.
2169 It is inserted in hierarchical newsgroup order if subscribed. If not,
2170 it is killed."
2171   (if (gnus-y-or-n-p (format "Subscribe new newsgroup: %s " newsgroup))
2172       (gnus-subscribe-hierarchically newsgroup)
2173     (setq gnus-killed-list (cons newsgroup gnus-killed-list))))
2174
2175 (defun gnus-subscribe-zombies (newsgroup)
2176   "Make new NEWSGROUP a zombie group."
2177   (setq gnus-zombie-list (cons newsgroup gnus-zombie-list)))
2178
2179 (defun gnus-subscribe-newsgroup (newsgroup &optional next)
2180   "Subscribe new NEWSGROUP.
2181 If NEXT is non-nil, it is inserted before NEXT. Otherwise it is made
2182 the first newsgroup."
2183   ;; We subscribe the group by changing its level to `subscribed'.
2184   (gnus-group-change-level 
2185    newsgroup gnus-level-default-subscribed
2186    gnus-level-killed (gnus-gethash (or next "dummy.group") gnus-newsrc-hashtb))
2187   (gnus-message 5 "Subscribe newsgroup: %s" newsgroup))
2188
2189 ;; For directories
2190
2191 (defun gnus-newsgroup-directory-form (newsgroup)
2192   "Make hierarchical directory name from NEWSGROUP name."
2193   (let ((newsgroup (substring newsgroup 0)) ;Copy string.
2194         (len (length newsgroup))
2195         idx)
2196     ;; If this is a foreign group, we don't want to translate the
2197     ;; entire name.  
2198     (if (setq idx (string-match ":" newsgroup))
2199         (aset newsgroup idx ?/)
2200       (setq idx 0))
2201     ;; Replace all occurrences of `.' with `/'.
2202     (while (< idx len)
2203       (if (= (aref newsgroup idx) ?.)
2204           (aset newsgroup idx ?/))
2205       (setq idx (1+ idx)))
2206     newsgroup))
2207
2208 (defun gnus-make-directory (dir)
2209   "Make DIRECTORY recursively."
2210   (let* ((dir (expand-file-name dir default-directory))
2211          dirs)
2212     (if (string-match "/$" dir)
2213         (setq dir (substring dir 0 (match-beginning 0))))
2214     (while (not (file-exists-p dir))
2215       (setq dirs (cons dir dirs))
2216       (string-match "/[^/]+$" dir)
2217       (setq dir (substring dir 0 (match-beginning 0))))
2218     (while dirs
2219       (make-directory (car dirs))
2220       (setq dirs (cdr dirs)))))
2221
2222 (defun gnus-capitalize-newsgroup (newsgroup)
2223   "Capitalize NEWSGROUP name."
2224   (and (not (zerop (length newsgroup)))
2225        (concat (char-to-string (upcase (aref newsgroup 0)))
2226                (substring newsgroup 1))))
2227
2228 ;; Var
2229
2230 (defun gnus-simplify-subject (subject &optional re-only)
2231   "Remove `Re:' and words in parentheses.
2232 If optional argument RE-ONLY is non-nil, strip `Re:' only."
2233   (let ((case-fold-search t))           ;Ignore case.
2234     ;; Remove `Re:' and `Re^N:'.
2235     (if (string-match "^re:[ \t]*" subject)
2236         (setq subject (substring subject (match-end 0))))
2237     ;; Remove words in parentheses from end.
2238     (or re-only
2239         (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
2240           (setq subject (substring subject 0 (match-beginning 0)))))
2241     ;; Return subject string.
2242     subject))
2243
2244 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
2245 ;; all whitespace.
2246 (defun gnus-simplify-subject-fuzzy (subject)
2247   (let ((case-fold-search t))
2248     (save-excursion
2249       (gnus-set-work-buffer)
2250       (insert subject)
2251       (inline (gnus-simplify-buffer-fuzzy))
2252       (buffer-string))))
2253
2254 (defun gnus-simplify-buffer-fuzzy ()
2255   (goto-char (point-min))
2256   ;; Fix by Stainless Steel Rat <ratinox@ccs.neu.edu>.
2257   (while (re-search-forward "^[ \t]*\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;][ \t]*"
2258                             nil t)
2259     (replace-match "" t t))
2260   (goto-char (point-min))
2261   (while (re-search-forward "[ \t\n]*([^()]*)[ \t\n]*$" nil t)
2262     (replace-match "" t t))
2263   (goto-char (point-min))
2264   (while (re-search-forward "[ \t]+" nil t)
2265     (replace-match " " t t))
2266   (goto-char (point-min))
2267   (while (re-search-forward "[ \t]+$" nil t)
2268     (replace-match "" t t))
2269   (goto-char (point-min))
2270   (while (re-search-forward "^[ \t]+" nil t)
2271     (replace-match "" t t))
2272   (if gnus-simplify-subject-fuzzy-regexp
2273       (while (re-search-forward gnus-simplify-subject-fuzzy-regexp nil t)
2274         (replace-match "" t t))))
2275
2276 ;; Add the current buffer to the list of buffers to be killed on exit. 
2277 (defun gnus-add-current-to-buffer-list ()
2278   (or (memq (current-buffer) gnus-buffer-list)
2279       (setq gnus-buffer-list (cons (current-buffer) gnus-buffer-list))))
2280
2281 (defun gnus-string> (s1 s2)
2282   (not (or (string< s1 s2)
2283            (string= s1 s2))))
2284
2285 ;; Functions accessing headers.
2286 ;; Functions are more convenient than macros in some cases.
2287
2288 (defun gnus-header-number (header)
2289   (header-number header))
2290
2291 (defun gnus-header-subject (header)
2292   (header-subject header))
2293
2294 (defun gnus-header-from (header)
2295   (header-from header))
2296
2297 (defun gnus-header-xref (header)
2298   (header-xref header))
2299
2300 (defun gnus-header-lines (header)
2301   (header-lines header))
2302
2303 (defun gnus-header-date (header)
2304   (header-date header))
2305
2306 (defun gnus-header-id (header)
2307   (header-id header))
2308
2309 (defun gnus-header-references (header)
2310   (header-references header))
2311
2312 ;;; General various misc type functions.
2313
2314 (defun gnus-clear-system ()
2315   "Clear all variables and buffers."
2316   ;; Clear Gnus variables.
2317   (let ((variables gnus-variable-list))
2318     (while variables
2319       (set (car variables) nil)
2320       (setq variables (cdr variables))))
2321   ;; Clear other internal variables.
2322   (setq gnus-list-of-killed-groups nil
2323         gnus-have-read-active-file nil
2324         gnus-newsrc-alist nil
2325         gnus-newsrc-hashtb nil
2326         gnus-killed-list nil
2327         gnus-zombie-list nil
2328         gnus-killed-hashtb nil
2329         gnus-active-hashtb nil
2330         gnus-moderated-list nil
2331         gnus-description-hashtb nil
2332         gnus-newsgroup-headers nil
2333         gnus-newsgroup-headers-hashtb-by-number nil
2334         gnus-newsgroup-name nil
2335         gnus-server-alist nil
2336         gnus-current-select-method nil)
2337   ;; Reset any score variables.
2338   (and (boundp 'gnus-score-cache)
2339        (set 'gnus-score-cache nil))
2340   (and (boundp 'gnus-internal-global-score-files)
2341        (set 'gnus-internal-global-score-files nil))
2342   ;; Kill the startup file.
2343   (and gnus-current-startup-file
2344        (get-file-buffer gnus-current-startup-file)
2345        (kill-buffer (get-file-buffer gnus-current-startup-file)))
2346   ;; Save any cache buffers.
2347   (and gnus-use-cache (gnus-cache-save-buffers))
2348   ;; Clear the dribble buffer.
2349   (gnus-dribble-clear)
2350   ;; Kill global KILL file buffer.
2351   (if (get-file-buffer (gnus-newsgroup-kill-file nil))
2352       (kill-buffer (get-file-buffer (gnus-newsgroup-kill-file nil))))
2353   (gnus-kill-buffer nntp-server-buffer)
2354   ;; Kill Gnus buffers.
2355   (while gnus-buffer-list
2356     (gnus-kill-buffer (car gnus-buffer-list))
2357     (setq gnus-buffer-list (cdr gnus-buffer-list))))
2358
2359 (defun gnus-windows-old-to-new (setting)
2360   (if (symbolp setting)
2361       (setq setting 
2362             (cond ((eq setting 'SelectArticle)
2363                    'article)
2364                   ((eq setting 'SelectSubject)
2365                    'summary)
2366                   ((eq setting 'SelectNewsgroup)
2367                    'group)
2368                   (t setting))))
2369   (if (or (listp setting)
2370           (not (and gnus-window-configuration
2371                     (memq setting '(group summary article)))))
2372       setting
2373     (let* ((setting (if (eq setting 'group) 
2374                         (if (assq 'newsgroup gnus-window-configuration)
2375                             'newsgroup
2376                           'newsgroups) setting))
2377            (elem (car (cdr (assq setting gnus-window-configuration))))
2378            (total (apply '+ elem))
2379            (types '(group summary article))
2380            (pbuf (if (eq setting 'newsgroups) 'group 'summary))
2381            (i 0)
2382            perc
2383            out)
2384       (while (< i 3)
2385         (or (zerop (nth i elem))
2386             (progn
2387               (setq perc  (/ (* 1.0 (nth 0 elem)) total))
2388               (setq out (cons (if (eq pbuf (nth i types))
2389                                   (vector (nth i types) perc 'point)
2390                                 (vector (nth i types) perc))
2391                               out))))
2392         (setq i (1+ i)))
2393       (list (nreverse out)))))
2394            
2395 (defun gnus-add-configuration (conf)
2396   (setq gnus-buffer-configuration 
2397         (cons conf (delq (assq (car conf) gnus-buffer-configuration)
2398                          gnus-buffer-configuration))))
2399
2400 (defun gnus-configure-windows (setting &optional force)
2401   (setq setting (gnus-windows-old-to-new setting))
2402   (let ((r (if (symbolp setting)
2403                (cdr (assq setting gnus-buffer-configuration))
2404              setting))
2405         (in-buf (current-buffer))
2406         rule val w height hor ohor heights sub jump-buffer
2407         rel total to-buf all-visible)
2408     (or r (error "No such setting: %s" setting))
2409
2410     (if (and (not force) (setq all-visible (gnus-all-windows-visible-p r)))
2411         ;; All the windows mentioned are already visibe, so we just
2412         ;; put point in the assigned buffer, and do not touch the
2413         ;; winconf. 
2414         (select-window (get-buffer-window all-visible))
2415
2416       ;; Either remove all windows or just remove all Gnus windows.
2417       (if gnus-use-full-window
2418           (delete-other-windows)
2419         (gnus-remove-some-windows)
2420         (switch-to-buffer nntp-server-buffer))
2421
2422       (while r
2423         (setq hor (car r)
2424               ohor nil)
2425
2426         ;; We have to do the (possible) horizontal splitting before the
2427         ;; vertical. 
2428         (if (and (listp (car hor)) 
2429                  (eq (car (car hor)) 'horizontal))
2430             (progn
2431               (split-window 
2432                nil
2433                (if (integerp (nth 1 (car hor)))
2434                    (nth 1 (car hor))
2435                  (- (frame-width) (floor (* (frame-width) (nth 1 (car hor))))))
2436                t)
2437               (setq hor (cdr hor))))
2438
2439         ;; Go through the rules and eval the elements that are to be
2440         ;; evaled.  
2441         (while hor
2442           (if (setq val (if (vectorp (car hor)) (car hor) (eval (car hor))))
2443               (progn
2444                 ;; Expand short buffer name.
2445                 (setq w (aref val 0))
2446                 (and (setq w (cdr (assq w gnus-window-to-buffer)))
2447                      (progn
2448                        (setq val (apply 'vector (mapcar 'identity val)))
2449                        (aset val 0 w)))
2450                 (setq ohor (cons val ohor))))
2451           (setq hor (cdr hor)))
2452         (setq rule (cons (nreverse ohor) rule))
2453         (setq r (cdr r)))
2454       (setq rule (nreverse rule))
2455
2456       ;; We tally the window sizes.
2457       (setq total (window-height))
2458       (while rule
2459         (setq hor (car rule))
2460         (if (and (listp (car hor)) (eq (car (car hor)) 'horizontal))
2461             (setq hor (cdr hor)))
2462         (setq sub 0)
2463         (while hor
2464           (setq rel (aref (car hor) 1)
2465                 heights (cons
2466                          (cond ((and (floatp rel) (= 1.0 rel))
2467                                 'x)
2468                                ((integerp rel)
2469                                 rel)
2470                                (t
2471                                 (max (floor (* total rel)) 4)))
2472                          heights)
2473                 sub (+ sub (if (numberp (car heights)) (car heights) 0))
2474                 hor (cdr hor)))
2475         (setq heights (nreverse heights)
2476               hor (car rule))
2477
2478         ;; We then go through these heighs and create windows for them.
2479         (while heights
2480           (setq height (car heights)
2481                 heights (cdr heights))
2482           (and (eq height 'x)
2483                (setq height (- total sub)))
2484           (and heights
2485                (split-window nil height))
2486           (setq to-buf (aref (car hor) 0))
2487           (switch-to-buffer 
2488            (cond ((not to-buf)
2489                   in-buf)
2490                  ((symbolp to-buf)
2491                   (symbol-value (aref (car hor) 0)))
2492                  (t
2493                   (aref (car hor) 0))))
2494           (and (> (length (car hor)) 2)
2495                (eq (aref (car hor) 2) 'point)
2496                (setq jump-buffer (current-buffer)))
2497           (other-window 1)
2498           (setq hor (cdr hor)))
2499       
2500         (setq rule (cdr rule)))
2501
2502       ;; Finally, we pop to the buffer that's supposed to have point. 
2503       (or jump-buffer (error "Missing `point' in spec for %s" setting))
2504
2505       (select-window (get-buffer-window jump-buffer))
2506       (set-buffer jump-buffer))))
2507
2508 (defun gnus-all-windows-visible-p (rule)
2509   (let (invisible hor jump-buffer val buffer)
2510     ;; Go through the rules and eval the elements that are to be
2511     ;; evaled.  
2512     (while (and rule (not invisible))
2513       (setq hor (car rule)
2514             rule (cdr rule))
2515       (while (and hor (not invisible))
2516         (if (setq val (if (vectorp (car hor)) 
2517                           (car hor)
2518                         (if (not (eq (car (car hor)) 'horizontal))
2519                             (eval (car hor)))))
2520             (progn
2521               ;; Expand short buffer name.
2522               (setq buffer (or (cdr (assq (aref val 0) gnus-window-to-buffer))
2523                                (aref val 0)))
2524               (setq buffer (if (symbolp buffer) (symbol-value buffer)
2525                              buffer))
2526               (and (> (length val) 2) (eq 'point (aref val 2))
2527                    (setq jump-buffer buffer))
2528               (setq invisible (not (and buffer (get-buffer-window buffer))))))
2529         (setq hor (cdr hor))))
2530     (and (not invisible) jump-buffer)))
2531
2532       
2533 (defun gnus-remove-some-windows ()
2534   (let ((buffers gnus-window-to-buffer)
2535         (first t)
2536         buf)
2537     (save-excursion
2538       ;; Remove windows on all known Gnus buffers.
2539       (while buffers
2540         (setq buf (cdr (car buffers)))
2541         (if (symbolp buf)
2542             (setq buf (and (boundp buf) (symbol-value buf))))
2543         (and buf 
2544              (get-buffer-window buf)
2545              (progn
2546                (if first
2547                    (progn
2548                      (pop-to-buffer buf)
2549                      (switch-to-buffer nntp-server-buffer)
2550                      (setq first nil))
2551                  (delete-window (get-buffer-window buf)))))
2552         (setq buffers (cdr buffers)))
2553       ;; Remove windows on *all* summary buffers.
2554       (walk-windows
2555        (lambda (win)
2556          (let ((buf (window-buffer win)))
2557            (if (string-match  "^\\*Summary" (buffer-name buf))
2558                (if first
2559                    (progn
2560                      (select-window win)
2561                      (switch-to-buffer nntp-server-buffer)
2562                      (setq first nil))
2563                  (delete-window win)))))))))
2564                           
2565 (defun gnus-version ()
2566   "Version numbers of this version of Gnus."
2567   (interactive)
2568   (let ((methods gnus-valid-select-methods)
2569         (mess gnus-version)
2570         meth)
2571     ;; Go through all the legal select methods and add their version
2572     ;; numbers to the total version string. Only the backends that are
2573     ;; currently in use will have their message numbers taken into
2574     ;; consideration. 
2575     (while methods
2576       (setq meth (intern (concat (car (car methods)) "-version")))
2577       (and (boundp meth)
2578            (stringp (symbol-value meth))
2579            (setq mess (concat mess "; " (symbol-value meth))))
2580       (setq methods (cdr methods)))
2581     (gnus-message 2 mess)))
2582
2583 (defun gnus-info-find-node ()
2584   "Find Info documentation of Gnus."
2585   (interactive)
2586   ;; Enlarge info window if needed.
2587   (let ((mode major-mode))
2588     (gnus-configure-windows 'info)
2589     (Info-goto-node (car (cdr (assq mode gnus-info-nodes))))))
2590
2591 (defun gnus-overload-functions (&optional overloads)
2592   "Overload functions specified by optional argument OVERLOADS.
2593 If nothing is specified, use the variable gnus-overload-functions."
2594   (let ((defs nil)
2595         (overloads (or overloads gnus-overload-functions)))
2596     (while overloads
2597       (setq defs (car overloads))
2598       (setq overloads (cdr overloads))
2599       ;; Load file before overloading function if necessary.  Make
2600       ;; sure we cannot use `require' always.
2601       (and (not (fboundp (car defs)))
2602            (car (cdr (cdr defs)))
2603            (load (car (cdr (cdr defs))) nil 'nomessage))
2604       (fset (car defs) (car (cdr defs))))))
2605
2606 (defun gnus-replace-chars-in-string (string from to)
2607   "Replace characters in STRING from FROM to TO."
2608   (let ((string (substring string 0))   ;Copy string.
2609         (len (length string))
2610         (idx 0))
2611     ;; Replace all occurrences of FROM with TO.
2612     (while (< idx len)
2613       (if (= (aref string idx) from)
2614           (aset string idx to))
2615       (setq idx (1+ idx)))
2616     string))
2617
2618 (defun gnus-days-between (date1 date2)
2619   ;; Return the number of days between date1 and date2.
2620   (- (gnus-day-number date1) (gnus-day-number date2)))
2621
2622 (defun gnus-day-number (date)
2623   (let ((dat (mapcar (lambda (s) (and s (string-to-int s)) )
2624                      (timezone-parse-date date))))
2625     (timezone-absolute-from-gregorian 
2626      (nth 1 dat) (nth 2 dat) (car dat))))
2627
2628 ;; Returns a floating point number that says how many seconds have
2629 ;; lapsed between Jan 1 12:00:00 1970 and DATE.
2630 (defun gnus-seconds-since-epoch (date)
2631   (let* ((tdate (mapcar (lambda (ti) (and ti (string-to-int ti)))
2632                         (timezone-parse-date date)))
2633          (ttime (mapcar (lambda (ti) (and ti (string-to-int ti)))
2634                         (timezone-parse-time
2635                          (aref (timezone-parse-date date) 3))))
2636          (edate (mapcar (lambda (ti) (and ti (string-to-int ti)))
2637                         (timezone-parse-date "Jan 1 12:00:00 1970")))
2638          (tday (- (timezone-absolute-from-gregorian 
2639                    (nth 1 tdate) (nth 2 tdate) (nth 0 tdate))
2640                   (timezone-absolute-from-gregorian 
2641                    (nth 1 edate) (nth 2 edate) (nth 0 edate)))))
2642     (+ (nth 2 ttime)
2643        (* (nth 1 ttime) 60)
2644        (* 1.0 (nth 0 ttime) 60 60)
2645        (* 1.0 tday 60 60 24))))
2646
2647 (defun gnus-file-newer-than (file date)
2648   (let ((fdate (nth 5 (file-attributes file))))
2649     (or (> (car fdate) (car date))
2650         (and (= (car fdate) (car date))
2651              (> (nth 1 fdate) (nth 1 date))))))
2652
2653 ;; Two silly functions to ensure that all `y-or-n-p' questions clear
2654 ;; the echo area.
2655 (defun gnus-y-or-n-p (prompt)
2656   (prog1
2657       (y-or-n-p prompt)
2658     (message "")))
2659
2660 (defun gnus-yes-or-no-p (prompt)
2661   (prog1
2662       (yes-or-no-p prompt)
2663     (message "")))
2664
2665 ;; Check whether to use long file names.
2666 (defun gnus-use-long-file-name (symbol)
2667   ;; The variable has to be set...
2668   (and gnus-use-long-file-name
2669        ;; If it isn't a list, then we return t.
2670        (or (not (listp gnus-use-long-file-name))
2671            ;; If it is a list, and the list contains `symbol', we
2672            ;; return nil.  
2673            (not (memq symbol gnus-use-long-file-name)))))
2674
2675 ;; I suspect there's a better way, but I haven't taken the time to do
2676 ;; it yet. -erik selberg@cs.washington.edu
2677 (defun gnus-dd-mmm (messy-date)
2678   "Return a string like DD-MMM from a big messy string"
2679   (let ((datevec (timezone-parse-date messy-date)))
2680     (format "%2s-%s"
2681             (or (aref datevec 2) "??")
2682             (capitalize
2683              (or (car 
2684                   (nth (1- (string-to-number (aref datevec 1)))
2685                        timezone-months-assoc))
2686                  "???")))))
2687
2688 ;; Make a hash table (default and minimum size is 255).
2689 ;; Optional argument HASHSIZE specifies the table size.
2690 (defun gnus-make-hashtable (&optional hashsize)
2691   (make-vector (if hashsize (max (gnus-create-hash-size hashsize) 255) 255) 0))
2692
2693 ;; Make a number that is suitable for hashing; bigger than MIN and one
2694 ;; less than 2^x.
2695 (defun gnus-create-hash-size (min)
2696   (let ((i 1))
2697     (while (< i min)
2698       (setq i (* 2 i)))
2699     (1- i)))
2700
2701 ;; Show message if message has a lower level than `gnus-verbose'. 
2702 ;; Guide-line for numbers:
2703 ;; 1 - error messages, 3 - non-serious error messages, 5 - messages
2704 ;; for things that take a long time, 7 - not very important messages
2705 ;; on stuff, 9 - messages inside loops.
2706 (defun gnus-message (level &rest args)
2707   (if (<= level gnus-verbose)
2708       (apply 'message args)
2709     ;; We have to do this format thingie here even if the result isn't
2710     ;; shown - the return value has to be the same as the return value
2711     ;; from `message'.
2712     (apply 'format args)))
2713
2714 ;; Generate a unique new group name.
2715 (defun gnus-generate-new-group-name (leaf)
2716   (let ((name leaf)
2717         (num 0))
2718     (while (gnus-gethash name gnus-newsrc-hashtb)
2719       (setq name (concat leaf "<" (int-to-string (setq num (1+ num))) ">")))
2720     name))
2721
2722 (defun gnus-find-file-noselect (file &optional force)
2723   "Does vaguely the same as find-file-noselect. No hooks are run."
2724   (let (buf insert)
2725     (if (setq buf (get-file-buffer file))
2726         (setq insert force)
2727       (setq buf (create-file-buffer file))
2728       (setq insert t))
2729     (if (not insert)
2730         buf
2731       (save-excursion
2732         (set-buffer buf)
2733         (erase-buffer)
2734         (and (file-readable-p file)
2735              (insert-file-contents file))
2736         (set-visited-file-name file)
2737         (set-buffer-modified-p nil)
2738         (current-buffer)))))
2739
2740 ;;; List and range functions
2741
2742 (defun gnus-last-element (list)
2743   "Return last element of LIST."
2744   (while (cdr list)
2745     (setq list (cdr list)))
2746   (car list))
2747
2748 (defun gnus-copy-sequence (list)
2749   "Do a complete, total copy of a list."
2750   (if (and (consp list) (not (consp (cdr list))))
2751       (cons (car list) (cdr list))
2752     (mapcar (lambda (elem) (if (consp elem) 
2753                                (if (consp (cdr elem))
2754                                    (gnus-copy-sequence elem)
2755                                  (cons (car elem) (cdr elem)))
2756                              elem))
2757             list)))
2758
2759 (defun gnus-set-difference (list1 list2)
2760   "Return a list of elements of LIST1 that do not appear in LIST2."
2761   (let ((list1 (copy-sequence list1)))
2762     (while list2
2763       (setq list1 (delq (car list2) list1))
2764       (setq list2 (cdr list2)))
2765     list1))
2766
2767 (defun gnus-sorted-complement (list1 list2)
2768   "Return a list of elements of LIST1 that do not appear in LIST2.
2769 Both lists have to be sorted over <."
2770   (let (out)
2771     (if (or (null list1) (null list2))
2772         (or list1 list2)
2773       (while (and list1 list2)
2774         (cond ((= (car list1) (car list2))
2775                (setq list1 (cdr list1)
2776                      list2 (cdr list2)))
2777               ((< (car list1) (car list2))
2778                (setq out (cons (car list1) out))
2779                (setq list1 (cdr list1)))
2780               (t
2781                (setq out (cons (car list2) out))
2782                (setq list2 (cdr list2)))))
2783       (nconc (nreverse out) (or list1 list2)))))
2784
2785 (defun gnus-intersection (list1 list2)      
2786   (let ((result nil))
2787     (while list2
2788       (if (memq (car list2) list1)
2789           (setq result (cons (car list2) result)))
2790       (setq list2 (cdr list2)))
2791     result))
2792
2793 (defun gnus-sorted-intersection (list1 list2)
2794   ;; LIST1 and LIST2 have to be sorted over <.
2795   (let (out)
2796     (while (and list1 list2)
2797       (cond ((= (car list1) (car list2))
2798              (setq out (cons (car list1) out)
2799                    list1 (cdr list1)
2800                    list2 (cdr list2)))
2801             ((< (car list1) (car list2))
2802              (setq list1 (cdr list1)))
2803             (t
2804              (setq list2 (cdr list2)))))
2805     (nreverse out)))
2806
2807 (defun gnus-set-sorted-intersection (list1 list2)
2808   ;; LIST1 and LIST2 have to be sorted over <.
2809   ;; This function modifies LIST1.
2810   (let* ((top (cons nil list1))
2811          (prev top))
2812   (while (and list1 list2)
2813     (cond ((= (car list1) (car list2))
2814            (setq prev list1
2815                  list1 (cdr list1)
2816                  list2 (cdr list2)))
2817           ((< (car list1) (car list2))
2818            (setcdr prev (cdr list1))
2819            (setq list1 (cdr list1)))
2820           (t
2821            (setq list2 (cdr list2)))))
2822   (setcdr prev nil)
2823   (cdr top)))
2824
2825 (defun gnus-compress-sequence (numbers &optional always-list)
2826   "Convert list of numbers to a list of ranges or a single range.
2827 If ALWAYS-LIST is non-nil, this function will always release a list of
2828 ranges."
2829   (let* ((first (car numbers))
2830          (last (car numbers))
2831          result)
2832     (if (null numbers)
2833         nil
2834       (if (not (listp (cdr numbers)))
2835           numbers
2836         (while numbers
2837           (cond ((= last (car numbers)) nil) ;Omit duplicated number
2838                 ((= (1+ last) (car numbers)) ;Still in sequence
2839                  (setq last (car numbers)))
2840                 (t                      ;End of one sequence
2841                  (setq result 
2842                        (cons (if (= first last) first
2843                                (cons first last)) result))
2844                  (setq first (car numbers))
2845                  (setq last  (car numbers))))
2846           (setq numbers (cdr numbers)))
2847         (if (and (not always-list) (null result))
2848             (if (= first last) (list first) (cons first last))
2849           (nreverse (cons (if (= first last) first (cons first last))
2850                           result)))))))
2851
2852 (defalias 'gnus-uncompress-sequence 'gnus-uncompress-range)
2853 (defun gnus-uncompress-range (ranges)
2854   "Expand a list of ranges into a list of numbers.
2855 RANGES is either a single range on the form `(num . num)' or a list of
2856 these ranges."
2857   (let (first last result)
2858     (cond 
2859      ((null ranges)
2860       nil)
2861      ((not (listp (cdr ranges)))
2862       (setq first (car ranges))
2863       (setq last (cdr ranges))
2864       (while (<= first last)
2865         (setq result (cons first result))
2866         (setq first (1+ first)))
2867       (nreverse result))
2868      (t
2869       (while ranges
2870         (if (atom (car ranges))
2871             (if (numberp (car ranges))
2872                 (setq result (cons (car ranges) result)))
2873           (setq first (car (car ranges)))
2874           (setq last  (cdr (car ranges)))
2875           (while (<= first last)
2876             (setq result (cons first result))
2877             (setq first (1+ first))))
2878         (setq ranges (cdr ranges)))
2879       (nreverse result)))))
2880
2881 (defun gnus-add-to-range (ranges list)
2882   "Return a list of ranges that has all articles from both RANGES and LIST.
2883 Note: LIST has to be sorted over `<'."
2884   (if (not ranges)
2885       (gnus-compress-sequence list t)
2886     (setq list (copy-sequence list))
2887     (or (listp (cdr ranges))
2888         (setq ranges (list ranges)))
2889     (let ((out ranges)
2890           ilist lowest highest temp)
2891       (while (and ranges list)
2892         (setq ilist list)
2893         (setq lowest (or (and (atom (car ranges)) (car ranges))
2894                          (car (car ranges))))
2895         (while (and list (cdr list) (< (car (cdr list)) lowest))
2896           (setq list (cdr list)))
2897         (if (< (car ilist) lowest)
2898             (progn
2899               (setq temp list)
2900               (setq list (cdr list))
2901               (setcdr temp nil)
2902               (setq out (nconc (gnus-compress-sequence ilist t) out))))
2903         (setq highest (or (and (atom (car ranges)) (car ranges))
2904                           (cdr (car ranges))))
2905         (while (and list (<= (car list) highest))
2906           (setq list (cdr list)))
2907         (setq ranges (cdr ranges)))
2908       (if list
2909           (setq out (nconc (gnus-compress-sequence list t) out)))
2910       (setq out (sort out (lambda (r1 r2) 
2911                             (< (or (and (atom r1) r1) (car r1))
2912                                (or (and (atom r2) r2) (car r2))))))
2913       (setq ranges out)
2914       (while ranges
2915         (if (atom (car ranges))
2916             (if (cdr ranges)
2917                 (if (atom (car (cdr ranges)))
2918                     (if (= (1+ (car ranges)) (car (cdr ranges)))
2919                         (progn
2920                           (setcar ranges (cons (car ranges) 
2921                                                (car (cdr ranges))))
2922                           (setcdr ranges (cdr (cdr ranges)))))
2923                   (if (= (1+ (car ranges)) (car (car (cdr ranges))))
2924                       (progn
2925                         (setcar (car (cdr ranges)) (car ranges))
2926                         (setcar ranges (car (cdr ranges)))
2927                         (setcdr ranges (cdr (cdr ranges)))))))
2928           (if (cdr ranges)
2929               (if (atom (car (cdr ranges)))
2930                   (if (= (1+ (cdr (car ranges))) (car (cdr ranges)))
2931                       (progn
2932                         (setcdr (car ranges) (car (cdr ranges)))
2933                         (setcdr ranges (cdr (cdr ranges)))))
2934                 (if (= (1+ (cdr (car ranges))) (car (car (cdr ranges))))
2935                     (progn
2936                       (setcdr (car ranges) (cdr (car (cdr ranges))))
2937                       (setcdr ranges (cdr (cdr ranges))))))))
2938         (setq ranges (cdr ranges)))
2939       out)))
2940
2941 (defun gnus-remove-from-range (ranges list)
2942   "Return a list of ranges that has all articles from LIST removed from RANGES.
2943 Note: LIST has to be sorted over `<'."
2944   ;; !!! This function shouldn't look like this, but I've got a headache.
2945   (gnus-compress-sequence 
2946    (gnus-sorted-complement
2947     (gnus-uncompress-range ranges) list)))
2948
2949 (defun gnus-member-of-range (number ranges)
2950   (if (not (listp (cdr ranges)))
2951       (and (>= number (car ranges)) 
2952            (<= number (cdr ranges)))
2953     (let ((not-stop t))
2954       (while (and ranges 
2955                   (if (numberp (car ranges))
2956                       (>= number (car ranges))
2957                     (>= number (car (car ranges))))
2958                   not-stop)
2959         (if (if (numberp (car ranges))
2960                 (= number (car ranges))
2961               (and (>= number (car (car ranges)))
2962                    (<= number (cdr (car ranges)))))
2963             (setq not-stop nil))
2964         (setq ranges (cdr ranges)))
2965       (not not-stop))))
2966
2967 \f
2968 ;;;
2969 ;;; Gnus group mode
2970 ;;;
2971
2972 (defvar gnus-group-mode-map nil)
2973 (defvar gnus-group-group-map nil)
2974 (defvar gnus-group-mark-map nil)
2975 (defvar gnus-group-list-map nil)
2976 (defvar gnus-group-sub-map nil)
2977 (put 'gnus-group-mode 'mode-class 'special)
2978
2979 (if gnus-group-mode-map
2980     nil
2981   (setq gnus-group-mode-map (make-keymap))
2982   (suppress-keymap gnus-group-mode-map)
2983   (define-key gnus-group-mode-map " " 'gnus-group-read-group)
2984   (define-key gnus-group-mode-map "=" 'gnus-group-select-group)
2985   (define-key gnus-group-mode-map "\r" 'gnus-group-select-group)
2986   (define-key gnus-group-mode-map "j" 'gnus-group-jump-to-group)
2987   (define-key gnus-group-mode-map "n" 'gnus-group-next-unread-group)
2988   (define-key gnus-group-mode-map "p" 'gnus-group-prev-unread-group)
2989   (define-key gnus-group-mode-map "\177" 'gnus-group-prev-unread-group)
2990   (define-key gnus-group-mode-map "N" 'gnus-group-next-group)
2991   (define-key gnus-group-mode-map "P" 'gnus-group-prev-group)
2992   (define-key gnus-group-mode-map "\M-n" 'gnus-group-next-unread-group-same-level)
2993   (define-key gnus-group-mode-map "\M-p" 'gnus-group-prev-unread-group-same-level)
2994   (define-key gnus-group-mode-map "," 'gnus-group-best-unread-group)
2995   (define-key gnus-group-mode-map "." 'gnus-group-first-unread-group)
2996   (define-key gnus-group-mode-map "u" 'gnus-group-unsubscribe-current-group)
2997   (define-key gnus-group-mode-map "U" 'gnus-group-unsubscribe-group)
2998   (define-key gnus-group-mode-map "c" 'gnus-group-catchup-current)
2999   (define-key gnus-group-mode-map "C" 'gnus-group-catchup-current-all)
3000   (define-key gnus-group-mode-map "l" 'gnus-group-list-groups)
3001   (define-key gnus-group-mode-map "L" 'gnus-group-list-all-groups)
3002   (define-key gnus-group-mode-map "m" 'gnus-group-mail)
3003   (define-key gnus-group-mode-map "g" 'gnus-group-get-new-news)
3004   (define-key gnus-group-mode-map "\M-g" 'gnus-group-get-new-news-this-group)
3005   (define-key gnus-group-mode-map "R" 'gnus-group-restart)
3006   (define-key gnus-group-mode-map "r" 'gnus-group-read-init-file)
3007   (define-key gnus-group-mode-map "B" 'gnus-group-browse-foreign-server)
3008   (define-key gnus-group-mode-map "b" 'gnus-group-check-bogus-groups)
3009   (define-key gnus-group-mode-map "F" 'gnus-find-new-newsgroups)
3010   (define-key gnus-group-mode-map "\C-c\C-d" 'gnus-group-describe-group)
3011   (define-key gnus-group-mode-map "\M-d" 'gnus-group-describe-all-groups)
3012   (define-key gnus-group-mode-map "\C-c\C-a" 'gnus-group-apropos)
3013   (define-key gnus-group-mode-map "\C-c\M-C-a" 'gnus-group-description-apropos)
3014   (define-key gnus-group-mode-map "a" 'gnus-group-post-news)
3015   (define-key gnus-group-mode-map "\ek" 'gnus-group-edit-local-kill)
3016   (define-key gnus-group-mode-map "\eK" 'gnus-group-edit-global-kill)
3017   (define-key gnus-group-mode-map "\C-k" 'gnus-group-kill-group)
3018   (define-key gnus-group-mode-map "\C-y" 'gnus-group-yank-group)
3019   (define-key gnus-group-mode-map "\C-w" 'gnus-group-kill-region)
3020   (define-key gnus-group-mode-map "\C-x\C-t" 'gnus-group-transpose-groups)
3021   (define-key gnus-group-mode-map "\C-c\C-l" 'gnus-group-list-killed)
3022   (define-key gnus-group-mode-map "\C-c\C-x" 'gnus-group-expire-articles)
3023   (define-key gnus-group-mode-map "\C-c\M-\C-x" 'gnus-group-expire-all-groups)
3024   (define-key gnus-group-mode-map "V" 'gnus-version)
3025   (define-key gnus-group-mode-map "s" 'gnus-group-save-newsrc)
3026   (define-key gnus-group-mode-map "z" 'gnus-group-suspend)
3027   (define-key gnus-group-mode-map "Z" 'gnus-group-clear-dribble)
3028   (define-key gnus-group-mode-map "q" 'gnus-group-exit)
3029   (define-key gnus-group-mode-map "Q" 'gnus-group-quit)
3030   (define-key gnus-group-mode-map "\M-f" 'gnus-group-fetch-faq)
3031   (define-key gnus-group-mode-map "?" 'gnus-group-describe-briefly)
3032   (define-key gnus-group-mode-map "\C-c\C-i" 'gnus-info-find-node)
3033   (define-key gnus-group-mode-map "\M-e" 'gnus-group-edit-group-method)
3034   (define-key gnus-group-mode-map "^" 'gnus-group-enter-server-mode)
3035   (define-key gnus-group-mode-map gnus-mouse-2 'gnus-mouse-pick-group)
3036   (define-key gnus-group-mode-map "<" 'beginning-of-buffer)
3037   (define-key gnus-group-mode-map ">" 'end-of-buffer)
3038   (define-key gnus-group-mode-map "\C-c\C-b" 'gnus-bug)
3039   (define-key gnus-group-mode-map "\C-c\C-s" 'gnus-group-sort-groups)
3040
3041   (define-key gnus-group-mode-map "#" 'gnus-group-mark-group)
3042   (define-key gnus-group-mode-map "\M-#" 'gnus-group-unmark-group)
3043   (define-prefix-command 'gnus-group-mark-map)
3044   (define-key gnus-group-mode-map "M" 'gnus-group-mark-map)
3045   (define-key gnus-group-mark-map "m" 'gnus-group-mark-group)
3046   (define-key gnus-group-mark-map "u" 'gnus-group-unmark-group)
3047   (define-key gnus-group-mark-map "w" 'gnus-group-mark-region)
3048
3049   (define-prefix-command 'gnus-group-group-map)
3050   (define-key gnus-group-mode-map "G" 'gnus-group-group-map)
3051   (define-key gnus-group-group-map "d" 'gnus-group-make-directory-group)
3052   (define-key gnus-group-group-map "h" 'gnus-group-make-help-group)
3053   (define-key gnus-group-group-map "a" 'gnus-group-make-archive-group)
3054   (define-key gnus-group-group-map "k" 'gnus-group-make-kiboze-group)
3055   (define-key gnus-group-group-map "m" 'gnus-group-make-group)
3056   (define-key gnus-group-group-map "E" 'gnus-group-edit-group)
3057   (define-key gnus-group-group-map "e" 'gnus-group-edit-group-method)
3058   (define-key gnus-group-group-map "p" 'gnus-group-edit-group-parameters)
3059   (define-key gnus-group-group-map "v" 'gnus-group-add-to-virtual)
3060   (define-key gnus-group-group-map "V" 'gnus-group-make-empty-virtual)
3061   (define-key gnus-group-group-map "D" 'gnus-group-enter-directory)
3062   (define-key gnus-group-group-map "f" 'gnus-group-make-doc-group)
3063   (define-key gnus-group-group-map "sb" 'gnus-group-brew-soup)
3064   (define-key gnus-group-group-map "sw" 'gnus-soup-save-areas)
3065   (define-key gnus-group-group-map "ss" 'gnus-soup-send-replies)
3066   (define-key gnus-group-group-map "sp" 'gnus-soup-pack-packet)
3067   (define-key gnus-group-group-map "sr" 'nnsoup-pack-replies)
3068
3069   (define-prefix-command 'gnus-group-list-map)
3070   (define-key gnus-group-mode-map "A" 'gnus-group-list-map)
3071   (define-key gnus-group-list-map "k" 'gnus-group-list-killed)
3072   (define-key gnus-group-list-map "z" 'gnus-group-list-zombies)
3073   (define-key gnus-group-list-map "s" 'gnus-group-list-groups)
3074   (define-key gnus-group-list-map "u" 'gnus-group-list-all-groups)
3075   (define-key gnus-group-list-map "a" 'gnus-group-apropos)
3076   (define-key gnus-group-list-map "d" 'gnus-group-description-apropos)
3077   (define-key gnus-group-list-map "m" 'gnus-group-list-matching)
3078   (define-key gnus-group-list-map "M" 'gnus-group-list-all-matching)
3079
3080   (define-prefix-command 'gnus-group-sub-map)
3081   (define-key gnus-group-mode-map "S" 'gnus-group-sub-map)
3082   (define-key gnus-group-sub-map "l" 'gnus-group-set-current-level)
3083   (define-key gnus-group-sub-map "t" 'gnus-group-unsubscribe-current-group)
3084   (define-key gnus-group-sub-map "s" 'gnus-group-unsubscribe-group)
3085   (define-key gnus-group-sub-map "k" 'gnus-group-kill-group)
3086   (define-key gnus-group-sub-map "y" 'gnus-group-yank-group)
3087   (define-key gnus-group-sub-map "w" 'gnus-group-kill-region)
3088   (define-key gnus-group-sub-map "z" 'gnus-group-kill-all-zombies))
3089
3090 (defun gnus-group-mode ()
3091   "Major mode for reading news.
3092
3093 All normal editing commands are switched off.
3094 \\<gnus-group-mode-map>
3095 The group buffer lists (some of) the groups available.  For instance,
3096 `\\[gnus-group-list-groups]' will list all subscribed groups with unread articles, while `\\[gnus-group-list-zombies]'
3097 lists all zombie groups. 
3098
3099 Groups that are displayed can be entered with `\\[gnus-group-read-group]'.  To subscribe 
3100 to a group not displayed, type `\\[gnus-group-unsubscribe-group]'. 
3101
3102 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]'). 
3103
3104 The following commands are available:
3105
3106 \\{gnus-group-mode-map}"
3107   (interactive)
3108   (if gnus-visual (gnus-group-make-menu-bar))
3109   (kill-all-local-variables)
3110   (setq mode-line-modified "-- ")
3111   (make-local-variable 'mode-line-format)
3112   (setq mode-line-format (copy-sequence mode-line-format))
3113   (and (equal (nth 3 mode-line-format) "   ")
3114        (setcar (nthcdr 3 mode-line-format) ""))
3115   (setq major-mode 'gnus-group-mode)
3116   (setq mode-name "Group")
3117   (gnus-group-set-mode-line)
3118   (setq mode-line-process nil)
3119   (use-local-map gnus-group-mode-map)
3120   (buffer-disable-undo (current-buffer))
3121   (setq truncate-lines t)
3122   (setq buffer-read-only t)
3123   (run-hooks 'gnus-group-mode-hook))
3124
3125 (defun gnus-mouse-pick-group (e)
3126   (interactive "e")
3127   (mouse-set-point e)
3128   (gnus-group-read-group nil))
3129
3130 ;;;###autoload
3131 (defun gnus-no-server (&optional arg)
3132   "Read network news.
3133 If ARG is a positive number, Gnus will use that as the
3134 startup level. If ARG is nil, Gnus will be started at level 2. 
3135 If ARG is non-nil and not a positive number, Gnus will
3136 prompt the user for the name of an NNTP server to use.
3137 As opposed to `gnus', this command will not connect to the local server."
3138   (interactive "P")
3139   (setq gnus-group-use-permanent-levels t)
3140   (gnus (or arg (1- gnus-level-default-subscribed)) t))
3141
3142 (defalias '\(ding\) 'gnus)
3143
3144 ;;;###autoload
3145 (defun gnus (&optional arg dont-connect)
3146   "Read network news.
3147 If ARG is non-nil and a positive number, Gnus will use that as the
3148 startup level. If ARG is non-nil and not a positive number, Gnus will
3149 prompt the user for the name of an NNTP server to use."
3150   (interactive "P")
3151   (if (get-buffer gnus-group-buffer)
3152       (progn
3153         (switch-to-buffer gnus-group-buffer)
3154         (gnus-group-get-new-news))
3155     (gnus-clear-system)
3156     (nnheader-init-server-buffer)
3157     (gnus-read-init-file)
3158
3159     (let ((level (and arg (numberp arg) (> arg 0) arg))
3160           did-connect)
3161       (unwind-protect
3162           (progn
3163             (gnus-group-setup-buffer)
3164             (or dont-connect 
3165                 (setq did-connect
3166                       (gnus-start-news-server (and arg (not level))))))
3167         (if (and (not dont-connect) 
3168                  (not did-connect))
3169             (gnus-group-quit)
3170           (run-hooks 'gnus-startup-hook)
3171           ;; NNTP server is successfully open. 
3172
3173           ;; Find the current startup file name.
3174           (setq gnus-current-startup-file 
3175                 (gnus-make-newsrc-file gnus-startup-file))
3176
3177           ;; Read the dribble file.
3178           (and gnus-use-dribble-file (gnus-dribble-read-file))
3179
3180           (gnus-update-format-specifications)
3181           (gnus-summary-make-display-table)
3182           (let ((buffer-read-only nil))
3183             (erase-buffer)
3184             (if (not gnus-inhibit-startup-message)
3185                 (progn
3186                   (gnus-group-startup-message)
3187                   (sit-for 0))))
3188           (gnus-setup-news nil level)
3189           (gnus-group-list-groups level)
3190           (gnus-configure-windows 'group))))))
3191
3192 (defun gnus-unload ()
3193   "Unload all Gnus features."
3194   (interactive)
3195   (let ((history load-history)
3196         feature)
3197     (while history
3198       (and (string-match "^gnus" (car (car history)))
3199            (setq feature (cdr (assq 'provide (car history))))
3200            (unload-feature feature 'force))
3201       (setq history (cdr history)))))
3202
3203 (defun gnus-group-startup-message (&optional x y)
3204   "Insert startup message in current buffer."
3205   ;; Insert the message.
3206   (erase-buffer)
3207   (insert
3208    (format "
3209      %s
3210            A newsreader 
3211       for GNU Emacs
3212
3213         Based on GNUS 
3214              written by 
3215      Masanobu UMEDA
3216
3217        A Praxis Release
3218       larsi@ifi.uio.no
3219
3220            gnus-version))
3221   ;; And then hack it.
3222   ;; 18 is the longest line.
3223   (indent-rigidly (point-min) (point-max) 
3224                   (/ (max (- (window-width) (or x 28)) 0) 2))
3225   (goto-char (point-min))
3226   ;; +4 is fuzzy factor.
3227   (insert-char ?\n (/ (max (- (window-height) (or y 12)) 0) 2))
3228
3229   ;; Fontify some.
3230   (goto-char (point-min))
3231   (search-forward "Praxis")
3232   (put-text-property (match-beginning 0) (match-end 0) 'face 'bold)
3233   (goto-char (point-min)))
3234
3235 (defun gnus-group-setup-buffer ()
3236   (or (get-buffer gnus-group-buffer)
3237       (progn
3238         (switch-to-buffer gnus-group-buffer)
3239         (gnus-add-current-to-buffer-list)
3240         (gnus-group-mode)
3241         (and gnus-carpal (gnus-carpal-setup-buffer 'group)))))
3242
3243 (defun gnus-group-list-groups (level &optional unread)
3244   "List newsgroups with level LEVEL or lower that have unread articles.
3245 Default is all subscribed groups.
3246 If argument UNREAD is non-nil, groups with no unread articles are also listed."
3247   (interactive (list (and current-prefix-arg
3248                           (prefix-numeric-value current-prefix-arg))))
3249   (if gnus-group-use-permanent-levels
3250       (progn
3251         (setq gnus-group-default-list-level 
3252               (or level gnus-group-default-list-level))
3253         (setq level (or gnus-group-default-list-level gnus-level-subscribed)))
3254     (setq level (or level gnus-group-default-list-level 
3255                     gnus-level-subscribed)))
3256   (gnus-group-setup-buffer)     ;May call from out of group buffer
3257   (let ((case-fold-search nil)
3258         (group (gnus-group-group-name)))
3259     (funcall gnus-group-prepare-function level unread nil)
3260     (if (zerop (buffer-size))
3261         (gnus-message 5 gnus-no-groups-message)
3262       (goto-char (point-min))
3263       (if (not group)
3264           ;; Go to the first group with unread articles.
3265           (gnus-group-search-forward nil nil nil t)
3266         ;; Find the right group to put point on. If the current group
3267         ;; has disapeared in the new listing, try to find the next
3268         ;; one. If no next one can be found, just leave point at the
3269         ;; first newsgroup in the buffer.
3270         (if (not (gnus-goto-char
3271                   (text-property-any (point-min) (point-max) 
3272                                      'gnus-group (intern group))))
3273             (let ((newsrc (nthcdr 3 (gnus-gethash group gnus-newsrc-hashtb))))
3274               (while (and newsrc
3275                           (not (gnus-goto-char 
3276                                 (text-property-any 
3277                                  (point-min) (point-max) 'gnus-group 
3278                                  (intern (car (car newsrc)))))))
3279                 (setq newsrc (cdr newsrc)))
3280               (or newsrc (progn (goto-char (point-max))
3281                                 (forward-line -1))))))
3282       ;; Adjust cursor point.
3283       (gnus-group-position-cursor))))
3284
3285 (defun gnus-group-prepare-flat (level &optional all lowest regexp) 
3286   "List all newsgroups with unread articles of level LEVEL or lower.
3287 If ALL is non-nil, list groups that have no unread articles.
3288 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher.
3289 If REGEXP, only list groups matching REGEXP."
3290   (set-buffer gnus-group-buffer)
3291   (let ((buffer-read-only nil)
3292         (newsrc (cdr gnus-newsrc-alist))
3293         (lowest (or lowest 1))
3294         info clevel unread group)
3295     (erase-buffer)
3296     (if (< lowest gnus-level-zombie)
3297         ;; List living groups.
3298         (while newsrc
3299           (setq info (car newsrc)
3300                 group (car info)
3301                 newsrc (cdr newsrc)
3302                 unread (car (gnus-gethash group gnus-newsrc-hashtb)))
3303           (and unread ; This group might be bogus
3304                (or (not regexp)
3305                    (string-match regexp group))
3306                (<= (setq clevel (car (cdr info))) level) 
3307                (>= clevel lowest)
3308                (or all            ; We list all groups?
3309                    (eq unread t)  ; We list unactivated groups
3310                    (> unread 0)   ; We list groups with unread articles
3311                    (cdr (assq 'tick (nth 3 info)))) ; And groups with tickeds
3312                (gnus-group-insert-group-line 
3313                 nil group (car (cdr info)) (nth 3 info) unread (nth 4 info)))))
3314
3315     ;; List dead groups.
3316     (and (>= level gnus-level-zombie) (<= lowest gnus-level-zombie)
3317          (gnus-group-prepare-flat-list-dead 
3318           (setq gnus-zombie-list (sort gnus-zombie-list 'string<)) 
3319           gnus-level-zombie ?Z
3320           regexp))
3321     (and (>= level gnus-level-killed) (<= lowest gnus-level-killed)
3322          (gnus-group-prepare-flat-list-dead 
3323           (setq gnus-killed-list (sort gnus-killed-list 'string<)) 
3324           gnus-level-killed ?K regexp))
3325
3326     (gnus-group-set-mode-line)
3327     (setq gnus-have-all-newsgroups all)
3328     (run-hooks 'gnus-group-prepare-hook)))
3329
3330 (defun gnus-group-prepare-flat-list-dead (groups level mark regexp)
3331   ;; List zombies and killed lists somehwat faster, which was
3332   ;; suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>. It does
3333   ;; this by ignoring the group format specification altogether.
3334   (let (group beg)
3335     (while groups
3336       (setq group (car groups)
3337             groups (cdr groups))
3338       (if (or (not regexp)
3339               (string-match regexp group))
3340           (progn
3341             (setq beg (point))
3342             (insert (format " %c     *: %s\n" mark group))
3343             (add-text-properties 
3344              beg (1+ beg) 
3345              (list 'gnus-group (intern group)
3346                    'gnus-unread t
3347                    'gnus-level level)))))))
3348
3349 (defun gnus-group-real-name (group)
3350   "Find the real name of a foreign newsgroup."
3351   (if (string-match ":[^:]+$" group)
3352       (substring group (1+ (match-beginning 0)))
3353     group))
3354
3355 (defun gnus-group-prefixed-name (group method)
3356   "Return the whole name from GROUP and METHOD."
3357   (and (stringp method) (setq method (gnus-server-to-method method)))
3358   (concat (format "%s" (car method))
3359           (if (and 
3360                (assoc (format "%s" (car method)) (gnus-methods-using 'address))
3361                (not (string= (nth 1 method) "")))
3362               (concat "+" (nth 1 method)))
3363           ":" group))
3364
3365 (defun gnus-group-real-prefix (group)
3366   "Return the prefix of the current group name."
3367   (if (string-match "^[^:]+:" group)
3368       (substring group 0 (match-end 0))
3369     ""))
3370
3371 (defun gnus-group-method-name (group)
3372   "Return the method used for selecting GROUP."
3373   (let ((prefix (gnus-group-real-prefix group)))
3374     (if (equal prefix "")
3375         gnus-select-method
3376       (if (string-match "^[^\\+]+\\+" prefix)
3377           (list (intern (substring prefix 0 (1- (match-end 0))))
3378                 (substring prefix (match-end 0) (1- (length prefix))))
3379         (list (intern (substring prefix 0 (1- (length prefix)))) "")))))
3380
3381 (defun gnus-group-foreign-p (group)
3382   "Return nil if GROUP is native, non-nil if it is foreign."
3383   (string-match ":" group))
3384
3385 (defun gnus-group-set-info (info &optional method-only-group part)
3386   (let* ((entry (gnus-gethash
3387                  (or method-only-group (car info)) gnus-newsrc-hashtb))
3388          (part-info info)
3389          (info (if method-only-group (nth 2 entry) info)))
3390     (if (not method-only-group)
3391         ()
3392       (or entry
3393           (error "Trying to change non-existent group %s" method-only-group))
3394       ;; We have recevied parts of the actual group info - either the
3395       ;; select method or the group parameters.  We first check
3396       ;; whether we have to extend the info, and if so, do that.
3397       (let ((len (length info))
3398             (total (if (eq part 'method) 5 6)))
3399         (and (< len total)
3400              (setcdr (nthcdr (1- len) info)
3401                      (make-list (- total len) nil)))
3402         ;; Then we enter the new info.
3403         (setcar (nthcdr (1- total) info) part-info)))
3404     ;; We uncompress some lists of marked articles.
3405     (let (marked)
3406       (if (not (setq marked (nth 3 info)))
3407           ()
3408         (while marked
3409           (or (eq 'score (car (car marked)))
3410               (eq 'bookmark (car (car marked)))
3411               (eq 'killed (car (car marked)))
3412               (setcdr (car marked) 
3413                       (gnus-uncompress-range (cdr (car marked)))))
3414           (setq marked (cdr marked)))))
3415     (if entry
3416         ()
3417       ;; This is a new group, so we just create it.
3418       (save-excursion
3419         (set-buffer gnus-group-buffer)
3420         (if (nth 4 info)
3421             ;; It's a foreign group...
3422             (gnus-group-make-group 
3423              (gnus-group-real-name (car info))
3424              (prin1-to-string (car (nth 4 info)))
3425              (nth 1 (nth 4 info)))
3426           ;; It's a native group.
3427           (gnus-group-make-group
3428            (car info)
3429            (prin1-to-string (car gnus-select-method))
3430            (nth 1 gnus-select-method)))
3431         (gnus-message 6 "Note: New group created")
3432         (setq entry 
3433               (gnus-gethash (gnus-group-prefixed-name 
3434                              (gnus-group-real-name (car info))
3435                              (or (nth 4 info) gnus-select-method))
3436                             gnus-newsrc-hashtb))))
3437     ;; Whether it was a new group or not, we now have the entry, so we
3438     ;; can do the update.
3439     (if entry
3440         (progn
3441           (setcar (nthcdr 2 entry) info)
3442           (if (and (not (eq (car entry) t)) 
3443                    (gnus-gethash (car info) gnus-active-hashtb))
3444               (let ((marked (nth 3 info)))
3445                 (setcar entry 
3446                         (max 0 (- (length (gnus-list-of-unread-articles 
3447                                            (car info)))
3448                                   (length (cdr (assq 'tick marked)))
3449                                   (length (cdr (assq 'dormant marked)))))))))
3450       (error "No such group: %s" (car info)))))
3451
3452 (defun gnus-group-set-method-info (group select-method)
3453   (gnus-group-set-info select-method group 'method))
3454
3455 (defun gnus-group-set-params-info (group params)
3456   (gnus-group-set-info params group 'params))
3457
3458 (defun gnus-group-update-group-line ()
3459   "This function updates the current line in the newsgroup buffer and
3460 moves the point to the colon."
3461   (let* ((buffer-read-only nil)
3462          (group (gnus-group-group-name))
3463          (entry (and group (gnus-gethash group gnus-newsrc-hashtb))))
3464     (if entry
3465         (gnus-dribble-enter 
3466          (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
3467                  ")")))
3468     (beginning-of-line)
3469     (delete-region (point) (progn (forward-line 1) (point)))
3470     (gnus-group-insert-group-line-info group)
3471     (forward-line -1)
3472     (gnus-group-position-cursor)))
3473
3474 (defun gnus-group-insert-group-line-info (group)
3475   (let ((entry (gnus-gethash group gnus-newsrc-hashtb)) 
3476         active info)
3477     (if entry
3478         (progn
3479           (setq info (nth 2 entry))
3480           (gnus-group-insert-group-line 
3481            nil group (nth 1 info) (nth 3 info) (car entry) (nth 4 info)))
3482       (setq active (gnus-gethash group gnus-active-hashtb))
3483       (gnus-group-insert-group-line 
3484        nil group 
3485        (if (member group gnus-zombie-list) gnus-level-zombie gnus-level-killed)
3486        nil (if active (- (1+ (cdr active)) (car active)) 0) nil))))
3487
3488 (defun gnus-group-insert-group-line (gformat group level marked number method)
3489   (let* ((gformat (or gformat gnus-group-line-format-spec))
3490          (active (gnus-gethash group gnus-active-hashtb))
3491          (number-total (if active (1+ (- (cdr active) (car active))) 0))
3492          (number-of-dormant (length (cdr (assq 'dormant marked))))
3493          (number-of-ticked (length (cdr (assq 'tick marked))))
3494          (number-of-ticked-and-dormant
3495           (+ number-of-ticked number-of-dormant))
3496          (number-of-unread-unticked 
3497           (if (numberp number) (int-to-string (max 0 number))
3498             "*"))
3499          (number-of-read
3500           (if (numberp number)
3501               (max 0 (- number-total number))
3502             "*"))
3503          (subscribed (cond ((<= level gnus-level-subscribed) ? )
3504                            ((<= level gnus-level-unsubscribed) ?U)
3505                            ((= level gnus-level-zombie) ?Z)
3506                            (t ?K)))
3507          (qualified-group (gnus-group-real-name group))
3508          (newsgroup-description 
3509           (if gnus-description-hashtb
3510               (or (gnus-gethash group gnus-description-hashtb) "")
3511             ""))
3512          (moderated (if (member group gnus-moderated-list) ?m ? ))
3513          (moderated-string (if (eq moderated ?m) "(m)" ""))
3514          (method (gnus-server-get-method group method))
3515          (news-server (or (car (cdr method)) ""))
3516          (news-method (or (car method) ""))
3517          (news-method-string 
3518           (if method (format "(%s:%s)" (car method) (car (cdr method))) ""))
3519          (marked (if (and 
3520                       (numberp number) 
3521                       (zerop number)
3522                       (> number-of-ticked 0))
3523                      ?* ? ))
3524          (number (if (eq number t) "*" (+ number number-of-dormant 
3525                                           number-of-ticked)))
3526          (process-marked (if (member qualified-group gnus-group-marked)
3527                              gnus-process-mark ? ))
3528          (buffer-read-only nil)
3529          header ; passed as parameter to user-funcs.
3530          b)
3531     (beginning-of-line)
3532     (setq b (point))
3533     ;; Insert the text.
3534     (insert (eval gformat))
3535
3536     (add-text-properties 
3537      b (1+ b) (list 'gnus-group (intern group)
3538                     'gnus-unread (if (numberp number)
3539                                      (string-to-int number-of-unread-unticked)
3540                                    t)
3541                     'gnus-marked marked
3542                     'gnus-level level))))
3543
3544 (defun gnus-group-update-group (group &optional visible-only)
3545   "Update newsgroup info of GROUP.
3546 If VISIBLE-ONLY is non-nil, the group won't be displayed if it isn't already."
3547   (save-excursion
3548     (set-buffer gnus-group-buffer)
3549     (let ((buffer-read-only nil)
3550           visible)
3551       (let ((entry (gnus-gethash group gnus-newsrc-hashtb)))
3552         (if entry
3553             (gnus-dribble-enter 
3554              (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
3555                      ")"))))
3556       ;; Buffer may be narrowed.
3557       (save-restriction
3558         (widen)
3559         ;; Search a line to modify.  If the buffer is large, the search
3560         ;; takes long time.  In most cases, current point is on the line
3561         ;; we are looking for.  So, first of all, check current line. 
3562         (if (or (progn
3563                   (beginning-of-line)
3564                   (eq (get-text-property (point) 'gnus-group)
3565                       (intern group)))
3566                 (progn
3567                   (gnus-goto-char 
3568                    (text-property-any 
3569                     (point-min) (point-max) 'gnus-group (intern group)))))
3570             ;; GROUP is listed in current buffer. So, delete old line.
3571             (progn
3572               (setq visible t)
3573               (beginning-of-line)
3574               (delete-region (point) (progn (forward-line 1) (point))))
3575           ;; No such line in the buffer, find out where it's supposed to
3576           ;; go, and insert it there (or at the end of the buffer).
3577           ;; Fix by Per Abrahamsen <amanda@iesd.auc.dk>.
3578           (or visible-only
3579               (let ((entry 
3580                      (cdr (cdr (gnus-gethash group gnus-newsrc-hashtb)))))
3581                 (while (and entry
3582                             (car entry)
3583                             (not
3584                              (gnus-goto-char
3585                               (text-property-any
3586                                (point-min) (point-max) 
3587                                'gnus-group (intern (car (car entry)))))))
3588                   (setq entry (cdr entry)))
3589                 (or entry (goto-char (point-max)))))))
3590       (if (or visible (not visible-only))
3591           (gnus-group-insert-group-line-info group))
3592       (gnus-group-set-mode-line))))
3593
3594 (defun gnus-group-set-mode-line ()
3595   (if (memq 'group gnus-updated-mode-lines)
3596       (let* ((gformat (or gnus-group-mode-line-format-spec
3597                           (setq gnus-group-mode-line-format-spec
3598                                 (gnus-parse-format 
3599                                  gnus-group-mode-line-format 
3600                                  gnus-group-mode-line-format-alist))))
3601              (news-server (car (cdr gnus-select-method)))
3602              (news-method (car gnus-select-method))
3603              (max-len 60)
3604              (mode-string (eval gformat)))
3605         (setq mode-string (eval gformat))
3606         (if (> (length mode-string) max-len) 
3607             (setq mode-string (substring mode-string 0 (- max-len 4))))
3608         (setq mode-line-buffer-identification mode-string)
3609         (set-buffer-modified-p t))))
3610
3611 (defun gnus-group-group-name ()
3612   "Get the name of the newsgroup on the current line."
3613   (let ((group (get-text-property (gnus-point-at-bol) 'gnus-group)))
3614     (and group (symbol-name group))))
3615
3616 (defun gnus-group-group-level ()
3617   "Get the level of the newsgroup on the current line."
3618   (get-text-property (gnus-point-at-bol) 'gnus-level))
3619
3620 (defun gnus-group-group-unread ()
3621   "Get the number of unread articles of the newsgroup on the current line."
3622   (get-text-property (gnus-point-at-bol) 'gnus-unread))
3623
3624 (defun gnus-group-search-forward (&optional backward all level first-too)
3625   "Find the next newsgroup with unread articles.
3626 If BACKWARD is non-nil, find the previous newsgroup instead.
3627 If ALL is non-nil, just find any newsgroup.
3628 If LEVEL is non-nil, find group with level LEVEL, or higher if no such
3629 group exists.
3630 If FIRST-TOO, the current line is also eligible as a target."
3631   (let ((way (if backward -1 1))
3632         (low gnus-level-killed)
3633         (beg (point))
3634         pos found lev)
3635     (if (and backward (progn (beginning-of-line)) (bobp))
3636         nil
3637       (or first-too (forward-line way))
3638       (while (and 
3639               (not (eobp))
3640               (not (setq 
3641                     found 
3642                     (and (or all
3643                              (and
3644                               (let ((unread 
3645                                      (get-text-property (point) 'gnus-unread)))
3646                                 (or (eq unread t) (and unread (> unread 0))))
3647                               (setq lev (get-text-property (point)
3648                                                            'gnus-level))
3649                               (<= lev gnus-level-subscribed)))
3650                          (or (not level)
3651                              (and (setq lev (get-text-property (point)
3652                                                                'gnus-level))
3653                                   (or (= lev level)
3654                                       (and (< lev low)
3655                                            (< level lev)
3656                                            (progn
3657                                              (setq low lev)
3658                                              (setq pos (point))
3659                                              nil))))))))
3660               (zerop (forward-line way)))))
3661     (if found 
3662         (progn (gnus-group-position-cursor) t)
3663       (goto-char (or pos beg))
3664       (and pos t))))
3665
3666 ;;; Gnus group mode commands
3667
3668 ;; Group marking.
3669
3670 (defun gnus-group-mark-group (n &optional unmark no-advance)
3671   "Mark the current group."
3672   (interactive "p")
3673   (let ((buffer-read-only nil)
3674         group)
3675     (while 
3676         (and (> n 0) 
3677              (setq group (gnus-group-group-name))
3678              (progn
3679                (beginning-of-line)
3680                (forward-char 2)
3681                (delete-char 1)
3682                (if unmark
3683                    (progn
3684                      (insert " ")
3685                      (setq gnus-group-marked (delete group gnus-group-marked)))
3686                  (insert "#")
3687                  (setq gnus-group-marked
3688                        (cons group (delete group gnus-group-marked))))
3689                t)
3690              (or no-advance (zerop (gnus-group-next-group 1))))
3691       (setq n (1- n)))
3692     (gnus-summary-position-cursor)
3693     n))
3694
3695 (defun gnus-group-unmark-group (n)
3696   "Remove the mark from the current group."
3697   (interactive "p")
3698   (gnus-group-mark-group n 'unmark))
3699
3700 (defun gnus-group-mark-region (unmark beg end)
3701   "Mark all groups between point and mark.
3702 If UNMARK, remove the mark instead."
3703   (interactive "P\nr")
3704   (let ((num (count-lines beg end)))
3705     (save-excursion
3706       (goto-char beg)
3707       (- num (gnus-group-mark-group num unmark)))))
3708
3709 (defun gnus-group-remove-mark (group)
3710   (and (gnus-group-goto-group group)
3711        (save-excursion
3712          (gnus-group-mark-group 1 'unmark t))))
3713
3714 ;; Return a list of groups to work on.  Take into consideration N (the
3715 ;; prefix) and the list of marked groups.
3716 (defun gnus-group-process-prefix (n)
3717   (cond (n
3718          (setq n (prefix-numeric-value n))
3719          ;; There is a prefix, so we return a list of the N next
3720          ;; groups. 
3721          (let ((way (if (< n 0) -1 1))
3722                (n (abs n))
3723                group groups)
3724            (save-excursion
3725              (while (and (> n 0)
3726                          (setq group (gnus-group-group-name)))
3727                (setq groups (cons group groups))
3728                (setq n (1- n))
3729                (forward-line way)))
3730            (nreverse groups)))
3731         (gnus-group-marked
3732          ;; No prefix, but a list of marked articles.
3733          (reverse gnus-group-marked))
3734         (t
3735          ;; Neither marked articles or a prefix, so we return the
3736          ;; current group.
3737          (let ((group (gnus-group-group-name)))
3738            (and group (list group))))))
3739
3740 ;; Selecting groups.
3741
3742 (defun gnus-group-read-group (&optional all no-article group)
3743   "Read news in this newsgroup.
3744 If the prefix argument ALL is non-nil, already read articles become
3745 readable. If the optional argument NO-ARTICLE is non-nil, no article
3746 will be auto-selected upon group entry."
3747   (interactive "P")
3748   (let ((group (or group (gnus-group-group-name)))
3749         number active marked entry)
3750     (or group (error "No group on current line"))
3751     (setq marked 
3752           (nth 3 (nth 2 (setq entry (gnus-gethash group gnus-newsrc-hashtb)))))
3753     ;; This group might be a dead group. In that case we have to get
3754     ;; the number of unread articles from `gnus-active-hashtb'.
3755     (if entry
3756         (setq number (car entry))
3757       (if (setq active (gnus-gethash group gnus-active-hashtb))
3758           (setq number (- (1+ (cdr active)) (car active)))))
3759     (gnus-summary-read-group 
3760      group (or all (and (numberp number) 
3761                         (zerop (+ number (length (cdr (assq 'tick marked)))
3762                                   (length (cdr (assq 'dormant marked)))))))
3763      no-article)))
3764
3765 (defun gnus-group-select-group (&optional all)
3766   "Select this newsgroup.
3767 No article is selected automatically.
3768 If argument ALL is non-nil, already read articles become readable."
3769   (interactive "P")
3770   (gnus-group-read-group all t))
3771
3772 ;; Enter a group that is not in the group buffer. Non-nil is returned
3773 ;; if selection was successful.
3774 (defun gnus-group-read-ephemeral-group 
3775   (group method &optional activate quit-config)
3776   (let ((group (if (gnus-group-foreign-p group) group
3777                  (gnus-group-prefixed-name group method))))
3778     (gnus-sethash 
3779      group
3780      (list t nil (list group gnus-level-default-subscribed nil nil 
3781                        (append method
3782                                (list
3783                                 (list 'quit-config 
3784                                       (if quit-config quit-config
3785                                         (cons (current-buffer) 'summary)))))))
3786      gnus-newsrc-hashtb)
3787     (set-buffer gnus-group-buffer)
3788     (or (gnus-server-opened method)
3789         (gnus-open-server method)
3790         (error "Unable to contact server: %s" (gnus-status-message method)))
3791     (if activate (or (gnus-request-group group)
3792                      (error "Couldn't request group")))
3793     (condition-case ()
3794         (gnus-group-read-group t t group)
3795       (error nil)
3796       (quit nil))
3797     (not (equal major-mode 'gnus-group-mode))))
3798   
3799 (defun gnus-group-jump-to-group (group)
3800   "Jump to newsgroup GROUP."
3801   (interactive 
3802    (list (completing-read 
3803           "Group: " gnus-active-hashtb nil 
3804           (memq gnus-select-method gnus-have-read-active-file))))
3805
3806   (if (equal group "")
3807       (error "Empty group name"))
3808
3809   (let ((b (text-property-any 
3810             (point-min) (point-max) 'gnus-group (intern group))))
3811     (if b
3812         ;; Either go to the line in the group buffer...
3813         (goto-char b)
3814       ;; ... or insert the line.
3815       (or
3816        (gnus-gethash group gnus-active-hashtb)
3817        (gnus-activate-newsgroup group)
3818        (error "%s error: %s" group (gnus-status-message group)))
3819
3820       (gnus-group-update-group group)
3821       (goto-char (text-property-any 
3822                   (point-min) (point-max) 'gnus-group (intern group)))))
3823   ;; Adjust cursor point.
3824   (gnus-group-position-cursor))
3825
3826 (defun gnus-group-goto-group (group)
3827   "Goto to newsgroup GROUP."
3828   (let ((b (text-property-any (point-min) (point-max) 
3829                               'gnus-group (intern group))))
3830     (and b (goto-char b))))
3831
3832 (defun gnus-group-next-group (n)
3833   "Go to next N'th newsgroup.
3834 If N is negative, search backward instead.
3835 Returns the difference between N and the number of skips actually
3836 done."
3837   (interactive "p")
3838   (gnus-group-next-unread-group n t))
3839
3840 (defun gnus-group-next-unread-group (n &optional all level)
3841   "Go to next N'th unread newsgroup.
3842 If N is negative, search backward instead.
3843 If ALL is non-nil, choose any newsgroup, unread or not.
3844 If LEVEL is non-nil, choose the next group with level LEVEL, or, if no
3845 such group can be found, the next group with a level higher than
3846 LEVEL.
3847 Returns the difference between N and the number of skips actually
3848 made."
3849   (interactive "p")
3850   (let ((backward (< n 0))
3851         (n (abs n)))
3852     (while (and (> n 0)
3853                 (gnus-group-search-forward 
3854                  backward (or (not gnus-group-goto-unread) all) level))
3855       (setq n (1- n)))
3856     (if (/= 0 n) (gnus-message 7 "No more%s newsgroups%s" (if all "" " unread")
3857                                (if level " on this level or higher" "")))
3858     n))
3859
3860 (defun gnus-group-prev-group (n)
3861   "Go to previous N'th newsgroup.
3862 Returns the difference between N and the number of skips actually
3863 done."
3864   (interactive "p")
3865   (gnus-group-next-unread-group (- n) t))
3866
3867 (defun gnus-group-prev-unread-group (n)
3868   "Go to previous N'th unread newsgroup.
3869 Returns the difference between N and the number of skips actually
3870 done."  
3871   (interactive "p")
3872   (gnus-group-next-unread-group (- n)))
3873
3874 (defun gnus-group-next-unread-group-same-level (n)
3875   "Go to next N'th unread newsgroup on the same level.
3876 If N is negative, search backward instead.
3877 Returns the difference between N and the number of skips actually
3878 done."
3879   (interactive "p")
3880   (gnus-group-next-unread-group n t (gnus-group-group-level))
3881   (gnus-group-position-cursor))
3882
3883 (defun gnus-group-prev-unread-group-same-level (n)
3884   "Go to next N'th unread newsgroup on the same level.
3885 Returns the difference between N and the number of skips actually
3886 done."
3887   (interactive "p")
3888   (gnus-group-next-unread-group (- n) t (gnus-group-group-level))
3889   (gnus-group-position-cursor))
3890
3891 (defun gnus-group-best-unread-group (&optional exclude-group)
3892   "Go to the group with the highest level.
3893 If EXCLUDE-GROUP, do not go to that group."
3894   (interactive)
3895   (goto-char (point-min))
3896   (let ((best 100000)
3897         unread best-point)
3898     (while (setq unread (get-text-property (point) 'gnus-unread))
3899       (if (and (numberp unread) (> unread 0))
3900           (progn
3901             (if (and (< (get-text-property (point) 'gnus-level) best)
3902                      (or (not exclude-group)
3903                          (not (equal exclude-group (gnus-group-group-name)))))
3904                 (progn 
3905                   (setq best (get-text-property (point) 'gnus-level))
3906                   (setq best-point (point))))))
3907       (forward-line 1))
3908     (if best-point (goto-char best-point))
3909     (gnus-summary-position-cursor)
3910     (and best-point (gnus-group-group-name))))
3911
3912 (defun gnus-group-first-unread-group ()
3913   "Go to the first group with unread articles."
3914   (interactive)
3915   (goto-char (point-min))
3916   (or (not (zerop (or (get-text-property (point) 'gnus-unread) 0)))
3917       (gnus-group-next-unread-group 1))
3918   (gnus-group-position-cursor))
3919
3920 (defun gnus-group-enter-server-mode ()
3921   "Jump to the server buffer."
3922   (interactive)
3923   (gnus-server-setup-buffer)
3924   (gnus-configure-windows 'server)
3925   (gnus-server-prepare))
3926
3927 (defun gnus-group-make-group (name method &optional address)
3928   "Add a new newsgroup.
3929 The user will be prompted for a NAME, for a select METHOD, and an
3930 ADDRESS."
3931   (interactive
3932    (cons 
3933     (read-string "Group name: ")
3934     (let ((method
3935            (completing-read 
3936             "Method: " (append gnus-valid-select-methods gnus-server-alist)
3937             nil t)))
3938       (if (assoc method gnus-valid-select-methods)
3939           (list method
3940                 (if (memq 'prompt-address
3941                           (assoc method gnus-valid-select-methods))
3942                     (read-string "Address: ")
3943                   ""))
3944         (list method nil)))))
3945   
3946   (let* ((meth (if address (list (intern method) address) method))
3947          (nname (gnus-group-prefixed-name name meth))
3948          info)
3949     (and (gnus-gethash nname gnus-newsrc-hashtb)
3950          (error "Group %s already exists" nname))
3951     (gnus-group-change-level 
3952      (setq info (list t nname gnus-level-default-subscribed nil nil meth))
3953      gnus-level-default-subscribed gnus-level-killed 
3954      (and (gnus-group-group-name)
3955           (gnus-gethash (gnus-group-group-name)
3956                         gnus-newsrc-hashtb))
3957      t)
3958     (gnus-sethash nname (cons 1 0) gnus-active-hashtb)
3959     (gnus-dribble-enter 
3960      (concat "(gnus-group-set-info '" (prin1-to-string (cdr info)) ")"))
3961     (gnus-group-insert-group-line-info nname)
3962
3963     (if (assoc method gnus-valid-select-methods)
3964         (require (intern method)))
3965     (and (gnus-check-backend-function 'request-create-group nname)
3966          (gnus-request-create-group nname))))
3967
3968 (defun gnus-group-edit-group (group &optional part)
3969   "Edit the group on the current line."
3970   (interactive (list (gnus-group-group-name)))
3971   (let ((done-func '(lambda () 
3972                       "Exit editing mode and update the information."
3973                       (interactive)
3974                       (gnus-group-edit-group-done 'part 'group)))
3975         (part (or part 'info))
3976         (winconf (current-window-configuration))
3977         info)
3978     (or group (error "No group on current line"))
3979     (or (setq info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
3980         (error "Killed group; can't be edited"))
3981     (set-buffer (get-buffer-create gnus-group-edit-buffer))
3982     (gnus-configure-windows 'edit-group)
3983     (gnus-add-current-to-buffer-list)
3984     (emacs-lisp-mode)
3985     ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3986     (use-local-map (copy-keymap emacs-lisp-mode-map))
3987     (local-set-key "\C-c\C-c" done-func)
3988     (make-local-variable 'gnus-prev-winconf)
3989     (setq gnus-prev-winconf winconf)
3990     ;; We modify the func to let it know what part it is editing.
3991     (setcar (cdr (nth 4 done-func)) (list 'quote part))
3992     (setcar (cdr (cdr (nth 4 done-func))) group)
3993     (erase-buffer)
3994     (insert
3995      (cond 
3996       ((eq part 'method)
3997        ";; Type `C-c C-c' after editing the select method.\n\n")
3998       ((eq part 'params)
3999        ";; Type `C-c C-c' after editing the group parameters.\n\n")
4000       ((eq part 'info)
4001        ";; Type `C-c C-c' after editing the group info.\n\n")))
4002     (let ((cinfo (gnus-copy-sequence info))
4003           marked)
4004       (if (not (setq marked (nth 3 cinfo)))
4005           ()
4006         (while marked
4007           (or (eq 'score (car (car marked)))
4008               (eq 'bookmark (car (car marked)))
4009               (eq 'killed (car (car marked)))
4010               (not (numberp (car (cdr (car marked)))))
4011               (setcdr (car marked) 
4012                       (gnus-compress-sequence (sort (cdr (car marked)) '<) t)))
4013           (setq marked (cdr marked))))
4014       (insert 
4015        (pp-to-string
4016         (cond ((eq part 'method)
4017                (or (nth 4 info) "native"))
4018               ((eq part 'params)
4019                (nth 5 info))
4020               (t
4021                cinfo)))
4022        "\n"))))
4023
4024 (defun gnus-group-edit-group-method (group)
4025   "Edit the select method of GROUP."
4026   (interactive (list (gnus-group-group-name)))
4027   (gnus-group-edit-group group 'method))
4028
4029 (defun gnus-group-edit-group-parameters (group)
4030   "Edit the group parameters of GROUP."
4031   (interactive (list (gnus-group-group-name)))
4032   (gnus-group-edit-group group 'params))
4033
4034 (defun gnus-group-edit-group-done (part group)
4035   "Get info from buffer, update variables and jump to the group buffer."
4036   (set-buffer (get-buffer-create gnus-group-edit-buffer))
4037   (goto-char (point-min))
4038   (let ((form (read (current-buffer)))
4039         (winconf gnus-prev-winconf))
4040     (if (eq part 'info) 
4041         (gnus-group-set-info form)
4042       (gnus-group-set-info form group part))
4043     (kill-buffer (current-buffer))
4044     (and winconf (set-window-configuration winconf))
4045     (set-buffer gnus-group-buffer)
4046     (gnus-group-update-group (gnus-group-group-name))
4047     (gnus-group-position-cursor)))
4048
4049 (defun gnus-group-make-help-group ()
4050   "Create the (ding) Gnus documentation group."
4051   (interactive)
4052   (let ((path load-path)
4053         name)
4054     (and (gnus-gethash (setq name (gnus-group-prefixed-name
4055                                    "gnus-help" '(nndoc "gnus-help")))
4056                        gnus-newsrc-hashtb)
4057          (error "Documentation group already exists"))
4058     (while (and path
4059                 (not (file-exists-p (concat (file-name-as-directory (car path))
4060                                             "doc.txt"))))
4061       (setq path (cdr path)))
4062     (or path (error "Couldn't find doc group"))
4063     (gnus-group-make-group 
4064      (gnus-group-real-name name)
4065      (list 'nndoc name
4066            (list 'nndoc-address (concat (file-name-as-directory (car path)) "doc.txt"))
4067            (list 'nndoc-article-type 'mbox))))
4068   (gnus-group-position-cursor))
4069
4070 (defun gnus-group-make-doc-group (file type)
4071   "Create a group that uses a single file as the source."
4072   (interactive 
4073    (list (read-file-name "File name: ") 
4074          (let ((err "")
4075                found char)
4076            (while (not found)
4077              (message "%sFile type (mbox, babyl, digest) [mbd]: " err)
4078              (setq found (cond ((= (setq char (read-char)) ?m) 'mbox)
4079                                ((= char ?b) 'babyl)
4080                                ((= char ?d) 'digest)
4081                                (t (setq err (format "%c unknown. " char))
4082                                   nil))))
4083            found)))
4084   (let* ((file (expand-file-name file))
4085          (name (gnus-generate-new-group-name
4086                 (gnus-group-prefixed-name
4087                  (file-name-nondirectory file) '(nndoc "")))))
4088     (gnus-group-make-group 
4089      (gnus-group-real-name name)
4090      (list 'nndoc name
4091            (list 'nndoc-address file)
4092            (list 'nndoc-article-type type)))))
4093
4094 (defun gnus-group-make-archive-group ()
4095   "Create the (ding) Gnus archive group."
4096   (interactive)
4097   (and (gnus-gethash (gnus-group-prefixed-name "ding.archives" '(nndir ""))
4098                      gnus-newsrc-hashtb)
4099        (error "Archive group already exists"))
4100   (gnus-group-make-group "ding.archives" "nndir" gnus-group-archive-directory)
4101   (gnus-group-position-cursor))
4102
4103 (defun gnus-group-make-directory-group (dir)
4104   "Create an nndir group.
4105 The user will be prompted for a directory. The contents of this
4106 directory will be used as a newsgroup. The directory should contain
4107 mail messages or news articles in files that have numeric names."
4108   (interactive
4109    (list (read-file-name "Create group from directory: ")))
4110   (or (file-exists-p dir) (error "No such directory"))
4111   (or (file-directory-p dir) (error "Not a directory"))
4112   (gnus-group-make-group dir "nndir" dir)
4113   (gnus-group-position-cursor))
4114
4115 (defun gnus-group-make-kiboze-group (group address scores)
4116   "Create an nnkiboze group.
4117 The user will be prompted for a name, a regexp to match groups, and
4118 score file entries for articles to include in the group."
4119   (interactive
4120    (list
4121     (read-string "nnkiboze group name: ")
4122     (read-string "Source groups (regexp): ")
4123     (let ((headers (mapcar (lambda (group) (list group))
4124                            '("subject" "from" "number" "date" "message-id"
4125                              "references" "chars" "lines" "xref")))
4126           scores header regexp regexps)
4127       (while (not (equal "" (setq header (completing-read 
4128                                           "Match on header: " headers nil t))))
4129         (setq regexps nil)
4130         (while (not (equal "" (setq regexp (read-string 
4131                                             (format "Match on %s (string): "
4132                                                     header)))))
4133           (setq regexps (cons (list regexp nil nil 'r) regexps)))
4134         (setq scores (cons (cons header regexps) scores)))
4135       scores)))
4136   (gnus-group-make-group group "nnkiboze" address)
4137   (save-excursion
4138     (gnus-set-work-buffer)
4139     (let (emacs-lisp-mode-hook)
4140       (pp scores (current-buffer)))
4141     (write-region (point-min) (point-max) 
4142                   (concat (or gnus-kill-files-directory "~/News")
4143                           "nnkiboze:" group "." gnus-score-file-suffix)))
4144   (gnus-group-position-cursor))
4145
4146 (defun gnus-group-add-to-virtual (n vgroup)
4147   "Add the current group to a virtual group."
4148   (interactive
4149    (list current-prefix-arg
4150          (completing-read "Add to virtual group: " gnus-newsrc-hashtb nil t
4151                           "nnvirtual:")))
4152   (or (eq (car (gnus-find-method-for-group vgroup)) 'nnvirtual)
4153       (error "%s is not an nnvirtual group" vgroup))
4154   (let* ((groups (gnus-group-process-prefix n))
4155          (method (nth 4 (nth 2 (gnus-gethash vgroup gnus-newsrc-hashtb)))))
4156     (setcar (cdr method)
4157             (concat 
4158              (nth 1 method) "\\|"
4159              (mapconcat 
4160               (lambda (s) 
4161                 (gnus-group-remove-mark s)
4162                 (concat "\\(^" (regexp-quote s) "$\\)"))
4163               groups "\\|"))))
4164   (gnus-group-position-cursor))
4165
4166 (defun gnus-group-make-empty-virtual (group)
4167   "Create a new, fresh, empty virtual group."
4168   (interactive "sCreate new, empty virtual group: ")
4169   (let* ((method (list 'nnvirtual "^$"))
4170          (pgroup (gnus-group-prefixed-name group method)))
4171     ;; Check whether it exists already.
4172     (and (gnus-gethash pgroup gnus-newsrc-hashtb)
4173          (error "Group %s already exists." pgroup))
4174     ;; Subscribe the new group after the group on the current line.
4175     (gnus-subscribe-group pgroup (gnus-group-group-name) method)
4176     (gnus-group-update-group pgroup)
4177     (forward-line -1)
4178     (gnus-group-position-cursor)))
4179
4180 (defun gnus-group-enter-directory (dir)
4181   "Enter an ephemeral nneething group."
4182   (interactive "DDirectory to read: ")
4183   (let* ((method (list 'nneething dir))
4184          (leaf (gnus-group-prefixed-name
4185                 (file-name-nondirectory (directory-file-name dir))
4186                 method))
4187          (name (gnus-generate-new-group-name leaf)))
4188     (let ((nneething-read-only t))
4189       (or (gnus-group-read-ephemeral-group 
4190            name method t
4191            (cons (current-buffer) (if (eq major-mode 'gnus-summary-mode)
4192                                       'summary 'group)))
4193           (error "Couldn't enter %s" dir)))))
4194
4195 ;; Group sorting commands
4196 ;; Suggested by Joe Hildebrand <hildjj@idaho.fuentez.com>.
4197
4198 (defun gnus-group-sort-groups ()
4199   "Sort the group buffer using `gnus-group-sort-function'."
4200   (interactive)
4201   (setq gnus-newsrc-alist 
4202         (sort (cdr gnus-newsrc-alist) gnus-group-sort-function))
4203   (gnus-make-hashtable-from-newsrc-alist)
4204   (gnus-group-list-groups (if gnus-have-all-newsgroups gnus-level-unsubscribed)
4205                           gnus-have-all-newsgroups))
4206
4207 (defun gnus-group-sort-by-alphabet (info1 info2)
4208   (string< (car info1) (car info2)))
4209
4210 (defun gnus-group-sort-by-unread (info1 info2)
4211   (let ((n1 (car (gnus-gethash (car info1) gnus-newsrc-hashtb)))
4212         (n2 (car (gnus-gethash (car info2) gnus-newsrc-hashtb))))
4213     (< (or (and (numberp n1) n1) 0)
4214        (or (and (numberp n2) n2) 0))))
4215
4216 (defun gnus-group-sort-by-level (info1 info2)
4217   (< (nth 1 info1) (nth 1 info2)))
4218
4219 ;; Group catching up.
4220
4221 (defun gnus-group-catchup-current (&optional n all)
4222   "Mark all articles not marked as unread in current newsgroup as read.
4223 If prefix argument N is numeric, the ARG next newsgroups will be
4224 caught up. If ALL is non-nil, marked articles will also be marked as
4225 read. Cross references (Xref: header) of articles are ignored.
4226 The difference between N and actual number of newsgroups that were
4227 caught up is returned."
4228   (interactive "P")
4229   (if (not (or (not gnus-interactive-catchup) ;Without confirmation?
4230                gnus-expert-user
4231                (gnus-y-or-n-p
4232                 (if all
4233                     "Do you really want to mark all articles as read? "
4234                   "Mark all unread articles as read? "))))
4235       n
4236     (let ((groups (gnus-group-process-prefix n))
4237           (ret 0))
4238       (while groups
4239         ;; Virtual groups have to be given special treatment. 
4240         (let ((method (gnus-find-method-for-group (car groups))))
4241           (if (eq 'nnvirtual (car method))
4242               (nnvirtual-catchup-group
4243                (gnus-group-real-name (car groups)) (nth 1 method) all)))
4244         (gnus-group-remove-mark (car groups))
4245         (if (prog1
4246                 (gnus-group-goto-group (car groups))
4247               (gnus-group-catchup (car groups) all))
4248             (gnus-group-update-group-line)
4249           (setq ret (1+ ret)))
4250         (setq groups (cdr groups)))
4251       (gnus-group-next-unread-group 1)
4252       ret)))
4253
4254 (defun gnus-group-catchup-current-all (&optional n)
4255   "Mark all articles in current newsgroup as read.
4256 Cross references (Xref: header) of articles are ignored."
4257   (interactive "P")
4258   (gnus-group-catchup-current n 'all))
4259
4260 (defun gnus-group-catchup (group &optional all)
4261   "Mark all articles in GROUP as read.
4262 If ALL is non-nil, all articles are marked as read.
4263 The return value is the number of articles that were marked as read,
4264 or nil if no action could be taken."
4265   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4266          (num (car entry))
4267          (marked (nth 3 (nth 2 entry))))
4268     (if (not (numberp (car entry)))
4269         (gnus-message 1 "Can't catch up; non-active group")
4270       ;; Do the updating only if the newsgroup isn't killed.
4271       (if (not entry)
4272           ()
4273         (gnus-update-read-articles 
4274          group (and (not all) (append (cdr (assq 'tick marked))
4275                                       (cdr (assq 'dormant marked))))
4276          nil (and (not all) (cdr (assq 'tick marked))))
4277         (and all marked
4278              (setcar (nthcdr 3 (nth 2 entry)) 
4279                      (delq (assq 'dormant marked) 
4280                            (nth 3 (nth 2 entry)))))))
4281     num))
4282
4283 (defun gnus-group-expire-articles (&optional n)
4284   "Expire all expirable articles in the current newsgroup."
4285   (interactive "P")
4286   (let ((groups (gnus-group-process-prefix n))
4287         group)
4288     (or groups (error "No groups to expire"))
4289     (while groups
4290       (setq group (car groups)
4291             groups (cdr groups))
4292       (gnus-group-remove-mark group)
4293       (if (not (gnus-check-backend-function 'request-expire-articles group))
4294           ()
4295         (let* ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
4296                (expirable (if (memq 'total-expire (nth 5 info))
4297                               (cons nil (gnus-list-of-read-articles group))
4298                             (assq 'expire (nth 3 info)))))
4299           (and expirable 
4300                (setcdr expirable
4301                        (gnus-request-expire-articles 
4302                         (cdr expirable) group))))))))
4303
4304 (defun gnus-group-expire-all-groups ()
4305   "Expire all expirable articles in all newsgroups."
4306   (interactive)
4307   (save-excursion
4308     (gnus-message 5 "Expiring...")
4309     (let ((gnus-group-marked (mapcar (lambda (info) (car info))
4310                                      (cdr gnus-newsrc-alist))))
4311       (gnus-group-expire-articles nil)))
4312   (gnus-group-position-cursor)
4313   (gnus-message 5 "Expiring...done"))
4314
4315 (defun gnus-group-set-current-level (n level)
4316   "Set the level of the next N groups to LEVEL."
4317   (interactive "P\nnLevel: ")
4318   (or (and (>= level 1) (<= level gnus-level-killed))
4319       (error "Illegal level: %d" level))
4320   (let ((groups (gnus-group-process-prefix n))
4321         group)
4322     (while groups
4323       (setq group (car groups)
4324             groups (cdr groups))
4325       (gnus-group-remove-mark group)
4326       (gnus-message 6 "Changed level of %s from %d to %d" 
4327                     group (gnus-group-group-level) level)
4328       (gnus-group-change-level group level
4329                                (gnus-group-group-level))
4330       (gnus-group-update-group-line)))
4331   (gnus-group-position-cursor))
4332
4333 (defun gnus-group-unsubscribe-current-group (&optional n)
4334   "Toggle subscription of the current group.
4335 If given numerical prefix, toggle the N next groups."
4336   (interactive "P")
4337   (let ((groups (gnus-group-process-prefix n))
4338         group)
4339     (while groups
4340       (setq group (car groups)
4341             groups (cdr groups))
4342       (gnus-group-remove-mark group)
4343       (gnus-group-unsubscribe-group
4344        group (if (<= (gnus-group-group-level) gnus-level-subscribed)
4345                  gnus-level-default-unsubscribed
4346                gnus-level-default-subscribed))
4347       (gnus-group-update-group-line))
4348     (gnus-group-next-group 1)))
4349
4350 (defun gnus-group-unsubscribe-group (group &optional level)
4351   "Toggle subscribe from/to unsubscribe GROUP.
4352 New newsgroup is added to .newsrc automatically."
4353   (interactive
4354    (list (completing-read
4355           "Group: " gnus-active-hashtb nil 
4356           (memq gnus-select-method gnus-have-read-active-file))))
4357   (let ((newsrc (gnus-gethash group gnus-newsrc-hashtb)))
4358     (cond (newsrc
4359            ;; Toggle subscription flag.
4360            (gnus-group-change-level 
4361             newsrc (if level level (if (<= (nth 1 (nth 2 newsrc)) 
4362                                            gnus-level-subscribed) 
4363                                        (1+ gnus-level-subscribed)
4364                                      gnus-level-default-subscribed)))
4365            (gnus-group-update-group group))
4366           ((and (stringp group)
4367                 (or (not gnus-have-read-active-file)
4368                     (gnus-gethash group gnus-active-hashtb)))
4369            ;; Add new newsgroup.
4370            (gnus-group-change-level 
4371             group 
4372             (if level level gnus-level-default-subscribed) 
4373             (or (and (member group gnus-zombie-list) 
4374                      gnus-level-zombie) 
4375                 gnus-level-killed)
4376             (and (gnus-group-group-name)
4377                  (gnus-gethash (gnus-group-group-name) gnus-newsrc-hashtb)))
4378            (gnus-group-update-group group))
4379           (t (error "No such newsgroup: %s" group)))
4380     (gnus-group-position-cursor)))
4381
4382 (defun gnus-group-transpose-groups (n)
4383   "Move the current newsgroup up N places.
4384 If given a negative prefix, move down instead. The difference between
4385 N and the number of steps taken is returned." 
4386   (interactive "p")
4387   (or (gnus-group-group-name)
4388       (error "No group on current line"))
4389   (gnus-group-kill-group 1)
4390   (prog1
4391       (forward-line (- n))
4392     (gnus-group-yank-group)
4393     (gnus-group-position-cursor)))
4394
4395 (defun gnus-group-kill-all-zombies ()
4396   "Kill all zombie newsgroups."
4397   (interactive)
4398   (setq gnus-killed-list (nconc gnus-zombie-list gnus-killed-list))
4399   (setq gnus-zombie-list nil)
4400   (funcall gnus-group-prepare-function gnus-level-subscribed nil nil)
4401   (goto-char (point-min))
4402   (gnus-group-position-cursor))
4403
4404 (defun gnus-group-kill-region (begin end)
4405   "Kill newsgroups in current region (excluding current point).
4406 The killed newsgroups can be yanked by using \\[gnus-group-yank-group]."
4407   (interactive "r")
4408   (let ((lines
4409          ;; Count lines.
4410          (save-excursion
4411            (count-lines
4412             (progn
4413               (goto-char begin)
4414               (beginning-of-line)
4415               (point))
4416             (progn
4417               (goto-char end)
4418               (beginning-of-line)
4419               (point))))))
4420     (goto-char begin)
4421     (beginning-of-line)                 ;Important when LINES < 1
4422     (gnus-group-kill-group lines)))
4423
4424 (defun gnus-group-kill-group (&optional n)
4425   "The the next N groups.
4426 The killed newsgroups can be yanked by using \\[gnus-group-yank-group].
4427 However, only groups that were alive can be yanked; already killed 
4428 groups or zombie groups can't be yanked.
4429 The return value is the name of the (last) group that was killed."
4430   (interactive "P")
4431   (let ((buffer-read-only nil)
4432         (groups (gnus-group-process-prefix n))
4433         group entry level)
4434     (while groups
4435       (setq group (car groups)
4436             groups (cdr groups))
4437       (gnus-group-remove-mark group)
4438       (setq level (gnus-group-group-level))
4439       (gnus-delete-line)
4440       (if (setq entry (gnus-gethash group gnus-newsrc-hashtb))
4441           (setq gnus-list-of-killed-groups 
4442                 (cons (cons (car entry) (nth 2 entry)) 
4443                       gnus-list-of-killed-groups)))
4444       (gnus-group-change-level 
4445        (if entry entry group) gnus-level-killed (if entry nil level)))
4446     (gnus-group-position-cursor)
4447     group))
4448
4449 (defun gnus-group-yank-group (&optional arg)
4450   "Yank the last newsgroups killed with \\[gnus-group-kill-group],
4451 inserting it before the current newsgroup.  The numeric ARG specifies
4452 how many newsgroups are to be yanked.  The name of the (last)
4453 newsgroup yanked is returned."
4454   (interactive "p")
4455   (if (not arg) (setq arg 1))
4456   (let (info group prev)
4457     (while (>= (setq arg (1- arg)) 0)
4458       (if (not (setq info (car gnus-list-of-killed-groups)))
4459           (error "No more newsgroups to yank"))
4460       (setq group (nth 2 info))
4461       ;; Find which newsgroup to insert this one before - search
4462       ;; backward until something suitable is found. If there are no
4463       ;; other newsgroups in this buffer, just make this newsgroup the
4464       ;; first newsgroup.
4465       (setq prev (gnus-group-group-name))
4466       (gnus-group-change-level 
4467        info (nth 2 info) gnus-level-killed 
4468        (and prev (gnus-gethash prev gnus-newsrc-hashtb))
4469        t)
4470       (gnus-group-insert-group-line-info (nth 1 info))
4471       (setq gnus-list-of-killed-groups 
4472             (cdr gnus-list-of-killed-groups)))
4473     (forward-line -1)
4474     (gnus-group-position-cursor)
4475     group))
4476       
4477 (defun gnus-group-list-all-groups (&optional arg)
4478   "List all newsgroups with level ARG or lower.
4479 Default is gnus-level-unsubscribed, which lists all subscribed and most
4480 unsubscribed groups."
4481   (interactive "P")
4482   (setq arg (or arg gnus-level-unsubscribed))
4483   (gnus-group-list-groups arg t))
4484
4485 (defun gnus-group-list-killed ()
4486   "List all killed newsgroups in the group buffer."
4487   (interactive)
4488   (if (not gnus-killed-list)
4489       (gnus-message 6 "No killed groups")
4490     (funcall gnus-group-prepare-function gnus-level-killed t gnus-level-killed)
4491     (goto-char (point-min)))
4492   (gnus-group-position-cursor))
4493
4494 (defun gnus-group-list-zombies ()
4495   "List all zombie newsgroups in the group buffer."
4496   (interactive)
4497   (if (not gnus-zombie-list)
4498       (gnus-message 6 "No zombie groups")
4499     (funcall gnus-group-prepare-function gnus-level-zombie t gnus-level-zombie)
4500     (goto-char (point-min)))
4501   (gnus-group-position-cursor))
4502
4503 (defun gnus-group-get-new-news (&optional arg)
4504   "Get newly arrived articles.
4505 If ARG is non-nil, it should be a number between one and nine to
4506 specify which levels you are interested in re-scanning."
4507   (interactive "P")
4508   (run-hooks 'gnus-get-new-news-hook)
4509   (if gnus-group-use-permanent-levels
4510       (setq arg
4511             (setq gnus-group-default-list-level 
4512                   (or arg gnus-group-default-list-level
4513                       gnus-level-subscribed))))
4514   (if (and gnus-read-active-file (not arg))
4515       (progn
4516         (gnus-read-active-file)
4517         (gnus-get-unread-articles (or arg (1+ gnus-level-subscribed))))
4518     (let ((gnus-read-active-file (not arg))
4519           (gnus-have-read-active-file 
4520            (and (not arg) gnus-have-read-active-file)))
4521       (gnus-get-unread-articles (or arg (1+ gnus-level-subscribed)))))
4522   (gnus-group-list-groups (or (and gnus-group-use-permanent-levels arg)
4523                               gnus-group-default-list-level
4524                               gnus-level-subscribed)
4525                           gnus-have-all-newsgroups))
4526
4527 (defun gnus-group-get-new-news-this-group (&optional n)
4528   "Check for newly arrived news in the current group (and the N-1 next groups).
4529 The difference between N and the number of newsgroup checked is returned.
4530 If N is negative, this group and the N-1 previous groups will be checked."
4531   (interactive "P")
4532   (let* ((groups (gnus-group-process-prefix n))
4533          (ret (if (numberp n) (- n (length groups)) 0))
4534          group)
4535     (while groups
4536       (setq group (car groups)
4537             groups (cdr groups))
4538       (gnus-group-remove-mark group)
4539       (or (gnus-get-new-news-in-group group)
4540           (progn 
4541             (ding) 
4542             (message "%s error: %s" group (gnus-status-message group))
4543             (sit-for 2))))
4544     ;; !!! I don't know why the buffer scrolls forward when updating
4545     ;; the first line in the group buffer, but it does. So we set the
4546     ;; window start forcibly.
4547 ;    (set-window-start (get-buffer-window (current-buffer)) w-p)
4548     (gnus-group-next-unread-group 1 t)
4549     (gnus-summary-position-cursor)
4550     ret))
4551
4552 (defun gnus-get-new-news-in-group (group)
4553   (and group 
4554        (gnus-activate-newsgroup group)
4555        (progn
4556          (gnus-get-unread-articles-in-group 
4557           (nth 2 (gnus-gethash group gnus-newsrc-hashtb))
4558           (gnus-gethash group gnus-active-hashtb))
4559          (gnus-group-update-group-line)
4560          t)))
4561
4562 (defun gnus-group-fetch-faq (group)
4563   "Fetch the FAQ for the current group."
4564   (interactive (list (gnus-group-real-name (gnus-group-group-name))))
4565   (or group (error "No group name given"))
4566   (let ((file (concat gnus-group-faq-directory group))) 
4567     (if (not (file-exists-p file))
4568         (error "No such file: %s" file)
4569       (find-file file))))
4570   
4571 (defun gnus-group-describe-group (force &optional group)
4572   "Display a description of the current newsgroup."
4573   (interactive (list current-prefix-arg (gnus-group-group-name)))
4574   (and force (setq gnus-description-hashtb nil))
4575   (let ((method (gnus-find-method-for-group group))
4576         desc)
4577     (or group (error "No group name given"))
4578     (and (or (and gnus-description-hashtb
4579                   ;; We check whether this group's method has been
4580                   ;; queried for a description file.  
4581                   (gnus-gethash 
4582                    (gnus-group-prefixed-name "" method) 
4583                    gnus-description-hashtb))
4584              (setq desc (gnus-group-get-description group))
4585              (gnus-read-descriptions-file method))
4586          (message
4587           (or desc (gnus-gethash group gnus-description-hashtb)
4588               "No description available")))))
4589
4590 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
4591 (defun gnus-group-describe-all-groups (&optional force)
4592   "Pop up a buffer with descriptions of all newsgroups."
4593   (interactive "P")
4594   (and force (setq gnus-description-hashtb nil))
4595   (if (not (or gnus-description-hashtb
4596                (gnus-read-all-descriptions-files)))
4597       (error "Couldn't request descriptions file"))
4598   (let ((buffer-read-only nil)
4599         b)
4600     (erase-buffer)
4601     (mapatoms
4602      (lambda (group)
4603        (setq b (point))
4604        (insert (format "      *: %-20s %s\n" (symbol-name group)
4605                        (symbol-value group)))
4606        (add-text-properties 
4607         b (1+ b) (list 'gnus-group group
4608                        'gnus-unread t 'gnus-marked nil
4609                        'gnus-level (1+ gnus-level-subscribed))))
4610      gnus-description-hashtb)
4611     (goto-char (point-min))
4612     (gnus-group-position-cursor)))
4613
4614 ;; Suggested by by Daniel Quinlan <quinlan@best.com>.
4615 (defun gnus-group-apropos (regexp &optional search-description)
4616   "List all newsgroups that have names that match a regexp."
4617   (interactive "sGnus apropos (regexp): ")
4618   (let ((prev "")
4619         (obuf (current-buffer))
4620         groups des)
4621     ;; Go through all newsgroups that are known to Gnus.
4622     (mapatoms 
4623      (lambda (group)
4624        (and (string-match regexp (symbol-name group))
4625             (setq groups (cons (symbol-name group) groups))))
4626      gnus-active-hashtb)
4627     ;; Go through all descriptions that are known to Gnus. 
4628     (if search-description
4629         (mapatoms 
4630          (lambda (group)
4631            (and (string-match regexp (symbol-value group))
4632                 (gnus-gethash (symbol-name group) gnus-active-hashtb)
4633                 (setq groups (cons (symbol-name group) groups))))
4634          gnus-description-hashtb))
4635     (if (not groups)
4636         (gnus-message 3 "No groups matched \"%s\"." regexp)
4637       ;; Print out all the groups.
4638       (save-excursion
4639         (pop-to-buffer "*Gnus Help*")
4640         (buffer-disable-undo (current-buffer))
4641         (erase-buffer)
4642         (setq groups (sort groups 'string<))
4643         (while groups
4644           ;; Groups may be entered twice into the list of groups.
4645           (if (not (string= (car groups) prev))
4646               (progn
4647                 (insert (setq prev (car groups)) "\n")
4648                 (if (and gnus-description-hashtb
4649                          (setq des (gnus-gethash (car groups) 
4650                                                  gnus-description-hashtb)))
4651                     (insert "  " des "\n"))))
4652           (setq groups (cdr groups)))
4653         (goto-char (point-min))))
4654     (pop-to-buffer obuf)))
4655
4656 (defun gnus-group-description-apropos (regexp)
4657   "List all newsgroups that have names or descriptions that match a regexp."
4658   (interactive "sGnus description apropos (regexp): ")
4659   (if (not (or gnus-description-hashtb
4660                (gnus-read-all-descriptions-files)))
4661       (error "Couldn't request descriptions file"))
4662   (gnus-group-apropos regexp t))
4663
4664 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
4665 (defun gnus-group-list-matching (level regexp &optional all lowest) 
4666   "List all groups with unread articles that match REGEXP.
4667 If the prefix LEVEL is non-nil, it should be a number that says which
4668 level to cut off listing groups. 
4669 If ALL, also list groups with no unread articles.
4670 If LOWEST, don't list groups with level lower than LOWEST."
4671   (interactive "P\nsList newsgroups matching: ")
4672   (gnus-group-prepare-flat (or level gnus-level-subscribed)
4673                            all (or lowest 1) regexp)
4674   (goto-char (point-min))
4675   (gnus-group-position-cursor))
4676
4677 (defun gnus-group-list-all-matching (level regexp &optional lowest) 
4678   "List all groups that match REGEXP.
4679 If the prefix LEVEL is non-nil, it should be a number that says which
4680 level to cut off listing groups. 
4681 If LOWEST, don't list groups with level lower than LOWEST."
4682   (interactive "P\nsList newsgroups matching: ")
4683   (gnus-group-list-matching (or level gnus-level-killed) regexp t lowest))
4684
4685 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
4686 (defun gnus-group-save-newsrc ()
4687   "Save the Gnus startup files."
4688   (interactive)
4689   (gnus-save-newsrc-file))
4690
4691 (defun gnus-group-restart (&optional arg)
4692   "Force Gnus to read the .newsrc file."
4693   (interactive "P")
4694   (gnus-save-newsrc-file)
4695   (gnus-setup-news 'force)
4696   (gnus-group-list-groups arg gnus-have-all-newsgroups))
4697
4698 (defun gnus-group-read-init-file ()
4699   "Read the Gnus elisp init file."
4700   (interactive)
4701   (gnus-read-init-file))
4702
4703 (defun gnus-group-check-bogus-groups (&optional silent)
4704   "Check bogus newsgroups.
4705 If given a prefix, don't ask for confirmation before removing a bogus
4706 group."
4707   (interactive "P")
4708   (gnus-check-bogus-newsgroups (and (not silent) (not gnus-expert-user)))
4709   (gnus-group-list-groups nil gnus-have-all-newsgroups))
4710
4711 (defun gnus-group-edit-global-kill (&optional article group)
4712   "Edit the global kill file.
4713 If GROUP, edit that local kill file instead."
4714   (interactive "P")
4715   (setq gnus-current-kill-article article)
4716   (gnus-kill-file-edit-file group)
4717   (gnus-message 6
4718    (substitute-command-keys
4719     "Editing a global kill file (Type \\[gnus-kill-file-exit] to exit)")))
4720
4721 (defun gnus-group-edit-local-kill (article group)
4722   "Edit a local kill file."
4723   (interactive (list nil (gnus-group-group-name)))
4724   (gnus-group-edit-global-kill article group))
4725
4726 (defun gnus-group-force-update ()
4727   "Update `.newsrc' file."
4728   (interactive)
4729   (gnus-save-newsrc-file))
4730
4731 (defun gnus-group-suspend ()
4732   "Suspend the current Gnus session.
4733 In fact, cleanup buffers except for group mode buffer.
4734 The hook gnus-suspend-gnus-hook is called before actually suspending."
4735   (interactive)
4736   (run-hooks 'gnus-suspend-gnus-hook)
4737   ;; Kill Gnus buffers except for group mode buffer.
4738   (let ((group-buf (get-buffer gnus-group-buffer)))
4739     ;; Do this on a separate list in case the user does a ^G before we finish
4740     (let ((gnus-buffer-list
4741            (delq group-buf (delq gnus-dribble-buffer
4742                                  (append gnus-buffer-list nil)))))
4743       (while gnus-buffer-list
4744         (gnus-kill-buffer (car gnus-buffer-list))
4745         (setq gnus-buffer-list (cdr gnus-buffer-list))))
4746     (if group-buf
4747         (progn
4748           (setq gnus-buffer-list (list group-buf))
4749           (bury-buffer group-buf)
4750           (delete-windows-on group-buf t)))))
4751
4752 (defun gnus-group-clear-dribble ()
4753   "Clear all information from the dribble buffer."
4754   (interactive)
4755   (gnus-dribble-clear))
4756
4757 (defun gnus-group-exit ()
4758   "Quit reading news after updating .newsrc.eld and .newsrc.
4759 The hook `gnus-exit-gnus-hook' is called before actually exiting."
4760   (interactive)
4761   (if (or noninteractive                ;For gnus-batch-kill
4762           (zerop (buffer-size))         ;No news is good news.
4763           (not (gnus-server-opened gnus-select-method)) ;NNTP connection closed
4764           (not gnus-interactive-exit)   ;Without confirmation
4765           gnus-expert-user
4766           (gnus-y-or-n-p "Are you sure you want to quit reading news? "))
4767       (progn
4768         (run-hooks 'gnus-exit-gnus-hook)
4769         ;; Offer to save data from non-quitted summary buffers.
4770         (gnus-offer-save-summaries)
4771         ;; Save the newsrc file(s).
4772         (gnus-save-newsrc-file)
4773         ;; Kill-em-all.
4774         (gnus-close-backends)
4775         ;; Reset everything.
4776         (gnus-clear-system))))
4777
4778 (defun gnus-close-backends ()
4779   ;; Send a close request to all backends that support such a request. 
4780   (let ((methods gnus-valid-select-methods)
4781         func)
4782     (while methods
4783       (if (fboundp (setq func (intern (concat (car (car methods))
4784                                               "-request-close"))))
4785           (funcall func))
4786       (setq methods (cdr methods)))))
4787
4788 (defun gnus-group-quit ()
4789   "Quit reading news without updating .newsrc.eld or .newsrc.
4790 The hook `gnus-exit-gnus-hook' is called before actually exiting."
4791   (interactive)
4792   (if (or noninteractive                ;For gnus-batch-kill
4793           (zerop (buffer-size))
4794           (not (gnus-server-opened gnus-select-method))
4795           gnus-expert-user
4796           (not gnus-current-startup-file)
4797           (gnus-yes-or-no-p
4798            (format "Quit reading news without saving %s? "
4799                    (file-name-nondirectory gnus-current-startup-file))))
4800       (progn
4801         (run-hooks 'gnus-exit-gnus-hook)
4802         (if gnus-use-full-window
4803             (delete-other-windows)
4804           (gnus-remove-some-windows))
4805         (gnus-dribble-save)
4806         (gnus-close-backends)
4807         (gnus-clear-system))))
4808
4809 (defun gnus-offer-save-summaries ()
4810   (let ((buffers (buffer-list)))
4811     (save-excursion
4812       (while buffers
4813         (and 
4814          ;; We look for buffers with "Summary" in the name.
4815          (string-match "Summary" (or (buffer-name (car buffers)) ""))
4816          (progn
4817            (set-buffer (car buffers))
4818            ;; We check that this is, indeed, a summary buffer.
4819            (eq major-mode 'gnus-summary-mode)) 
4820          ;; We ask the user whether she wants to save the info.
4821          (gnus-y-or-n-p
4822                (format "Update summary buffer %s? " (buffer-name)))
4823          ;; We do it by simply exiting.
4824          (gnus-summary-exit))
4825         (setq buffers (cdr buffers))))))
4826
4827 (defun gnus-group-describe-briefly ()
4828   "Give a one line description of the group mode commands."
4829   (interactive)
4830   (gnus-message 7 (substitute-command-keys "\\<gnus-group-mode-map>\\[gnus-group-read-group]:Select  \\[gnus-group-next-unread-group]:Forward  \\[gnus-group-prev-unread-group]:Backward  \\[gnus-group-exit]:Exit  \\[gnus-info-find-node]:Run Info  \\[gnus-group-describe-briefly]:This help")))
4831
4832 (defun gnus-group-browse-foreign-server (method)
4833   "Browse a foreign news server.
4834 If called interactively, this function will ask for a select method
4835  (nntp, nnspool, etc.) and a server address (eg. nntp.some.where). 
4836 If not, METHOD should be a list where the first element is the method
4837 and the second element is the address."
4838   (interactive
4839    (list (let ((how (completing-read 
4840                      "Which backend: "
4841                      (append gnus-valid-select-methods gnus-server-alist)
4842                      nil t "nntp")))
4843            ;; We either got a backend name or a virtual server name.
4844            ;; If the first, we also need an address.
4845            (if (assoc how gnus-valid-select-methods)
4846                (list (intern how)
4847                      ;; Suggested by mapjph@bath.ac.uk.
4848                      (completing-read 
4849                       "Address: " 
4850                       (mapcar (lambda (server) (list server))
4851                               gnus-secondary-servers)))
4852              ;; We got a server name, so we find the method.
4853              (gnus-server-to-method how)))))
4854   (gnus-browse-foreign-server method))
4855
4856 \f
4857 ;;;
4858 ;;; Browse Server Mode
4859 ;;;
4860
4861 (defvar gnus-browse-mode-hook nil)
4862 (defvar gnus-browse-mode-map nil)
4863 (put 'gnus-browse-mode 'mode-class 'special)
4864
4865 (if gnus-browse-mode-map
4866     nil
4867   (setq gnus-browse-mode-map (make-keymap))
4868   (suppress-keymap gnus-browse-mode-map)
4869   (define-key gnus-browse-mode-map " " 'gnus-browse-read-group)
4870   (define-key gnus-browse-mode-map "=" 'gnus-browse-select-group)
4871   (define-key gnus-browse-mode-map "n" 'gnus-browse-next-group)
4872   (define-key gnus-browse-mode-map "p" 'gnus-browse-prev-group)
4873   (define-key gnus-browse-mode-map "\177" 'gnus-browse-prev-group)
4874   (define-key gnus-browse-mode-map "N" 'gnus-browse-next-group)
4875   (define-key gnus-browse-mode-map "P" 'gnus-browse-prev-group)
4876   (define-key gnus-browse-mode-map "\M-n" 'gnus-browse-next-group)
4877   (define-key gnus-browse-mode-map "\M-p" 'gnus-browse-prev-group)
4878   (define-key gnus-browse-mode-map "\r" 'gnus-browse-select-group)
4879   (define-key gnus-browse-mode-map "u" 'gnus-browse-unsubscribe-current-group)
4880   (define-key gnus-browse-mode-map "l" 'gnus-browse-exit)
4881   (define-key gnus-browse-mode-map "L" 'gnus-browse-exit)
4882   (define-key gnus-browse-mode-map "q" 'gnus-browse-exit)
4883   (define-key gnus-browse-mode-map "Q" 'gnus-browse-exit)
4884   (define-key gnus-browse-mode-map "\C-c\C-c" 'gnus-browse-exit)
4885   (define-key gnus-browse-mode-map "?" 'gnus-browse-describe-briefly)
4886   (define-key gnus-browse-mode-map "\C-c\C-i" 'gnus-info-find-node)
4887   )
4888
4889 (defvar gnus-browse-current-method nil)
4890 (defvar gnus-browse-return-buffer nil)
4891
4892 (defvar gnus-browse-buffer "*Gnus Browse Server*")
4893
4894 (defun gnus-browse-foreign-server (method &optional return-buffer)
4895   (setq gnus-browse-current-method method)
4896   (setq gnus-browse-return-buffer return-buffer)
4897   (let ((gnus-select-method method)
4898         groups group)
4899     (gnus-message 5 "Connecting to %s..." (nth 1 method))
4900     (or (gnus-server-opened method)
4901         (gnus-open-server method)
4902         (error "Unable to contact server: %s" (gnus-status-message method)))
4903     (or (gnus-request-list method)
4904         (error "Couldn't request list: %s" (gnus-status-message method)))
4905     (get-buffer-create gnus-browse-buffer)
4906     (gnus-add-current-to-buffer-list)
4907     (and gnus-carpal (gnus-carpal-setup-buffer 'browse))
4908     (gnus-configure-windows 'browse)
4909     (buffer-disable-undo (current-buffer))
4910     (let ((buffer-read-only nil))
4911       (erase-buffer))
4912     (gnus-browse-mode)
4913     (setq mode-line-buffer-identification
4914           (format
4915            "(ding) Browse Server {%s:%s}" (car method) (car (cdr method))))
4916     (save-excursion
4917       (set-buffer nntp-server-buffer)
4918       (let ((cur (current-buffer)))
4919         (goto-char (point-min))
4920         (or (string= gnus-ignored-newsgroups "")
4921             (delete-matching-lines gnus-ignored-newsgroups))
4922         (while (re-search-forward 
4923                 "\\(^[^ \t]+\\)[ \t]+[0-9]+[ \t]+[0-9]+" nil t)
4924           (goto-char (match-end 1))
4925           (setq groups (cons (cons (buffer-substring (match-beginning 1)
4926                                                      (match-end 1))
4927                                    (max 0 (- (1+ (read cur)) (read cur))))
4928                              groups)))))
4929     (setq groups (sort groups 
4930                        (lambda (l1 l2)
4931                          (string< (car l1) (car l2)))))
4932     (let ((buffer-read-only nil))
4933       (while groups
4934         (setq group (car groups))
4935         (insert 
4936          (format "K%7d: %s\n" (cdr group) (car group)))
4937         (setq groups (cdr groups))))
4938     (switch-to-buffer (current-buffer))
4939     (goto-char (point-min))
4940     (gnus-group-position-cursor)))
4941
4942 (defun gnus-browse-mode ()
4943   "Major mode for browsing a foreign server.
4944
4945 All normal editing commands are switched off.
4946
4947 \\<gnus-browse-mode-map>
4948 The only things you can do in this buffer is
4949
4950 1) `\\[gnus-browse-unsubscribe-current-group]' to subscribe to a group.
4951 The group will be inserted into the group buffer upon exit from this
4952 buffer.  
4953
4954 2) `\\[gnus-browse-read-group]' to read a group ephemerally.
4955
4956 3) `\\[gnus-browse-exit]' to return to the group buffer."
4957   (interactive)
4958   (kill-all-local-variables)
4959   (if gnus-visual (gnus-browse-make-menu-bar))
4960   (setq mode-line-modified "-- ")
4961   (make-local-variable 'mode-line-format)
4962   (setq mode-line-format (copy-sequence mode-line-format))
4963   (and (equal (nth 3 mode-line-format) "   ")
4964        (setcar (nthcdr 3 mode-line-format) ""))
4965   (setq major-mode 'gnus-browse-mode)
4966   (setq mode-name "Browse Server")
4967   (setq mode-line-process nil)
4968   (use-local-map gnus-browse-mode-map)
4969   (buffer-disable-undo (current-buffer))
4970   (setq truncate-lines t)
4971   (setq buffer-read-only t)
4972   (run-hooks 'gnus-browse-mode-hook))
4973
4974 (defun gnus-browse-read-group (&optional no-article)
4975   "Enter the group at the current line."
4976   (interactive)
4977   (let ((group (gnus-browse-group-name)))
4978     (or (gnus-group-read-ephemeral-group 
4979          group gnus-browse-current-method nil
4980          (cons (current-buffer) 'browse))
4981         (error "Couldn't enter %s" group))))
4982
4983 (defun gnus-browse-select-group ()
4984   "Select the current group."
4985   (interactive)
4986   (gnus-browse-read-group 'no))
4987
4988 (defun gnus-browse-next-group (n)
4989   "Go to the next group."
4990   (interactive "p")
4991   (prog1
4992       (forward-line n)
4993     (gnus-group-position-cursor)))
4994
4995 (defun gnus-browse-prev-group (n)
4996   "Go to the next group."
4997   (interactive "p")
4998   (gnus-browse-next-group (- n)))
4999
5000 (defun gnus-browse-unsubscribe-current-group (arg)
5001   "(Un)subscribe to the next ARG groups."
5002   (interactive "p")
5003   (and (eobp)
5004        (error "No group at current line."))
5005   (let ((ward (if (< arg 0) -1 1))
5006         (arg (abs arg)))
5007     (while (and (> arg 0)
5008                 (not (eobp))
5009                 (gnus-browse-unsubscribe-group)
5010                 (zerop (gnus-browse-next-group ward)))
5011       (setq arg (1- arg)))
5012     (gnus-group-position-cursor)
5013     (if (/= 0 arg) (gnus-message 7 "No more newsgroups"))
5014     arg))
5015
5016 (defun gnus-browse-group-name ()
5017   (save-excursion
5018     (beginning-of-line)
5019     (if (not (re-search-forward ": \\(.*\\)$" (gnus-point-at-eol) t))
5020         ()
5021       (gnus-group-prefixed-name 
5022        (buffer-substring (match-beginning 1) (match-end 1))
5023        gnus-browse-current-method))))
5024   
5025 (defun gnus-browse-unsubscribe-group ()
5026   (let ((sub nil)
5027         (buffer-read-only nil)
5028         group)
5029     (save-excursion
5030       (beginning-of-line)
5031       (if (= (following-char) ?K) (setq sub t))
5032       (setq group (gnus-browse-group-name))
5033       (beginning-of-line)
5034       (delete-char 1)
5035       (if sub
5036           (progn
5037             (gnus-group-change-level 
5038              (list t group gnus-level-default-subscribed
5039                    nil nil gnus-browse-current-method) 
5040              gnus-level-default-subscribed gnus-level-killed
5041              (and (car (nth 1 gnus-newsrc-alist))
5042                   (gnus-gethash (car (nth 1 gnus-newsrc-alist))
5043                                 gnus-newsrc-hashtb))
5044              t)
5045             (insert ? ))
5046         (gnus-group-change-level 
5047          group gnus-level-killed gnus-level-default-subscribed)
5048         (insert ?K)))
5049     t))
5050
5051 (defun gnus-browse-exit ()
5052   "Quit browsing and return to the group buffer."
5053   (interactive)
5054   (if (eq major-mode 'gnus-browse-mode)
5055       (kill-buffer (current-buffer)))
5056   (if gnus-browse-return-buffer
5057       (gnus-configure-windows 'server 'force)
5058     (gnus-configure-windows 'group 'force)
5059     (gnus-group-list-groups nil)))
5060
5061 (defun gnus-browse-describe-briefly ()
5062   "Give a one line description of the group mode commands."
5063   (interactive)
5064   (gnus-message 6
5065    (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")))
5066       
5067 \f
5068 ;;;
5069 ;;; Gnus summary mode
5070 ;;;
5071
5072 (defvar gnus-summary-mode-map nil)
5073 (defvar gnus-summary-mark-map nil)
5074 (defvar gnus-summary-mscore-map nil)
5075 (defvar gnus-summary-article-map nil)
5076 (defvar gnus-summary-thread-map nil)
5077 (defvar gnus-summary-goto-map nil)
5078 (defvar gnus-summary-exit-map nil)
5079 (defvar gnus-summary-various-map nil)
5080 (defvar gnus-summary-interest-map nil)
5081 (defvar gnus-summary-sort-map nil)
5082 (defvar gnus-summary-backend-map nil)
5083 (defvar gnus-summary-save-map nil)
5084 (defvar gnus-summary-wash-map nil)
5085 (defvar gnus-summary-help-map nil)
5086
5087 (put 'gnus-summary-mode 'mode-class 'special)
5088
5089 (if gnus-summary-mode-map
5090     nil
5091   (setq gnus-summary-mode-map (make-keymap))
5092   (suppress-keymap gnus-summary-mode-map)
5093
5094   ;; Non-orthogonal keys
5095
5096   (define-key gnus-summary-mode-map " " 'gnus-summary-next-page)
5097   (define-key gnus-summary-mode-map "\177" 'gnus-summary-prev-page)
5098   (define-key gnus-summary-mode-map "\r" 'gnus-summary-scroll-up)
5099   (define-key gnus-summary-mode-map "n" 'gnus-summary-next-unread-article)
5100   (define-key gnus-summary-mode-map "p" 'gnus-summary-prev-unread-article)
5101   (define-key gnus-summary-mode-map "N" 'gnus-summary-next-article)
5102   (define-key gnus-summary-mode-map "P" 'gnus-summary-prev-article)
5103   (define-key gnus-summary-mode-map "\M-\C-n" 'gnus-summary-next-same-subject)
5104   (define-key gnus-summary-mode-map "\M-\C-p" 'gnus-summary-prev-same-subject)
5105   (define-key gnus-summary-mode-map "\M-n" 'gnus-summary-next-unread-subject)
5106   (define-key gnus-summary-mode-map "\M-p" 'gnus-summary-prev-unread-subject)
5107   (define-key gnus-summary-mode-map "." 'gnus-summary-first-unread-article)
5108   (define-key gnus-summary-mode-map "," 'gnus-summary-best-unread-article)
5109   (define-key gnus-summary-mode-map "\M-s" 'gnus-summary-search-article-forward)
5110   (define-key gnus-summary-mode-map "\M-r" 'gnus-summary-search-article-backward)
5111   (define-key gnus-summary-mode-map "<" 'gnus-summary-beginning-of-article)
5112   (define-key gnus-summary-mode-map ">" 'gnus-summary-end-of-article)
5113   (define-key gnus-summary-mode-map "j" 'gnus-summary-goto-subject)
5114   (define-key gnus-summary-mode-map "^" 'gnus-summary-refer-parent-article)
5115   (define-key gnus-summary-mode-map "\M-^" 'gnus-summary-refer-article)
5116   (define-key gnus-summary-mode-map "u" 'gnus-summary-tick-article-forward)
5117   (define-key gnus-summary-mode-map "!" 'gnus-summary-tick-article-forward)
5118   (define-key gnus-summary-mode-map "U" 'gnus-summary-tick-article-backward)
5119   (define-key gnus-summary-mode-map "d" 'gnus-summary-mark-as-read-forward)
5120   (define-key gnus-summary-mode-map "D" 'gnus-summary-mark-as-read-backward)
5121   (define-key gnus-summary-mode-map "E" 'gnus-summary-mark-as-expirable)
5122   (define-key gnus-summary-mode-map "\M-u" 'gnus-summary-clear-mark-forward)
5123   (define-key gnus-summary-mode-map "\M-U" 'gnus-summary-clear-mark-backward)
5124   (define-key gnus-summary-mode-map "k" 'gnus-summary-kill-same-subject-and-select)
5125   (define-key gnus-summary-mode-map "\C-k" 'gnus-summary-kill-same-subject)
5126   (define-key gnus-summary-mode-map "\M-\C-k" 'gnus-summary-kill-thread)
5127   (define-key gnus-summary-mode-map "\M-\C-l" 'gnus-summary-lower-thread)
5128   (define-key gnus-summary-mode-map "e" 'gnus-summary-edit-article)
5129   (define-key gnus-summary-mode-map "#" 'gnus-summary-mark-as-processable)
5130   (define-key gnus-summary-mode-map "\M-#" 'gnus-summary-unmark-as-processable)
5131   (define-key gnus-summary-mode-map "\M-\C-t" 'gnus-summary-toggle-threads)
5132   (define-key gnus-summary-mode-map "\M-\C-s" 'gnus-summary-show-thread)
5133   (define-key gnus-summary-mode-map "\M-\C-h" 'gnus-summary-hide-thread)
5134   (define-key gnus-summary-mode-map "\M-\C-f" 'gnus-summary-next-thread)
5135   (define-key gnus-summary-mode-map "\M-\C-b" 'gnus-summary-prev-thread)
5136   (define-key gnus-summary-mode-map "\M-\C-u" 'gnus-summary-up-thread)
5137   (define-key gnus-summary-mode-map "\M-\C-d" 'gnus-summary-down-thread)
5138   (define-key gnus-summary-mode-map "&" 'gnus-summary-execute-command)
5139   (define-key gnus-summary-mode-map "c" 'gnus-summary-catchup-and-exit)
5140   (define-key gnus-summary-mode-map "\C-w" 'gnus-summary-mark-region-as-read)
5141   (define-key gnus-summary-mode-map "\C-t" 'gnus-summary-toggle-truncation)
5142   (define-key gnus-summary-mode-map "?" 'gnus-summary-mark-as-dormant)
5143   (define-key gnus-summary-mode-map "\C-c\M-\C-s" 'gnus-summary-show-all-expunged)
5144   (define-key gnus-summary-mode-map "\C-c\C-s\C-n" 'gnus-summary-sort-by-number)
5145   (define-key gnus-summary-mode-map "\C-c\C-s\C-a" 'gnus-summary-sort-by-author)
5146   (define-key gnus-summary-mode-map "\C-c\C-s\C-s" 'gnus-summary-sort-by-subject)
5147   (define-key gnus-summary-mode-map "\C-c\C-s\C-d" 'gnus-summary-sort-by-date)
5148   (define-key gnus-summary-mode-map "\C-c\C-s\C-i" 'gnus-summary-sort-by-score)
5149   (define-key gnus-summary-mode-map "=" 'gnus-summary-expand-window)
5150   (define-key gnus-summary-mode-map "\C-x\C-s" 'gnus-summary-reselect-current-group)
5151   (define-key gnus-summary-mode-map "\M-g" 'gnus-summary-rescan-group)
5152   (define-key gnus-summary-mode-map "w" 'gnus-summary-stop-page-breaking)
5153   (define-key gnus-summary-mode-map "\C-c\C-r" 'gnus-summary-caesar-message)
5154   (define-key gnus-summary-mode-map "\M-t" 'gnus-summary-toggle-mime)
5155   (define-key gnus-summary-mode-map "f" 'gnus-summary-followup)
5156   (define-key gnus-summary-mode-map "F" 'gnus-summary-followup-with-original)
5157   (define-key gnus-summary-mode-map "C" 'gnus-summary-cancel-article)
5158   (define-key gnus-summary-mode-map "r" 'gnus-summary-reply)
5159   (define-key gnus-summary-mode-map "R" 'gnus-summary-reply-with-original)
5160   (define-key gnus-summary-mode-map "\C-c\C-f" 'gnus-summary-mail-forward)
5161   (define-key gnus-summary-mode-map "o" 'gnus-summary-save-article)
5162   (define-key gnus-summary-mode-map "\C-o" 'gnus-summary-save-article-mail)
5163   (define-key gnus-summary-mode-map "|" 'gnus-summary-pipe-output)
5164   (define-key gnus-summary-mode-map "\M-k" 'gnus-summary-edit-local-kill)
5165   (define-key gnus-summary-mode-map "\M-K" 'gnus-summary-edit-global-kill)
5166   (define-key gnus-summary-mode-map "V" 'gnus-version)
5167   (define-key gnus-summary-mode-map "\C-c\C-d" 'gnus-summary-describe-group)
5168   (define-key gnus-summary-mode-map "q" 'gnus-summary-exit)
5169   (define-key gnus-summary-mode-map "Q" 'gnus-summary-exit-no-update)
5170   (define-key gnus-summary-mode-map "\C-c\C-i" 'gnus-info-find-node)
5171   (define-key gnus-summary-mode-map gnus-mouse-2 'gnus-mouse-pick-article)
5172   (define-key gnus-summary-mode-map "m" 'gnus-summary-mail-other-window)
5173   (define-key gnus-summary-mode-map "a" 'gnus-summary-post-news)
5174   (define-key gnus-summary-mode-map "x" 'gnus-summary-remove-lines-marked-as-read)
5175 ; (define-key gnus-summary-mode-map "X" 'gnus-summary-remove-lines-marked-with)
5176   (define-key gnus-summary-mode-map "s" 'gnus-summary-isearch-article)
5177   (define-key gnus-summary-mode-map "t" 'gnus-summary-toggle-header)
5178   (define-key gnus-summary-mode-map "g" 'gnus-summary-show-article)
5179 ;  (define-key gnus-summary-mode-map "?" 'gnus-summary-describe-briefly)
5180   (define-key gnus-summary-mode-map "l" 'gnus-summary-goto-last-article)
5181   (define-key gnus-summary-mode-map "\C-c\C-v\C-v" 'gnus-uu-decode-uu-view)
5182   (define-key gnus-summary-mode-map "\C-d" 'gnus-summary-enter-digest-group)
5183   (define-key gnus-summary-mode-map "v" 'gnus-summary-verbose-headers)
5184   (define-key gnus-summary-mode-map "\C-c\C-b" 'gnus-bug)
5185
5186
5187   ;; Sort of orthogonal keymap
5188   (define-prefix-command 'gnus-summary-mark-map)
5189   (define-key gnus-summary-mode-map "M" 'gnus-summary-mark-map)
5190   (define-key gnus-summary-mark-map "t" 'gnus-summary-tick-article-forward)
5191   (define-key gnus-summary-mark-map "!" 'gnus-summary-tick-article-forward)
5192   (define-key gnus-summary-mark-map "d" 'gnus-summary-mark-as-read-forward)
5193   (define-key gnus-summary-mark-map "r" 'gnus-summary-mark-as-read-forward)
5194   (define-key gnus-summary-mark-map "c" 'gnus-summary-clear-mark-forward)
5195   (define-key gnus-summary-mark-map " " 'gnus-summary-clear-mark-forward)
5196   (define-key gnus-summary-mark-map "e" 'gnus-summary-mark-as-expirable)
5197   (define-key gnus-summary-mark-map "x" 'gnus-summary-mark-as-expirable)
5198   (define-key gnus-summary-mark-map "?" 'gnus-summary-mark-as-dormant)
5199   (define-key gnus-summary-mark-map "b" 'gnus-summary-set-bookmark)
5200   (define-key gnus-summary-mark-map "B" 'gnus-summary-remove-bookmark)
5201   (define-key gnus-summary-mark-map "#" 'gnus-summary-mark-as-processable)
5202   (define-key gnus-summary-mark-map "\M-#" 'gnus-summary-unmark-as-processable)
5203   (define-key gnus-summary-mark-map "\M-r" 'gnus-summary-remove-lines-marked-as-read)
5204   (define-key gnus-summary-mark-map "\M-\C-r" 'gnus-summary-remove-lines-marked-with)
5205   (define-key gnus-summary-mark-map "D" 'gnus-summary-show-all-dormant)
5206   (define-key gnus-summary-mark-map "\M-D" 'gnus-summary-hide-all-dormant)
5207   (define-key gnus-summary-mark-map "S" 'gnus-summary-show-all-expunged)
5208   (define-key gnus-summary-mark-map "C" 'gnus-summary-catchup)
5209   (define-key gnus-summary-mark-map "H" 'gnus-summary-catchup-to-here)
5210   (define-key gnus-summary-mark-map "\C-c" 'gnus-summary-catchup-all)
5211   (define-key gnus-summary-mark-map "k" 'gnus-summary-kill-same-subject-and-select)
5212   (define-key gnus-summary-mark-map "K" 'gnus-summary-kill-same-subject)
5213
5214   (define-prefix-command 'gnus-summary-mscore-map)
5215   (define-key gnus-summary-mark-map "s" 'gnus-summary-mscore-map)
5216   (define-key gnus-summary-mscore-map "c" 'gnus-summary-clear-above)
5217   (define-key gnus-summary-mscore-map "u" 'gnus-summary-tick-above)
5218   (define-key gnus-summary-mscore-map "m" 'gnus-summary-mark-above)
5219   (define-key gnus-summary-mscore-map "k" 'gnus-summary-kill-below)
5220
5221   (define-key gnus-summary-mark-map "p" 'gnus-uu-mark-map)
5222   
5223   (define-key gnus-summary-mode-map "S" 'gnus-summary-send-map)
5224   
5225   (define-prefix-command 'gnus-summary-goto-map)
5226   (define-key gnus-summary-mode-map "G" 'gnus-summary-goto-map)
5227   (define-key gnus-summary-goto-map "n" 'gnus-summary-next-unread-article)
5228   (define-key gnus-summary-goto-map "p" 'gnus-summary-prev-unread-article)
5229   (define-key gnus-summary-goto-map "N" 'gnus-summary-next-article)
5230   (define-key gnus-summary-goto-map "P" 'gnus-summary-prev-article)
5231   (define-key gnus-summary-goto-map "\C-n" 'gnus-summary-next-same-subject)
5232   (define-key gnus-summary-goto-map "\C-p" 'gnus-summary-prev-same-subject)
5233   (define-key gnus-summary-goto-map "\M-n" 'gnus-summary-next-unread-subject)
5234   (define-key gnus-summary-goto-map "\M-p" 'gnus-summary-prev-unread-subject)
5235   (define-key gnus-summary-goto-map "f" 'gnus-summary-first-unread-article)
5236   (define-key gnus-summary-goto-map "b" 'gnus-summary-best-unread-article)
5237   (define-key gnus-summary-goto-map "g" 'gnus-summary-goto-subject)
5238   (define-key gnus-summary-goto-map "l" 'gnus-summary-goto-last-article)
5239   (define-key gnus-summary-goto-map "p" 'gnus-summary-pop-article)
5240
5241
5242   (define-prefix-command 'gnus-summary-thread-map)
5243   (define-key gnus-summary-mode-map "T" 'gnus-summary-thread-map)
5244   (define-key gnus-summary-thread-map "k" 'gnus-summary-kill-thread)
5245   (define-key gnus-summary-thread-map "l" 'gnus-summary-lower-thread)
5246   (define-key gnus-summary-thread-map "i" 'gnus-summary-raise-thread)
5247   (define-key gnus-summary-thread-map "T" 'gnus-summary-toggle-threads)
5248   (define-key gnus-summary-thread-map "s" 'gnus-summary-show-thread)
5249   (define-key gnus-summary-thread-map "S" 'gnus-summary-show-all-threads)
5250   (define-key gnus-summary-thread-map "h" 'gnus-summary-hide-thread)
5251   (define-key gnus-summary-thread-map "H" 'gnus-summary-hide-all-threads)
5252   (define-key gnus-summary-thread-map "n" 'gnus-summary-next-thread)
5253   (define-key gnus-summary-thread-map "p" 'gnus-summary-prev-thread)
5254   (define-key gnus-summary-thread-map "u" 'gnus-summary-up-thread)
5255   (define-key gnus-summary-thread-map "d" 'gnus-summary-down-thread)
5256   (define-key gnus-summary-thread-map "#" 'gnus-uu-mark-thread)
5257
5258   
5259   (define-prefix-command 'gnus-summary-exit-map)
5260   (define-key gnus-summary-mode-map "Z" 'gnus-summary-exit-map)
5261   (define-key gnus-summary-exit-map "c" 'gnus-summary-catchup-and-exit)
5262   (define-key gnus-summary-exit-map "C" 'gnus-summary-catchup-all-and-exit)
5263   (define-key gnus-summary-exit-map "E" 'gnus-summary-exit-no-update)
5264   (define-key gnus-summary-exit-map "Q" 'gnus-summary-exit)
5265   (define-key gnus-summary-exit-map "Z" 'gnus-summary-exit)
5266   (define-key gnus-summary-exit-map "n" 'gnus-summary-catchup-and-goto-next-group)
5267   (define-key gnus-summary-exit-map "R" 'gnus-summary-reselect-current-group)
5268   (define-key gnus-summary-exit-map "G" 'gnus-summary-rescan-group)
5269   (define-key gnus-summary-exit-map "N" 'gnus-summary-next-group)
5270   (define-key gnus-summary-exit-map "P" 'gnus-summary-prev-group)
5271
5272
5273   (define-prefix-command 'gnus-summary-article-map)
5274   (define-key gnus-summary-mode-map "A" 'gnus-summary-article-map)
5275   (define-key gnus-summary-article-map " " 'gnus-summary-next-page)
5276   (define-key gnus-summary-article-map "n" 'gnus-summary-next-page)
5277   (define-key gnus-summary-article-map "\177" 'gnus-summary-prev-page)
5278   (define-key gnus-summary-article-map "p" 'gnus-summary-prev-page)
5279   (define-key gnus-summary-article-map "\r" 'gnus-summary-scroll-up)
5280   (define-key gnus-summary-article-map "<" 'gnus-summary-beginning-of-article)
5281   (define-key gnus-summary-article-map ">" 'gnus-summary-end-of-article)
5282   (define-key gnus-summary-article-map "b" 'gnus-summary-beginning-of-article)
5283   (define-key gnus-summary-article-map "e" 'gnus-summary-end-of-article)
5284   (define-key gnus-summary-article-map "^" 'gnus-summary-refer-parent-article)
5285   (define-key gnus-summary-article-map "r" 'gnus-summary-refer-parent-article)
5286   (define-key gnus-summary-article-map "w" 'gnus-summary-stop-page-breaking)
5287   (define-key gnus-summary-article-map "c" 'gnus-summary-caesar-message)
5288   (define-key gnus-summary-article-map "g" 'gnus-summary-show-article)
5289   (define-key gnus-summary-article-map "t" 'gnus-summary-toggle-header)
5290   (define-key gnus-summary-article-map "m" 'gnus-summary-toggle-mime)
5291   (define-key gnus-summary-article-map "s" 'gnus-summary-isearch-article)
5292
5293
5294   (define-prefix-command 'gnus-summary-wash-map)
5295   (define-key gnus-summary-mode-map "W" 'gnus-summary-wash-map)
5296   (define-key gnus-summary-wash-map "h" 'gnus-article-hide-headers)
5297   (define-key gnus-summary-wash-map "s" 'gnus-article-hide-signature)
5298   (define-key gnus-summary-wash-map "c" 'gnus-article-hide-citation)
5299   (define-key gnus-summary-wash-map "\C-c" 'gnus-article-hide-citation-maybe)
5300   (define-key gnus-summary-wash-map "o" 'gnus-article-treat-overstrike)
5301   (define-key gnus-summary-wash-map "w" 'gnus-article-word-wrap)
5302   (define-key gnus-summary-wash-map "m" 'gnus-article-remove-cr)
5303   (define-key gnus-summary-wash-map "q" 'gnus-article-de-quoted-unreadable)
5304   (define-key gnus-summary-wash-map "f" 'gnus-article-display-x-face)
5305   (define-key gnus-summary-wash-map "t" 'gnus-article-date-ut)
5306   (define-key gnus-summary-wash-map "\C-t" 'gnus-article-date-local)
5307   (define-key gnus-summary-wash-map "T" 'gnus-article-date-lapsed)
5308
5309   (define-key gnus-summary-wash-map "A" 'gnus-article-highlight)
5310   (define-key gnus-summary-wash-map "a" 'gnus-article-hide)
5311   (define-key gnus-summary-wash-map "H" 'gnus-article-highlight-headers)
5312   (define-key gnus-summary-wash-map "C" 'gnus-article-highlight-citation)
5313   (define-key gnus-summary-wash-map "S" 'gnus-article-highlight-signature)
5314   (define-key gnus-summary-wash-map "b" 'gnus-article-add-buttons)
5315
5316
5317   (define-prefix-command 'gnus-summary-help-map)
5318   (define-key gnus-summary-mode-map "H" 'gnus-summary-help-map)
5319   (define-key gnus-summary-help-map "v" 'gnus-version)
5320   (define-key gnus-summary-help-map "f" 'gnus-summary-fetch-faq)
5321   (define-key gnus-summary-help-map "d" 'gnus-summary-describe-group)
5322   (define-key gnus-summary-help-map "h" 'gnus-summary-describe-briefly)
5323   (define-key gnus-summary-help-map "i" 'gnus-info-find-node)
5324
5325
5326   (define-prefix-command 'gnus-summary-backend-map)
5327   (define-key gnus-summary-mode-map "B" 'gnus-summary-backend-map)
5328   (define-key gnus-summary-backend-map "e" 'gnus-summary-expire-articles)
5329   (define-key gnus-summary-backend-map "\M-\C-e" 
5330     'gnus-summary-expire-articles-now)
5331   (define-key gnus-summary-backend-map "\177" 'gnus-summary-delete-article)
5332   (define-key gnus-summary-backend-map "m" 'gnus-summary-move-article)
5333   (define-key gnus-summary-backend-map "r" 'gnus-summary-respool-article)
5334   (define-key gnus-summary-backend-map "w" 'gnus-summary-edit-article)
5335   (define-key gnus-summary-backend-map "c" 'gnus-summary-copy-article)
5336   (define-key gnus-summary-backend-map "q" 'gnus-summary-fancy-query)
5337   (define-key gnus-summary-backend-map "i" 'gnus-summary-import-article)
5338
5339
5340   (define-prefix-command 'gnus-summary-save-map)
5341   (define-key gnus-summary-mode-map "O" 'gnus-summary-save-map)
5342   (define-key gnus-summary-save-map "o" 'gnus-summary-save-article)
5343   (define-key gnus-summary-save-map "m" 'gnus-summary-save-article-mail)
5344   (define-key gnus-summary-save-map "r" 'gnus-summary-save-article-rmail)
5345   (define-key gnus-summary-save-map "f" 'gnus-summary-save-article-file)
5346   (define-key gnus-summary-save-map "h" 'gnus-summary-save-article-folder)
5347   (define-key gnus-summary-save-map "v" 'gnus-summary-save-article-vm)
5348   (define-key gnus-summary-save-map "p" 'gnus-summary-pipe-output)
5349 ;  (define-key gnus-summary-save-map "s" 'gnus-soup-add-article)
5350
5351   (define-key gnus-summary-mode-map "X" 'gnus-uu-extract-map)
5352   
5353   (define-prefix-command 'gnus-summary-various-map)
5354   (define-key gnus-summary-mode-map "V" 'gnus-summary-various-map)
5355   (define-key gnus-summary-various-map "u" 'gnus-summary-universal-argument)
5356   (define-key gnus-summary-various-map "\C-s" 'gnus-summary-search-article-forward)
5357   (define-key gnus-summary-various-map "\C-r" 'gnus-summary-search-article-backward)
5358   (define-key gnus-summary-various-map "r" 'gnus-summary-refer-article)
5359   (define-key gnus-summary-various-map "&" 'gnus-summary-execute-command)
5360   (define-key gnus-summary-various-map "T" 'gnus-summary-toggle-truncation)
5361   (define-key gnus-summary-various-map "e" 'gnus-summary-expand-window)
5362   (define-key gnus-summary-various-map "D" 'gnus-summary-enter-digest-group)
5363   (define-key gnus-summary-various-map "k" 'gnus-summary-edit-local-kill)
5364   (define-key gnus-summary-various-map "K" 'gnus-summary-edit-global-kill)
5365
5366   (define-key gnus-summary-various-map "S" 'gnus-summary-score-map)
5367
5368   (define-prefix-command 'gnus-summary-sort-map)
5369   (define-key gnus-summary-various-map "s" 'gnus-summary-sort-map)
5370   (define-key gnus-summary-sort-map "n" 'gnus-summary-sort-by-number)
5371   (define-key gnus-summary-sort-map "a" 'gnus-summary-sort-by-author)
5372   (define-key gnus-summary-sort-map "s" 'gnus-summary-sort-by-subject)
5373   (define-key gnus-summary-sort-map "d" 'gnus-summary-sort-by-date)
5374   (define-key gnus-summary-sort-map "i" 'gnus-summary-sort-by-score)
5375
5376   (define-key gnus-summary-mode-map "I" 'gnus-summary-increase-score)
5377   (define-key gnus-summary-mode-map "L" 'gnus-summary-lower-score)
5378   )
5379
5380
5381 \f
5382
5383 (defun gnus-summary-mode (&optional group)
5384   "Major mode for reading articles.
5385
5386 All normal editing commands are switched off.
5387 \\<gnus-summary-mode-map>
5388 Each line in this buffer represents one article.  To read an
5389 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
5390 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]', 
5391 respectively.
5392
5393 You can also post articles and send mail from this buffer.  To 
5394 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author 
5395 of an article, type `\\[gnus-summary-reply]'.
5396
5397 There are approx. one gazillion commands you can execute in this 
5398 buffer; read the info pages for more information (`\\[gnus-info-find-node]'). 
5399
5400 The following commands are available:
5401
5402 \\{gnus-summary-mode-map}"
5403   (interactive)
5404   (if gnus-visual (gnus-summary-make-menu-bar))
5405   (kill-all-local-variables)
5406   (let ((locals gnus-summary-local-variables))
5407     (while locals
5408       (if (consp (car locals))
5409           (progn
5410             (make-local-variable (car (car locals)))
5411             (set (car (car locals)) (eval (cdr (car locals)))))
5412         (make-local-variable (car locals))
5413         (set (car locals) nil))
5414       (setq locals (cdr locals))))
5415   (gnus-make-thread-indent-array)
5416   (setq mode-line-modified "-- ")
5417   (make-local-variable 'mode-line-format)
5418   (setq mode-line-format (copy-sequence mode-line-format))
5419   (and (equal (nth 3 mode-line-format) "   ")
5420        (setcar (nthcdr 3 mode-line-format) ""))
5421   (setq major-mode 'gnus-summary-mode)
5422   (setq mode-name "Summary")
5423   (make-local-variable 'minor-mode-alist)
5424   (use-local-map gnus-summary-mode-map)
5425   (buffer-disable-undo (current-buffer))
5426   (setq buffer-read-only t)             ;Disable modification
5427   (setq truncate-lines t)
5428   (setq selective-display t)
5429   (setq selective-display-ellipses t)   ;Display `...'
5430   (setq buffer-display-table gnus-summary-display-table)
5431   (setq gnus-newsgroup-name group)
5432   (run-hooks 'gnus-summary-mode-hook))
5433
5434 (defun gnus-summary-make-display-table ()
5435   ;; Change the display table.  Odd characters have a tendency to mess
5436   ;; up nicely formatted displays - we make all possible glyphs
5437   ;; display only a single character.
5438
5439   ;; We start from the standard display table, if any.
5440   (setq gnus-summary-display-table 
5441         (or (copy-sequence standard-display-table)
5442             (make-display-table)))
5443   ;; Nix out all the control chars...
5444   (let ((i 32))
5445     (while (>= (setq i (1- i)) 0)
5446       (aset gnus-summary-display-table i [??])))
5447   ;; ... but not newline and cr, of course. (cr is necessary for the
5448   ;; selective display).  
5449   (aset gnus-summary-display-table ?\n nil)
5450   (aset gnus-summary-display-table ?\r nil)
5451   ;; We nix out any glyphs over 126 that are not set already.  
5452   (let ((i 256))
5453     (while (>= (setq i (1- i)) 127)
5454       ;; Only modify if the entry is nil.
5455       (or (aref gnus-summary-display-table i) 
5456           (aset gnus-summary-display-table i [??])))))
5457
5458 (defun gnus-summary-clear-local-variables ()
5459   (let ((locals gnus-summary-local-variables))
5460     (while locals
5461       (if (consp (car locals))
5462           (and (vectorp (car (car locals)))
5463                (set (car (car locals)) nil))
5464         (and (vectorp (car locals))
5465              (set (car locals) nil)))
5466       (setq locals (cdr locals)))))
5467
5468 ;; Some summary mode macros.
5469
5470 ;; Return a header specified by a NUMBER.
5471 (defun gnus-get-header-by-number (number)
5472   (save-excursion
5473     (set-buffer gnus-summary-buffer)
5474     (or gnus-newsgroup-headers-hashtb-by-number
5475         (gnus-make-headers-hashtable-by-number))
5476     (gnus-gethash (int-to-string number)
5477                   gnus-newsgroup-headers-hashtb-by-number)))
5478
5479 ;; Fast version of the function above.
5480 (defmacro gnus-get-header-by-num (number)
5481   (` (gnus-gethash (int-to-string (, number)) 
5482                    gnus-newsgroup-headers-hashtb-by-number)))
5483
5484 (defmacro gnus-summary-search-forward (&optional unread subject backward)
5485   "Search for article forward.
5486 If UNREAD is non-nil, only unread articles are selected.
5487 If SUBJECT is non-nil, the article which has the same subject will be
5488 searched for. 
5489 If BACKWARD is non-nil, the search will be performed backwards instead."
5490   (` (gnus-summary-search-subject (, backward) (, unread) (, subject))))
5491
5492 (defmacro gnus-summary-search-backward (&optional unread subject)
5493   "Search for article backward.
5494 If 1st optional argument UNREAD is non-nil, only unread article is selected.
5495 If 2nd optional argument SUBJECT is non-nil, the article which has
5496 the same subject will be searched for."
5497   (` (gnus-summary-search-forward (, unread) (, subject) t)))
5498
5499 (defmacro gnus-summary-article-number (&optional number-or-nil)
5500   "The article number of the article on the current line.
5501 If there isn's an article number here, then we return the current
5502 article number."
5503   (if number-or-nil
5504       '(get-text-property (gnus-point-at-bol) 'gnus-number)
5505     '(or (get-text-property (gnus-point-at-bol) 'gnus-number) 
5506          gnus-current-article)))
5507
5508 (defmacro gnus-summary-thread-level ()
5509   "The thread level of the article on the current line."
5510   '(or (get-text-property (gnus-point-at-bol) 'gnus-level)
5511        0))
5512
5513 (defmacro gnus-summary-article-mark ()
5514   "The mark on the current line."
5515   '(get-text-property (gnus-point-at-bol) 'gnus-mark))
5516
5517 (defun gnus-summary-subject-string ()
5518   "Return current subject string or nil if nothing."
5519   (let ((article (gnus-summary-article-number))
5520         header)
5521     (and article 
5522          (setq header (gnus-get-header-by-num article))
5523          (vectorp header)
5524          (header-subject header))))
5525
5526
5527 (defun gnus-mouse-pick-article (e)
5528   (interactive "e")
5529   (mouse-set-point e)
5530   (gnus-summary-next-page nil t))
5531
5532 (defun gnus-summary-setup-buffer (group)
5533   "Initialize summary buffer."
5534   (let ((buffer (concat "*Summary " group "*")))
5535     (if (get-buffer buffer)
5536         (progn
5537           (set-buffer buffer)
5538           (not gnus-newsgroup-begin))
5539       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
5540       (setq gnus-summary-buffer (set-buffer (get-buffer-create buffer)))
5541       (gnus-add-current-to-buffer-list)
5542       (gnus-summary-mode group)
5543       (and gnus-carpal (gnus-carpal-setup-buffer 'summary))
5544       (setq gnus-newsgroup-name group)
5545       t)))
5546
5547 (defun gnus-set-global-variables ()
5548   ;; Set the global equivalents of the summary buffer-local variables
5549   ;; to the latest values they had. These reflect the summary buffer
5550   ;; that was in action when the last article was fetched.
5551   (if (eq major-mode 'gnus-summary-mode) 
5552       (progn
5553         (setq gnus-summary-buffer (current-buffer))
5554         (let ((name gnus-newsgroup-name)
5555               (marked gnus-newsgroup-marked)
5556               (unread gnus-newsgroup-unreads)
5557               (headers gnus-current-headers)
5558               (score-file gnus-current-score-file))
5559           (save-excursion
5560             (set-buffer gnus-group-buffer)
5561             (setq gnus-newsgroup-name name)
5562             (setq gnus-newsgroup-marked marked)
5563             (setq gnus-newsgroup-unreads unread)
5564             (setq gnus-current-headers headers)
5565             (setq gnus-current-score-file score-file))))))
5566
5567 (defun gnus-summary-insert-dummy-line (sformat subject number)
5568   (if (not sformat) 
5569       (setq sformat gnus-summary-dummy-line-format-spec))
5570   (let (b)
5571     (beginning-of-line)
5572     (setq b (point))
5573     (insert (eval sformat))
5574     (add-text-properties
5575      b (1+ b)
5576      (list 'gnus-number number 
5577            'gnus-mark gnus-dummy-mark
5578            'gnus-level 0))))
5579
5580 (defvar gnus-thread-indent-array nil)
5581 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
5582 (defun gnus-make-thread-indent-array ()
5583   (let ((n 200))
5584     (if (and gnus-thread-indent-array
5585              (= gnus-thread-indent-level gnus-thread-indent-array-level))
5586         nil
5587       (setq gnus-thread-indent-array (make-vector 201 "")
5588             gnus-thread-indent-array-level gnus-thread-indent-level)
5589       (while (>= n 0)
5590         (aset gnus-thread-indent-array n
5591               (make-string (* n gnus-thread-indent-level) ? ))
5592         (setq n (1- n))))))
5593
5594 (defun gnus-summary-insert-line 
5595   (sformat header level current unread replied expirable subject-or-nil
5596            &optional dummy score)
5597   (or sformat (setq sformat gnus-summary-line-format-spec))
5598   (let* ((indentation (aref gnus-thread-indent-array level))
5599          (lines (header-lines header))
5600          (score (or score gnus-summary-default-score 0))
5601          (score-char
5602           (if (or (null gnus-summary-default-score)
5603                   (<= (abs (- score gnus-summary-default-score))
5604                       gnus-summary-zcore-fuzz)) ? 
5605             (if (< score gnus-summary-default-score)
5606                 gnus-score-below-mark gnus-score-over-mark)))
5607          (replied (if replied gnus-replied-mark ? ))
5608          (from (header-from header))
5609          (name (cond 
5610                 ((string-match "(.+)" from)
5611                  (substring from (1+ (match-beginning 0)) (1- (match-end 0))))
5612                 ((string-match "<[^>]+> *$" from)
5613                  (let ((beg (match-beginning 0)))
5614                    (or (and (string-match "^\"[^\"]*\"" from)
5615                             (substring from (1+ (match-beginning 0))
5616                                        (1- (match-end 0))))
5617                        (substring from 0 beg))))
5618                 (t from)))
5619          (subject (header-subject header))
5620          (number (header-number header))
5621          (opening-bracket (if dummy ?\< ?\[))
5622          (closing-bracket (if dummy ?\> ?\]))
5623          (buffer-read-only nil)
5624          (b (progn (beginning-of-line) (point))))
5625     (or (numberp lines) (setq lines 0))
5626     (insert (eval sformat))
5627     (add-text-properties
5628      b (1+ b) (list 'gnus-number number 
5629                     'gnus-mark (or unread gnus-unread-mark)
5630                     'gnus-level level))))
5631
5632 (defun gnus-summary-update-line (&optional dont-update)
5633   ;; Update summary line after change.
5634   (or (not gnus-summary-default-score)
5635       gnus-summary-inhibit-highlight
5636       (let ((gnus-summary-inhibit-highlight t)
5637             (article (gnus-summary-article-number)))
5638         (progn
5639           (or dont-update
5640               (if (and gnus-summary-mark-below
5641                        (< (gnus-summary-article-score)
5642                           gnus-summary-mark-below))
5643                   (and (not (memq article gnus-newsgroup-marked))
5644                        (not (memq article gnus-newsgroup-dormant))
5645                        (memq article gnus-newsgroup-unreads)
5646                        (gnus-summary-mark-article-as-read gnus-low-score-mark))
5647                 (and (eq (gnus-summary-article-mark) gnus-low-score-mark)
5648                      (gnus-summary-mark-article-as-unread gnus-unread-mark))))
5649           (and gnus-visual
5650                (run-hooks 'gnus-summary-update-hook))))))
5651
5652 (defun gnus-summary-update-lines (&optional beg end)
5653   ;; Mark article as read (or not) by taking into account scores.
5654   (let ((beg (or beg (point-min)))
5655         (end (or end (point-max))))
5656     (if (or (not gnus-summary-default-score)
5657             gnus-summary-inhibit-highlight)
5658         ()
5659       (let ((gnus-summary-inhibit-highlight t)
5660             article)
5661         (save-excursion
5662           (set-buffer gnus-summary-buffer)
5663           (goto-char beg)
5664           (beginning-of-line)
5665           (while (and (not (eobp)) (< (point) end))
5666             (if (and gnus-summary-mark-below
5667                      (< (or (cdr (assq 
5668                                   (setq article (get-text-property 
5669                                                  (point) 'gnus-number))
5670                                   gnus-newsgroup-scored))
5671                             gnus-summary-default-score 0)
5672                         gnus-summary-mark-below))
5673                 ;; We want to possibly mark it as read...
5674                 (and (not (memq article gnus-newsgroup-marked))
5675                      (not (memq article gnus-newsgroup-dormant))
5676                      (memq article gnus-newsgroup-unreads)
5677                      (gnus-summary-mark-article-as-read gnus-low-score-mark))
5678               ;; We want to possibly mark it as unread.
5679               (and (eq (get-text-property (point) 'gnus-mark)
5680                        gnus-low-score-mark)
5681                    (gnus-summary-mark-article-as-unread gnus-unread-mark)))
5682             ;; Do the visual highlights at the same time.
5683             (and gnus-visual (run-hooks 'gnus-summary-update-hook))
5684             (forward-line 1)))))))
5685
5686 (defvar gnus-tmp-gathered nil)
5687
5688 (defun gnus-summary-number-of-articles-in-thread (thread &optional char)
5689   ;; Sum up all elements (and sub-elements) in a list.
5690   (let* ((number
5691           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
5692           (if (and (consp thread) (cdr thread))
5693               (apply
5694                '+ 1 (mapcar
5695                      (function
5696                       (lambda (arg)
5697                         (if (memq (header-number (car arg)) gnus-tmp-gathered)
5698                             0
5699                           (gnus-summary-number-of-articles-in-thread arg))))
5700                      (cdr thread)))
5701             1)))
5702     (if char 
5703         (if (> number 1) gnus-not-empty-thread-mark
5704           gnus-empty-thread-mark)
5705       number)))
5706
5707 (defun gnus-summary-read-group 
5708   (group &optional show-all no-article kill-buffer)
5709   "Start reading news in newsgroup GROUP.
5710 If SHOW-ALL is non-nil, already read articles are also listed.
5711 If NO-ARTICLE is non-nil, no article is selected initially."
5712   (gnus-message 5 "Retrieving newsgroup: %s..." group)
5713   (let* ((new-group (gnus-summary-setup-buffer group))
5714          (quit-config (nth 1 (assoc 'quit-config (gnus-find-method-for-group
5715                                                   group))))
5716          (did-select (and new-group (gnus-select-newsgroup group show-all))))
5717     (cond 
5718      ((not new-group)
5719       (gnus-set-global-variables)
5720       (gnus-kill-buffer kill-buffer)
5721       (gnus-configure-windows 'summary)
5722       (gnus-set-mode-line 'summary)
5723       (gnus-summary-position-cursor)
5724       (message "")
5725       t)
5726      ((null did-select) 
5727       (and (eq major-mode 'gnus-summary-mode)
5728            (not (equal (current-buffer) kill-buffer))
5729            (progn
5730              (kill-buffer (current-buffer))
5731              (if (not quit-config)
5732                  (progn
5733                    (set-buffer gnus-group-buffer)
5734                    (gnus-group-jump-to-group group)
5735                    (gnus-group-next-unread-group 1))
5736                (if (not (buffer-name (car quit-config)))
5737                    (gnus-configure-windows 'group 'force)
5738                  (set-buffer (car quit-config))
5739                  (and (eq major-mode 'gnus-summary-mode)
5740                       (gnus-set-global-variables))
5741                  (gnus-configure-windows (cdr quit-config))))))
5742       (message "Can't select group")
5743       nil)
5744      ((eq did-select 'quit)
5745       (and (eq major-mode 'gnus-summary-mode)
5746            (not (equal (current-buffer) kill-buffer))
5747            (kill-buffer (current-buffer)))
5748       (gnus-kill-buffer kill-buffer)
5749       (if (not quit-config)
5750           (progn
5751             (set-buffer gnus-group-buffer)
5752             (gnus-group-jump-to-group group)
5753             (gnus-group-next-unread-group 1)
5754             (gnus-configure-windows 'group 'force))
5755         (if (not (buffer-name (car quit-config)))
5756             (gnus-configure-windows 'group 'force)
5757           (set-buffer (car quit-config))
5758           (and (eq major-mode 'gnus-summary-mode)
5759                (gnus-set-global-variables))
5760           (gnus-configure-windows (cdr quit-config))))
5761       (signal 'quit nil))
5762      (t
5763       (gnus-set-global-variables)
5764       ;; Save the active value in effect when the group was entered.
5765       (setq gnus-newsgroup-active 
5766             (gnus-copy-sequence
5767              (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
5768       ;; You can change the subjects in this hook.
5769       (run-hooks 'gnus-select-group-hook)
5770       ;; Do score processing.
5771       (and gnus-use-scoring (gnus-possibly-score-headers))
5772       (gnus-update-format-specifications)
5773       ;; Generate the summary buffer.
5774       (gnus-summary-prepare)
5775       (if (zerop (buffer-size))
5776           (cond (gnus-newsgroup-dormant
5777                  (gnus-summary-show-all-dormant))
5778                 ((and gnus-newsgroup-scored show-all)
5779                  (gnus-summary-show-all-expunged))))
5780       ;; Function `gnus-apply-kill-file' must be called in this hook.
5781       (run-hooks 'gnus-apply-kill-hook)
5782       (if (zerop (buffer-size))
5783           (progn
5784             ;; This newsgroup is empty.
5785             (gnus-summary-catchup-and-exit nil t) ;Without confirmations.
5786             (gnus-message 6 "No unread news")
5787             (gnus-kill-buffer kill-buffer)
5788             nil)
5789         ;;(save-excursion
5790         ;;  (if kill-buffer
5791         ;;      (let ((gnus-summary-buffer kill-buffer))
5792         ;;      (gnus-configure-windows 'group))))
5793         ;; Hide conversation thread subtrees.  We cannot do this in
5794         ;; gnus-summary-prepare-hook since kill processing may not
5795         ;; work with hidden articles.
5796         (and gnus-show-threads
5797              gnus-thread-hide-subtree
5798              (gnus-summary-hide-all-threads))
5799         ;; Show first unread article if requested.
5800         (goto-char (point-min))
5801         (if (and (not no-article)
5802                  gnus-auto-select-first
5803                  (gnus-summary-first-unread-article))
5804             ()
5805           (gnus-configure-windows 'summary))
5806         (gnus-set-mode-line 'summary)
5807         (gnus-summary-position-cursor)
5808         ;; If in async mode, we send some info to the backend.
5809         (and gnus-newsgroup-async
5810              (setq gnus-newsgroup-threads (nreverse gnus-newsgroup-threads))
5811              (gnus-request-asynchronous 
5812               gnus-newsgroup-name
5813               (if (and gnus-asynchronous-article-function
5814                        (fboundp gnus-asynchronous-article-function))
5815                   (funcall gnus-asynchronous-article-function
5816                            gnus-newsgroup-threads)
5817                 gnus-newsgroup-threads)))
5818         (gnus-kill-buffer kill-buffer)
5819         (if (not (get-buffer-window gnus-group-buffer))
5820             ()
5821           ;; gotta use windows, because recenter does wierd stuff if
5822           ;; the current buffer ain't the displayed window.
5823           (let ((owin (selected-window))) 
5824             (select-window (get-buffer-window gnus-group-buffer))
5825             (and (gnus-group-goto-group group)
5826                  (recenter))
5827             (select-window owin))))
5828       t))))
5829
5830 (defun gnus-summary-prepare ()
5831   ;; Generate the summary buffer.
5832   (let ((buffer-read-only nil))
5833     (erase-buffer)
5834     (gnus-message 5 "Threading...")
5835     (gnus-summary-prepare-threads 
5836      (if gnus-show-threads
5837          (gnus-gather-threads 
5838           (gnus-sort-threads 
5839            (if (and gnus-summary-expunge-below
5840                     (not gnus-fetch-old-headers))
5841                (gnus-make-threads-and-expunge)
5842              (gnus-make-threads))))
5843        gnus-newsgroup-headers)
5844      'cull)
5845     (gnus-message 5 "Threading...done")
5846     (gnus-summary-update-lines)
5847     ;; Remove the final newline.
5848     ;;(goto-char (point-max))
5849     ;;(delete-char -1)
5850     ;; Call hooks for modifying summary buffer.
5851     ;; Suggested by sven@tde.LTH.Se (Sven Mattisson).
5852     (goto-char (point-min))
5853     (run-hooks 'gnus-summary-prepare-hook)))
5854
5855 (defun gnus-gather-threads (threads)
5856   "Gather threads that have lost their roots."
5857   (if (not gnus-summary-make-false-root)
5858       threads 
5859     (let ((hashtb (gnus-make-hashtable 1023))
5860           (prev threads)
5861           (result threads)
5862           subject hthread whole-subject)
5863       (while threads
5864         (setq whole-subject 
5865               (setq subject (header-subject (car (car threads)))))
5866         (if gnus-summary-gather-subject-limit
5867             (or (and (numberp gnus-summary-gather-subject-limit)
5868                      (> (length subject) gnus-summary-gather-subject-limit)
5869                      (setq subject
5870                            (substring subject 0 
5871                                       gnus-summary-gather-subject-limit)))
5872                 (and (eq 'fuzzy gnus-summary-gather-subject-limit)
5873                      (setq subject (gnus-simplify-subject-fuzzy subject))))
5874           (setq subject (gnus-simplify-subject-re subject)))
5875         (if (setq hthread 
5876                   (gnus-gethash subject hashtb))
5877             (progn
5878               (or (stringp (car (car hthread)))
5879                   (setcar hthread (list whole-subject (car hthread))))
5880               (setcdr (car hthread) (nconc (cdr (car hthread)) 
5881                                            (list (car threads))))
5882               (setcdr prev (cdr threads))
5883               (setq threads prev))
5884           (gnus-sethash subject threads hashtb))
5885         (setq prev threads)
5886         (setq threads (cdr threads)))
5887       result)))
5888
5889 (defun gnus-make-threads ()
5890   ;; This function takes the dependencies already made by 
5891   ;; `gnus-get-newsgroup-headers' and builds the trees. First we go
5892   ;; through the dependecies in the hash table and finds all the
5893   ;; roots. Roots do not refer back to any valid articles.
5894   (let (roots new-roots)
5895     (and gnus-fetch-old-headers (eq gnus-headers-retrieved-by 'nov)
5896          (gnus-build-old-threads))
5897     (mapatoms
5898      (lambda (refs)
5899        (if (not (car (symbol-value refs)))
5900            (setq roots (append (cdr (symbol-value refs)) roots))
5901          ;; Ok, these refer back to valid articles, but if
5902          ;; `gnus-thread-ignore-subject' is nil, we have to check that
5903          ;; the root has the same subject as its children. The children
5904          ;; that do not are made into roots and removed from the list
5905          ;; of children. 
5906          (or gnus-thread-ignore-subject
5907              (let* ((prev (symbol-value refs))
5908                     (subject (gnus-simplify-subject-re 
5909                               (header-subject (car prev))))
5910                     (headers (cdr prev)))
5911                (while headers
5912                  (if (not (string= subject
5913                                    (gnus-simplify-subject-re 
5914                                     (header-subject (car headers)))))
5915                      (progn
5916                        (setq new-roots (cons (car headers) new-roots))
5917                        (setcdr prev (cdr headers)))
5918                    (setq prev headers))
5919                  (setq headers (cdr headers)))))))
5920      gnus-newsgroup-dependencies)
5921
5922     ;; We enter the new roots into the dependencies structure to
5923     ;; ensure that any possible later thread-regeneration will be
5924     ;; possible. 
5925     (let ((r new-roots))
5926       (while r
5927         (gnus-sethash (concat (header-id (car r)) ".boo")
5928                       (list nil (car r)) gnus-newsgroup-dependencies)
5929         (setq r (cdr r))))
5930
5931     (setq roots (nconc new-roots roots))
5932     
5933     (mapcar 'gnus-trim-thread
5934             (apply 'append
5935                    (mapcar 'gnus-cut-thread
5936                            (mapcar 'gnus-make-sub-thread roots))))))
5937   
5938 (defun gnus-make-threads-and-expunge ()
5939   ;; This function takes the dependencies already made by 
5940   ;; `gnus-get-newsgroup-headers' and builds the trees. First we go
5941   ;; through the dependecies in the hash table and finds all the
5942   ;; roots. Roots do not refer back to any valid articles.
5943   (let ((default (or gnus-summary-default-score 0))
5944         (below gnus-summary-expunge-below)
5945         roots article new-roots)
5946     (and gnus-fetch-old-headers (eq gnus-headers-retrieved-by 'nov)
5947          (gnus-build-old-threads))
5948     (mapatoms
5949      (lambda (refs)
5950        (if (not (car (symbol-value refs)))
5951            ;; These articles do not refer back to any other articles -
5952            ;; they are roots.
5953            (let ((headers (cdr (symbol-value refs))))
5954              ;; We weed out the low-scored articles.
5955              (while headers
5956                (if (not (< (or (cdr (assq (header-number (car headers))
5957                                           gnus-newsgroup-scored)) default)
5958                            below))
5959                    ;; It is over.
5960                    (setq roots (cons (car headers) roots))
5961                  ;; It is below, so we mark it as read.
5962                  (setq gnus-newsgroup-unreads
5963                        (delq (header-number (car headers))
5964                              gnus-newsgroup-unreads)))
5965                (setq headers (cdr headers))))
5966          ;; Ok, these refer back to valid articles, but if
5967          ;; `gnus-thread-ignore-subject' is nil, we have to check that
5968          ;; the root has the same subject as its children. The children
5969          ;; that do not are made into roots and removed from the list
5970          ;; of children. 
5971          (or gnus-thread-ignore-subject
5972              (let* ((prev (symbol-value refs))
5973                     (subject (gnus-simplify-subject-re 
5974                               (header-subject (car prev))))
5975                     (headers (cdr prev)))
5976                (while headers
5977                  (if (not (string= subject
5978                                    (gnus-simplify-subject-re 
5979                                     (header-subject (car headers)))))
5980                      (progn
5981                        (if (not (< (or (cdr (assq (header-number (car headers))
5982                                                   gnus-newsgroup-scored))
5983                                        default) below))
5984                            (setq new-roots (cons (car headers) new-roots))
5985                          (setq gnus-newsgroup-unreads
5986                                (delq (header-number (car headers))
5987                                      gnus-newsgroup-unreads)))
5988                        (setcdr prev (cdr headers)))
5989                    (setq prev headers))
5990                  (setq headers (cdr headers)))))
5991          ;; If this article is expunged, some of the children might be
5992          ;; roots.  
5993          (if (< (or (cdr (assq (header-number (car (symbol-value refs)))
5994                                gnus-newsgroup-scored)) default)
5995                 below)
5996              (let* ((prev (symbol-value refs))
5997                     (headers (cdr prev)))
5998                (while headers
5999                  (setq article (header-number (car headers)))
6000                  (if (not (< (or (cdr (assq article gnus-newsgroup-scored))
6001                                  default) below))
6002                      (progn (setq new-roots (cons (car headers) new-roots))
6003                             (setq prev headers))
6004                    (setq gnus-newsgroup-unreads 
6005                          (delq article gnus-newsgroup-unreads))
6006                    (setcdr prev (cdr headers)))
6007                  (setq headers (cdr headers))))
6008            ;; It was not expunged, but we look at expunged children.
6009            (let* ((prev (symbol-value refs))
6010                   (headers (cdr prev))
6011                   article)
6012              (while headers
6013                (setq article (header-number (car headers)))
6014                (if (not (< (or (cdr (assq article gnus-newsgroup-scored))
6015                                default) below))
6016                    (setq prev headers)
6017                  (setq gnus-newsgroup-unreads 
6018                        (delq article gnus-newsgroup-unreads))
6019                  (setcdr prev (cdr headers)))
6020                (setq headers (cdr headers)))))))
6021      gnus-newsgroup-dependencies)
6022
6023     ;; We enter the new roots into the dependencies structure to
6024     ;; ensure that any possible later thread-regeneration will be
6025     ;; possible. 
6026     (let ((r new-roots))
6027       (while r
6028         (gnus-sethash (concat (header-id (car r)) ".boo")
6029                       (list nil (car r)) gnus-newsgroup-dependencies)
6030         (setq r (cdr r))))
6031
6032     (setq roots (nconc new-roots roots))
6033     
6034     (mapcar 'gnus-trim-thread
6035             (apply 'append
6036                    (mapcar 'gnus-cut-thread
6037                            (mapcar 'gnus-make-sub-thread roots))))))
6038   
6039 (defun gnus-cut-thread (thread)
6040   ;; Remove leaf dormant or ancient articles from THREAD.
6041   (let ((head (car thread))
6042         (tail (apply 'append (mapcar 'gnus-cut-thread (cdr thread)))))
6043     (if (and (null tail)
6044              (let ((number (header-number head)))
6045                (or (memq number gnus-newsgroup-ancient)
6046                    (memq number gnus-newsgroup-dormant)
6047                    (and gnus-summary-expunge-below
6048                         (eq gnus-fetch-old-headers 'some)
6049                         (< (or (cdr (assq number gnus-newsgroup-scored))
6050                                gnus-summary-default-score 0)
6051                            gnus-summary-expunge-below)
6052                         (progn
6053                           (setq gnus-newsgroup-unreads
6054                                 (delq number gnus-newsgroup-unreads))
6055                           t)))))
6056         nil
6057       (list (cons head tail)))))
6058
6059 (defun gnus-trim-thread (thread)
6060   ;; Remove root ancient articles with only one child from THREAD.
6061   (if (and (eq gnus-fetch-old-headers 'some)
6062            (memq (header-number (car thread)) gnus-newsgroup-ancient)
6063            (= (length thread) 2))
6064       (gnus-trim-thread (nth 1 thread))
6065     thread))
6066
6067 (defun gnus-make-sub-thread (root)
6068   ;; This function makes a sub-tree for a node in the tree.
6069   (let ((children (reverse (cdr (gnus-gethash (downcase (header-id root))
6070                                               gnus-newsgroup-dependencies)))))
6071     (cons root (mapcar 'gnus-make-sub-thread children))))
6072
6073 (defun gnus-build-old-threads ()
6074   ;; Look at all the articles that refer back to old articles, and
6075   ;; fetch the headers for the articles that aren't there. This will
6076   ;; build complete threads - if the roots haven't been expired by the
6077   ;; server, that is.
6078   (let (id heads)
6079     (mapatoms
6080      (lambda (refs)
6081        (if (not (car (symbol-value refs)))
6082            (progn
6083              (setq heads (cdr (symbol-value refs)))
6084              (while heads
6085                (if (not (memq (header-number (car heads))
6086                               gnus-newsgroup-dormant))
6087                    (progn
6088                      (setq id (symbol-name refs))
6089                      (while (and (setq id (gnus-build-get-header id))
6090                                  (not (car (gnus-gethash 
6091                                             id gnus-newsgroup-dependencies)))))
6092                      (setq heads nil))
6093                  (setq heads (cdr heads)))))))
6094      gnus-newsgroup-dependencies)))
6095
6096 (defun gnus-build-get-header (id)
6097   ;; Look through the buffer of NOV lines and find the header to
6098   ;; ID. Enter this line into the dependencies hash table, and return
6099   ;; the id of the parent article (if any).
6100   (let ((deps gnus-newsgroup-dependencies)
6101         found header)
6102     (prog1
6103         (save-excursion
6104           (set-buffer nntp-server-buffer)
6105           (goto-char (point-min))
6106           (while (and (not found) (search-forward id nil t))
6107             (beginning-of-line)
6108             (setq found (looking-at 
6109                          (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
6110                                  (regexp-quote id))))
6111             (or found (beginning-of-line 2)))
6112           (if found
6113               (let (ref)
6114                 (beginning-of-line)
6115                 (and
6116                  (setq header (gnus-nov-parse-line 
6117                                (read (current-buffer)) deps))
6118                  (setq ref (header-references header))
6119                  (string-match "\\(<[^>]+>\\) *$" ref)
6120                  (substring ref (match-beginning 1) (match-end 1))))))
6121       (and header
6122            (setq gnus-newsgroup-headers (cons header gnus-newsgroup-headers)
6123                  gnus-newsgroup-ancient (cons (header-number header)
6124                                               gnus-newsgroup-ancient))))))
6125
6126 ;; Re-build the thread containing ID.
6127 (defun gnus-rebuild-thread (id)
6128   (let ((dep gnus-newsgroup-dependencies)
6129         (buffer-read-only nil)
6130         parent headers refs thread art)
6131     (while (and id (setq headers
6132                          (car (setq art (gnus-gethash (downcase id) dep)))))
6133       (setq parent art)
6134       (setq id (and (setq refs (header-references headers))
6135                     (string-match "\\(<[^>]+>\\) *$" refs)
6136                     (substring refs (match-beginning 1) (match-end 1)))))
6137     (setq thread (gnus-make-sub-thread (car parent)))
6138     (gnus-rebuild-remove-articles thread)
6139     (let ((beg (point)))
6140       (gnus-summary-prepare-threads (list thread))
6141       (gnus-summary-update-lines beg (point)))))
6142
6143 ;; Delete all lines in the summary buffer that correspond to articles
6144 ;; in this thread.
6145 (defun gnus-rebuild-remove-articles (thread)
6146   (and (gnus-summary-goto-subject (header-number (car thread)))
6147        (gnus-delete-line))
6148   (mapcar (lambda (th) (gnus-rebuild-remove-articles th)) (cdr thread)))
6149
6150 (defun gnus-sort-threads (threads)
6151   ;; Sort threads as specified in `gnus-thread-sort-functions'.
6152   (let ((fun gnus-thread-sort-functions))
6153     (while fun
6154       (setq threads (sort threads (car fun))
6155             fun (cdr fun))))
6156   threads)
6157
6158 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
6159 (defmacro gnus-thread-header (thread)
6160   ;; Return header of first article in THREAD.
6161   ;; Note that THREAD must never, evr be anything else than a variable -
6162   ;; using some other form will lead to serious barfage.
6163   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
6164   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
6165   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" ; 
6166         (vector thread) 2))
6167
6168 (defun gnus-thread-sort-by-number (h1 h2)
6169   "Sort threads by root article number."
6170   (< (header-number (gnus-thread-header h1))
6171      (header-number (gnus-thread-header h2))))
6172
6173 (defun gnus-thread-sort-by-author (h1 h2)
6174   "Sort threads by root author."
6175   (string-lessp
6176    (let ((extract (funcall 
6177                    gnus-extract-address-components
6178                    (header-from (gnus-thread-header h1)))))
6179      (or (car extract) (cdr extract)))
6180    (let ((extract (funcall
6181                    gnus-extract-address-components 
6182                    (header-from (gnus-thread-header h2)))))
6183      (or (car extract) (cdr extract)))))
6184
6185 (defun gnus-thread-sort-by-subject (h1 h2)
6186   "Sort threads by root subject."
6187   (string-lessp
6188    (downcase (gnus-simplify-subject 
6189               (header-subject (gnus-thread-header h1))))
6190    (downcase (gnus-simplify-subject 
6191               (header-subject (gnus-thread-header h2))))))
6192
6193 (defun gnus-thread-sort-by-date (h1 h2)
6194   "Sort threads by root article date."
6195   (string-lessp
6196    (gnus-sortable-date (header-date (gnus-thread-header h1)))
6197    (gnus-sortable-date (header-date (gnus-thread-header h2)))))
6198
6199 (defun gnus-thread-sort-by-score (h1 h2)
6200   "Sort threads by root article score.
6201 Unscored articles will be counted as having a score of zero."
6202   (> (or (cdr (assq (header-number (gnus-thread-header h1))
6203                     gnus-newsgroup-scored))
6204          gnus-summary-default-score 0)
6205      (or (cdr (assq (header-number (gnus-thread-header h2))
6206                     gnus-newsgroup-scored))
6207          gnus-summary-default-score 0)))
6208
6209 (defun gnus-thread-sort-by-total-score (h1 h2)
6210   "Sort threads by the sum of all scores in the thread.
6211 Unscored articles will be counted as having a score of zero."
6212   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
6213
6214 (defun gnus-thread-total-score (thread)
6215   ;;  This function find the total score of THREAD.
6216   (if (consp thread)
6217       (if (stringp (car thread))
6218           (apply gnus-thread-score-function 0
6219                  (mapcar 'gnus-thread-total-score-1 (cdr thread)))
6220         (gnus-thread-total-score-1 thread))
6221     (gnus-thread-total-score-1 (list thread))))
6222
6223 (defun gnus-thread-total-score-1 (root)
6224   ;; This function find the total score of the thread below ROOT.
6225   (setq root (car root))
6226   (apply gnus-thread-score-function
6227          (or (cdr (assq (header-number root) gnus-newsgroup-scored))
6228              gnus-summary-default-score 0)
6229          (mapcar 'gnus-thread-total-score
6230                  (cdr (gnus-gethash (downcase (header-id root))
6231                                     gnus-newsgroup-dependencies)))))
6232
6233 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
6234 (defvar gnus-tmp-prev-subject "")
6235
6236 (defun gnus-summary-prepare-threads (threads &optional cull)
6237   "Prepare summary buffer from THREADS and indentation LEVEL.  
6238 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'  
6239 or a straight list of headers."
6240   (let ((level 0)
6241         thread header number subject stack state gnus-tmp-gathered)
6242     (if (vectorp (car threads))
6243         ;; If this is a straight (sic) list of headers, then a
6244         ;; threaded summary display isn't required, so we just create
6245         ;; an unthreaded one.
6246         (gnus-summary-prepare-unthreaded threads cull)
6247
6248       ;; Do the threaded display.
6249
6250       (while (or threads stack)
6251         
6252         (if threads
6253             ;; If there are some threads, we do them before the
6254             ;; threads on the stack.
6255             (setq thread threads
6256                   header (car (car thread)))
6257           ;; There were no current threads, so we pop something off
6258           ;; the stack. 
6259           (setq state (car stack)
6260                 level (car state)
6261                 thread (cdr state)
6262                 stack (cdr stack)
6263                 header (car (car thread))))
6264
6265         (if (stringp header)
6266             (progn
6267               ;; The header is a dummy root.
6268               (cond 
6269                ((eq gnus-summary-make-false-root 'adopt)
6270                 ;; We let the first article adopt the rest.
6271                 (let ((th (car (cdr (car thread)))))
6272                   (while (cdr th)
6273                     (setq th (cdr th)))
6274                   (setcdr th (cdr (cdr (car thread))))
6275                   (setq gnus-tmp-gathered 
6276                         (nconc (mapcar
6277                                 (lambda (h) (header-number (car h)))
6278                                 (cdr (cdr (car thread))))
6279                                gnus-tmp-gathered))
6280                   (setcdr (cdr (car thread)) nil))
6281                 (setq level -1))
6282                ((eq gnus-summary-make-false-root 'empty)
6283                 ;; We print adopted articles with empty subject fields.
6284                 (setq gnus-tmp-gathered 
6285                       (nconc (mapcar
6286                               (lambda (h) (header-number (car h)))
6287                               (cdr (cdr (car thread))))
6288                              gnus-tmp-gathered))
6289                 (setq level -1))
6290                ((eq gnus-summary-make-false-root 'dummy)
6291                 ;; We output a dummy root.
6292                 (gnus-summary-insert-dummy-line 
6293                  nil header (header-number (car (car (cdr (car thread)))))))
6294                (t
6295                 ;; We do not make a root for the gathered
6296                 ;; sub-threads at all.  
6297                 (setq level -1))))
6298       
6299           (setq number (header-number header)
6300                 subject (header-subject header))
6301
6302           ;; Do the async thing.
6303           (and gnus-newsgroup-async
6304                (setq gnus-newsgroup-threads
6305                      (cons (cons number (header-lines header)) 
6306                            gnus-newsgroup-threads)))
6307
6308           ;; We may have to root out some bad articles...
6309           (and cull
6310                (= level 0)
6311                (cond ((and (memq (setq number (header-number header))
6312                                  gnus-newsgroup-dormant)
6313                            (null thread))
6314                       (setq header nil))
6315                      ((and gnus-summary-expunge-below
6316                            (< (or (cdr (assq number gnus-newsgroup-scored))
6317                                   gnus-summary-default-score 0)
6318                               gnus-summary-expunge-below))
6319                       (setq header nil)
6320                       (setq gnus-newsgroup-unreads 
6321                             (delq number gnus-newsgroup-unreads)))))
6322
6323           (and
6324            header
6325            (progn
6326              (inline
6327                (gnus-summary-insert-line
6328                 nil header level nil 
6329                 (cond 
6330                  ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
6331                  ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
6332                  ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
6333                  ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
6334                  (t gnus-ancient-mark))
6335                 (memq number gnus-newsgroup-replied)
6336                 (memq number gnus-newsgroup-expirable)
6337                 (if (and (eq gnus-summary-make-false-root 'empty)
6338                          (memq number gnus-tmp-gathered))
6339                     gnus-summary-same-subject
6340                   (if (or (zerop level)
6341                           (and gnus-thread-ignore-subject
6342                                (not (string= 
6343                                      (gnus-simplify-subject-re
6344                                       gnus-tmp-prev-subject)
6345                                      (gnus-simplify-subject-re
6346                                       subject)))))
6347                       subject
6348                     gnus-summary-same-subject))
6349                 (and (eq gnus-summary-make-false-root 'adopt)
6350                      (memq number gnus-tmp-gathered))
6351                 (cdr (assq number gnus-newsgroup-scored)))
6352
6353                (setq gnus-tmp-prev-subject subject)))))
6354
6355         (if (nth 1 thread) 
6356             (setq stack (cons (cons (max 0 level) (nthcdr 1 thread)) stack)))
6357         (setq level (1+ level))
6358         (setq threads (cdr (car thread)))))))
6359
6360
6361 (defun gnus-summary-prepare-unthreaded (headers &optional cull)
6362   (let (header number)
6363
6364     ;; Do the async thing, if that is required.
6365     (if gnus-newsgroup-async
6366         (setq gnus-newsgroup-threads
6367               (mapcar (lambda (h) (cons (header-number h) (header-lines h)))
6368                       headers)))
6369
6370     (while headers
6371       (setq header (car headers)
6372             headers (cdr headers)
6373             number (header-number header))
6374
6375       ;; We may have to root out some bad articles...
6376       (cond 
6377        ((and cull
6378              (memq (setq number (header-number header))
6379                    gnus-newsgroup-dormant)))
6380        ((and cull gnus-summary-expunge-below
6381              (< (or (cdr (assq number gnus-newsgroup-scored))
6382                     gnus-summary-default-score 0)
6383                 gnus-summary-expunge-below))
6384         (setq gnus-newsgroup-unreads 
6385               (delq number gnus-newsgroup-unreads)))
6386        (t
6387         (gnus-summary-insert-line
6388          nil header 0 nil 
6389          (cond ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
6390                ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
6391                ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
6392                ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
6393                (t gnus-ancient-mark))
6394          (memq number gnus-newsgroup-replied)
6395          (memq number gnus-newsgroup-expirable)
6396          (header-subject header) nil
6397          (cdr (assq number gnus-newsgroup-scored))))))))
6398
6399 (defun gnus-select-newsgroup (group &optional read-all)
6400   "Select newsgroup GROUP.
6401 If READ-ALL is non-nil, all articles in the group are selected."
6402   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
6403          (info (nth 2 entry))
6404          articles)
6405     (gnus-check-news-server
6406      (setq gnus-current-select-method (gnus-find-method-for-group group)))
6407
6408     (or (gnus-server-opened gnus-current-select-method)
6409         (gnus-open-server gnus-current-select-method)
6410         (error "Couldn't open server"))
6411     
6412     (or (and (eq (car entry) t)
6413              (gnus-activate-newsgroup (car info)))
6414         (gnus-request-group group t)
6415         (progn
6416           (kill-buffer (current-buffer))
6417           (error "Couldn't request group %s: %s" 
6418                  group (gnus-status-message group))))
6419
6420     (setq gnus-newsgroup-name group)
6421     (setq gnus-newsgroup-unselected nil)
6422     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
6423
6424     (and gnus-asynchronous
6425          (gnus-check-backend-function 
6426           'request-asynchronous gnus-newsgroup-name)
6427          (setq gnus-newsgroup-async
6428                (gnus-request-asynchronous gnus-newsgroup-name)))
6429
6430     (setq articles (gnus-articles-to-read group read-all))
6431
6432     (cond 
6433      ((null articles) 
6434       (gnus-message 3 "Couldn't select newsgroup")
6435       'quit)
6436      ((eq articles 0) nil)
6437      (t
6438       ;; Init the dependencies hash table.
6439       (setq gnus-newsgroup-dependencies 
6440             (gnus-make-hashtable (length articles)))
6441       ;; Retrieve the headers and read them in.
6442       (gnus-message 5 "Fetching headers...")
6443       (setq gnus-newsgroup-headers 
6444             (if (eq 'nov (setq gnus-headers-retrieved-by
6445                                ;; This is a naughty hack. To get the
6446                                ;; retrieval of old headers to work, we
6447                                ;; set `nntp-nov-gap' to nil (locally),
6448                                ;; and then just retrieve the headers.
6449                                ;; Mucho magic.
6450                                (if gnus-fetch-old-headers
6451                                    (let (nntp-nov-gap)
6452                                      (gnus-retrieve-headers 
6453                                       (if (not (eq 1 (car articles)))
6454                                           (cons 1 articles)
6455                                         articles)
6456                                       gnus-newsgroup-name))
6457                                  (gnus-retrieve-headers 
6458                                   articles gnus-newsgroup-name))))
6459                 (progn
6460                   (gnus-get-newsgroup-headers-xover articles))
6461               ;; If we were to fetch old headers, but the backend didn't
6462               ;; support XOVER, then it is possible we fetched one article
6463               ;; that we shouldn't have. If that's the case, we remove it.
6464               (if (not gnus-fetch-old-headers)
6465                   ()
6466                 (save-excursion
6467                   (set-buffer nntp-server-buffer)
6468                   (goto-char (point-min))
6469                   (and 
6470                    (looking-at "[0-9]+[ \t]+1[ \t]") ; This is not a NOV line.
6471                    (delete-region       ; So we delete this head.
6472                     (point) 
6473                     (search-forward "\n.\n" nil t)))))
6474               (gnus-get-newsgroup-headers)))
6475       (gnus-message 5 "Fetching headers...done")      
6476       ;; Remove canceled articles from the list of unread articles.
6477       (setq gnus-newsgroup-unreads
6478             (gnus-set-sorted-intersection 
6479              gnus-newsgroup-unreads
6480              (mapcar (lambda (headers) (header-number headers))
6481                      gnus-newsgroup-headers)))
6482       ;; Adjust and set lists of article marks.
6483       (and info
6484            (let (marked)
6485              (gnus-adjust-marked-articles info)
6486              (setq gnus-newsgroup-marked 
6487                    (copy-sequence
6488                     (cdr (assq 'tick (setq marked (nth 3 info))))))
6489              (setq gnus-newsgroup-replied 
6490                    (copy-sequence (cdr (assq 'reply marked))))
6491              (setq gnus-newsgroup-expirable
6492                    (copy-sequence (cdr (assq 'expire marked))))
6493              (setq gnus-newsgroup-killed
6494                    (copy-sequence (cdr (assq 'killed marked))))
6495              (setq gnus-newsgroup-bookmarks 
6496                    (copy-sequence (cdr (assq 'bookmark marked))))
6497              (setq gnus-newsgroup-dormant 
6498                    (copy-sequence (cdr (assq 'dormant marked))))
6499              (setq gnus-newsgroup-scored 
6500                    (copy-sequence (cdr (assq 'score marked))))
6501              (setq gnus-newsgroup-processable nil)))
6502       ;; Create the header hashtb.
6503       (or gnus-newsgroup-headers-hashtb-by-number
6504           (gnus-make-headers-hashtable-by-number))
6505       ;; Check whether auto-expire is to be done in this group.
6506       (setq gnus-newsgroup-auto-expire
6507             (or (and (stringp gnus-auto-expirable-newsgroups)
6508                      (string-match gnus-auto-expirable-newsgroups group))
6509                 (memq 'auto-expire (nth 5 info))))
6510       ;; First and last article in this newsgroup.
6511       (and gnus-newsgroup-headers
6512            (setq gnus-newsgroup-begin 
6513                  (header-number (car gnus-newsgroup-headers)))
6514            (setq gnus-newsgroup-end
6515                  (header-number (gnus-last-element gnus-newsgroup-headers))))
6516       (setq gnus-reffed-article-number -1)
6517       ;; GROUP is successfully selected.
6518       (or gnus-newsgroup-headers t)))))
6519
6520 (defun gnus-articles-to-read (group read-all)
6521   ;; Find out what articles the user wants to read.
6522   (let* ((articles
6523           ;; Select all articles if `read-all' is non-nil, or if all the
6524           ;; unread articles are dormant articles.
6525           (if (or (and read-all (not (numberp read-all)))
6526                   (= (length gnus-newsgroup-unreads) 
6527                      (length gnus-newsgroup-dormant)))
6528               (gnus-uncompress-range 
6529                (gnus-gethash group gnus-active-hashtb))
6530             gnus-newsgroup-unreads))
6531          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
6532          (scored (length scored-list))
6533          (number (length articles))
6534          (marked (+ (length gnus-newsgroup-marked)
6535                     (length gnus-newsgroup-dormant)))
6536          (select
6537           (cond 
6538            ((numberp read-all)
6539             read-all)
6540            (t
6541             (condition-case ()
6542                 (cond ((and (or (<= scored marked)
6543                                 (= scored number))
6544                             (numberp gnus-large-newsgroup)
6545                             (> number gnus-large-newsgroup))
6546                        (let ((input
6547                               (read-string
6548                                (format
6549                                 "How many articles from %s (default %d): "
6550                                 gnus-newsgroup-name number))))
6551                          (if (string-match "^[ \t]*$" input)
6552                              number input)))
6553                       ((and (> scored marked) (< scored number))
6554                        (let ((input
6555                               (read-string
6556                                (format 
6557                                 "%s %s (%d scored, %d total): "
6558                                 "How many articles from"
6559                                 group scored number))))
6560                          (if (string-match "^[ \t]*$" input)
6561                              number input)))
6562                       (t number))
6563               (quit nil))))))
6564     (setq select (if (stringp select) (string-to-number select) select))
6565     (if (or (null select) (zerop select))
6566         select
6567       (if (and (not (zerop scored)) (<= (abs select) scored))
6568           (progn
6569             (setq articles (sort scored-list '<))
6570             (setq number (length articles)))
6571         (setq articles (copy-sequence articles)))
6572
6573       (if (< (abs select) number)
6574           (if (< select 0) 
6575               ;; Select the N oldest articles.
6576               (setcdr (nthcdr (1- (abs select)) articles) nil)
6577             ;; Select the N most recent articles.
6578             (setq articles (nthcdr (- number select) articles))))
6579       (setq gnus-newsgroup-unselected
6580             (gnus-sorted-intersection
6581              gnus-newsgroup-unreads
6582              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
6583       articles)))
6584
6585 (defun gnus-killed-articles (killed articles)
6586   (let (out)
6587     (while articles
6588       (if (inline (gnus-member-of-range (car articles) killed))
6589           (setq out (cons (car articles) out)))
6590       (setq articles (cdr articles)))
6591     out))
6592
6593 (defun gnus-adjust-marked-articles (info &optional active)
6594   "Remove all marked articles that are no longer legal."
6595   (let ((marked-lists (nth 3 info))
6596         (active (or active (gnus-gethash (car info) gnus-active-hashtb)))
6597         m prev)
6598     ;; There are many types of marked articles.
6599     (while marked-lists
6600       (setq m (cdr (setq prev (car marked-lists))))
6601       (cond ((or (eq 'tick (car prev)) (eq 'dormant (car prev)))
6602              ;; Make sure that all ticked articles are a subset of the
6603              ;; unread/unselected articles.
6604              (while m
6605                (if (or (memq (car m) gnus-newsgroup-unreads)
6606                        (memq (car m) gnus-newsgroup-unselected))
6607                    (setq prev m)
6608                  (setcdr prev (cdr m)))
6609                (setq m (cdr m))))
6610             ((eq 'score (car prev))
6611              ;; Scored articles should be a subset of
6612              ;; unread/unselected articles. 
6613              (while m
6614                (if (or (memq (car (car m)) gnus-newsgroup-unreads)
6615                        (memq (car (car m)) gnus-newsgroup-unreads))
6616                    (setq prev m)
6617                  (setcdr prev (cdr m)))
6618                (setq m (cdr m))))
6619             ((eq 'bookmark (car prev))
6620              ;; Bookmarks should be a subset of active articles.
6621              (while m
6622                (if (< (car (car m)) (car active))
6623                    (setcdr prev (cdr m))
6624                  (setq prev m))
6625                (setq m (cdr m))))
6626             ((eq 'killed (car prev))
6627              ;; Articles that have been through the kill process are
6628              ;; to be a subset of active articles.
6629              (while (and m (< (or (and (numberp (car m)) (car m))
6630                                   (cdr (car m)))
6631                               (car active)))
6632                (setcdr prev (cdr m))
6633                (setq m (cdr m)))
6634              (if (and m (< (or (and (numberp (car m)) (car m))
6635                                (car (car m)))
6636                            (car active))) 
6637                  (setcar (if (numberp (car m)) m (car m)) (car active))))
6638             ((or (eq 'reply (car prev)) (eq 'expire (car prev)))
6639              ;; The replied and expirable articles have to be articles
6640              ;; that are active. 
6641              (while m
6642                (if (< (car m) (car active))
6643                    (setcdr prev (cdr m))
6644                  (setq prev m))
6645                (setq m (cdr m)))))
6646       (setq marked-lists (cdr marked-lists)))
6647     ;; Remove all lists that are empty.
6648     (setq marked-lists (nth 3 info))
6649     (if marked-lists
6650         (progn
6651           (while (= 1 (length (car marked-lists)))
6652             (setq marked-lists (cdr marked-lists)))
6653           (setq m (cdr (setq prev marked-lists)))
6654           (while m
6655             (if (= 1 (length (car m)))
6656                 (setcdr prev (cdr m))
6657               (setq prev m))
6658             (setq m (cdr m)))
6659           (setcar (nthcdr 3 info) marked-lists)))
6660     ;; Finally, if there are no marked lists at all left, and if there
6661     ;; are no elements after the lists in the info list, we just chop
6662     ;; the info list off before the marked lists.
6663     (and (null marked-lists) 
6664          (not (nthcdr 4 info))
6665          (setcdr (nthcdr 2 info) nil)))
6666   info)
6667
6668 (defun gnus-set-marked-articles 
6669   (info ticked replied expirable killed dormant bookmark score) 
6670   "Enter the various lists of marked articles into the newsgroup info list."
6671   (let (newmarked)
6672     (and ticked (setq newmarked (cons (cons 'tick ticked) nil)))
6673     (and replied (setq newmarked (cons (cons 'reply replied) newmarked)))
6674     (and expirable (setq newmarked (cons (cons 'expire expirable) 
6675                                          newmarked)))
6676     (and killed (setq newmarked (cons (cons 'killed killed) newmarked)))
6677     (and dormant (setq newmarked (cons (cons 'dormant dormant) newmarked)))
6678     (and bookmark (setq newmarked (cons (cons 'bookmark bookmark) 
6679                                         newmarked)))
6680     (and score (setq newmarked (cons (cons 'score score) newmarked)))
6681     (if (nthcdr 3 info)
6682         (progn
6683           (setcar (nthcdr 3 info) newmarked)
6684           (and (not newmarked)
6685                (not (nthcdr 4 info))
6686                (setcdr (nthcdr 2 info) nil)))
6687       (if newmarked
6688           (setcdr (nthcdr 2 info) (list newmarked))))))
6689
6690 (defun gnus-add-marked-articles (group type articles &optional info force)
6691   ;; Add ARTICLES of TYPE to the info of GROUP.
6692   ;; If INFO is non-nil, use that info. If FORCE is non-nil, don't
6693   ;; add, but replace marked articles of TYPE with ARTICLES.
6694   (let ((info (or info (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
6695         marked m)
6696     (or (not info)
6697         (and (not (setq marked (nthcdr 3 info)))
6698              (setcdr (nthcdr 2 info) (list (list (cons type articles)))))
6699         (and (not (setq m (assq type (car marked))))
6700              (setcar marked (cons (cons type articles) (car marked))))
6701         (if force
6702             (setcdr m articles)
6703           (nconc m articles)))))
6704          
6705 (defun gnus-set-mode-line (where)
6706   "This function sets the mode line of the article or summary buffers.
6707 If WHERE is `summary', the summary mode line format will be used."
6708   (if (memq where gnus-updated-mode-lines)
6709       (let (mode-string)
6710         (save-excursion
6711           (set-buffer gnus-summary-buffer)
6712           (let* ((mformat (if (eq where 'article) 
6713                               gnus-article-mode-line-format-spec
6714                             gnus-summary-mode-line-format-spec))
6715                  (group-name gnus-newsgroup-name)
6716                  (article-number (or gnus-current-article 0))
6717                  (unread (- (length gnus-newsgroup-unreads)
6718                             (length gnus-newsgroup-dormant)))
6719                  (unread-and-unticked 
6720                   (- unread (length gnus-newsgroup-marked)))
6721                  (unselected (length gnus-newsgroup-unselected))
6722                  (unread-and-unselected
6723                   (cond ((and (zerop unread-and-unticked)
6724                               (zerop unselected)) "")
6725                         ((zerop unselected) 
6726                          (format "{%d more}" unread-and-unticked))
6727                         (t (format "{%d(+%d) more}"
6728                                    unread-and-unticked unselected))))
6729                  (subject
6730                   (if gnus-current-headers
6731                       (header-subject gnus-current-headers) ""))
6732                  (max-len (and gnus-mode-non-string-length
6733                                (- (frame-width) gnus-mode-non-string-length)))
6734                  header) ;; passed as argument to any user-format-funcs
6735             (setq mode-string (eval mformat))
6736             (or (numberp max-len)
6737                 (setq max-len (length mode-string)))
6738             (if (< max-len 4) (setq max-len 4))
6739             (if (> (length mode-string) max-len)
6740                 ;; modified by MORIOKA Tomohiko <morioka@jaist.ac.jp>
6741                 ;;  function `substring' might cut on a middle
6742                 ;;  of multi-octet character.
6743                 (setq mode-string 
6744                       (concat (gnus-truncate-string mode-string (- max-len 3))
6745                               "...")))
6746             (setq mode-string (format (format "%%-%ds" max-len)
6747                                       mode-string))))
6748         (setq mode-line-buffer-identification mode-string)
6749         (set-buffer-modified-p t))))
6750
6751 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
6752   "Go through the HEADERS list and add all Xrefs to a hash table.
6753 The resulting hash table is returned, or nil if no Xrefs were found."
6754   (let* ((from-method (gnus-find-method-for-group from-newsgroup))
6755          (prefix (if (and 
6756                       (gnus-group-foreign-p from-newsgroup)
6757                       (not (memq 'virtual 
6758                                  (assoc (symbol-name (car from-method))
6759                                         gnus-valid-select-methods))))
6760                      (gnus-group-real-prefix from-newsgroup)))
6761          (xref-hashtb (make-vector 63 0))
6762          start group entry number xrefs header)
6763     (while headers
6764       (setq header (car headers))
6765       (if (and (setq xrefs (header-xref header))
6766                (not (memq (header-number header) unreads)))
6767           (progn
6768             (setq start 0)
6769             (while (string-match "\\([^ ]+\\):\\([0-9]+\\)" xrefs start)
6770               (setq start (match-end 0))
6771               (setq group (concat prefix (substring xrefs (match-beginning 1) 
6772                                                     (match-end 1))))
6773               (setq number 
6774                     (string-to-int (substring xrefs (match-beginning 2) 
6775                                               (match-end 2))))
6776               (if (setq entry (gnus-gethash group xref-hashtb))
6777                   (setcdr entry (cons number (cdr entry)))
6778                 (gnus-sethash group (cons number nil) xref-hashtb)))))
6779       (setq headers (cdr headers)))
6780     (if start xref-hashtb nil)))
6781
6782 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads expirable)
6783   "Look through all the headers and mark the Xrefs as read."
6784   (let ((virtual (memq 'virtual 
6785                        (assoc (symbol-name (car (gnus-find-method-for-group 
6786                                                  from-newsgroup)))
6787                               gnus-valid-select-methods)))
6788         name entry info xref-hashtb idlist method
6789         nth4)
6790     (save-excursion
6791       (set-buffer gnus-group-buffer)
6792       (if (setq xref-hashtb 
6793                 (gnus-create-xref-hashtb from-newsgroup headers unreads))
6794           (mapatoms 
6795            (lambda (group)
6796              (if (string= from-newsgroup (setq name (symbol-name group)))
6797                  ()
6798                (setq idlist (symbol-value group))
6799                ;; Dead groups are not updated.
6800                (if (and (prog1 
6801                             (setq entry (gnus-gethash name gnus-newsrc-hashtb)
6802                                   info (nth 2 entry))
6803                           (if (stringp (setq nth4 (nth 4 info)))
6804                               (setq nth4 (gnus-server-to-method nth4))))
6805                         ;; Only do the xrefs if the group has the same
6806                         ;; select method as the group we have just read.
6807                         (or (gnus-methods-equal-p 
6808                              nth4 (gnus-find-method-for-group from-newsgroup))
6809                             virtual
6810                             (equal nth4 
6811                                    (setq method (gnus-find-method-for-group 
6812                                                  from-newsgroup)))
6813                             (and (equal (car nth4) (car method))
6814                                  (equal (nth 1 nth4) (nth 1 method))))
6815                         gnus-use-cross-reference
6816                         (or (not (eq gnus-use-cross-reference t))
6817                             virtual
6818                             ;; Only do cross-references on subscribed
6819                             ;; groups, if that is what is wanted.  
6820                             (<= (nth 1 info) gnus-level-subscribed)))
6821                    (gnus-group-make-articles-read name idlist expirable))))
6822            xref-hashtb)))))
6823
6824 (defun gnus-group-make-articles-read (group articles expirable)
6825   (let* ((num 0)
6826          (entry (gnus-gethash group gnus-newsrc-hashtb))
6827          (info (nth 2 entry))
6828          (active (gnus-gethash group gnus-active-hashtb))
6829          exps expirable range)
6830     ;; First peel off all illegal article numbers.
6831     (if active
6832         (let ((ids articles)
6833               (ticked (cdr (assq 'tick (nth 3 info))))
6834               (dormant (cdr (assq 'dormant (nth 3 info))))
6835               id)
6836           (setq exps nil)
6837           (while ids
6838             (setq id (car ids))
6839             (if (or (> id (cdr active))
6840                     (< id (car active))
6841                     (memq id ticked)
6842                     (memq id dormant))
6843                 (setq articles (delq id articles)))
6844             (and (memq id expirable)
6845                  (setq exps (cons id exps)))
6846             (setq ids (cdr ids)))))
6847     ;; Update expirable articles.
6848     (gnus-add-marked-articles nil 'expirable exps info)
6849     (and active
6850          (null (nth 2 info))
6851          (> (car active) 1)
6852          (setcar (nthcdr 2 info) (cons 1 (1- (car active)))))
6853     (setcar (nthcdr 2 info)
6854             (setq range
6855                   (gnus-add-to-range 
6856                    (nth 2 info) 
6857                    (setq articles (sort articles '<)))))
6858     ;; Then we have to re-compute how many unread
6859     ;; articles there are in this group.
6860     (if active
6861         (progn
6862           (cond 
6863            ((not range)
6864             (setq num (- (1+ (cdr active)) (car active))))
6865            ((not (listp (cdr range)))
6866             (setq num (- (cdr active) (- (1+ (cdr range)) 
6867                                          (car range)))))
6868            (t
6869             (while range
6870               (if (numberp (car range))
6871                   (setq num (1+ num))
6872                 (setq num (+ num (- (1+ (cdr (car range)))
6873                                     (car (car range))))))
6874               (setq range (cdr range)))
6875             (setq num (- (cdr active) num))))
6876           ;; Update the number of unread articles.
6877           (setcar 
6878            entry 
6879            (max 0 (- num 
6880                      (length (cdr (assq 'tick (nth 3 info))))
6881                      (length 
6882                       (cdr (assq 'dormant (nth 3 info)))))))
6883           ;; Update the group buffer.
6884           (gnus-group-update-group group t)))))
6885
6886 (defun gnus-methods-equal-p (m1 m2)
6887   (let ((m1 (or m1 gnus-select-method))
6888         (m2 (or m2 gnus-select-method)))
6889     (or (equal m1 m2)
6890         (and (eq (car m1) (car m2))
6891              (or (not (memq 'address (assoc (symbol-name (car m1))
6892                                             gnus-valid-select-methods)))
6893                  (equal (nth 1 m1) (nth 1 m2)))))))
6894
6895 (defsubst gnus-header-value ()
6896   (buffer-substring (match-end 0) (gnus-point-at-eol)))
6897
6898 (defvar gnus-newsgroup-none-id 0)
6899
6900 (defun gnus-get-newsgroup-headers ()
6901   (setq gnus-article-internal-prepare-hook nil)
6902   (let ((cur nntp-server-buffer)
6903         (dependencies gnus-newsgroup-dependencies)
6904         headers id dep end ref)
6905     (save-excursion
6906       (set-buffer nntp-server-buffer)
6907       (goto-char (point-min))
6908       ;; Search to the beginning of the next header. Error messages
6909       ;; do not begin with 2 or 3.
6910       (while (re-search-forward "^[23][0-9]+ " nil t)
6911         (let ((header (make-vector 9 nil))
6912               (case-fold-search t)
6913               (p (point))
6914               in-reply-to)
6915           (setq id nil
6916                 ref nil)
6917           (header-set-number header (read cur))
6918           ;; This implementation of this function, with nine
6919           ;; search-forwards instead of the one re-search-forward and
6920           ;; a case (which basically was the old function) is actually
6921           ;; about twice as fast, even though it looks messier. You
6922           ;; can't have everything, I guess. Speed and elegance
6923           ;; doesn't always come hand in hand.
6924           (save-restriction
6925             (narrow-to-region (point) (or (save-excursion 
6926                                             (search-forward "\n.\n" nil t))
6927                                           (point)))
6928             (if (search-forward "\nfrom: " nil t)
6929                 (header-set-from header (gnus-header-value))
6930               (header-set-from header "(nobody)"))
6931             (goto-char p)
6932             (if (search-forward "\nsubject: " nil t)
6933                 (header-set-subject header (gnus-header-value))
6934               (header-set-subject header "(none)"))
6935             (goto-char p)
6936             (and (search-forward "\nxref: " nil t)
6937                  (header-set-xref header (gnus-header-value)))
6938             (goto-char p)
6939             (or (numberp (and (search-forward "\nlines: " nil t)
6940                               (header-set-lines header (read cur))))
6941                 (header-set-lines header 0))
6942             (goto-char p)
6943             (and (search-forward "\ndate: " nil t)
6944                  (header-set-date header (gnus-header-value)))
6945             (goto-char p)
6946             (if (search-forward "\nmessage-id: " nil t)
6947                 (header-set-id header (setq id (gnus-header-value)))
6948               ;; If there was no message-id, we just fake one to make
6949               ;; subsequent routines simpler.
6950               (header-set-id 
6951                header 
6952                (setq id (concat "none+" 
6953                                 (int-to-string 
6954                                  (setq gnus-newsgroup-none-id 
6955                                        (1+ gnus-newsgroup-none-id)))))))
6956             (goto-char p)
6957             (if (search-forward "\nreferences: " nil t)
6958                 (progn
6959                   (header-set-references header (gnus-header-value))
6960                   (setq end (match-end 0))
6961                   (save-excursion
6962                     (setq ref 
6963                           (downcase
6964                            (buffer-substring
6965                             (progn 
6966                               (end-of-line)
6967                               (search-backward ">" end t)
6968                               (1+ (point)))
6969                             (progn
6970                               (search-backward "<" end t)
6971                               (point)))))))
6972               ;; Get the references from the in-reply-to header if there
6973               ;; ware no references and the in-reply-to header looks
6974               ;; promising. 
6975               (if (and (search-forward "\nin-reply-to: " nil t)
6976                        (setq in-reply-to (gnus-header-value))
6977                        (string-match "<[^>]+>" in-reply-to))
6978                   (progn
6979                     (header-set-references 
6980                      header 
6981                      (setq ref (substring in-reply-to (match-beginning 0)
6982                                           (match-end 0))))
6983                     (setq ref (downcase ref)))
6984                 (setq ref "none")))
6985             ;; We do some threading while we read the headers. The
6986             ;; message-id and the last reference are both entered into
6987             ;; the same hash table. Some tippy-toeing around has to be
6988             ;; done in case an article has arrived before the article
6989             ;; which it refers to.
6990             (if (boundp (setq dep (intern (downcase id) dependencies)))
6991                 (if (car (symbol-value dep))
6992                     ;; An article with this Message-ID has already
6993                     ;; been seen, so we ignore this one, except we add
6994                     ;; any additional Xrefs (in case the two articles
6995                     ;; came from different servers.
6996                     (progn
6997                       (header-set-xref 
6998                        (car (symbol-value dep))
6999                        (concat (or (header-xref (car (symbol-value dep))) "")
7000                                (or (header-xref header) "")))
7001                       (setq header nil))
7002                   (setcar (symbol-value dep) header))
7003               (set dep (list header)))
7004             (if header
7005                 (progn
7006                   (if (boundp (setq dep (intern ref dependencies)))
7007                       (setcdr (symbol-value dep) 
7008                               (cons header (cdr (symbol-value dep))))
7009                     (set dep (list nil header)))
7010                   (setq headers (cons header headers))))
7011             (goto-char (point-max))))))
7012     (nreverse headers)))
7013
7014 ;; The following macros and functions were written by Felix Lee
7015 ;; <flee@cse.psu.edu>. 
7016
7017 (defmacro gnus-nov-read-integer ()
7018   '(prog1
7019        (if (= (following-char) ?\t)
7020            0
7021          (let ((num (condition-case nil (read buffer) (error nil))))
7022            (if (numberp num) num 0)))
7023      (or (eobp) (forward-char 1))))
7024
7025 (defmacro gnus-nov-skip-field ()
7026   '(search-forward "\t" eol 'move))
7027
7028 (defmacro gnus-nov-field ()
7029   '(buffer-substring (point) (if (gnus-nov-skip-field) (1- (point)) eol)))
7030
7031 ;; Goes through the xover lines and returns a list of vectors
7032 (defun gnus-get-newsgroup-headers-xover (sequence)
7033   "Parse the news overview data in the server buffer, and return a
7034 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
7035   ;; Get the Xref when the users reads the articles since most/some
7036   ;; NNTP servers do not include Xrefs when using XOVER.
7037   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
7038   (let ((cur nntp-server-buffer)
7039         (dependencies gnus-newsgroup-dependencies)
7040         number headers header)
7041     (save-excursion
7042       (set-buffer nntp-server-buffer)
7043       (goto-char (point-min))
7044       (while (and sequence (not (eobp)))
7045         (setq number (read cur))
7046         (while (and sequence (< (car sequence) number))
7047           (setq sequence (cdr sequence)))
7048         (and sequence 
7049              (eq number (car sequence))
7050              (progn
7051                (setq sequence (cdr sequence))
7052                (if (setq header 
7053                          (inline (gnus-nov-parse-line number dependencies)))
7054                    (setq headers (cons header headers)))))
7055         (forward-line 1))
7056       (setq headers (nreverse headers)))
7057     headers))
7058
7059 ;; This function has to be called with point after the article number
7060 ;; on the beginning of the line.
7061 (defun gnus-nov-parse-line (number dependencies)
7062   (let ((none 0)
7063         (eol (gnus-point-at-eol)) 
7064         (buffer (current-buffer))
7065         header ref id dep)
7066
7067     ;; overview: [num subject from date id refs chars lines misc]
7068     (narrow-to-region (point) eol)
7069     (forward-char)
7070
7071     (condition-case nil
7072         (setq header
7073               (vector 
7074                number                   ; number
7075                (gnus-nov-field)         ; subject
7076                (gnus-nov-field)         ; from
7077                (gnus-nov-field)         ; date
7078                (setq id (or (gnus-nov-field)
7079                             (concat "none+"
7080                                     (int-to-string 
7081                                      (setq none (1+ none)))))) ; id
7082                (progn
7083                  (save-excursion
7084                    (let ((beg (point)))
7085                      (search-forward "\t" eol)
7086                      (if (search-backward ">" beg t)
7087                          (setq ref 
7088                                (downcase 
7089                                 (buffer-substring 
7090                                  (1+ (point))
7091                                  (progn
7092                                    (search-backward "<" beg t)
7093                                    (point)))))
7094                        (setq ref nil))))
7095                  (gnus-nov-field))      ; refs
7096                (gnus-nov-read-integer)  ; chars
7097                (gnus-nov-read-integer)  ; lines
7098                (if (= (following-char) ?\n)
7099                    nil
7100                  (gnus-nov-field))      ; misc
7101                ))
7102       (error (progn 
7103                (ding)
7104                (message "Strange nov line.")
7105                (setq header nil)
7106                (goto-char eol))))
7107
7108     (widen)
7109
7110     ;; We build the thread tree.
7111     (and header
7112          (if (boundp (setq dep (intern (downcase id) dependencies)))
7113              (if (car (symbol-value dep))
7114                  ;; An article with this Message-ID has already been seen,
7115                  ;; so we ignore this one, except we add any additional
7116                  ;; Xrefs (in case the two articles came from different
7117                  ;; servers.
7118                  (progn
7119                    (header-set-xref 
7120                     (car (symbol-value dep))
7121                     (concat (or (header-xref (car (symbol-value dep))) "")
7122                             (or (header-xref header) "")))
7123                    (setq header nil))
7124                (setcar (symbol-value dep) header))
7125            (set dep (list header))))
7126     (if header
7127         (progn
7128           (if (boundp (setq dep (intern (or ref "none") 
7129                                         dependencies)))
7130               (setcdr (symbol-value dep) 
7131                       (cons header (cdr (symbol-value dep))))
7132             (set dep (list nil header)))))
7133     header))
7134
7135 (defun gnus-article-get-xrefs ()
7136   "Fill in the Xref value in `gnus-current-headers', if necessary.
7137 This is meant to be called in `gnus-article-internal-prepare-hook'."
7138   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
7139                                  gnus-current-headers)))
7140     (or (not gnus-use-cross-reference)
7141         (not headers)
7142         (and (header-xref headers)
7143              (not (string= (header-xref headers) "")))
7144         (let ((case-fold-search t)
7145               xref)
7146           (save-restriction
7147             (gnus-narrow-to-headers)
7148             (goto-char (point-min))
7149             (if (or (and (eq (downcase (following-char)) ?x)
7150                          (looking-at "Xref:"))
7151                     (search-forward "\nXref:" nil t))
7152                 (progn
7153                   (goto-char (1+ (match-end 0)))
7154                   (setq xref (buffer-substring (point) 
7155                                                (progn (end-of-line) (point))))
7156                   (header-set-xref headers xref))))))))
7157
7158 (defalias 'gnus-find-header-by-number 'gnus-get-header-by-number)
7159 (make-obsolete 'gnus-find-header-by-number 'gnus-get-header-by-number)
7160
7161 (defun gnus-make-headers-hashtable-by-number ()
7162   "Make hashtable for the variable gnus-newsgroup-headers by number."
7163   (save-excursion
7164     (set-buffer gnus-summary-buffer)
7165     (let ((headers gnus-newsgroup-headers)
7166           header)
7167       (setq gnus-newsgroup-headers-hashtb-by-number
7168             (gnus-make-hashtable (length headers)))
7169       (while headers
7170         (setq header (car headers))
7171         (gnus-sethash (int-to-string (header-number header))
7172                       header gnus-newsgroup-headers-hashtb-by-number)
7173         (setq headers (cdr headers))))))
7174
7175 (defun gnus-more-header-backward ()
7176   "Find new header backward."
7177   (let ((first (car (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
7178         (artnum gnus-newsgroup-begin)
7179         (header nil))
7180     (while (and (not header)
7181                 (> artnum first))
7182       (setq artnum (1- artnum))
7183       (setq header (gnus-read-header artnum)))
7184     header))
7185
7186 (defun gnus-more-header-forward (&optional backward)
7187   "Find new header forward.
7188 If BACKWARD, find new header backward instead."
7189   (if backward
7190       (gnus-more-header-backward)
7191     (let ((last (cdr (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
7192           (artnum gnus-newsgroup-end)
7193           (header nil))
7194       (while (and (not header)
7195                   (< artnum last))
7196         (setq artnum (1+ artnum))
7197         (setq header (gnus-read-header artnum)))
7198       header)))
7199
7200 (defun gnus-extend-newsgroup (header &optional backward)
7201   "Extend newsgroup selection with HEADER.
7202 Optional argument BACKWARD means extend toward backward."
7203   (if header
7204       (let ((artnum (header-number header)))
7205         (setq gnus-newsgroup-headers
7206               (if backward
7207                   (cons header gnus-newsgroup-headers)
7208                 (nconc gnus-newsgroup-headers (list header))))
7209         (setq gnus-newsgroup-unselected
7210               (delq artnum gnus-newsgroup-unselected))
7211         (setq gnus-newsgroup-begin (min gnus-newsgroup-begin artnum))
7212         (setq gnus-newsgroup-end (max gnus-newsgroup-end artnum)))))
7213
7214 (defun gnus-summary-work-articles (n)
7215   "Return a list of articles to be worked upon. The prefix argument,
7216 the list of process marked articles, and the current article will be
7217 taken into consideration."
7218   (let (articles)
7219     (if (and n (numberp n))
7220         (let ((backward (< n 0))
7221               (n (abs n)))
7222           (save-excursion
7223             (while (and (> n 0)
7224                         (setq articles (cons (gnus-summary-article-number) 
7225                                              articles))
7226                         (gnus-summary-search-forward nil nil backward))
7227               (setq n (1- n))))
7228           (sort articles (function <)))
7229       (or (reverse gnus-newsgroup-processable)
7230           (list (gnus-summary-article-number))))))
7231
7232 (defun gnus-summary-search-group (&optional backward use-level)
7233   "Search for next unread newsgroup.
7234 If optional argument BACKWARD is non-nil, search backward instead."
7235   (save-excursion
7236     (set-buffer gnus-group-buffer)
7237     (if (gnus-group-search-forward 
7238          backward nil (if use-level (gnus-group-group-level) nil))
7239         (gnus-group-group-name))))
7240
7241 (defun gnus-summary-best-group (&optional exclude-group)
7242   "Find the name of the best unread group.
7243 If EXCLUDE-GROUP, do not go to this group."
7244   (save-excursion
7245     (set-buffer gnus-group-buffer)
7246     (save-excursion
7247       (gnus-group-best-unread-group exclude-group))))
7248
7249 (defun gnus-subject-equal (s1 s2)
7250   (cond
7251    ((null gnus-summary-gather-subject-limit)
7252     (equal (gnus-simplify-subject-re s1)
7253            (gnus-simplify-subject-re s2)))
7254    ((eq gnus-summary-gather-subject-limit 'fuzzy)
7255     (equal (gnus-simplify-subject-fuzzy s1)
7256            (gnus-simplify-subject-fuzzy s2)))
7257    ((numberp gnus-summary-gather-subject-limit)
7258     (equal (gnus-limit-string s1 gnus-summary-gather-subject-limit)
7259            (gnus-limit-string s2 gnus-summary-gather-subject-limit)))
7260    (t
7261     (equal s1 s2))))
7262     
7263 (defun gnus-summary-search-subject (&optional backward unread subject)
7264   "Search for article forward.
7265 If BACKWARD is non-nil, search backward.
7266 If UNREAD is non-nil, only unread articles are selected.
7267 If SUBJECT is non-nil, the article which has the same subject will be
7268 searched for." 
7269   (let ((func (if backward 'previous-single-property-change
7270                 'next-single-property-change))
7271         (beg (point))
7272         (did t)
7273         pos psubject)
7274     (beginning-of-line)
7275     (and gnus-summary-check-current unread
7276          (eq (get-text-property (point) 'gnus-mark) gnus-unread-mark)
7277          (setq did nil))
7278     (if (not did)
7279         ()
7280       (forward-char (if backward (if (bobp) 0 -1) (if (eobp) 0 1)))
7281       (while
7282           (and 
7283            (setq pos (funcall func (point) 'gnus-number))
7284            (goto-char (if backward (1- pos) pos))
7285            (setq did
7286                  (not (and
7287                        (or (not unread)
7288                            (eq (get-text-property (point) 'gnus-mark)
7289                                gnus-unread-mark))
7290                        (or (not subject)
7291                            (and (setq psubject (inline (gnus-summary-subject-string)))
7292                                 (inline (gnus-subject-equal subject psubject)))))))
7293            (if backward (if (bobp) nil (forward-char -1) t)
7294              (if (eobp) nil (forward-char 1) t)))))
7295     (if did
7296         (progn (goto-char beg) nil)
7297       (prog1
7298           (get-text-property (point) 'gnus-number)
7299         (gnus-summary-position-cursor)))))
7300
7301 (defun gnus-summary-pseudo-article ()
7302   "The thread level of the article on the current line."
7303   (get-text-property (gnus-point-at-bol) 'gnus-pseudo))
7304
7305 (defalias 'gnus-summary-score 'gnus-summary-article-score)
7306 (make-obsolete 'gnus-summary-score 'gnus-summary-article-score)
7307 (defun gnus-summary-article-score ()
7308   "Return current article score."
7309   (or (cdr (assq (gnus-summary-article-number) gnus-newsgroup-scored))
7310       gnus-summary-default-score 0))
7311
7312 (defun gnus-summary-recenter ()
7313   "Center point in the summary window.
7314 If `gnus-auto-center-summary' is nil, or the article buffer isn't
7315 displayed, no centering will be performed." 
7316   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
7317   ;; Recenter only when requested. Suggested by popovich@park.cs.columbia.edu.
7318   (let* ((top (cond ((< (window-height) 4) 0)
7319                     ((< (window-height) 7) 1)
7320                     (t 2)))
7321          (height (1- (window-height)))
7322          (bottom (save-excursion (goto-char (point-max))
7323                                  (forward-line (- height))
7324                                  (point)))
7325          (window (get-buffer-window (current-buffer))))
7326     (and 
7327      ;; The user has to want it,
7328      gnus-auto-center-summary 
7329      ;; the article buffer must be displayed,
7330      (get-buffer-window gnus-article-buffer)
7331      ;; Set the window start to either `bottom', which is the biggest
7332      ;; possible valid number, or the second line from the top,
7333      ;; whichever is the least.
7334      (set-window-start
7335       window (min bottom (save-excursion (forward-line (- top)) (point)))))))
7336
7337 ;; Function written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
7338 (defun gnus-short-group-name (group &optional levels)
7339   "Collapse GROUP name LEVELS."
7340   (let* ((name "") (foreign "") (depth -1) (skip 1)
7341          (levels (or levels
7342                      (progn
7343                        (while (string-match "\\." group skip)
7344                          (setq skip (match-end 0)
7345                                depth (+ depth 1)))
7346                        depth))))
7347     (if (string-match ":" group)
7348         (setq foreign (substring group 0 (match-end 0))
7349               group (substring group (match-end 0))))
7350     (while group
7351       (if (and (string-match "\\." group) (> levels 0))
7352           (setq name (concat name (substring group 0 1))
7353                 group (substring group (match-end 0))
7354                 levels (- levels 1)
7355                 name (concat name "."))
7356         (setq name (concat foreign name group)
7357               group nil)))
7358     name))
7359
7360 (defun gnus-summary-jump-to-group (newsgroup)
7361   "Move point to NEWSGROUP in group mode buffer."
7362   ;; Keep update point of group mode buffer if visible.
7363   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
7364       (save-window-excursion
7365         ;; Take care of tree window mode.
7366         (if (get-buffer-window gnus-group-buffer)
7367             (pop-to-buffer gnus-group-buffer))
7368         (gnus-group-jump-to-group newsgroup))
7369     (save-excursion
7370       ;; Take care of tree window mode.
7371       (if (get-buffer-window gnus-group-buffer)
7372           (pop-to-buffer gnus-group-buffer)
7373         (set-buffer gnus-group-buffer))
7374       (gnus-group-jump-to-group newsgroup))))
7375
7376 ;; This function returns a list of article numbers based on the
7377 ;; difference between the ranges of read articles in this group and
7378 ;; the range of active articles.
7379 (defun gnus-list-of-unread-articles (group)
7380   (let* ((read (nth 2 (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
7381          (active (gnus-gethash group gnus-active-hashtb))
7382          (last (cdr active))
7383          first nlast unread)
7384     ;; If none are read, then all are unread. 
7385     (if (not read)
7386         (setq first (car active))
7387       ;; If the range of read articles is a single range, then the
7388       ;; first unread article is the article after the last read
7389       ;; article. Sounds logical, doesn't it?
7390       (if (not (listp (cdr read)))
7391           (setq first (1+ (cdr read)))
7392         ;; `read' is a list of ranges.
7393         (if (/= (setq nlast (or (and (numberp (car read)) (car read)) 
7394                                 (car (car read)))) 1)
7395             (setq first 1))
7396         (while read
7397           (if first 
7398               (while (< first nlast)
7399                 (setq unread (cons first unread))
7400                 (setq first (1+ first))))
7401           (setq first (1+ (if (atom (car read)) (car read) (cdr (car read)))))
7402           (setq nlast (if (atom (car (cdr read))) 
7403                           (car (cdr read))
7404                         (car (car (cdr read)))))
7405           (setq read (cdr read)))))
7406     ;; And add the last unread articles.
7407     (while (<= first last)
7408       (setq unread (cons first unread))
7409       (setq first (1+ first)))
7410     ;; Return the list of unread articles.
7411     (nreverse unread)))
7412
7413 (defun gnus-list-of-read-articles (group)
7414   (let ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
7415         (active (gnus-gethash group gnus-active-hashtb)))
7416     (and info active
7417          (gnus-sorted-complement 
7418           (gnus-uncompress-range active) 
7419           (gnus-list-of-unread-articles group)))))
7420
7421 ;; Various summary commands
7422
7423 (defun gnus-summary-universal-argument ()
7424   "Perform any operation on all articles marked with the process mark."
7425   (interactive)
7426   (gnus-set-global-variables)
7427   (let ((articles (reverse gnus-newsgroup-processable))
7428         func)
7429     (or articles (error "No articles marked"))
7430     (or (setq func (key-binding (read-key-sequence "C-c C-u")))
7431         (error "Undefined key"))
7432     (while articles
7433       (gnus-summary-goto-subject (car articles))
7434       (command-execute func)
7435       (gnus-summary-remove-process-mark (car articles))
7436       (setq articles (cdr articles)))))
7437
7438 (defun gnus-summary-toggle-truncation (&optional arg)
7439   "Toggle truncation of summary lines.
7440 With arg, turn line truncation on iff arg is positive."
7441   (interactive "P")
7442   (setq truncate-lines
7443         (if (null arg) (not truncate-lines)
7444           (> (prefix-numeric-value arg) 0)))
7445   (redraw-display))
7446
7447 (defun gnus-summary-reselect-current-group (&optional all)
7448   "Once exit and then reselect the current newsgroup.
7449 The prefix argument ALL means to select all articles."
7450   (interactive "P")
7451   (gnus-set-global-variables)
7452   (let ((current-subject (gnus-summary-article-number))
7453         (group gnus-newsgroup-name))
7454     (setq gnus-newsgroup-begin nil)
7455     (gnus-summary-exit t)
7456     ;; We have to adjust the point of group mode buffer because the
7457     ;; current point was moved to the next unread newsgroup by
7458     ;; exiting.
7459     (gnus-summary-jump-to-group group)
7460     (gnus-group-read-group all t)
7461     (gnus-summary-goto-subject current-subject)))
7462
7463 (defun gnus-summary-rescan-group (&optional all)
7464   "Exit the newsgroup, ask for new articles, and select the newsgroup."
7465   (interactive "P")
7466   (gnus-set-global-variables)
7467   ;; Fix by Ilja Weis <kult@uni-paderborn.de>.
7468   (let ((group gnus-newsgroup-name))
7469     (gnus-summary-exit)
7470     (gnus-summary-jump-to-group group)
7471     (save-excursion
7472       (set-buffer gnus-group-buffer)
7473       (gnus-group-get-new-news-this-group 1))
7474     (gnus-summary-jump-to-group group)
7475     (gnus-group-read-group all)))
7476
7477 (defun gnus-summary-update-info ()
7478   (let* ((group gnus-newsgroup-name))
7479     (if gnus-newsgroup-kill-headers
7480         (setq gnus-newsgroup-killed
7481               (gnus-compress-sequence
7482                (nconc
7483                 (gnus-set-sorted-intersection
7484                  (gnus-uncompress-range gnus-newsgroup-killed)
7485                  (setq gnus-newsgroup-unselected
7486                        (sort gnus-newsgroup-unselected '<)))
7487                 (setq gnus-newsgroup-unreads
7488                       (sort gnus-newsgroup-unreads '<))) t)))
7489     (or (listp (cdr gnus-newsgroup-killed))
7490         (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
7491     (let ((headers gnus-newsgroup-headers))
7492       (gnus-close-group group)
7493       (run-hooks 'gnus-exit-group-hook)
7494       (gnus-update-read-articles 
7495        group gnus-newsgroup-unreads gnus-newsgroup-unselected 
7496        gnus-newsgroup-marked
7497        t gnus-newsgroup-replied gnus-newsgroup-expirable
7498        gnus-newsgroup-killed gnus-newsgroup-dormant
7499        gnus-newsgroup-bookmarks 
7500        (and gnus-save-score gnus-newsgroup-scored))
7501       (and gnus-use-cross-reference
7502            (gnus-mark-xrefs-as-read 
7503             group headers gnus-newsgroup-unreads gnus-newsgroup-expirable))
7504       ;; Do adaptive scoring, and possibly save score files.
7505       (and gnus-newsgroup-adaptive
7506            (gnus-score-adaptive))
7507       (and gnus-use-scoring 
7508            (fboundp 'gnus-score-save)
7509            (funcall 'gnus-score-save))
7510       ;; Do not switch windows but change the buffer to work.
7511       (set-buffer gnus-group-buffer)
7512       (or (assoc 'quit-config (gnus-find-method-for-group gnus-newsgroup-name))
7513           (gnus-group-update-group group)))))
7514   
7515 (defun gnus-summary-exit (&optional temporary)
7516   "Exit reading current newsgroup, and then return to group selection mode.
7517 gnus-exit-group-hook is called with no arguments if that value is non-nil."
7518   (interactive)
7519   (gnus-set-global-variables)
7520   (gnus-kill-save-kill-buffer)
7521   (let* ((group gnus-newsgroup-name)
7522          (quit-config (nth 1 (assoc 'quit-config (gnus-find-method-for-group
7523                                                   gnus-newsgroup-name))))
7524          (mode major-mode)
7525          (buf (current-buffer)))
7526     (run-hooks 'gnus-summary-prepare-exit-hook)
7527     (gnus-summary-update-info) ; Make all changes in this group permanent.
7528     (set-buffer buf)
7529     (and gnus-use-cache (gnus-cache-possibly-remove-articles))
7530     ;; Make sure where I was, and go to next newsgroup.
7531     (set-buffer gnus-group-buffer)
7532     (or quit-config
7533         (progn
7534           (gnus-group-jump-to-group group)
7535           (gnus-group-next-unread-group 1)))
7536     (if temporary
7537         nil                             ;Nothing to do.
7538       ;; We set all buffer-local variables to nil. It is unclear why
7539       ;; this is needed, but if we don't, buffer-local variables are
7540       ;; not garbage-collected, it seems. This would the lead to en
7541       ;; ever-growing Emacs.
7542       (set-buffer buf)
7543       (gnus-summary-clear-local-variables)
7544       ;; We clear the global counterparts of the buffer-local
7545       ;; variables as well, just to be on the safe side.
7546       (gnus-configure-windows 'group 'force)
7547       (gnus-summary-clear-local-variables)
7548       ;; Return to group mode buffer. 
7549       (if (eq mode 'gnus-summary-mode)
7550           (gnus-kill-buffer buf))
7551       (if (get-buffer gnus-article-buffer)
7552           (bury-buffer gnus-article-buffer))
7553       (setq gnus-current-select-method gnus-select-method)
7554       (pop-to-buffer gnus-group-buffer)
7555       (if (not quit-config)
7556           (progn
7557             (gnus-group-jump-to-group group)
7558             (gnus-group-next-unread-group 1))
7559         (if (not (buffer-name (car quit-config)))
7560             (gnus-configure-windows 'group 'force)
7561           (set-buffer (car quit-config))
7562           (and (eq major-mode 'gnus-summary-mode)
7563                (gnus-set-global-variables))
7564           (gnus-configure-windows (cdr quit-config))))
7565       (run-hooks 'gnus-summary-exit-hook))))
7566
7567 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
7568 (defun gnus-summary-exit-no-update (&optional no-questions)
7569   "Quit reading current newsgroup without updating read article info."
7570   (interactive)
7571   (gnus-set-global-variables)
7572   (let* ((group gnus-newsgroup-name)
7573          (quit-config (nth 1 (assoc 'quit-config 
7574                                     (gnus-find-method-for-group group)))))
7575     (if (or no-questions
7576             gnus-expert-user
7577             (gnus-y-or-n-p "Do you really wanna quit reading this group? "))
7578         (progn
7579           (gnus-close-group group)
7580           (gnus-summary-clear-local-variables)
7581           (set-buffer gnus-group-buffer)
7582           (gnus-summary-clear-local-variables)
7583           ;; Return to group selection mode.
7584           (gnus-configure-windows 'group 'force)
7585           (if (get-buffer gnus-summary-buffer)
7586               (kill-buffer gnus-summary-buffer))
7587           (if (get-buffer gnus-article-buffer)
7588               (bury-buffer gnus-article-buffer))
7589           (if (equal (gnus-group-group-name) group)
7590               (gnus-group-next-unread-group 1))
7591           (if quit-config
7592               (progn
7593                 (if (not (buffer-name (car quit-config)))
7594                     (gnus-configure-windows 'group 'force)
7595                   (set-buffer (car quit-config))
7596                   (and (eq major-mode 'gnus-summary-mode)
7597                        (gnus-set-global-variables))
7598                   (gnus-configure-windows (cdr quit-config)))))))))
7599
7600 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
7601 (defun gnus-summary-fetch-faq (group)
7602   "Fetch the FAQ for the current group."
7603   (interactive (list gnus-newsgroup-name))
7604   (let ((gnus-faq-buffer 
7605          (find-file (concat gnus-group-faq-directory 
7606                             (gnus-group-real-name group)))))
7607   (and gnus-faq-buffer (gnus-configure-windows 'summary-faq))))
7608
7609 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
7610 (defun gnus-summary-describe-group (&optional force)
7611   "Describe the current newsgroup."
7612   (interactive "P")
7613   (gnus-group-describe-group force gnus-newsgroup-name))
7614
7615 (defun gnus-summary-describe-briefly ()
7616   "Describe summary mode commands briefly."
7617   (interactive)
7618   (gnus-message 6
7619     (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")))
7620
7621 ;; Walking around group mode buffer from summary mode.
7622
7623 (defun gnus-summary-next-group (&optional no-article target-group backward)
7624   "Exit current newsgroup and then select next unread newsgroup.
7625 If prefix argument NO-ARTICLE is non-nil, no article is selected
7626 initially. If NEXT-GROUP, go to this group. If BACKWARD, go to
7627 previous group instead."
7628   (interactive "P")
7629   (gnus-set-global-variables)
7630   (let ((current-group gnus-newsgroup-name)
7631         (current-buffer (current-buffer))
7632         entered)
7633     ;; First we semi-exit this group to update Xrefs and all variables.
7634     ;; We can't do a real exit, because the window conf must remain
7635     ;; the same in case the user is prompted for info, and we don't
7636     ;; want the window conf to change before that...
7637     (gnus-summary-exit t)
7638     (while (not entered)
7639       ;; Then we find what group we are supposed to enter.
7640       (set-buffer gnus-group-buffer)
7641       (gnus-group-jump-to-group current-group)
7642       (setq target-group 
7643             (or target-group        
7644                 (if (eq gnus-keep-same-level 'best) 
7645                     (gnus-summary-best-group gnus-newsgroup-name)
7646                   (gnus-summary-search-group backward gnus-keep-same-level))))
7647       (if (not target-group)
7648           ;; There are no further groups, so we return to the group
7649           ;; buffer.
7650           (progn
7651             (gnus-message 5 "Returning to the group buffer")
7652             (setq entered t)
7653             (set-buffer current-buffer)
7654             (gnus-summary-exit))
7655         ;; We try to enter the target group.
7656         (gnus-group-jump-to-group target-group)
7657         (let ((unreads (gnus-group-group-unread)))
7658           (if (and (or (eq t unreads)
7659                        (and unreads (not (zerop unreads))))
7660                    (gnus-summary-read-group
7661                     target-group nil no-article current-buffer))
7662               (setq entered t)
7663             (setq current-group target-group
7664                   target-group nil)))))))
7665
7666 (defun gnus-summary-next-group-old (&optional no-article group backward)
7667   "Exit current newsgroup and then select next unread newsgroup.
7668 If prefix argument NO-ARTICLE is non-nil, no article is selected initially.
7669 If BACKWARD, go to previous group instead."
7670   (interactive "P")
7671   (gnus-set-global-variables)
7672   (let ((ingroup gnus-newsgroup-name)
7673         (sumbuf (current-buffer))
7674         num)
7675     (set-buffer gnus-group-buffer)
7676     (if (and group
7677              (or (and (numberp (setq num (car (gnus-gethash
7678                                                group gnus-newsrc-hashtb))))
7679                       (< num 1))
7680                  (null num)))
7681         (progn
7682           (gnus-group-jump-to-group group)
7683           (setq group nil))
7684       (gnus-group-jump-to-group ingroup))
7685     (gnus-summary-search-group backward)
7686     (let ((group (or group (gnus-summary-search-group backward))))
7687       (set-buffer sumbuf)
7688       (gnus-summary-exit t)             ;Update all information.
7689       (if (null group)
7690           (gnus-summary-exit-no-update t)
7691         (gnus-group-jump-to-group ingroup)
7692         (setq group (gnus-summary-search-group backward))
7693         (gnus-message 5 "Selecting %s..." group)
7694         (set-buffer gnus-group-buffer)
7695         ;; We are now in group mode buffer.
7696         ;; Make sure group mode buffer point is on GROUP.
7697         (gnus-group-jump-to-group group)
7698         (if (not (eq gnus-auto-select-next 'quietly))
7699             (progn
7700               (gnus-summary-read-group group nil no-article sumbuf)
7701               (and (string= gnus-newsgroup-name ingroup)
7702                    (bufferp sumbuf) (buffer-name sumbuf)
7703                    (progn
7704                      (set-buffer (setq gnus-summary-buffer sumbuf))
7705                      (gnus-summary-exit-no-update t))))
7706           (let ((prevgroup group))
7707             (gnus-group-jump-to-group ingroup)
7708             (setq group (gnus-summary-search-group backward))
7709             (gnus-summary-read-group group nil no-article sumbuf)
7710             (while (and (string= gnus-newsgroup-name ingroup)
7711                         (bufferp sumbuf) 
7712                         (buffer-name sumbuf)
7713                         (not (string= prevgroup (gnus-group-group-name))))
7714               (set-buffer gnus-group-buffer)
7715               (gnus-summary-read-group 
7716                (setq prevgroup (gnus-group-group-name)) 
7717                nil no-article sumbuf))
7718             (and (string= prevgroup (gnus-group-group-name))
7719                  ;; We have reached the final group in the group
7720                  ;; buffer.
7721                  (progn
7722                    (if (buffer-name sumbuf)
7723                        (progn
7724                          (set-buffer sumbuf)
7725                          (gnus-summary-exit)))))))))))
7726
7727 (defun gnus-summary-prev-group (&optional no-article)
7728   "Exit current newsgroup and then select previous unread newsgroup.
7729 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7730   (interactive "P")
7731   (gnus-summary-next-group no-article nil t))
7732
7733 ;; Walking around summary lines.
7734
7735 (defun gnus-summary-first-subject (&optional unread)
7736   "Go to the first unread subject.
7737 If UNREAD is non-nil, go to the first unread article.
7738 Returns nil if there are no unread articles."
7739   (interactive "P")
7740   (prog1
7741       (cond ((not unread)
7742              (goto-char (point-min)))
7743             ((gnus-goto-char 
7744               (text-property-any 
7745                (point-min) (point-max) 'gnus-mark gnus-unread-mark))
7746              t)
7747             (t 
7748              ;; There are no unread articles.
7749              (gnus-message 3 "No more unread articles")
7750              nil))
7751     (gnus-summary-position-cursor)))
7752
7753 (defun gnus-summary-next-subject (n &optional unread dont-display)
7754   "Go to next N'th summary line.
7755 If N is negative, go to the previous N'th subject line.
7756 If UNREAD is non-nil, only unread articles are selected.
7757 The difference between N and the actual number of steps taken is
7758 returned."
7759   (interactive "p")
7760   (let ((backward (< n 0))
7761         (n (abs n)))
7762     (while (and (> n 0)
7763                 (gnus-summary-search-forward unread nil backward))
7764       (setq n (1- n)))
7765     (if (/= 0 n) (gnus-message 7 "No more%s articles"
7766                                (if unread " unread" "")))
7767     (or dont-display
7768         (progn
7769           (gnus-summary-recenter)
7770           (gnus-summary-position-cursor)))
7771   n))
7772
7773 (defun gnus-summary-next-unread-subject (n)
7774   "Go to next N'th unread summary line."
7775   (interactive "p")
7776   (gnus-summary-next-subject n t))
7777
7778 (defun gnus-summary-prev-subject (n &optional unread)
7779   "Go to previous N'th summary line.
7780 If optional argument UNREAD is non-nil, only unread article is selected."
7781   (interactive "p")
7782   (gnus-summary-next-subject (- n) unread))
7783
7784 (defun gnus-summary-prev-unread-subject (n)
7785   "Go to previous N'th unread summary line."
7786   (interactive "p")
7787   (gnus-summary-next-subject (- n) t))
7788
7789 (defun gnus-summary-goto-subject (article)
7790   "Go the subject line of ARTICLE."
7791   (interactive
7792    (list
7793     (string-to-int
7794      (completing-read "Article number: "
7795                       (mapcar
7796                        (lambda (headers)
7797                          (list
7798                           (int-to-string (header-number headers))))
7799                        gnus-newsgroup-headers)
7800                       nil 'require-match))))
7801   (or article (error "No article number"))
7802   (let ((b (point)))
7803     (if (not (gnus-goto-char (text-property-any (point-min) (point-max)
7804                                                 'gnus-number article)))
7805         ()
7806       (gnus-summary-show-thread)
7807       ;; Skip dummy articles. 
7808       (if (eq (gnus-summary-article-mark) gnus-dummy-mark)
7809           (forward-line 1))
7810       (prog1
7811           (if (not (eobp))
7812               article
7813             (goto-char b)
7814             nil)
7815         (gnus-summary-position-cursor)))))
7816
7817 ;; Walking around summary lines with displaying articles.
7818
7819 (defun gnus-summary-expand-window (&optional arg)
7820   "Make the summary buffer take up the entire Emacs frame.
7821 Given a prefix, will force an `article' buffer configuration."
7822   (interactive "P")
7823   (gnus-set-global-variables)
7824   (if arg
7825       (gnus-configure-windows 'article 'force)
7826     (gnus-configure-windows 'summary 'force)))
7827
7828 (defun gnus-summary-display-article (article &optional all-header)
7829   "Display ARTICLE in article buffer."
7830   (gnus-set-global-variables)
7831   (if (null article)
7832       nil
7833     (prog1
7834         (gnus-article-prepare article all-header)
7835       (gnus-summary-show-thread)
7836       (if (eq (gnus-summary-article-mark) gnus-dummy-mark)
7837           (progn
7838             (forward-line 1)
7839             (gnus-summary-position-cursor)))
7840       (run-hooks 'gnus-select-article-hook)
7841       (gnus-summary-recenter)
7842       (gnus-summary-goto-subject article)
7843       ;; Successfully display article.
7844       (gnus-summary-update-line)
7845       (gnus-article-set-window-start 
7846        (cdr (assq article gnus-newsgroup-bookmarks)))
7847       t)))
7848
7849 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7850   "Select the current article.
7851 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7852 non-nil, the article will be re-fetched even if it already present in
7853 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7854 be displayed."
7855   (and (not pseudo) (gnus-summary-pseudo-article)
7856        (error "This is a pseudo-article."))
7857   (let ((article (or article (gnus-summary-article-number)))
7858         (all-headers (not (not all-headers))) ;Must be T or NIL.
7859         did) 
7860     (prog1
7861         (save-excursion
7862           (set-buffer gnus-summary-buffer)
7863           (if (or (null gnus-current-article)
7864                   (null gnus-article-current)
7865                   (null (get-buffer gnus-article-buffer))
7866                   (not (eq article (cdr gnus-article-current)))
7867                   (not (equal (car gnus-article-current) gnus-newsgroup-name))
7868                   force)
7869               ;; The requested article is different from the current article.
7870               (progn
7871                 (gnus-summary-display-article article all-headers)
7872                 (setq did article))
7873             (if (or all-headers gnus-show-all-headers) 
7874                 (gnus-article-show-all-headers))
7875             nil))
7876       (if did 
7877           (gnus-article-set-window-start 
7878            (cdr (assq article gnus-newsgroup-bookmarks)))))))
7879
7880 (defun gnus-summary-set-current-mark (&optional current-mark)
7881   "Obsolete function."
7882   nil)
7883
7884 (defun gnus-summary-next-article (&optional unread subject backward)
7885   "Select the next article.
7886 If UNREAD, only unread articles are selected.
7887 If SUBJECT, only articles with SUBJECT are selected.
7888 If BACKWARD, the previous article is selected instead of the next."
7889   (interactive "P")
7890   (gnus-set-global-variables)
7891   (let (header)
7892     (cond
7893      ;; Is there such an article?
7894      ((or (gnus-summary-display-article 
7895            (gnus-summary-search-forward unread subject backward))
7896           (eq (gnus-summary-article-mark) gnus-canceled-mark))
7897       (gnus-summary-position-cursor))
7898      ;; If not, we try the first unread, if that is wanted.
7899      ((and subject
7900            gnus-auto-select-same
7901            (gnus-summary-first-unread-article))
7902       (gnus-message 6 "Wrapped"))
7903      ;; Try to get next/previous article not displayed in this group.
7904      ((and gnus-auto-extend-newsgroup
7905            (not unread) (not subject)
7906            (setq header (gnus-more-header-forward backward)))
7907       (gnus-extend-newsgroup header backward)
7908       (let ((buffer-read-only nil))
7909         (goto-char (if backward (point-min) (point-max)))
7910         (gnus-summary-prepare-threads (list header)))
7911       (gnus-summary-goto-article (if backward gnus-newsgroup-begin
7912                                    gnus-newsgroup-end)))
7913      ;; Go to next/previous group.
7914      (t
7915       (or (assoc 'quit-config (gnus-find-method-for-group gnus-newsgroup-name))
7916           (gnus-summary-jump-to-group gnus-newsgroup-name))
7917       (let ((cmd (aref (this-command-keys) 0))
7918             (group 
7919              (if (eq gnus-keep-same-level 'best) 
7920                  (gnus-summary-best-group gnus-newsgroup-name)
7921                (gnus-summary-search-group backward gnus-keep-same-level))))
7922         ;; For some reason, the group window gets selected. We change
7923         ;; it back.  
7924         (select-window (get-buffer-window (current-buffer)))
7925         ;; Keep just the event type of CMD.
7926         (and (listp cmd) (setq cmd (car cmd)))
7927         ;; Select next unread newsgroup automagically.
7928         (cond 
7929          ((not gnus-auto-select-next)
7930           (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7931          ((eq gnus-auto-select-next 'quietly)
7932           ;; Select quietly.
7933           (if (assoc 'quit-config (gnus-find-method-for-group 
7934                                    gnus-newsgroup-name))
7935               (gnus-summary-exit)
7936             (gnus-message 7 "No more%s articles (%s)..."
7937                           (if unread " unread" "") 
7938                           (if group (concat "selecting " group)
7939                             "exiting"))
7940             (gnus-summary-next-group nil group backward)))
7941          (t
7942           (let ((keystrokes '(?\C-n ?\C-p))
7943                 key)
7944             (while (or (null key) (memq key keystrokes))
7945               (gnus-message 
7946                7 "No more%s articles%s" (if unread " unread" "")
7947                (if (and group (not (assoc 'quit-config
7948                                           (gnus-find-method-for-group 
7949                                            gnus-newsgroup-name))))
7950                    (format " (Type %s for %s [%s])"
7951                            (single-key-description cmd) group
7952                            (car (gnus-gethash group gnus-newsrc-hashtb)))
7953                  (format " (Type %s to exit %s)"
7954                          (single-key-description cmd)
7955                          gnus-newsgroup-name)))
7956               ;; Confirm auto selection.
7957               (let* ((event (read-event)))
7958                 (setq key (if (listp event) (car event) event))
7959                 (if (memq key keystrokes)
7960                     (let ((obuf (current-buffer)))
7961                       (switch-to-buffer gnus-group-buffer)
7962                       (gnus-group-jump-to-group group)
7963                       (execute-kbd-macro (char-to-string key))
7964                       (setq group (gnus-group-group-name))
7965                       (switch-to-buffer obuf)))))
7966             (if (equal key cmd)
7967                 (if (or (not group) (assoc 'quit-config
7968                                            (gnus-find-method-for-group
7969                                             gnus-newsgroup-name)))
7970                     (gnus-summary-exit)
7971                   (gnus-summary-next-group nil group backward))
7972               (setq unread-command-events (list key)))))))))))
7973
7974 (defun gnus-summary-next-unread-article ()
7975   "Select unread article after current one."
7976   (interactive)
7977   (gnus-summary-next-article t (and gnus-auto-select-same
7978                                     (gnus-summary-subject-string))))
7979
7980 (defun gnus-summary-prev-article (&optional unread subject)
7981   "Select the article after the current one.
7982 If UNREAD is non-nil, only unread articles are selected."
7983   (interactive "P")
7984   (gnus-summary-next-article unread subject t))
7985
7986 (defun gnus-summary-prev-unread-article ()
7987   "Select unred article before current one."
7988   (interactive)
7989   (gnus-summary-prev-article t (and gnus-auto-select-same
7990                                     (gnus-summary-subject-string))))
7991
7992 (defun gnus-summary-next-page (&optional lines circular)
7993   "Show next page of selected article.
7994 If end of article, select next article.
7995 Argument LINES specifies lines to be scrolled up.
7996 If CIRCULAR is non-nil, go to the start of the article instead of 
7997 instead of selecting the next article when reaching the end of the
7998 current article." 
7999   (interactive "P")
8000   (setq gnus-summary-buffer (current-buffer))
8001   (gnus-set-global-variables)
8002   (let ((article (gnus-summary-article-number))
8003         (endp nil))
8004     (gnus-configure-windows 'article)
8005     (if (or (null gnus-current-article)
8006             (null gnus-article-current)
8007             (/= article (cdr gnus-article-current))
8008             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
8009         ;; Selected subject is different from current article's.
8010         (gnus-summary-display-article article)
8011       (gnus-eval-in-buffer-window
8012        gnus-article-buffer
8013        (setq endp (gnus-article-next-page lines)))
8014       (if endp
8015           (cond (circular
8016                  (gnus-summary-beginning-of-article))
8017                 (lines
8018                  (gnus-message 3 "End of message"))
8019                 ((null lines)
8020                  (gnus-summary-next-unread-article)))))
8021     (gnus-summary-recenter)
8022     (gnus-summary-position-cursor)))
8023
8024 (defun gnus-summary-prev-page (&optional lines)
8025   "Show previous page of selected article.
8026 Argument LINES specifies lines to be scrolled down."
8027   (interactive "P")
8028   (gnus-set-global-variables)
8029   (let ((article (gnus-summary-article-number)))
8030     (gnus-configure-windows 'article)
8031     (if (or (null gnus-current-article)
8032             (null gnus-article-current)
8033             (/= article (cdr gnus-article-current))
8034             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
8035         ;; Selected subject is different from current article's.
8036         (gnus-summary-display-article article)
8037       (gnus-summary-recenter)
8038       (gnus-eval-in-buffer-window gnus-article-buffer
8039         (gnus-article-prev-page lines))))
8040   (gnus-summary-position-cursor))
8041
8042 (defun gnus-summary-scroll-up (lines)
8043   "Scroll up (or down) one line current article.
8044 Argument LINES specifies lines to be scrolled up (or down if negative)."
8045   (interactive "p")
8046   (gnus-set-global-variables)
8047   (gnus-configure-windows 'article)
8048   (or (gnus-summary-select-article nil nil 'pseudo)
8049       (gnus-eval-in-buffer-window 
8050        gnus-article-buffer
8051        (cond ((> lines 0)
8052               (if (gnus-article-next-page lines)
8053                   (gnus-message 3 "End of message")))
8054              ((< lines 0)
8055               (gnus-article-prev-page (- lines))))))
8056   (gnus-summary-recenter)
8057   (gnus-summary-position-cursor))
8058
8059 (defun gnus-summary-next-same-subject ()
8060   "Select next article which has the same subject as current one."
8061   (interactive)
8062   (gnus-set-global-variables)
8063   (gnus-summary-next-article nil (gnus-summary-subject-string)))
8064
8065 (defun gnus-summary-prev-same-subject ()
8066   "Select previous article which has the same subject as current one."
8067   (interactive)
8068   (gnus-set-global-variables)
8069   (gnus-summary-prev-article nil (gnus-summary-subject-string)))
8070
8071 (defun gnus-summary-next-unread-same-subject ()
8072   "Select next unread article which has the same subject as current one."
8073   (interactive)
8074   (gnus-set-global-variables)
8075   (gnus-summary-next-article t (gnus-summary-subject-string)))
8076
8077 (defun gnus-summary-prev-unread-same-subject ()
8078   "Select previous unread article which has the same subject as current one."
8079   (interactive)
8080   (gnus-set-global-variables)
8081   (gnus-summary-prev-article t (gnus-summary-subject-string)))
8082
8083 (defun gnus-summary-first-unread-article ()
8084   "Select the first unread article. 
8085 Return nil if there are no unread articles."
8086   (interactive)
8087   (gnus-set-global-variables)
8088   (prog1
8089       (if (gnus-summary-first-subject t)
8090           (gnus-summary-display-article (gnus-summary-article-number)))
8091     (gnus-summary-position-cursor)))
8092
8093 (defun gnus-summary-best-unread-article ()
8094   "Select the unread article with the highest score."
8095   (interactive)
8096   (gnus-set-global-variables)
8097   (let ((scored gnus-newsgroup-scored)
8098         (best -1000000)
8099         article art)
8100     (while scored
8101       (or (> best (cdr (car scored)))
8102           (and (memq (setq art (car (car scored))) gnus-newsgroup-unreads)
8103                (not (memq art gnus-newsgroup-marked))
8104                (not (memq art gnus-newsgroup-dormant))
8105                (if (= best (cdr (car scored)))
8106                    (setq article (min art article))
8107                  (setq article art)
8108                  (setq best (cdr (car scored))))))
8109       (setq scored (cdr scored)))
8110     (if article 
8111         (gnus-summary-goto-article article)
8112       (gnus-summary-first-unread-article))
8113     (gnus-summary-position-cursor)))
8114
8115 (defun gnus-summary-goto-article (article &optional all-headers)
8116   "Fetch ARTICLE and display it if it exists.
8117 If ALL-HEADERS is non-nil, no header lines are hidden."
8118   (interactive
8119    (list
8120     (string-to-int
8121      (completing-read 
8122       "Article number: "
8123       (mapcar (lambda (headers) (list (int-to-string (header-number headers))))
8124               gnus-newsgroup-headers) 
8125       nil 'require-match))))
8126   (prog1
8127       (and (gnus-summary-goto-subject article)
8128            (gnus-summary-display-article article all-headers))
8129     (gnus-summary-position-cursor)))
8130
8131 (defun gnus-summary-goto-last-article ()
8132   "Go to the previously read article."
8133   (interactive)
8134   (prog1
8135       (and gnus-last-article
8136            (gnus-summary-goto-article gnus-last-article))
8137     (gnus-summary-position-cursor)))
8138
8139 (defun gnus-summary-pop-article (number)
8140   "Pop one article off the history and go to the previous.
8141 NUMBER articles will be popped off."
8142   (interactive "p")
8143   (let (to)
8144     (setq gnus-newsgroup-history
8145           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
8146     (if to
8147         (gnus-summary-goto-article (car to))
8148       (error "Article history empty")))
8149   (gnus-summary-position-cursor))
8150
8151 ;; Summary article oriented commands
8152
8153 (defun gnus-summary-refer-parent-article (n)
8154   "Refer parent article N times.
8155 The difference between N and the number of articles fetched is returned."
8156   (interactive "p")
8157   (gnus-set-global-variables)
8158   (while 
8159       (and 
8160        (> n 0)
8161        (let ((ref (header-references (gnus-get-header-by-num
8162                                       (gnus-summary-article-number)))))
8163          (if (and ref (not (equal ref ""))
8164                   (string-match "<[^<>]*>[ \t]*$" ref))
8165              (gnus-summary-refer-article 
8166               (substring ref (match-beginning 0) (match-end 0)))
8167            (gnus-message 1 "No references in article %d"
8168                          (gnus-summary-article-number))
8169            nil)))
8170     (setq n (1- n)))
8171   (gnus-summary-position-cursor)
8172   n)
8173     
8174 (defun gnus-summary-refer-article (message-id)
8175   "Refer article specified by MESSAGE-ID.
8176 NOTE: This command only works with newsgroups that use real or simulated NNTP."
8177   (interactive "sMessage-ID: ")
8178   (if (or (not (stringp message-id))
8179           (zerop (length message-id)))
8180       ()
8181     ;; Construct the correct Message-ID if necessary.
8182     ;; Suggested by tale@pawl.rpi.edu.
8183     (or (string-match "^<" message-id)
8184         (setq message-id (concat "<" message-id)))
8185     (or (string-match ">$" message-id)
8186         (setq message-id (concat message-id ">")))
8187     (let ((header (car (gnus-gethash (downcase message-id)
8188                                      gnus-newsgroup-dependencies))))
8189       (if header
8190           (or (gnus-summary-goto-article (header-number header))
8191               ;; The header has been read, but the article had been
8192               ;; expunged, so we insert it again.
8193               (progn
8194                 (gnus-summary-insert-line
8195                  nil header 0 nil gnus-read-mark nil nil
8196                  (header-subject header))
8197                 (forward-line -1)
8198                 (header-number header)))
8199         (let ((gnus-override-method gnus-refer-article-method)
8200               (gnus-ancient-mark gnus-read-mark)
8201               (tmp-point (window-start
8202                           (get-buffer-window gnus-article-buffer)))
8203               number tmp-buf)
8204           (and gnus-refer-article-method
8205                (or (gnus-server-opened gnus-refer-article-method)
8206                    (gnus-open-server gnus-refer-article-method)))
8207           ;; Save the old article buffer.
8208           (save-excursion
8209             (set-buffer gnus-article-buffer)
8210             (gnus-kill-buffer " *temp Article*")
8211             (setq tmp-buf (rename-buffer " *temp Article*")))
8212           (prog1
8213               (if (gnus-article-prepare 
8214                    message-id nil (gnus-read-header message-id))
8215                   (progn
8216                     (setq number (header-number gnus-current-headers))
8217                     (gnus-rebuild-thread message-id)
8218                     (gnus-summary-goto-subject number)
8219                     (gnus-summary-recenter)
8220                     (gnus-article-set-window-start 
8221                      (cdr (assq number gnus-newsgroup-bookmarks)))
8222                     message-id)
8223                 ;; We restore the old article buffer.
8224                 (save-excursion
8225                   (kill-buffer gnus-article-buffer)
8226                   (set-buffer tmp-buf)
8227                   (rename-buffer gnus-article-buffer)
8228                   (let ((buffer-read-only nil))
8229                     (and tmp-point
8230                          (set-window-start (get-buffer-window (current-buffer))
8231                                            tmp-point)))))))))))
8232
8233 (defun gnus-summary-enter-digest-group ()
8234   "Enter a digest group based on the current article."
8235   (interactive)
8236   (gnus-set-global-variables)
8237   (gnus-summary-select-article)
8238   ;; We do not want a narrowed article.
8239   (gnus-summary-stop-page-breaking)
8240   (let ((name (format "%s-%d" 
8241                       (gnus-group-prefixed-name 
8242                        gnus-newsgroup-name (list 'nndoc "")) 
8243                       gnus-current-article))
8244         (ogroup gnus-newsgroup-name)
8245         (buf (current-buffer)))
8246     (if (gnus-group-read-ephemeral-group 
8247          name (list 'nndoc name
8248                     (list 'nndoc-address (get-buffer gnus-article-buffer))
8249                     '(nndoc-article-type digest))
8250          t)
8251         (setcdr (nthcdr 4 (nth 2 (gnus-gethash name gnus-newsrc-hashtb)))
8252                 (list (list (cons 'to-group ogroup))))
8253       (switch-to-buffer buf)
8254       (gnus-set-global-variables)
8255       (gnus-configure-windows 'summary)
8256       (gnus-message 3 "Article not a digest?"))))
8257
8258 (defun gnus-summary-isearch-article ()
8259   "Do incremental search forward on current article."
8260   (interactive)
8261   (gnus-set-global-variables)
8262   (gnus-summary-select-article)
8263   (gnus-eval-in-buffer-window 
8264    gnus-article-buffer (isearch-forward)))
8265
8266 (defun gnus-summary-search-article-forward (regexp &optional backward)
8267   "Search for an article containing REGEXP forward.
8268 If BACKWARD, search backward instead."
8269   (interactive
8270    (list (read-string
8271           (format "Search article %s (regexp%s): "
8272                   (if current-prefix-arg "backward" "forward")
8273                   (if gnus-last-search-regexp
8274                       (concat ", default " gnus-last-search-regexp)
8275                     "")))
8276          current-prefix-arg))
8277   (gnus-set-global-variables)
8278   (if (string-equal regexp "")
8279       (setq regexp (or gnus-last-search-regexp ""))
8280     (setq gnus-last-search-regexp regexp))
8281   (if (gnus-summary-search-article regexp backward)
8282       (gnus-article-set-window-start 
8283        (cdr (assq (gnus-summary-article-number) gnus-newsgroup-bookmarks)))
8284     (error "Search failed: \"%s\"" regexp)))
8285
8286 (defun gnus-summary-search-article-backward (regexp)
8287   "Search for an article containing REGEXP backward."
8288   (interactive
8289    (list (read-string
8290           (format "Search article backward (regexp%s): "
8291                   (if gnus-last-search-regexp
8292                       (concat ", default " gnus-last-search-regexp)
8293                     "")))))
8294   (gnus-summary-search-article-forward regexp 'backward))
8295
8296 (defun gnus-summary-search-article (regexp &optional backward)
8297   "Search for an article containing REGEXP.
8298 Optional argument BACKWARD means do search for backward.
8299 gnus-select-article-hook is not called during the search."
8300   (let ((gnus-select-article-hook nil)  ;Disable hook.
8301         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8302         (re-search
8303          (if backward
8304              (function re-search-backward) (function re-search-forward)))
8305         (found nil)
8306         (last nil))
8307     ;; Hidden thread subtrees must be searched for ,too.
8308     (gnus-summary-show-all-threads)
8309     (if (eobp) (forward-line -1))
8310     ;; First of all, search current article.
8311     ;; We don't want to read article again from NNTP server nor reset
8312     ;; current point.
8313     (gnus-summary-select-article)
8314     (gnus-message 9 "Searching article: %d..." gnus-current-article)
8315     (setq last gnus-current-article)
8316     (gnus-eval-in-buffer-window gnus-article-buffer
8317       (save-restriction
8318         (widen)
8319         ;; Begin search from current point.
8320         (setq found (funcall re-search regexp nil t))))
8321     ;; Then search next articles.
8322     (while (and (not found)
8323                 (gnus-summary-display-article 
8324                  (gnus-summary-search-subject backward nil nil)))
8325       (gnus-message 9 "Searching article: %d..." gnus-current-article)
8326       (gnus-eval-in-buffer-window gnus-article-buffer
8327         (save-restriction
8328           (widen)
8329           (goto-char (if backward (point-max) (point-min)))
8330           (setq found (funcall re-search regexp nil t)))))
8331     (message "")
8332     ;; Adjust article pointer.
8333     (or (eq last gnus-current-article)
8334         (setq gnus-last-article last))
8335     ;; Return T if found such article.
8336     found))
8337
8338 (defun gnus-summary-execute-command (header regexp command &optional backward)
8339   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8340 If HEADER is an empty string (or nil), the match is done on the entire
8341 article. If BACKWARD (the prefix) is non-nil, search backward instead."
8342   (interactive
8343    (list (let ((completion-ignore-case t))
8344            (completing-read 
8345             "Header name: "
8346             (mapcar (lambda (string) (list string))
8347                     '("Number" "Subject" "From" "Lines" "Date"
8348                       "Message-ID" "Xref" "References"))
8349             nil 'require-match))
8350          (read-string "Regexp: ")
8351          (read-key-sequence "Command: ")
8352          current-prefix-arg))
8353   (gnus-set-global-variables)
8354   ;; Hidden thread subtrees must be searched as well.
8355   (gnus-summary-show-all-threads)
8356   ;; We don't want to change current point nor window configuration.
8357   (save-excursion
8358     (save-window-excursion
8359       (gnus-message 6 "Executing %s..." (key-description command))
8360       ;; We'd like to execute COMMAND interactively so as to give arguments.
8361       (gnus-execute header regexp
8362                     (` (lambda ()
8363                          (call-interactively '(, (key-binding command)))))
8364                     backward)
8365       (gnus-message 6 "Executing %s...done" (key-description command)))))
8366
8367 (defun gnus-summary-beginning-of-article ()
8368   "Scroll the article back to the beginning."
8369   (interactive)
8370   (gnus-set-global-variables)
8371   (gnus-summary-select-article)
8372   (gnus-configure-windows 'article)
8373   (gnus-eval-in-buffer-window
8374    gnus-article-buffer
8375    (widen)
8376    (goto-char (point-min))
8377    (and gnus-break-pages (gnus-narrow-to-page))))
8378
8379 (defun gnus-summary-end-of-article ()
8380   "Scroll to the end of the article."
8381   (interactive)
8382   (gnus-set-global-variables)
8383   (gnus-summary-select-article)
8384   (gnus-configure-windows 'article)
8385   (gnus-eval-in-buffer-window 
8386    gnus-article-buffer
8387    (widen)
8388    (goto-char (point-max))
8389    (and gnus-break-pages (gnus-narrow-to-page))))
8390
8391 (defun gnus-summary-show-article (&optional no-refetch)
8392   "Force re-fetching of the current article.
8393 If the prefix argument NO-REFETCH is non-nil, no actual refetch will
8394 be performed.  The current article will simply be redisplayed."
8395   (interactive "P")
8396   (gnus-set-global-variables)
8397   (if (not no-refetch)
8398       (gnus-summary-select-article gnus-have-all-headers t)
8399     (or gnus-current-article
8400         (error "There is no current article"))
8401     (gnus-summary-goto-subject gnus-current-article)
8402     (gnus-configure-windows 'article)
8403     (gnus-summary-position-cursor)))
8404
8405 (defun gnus-summary-verbose-headers (&optional arg)
8406   "Toggle permanent full header display.
8407 If ARG is a positive number, turn header display on.
8408 If ARG is a negative number, turn header display off."
8409   (interactive "P")
8410   (gnus-set-global-variables)
8411   (gnus-summary-toggle-header arg)
8412   (setq gnus-show-all-headers
8413         (cond ((or (not (numberp arg))
8414                    (zerop arg))
8415                (not gnus-show-all-headers))
8416               ((natnump arg)
8417                t))))
8418
8419 (defun gnus-summary-toggle-header (&optional arg)
8420   "Show the headers if they are hidden, or hide them if they are shown.
8421 If ARG is a positive number, show the entire header.
8422 If ARG is a negative number, hide the unwanted header lines."
8423   (interactive "P")
8424   (gnus-set-global-variables)
8425   (save-excursion
8426     (set-buffer gnus-article-buffer)
8427     (let ((buffer-read-only nil))
8428       (if (numberp arg) 
8429           (if (> arg 0) (remove-text-properties (point-min) (point-max) 
8430                                                 gnus-hidden-properties)
8431             (if (< arg 0) (run-hooks 'gnus-article-display-hook)))
8432         (if (text-property-any (point-min) (point-max) 'invisible t)
8433             (remove-text-properties 
8434              (point-min) (point-max) gnus-hidden-properties)
8435           ;; We hide the headers. This song and dance act below is
8436           ;; done because `gnus-have-all-headers' is buffer-local to
8437           ;; the summary buffer, and we only want to temporarily
8438           ;; change it in that buffer. Ugh.
8439           (let ((have gnus-have-all-headers))
8440             (save-excursion
8441               (set-buffer gnus-summary-buffer)
8442               (setq gnus-have-all-headers nil)
8443               (save-excursion
8444                 (set-buffer gnus-article-buffer)
8445                 (run-hooks 'gnus-article-display-hook))
8446               (setq gnus-have-all-headers have)))))
8447       (set-window-point (get-buffer-window (current-buffer)) (point-min)))))
8448
8449 (defun gnus-summary-show-all-headers ()
8450   "Make all header lines visible."
8451   (interactive)
8452   (gnus-set-global-variables)
8453   (gnus-article-show-all-headers))
8454
8455 (defun gnus-summary-toggle-mime (&optional arg)
8456   "Toggle MIME processing.
8457 If ARG is a positive number, turn MIME processing on."
8458   (interactive "P")
8459   (gnus-set-global-variables)
8460   (setq gnus-show-mime
8461         (if (null arg) (not gnus-show-mime)
8462           (> (prefix-numeric-value arg) 0)))
8463   (gnus-summary-select-article t 'force))
8464
8465 (defun gnus-summary-caesar-message (&optional arg)
8466   "Caesar rotate the current article by 13.
8467 The numerical prefix specifies how manu places to rotate each letter
8468 forward."
8469   (interactive "P")
8470   (gnus-set-global-variables)
8471   (gnus-summary-select-article)
8472   (let ((mail-header-separator ""))
8473     (gnus-eval-in-buffer-window 
8474      gnus-article-buffer
8475      (save-restriction
8476        (widen)
8477        (let ((last (point)))
8478          (news-caesar-buffer-body arg)
8479          (goto-char last)
8480          (recenter 0))))))
8481
8482 (defun gnus-summary-stop-page-breaking ()
8483   "Stop page breaking in the current article."
8484   (interactive)
8485   (gnus-set-global-variables)
8486   (gnus-summary-select-article)
8487   (gnus-eval-in-buffer-window gnus-article-buffer (widen)))
8488
8489 ;; Suggested by Brian Edmonds <bedmonds@prodigy.bc.ca>.
8490
8491 (defun gnus-summary-move-article (&optional n to-newsgroup select-method)
8492   "Move the current article to a different newsgroup.
8493 If N is a positive number, move the N next articles.
8494 If N is a negative number, move the N previous articles.
8495 If N is nil and any articles have been marked with the process mark,
8496 move those articles instead.
8497 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to. 
8498 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
8499 re-spool using this method.
8500 For this function to work, both the current newsgroup and the
8501 newsgroup that you want to move to have to support the `request-move'
8502 and `request-accept' functions. (Ie. mail newsgroups at present.)"
8503   (interactive "P")
8504   (gnus-set-global-variables)
8505   (or (gnus-check-backend-function 'request-move-article gnus-newsgroup-name)
8506       (error "The current newsgroup does not support article moving"))
8507   (let ((articles (gnus-summary-work-articles n))
8508         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
8509         art-group to-method sel-met)
8510     (if (and (not to-newsgroup) (not select-method))
8511         (setq to-newsgroup
8512               (completing-read 
8513                (format "Where do you want to move %s? %s"
8514                        (if (> (length articles) 1)
8515                            (format "these %d articles" (length articles))
8516                          "this article")
8517                        (if gnus-current-move-group
8518                            (format "(%s default) " gnus-current-move-group)
8519                          ""))
8520                gnus-active-hashtb nil nil prefix)))
8521     (if to-newsgroup
8522         (progn
8523           (if (or (string= to-newsgroup "") (string= to-newsgroup prefix))
8524               (setq to-newsgroup (or gnus-current-move-group "")))
8525           (or (gnus-gethash to-newsgroup gnus-active-hashtb)
8526               (gnus-activate-newsgroup to-newsgroup)
8527               (error "No such group: %s" to-newsgroup))
8528           (setq gnus-current-move-group to-newsgroup)))
8529     (setq to-method (if select-method (list select-method "")
8530                       (gnus-find-method-for-group to-newsgroup)))
8531     (or (gnus-check-backend-function 'request-accept-article (car to-method))
8532         (error "%s does not support article copying" (car to-method)))
8533     (or (gnus-server-opened to-method)
8534         (gnus-open-server to-method)
8535         (error "Can't open server %s" (car to-method)))
8536     (gnus-message 6 "Moving to %s: %s..." 
8537                   (or select-method to-newsgroup) articles)
8538     (while articles
8539       (if (setq art-group
8540                 (gnus-request-move-article 
8541                  (car articles)                   ; Article to move
8542                  gnus-newsgroup-name              ; From newsgrouo
8543                  (nth 1 (gnus-find-method-for-group 
8544                          gnus-newsgroup-name))    ; Server
8545                  (list 'gnus-request-accept-article 
8546                        (if select-method
8547                            (list 'quote select-method)
8548                          to-newsgroup)
8549                        (not (cdr articles)))     ; Accept form
8550                  (not (cdr articles))))          ; Only save nov last time
8551           (let* ((buffer-read-only nil)
8552                  (entry 
8553                   (or
8554                    (gnus-gethash (car art-group) gnus-newsrc-hashtb)
8555                    (gnus-gethash 
8556                     (gnus-group-prefixed-name 
8557                      (car art-group) 
8558                      (if select-method (list select-method "")
8559                        (gnus-find-method-for-group to-newsgroup)))
8560                     gnus-newsrc-hashtb)))
8561                  (info (nth 2 entry))
8562                  (article (car articles)))
8563             (gnus-summary-goto-subject article)
8564             (beginning-of-line)
8565             (delete-region (point)
8566                            (progn (forward-line 1) (point)))
8567             (if (not (memq article gnus-newsgroup-unreads))
8568                 (setcar (cdr (cdr info))
8569                         (gnus-add-to-range (nth 2 info) 
8570                                            (list (cdr art-group)))))
8571             ;; Copy any marks over to the new group.
8572             (let ((marks '((tick . gnus-newsgroup-marked)
8573                            (dormant . gnus-newsgroup-dormant)
8574                            (expire . gnus-newsgroup-expirable)
8575                            (bookmark . gnus-newsgroup-bookmarks)
8576                         ;   (score . gnus-newsgroup-scored)
8577                            (reply . gnus-newsgroup-replied)))
8578                   (to-article (cdr art-group)))
8579               (while marks
8580                 (if (memq article (symbol-value (cdr (car marks))))
8581                     (gnus-add-marked-articles 
8582                      (car info) (car (car marks)) (list to-article) info))
8583                 (setq marks (cdr marks))))
8584             (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8585             (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8586             (setq gnus-newsgroup-dormant
8587                   (delq article gnus-newsgroup-dormant)))
8588         (gnus-message 1 "Couldn't move article %s" (car articles)))
8589       (gnus-summary-remove-process-mark (car articles))
8590       (setq articles (cdr articles)))))
8591
8592 (defun gnus-summary-respool-article (&optional n respool-method)
8593   "Respool the current article.
8594 The article will be squeezed through the mail spooling process again,
8595 which means that it will be put in some mail newsgroup or other
8596 depending on `nnmail-split-methods'.
8597 If N is a positive number, respool the N next articles.
8598 If N is a negative number, respool the N previous articles.
8599 If N is nil and any articles have been marked with the process mark,
8600 respool those articles instead.
8601
8602 Respooling can be done both from mail groups and \"real\" newsgroups.
8603 In the former case, the articles in question will be moved from the
8604 current group into whatever groups they are destined to.  In the
8605 latter case, they will be copied into the relevant groups."
8606   (interactive "P")
8607   (gnus-set-global-variables)
8608   (let ((respool-methods (gnus-methods-using 'respool))
8609         (methname 
8610          (symbol-name (car (gnus-find-method-for-group gnus-newsgroup-name)))))
8611     (or respool-method
8612         (setq respool-method
8613               (completing-read
8614                "What method do you want to use when respooling? "
8615                respool-methods nil t methname)))
8616     (or (string= respool-method "")
8617         (if (assoc (symbol-name
8618                     (car (gnus-find-method-for-group gnus-newsgroup-name)))
8619                    respool-methods)
8620             (gnus-summary-move-article n nil (intern respool-method))
8621           (gnus-summary-copy-article n nil (intern respool-method))))))
8622
8623 ;; Suggested by gregj@unidata.com (Gregory J. Grubbs).
8624 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
8625   "Move the current article to a different newsgroup.
8626 If N is a positive number, move the N next articles.
8627 If N is a negative number, move the N previous articles.
8628 If N is nil and any articles have been marked with the process mark,
8629 move those articles instead.
8630 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to. 
8631 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
8632 re-spool using this method.
8633 For this function to work, the newsgroup that you want to move to have
8634 to support the `request-move' and `request-accept'
8635 functions. (Ie. mail newsgroups at present.)"
8636   (interactive "P")
8637   (gnus-set-global-variables)
8638   (let ((articles (gnus-summary-work-articles n))
8639         (copy-buf (get-buffer-create "*copy work*"))
8640         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
8641         art-group to-method)
8642     (buffer-disable-undo copy-buf)
8643     (if (and (not to-newsgroup) (not select-method))
8644         (setq to-newsgroup
8645               (completing-read 
8646                (format "Where do you want to copy %s? %s"
8647                        (if (> (length articles) 1)
8648                            (format "these %d articles" (length articles))
8649                          "this article")
8650                        (if gnus-current-move-group
8651                            (format "(%s default) " gnus-current-move-group)
8652                          ""))
8653                gnus-active-hashtb nil nil prefix)))
8654     (if to-newsgroup
8655         (progn
8656           (if (or (string= to-newsgroup "") (string= to-newsgroup prefix))
8657               (setq to-newsgroup (or gnus-current-move-group "")))
8658           (or (gnus-gethash to-newsgroup gnus-active-hashtb)
8659               (gnus-activate-newsgroup to-newsgroup)
8660               (error "No such group: %s" to-newsgroup))
8661           (setq gnus-current-move-group to-newsgroup)))
8662     (setq to-method (if select-method (list select-method "")
8663                       (gnus-find-method-for-group to-newsgroup)))
8664     (or (gnus-check-backend-function 'request-accept-article (car to-method))
8665         (error "%s does not support article copying" (car to-method)))
8666     (or (gnus-server-opened to-method)
8667         (gnus-open-server to-method)
8668         (error "Can't open server %s" (car to-method)))
8669     (while articles
8670       (gnus-message 6 "Copying to %s: %s..." 
8671                     (or select-method to-newsgroup) articles)
8672       (if (setq art-group
8673                 (save-excursion
8674                   (set-buffer copy-buf)
8675                   (gnus-request-article-this-buffer
8676                    (car articles) gnus-newsgroup-name)
8677                   (gnus-request-accept-article
8678                    (if select-method (quote select-method) to-newsgroup)
8679                    (not (cdr articles)))))
8680           (let* ((entry 
8681                   (or
8682                    (gnus-gethash (car art-group) gnus-newsrc-hashtb)
8683                    (gnus-gethash 
8684                     (gnus-group-prefixed-name 
8685                      (car art-group) 
8686                      (if select-method (list select-method "")
8687                        (gnus-find-method-for-group to-newsgroup)))
8688                     gnus-newsrc-hashtb)))
8689                  (info (nth 2 entry))
8690                  (article (car articles)))
8691             (if (not (memq article gnus-newsgroup-unreads))
8692                 (setcar (cdr (cdr info))
8693                         (gnus-add-to-range (nth 2 info) 
8694                                            (list (cdr art-group)))))
8695             ;; Copy any marks over to the new group.
8696             (let ((marks '((tick . gnus-newsgroup-marked)
8697                            (dormant . gnus-newsgroup-dormant)
8698                            (expire . gnus-newsgroup-expirable)
8699                            (bookmark . gnus-newsgroup-bookmarks)
8700                         ;   (score . gnus-newsgroup-scored)
8701                            (reply . gnus-newsgroup-replied)))
8702                   (to-article (cdr art-group)))
8703               (while marks
8704                 (if (memq article (symbol-value (cdr (car marks))))
8705                     (gnus-add-marked-articles 
8706                      (car info) (car (car marks)) (list to-article) info))
8707                 (setq marks (cdr marks)))))
8708         (gnus-message 1 "Couldn't copy article %s" (car articles)))
8709       (gnus-summary-remove-process-mark (car articles))
8710       (setq articles (cdr articles)))
8711     (kill-buffer copy-buf)))
8712
8713 (defun gnus-summary-import-article (file)
8714   "Import a random file into a mail newsgroup."
8715   (interactive "fImport file: ")
8716   (let ((group gnus-newsgroup-name)
8717         atts)
8718     (or (gnus-check-backend-function 'request-accept-article group)
8719         (error "%s does not support article importing" group))
8720     (or (file-readable-p file)
8721         (not (file-regular-p file))
8722         (error "Can't read %s" file))
8723     (save-excursion
8724       (set-buffer (get-buffer-create " *import file*"))
8725       (buffer-disable-undo (current-buffer))
8726       (erase-buffer)
8727       (insert-file-contents file)
8728       (goto-char (point-min))
8729       (if (nnheader-article-p)
8730           ()
8731         (setq atts (file-attributes file))
8732         (insert "From: " (read-string "From: ") "\n"
8733                 "Subject: " (read-string "Subject: ") "\n"
8734                 "Date: " (current-time-string (nth 5 atts)) "\n"
8735                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8736       (gnus-request-accept-article group t)
8737       (kill-buffer (current-buffer)))))
8738
8739 (defun gnus-summary-expire-articles ()
8740   "Expire all articles that are marked as expirable in the current group."
8741   (interactive)
8742   (if (not (gnus-check-backend-function 
8743             'request-expire-articles gnus-newsgroup-name))
8744       ()
8745     (let* ((info (nth 2 (gnus-gethash gnus-newsgroup-name 
8746                                       gnus-newsrc-hashtb)))
8747            (total (memq 'total-expire (nth 5 info)))
8748            (expirable (if total
8749                           (gnus-list-of-read-articles gnus-newsgroup-name)
8750                         (setq gnus-newsgroup-expirable
8751                               (sort gnus-newsgroup-expirable '<))))
8752            es)
8753       (if (not expirable)
8754           ()
8755         (gnus-message 6 "Expiring articles...")
8756         ;; The list of articles that weren't expired is returned.
8757         (setq es (gnus-request-expire-articles expirable gnus-newsgroup-name))
8758         (or total (setq gnus-newsgroup-expirable es))
8759         ;; We go through the old list of expirable, and mark all
8760         ;; really expired articles as non-existant.
8761         (or (eq es expirable) ; If nothing was expired, we don't mark.
8762             (let ((gnus-use-cache nil))
8763               (while expirable
8764                 (or (memq (car expirable) es)
8765                     (gnus-summary-mark-article
8766                      (car expirable) gnus-canceled-mark))
8767                 (setq expirable (cdr expirable)))))
8768         (gnus-message 6 "Expiring articles...done")))))
8769
8770 (defun gnus-summary-expire-articles-now ()
8771   "Expunge all expirable articles in the current group.
8772 This means that *all* articles that are marked as expirable will be
8773 deleted forever, right now."
8774   (interactive)
8775   (or gnus-expert-user
8776       (gnus-y-or-n-p
8777        "Are you really, really, really sure you want to expunge? ")
8778       (error "Phew!"))
8779   (let ((nnmail-expiry-wait -1)
8780         (nnmail-expiry-wait-function nil))
8781     (gnus-summary-expire-articles)))
8782
8783 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
8784 (defun gnus-summary-delete-article (&optional n)
8785   "Delete the N next (mail) articles.
8786 This command actually deletes articles. This is not a marking
8787 command. The article will disappear forever from you life, never to
8788 return. 
8789 If N is negative, delete backwards.
8790 If N is nil and articles have been marked with the process mark,
8791 delete these instead."
8792   (interactive "P")
8793   (or (gnus-check-backend-function 'request-expire-articles 
8794                                    gnus-newsgroup-name)
8795       (error "The current newsgroup does not support article deletion."))
8796   ;; Compute the list of articles to delete.
8797   (let ((articles (gnus-summary-work-articles n))
8798         not-deleted)
8799     (if (and gnus-novice-user
8800              (not (gnus-y-or-n-p 
8801                    (format "Do you really want to delete %s forever? "
8802                            (if (> (length articles) 1) "these articles"
8803                              "this article")))))
8804         ()
8805       ;; Delete the articles.
8806       (setq not-deleted (gnus-request-expire-articles 
8807                          articles gnus-newsgroup-name 'force))
8808       (while articles
8809         (gnus-summary-remove-process-mark (car articles))       
8810         ;; The backend might not have been able to delete the article
8811         ;; after all.  
8812         (or (memq (car articles) not-deleted)
8813             (gnus-summary-mark-article (car articles) gnus-canceled-mark))
8814         (setq articles (cdr articles))))
8815     (gnus-summary-position-cursor)
8816     not-deleted))
8817
8818 (defun gnus-summary-edit-article ()
8819   "Enter into a buffer and edit the current article.
8820 This will have permanent effect only in mail groups."
8821   (interactive)
8822   (or (gnus-check-backend-function 
8823        'request-replace-article gnus-newsgroup-name)
8824       (error "The current newsgroup does not support article editing."))
8825   (gnus-summary-select-article t)
8826   (gnus-configure-windows 'article)
8827   (select-window (get-buffer-window gnus-article-buffer))
8828   (gnus-message 6 "C-c C-c to end edits")
8829   (setq buffer-read-only nil)
8830   (text-mode)
8831   (use-local-map (copy-keymap (current-local-map)))
8832   (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
8833   (buffer-enable-undo)
8834   (widen)
8835   (goto-char (point-min))
8836   (search-forward "\n\n" nil t))
8837
8838 (defun gnus-summary-edit-article-done ()
8839   "Make edits to the current article permanent."
8840   (interactive)
8841   (let ((buf (buffer-substring-no-properties (point-min) (point-max))))
8842     (erase-buffer)
8843     (insert buf)
8844     (if (not (gnus-request-replace-article 
8845               (cdr gnus-article-current) (car gnus-article-current) 
8846               (current-buffer)))
8847         (error "Couldn't replace article.")
8848       (gnus-article-mode)
8849       (use-local-map gnus-article-mode-map)
8850       (setq buffer-read-only t)
8851       (buffer-disable-undo (current-buffer))
8852       (gnus-configure-windows 'summary))))
8853
8854 (defun gnus-summary-fancy-query ()
8855   "Query where the fancy respool algorithm would put this article."
8856   (interactive)
8857   (gnus-summary-select-article)
8858   (save-excursion
8859     (set-buffer gnus-article-buffer)
8860     (save-restriction
8861       (goto-char (point-min))
8862       (search-forward "\n\n")
8863       (narrow-to-region (point-min) (point))
8864       (pp-eval-expression (list 'quote (nnmail-split-fancy))))))
8865
8866 ;; Summary score commands.
8867
8868 ;; Suggested by boubaker@cenatls.cena.dgac.fr.
8869
8870 (defun gnus-summary-raise-score (n)
8871   "Raise the score of the current article by N."
8872   (interactive "p")
8873   (gnus-summary-set-score (+ (gnus-summary-article-score) n)))
8874
8875 (defun gnus-summary-set-score (n)
8876   "Set the score of the current article to N."
8877   (interactive "p")
8878   ;; Skip dummy header line.
8879   (save-excursion
8880     (gnus-summary-show-thread)
8881     (if (eq (gnus-summary-article-mark) gnus-dummy-mark)
8882         (forward-line 1))
8883     (let ((buffer-read-only nil))
8884       ;; Set score.
8885       (gnus-summary-update-mark
8886        (if (= n (or gnus-summary-default-score 0)) ? 
8887          (if (< n (or gnus-summary-default-score 0)) 
8888              gnus-score-below-mark gnus-score-over-mark)) 'score))
8889     (let* ((article (gnus-summary-article-number))
8890            (score (assq article gnus-newsgroup-scored)))
8891       (if score (setcdr score n)
8892         (setq gnus-newsgroup-scored 
8893               (cons (cons article n) gnus-newsgroup-scored))))
8894     (gnus-summary-update-line)))
8895
8896 (defun gnus-summary-current-score ()
8897   "Return the score of the current article."
8898   (interactive)
8899   (message "%s" (gnus-summary-article-score)))
8900
8901 ;; Summary marking commands.
8902
8903 (defun gnus-summary-raise-same-subject-and-select (score)
8904   "Raise articles which has the same subject with SCORE and select the next."
8905   (interactive "p")
8906   (let ((subject (gnus-summary-subject-string)))
8907     (gnus-summary-raise-score score)
8908     (while (gnus-summary-search-subject nil nil subject)
8909       (gnus-summary-raise-score score))
8910     (gnus-summary-next-article t)))
8911
8912 (defun gnus-summary-raise-same-subject (score)
8913   "Raise articles which has the same subject with SCORE."
8914   (interactive "p")
8915   (let ((subject (gnus-summary-subject-string)))
8916     (gnus-summary-raise-score score)
8917     (while (gnus-summary-search-subject nil nil subject)
8918       (gnus-summary-raise-score score))
8919     (gnus-summary-next-subject 1 t)))
8920
8921 (defun gnus-score-default (level)
8922   (if level (prefix-numeric-value level) 
8923     gnus-score-interactive-default-score))
8924
8925 (defun gnus-summary-raise-thread (&optional score)
8926   "Raise the score of the articles in the current thread with SCORE."
8927   (interactive "P")
8928   (setq score (1- (gnus-score-default score)))
8929   (let (e)
8930     (save-excursion
8931       (let ((level (gnus-summary-thread-level)))
8932         (gnus-summary-raise-score score)
8933         (while (and (zerop (gnus-summary-next-subject 1 nil t))
8934                     (> (gnus-summary-thread-level) level))
8935           (gnus-summary-raise-score score))
8936         (setq e (point))))
8937     (let ((gnus-summary-check-current t))
8938       (or (zerop (gnus-summary-next-subject 1 t))
8939           (goto-char e))))
8940   (gnus-summary-recenter)
8941   (gnus-summary-position-cursor)
8942   (gnus-set-mode-line 'summary))
8943
8944 (defun gnus-summary-lower-same-subject-and-select (score)
8945   "Raise articles which has the same subject with SCORE and select the next."
8946   (interactive "p")
8947   (gnus-summary-raise-same-subject-and-select (- score)))
8948
8949 (defun gnus-summary-lower-same-subject (score)
8950   "Raise articles which has the same subject with SCORE."
8951   (interactive "p")
8952   (gnus-summary-raise-same-subject (- score)))
8953
8954 (defun gnus-summary-lower-thread (&optional score)
8955   "Lower score of articles in the current thread with SCORE."
8956   (interactive "P")
8957   (gnus-summary-raise-thread (- (1- (gnus-score-default score)))))
8958
8959 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
8960   "Mark articles which has the same subject as read, and then select the next.
8961 If UNMARK is positive, remove any kind of mark.
8962 If UNMARK is negative, tick articles."
8963   (interactive "P")
8964   (if unmark
8965       (setq unmark (prefix-numeric-value unmark)))
8966   (let ((count
8967          (gnus-summary-mark-same-subject
8968           (gnus-summary-subject-string) unmark)))
8969     ;; Select next unread article. If auto-select-same mode, should
8970     ;; select the first unread article.
8971     (gnus-summary-next-article t (and gnus-auto-select-same
8972                                       (gnus-summary-subject-string)))
8973     (gnus-message 7 "%d articles are marked as %s"
8974                   count (if unmark "unread" "read"))))
8975
8976 (defun gnus-summary-kill-same-subject (&optional unmark)
8977   "Mark articles which has the same subject as read. 
8978 If UNMARK is positive, remove any kind of mark.
8979 If UNMARK is negative, tick articles."
8980   (interactive "P")
8981   (if unmark
8982       (setq unmark (prefix-numeric-value unmark)))
8983   (let ((count
8984          (gnus-summary-mark-same-subject
8985           (gnus-summary-subject-string) unmark)))
8986     ;; If marked as read, go to next unread subject.
8987     (if (null unmark)
8988         ;; Go to next unread subject.
8989         (gnus-summary-next-subject 1 t))
8990     (gnus-message 7 "%d articles are marked as %s"
8991                   count (if unmark "unread" "read"))))
8992
8993 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8994   "Mark articles with same SUBJECT as read, and return marked number.
8995 If optional argument UNMARK is positive, remove any kinds of marks.
8996 If optional argument UNMARK is negative, mark articles as unread instead."
8997   (let ((count 1))
8998     (save-excursion
8999       (cond 
9000        ((null unmark)                   ; Mark as read.
9001         (while (and 
9002                 (progn
9003                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9004                   (gnus-summary-show-thread) t)
9005                 (gnus-summary-search-forward nil subject))
9006           (setq count (1+ count))))
9007        ((> unmark 0)                    ; Tick.
9008         (while (and
9009                 (progn
9010                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9011                   (gnus-summary-show-thread) t)
9012                 (gnus-summary-search-forward nil subject))
9013           (setq count (1+ count))))
9014        (t                               ; Mark as unread.
9015         (while (and
9016                 (progn
9017                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9018                   (gnus-summary-show-thread) t)
9019                 (gnus-summary-search-forward nil subject))
9020           (setq count (1+ count)))))
9021       ;; Return the number of marked articles.
9022       count)))
9023
9024 (defun gnus-summary-mark-as-processable (n &optional unmark)
9025   "Set the process mark on the next N articles.
9026 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9027 the process mark instead.  The difference between N and the actual
9028 number of articles marked is returned."
9029   (interactive "p")
9030   (let ((backward (< n 0))
9031         (n (abs n)))
9032   (while (and 
9033           (> n 0)
9034           (if unmark
9035               (gnus-summary-remove-process-mark (gnus-summary-article-number))
9036             (gnus-summary-set-process-mark (gnus-summary-article-number)))
9037           (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9038     (setq n (1- n)))
9039   (if (/= 0 n) (gnus-message 7 "No more articles"))
9040   (gnus-summary-recenter)
9041   (gnus-summary-position-cursor)
9042   n))
9043
9044 (defun gnus-summary-unmark-as-processable (n)
9045   "Remove the process mark from the next N articles.
9046 If N is negative, mark backward instead.  The difference between N and
9047 the actual number of articles marked is returned."
9048   (interactive "p")
9049   (gnus-summary-mark-as-processable n t))
9050
9051 (defun gnus-summary-unmark-all-processable ()
9052   "Remove the process mark from all articles."
9053   (interactive)
9054   (save-excursion
9055     (while gnus-newsgroup-processable
9056       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9057   (gnus-summary-position-cursor))
9058
9059 (defun gnus-summary-mark-as-expirable (n)
9060   "Mark N articles forward as expirable.
9061 If N is negative, mark backward instead. The difference between N and
9062 the actual number of articles marked is returned."
9063   (interactive "p")
9064   (gnus-summary-mark-forward n gnus-expirable-mark))
9065
9066 (defun gnus-summary-mark-article-as-replied (article)
9067   "Mark ARTICLE replied and update the summary line."
9068   (setq gnus-newsgroup-replied (cons article gnus-newsgroup-replied))
9069   (let ((buffer-read-only nil))
9070     (if (gnus-summary-goto-subject article)
9071         (progn
9072           (gnus-summary-update-mark gnus-replied-mark 'replied)
9073           t))))
9074
9075 (defun gnus-summary-set-bookmark (article)
9076   "Set a bookmark in current article."
9077   (interactive (list (gnus-summary-article-number)))
9078   (if (or (not (get-buffer gnus-article-buffer))
9079           (not gnus-current-article)
9080           (not gnus-article-current)
9081           (not (equal gnus-newsgroup-name (car gnus-article-current))))
9082       (error "No current article selected"))
9083   ;; Remove old bookmark, if one exists.
9084   (let ((old (assq article gnus-newsgroup-bookmarks)))
9085     (if old (setq gnus-newsgroup-bookmarks 
9086                   (delq old gnus-newsgroup-bookmarks))))
9087   ;; Set the new bookmark, which is on the form 
9088   ;; (article-number . line-number-in-body).
9089   (setq gnus-newsgroup-bookmarks 
9090         (cons 
9091          (cons article 
9092                (save-excursion
9093                  (set-buffer gnus-article-buffer)
9094                  (count-lines
9095                   (min (point)
9096                        (save-excursion
9097                          (goto-char (point-min))
9098                          (search-forward "\n\n" nil t)
9099                          (point)))
9100                   (point))))
9101          gnus-newsgroup-bookmarks))
9102   (gnus-message 6 "A bookmark has been added to the current article."))
9103
9104 (defun gnus-summary-remove-bookmark (article)
9105   "Remove the bookmark from the current article."
9106   (interactive (list (gnus-summary-article-number)))
9107   ;; Remove old bookmark, if one exists.
9108   (let ((old (assq article gnus-newsgroup-bookmarks)))
9109     (if old 
9110         (progn
9111           (setq gnus-newsgroup-bookmarks 
9112                 (delq old gnus-newsgroup-bookmarks))
9113           (gnus-message 6 "Removed bookmark."))
9114       (gnus-message 6 "No bookmark in current article."))))
9115
9116 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9117 (defun gnus-summary-mark-as-dormant (n)
9118   "Mark N articles forward as dormant.
9119 If N is negative, mark backward instead.  The difference between N and
9120 the actual number of articles marked is returned."
9121   (interactive "p")
9122   (gnus-summary-mark-forward n gnus-dormant-mark))
9123
9124 (defun gnus-summary-set-process-mark (article)
9125   "Set the process mark on ARTICLE and update the summary line."
9126   (setq gnus-newsgroup-processable (cons article gnus-newsgroup-processable))
9127   (let ((buffer-read-only nil))
9128     (if (gnus-summary-goto-subject article)
9129         (progn
9130           (gnus-summary-show-thread)
9131           (and (eq (gnus-summary-article-mark) gnus-dummy-mark)
9132                (forward-line 1))
9133           (gnus-summary-update-mark gnus-process-mark 'replied)
9134           t))))
9135
9136 (defun gnus-summary-remove-process-mark (article)
9137   "Remove the process mark from ARTICLE and update the summary line."
9138   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9139   (let ((buffer-read-only nil))
9140     (if (gnus-summary-goto-subject article)
9141         (progn
9142           (gnus-summary-show-thread)
9143           (and (eq (gnus-summary-article-mark) gnus-dummy-mark)
9144                (forward-line 1))
9145           (gnus-summary-update-mark ?  'replied)
9146           (if (memq article gnus-newsgroup-replied) 
9147               (gnus-summary-update-mark gnus-replied-mark 'replied))
9148           t))))
9149
9150 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9151   "Mark N articles as read forwards.
9152 If N is negative, mark backwards instead.
9153 Mark with MARK. If MARK is ? , ?! or ??, articles will be
9154 marked as unread. 
9155 The difference between N and the actual number of articles marked is
9156 returned."
9157   (interactive "p")
9158   (gnus-set-global-variables)
9159   (let ((backward (< n 0))
9160         (gnus-summary-goto-unread
9161          (and gnus-summary-goto-unread
9162               (not (memq mark (list gnus-unread-mark
9163                                     gnus-ticked-mark gnus-dormant-mark)))))
9164         (n (abs n))
9165         (mark (or mark gnus-del-mark)))
9166   (while (and (> n 0)
9167               (gnus-summary-mark-article nil mark no-expire)
9168               (zerop (gnus-summary-next-subject 
9169                       (if backward -1 1) gnus-summary-goto-unread t)))
9170     (setq n (1- n)))
9171   (if (/= 0 n) (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9172   (gnus-summary-recenter)
9173   (gnus-summary-position-cursor)
9174   (gnus-set-mode-line 'summary)
9175   n))
9176
9177 (defun gnus-summary-mark-article-as-read (mark)
9178   "Mark the current article quickly as read with MARK."
9179   (let ((article (gnus-summary-article-number)))
9180     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9181     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9182     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9183     ;; Possibly remove from cache, if that is used. 
9184     (and gnus-use-cache (gnus-cache-enter-remove-article article))
9185     (and gnus-newsgroup-auto-expire 
9186          (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
9187              (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
9188              (= mark gnus-read-mark))
9189          (progn
9190            (setq mark gnus-expirable-mark)
9191            (setq gnus-newsgroup-expirable 
9192                  (cons article gnus-newsgroup-expirable))))
9193     (while (eq (gnus-summary-article-mark) gnus-dummy-mark)
9194       (forward-line 1))
9195     ;; Fix the mark.
9196     (gnus-summary-update-mark mark 'unread)
9197     t))
9198
9199 (defun gnus-summary-mark-article-as-unread (mark)
9200   "Mark the current article quickly as unread with MARK."
9201   (let ((article (gnus-summary-article-number)))
9202     (or (memq article gnus-newsgroup-unreads)
9203         (setq gnus-newsgroup-unreads (cons article gnus-newsgroup-unreads)))
9204     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9205     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9206     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9207     (if (= mark gnus-ticked-mark)
9208         (setq gnus-newsgroup-marked (cons article gnus-newsgroup-marked)))
9209     (if (= mark gnus-dormant-mark)
9210         (setq gnus-newsgroup-dormant (cons article gnus-newsgroup-dormant)))
9211
9212     ;; See whether the article is to be put in the cache.
9213     (and gnus-use-cache
9214          (vectorp (gnus-get-header-by-num article))
9215          (save-excursion
9216            (gnus-cache-possibly-enter-article 
9217             gnus-newsgroup-name article 
9218             (gnus-get-header-by-num article)
9219             (= mark gnus-ticked-mark)
9220             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9221
9222     (while (eq (gnus-summary-article-mark) gnus-dummy-mark)
9223       (forward-line 1))
9224     ;; Fix the mark.
9225     (gnus-summary-update-mark mark 'unread)
9226     t))
9227
9228 (defun gnus-summary-mark-article (&optional article mark no-expire)
9229   "Mark ARTICLE with MARK.  MARK can be any character.
9230 Four MARK strings are reserved: `? ' (unread), `?!' (ticked), `??'
9231 (dormant) and `?E' (expirable).
9232 If MARK is nil, then the default character `?D' is used.
9233 If ARTICLE is nil, then the article on the current line will be
9234 marked." 
9235   (and (stringp mark)
9236        (setq mark (aref mark 0)))
9237   ;; If no mark is given, then we check auto-expiring.
9238   (and (not no-expire)
9239        gnus-newsgroup-auto-expire 
9240        (or (not mark)
9241            (and (numberp mark) 
9242                 (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
9243                     (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
9244                     (= mark gnus-read-mark))))
9245        (setq mark gnus-expirable-mark))
9246   (let* ((mark (or mark gnus-del-mark))
9247          (article (or article (gnus-summary-article-number))))
9248     (or article (error "No article on current line"))
9249     (if (or (= mark gnus-unread-mark) 
9250             (= mark gnus-ticked-mark) 
9251             (= mark gnus-dormant-mark))
9252         (gnus-mark-article-as-unread article mark)
9253       (gnus-mark-article-as-read article mark))
9254
9255     ;; See whether the article is to be put in the cache.
9256     (and gnus-use-cache
9257          (not (= mark gnus-canceled-mark))
9258          (vectorp (gnus-get-header-by-num article))
9259          (save-excursion
9260            (gnus-cache-possibly-enter-article 
9261             gnus-newsgroup-name article 
9262             (gnus-get-header-by-num article)
9263             (= mark gnus-ticked-mark)
9264             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9265
9266     (if (gnus-summary-goto-subject article)
9267         (let ((buffer-read-only nil))
9268           (gnus-summary-show-thread)
9269           (and (eq (gnus-summary-article-mark) gnus-dummy-mark)
9270                (forward-line 1))
9271           ;; Fix the mark.
9272           (gnus-summary-update-mark mark 'unread)
9273           t))))
9274
9275 (defun gnus-summary-update-mark (mark type)
9276   (beginning-of-line)
9277   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
9278         (buffer-read-only nil)
9279         plist)
9280     (if (not forward)
9281         ()
9282       (forward-char forward)
9283       (setq plist (text-properties-at (point)))
9284       (delete-char 1)
9285       (insert mark)
9286       (and plist (add-text-properties (1- (point)) (point) plist))
9287       (and (eq type 'unread)
9288            (add-text-properties (1- (point)) (point) (list 'gnus-mark mark)))
9289       (gnus-summary-update-line (eq mark gnus-unread-mark)))))
9290   
9291 (defun gnus-mark-article-as-read (article &optional mark)
9292   "Enter ARTICLE in the pertinent lists and remove it from others."
9293   ;; Make the article expirable.
9294   (let ((mark (or mark gnus-del-mark)))
9295     (if (= mark gnus-expirable-mark)
9296         (setq gnus-newsgroup-expirable (cons article gnus-newsgroup-expirable))
9297       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
9298     ;; Remove from unread and marked lists.
9299     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9300     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9301     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9302     ;; Possibly remove from cache, if that is used. 
9303     (and gnus-use-cache (gnus-cache-enter-remove-article article))))
9304
9305 (defun gnus-mark-article-as-unread (article &optional mark)
9306   "Enter ARTICLE in the pertinent lists and remove it from others."
9307   (let ((mark (or mark gnus-ticked-mark)))
9308     ;; Add to unread list.
9309     (or (memq article gnus-newsgroup-unreads)
9310         (setq gnus-newsgroup-unreads (cons article gnus-newsgroup-unreads)))
9311     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9312     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9313     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9314     (if (= mark gnus-ticked-mark)
9315         (setq gnus-newsgroup-marked (cons article gnus-newsgroup-marked)))
9316     (if (= mark gnus-dormant-mark)
9317         (setq gnus-newsgroup-dormant (cons article gnus-newsgroup-dormant)))))
9318
9319 (defalias 'gnus-summary-mark-as-unread-forward 
9320   'gnus-summary-tick-article-forward)
9321 (make-obsolete 'gnus-summary-mark-as-unread-forward 
9322                'gnus-summary-tick-article-forward)
9323 (defun gnus-summary-tick-article-forward (n)
9324   "Tick N articles forwards.
9325 If N is negative, tick backwards instead.
9326 The difference between N and the number of articles ticked is returned."
9327   (interactive "p")
9328   (gnus-summary-mark-forward n gnus-ticked-mark))
9329
9330 (defalias 'gnus-summary-mark-as-unread-backward 
9331   'gnus-summary-tick-article-backward)
9332 (make-obsolete 'gnus-summary-mark-as-unread-backward 
9333                'gnus-summary-tick-article-backward)
9334 (defun gnus-summary-tick-article-backward (n)
9335   "Tick N articles backwards.
9336 The difference between N and the number of articles ticked is returned."
9337   (interactive "p")
9338   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
9339
9340 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9341 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9342 (defun gnus-summary-tick-article (&optional article clear-mark)
9343   "Mark current article as unread.
9344 Optional 1st argument ARTICLE specifies article number to be marked as unread.
9345 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
9346   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
9347                                        gnus-ticked-mark)))
9348
9349 (defun gnus-summary-mark-as-read-forward (n)
9350   "Mark N articles as read forwards.
9351 If N is negative, mark backwards instead.
9352 The difference between N and the actual number of articles marked is
9353 returned."
9354   (interactive "p")
9355   (gnus-summary-mark-forward n gnus-del-mark t))
9356
9357 (defun gnus-summary-mark-as-read-backward (n)
9358   "Mark the N articles as read backwards.
9359 The difference between N and the actual number of articles marked is
9360 returned."
9361   (interactive "p")
9362   (gnus-summary-mark-forward (- n) gnus-del-mark t))
9363
9364 (defun gnus-summary-mark-as-read (&optional article mark)
9365   "Mark current article as read.
9366 ARTICLE specifies the article to be marked as read.
9367 MARK specifies a string to be inserted at the beginning of the line."
9368   (gnus-summary-mark-article article mark))
9369
9370 (defun gnus-summary-clear-mark-forward (n)
9371   "Clear marks from N articles forward.
9372 If N is negative, clear backward instead.
9373 The difference between N and the number of marks cleared is returned."
9374   (interactive "p")
9375   (gnus-summary-mark-forward n gnus-unread-mark))
9376
9377 (defun gnus-summary-clear-mark-backward (n)
9378   "Clear marks from N articles backward.
9379 The difference between N and the number of marks cleared is returned."
9380   (interactive "p")
9381   (gnus-summary-mark-forward (- n) gnus-unread-mark))
9382
9383 (defun gnus-summary-mark-unread-as-read ()
9384   "Intended to be used by `gnus-summary-mark-article-hook'."
9385   (and (memq gnus-current-article gnus-newsgroup-unreads)
9386        (or (memq gnus-current-article gnus-newsgroup-marked)
9387            (memq gnus-current-article gnus-newsgroup-dormant)
9388            (memq gnus-current-article gnus-newsgroup-expirable)
9389            (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
9390
9391 (defun gnus-summary-mark-region-as-read (point mark all)
9392   "Mark all unread articles between point and mark as read.
9393 If given a prefix, mark all articles between point and mark as read,
9394 even ticked and dormant ones."
9395   (interactive "r\nP")
9396   (save-excursion
9397     (goto-char point)
9398     (beginning-of-line)
9399     (while (and 
9400             (< (point) mark)
9401             (progn
9402               (and
9403                (or all
9404                    (and
9405                     (not (memq (gnus-summary-article-number)
9406                                gnus-newsgroup-marked))
9407                     (not (memq (gnus-summary-article-number)
9408                                gnus-newsgroup-dormant))))
9409                (gnus-summary-mark-article
9410                 (gnus-summary-article-number) gnus-del-mark))
9411               t)
9412             (zerop (forward-line 1))))))
9413
9414 ;; Fix by Per Abrahamsen <amanda@iesd.auc.dk>.
9415 (defalias 'gnus-summary-delete-marked-as-read 
9416   'gnus-summary-remove-lines-marked-as-read)
9417 (make-obsolete 'gnus-summary-delete-marked-as-read 
9418                'gnus-summary-remove-lines-marked-as-read)
9419 (defun gnus-summary-remove-lines-marked-as-read ()
9420   "Remove lines that are marked as read."
9421   (interactive)
9422   (gnus-summary-remove-lines-marked-with 
9423    (concat (mapconcat
9424             (lambda (char) (char-to-string (symbol-value char)))
9425             '(gnus-del-mark gnus-read-mark gnus-ancient-mark
9426               gnus-killed-mark gnus-kill-file-mark
9427               gnus-low-score-mark gnus-expirable-mark
9428               gnus-canceled-mark)
9429             ""))))
9430
9431 (defalias 'gnus-summary-delete-marked-with 
9432   'gnus-summary-remove-lines-marked-with)
9433 (make-obsolete 'gnus-summary-delete-marked-with 
9434                'gnus-summary-remove-lines-marked-with)
9435 ;; Rewrite by Daniel Quinlan <quinlan@best.com>.
9436 (defun gnus-summary-remove-lines-marked-with (marks)
9437   "Remove lines that are marked with MARKS (e.g. \"DK\")."
9438   (interactive "sMarks: ")
9439   ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
9440   (gnus-set-global-variables)
9441   (let ((buffer-read-only nil)
9442         (marks (concat "^[" marks "]")))
9443     (goto-char (point-min))
9444     (if gnus-newsgroup-adaptive
9445         (gnus-score-remove-lines-adaptive marks)
9446       (while (re-search-forward marks nil t)
9447         (gnus-delete-line)))
9448     ;; If we use dummy roots, we have to do an additional sweep over
9449     ;; the buffer.
9450     (if (not (eq gnus-summary-make-false-root 'dummy))
9451         ()
9452       (goto-char (point-min))
9453       (setq marks (concat "^[" (char-to-string gnus-dummy-mark) "]"))
9454       (while (re-search-forward marks nil t)
9455         (if (gnus-subject-equal
9456              (gnus-summary-subject-string)
9457              (progn
9458                (forward-line 1)
9459                (gnus-summary-subject-string)))
9460             ()
9461           (forward-line -1)
9462           (gnus-delete-line)))))
9463   (or (zerop (buffer-size))
9464       (if (eobp)
9465           (gnus-summary-prev-subject 1)
9466         (gnus-summary-position-cursor))))
9467
9468 (defun gnus-summary-expunge-below (&optional score)
9469   "Remove articles with score less than SCORE."
9470   (interactive "P")
9471   (gnus-set-global-variables)
9472   (setq score (if score
9473                   (prefix-numeric-value score)
9474                 (or gnus-summary-default-score 0)))
9475   (save-excursion
9476     (set-buffer gnus-summary-buffer)
9477     (goto-char (point-min))
9478     (let ((buffer-read-only nil)
9479           beg)
9480       (while (not (eobp))
9481         (if (< (gnus-summary-article-score) score)
9482             (progn
9483               (setq beg (point))
9484               (forward-line 1)
9485               (delete-region beg (point)))
9486           (forward-line 1)))
9487       ;; Adjust point.
9488       (or (zerop (buffer-size))
9489           (if (eobp)
9490               (gnus-summary-prev-subject 1)
9491             (gnus-summary-position-cursor))))))
9492
9493 (defun gnus-summary-mark-below (score mark)
9494   "Mark articles with score less than SCORE with MARK."
9495   (interactive "P\ncMark: ")
9496   (gnus-set-global-variables)
9497   (setq score (if score
9498                   (prefix-numeric-value score)
9499                 (or gnus-summary-default-score 0)))
9500   (save-excursion
9501     (set-buffer gnus-summary-buffer)
9502     (goto-char (point-min))
9503     (while (not (eobp))
9504       (and (< (gnus-summary-article-score) score)
9505            (gnus-summary-mark-article nil mark))
9506       (forward-line 1))))
9507
9508 (defun gnus-summary-kill-below (&optional score)
9509   "Mark articles with score below SCORE as read."
9510   (interactive "P")
9511   (gnus-set-global-variables)
9512   (gnus-summary-mark-below score gnus-killed-mark))
9513
9514 (defun gnus-summary-clear-above (&optional score)
9515   "Clear all marks from articles with score above SCORE."
9516   (interactive "P")
9517   (gnus-set-global-variables)
9518   (gnus-summary-mark-above score gnus-unread-mark))
9519
9520 (defun gnus-summary-tick-above (&optional score)
9521   "Tick all articles with score above SCORE."
9522   (interactive "P")
9523   (gnus-set-global-variables)
9524   (gnus-summary-mark-above score gnus-ticked-mark))
9525
9526 (defun gnus-summary-mark-above (score mark)
9527   "Mark articles with score over SCORE with MARK."
9528   (interactive "P\ncMark: ")
9529   (gnus-set-global-variables)
9530   (setq score (if score
9531                   (prefix-numeric-value score)
9532                 (or gnus-summary-default-score 0)))
9533   (save-excursion
9534     (set-buffer gnus-summary-buffer)
9535     (goto-char (point-min))
9536     (while (not (eobp))
9537       (if (> (gnus-summary-article-score) score)
9538           (progn
9539             (gnus-summary-mark-article nil mark)
9540             (forward-line 1))
9541         (forward-line 1)))))
9542
9543 ;; Suggested by Daniel Quinlan <quinlan@best.com>.  
9544 (defun gnus-summary-show-all-expunged ()
9545   "Display all the hidden articles that were expunged for low scores."
9546   (interactive)
9547   (gnus-set-global-variables)
9548   (let ((buffer-read-only nil))
9549     (let ((scored gnus-newsgroup-scored)
9550           headers h)
9551       (while scored
9552         (or (gnus-summary-goto-subject (car (car scored)))
9553             (and (setq h (gnus-get-header-by-num (car (car scored))))
9554                  (< (cdr (car scored)) gnus-summary-expunge-below)
9555                  (setq headers (cons h headers))))
9556         (setq scored (cdr scored)))
9557       (or headers (error "No expunged articles hidden."))
9558       (goto-char (point-min))
9559       (save-excursion 
9560         (gnus-summary-update-lines 
9561          (point)
9562          (progn
9563            (gnus-summary-prepare-unthreaded (nreverse headers))
9564            (point)))))
9565     (goto-char (point-min))
9566     (gnus-summary-position-cursor)))
9567
9568 (defun gnus-summary-show-all-dormant ()
9569   "Display all the hidden articles that are marked as dormant."
9570   (interactive)
9571   (gnus-set-global-variables)
9572   (let ((buffer-read-only nil))
9573     (let ((dormant gnus-newsgroup-dormant)
9574           headers h)
9575       (while dormant
9576         (or (gnus-summary-goto-subject (car dormant))
9577             (and (setq h (gnus-get-header-by-num (car dormant)))
9578                  (setq headers (cons h headers))))
9579         (setq dormant (cdr dormant)))
9580       (or headers (error "No dormant articles hidden."))
9581       (goto-char (point-min))
9582       (save-excursion 
9583         (gnus-summary-update-lines 
9584          (point)
9585          (progn
9586            (gnus-summary-prepare-unthreaded (nreverse headers))
9587            (point)))))
9588     (goto-char (point-min))
9589     (gnus-summary-position-cursor)))
9590
9591 (defun gnus-summary-hide-all-dormant ()
9592   "Hide all dormant articles."
9593   (interactive)
9594   (gnus-set-global-variables)
9595   (gnus-summary-remove-lines-marked-with (char-to-string gnus-dormant-mark))
9596   (gnus-summary-position-cursor))
9597
9598 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
9599   "Mark all articles not marked as unread in this newsgroup as read.
9600 If prefix argument ALL is non-nil, all articles are marked as read.
9601 If QUIETLY is non-nil, no questions will be asked.
9602 If TO-HERE is non-nil, it should be a point in the buffer. All
9603 articles before this point will be marked as read.
9604 The number of articles marked as read is returned."
9605   (interactive "P")
9606   (gnus-set-global-variables)
9607   (prog1
9608       (if (or quietly
9609               (not gnus-interactive-catchup) ;Without confirmation?
9610               gnus-expert-user
9611               (gnus-y-or-n-p
9612                (if all
9613                    "Mark absolutely all articles as read? "
9614                  "Mark all unread articles as read? ")))
9615           (if (and not-mark 
9616                    (not gnus-newsgroup-adaptive)
9617                    (not gnus-newsgroup-auto-expire))
9618               (progn
9619                 (and all (setq gnus-newsgroup-marked nil
9620                                gnus-newsgroup-dormant nil))
9621                 (setq gnus-newsgroup-unreads 
9622                       (append gnus-newsgroup-marked gnus-newsgroup-dormant)))
9623             ;; We actually mark all articles as canceled, which we
9624             ;; have to do when using auto-expiry or adaptive scoring. 
9625             (let ((unreads (length gnus-newsgroup-unreads)))
9626               (gnus-summary-show-all-threads)
9627               (if (gnus-summary-first-subject (not all))
9628                   (while (and (if to-here (< (point) to-here) t)
9629                               (gnus-summary-mark-article-as-read
9630                                gnus-catchup-mark)
9631                               (gnus-summary-search-subject nil (not all)))))
9632               (- unreads (length gnus-newsgroup-unreads))
9633               (or to-here
9634                   (setq gnus-newsgroup-unreads gnus-newsgroup-marked)))))
9635     (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
9636       (if (and (not to-here) (eq 'nnvirtual (car method)))
9637           (nnvirtual-catchup-group
9638            (gnus-group-real-name gnus-newsgroup-name) (nth 1 method) all)))
9639     (gnus-summary-position-cursor)))
9640
9641 (defun gnus-summary-catchup-to-here (&optional all)
9642   "Mark all unticked articles before the current one as read.
9643 If ALL is non-nil, also mark ticked and dormant articles as read."
9644   (interactive)
9645   (gnus-set-global-variables)
9646   (save-excursion
9647     (and (zerop (forward-line -1))
9648          (progn
9649            (end-of-line)
9650            (gnus-summary-catchup all t (point))
9651            (gnus-set-mode-line 'summary))))
9652   (gnus-summary-position-cursor))
9653
9654 (defun gnus-summary-catchup-all (&optional quietly)
9655   "Mark all articles in this newsgroup as read."
9656   (interactive)
9657   (gnus-set-global-variables)
9658   (gnus-summary-catchup t quietly))
9659
9660 (defun gnus-summary-catchup-and-exit (&optional all quietly)
9661   "Mark all articles not marked as unread in this newsgroup as read, then exit.
9662 If prefix argument ALL is non-nil, all articles are marked as read."
9663   (interactive "P")
9664   (gnus-set-global-variables)
9665   (gnus-summary-catchup all quietly nil 'fast)
9666   ;; Select next newsgroup or exit.
9667   (if (eq gnus-auto-select-next 'quietly)
9668       (gnus-summary-next-group nil)
9669     (gnus-summary-exit)))
9670
9671 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
9672   "Mark all articles in this newsgroup as read, and then exit."
9673   (interactive)
9674   (gnus-set-global-variables)
9675   (gnus-summary-catchup-and-exit t quietly))
9676
9677 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
9678 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
9679   "Mark all articles in this group as read and select the next group.
9680 If given a prefix, mark all articles, unread as well as ticked, as
9681 read." 
9682   (interactive "P")
9683   (gnus-set-global-variables)
9684   (gnus-summary-catchup all)
9685   (gnus-summary-next-group))
9686
9687 ;; Thread-based commands.
9688
9689 (defun gnus-summary-toggle-threads (&optional arg)
9690   "Toggle showing conversation threads.
9691 If ARG is positive number, turn showing conversation threads on."
9692   (interactive "P")
9693   (gnus-set-global-variables)
9694   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
9695     (setq gnus-show-threads
9696           (if (null arg) (not gnus-show-threads)
9697             (> (prefix-numeric-value arg) 0)))
9698     (gnus-summary-prepare)
9699     (gnus-summary-goto-subject current)
9700     (gnus-summary-position-cursor)))
9701
9702 (defun gnus-summary-show-all-threads ()
9703   "Show all threads."
9704   (interactive)
9705   (gnus-set-global-variables)
9706   (save-excursion
9707     (let ((buffer-read-only nil))
9708       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
9709   (gnus-summary-position-cursor))
9710
9711 (defun gnus-summary-show-thread ()
9712   "Show thread subtrees.
9713 Returns nil if no thread was there to be shown."
9714   (interactive)
9715   (gnus-set-global-variables)
9716   (let ((buffer-read-only nil)
9717         (orig (point))
9718         ;; first goto end then to beg, to have point at beg after let
9719         (end (progn (end-of-line) (point)))
9720         (beg (progn (beginning-of-line) (point))))
9721     (prog1
9722         ;; Any hidden lines here?
9723         (search-forward "\r" end t)
9724       (subst-char-in-region beg end ?\^M ?\n t)
9725       (goto-char orig)
9726       (gnus-summary-position-cursor))))
9727
9728 (defun gnus-summary-hide-all-threads ()
9729   "Hide all thread subtrees."
9730   (interactive)
9731   (gnus-set-global-variables)
9732   (save-excursion
9733     (goto-char (point-min))
9734     (gnus-summary-hide-thread)
9735     (while (and (not (eobp)) (zerop (forward-line 1)))
9736       (gnus-summary-hide-thread)))
9737   (gnus-summary-position-cursor))
9738
9739 (defun gnus-summary-hide-thread ()
9740   "Hide thread subtrees.
9741 Returns nil if no threads were there to be hidden."
9742   (interactive)
9743   (gnus-set-global-variables)
9744   (let ((buffer-read-only nil)
9745         (start (point))
9746         (level (gnus-summary-thread-level))
9747         (end (point)))
9748     ;; Go forward until either the buffer ends or the subthread
9749     ;; ends. 
9750     (if (eobp)
9751         ()
9752       (while (and (zerop (forward-line 1))
9753                   (> (gnus-summary-thread-level) level))
9754         (setq end (point)))
9755       (prog1
9756           (save-excursion
9757             (goto-char end)
9758             (search-backward "\n" start t))
9759         (subst-char-in-region start end ?\n ?\^M t)
9760         (forward-line -1)))))
9761
9762 (defun gnus-summary-go-to-next-thread (&optional previous)
9763   "Go to the same level (or less) next thread.
9764 If PREVIOUS is non-nil, go to previous thread instead.
9765 Return the article number moved to, or nil if moving was impossible."
9766   (let ((level (gnus-summary-thread-level))
9767         (article (gnus-summary-article-number)))
9768     (if previous 
9769         (while (and (zerop (forward-line -1))
9770                     (> (gnus-summary-thread-level) level)))
9771       (while (and (save-excursion
9772                     (forward-line 1)
9773                     (not (eobp)))
9774                   (zerop (forward-line 1))
9775                   (> (gnus-summary-thread-level) level))))
9776     (gnus-summary-recenter)
9777     (gnus-summary-position-cursor)
9778     (let ((oart (gnus-summary-article-number)))
9779       (and (/= oart article) oart))))
9780
9781 (defun gnus-summary-next-thread (n)
9782   "Go to the same level next N'th thread.
9783 If N is negative, search backward instead.
9784 Returns the difference between N and the number of skips actually
9785 done."
9786   (interactive "p")
9787   (gnus-set-global-variables)
9788   (let ((backward (< n 0))
9789         (n (abs n)))
9790   (while (and (> n 0)
9791               (gnus-summary-go-to-next-thread backward))
9792     (setq n (1- n)))
9793   (gnus-summary-position-cursor)
9794   (if (/= 0 n) (gnus-message 7 "No more threads"))
9795   n))
9796
9797 (defun gnus-summary-prev-thread (n)
9798   "Go to the same level previous N'th thread.
9799 Returns the difference between N and the number of skips actually
9800 done."
9801   (interactive "p")
9802   (gnus-set-global-variables)
9803   (gnus-summary-next-thread (- n)))
9804
9805 (defun gnus-summary-go-down-thread (&optional same)
9806   "Go down one level in the current thread.
9807 If SAME is non-nil, also move to articles of the same level."
9808   (let ((level (gnus-summary-thread-level))
9809         (start (point)))
9810     (if (and (zerop (forward-line 1))
9811              (> (gnus-summary-thread-level) level))
9812         t
9813       (goto-char start)
9814       nil)))
9815
9816 (defun gnus-summary-go-up-thread ()
9817   "Go up one level in the current thread."
9818   (let ((level (gnus-summary-thread-level))
9819         (start (point)))
9820     (while (and (zerop (forward-line -1))
9821                 (>= (gnus-summary-thread-level) level)))
9822     (if (>= (gnus-summary-thread-level) level)
9823         (progn
9824           (goto-char start)
9825           nil)
9826       t)))
9827
9828 (defun gnus-summary-down-thread (n)
9829   "Go down thread N steps.
9830 If N is negative, go up instead.
9831 Returns the difference between N and how many steps down that were
9832 taken."
9833   (interactive "p")
9834   (gnus-set-global-variables)
9835   (let ((up (< n 0))
9836         (n (abs n)))
9837   (while (and (> n 0)
9838               (if up (gnus-summary-go-up-thread)
9839                 (gnus-summary-go-down-thread)))
9840     (setq n (1- n)))
9841   (gnus-summary-position-cursor)
9842   (if (/= 0 n) (gnus-message 7 "Can't go further"))
9843   n))
9844
9845 (defun gnus-summary-up-thread (n)
9846   "Go up thread N steps.
9847 If N is negative, go up instead.
9848 Returns the difference between N and how many steps down that were
9849 taken."
9850   (interactive "p")
9851   (gnus-set-global-variables)
9852   (gnus-summary-down-thread (- n)))
9853
9854 (defun gnus-summary-kill-thread (&optional unmark)
9855   "Mark articles under current thread as read.
9856 If the prefix argument is positive, remove any kinds of marks.
9857 If the prefix argument is negative, tick articles instead."
9858   (interactive "P")
9859   (gnus-set-global-variables)
9860   (if unmark
9861       (setq unmark (prefix-numeric-value unmark)))
9862   (let ((killing t)
9863         (level (gnus-summary-thread-level)))
9864     (save-excursion
9865       (while killing
9866         ;; Mark the article...
9867         (cond ((null unmark) (gnus-summary-mark-article-as-read
9868                                gnus-killed-mark))
9869               ((> unmark 0) (gnus-summary-mark-article-as-unread 
9870                              gnus-unread-mark))
9871               (t (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
9872         ;; ...and go forward until either the buffer ends or the subtree
9873         ;; ends. 
9874         (if (not (and (zerop (forward-line 1))
9875                       (> (gnus-summary-thread-level) level)))
9876             (setq killing nil))))
9877     ;; Hide killed subtrees.
9878     (and (null unmark)
9879          gnus-thread-hide-killed
9880          (gnus-summary-hide-thread))
9881     ;; If marked as read, go to next unread subject.
9882     (if (null unmark)
9883         ;; Go to next unread subject.
9884         (gnus-summary-next-subject 1 t)))
9885   (gnus-set-mode-line 'summary))
9886
9887 ;; Summary sorting commands
9888
9889 (defun gnus-summary-sort-by-number (&optional reverse)
9890   "Sort summary buffer by article number.
9891 Argument REVERSE means reverse order."
9892   (interactive "P")
9893   (gnus-set-global-variables)
9894   (gnus-summary-sort 
9895    ;; `gnus-summary-article-number' is a macro, and `sort-subr' wants
9896    ;; a function, so we wrap it.
9897    (cons (lambda () (gnus-summary-article-number))
9898          'gnus-thread-sort-by-number) reverse))
9899
9900 (defun gnus-summary-sort-by-author (&optional reverse)
9901   "Sort summary buffer by author name alphabetically.
9902 If case-fold-search is non-nil, case of letters is ignored.
9903 Argument REVERSE means reverse order."
9904   (interactive "P")
9905   (gnus-set-global-variables)
9906   (gnus-summary-sort
9907    (cons
9908     (lambda ()
9909       (let ((extract (funcall
9910                       gnus-extract-address-components
9911                       (header-from (gnus-get-header-by-num
9912                                     (gnus-summary-article-number))))))
9913         (or (car extract) (cdr extract))))
9914     'gnus-thread-sort-by-author)
9915    reverse))
9916
9917 (defun gnus-summary-sort-by-subject (&optional reverse)
9918   "Sort summary buffer by subject alphabetically. `Re:'s are ignored.
9919 If case-fold-search is non-nil, case of letters is ignored.
9920 Argument REVERSE means reverse order."
9921   (interactive "P")
9922   (gnus-set-global-variables)
9923   (gnus-summary-sort
9924    (cons
9925     (lambda ()
9926       (downcase (gnus-simplify-subject (gnus-summary-subject-string))))
9927     'gnus-thread-sort-by-subject)
9928    reverse))
9929
9930 (defun gnus-summary-sort-by-date (&optional reverse)
9931   "Sort summary buffer by date.
9932 Argument REVERSE means reverse order."
9933   (interactive "P")
9934   (gnus-set-global-variables)
9935   (gnus-summary-sort
9936    (cons
9937     (lambda ()
9938       (gnus-sortable-date
9939        (header-date 
9940         (gnus-get-header-by-num (gnus-summary-article-number)))))
9941     'gnus-thread-sort-by-date)
9942    reverse))
9943
9944 (defun gnus-summary-sort-by-score (&optional reverse)
9945   "Sort summary buffer by score.
9946 Argument REVERSE means reverse order."
9947   (interactive "P")
9948   (gnus-set-global-variables)
9949   (gnus-summary-sort 
9950    (cons (lambda () (gnus-summary-article-score))
9951          'gnus-thread-sort-by-score)
9952    (not reverse)))
9953
9954 (defvar gnus-summary-already-sorted nil)
9955 (defun gnus-summary-sort (predicate reverse)
9956   ;; Sort summary buffer by PREDICATE.  REVERSE means reverse order. 
9957   (if gnus-summary-already-sorted
9958       ()
9959     (let (buffer-read-only)
9960       (if (not gnus-show-threads)
9961           ;; We do untreaded sorting...
9962           (progn
9963             (goto-char (point-min))
9964             (sort-subr reverse 'forward-line 'end-of-line (car predicate)))
9965         ;; ... or we do threaded sorting.
9966         (let ((gnus-thread-sort-functions (list (cdr predicate)))
9967               (gnus-summary-prepare-hook nil)
9968               (gnus-summary-already-sorted nil))
9969           ;; We do that by simply regenerating the threads.
9970           (gnus-summary-prepare)
9971           (and gnus-show-threads
9972                gnus-thread-hide-subtree
9973                (gnus-summary-hide-all-threads))
9974           ;; If in async mode, we send some info to the backend.
9975           (and gnus-newsgroup-async
9976                (setq gnus-newsgroup-threads (nreverse gnus-newsgroup-threads))
9977                (gnus-request-asynchronous 
9978                 gnus-newsgroup-name
9979                 (if (and gnus-asynchronous-article-function
9980                          (fboundp gnus-asynchronous-article-function))
9981                     (funcall gnus-asynchronous-article-function
9982                              gnus-newsgroup-threads)))))))))
9983
9984   
9985 (defun gnus-sortable-date (date)
9986   "Make sortable string by string-lessp from DATE.
9987 Timezone package is used."
9988   (let* ((date (timezone-fix-time date nil nil)) ;[Y M D H M S]
9989          (year (aref date 0))
9990          (month (aref date 1))
9991          (day (aref date 2)))
9992     (timezone-make-sortable-date 
9993      year month day 
9994      (timezone-make-time-string
9995       (aref date 3) (aref date 4) (aref date 5)))))
9996
9997
9998 ;; Summary saving commands.
9999
10000 (defun gnus-summary-save-article (&optional n)
10001   "Save the current article using the default saver function.
10002 If N is a positive number, save the N next articles.
10003 If N is a negative number, save the N previous articles.
10004 If N is nil and any articles have been marked with the process mark,
10005 save those articles instead.
10006 The variable `gnus-default-article-saver' specifies the saver function."
10007   (interactive "P")
10008   (gnus-set-global-variables)
10009   (let ((articles (gnus-summary-work-articles n)))
10010     (while articles
10011       (let ((header (gnus-get-header-by-num (car articles))))
10012         (if (vectorp header)
10013             (progn
10014               (save-window-excursion
10015                 (gnus-summary-select-article t nil nil (car articles)))
10016               (or gnus-save-all-headers
10017                   (gnus-article-hide-headers t))
10018               ;; Remove any X-Gnus lines.
10019               (save-excursion
10020                 (save-restriction
10021                   (set-buffer gnus-article-buffer)
10022                   (let ((buffer-read-only nil))
10023                     (goto-char (point-min))
10024                     (narrow-to-region (point) (or (search-forward "\n\n" nil t)
10025                                                   (point-max)))
10026                     (while (re-search-forward "^X-Gnus" nil t)
10027                       (beginning-of-line)
10028                       (delete-region (point)
10029                                      (progn (forward-line 1) (point))))
10030                     (widen))))
10031               (save-window-excursion
10032                 (if gnus-default-article-saver
10033                     (funcall gnus-default-article-saver)
10034                   (error "No default saver is defined."))))
10035           (if (assq 'name header)
10036               (gnus-copy-file (cdr (assq 'name header)))
10037             (gnus-message 1 "Article %d is unsaveable" (car articles)))))
10038       (gnus-summary-remove-process-mark (car articles))
10039       (setq articles (cdr articles)))
10040     (gnus-summary-position-cursor)
10041     n))
10042
10043 (defun gnus-summary-pipe-output (&optional arg)
10044   "Pipe the current article to a subprocess.
10045 If N is a positive number, pipe the N next articles.
10046 If N is a negative number, pipe the N previous articles.
10047 If N is nil and any articles have been marked with the process mark,
10048 pipe those articles instead."
10049   (interactive "P")
10050   (gnus-set-global-variables)
10051   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
10052     (gnus-summary-save-article arg)))
10053
10054 (defun gnus-summary-save-article-mail (&optional arg)
10055   "Append the current article to an mail file.
10056 If N is a positive number, save the N next articles.
10057 If N is a negative number, save the N previous articles.
10058 If N is nil and any articles have been marked with the process mark,
10059 save those articles instead."
10060   (interactive "P")
10061   (gnus-set-global-variables)
10062   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10063     (gnus-summary-save-article arg)))
10064
10065 (defun gnus-summary-save-article-rmail (&optional arg)
10066   "Append the current article to an rmail file.
10067 If N is a positive number, save the N next articles.
10068 If N is a negative number, save the N previous articles.
10069 If N is nil and any articles have been marked with the process mark,
10070 save those articles instead."
10071   (interactive "P")
10072   (gnus-set-global-variables)
10073   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10074     (gnus-summary-save-article arg)))
10075
10076 (defun gnus-summary-save-article-file (&optional arg)
10077   "Append the current article to a file.
10078 If N is a positive number, save the N next articles.
10079 If N is a negative number, save the N previous articles.
10080 If N is nil and any articles have been marked with the process mark,
10081 save those articles instead."
10082   (interactive "P")
10083   (gnus-set-global-variables)
10084   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10085     (gnus-summary-save-article arg)))
10086
10087 (defun gnus-read-save-file-name (prompt default-name)
10088   (let ((methods gnus-split-methods)
10089         split-name)
10090     (if (not gnus-split-methods)
10091         ()
10092       (save-excursion
10093         (set-buffer gnus-article-buffer)
10094         (gnus-narrow-to-headers)
10095         (while methods
10096           (goto-char (point-min))
10097           (and (condition-case () 
10098                    (re-search-forward (car (car methods)) nil t)
10099                  (error nil))
10100                (setq split-name (cons (nth 1 (car methods)) split-name)))
10101           (setq methods (cdr methods)))
10102         (widen)))
10103     (cond ((null split-name)
10104            (read-file-name
10105             (concat prompt " (default "
10106                     (file-name-nondirectory default-name) ") ")
10107             (file-name-directory default-name)
10108             default-name))
10109           ((= 1 (length split-name))
10110            (read-file-name
10111             (concat prompt " (default " (car split-name) ") ")
10112             gnus-article-save-directory
10113             (concat gnus-article-save-directory (car split-name))))
10114           (t
10115            (setq split-name (mapcar (lambda (el) (list el))
10116                                     (nreverse split-name)))
10117            (let ((result (completing-read 
10118                           (concat prompt " ")
10119                           split-name nil nil)))
10120              (concat gnus-article-save-directory
10121                      (if (string= result "")
10122                          (car (car split-name))
10123                        result)))))))
10124
10125 (defun gnus-summary-save-in-rmail (&optional filename)
10126   "Append this article to Rmail file.
10127 Optional argument FILENAME specifies file name.
10128 Directory to save to is default to `gnus-article-save-directory' which
10129 is initialized from the SAVEDIR environment variable."
10130   (interactive)
10131   (gnus-set-global-variables)
10132   (let ((default-name
10133           (funcall gnus-rmail-save-name gnus-newsgroup-name
10134                    gnus-current-headers gnus-newsgroup-last-rmail)))
10135     (or filename
10136         (setq filename (gnus-read-save-file-name 
10137                         "Save in rmail file:" default-name)))
10138     (gnus-make-directory (file-name-directory filename))
10139     (gnus-eval-in-buffer-window 
10140      gnus-article-buffer
10141      (save-excursion
10142        (save-restriction
10143          (widen)
10144          (gnus-output-to-rmail filename))))
10145     ;; Remember the directory name to save articles
10146     (setq gnus-newsgroup-last-rmail filename)))
10147
10148 (defun gnus-summary-save-in-mail (&optional filename)
10149   "Append this article to Unix mail file.
10150 Optional argument FILENAME specifies file name.
10151 Directory to save to is default to `gnus-article-save-directory' which
10152 is initialized from the SAVEDIR environment variable."
10153   (interactive)
10154   (gnus-set-global-variables)
10155   (let ((default-name
10156           (funcall gnus-mail-save-name gnus-newsgroup-name
10157                    gnus-current-headers gnus-newsgroup-last-mail)))
10158     (or filename
10159         (setq filename (gnus-read-save-file-name 
10160                         "Save in Unix mail file:" default-name)))
10161     (setq filename
10162           (expand-file-name filename
10163                             (and default-name
10164                                  (file-name-directory default-name))))
10165     (gnus-make-directory (file-name-directory filename))
10166     (gnus-eval-in-buffer-window 
10167      gnus-article-buffer
10168      (save-excursion
10169        (save-restriction
10170          (widen)
10171          (if (and (file-readable-p filename) (rmail-file-p filename))
10172              (gnus-output-to-rmail filename)
10173            (rmail-output filename 1 t t)))))
10174     ;; Remember the directory name to save articles.
10175     (setq gnus-newsgroup-last-mail filename)))
10176
10177 (defun gnus-summary-save-in-file (&optional filename)
10178   "Append this article to file.
10179 Optional argument FILENAME specifies file name.
10180 Directory to save to is default to `gnus-article-save-directory' which
10181 is initialized from the SAVEDIR environment variable."
10182   (interactive)
10183   (gnus-set-global-variables)
10184   (let ((default-name
10185           (funcall gnus-file-save-name gnus-newsgroup-name
10186                    gnus-current-headers gnus-newsgroup-last-file)))
10187     (or filename
10188         (setq filename (gnus-read-save-file-name 
10189                         "Save in file:" default-name)))
10190     (gnus-make-directory (file-name-directory filename))
10191     (gnus-eval-in-buffer-window 
10192      gnus-article-buffer
10193      (save-excursion
10194        (save-restriction
10195          (widen)
10196          (gnus-output-to-file filename))))
10197     ;; Remember the directory name to save articles.
10198     (setq gnus-newsgroup-last-file filename)))
10199
10200 (defun gnus-summary-save-in-pipe (&optional command)
10201   "Pipe this article to subprocess."
10202   (interactive)
10203   (gnus-set-global-variables)
10204   (let ((command (read-string "Shell command on article: "
10205                               gnus-last-shell-command)))
10206     (if (string-equal command "")
10207         (setq command gnus-last-shell-command))
10208     (gnus-eval-in-buffer-window 
10209      gnus-article-buffer
10210      (save-restriction
10211        (widen)
10212        (shell-command-on-region (point-min) (point-max) command nil)))
10213     (setq gnus-last-shell-command command)))
10214
10215 ;; Summary extract commands
10216
10217 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
10218   (let ((buffer-read-only nil)
10219         (article (gnus-summary-article-number))
10220         b)
10221     (or (gnus-summary-goto-subject article)
10222         (error (format "No such article: %d" article)))
10223     (or gnus-newsgroup-headers-hashtb-by-number
10224         (gnus-make-headers-hashtable-by-number))
10225     (gnus-summary-position-cursor)
10226     ;; If all commands are to be bunched up on one line, we collect
10227     ;; them here.  
10228     (if gnus-view-pseudos-separately
10229         ()
10230       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
10231             files action)
10232         (while ps
10233           (setq action (cdr (assq 'action (car ps))))
10234           (setq files (list (cdr (assq 'name (car ps)))))
10235           (while (and ps (cdr ps)
10236                       (string= (or action "1")
10237                                (or (cdr (assq 'action (car (cdr ps)))) "2")))
10238             (setq files (cons (cdr (assq 'name (car (cdr ps)))) files))
10239             (setcdr ps (cdr (cdr ps))))
10240           (if (not files)
10241               ()
10242             (if (not (string-match "%s" action))
10243                 (setq files (cons " " files)))
10244             (setq files (cons " " files))
10245             (and (assq 'execute (car ps))
10246                  (setcdr (assq 'execute (car ps))
10247                          (funcall (if (string-match "%s" action)
10248                                       'format 'concat)
10249                                   action 
10250                                   (mapconcat (lambda (f) f) files " ")))))
10251           (setq ps (cdr ps)))))
10252     (if (and gnus-view-pseudos (not not-view))
10253         (while pslist
10254           (and (assq 'execute (car pslist))
10255                (gnus-execute-command (cdr (assq 'execute (car pslist)))
10256                                      (eq gnus-view-pseudos 'not-confirm)))
10257           (setq pslist (cdr pslist)))
10258       (save-excursion
10259         (while pslist
10260           (gnus-summary-goto-subject (or (cdr (assq 'article (car pslist)))
10261                                          (gnus-summary-article-number)))
10262           (forward-line 1)
10263           (setq b (point))
10264           (insert "          " (file-name-nondirectory 
10265                                 (cdr (assq 'name (car pslist))))
10266                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
10267           (add-text-properties 
10268            b (1+ b) (list 'gnus-number gnus-reffed-article-number
10269                           'gnus-mark gnus-unread-mark 
10270                           'gnus-level 0
10271                           'gnus-pseudo (car pslist)))
10272           (forward-line -1)
10273           (gnus-sethash (int-to-string gnus-reffed-article-number)
10274                         (car pslist) gnus-newsgroup-headers-hashtb-by-number)
10275           (setq gnus-newsgroup-unreads
10276                 (cons gnus-reffed-article-number gnus-newsgroup-unreads))
10277           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
10278           (setq pslist (cdr pslist)))))))
10279
10280 (defun gnus-pseudos< (p1 p2)
10281   (let ((c1 (cdr (assq 'action p1)))
10282         (c2 (cdr (assq 'action p2))))
10283     (and c1 c2 (string< c1 c2))))
10284
10285 (defun gnus-request-pseudo-article (props)
10286   (cond ((assq 'execute props)
10287          (gnus-execute-command (cdr (assq 'execute props)))))
10288   (let ((gnus-current-article (gnus-summary-article-number)))
10289     (run-hooks 'gnus-mark-article-hook)))
10290
10291 (defun gnus-execute-command (command &optional automatic)
10292   (save-excursion
10293     (gnus-article-setup-buffer)
10294     (set-buffer gnus-article-buffer)
10295     (let ((command (if automatic command (read-string "Command: " command)))
10296           (buffer-read-only nil))
10297       (erase-buffer)
10298       (insert "$ " command "\n\n")
10299       (if gnus-view-pseudo-asynchronously
10300           (start-process "gnus-execute" nil "sh" "-c" command)
10301         (call-process "sh" nil t nil "-c" command)))))
10302
10303 (defun gnus-copy-file (file &optional to)
10304   "Copy FILE to TO."
10305   (interactive
10306    (list (read-file-name "Copy file: " default-directory)
10307          (read-file-name "Copy file to: " default-directory)))
10308   (gnus-set-global-variables)
10309   (or to (setq to (read-file-name "Copy file to: " default-directory)))
10310   (and (file-directory-p to) 
10311        (setq to (concat (file-name-as-directory to)
10312                         (file-name-nondirectory file))))
10313   (copy-file file to))
10314
10315 ;; Summary kill commands.
10316
10317 (defun gnus-summary-edit-global-kill (article)
10318   "Edit the \"global\" kill file."
10319   (interactive (list (gnus-summary-article-number)))
10320   (gnus-set-global-variables)
10321   (gnus-group-edit-global-kill article))
10322
10323 (defun gnus-summary-edit-local-kill ()
10324   "Edit a local kill file applied to the current newsgroup."
10325   (interactive)
10326   (gnus-set-global-variables)
10327   (setq gnus-current-headers 
10328         (gnus-gethash 
10329          (int-to-string (gnus-summary-article-number))
10330          gnus-newsgroup-headers-hashtb-by-number))
10331   (gnus-set-global-variables)
10332   (gnus-group-edit-local-kill 
10333    (gnus-summary-article-number) gnus-newsgroup-name))
10334
10335 \f
10336 ;;;
10337 ;;; Gnus article mode
10338 ;;;
10339
10340 (put 'gnus-article-mode 'mode-class 'special)
10341
10342 (if gnus-article-mode-map
10343     nil
10344   (setq gnus-article-mode-map (make-keymap))
10345   (suppress-keymap gnus-article-mode-map)
10346   (define-key gnus-article-mode-map " " 'gnus-article-next-page)
10347   (define-key gnus-article-mode-map "\177" 'gnus-article-prev-page)
10348   (define-key gnus-article-mode-map "\C-c^" 'gnus-article-refer-article)
10349   (define-key gnus-article-mode-map "h" 'gnus-article-show-summary)
10350   (define-key gnus-article-mode-map "s" 'gnus-article-show-summary)
10351   (define-key gnus-article-mode-map "\C-c\C-m" 'gnus-article-mail)
10352   (define-key gnus-article-mode-map "\C-c\C-M" 'gnus-article-mail-with-original)
10353   (define-key gnus-article-mode-map "?" 'gnus-article-describe-briefly)
10354   (define-key gnus-article-mode-map gnus-mouse-2 'gnus-article-push-button)
10355   (define-key gnus-article-mode-map "\r" 'gnus-article-press-button)
10356   (define-key gnus-article-mode-map "\t" 'gnus-article-next-button)
10357   (define-key gnus-article-mode-map "\C-c\C-b" 'gnus-bug)
10358   
10359   ;; Duplicate almost all summary keystrokes in the article mode map.
10360   (let ((commands 
10361          (list 
10362           " " "\177" "p" "N" "P" "\M-\C-n" "\M-\C-p"
10363           "\M-n" "\M-p" "." "," "\M-s" "\M-r" "<" ">" "j" "^" "\M-^"
10364           "u" "!" "U" "d" "D" "E" "\M-u" "\M-U" "k" "\C-k" "\M-\C-k"
10365           "\M-\C-l" "e" "#" "\M-#" "\M-\C-t" "\M-\C-s" "\M-\C-h"
10366           "\M-\C-f" "\M-\C-b" "\M-\C-u" "\M-\C-d" "&" "\C-w"
10367           "\C-t" "?" "\C-c\M-\C-s" "\C-c\C-s\C-n" "\C-c\C-s\C-a"
10368           "\C-c\C-s\C-s" "\C-c\C-s\C-d" "\C-c\C-s\C-i" "\C-x\C-s"
10369           "\M-g" "w" "\C-c\C-r" "\M-t" "C"
10370           "o" "\C-o" "|" "\M-k" "\M-K" "V" "\C-c\C-d"
10371           "\C-c\C-i" "x" "X" "t" "g" "?" "l"
10372           "\C-c\C-v\C-v" "\C-d" "v" 
10373 ;;        "Mt" "M!" "Md" "Mr"
10374 ;;        "Mc" "M " "Me" "Mx" "M?" "Mb" "MB" "M#" "M\M-#" "M\M-r"
10375 ;;        "M\M-\C-r" "MD" "M\M-D" "MS" "MC" "MH" "M\C-c" "Mk" "MK"
10376 ;;        "Ms" "Mc" "Mu" "Mm" "Mk" "Gn" "Gp" "GN" "GP" "G\C-n" "G\C-p"
10377 ;;        "G\M-n" "G\M-p" "Gf" "Gb" "Gg" "Gl" "Gp" "Tk" "Tl" "Ti" "TT"
10378 ;;        "Ts" "TS" "Th" "TH" "Tn" "Tp" "Tu" "Td" "T#" "A " "An" "A\177" "Ap"
10379 ;;        "A\r" "A<" "A>" "Ab" "Ae" "A^" "Ar" "Aw" "Ac" "Ag" "At" "Am"
10380 ;;        "As" "Wh" "Ws" "Wc" "Wo" "Ww" "Wd" "Wq" "Wf" "Wt" "W\C-t"
10381 ;;        "WT" "WA" "Wa" "WH" "WC" "WS" "Wb" "Hv" "Hf" "Hd" "Hh" "Hi"
10382 ;;        "Be" "B\177" "Bm" "Br" "Bw" "Bc" "Bq" "Bi" "Oo" "Om" "Or"
10383 ;;        "Of" "Oh" "Ov" "Op" "Vu" "V\C-s" "V\C-r" "Vr" "V&" "VT" "Ve"
10384 ;;        "VD" "Vk" "VK" "Vsn" "Vsa" "Vss" "Vsd" "Vsi"
10385           )))
10386     (while commands
10387       (define-key gnus-article-mode-map (car commands) 
10388         'gnus-article-summary-command)
10389       (setq commands (cdr commands))))
10390
10391   (let ((commands (list "q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
10392 ;;                      "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP" 
10393                          "=" "n")))
10394     (while commands
10395       (define-key gnus-article-mode-map (car commands) 
10396         'gnus-article-summary-command-nosave)
10397       (setq commands (cdr commands)))))
10398
10399
10400 (defun gnus-article-mode ()
10401   "Major mode for displaying an article.
10402
10403 All normal editing commands are switched off.
10404
10405 The following commands are available:
10406
10407 \\<gnus-article-mode-map>
10408 \\[gnus-article-next-page]\t Scroll the article one page forwards
10409 \\[gnus-article-prev-page]\t Scroll the article one page backwards
10410 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
10411 \\[gnus-article-show-summary]\t Display the summary buffer
10412 \\[gnus-article-mail]\t Send a reply to the address near point
10413 \\[gnus-article-mail-with-original]\t Send a reply to the address near point; include the original article
10414 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
10415 \\[gnus-info-find-node]\t Go to the Gnus info node"
10416   (interactive)
10417   (if gnus-visual (gnus-article-make-menu-bar))
10418   (kill-all-local-variables)
10419   (setq mode-line-modified "-- ")
10420   (make-local-variable 'mode-line-format)
10421   (setq mode-line-format (copy-sequence mode-line-format))
10422   (and (equal (nth 3 mode-line-format) "   ")
10423        (setcar (nthcdr 3 mode-line-format) ""))
10424   (setq mode-name "Article")
10425   (setq major-mode 'gnus-article-mode)
10426   (make-local-variable 'minor-mode-alist)
10427   (or (assq 'gnus-show-mime minor-mode-alist)
10428       (setq minor-mode-alist
10429             (cons (list 'gnus-show-mime " MIME") minor-mode-alist)))
10430   (use-local-map gnus-article-mode-map)
10431   (make-local-variable 'page-delimiter)
10432   (setq page-delimiter gnus-page-delimiter)
10433   (buffer-disable-undo (current-buffer))
10434   (setq buffer-read-only t)             ;Disable modification
10435   (run-hooks 'gnus-article-mode-hook))
10436
10437 (defun gnus-article-setup-buffer ()
10438   "Initialize article mode buffer."
10439   (if (get-buffer gnus-article-buffer)
10440       (save-excursion
10441         (set-buffer gnus-article-buffer)
10442         (buffer-disable-undo (current-buffer))
10443         (setq buffer-read-only t)
10444         (gnus-add-current-to-buffer-list)
10445         (or (eq major-mode 'gnus-article-mode)
10446             (gnus-article-mode)))
10447     (save-excursion
10448       (set-buffer (get-buffer-create gnus-article-buffer))
10449       (gnus-add-current-to-buffer-list)
10450       (gnus-article-mode))))
10451
10452 ;; Set article window start at LINE, where LINE is the number of lines
10453 ;; from the head of the article.
10454 (defun gnus-article-set-window-start (&optional line)
10455   (set-window-start 
10456    (get-buffer-window gnus-article-buffer)
10457    (save-excursion
10458      (set-buffer gnus-article-buffer)
10459      (goto-char (point-min))
10460      (if (not line)
10461          (point-min)
10462        (gnus-message 6 "Moved to bookmark")
10463        (search-forward "\n\n" nil t)
10464        (forward-line line)
10465        (point)))))
10466
10467 (defun gnus-request-article-this-buffer (article group)
10468   "Get an article and insert it into this buffer."
10469   (setq group (or group gnus-newsgroup-name))
10470
10471   ;; Open server if it has closed.
10472   (gnus-check-news-server (gnus-find-method-for-group group))
10473
10474   ;; Using `gnus-request-article' directly will insert the article into
10475   ;; `nntp-server-buffer' - so we'll save some time by not having to
10476   ;; copy it from the server buffer into the article buffer.
10477
10478   ;; We only request an article by message-id when we do not have the
10479   ;; headers for it, so we'll have to get those.
10480   (and (stringp article) 
10481        (let ((gnus-override-method gnus-refer-article-method))
10482          (gnus-read-header article)))
10483
10484   ;; If the article number is negative, that means that this article
10485   ;; doesn't belong in this newsgroup (possibly), so we find its
10486   ;; message-id and request it by id instead of number.
10487   (if (not (numberp article))
10488       ()
10489     (save-excursion
10490       (set-buffer gnus-summary-buffer)
10491       (let ((header (gnus-get-header-by-num article)))
10492         (if (< article 0)
10493             (if (vectorp header)
10494                 ;; It's a real article.
10495                 (setq article (header-id header))
10496               ;; It is an extracted pseudo-article.
10497               (setq article 'pseudo)
10498               (gnus-request-pseudo-article header)))
10499
10500         (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
10501           (if (not (eq (car method) 'nneething))
10502               ()
10503             (let ((dir (concat (file-name-as-directory (nth 1 method))
10504                                (header-subject header))))
10505               (if (file-directory-p dir)
10506                   (progn
10507                     (setq article 'nneething)
10508                     (gnus-group-enter-directory dir)))))))))
10509
10510   ;; Check the cache.
10511   (if (and gnus-use-cache
10512            (numberp article)
10513            (gnus-cache-request-article article group))
10514       'article
10515     ;; Get the article and into the article buffer.
10516     (if (or (stringp article) (numberp article))
10517         (progn
10518           (erase-buffer)
10519           (let ((gnus-override-method 
10520                  (and (stringp article) gnus-refer-article-method)))
10521             (and (gnus-request-article article group (current-buffer))
10522                  'article)))
10523       article)))
10524
10525 (defun gnus-read-header (id)
10526   "Read the headers of article ID and enter them into the Gnus system."
10527   (or gnus-newsgroup-headers-hashtb-by-number
10528       (gnus-make-headers-hashtable-by-number))
10529   (let (header)
10530     (if (not (setq header 
10531                    (car (if (let ((gnus-nov-is-evil t))
10532                               (gnus-retrieve-headers 
10533                                (list id) gnus-newsgroup-name))
10534                             (gnus-get-newsgroup-headers)))))
10535         nil
10536       (if (stringp id)
10537           (header-set-number header gnus-reffed-article-number))
10538       (setq gnus-newsgroup-headers (cons header gnus-newsgroup-headers))
10539       (gnus-sethash (int-to-string (header-number header)) header
10540                     gnus-newsgroup-headers-hashtb-by-number)
10541       (if (stringp id)
10542           (setq gnus-reffed-article-number (1- gnus-reffed-article-number)))
10543       (setq gnus-current-headers header)
10544       header)))
10545
10546 (defun gnus-article-prepare (article &optional all-headers header)
10547   "Prepare ARTICLE in article mode buffer.
10548 ARTICLE should either be an article number or a Message-ID.
10549 If ARTICLE is an id, HEADER should be the article headers.
10550 If ALL-HEADERS is non-nil, no headers are hidden."
10551   (save-excursion
10552     ;; Make sure we start in a summary buffer.
10553     (or (eq major-mode 'gnus-summary-mode)
10554         (set-buffer gnus-summary-buffer))
10555     (setq gnus-summary-buffer (current-buffer))
10556     ;; Make sure the connection to the server is alive.
10557     (or (gnus-server-opened (gnus-find-method-for-group gnus-newsgroup-name))
10558         (progn
10559           (gnus-check-news-server 
10560            (gnus-find-method-for-group gnus-newsgroup-name))
10561           (gnus-request-group gnus-newsgroup-name t)))
10562     (or gnus-newsgroup-headers-hashtb-by-number
10563         (gnus-make-headers-hashtable-by-number))
10564     (let* ((article (if header (header-number header) article))
10565            (summary-buffer (current-buffer))
10566            (internal-hook gnus-article-internal-prepare-hook)
10567            (group gnus-newsgroup-name)
10568            result)
10569       (save-excursion
10570         (gnus-article-setup-buffer)
10571         (set-buffer gnus-article-buffer)
10572         (if (not (setq result (let ((buffer-read-only nil))
10573                                 (gnus-request-article-this-buffer 
10574                                  article group))))
10575             ;; There is no such article.
10576             (save-excursion
10577               (if (not (numberp article))
10578                   ()
10579                 (setq gnus-article-current 
10580                       (cons gnus-newsgroup-name article))
10581                 (set-buffer gnus-summary-buffer)
10582                 (setq gnus-current-article article)
10583                 (gnus-summary-mark-article article gnus-canceled-mark))
10584               (gnus-message 1 "No such article (may be canceled)")
10585               (ding)
10586               nil)
10587           (if (or (eq result 'pseudo) (eq result 'nneething))
10588               (progn
10589                 (save-excursion
10590                   (set-buffer summary-buffer)
10591                   (setq gnus-last-article gnus-current-article
10592                         gnus-newsgroup-history (cons gnus-current-article
10593                                                      gnus-newsgroup-history)
10594                         gnus-current-article 0
10595                         gnus-current-headers nil
10596                         gnus-article-current nil)
10597                   (if (eq result 'nneething)
10598                       (gnus-configure-windows 'summary)
10599                     (gnus-configure-windows 'article))
10600                   (gnus-set-global-variables))
10601                 (gnus-set-mode-line 'article))
10602             ;; The result from the `request' was an actual article -
10603             ;; or at least some text that is now displayed in the
10604             ;; article buffer.
10605             (if (and (numberp article)
10606                      (not (eq article gnus-current-article)))
10607                 ;; Seems like a new article has been selected.
10608                 ;; `gnus-current-article' must be an article number.
10609                 (save-excursion
10610                   (set-buffer summary-buffer)
10611                   (setq gnus-last-article gnus-current-article
10612                         gnus-newsgroup-history (cons gnus-current-article
10613                                                      gnus-newsgroup-history)
10614                         gnus-current-article article
10615                         gnus-current-headers 
10616                         (gnus-get-header-by-num gnus-current-article)
10617                         gnus-article-current 
10618                         (cons gnus-newsgroup-name gnus-current-article))
10619                   (gnus-summary-show-thread)
10620                   (run-hooks 'gnus-mark-article-hook)
10621                   (gnus-set-mode-line 'summary)
10622                   (and gnus-visual 
10623                        (run-hooks 'gnus-visual-mark-article-hook))
10624                   ;; Set the global newsgroup variables here.
10625                   ;; Suggested by Jim Sisolak
10626                   ;; <sisolak@trans4.neep.wisc.edu>.
10627                   (gnus-set-global-variables)
10628                   (setq gnus-have-all-headers 
10629                         (or all-headers gnus-show-all-headers))
10630                   (and gnus-use-cache 
10631                        (vectorp (gnus-get-header-by-number article))
10632                        (gnus-cache-possibly-enter-article
10633                         group article
10634                         (gnus-get-header-by-number article)
10635                         (memq article gnus-newsgroup-marked)
10636                         (memq article gnus-newsgroup-dormant)
10637                         (memq article gnus-newsgroup-unreads)))))
10638             ;; Hooks for getting information from the article.
10639             ;; This hook must be called before being narrowed.
10640             (let (buffer-read-only)
10641               (run-hooks 'internal-hook)
10642               (run-hooks 'gnus-article-prepare-hook)
10643               ;; Decode MIME message.
10644               (if (and gnus-show-mime
10645                        (or (not gnus-strict-mime)
10646                            (gnus-fetch-field "Mime-Version")))
10647                   (funcall gnus-show-mime-method))
10648               ;; Perform the article display hooks.
10649               (run-hooks 'gnus-article-display-hook))
10650             ;; Do page break.
10651             (goto-char (point-min))
10652             (and gnus-break-pages (gnus-narrow-to-page))
10653             (gnus-set-mode-line 'article)
10654             (gnus-configure-windows 'article)
10655             (goto-char (point-min))
10656             t))))))
10657
10658 (defun gnus-article-show-all-headers ()
10659   "Show all article headers in article mode buffer."
10660   (save-excursion 
10661     (gnus-article-setup-buffer)
10662     (set-buffer gnus-article-buffer)
10663     (let ((buffer-read-only nil))
10664       (remove-text-properties (point-min) (point-max) 
10665                               gnus-hidden-properties))))
10666
10667 (defun gnus-article-hide-headers-if-wanted ()
10668   "Hide unwanted headers if `gnus-have-all-headers' is nil.
10669 Provided for backwards compatability."
10670   (or (save-excursion (set-buffer gnus-summary-buffer) gnus-have-all-headers)
10671       (gnus-article-hide-headers)))
10672
10673 (defun gnus-article-hide-headers (&optional delete)
10674   "Hide unwanted headers and possibly sort them as well."
10675   (interactive "P")
10676   (save-excursion
10677     (set-buffer gnus-article-buffer)
10678     (save-restriction
10679       (let ((sorted gnus-sorted-header-list)
10680             (buffer-read-only nil)
10681             want-list beg want-l)
10682         ;; First we narrow to just the headers.
10683         (widen)
10684         (goto-char (point-min))
10685         ;; Hide any "From " lines at the beginning of (mail) articles. 
10686         (while (looking-at "From ")
10687           (forward-line 1))
10688         (if (bobp) 
10689             (add-text-properties (point-min) (point) gnus-hidden-properties))
10690         ;; Then treat the rest of the header lines.
10691         (narrow-to-region 
10692          (point) 
10693          (progn (search-forward "\n\n" nil t) (forward-line -1) (point)))
10694         ;; Then we use the two regular expressions
10695         ;; `gnus-ignored-headers' and `gnus-visible-headers' to
10696         ;; select which header lines is to remain visible in the
10697         ;; article buffer.
10698         (goto-char (point-min))
10699         (while (re-search-forward "^[^ \t]*:" nil t)
10700           (beginning-of-line)
10701           ;; We add the headers we want to keep to a list and delete
10702           ;; them from the buffer.
10703           (if (or (and (stringp gnus-visible-headers)
10704                        (looking-at gnus-visible-headers))
10705                   (and (not (stringp gnus-visible-headers))
10706                        (stringp gnus-ignored-headers)
10707                        (not (looking-at gnus-ignored-headers))))
10708               (progn
10709                 (setq beg (point))
10710                 (forward-line 1)
10711                 ;; Be sure to get multi-line headers...
10712                 (re-search-forward "^[^ \t]*:" nil t)
10713                 (beginning-of-line)
10714                 (setq want-list 
10715                       (cons (buffer-substring beg (point)) want-list))
10716                 (delete-region beg (point))
10717                 (goto-char beg))
10718             (forward-line 1)))
10719         ;; Next we perform the sorting by looking at
10720         ;; `gnus-sorted-header-list'. 
10721         (goto-char (point-min))
10722         (while (and sorted want-list)
10723           (setq want-l want-list)
10724           (while (and want-l
10725                       (not (string-match (car sorted) (car want-l))))
10726             (setq want-l (cdr want-l)))
10727           (if want-l 
10728               (progn
10729                 (insert (car want-l))
10730                 (setq want-list (delq (car want-l) want-list))))
10731           (setq sorted (cdr sorted)))
10732         ;; Any headers that were not matched by the sorted list we
10733         ;; just tack on the end of the visible header list.
10734         (while want-list
10735           (insert (car want-list))
10736           (setq want-list (cdr want-list)))
10737         ;; And finally we make the unwanted headers invisible.
10738         (if delete
10739             (delete-region (point) (point-max))
10740           ;; Suggested by Sudish Joseph <joseph@cis.ohio-state.edu>.
10741           (add-text-properties (point) (point-max) gnus-hidden-properties))))))
10742
10743 ;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.
10744 (defun gnus-article-treat-overstrike ()
10745   "Translate overstrikes into bold text."
10746   (interactive)
10747   (save-excursion
10748     (set-buffer gnus-article-buffer)
10749     (let ((buffer-read-only nil))
10750       (while (search-forward "\b" nil t)
10751         (let ((next (following-char))
10752               (previous (char-after (- (point) 2))))
10753           (cond ((eq next previous)
10754                  (delete-region (- (point) 2) (point))
10755                  (put-text-property (point) (1+ (point))
10756                                     'face 'bold))
10757                 ((eq next ?_)
10758                  (delete-region (1- (point)) (1+ (point)))
10759                  (put-text-property (1- (point)) (point)
10760                                     'face 'underline))
10761                 ((eq previous ?_)
10762                  (delete-region (- (point) 2) (point))
10763                  (put-text-property (point) (1+ (point))
10764                                     'face 'underline))))))))
10765
10766 (defun gnus-article-word-wrap ()
10767   "Format too long lines."
10768   (interactive)
10769   (save-excursion
10770     (set-buffer gnus-article-buffer)
10771     (let ((buffer-read-only nil))
10772       (goto-char (point-min))
10773       (search-forward "\n\n" nil t)
10774       (end-of-line 1)
10775       (let ((paragraph-start "^[>|#:<;* ]*[ \t]*$")
10776             (adaptive-fill-regexp "[ \t]*\\([|#:<;>*]+ *\\)?")
10777             (adaptive-fill-mode t))
10778         (while (not (eobp))
10779           (and (>= (current-column) (min fill-column (window-width)))
10780                (/= (preceding-char) ?:)
10781                (fill-paragraph nil))
10782           (end-of-line 2))))))
10783
10784 (defun gnus-article-remove-cr ()
10785   "Remove carriage returns from an article."
10786   (interactive)
10787   (save-excursion
10788     (set-buffer gnus-article-buffer)
10789     (let ((buffer-read-only nil))
10790       (goto-char (point-min))
10791       (while (search-forward "\r" nil t)
10792         (replace-match "" t t)))))
10793
10794 (defun gnus-article-display-x-face (&optional force)
10795   "Look for an X-Face header and display it if present."
10796   (interactive (list 'force))
10797   (save-excursion
10798     (set-buffer gnus-article-buffer)
10799     (let ((inhibit-point-motion-hooks t)
10800           (case-fold-search nil))
10801       (save-restriction
10802         (goto-char (point-min))
10803         (search-forward "\n\n")
10804         (narrow-to-region (point-min) (point))
10805         (goto-char (point-min))
10806         (if (or (not gnus-article-x-face-command)
10807                 (and (not force)
10808                      (or (not gnus-article-x-face-too-ugly)
10809                          (string-match gnus-article-x-face-too-ugly
10810                                        (mail-fetch-field "from"))))
10811                 (progn
10812                   (goto-char (point-min))
10813                   (not (re-search-forward "^X-Face: " nil t))))
10814             nil
10815           (let ((beg (point))
10816                 (end (1- (re-search-forward "^\\($\\|[^ \t]\\)" nil t))))
10817             (if (symbolp gnus-article-x-face-command)
10818                 (and (or (fboundp gnus-article-x-face-command)
10819                          (error "%s is not a function"
10820                                 gnus-article-x-face-command))
10821                      (funcall gnus-article-x-face-command beg end))
10822               (call-process-region beg end "sh" nil 0 nil
10823                                    "-c" gnus-article-x-face-command))))))))
10824
10825 (defun gnus-article-de-quoted-unreadable (&optional force)
10826   "Do a naïve translation of a quoted-printable-encoded article.
10827 This is in no way, shape or form meant as a replacement for real MIME
10828 processing, but is simply a stop-gap measure until MIME support is
10829 written.
10830 If FORCE, decode the article whether it is marked as quoted-printable
10831 or not." 
10832   (interactive (list 'force))
10833   (save-excursion
10834     (set-buffer gnus-article-buffer)
10835     (let ((case-fold-search t)
10836           (buffer-read-only nil)
10837           (type (gnus-fetch-field "content-transfer-encoding")))
10838       (if (or force (and type (string-match "quoted-printable" type)))
10839           (progn
10840             (goto-char (point-min))
10841             (search-forward "\n\n" nil 'move)
10842             (gnus-mime-decode-quoted-printable (point) (point-max)))))))
10843
10844 (defun gnus-mime-decode-quoted-printable (from to)
10845   ;; Decode quoted-printable from region between FROM and TO.
10846   (save-excursion
10847     (goto-char from)
10848     (while (search-forward "=" to t)
10849       (cond ((eq (following-char) ?\n)
10850              (delete-char -1)
10851              (delete-char 1))
10852             ((looking-at "[0-9A-F][0-9A-F]")
10853              (delete-char -1)
10854              (insert (hexl-hex-string-to-integer
10855                       (buffer-substring (point) (+ 2 (point)))))
10856              (delete-char 2))
10857             ((looking-at "=")
10858              (delete-char 1))
10859             ((gnus-message 3 "Malformed MIME quoted-printable message"))))))
10860
10861 (defvar gnus-article-time-units
10862   (list (cons 'year (* 365.25 24 60 60))
10863         (cons 'week (* 7 24 60 60))
10864         (cons 'day (* 24 60 60))
10865         (cons 'hour (* 60 60))
10866         (cons 'minute 60)
10867         (cons 'second 1)))
10868
10869 (defun gnus-article-date-ut (&optional type)
10870   "Convert DATE date to universal time in the current article.
10871 If TYPE is `local', convert to local time; if it is `lapsed', output
10872 how much time has lapsed since DATE."
10873   (interactive (list 'ut))
10874   (let ((date (header-date (or gnus-current-headers 
10875                                (gnus-get-header-by-number
10876                                 (gnus-summary-article-number))"")))
10877         (date-regexp "^Date: \\|^X-Sent: "))
10878     (if (or (not date)
10879             (string= date ""))
10880         ()
10881       (save-excursion
10882         (set-buffer gnus-article-buffer)
10883         (let ((buffer-read-only nil))
10884           (goto-char (point-min))
10885           (if (and (re-search-forward date-regexp nil t)
10886                    (progn 
10887                      (beginning-of-line)
10888                      (looking-at date-regexp)))
10889               (delete-region (gnus-point-at-bol)
10890                              (progn (end-of-line) (1+ (point))))
10891             (goto-char (point-min))
10892             (goto-char (- (search-forward "\n\n") 2)))
10893           (insert
10894            (cond 
10895             ((eq type 'local)
10896              (concat "Date: " (condition-case ()
10897                                   (timezone-make-date-arpa-standard date)
10898                                 (error date))
10899                      "\n"))
10900             ((eq type 'ut)
10901              (concat "Date: "
10902                      (condition-case ()
10903                          (timezone-make-date-arpa-standard date nil "UT")
10904                        (error date))
10905                      "\n"))
10906             ((eq type 'lapsed)
10907              ;; If the date is seriously mangled, the timezone
10908              ;; functions are liable to bug out, so we condition-case
10909              ;; the entire thing.  
10910              (let* ((real-sec (condition-case ()
10911                                   (- (gnus-seconds-since-epoch 
10912                                       (timezone-make-date-arpa-standard
10913                                        (current-time-string) 
10914                                        (current-time-zone) "UT"))
10915                                      (gnus-seconds-since-epoch 
10916                                       (timezone-make-date-arpa-standard 
10917                                        date nil "UT")))
10918                                 (error 0)))
10919                     (sec (abs real-sec))
10920                     num prev)
10921                (if (zerop sec)
10922                    "X-Sent: Now\n"
10923                  (concat
10924                   "X-Sent: "
10925                   (mapconcat 
10926                    (lambda (unit)
10927                      (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
10928                          ""
10929                        (setq sec (- sec (* num (cdr unit))))
10930                        (prog1
10931                            (concat (if prev ", " "") (int-to-string 
10932                                                       (floor num))
10933                                    " " (symbol-name (car unit))
10934                                    (if (> num 1) "s" ""))
10935                          (setq prev t))))
10936                    gnus-article-time-units "")
10937                   (if (> real-sec 0)
10938                       " ago\n"
10939                     " in the future\n")))))
10940             (t
10941              (error "Unknown conversion type: %s" type)))))))))
10942
10943 (defun gnus-article-date-local ()
10944   "Convert the current article date to the local timezone."
10945   (interactive)
10946   (gnus-article-date-ut 'local))
10947
10948 (defun gnus-article-date-lapsed ()
10949   "Convert the current article date to time lapsed since it was sent."
10950   (interactive)
10951   (gnus-article-date-ut 'lapsed))
10952
10953 (defun gnus-article-maybe-highlight ()
10954   (if gnus-visual (gnus-article-highlight)))
10955
10956 ;; Article savers.
10957
10958 (defun gnus-output-to-rmail (file-name)
10959   "Append the current article to an Rmail file named FILE-NAME."
10960   (require 'rmail)
10961   ;; Most of these codes are borrowed from rmailout.el.
10962   (setq file-name (expand-file-name file-name))
10963   (setq rmail-default-rmail-file file-name)
10964   (let ((artbuf (current-buffer))
10965         (tmpbuf (get-buffer-create " *Gnus-output*")))
10966     (save-excursion
10967       (or (get-file-buffer file-name)
10968           (file-exists-p file-name)
10969           (if (gnus-yes-or-no-p
10970                (concat "\"" file-name "\" does not exist, create it? "))
10971               (let ((file-buffer (create-file-buffer file-name)))
10972                 (save-excursion
10973                   (set-buffer file-buffer)
10974                   (rmail-insert-rmail-file-header)
10975                   (let ((require-final-newline nil))
10976                     (write-region (point-min) (point-max) file-name t 1)))
10977                 (kill-buffer file-buffer))
10978             (error "Output file does not exist")))
10979       (set-buffer tmpbuf)
10980       (buffer-disable-undo (current-buffer))
10981       (erase-buffer)
10982       (insert-buffer-substring artbuf)
10983       (gnus-convert-article-to-rmail)
10984       ;; Decide whether to append to a file or to an Emacs buffer.
10985       (let ((outbuf (get-file-buffer file-name)))
10986         (if (not outbuf)
10987             (append-to-file (point-min) (point-max) file-name)
10988           ;; File has been visited, in buffer OUTBUF.
10989           (set-buffer outbuf)
10990           (let ((buffer-read-only nil)
10991                 (msg (and (boundp 'rmail-current-message)
10992                           (symbol-value 'rmail-current-message))))
10993             ;; If MSG is non-nil, buffer is in RMAIL mode.
10994             (if msg
10995                 (progn (widen)
10996                        (narrow-to-region (point-max) (point-max))))
10997             (insert-buffer-substring tmpbuf)
10998             (if msg
10999                 (progn
11000                   (goto-char (point-min))
11001                   (widen)
11002                   (search-backward "\^_")
11003                   (narrow-to-region (point) (point-max))
11004                   (goto-char (1+ (point-min)))
11005                   (rmail-count-new-messages t)
11006                   (rmail-show-message msg)))))))
11007     (kill-buffer tmpbuf)))
11008
11009 (defun gnus-output-to-file (file-name)
11010   "Append the current article to a file named FILE-NAME."
11011   (setq file-name (expand-file-name file-name))
11012   (let ((artbuf (current-buffer))
11013         (tmpbuf (get-buffer-create " *Gnus-output*")))
11014     (save-excursion
11015       (set-buffer tmpbuf)
11016       (buffer-disable-undo (current-buffer))
11017       (erase-buffer)
11018       (insert-buffer-substring artbuf)
11019       ;; Append newline at end of the buffer as separator, and then
11020       ;; save it to file.
11021       (goto-char (point-max))
11022       (insert "\n")
11023       (append-to-file (point-min) (point-max) file-name))
11024     (kill-buffer tmpbuf)))
11025
11026 (defun gnus-convert-article-to-rmail ()
11027   "Convert article in current buffer to Rmail message format."
11028   (let ((buffer-read-only nil))
11029     ;; Convert article directly into Babyl format.
11030     ;; Suggested by Rob Austein <sra@lcs.mit.edu>
11031     (goto-char (point-min))
11032     (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
11033     (while (search-forward "\n\^_" nil t) ;single char
11034       (replace-match "\n^_" t t))               ;2 chars: "^" and "_"
11035     (goto-char (point-max))
11036     (insert "\^_")))
11037
11038 (defun gnus-narrow-to-page (&optional arg)
11039   "Make text outside current page invisible except for page delimiter.
11040 A numeric arg specifies to move forward or backward by that many pages,
11041 thus showing a page other than the one point was originally in."
11042   (interactive "P")
11043   (setq arg (if arg (prefix-numeric-value arg) 0))
11044   (save-excursion
11045     (forward-page -1)                   ;Beginning of current page.
11046     (widen)
11047     (if (> arg 0)
11048         (forward-page arg)
11049       (if (< arg 0)
11050           (forward-page (1- arg))))
11051     ;; Find the end of the page.
11052     (forward-page)
11053     ;; If we stopped due to end of buffer, stay there.
11054     ;; If we stopped after a page delimiter, put end of restriction
11055     ;; at the beginning of that line.
11056     ;; These are commented out.
11057     ;;    (if (save-excursion (beginning-of-line)
11058     ;;                  (looking-at page-delimiter))
11059     ;;  (beginning-of-line))
11060     (narrow-to-region (point)
11061                       (progn
11062                         ;; Find the top of the page.
11063                         (forward-page -1)
11064                         ;; If we found beginning of buffer, stay there.
11065                         ;; If extra text follows page delimiter on same line,
11066                         ;; include it.
11067                         ;; Otherwise, show text starting with following line.
11068                         (if (and (eolp) (not (bobp)))
11069                             (forward-line 1))
11070                         (point)))))
11071
11072 (defun gnus-gmt-to-local ()
11073   "Rewrite Date header described in GMT to local in current buffer.
11074 Intended to be used with gnus-article-prepare-hook."
11075   (save-excursion
11076     (save-restriction
11077       (widen)
11078       (goto-char (point-min))
11079       (narrow-to-region (point-min)
11080                         (progn (search-forward "\n\n" nil 'move) (point)))
11081       (goto-char (point-min))
11082       (if (re-search-forward "^Date:[ \t]\\(.*\\)$" nil t)
11083           (let ((buffer-read-only nil)
11084                 (date (buffer-substring-no-properties
11085                        (match-beginning 1) (match-end 1))))
11086             (delete-region (match-beginning 1) (match-end 1))
11087             (insert
11088              (timezone-make-date-arpa-standard 
11089               date nil (current-time-zone))))))))
11090
11091
11092 ;; Article mode commands
11093
11094 (defun gnus-article-next-page (&optional lines)
11095   "Show next page of current article.
11096 If end of article, return non-nil. Otherwise return nil.
11097 Argument LINES specifies lines to be scrolled up."
11098   (interactive "P")
11099   (move-to-window-line -1)
11100   ;; Fixed by enami@ptgd.sony.co.jp (enami tsugutomo)
11101   (if (save-excursion
11102         (end-of-line)
11103         (and (pos-visible-in-window-p)  ;Not continuation line.
11104              (eobp)))
11105       ;; Nothing in this page.
11106       (if (or (not gnus-break-pages)
11107               (save-excursion
11108                 (save-restriction
11109                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
11110           t                             ;Nothing more.
11111         (gnus-narrow-to-page 1)         ;Go to next page.
11112         nil)
11113     ;; More in this page.
11114     (condition-case ()
11115         (scroll-up lines)
11116       (end-of-buffer
11117        ;; Long lines may cause an end-of-buffer error.
11118        (goto-char (point-max))))
11119     nil))
11120
11121 (defun gnus-article-prev-page (&optional lines)
11122   "Show previous page of current article.
11123 Argument LINES specifies lines to be scrolled down."
11124   (interactive "P")
11125   (move-to-window-line 0)
11126   (if (and gnus-break-pages
11127            (bobp)
11128            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
11129       (progn
11130         (gnus-narrow-to-page -1) ;Go to previous page.
11131         (goto-char (point-max))
11132         (recenter -1))
11133     (scroll-down lines)))
11134
11135 (defun gnus-article-refer-article ()
11136   "Read article specified by message-id around point."
11137   (interactive)
11138   (search-forward ">" nil t)    ;Move point to end of "<....>".
11139   (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
11140       (let ((message-id
11141              (buffer-substring (match-beginning 1) (match-end 1))))
11142         (set-buffer gnus-summary-buffer)
11143         (gnus-summary-refer-article message-id))
11144     (error "No references around point")))
11145
11146 (defun gnus-article-show-summary ()
11147   "Reconfigure windows to show summary buffer."
11148   (interactive)
11149   (gnus-configure-windows 'article)
11150   (gnus-summary-goto-subject gnus-current-article))
11151
11152 (defun gnus-article-describe-briefly ()
11153   "Describe article mode commands briefly."
11154   (interactive)
11155   (gnus-message 6
11156    (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")))
11157
11158 (defun gnus-article-summary-command ()
11159   "Execute the last keystroke in the summary buffer."
11160   (interactive)
11161   (let ((obuf (current-buffer))
11162         (owin (current-window-configuration))
11163         func)
11164     (switch-to-buffer gnus-summary-buffer 'norecord)
11165     (setq func (lookup-key (current-local-map) (this-command-keys)))
11166     (call-interactively func)
11167     (set-buffer obuf)
11168     (set-window-configuration owin)
11169     (set-window-start (get-buffer-window (current-buffer)) (point))))
11170
11171 (defun gnus-article-summary-command-nosave ()
11172   "Execute the last keystroke in the summary buffer."
11173   (interactive)
11174   (let (func)
11175     (pop-to-buffer gnus-summary-buffer 'norecord)
11176     (setq func (lookup-key (current-local-map) (this-command-keys)))
11177     (call-interactively func)))
11178
11179 \f
11180 ;; Basic ideas by emv@math.lsa.umich.edu (Edward Vielmetti)
11181
11182 ;;;###autoload
11183 (defalias 'gnus-batch-kill 'gnus-batch-score)
11184 ;;;###autoload
11185 (defun gnus-batch-score ()
11186   "Run batched scoring.
11187 Usage: emacs -batch -l gnus -f gnus-batch-score <newsgroups> ...
11188 Newsgroups is a list of strings in Bnews format.  If you want to score
11189 the comp hierarchy, you'd say \"comp.all\". If you would not like to
11190 score the alt hierarchy, you'd say \"!alt.all\"."
11191   (interactive)
11192   (let* ((yes-and-no
11193           (gnus-newsrc-parse-options
11194            (apply (function concat)
11195                   (mapcar (lambda (g) (concat g " "))
11196                           command-line-args-left))))
11197          (gnus-expert-user t)
11198          (nnmail-spool-file nil)
11199          (gnus-use-dribble-file nil)
11200          (yes (car yes-and-no))
11201          (no (cdr yes-and-no))
11202          group newsrc entry
11203          ;; Disable verbose message.
11204          gnus-novice-user gnus-large-newsgroup)
11205     ;; Eat all arguments.
11206     (setq command-line-args-left nil)
11207     ;; Start Gnus.
11208     (gnus)
11209     ;; Apply kills to specified newsgroups in command line arguments.
11210     (setq newsrc (cdr gnus-newsrc-alist))
11211     (while newsrc
11212       (setq group (car (car newsrc)))
11213       (setq entry (gnus-gethash group gnus-newsrc-hashtb))
11214       (if (and (<= (nth 1 (car newsrc)) gnus-level-subscribed)
11215                (and (car entry)
11216                     (or (eq (car entry) t)
11217                         (not (zerop (car entry)))))
11218                (if yes (string-match yes group) t)
11219                (or (null no) (not (string-match no group))))
11220           (progn
11221             (gnus-summary-read-group group nil t)
11222             (and (eq (current-buffer) (get-buffer gnus-summary-buffer))
11223                  (gnus-summary-exit))))
11224       (setq newsrc (cdr newsrc)))
11225     ;; Exit Emacs.
11226     (switch-to-buffer gnus-group-buffer)
11227     (gnus-group-save-newsrc)))
11228
11229 (defun gnus-apply-kill-file ()
11230   "Apply a kill file to the current newsgroup.
11231 Returns the number of articles marked as read."
11232   (if (or (file-exists-p (gnus-newsgroup-kill-file nil))
11233           (file-exists-p (gnus-newsgroup-kill-file gnus-newsgroup-name)))
11234       (gnus-apply-kill-file-internal)
11235     0))
11236
11237 (defun gnus-kill-save-kill-buffer ()
11238   (save-excursion
11239     (let ((file (gnus-newsgroup-kill-file gnus-newsgroup-name)))
11240       (if (get-file-buffer file)
11241           (progn
11242             (set-buffer (get-file-buffer file))
11243             (and (buffer-modified-p) (save-buffer))
11244             (kill-buffer (current-buffer)))))))
11245
11246 (defvar gnus-kill-file-name "KILL"
11247   "Suffix of the kill files.")
11248
11249 (defun gnus-newsgroup-kill-file (newsgroup)
11250   "Return the name of a kill file name for NEWSGROUP.
11251 If NEWSGROUP is nil, return the global kill file name instead."
11252   (cond ((or (null newsgroup)
11253              (string-equal newsgroup ""))
11254          ;; The global KILL file is placed at top of the directory.
11255          (expand-file-name gnus-kill-file-name
11256                            (or gnus-kill-files-directory "~/News")))
11257         ((gnus-use-long-file-name 'not-kill)
11258          ;; Append ".KILL" to newsgroup name.
11259          (expand-file-name (concat newsgroup "." gnus-kill-file-name)
11260                            (or gnus-kill-files-directory "~/News")))
11261         (t
11262          ;; Place "KILL" under the hierarchical directory.
11263          (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
11264                                    "/" gnus-kill-file-name)
11265                            (or gnus-kill-files-directory "~/News")))))
11266
11267 \f
11268 ;;;
11269 ;;; Dribble file
11270 ;;;
11271
11272 (defvar gnus-dribble-ignore nil)
11273 (defvar gnus-dribble-eval-file nil)
11274
11275 (defun gnus-dribble-file-name ()
11276   (concat gnus-current-startup-file "-dribble"))
11277
11278 (defun gnus-dribble-enter (string)
11279   (if (and (not gnus-dribble-ignore)
11280            gnus-dribble-buffer
11281            (buffer-name gnus-dribble-buffer))
11282       (let ((obuf (current-buffer)))
11283         (set-buffer gnus-dribble-buffer)
11284         (insert string "\n")
11285         (set-window-point (get-buffer-window (current-buffer)) (point-max))
11286         (set-buffer obuf))))
11287
11288 (defun gnus-dribble-read-file ()
11289   (let ((dribble-file (gnus-dribble-file-name)))
11290     (save-excursion 
11291       (set-buffer (setq gnus-dribble-buffer 
11292                         (get-buffer-create 
11293                          (file-name-nondirectory dribble-file))))
11294       (gnus-add-current-to-buffer-list)
11295       (erase-buffer)
11296       (set-visited-file-name dribble-file)
11297       (buffer-disable-undo (current-buffer))
11298       (bury-buffer (current-buffer))
11299       (set-buffer-modified-p nil)
11300       (let ((auto (make-auto-save-file-name))
11301             (gnus-dribble-ignore t))
11302         (if (or (file-exists-p auto) (file-exists-p dribble-file))
11303             (progn
11304               (if (file-newer-than-file-p auto dribble-file)
11305                   (setq dribble-file auto))
11306               (insert-file-contents dribble-file)
11307               (if (not (zerop (buffer-size)))
11308                   (set-buffer-modified-p t))
11309               (if (gnus-y-or-n-p 
11310                    "Auto-save file exists. Do you want to read it? ")
11311                   (setq gnus-dribble-eval-file t))))))))
11312
11313 (defun gnus-dribble-eval-file ()
11314   (if (not gnus-dribble-eval-file)
11315       ()
11316     (setq gnus-dribble-eval-file nil)
11317     (save-excursion
11318       (set-buffer gnus-dribble-buffer)
11319       (eval-buffer (current-buffer)))))
11320
11321 (defun gnus-dribble-delete-file ()
11322   (if (file-exists-p (gnus-dribble-file-name))
11323       (delete-file (gnus-dribble-file-name)))
11324   (if gnus-dribble-buffer
11325       (save-excursion
11326         (set-buffer gnus-dribble-buffer)
11327         (let ((auto (make-auto-save-file-name)))
11328           (if (file-exists-p auto)
11329               (delete-file auto))
11330           (erase-buffer)
11331           (set-buffer-modified-p nil)))))
11332
11333 (defun gnus-dribble-save ()
11334   (if (and gnus-dribble-buffer
11335            (buffer-name gnus-dribble-buffer))
11336       (save-excursion
11337         (set-buffer gnus-dribble-buffer)
11338         (save-buffer))))
11339
11340 (defun gnus-dribble-clear ()
11341   (save-excursion
11342     (if (gnus-buffer-exists-p gnus-dribble-buffer)
11343         (progn
11344           (set-buffer gnus-dribble-buffer)
11345           (erase-buffer)
11346           (set-buffer-modified-p nil)
11347           (setq buffer-saved-size (buffer-size))))))
11348
11349 ;;;
11350 ;;; Server Communication
11351 ;;;
11352
11353 ;; All the Gnus backends have the same interface, and should return
11354 ;; data in a similar format. Below is an overview of what functions
11355 ;; these packages must supply and what results they should return.
11356 ;;
11357 ;; Variables:
11358 ;;
11359 ;; `nntp-server-buffer' - All data should be returned to Gnus in this
11360 ;; buffer. 
11361 ;;
11362 ;; Functions for the imaginary backend `choke':
11363 ;;
11364 ;; `choke-retrieve-headers ARTICLES &optional GROUP SERVER'
11365 ;; Should return all headers for all ARTICLES, or return NOV lines for
11366 ;; the same.
11367 ;;
11368 ;; `choke-request-group GROUP &optional SERVER DISCARD'
11369 ;; Switch to GROUP. If DISCARD is nil, active information on the group
11370 ;; must be returned.
11371 ;;
11372 ;; `choke-close-group GROUP &optional SERVER'
11373 ;; Close group. Most backends won't have to do anything with this
11374 ;; call, but it is an opportunity to clean up, if that is needed. It
11375 ;; is called when Gnus exits a group.
11376 ;;
11377 ;; `choke-request-article ARTICLE &optional GROUP SERVER'
11378 ;; Return ARTICLE, which is either an article number or
11379 ;; message-id. Note that not all backends can return articles based on
11380 ;; message-id. 
11381 ;;
11382 ;; `choke-request-list SERVER'
11383 ;; Return a list of all newsgroups on SERVER.
11384 ;;
11385 ;; `choke-request-list-newsgroups SERVER'
11386 ;; Return a list of descriptions of all newsgroups on SERVER.
11387 ;;
11388 ;; `choke-request-newgroups DATE &optional SERVER'
11389 ;; Return a list of all groups that have arrived after DATE on
11390 ;; SERVER. Note that the date doesn't have to be respected - Gnus will
11391 ;; always check whether the groups are old or not. Backends that do
11392 ;; not store date information may just return the entire list of
11393 ;; groups, although this might not be a good idea in general.
11394 ;;
11395 ;; `choke-request-post-buffer METHOD HEADER ARTICLE-BUFFER GROUP INFO'
11396 ;; Should return a buffer that is suitable for "posting". nnspool and
11397 ;; nntp return a `*post-buffer*', and nnmail return a `*mail*'
11398 ;; buffer. This function should fill out the appropriate headers. 
11399 ;;
11400 ;; `choke-request-post &optional SERVER'
11401 ;; Function that will be called from a buffer to be posted. 
11402 ;;
11403 ;; `choke-open-server SERVER &optional ARGUMENT'
11404 ;; Open a connection to SERVER.
11405 ;;
11406 ;; `choke-close-server &optional SERVER'
11407 ;; Close the connection to SERVER.
11408 ;;
11409 ;; `choke-server-opened &optional SERVER'
11410 ;; Whether the conenction to SERVER is opened or not.
11411 ;;
11412 ;; `choke-server-status &optional SERVER'
11413 ;; Should return a status string (not in the nntp buffer, but as the
11414 ;; result of the function).
11415 ;;
11416 ;; `choke-retrieve-groups GROUPS &optional SERVER'
11417 ;; Optional function for retrieving active file info on all groups in
11418 ;; GROUPS.  Two return formats are supported: The normal active file
11419 ;; format, and a list of GROUP lines.  This function should return (as
11420 ;; a function value) either `active' or `group', depending on what
11421 ;; format it returns.
11422 ;;
11423 ;; The following functions are optional and apply only to backends
11424 ;; that are able to control the contents of their groups totally
11425 ;; (ie. mail backends.)  Backends that aren't able to do that
11426 ;; shouldn't define these functions at all. Gnus will check for their
11427 ;; presence before attempting to call them.
11428 ;;
11429 ;; `choke-request-expire-articles ARTICLES &optional NEWSGROUP SERVER'
11430 ;; Should expire (according to some aging scheme) all ARTICLES. Most
11431 ;; backends will not be able to expire articles. Should return a list
11432 ;; of all articles that were not expired.
11433 ;;
11434 ;; `choke-request-move-article ARTICLE GROUP SERVER ACCEPT-FORM &optional LAST'
11435 ;; Should move ARTICLE from GROUP on SERVER by using ACCEPT-FORM.
11436 ;; Removes any information it has added to the article (extra headers,
11437 ;; whatever - make it as clean as possible), and then passes the
11438 ;; article on by evaling ACCEPT-FORM, which is normally a call to the
11439 ;; function described below. If the ACCEPT-FORM returns a non-nil
11440 ;; value, the article should then be deleted. If LAST is nil, that
11441 ;; means that there will be further calls to this function. This might
11442 ;; be taken as an advice not to save buffers/internal variables just
11443 ;; yet, but wait until the last call to speed things up.
11444 ;;
11445 ;; `choke-request-accept-article GROUP &optional LAST' 
11446 ;; The contents of the current buffer will be put into GROUP.  There
11447 ;; should, of course, be an article in the current buffer.  This
11448 ;; function is normally only called by the function described above,
11449 ;; and LAST works the same way as in that function.
11450 ;;
11451 ;; `choke-request-replace-article ARTICLE GROUP BUFFER'
11452 ;; Replace ARTICLE in GROUP with the contents of BUFFER.
11453 ;; This provides an easy interface for allowing editing of
11454 ;; articles. Note that even headers may be edited, so the backend has
11455 ;; to update any tables (nov buffers, etc) that it maintains after
11456 ;; replacing the article.
11457 ;;
11458 ;; `choke-request-create-group GROUP &optional SERVER'
11459 ;; Create GROUP on SERVER.  This might be a new, empty group, or it
11460 ;; might be a group that already exists, but hasn't been registered
11461 ;; yet. 
11462 ;;
11463 ;; All these functions must return nil if they couldn't service the
11464 ;; request. If the optional arguments are not supplied, some "current"
11465 ;; or "default" values should be used. In short, one should emulate an
11466 ;; NNTP server, in a way.
11467 ;;
11468 ;; If you want to write a new backend, you just have to supply the
11469 ;; functions listed above. In addition, you must enter the new backend
11470 ;; into the list of valid select methods:
11471 ;; (setq gnus-valid-select-methods 
11472 ;;       (cons '("choke" mail) gnus-valid-select-methods))
11473 ;; The first element in this list is the name of the backend. Other
11474 ;; elemnets may be `mail' (for mail groups),  `post' (for news
11475 ;; groups), `none' (neither), `respool' (for groups that can control
11476 ;; their contents). 
11477
11478 (defun gnus-start-news-server (&optional confirm)
11479   "Open a method for getting news.
11480 If CONFIRM is non-nil, the user will be asked for an NNTP server."
11481   (let (how)
11482     (if gnus-current-select-method
11483         ;; Stream is already opened.
11484         nil
11485       ;; Open NNTP server.
11486       (if (null gnus-nntp-service) (setq gnus-nntp-server nil))
11487       (if confirm
11488           (progn
11489             ;; Read server name with completion.
11490             (setq gnus-nntp-server
11491                   (completing-read "NNTP server: "
11492                                    (mapcar (lambda (server) (list server))
11493                                            (cons (list gnus-nntp-server)
11494                                                  gnus-secondary-servers))
11495                                    nil nil gnus-nntp-server))))
11496
11497       (if (and gnus-nntp-server 
11498                (stringp gnus-nntp-server)
11499                (not (string= gnus-nntp-server "")))
11500           (setq gnus-select-method
11501                 (cond ((or (string= gnus-nntp-server "")
11502                            (string= gnus-nntp-server "::"))
11503                        (list 'nnspool (system-name)))
11504                       ((string-match "^:" gnus-nntp-server)
11505                        (list 'nnmh gnus-nntp-server 
11506                              (list 'nnmh-directory 
11507                                    (file-name-as-directory
11508                                     (expand-file-name
11509                                      (concat "~/" (substring
11510                                                    gnus-nntp-server 1)))))
11511                              (list 'nnmh-get-new-mail nil)))
11512                       (t
11513                        (list 'nntp gnus-nntp-server)))))
11514
11515       (setq how (car gnus-select-method))
11516       (cond ((eq how 'nnspool)
11517              (require 'nnspool)
11518              (gnus-message 5 "Looking up local news spool..."))
11519             ((eq how 'nnmh)
11520              (require 'nnmh)
11521              (gnus-message 5 "Looking up mh spool..."))
11522             (t
11523              (require 'nntp)))
11524       (setq gnus-current-select-method gnus-select-method)
11525       (run-hooks 'gnus-open-server-hook)
11526       (or 
11527        ;; gnus-open-server-hook might have opened it
11528        (gnus-server-opened gnus-select-method)  
11529        (gnus-open-server gnus-select-method)
11530        (gnus-y-or-n-p
11531         (format
11532          "%s server on %s can't be opened. Continue? "
11533          (car gnus-select-method) (nth 1 gnus-select-method)))
11534        (progn
11535          (gnus-message 1 "Couldn't open server on %s" 
11536                        (nth 1 gnus-select-method))
11537          (ding)
11538          nil)))))
11539
11540 (defun gnus-check-news-server (&optional method)
11541   "If the news server is down, start it up again."
11542   (let ((method (if method method gnus-select-method)))
11543     (and (stringp method)
11544          (setq method (gnus-server-to-method method)))
11545     (if (gnus-server-opened method)
11546         ;; Stream is already opened.
11547         t
11548       ;; Open server.
11549       (gnus-message 5 "Opening server %s on %s..." (car method) (nth 1 method))
11550       (run-hooks 'gnus-open-server-hook)
11551       (or (gnus-server-opened method)
11552           (gnus-open-server method))
11553       (message ""))))
11554
11555 (defun gnus-nntp-message (&optional message)
11556   "Check the status of the NNTP server.
11557 If the status of the server is clear and MESSAGE is non-nil, MESSAGE
11558 is returned insted of the status string."
11559   (let ((status (gnus-status-message (gnus-find-method-for-group 
11560                                       gnus-newsgroup-name)))
11561         (message (or message "")))
11562     (if (and (stringp status) (> (length status) 0))
11563         status message)))
11564
11565 (defun gnus-get-function (method function)
11566   (and (stringp method)
11567        (setq method (gnus-server-to-method method)))
11568   (let ((func (intern (format "%s-%s" (car method) function))))
11569     (if (not (fboundp func)) 
11570         (progn
11571           (require (car method))
11572           (if (not (fboundp func)) 
11573               (error "No such function: %s" func))))
11574     func))
11575
11576 ;;; Interface functions to the backends.
11577
11578 (defun gnus-open-server (method)
11579   (funcall (gnus-get-function method 'open-server)
11580            (nth 1 method) (nthcdr 2 method)))
11581
11582 (defun gnus-close-server (method)
11583   (funcall (gnus-get-function method 'close-server) (nth 1 method)))
11584
11585 (defun gnus-request-list (method)
11586   (funcall (gnus-get-function method 'request-list) (nth 1 method)))
11587
11588 (defun gnus-request-list-newsgroups (method)
11589   (funcall (gnus-get-function method 'request-list-newsgroups) (nth 1 method)))
11590
11591 (defun gnus-request-newgroups (date method)
11592   (funcall (gnus-get-function method 'request-newgroups) 
11593            date (nth 1 method)))
11594
11595 (defun gnus-server-opened (method)
11596   (funcall (gnus-get-function method 'server-opened) (nth 1 method)))
11597
11598 (defun gnus-status-message (method)
11599   (let ((method (if (stringp method) (gnus-find-method-for-group method)
11600                   method)))
11601     (funcall (gnus-get-function method 'status-message) (nth 1 method))))
11602
11603 (defun gnus-request-group (group &optional dont-check)
11604   (let ((method (gnus-find-method-for-group group)))
11605 ;    (and t (message "%s GROUP %s" (car method) group))
11606     (funcall (gnus-get-function method 'request-group) 
11607              (gnus-group-real-name group) (nth 1 method) dont-check)))
11608
11609 (defun gnus-request-asynchronous (group &optional articles)
11610   (let ((method (gnus-find-method-for-group group)))
11611     (funcall (gnus-get-function method 'request-asynchronous) 
11612              (gnus-group-real-name group) (nth 1 method) articles)))
11613
11614 (defun gnus-list-active-group (group)
11615   (let ((method (gnus-find-method-for-group group))
11616         (func 'list-active-group))
11617     (and (gnus-check-backend-function func group)
11618          (funcall (gnus-get-function method func) 
11619                   (gnus-group-real-name group) (nth 1 method)))))
11620
11621 (defun gnus-request-group-description (group)
11622   (let ((method (gnus-find-method-for-group group))
11623         (func 'request-group-description))
11624     (and (gnus-check-backend-function func group)
11625          (funcall (gnus-get-function method func) 
11626                   (gnus-group-real-name group) (nth 1 method)))))
11627
11628 (defun gnus-close-group (group)
11629   (let ((method (gnus-find-method-for-group group)))
11630     (funcall (gnus-get-function method 'close-group) 
11631              (gnus-group-real-name group) (nth 1 method))))
11632
11633 (defun gnus-retrieve-headers (articles group)
11634   (let ((method (gnus-find-method-for-group group)))
11635     (if (and gnus-use-cache (numberp (car articles)))
11636         (gnus-cache-retrieve-headers articles group)
11637       (funcall (gnus-get-function method 'retrieve-headers) 
11638                articles (gnus-group-real-name group) (nth 1 method)))))
11639
11640 (defun gnus-retrieve-groups (groups method)
11641   (funcall (gnus-get-function method 'retrieve-groups) groups (nth 1 method)))
11642
11643 (defun gnus-request-article (article group &optional buffer)
11644   (let ((method (gnus-find-method-for-group group)))
11645     (funcall (gnus-get-function method 'request-article) 
11646              article (gnus-group-real-name group) (nth 1 method) buffer)))
11647
11648 (defun gnus-request-head (article group)
11649   (let ((method (gnus-find-method-for-group group)))
11650     (funcall (gnus-get-function method 'request-head) 
11651              article (gnus-group-real-name group) (nth 1 method))))
11652
11653 ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
11654 (defun gnus-request-post-buffer (post group subject header artbuf
11655                                       info follow-to respect-poster)
11656    (let* ((info (or info (and group (nth 2 (gnus-gethash 
11657                                             group gnus-newsrc-hashtb)))))
11658           (method
11659            (if (and gnus-post-method
11660                     ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
11661                     (memq 'post (assoc
11662                                  (format "%s" (car (gnus-find-method-for-group
11663                                                     gnus-newsgroup-name)))
11664                                         gnus-valid-select-methods)))
11665                gnus-post-method
11666              (gnus-find-method-for-group gnus-newsgroup-name))))
11667      (or (gnus-server-opened method)
11668          (gnus-open-server method)
11669          (error "Can't open server %s:%s" (car method) (nth 1 method)))
11670      (let ((mail-self-blind nil)
11671            (mail-archive-file-name nil))
11672        (funcall (gnus-get-function method 'request-post-buffer) 
11673                 post group subject header artbuf info follow-to
11674                 respect-poster))))
11675
11676 (defun gnus-request-post (method &optional force)
11677   (and (stringp method)
11678        (setq method (gnus-server-to-method method)))
11679   (and (not force) gnus-post-method
11680        (memq 'post (assoc (format "%s" (car method))
11681                           gnus-valid-select-methods))
11682        (setq method gnus-post-method))
11683   (funcall (gnus-get-function method 'request-post) 
11684            (nth 1 method)))
11685
11686 (defun gnus-request-expire-articles (articles group &optional force)
11687   (let ((method (gnus-find-method-for-group group)))
11688     (funcall (gnus-get-function method 'request-expire-articles) 
11689              articles (gnus-group-real-name group) (nth 1 method)
11690              force)))
11691
11692 (defun gnus-request-move-article 
11693   (article group server accept-function &optional last)
11694   (let ((method (gnus-find-method-for-group group)))
11695     (funcall (gnus-get-function method 'request-move-article) 
11696              article (gnus-group-real-name group) 
11697              (nth 1 method) accept-function last)))
11698
11699 (defun gnus-request-accept-article (group &optional last)
11700   (let ((func (if (symbolp group) group
11701                 (car (gnus-find-method-for-group group)))))
11702     (funcall (intern (format "%s-request-accept-article" func))
11703              (if (stringp group) (gnus-group-real-name group) group)
11704              last)))
11705
11706 (defun gnus-request-replace-article (article group buffer)
11707   (let ((func (car (gnus-find-method-for-group group))))
11708     (funcall (intern (format "%s-request-replace-article" func))
11709              article (gnus-group-real-name group) buffer)))
11710
11711 (defun gnus-request-create-group (group)
11712   (let ((method (gnus-find-method-for-group group)))
11713     (funcall (gnus-get-function method 'request-create-group) 
11714              (gnus-group-real-name group) (nth 1 method))))
11715
11716 (defun gnus-member-of-valid (symbol group)
11717   (memq symbol (assoc
11718                 (format "%s" (car (gnus-find-method-for-group group)))
11719                 gnus-valid-select-methods)))
11720
11721 (defsubst gnus-secondary-method-p (method)
11722   (member method gnus-secondary-select-methods))
11723
11724 (defun gnus-find-method-for-group (group &optional info)
11725   (or gnus-override-method
11726       (and (not group)
11727            gnus-select-method)
11728       (let ((info (or info (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
11729             method)
11730         (if (or (not info)
11731                 (not (setq method (nth 4 info))))
11732             (setq method gnus-select-method)
11733           (setq method
11734                 (cond ((stringp method)
11735                        (gnus-server-to-method method))
11736                       ((stringp (car method))
11737                        (gnus-server-extend-method group method))
11738                       (t
11739                        method))))
11740         (gnus-server-add-address method))))
11741
11742 (defun gnus-check-backend-function (func group)
11743   (let ((method (if (stringp group) (car (gnus-find-method-for-group group))
11744                  group)))
11745     (fboundp (intern (format "%s-%s" method func)))))
11746
11747 (defun gnus-methods-using (method)
11748   (let ((valids gnus-valid-select-methods)
11749         outs)
11750     (while valids
11751       (if (memq method (car valids)) 
11752           (setq outs (cons (car valids) outs)))
11753       (setq valids (cdr valids)))
11754     outs))
11755
11756 ;;; 
11757 ;;; Active & Newsrc File Handling
11758 ;;;
11759
11760 ;; Newsrc related functions.
11761 ;; Gnus internal format of gnus-newsrc-alist:
11762 ;; (("alt.general" 3 (1 . 1))
11763 ;;  ("alt.misc"    3 ((1 . 10) (12 . 15)))
11764 ;;  ("alt.test"    7 (1 . 99) (45 57 93)) ...)
11765 ;; The first item is the group name; the second is the subscription
11766 ;; level; the third is either a range of a list of ranges of read
11767 ;; articles, the optional fourth element is a list of marked articles,
11768 ;; the optional fifth element is the select method.
11769 ;;
11770 ;; Gnus internal format of gnus-newsrc-hashtb:
11771 ;; (95 ("alt.general" 3 (1 . 1)) ("alt.misc" 3 ((1 . 10) (12 . 15))) ...)
11772 ;; This is the entry for "alt.misc". The first element is the number
11773 ;; of unread articles in "alt.misc". The cdr of this entry is the
11774 ;; element *before* "alt.misc" in gnus-newsrc-alist, which makes is
11775 ;; trivial to remove or add new elements into gnus-newsrc-alist
11776 ;; without scanning the entire list. So, to get the actual information
11777 ;; of "alt.misc", you'd say something like 
11778 ;; (nth 2 (gnus-gethash "alt.misc" gnus-newsrc-hashtb))
11779 ;;
11780 ;; Gnus internal format of gnus-active-hashtb:
11781 ;; ((1 . 1))
11782 ;;  (5 . 10))
11783 ;;  (67 . 99)) ...)
11784 ;; The only element in each entry in this hash table is a range of
11785 ;; (possibly) available articles. (Articles in this range may have
11786 ;; been expired or canceled.)
11787 ;;
11788 ;; Gnus internal format of gnus-killed-list and gnus-zombie-list:
11789 ;; ("alt.misc" "alt.test" "alt.general" ...)
11790
11791 (defun gnus-setup-news (&optional rawfile level)
11792   "Setup news information.
11793 If RAWFILE is non-nil, the .newsrc file will also be read.
11794 If LEVEL is non-nil, the news will be set up at level LEVEL."
11795   (let ((init (not (and gnus-newsrc-alist gnus-active-hashtb (not rawfile)))))
11796     ;; Clear some variables to re-initialize news information.
11797     (if init (setq gnus-newsrc-alist nil 
11798                    gnus-active-hashtb nil))
11799
11800     ;; Read the newsrc file and create `gnus-newsrc-hashtb'.
11801     (if init (gnus-read-newsrc-file rawfile))
11802
11803     ;; Read the active file and create `gnus-active-hashtb'.
11804     ;; If `gnus-read-active-file' is nil, then we just create an empty
11805     ;; hash table. The partial filling out of the hash table will be
11806     ;; done in `gnus-get-unread-articles'.
11807     (if (and gnus-read-active-file 
11808              (not level))
11809         (gnus-read-active-file)
11810       (setq gnus-active-hashtb (make-vector 4095 0)))
11811
11812     ;; Possibly eval the dribble file.
11813     (and init gnus-use-dribble-file (gnus-dribble-eval-file))
11814
11815     ;; Find the number of unread articles in each non-dead group.
11816     (gnus-get-unread-articles (or level (1+ gnus-level-subscribed)))
11817     ;; Find new newsgroups and treat them.
11818     (if (and init gnus-check-new-newsgroups gnus-read-active-file (not level)
11819              (gnus-server-opened gnus-select-method))
11820         (gnus-find-new-newsgroups))
11821     (if (and init gnus-check-bogus-newsgroups 
11822              gnus-read-active-file (not level)
11823              (gnus-server-opened gnus-select-method))
11824         (gnus-check-bogus-newsgroups))))
11825
11826 (defun gnus-find-new-newsgroups ()
11827   "Search for new newsgroups and add them.
11828 Each new newsgroup will be treated with `gnus-subscribe-newsgroup-method.'
11829 The `-n' option line from .newsrc is respected."
11830   (interactive)
11831   (or (gnus-check-first-time-used)
11832       (if (or (consp gnus-check-new-newsgroups)
11833               (eq gnus-check-new-newsgroups 'ask-server))
11834           (gnus-ask-server-for-new-groups)
11835         (let ((groups 0)
11836               group new-newsgroups)
11837           (gnus-message 5 "Checking for new newsgroups...")
11838           (or gnus-have-read-active-file (gnus-read-active-file))
11839           (setq gnus-newsrc-last-checked-date (current-time-string))
11840           (if (not gnus-killed-hashtb) (gnus-make-hashtable-from-killed))
11841           ;; Go though every newsgroup in `gnus-active-hashtb' and compare
11842           ;; with `gnus-newsrc-hashtb' and `gnus-killed-hashtb'.
11843           (mapatoms
11844            (lambda (sym)
11845              (if (or (null (setq group (symbol-name sym)))
11846                      (gnus-gethash group gnus-killed-hashtb)
11847                      (gnus-gethash group gnus-newsrc-hashtb))
11848                  ()
11849                (let ((do-sub (gnus-matches-options-n group)))
11850                  (cond ((eq do-sub 'subscribe)
11851                         (setq groups (1+ groups))
11852                         (gnus-sethash group group gnus-killed-hashtb)
11853                         (funcall 
11854                          gnus-subscribe-options-newsgroup-method group))
11855                        ((eq do-sub 'ignore)
11856                         nil)
11857                        (t
11858                         (setq groups (1+ groups))
11859                         (gnus-sethash group group gnus-killed-hashtb)
11860                         (if gnus-subscribe-hierarchical-interactive
11861                             (setq new-newsgroups (cons group new-newsgroups))
11862                           (funcall gnus-subscribe-newsgroup-method group)))))))
11863            gnus-active-hashtb)
11864           (if new-newsgroups 
11865               (gnus-subscribe-hierarchical-interactive new-newsgroups))
11866           ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
11867           (if (> groups 0)
11868               (gnus-message 6 "%d new newsgroup%s arrived." 
11869                             groups (if (> groups 1) "s have" " has"))
11870             (gnus-message 6 "No new newsgroups."))))))
11871
11872 (defun gnus-matches-options-n (group)
11873   ;; Returns `subscribe' if the group is to be uncoditionally
11874   ;; subscribed, `ignore' if it is to be ignored, and nil if there is
11875   ;; no match for the group.
11876
11877   ;; First we check the two user variables.
11878   (cond
11879    ((and gnus-options-subscribe
11880          (string-match gnus-options-subscribe group))
11881     'subscribe)
11882    ((and gnus-options-not-subscribe
11883          (string-match gnus-options-not-subscribe group))
11884     'ignore)
11885    ;; Then we go through the list that was retrieved from the .newsrc
11886    ;; file.  This list has elements on the form 
11887    ;; `(REGEXP . {ignore,subscribe})'. The first match found (the list
11888    ;; is in the reverse order of the options line) is returned.
11889    (t
11890     (let ((regs gnus-newsrc-options-n))
11891       (while (and regs
11892                   (not (string-match (car (car regs)) group)))
11893         (setq regs (cdr regs)))
11894       (and regs (cdr (car regs)))))))
11895
11896 (defun gnus-ask-server-for-new-groups ()
11897   (let* ((date (or gnus-newsrc-last-checked-date (current-time-string)))
11898          (methods (cons gnus-select-method 
11899                         (append
11900                          (and (consp gnus-check-new-newsgroups)
11901                               gnus-check-new-newsgroups)
11902                          gnus-secondary-select-methods)))
11903          (groups 0)
11904          (new-date (current-time-string))
11905          hashtb group new-newsgroups got-new)
11906     ;; Go thorugh both primary and secondary select methods and
11907     ;; request new newsgroups.  
11908     (while methods
11909       (and (or (gnus-server-opened (car methods))
11910                (gnus-open-server (car methods)))
11911            (gnus-request-newgroups date (car methods))
11912            (save-excursion
11913              (setq got-new t)
11914              (set-buffer nntp-server-buffer)
11915              (or hashtb (setq hashtb (gnus-make-hashtable 
11916                                       (count-lines (point-min) (point-max)))))
11917              ;; Enter all the new groups in a hashtable.
11918              (gnus-active-to-gnus-format (car methods) hashtb 'ignore)))
11919       (setq methods (cdr methods)))
11920     (and got-new (setq gnus-newsrc-last-checked-date new-date))
11921     ;; Now all new groups from all select methods are in `hashtb'.
11922     (mapatoms
11923      (lambda (group-sym)
11924        (setq group (symbol-name group-sym))
11925        (if (or (gnus-gethash group gnus-newsrc-hashtb)
11926                (member group gnus-zombie-list)
11927                (member group gnus-killed-list))
11928            ;; The group is already known.
11929            ()
11930          (and (symbol-value group-sym)
11931               (gnus-sethash group (symbol-value group-sym) gnus-active-hashtb))
11932          (let ((do-sub (gnus-matches-options-n group)))
11933            (cond ((eq do-sub 'subscribe)
11934                   (setq groups (1+ groups))
11935                   (gnus-sethash group group gnus-killed-hashtb)
11936                   (funcall 
11937                    gnus-subscribe-options-newsgroup-method group))
11938                  ((eq do-sub 'ignore)
11939                   nil)
11940                  (t
11941                   (setq groups (1+ groups))
11942                   (gnus-sethash group group gnus-killed-hashtb)
11943                   (if gnus-subscribe-hierarchical-interactive
11944                       (setq new-newsgroups (cons group new-newsgroups))
11945                     (funcall gnus-subscribe-newsgroup-method group)))))))
11946      hashtb)
11947     (if new-newsgroups 
11948         (gnus-subscribe-hierarchical-interactive new-newsgroups))
11949     ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
11950     (if (> groups 0)
11951         (gnus-message 6 "%d new newsgroup%s arrived." 
11952                       groups (if (> groups 1) "s have" " has")))
11953     got-new))
11954
11955 (defun gnus-check-first-time-used ()
11956   (if (or (> (length gnus-newsrc-alist) 1)
11957           (file-exists-p gnus-startup-file)
11958           (file-exists-p (concat gnus-startup-file ".el"))
11959           (file-exists-p (concat gnus-startup-file ".eld")))
11960       nil
11961     (gnus-message 6 "First time user; subscribing you to default groups")
11962     (or gnus-have-read-active-file (gnus-read-active-file))
11963     (setq gnus-newsrc-last-checked-date (current-time-string))
11964     (let ((groups gnus-default-subscribed-newsgroups)
11965           group)
11966       (if (eq groups t)
11967           nil
11968         (setq groups (or groups gnus-backup-default-subscribed-newsgroups))
11969         (mapatoms
11970          (lambda (sym)
11971            (setq group (symbol-name sym))
11972            (let ((do-sub (gnus-matches-options-n group)))
11973              (cond ((eq do-sub 'subscribe)
11974                     (gnus-sethash group group gnus-killed-hashtb)
11975                     (funcall 
11976                      gnus-subscribe-options-newsgroup-method group))
11977                    ((eq do-sub 'ignore)
11978                     nil)
11979                    (t
11980                     (setq gnus-killed-list (cons group gnus-killed-list))))))
11981          gnus-active-hashtb)
11982         (while groups
11983           (if (gnus-gethash (car groups) gnus-active-hashtb)
11984               (gnus-group-change-level 
11985                (car groups) gnus-level-default-subscribed gnus-level-killed))
11986           (setq groups (cdr groups)))
11987         (gnus-group-make-help-group)
11988         (and gnus-novice-user
11989              (gnus-message 7 "`A k' to list killed groups"))))))
11990
11991 (defun gnus-subscribe-group (group previous &optional method)
11992   (gnus-group-change-level 
11993    (if method
11994        (list t group gnus-level-default-subscribed nil nil method)
11995      group) 
11996    gnus-level-default-subscribed gnus-level-killed previous t))
11997
11998 ;; `gnus-group-change-level' is the fundamental function for changing
11999 ;; subscription levels of newsgroups. This might mean just changing
12000 ;; from level 1 to 2, which is pretty trivial, from 2 to 6 or back
12001 ;; again, which subscribes/unsubscribes a group, which is equally
12002 ;; trivial. Changing from 1-7 to 8-9 means that you kill a group, and
12003 ;; from 8-9 to 1-7 means that you remove the group from the list of
12004 ;; killed (or zombie) groups and add them to the (kinda) subscribed
12005 ;; groups. And last but not least, moving from 8 to 9 and 9 to 8,
12006 ;; which is trivial.
12007 ;; ENTRY can either be a string (newsgroup name) or a list (if
12008 ;; FROMKILLED is t, it's a list on the format (NUM INFO-LIST),
12009 ;; otherwise it's a list in the format of the `gnus-newsrc-hashtb'
12010 ;; entries. 
12011 ;; LEVEL is the new level of the group, OLDLEVEL is the old level and
12012 ;; PREVIOUS is the group (in hashtb entry format) to insert this group
12013 ;; after. 
12014 (defun gnus-group-change-level (entry level &optional oldlevel
12015                                       previous fromkilled)
12016   (let (group info active num)
12017     ;; Glean what info we can from the arguments
12018     (if (consp entry)
12019         (if fromkilled (setq group (nth 1 entry))
12020           (setq group (car (nth 2 entry))))
12021       (setq group entry))
12022     (if (and (stringp entry)
12023              oldlevel 
12024              (< oldlevel gnus-level-zombie))
12025         (setq entry (gnus-gethash entry gnus-newsrc-hashtb)))
12026     (if (and (not oldlevel)
12027              (consp entry))
12028         (setq oldlevel (car (cdr (nth 2 entry)))))
12029     (if (stringp previous)
12030         (setq previous (gnus-gethash previous gnus-newsrc-hashtb)))
12031
12032     (if (and (>= oldlevel gnus-level-zombie)
12033              (gnus-gethash group gnus-newsrc-hashtb))
12034         ;; We are trying to subscribe a group that is already
12035         ;; subscribed. 
12036         () ; Do nothing. 
12037
12038       (gnus-dribble-enter
12039        (format "(gnus-group-change-level %S %S %S %S %S)" 
12040                group level oldlevel (car (nth 2 previous)) fromkilled))
12041     
12042       ;; Then we remove the newgroup from any old structures, if needed.
12043       ;; If the group was killed, we remove it from the killed or zombie
12044       ;; list. If not, and it is in fact going to be killed, we remove
12045       ;; it from the newsrc hash table and assoc.
12046       (cond ((>= oldlevel gnus-level-zombie)
12047              (if (= oldlevel gnus-level-zombie)
12048                  (setq gnus-zombie-list (delete group gnus-zombie-list))
12049                (setq gnus-killed-list (delete group gnus-killed-list))))
12050             (t
12051              (if (and (>= level gnus-level-zombie)
12052                       entry)
12053                  (progn
12054                    (gnus-sethash (car (nth 2 entry)) nil gnus-newsrc-hashtb)
12055                    (if (nth 3 entry)
12056                        (setcdr (gnus-gethash (car (nth 3 entry))
12057                                              gnus-newsrc-hashtb)
12058                                (cdr entry)))
12059                    (setcdr (cdr entry) (cdr (cdr (cdr entry))))))))
12060
12061       ;; Finally we enter (if needed) the list where it is supposed to
12062       ;; go, and change the subscription level. If it is to be killed,
12063       ;; we enter it into the killed or zombie list.
12064       (cond ((>= level gnus-level-zombie)
12065              ;; Remove from the hash table.
12066              (gnus-sethash group nil gnus-newsrc-hashtb)
12067              (or (gnus-group-foreign-p group)
12068                  ;; We do not enter foreign groups into the list of dead
12069                  ;; groups.  
12070                  (if (= level gnus-level-zombie)
12071                      (setq gnus-zombie-list (cons group gnus-zombie-list))
12072                    (setq gnus-killed-list (cons group gnus-killed-list)))))
12073             (t
12074              ;; If the list is to be entered into the newsrc assoc, and
12075              ;; it was killed, we have to create an entry in the newsrc
12076              ;; hashtb format and fix the pointers in the newsrc assoc.
12077              (if (>= oldlevel gnus-level-zombie)
12078                  (progn
12079                    (if (listp entry)
12080                        (progn
12081                          (setq info (cdr entry))
12082                          (setq num (car entry)))
12083                      (setq active (gnus-gethash group gnus-active-hashtb))
12084                      (setq num (if active (- (1+ (cdr active)) (car active)) t))
12085                      ;; Check whether the group is foreign. If so, the
12086                      ;; foreign select method has to be entered into the
12087                      ;; info. 
12088                      (let ((method (gnus-group-method-name group)))
12089                        (if (eq method gnus-select-method)
12090                            (setq info (list group level nil))
12091                          (setq info (list group level nil nil method)))))
12092                    (or previous 
12093                        (setq previous 
12094                              (let ((p gnus-newsrc-alist))
12095                                (while (cdr (cdr p))
12096                                  (setq p (cdr p)))
12097                                p)))
12098                    (setq entry (cons info (cdr (cdr previous))))
12099                    (if (cdr previous)
12100                        (progn
12101                          (setcdr (cdr previous) entry)
12102                          (gnus-sethash group (cons num (cdr previous)) 
12103                                        gnus-newsrc-hashtb))
12104                      (setcdr previous entry)
12105                      (gnus-sethash group (cons num previous)
12106                                    gnus-newsrc-hashtb))
12107                    (if (cdr entry)
12108                        (setcdr (gnus-gethash (car (car (cdr entry)))
12109                                              gnus-newsrc-hashtb)
12110                                entry)))
12111                ;; It was alive, and it is going to stay alive, so we
12112                ;; just change the level and don't change any pointers or
12113                ;; hash table entries.
12114                (setcar (cdr (car (cdr (cdr entry)))) level)))))))
12115
12116 (defun gnus-kill-newsgroup (newsgroup)
12117   "Obsolete function. Kills a newsgroup."
12118   (gnus-group-change-level
12119    (gnus-gethash newsgroup gnus-newsrc-hashtb) gnus-level-killed))
12120
12121 (defun gnus-check-bogus-newsgroups (&optional confirm)
12122   "Remove bogus newsgroups.
12123 If CONFIRM is non-nil, the user has to confirm the deletion of every
12124 newsgroup." 
12125   (let ((newsrc (cdr gnus-newsrc-alist))
12126         bogus group entry)
12127     (gnus-message 5 "Checking bogus newsgroups...")
12128     (or gnus-have-read-active-file (gnus-read-active-file))
12129     ;; Find all bogus newsgroup that are subscribed.
12130     (while newsrc
12131       (setq group (car (car newsrc)))
12132       (if (or (gnus-gethash group gnus-active-hashtb) ; Active
12133               (nth 4 (car newsrc))      ; Foreign
12134               (and confirm
12135                    (not (gnus-y-or-n-p
12136                          (format "Remove bogus newsgroup: %s " group)))))
12137           ;; Don't remove.
12138           ()
12139         ;; Found a bogus newsgroup.
12140         (setq bogus (cons group bogus)))
12141       (setq newsrc (cdr newsrc)))
12142     ;; Remove all bogus subscribed groups by first killing them, and
12143     ;; then removing them from the list of killed groups.
12144     (while bogus
12145       (and (setq entry (gnus-gethash (car bogus) gnus-newsrc-hashtb))
12146            (progn
12147              (gnus-group-change-level entry gnus-level-killed)
12148              (setq gnus-killed-list (delete (car bogus) gnus-killed-list))))
12149       (setq bogus (cdr bogus)))
12150     ;; Then we remove all bogus groups from the list of killed and
12151     ;; zombie groups. They are are removed without confirmation.
12152     (let ((dead-lists '(gnus-killed-list gnus-zombie-list))
12153           killed)
12154       (while dead-lists
12155         (setq killed (symbol-value (car dead-lists)))
12156         (while killed
12157           (setq group (car killed))
12158           (or (gnus-gethash group gnus-active-hashtb)
12159               ;; The group is bogus.
12160               (set (car dead-lists)
12161                    (delete group (symbol-value (car dead-lists)))))
12162           (setq killed (cdr killed)))
12163         (setq dead-lists (cdr dead-lists))))
12164     (gnus-message 5 "Checking bogus newsgroups...done")))
12165
12166 (defun gnus-check-duplicate-killed-groups ()
12167   "Remove duplicates from the list of killed groups."
12168   (interactive)
12169   (let ((killed gnus-killed-list))
12170     (while killed
12171       (gnus-message 9 "%d" (length killed))
12172       (setcdr killed (delete (car killed) (cdr killed)))
12173       (setq killed (cdr killed)))))
12174
12175 ;; Go though `gnus-newsrc-alist' and compare with `gnus-active-hashtb'
12176 ;; and compute how many unread articles there are in each group.
12177 (defun gnus-get-unread-articles (&optional level) 
12178   (let* ((newsrc (cdr gnus-newsrc-alist))
12179          (level (or level (1+ gnus-level-subscribed)))
12180          (foreign-level
12181           (min 
12182            (cond ((and gnus-activate-foreign-newsgroups 
12183                        (not (numberp gnus-activate-foreign-newsgroups)))
12184                   (1+ gnus-level-subscribed))
12185                  ((numberp gnus-activate-foreign-newsgroups)
12186                   gnus-activate-foreign-newsgroups)
12187                  (t 0))
12188            level))
12189          info group active virtuals method)
12190     (gnus-message 5 "Checking new news...")
12191
12192     (while newsrc
12193       (setq info (car newsrc)
12194             group (car info)
12195             active (gnus-gethash group gnus-active-hashtb))
12196
12197       ;; Check newsgroups. If the user doesn't want to check them, or
12198       ;; they can't be checked (for instance, if the news server can't
12199       ;; be reached) we just set the number of unread articles in this
12200       ;; newsgroup to t. This means that Gnus thinks that there are
12201       ;; unread articles, but it has no idea how many.
12202       (if (and (setq method (nth 4 info))
12203                (not (gnus-server-equal gnus-select-method
12204                                        (gnus-server-get-method nil method)))
12205                (not (gnus-secondary-method-p method)))
12206           ;; These groups are foreign. Check the level.
12207           (if (<= (nth 1 info) foreign-level)
12208               (if (eq (car (if (stringp method) 
12209                                (gnus-server-to-method method)
12210                              (nth 4 info))) 'nnvirtual)
12211                   ;; We have to activate the virtual groups after all
12212                   ;; the others, so we just pop them on a list for
12213                   ;; now. 
12214                   (setq virtuals (cons info virtuals))
12215                 (and (setq active (gnus-activate-newsgroup (car info)))
12216                      ;; Close the groups as we look at them!
12217                      (gnus-close-group group))))
12218                 
12219         ;; These groups are native or secondary. 
12220         (if (and (not gnus-read-active-file)
12221                  (<= (nth 1 info) level))
12222             (setq active (gnus-activate-newsgroup (car info)))))
12223       
12224       (if active
12225           (gnus-get-unread-articles-in-group info active)
12226         ;; The group couldn't be reached, so we nix out the number of
12227         ;; unread articles and stuff.
12228         (gnus-sethash group nil gnus-active-hashtb)
12229         (setcar (gnus-gethash group gnus-newsrc-hashtb) t))
12230
12231       (setq newsrc (cdr newsrc)))
12232
12233     ;; Activate the virtual groups. This has to be done after all the
12234     ;; other groups. 
12235     ;; !!! If one virtual group contains another virtual group, even
12236     ;; doing it this way might cause problems.
12237    (while virtuals
12238       (and (setq active (gnus-activate-newsgroup (car (car virtuals))))
12239            (gnus-get-unread-articles-in-group (car virtuals) active))
12240       (setq virtuals (cdr virtuals)))
12241
12242     (gnus-message 5 "Checking new news...done")))
12243
12244 ;; Create a hash table out of the newsrc alist. The `car's of the
12245 ;; alist elements are used as keys.
12246 (defun gnus-make-hashtable-from-newsrc-alist ()
12247   (let ((alist gnus-newsrc-alist)
12248         (ohashtb gnus-newsrc-hashtb)
12249         prev)
12250     (setq gnus-newsrc-hashtb (gnus-make-hashtable (length alist)))
12251     (setq alist 
12252           (setq prev (setq gnus-newsrc-alist 
12253                            (if (equal (car (car gnus-newsrc-alist))
12254                                       "dummy.group")
12255                                gnus-newsrc-alist
12256                              (cons (list "dummy.group" 0 nil) alist)))))
12257     (while alist
12258       (gnus-sethash (car (car alist)) 
12259                     (cons (and ohashtb (car (gnus-gethash 
12260                                              (car (car alist)) ohashtb))) 
12261                           prev) gnus-newsrc-hashtb)
12262       (setq prev alist
12263             alist (cdr alist)))))
12264
12265 (defun gnus-make-hashtable-from-killed ()
12266   "Create a hash table from the killed and zombie lists."
12267   (let ((lists '(gnus-killed-list gnus-zombie-list))
12268         list)
12269     (setq gnus-killed-hashtb 
12270           (gnus-make-hashtable 
12271            (+ (length gnus-killed-list) (length gnus-zombie-list))))
12272     (while lists
12273       (setq list (symbol-value (car lists)))
12274       (setq lists (cdr lists))
12275       (while list
12276         (gnus-sethash (car list) (car list) gnus-killed-hashtb)
12277         (setq list (cdr list))))))
12278
12279 (defun gnus-get-unread-articles-in-group (info active)
12280   (let* ((range (nth 2 info))
12281          (num 0)
12282          (marked (nth 3 info)))
12283     ;; If a cache is present, we may have to alter the active info.
12284     (and gnus-use-cache
12285          (gnus-cache-possibly-alter-active (car info) active))
12286     ;; Modify the list of read articles according to what articles 
12287     ;; are available; then tally the unread articles and add the
12288     ;; number to the group hash table entry.
12289     (cond 
12290      ((zerop (cdr active))
12291       (setq num 0))
12292      ((not range)
12293       (setq num (- (1+ (cdr active)) (car active))))
12294      ((not (listp (cdr range)))
12295       ;; Fix a single (num . num) range according to the
12296       ;; active hash table.
12297       ;; Fix by Carsten Bormann <cabo@Informatik.Uni-Bremen.DE>.
12298       (and (< (cdr range) (car active)) (setcdr range (1- (car active))))
12299       (and (> (cdr range) (cdr active)) (setcdr range (cdr active)))
12300       ;; Compute number of unread articles.
12301       (setq num (max 0 (- (cdr active) (- (1+ (cdr range)) (car range))))))
12302      (t
12303       ;; The read list is a list of ranges. Fix them according to
12304       ;; the active hash table.
12305       ;; First peel off any elements that are below the lower
12306       ;; active limit. 
12307       (while (and (cdr range) 
12308                   (>= (car active) 
12309                       (or (and (atom (car (cdr range))) (car (cdr range)))
12310                           (car (car (cdr range))))))
12311         (if (numberp (car range))
12312             (setcar range 
12313                     (cons (car range) 
12314                           (or (and (numberp (car (cdr range)))
12315                                    (car (cdr range))) 
12316                               (cdr (car (cdr range))))))
12317           (setcdr (car range) 
12318                   (or (and (numberp (nth 1 range)) (nth 1 range))
12319                       (cdr (car (cdr range))))))
12320         (setcdr range (cdr (cdr range))))
12321       ;; Adjust the first element to be the same as the lower limit. 
12322       (if (and (not (atom (car range))) 
12323                (< (cdr (car range)) (car active)))
12324           (setcdr (car range) (1- (car active))))
12325       ;; Then we want to peel off any elements that are higher
12326       ;; than the upper active limit.  
12327       (let ((srange range))
12328         ;; Go past all legal elements.
12329         (while (and (cdr srange) 
12330                     (<= (or (and (atom (car (cdr srange)))
12331                                  (car (cdr srange)))
12332                             (car (car (cdr srange)))) (cdr active)))
12333           (setq srange (cdr srange)))
12334         (if (cdr srange)
12335             ;; Nuke all remaining illegal elements.
12336             (setcdr srange nil))
12337
12338         ;; Adjust the final element.
12339         (if (and (not (atom (car srange)))
12340                  (> (cdr (car srange)) (cdr active)))
12341             (setcdr (car srange) (cdr active))))
12342       ;; Compute the number of unread articles.
12343       (while range
12344         (setq num (+ num (- (1+ (or (and (atom (car range)) (car range))
12345                                     (cdr (car range))))
12346                             (or (and (atom (car range)) (car range))
12347                                 (car (car range))))))
12348         (setq range (cdr range)))
12349       (setq num (max 0 (- (cdr active) num)))))
12350     (and info
12351          (progn
12352            (and (assq 'tick marked)
12353                 (inline (gnus-remove-illegal-marked-articles
12354                          (assq 'tick marked) (nth 2 info))))
12355            (and (assq 'dormant marked)
12356                 (inline (gnus-remove-illegal-marked-articles
12357                          (assq 'dormant marked) (nth 2 info))))
12358            (setcar
12359             (gnus-gethash (car info) gnus-newsrc-hashtb) 
12360             (setq num (max 0 (- num (length (cdr (assq 'tick marked)))
12361                                 (length (cdr (assq 'dormant marked)))))))))
12362     num))
12363
12364 (defun gnus-remove-illegal-marked-articles (marked ranges)
12365   (let ((m (cdr marked)))
12366     ;; Make sure that all ticked articles are a subset of the unread
12367     ;; articles. 
12368     (while m
12369       (if (gnus-member-of-range (car m) ranges)
12370           (setcdr marked (cdr m))
12371         (setq marked m))
12372       (setq m (cdr m)))))
12373
12374 (defun gnus-activate-newsgroup (group)
12375   (let ((method (gnus-find-method-for-group group))
12376         active)
12377     (and (or (gnus-server-opened method) (gnus-open-server method))
12378          (gnus-request-group group)
12379          (save-excursion
12380            (set-buffer nntp-server-buffer)
12381            (goto-char (point-min))
12382            (and (looking-at "[0-9]+ [0-9]+ \\([0-9]+\\) [0-9]+")
12383                 (progn
12384                   (goto-char (match-beginning 1))
12385                   (gnus-sethash 
12386                    group (setq active (cons (read (current-buffer))
12387                                             (read (current-buffer))))
12388                    gnus-active-hashtb))
12389                 active)))))
12390
12391 (defun gnus-update-read-articles 
12392   (group unread unselected ticked &optional domarks replied expirable killed
12393          dormant bookmark score)
12394   "Update the list of read and ticked articles in GROUP using the
12395 UNREAD and TICKED lists.
12396 Note: UNSELECTED has to be sorted over `<'.
12397 Returns whether the updating was successful."
12398   (let* ((active (or gnus-newsgroup-active 
12399                      (gnus-gethash group gnus-active-hashtb)))
12400          (entry (gnus-gethash group gnus-newsrc-hashtb))
12401          (info (nth 2 entry))
12402          (marked (nth 3 info))
12403          (prev 1)
12404          (unread (sort (copy-sequence unread) (function <)))
12405          read)
12406     (if (or (not info) (not active))
12407         ;; There is no info on this group if it was, in fact,
12408         ;; killed. Gnus stores no information on killed groups, so
12409         ;; there's nothing to be done. 
12410         ;; One could store the information somewhere temporarily,
12411         ;; perhaps... Hmmm... 
12412         ()
12413       ;; Remove any negative articles numbers.
12414       (while (and unread (< (car unread) 0))
12415         (setq unread (cdr unread)))
12416       ;; Remove any expired article numbers
12417       (while (and unread (< (car unread) (car active)))
12418         (setq unread (cdr unread)))
12419       (while (and ticked (< (car ticked) (car active)))
12420         (setq ticked (cdr ticked)))
12421       (while (and dormant (< (car dormant) (car active)))
12422         (setq dormant (cdr dormant)))
12423       (setq unread (sort (append unselected unread) '<))
12424       ;; Set the number of unread articles in gnus-newsrc-hashtb.
12425       (setcar entry (max 0 (- (length unread) (length ticked) 
12426                               (length dormant))))
12427       ;; Compute the ranges of read articles by looking at the list of
12428       ;; unread articles.  
12429       (while unread
12430         (if (/= (car unread) prev)
12431             (setq read (cons (if (= prev (1- (car unread))) prev
12432                                (cons prev (1- (car unread)))) read)))
12433         (setq prev (1+ (car unread)))
12434         (setq unread (cdr unread)))
12435       (if (<= prev (cdr active))
12436           (setq read (cons (cons prev (cdr active)) read)))
12437       ;; Enter this list into the group info.
12438       (setcar (cdr (cdr info)) 
12439               (if (> (length read) 1) (nreverse read) read))
12440       ;; Enter the list of ticked articles.
12441       (gnus-set-marked-articles 
12442        info ticked
12443        (if domarks replied (cdr (assq 'reply marked)))
12444        (if domarks expirable (cdr (assq 'expire marked)))
12445        (if domarks killed (cdr (assq 'killed marked)))
12446        (if domarks dormant (cdr (assq 'dormant marked)))
12447        (if domarks bookmark (cdr (assq 'bookmark marked)))
12448        (if domarks score (cdr (assq 'score marked))))
12449       t)))
12450
12451 (defun gnus-make-articles-unread (group articles)
12452   "Mark ARTICLES in GROUP as unread."
12453   (let* ((info (nth 2 (or (gnus-gethash group gnus-newsrc-hashtb)
12454                           (gnus-gethash (gnus-group-real-name group)
12455                                         gnus-newsrc-hashtb))))
12456          (ranges (nth 2 info))
12457          news)
12458     (while articles
12459       (and (gnus-member-of-range (car articles) ranges)
12460            (setq news (cons (car articles) news)))
12461       (setq articles (cdr articles)))
12462     (if (not news)
12463         ()
12464       (setcar (nthcdr 2 info)
12465               (gnus-remove-from-range (nth 2 info) (nreverse news)))
12466       (gnus-group-update-group group t))))
12467
12468 ;; Get the active file(s) from the backend(s).
12469 (defun gnus-read-active-file ()
12470   (gnus-group-set-mode-line)
12471   (let ((methods (if (or (gnus-server-opened gnus-select-method)
12472                          (gnus-open-server gnus-select-method))
12473                      ;; The native server is available.
12474                      (cons gnus-select-method gnus-secondary-select-methods)
12475                    ;; The native server is down, so we just do the
12476                    ;; secondary ones.   
12477                    gnus-secondary-select-methods))
12478         list-type)
12479     (setq gnus-have-read-active-file nil)
12480     (save-excursion
12481       (set-buffer nntp-server-buffer)
12482       (while methods
12483         (let* ((where (nth 1 (car methods)))
12484                (mesg (format "Reading active file%s via %s..."
12485                              (if (and where (not (zerop (length where))))
12486                                  (concat " from " where) "")
12487                              (car (car methods)))))
12488           (gnus-message 5 mesg)
12489           (gnus-check-news-server (car methods))
12490           (cond 
12491            ((and (eq gnus-read-active-file 'some)
12492                  (gnus-check-backend-function
12493                   'retrieve-groups (car (car methods))))
12494             (let ((newsrc (cdr gnus-newsrc-alist))
12495                   groups)
12496               (while newsrc
12497                 (and (gnus-server-equal 
12498                       (gnus-find-method-for-group
12499                        (car (car newsrc)) (car newsrc))
12500                       (gnus-server-get-method nil (car methods)))
12501                      (setq groups (cons (car (car newsrc)) groups)))
12502                 (setq newsrc (cdr newsrc)))
12503               (setq list-type (gnus-retrieve-groups groups (car methods)))
12504               (cond ((not list-type)
12505                      (gnus-message 
12506                       1 "Cannot read partial active file from %s server." 
12507                       (car (car methods)))
12508                      (ding)
12509                      (sit-for 2))
12510                     ((eq list-type 'active)
12511                      (gnus-active-to-gnus-format (car methods)))
12512                     (t
12513                      (gnus-groups-to-gnus-format (car methods))))))
12514            (t
12515             (if (not (gnus-request-list (car methods)))
12516                 (progn
12517                   (gnus-message 1 "Cannot read active file from %s server." 
12518                                 (car (car methods)))
12519                   (ding))
12520               (gnus-active-to-gnus-format (car methods))
12521               ;; We mark this active file as read.
12522               (setq gnus-have-read-active-file
12523                     (cons (car methods) gnus-have-read-active-file))
12524               (gnus-message 5 "%sdone" mesg)))))
12525         (setq methods (cdr methods))))))
12526
12527 ;; Read an active file and place the results in `gnus-active-hashtb'.
12528 (defun gnus-active-to-gnus-format (method &optional hashtb ignore-errors)
12529   (let ((cur (current-buffer))
12530         (hashtb (or hashtb 
12531                     (if (and gnus-active-hashtb 
12532                              (not (equal method gnus-select-method)))
12533                         gnus-active-hashtb
12534                       (setq gnus-active-hashtb
12535                             (if (equal method gnus-select-method)
12536                                 (gnus-make-hashtable 
12537                                  (count-lines (point-min) (point-max)))
12538                               (gnus-make-hashtable 4096))))))
12539         (flag-hashtb (gnus-make-hashtable 60)))
12540     ;; Delete unnecessary lines.
12541     (goto-char (point-min))
12542     (while (search-forward "\nto." nil t)
12543       (delete-region (1+ (match-beginning 0)) 
12544                      (progn (forward-line 1) (point))))
12545     (or (string= gnus-ignored-newsgroups "")
12546         (progn
12547           (goto-char (point-min))
12548           (delete-matching-lines gnus-ignored-newsgroups)))
12549     ;; If these are groups from a foreign select method, we insert the
12550     ;; group prefix in front of the group names. 
12551     (and method (not (eq method gnus-select-method))
12552          (let ((prefix (gnus-group-prefixed-name "" method)))
12553            (goto-char (point-min))
12554            (while (and (not (eobp))
12555                        (progn (insert prefix)
12556                               (zerop (forward-line 1)))))))
12557     (goto-char (point-min))
12558     ;; Store active file in hashtable.
12559     (goto-char (point-min))
12560     (if (string-match "%[oO]" gnus-group-line-format)
12561         ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
12562         ;; If we want information on moderated groups, we use this
12563         ;; loop...   
12564         (let* ((mod-hashtb (make-vector 7 0))
12565                (m (intern "m" mod-hashtb))
12566                group max min)
12567           (while (not (eobp))
12568             (condition-case nil
12569                 (progn
12570                   (narrow-to-region (point) (gnus-point-at-eol))
12571                   (setq group (let ((obarray hashtb)) (read cur)))
12572                   (if (and (numberp (setq max (read cur)))
12573                            (numberp (setq min (read cur)))
12574                            (progn 
12575                              (skip-chars-forward " \t")
12576                              (not
12577                               (or (= (following-char) ?=)
12578                                   (= (following-char) ?x)
12579                                   (= (following-char) ?j)))))
12580                       (set group (cons min max))
12581                     (set group nil))
12582                   ;; Enter moderated groups into a list.
12583                   (if (eq (let ((obarray mod-hashtb)) (read cur)) m)
12584                       (setq gnus-moderated-list 
12585                             (cons (symbol-name group) gnus-moderated-list))))
12586               (error nil))
12587             (widen)
12588             (forward-line 1)))
12589       ;; And if we do not care about moderation, we use this loop,
12590       ;; which is faster.
12591       (let (group max min)
12592         (while (not (eobp))
12593           (condition-case ()
12594               (progn
12595                 (narrow-to-region (point) (gnus-point-at-eol))
12596                 ;; group gets set to a symbol interned in the hash table
12597                 ;; (what a hack!!) - jwz
12598                 (setq group (let ((obarray hashtb)) (read cur)))
12599                 (if (and (numberp (setq max (read cur)))
12600                          (numberp (setq min (read cur)))
12601                          (progn 
12602                            (skip-chars-forward " \t")
12603                            (not
12604                             (or (= (following-char) ?=)
12605                                 (= (following-char) ?x)
12606                                 (= (following-char) ?j)))))
12607                     (set group (cons min max))
12608                   (set group nil)))
12609             (error 
12610              (progn 
12611                (if ignore-errors
12612                    (set group nil)
12613                  (ding) 
12614                  (gnus-message 3 "Warning - illegal active: %s"
12615                                (buffer-substring 
12616                                 (gnus-point-at-bol) (gnus-point-at-eol)))
12617                  nil))))
12618           (widen)
12619           (forward-line 1))))))
12620
12621 (defun gnus-groups-to-gnus-format (method &optional hashtb)
12622   ;; Parse a "groups" active file.
12623   (let ((cur (current-buffer))
12624         (hashtb (or hashtb 
12625                     (if method
12626                         gnus-active-hashtb
12627                       (setq gnus-active-hashtb
12628                             (gnus-make-hashtable 
12629                              (count-lines (point-min) (point-max)))))))
12630         (prefix (and method (not (eq method gnus-select-method))
12631                      (gnus-group-prefixed-name "" method))))
12632
12633     (goto-char (point-min))
12634     (condition-case ()
12635         ;; We split this into to separate loops, one with the prefix
12636         ;; and one without to speed the reading up somewhat.
12637         (if prefix
12638             (let (min max opoint)
12639               (while (not (eobp))
12640                 (read cur) (read cur)
12641                 (setq min (read cur)
12642                       max (read cur)
12643                       opoint (point))
12644                 (skip-chars-forward " \t")
12645                 (insert prefix)
12646                 (goto-char opoint)
12647                 (set (let ((obarray hashtb)) (read cur)) 
12648                      (cons min max))
12649                 (forward-line 1)))
12650           (let (min max)
12651             (while (not (eobp))
12652               (if (= (following-char) ?2)
12653                   (progn
12654                     (read cur) (read cur)
12655                     (setq min (read cur)
12656                           max (read cur))
12657                     (set (let ((obarray hashtb)) (read cur)) 
12658                          (cons min max))))
12659               (forward-line 1))))
12660       (error 
12661        (progn (ding) (gnus-message 3 "Possible error in active file."))))))
12662
12663 (defun gnus-read-newsrc-file (&optional force)
12664   "Read startup file.
12665 If FORCE is non-nil, the .newsrc file is read."
12666   ;; Reset variables that might be defined in the .newsrc.eld file.
12667   (let ((variables gnus-variable-list))
12668     (while variables
12669       (set (car variables) nil)
12670       (setq variables (cdr variables))))
12671   (let* ((newsrc-file gnus-current-startup-file)
12672          (quick-file (concat newsrc-file ".el")))
12673     (save-excursion
12674       ;; We always load the .newsrc.eld file. If always contains
12675       ;; much information that can not be gotten from the .newsrc
12676       ;; file (ticked articles, killed groups, foreign methods, etc.)
12677       (gnus-read-newsrc-el-file quick-file)
12678  
12679       (if (or force
12680               (and (file-newer-than-file-p newsrc-file quick-file)
12681                    (file-newer-than-file-p newsrc-file 
12682                                            (concat quick-file "d")))
12683               (not gnus-newsrc-alist))
12684           ;; We read the .newsrc file. Note that if there if a
12685           ;; .newsrc.eld file exists, it has already been read, and
12686           ;; the `gnus-newsrc-hashtb' has been created. While reading
12687           ;; the .newsrc file, Gnus will only use the information it
12688           ;; can find there for changing the data already read -
12689           ;; ie. reading the .newsrc file will not trash the data
12690           ;; already read (except for read articles).
12691           (save-excursion
12692             (gnus-message 5 "Reading %s..." newsrc-file)
12693             (set-buffer (find-file-noselect newsrc-file))
12694             (buffer-disable-undo (current-buffer))
12695             (gnus-newsrc-to-gnus-format)
12696             (kill-buffer (current-buffer))
12697             (gnus-message 5 "Reading %s...done" newsrc-file))))))
12698
12699 (defun gnus-read-newsrc-el-file (file)
12700   (let ((ding-file (concat file "d")))
12701     ;; We always, always read the .eld file.
12702     (gnus-message 5 "Reading %s..." ding-file)
12703     (let (gnus-newsrc-assoc)
12704       (condition-case nil
12705           (load ding-file t t t)
12706         (error nil))
12707       (and gnus-newsrc-assoc (setq gnus-newsrc-alist gnus-newsrc-assoc)))
12708     (let ((inhibit-quit t))
12709       (gnus-uncompress-newsrc-alist))
12710     (gnus-make-hashtable-from-newsrc-alist)
12711     (if (not (file-newer-than-file-p file ding-file))
12712         ()
12713       ;; Old format quick file
12714       (gnus-message 5 "Reading %s..." file)
12715       ;; The .el file is newer than the .eld file, so we read that one
12716       ;; as well. 
12717       (gnus-read-old-newsrc-el-file file))))
12718
12719 ;; Parse the old-style quick startup file
12720 (defun gnus-read-old-newsrc-el-file (file)
12721   (let (newsrc killed marked group m)
12722     (prog1
12723         (let ((gnus-killed-assoc nil)
12724               gnus-marked-assoc gnus-newsrc-alist gnus-newsrc-assoc)
12725           (prog1
12726               (condition-case nil
12727                   (load file t t t)
12728                 (error nil))
12729             (setq newsrc gnus-newsrc-assoc
12730                   killed gnus-killed-assoc
12731                   marked gnus-marked-assoc)))
12732       (setq gnus-newsrc-alist nil)
12733       (while newsrc
12734         (setq group (car newsrc))
12735         (let ((info (nth 2 (gnus-gethash (car group) gnus-newsrc-hashtb))))
12736           (if info
12737               (progn
12738                 (setcar (nthcdr 2 info) (cdr (cdr group)))
12739                 (setcar (cdr info)
12740                         (if (nth 1 group) gnus-level-default-subscribed 
12741                           gnus-level-default-unsubscribed))
12742                 (setq gnus-newsrc-alist (cons info gnus-newsrc-alist)))
12743             (setq gnus-newsrc-alist
12744                   (cons 
12745                    (setq info
12746                          (list (car group)
12747                                (if (nth 1 group) gnus-level-default-subscribed
12748                                  gnus-level-default-unsubscribed) 
12749                                (cdr (cdr group))))
12750                    gnus-newsrc-alist)))
12751           (if (setq m (assoc (car group) marked))
12752             (setcdr (cdr (cdr info)) (cons (list (cons 'tick (cdr m))) nil))))
12753         (setq newsrc (cdr newsrc)))
12754       (setq newsrc killed)
12755       (while newsrc
12756         (setcar newsrc (car (car newsrc)))
12757         (setq newsrc (cdr newsrc)))
12758       (setq gnus-killed-list killed))
12759     ;; The .el file version of this variable does not begin with
12760     ;; "options", while the .eld version does, so we just add it if it
12761     ;; isn't there.
12762     (and
12763      gnus-newsrc-options 
12764      (progn
12765        (and (not (string-match "^ *options" gnus-newsrc-options))
12766             (setq gnus-newsrc-options (concat "options " gnus-newsrc-options)))
12767        (and (not (string-match "\n$" gnus-newsrc-options))
12768             (setq gnus-newsrc-options (concat gnus-newsrc-options "\n")))))
12769     (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist))
12770     (gnus-make-hashtable-from-newsrc-alist)))
12771       
12772 (defun gnus-make-newsrc-file (file)
12773   "Make server dependent file name by catenating FILE and server host name."
12774   (let* ((file (expand-file-name file nil))
12775          (real-file (concat file "-" (nth 1 gnus-select-method))))
12776     (if (or (file-exists-p real-file)
12777             (file-exists-p (concat real-file ".el"))
12778             (file-exists-p (concat real-file ".eld")))
12779         real-file file)))
12780
12781 (defun gnus-uncompress-newsrc-alist ()
12782   ;; Uncompress all lists of marked articles in the newsrc assoc.
12783   (let ((newsrc gnus-newsrc-alist)
12784         marked)
12785     (while newsrc
12786       (if (not (setq marked (nth 3 (car newsrc))))
12787           ()
12788         (while marked
12789           (or (eq 'score (car (car marked)))
12790               (eq 'bookmark (car (car marked)))
12791               (eq 'killed (car (car marked)))
12792               (setcdr (car marked) (gnus-uncompress-range (cdr (car marked)))))
12793           (setq marked (cdr marked))))
12794       (setq newsrc (cdr newsrc)))))
12795
12796 (defun gnus-compress-newsrc-alist ()
12797   ;; Compress all lists of marked articles in the newsrc assoc.
12798   (let ((newsrc gnus-newsrc-alist)
12799         marked)
12800     (while newsrc
12801       (if (not (setq marked (nth 3 (car newsrc))))
12802           ()
12803         (while marked
12804           (or (eq 'score (car (car marked)))
12805               (eq 'bookmark (car (car marked)))
12806               (eq 'killed (car (car marked)))
12807               (setcdr (car marked) 
12808                       (condition-case ()
12809                           (gnus-compress-sequence 
12810                            (sort (cdr (car marked)) '<) t)
12811                         (error (cdr (car marked))))))
12812           (setq marked (cdr marked))))
12813       (setq newsrc (cdr newsrc)))))
12814
12815 (defun gnus-newsrc-to-gnus-format ()
12816   (setq gnus-newsrc-options "")
12817   (setq gnus-newsrc-options-n nil)
12818
12819   (or gnus-active-hashtb
12820       (setq gnus-active-hashtb (make-vector 4095 0)))
12821   (let ((buf (current-buffer))
12822         (already-read (> (length gnus-newsrc-alist) 1))
12823         group subscribed options-symbol newsrc Options-symbol
12824         symbol reads num1)
12825     (goto-char (point-min))
12826     ;; We intern the symbol `options' in the active hashtb so that we
12827     ;; can `eq' against it later.
12828     (setq options-symbol (intern "options" gnus-active-hashtb))
12829     (setq Options-symbol (intern "Options" gnus-active-hashtb))
12830   
12831     (while (not (eobp))
12832       ;; We first read the first word on the line by narrowing and
12833       ;; then reading into `gnus-active-hashtb'.  Most groups will
12834       ;; already exist in that hashtb, so this will save some string
12835       ;; space.
12836       (narrow-to-region
12837        (point)
12838        (progn (skip-chars-forward "^ \t!:\n") (point)))
12839       (goto-char (point-min))
12840       (setq symbol 
12841             (and (/= (point-min) (point-max))
12842                  (let ((obarray gnus-active-hashtb)) (read buf))))
12843       (widen)
12844       ;; Now, the symbol we have read is either `options' or a group
12845       ;; name.  If it is an options line, we just add it to a string. 
12846       (cond 
12847        ((or (eq symbol options-symbol)
12848             (eq symbol Options-symbol))
12849         (setq gnus-newsrc-options
12850               ;; This concatting is quite inefficient, but since our
12851               ;; thorough studies show that approx 99.37% of all
12852               ;; .newsrc files only contain a single options line, we
12853               ;; don't give a damn, frankly, my dear.
12854               (concat gnus-newsrc-options
12855                       (buffer-substring 
12856                        (gnus-point-at-bol)
12857                        ;; Options may continue on the next line.
12858                        (or (and (re-search-forward "^[^ \t]" nil 'move)
12859                                 (progn (beginning-of-line) (point)))
12860                            (point))))))
12861        (symbol
12862         ;; It was a group name.
12863         (setq subscribed (= (following-char) ?:)
12864               group (symbol-name symbol)
12865               reads nil)
12866         (if (eolp)
12867             ;; If the line ends here, this is clearly a buggy line, so
12868             ;; we put point a the beginning of line and let the cond
12869             ;; below do the error handling.
12870             (beginning-of-line)
12871           ;; We skip to the beginning of the ranges.
12872           (skip-chars-forward "!: \t"))
12873         ;; We are now at the beginning of the list of read articles.
12874         ;; We read them range by range.
12875         (while
12876             (cond 
12877              ((looking-at "[0-9]+")
12878               ;; We narrow and read a number instead of buffer-substring/
12879               ;; string-to-int because it's faster. narrow/widen is
12880               ;; faster than save-restriction/narrow, and save-restriction
12881               ;; produces a garbage object.
12882               (setq num1 (progn
12883                            (narrow-to-region (match-beginning 0) (match-end 0))
12884                            (read buf)))
12885               (widen)
12886               ;; If the next character is a dash, then this is a range.
12887               (if (= (following-char) ?-)
12888                   (progn
12889                     ;; We read the upper bound of the range.
12890                     (forward-char 1)
12891                     (if (not (looking-at "[0-9]+"))
12892                         ;; This is a buggy line, by we pretend that
12893                         ;; it's kinda OK. Perhaps the user should be
12894                         ;; dinged? 
12895                         (setq reads (cons num1 reads))
12896                       (setq reads 
12897                             (cons 
12898                              (cons num1 (progn
12899                                           (narrow-to-region (match-beginning 0) 
12900                                                             (match-end 0))
12901                                           (read buf)))
12902                              reads))
12903                       (widen)))
12904                 ;; It was just a simple number, so we add it to the
12905                 ;; list of ranges.
12906                 (setq reads (cons num1 reads)))
12907               ;; If the next char in ?\n, then we have reached the end
12908               ;; of the line and return nil.
12909               (/= (following-char) ?\n))
12910              ((= (following-char) ?\n)
12911               ;; End of line, so we end.
12912               nil)
12913              (t
12914               ;; Not numbers and not eol, so this might be a buggy
12915               ;; line... 
12916               (or (eobp) ; If it was eob instead of ?\n, we allow it.
12917                   (progn
12918                     ;; The line was buggy.
12919                     (setq group nil)
12920                     (gnus-message 3 "Mangled line: %s" 
12921                                   (buffer-substring (gnus-point-at-bol) 
12922                                                     (gnus-point-at-eol)))
12923                     (ding)
12924                     (sit-for 1)))
12925               nil))
12926           ;; Skip past ", ". Spaces are illegal in these ranges, but
12927           ;; we allow them, because it's a common mistake to put a
12928           ;; space after the comma.
12929           (skip-chars-forward ", "))
12930
12931         ;; We have already read .newsrc.eld, so we gently update the
12932         ;; data in the hash table with the information we have just
12933         ;; read. 
12934         (if (not group)
12935             ()
12936           (let ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
12937                 level)
12938             (if info
12939                 ;; There is an entry for this file in the alist.
12940                 (progn
12941                   (setcar (nthcdr 2 info) (nreverse reads))
12942                   ;; We update the level very gently.  In fact, we
12943                   ;; only change it if there's been a status change
12944                   ;; from subscribed to unsubscribed, or vice versa.
12945                   (setq level (nth 1 info))
12946                   (cond ((and (<= level gnus-level-subscribed)
12947                               (not subscribed))
12948                          (setq level (if reads
12949                                          gnus-level-default-unsubscribed 
12950                                        (1+ gnus-level-default-unsubscribed))))
12951                         ((and (> level gnus-level-subscribed) subscribed)
12952                          (setq level gnus-level-default-subscribed)))
12953                   (setcar (cdr info) level))
12954               ;; This is a new group.
12955               (setq info (list group 
12956                                (if subscribed
12957                                    gnus-level-default-subscribed 
12958                                  (if reads
12959                                      (1+ gnus-level-subscribed)
12960                                    gnus-level-default-unsubscribed))
12961                                (nreverse reads))))
12962             (setq newsrc (cons info newsrc))))))
12963       (forward-line 1))
12964     
12965     (setq newsrc (nreverse newsrc))
12966
12967     (if (not already-read)
12968         ()
12969       ;; We now have two newsrc lists - `newsrc', which is what we
12970       ;; have read from .newsrc, and `gnus-newsrc-alist', which is
12971       ;; what we've read from .newsrc.eld. We have to merge these
12972       ;; lists. We do this by "attaching" any (foreign) groups in the
12973       ;; gnus-newsrc-alist to the (native) group that precedes them. 
12974       (let ((rc (cdr gnus-newsrc-alist))
12975             (prev gnus-newsrc-alist)
12976             entry mentry)
12977         (while rc
12978           (or (null (nth 4 (car rc))) ; It's a native group.
12979               (assoc (car (car rc)) newsrc) ; It's already in the alist.
12980               (if (setq entry (assoc (car (car prev)) newsrc))
12981                   (setcdr (setq mentry (memq entry newsrc))
12982                           (cons (car rc) (cdr mentry)))
12983                 (setq newsrc (cons (car rc) newsrc))))
12984           (setq prev rc
12985                 rc (cdr rc)))))
12986
12987     (setq gnus-newsrc-alist newsrc)
12988     ;; We make the newsrc hashtb.
12989     (gnus-make-hashtable-from-newsrc-alist)
12990
12991     ;; Finally, if we read some options lines, we parse them.
12992     (or (string= gnus-newsrc-options "")
12993         (gnus-newsrc-parse-options gnus-newsrc-options))))
12994
12995 ;; Parse options lines to find "options -n !all rec.all" and stuff.
12996 ;; The return value will be a list on the form
12997 ;; ((regexp1 . ignore)
12998 ;;  (regexp2 . subscribe)...)
12999 ;; When handling new newsgroups, groups that match a `ignore' regexp
13000 ;; will be ignored, and groups that match a `subscribe' regexp will be
13001 ;; subscribed. A line like
13002 ;; options -n !all rec.all
13003 ;; will lead to a list that looks like
13004 ;; (("^rec\\..+" . subscribe) 
13005 ;;  ("^.+" . ignore))
13006 ;; So all "rec.*" groups will be subscribed, while all the other
13007 ;; groups will be ignored. Note that "options -n !all rec.all" is very
13008 ;; different from "options -n rec.all !all". 
13009 (defun gnus-newsrc-parse-options (options)
13010   (let (out eol)
13011     (save-excursion
13012       (gnus-set-work-buffer)
13013       (insert (regexp-quote options))
13014       ;; First we treat all continuation lines.
13015       (goto-char (point-min))
13016       (while (re-search-forward "\n[ \t]+" nil t)
13017         (replace-match " " t t))
13018       ;; Then we transform all "all"s into ".+"s.
13019       (goto-char (point-min))
13020       (while (re-search-forward "\\ball\\b" nil t)
13021         (replace-match ".+" t t))
13022       (goto-char (point-min))
13023       ;; We remove all other options than the "-n" ones.
13024       (while (re-search-forward "[ \t]-[^n][^-]*" nil t)
13025         (replace-match " ")
13026         (forward-char -1))
13027       (goto-char (point-min))
13028
13029       ;; We are only interested in "options -n" lines - we
13030       ;; ignore the other option lines.
13031       (while (re-search-forward "[ \t]-n" nil t)
13032         (setq eol 
13033               (or (save-excursion
13034                     (and (re-search-forward "[ \t]-n" (gnus-point-at-eol) t)
13035                          (- (point) 2)))
13036                   (gnus-point-at-eol)))
13037         ;; Search for all "words"...
13038         (while (re-search-forward "[^ \t,\n]+" eol t)
13039           (if (= (char-after (match-beginning 0)) ?!)
13040               ;; If the word begins with a bang (!), this is a "not"
13041               ;; spec. We put this spec (minus the bang) and the
13042               ;; symbol `ignore' into the list.
13043               (setq out (cons (cons (concat 
13044                                      "^" (buffer-substring 
13045                                           (1+ (match-beginning 0))
13046                                           (match-end 0)))
13047                                     'ignore) out))
13048             ;; There was no bang, so this is a "yes" spec.
13049             (setq out (cons (cons (concat 
13050                                    "^" (buffer-substring (match-beginning 0)
13051                                                          (match-end 0)))
13052                                   'subscribe) out)))))
13053     
13054       (setq gnus-newsrc-options-n out))))
13055                
13056
13057 (defun gnus-save-newsrc-file ()
13058   "Save .newsrc file."
13059   ;; Note: We cannot save .newsrc file if all newsgroups are removed
13060   ;; from the variable gnus-newsrc-alist.
13061   (and (or gnus-newsrc-alist gnus-killed-list)
13062        gnus-current-startup-file
13063        (progn
13064          (run-hooks 'gnus-save-newsrc-hook)
13065          (save-excursion
13066            (if (and gnus-use-dribble-file
13067                     (or (not gnus-dribble-buffer)
13068                         (not (buffer-name gnus-dribble-buffer))
13069                         (zerop (save-excursion
13070                                  (set-buffer gnus-dribble-buffer)
13071                                  (buffer-size)))))
13072                (gnus-message 4 "(No changes need to be saved)")
13073              (if gnus-save-newsrc-file
13074                  (progn
13075                    (gnus-message 5 "Saving %s..." gnus-current-startup-file)
13076                    ;; Make backup file of master newsrc.
13077                    (gnus-gnus-to-newsrc-format)
13078                    (gnus-message 5 "Saving %s...done"
13079                                  gnus-current-startup-file)))
13080              ;; Quickly loadable .newsrc.
13081              (set-buffer (get-buffer-create " *Gnus-newsrc*"))
13082              (set-visited-file-name (concat gnus-current-startup-file ".eld"))
13083              (gnus-add-current-to-buffer-list)
13084              (buffer-disable-undo (current-buffer))
13085              (erase-buffer)
13086              (gnus-message 5 "Saving %s.eld..." gnus-current-startup-file)
13087              (gnus-gnus-to-quick-newsrc-format)
13088              (save-buffer)
13089              (kill-buffer (current-buffer))
13090              (gnus-message 5 "Saving %s.eld...done" gnus-current-startup-file)
13091              (gnus-dribble-delete-file))))))
13092
13093 (defun gnus-gnus-to-quick-newsrc-format ()
13094   "Insert Gnus variables such as gnus-newsrc-alist in lisp format."
13095   (insert ";; (ding) Gnus startup file.\n")
13096   (insert ";; Never delete this file - touch .newsrc instead to force Gnus\n")
13097   (insert ";; to read .newsrc.\n")
13098   (let ((variables gnus-variable-list)
13099         (inhibit-quit t)
13100         (gnus-newsrc-alist (cdr gnus-newsrc-alist))
13101         variable)
13102     ;; insert lisp expressions.
13103     (gnus-compress-newsrc-alist)
13104     (while variables
13105       (setq variable (car variables))
13106       (and (boundp variable)
13107            (symbol-value variable)
13108            (or gnus-save-killed-list (not (eq variable 'gnus-killed-list)))
13109            (insert "(setq " (symbol-name variable) " '"
13110                    (prin1-to-string (symbol-value variable))
13111                    ")\n"))
13112       (setq variables (cdr variables)))
13113     (gnus-uncompress-newsrc-alist)))
13114
13115
13116 (defun gnus-gnus-to-newsrc-format ()
13117   ;; Generate and save the .newsrc file.
13118   (let ((newsrc (cdr gnus-newsrc-alist))
13119         info ranges range)
13120     (save-excursion
13121       (set-buffer (create-file-buffer gnus-current-startup-file))
13122       (set-visited-file-name gnus-current-startup-file)
13123       (buffer-disable-undo (current-buffer))
13124       (erase-buffer)
13125       ;; Write options.
13126       (if gnus-newsrc-options (insert gnus-newsrc-options))
13127       ;; Write subscribed and unsubscribed.
13128       (while newsrc
13129         (setq info (car newsrc))
13130         (if (not (nth 4 info))          ;Don't write foreign groups to .newsrc.
13131             (progn
13132               (insert (car info) (if (> (nth 1 info) gnus-level-subscribed)
13133                                      "!" ":"))
13134               (if (setq ranges (nth 2 info))
13135                   (progn
13136                     (insert " ")
13137                     (if (not (listp (cdr ranges)))
13138                         (if (= (car ranges) (cdr ranges))
13139                             (insert (int-to-string (car ranges)))
13140                           (insert (int-to-string (car ranges)) "-" 
13141                                   (int-to-string (cdr ranges))))
13142                       (while ranges
13143                         (setq range (car ranges)
13144                               ranges (cdr ranges))
13145                         (if (or (atom range) (= (car range) (cdr range)))
13146                             (insert (int-to-string 
13147                                      (or (and (atom range) range) 
13148                                          (car range))))
13149                           (insert (int-to-string (car range)) "-"
13150                                   (int-to-string (cdr range))))
13151                         (if ranges (insert ","))))))
13152               (insert "\n")))
13153         (setq newsrc (cdr newsrc)))
13154       (save-buffer)
13155       (kill-buffer (current-buffer)))))
13156
13157 (defun gnus-read-all-descriptions-files ()
13158   (let ((methods (cons gnus-select-method gnus-secondary-select-methods)))
13159     (while methods
13160       (gnus-read-descriptions-file (car methods))
13161       (setq methods (cdr methods)))
13162     t))
13163
13164 (defun gnus-read-descriptions-file (&optional method)
13165   (let ((method (or method gnus-select-method)))
13166     ;; We create the hashtable whether we manage to read the desc file
13167     ;; to avoid trying to re-read after a failed read.
13168     (or gnus-description-hashtb
13169         (setq gnus-description-hashtb 
13170               (gnus-make-hashtable (length gnus-active-hashtb))))
13171     ;; Mark this method's desc file as read.
13172     (gnus-sethash (gnus-group-prefixed-name "" method) "Has read"
13173                   gnus-description-hashtb)
13174
13175     (gnus-message 5 "Reading descriptions file via %s..." (car method))
13176     (cond 
13177      ((not (or (gnus-server-opened method)
13178                (gnus-open-server method)))
13179       (gnus-message 1 "Couldn't open server")
13180       nil)
13181      ((not (gnus-request-list-newsgroups method))
13182       (gnus-message 1 "Couldn't read newsgroups descriptions")
13183       nil)
13184      (t
13185       (let (group)
13186         (save-excursion
13187           (save-restriction
13188             (set-buffer nntp-server-buffer)
13189             (goto-char (point-min))
13190             (if (or (search-forward "\n.\n" nil t)
13191                     (goto-char (point-max)))
13192                 (progn
13193                   (beginning-of-line)
13194                   (narrow-to-region (point-min) (point))))
13195             (goto-char (point-min))
13196             (while (not (eobp))
13197               ;; If we get an error, we set group to 0, which is not a
13198               ;; symbol... 
13199               (setq group 
13200                     (condition-case ()
13201                         (let ((obarray gnus-description-hashtb))
13202                           ;; Group is set to a symbol interned in this
13203                           ;; hash table.
13204                           (read nntp-server-buffer))
13205                       (error 0)))
13206               (skip-chars-forward " \t")
13207               ;; ... which leads to this line being effectively ignored.
13208               (and (symbolp group)
13209                    (set group (buffer-substring 
13210                                (point) (progn (end-of-line) (point)))))
13211               (forward-line 1))))
13212         (gnus-message 5 "Reading descriptions file...done")
13213         t)))))
13214
13215 (defun gnus-group-get-description (group)
13216   ;; Get the description of a group by sending XGTITLE to the server.
13217   (and (gnus-request-group-description group)
13218        (save-excursion
13219          (set-buffer nntp-server-buffer)
13220          (goto-char (point-min))
13221          (and (looking-at "[^ \t]+[ \t]+\\(.*\\)")
13222               (buffer-substring (match-beginning 1) (match-end 1))))))
13223
13224 ;;;
13225 ;;; Server
13226 ;;;
13227
13228 (defvar gnus-server-mode-hook nil
13229   "Hook run in `gnus-server-mode' buffers.")
13230
13231 (defconst gnus-server-line-format "     {%(%h:%w%)}\n"
13232   "Format of server lines.
13233 It works along the same lines as a normal formatting string,
13234 with some simple extensions.")
13235
13236 (defvar gnus-server-mode-line-format "(ding) List of servers"
13237   "The format specification for the server mode line.")
13238
13239 (defconst gnus-server-line-format-alist
13240   (list (list ?h 'how ?s)
13241         (list ?n 'name ?s)
13242         (list ?w 'where ?s)
13243         ))
13244
13245 (defconst gnus-server-mode-line-format-alist 
13246   (list (list ?S 'news-server ?s)
13247         (list ?M 'news-method ?s)
13248         (list ?u 'user-defined ?s)))
13249
13250 (defvar gnus-server-line-format-spec nil)
13251 (defvar gnus-server-mode-line-format-spec nil)
13252 (defvar gnus-server-killed-servers nil)
13253
13254 (defvar gnus-server-mode-map nil)
13255 (put 'gnus-server-mode 'mode-class 'special)
13256
13257 (if gnus-server-mode-map
13258     nil
13259   (setq gnus-server-mode-map (make-sparse-keymap))
13260   (suppress-keymap gnus-server-mode-map)
13261   (define-key gnus-server-mode-map " " 'gnus-server-read-server)
13262   (define-key gnus-server-mode-map "\r" 'gnus-server-read-server)
13263   (define-key gnus-server-mode-map gnus-mouse-2 'gnus-server-pick-server)
13264   (define-key gnus-server-mode-map "q" 'gnus-server-exit)
13265   (define-key gnus-server-mode-map "l" 'gnus-server-list-servers)
13266   (define-key gnus-server-mode-map "k" 'gnus-server-kill-server)
13267   (define-key gnus-server-mode-map "y" 'gnus-server-yank-server)
13268   (define-key gnus-server-mode-map "c" 'gnus-server-copy-server)
13269   (define-key gnus-server-mode-map "a" 'gnus-server-add-server)
13270   (define-key gnus-server-mode-map "e" 'gnus-server-edit-server))
13271
13272 (defun gnus-server-mode ()
13273   "Major mode for listing and editing servers.
13274
13275 All normal editing commands are switched off.
13276 \\<gnus-server-mode-map>
13277
13278 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]'). 
13279
13280 The following commands are available:
13281
13282 \\{gnus-server-mode-map}"
13283   (interactive)
13284   (if gnus-visual (gnus-server-make-menu-bar))
13285   (kill-all-local-variables)
13286   (setq mode-line-modified "-- ")
13287   (make-local-variable 'mode-line-format)
13288   (setq mode-line-format (copy-sequence mode-line-format))
13289   (and (equal (nth 3 mode-line-format) "   ")
13290        (setcar (nthcdr 3 mode-line-format) ""))
13291   (setq major-mode 'gnus-server-mode)
13292   (setq mode-name "Server")
13293 ;  (gnus-group-set-mode-line)
13294   (setq mode-line-process nil)
13295   (use-local-map gnus-server-mode-map)
13296   (buffer-disable-undo (current-buffer))
13297   (setq truncate-lines t)
13298   (setq buffer-read-only t)
13299   (run-hooks 'gnus-server-mode-hook))
13300
13301 (defun gnus-server-insert-server-line (sformat name method)
13302   (let* ((sformat (or sformat gnus-server-line-format-spec))
13303          (how (car method))
13304          (where (nth 1 method))
13305          b)
13306     (beginning-of-line)
13307     (setq b (point))
13308     ;; Insert the text.
13309     (insert (eval sformat))
13310     (add-text-properties b (1+ b) (list 'gnus-server (intern name)))))
13311
13312 (defun gnus-server-setup-buffer ()
13313   (if (get-buffer gnus-server-buffer)
13314       ()
13315     (save-excursion
13316       (set-buffer (get-buffer-create gnus-server-buffer))
13317       (gnus-server-mode)
13318       (and gnus-carpal (gnus-carpal-setup-buffer 'server)))))
13319
13320 (defun gnus-server-prepare ()
13321   (setq gnus-server-mode-line-format-spec 
13322         (gnus-parse-format gnus-server-mode-line-format 
13323                            gnus-server-mode-line-format-alist))
13324   (setq gnus-server-line-format-spec 
13325         (gnus-parse-format gnus-server-line-format 
13326                            gnus-server-line-format-alist))
13327   (let ((alist gnus-server-alist)
13328         (buffer-read-only nil))
13329     (erase-buffer)
13330     (while alist
13331       (gnus-server-insert-server-line nil (car (car alist)) (cdr (car alist)))
13332       (setq alist (cdr alist))))
13333   (goto-char (point-min))
13334   (gnus-server-position-cursor))
13335
13336 (defun gnus-server-server-name ()
13337   (let ((server (get-text-property (gnus-point-at-bol) 'gnus-server)))
13338     (and server (symbol-name server))))
13339
13340 (defalias 'gnus-server-position-cursor 'gnus-goto-colon)
13341
13342 (defconst gnus-server-edit-buffer "*Gnus edit server*")
13343
13344 (defun gnus-server-update-server (server)
13345   (save-excursion
13346     (set-buffer gnus-server-buffer)
13347     (let ((buffer-read-only nil)
13348           (info (cdr (assoc server gnus-server-alist))))
13349       (gnus-dribble-enter 
13350        (concat "(gnus-server-set-info \"" server "\" '"
13351                (prin1-to-string info) ")"))
13352       ;; Buffer may be narrowed.
13353       (save-restriction
13354         (widen)
13355         (if (gnus-server-goto-server server)
13356             (delete-region (progn (beginning-of-line) (point))
13357                            (progn (forward-line 1) (point))))
13358         (let ((entry (assoc server gnus-server-alist)))
13359           (gnus-server-insert-server-line nil (car entry) (cdr entry))
13360           (gnus-server-position-cursor))))))
13361
13362 (defun gnus-server-set-info (server info)
13363   ;; Enter a select method into the virtual server alist.
13364   (gnus-dribble-enter 
13365    (concat "(gnus-server-set-info \"" server "\" '"
13366            (prin1-to-string info) ")"))
13367   (let* ((server (nth 1 info))
13368          (entry (assoc server gnus-server-alist)))
13369     (if entry (setcdr entry info)
13370       (setq gnus-server-alist
13371             (nconc gnus-server-alist (list (cons server info)))))))
13372
13373 (defun gnus-server-to-method (server)
13374   ;; Map virtual server names to select methods.
13375   (or (and (equal server "native") gnus-select-method)
13376       (cdr (assoc server gnus-server-alist))))
13377
13378 (defun gnus-server-extend-method (group method)
13379   ;; This function "extends" a virtual server.  If the server is
13380   ;; "hello", and the select method is ("hello" (my-var "something")) 
13381   ;; in the group "alt.alt", this will result in a new virtual server
13382   ;; called "helly+alt.alt".
13383   (let ((entry
13384          (gnus-copy-sequence 
13385           (if (equal (car method) "native") gnus-select-method
13386               (cdr (assoc (car method) gnus-server-alist))))))
13387     (setcar (cdr entry) (concat (nth 1 entry) "+" group))
13388     (nconc entry (cdr method))))
13389
13390 (defun gnus-server-get-method (group method)
13391   ;; Input either a server name, and extended server name, or a
13392   ;; select method, and return a select method. 
13393   (cond ((stringp method)
13394          (gnus-server-to-method method))
13395         ((stringp (car method))
13396          (gnus-server-extend-method group method))
13397         (t
13398          (gnus-server-add-address method))))
13399
13400 (defun gnus-server-add-address (method)
13401   (let ((method-name (symbol-name (car method))))
13402     (if (and (memq 'address (assoc method-name gnus-valid-select-methods))
13403              (not (assq (intern (concat method-name "-address")) method)))
13404         (append method (list (list (intern (concat method-name "-address"))
13405                                    (nth 1 method))))
13406       method)))
13407
13408 (defun gnus-server-equal (s1 s2)
13409   (or (equal s1 s2)
13410       (and (= (length s1) (length s2))
13411            (progn
13412              (while (and s1 (member (car s1) s2))
13413                (setq s1 (cdr s1)))
13414              (null s1)))))
13415
13416 ;;; Interactive server functions.
13417
13418 (defun gnus-server-kill-server (server)
13419   "Kill the server on the current line."
13420   (interactive (list (gnus-server-server-name)))
13421   (or (gnus-server-goto-server server)
13422       (if server (error "No such server: %s" server)
13423         (error "No server on the current line")))
13424   (let ((buffer-read-only nil))
13425     (delete-region (progn (beginning-of-line) (point))
13426                    (progn (forward-line 1) (point))))
13427   (setq gnus-server-killed-servers 
13428         (cons (assoc server gnus-server-alist) gnus-server-killed-servers))
13429   (setq gnus-server-alist (delq (car gnus-server-killed-servers)
13430                                 gnus-server-alist))
13431   (gnus-server-position-cursor))
13432
13433 (defun gnus-server-yank-server ()
13434   "Yank the previously killed server."
13435   (interactive)
13436   (or gnus-server-killed-servers
13437       (error "No killed servers to be yanked"))
13438   (let ((alist gnus-server-alist)
13439         (server (gnus-server-server-name))
13440         (killed (car gnus-server-killed-servers)))
13441     (if (not server) 
13442         (setq gnus-server-alist (nconc gnus-server-alist (list killed)))
13443       (if (string= server (car (car gnus-server-alist)))
13444           (setq gnus-server-alist (cons killed gnus-server-alist))
13445         (while (and (cdr alist)
13446                     (not (string= server (car (car (cdr alist))))))
13447           (setq alist (cdr alist)))
13448         (setcdr alist (cons killed (cdr alist)))))
13449     (gnus-server-update-server (car killed))
13450     (setq gnus-server-killed-servers (cdr gnus-server-killed-servers))
13451     (gnus-server-position-cursor)))
13452
13453 (defun gnus-server-exit ()
13454   "Return to the group buffer."
13455   (interactive)
13456   (kill-buffer (current-buffer))
13457   (switch-to-buffer gnus-group-buffer))
13458
13459 (defun gnus-server-list-servers ()
13460   "List all available servers."
13461   (interactive)
13462   (let ((cur (gnus-server-server-name)))
13463     (gnus-server-prepare)
13464     (if cur (gnus-server-goto-server cur)
13465       (goto-char (point-max))
13466       (forward-line -1))
13467     (gnus-server-position-cursor)))
13468
13469 (defun gnus-server-copy-server (from to)
13470   (interactive
13471    (list
13472     (or (gnus-server-server-name)
13473         (error "No server on the current line"))
13474     (read-string "Copy to: ")))
13475   (or from (error "No server on current line"))
13476   (or (and to (not (string= to ""))) (error "No name to copy to"))
13477   (and (assoc to gnus-server-alist) (error "%s already exists" to))
13478   (or (assoc from gnus-server-alist) 
13479       (error "%s: no such server" from))
13480   (let ((to-entry (gnus-copy-sequence (assoc from gnus-server-alist))))
13481     (setcar to-entry to)
13482     (setcar (nthcdr 2 to-entry) to)
13483     (setq gnus-server-killed-servers 
13484           (cons to-entry gnus-server-killed-servers))
13485     (gnus-server-yank-server)))
13486
13487 (defun gnus-server-add-server (how where)
13488   (interactive 
13489    (list (intern (completing-read "Server method: "
13490                                   gnus-valid-select-methods nil t))
13491          (read-string "Server name: ")))
13492   (setq gnus-server-killed-servers 
13493         (cons (list where how where) gnus-server-killed-servers))
13494   (gnus-server-yank-server))
13495
13496 (defun gnus-server-goto-server (server)
13497   "Jump to a server line."
13498   (interactive
13499    (list (completing-read "Goto server: " gnus-server-alist nil t)))
13500   (let ((to (text-property-any (point-min) (point-max) 
13501                                'gnus-server (intern server))))
13502     (and to
13503          (progn
13504            (goto-char to) 
13505            (gnus-server-position-cursor)))))
13506
13507 (defun gnus-server-edit-server (server)
13508   "Edit the server on the current line."
13509   (interactive (list (gnus-server-server-name)))
13510   (or server
13511       (error "No server on current line"))
13512   (let ((winconf (current-window-configuration)))
13513     (get-buffer-create gnus-server-edit-buffer)
13514     (gnus-configure-windows 'edit-server)
13515     (gnus-add-current-to-buffer-list)
13516     (emacs-lisp-mode)
13517     (make-local-variable 'gnus-prev-winconf)
13518     (setq gnus-prev-winconf winconf)
13519     (use-local-map (copy-keymap (current-local-map)))
13520     (let ((done-func '(lambda () 
13521                         "Exit editing mode and update the information."
13522                         (interactive)
13523                         (gnus-server-edit-server-done 'group))))
13524       (setcar (cdr (nth 4 done-func)) server)
13525       (local-set-key "\C-c\C-c" done-func))
13526     (erase-buffer)
13527     (insert ";; Type `C-c C-c' after you have edited the server.\n\n")
13528     (insert (pp-to-string (cdr (assoc server gnus-server-alist))))))
13529
13530 (defun gnus-server-edit-server-done (server)
13531   (interactive)
13532   (set-buffer (get-buffer-create gnus-server-edit-buffer))
13533   (goto-char (point-min))
13534   (let ((form (read (current-buffer)))
13535         (winconf gnus-prev-winconf))
13536     (gnus-server-set-info server form)
13537     (kill-buffer (current-buffer))
13538     (and winconf (set-window-configuration winconf))
13539     (set-buffer gnus-server-buffer)
13540     (gnus-server-update-server (gnus-server-server-name))
13541     (gnus-server-position-cursor)))
13542
13543 (defun gnus-server-read-server (server)
13544   "Browse a server."
13545   (interactive (list (gnus-server-server-name)))
13546   (gnus-browse-foreign-server (gnus-server-to-method server) (current-buffer)))
13547
13548 (defun gnus-mouse-pick-server (e)
13549   (interactive "e")
13550   (mouse-set-point e)
13551   (gnus-server-read-server (gnus-server-server-name)))
13552
13553 ;;;
13554 ;;; entry points into gnus-score.el
13555 ;;;
13556
13557 ;;; Finding score files. 
13558
13559 (defvar gnus-global-score-files nil
13560   "*List of global score files and directories.
13561 Set this variable if you want to use people's score files.  One entry
13562 for each score file or each score file directory.  Gnus will decide
13563 by itself what score files are applicable to which group.
13564
13565 Say you want to use the single score file
13566 \"/ftp.ifi.uio.no@ftp:/pub/larsi/ding/score/soc.motss.SCORE\" and all
13567 score files in the \"/ftp.some-where:/pub/score\" directory.
13568
13569  (setq gnus-global-score-files
13570        '(\"/ftp.ifi.uio.no:/pub/larsi/ding/score/soc.motss.SCORE\"
13571          \"/ftp.some-where:/pub/score\"))")
13572
13573 (defun gnus-score-score-files (group)
13574   "Return a list of all possible score files."
13575   ;; Search and set any global score files.
13576   (and gnus-global-score-files 
13577        (or gnus-internal-global-score-files
13578            (gnus-score-search-global-directories gnus-global-score-files)))
13579   ;; Fix the kill-file dir variable.
13580   (setq gnus-kill-files-directory 
13581         (file-name-as-directory
13582          (or gnus-kill-files-directory "~/News/")))
13583   ;; If we can't read it, there are no score files.
13584   (if (not (file-exists-p (expand-file-name gnus-kill-files-directory)))
13585       (setq gnus-score-file-list nil)
13586     (if (gnus-use-long-file-name 'not-score)
13587         ;; We want long file names.
13588         (if (or (not gnus-score-file-list)
13589                 (not (car gnus-score-file-list))
13590                 (gnus-file-newer-than gnus-kill-files-directory
13591                                       (car gnus-score-file-list)))
13592               (setq gnus-score-file-list 
13593                     (cons (nth 5 (file-attributes gnus-kill-files-directory))
13594                           (nreverse 
13595                            (directory-files 
13596                             gnus-kill-files-directory t 
13597                             (gnus-score-file-regexp))))))
13598       ;; We do not use long file names, so we have to do some
13599       ;; directory traversing.  
13600       (let ((mdir (length (expand-file-name gnus-kill-files-directory)))
13601             (suffixes (list gnus-score-file-suffix gnus-adaptive-file-suffix))
13602             dir files suffix)
13603         (while suffixes
13604           (setq dir (expand-file-name
13605                      (concat gnus-kill-files-directory
13606                              (gnus-replace-chars-in-string group ?. ?/))))
13607           (setq dir (gnus-replace-chars-in-string dir ?: ?/))
13608           (setq suffix (car suffixes)
13609                 suffixes (cdr suffixes))
13610           (if (file-exists-p (concat dir "/" suffix))
13611               (setq files (cons (concat dir "/" suffix) files)))
13612           (while (>= (1+ (length dir)) mdir)
13613             (and (file-exists-p (concat dir "/all/" suffix))
13614                  (setq files (cons (concat dir "/all/" suffix) files)))
13615             (string-match "/[^/]*$" dir)
13616             (setq dir (substring dir 0 (match-beginning 0)))))
13617         (setq gnus-score-file-list 
13618               (cons nil (nreverse files)))))
13619     (cdr gnus-score-file-list)))
13620
13621 (defun gnus-score-file-regexp ()
13622   (concat "\\(" gnus-score-file-suffix 
13623           "\\|" gnus-adaptive-file-suffix "\\)$"))
13624         
13625 (defun gnus-score-find-bnews (group)
13626   "Return a list of score files for GROUP.
13627 The score files are those files in the ~/News directory which matches
13628 GROUP using BNews sys file syntax."
13629   (let* ((sfiles (append (gnus-score-score-files group)
13630                          gnus-internal-global-score-files))
13631          (kill-dir (file-name-as-directory 
13632                     (expand-file-name gnus-kill-files-directory)))
13633          (klen (length kill-dir))
13634          ofiles not-match regexp)
13635     (save-excursion
13636       (set-buffer (get-buffer-create "*gnus score files*"))
13637       (buffer-disable-undo (current-buffer))
13638       ;; Go through all score file names and create regexp with them
13639       ;; as the source.  
13640       (while sfiles
13641         (erase-buffer)
13642         (insert (car sfiles))
13643         (goto-char (point-min))
13644         ;; First remove the suffix itself.
13645         (re-search-forward (concat "." (gnus-score-file-regexp)))
13646         (replace-match "" t t) 
13647         (goto-char (point-min))
13648         (if (looking-at (regexp-quote kill-dir))
13649             ;; If the file name was just "SCORE", `klen' is one character
13650             ;; too much.
13651             (delete-char (min (1- (point-max)) klen))
13652           (goto-char (point-max))
13653           (search-backward "/")
13654           (delete-region (1+ (point)) (point-min)))
13655         ;; If short file names were used, we have to translate slashes.
13656         (goto-char (point-min))
13657         (while (re-search-forward "[/:]" nil t)
13658           (replace-match "." t t))
13659         ;; Translate "all" to ".*".
13660         (while (search-forward "all" nil t)
13661           (replace-match ".*" t t))
13662         (goto-char (point-min))
13663         ;; Deal with "not."s.
13664         (if (looking-at "not.")
13665             (progn
13666               (setq not-match t)
13667               (setq regexp (buffer-substring 5 (point-max))))
13668           (setq regexp (buffer-substring 1 (point-max)))
13669           (setq not-match nil))
13670         ;; Finally - if this resulting regexp matches the group name,
13671         ;; we add this score file to the list of score files
13672         ;; applicable to this group.
13673         (if (or (and not-match
13674                      (not (string-match regexp group)))
13675                 (and (not not-match)
13676                      (string-match regexp group)))
13677             (setq ofiles (cons (car sfiles) ofiles)))
13678         (setq sfiles (cdr sfiles)))
13679       (kill-buffer (current-buffer))
13680       ;; Slight kludge here - the last score file returned should be
13681       ;; the local score file, whether it exists or not. This is so
13682       ;; that any score commands the user enters will go to the right
13683       ;; file, and not end up in some global score file.
13684       (let ((localscore
13685              (expand-file-name
13686               (if (gnus-use-long-file-name 'not-score)
13687                   (concat gnus-kill-files-directory group "." 
13688                           gnus-score-file-suffix)
13689                 (concat gnus-kill-files-directory
13690                         (gnus-replace-chars-in-string group ?. ?/)
13691                         "/" gnus-score-file-suffix)))))
13692         (and (member localscore ofiles)
13693              (delete localscore ofiles))
13694         (setq ofiles (cons localscore ofiles)))
13695       (nreverse ofiles))))
13696
13697 (defun gnus-score-find-single (group)
13698   "Return list containing the score file for GROUP."
13699   (list (gnus-score-file-name group gnus-adaptive-file-suffix)
13700         (gnus-score-file-name group)))
13701
13702 (defun gnus-score-find-hierarchical (group)
13703   "Return list of score files for GROUP.
13704 This includes the score file for the group and all its parents."
13705   (let ((all (copy-sequence '(nil)))
13706         (start 0))
13707     (while (string-match "\\." group (1+ start))
13708       (setq start (match-beginning 0))
13709       (setq all (cons (substring group 0 start) all)))
13710     (setq all (cons group all))
13711     (nconc
13712      (mapcar (lambda (newsgroup)
13713                (gnus-score-file-name newsgroup gnus-adaptive-file-suffix))
13714              (setq all (nreverse all)))
13715      (mapcar 'gnus-score-file-name all))))
13716
13717 (defvar gnus-score-file-alist-cache nil)
13718
13719 (defun gnus-score-find-alist (group)
13720   "Return list of score files for GROUP.
13721 The list is determined from the variable gnus-score-file-alist."
13722   (let ((alist gnus-score-file-multiple-match-alist)
13723         score-files)
13724     ;; if this group has been seen before, return the cached entry
13725     (if (setq score-files (assoc group gnus-score-file-alist-cache))
13726         (cdr score-files)       ; ensures caching of groups with no matches
13727       ;; handle the multiple match alist
13728       (while alist
13729         (and (string-match (car (car alist)) group)
13730              (setq score-files
13731                    (nconc score-files (cdr (car alist)))))
13732         (setq alist (cdr alist)))
13733       (setq alist gnus-score-file-single-match-alist)
13734       ;; handle the single match alist
13735       (catch 'done
13736         (while alist
13737           (and (string-match (car (car alist)) group)
13738                ;; progn used just in case ("regexp") has no files
13739                ;; and score-files is still nil. -sj
13740                ;; this can be construed as a "stop searching here" feature :>
13741                ;; and used to simplify regexps in the single-alist 
13742                (progn
13743                  (setq score-files
13744                        (append score-files (cdr (car alist))))
13745                  (throw 'done nil)))
13746           (setq alist (cdr alist))))
13747       ;; cache the score files
13748       (setq gnus-score-file-alist-cache
13749             (cons (cons group score-files) gnus-score-file-alist-cache))
13750       score-files)))
13751
13752
13753 (defun gnus-possibly-score-headers (&optional trace)
13754   (let ((func gnus-score-find-score-files-function)
13755         score-files)
13756     (and func (not (listp func))
13757          (setq func (list func)))
13758     ;; Go through all the functions for finding score files (or actual
13759     ;; scores) and add them to a list.
13760     (setq score-files (copy-sequence
13761                        (gnus-score-find-alist gnus-newsgroup-name)))
13762     (while func
13763       (and (symbolp (car func))
13764            (fboundp (car func))
13765            (setq score-files 
13766                  (nconc score-files (funcall (car func) gnus-newsgroup-name))))
13767       (setq func (cdr func)))
13768     (if score-files (gnus-score-headers score-files trace))))
13769
13770 (defun gnus-score-file-name (newsgroup &optional suffix)
13771   "Return the name of a score file for NEWSGROUP."
13772   (let ((suffix (or suffix gnus-score-file-suffix)))
13773     (cond  ((or (null newsgroup)
13774                 (string-equal newsgroup ""))
13775             ;; The global score file is placed at top of the directory.
13776             (expand-file-name 
13777              suffix (or gnus-kill-files-directory "~/News")))
13778            ((gnus-use-long-file-name 'not-score)
13779             ;; Append ".SCORE" to newsgroup name.
13780             (expand-file-name (concat newsgroup "." suffix)
13781                               (or gnus-kill-files-directory "~/News")))
13782            (t
13783             ;; Place "SCORE" under the hierarchical directory.
13784             (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
13785                                       "/" suffix)
13786                               (or gnus-kill-files-directory "~/News"))))))
13787
13788 (defun gnus-score-search-global-directories (files)
13789   "Scan all global score directories for score files."
13790   ;; Set the variable `gnus-internal-global-score-files' to all
13791   ;; available global score files.
13792   (interactive (list gnus-global-score-files))
13793   (let (out)
13794     (while files
13795       (if (string-match "/$" (car files))
13796           (setq out (nconc (directory-files 
13797                             (car files) t
13798                             (concat (gnus-score-file-regexp) "$"))))
13799         (setq out (cons (car files) out)))
13800       (setq files (cdr files)))
13801     (setq gnus-internal-global-score-files out)))
13802
13803 ;; Allow redefinition of Gnus functions.
13804
13805 (gnus-ems-redefine)
13806
13807 (provide 'gnus)
13808
13809 ;;; gnus.el ends here