*** empty log message ***
[gnus] / lisp / gnus.el
1 ;;; gnus.el --- a newsreader for GNU Emacs
2 ;; Copyright (C) 1987,88,89,90,93,94,95 Free Software Foundation, Inc.
3
4 ;; Author: Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
5 ;;      Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23
24 ;;; Commentary:
25
26 ;; Although (ding) Gnus looks suspiciously like GNUS, it isn't quite
27 ;; the same beast. Most internal structures have been changed. If you
28 ;; have written packages that depend on any of the hash tables,
29 ;; `gnus-newsrc-alist', `gnus-killed-assoc', marked lists, the .newsrc
30 ;; buffer, or internal knowledge of the `nntp-header-' macros, or
31 ;; dependence on the buffers having a certain format, your code will
32 ;; fail.
33
34 ;;; Code:
35
36 (require 'mail-utils)
37 (require 'timezone)
38 (require 'nnheader)
39
40 ;; Site dependent variables. These variables should be defined in
41 ;; paths.el.
42
43 (defvar gnus-default-nntp-server nil
44   "Specify a default NNTP server.
45 This variable should be defined in paths.el, and should never be set
46 by the user.
47 If you want to change servers, you should use `gnus-select-method'.
48 See the documentation to that variable.")
49
50 (defconst gnus-backup-default-subscribed-newsgroups 
51   '("news.announce.newusers" "news.groups.questions" "gnu.emacs.gnus")
52   "Default default new newsgroups the first time Gnus is run.
53 Should be set in paths.el, and shouldn't be touched by the user.")
54
55 (defvar gnus-local-domain nil
56   "Local domain name without a host name.
57 The DOMAINNAME environment variable is used instead if it is defined.
58 If the `system-name' function returns the full Internet name, there is
59 no need to set this variable.")
60
61 (defvar gnus-local-organization nil
62   "String with a description of what organization (if any) the user belongs to.
63 The ORGANIZATION environment variable is used instead if it is defined.
64 If this variable contains a function, this function will be called
65 with the current newsgroup name as the argument. The function should
66 return a string.
67
68 In any case, if the string (either in the variable, in the environment
69 variable, or returned by the function) is a file name, the contents of
70 this file will be used as the organization.")
71
72 (defvar gnus-use-generic-from nil
73   "If nil, the full host name will be the system name prepended to the domain name.
74 If this is a string, the full host name will be this string.
75 If this is non-nil, non-string, the domain name will be used as the
76 full host name.")
77
78 (defvar gnus-use-generic-path nil
79   "If nil, use the NNTP server name in the Path header.
80 If stringp, use this; if non-nil, use no host name (user name only).")
81
82
83 ;; Customization variables
84
85 (defvar gnus-select-method 
86   (list 'nntp (or (getenv "NNTPSERVER") 
87                   (if (and gnus-default-nntp-server
88                            (not (string= gnus-default-nntp-server "")))
89                       gnus-default-nntp-server)
90                   (system-name)))
91   "*Default method for selecting a newsgroup.
92 This variable should be a list, where the first element is how the
93 news is to be fetched, the second is the address. 
94
95 For instance, if you want to get your news via NNTP from
96 \"flab.flab.edu\", you could say:
97
98 (setq gnus-select-method '(nntp \"flab.flab.edu\"))
99
100 If you want to use your local spool, say:
101
102 (setq gnus-select-method (list 'nnspool (system-name)))
103
104 If you use this variable, you must set `gnus-nntp-server' to nil.
105
106 There is a lot more to know about select methods and virtual servers -
107 see the manual for details.")
108
109 ;; Added by Sudish Joseph <joseph@cis.ohio-state.edu>.
110 (defvar gnus-post-method nil
111   "*Preferred method for posting USENET news.
112 If this variable is nil, Gnus will use the current method to decide
113 which method to use when posting.  If it is non-nil, it will override
114 the current method.  This method will not be used in mail groups and
115 the like, only in \"real\" newsgroups.
116
117 The value must be a valid method as discussed in the documentation of
118 `gnus-select-method'.")
119
120 (defvar gnus-refer-article-method nil
121   "*Preferred method for fetching an article by Message-ID.
122 If you are reading news from the local spool (with nnspool), fetching
123 articles by Message-ID is painfully slow. By setting this method to an
124 nntp method, you might get acceptable results.
125
126 The value of this variable must be a valid select method as discussed
127 in the documentation of `gnus-select-method'")
128
129 (defvar gnus-secondary-select-methods nil
130   "*A list of secondary methods that will be used for reading news.
131 This is a list where each element is a complete select method (see
132 `gnus-select-method').  
133
134 If, for instance, you want to read your mail with the nnml backend,
135 you could set this variable:
136
137 (setq gnus-secondary-select-methods '((nnml \"\"))")
138
139 (defvar gnus-secondary-servers nil
140   "*List of NNTP servers that the user can choose between interactively.
141 To make Gnus query you for a server, you have to give `gnus' a
142 non-numeric prefix - `C-u M-x gnus', in short.")
143
144 (defvar gnus-nntp-server nil
145   "*The name of the host running the NNTP server.
146 This variable is semi-obsolete. Use the `gnus-select-method'
147 variable instead.")
148
149 (defvar gnus-nntp-service "nntp"
150   "*NNTP service name (\"nntp\" or 119).
151 This is an obsolete variable, which is scarcely used. If you use an
152 nntp server for your newsgroup and want to change the port number
153 used to 899, you would say something along these lines:
154
155  (setq gnus-select-method '(nntp \"my.nntp.server\" (nntp-port-number 899)))")
156
157 (defvar gnus-startup-file "~/.newsrc"
158   "*Your `.newsrc' file.
159 `.newsrc-SERVER' will be used instead if that exists.")
160
161 (defvar gnus-init-file "~/.gnus"
162   "*Your Gnus elisp startup file.
163 If a file with the .el or .elc suffixes exist, it will be read
164 instead.") 
165
166 (defvar gnus-group-faq-directory
167   "/anonymous@rtfm.mit.edu:/pub/usenet-by-group/"
168   "*Directory where the group FAQs are stored.
169 This will most commonly be on a remote machine, and the file will be
170 fetched by ange-ftp.")
171
172 (defvar gnus-group-archive-directory
173   "/ftp@sina.tcamc.uh.edu:/pub/emacs/ding-list/" 
174   "*The address of the (ding) archives.")
175
176 (defvar gnus-default-subscribed-newsgroups nil
177   "*This variable lists what newsgroups should be subscribed the first time Gnus is used.
178 It should be a list of strings.
179 If it is `t', Gnus will not do anything special the first time it is
180 started; it'll just use the normal newsgroups subscription methods.")
181
182 (defvar gnus-use-cross-reference t
183   "*Non-nil means that cross referenced articles will be marked as read.
184 If nil, ignore cross references.  If t, mark articles as read in
185 subscribed newsgroups. If neither t nor nil, mark as read in all
186 newsgroups.") 
187
188 (defvar gnus-use-dribble-file t
189   "*Non-nil means that Gnus will use a dribble file to store user updates.
190 If Emacs should crash without saving the .newsrc files, complete
191 information can be restored from the dribble file.")
192
193 (defvar gnus-asynchronous nil
194   "*If non-nil, Gnus will supply backends with data needed for async article fetching.")
195
196 (defvar gnus-asynchronous-article-function nil
197   "*Function for picking articles to pre-fetch, possibly.")
198
199 (defvar gnus-score-file-single-match-alist nil
200   "*Alist mapping regexps to lists of score files.
201 Each element of this alist should be of the form
202         (\"REGEXP\" [ \"SCORE-FILE-1\" ] [ \"SCORE-FILE-2\" ] ... )
203
204 If the name of a group is matched by REGEXP, the corresponding scorefiles
205 will be used for that group.
206 The first match found is used, subsequent matching entries are ignored (to
207 use multiple matches, see gnus-score-file-multiple-match-alist).
208
209 These score files are loaded in addition to any files returned by
210 gnus-score-find-score-files-function (which see).")
211
212 (defvar gnus-score-file-multiple-match-alist nil
213   "*Alist mapping regexps to lists of score files.
214 Each element of this alist should be of the form
215         (\"REGEXP\" [ \"SCORE-FILE-1\" ] [ \"SCORE-FILE-2\" ] ... )
216
217 If the name of a group is matched by REGEXP, the corresponding scorefiles
218 will be used for that group.
219 If multiple REGEXPs match a group, the score files corresponding to each
220 match will be used (for only one match to be used, see
221 gnus-score-file-single-match-alist).
222
223 These score files are loaded in addition to any files returned by
224 gnus-score-find-score-files-function (which see).")
225
226
227 (defvar gnus-score-file-suffix "SCORE"
228   "*Suffix of the score files.")
229
230 (defvar gnus-adaptive-file-suffix "ADAPT"
231   "*Suffix of the adaptive score files.")
232
233 (defvar gnus-score-find-score-files-function 'gnus-score-find-bnews
234   "*Function used to find SCORE files.
235 The function will be called with the group name as the argument, and
236 should return a list of score files to apply to that group.  The score
237 files do not actually have to exist.
238
239 Predefined values are:
240
241 gnus-score-find-single: Only apply the group's own SCORE file.
242 gnus-score-find-hierarchical: Also apply SCORE files from parent groups.
243 gnus-score-find-bnews: Apply SCORE files whose names matches.
244
245 See the documentation to these functions for more information.
246
247 This variable can also be a list of functions to be called.  Each
248 function should either return a list of score files, or a list of
249 score alists.")
250
251 (defvar gnus-score-interactive-default-score 1000
252   "*Scoring commands will raise/lower the score with this number as the default.")
253
254 (defvar gnus-large-newsgroup 200
255   "*The number of articles which indicates a large newsgroup.
256 If the number of articles in a newsgroup is greater than this value,
257 confirmation is required for selecting the newsgroup.")
258
259 ;; Suggested by Andrew Eskilsson <pi92ae@lelle.pt.hk-r.se>.
260 (defvar gnus-no-groups-message "No news is horrible news"
261   "*Message displayed by Gnus when no groups are available.")
262
263 (defvar gnus-use-long-file-name (not (memq system-type '(usg-unix-v xenix)))
264   "*Non-nil means that the default name of a file to save articles in is the group name.
265 If it's nil, the directory form of the group name is used instead.
266
267 If this variable is a list, and the list contains the element
268 `not-score', long file names will not be used for score files; if it
269 contains the element `not-save', long file names will not be used for
270 saving; and if it contains the element `not-kill', long file names
271 will not be used for kill files.")
272
273 (defvar gnus-article-save-directory (or (getenv "SAVEDIR") "~/News/")
274   "*Name of the directory articles will be saved in (default \"~/News\").
275 Initialized from the SAVEDIR environment variable.")
276
277 (defvar gnus-kill-files-directory (or (getenv "SAVEDIR") "~/News/")
278   "*Name of the directory where kill files will be stored (default \"~/News\").
279 Initialized from the SAVEDIR environment variable.")
280
281 (defvar gnus-default-article-saver 'gnus-summary-save-in-rmail
282   "*A function to save articles in your favorite format.
283 The function must be interactively callable (in other words, it must
284 be an Emacs command).
285
286 Gnus provides the following functions:
287
288 * gnus-summary-save-in-rmail (Rmail format)
289 * gnus-summary-save-in-mail (Unix mail format)
290 * gnus-summary-save-in-folder (MH folder)
291 * gnus-summary-save-in-file (article format).
292 * gnus-summary-save-in-vm (use VM's folder format).")
293
294 (defvar gnus-rmail-save-name (function gnus-plain-save-name)
295   "*A function generating a file name to save articles in Rmail format.
296 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
297
298 (defvar gnus-mail-save-name (function gnus-plain-save-name)
299   "*A function generating a file name to save articles in Unix mail format.
300 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
301
302 (defvar gnus-folder-save-name (function gnus-folder-save-name)
303   "*A function generating a file name to save articles in MH folder.
304 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER.")
305
306 (defvar gnus-file-save-name (function gnus-numeric-save-name)
307   "*A function generating a file name to save articles in article format.
308 The function is called with NEWSGROUP, HEADERS, and optional
309 LAST-FILE.")
310
311 (defvar gnus-split-methods nil
312   "*Variable used to suggest where articles are to be saved.
313 The syntax of this variable is the same as `nnmail-split-methods'.  
314
315 For instance, if you would like to save articles related to Gnus in
316 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
317 you could set this variable to something like:
318
319  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
320    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))")
321
322 (defvar gnus-save-score nil
323   "*If non-nil, save group scoring info.")
324
325 (defvar gnus-use-adaptive-scoring nil
326   "*If non-nil, use some adaptive scoring scheme.")
327
328 (defvar gnus-use-cache nil
329   "*If non-nil, Gnus will cache (some) articles locally.")
330
331 (defvar gnus-use-scoring t
332   "*If non-nil, enable scoring.")
333
334 (defvar gnus-fetch-old-headers nil
335   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
336 If an unread article in the group refers to an older, already read (or
337 just marked as read) article, the old article will not normally be
338 displayed in the Summary buffer.  If this variable is non-nil, Gnus
339 will attempt to grab the headers to the old articles, and thereby
340 build complete threads.  If it has the value `some', only enough
341 headers to connect otherwise loose threads will be displayed.
342
343 The server has to support XOVER for any of this to work.")
344
345 (defvar gnus-visual t
346   "*If non-nil, will do various highlighting.
347 If nil, no mouse highlights (or any other highlights) will be
348 performed.  This might speed up Gnus some when generating large group
349 and summary buffers.")
350
351 (defvar gnus-novice-user t
352   "*Non-nil means that you are a usenet novice.
353 If non-nil, verbose messages may be displayed and confirmations may be
354 required.")
355
356 (defvar gnus-expert-user nil
357   "*Non-nil means that you will never be asked for confirmation about anything.
358 And that means *anything*.")
359
360 (defvar gnus-verbose 7
361   "*Integer that says how verbose Gnus should be.
362 The higher the number, the more messages Gnus will flash to say what
363 it's doing.  At zero, Gnus will be totally mute; at five, Gnus will
364 display most important messages; and at ten, Gnus will keep on
365 jabbering all the time.")
366
367 (defvar gnus-keep-same-level nil
368   "*Non-nil means that the next newsgroup after the current will be on the same level.
369 When you type, for instance, `n' after reading the last article in the
370 current newsgroup, you will go to the next newsgroup. If this variable
371 is nil, the next newsgroup will be the next from the group
372 buffer. 
373 If this variable is non-nil, Gnus will either put you in the
374 next newsgroup with the same level, or, if no such newsgroup is
375 available, the next newsgroup with the lowest possible level higher
376 than the current level.
377 If this variable is `best', Gnus will make the next newsgroup the one
378 with the best level.")
379
380 (defvar gnus-summary-make-false-root 'adopt
381   "*nil means that Gnus won't gather loose threads.
382 If the root of a thread has expired or been read in a previous
383 session, the information necessary to build a complete thread has been
384 lost. Instead of having many small sub-threads from this original thread
385 scattered all over the summary buffer, Gnus can gather them. 
386
387 If non-nil, Gnus will try to gather all loose sub-threads from an
388 original thread into one large thread.
389
390 If this variable is non-nil, it should be one of `none', `adopt',
391 `dummy' or `empty'.
392
393 If this variable is `none', Gnus will not make a false root, but just
394 present the sub-threads after another.
395 If this variable is `dummy', Gnus will create a dummy root that will
396 have all the sub-threads as children.
397 If this variable is `adopt', Gnus will make one of the \"children\"
398 the parent and mark all the step-children as such.
399 If this variable is `empty', the \"children\" are printed with empty
400 subject fields.  (Or rather, they will be printed with a string
401 given by the `gnus-summary-same-subject' variable.)")
402
403 (defvar gnus-summary-gather-subject-limit nil
404   "*Maximum length of subject comparisons when gathering loose threads.
405 Use nil to compare full subjects.  Setting this variable to a low
406 number will help gather threads that have been corrupted by
407 newsreaders chopping off subject lines, but it might also mean that
408 unrelated articles that have subject that happen to begin with the
409 same few characters will be incorrectly gathered.
410
411 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
412 comparing subjects.")
413
414 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
415 (defvar gnus-summary-same-subject ""
416   "*String indicating that the current article has the same subject as the previous.
417 This variable will only be used if the value of
418 `gnus-summary-make-false-root' is `empty'.")
419
420 (defvar gnus-summary-goto-unread 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-exit-hook nil
1046   "*A hook called on exit from the summary buffer.
1047 It calls `gnus-summary-expire-articles' by default.")
1048 (add-hook 'gnus-summary-exit-hook 'gnus-summary-expire-articles)
1049
1050 (defvar gnus-open-server-hook nil
1051   "*A hook called just before opening connection to the news server.")
1052
1053 (defvar gnus-startup-hook nil
1054   "*A hook called at startup.
1055 This hook is called after Gnus is connected to the NNTP server.")
1056
1057 (defvar gnus-get-new-news-hook nil
1058   "*A hook run just before Gnus checks for new news.")
1059
1060 (defvar gnus-group-prepare-function 'gnus-group-prepare-flat
1061   "*A function that is called to generate the group buffer.
1062 The function is called with three arguments: The first is a number;
1063 all group with a level less or equal to that number should be listed,
1064 if the second is non-nil, empty groups should also be displayed. If
1065 the third is non-nil, it is a number. No groups with a level lower
1066 than this number should be displayed.
1067
1068 The only current function implemented is `gnus-group-prepare-flat'.")
1069
1070 (defvar gnus-group-prepare-hook nil
1071   "*A hook called after the group buffer has been generated.
1072 If you want to modify the group buffer, you can use this hook.")
1073
1074 (defvar gnus-summary-prepare-hook nil
1075   "*A hook called after the summary buffer has been generated.
1076 If you want to modify the summary buffer, you can use this hook.")
1077
1078 (defvar gnus-article-prepare-hook nil
1079   "*A hook called after an article has been prepared in the article buffer.
1080 If you want to run a special decoding program like nkf, use this hook.")
1081
1082 (defvar gnus-article-display-hook nil
1083   "*A hook called after the article is displayed in the article buffer.
1084 The hook is designed to change the contents of the article
1085 buffer. Typical functions that this hook may contain are
1086 `gnus-article-hide-headers' (hide selected headers),
1087 `gnus-article-maybe-highlight' (perform fancy article highlighting), 
1088 `gnus-article-hide-signature' (hide signature) and
1089 `gnus-article-treat-overstrike' (turn \"^H_\" into bold characters).")
1090 (add-hook 'gnus-article-display-hook 'gnus-article-hide-headers-if-wanted)
1091 (add-hook 'gnus-article-display-hook 'gnus-article-treat-overstrike)
1092
1093 (defvar gnus-article-x-face-command
1094   "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | xv -quit -"
1095   "String or function to be executed to display an X-Face header.
1096 If it is a string, the command will be executed in a sub-shell
1097 asynchronously. The compressed face will be piped to this command.") 
1098
1099 (defvar gnus-article-x-face-too-ugly nil
1100   "Regexp matching posters whose face shouldn't be shown automatically.")
1101
1102 (defvar gnus-select-group-hook nil
1103   "*A hook called when a newsgroup is selected.
1104
1105 If you'd like to simplify subjects like the
1106 `gnus-summary-next-same-subject' command does, you can use the
1107 following hook:
1108
1109  (setq gnus-select-group-hook
1110       (list
1111         (lambda ()
1112           (mapcar (lambda (header)
1113                      (header-set-subject
1114                       header
1115                       (gnus-simplify-subject
1116                        (header-subject header) 're-only)))
1117                   gnus-newsgroup-headers))))")
1118
1119 (defvar gnus-select-article-hook
1120   '(gnus-summary-show-thread)
1121   "*A hook called when an article is selected.
1122 The default hook shows conversation thread subtrees of the selected
1123 article automatically using `gnus-summary-show-thread'.")
1124
1125 (defvar gnus-apply-kill-hook '(gnus-apply-kill-file)
1126   "*A hook called to apply kill files to a group.
1127 This hook is intended to apply a kill file to the selected newsgroup.
1128 The function `gnus-apply-kill-file' is called by default.
1129
1130 Since a general kill file is too heavy to use only for a few
1131 newsgroups, I recommend you to use a lighter hook function. For
1132 example, if you'd like to apply a kill file to articles which contains
1133 a string `rmgroup' in subject in newsgroup `control', you can use the
1134 following hook:
1135
1136 \(setq gnus-apply-kill-hook
1137       (list
1138         (lambda ()
1139           (cond ((string-match \"control\" gnus-newsgroup-name)
1140                  (gnus-kill \"Subject\" \"rmgroup\")
1141                  (gnus-expunge \"X\"))))))")
1142
1143 (defvar gnus-visual-mark-article-hook 
1144   (list 'gnus-highlight-selected-summary)
1145   "*Hook run after selecting an article in the summary buffer.
1146 It is meant to be used for highlighting the article in some way.  It
1147 is not run if `gnus-visual' is nil.")
1148
1149 (defvar gnus-prepare-article-hook (list 'gnus-inews-insert-signature)
1150   "*A hook called after preparing body, but before preparing header headers.
1151 The default hook (`gnus-inews-insert-signature') inserts a signature
1152 file specified by the variable `gnus-signature-file'.")
1153
1154 (defvar gnus-exit-group-hook nil
1155   "*A hook called when exiting (not quitting) summary mode.")
1156
1157 (defvar gnus-suspend-gnus-hook nil
1158   "*A hook called when suspending (not exiting) Gnus.")
1159
1160 (defvar gnus-exit-gnus-hook nil
1161   "*A hook called when exiting Gnus.")
1162
1163 (defvar gnus-save-newsrc-hook nil
1164   "*A hook called when saving the newsrc file.")
1165
1166 (defvar gnus-summary-update-hook 
1167   (list 'gnus-summary-highlight-line)
1168   "*A hook called when a summary line is changed.
1169 The hook will not be called if `gnus-visual' is nil.
1170
1171 The default function `gnus-summary-highlight-line' will
1172 highlight the line according to the `gnus-summary-highlight'
1173 variable.")
1174
1175 (defvar gnus-mark-article-hook (list 'gnus-summary-mark-unread-as-read)
1176   "*A hook called when an article is selected for the first time.
1177 The hook is intended to mark an article as read (or unread)
1178 automatically when it is selected.")
1179
1180 ;; Remove any hilit infestation.
1181 (add-hook 'gnus-startup-hook
1182           (lambda ()
1183             (remove-hook 'gnus-summary-prepare-hook
1184                          'hilit-rehighlight-buffer-quietly)
1185             (remove-hook 'gnus-summary-prepare-hook 'hilit-install-line-hooks)
1186             (setq gnus-mark-article-hook '(gnus-summary-mark-unread-as-read))
1187             (remove-hook 'gnus-article-prepare-hook
1188                          'hilit-rehighlight-buffer-quietly)))
1189
1190
1191 \f
1192 ;; Internal variables
1193
1194 ;; Avoid highlighting in kill files.
1195 (defvar gnus-summary-inhibit-highlight nil)
1196 (defvar gnus-newsgroup-selected-overlay nil)
1197
1198 (defvar gnus-article-mode-map nil)
1199 (defvar gnus-dribble-buffer nil)
1200 (defvar gnus-headers-retrieved-by nil)
1201 (defvar gnus-article-reply nil)
1202 (defvar gnus-override-method nil)
1203 (defvar gnus-article-check-size nil)
1204
1205 (defvar gnus-current-score-file nil)
1206 (defvar gnus-internal-global-score-files nil)
1207 (defvar gnus-score-file-list nil)
1208
1209
1210 (defvar gnus-current-move-group nil)
1211
1212 (defvar gnus-newsgroup-dependencies nil)
1213 (defvar gnus-newsgroup-threads nil)
1214 (defvar gnus-newsgroup-async nil)
1215 (defconst gnus-group-edit-buffer "*Gnus edit newsgroup*")
1216
1217 (defvar gnus-newsgroup-adaptive nil)
1218
1219 (defvar gnus-summary-display-table nil)
1220
1221 (defconst gnus-group-line-format-alist
1222   (list (list ?M 'marked ?c)
1223         (list ?S 'subscribed ?c)
1224         (list ?L 'level ?d)
1225         (list ?N 'number ?s)
1226         (list ?I 'number-of-dormant ?d)
1227         (list ?T 'number-of-ticked ?d)
1228         (list ?R 'number-of-read ?s)
1229         (list ?t 'number-total ?d)
1230         (list ?y 'number-of-unread-unticked ?s)
1231         (list ?i 'number-of-ticked-and-dormant ?d)
1232         (list ?g 'group ?s)
1233         (list ?G 'qualified-group ?s)
1234         (list ?D 'newsgroup-description ?s)
1235         (list ?o 'moderated ?c)
1236         (list ?O 'moderated-string ?s)
1237         (list ?p 'process-marked ?c)
1238         (list ?s 'news-server ?s)
1239         (list ?n 'news-method ?s)
1240         (list ?z 'news-method-string ?s)
1241         (list ?u 'user-defined ?s)))
1242
1243 (defconst gnus-summary-line-format-alist 
1244   (list (list ?N 'number ?d)
1245         (list ?S 'subject ?s)
1246         (list ?s 'subject-or-nil ?s)
1247         (list ?n 'name ?s)
1248         (list ?A '(car (cdr (funcall gnus-extract-address-components from))) ?s)
1249         (list ?a '(or (car (funcall gnus-extract-address-components from)) from) ?s)
1250         (list ?F 'from ?s)
1251         (list ?x (macroexpand '(header-xref header)) ?s)
1252         (list ?D (macroexpand '(header-date header)) ?s)
1253         (list ?d '(gnus-dd-mmm (header-date header)) ?s)
1254         (list ?M (macroexpand '(header-id header)) ?s)
1255         (list ?r (macroexpand '(header-references header)) ?s)
1256         (list ?c '(or (header-chars header) 0) ?d)
1257         (list ?L 'lines ?d)
1258         (list ?I 'indentation ?s)
1259         (list ?T '(if (= level 0) "" (make-string (frame-width) ? )) ?s)
1260         (list ?R 'replied ?c)
1261         (list ?\[ 'opening-bracket ?c)
1262         (list ?\] 'closing-bracket ?c)
1263         (list ?\> '(make-string level ? ) ?s)
1264         (list ?\< '(make-string (max 0 (- 20 level)) ? ) ?s)
1265         (list ?i 'score ?d)
1266         (list ?z 'score-char ?c)
1267         (list ?U 'unread ?c)
1268         (list ?t '(gnus-summary-number-of-articles-in-thread 
1269                    (and (boundp 'thread) (car thread)))
1270               ?d)
1271         (list ?e '(gnus-summary-number-of-articles-in-thread 
1272                    (and (boundp 'thread) (car thread)) t)
1273               ?c)
1274         (list ?u 'user-defined ?s))
1275   "An alist of format specifications that can appear in summary lines,
1276 and what variables they correspond with, along with the type of the
1277 variable (string, integer, character, etc).")
1278
1279 (defconst gnus-summary-dummy-line-format-alist
1280   (list (list ?S 'subject ?s)
1281         (list ?N 'number ?d)
1282         (list ?u 'user-defined ?s)))
1283
1284 (defconst gnus-summary-mode-line-format-alist 
1285   (list (list ?G 'group-name ?s)
1286         (list ?g '(gnus-short-group-name group-name) ?s)
1287         (list ?A 'article-number ?d)
1288         (list ?Z 'unread-and-unselected ?s)
1289         (list ?V 'gnus-version ?s)
1290         (list ?U 'unread ?d)
1291         (list ?S 'subject ?s)
1292         (list ?e 'unselected ?d)
1293         (list ?u 'user-defined ?s)
1294         (list ?s '(gnus-current-score-file-nondirectory) ?s)))
1295
1296 (defconst gnus-group-mode-line-format-alist 
1297   (list (list ?S 'news-server ?s)
1298         (list ?M 'news-method ?s)
1299         (list ?u 'user-defined ?s)))
1300
1301 (defvar gnus-have-read-active-file nil)
1302
1303 (defconst gnus-maintainer "gnus-bug@ifi.uio.no (The Gnus Bugfixing Girls + Boys)"
1304   "The mail address of the Gnus maintainers.")
1305
1306 (defconst gnus-version "(ding) Gnus v0.98"
1307   "Version number for this version of Gnus.")
1308
1309 (defvar gnus-info-nodes
1310   '((gnus-group-mode            "(gnus)The Group Buffer")
1311     (gnus-summary-mode          "(gnus)The Summary Buffer")
1312     (gnus-article-mode          "(gnus)The Article Buffer"))
1313   "Assoc list of major modes and related Info nodes.")
1314
1315 (defvar gnus-documentation-group-file "~/dgnus/lisp/doc.txt"
1316   "The location of the (ding) Gnus documentation group.")
1317
1318 (defvar gnus-group-buffer "*Group*")
1319 (defvar gnus-summary-buffer "*Summary*")
1320 (defvar gnus-article-buffer "*Article*")
1321 (defvar gnus-server-buffer "*Server*")
1322
1323 (defvar gnus-work-buffer " *gnus work*")
1324
1325 (defvar gnus-buffer-list nil
1326   "Gnus buffers that should be killed on exit.")
1327
1328 (defvar gnus-server-alist nil
1329   "List of available servers.")
1330
1331 (defvar gnus-variable-list
1332   '(gnus-newsrc-options gnus-newsrc-options-n
1333     gnus-newsrc-last-checked-date 
1334     gnus-newsrc-alist gnus-server-alist
1335     gnus-killed-list gnus-zombie-list)
1336   "Gnus variables saved in the quick startup file.")
1337
1338 (defvar gnus-overload-functions
1339   '((news-inews gnus-inews-news "rnewspost"))
1340   "Functions overloaded by gnus.
1341 It is a list of `(original overload &optional file)'.")
1342
1343 (defvar gnus-newsrc-options nil
1344   "Options line in the .newsrc file.")
1345
1346 (defvar gnus-newsrc-options-n nil
1347   "List of regexps representing groups to be subscribed/ignored unconditionally.") 
1348
1349 (defvar gnus-newsrc-last-checked-date nil
1350   "Date Gnus last asked server for new newsgroups.")
1351
1352 (defvar gnus-newsrc-alist nil
1353   "Assoc list of read articles.
1354 gnus-newsrc-hashtb should be kept so that both hold the same information.")
1355
1356 (defvar gnus-newsrc-hashtb nil
1357   "Hashtable of gnus-newsrc-alist.")
1358
1359 (defvar gnus-killed-list nil
1360   "List of killed newsgroups.")
1361
1362 (defvar gnus-killed-hashtb nil
1363   "Hash table equivalent of gnus-killed-list.")
1364
1365 (defvar gnus-zombie-list nil
1366   "List of almost dead newsgroups.")
1367
1368 (defvar gnus-description-hashtb nil
1369   "Descriptions of newsgroups.")
1370
1371 (defvar gnus-list-of-killed-groups nil
1372   "List of newsgroups that have recently been killed by the user.")
1373
1374 (defvar gnus-active-hashtb nil
1375   "Hashtable of active articles.")
1376
1377 (defvar gnus-moderated-list nil
1378   "List of moderated newsgroups.")
1379
1380 (defvar gnus-group-marked nil)
1381
1382 (defvar gnus-current-startup-file nil
1383   "Startup file for the current host.")
1384
1385 (defvar gnus-last-search-regexp nil
1386   "Default regexp for article search command.")
1387
1388 (defvar gnus-last-shell-command nil
1389   "Default shell command on article.")
1390
1391 (defvar gnus-current-select-method nil
1392   "The current method for selecting a newsgroup.")
1393
1394 (defvar gnus-have-all-newsgroups nil)
1395
1396 (defvar gnus-article-internal-prepare-hook nil)
1397
1398 (defvar gnus-newsgroup-name nil)
1399 (defvar gnus-newsgroup-begin nil)
1400 (defvar gnus-newsgroup-end nil)
1401 (defvar gnus-newsgroup-last-rmail nil)
1402 (defvar gnus-newsgroup-last-mail nil)
1403 (defvar gnus-newsgroup-last-folder nil)
1404 (defvar gnus-newsgroup-last-file nil)
1405 (defvar gnus-newsgroup-auto-expire nil)
1406 (defvar gnus-newsgroup-active nil)
1407
1408 (defvar gnus-newsgroup-unreads nil
1409   "List of unread articles in the current newsgroup.")
1410
1411 (defvar gnus-newsgroup-unselected nil
1412   "List of unselected unread articles in the current newsgroup.")
1413
1414 (defvar gnus-newsgroup-marked nil
1415   "List of ticked articles in the current newsgroup (a subset of unread art).")
1416
1417 (defvar gnus-newsgroup-killed nil
1418   "List of ranges of articles that have been through the scoring process.")
1419
1420 (defvar gnus-newsgroup-kill-headers nil)
1421
1422 (defvar gnus-newsgroup-replied nil
1423   "List of articles that have been replied to in the current newsgroup.")
1424
1425 (defvar gnus-newsgroup-expirable nil
1426   "List of articles in the current newsgroup that can be expired.")
1427
1428 (defvar gnus-newsgroup-processable nil
1429   "List of articles in the current newsgroup that can be processed.")
1430
1431 (defvar gnus-newsgroup-bookmarks nil
1432   "List of articles in the current newsgroup that have bookmarks.")
1433
1434 (defvar gnus-newsgroup-dormant nil
1435   "List of dormant articles in the current newsgroup.")
1436
1437 (defvar gnus-newsgroup-scored nil
1438   "List of scored articles in the current newsgroup.")
1439
1440 (defvar gnus-newsgroup-headers nil
1441   "List of article headers in the current newsgroup.")
1442 (defvar gnus-newsgroup-headers-hashtb-by-number nil)
1443
1444 (defvar gnus-newsgroup-ancient nil
1445   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1446
1447 (defvar gnus-current-article nil)
1448 (defvar gnus-article-current nil)
1449 (defvar gnus-current-headers nil)
1450 (defvar gnus-have-all-headers nil)
1451 (defvar gnus-last-article nil)
1452 (defvar gnus-newsgroup-history nil)
1453 (defvar gnus-current-kill-article nil)
1454
1455 ;; Save window configuration.
1456 (defvar gnus-prev-winconf nil)
1457
1458 ;; Format specs
1459 (defvar gnus-summary-line-format-spec nil)
1460 (defvar gnus-summary-dummy-line-format-spec nil)
1461 (defvar gnus-group-line-format-spec nil)
1462 (defvar gnus-summary-mode-line-format-spec nil)
1463 (defvar gnus-article-mode-line-format-spec nil)
1464 (defvar gnus-group-mode-line-format-spec nil)
1465 (defvar gnus-summary-mark-positions nil)
1466
1467 (defvar gnus-summary-expunge-below nil)
1468 (defvar gnus-reffed-article-number nil)
1469
1470 (defvar rmail-default-file (expand-file-name "~/XMBOX"))
1471 (defvar rmail-default-rmail-file (expand-file-name "~/XNEWS"))
1472
1473 (defvar gnus-cache-removeable-articles nil)
1474
1475 (defconst gnus-summary-local-variables 
1476   '(gnus-newsgroup-name 
1477     gnus-newsgroup-begin gnus-newsgroup-end 
1478     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail 
1479     gnus-newsgroup-last-folder gnus-newsgroup-last-file 
1480     gnus-newsgroup-auto-expire gnus-newsgroup-unreads 
1481     gnus-newsgroup-unselected gnus-newsgroup-marked
1482     gnus-newsgroup-replied gnus-newsgroup-expirable
1483     gnus-newsgroup-processable gnus-newsgroup-killed
1484     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1485     gnus-newsgroup-headers gnus-newsgroup-headers-hashtb-by-number
1486     gnus-current-article gnus-current-headers gnus-have-all-headers
1487     gnus-last-article gnus-article-internal-prepare-hook
1488     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1489     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1490     gnus-newsgroup-threads gnus-newsgroup-async
1491     gnus-score-alist gnus-current-score-file gnus-summary-expunge-below 
1492     gnus-summary-mark-below gnus-newsgroup-active gnus-scores-exclude-files
1493     gnus-newsgroup-history gnus-newsgroup-ancient
1494     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1495     gnus-cache-removeable-articles)
1496   "Variables that are buffer-local to the summary buffers.")
1497
1498 (defconst gnus-bug-message
1499   "Sending a bug report to the Gnus Towers.
1500 ========================================
1501
1502 The buffer below is a mail buffer.  When you press `C-c C-c', it will
1503 be sent to the Gnus Bug Exterminators. 
1504
1505 At the bottom of the buffer you'll see lots of variable settings.
1506 Please do not delete those.  They will tell the Bug People what your
1507 environment is, so that it will be easier to locate the bugs.
1508
1509 If you have found a bug that makes Emacs go \"beep\", set
1510 debug-on-error to t (`M-x set-variable RET debug-on-error RET t RET') 
1511 and include the backtrace in your bug report.
1512
1513 Please describe the bug in annoying, painstaking detail.
1514
1515 Thank you for your help in stamping out bugs.
1516 ")
1517
1518 ;;; End of variables.
1519
1520 ;; Define some autoload functions Gnus might use.
1521 (eval-and-compile
1522
1523   ;; Various 
1524   (autoload 'metamail-buffer "metamail")
1525   (autoload 'Info-goto-node "info")
1526   (autoload 'hexl-hex-string-to-integer "hexl")
1527   (autoload 'pp "pp")
1528   (autoload 'pp-to-string "pp")
1529   (autoload 'pp-eval-expression "pp")
1530   (autoload 'mail-extract-address-components "mail-extr")
1531
1532   (autoload 'nnmail-split-fancy "nnmail")
1533   (autoload 'nnvirtual-catchup-group "nnvirtual")
1534
1535   ;; timezone
1536   (autoload 'timezone-make-date-arpa-standard "timezone")
1537   (autoload 'timezone-fix-time "timezone")
1538   (autoload 'timezone-make-sortable-date "timezone")
1539   (autoload 'timezone-make-time-string "timezone")
1540
1541   ;; rmail & friends
1542   (autoload 'mail-position-on-field "sendmail")
1543   (autoload 'mail-setup "sendmail")
1544   (autoload 'rmail-output "rmailout")
1545   (autoload 'news-mail-other-window "rnewspost")
1546   (autoload 'news-reply-yank-original "rnewspost")
1547   (autoload 'news-caesar-buffer-body "rnewspost")
1548   (autoload 'rmail-insert-rmail-file-header "rmail")
1549   (autoload 'rmail-count-new-messages "rmail")
1550   (autoload 'rmail-show-message "rmail")
1551
1552   ;; gnus-soup
1553   (autoload 'gnus-group-brew-soup "gnus-soup" nil t)
1554   (autoload 'gnus-brew-soup "gnus-soup" nil t)
1555   (autoload 'gnus-soup-add-article "gnus-soup" nil t)
1556   (autoload 'gnus-soup-send-replies "gnus-soup" nil t)
1557   (autoload 'gnus-soup-save-areas "gnus-soup" nil t)
1558   (autoload 'gnus-soup-pack-packet "gnus-soup" nil t)
1559   (autoload 'nnsoup-pack-replies "nnsoup" nil t)
1560
1561   ;; gnus-mh
1562   (autoload 'gnus-mail-reply-using-mhe "gnus-mh")
1563   (autoload 'gnus-mail-forward-using-mhe "gnus-mh")
1564   (autoload 'gnus-mail-other-window-using-mhe "gnus-mh")
1565   (autoload 'gnus-summary-save-in-folder "gnus-mh")
1566   (autoload 'gnus-summary-save-article-folder "gnus-mh")
1567   (autoload 'gnus-Folder-save-name "gnus-mh")
1568   (autoload 'gnus-folder-save-name "gnus-mh")
1569
1570   ;; gnus-vis misc
1571   (autoload 'gnus-group-make-menu-bar "gnus-vis")
1572   (autoload 'gnus-summary-make-menu-bar "gnus-vis")
1573   (autoload 'gnus-server-make-menu-bar "gnus-vis")
1574   (autoload 'gnus-article-make-menu-bar "gnus-vis")
1575   (autoload 'gnus-browse-make-menu-bar "gnus-vis")
1576   (autoload 'gnus-highlight-selected-summary "gnus-vis")
1577   (autoload 'gnus-summary-highlight-line "gnus-vis")
1578   (autoload 'gnus-carpal-setup-buffer "gnus-vis")
1579
1580   ;; gnus-vis article
1581   (autoload 'gnus-article-push-button "gnus-vis" nil t)
1582   (autoload 'gnus-article-press-button "gnus-vis" nil t)
1583   (autoload 'gnus-article-highlight "gnus-vis" nil t)
1584   (autoload 'gnus-article-hide "gnus-vis" nil t)
1585   (autoload 'gnus-article-hide-signature "gnus-vis" nil t)
1586   (autoload 'gnus-article-highlight-headers "gnus-vis" nil t)
1587   (autoload 'gnus-article-highlight-signature "gnus-vis" nil t)
1588   (autoload 'gnus-article-add-buttons "gnus-vis" nil t)
1589   (autoload 'gnus-article-next-button "gnus-vis" nil t)
1590   (autoload 'gnus-article-add-button "gnus-vis")
1591
1592   ;; gnus-cite
1593   (autoload 'gnus-article-highlight-citation "gnus-cite" nil t)
1594   (autoload 'gnus-article-hide-citation-maybe "gnus-cite" nil t)
1595   (autoload 'gnus-article-hide-citation "gnus-cite" nil t)
1596
1597   ;; gnus-kill
1598   (autoload 'gnus-kill "gnus-kill")
1599   (autoload 'gnus-apply-kill-file-internal "gnus-kill")
1600   (autoload 'gnus-kill-file-edit-file "gnus-kill")
1601   (autoload 'gnus-kill-file-raise-followups-to-author "gnus-kill")
1602   (autoload 'gnus-execute "gnus-kill")
1603   (autoload 'gnus-expunge "gnus-kill")
1604
1605   ;; gnus-cache
1606   (autoload 'gnus-cache-possibly-enter-article "gnus-cache")
1607   (autoload 'gnus-cache-save-buffers "gnus-cache")
1608   (autoload 'gnus-cache-possibly-remove-articles "gnus-cache")
1609   (autoload 'gnus-cache-request-article "gnus-cache")
1610   (autoload 'gnus-cache-retrieve-headers "gnus-cache")
1611   (autoload 'gnus-cache-possibly-alter-active "gnus-cache")
1612   (autoload 'gnus-jog-cache "gnus-cache" nil t)
1613   (autoload 'gnus-cache-enter-remove-article "gnus-cache")
1614
1615   ;; gnus-score
1616   (autoload 'gnus-summary-increase-score "gnus-score" nil t)
1617   (autoload 'gnus-summary-lower-score "gnus-score" nil t)
1618   (autoload 'gnus-summary-score-map "gnus-score" nil nil 'keymap)
1619   (autoload 'gnus-score-save "gnus-score")
1620   (autoload 'gnus-score-headers "gnus-score")
1621   (autoload 'gnus-current-score-file-nondirectory "gnus-score")
1622   (autoload 'gnus-score-adaptive "gnus-score")
1623   (autoload 'gnus-score-remove-lines-adaptive "gnus-score")
1624   (autoload 'gnus-score-find-trace "gnus-score")
1625
1626   ;; gnus-edit
1627   (autoload 'gnus-score-customize "gnus-edit" nil t)
1628
1629   ;; gnus-uu
1630   (autoload 'gnus-uu-extract-map "gnus-uu" nil nil 'keymap)
1631   (autoload 'gnus-uu-mark-map "gnus-uu" nil nil 'keymap)
1632   (autoload 'gnus-uu-digest-mail-forward "gnus-uu" nil t)
1633   (autoload 'gnus-uu-digest-post-forward "gnus-uu" nil t)
1634   (autoload 'gnus-uu-mark-series "gnus-uu" nil t)
1635   (autoload 'gnus-uu-mark-region "gnus-uu" nil t)
1636   (autoload 'gnus-uu-mark-by-regexp "gnus-uu" nil t)
1637   (autoload 'gnus-uu-mark-all "gnus-uu" nil t)
1638   (autoload 'gnus-uu-mark-sparse "gnus-uu" nil t)
1639   (autoload 'gnus-uu-mark-thread "gnus-uu" nil t)
1640   (autoload 'gnus-uu-decode-uu "gnus-uu" nil t)
1641   (autoload 'gnus-uu-decode-uu-and-save "gnus-uu" nil t)
1642   (autoload 'gnus-uu-decode-unshar "gnus-uu" nil t)
1643   (autoload 'gnus-uu-decode-unshar-and-save "gnus-uu" nil t)
1644   (autoload 'gnus-uu-decode-save "gnus-uu" nil t)
1645   (autoload 'gnus-uu-decode-binhex "gnus-uu" nil t)
1646   (autoload 'gnus-uu-decode-uu-view "gnus-uu" nil t)
1647   (autoload 'gnus-uu-decode-uu-and-save-view "gnus-uu" nil t)
1648   (autoload 'gnus-uu-decode-unshar-view "gnus-uu" nil t)
1649   (autoload 'gnus-uu-decode-unshar-and-save-view "gnus-uu" nil t)
1650   (autoload 'gnus-uu-decode-save-view "gnus-uu" nil t)
1651   (autoload 'gnus-uu-decode-binhex-view "gnus-uu" nil t)
1652
1653   ;; gnus-msg
1654   (autoload 'gnus-summary-send-map "gnus-msg" nil nil 'keymap)
1655   (autoload 'gnus-group-post-news "gnus-msg" nil t)
1656   (autoload 'gnus-group-mail "gnus-msg" nil t)
1657   (autoload 'gnus-summary-post-news "gnus-msg" nil t)
1658   (autoload 'gnus-summary-followup "gnus-msg" nil t)
1659   (autoload 'gnus-summary-followup-with-original "gnus-msg" nil t)
1660   (autoload 'gnus-summary-followup-and-reply "gnus-msg" nil t)
1661   (autoload 'gnus-summary-followup-and-reply-with-original "gnus-msg" nil t)
1662   (autoload 'gnus-summary-cancel-article "gnus-msg" nil t)
1663   (autoload 'gnus-summary-supersede-article "gnus-msg" nil t)
1664   (autoload 'gnus-post-news "gnus-msg" nil t)
1665   (autoload 'gnus-inews-news "gnus-msg" nil t)
1666   (autoload 'gnus-cancel-news "gnus-msg" nil t)
1667   (autoload 'gnus-summary-reply "gnus-msg" nil t)
1668   (autoload 'gnus-summary-reply-with-original "gnus-msg" nil t)
1669   (autoload 'gnus-summary-mail-forward "gnus-msg" nil t)
1670   (autoload 'gnus-summary-mail-other-window "gnus-msg" nil t)
1671   (autoload 'gnus-mail-reply-using-mail "gnus-msg")
1672   (autoload 'gnus-mail-yank-original "gnus-msg")
1673   (autoload 'gnus-mail-send-and-exit "gnus-msg")
1674   (autoload 'gnus-mail-forward-using-mail "gnus-msg")
1675   (autoload 'gnus-mail-other-window-using-mail "gnus-msg")
1676   (autoload 'gnus-article-mail-with-original "gnus-msg")
1677   (autoload 'gnus-article-mail "gnus-msg")
1678   (autoload 'gnus-bug "gnus-msg" nil t)
1679
1680   ;; gnus-vm
1681   (autoload 'gnus-summary-save-in-vm "gnus-vm" nil t)
1682   (autoload 'gnus-summary-save-article-vm "gnus-vm" nil t)
1683   (autoload 'gnus-mail-forward-using-vm "gnus-vm")
1684   (autoload 'gnus-mail-reply-using-vm "gnus-vm")
1685   (autoload 'gnus-mail-other-window-using-vm "gnus-vm" nil t)
1686   (autoload 'gnus-yank-article "gnus-vm" nil t)
1687
1688   )
1689
1690 \f
1691
1692 ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
1693 ;; If you want the cursor to go somewhere else, set these two
1694 ;; functions in some startup hook to whatever you want.
1695 (defalias 'gnus-summary-position-cursor 'gnus-goto-colon)
1696 (defalias 'gnus-group-position-cursor 'gnus-goto-colon)
1697
1698 ;;; Various macros and substs.
1699
1700 (defmacro gnus-eval-in-buffer-window (buffer &rest forms)
1701   "Pop to BUFFER, evaluate FORMS, and then returns to original window."
1702   (` (let ((GnusStartBufferWindow (selected-window)))
1703        (unwind-protect
1704            (progn
1705              (pop-to-buffer (, buffer))
1706              (,@ forms))
1707          (select-window GnusStartBufferWindow)))))
1708
1709 (defmacro gnus-gethash (string hashtable)
1710   "Get hash value of STRING in HASHTABLE."
1711   ;;(` (symbol-value (abbrev-symbol (, string) (, hashtable))))
1712   ;;(` (abbrev-expansion (, string) (, hashtable)))
1713   (` (symbol-value (intern-soft (, string) (, hashtable)))))
1714
1715 (defmacro gnus-sethash (string value hashtable)
1716   "Set hash value. Arguments are STRING, VALUE, and HASHTABLE."
1717   ;; We cannot use define-abbrev since it only accepts string as value.
1718   ;; (set (intern string hashtable) value))
1719   (` (set (intern (, string) (, hashtable)) (, value))))
1720
1721 (defsubst gnus-buffer-substring (beg end)
1722   (buffer-substring (match-beginning beg) (match-end end)))
1723
1724 ;; modified by MORIOKA Tomohiko <morioka@jaist.ac.jp>
1725 ;;   function `substring' might cut on a middle of multi-octet
1726 ;;   character.
1727
1728 (defun gnus-truncate-string (str width)
1729   (substring str 0 width))
1730
1731 ;; Added by Geoffrey T. Dairiki <dairiki@u.washington.edu>. A safe way
1732 ;; to limit the length of a string. This function is necessary since
1733 ;; `(substr "abc" 0 30)' pukes with "Args out of range".
1734 (defsubst gnus-limit-string (str width)
1735   (if (> (length str) width)
1736       (substring str 0 width)
1737     str))
1738
1739 (defsubst gnus-simplify-subject-re (subject)
1740   "Remove \"Re:\" from subject lines."
1741   (let ((case-fold-search t))
1742     (if (string-match "^re: *" subject)
1743         (substring subject (match-end 0))
1744       subject)))
1745
1746 (defsubst gnus-goto-char (point)
1747   (and point (goto-char point)))
1748
1749 (defmacro gnus-buffer-exists-p (buffer)
1750   (` (and (, buffer)
1751           (funcall (if (stringp (, buffer)) 'get-buffer 'buffer-name)
1752                    (, buffer)))))
1753
1754 (defmacro gnus-kill-buffer (buffer)
1755   (` (if (gnus-buffer-exists-p (, buffer))
1756          (kill-buffer (, buffer)))))
1757
1758 (defsubst gnus-point-at-bol ()
1759   "Return point at the beginning of line."
1760   (let ((p (point)))
1761     (beginning-of-line)
1762     (prog1
1763         (point)
1764       (goto-char p))))
1765
1766 (defsubst gnus-point-at-eol ()
1767   "Return point at the beginning of line."
1768   (let ((p (point)))
1769     (end-of-line)
1770     (prog1
1771         (point)
1772       (goto-char p))))
1773
1774 ;; Delete the current line (and the next N lines.);
1775 (defmacro gnus-delete-line (&optional n)
1776   (` (delete-region (progn (beginning-of-line) (point))
1777                     (progn (forward-line (, (or n 1))) (point)))))
1778
1779 ;;; Load the compatability functions. 
1780
1781 (require 'gnus-ems)
1782
1783 \f
1784 ;;;
1785 ;;; Gnus Utility Functions
1786 ;;;
1787
1788 (defun gnus-extract-address-components (from)
1789   (let (name address)
1790     ;; First find the address - the thing with the @ in it.  This may
1791     ;; not be accurate in mail addresses, but does the trick most of
1792     ;; the time in news messages.
1793     (if (string-match "\\b[^@ \t<>]+[!@][^@ \t<>]+\\b" from)
1794         (setq address (substring from (match-beginning 0) (match-end 0))))
1795     ;; Then we check whether the "name <address>" format is used.
1796     (and address
1797          (string-match (concat "<" (regexp-quote address) ">") from)
1798          (and (setq name (substring from 0 (1- (match-beginning 0))))
1799               ;; Strip any quotes from the name.
1800               (string-match "\".*\"" name)
1801               (setq name (substring name 1 (1- (match-end 0))))))
1802     ;; If not, then "address (name)" is used.
1803     (or name
1804         (and (string-match "(.+)" from)
1805              (setq name (substring from (1+ (match-beginning 0)) 
1806                                    (1- (match-end 0)))))
1807         (and (string-match "()" from)
1808              (setq name address))
1809         ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>.
1810         ;; XOVER might not support folded From headers.
1811         (and (string-match "(.*" from)
1812              (setq name (substring from (1+ (match-beginning 0)) 
1813                                    (match-end 0)))))
1814     ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
1815     (list (or name from) (or address from))))
1816
1817 (defun gnus-fetch-field (field)
1818   "Return the value of the header FIELD of current article."
1819   (save-excursion
1820     (save-restriction
1821       (let ((case-fold-search t))
1822         (gnus-narrow-to-headers)
1823         (mail-fetch-field field)))))
1824
1825 (defun gnus-goto-colon ()
1826   (beginning-of-line)
1827   (search-forward ":" (gnus-point-at-eol) t))
1828
1829 (defun gnus-narrow-to-headers ()
1830   (widen)
1831   (save-excursion
1832     (narrow-to-region
1833      (goto-char (point-min))
1834      (if (search-forward "\n\n" nil t)
1835          (1- (point))
1836        (point-max)))))
1837
1838 (defun gnus-update-format-specifications ()
1839   (gnus-make-thread-indent-array)
1840   (setq gnus-summary-line-format-spec 
1841         (gnus-parse-format
1842          gnus-summary-line-format gnus-summary-line-format-alist))
1843   (gnus-update-summary-mark-positions)
1844   (setq gnus-summary-dummy-line-format-spec 
1845         (gnus-parse-format gnus-summary-dummy-line-format 
1846                            gnus-summary-dummy-line-format-alist))
1847   (setq gnus-group-line-format-spec
1848         (gnus-parse-format 
1849          gnus-group-line-format 
1850          gnus-group-line-format-alist))
1851   (if (and (string-match "%D" gnus-group-line-format)
1852            (not gnus-description-hashtb)
1853            gnus-read-active-file)
1854       (gnus-read-all-descriptions-files))
1855   (setq gnus-summary-mode-line-format-spec 
1856         (gnus-parse-format gnus-summary-mode-line-format 
1857                            gnus-summary-mode-line-format-alist))
1858   (setq gnus-article-mode-line-format-spec 
1859         (gnus-parse-format gnus-article-mode-line-format 
1860                            gnus-summary-mode-line-format-alist))
1861   (setq gnus-group-mode-line-format-spec 
1862         (gnus-parse-format gnus-group-mode-line-format 
1863                            gnus-group-mode-line-format-alist)))
1864
1865 (defun gnus-update-summary-mark-positions ()
1866   (save-excursion
1867     (let ((gnus-replied-mark 129)
1868           (gnus-score-below-mark 130)
1869           (gnus-score-over-mark 130)
1870           (thread nil)
1871           pos)
1872       (gnus-set-work-buffer)
1873       (gnus-summary-insert-line 
1874        nil [0 "" "" "" "" "" 0 0 ""]  0 nil 128 t nil "" nil 1)
1875       (goto-char (point-min))
1876       (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
1877                                          (- (point) 2)))))
1878       (goto-char (point-min))
1879       (setq pos (cons (cons 'replied (and (search-forward "\201" nil t)
1880                                           (- (point) 2))) pos))
1881       (goto-char (point-min))
1882       (setq pos (cons (cons 'score (and (search-forward "\202" nil t)
1883                                         (- (point) 2))) pos))
1884       (setq gnus-summary-mark-positions pos))))
1885
1886 (defun gnus-format-max-width (form length)
1887   (let* ((val (eval form))
1888          (valstr (if (numberp val) (int-to-string val) val)))
1889     (gnus-limit-string valstr length)))
1890
1891 (defun gnus-set-mouse-face (string)
1892   ;; Set mouse face property on STRING.
1893   (put-text-property 0 (length string) 'mouse-face gnus-mouse-face string)
1894   string)
1895
1896
1897 (defun gnus-mouse-face-function (form)
1898   (` (let ((string (, form)))
1899        (put-text-property 0 (length string) 'mouse-face gnus-mouse-face string)
1900        string)))
1901
1902 (defun gnus-max-width-function (el max-width)
1903   (` (let* ((val (eval (, el)))
1904             (valstr (if (numberp val)
1905                         (int-to-string val) val)))
1906        (if (> (length valstr) (, max-width))
1907            (substring valstr 0 (, max-width))
1908          valstr))))
1909
1910 (defun gnus-parse-format (format spec-alist)
1911   ;; This function parses the FORMAT string with the help of the
1912   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
1913   ;; string.  If the FORMAT string contains the specifiers %( and %)
1914   ;; the text between them will have the mouse-face text property.
1915   (if (string-match "\\`\\(.*\\)%(\\(.*\\)%)\\(.*\n?\\)\\'" format)
1916       (if (and gnus-visual gnus-mouse-face)
1917           (let ((pre (substring format (match-beginning 1) (match-end 1)))
1918                 (button (substring format (match-beginning 2) (match-end 2)))
1919                 (post (substring format (match-beginning 3) (match-end 3))))
1920             (list 'concat
1921                   (gnus-parse-simple-format pre spec-alist)
1922                   (gnus-mouse-face-function 
1923                    (gnus-parse-simple-format button spec-alist))
1924                   (gnus-parse-simple-format post spec-alist)))
1925         (gnus-parse-simple-format
1926          (concat (substring format (match-beginning 1) (match-end 1))
1927                  (substring format (match-beginning 2) (match-end 2))
1928                  (substring format (match-beginning 3) (match-end 3)))
1929          spec-alist))
1930     (gnus-parse-simple-format format spec-alist)))
1931
1932 (defun gnus-parse-simple-format (format spec-alist)
1933   ;; This function parses the FORMAT string with the help of the
1934   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
1935   ;; string. The list will consist of the symbol `format', a format
1936   ;; specification string, and a list of forms depending on the
1937   ;; SPEC-ALIST.
1938   (let ((max-width 0)
1939         spec flist fstring newspec elem beg)
1940     (save-excursion
1941       (gnus-set-work-buffer)
1942       (insert format)
1943       (goto-char (point-min))
1944       (while (re-search-forward "%[-0-9]*\\(,[0-9]*\\)*\\(.\\)\\(.\\)?" nil t)
1945         (setq spec (string-to-char (buffer-substring (match-beginning 2)
1946                                                      (match-end 2))))
1947         ;; First check if there are any specs that look anything like
1948         ;; "%12,12A", ie. with a "max width specification". These have
1949         ;; to be treated specially.
1950         (if (setq beg (match-beginning 1))
1951             (setq max-width 
1952                   (string-to-int 
1953                    (buffer-substring (1+ (match-beginning 1)) (match-end 1))))
1954           (setq max-width 0)
1955           (setq beg (match-beginning 2)))
1956         ;; Find the specification from `spec-alist'.
1957         (if (not (setq elem (cdr (assq spec spec-alist))))
1958             (setq elem '("*" ?s)))
1959         ;; Treat user defined format specifiers specially
1960         (and (eq (car elem) 'user-defined)
1961              (setq elem
1962                    (list 
1963                     (list (intern (concat "gnus-user-format-function-"
1964                                           (buffer-substring
1965                                            (match-beginning 3)
1966                                            (match-end 3))))
1967                           'header)
1968                     ?s))
1969              (delete-region (match-beginning 3) (match-end 3)))
1970         (if (not (zerop max-width))
1971             (let ((el (car elem)))
1972               (cond ((= (car (cdr elem)) ?c) 
1973                      (setq el (list 'char-to-string el)))
1974                     ((= (car (cdr elem)) ?d)
1975                      (numberp el) (setq el (list 'int-to-string el))))
1976               (setq flist (cons (gnus-max-width-function el max-width)
1977                                 flist))
1978               (setq newspec ?s))
1979           (setq flist (cons (car elem) flist))
1980           (setq newspec (car (cdr elem))))
1981         ;; Remove the old specification (and possibly a ",12" string).
1982         (delete-region beg (match-end 2))
1983         ;; Insert the new specification.
1984         (goto-char beg)
1985         (insert newspec))
1986       (setq fstring (buffer-substring 1 (point-max))))
1987     (cons 'format (cons fstring (nreverse flist)))))
1988
1989 ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
1990 (defun gnus-read-init-file ()
1991   (and gnus-init-file
1992        (or (and (file-exists-p gnus-init-file) 
1993                 ;; Don't try to load a directory.
1994                 (not (file-directory-p gnus-init-file)))
1995            (file-exists-p (concat gnus-init-file ".el"))
1996            (file-exists-p (concat gnus-init-file ".elc")))
1997        (load gnus-init-file nil t)))
1998
1999 (defun gnus-set-work-buffer ()
2000   (if (get-buffer gnus-work-buffer)
2001       (progn
2002         (set-buffer gnus-work-buffer)
2003         (erase-buffer))
2004     (set-buffer (get-buffer-create gnus-work-buffer))
2005     (kill-all-local-variables)
2006     (buffer-disable-undo (current-buffer))
2007     (gnus-add-current-to-buffer-list)))
2008
2009 ;; Article file names when saving.
2010
2011 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
2012   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2013 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group/num.
2014 Otherwise, it is like ~/News/news/group/num."
2015   (let ((default
2016           (expand-file-name
2017            (concat (if (gnus-use-long-file-name 'not-save)
2018                        (gnus-capitalize-newsgroup newsgroup)
2019                      (gnus-newsgroup-directory-form newsgroup))
2020                    "/" (int-to-string (header-number headers)))
2021            (or gnus-article-save-directory "~/News"))))
2022     (if (and last-file
2023              (string-equal (file-name-directory default)
2024                            (file-name-directory last-file))
2025              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2026         default
2027       (or last-file default))))
2028
2029 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
2030   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2031 If variable `gnus-use-long-file-name' is nil, it is ~/News/news.group/num.
2032 Otherwise, it is like ~/News/news/group/num."
2033   (let ((default
2034           (expand-file-name
2035            (concat (if (gnus-use-long-file-name 'not-save)
2036                        newsgroup
2037                      (gnus-newsgroup-directory-form newsgroup))
2038                    "/" (int-to-string (header-number headers)))
2039            (or gnus-article-save-directory "~/News"))))
2040     (if (and last-file
2041              (string-equal (file-name-directory default)
2042                            (file-name-directory last-file))
2043              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2044         default
2045       (or last-file default))))
2046
2047 (defun gnus-Plain-save-name (newsgroup headers &optional last-file)
2048   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2049 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group.
2050 Otherwise, it is like ~/News/news/group/news."
2051   (or last-file
2052       (expand-file-name
2053        (if (gnus-use-long-file-name 'not-save)
2054            (gnus-capitalize-newsgroup newsgroup)
2055          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2056        (or gnus-article-save-directory "~/News"))))
2057
2058 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
2059   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2060 If variable `gnus-use-long-file-name' is nil, it is ~/News/news.group.
2061 Otherwise, it is like ~/News/news/group/news."
2062   (or last-file
2063       (expand-file-name
2064        (if (gnus-use-long-file-name 'not-save)
2065            newsgroup
2066          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2067        (or gnus-article-save-directory "~/News"))))
2068
2069 ;; For subscribing new newsgroup
2070
2071 (defun gnus-subscribe-hierarchical-interactive (groups)
2072   (let ((groups (sort groups 'string<))
2073         prefixes prefix start ans group starts)
2074     (while groups
2075       (setq prefixes (list "^"))
2076       (while (and groups prefixes)
2077         (while (not (string-match (car prefixes) (car groups)))
2078           (setq prefixes (cdr prefixes)))
2079         (setq prefix (car prefixes))
2080         (setq start (1- (length prefix)))
2081         (if (and (string-match "[^\\.]\\." (car groups) start)
2082                  (cdr groups)
2083                  (setq prefix 
2084                        (concat "^" (substring (car groups) 0 (match-end 0))))
2085                  (string-match prefix (car (cdr groups))))
2086             (progn
2087               (setq prefixes (cons prefix prefixes))
2088               (message "Descend hierarchy %s? ([y]nsq): " 
2089                        (substring prefix 1 (1- (length prefix))))
2090               (setq ans (read-char))
2091               (cond ((= ans ?n)
2092                      (while (and groups 
2093                                  (string-match prefix 
2094                                                (setq group (car groups))))
2095                        (setq gnus-killed-list 
2096                              (cons group gnus-killed-list))
2097                        (gnus-sethash group group gnus-killed-hashtb)
2098                        (setq groups (cdr groups)))
2099                      (setq starts (cdr starts)))
2100                     ((= ans ?s)
2101                      (while (and groups 
2102                                  (string-match prefix 
2103                                                (setq group (car groups))))
2104                        (gnus-sethash group group gnus-killed-hashtb)
2105                        (gnus-subscribe-alphabetically (car groups))
2106                        (setq groups (cdr groups)))
2107                      (setq starts (cdr starts)))
2108                     ((= ans ?q)
2109                      (while groups
2110                        (setq group (car groups))
2111                        (setq gnus-killed-list (cons group gnus-killed-list))
2112                        (gnus-sethash group group gnus-killed-hashtb)
2113                        (setq groups (cdr groups))))
2114                     (t nil)))
2115           (message "Subscribe %s? ([n]yq)" (car groups))
2116           (setq ans (read-char))
2117           (setq group (car groups))
2118           (cond ((= ans ?y)
2119                  (gnus-subscribe-alphabetically (car groups))
2120                  (gnus-sethash group group gnus-killed-hashtb))
2121                 ((= ans ?q)
2122                  (while groups
2123                    (setq group (car groups))
2124                    (setq gnus-killed-list (cons group gnus-killed-list))
2125                    (gnus-sethash group group gnus-killed-hashtb)
2126                    (setq groups (cdr groups))))
2127                 (t 
2128                  (setq gnus-killed-list (cons group gnus-killed-list))
2129                  (gnus-sethash group group gnus-killed-hashtb)))
2130           (setq groups (cdr groups)))))))
2131
2132 (defun gnus-subscribe-randomly (newsgroup)
2133   "Subscribe new NEWSGROUP by making it the first newsgroup."
2134   (gnus-subscribe-newsgroup newsgroup))
2135
2136 (defun gnus-subscribe-alphabetically (newgroup)
2137   "Subscribe new NEWSGROUP and insert it in alphabetical order."
2138   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2139   (let ((groups (cdr gnus-newsrc-alist))
2140         before)
2141     (while (and (not before) groups)
2142       (if (string< newgroup (car (car groups)))
2143           (setq before (car (car groups)))
2144         (setq groups (cdr groups))))
2145     (gnus-subscribe-newsgroup newgroup before)))
2146
2147 (defun gnus-subscribe-hierarchically (newgroup)
2148   "Subscribe new NEWSGROUP and insert it in hierarchical newsgroup order."
2149   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2150   (save-excursion
2151     (set-buffer (find-file-noselect gnus-current-startup-file))
2152     (let ((groupkey newgroup)
2153           before)
2154       (while (and (not before) groupkey)
2155         (goto-char (point-min))
2156         (let ((groupkey-re
2157                (concat "^\\(" (regexp-quote groupkey) ".*\\)[!:]")))
2158           (while (and (re-search-forward groupkey-re nil t)
2159                       (progn
2160                         (setq before (buffer-substring
2161                                       (match-beginning 1) (match-end 1)))
2162                         (string< before newgroup)))))
2163         ;; Remove tail of newsgroup name (eg. a.b.c -> a.b)
2164         (setq groupkey
2165               (if (string-match "^\\(.*\\)\\.[^.]+$" groupkey)
2166                   (substring groupkey (match-beginning 1) (match-end 1)))))
2167       (gnus-subscribe-newsgroup newgroup before))))
2168
2169 (defun gnus-subscribe-interactively (newsgroup)
2170   "Subscribe new NEWSGROUP interactively.
2171 It is inserted in hierarchical newsgroup order if subscribed. If not,
2172 it is killed."
2173   (if (gnus-y-or-n-p (format "Subscribe new newsgroup: %s " newsgroup))
2174       (gnus-subscribe-hierarchically newsgroup)
2175     (setq gnus-killed-list (cons newsgroup gnus-killed-list))))
2176
2177 (defun gnus-subscribe-zombies (newsgroup)
2178   "Make new NEWSGROUP a zombie group."
2179   (setq gnus-zombie-list (cons newsgroup gnus-zombie-list)))
2180
2181 (defun gnus-subscribe-newsgroup (newsgroup &optional next)
2182   "Subscribe new NEWSGROUP.
2183 If NEXT is non-nil, it is inserted before NEXT. Otherwise it is made
2184 the first newsgroup."
2185   ;; We subscribe the group by changing its level to `subscribed'.
2186   (gnus-group-change-level 
2187    newsgroup gnus-level-default-subscribed
2188    gnus-level-killed (gnus-gethash (or next "dummy.group") gnus-newsrc-hashtb))
2189   (gnus-message 5 "Subscribe newsgroup: %s" newsgroup))
2190
2191 ;; For directories
2192
2193 (defun gnus-newsgroup-directory-form (newsgroup)
2194   "Make hierarchical directory name from NEWSGROUP name."
2195   (let ((newsgroup (substring newsgroup 0)) ;Copy string.
2196         (len (length newsgroup))
2197         idx)
2198     ;; If this is a foreign group, we don't want to translate the
2199     ;; entire name.  
2200     (if (setq idx (string-match ":" newsgroup))
2201         (aset newsgroup idx ?/)
2202       (setq idx 0))
2203     ;; Replace all occurrences of `.' with `/'.
2204     (while (< idx len)
2205       (if (= (aref newsgroup idx) ?.)
2206           (aset newsgroup idx ?/))
2207       (setq idx (1+ idx)))
2208     newsgroup))
2209
2210 (defun gnus-make-directory (dir)
2211   "Make DIRECTORY recursively."
2212   (let* ((dir (expand-file-name dir default-directory))
2213          dirs)
2214     (if (string-match "/$" dir)
2215         (setq dir (substring dir 0 (match-beginning 0))))
2216     (while (not (file-exists-p dir))
2217       (setq dirs (cons dir dirs))
2218       (string-match "/[^/]+$" dir)
2219       (setq dir (substring dir 0 (match-beginning 0))))
2220     (while dirs
2221       (make-directory (car dirs))
2222       (setq dirs (cdr dirs)))))
2223
2224 (defun gnus-capitalize-newsgroup (newsgroup)
2225   "Capitalize NEWSGROUP name."
2226   (and (not (zerop (length newsgroup)))
2227        (concat (char-to-string (upcase (aref newsgroup 0)))
2228                (substring newsgroup 1))))
2229
2230 ;; Var
2231
2232 (defun gnus-simplify-subject (subject &optional re-only)
2233   "Remove `Re:' and words in parentheses.
2234 If optional argument RE-ONLY is non-nil, strip `Re:' only."
2235   (let ((case-fold-search t))           ;Ignore case.
2236     ;; Remove `Re:' and `Re^N:'.
2237     (if (string-match "^re:[ \t]*" subject)
2238         (setq subject (substring subject (match-end 0))))
2239     ;; Remove words in parentheses from end.
2240     (or re-only
2241         (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
2242           (setq subject (substring subject 0 (match-beginning 0)))))
2243     ;; Return subject string.
2244     subject))
2245
2246 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
2247 ;; all whitespace.
2248 (defun gnus-simplify-subject-fuzzy (subject)
2249   (let ((case-fold-search t))
2250     (save-excursion
2251       (gnus-set-work-buffer)
2252       (insert subject)
2253       (inline (gnus-simplify-buffer-fuzzy))
2254       (buffer-string))))
2255
2256 (defun gnus-simplify-buffer-fuzzy ()
2257   (goto-char (point-min))
2258   ;; Fix by Stainless Steel Rat <ratinox@ccs.neu.edu>.
2259   (while (re-search-forward "^[ \t]*\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;][ \t]*"
2260                             nil t)
2261     (replace-match "" t t))
2262   (goto-char (point-min))
2263   (while (re-search-forward "[ \t\n]*([^()]*)[ \t\n]*$" nil t)
2264     (replace-match "" t t))
2265   (goto-char (point-min))
2266   (while (re-search-forward "[ \t]+" nil t)
2267     (replace-match " " t t))
2268   (goto-char (point-min))
2269   (while (re-search-forward "[ \t]+$" nil t)
2270     (replace-match "" t t))
2271   (goto-char (point-min))
2272   (while (re-search-forward "^[ \t]+" nil t)
2273     (replace-match "" t t))
2274   (if gnus-simplify-subject-fuzzy-regexp
2275       (while (re-search-forward gnus-simplify-subject-fuzzy-regexp nil t)
2276         (replace-match "" t t)))
2277     )
2278
2279 ;; Add the current buffer to the list of buffers to be killed on exit. 
2280 (defun gnus-add-current-to-buffer-list ()
2281   (or (memq (current-buffer) gnus-buffer-list)
2282       (setq gnus-buffer-list (cons (current-buffer) gnus-buffer-list))))
2283
2284 (defun gnus-string> (s1 s2)
2285   (not (or (string< s1 s2)
2286            (string= s1 s2))))
2287
2288 ;; Functions accessing headers.
2289 ;; Functions are more convenient than macros in some cases.
2290
2291 (defun gnus-header-number (header)
2292   (header-number header))
2293
2294 (defun gnus-header-subject (header)
2295   (header-subject header))
2296
2297 (defun gnus-header-from (header)
2298   (header-from header))
2299
2300 (defun gnus-header-xref (header)
2301   (header-xref header))
2302
2303 (defun gnus-header-lines (header)
2304   (header-lines header))
2305
2306 (defun gnus-header-date (header)
2307   (header-date header))
2308
2309 (defun gnus-header-id (header)
2310   (header-id header))
2311
2312 (defun gnus-header-references (header)
2313   (header-references header))
2314
2315 ;;; General various misc type functions.
2316
2317 (defun gnus-clear-system ()
2318   "Clear all variables and buffers."
2319   ;; Clear Gnus variables.
2320   (let ((variables gnus-variable-list))
2321     (while variables
2322       (set (car variables) nil)
2323       (setq variables (cdr variables))))
2324   ;; Clear other internal variables.
2325   (setq gnus-list-of-killed-groups nil
2326         gnus-have-read-active-file nil
2327         gnus-newsrc-alist nil
2328         gnus-newsrc-hashtb nil
2329         gnus-killed-list nil
2330         gnus-zombie-list nil
2331         gnus-killed-hashtb nil
2332         gnus-active-hashtb nil
2333         gnus-moderated-list nil
2334         gnus-description-hashtb nil
2335         gnus-newsgroup-headers nil
2336         gnus-newsgroup-headers-hashtb-by-number nil
2337         gnus-newsgroup-name nil
2338         gnus-server-alist nil
2339         gnus-current-select-method nil)
2340   ;; Reset any score variables.
2341   (and (boundp 'gnus-score-cache)
2342        (set 'gnus-score-cache nil))
2343   (and (boundp 'gnus-internal-global-score-files)
2344        (set 'gnus-internal-global-score-files nil))
2345   ;; Kill the startup file.
2346   (and gnus-current-startup-file
2347        (get-file-buffer gnus-current-startup-file)
2348        (kill-buffer (get-file-buffer gnus-current-startup-file)))
2349   ;; Save any cache buffers.
2350   (and gnus-use-cache (gnus-cache-save-buffers))
2351   ;; Clear the dribble buffer.
2352   (gnus-dribble-clear)
2353   ;; Kill global KILL file buffer.
2354   (if (get-file-buffer (gnus-newsgroup-kill-file nil))
2355       (kill-buffer (get-file-buffer (gnus-newsgroup-kill-file nil))))
2356   (gnus-kill-buffer nntp-server-buffer)
2357   ;; Kill Gnus buffers.
2358   (while gnus-buffer-list
2359     (gnus-kill-buffer (car gnus-buffer-list))
2360     (setq gnus-buffer-list (cdr gnus-buffer-list))))
2361
2362 (defun gnus-windows-old-to-new (setting)
2363   (if (symbolp setting)
2364       (setq setting 
2365             (cond ((eq setting 'SelectArticle)
2366                    'article)
2367                   ((eq setting 'SelectSubject)
2368                    'summary)
2369                   ((eq setting 'SelectNewsgroup)
2370                    'group)
2371                   (t setting))))
2372   (if (or (listp setting)
2373           (not (and gnus-window-configuration
2374                     (memq setting '(group summary article)))))
2375       setting
2376     (let* ((setting (if (eq setting 'group) 
2377                         (if (assq 'newsgroup gnus-window-configuration)
2378                             'newsgroup
2379                           'newsgroups) setting))
2380            (elem (car (cdr (assq setting gnus-window-configuration))))
2381            (total (apply '+ elem))
2382            (types '(group summary article))
2383            (pbuf (if (eq setting 'newsgroups) 'group 'summary))
2384            (i 0)
2385            perc
2386            out)
2387       (while (< i 3)
2388         (or (zerop (nth i elem))
2389             (progn
2390               (setq perc  (/ (* 1.0 (nth 0 elem)) total))
2391               (setq out (cons (if (eq pbuf (nth i types))
2392                                   (vector (nth i types) perc 'point)
2393                                 (vector (nth i types) perc))
2394                               out))))
2395         (setq i (1+ i)))
2396       (list (nreverse out)))))
2397            
2398 (defun gnus-add-configuration (conf)
2399   (setq gnus-buffer-configuration 
2400         (cons conf (delq (assq (car conf) gnus-buffer-configuration)
2401                          gnus-buffer-configuration))))
2402
2403 (defun gnus-configure-windows (setting &optional force)
2404   (setq setting (gnus-windows-old-to-new setting))
2405   (let ((r (if (symbolp setting)
2406                (cdr (assq setting gnus-buffer-configuration))
2407              setting))
2408         (in-buf (current-buffer))
2409         rule val w height hor ohor heights sub jump-buffer
2410         rel total to-buf all-visible)
2411     (or r (error "No such setting: %s" setting))
2412
2413     (if (and (not force) (setq all-visible (gnus-all-windows-visible-p r)))
2414         ;; All the windows mentioned are already visibe, so we just
2415         ;; put point in the assigned buffer, and do not touch the
2416         ;; winconf. 
2417         (select-window (get-buffer-window all-visible))
2418
2419       ;; Either remove all windows or just remove all Gnus windows.
2420       (if gnus-use-full-window
2421           (delete-other-windows)
2422         (gnus-remove-some-windows)
2423         (switch-to-buffer nntp-server-buffer))
2424
2425       (while r
2426         (setq hor (car r)
2427               ohor nil)
2428
2429         ;; We have to do the (possible) horizontal splitting before the
2430         ;; vertical. 
2431         (if (and (listp (car hor)) 
2432                  (eq (car (car hor)) 'horizontal))
2433             (progn
2434               (split-window 
2435                nil
2436                (if (integerp (nth 1 (car hor)))
2437                    (nth 1 (car hor))
2438                  (- (frame-width) (floor (* (frame-width) (nth 1 (car hor))))))
2439                t)
2440               (setq hor (cdr hor))))
2441
2442         ;; Go through the rules and eval the elements that are to be
2443         ;; evaled.  
2444         (while hor
2445           (if (setq val (if (vectorp (car hor)) (car hor) (eval (car hor))))
2446               (progn
2447                 ;; Expand short buffer name.
2448                 (setq w (aref val 0))
2449                 (and (setq w (cdr (assq w gnus-window-to-buffer)))
2450                      (progn
2451                        (setq val (apply 'vector (mapcar 'identity val)))
2452                        (aset val 0 w)))
2453                 (setq ohor (cons val ohor))))
2454           (setq hor (cdr hor)))
2455         (setq rule (cons (nreverse ohor) rule))
2456         (setq r (cdr r)))
2457       (setq rule (nreverse rule))
2458
2459       ;; We tally the window sizes.
2460       (setq total (window-height))
2461       (while rule
2462         (setq hor (car rule))
2463         (if (and (listp (car hor)) (eq (car (car hor)) 'horizontal))
2464             (setq hor (cdr hor)))
2465         (setq sub 0)
2466         (while hor
2467           (setq rel (aref (car hor) 1)
2468                 heights (cons
2469                          (cond ((and (floatp rel) (= 1.0 rel))
2470                                 'x)
2471                                ((integerp rel)
2472                                 rel)
2473                                (t
2474                                 (max (floor (* total rel)) 4)))
2475                          heights)
2476                 sub (+ sub (if (numberp (car heights)) (car heights) 0))
2477                 hor (cdr hor)))
2478         (setq heights (nreverse heights)
2479               hor (car rule))
2480
2481         ;; We then go through these heighs and create windows for them.
2482         (while heights
2483           (setq height (car heights)
2484                 heights (cdr heights))
2485           (and (eq height 'x)
2486                (setq height (- total sub)))
2487           (and heights
2488                (split-window nil height))
2489           (setq to-buf (aref (car hor) 0))
2490           (switch-to-buffer 
2491            (cond ((not to-buf)
2492                   in-buf)
2493                  ((symbolp to-buf)
2494                   (symbol-value (aref (car hor) 0)))
2495                  (t
2496                   (aref (car hor) 0))))
2497           (and (> (length (car hor)) 2)
2498                (eq (aref (car hor) 2) 'point)
2499                (setq jump-buffer (current-buffer)))
2500           (other-window 1)
2501           (setq hor (cdr hor)))
2502       
2503         (setq rule (cdr rule)))
2504
2505       ;; Finally, we pop to the buffer that's supposed to have point. 
2506       (or jump-buffer (error "Missing `point' in spec for %s" setting))
2507
2508       (select-window (get-buffer-window jump-buffer))
2509       (set-buffer jump-buffer))))
2510
2511 (defun gnus-all-windows-visible-p (rule)
2512   (let (invisible hor jump-buffer val buffer)
2513     ;; Go through the rules and eval the elements that are to be
2514     ;; evaled.  
2515     (while (and rule (not invisible))
2516       (setq hor (car rule)
2517             rule (cdr rule))
2518       (while (and hor (not invisible))
2519         (if (setq val (if (vectorp (car hor)) 
2520                           (car hor)
2521                         (if (not (eq (car (car hor)) 'horizontal))
2522                             (eval (car hor)))))
2523             (progn
2524               ;; Expand short buffer name.
2525               (setq buffer (or (cdr (assq (aref val 0) gnus-window-to-buffer))
2526                                (aref val 0)))
2527               (setq buffer (if (symbolp buffer) (symbol-value buffer)
2528                              buffer))
2529               (and (> (length val) 2) (eq 'point (aref val 2))
2530                    (setq jump-buffer buffer))
2531               (setq invisible (not (and buffer (get-buffer-window buffer))))))
2532         (setq hor (cdr hor))))
2533     (and (not invisible) jump-buffer)))
2534
2535       
2536 (defun gnus-remove-some-windows ()
2537   (let ((buffers gnus-window-to-buffer)
2538         (first t)
2539         buf)
2540     (save-excursion
2541       ;; Remove windows on all known Gnus buffers.
2542       (while buffers
2543         (setq buf (cdr (car buffers)))
2544         (if (symbolp buf)
2545             (setq buf (and (boundp buf) (symbol-value buf))))
2546         (and buf 
2547              (get-buffer-window buf)
2548              (progn
2549                (if first
2550                    (progn
2551                      (pop-to-buffer buf)
2552                      (switch-to-buffer nntp-server-buffer)
2553                      (setq first nil))
2554                  (delete-window (get-buffer-window buf)))))
2555         (setq buffers (cdr buffers)))
2556       ;; Remove windows on *all* summary buffers.
2557       (let ((buffers (buffer-list)))
2558         (while buffers
2559           (if (and (string-match 
2560                     "^\\*Summary" (or (buffer-name (car buffers)) ""))
2561                    (get-buffer-window (car buffers)))
2562               (delete-window (get-buffer-window (car buffers))))
2563           (setq buffers (cdr buffers)))))))
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     (let ((level (and arg (numberp arg) (> arg 0) arg))
3159           did-connect)
3160       (unwind-protect
3161           (progn
3162             (gnus-group-setup-buffer)
3163             (or dont-connect 
3164                 (setq did-connect
3165                       (gnus-start-news-server (and arg (not level))))))
3166         (if (and (not dont-connect) 
3167                  (not did-connect))
3168             (gnus-group-quit)
3169           (run-hooks 'gnus-startup-hook)
3170           ;; NNTP server is successfully open. 
3171           (gnus-update-format-specifications)
3172           (gnus-summary-make-display-table)
3173           (let ((buffer-read-only nil))
3174             (erase-buffer)
3175             (if (not gnus-inhibit-startup-message)
3176                 (progn
3177                   (gnus-group-startup-message)
3178                   (sit-for 0))))
3179           (gnus-setup-news nil level)
3180           (and gnus-use-dribble-file (gnus-dribble-open))
3181           (gnus-group-list-groups level)
3182           (gnus-configure-windows 'group))))))
3183
3184 (defun gnus-unload ()
3185   "Unload all Gnus features."
3186   (interactive)
3187   (let ((history load-history)
3188         feature)
3189     (while history
3190       (and (string-match "^gnus" (car (car history)))
3191            (setq feature (cdr (assq 'provide (car history))))
3192            (unload-feature feature 'force))
3193       (setq history (cdr history)))))
3194
3195 (defun gnus-group-startup-message (&optional x y)
3196   "Insert startup message in current buffer."
3197   ;; Insert the message.
3198   (erase-buffer)
3199   (insert
3200    (format "
3201      %s
3202            A newsreader 
3203       for GNU Emacs
3204
3205         Based on GNUS 
3206              written by 
3207      Masanobu UMEDA
3208
3209        A Praxis Release
3210       larsi@ifi.uio.no
3211
3212            gnus-version))
3213   ;; And then hack it.
3214   ;; 18 is the longest line.
3215   (indent-rigidly (point-min) (point-max) 
3216                   (/ (max (- (window-width) (or x 28)) 0) 2))
3217   (goto-char (point-min))
3218   ;; +4 is fuzzy factor.
3219   (insert-char ?\n (/ (max (- (window-height) (or y 12)) 0) 2))
3220
3221   ;; Fontify some.
3222   (goto-char (point-min))
3223   (search-forward "Praxis")
3224   (put-text-property (match-beginning 0) (match-end 0) 'face 'bold)
3225   (goto-char (point-min)))
3226
3227 (defun gnus-group-setup-buffer ()
3228   (or (get-buffer gnus-group-buffer)
3229       (progn
3230         (switch-to-buffer gnus-group-buffer)
3231         (gnus-add-current-to-buffer-list)
3232         (gnus-group-mode)
3233         (and gnus-carpal (gnus-carpal-setup-buffer 'group)))))
3234
3235 (defun gnus-group-list-groups (level &optional unread)
3236   "List newsgroups with level LEVEL or lower that have unread articles.
3237 Default is all subscribed groups.
3238 If argument UNREAD is non-nil, groups with no unread articles are also listed."
3239   (interactive (list (and current-prefix-arg
3240                           (prefix-numeric-value current-prefix-arg))))
3241   (if gnus-group-use-permanent-levels
3242       (progn
3243         (setq gnus-group-default-list-level 
3244               (or level gnus-group-default-list-level))
3245         (setq level (or gnus-group-default-list-level gnus-level-subscribed)))
3246     (setq level (or level gnus-group-default-list-level 
3247                     gnus-level-subscribed)))
3248   (gnus-group-setup-buffer)     ;May call from out of group buffer
3249   (let ((case-fold-search nil)
3250         (group (gnus-group-group-name)))
3251     (funcall gnus-group-prepare-function level unread nil)
3252     (if (zerop (buffer-size))
3253         (gnus-message 5 gnus-no-groups-message)
3254       (goto-char (point-min))
3255       (if (not group)
3256           ;; Go to the first group with unread articles.
3257           (gnus-group-search-forward nil nil nil t)
3258         ;; Find the right group to put point on. If the current group
3259         ;; has disapeared in the new listing, try to find the next
3260         ;; one. If no next one can be found, just leave point at the
3261         ;; first newsgroup in the buffer.
3262         (if (not (gnus-goto-char
3263                   (text-property-any (point-min) (point-max) 
3264                                      'gnus-group (intern group))))
3265             (let ((newsrc (nthcdr 3 (gnus-gethash group gnus-newsrc-hashtb))))
3266               (while (and newsrc
3267                           (not (gnus-goto-char 
3268                                 (text-property-any 
3269                                  (point-min) (point-max) 'gnus-group 
3270                                  (intern (car (car newsrc)))))))
3271                 (setq newsrc (cdr newsrc)))
3272               (or newsrc (progn (goto-char (point-max))
3273                                 (forward-line -1))))))
3274       ;; Adjust cursor point.
3275       (gnus-group-position-cursor))))
3276
3277 (defun gnus-group-prepare-flat (level &optional all lowest regexp) 
3278   "List all newsgroups with unread articles of level LEVEL or lower.
3279 If ALL is non-nil, list groups that have no unread articles.
3280 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher.
3281 If REGEXP, only list groups matching REGEXP."
3282   (set-buffer gnus-group-buffer)
3283   (let ((buffer-read-only nil)
3284         (newsrc (cdr gnus-newsrc-alist))
3285         (lowest (or lowest 1))
3286         info clevel unread group)
3287     (erase-buffer)
3288     (if (< lowest gnus-level-zombie)
3289         ;; List living groups.
3290         (while newsrc
3291           (setq info (car newsrc)
3292                 group (car info)
3293                 newsrc (cdr newsrc)
3294                 unread (car (gnus-gethash group gnus-newsrc-hashtb)))
3295           (and unread ; This group might be bogus
3296                (or (not regexp)
3297                    (string-match regexp group))
3298                (<= (setq clevel (car (cdr info))) level) 
3299                (>= clevel lowest)
3300                (or all            ; We list all groups?
3301                    (eq unread t)  ; We list unactivated groups
3302                    (> unread 0)   ; We list groups with unread articles
3303                    (cdr (assq 'tick (nth 3 info)))) ; And groups with tickeds
3304                (gnus-group-insert-group-line 
3305                 nil group (car (cdr info)) (nth 3 info) unread (nth 4 info)))))
3306
3307     ;; List dead groups.
3308     (and (>= level gnus-level-zombie) (<= lowest gnus-level-zombie)
3309          (gnus-group-prepare-flat-list-dead 
3310           (setq gnus-zombie-list (sort gnus-zombie-list 'string<)) 
3311           gnus-level-zombie ?Z
3312           regexp))
3313     (and (>= level gnus-level-killed) (<= lowest gnus-level-killed)
3314          (gnus-group-prepare-flat-list-dead 
3315           (setq gnus-killed-list (sort gnus-killed-list 'string<)) 
3316           gnus-level-killed ?K regexp))
3317
3318     (gnus-group-set-mode-line)
3319     (setq gnus-have-all-newsgroups all)
3320     (run-hooks 'gnus-group-prepare-hook)))
3321
3322 (defun gnus-group-prepare-flat-list-dead (groups level mark regexp)
3323   ;; List zombies and killed lists somehwat faster, which was
3324   ;; suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>. It does
3325   ;; this by ignoring the group format specification altogether.
3326   (let (group beg)
3327     (while groups
3328       (setq group (car groups)
3329             groups (cdr groups))
3330       (if (or (not regexp)
3331               (string-match regexp group))
3332           (progn
3333             (setq beg (point))
3334             (insert (format " %c     *: %s\n" mark group))
3335             (add-text-properties 
3336              beg (1+ beg) 
3337              (list 'gnus-group (intern group)
3338                    'gnus-unread t
3339                    'gnus-level level)))))))
3340
3341 (defun gnus-group-real-name (group)
3342   "Find the real name of a foreign newsgroup."
3343   (if (string-match ":[^:]+$" group)
3344       (substring group (1+ (match-beginning 0)))
3345     group))
3346
3347 (defun gnus-group-prefixed-name (group method)
3348   "Return the whole name from GROUP and METHOD."
3349   (and (stringp method) (setq method (gnus-server-to-method method)))
3350   (concat (format "%s" (car method))
3351           (if (and 
3352                (assoc (format "%s" (car method)) (gnus-methods-using 'address))
3353                (not (string= (nth 1 method) "")))
3354               (concat "+" (nth 1 method)))
3355           ":" group))
3356
3357 (defun gnus-group-real-prefix (group)
3358   "Return the prefix of the current group name."
3359   (if (string-match "^[^:]+:" group)
3360       (substring group 0 (match-end 0))
3361     ""))
3362
3363 (defun gnus-group-method-name (group)
3364   "Return the method used for selecting GROUP."
3365   (let ((prefix (gnus-group-real-prefix group)))
3366     (if (equal prefix "")
3367         gnus-select-method
3368       (if (string-match "^[^\\+]+\\+" prefix)
3369           (list (intern (substring prefix 0 (1- (match-end 0))))
3370                 (substring prefix (match-end 0) (1- (length prefix))))
3371         (list (intern (substring prefix 0 (1- (length prefix)))) "")))))
3372
3373 (defun gnus-group-foreign-p (group)
3374   "Return nil if GROUP is native, non-nil if it is foreign."
3375   (string-match ":" group))
3376
3377 (defun gnus-group-set-info (info &optional method-only-group part)
3378   (let* ((entry (gnus-gethash
3379                  (or method-only-group (car info)) gnus-newsrc-hashtb))
3380          (part-info info)
3381          (info (if method-only-group (nth 2 entry) info)))
3382     (if (not method-only-group)
3383         ()
3384       (or entry
3385           (error "Trying to change non-existent group %s" method-only-group))
3386       ;; We have recevied parts of the actual group info - either the
3387       ;; select method or the group parameters.  We first check
3388       ;; whether we have to extend the info, and if so, do that.
3389       (let ((len (length info))
3390             (total (if (eq part 'method) 5 6)))
3391         (and (< len total)
3392              (setcdr (nthcdr (1- len) info)
3393                      (make-list (- total len) nil)))
3394         ;; Then we enter the new info.
3395         (setcar (nthcdr (1- total) info) part-info)))
3396     ;; We uncompress some lists of marked articles.
3397     (let (marked)
3398       (if (not (setq marked (nth 3 info)))
3399           ()
3400         (while marked
3401           (or (eq 'score (car (car marked)))
3402               (eq 'bookmark (car (car marked)))
3403               (eq 'killed (car (car marked)))
3404               (setcdr (car marked) 
3405                       (gnus-uncompress-range (cdr (car marked)))))
3406           (setq marked (cdr marked)))))
3407     (if entry
3408         ()
3409       ;; This is a new group, so we just create it.
3410       (save-excursion
3411         (set-buffer gnus-group-buffer)
3412         (if (nth 4 info)
3413             ;; It's a foreign group...
3414             (gnus-group-make-group 
3415              (gnus-group-real-name (car info))
3416              (prin1-to-string (car (nth 4 info)))
3417              (nth 1 (nth 4 info)))
3418           ;; It's a native group.
3419           (gnus-group-make-group
3420            (car info)
3421            (prin1-to-string (car gnus-select-method))
3422            (nth 1 gnus-select-method)))
3423         (gnus-message 6 "Note: New group created")
3424         (setq entry 
3425               (gnus-gethash (gnus-group-prefixed-name 
3426                              (gnus-group-real-name (car info))
3427                              (or (nth 4 info) gnus-select-method))
3428                             gnus-newsrc-hashtb))))
3429     ;; Whether it was a new group or not, we now have the entry, so we
3430     ;; can do the update.
3431     (if entry
3432         (progn
3433           (setcar (nthcdr 2 entry) info)
3434           (if (and (not (eq (car entry) t)) 
3435                    (gnus-gethash (car info) gnus-active-hashtb))
3436               (let ((marked (nth 3 info)))
3437                 (setcar entry 
3438                         (max 0 (- (length (gnus-list-of-unread-articles 
3439                                            (car info)))
3440                                   (length (cdr (assq 'tick marked)))
3441                                   (length (cdr (assq 'dormant marked)))))))))
3442       (error "No such group: %s" (car info)))))
3443
3444 (defun gnus-group-set-method-info (group select-method)
3445   (gnus-group-set-info select-method group 'method))
3446
3447 (defun gnus-group-set-params-info (group params)
3448   (gnus-group-set-info params group 'params))
3449
3450 (defun gnus-group-update-group-line ()
3451   "This function updates the current line in the newsgroup buffer and
3452 moves the point to the colon."
3453   (let* ((buffer-read-only nil)
3454          (group (gnus-group-group-name))
3455          (entry (and group (gnus-gethash group gnus-newsrc-hashtb))))
3456     (if entry
3457         (gnus-dribble-enter 
3458          (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
3459                  ")")))
3460     (beginning-of-line)
3461     (delete-region (point) (progn (forward-line 1) (point)))
3462     (gnus-group-insert-group-line-info group)
3463     (forward-line -1)
3464     (gnus-group-position-cursor)))
3465
3466 (defun gnus-group-insert-group-line-info (group)
3467   (let ((entry (gnus-gethash group gnus-newsrc-hashtb)) 
3468         active info)
3469     (if entry
3470         (progn
3471           (setq info (nth 2 entry))
3472           (gnus-group-insert-group-line 
3473            nil group (nth 1 info) (nth 3 info) (car entry) (nth 4 info)))
3474       (setq active (gnus-gethash group gnus-active-hashtb))
3475       (gnus-group-insert-group-line 
3476        nil group 
3477        (if (member group gnus-zombie-list) gnus-level-zombie gnus-level-killed)
3478        nil (if active (- (1+ (cdr active)) (car active)) 0) nil))))
3479
3480 (defun gnus-group-insert-group-line (gformat group level marked number method)
3481   (let* ((gformat (or gformat gnus-group-line-format-spec))
3482          (active (gnus-gethash group gnus-active-hashtb))
3483          (number-total (if active (1+ (- (cdr active) (car active))) 0))
3484          (number-of-dormant (length (cdr (assq 'dormant marked))))
3485          (number-of-ticked (length (cdr (assq 'tick marked))))
3486          (number-of-ticked-and-dormant
3487           (+ number-of-ticked number-of-dormant))
3488          (number-of-unread-unticked 
3489           (if (numberp number) (int-to-string (max 0 number))
3490             "*"))
3491          (number-of-read
3492           (if (numberp number)
3493               (max 0 (- number-total number))
3494             "*"))
3495          (subscribed (cond ((<= level gnus-level-subscribed) ? )
3496                            ((<= level gnus-level-unsubscribed) ?U)
3497                            ((= level gnus-level-zombie) ?Z)
3498                            (t ?K)))
3499          (qualified-group (gnus-group-real-name group))
3500          (newsgroup-description 
3501           (if gnus-description-hashtb
3502               (or (gnus-gethash group gnus-description-hashtb) "")
3503             ""))
3504          (moderated (if (member group gnus-moderated-list) ?m ? ))
3505          (moderated-string (if (eq moderated ?m) "(m)" ""))
3506          (method (gnus-server-get-method group method))
3507          (news-server (or (car (cdr method)) ""))
3508          (news-method (or (car method) ""))
3509          (news-method-string 
3510           (if method (format "(%s:%s)" (car method) (car (cdr method))) ""))
3511          (marked (if (and 
3512                       (numberp number) 
3513                       (zerop number)
3514                       (> number-of-ticked 0))
3515                      ?* ? ))
3516          (number (if (eq number t) "*" (+ number number-of-dormant 
3517                                           number-of-ticked)))
3518          (process-marked (if (member qualified-group gnus-group-marked)
3519                              gnus-process-mark ? ))
3520          (buffer-read-only nil)
3521          header ; passed as parameter to user-funcs.
3522          b)
3523     (beginning-of-line)
3524     (setq b (point))
3525     ;; Insert the text.
3526     (insert (eval gformat))
3527
3528     (add-text-properties 
3529      b (1+ b) (list 'gnus-group (intern group)
3530                     'gnus-unread (if (numberp number)
3531                                      (string-to-int number-of-unread-unticked)
3532                                    t)
3533                     'gnus-marked marked
3534                     'gnus-level level))))
3535
3536 (defun gnus-group-update-group (group &optional visible-only)
3537   "Update newsgroup info of GROUP.
3538 If VISIBLE-ONLY is non-nil, the group won't be displayed if it isn't already."
3539   (save-excursion
3540     (set-buffer gnus-group-buffer)
3541     (let ((buffer-read-only nil)
3542           visible)
3543       (let ((entry (gnus-gethash group gnus-newsrc-hashtb)))
3544         (if entry
3545             (gnus-dribble-enter 
3546              (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
3547                      ")"))))
3548       ;; Buffer may be narrowed.
3549       (save-restriction
3550         (widen)
3551         ;; Search a line to modify.  If the buffer is large, the search
3552         ;; takes long time.  In most cases, current point is on the line
3553         ;; we are looking for.  So, first of all, check current line. 
3554         (if (or (progn
3555                   (beginning-of-line)
3556                   (eq (get-text-property (point) 'gnus-group)
3557                       (intern group)))
3558                 (progn
3559                   (gnus-goto-char 
3560                    (text-property-any 
3561                     (point-min) (point-max) 'gnus-group (intern group)))))
3562             ;; GROUP is listed in current buffer. So, delete old line.
3563             (progn
3564               (setq visible t)
3565               (beginning-of-line)
3566               (delete-region (point) (progn (forward-line 1) (point))))
3567           ;; No such line in the buffer, find out where it's supposed to
3568           ;; go, and insert it there (or at the end of the buffer).
3569           ;; Fix by Per Abrahamsen <amanda@iesd.auc.dk>.
3570           (or visible-only
3571               (let ((entry 
3572                      (cdr (cdr (gnus-gethash group gnus-newsrc-hashtb)))))
3573                 (while (and entry
3574                             (car entry)
3575                             (not
3576                              (gnus-goto-char
3577                               (text-property-any
3578                                (point-min) (point-max) 
3579                                'gnus-group (intern (car (car entry)))))))
3580                   (setq entry (cdr entry)))
3581                 (or entry (goto-char (point-max)))))))
3582       (if (or visible (not visible-only))
3583           (gnus-group-insert-group-line-info group))
3584       (gnus-group-set-mode-line))))
3585
3586 (defun gnus-group-set-mode-line ()
3587   (if (memq 'group gnus-updated-mode-lines)
3588       (let* ((gformat (or gnus-group-mode-line-format-spec
3589                           (setq gnus-group-mode-line-format-spec
3590                                 (gnus-parse-format 
3591                                  gnus-group-mode-line-format 
3592                                  gnus-group-mode-line-format-alist))))
3593              (news-server (car (cdr gnus-select-method)))
3594              (news-method (car gnus-select-method))
3595              (max-len 60)
3596              (mode-string (eval gformat)))
3597         (setq mode-string (eval gformat))
3598         (if (> (length mode-string) max-len) 
3599             (setq mode-string (substring mode-string 0 (- max-len 4))))
3600         (setq mode-line-buffer-identification mode-string)
3601         (set-buffer-modified-p t))))
3602
3603 (defun gnus-group-group-name ()
3604   "Get the name of the newsgroup on the current line."
3605   (let ((group (get-text-property (gnus-point-at-bol) 'gnus-group)))
3606     (and group (symbol-name group))))
3607
3608 (defun gnus-group-group-level ()
3609   "Get the level of the newsgroup on the current line."
3610   (get-text-property (gnus-point-at-bol) 'gnus-level))
3611
3612 (defun gnus-group-group-unread ()
3613   "Get the number of unread articles of the newsgroup on the current line."
3614   (get-text-property (gnus-point-at-bol) 'gnus-unread))
3615
3616 (defun gnus-group-search-forward (&optional backward all level first-too)
3617   "Find the next newsgroup with unread articles.
3618 If BACKWARD is non-nil, find the previous newsgroup instead.
3619 If ALL is non-nil, just find any newsgroup.
3620 If LEVEL is non-nil, find group with level LEVEL, or higher if no such
3621 group exists.
3622 If FIRST-TOO, the current line is also eligible as a target."
3623   (let ((way (if backward -1 1))
3624         (low gnus-level-killed)
3625         (beg (point))
3626         pos found lev)
3627     (if (and backward (progn (beginning-of-line)) (bobp))
3628         nil
3629       (or first-too (forward-line way))
3630       (while (and 
3631               (not (eobp))
3632               (not (setq 
3633                     found 
3634                     (and (or all
3635                              (and
3636                               (let ((unread 
3637                                      (get-text-property (point) 'gnus-unread)))
3638                                 (or (eq unread t) (and unread (> unread 0))))
3639                               (setq lev (get-text-property (point)
3640                                                            'gnus-level))
3641                               (<= lev gnus-level-subscribed)))
3642                          (or (not level)
3643                              (and (setq lev (get-text-property (point)
3644                                                                'gnus-level))
3645                                   (or (= lev level)
3646                                       (and (< lev low)
3647                                            (< level lev)
3648                                            (progn
3649                                              (setq low lev)
3650                                              (setq pos (point))
3651                                              nil))))))))
3652               (zerop (forward-line way)))))
3653     (if found 
3654         (progn (gnus-group-position-cursor) t)
3655       (goto-char (or pos beg))
3656       (and pos t))))
3657
3658 ;;; Gnus group mode commands
3659
3660 ;; Group marking.
3661
3662 (defun gnus-group-mark-group (n &optional unmark no-advance)
3663   "Mark the current group."
3664   (interactive "p")
3665   (let ((buffer-read-only nil)
3666         group)
3667     (while 
3668         (and (> n 0) 
3669              (setq group (gnus-group-group-name))
3670              (progn
3671                (beginning-of-line)
3672                (forward-char 2)
3673                (delete-char 1)
3674                (if unmark
3675                    (progn
3676                      (insert " ")
3677                      (setq gnus-group-marked (delete group gnus-group-marked)))
3678                  (insert "#")
3679                  (setq gnus-group-marked
3680                        (cons group (delete group gnus-group-marked))))
3681                t)
3682              (or no-advance (zerop (gnus-group-next-group 1))))
3683       (setq n (1- n)))
3684     (gnus-summary-position-cursor)
3685     n))
3686
3687 (defun gnus-group-unmark-group (n)
3688   "Remove the mark from the current group."
3689   (interactive "p")
3690   (gnus-group-mark-group n 'unmark))
3691
3692 (defun gnus-group-mark-region (unmark beg end)
3693   "Mark all groups between point and mark.
3694 If UNMARK, remove the mark instead."
3695   (interactive "P\nr")
3696   (let ((num (count-lines beg end)))
3697     (save-excursion
3698       (goto-char beg)
3699       (- num (gnus-group-mark-group num unmark)))))
3700
3701 (defun gnus-group-remove-mark (group)
3702   (and (gnus-group-goto-group group)
3703        (save-excursion
3704          (gnus-group-mark-group 1 'unmark t))))
3705
3706 ;; Return a list of groups to work on.  Take into consideration N (the
3707 ;; prefix) and the list of marked groups.
3708 (defun gnus-group-process-prefix (n)
3709   (cond (n
3710          (setq n (prefix-numeric-value n))
3711          ;; There is a prefix, so we return a list of the N next
3712          ;; groups. 
3713          (let ((way (if (< n 0) -1 1))
3714                (n (abs n))
3715                group groups)
3716            (save-excursion
3717              (while (and (> n 0)
3718                          (setq group (gnus-group-group-name)))
3719                (setq groups (cons group groups))
3720                (setq n (1- n))
3721                (forward-line way)))
3722            (nreverse groups)))
3723         (gnus-group-marked
3724          ;; No prefix, but a list of marked articles.
3725          (reverse gnus-group-marked))
3726         (t
3727          ;; Neither marked articles or a prefix, so we return the
3728          ;; current group.
3729          (let ((group (gnus-group-group-name)))
3730            (and group (list group))))))
3731
3732 ;; Selecting groups.
3733
3734 (defun gnus-group-read-group (all &optional no-article group)
3735   "Read news in this newsgroup.
3736 If argument ALL is non-nil, already read articles become readable.
3737 If optional argument NO-ARTICLE is non-nil, no article body is displayed."
3738   (interactive "P")
3739   (let ((group (or group (gnus-group-group-name)))
3740         number active marked entry)
3741     (or group (error "No group on current line"))
3742     (setq marked 
3743           (nth 3 (nth 2 (setq entry (gnus-gethash group gnus-newsrc-hashtb)))))
3744     ;; This group might be a dead group. In that case we have to get
3745     ;; the number of unread articles from `gnus-active-hashtb'.
3746     (if entry
3747         (setq number (car entry))
3748       (if (setq active (gnus-gethash group gnus-active-hashtb))
3749           (setq number (- (1+ (cdr active)) (car active)))))
3750     (gnus-summary-read-group 
3751      group (or all (and (numberp number) 
3752                         (zerop (+ number (length (cdr (assq 'tick marked)))
3753                                   (length (cdr (assq 'dormant marked)))))))
3754      no-article)))
3755
3756 (defun gnus-group-select-group (all)
3757   "Select this newsgroup.
3758 No article is selected automatically.
3759 If argument ALL is non-nil, already read articles become readable."
3760   (interactive "P")
3761   (gnus-group-read-group all t))
3762
3763 ;; Enter a group that is not in the group buffer. Non-nil is returned
3764 ;; if selection was successful.
3765 (defun gnus-group-read-ephemeral-group 
3766   (group method &optional activate quit-config)
3767   (let ((group (if (gnus-group-foreign-p group) group
3768                  (gnus-group-prefixed-name group method))))
3769     (gnus-sethash 
3770      group
3771      (list t nil (list group gnus-level-default-subscribed nil nil 
3772                        (append method
3773                                (list
3774                                 (list 'quit-config 
3775                                       (if quit-config quit-config
3776                                         (cons (current-buffer) 'summary)))))))
3777      gnus-newsrc-hashtb)
3778     (set-buffer gnus-group-buffer)
3779     (or (gnus-server-opened method)
3780         (gnus-open-server method)
3781         (error "Unable to contact server: %s" (gnus-status-message method)))
3782     (if activate (or (gnus-request-group group)
3783                      (error "Couldn't request group")))
3784     (condition-case ()
3785         (gnus-group-read-group t t group)
3786       (error nil)
3787       (quit nil))
3788     (not (equal major-mode 'gnus-group-mode))))
3789   
3790 (defun gnus-group-jump-to-group (group)
3791   "Jump to newsgroup GROUP."
3792   (interactive 
3793    (list (completing-read 
3794           "Group: " gnus-active-hashtb nil (not (not gnus-read-active-file)))))
3795
3796   (if (equal group "")
3797       (error "Empty group name"))
3798
3799   (let ((b (text-property-any 
3800             (point-min) (point-max) 'gnus-group (intern group))))
3801     (if b
3802         ;; Either go to the line in the group buffer...
3803         (goto-char b)
3804       ;; ... or insert the line.
3805       (or
3806        (gnus-gethash group gnus-active-hashtb)
3807        (gnus-activate-newsgroup group)
3808        (error "%s error: %s" group (gnus-status-message group)))
3809
3810       (gnus-group-update-group group)
3811       (goto-char (text-property-any 
3812                   (point-min) (point-max) 'gnus-group (intern group)))))
3813   ;; Adjust cursor point.
3814   (gnus-group-position-cursor))
3815
3816 (defun gnus-group-goto-group (group)
3817   "Goto to newsgroup GROUP."
3818   (let ((b (text-property-any (point-min) (point-max) 
3819                               'gnus-group (intern group))))
3820     (and b (goto-char b))))
3821
3822 (defun gnus-group-next-group (n)
3823   "Go to next N'th newsgroup.
3824 If N is negative, search backward instead.
3825 Returns the difference between N and the number of skips actually
3826 done."
3827   (interactive "p")
3828   (gnus-group-next-unread-group n t))
3829
3830 (defun gnus-group-next-unread-group (n &optional all level)
3831   "Go to next N'th unread newsgroup.
3832 If N is negative, search backward instead.
3833 If ALL is non-nil, choose any newsgroup, unread or not.
3834 If LEVEL is non-nil, choose the next group with level LEVEL, or, if no
3835 such group can be found, the next group with a level higher than
3836 LEVEL.
3837 Returns the difference between N and the number of skips actually
3838 made."
3839   (interactive "p")
3840   (let ((backward (< n 0))
3841         (n (abs n)))
3842     (while (and (> n 0)
3843                 (gnus-group-search-forward 
3844                  backward (or (not gnus-group-goto-unread) all) level))
3845       (setq n (1- n)))
3846     (if (/= 0 n) (gnus-message 7 "No more%s newsgroups%s" (if all "" " unread")
3847                                (if level " on this level or higher" "")))
3848     n))
3849
3850 (defun gnus-group-prev-group (n)
3851   "Go to previous N'th newsgroup.
3852 Returns the difference between N and the number of skips actually
3853 done."
3854   (interactive "p")
3855   (gnus-group-next-unread-group (- n) t))
3856
3857 (defun gnus-group-prev-unread-group (n)
3858   "Go to previous N'th unread newsgroup.
3859 Returns the difference between N and the number of skips actually
3860 done."  
3861   (interactive "p")
3862   (gnus-group-next-unread-group (- n)))
3863
3864 (defun gnus-group-next-unread-group-same-level (n)
3865   "Go to next N'th unread newsgroup on the same level.
3866 If N is negative, search backward instead.
3867 Returns the difference between N and the number of skips actually
3868 done."
3869   (interactive "p")
3870   (gnus-group-next-unread-group n t (gnus-group-group-level))
3871   (gnus-group-position-cursor))
3872
3873 (defun gnus-group-prev-unread-group-same-level (n)
3874   "Go to next N'th unread newsgroup on the same level.
3875 Returns the difference between N and the number of skips actually
3876 done."
3877   (interactive "p")
3878   (gnus-group-next-unread-group (- n) t (gnus-group-group-level))
3879   (gnus-group-position-cursor))
3880
3881 (defun gnus-group-best-unread-group (&optional exclude-group)
3882   "Go to the group with the highest level.
3883 If EXCLUDE-GROUP, do not go to that group."
3884   (interactive)
3885   (goto-char (point-min))
3886   (let ((best 100000)
3887         unread best-point)
3888     (while (setq unread (get-text-property (point) 'gnus-unread))
3889       (if (and (numberp unread) (> unread 0))
3890           (progn
3891             (if (and (< (get-text-property (point) 'gnus-level) best)
3892                      (or (not exclude-group)
3893                          (not (equal exclude-group (gnus-group-group-name)))))
3894                 (progn 
3895                   (setq best (get-text-property (point) 'gnus-level))
3896                   (setq best-point (point))))))
3897       (forward-line 1))
3898     (if best-point (goto-char best-point))
3899     (gnus-summary-position-cursor)
3900     (and best-point (gnus-group-group-name))))
3901
3902 (defun gnus-group-first-unread-group ()
3903   "Go to the first group with unread articles."
3904   (interactive)
3905   (goto-char (point-min))
3906   (or (not (zerop (or (get-text-property (point) 'gnus-unread) 0)))
3907       (gnus-group-next-unread-group 1))
3908   (gnus-group-position-cursor))
3909
3910 (defun gnus-group-enter-server-mode ()
3911   "Jump to the server buffer."
3912   (interactive)
3913   (gnus-server-setup-buffer)
3914   (gnus-configure-windows 'server)
3915   (gnus-server-prepare))
3916
3917 (defun gnus-group-make-group (name method &optional address)
3918   "Add a new newsgroup.
3919 The user will be prompted for a NAME, for a select METHOD, and an
3920 ADDRESS."
3921   (interactive
3922    (cons 
3923     (read-string "Group name: ")
3924     (let ((method
3925            (completing-read 
3926             "Method: " (append gnus-valid-select-methods gnus-server-alist)
3927             nil t)))
3928       (if (assoc method gnus-valid-select-methods)
3929           (list method
3930                 (if (memq 'prompt-address
3931                           (assoc method gnus-valid-select-methods))
3932                     (read-string "Address: ")
3933                   ""))
3934         (list method nil)))))
3935   
3936   (let* ((meth (if address (list (intern method) address) method))
3937          (nname (gnus-group-prefixed-name name meth))
3938          info)
3939     (and (gnus-gethash nname gnus-newsrc-hashtb)
3940          (error "Group %s already exists" nname))
3941     (gnus-group-change-level 
3942      (setq info (list t nname gnus-level-default-subscribed nil nil meth))
3943      gnus-level-default-subscribed gnus-level-killed 
3944      (and (gnus-group-group-name)
3945           (gnus-gethash (gnus-group-group-name)
3946                         gnus-newsrc-hashtb))
3947      t)
3948     (gnus-sethash nname (cons 1 0) gnus-active-hashtb)
3949     (gnus-dribble-enter 
3950      (concat "(gnus-group-set-info '" (prin1-to-string (cdr info)) ")"))
3951     (gnus-group-insert-group-line-info nname)
3952
3953     (if (assoc method gnus-valid-select-methods)
3954         (require (intern method)))
3955     (and (gnus-check-backend-function 'request-create-group nname)
3956          (gnus-request-create-group nname))))
3957
3958 (defun gnus-group-edit-group (group &optional part)
3959   "Edit the group on the current line."
3960   (interactive (list (gnus-group-group-name)))
3961   (let ((done-func '(lambda () 
3962                       "Exit editing mode and update the information."
3963                       (interactive)
3964                       (gnus-group-edit-group-done 'part 'group)))
3965         (part (or part 'info))
3966         (winconf (current-window-configuration))
3967         info)
3968     (or group (error "No group on current line"))
3969     (or (setq info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
3970         (error "Killed group; can't be edited"))
3971     (set-buffer (get-buffer-create gnus-group-edit-buffer))
3972     (gnus-configure-windows 'edit-group)
3973     (gnus-add-current-to-buffer-list)
3974     (emacs-lisp-mode)
3975     ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3976     (use-local-map (copy-keymap emacs-lisp-mode-map))
3977     (local-set-key "\C-c\C-c" done-func)
3978     (make-local-variable 'gnus-prev-winconf)
3979     (setq gnus-prev-winconf winconf)
3980     ;; We modify the func to let it know what part it is editing.
3981     (setcar (cdr (nth 4 done-func)) (list 'quote part))
3982     (setcar (cdr (cdr (nth 4 done-func))) group)
3983     (erase-buffer)
3984     (insert
3985      (cond 
3986       ((eq part 'method)
3987        ";; Type `C-c C-c' after editing the select method.\n\n")
3988       ((eq part 'params)
3989        ";; Type `C-c C-c' after editing the group parameters.\n\n")
3990       ((eq part 'info)
3991        ";; Type `C-c C-c' after editing the group info.\n\n")))
3992     (let ((cinfo (gnus-copy-sequence info))
3993           marked)
3994       (if (not (setq marked (nth 3 cinfo)))
3995           ()
3996         (while marked
3997           (or (eq 'score (car (car marked)))
3998               (eq 'bookmark (car (car marked)))
3999               (eq 'killed (car (car marked)))
4000               (not (numberp (car (cdr (car marked)))))
4001               (setcdr (car marked) 
4002                       (gnus-compress-sequence (sort (cdr (car marked)) '<) t)))
4003           (setq marked (cdr marked))))
4004       (insert 
4005        (pp-to-string
4006         (cond ((eq part 'method)
4007                (or (nth 4 info) "native"))
4008               ((eq part 'params)
4009                (nth 5 info))
4010               (t
4011                cinfo)))
4012        "\n"))))
4013
4014 (defun gnus-group-edit-group-method (group)
4015   "Edit the select method of GROUP."
4016   (interactive (list (gnus-group-group-name)))
4017   (gnus-group-edit-group group 'method))
4018
4019 (defun gnus-group-edit-group-parameters (group)
4020   "Edit the group parameters of GROUP."
4021   (interactive (list (gnus-group-group-name)))
4022   (gnus-group-edit-group group 'params))
4023
4024 (defun gnus-group-edit-group-done (part group)
4025   "Get info from buffer, update variables and jump to the group buffer."
4026   (set-buffer (get-buffer-create gnus-group-edit-buffer))
4027   (goto-char (point-min))
4028   (let ((form (read (current-buffer)))
4029         (winconf gnus-prev-winconf))
4030     (if (eq part 'info) 
4031         (gnus-group-set-info form)
4032       (gnus-group-set-info form group part))
4033     (kill-buffer (current-buffer))
4034     (and winconf (set-window-configuration winconf))
4035     (set-buffer gnus-group-buffer)
4036     (gnus-group-update-group (gnus-group-group-name))
4037     (gnus-group-position-cursor)))
4038
4039 (defun gnus-group-make-help-group ()
4040   "Create the (ding) Gnus documentation group."
4041   (interactive)
4042   (let ((path load-path)
4043         name)
4044     (and (gnus-gethash (setq name (gnus-group-prefixed-name
4045                                    "gnus-help" '(nndoc "gnus-help")))
4046                        gnus-newsrc-hashtb)
4047          (error "Documentation group already exists"))
4048     (while (and path
4049                 (not (file-exists-p (concat (file-name-as-directory (car path))
4050                                             "doc.txt"))))
4051       (setq path (cdr path)))
4052     (or path (error "Couldn't find doc group"))
4053     (gnus-group-make-group 
4054      (gnus-group-real-name name)
4055      (list 'nndoc name
4056            (list 'nndoc-address (concat (file-name-as-directory (car path)) "doc.txt"))
4057            (list 'nndoc-article-type 'mbox))))
4058   (gnus-group-position-cursor))
4059
4060 (defun gnus-group-make-doc-group (file type)
4061   "Create a group that uses a single file as the source."
4062   (interactive 
4063    (list (read-file-name "File name: ") 
4064          (let ((err "")
4065                found char)
4066            (while (not found)
4067              (message "%sFile type (mbox, babyl, digest) [mbd]: " err)
4068              (setq found (cond ((= (setq char (read-char)) ?m) 'mbox)
4069                                ((= char ?b) 'babyl)
4070                                ((= char ?d) 'digest)
4071                                (t (setq err "%c unknown. " char)
4072                                   nil))))
4073            found)))
4074   (let* ((file (expand-file-name file))
4075          (name (gnus-generate-new-group-name
4076                 (gnus-group-prefixed-name
4077                  (file-name-nondirectory file) '(nndoc "")))))
4078     (gnus-group-make-group 
4079      (gnus-group-real-name name)
4080      (list 'nndoc name
4081            (list 'nndoc-address file)
4082            (list 'nndoc-article-type type)))))
4083
4084 (defun gnus-group-make-archive-group ()
4085   "Create the (ding) Gnus archive group."
4086   (interactive)
4087   (and (gnus-gethash (gnus-group-prefixed-name "ding.archives" '(nndir ""))
4088                      gnus-newsrc-hashtb)
4089        (error "Archive group already exists"))
4090   (gnus-group-make-group "ding.archives" "nndir" gnus-group-archive-directory)
4091   (gnus-group-position-cursor))
4092
4093 (defun gnus-group-make-directory-group (dir)
4094   "Create an nndir group.
4095 The user will be prompted for a directory. The contents of this
4096 directory will be used as a newsgroup. The directory should contain
4097 mail messages or news articles in files that have numeric names."
4098   (interactive
4099    (list (read-file-name "Create group from directory: ")))
4100   (or (file-exists-p dir) (error "No such directory"))
4101   (or (file-directory-p dir) (error "Not a directory"))
4102   (gnus-group-make-group dir "nndir" dir)
4103   (gnus-group-position-cursor))
4104
4105 (defun gnus-group-make-kiboze-group (group address scores)
4106   "Create an nnkiboze group.
4107 The user will be prompted for a name, a regexp to match groups, and
4108 score file entries for articles to include in the group."
4109   (interactive
4110    (list
4111     (read-string "nnkiboze group name: ")
4112     (read-string "Source groups (regexp): ")
4113     (let ((headers (mapcar (lambda (group) (list group))
4114                            '("subject" "from" "number" "date" "message-id"
4115                              "references" "chars" "lines" "xref")))
4116           scores header regexp regexps)
4117       (while (not (equal "" (setq header (completing-read 
4118                                           "Match on header: " headers nil t))))
4119         (setq regexps nil)
4120         (while (not (equal "" (setq regexp (read-string 
4121                                             (format "Match on %s (string): "
4122                                                     header)))))
4123           (setq regexps (cons (list regexp nil nil 'r) regexps)))
4124         (setq scores (cons (cons header regexps) scores)))
4125       scores)))
4126   (gnus-group-make-group group "nnkiboze" address)
4127   (save-excursion
4128     (gnus-set-work-buffer)
4129     (let (emacs-lisp-mode-hook)
4130       (pp scores (current-buffer)))
4131     (write-region (point-min) (point-max) 
4132                   (concat (or gnus-kill-files-directory "~/News")
4133                           "nnkiboze:" group "." gnus-score-file-suffix)))
4134   (gnus-group-position-cursor))
4135
4136 (defun gnus-group-add-to-virtual (n vgroup)
4137   "Add the current group to a virtual group."
4138   (interactive
4139    (list current-prefix-arg
4140          (completing-read "Add to virtual group: " gnus-newsrc-hashtb nil t
4141                           "nnvirtual:")))
4142   (or (eq (car (gnus-find-method-for-group vgroup)) 'nnvirtual)
4143       (error "%s is not an nnvirtual group" vgroup))
4144   (let* ((groups (gnus-group-process-prefix n))
4145          (method (nth 4 (nth 2 (gnus-gethash vgroup gnus-newsrc-hashtb)))))
4146     (setcar (cdr method)
4147             (concat 
4148              (nth 1 method) "\\|"
4149              (mapconcat 
4150               (lambda (s) 
4151                 (gnus-group-remove-mark s)
4152                 (concat "\\(^" (regexp-quote s) "$\\)"))
4153               groups "\\|"))))
4154   (gnus-group-position-cursor))
4155
4156 (defun gnus-group-make-empty-virtual (group)
4157   "Create a new, fresh, empty virtual group."
4158   (interactive "sCreate new, empty virtual group: ")
4159   (let* ((method (list 'nnvirtual "^$"))
4160          (pgroup (gnus-group-prefixed-name group method)))
4161     ;; Check whether it exists already.
4162     (and (gnus-gethash pgroup gnus-newsrc-hashtb)
4163          (error "Group %s already exists." pgroup))
4164     ;; Subscribe the new group after the group on the current line.
4165     (gnus-subscribe-group pgroup (gnus-group-group-name) method)
4166     (gnus-group-update-group pgroup)
4167     (forward-line -1)
4168     (gnus-group-position-cursor)))
4169
4170 (defun gnus-group-enter-directory (dir)
4171   "Enter an ephemeral nneething group."
4172   (interactive "DDirectory to read: ")
4173   (let* ((method (list 'nneething dir))
4174          (leaf (gnus-group-prefixed-name
4175                 (file-name-nondirectory (directory-file-name dir))
4176                 method))
4177          (name (gnus-generate-new-group-name leaf)))
4178     (let ((nneething-read-only t))
4179       (or (gnus-group-read-ephemeral-group 
4180            name method t
4181            (cons (current-buffer) (if (eq major-mode 'gnus-summary-mode)
4182                                       'summary 'group)))
4183           (error "Couldn't enter %s" dir)))))
4184
4185 ;; Group sorting commands
4186 ;; Suggested by Joe Hildebrand <hildjj@idaho.fuentez.com>.
4187
4188 (defun gnus-group-sort-groups ()
4189   "Sort the group buffer using `gnus-group-sort-function'."
4190   (interactive)
4191   (setq gnus-newsrc-alist 
4192         (sort (cdr gnus-newsrc-alist) gnus-group-sort-function))
4193   (gnus-make-hashtable-from-newsrc-alist)
4194   (gnus-group-list-groups (if gnus-have-all-newsgroups gnus-level-unsubscribed)
4195                           gnus-have-all-newsgroups))
4196
4197 (defun gnus-group-sort-by-alphabet (info1 info2)
4198   (string< (car info1) (car info2)))
4199
4200 (defun gnus-group-sort-by-unread (info1 info2)
4201   (let ((n1 (car (gnus-gethash (car info1) gnus-newsrc-hashtb)))
4202         (n2 (car (gnus-gethash (car info2) gnus-newsrc-hashtb))))
4203     (< (or (and (numberp n1) n1) 0)
4204        (or (and (numberp n2) n2) 0))))
4205
4206 (defun gnus-group-sort-by-level (info1 info2)
4207   (< (nth 1 info1) (nth 1 info2)))
4208
4209 ;; Group catching up.
4210
4211 (defun gnus-group-catchup-current (n &optional all)
4212   "Mark all articles not marked as unread in current newsgroup as read.
4213 If prefix argument N is numeric, the ARG next newsgroups will be
4214 caught up. If ALL is non-nil, marked articles will also be marked as
4215 read. Cross references (Xref: header) of articles are ignored.
4216 The difference between N and actual number of newsgroups that were
4217 caught up is returned."
4218   (interactive "P")
4219   (if (not (or (not gnus-interactive-catchup) ;Without confirmation?
4220                gnus-expert-user
4221                (gnus-y-or-n-p
4222                 (if all
4223                     "Do you really want to mark all articles as read? "
4224                   "Mark all unread articles as read? "))))
4225       n
4226     (let ((groups (gnus-group-process-prefix n))
4227           (ret 0))
4228       (while groups
4229         ;; Virtual groups have to be given special treatment. 
4230         (let ((method (gnus-find-method-for-group (car groups))))
4231           (if (eq 'nnvirtual (car method))
4232               (nnvirtual-catchup-group
4233                (gnus-group-real-name (car groups)) (nth 1 method) all)))
4234         (gnus-group-remove-mark (car groups))
4235         (if (prog1
4236                 (gnus-group-goto-group (car groups))
4237               (gnus-group-catchup (car groups) all))
4238             (gnus-group-update-group-line)
4239           (setq ret (1+ ret)))
4240         (setq groups (cdr groups)))
4241       (gnus-group-next-unread-group 1)
4242       ret)))
4243
4244 (defun gnus-group-catchup-current-all (n)
4245   "Mark all articles in current newsgroup as read.
4246 Cross references (Xref: header) of articles are ignored."
4247   (interactive "P")
4248   (gnus-group-catchup-current n 'all))
4249
4250 (defun gnus-group-catchup (group &optional all)
4251   "Mark all articles in GROUP as read.
4252 If ALL is non-nil, all articles are marked as read.
4253 The return value is the number of articles that were marked as read,
4254 or nil if no action could be taken."
4255   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4256          (num (car entry))
4257          (marked (nth 3 (nth 2 entry))))
4258     (if (not (numberp (car entry)))
4259         (gnus-message 1 "Can't catch up; non-active group")
4260       ;; Do the updating only if the newsgroup isn't killed.
4261       (if (not entry)
4262           ()
4263         (gnus-update-read-articles 
4264          group (and (not all) (append (cdr (assq 'tick marked))
4265                                       (cdr (assq 'dormant marked))))
4266          nil (and (not all) (cdr (assq 'tick marked))))
4267         (and all marked
4268              (setcar (nthcdr 3 (nth 2 entry)) 
4269                      (delq (assq 'dormant marked) 
4270                            (nth 3 (nth 2 entry)))))))
4271     num))
4272
4273 (defun gnus-group-expire-articles (n)
4274   "Expire all expirable articles in the current newsgroup."
4275   (interactive "P")
4276   (let ((groups (gnus-group-process-prefix n))
4277         group)
4278     (or groups (error "No groups to expire"))
4279     (while groups
4280       (setq group (car groups)
4281             groups (cdr groups))
4282       (gnus-group-remove-mark group)
4283       (if (not (gnus-check-backend-function 'request-expire-articles group))
4284           ()
4285         (let* ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
4286                (expirable (if (memq 'total-expire (nth 5 info))
4287                               (cons nil (gnus-list-of-read-articles group))
4288                             (assq 'expire (nth 3 info)))))
4289           (and expirable 
4290                (setcdr expirable
4291                        (gnus-request-expire-articles 
4292                         (cdr expirable) group))))))))
4293
4294 (defun gnus-group-expire-all-groups ()
4295   "Expire all expirable articles in all newsgroups."
4296   (interactive)
4297   (save-excursion
4298     (gnus-message 5 "Expiring...")
4299     (let ((gnus-group-marked (mapcar (lambda (info) (car info))
4300                                      (cdr gnus-newsrc-alist))))
4301       (gnus-group-expire-articles nil)))
4302   (gnus-group-position-cursor)
4303   (gnus-message 5 "Expiring...done"))
4304
4305 (defun gnus-group-set-current-level (n level)
4306   "Set the level of the next N groups to LEVEL."
4307   (interactive "P\nnLevel: ")
4308   (or (and (>= level 1) (<= level gnus-level-killed))
4309       (error "Illegal level: %d" level))
4310   (let ((groups (gnus-group-process-prefix n))
4311         group)
4312     (while groups
4313       (setq group (car groups)
4314             groups (cdr groups))
4315       (gnus-group-remove-mark group)
4316       (gnus-message 6 "Changed level of %s from %d to %d" 
4317                     group (gnus-group-group-level) level)
4318       (gnus-group-change-level group level
4319                                (gnus-group-group-level))
4320       (gnus-group-update-group-line)))
4321   (gnus-group-position-cursor))
4322
4323 (defun gnus-group-unsubscribe-current-group (n)
4324   "Toggle subscription of the current group.
4325 If given numerical prefix, toggle the N next groups."
4326   (interactive "P")
4327   (let ((groups (gnus-group-process-prefix n))
4328         group)
4329     (while groups
4330       (setq group (car groups)
4331             groups (cdr groups))
4332       (gnus-group-remove-mark group)
4333       (gnus-group-unsubscribe-group
4334        group (if (<= (gnus-group-group-level) gnus-level-subscribed)
4335                  gnus-level-default-unsubscribed
4336                gnus-level-default-subscribed))
4337       (gnus-group-update-group-line))
4338     (gnus-group-next-group 1)))
4339
4340 (defun gnus-group-unsubscribe-group (group &optional level)
4341   "Toggle subscribe from/to unsubscribe GROUP.
4342 New newsgroup is added to .newsrc automatically."
4343   (interactive
4344    (list (completing-read "Group: " gnus-active-hashtb nil 
4345                           gnus-have-read-active-file)))
4346   (let ((newsrc (gnus-gethash group gnus-newsrc-hashtb)))
4347     (cond (newsrc
4348            ;; Toggle subscription flag.
4349            (gnus-group-change-level 
4350             newsrc (if level level (if (<= (nth 1 (nth 2 newsrc)) 
4351                                            gnus-level-subscribed) 
4352                                        (1+ gnus-level-subscribed)
4353                                      gnus-level-default-subscribed)))
4354            (gnus-group-update-group group))
4355           ((and (stringp group)
4356                 (or (not gnus-have-read-active-file)
4357                     (gnus-gethash group gnus-active-hashtb)))
4358            ;; Add new newsgroup.
4359            (gnus-group-change-level 
4360             group 
4361             (if level level gnus-level-default-subscribed) 
4362             (or (and (member group gnus-zombie-list) 
4363                      gnus-level-zombie) 
4364                 gnus-level-killed)
4365             (and (gnus-group-group-name)
4366                  (gnus-gethash (gnus-group-group-name) gnus-newsrc-hashtb)))
4367            (gnus-group-update-group group))
4368           (t (error "No such newsgroup: %s" group)))
4369     (gnus-group-position-cursor)))
4370
4371 (defun gnus-group-transpose-groups (n)
4372   "Move the current newsgroup up N places.
4373 If given a negative prefix, move down instead. The difference between
4374 N and the number of steps taken is returned." 
4375   (interactive "p")
4376   (or (gnus-group-group-name)
4377       (error "No group on current line"))
4378   (gnus-group-kill-group 1)
4379   (prog1
4380       (forward-line (- n))
4381     (gnus-group-yank-group)
4382     (gnus-group-position-cursor)))
4383
4384 (defun gnus-group-kill-all-zombies ()
4385   "Kill all zombie newsgroups."
4386   (interactive)
4387   (setq gnus-killed-list (nconc gnus-zombie-list gnus-killed-list))
4388   (setq gnus-zombie-list nil)
4389   (funcall gnus-group-prepare-function gnus-level-subscribed nil nil)
4390   (goto-char (point-min))
4391   (gnus-group-position-cursor))
4392
4393 (defun gnus-group-kill-region (begin end)
4394   "Kill newsgroups in current region (excluding current point).
4395 The killed newsgroups can be yanked by using \\[gnus-group-yank-group]."
4396   (interactive "r")
4397   (let ((lines
4398          ;; Count lines.
4399          (save-excursion
4400            (count-lines
4401             (progn
4402               (goto-char begin)
4403               (beginning-of-line)
4404               (point))
4405             (progn
4406               (goto-char end)
4407               (beginning-of-line)
4408               (point))))))
4409     (goto-char begin)
4410     (beginning-of-line)                 ;Important when LINES < 1
4411     (gnus-group-kill-group lines)))
4412
4413 (defun gnus-group-kill-group (n)
4414   "The the next N groups.
4415 The killed newsgroups can be yanked by using \\[gnus-group-yank-group].
4416 However, only groups that were alive can be yanked; already killed 
4417 groups or zombie groups can't be yanked.
4418 The return value is the name of the (last) group that was killed."
4419   (interactive "P")
4420   (let ((buffer-read-only nil)
4421         (groups (gnus-group-process-prefix n))
4422         group entry level)
4423     (while groups
4424       (setq group (car groups)
4425             groups (cdr groups))
4426       (gnus-group-remove-mark group)
4427       (setq level (gnus-group-group-level))
4428       (gnus-delete-line)
4429       (if (setq entry (gnus-gethash group gnus-newsrc-hashtb))
4430           (setq gnus-list-of-killed-groups 
4431                 (cons (cons (car entry) (nth 2 entry)) 
4432                       gnus-list-of-killed-groups)))
4433       (gnus-group-change-level 
4434        (if entry entry group) gnus-level-killed (if entry nil level)))
4435     (gnus-group-position-cursor)
4436     group))
4437
4438 (defun gnus-group-yank-group (&optional arg)
4439   "Yank the last newsgroups killed with \\[gnus-group-kill-group],
4440 inserting it before the current newsgroup.  The numeric ARG specifies
4441 how many newsgroups are to be yanked.  The name of the (last)
4442 newsgroup yanked is returned."
4443   (interactive "p")
4444   (if (not arg) (setq arg 1))
4445   (let (info group prev)
4446     (while (>= (setq arg (1- arg)) 0)
4447       (if (not (setq info (car gnus-list-of-killed-groups)))
4448           (error "No more newsgroups to yank"))
4449       (setq group (nth 2 info))
4450       ;; Find which newsgroup to insert this one before - search
4451       ;; backward until something suitable is found. If there are no
4452       ;; other newsgroups in this buffer, just make this newsgroup the
4453       ;; first newsgroup.
4454       (setq prev (gnus-group-group-name))
4455       (gnus-group-change-level 
4456        info (nth 2 info) gnus-level-killed 
4457        (and prev (gnus-gethash prev gnus-newsrc-hashtb))
4458        t)
4459       (gnus-group-insert-group-line-info (nth 1 info))
4460       (setq gnus-list-of-killed-groups 
4461             (cdr gnus-list-of-killed-groups)))
4462     (forward-line -1)
4463     (gnus-group-position-cursor)
4464     group))
4465       
4466 (defun gnus-group-list-all-groups (arg)
4467   "List all newsgroups with level ARG or lower.
4468 Default is gnus-level-unsubscribed, which lists all subscribed and most
4469 unsubscribed groups."
4470   (interactive "P")
4471   (setq arg (or arg gnus-level-unsubscribed))
4472   (gnus-group-list-groups arg t))
4473
4474 (defun gnus-group-list-killed ()
4475   "List all killed newsgroups in the group buffer."
4476   (interactive)
4477   (if (not gnus-killed-list)
4478       (gnus-message 6 "No killed groups")
4479     (funcall gnus-group-prepare-function gnus-level-killed t gnus-level-killed)
4480     (goto-char (point-min)))
4481   (gnus-group-position-cursor))
4482
4483 (defun gnus-group-list-zombies ()
4484   "List all zombie newsgroups in the group buffer."
4485   (interactive)
4486   (if (not gnus-zombie-list)
4487       (gnus-message 6 "No zombie groups")
4488     (funcall gnus-group-prepare-function gnus-level-zombie t gnus-level-zombie)
4489     (goto-char (point-min)))
4490   (gnus-group-position-cursor))
4491
4492 (defun gnus-group-get-new-news (&optional arg)
4493   "Get newly arrived articles.
4494 If ARG is non-nil, it should be a number between one and nine to
4495 specify which levels you are interested in re-scanning."
4496   (interactive "P")
4497   (run-hooks 'gnus-get-new-news-hook)
4498   (if gnus-group-use-permanent-levels
4499       (setq arg
4500             (setq gnus-group-default-list-level 
4501                   (or arg gnus-group-default-list-level
4502                       gnus-level-subscribed))))
4503   (if (and gnus-read-active-file (not arg))
4504       (progn
4505         (gnus-read-active-file)
4506         (gnus-get-unread-articles (or arg (1+ gnus-level-subscribed))))
4507     (let ((gnus-read-active-file nil)
4508           (gnus-have-read-active-file (not arg)))
4509       (gnus-get-unread-articles (or arg (1+ gnus-level-subscribed)))))
4510   (gnus-group-list-groups (or (and gnus-group-use-permanent-levels arg)
4511                               gnus-group-default-list-level
4512                               gnus-level-subscribed)
4513                           gnus-have-all-newsgroups))
4514
4515 (defun gnus-group-get-new-news-this-group (n)
4516   "Check for newly arrived news in the current group (and the N-1 next groups).
4517 The difference between N and the number of newsgroup checked is returned.
4518 If N is negative, this group and the N-1 previous groups will be checked."
4519   (interactive "P")
4520   (let* ((groups (gnus-group-process-prefix n))
4521          (ret (if (numberp n) (- n (length groups)) 0))
4522          group)
4523     (while groups
4524       (setq group (car groups)
4525             groups (cdr groups))
4526       (gnus-group-remove-mark group)
4527       (or (gnus-get-new-news-in-group group)
4528           (progn 
4529             (ding) 
4530             (message "%s error: %s" group (gnus-status-message group))
4531             (sit-for 2))))
4532     ;; !!! I don't know why the buffer scrolls forward when updating
4533     ;; the first line in the group buffer, but it does. So we set the
4534     ;; window start forcibly.
4535 ;    (set-window-start (get-buffer-window (current-buffer)) w-p)
4536     (gnus-group-next-unread-group 1 t)
4537     (gnus-summary-position-cursor)
4538     ret))
4539
4540 (defun gnus-get-new-news-in-group (group)
4541   (and group 
4542        (gnus-activate-newsgroup group)
4543        (progn
4544          (gnus-get-unread-articles-in-group 
4545           (nth 2 (gnus-gethash group gnus-newsrc-hashtb))
4546           (gnus-gethash group gnus-active-hashtb))
4547          (gnus-group-update-group-line)
4548          t)))
4549
4550 (defun gnus-group-fetch-faq (group)
4551   "Fetch the FAQ for the current group."
4552   (interactive (list (gnus-group-real-name (gnus-group-group-name))))
4553   (or group (error "No group name given"))
4554   (let ((file (concat gnus-group-faq-directory group))) 
4555     (if (not (file-exists-p file))
4556         (error "No such file: %s" file)
4557       (find-file file))))
4558   
4559 (defun gnus-group-describe-group (force &optional group)
4560   "Display a description of the current newsgroup."
4561   (interactive (list current-prefix-arg (gnus-group-group-name)))
4562   (and force (setq gnus-description-hashtb nil))
4563   (let ((method (gnus-find-method-for-group group))
4564         desc)
4565     (or group (error "No group name given"))
4566     (and (or (and gnus-description-hashtb
4567                   ;; We check whether this group's method has been
4568                   ;; queried for a description file.  
4569                   (gnus-gethash 
4570                    (gnus-group-prefixed-name "" method) 
4571                    gnus-description-hashtb))
4572              (setq desc (gnus-group-get-description group))
4573              (gnus-read-descriptions-file method))
4574          (message
4575           (or desc (gnus-gethash group gnus-description-hashtb)
4576               "No description available")))))
4577
4578 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
4579 (defun gnus-group-describe-all-groups (force)
4580   "Pop up a buffer with descriptions of all newsgroups."
4581   (interactive "P")
4582   (and force (setq gnus-description-hashtb nil))
4583   (if (not (or gnus-description-hashtb
4584                (gnus-read-all-descriptions-files)))
4585       (error "Couldn't request descriptions file"))
4586   (let ((buffer-read-only nil)
4587         b)
4588     (erase-buffer)
4589     (mapatoms
4590      (lambda (group)
4591        (setq b (point))
4592        (insert (format "      *: %-20s %s\n" (symbol-name group)
4593                        (symbol-value group)))
4594        (add-text-properties 
4595         b (1+ b) (list 'gnus-group group
4596                        'gnus-unread t 'gnus-marked nil
4597                        'gnus-level (1+ gnus-level-subscribed))))
4598      gnus-description-hashtb)
4599     (goto-char (point-min))
4600     (gnus-group-position-cursor)))
4601
4602 ;; Suggested by by Daniel Quinlan <quinlan@best.com>.
4603 (defun gnus-group-apropos (regexp &optional search-description)
4604   "List all newsgroups that have names that match a regexp."
4605   (interactive "sGnus apropos (regexp): ")
4606   (let ((prev "")
4607         (obuf (current-buffer))
4608         groups des)
4609     ;; Go through all newsgroups that are known to Gnus.
4610     (mapatoms 
4611      (lambda (group)
4612        (and (string-match regexp (symbol-name group))
4613             (setq groups (cons (symbol-name group) groups))))
4614      gnus-active-hashtb)
4615     ;; Go through all descriptions that are known to Gnus. 
4616     (if search-description
4617         (mapatoms 
4618          (lambda (group)
4619            (and (string-match regexp (symbol-value group))
4620                 (gnus-gethash (symbol-name group) gnus-active-hashtb)
4621                 (setq groups (cons (symbol-name group) groups))))
4622          gnus-description-hashtb))
4623     (if (not groups)
4624         (gnus-message 3 "No groups matched \"%s\"." regexp)
4625       ;; Print out all the groups.
4626       (save-excursion
4627         (pop-to-buffer "*Gnus Help*")
4628         (buffer-disable-undo (current-buffer))
4629         (erase-buffer)
4630         (setq groups (sort groups 'string<))
4631         (while groups
4632           ;; Groups may be entered twice into the list of groups.
4633           (if (not (string= (car groups) prev))
4634               (progn
4635                 (insert (setq prev (car groups)) "\n")
4636                 (if (and gnus-description-hashtb
4637                          (setq des (gnus-gethash (car groups) 
4638                                                  gnus-description-hashtb)))
4639                     (insert "  " des "\n"))))
4640           (setq groups (cdr groups)))
4641         (goto-char (point-min))))
4642     (pop-to-buffer obuf)))
4643
4644 (defun gnus-group-description-apropos (regexp)
4645   "List all newsgroups that have names or descriptions that match a regexp."
4646   (interactive "sGnus description apropos (regexp): ")
4647   (if (not (or gnus-description-hashtb
4648                (gnus-read-all-descriptions-files)))
4649       (error "Couldn't request descriptions file"))
4650   (gnus-group-apropos regexp t))
4651
4652 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
4653 (defun gnus-group-list-matching (level regexp &optional all lowest) 
4654   "List all groups with unread articles that match REGEXP.
4655 If the prefix LEVEL is non-nil, it should be a number that says which
4656 level to cut off listing groups. 
4657 If ALL, also list groups with no unread articles.
4658 If LOWEST, don't list groups with level lower than LOWEST."
4659   (interactive "P\nsList newsgroups matching: ")
4660   (gnus-group-prepare-flat (or level gnus-level-subscribed)
4661                            all (or lowest 1) regexp)
4662   (goto-char (point-min))
4663   (gnus-group-position-cursor))
4664
4665 (defun gnus-group-list-all-matching (level regexp &optional lowest) 
4666   "List all groups 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 LOWEST, don't list groups with level lower than LOWEST."
4670   (interactive "P\nsList newsgroups matching: ")
4671   (gnus-group-list-matching (or level gnus-level-killed) regexp t lowest))
4672
4673 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
4674 (defun gnus-group-save-newsrc ()
4675   "Save the Gnus startup files."
4676   (interactive)
4677   (gnus-save-newsrc-file))
4678
4679 (defun gnus-group-restart (&optional arg)
4680   "Force Gnus to read the .newsrc file."
4681   (interactive "P")
4682   (gnus-save-newsrc-file)
4683   (gnus-setup-news 'force)
4684   (gnus-group-list-groups arg gnus-have-all-newsgroups))
4685
4686 (defun gnus-group-read-init-file ()
4687   "Read the Gnus elisp init file."
4688   (interactive)
4689   (gnus-read-init-file))
4690
4691 (defun gnus-group-check-bogus-groups (silent)
4692   "Check bogus newsgroups.
4693 If given a prefix, don't ask for confirmation before removing a bogus
4694 group."
4695   (interactive "P")
4696   (gnus-check-bogus-newsgroups (and (not silent) (not gnus-expert-user)))
4697   (gnus-group-list-groups nil gnus-have-all-newsgroups))
4698
4699 (defun gnus-group-edit-global-kill (article &optional group)
4700   "Edit the global kill file.
4701 If GROUP, edit that local kill file instead."
4702   (interactive "P")
4703   (setq gnus-current-kill-article article)
4704   (gnus-kill-file-edit-file group)
4705   (gnus-message 6
4706    (substitute-command-keys
4707     "Editing a global kill file (Type \\[gnus-kill-file-exit] to exit)")))
4708
4709 (defun gnus-group-edit-local-kill (article group)
4710   "Edit a local kill file."
4711   (interactive (list nil (gnus-group-group-name)))
4712   (gnus-group-edit-global-kill article group))
4713
4714 (defun gnus-group-force-update ()
4715   "Update `.newsrc' file."
4716   (interactive)
4717   (gnus-save-newsrc-file))
4718
4719 (defun gnus-group-suspend ()
4720   "Suspend the current Gnus session.
4721 In fact, cleanup buffers except for group mode buffer.
4722 The hook gnus-suspend-gnus-hook is called before actually suspending."
4723   (interactive)
4724   (run-hooks 'gnus-suspend-gnus-hook)
4725   ;; Kill Gnus buffers except for group mode buffer.
4726   (let ((group-buf (get-buffer gnus-group-buffer)))
4727     ;; Do this on a separate list in case the user does a ^G before we finish
4728     (let ((gnus-buffer-list
4729            (delq group-buf (delq gnus-dribble-buffer
4730                                  (append gnus-buffer-list nil)))))
4731       (while gnus-buffer-list
4732         (gnus-kill-buffer (car gnus-buffer-list))
4733         (setq gnus-buffer-list (cdr gnus-buffer-list))))
4734     (if group-buf
4735         (progn
4736           (setq gnus-buffer-list (list group-buf))
4737           (bury-buffer group-buf)
4738           (delete-windows-on group-buf t)))))
4739
4740 (defun gnus-group-clear-dribble ()
4741   "Clear all information from the dribble buffer."
4742   (interactive)
4743   (gnus-dribble-clear))
4744
4745 (defun gnus-group-exit ()
4746   "Quit reading news after updating .newsrc.eld and .newsrc.
4747 The hook `gnus-exit-gnus-hook' is called before actually exiting."
4748   (interactive)
4749   (if (or noninteractive                ;For gnus-batch-kill
4750           (zerop (buffer-size))         ;No news is good news.
4751           (not (gnus-server-opened gnus-select-method)) ;NNTP connection closed
4752           (not gnus-interactive-exit)   ;Without confirmation
4753           gnus-expert-user
4754           (gnus-y-or-n-p "Are you sure you want to quit reading news? "))
4755       (progn
4756         (if gnus-use-full-window
4757             (delete-other-windows)
4758           (gnus-remove-some-windows))
4759         (run-hooks 'gnus-exit-gnus-hook)
4760         (gnus-offer-save-summaries)
4761         (gnus-save-newsrc-file)
4762         (gnus-close-backends)
4763         (gnus-clear-system))))
4764
4765 (defun gnus-close-backends ()
4766   ;; Send a close request to all backends that support such a request. 
4767   (let ((methods gnus-valid-select-methods)
4768         func)
4769     (while methods
4770       (if (fboundp (setq func (intern (concat (car (car methods))
4771                                               "-request-close"))))
4772           (funcall func))
4773       (setq methods (cdr methods)))))
4774
4775 (defun gnus-group-quit ()
4776   "Quit reading news without updating .newsrc.eld or .newsrc.
4777 The hook `gnus-exit-gnus-hook' is called before actually exiting."
4778   (interactive)
4779   (if (or noninteractive                ;For gnus-batch-kill
4780           (zerop (buffer-size))
4781           (not (gnus-server-opened gnus-select-method))
4782           gnus-expert-user
4783           (not gnus-current-startup-file)
4784           (gnus-yes-or-no-p
4785            (format "Quit reading news without saving %s? "
4786                    (file-name-nondirectory gnus-current-startup-file))))
4787       (progn
4788         (run-hooks 'gnus-exit-gnus-hook)
4789         (if gnus-use-full-window
4790             (delete-other-windows)
4791           (gnus-remove-some-windows))
4792         (gnus-dribble-save)
4793         (gnus-close-backends)
4794         (gnus-clear-system))))
4795
4796 (defun gnus-offer-save-summaries ()
4797   (let ((buffers (buffer-list)))
4798     (save-excursion
4799       (while buffers
4800         (and 
4801          ;; We look for buffers with "Summary" in the name.
4802          (string-match "Summary" (or (buffer-name (car buffers)) ""))
4803          (progn
4804            (set-buffer (car buffers))
4805            ;; We check that this is, indeed, a summary buffer.
4806            (eq major-mode 'gnus-summary-mode)) 
4807          ;; We ask the user whether she wants to save the info.
4808          (gnus-y-or-n-p
4809                (format "Update summary buffer %s? " (buffer-name)))
4810          ;; We do it by simply exiting.
4811          (gnus-summary-exit))
4812         (setq buffers (cdr buffers))))))
4813
4814 (defun gnus-group-describe-briefly ()
4815   "Give a one line description of the group mode commands."
4816   (interactive)
4817   (gnus-message 6
4818    (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")))
4819
4820 (defun gnus-group-browse-foreign-server (method)
4821   "Browse a foreign news server.
4822 If called interactively, this function will ask for a select method
4823  (nntp, nnspool, etc.) and a server address (eg. nntp.some.where). 
4824 If not, METHOD should be a list where the first element is the method
4825 and the second element is the address."
4826   (interactive
4827    (list (let ((how (completing-read 
4828                      "Which backend: "
4829                      (append gnus-valid-select-methods gnus-server-alist)
4830                      nil t "nntp")))
4831            ;; We either got a backend name or a virtual server name.
4832            ;; If the first, we also need an address.
4833            (if (assoc how gnus-valid-select-methods)
4834                (list (intern how)
4835                      ;; Suggested by mapjph@bath.ac.uk.
4836                      (completing-read 
4837                       "Address: " 
4838                       (mapcar (lambda (server) (list server))
4839                               gnus-secondary-servers)))
4840              ;; We got a server name, so we find the method.
4841              (gnus-server-to-method how)))))
4842   (gnus-browse-foreign-server method))
4843
4844 \f
4845 ;;;
4846 ;;; Browse Server Mode
4847 ;;;
4848
4849 (defvar gnus-browse-mode-hook nil)
4850 (defvar gnus-browse-mode-map nil)
4851 (put 'gnus-browse-mode 'mode-class 'special)
4852
4853 (if gnus-browse-mode-map
4854     nil
4855   (setq gnus-browse-mode-map (make-keymap))
4856   (suppress-keymap gnus-browse-mode-map)
4857   (define-key gnus-browse-mode-map " " 'gnus-browse-read-group)
4858   (define-key gnus-browse-mode-map "=" 'gnus-browse-select-group)
4859   (define-key gnus-browse-mode-map "n" 'gnus-browse-next-group)
4860   (define-key gnus-browse-mode-map "p" 'gnus-browse-prev-group)
4861   (define-key gnus-browse-mode-map "\177" 'gnus-browse-prev-group)
4862   (define-key gnus-browse-mode-map "N" 'gnus-browse-next-group)
4863   (define-key gnus-browse-mode-map "P" 'gnus-browse-prev-group)
4864   (define-key gnus-browse-mode-map "\M-n" 'gnus-browse-next-group)
4865   (define-key gnus-browse-mode-map "\M-p" 'gnus-browse-prev-group)
4866   (define-key gnus-browse-mode-map "\r" 'gnus-browse-select-group)
4867   (define-key gnus-browse-mode-map "u" 'gnus-browse-unsubscribe-current-group)
4868   (define-key gnus-browse-mode-map "l" 'gnus-browse-exit)
4869   (define-key gnus-browse-mode-map "L" 'gnus-browse-exit)
4870   (define-key gnus-browse-mode-map "q" 'gnus-browse-exit)
4871   (define-key gnus-browse-mode-map "Q" 'gnus-browse-exit)
4872   (define-key gnus-browse-mode-map "\C-c\C-c" 'gnus-browse-exit)
4873   (define-key gnus-browse-mode-map "?" 'gnus-browse-describe-briefly)
4874   (define-key gnus-browse-mode-map "\C-c\C-i" 'gnus-info-find-node)
4875   )
4876
4877 (defvar gnus-browse-current-method nil)
4878 (defvar gnus-browse-return-buffer nil)
4879
4880 (defvar gnus-browse-buffer "*Gnus Browse Server*")
4881
4882 (defun gnus-browse-foreign-server (method &optional return-buffer)
4883   (setq gnus-browse-current-method method)
4884   (setq gnus-browse-return-buffer return-buffer)
4885   (let ((gnus-select-method method)
4886         groups group)
4887     (gnus-message 5 "Connecting to %s..." (nth 1 method))
4888     (or (gnus-server-opened method)
4889         (gnus-open-server method)
4890         (error "Unable to contact server: %s" (gnus-status-message method)))
4891     (or (gnus-request-list method)
4892         (error "Couldn't request list: %s" (gnus-status-message method)))
4893     (get-buffer-create gnus-browse-buffer)
4894     (gnus-add-current-to-buffer-list)
4895     (and gnus-carpal (gnus-carpal-setup-buffer 'browse))
4896     (gnus-configure-windows 'browse)
4897     (buffer-disable-undo (current-buffer))
4898     (let ((buffer-read-only nil))
4899       (erase-buffer))
4900     (gnus-browse-mode)
4901     (setq mode-line-buffer-identification
4902           (format
4903            "(ding) Browse Server {%s:%s}" (car method) (car (cdr method))))
4904     (save-excursion
4905       (set-buffer nntp-server-buffer)
4906       (let ((cur (current-buffer)))
4907         (goto-char (point-min))
4908         (or (string= gnus-ignored-newsgroups "")
4909             (delete-matching-lines gnus-ignored-newsgroups))
4910         (while (re-search-forward 
4911                 "\\(^[^ \t]+\\)[ \t]+[0-9]+[ \t]+[0-9]+" nil t)
4912           (goto-char (match-end 1))
4913           (setq groups (cons (cons (buffer-substring (match-beginning 1)
4914                                                      (match-end 1))
4915                                    (max 0 (- (1+ (read cur)) (read cur))))
4916                              groups)))))
4917     (setq groups (sort groups 
4918                        (lambda (l1 l2)
4919                          (string< (car l1) (car l2)))))
4920     (let ((buffer-read-only nil))
4921       (while groups
4922         (setq group (car groups))
4923         (insert 
4924          (format "K%7d: %s\n" (cdr group) (car group)))
4925         (setq groups (cdr groups))))
4926     (switch-to-buffer (current-buffer))
4927     (goto-char (point-min))
4928     (gnus-group-position-cursor)))
4929
4930 (defun gnus-browse-mode ()
4931   "Major mode for browsing a foreign server.
4932
4933 All normal editing commands are switched off.
4934
4935 \\<gnus-browse-mode-map>
4936 The only things you can do in this buffer is
4937
4938 1) `\\[gnus-browse-unsubscribe-current-group]' to subscribe to a group.
4939 The group will be inserted into the group buffer upon exit from this
4940 buffer.  
4941
4942 2) `\\[gnus-browse-read-group]' to read a group ephemerally.
4943
4944 3) `\\[gnus-browse-exit]' to return to the group buffer."
4945   (interactive)
4946   (kill-all-local-variables)
4947   (if gnus-visual (gnus-browse-make-menu-bar))
4948   (setq mode-line-modified "-- ")
4949   (make-local-variable 'mode-line-format)
4950   (setq mode-line-format (copy-sequence mode-line-format))
4951   (and (equal (nth 3 mode-line-format) "   ")
4952        (setcar (nthcdr 3 mode-line-format) ""))
4953   (setq major-mode 'gnus-browse-mode)
4954   (setq mode-name "Browse Server")
4955   (setq mode-line-process nil)
4956   (use-local-map gnus-browse-mode-map)
4957   (buffer-disable-undo (current-buffer))
4958   (setq truncate-lines t)
4959   (setq buffer-read-only t)
4960   (run-hooks 'gnus-browse-mode-hook))
4961
4962 (defun gnus-browse-read-group (&optional no-article)
4963   "Enter the group at the current line."
4964   (interactive)
4965   (let ((group (gnus-browse-group-name)))
4966     (or (gnus-group-read-ephemeral-group 
4967          group gnus-browse-current-method nil
4968          (cons (current-buffer) 'browse))
4969         (error "Couldn't enter %s" group))))
4970
4971 (defun gnus-browse-select-group ()
4972   "Select the current group."
4973   (interactive)
4974   (gnus-browse-read-group 'no))
4975
4976 (defun gnus-browse-next-group (n)
4977   "Go to the next group."
4978   (interactive "p")
4979   (prog1
4980       (forward-line n)
4981     (gnus-group-position-cursor)))
4982
4983 (defun gnus-browse-prev-group (n)
4984   "Go to the next group."
4985   (interactive "p")
4986   (gnus-browse-next-group (- n)))
4987
4988 (defun gnus-browse-unsubscribe-current-group (arg)
4989   "(Un)subscribe to the next ARG groups."
4990   (interactive "p")
4991   (and (eobp)
4992        (error "No group at current line."))
4993   (let ((ward (if (< arg 0) -1 1))
4994         (arg (abs arg)))
4995     (while (and (> arg 0)
4996                 (not (eobp))
4997                 (gnus-browse-unsubscribe-group)
4998                 (zerop (gnus-browse-next-group ward)))
4999       (setq arg (1- arg)))
5000     (gnus-group-position-cursor)
5001     (if (/= 0 arg) (gnus-message 7 "No more newsgroups"))
5002     arg))
5003
5004 (defun gnus-browse-group-name ()
5005   (save-excursion
5006     (beginning-of-line)
5007     (if (not (re-search-forward ": \\(.*\\)$" (gnus-point-at-eol) t))
5008         ()
5009       (gnus-group-prefixed-name 
5010        (buffer-substring (match-beginning 1) (match-end 1))
5011        gnus-browse-current-method))))
5012   
5013 (defun gnus-browse-unsubscribe-group ()
5014   (let ((sub nil)
5015         (buffer-read-only nil)
5016         group)
5017     (save-excursion
5018       (beginning-of-line)
5019       (if (= (following-char) ?K) (setq sub t))
5020       (setq group (gnus-browse-group-name))
5021       (beginning-of-line)
5022       (delete-char 1)
5023       (if sub
5024           (progn
5025             (gnus-group-change-level 
5026              (list t group gnus-level-default-subscribed
5027                    nil nil gnus-browse-current-method) 
5028              gnus-level-default-subscribed gnus-level-killed
5029              (and (car (nth 1 gnus-newsrc-alist))
5030                   (gnus-gethash (car (nth 1 gnus-newsrc-alist))
5031                                 gnus-newsrc-hashtb))
5032              t)
5033             (insert ? ))
5034         (gnus-group-change-level 
5035          group gnus-level-killed gnus-level-default-subscribed)
5036         (insert ?K)))
5037     t))
5038
5039 (defun gnus-browse-exit ()
5040   "Quit browsing and return to the group buffer."
5041   (interactive)
5042   (if (eq major-mode 'gnus-browse-mode)
5043       (kill-buffer (current-buffer)))
5044   (if gnus-browse-return-buffer
5045       (gnus-configure-windows 'server 'force)
5046     (gnus-configure-windows 'group 'force)
5047     (gnus-group-list-groups nil)))
5048
5049 (defun gnus-browse-describe-briefly ()
5050   "Give a one line description of the group mode commands."
5051   (interactive)
5052   (gnus-message 6
5053    (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")))
5054       
5055 \f
5056 ;;;
5057 ;;; Gnus summary mode
5058 ;;;
5059
5060 (defvar gnus-summary-mode-map nil)
5061 (defvar gnus-summary-mark-map nil)
5062 (defvar gnus-summary-mscore-map nil)
5063 (defvar gnus-summary-article-map nil)
5064 (defvar gnus-summary-thread-map nil)
5065 (defvar gnus-summary-goto-map nil)
5066 (defvar gnus-summary-exit-map nil)
5067 (defvar gnus-summary-various-map nil)
5068 (defvar gnus-summary-interest-map nil)
5069 (defvar gnus-summary-sort-map nil)
5070 (defvar gnus-summary-backend-map nil)
5071 (defvar gnus-summary-save-map nil)
5072 (defvar gnus-summary-wash-map nil)
5073 (defvar gnus-summary-help-map nil)
5074
5075 (put 'gnus-summary-mode 'mode-class 'special)
5076
5077 (if gnus-summary-mode-map
5078     nil
5079   (setq gnus-summary-mode-map (make-keymap))
5080   (suppress-keymap gnus-summary-mode-map)
5081
5082   ;; Non-orthogonal keys
5083
5084   (define-key gnus-summary-mode-map " " 'gnus-summary-next-page)
5085   (define-key gnus-summary-mode-map "\177" 'gnus-summary-prev-page)
5086   (define-key gnus-summary-mode-map "\r" 'gnus-summary-scroll-up)
5087   (define-key gnus-summary-mode-map "n" 'gnus-summary-next-unread-article)
5088   (define-key gnus-summary-mode-map "p" 'gnus-summary-prev-unread-article)
5089   (define-key gnus-summary-mode-map "N" 'gnus-summary-next-article)
5090   (define-key gnus-summary-mode-map "P" 'gnus-summary-prev-article)
5091   (define-key gnus-summary-mode-map "\M-\C-n" 'gnus-summary-next-same-subject)
5092   (define-key gnus-summary-mode-map "\M-\C-p" 'gnus-summary-prev-same-subject)
5093   (define-key gnus-summary-mode-map "\M-n" 'gnus-summary-next-unread-subject)
5094   (define-key gnus-summary-mode-map "\M-p" 'gnus-summary-prev-unread-subject)
5095   (define-key gnus-summary-mode-map "." 'gnus-summary-first-unread-article)
5096   (define-key gnus-summary-mode-map "," 'gnus-summary-best-unread-article)
5097   (define-key gnus-summary-mode-map "\M-s" 'gnus-summary-search-article-forward)
5098   (define-key gnus-summary-mode-map "\M-r" 'gnus-summary-search-article-backward)
5099   (define-key gnus-summary-mode-map "<" 'gnus-summary-beginning-of-article)
5100   (define-key gnus-summary-mode-map ">" 'gnus-summary-end-of-article)
5101   (define-key gnus-summary-mode-map "j" 'gnus-summary-goto-subject)
5102   (define-key gnus-summary-mode-map "^" 'gnus-summary-refer-parent-article)
5103   (define-key gnus-summary-mode-map "\M-^" 'gnus-summary-refer-article)
5104   (define-key gnus-summary-mode-map "u" 'gnus-summary-tick-article-forward)
5105   (define-key gnus-summary-mode-map "!" 'gnus-summary-tick-article-forward)
5106   (define-key gnus-summary-mode-map "U" 'gnus-summary-tick-article-backward)
5107   (define-key gnus-summary-mode-map "d" 'gnus-summary-mark-as-read-forward)
5108   (define-key gnus-summary-mode-map "D" 'gnus-summary-mark-as-read-backward)
5109   (define-key gnus-summary-mode-map "E" 'gnus-summary-mark-as-expirable)
5110   (define-key gnus-summary-mode-map "\M-u" 'gnus-summary-clear-mark-forward)
5111   (define-key gnus-summary-mode-map "\M-U" 'gnus-summary-clear-mark-backward)
5112   (define-key gnus-summary-mode-map "k" 'gnus-summary-kill-same-subject-and-select)
5113   (define-key gnus-summary-mode-map "\C-k" 'gnus-summary-kill-same-subject)
5114   (define-key gnus-summary-mode-map "\M-\C-k" 'gnus-summary-kill-thread)
5115   (define-key gnus-summary-mode-map "\M-\C-l" 'gnus-summary-lower-thread)
5116   (define-key gnus-summary-mode-map "e" 'gnus-summary-edit-article)
5117   (define-key gnus-summary-mode-map "#" 'gnus-summary-mark-as-processable)
5118   (define-key gnus-summary-mode-map "\M-#" 'gnus-summary-unmark-as-processable)
5119   (define-key gnus-summary-mode-map "\M-\C-t" 'gnus-summary-toggle-threads)
5120   (define-key gnus-summary-mode-map "\M-\C-s" 'gnus-summary-show-thread)
5121   (define-key gnus-summary-mode-map "\M-\C-h" 'gnus-summary-hide-thread)
5122   (define-key gnus-summary-mode-map "\M-\C-f" 'gnus-summary-next-thread)
5123   (define-key gnus-summary-mode-map "\M-\C-b" 'gnus-summary-prev-thread)
5124   (define-key gnus-summary-mode-map "\M-\C-u" 'gnus-summary-up-thread)
5125   (define-key gnus-summary-mode-map "\M-\C-d" 'gnus-summary-down-thread)
5126   (define-key gnus-summary-mode-map "&" 'gnus-summary-execute-command)
5127   (define-key gnus-summary-mode-map "c" 'gnus-summary-catchup-and-exit)
5128   (define-key gnus-summary-mode-map "\C-w" 'gnus-summary-mark-region-as-read)
5129   (define-key gnus-summary-mode-map "\C-t" 'gnus-summary-toggle-truncation)
5130   (define-key gnus-summary-mode-map "?" 'gnus-summary-mark-as-dormant)
5131   (define-key gnus-summary-mode-map "\C-c\M-\C-s" 'gnus-summary-show-all-expunged)
5132   (define-key gnus-summary-mode-map "\C-c\C-s\C-n" 'gnus-summary-sort-by-number)
5133   (define-key gnus-summary-mode-map "\C-c\C-s\C-a" 'gnus-summary-sort-by-author)
5134   (define-key gnus-summary-mode-map "\C-c\C-s\C-s" 'gnus-summary-sort-by-subject)
5135   (define-key gnus-summary-mode-map "\C-c\C-s\C-d" 'gnus-summary-sort-by-date)
5136   (define-key gnus-summary-mode-map "\C-c\C-s\C-i" 'gnus-summary-sort-by-score)
5137   (define-key gnus-summary-mode-map "=" 'gnus-summary-expand-window)
5138   (define-key gnus-summary-mode-map "\C-x\C-s" 'gnus-summary-reselect-current-group)
5139   (define-key gnus-summary-mode-map "\M-g" 'gnus-summary-rescan-group)
5140   (define-key gnus-summary-mode-map "w" 'gnus-summary-stop-page-breaking)
5141   (define-key gnus-summary-mode-map "\C-c\C-r" 'gnus-summary-caesar-message)
5142   (define-key gnus-summary-mode-map "\M-t" 'gnus-summary-toggle-mime)
5143   (define-key gnus-summary-mode-map "f" 'gnus-summary-followup)
5144   (define-key gnus-summary-mode-map "F" 'gnus-summary-followup-with-original)
5145   (define-key gnus-summary-mode-map "C" 'gnus-summary-cancel-article)
5146   (define-key gnus-summary-mode-map "r" 'gnus-summary-reply)
5147   (define-key gnus-summary-mode-map "R" 'gnus-summary-reply-with-original)
5148   (define-key gnus-summary-mode-map "\C-c\C-f" 'gnus-summary-mail-forward)
5149   (define-key gnus-summary-mode-map "o" 'gnus-summary-save-article)
5150   (define-key gnus-summary-mode-map "\C-o" 'gnus-summary-save-article-mail)
5151   (define-key gnus-summary-mode-map "|" 'gnus-summary-pipe-output)
5152   (define-key gnus-summary-mode-map "\M-k" 'gnus-summary-edit-local-kill)
5153   (define-key gnus-summary-mode-map "\M-K" 'gnus-summary-edit-global-kill)
5154   (define-key gnus-summary-mode-map "V" 'gnus-version)
5155   (define-key gnus-summary-mode-map "\C-c\C-d" 'gnus-summary-describe-group)
5156   (define-key gnus-summary-mode-map "q" 'gnus-summary-exit)
5157   (define-key gnus-summary-mode-map "Q" 'gnus-summary-exit-no-update)
5158   (define-key gnus-summary-mode-map "\C-c\C-i" 'gnus-info-find-node)
5159   (define-key gnus-summary-mode-map gnus-mouse-2 'gnus-mouse-pick-article)
5160   (define-key gnus-summary-mode-map "m" 'gnus-summary-mail-other-window)
5161   (define-key gnus-summary-mode-map "a" 'gnus-summary-post-news)
5162   (define-key gnus-summary-mode-map "x" 'gnus-summary-remove-lines-marked-as-read)
5163 ; (define-key gnus-summary-mode-map "X" 'gnus-summary-remove-lines-marked-with)
5164   (define-key gnus-summary-mode-map "s" 'gnus-summary-isearch-article)
5165   (define-key gnus-summary-mode-map "t" 'gnus-summary-toggle-header)
5166   (define-key gnus-summary-mode-map "g" 'gnus-summary-show-article)
5167 ;  (define-key gnus-summary-mode-map "?" 'gnus-summary-describe-briefly)
5168   (define-key gnus-summary-mode-map "l" 'gnus-summary-goto-last-article)
5169   (define-key gnus-summary-mode-map "\C-c\C-v\C-v" 'gnus-uu-decode-uu-view)
5170   (define-key gnus-summary-mode-map "\C-d" 'gnus-summary-enter-digest-group)
5171   (define-key gnus-summary-mode-map "v" 'gnus-summary-verbose-headers)
5172   (define-key gnus-summary-mode-map "\C-c\C-b" 'gnus-bug)
5173
5174
5175   ;; Sort of orthogonal keymap
5176   (define-prefix-command 'gnus-summary-mark-map)
5177   (define-key gnus-summary-mode-map "M" 'gnus-summary-mark-map)
5178   (define-key gnus-summary-mark-map "t" 'gnus-summary-tick-article-forward)
5179   (define-key gnus-summary-mark-map "!" 'gnus-summary-tick-article-forward)
5180   (define-key gnus-summary-mark-map "d" 'gnus-summary-mark-as-read-forward)
5181   (define-key gnus-summary-mark-map "r" 'gnus-summary-mark-as-read-forward)
5182   (define-key gnus-summary-mark-map "c" 'gnus-summary-clear-mark-forward)
5183   (define-key gnus-summary-mark-map " " 'gnus-summary-clear-mark-forward)
5184   (define-key gnus-summary-mark-map "e" 'gnus-summary-mark-as-expirable)
5185   (define-key gnus-summary-mark-map "x" 'gnus-summary-mark-as-expirable)
5186   (define-key gnus-summary-mark-map "?" 'gnus-summary-mark-as-dormant)
5187   (define-key gnus-summary-mark-map "b" 'gnus-summary-set-bookmark)
5188   (define-key gnus-summary-mark-map "B" 'gnus-summary-remove-bookmark)
5189   (define-key gnus-summary-mark-map "#" 'gnus-summary-mark-as-processable)
5190   (define-key gnus-summary-mark-map "\M-#" 'gnus-summary-unmark-as-processable)
5191   (define-key gnus-summary-mark-map "\M-r" 'gnus-summary-remove-lines-marked-as-read)
5192   (define-key gnus-summary-mark-map "\M-\C-r" 'gnus-summary-remove-lines-marked-with)
5193   (define-key gnus-summary-mark-map "D" 'gnus-summary-show-all-dormant)
5194   (define-key gnus-summary-mark-map "\M-D" 'gnus-summary-hide-all-dormant)
5195   (define-key gnus-summary-mark-map "S" 'gnus-summary-show-all-expunged)
5196   (define-key gnus-summary-mark-map "C" 'gnus-summary-catchup)
5197   (define-key gnus-summary-mark-map "H" 'gnus-summary-catchup-to-here)
5198   (define-key gnus-summary-mark-map "\C-c" 'gnus-summary-catchup-all)
5199   (define-key gnus-summary-mark-map "k" 'gnus-summary-kill-same-subject-and-select)
5200   (define-key gnus-summary-mark-map "K" 'gnus-summary-kill-same-subject)
5201
5202   (define-prefix-command 'gnus-summary-mscore-map)
5203   (define-key gnus-summary-mark-map "s" 'gnus-summary-mscore-map)
5204   (define-key gnus-summary-mscore-map "c" 'gnus-summary-clear-above)
5205   (define-key gnus-summary-mscore-map "u" 'gnus-summary-tick-above)
5206   (define-key gnus-summary-mscore-map "m" 'gnus-summary-mark-above)
5207   (define-key gnus-summary-mscore-map "k" 'gnus-summary-kill-below)
5208
5209   (define-key gnus-summary-mark-map "p" 'gnus-uu-mark-map)
5210   
5211   (define-key gnus-summary-mode-map "S" 'gnus-summary-send-map)
5212   
5213   (define-prefix-command 'gnus-summary-goto-map)
5214   (define-key gnus-summary-mode-map "G" 'gnus-summary-goto-map)
5215   (define-key gnus-summary-goto-map "n" 'gnus-summary-next-unread-article)
5216   (define-key gnus-summary-goto-map "p" 'gnus-summary-prev-unread-article)
5217   (define-key gnus-summary-goto-map "N" 'gnus-summary-next-article)
5218   (define-key gnus-summary-goto-map "P" 'gnus-summary-prev-article)
5219   (define-key gnus-summary-goto-map "\C-n" 'gnus-summary-next-same-subject)
5220   (define-key gnus-summary-goto-map "\C-p" 'gnus-summary-prev-same-subject)
5221   (define-key gnus-summary-goto-map "\M-n" 'gnus-summary-next-unread-subject)
5222   (define-key gnus-summary-goto-map "\M-p" 'gnus-summary-prev-unread-subject)
5223   (define-key gnus-summary-goto-map "f" 'gnus-summary-first-unread-article)
5224   (define-key gnus-summary-goto-map "b" 'gnus-summary-best-unread-article)
5225   (define-key gnus-summary-goto-map "g" 'gnus-summary-goto-subject)
5226   (define-key gnus-summary-goto-map "l" 'gnus-summary-goto-last-article)
5227   (define-key gnus-summary-goto-map "p" 'gnus-summary-pop-article)
5228
5229
5230   (define-prefix-command 'gnus-summary-thread-map)
5231   (define-key gnus-summary-mode-map "T" 'gnus-summary-thread-map)
5232   (define-key gnus-summary-thread-map "k" 'gnus-summary-kill-thread)
5233   (define-key gnus-summary-thread-map "l" 'gnus-summary-lower-thread)
5234   (define-key gnus-summary-thread-map "i" 'gnus-summary-raise-thread)
5235   (define-key gnus-summary-thread-map "T" 'gnus-summary-toggle-threads)
5236   (define-key gnus-summary-thread-map "s" 'gnus-summary-show-thread)
5237   (define-key gnus-summary-thread-map "S" 'gnus-summary-show-all-threads)
5238   (define-key gnus-summary-thread-map "h" 'gnus-summary-hide-thread)
5239   (define-key gnus-summary-thread-map "H" 'gnus-summary-hide-all-threads)
5240   (define-key gnus-summary-thread-map "n" 'gnus-summary-next-thread)
5241   (define-key gnus-summary-thread-map "p" 'gnus-summary-prev-thread)
5242   (define-key gnus-summary-thread-map "u" 'gnus-summary-up-thread)
5243   (define-key gnus-summary-thread-map "d" 'gnus-summary-down-thread)
5244   (define-key gnus-summary-thread-map "#" 'gnus-uu-mark-thread)
5245
5246   
5247   (define-prefix-command 'gnus-summary-exit-map)
5248   (define-key gnus-summary-mode-map "Z" 'gnus-summary-exit-map)
5249   (define-key gnus-summary-exit-map "c" 'gnus-summary-catchup-and-exit)
5250   (define-key gnus-summary-exit-map "C" 'gnus-summary-catchup-all-and-exit)
5251   (define-key gnus-summary-exit-map "E" 'gnus-summary-exit-no-update)
5252   (define-key gnus-summary-exit-map "Q" 'gnus-summary-exit)
5253   (define-key gnus-summary-exit-map "Z" 'gnus-summary-exit)
5254   (define-key gnus-summary-exit-map "n" 'gnus-summary-catchup-and-goto-next-group)
5255   (define-key gnus-summary-exit-map "R" 'gnus-summary-reselect-current-group)
5256   (define-key gnus-summary-exit-map "G" 'gnus-summary-rescan-group)
5257   (define-key gnus-summary-exit-map "N" 'gnus-summary-next-group)
5258   (define-key gnus-summary-exit-map "P" 'gnus-summary-prev-group)
5259
5260
5261   (define-prefix-command 'gnus-summary-article-map)
5262   (define-key gnus-summary-mode-map "A" 'gnus-summary-article-map)
5263   (define-key gnus-summary-article-map " " 'gnus-summary-next-page)
5264   (define-key gnus-summary-article-map "n" 'gnus-summary-next-page)
5265   (define-key gnus-summary-article-map "\177" 'gnus-summary-prev-page)
5266   (define-key gnus-summary-article-map "p" 'gnus-summary-prev-page)
5267   (define-key gnus-summary-article-map "\r" 'gnus-summary-scroll-up)
5268   (define-key gnus-summary-article-map "<" 'gnus-summary-beginning-of-article)
5269   (define-key gnus-summary-article-map ">" 'gnus-summary-end-of-article)
5270   (define-key gnus-summary-article-map "b" 'gnus-summary-beginning-of-article)
5271   (define-key gnus-summary-article-map "e" 'gnus-summary-end-of-article)
5272   (define-key gnus-summary-article-map "^" 'gnus-summary-refer-parent-article)
5273   (define-key gnus-summary-article-map "r" 'gnus-summary-refer-parent-article)
5274   (define-key gnus-summary-article-map "w" 'gnus-summary-stop-page-breaking)
5275   (define-key gnus-summary-article-map "c" 'gnus-summary-caesar-message)
5276   (define-key gnus-summary-article-map "g" 'gnus-summary-show-article)
5277   (define-key gnus-summary-article-map "t" 'gnus-summary-toggle-header)
5278   (define-key gnus-summary-article-map "m" 'gnus-summary-toggle-mime)
5279   (define-key gnus-summary-article-map "s" 'gnus-summary-isearch-article)
5280
5281
5282   (define-prefix-command 'gnus-summary-wash-map)
5283   (define-key gnus-summary-mode-map "W" 'gnus-summary-wash-map)
5284   (define-key gnus-summary-wash-map "h" 'gnus-article-hide-headers)
5285   (define-key gnus-summary-wash-map "s" 'gnus-article-hide-signature)
5286   (define-key gnus-summary-wash-map "c" 'gnus-article-hide-citation)
5287   (define-key gnus-summary-wash-map "\C-c" 'gnus-article-hide-citation-maybe)
5288   (define-key gnus-summary-wash-map "o" 'gnus-article-treat-overstrike)
5289   (define-key gnus-summary-wash-map "w" 'gnus-article-word-wrap)
5290   (define-key gnus-summary-wash-map "d" 'gnus-article-remove-cr)
5291   (define-key gnus-summary-wash-map "q" 'gnus-article-de-quoted-unreadable)
5292   (define-key gnus-summary-wash-map "f" 'gnus-article-display-x-face)
5293   (define-key gnus-summary-wash-map "t" 'gnus-article-date-ut)
5294   (define-key gnus-summary-wash-map "\C-t" 'gnus-article-date-local)
5295   (define-key gnus-summary-wash-map "T" 'gnus-article-date-lapsed)
5296
5297   (define-key gnus-summary-wash-map "A" 'gnus-article-highlight)
5298   (define-key gnus-summary-wash-map "a" 'gnus-article-hide)
5299   (define-key gnus-summary-wash-map "H" 'gnus-article-highlight-headers)
5300   (define-key gnus-summary-wash-map "C" 'gnus-article-highlight-citation)
5301   (define-key gnus-summary-wash-map "S" 'gnus-article-highlight-signature)
5302   (define-key gnus-summary-wash-map "b" 'gnus-article-add-buttons)
5303
5304
5305   (define-prefix-command 'gnus-summary-help-map)
5306   (define-key gnus-summary-mode-map "H" 'gnus-summary-help-map)
5307   (define-key gnus-summary-help-map "v" 'gnus-version)
5308   (define-key gnus-summary-help-map "f" 'gnus-summary-fetch-faq)
5309   (define-key gnus-summary-help-map "d" 'gnus-summary-describe-group)
5310   (define-key gnus-summary-help-map "h" 'gnus-summary-describe-briefly)
5311   (define-key gnus-summary-help-map "i" 'gnus-info-find-node)
5312
5313
5314   (define-prefix-command 'gnus-summary-backend-map)
5315   (define-key gnus-summary-mode-map "B" 'gnus-summary-backend-map)
5316   (define-key gnus-summary-backend-map "e" 'gnus-summary-expire-articles)
5317   (define-key gnus-summary-backend-map "\M-\C-e" 
5318     'gnus-summary-expire-articles-now)
5319   (define-key gnus-summary-backend-map "\177" 'gnus-summary-delete-article)
5320   (define-key gnus-summary-backend-map "m" 'gnus-summary-move-article)
5321   (define-key gnus-summary-backend-map "r" 'gnus-summary-respool-article)
5322   (define-key gnus-summary-backend-map "w" 'gnus-summary-edit-article)
5323   (define-key gnus-summary-backend-map "c" 'gnus-summary-copy-article)
5324   (define-key gnus-summary-backend-map "q" 'gnus-summary-fancy-query)
5325   (define-key gnus-summary-backend-map "i" 'gnus-summary-import-article)
5326
5327
5328   (define-prefix-command 'gnus-summary-save-map)
5329   (define-key gnus-summary-mode-map "O" 'gnus-summary-save-map)
5330   (define-key gnus-summary-save-map "o" 'gnus-summary-save-article)
5331   (define-key gnus-summary-save-map "m" 'gnus-summary-save-article-mail)
5332   (define-key gnus-summary-save-map "r" 'gnus-summary-save-article-rmail)
5333   (define-key gnus-summary-save-map "f" 'gnus-summary-save-article-file)
5334   (define-key gnus-summary-save-map "h" 'gnus-summary-save-article-folder)
5335   (define-key gnus-summary-save-map "v" 'gnus-summary-save-article-vm)
5336   (define-key gnus-summary-save-map "p" 'gnus-summary-pipe-output)
5337 ;  (define-key gnus-summary-save-map "s" 'gnus-soup-add-article)
5338
5339   (define-key gnus-summary-mode-map "X" 'gnus-uu-extract-map)
5340   
5341   (define-prefix-command 'gnus-summary-various-map)
5342   (define-key gnus-summary-mode-map "V" 'gnus-summary-various-map)
5343   (define-key gnus-summary-various-map "u" 'gnus-summary-universal-argument)
5344   (define-key gnus-summary-various-map "\C-s" 'gnus-summary-search-article-forward)
5345   (define-key gnus-summary-various-map "\C-r" 'gnus-summary-search-article-backward)
5346   (define-key gnus-summary-various-map "r" 'gnus-summary-refer-article)
5347   (define-key gnus-summary-various-map "&" 'gnus-summary-execute-command)
5348   (define-key gnus-summary-various-map "T" 'gnus-summary-toggle-truncation)
5349   (define-key gnus-summary-various-map "e" 'gnus-summary-expand-window)
5350   (define-key gnus-summary-various-map "D" 'gnus-summary-enter-digest-group)
5351   (define-key gnus-summary-various-map "k" 'gnus-summary-edit-local-kill)
5352   (define-key gnus-summary-various-map "K" 'gnus-summary-edit-global-kill)
5353
5354   (define-key gnus-summary-various-map "S" 'gnus-summary-score-map)
5355
5356   (define-prefix-command 'gnus-summary-sort-map)
5357   (define-key gnus-summary-various-map "s" 'gnus-summary-sort-map)
5358   (define-key gnus-summary-sort-map "n" 'gnus-summary-sort-by-number)
5359   (define-key gnus-summary-sort-map "a" 'gnus-summary-sort-by-author)
5360   (define-key gnus-summary-sort-map "s" 'gnus-summary-sort-by-subject)
5361   (define-key gnus-summary-sort-map "d" 'gnus-summary-sort-by-date)
5362   (define-key gnus-summary-sort-map "i" 'gnus-summary-sort-by-score)
5363
5364   (define-key gnus-summary-mode-map "I" 'gnus-summary-increase-score)
5365   (define-key gnus-summary-mode-map "L" 'gnus-summary-lower-score)
5366   )
5367
5368
5369 \f
5370
5371 (defun gnus-summary-mode (&optional group)
5372   "Major mode for reading articles.
5373
5374 All normal editing commands are switched off.
5375 \\<gnus-summary-mode-map>
5376 Each line in this buffer represents one article.  To read an
5377 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
5378 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]', 
5379 respectively.
5380
5381 You can also post articles and send mail from this buffer.  To 
5382 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author 
5383 of an article, type `\\[gnus-summary-reply]'.
5384
5385 There are approx. one gazillion commands you can execute in this 
5386 buffer; read the info pages for more information (`\\[gnus-info-find-node]'). 
5387
5388 The following commands are available:
5389
5390 \\{gnus-summary-mode-map}"
5391   (interactive)
5392   (if gnus-visual (gnus-summary-make-menu-bar))
5393   (kill-all-local-variables)
5394   (let ((locals gnus-summary-local-variables))
5395     (while locals
5396       (if (consp (car locals))
5397           (progn
5398             (make-local-variable (car (car locals)))
5399             (set (car (car locals)) (eval (cdr (car locals)))))
5400         (make-local-variable (car locals))
5401         (set (car locals) nil))
5402       (setq locals (cdr locals))))
5403   (gnus-make-thread-indent-array)
5404   (setq mode-line-modified "-- ")
5405   (make-local-variable 'mode-line-format)
5406   (setq mode-line-format (copy-sequence mode-line-format))
5407   (and (equal (nth 3 mode-line-format) "   ")
5408        (setcar (nthcdr 3 mode-line-format) ""))
5409   (setq major-mode 'gnus-summary-mode)
5410   (setq mode-name "Summary")
5411   (make-local-variable 'minor-mode-alist)
5412   (use-local-map gnus-summary-mode-map)
5413   (buffer-disable-undo (current-buffer))
5414   (setq buffer-read-only t)             ;Disable modification
5415   (setq truncate-lines t)
5416   (setq selective-display t)
5417   (setq selective-display-ellipses t)   ;Display `...'
5418   (setq buffer-display-table gnus-summary-display-table)
5419   (setq gnus-newsgroup-name group)
5420   (run-hooks 'gnus-summary-mode-hook))
5421
5422 (defun gnus-summary-make-display-table ()
5423   ;; Change the display table.  Odd characters have a tendency to mess
5424   ;; up nicely formatted displays - we make all possible glyphs
5425   ;; display only a single character.
5426
5427   ;; We start from the standard display table, if any.
5428   (setq gnus-summary-display-table 
5429         (or (copy-sequence standard-display-table)
5430             (make-display-table)))
5431   ;; Nix out all the control chars...
5432   (let ((i 32))
5433     (while (>= (setq i (1- i)) 0)
5434       (aset gnus-summary-display-table i [??])))
5435   ;; ... but not newline and cr, of course. (cr is necessary for the
5436   ;; selective display).  
5437   (aset gnus-summary-display-table ?\n nil)
5438   (aset gnus-summary-display-table ?\r nil)
5439   ;; We nix out any glyphs over 126 that are not set already.  
5440   (let ((i 256))
5441     (while (>= (setq i (1- i)) 127)
5442       ;; Only modify if the entry is nil.
5443       (or (aref gnus-summary-display-table i) 
5444           (aset gnus-summary-display-table i [??])))))
5445
5446 (defun gnus-summary-clear-local-variables ()
5447   (let ((locals gnus-summary-local-variables))
5448     (while locals
5449       (if (consp (car locals))
5450           (and (vectorp (car (car locals)))
5451                (set (car (car locals)) nil))
5452         (and (vectorp (car locals))
5453              (set (car locals) nil)))
5454       (setq locals (cdr locals)))))
5455
5456 (defun gnus-mouse-pick-article (e)
5457   (interactive "e")
5458   (mouse-set-point e)
5459   (gnus-summary-next-page nil t))
5460
5461 (defun gnus-summary-setup-buffer (group)
5462   "Initialize summary buffer."
5463   (let ((buffer (concat "*Summary " group "*")))
5464     (if (get-buffer buffer)
5465         (progn
5466           (set-buffer buffer)
5467           (not gnus-newsgroup-begin))
5468       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
5469       (setq gnus-summary-buffer (set-buffer (get-buffer-create buffer)))
5470       (gnus-add-current-to-buffer-list)
5471       (gnus-summary-mode group)
5472       (and gnus-carpal (gnus-carpal-setup-buffer 'summary))
5473       (setq gnus-newsgroup-name group)
5474       t)))
5475
5476 (defun gnus-set-global-variables ()
5477   ;; Set the global equivalents of the summary buffer-local variables
5478   ;; to the latest values they had. These reflect the summary buffer
5479   ;; that was in action when the last article was fetched.
5480   (if (eq major-mode 'gnus-summary-mode) 
5481       (progn
5482         (setq gnus-summary-buffer (current-buffer))
5483         (let ((name gnus-newsgroup-name)
5484               (marked gnus-newsgroup-marked)
5485               (unread gnus-newsgroup-unreads)
5486               (headers gnus-current-headers)
5487               (score-file gnus-current-score-file))
5488           (save-excursion
5489             (set-buffer gnus-group-buffer)
5490             (setq gnus-newsgroup-name name)
5491             (setq gnus-newsgroup-marked marked)
5492             (setq gnus-newsgroup-unreads unread)
5493             (setq gnus-current-headers headers)
5494             (setq gnus-current-score-file score-file))))))
5495
5496 (defun gnus-summary-insert-dummy-line (sformat subject number)
5497   (if (not sformat) 
5498       (setq sformat gnus-summary-dummy-line-format-spec))
5499   (let (b)
5500     (beginning-of-line)
5501     (setq b (point))
5502     (insert (eval sformat))
5503     (add-text-properties
5504      b (1+ b)
5505      (list 'gnus-number number 
5506            'gnus-mark gnus-dummy-mark
5507            'gnus-level 0))))
5508
5509 (defvar gnus-thread-indent-array nil)
5510 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
5511 (defun gnus-make-thread-indent-array ()
5512   (let ((n 200))
5513     (if (and gnus-thread-indent-array
5514              (= gnus-thread-indent-level gnus-thread-indent-array-level))
5515         nil
5516       (setq gnus-thread-indent-array (make-vector 201 "")
5517             gnus-thread-indent-array-level gnus-thread-indent-level)
5518       (while (>= n 0)
5519         (aset gnus-thread-indent-array n
5520               (make-string (* n gnus-thread-indent-level) ? ))
5521         (setq n (1- n))))))
5522
5523 (defun gnus-summary-insert-line 
5524   (sformat header level current unread replied expirable subject-or-nil
5525            &optional dummy score)
5526   (or sformat (setq sformat gnus-summary-line-format-spec))
5527   (let* ((indentation (aref gnus-thread-indent-array level))
5528          (lines (header-lines header))
5529          (score (or score gnus-summary-default-score 0))
5530          (score-char
5531           (if (or (null gnus-summary-default-score)
5532                   (<= (abs (- score gnus-summary-default-score))
5533                       gnus-summary-zcore-fuzz)) ? 
5534             (if (< score gnus-summary-default-score)
5535                 gnus-score-below-mark gnus-score-over-mark)))
5536          (replied (if replied gnus-replied-mark ? ))
5537          (from (header-from header))
5538          (name (cond 
5539                 ((string-match "(.+)" from)
5540                  (substring from (1+ (match-beginning 0)) (1- (match-end 0))))
5541                 ((string-match "<[^>]+> *$" from)
5542                  (let ((beg (match-beginning 0)))
5543                    (or (and (string-match "^\"[^\"]*\"" from)
5544                             (substring from (1+ (match-beginning 0))
5545                                        (1- (match-end 0))))
5546                        (substring from 0 beg))))
5547                 (t from)))
5548          (subject (header-subject header))
5549          (number (header-number header))
5550          (opening-bracket (if dummy ?\< ?\[))
5551          (closing-bracket (if dummy ?\> ?\]))
5552          (buffer-read-only nil)
5553          (b (progn (beginning-of-line) (point))))
5554     (or (numberp lines) (setq lines 0))
5555     (insert (eval sformat))
5556     (add-text-properties
5557      b (1+ b) (list 'gnus-number number 
5558                     'gnus-mark (or unread gnus-unread-mark)
5559                     'gnus-level level))))
5560
5561 (defun gnus-summary-update-line (&optional dont-update)
5562   ;; Update summary line after change.
5563   (or (not gnus-summary-default-score)
5564       gnus-summary-inhibit-highlight
5565       (let ((gnus-summary-inhibit-highlight t)
5566             (article (gnus-summary-article-number)))
5567         (progn
5568           (or dont-update
5569               (if (and gnus-summary-mark-below
5570                        (< (gnus-summary-article-score)
5571                           gnus-summary-mark-below))
5572                   (and (not (memq article gnus-newsgroup-marked))
5573                        (not (memq article gnus-newsgroup-dormant))
5574                        (memq article gnus-newsgroup-unreads)
5575                        (gnus-summary-mark-article nil gnus-low-score-mark))
5576                 (and (eq (gnus-summary-article-mark) gnus-low-score-mark)
5577                      (gnus-summary-mark-article nil gnus-unread-mark))))
5578           (and gnus-visual
5579                (run-hooks 'gnus-summary-update-hook))))))
5580
5581 (defun gnus-summary-update-lines (&optional beg end)
5582   ;; Mark article as read (or not) by taking into account scores.
5583   (let ((beg (or beg (point-min)))
5584         (end (or end (point-max))))
5585     (if (or (not gnus-summary-default-score)
5586             gnus-summary-inhibit-highlight)
5587         ()
5588       (let ((gnus-summary-inhibit-highlight t)
5589             article)
5590         (save-excursion
5591           (set-buffer gnus-summary-buffer)
5592           (goto-char beg)
5593           (beginning-of-line)
5594           (while (and (not (eobp)) (< (point) end))
5595             (if (and gnus-summary-mark-below
5596                      (< (or (cdr (assq 
5597                                   (setq article (get-text-property 
5598                                                  (point) 'gnus-number))
5599                                   gnus-newsgroup-scored))
5600                             gnus-summary-default-score 0)
5601                         gnus-summary-mark-below))
5602                 ;; We want to possibly mark it as read...
5603                 (and (not (memq article gnus-newsgroup-marked))
5604                      (not (memq article gnus-newsgroup-dormant))
5605                      (memq article gnus-newsgroup-unreads)
5606                      (gnus-summary-mark-article nil gnus-low-score-mark))
5607               ;; We want to possibly mark it as unread.
5608               (and (eq (get-text-property (point) 'gnus-mark)
5609                        gnus-low-score-mark)
5610                    (gnus-summary-mark-article nil gnus-unread-mark)))
5611             ;; Do the visual highlights at the same time.
5612             (and gnus-visual (run-hooks 'gnus-summary-update-hook))
5613             (forward-line 1)))))))
5614
5615 (defvar gnus-tmp-gathered nil)
5616
5617 (defun gnus-summary-number-of-articles-in-thread (thread &optional char)
5618   ;; Sum up all elements (and sub-elements) in a list.
5619   (let* ((number 
5620           (if (and (consp thread) (cdr thread)
5621                    (not (memq (header-number (car (car (cdr thread))))
5622                               gnus-tmp-gathered)))
5623               (apply 
5624                '+ 1 (mapcar 'gnus-summary-number-of-articles-in-thread 
5625                             (cdr thread)))
5626             1)))
5627     (if char 
5628         (if (> number 1) gnus-not-empty-thread-mark
5629           gnus-empty-thread-mark)
5630       number)))
5631
5632 (defun gnus-summary-read-group 
5633   (group &optional show-all no-article kill-buffer)
5634   "Start reading news in newsgroup GROUP.
5635 If SHOW-ALL is non-nil, already read articles are also listed.
5636 If NO-ARTICLE is non-nil, no article is selected initially."
5637   (gnus-message 5 "Retrieving newsgroup: %s..." group)
5638   (let* ((new-group (gnus-summary-setup-buffer group))
5639          (quit-config (nth 1 (assoc 'quit-config (gnus-find-method-for-group
5640                                                   group))))
5641          (did-select (and new-group (gnus-select-newsgroup group show-all))))
5642     (cond 
5643      ((not new-group)
5644       (gnus-set-global-variables)
5645       (gnus-kill-buffer kill-buffer)
5646       (gnus-configure-windows 'summary)
5647       (gnus-set-mode-line 'summary)
5648       (gnus-summary-position-cursor)
5649       (message "")
5650       t)
5651      ((null did-select) 
5652       (and (eq major-mode 'gnus-summary-mode)
5653            (not (equal (current-buffer) kill-buffer))
5654            (progn
5655              (kill-buffer (current-buffer))
5656              (if (not quit-config)
5657                  (progn
5658                    (set-buffer gnus-group-buffer)
5659                    (gnus-group-jump-to-group group)
5660                    (gnus-group-next-unread-group 1))
5661                (if (not (buffer-name (car quit-config)))
5662                    (gnus-configure-windows 'group 'force)
5663                  (set-buffer (car quit-config))
5664                  (and (eq major-mode 'gnus-summary-mode)
5665                       (gnus-set-global-variables))
5666                  (gnus-configure-windows (cdr quit-config))))))
5667       (message "Can't select group")
5668       nil)
5669      ((eq did-select 'quit)
5670       (and (eq major-mode 'gnus-summary-mode)
5671            (not (equal (current-buffer) kill-buffer))
5672            (kill-buffer (current-buffer)))
5673       (gnus-kill-buffer kill-buffer)
5674       (if (not quit-config)
5675           (progn
5676             (set-buffer gnus-group-buffer)
5677             (gnus-group-jump-to-group group)
5678             (gnus-group-next-unread-group 1)
5679             (gnus-configure-windows 'group 'force))
5680         (if (not (buffer-name (car quit-config)))
5681             (gnus-configure-windows 'group 'force)
5682           (set-buffer (car quit-config))
5683           (and (eq major-mode 'gnus-summary-mode)
5684                (gnus-set-global-variables))
5685           (gnus-configure-windows (cdr quit-config))))
5686       (signal 'quit nil))
5687      (t
5688       (gnus-set-global-variables)
5689       ;; Save the active value in effect when the group was entered.
5690       (setq gnus-newsgroup-active 
5691             (gnus-copy-sequence
5692              (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
5693       ;; You can change the subjects in this hook.
5694       (run-hooks 'gnus-select-group-hook)
5695       ;; Do score processing.
5696       (and gnus-use-scoring (gnus-possibly-score-headers))
5697       ;; Generate the summary buffer.
5698       (gnus-summary-prepare)
5699       (if (zerop (buffer-size))
5700           (cond (gnus-newsgroup-dormant
5701                  (gnus-summary-show-all-dormant))
5702                 ((and gnus-newsgroup-scored show-all)
5703                  (gnus-summary-show-all-expunged))))
5704       ;; Function `gnus-apply-kill-file' must be called in this hook.
5705       (run-hooks 'gnus-apply-kill-hook)
5706       (if (zerop (buffer-size))
5707           (progn
5708             ;; This newsgroup is empty.
5709             (gnus-summary-catchup-and-exit nil t) ;Without confirmations.
5710             (gnus-message 6 "No unread news")
5711             (gnus-kill-buffer kill-buffer)
5712             nil)
5713         ;;(save-excursion
5714         ;;  (if kill-buffer
5715         ;;      (let ((gnus-summary-buffer kill-buffer))
5716         ;;      (gnus-configure-windows 'group))))
5717         ;; Hide conversation thread subtrees.  We cannot do this in
5718         ;; gnus-summary-prepare-hook since kill processing may not
5719         ;; work with hidden articles.
5720         (and gnus-show-threads
5721              gnus-thread-hide-subtree
5722              (gnus-summary-hide-all-threads))
5723         ;; Show first unread article if requested.
5724         (goto-char (point-min))
5725         (if (and (not no-article)
5726                  gnus-auto-select-first
5727                  (gnus-summary-first-unread-article))
5728             ()
5729           (gnus-configure-windows 'summary))
5730         (gnus-set-mode-line 'summary)
5731         (gnus-summary-position-cursor)
5732         ;; If in async mode, we send some info to the backend.
5733         (and gnus-newsgroup-async
5734              (setq gnus-newsgroup-threads (nreverse gnus-newsgroup-threads))
5735              (gnus-request-asynchronous 
5736               gnus-newsgroup-name
5737               (if (and gnus-asynchronous-article-function
5738                        (fboundp gnus-asynchronous-article-function))
5739                   (funcall gnus-asynchronous-article-function
5740                            gnus-newsgroup-threads)
5741                 gnus-newsgroup-threads)))
5742         (gnus-kill-buffer kill-buffer)
5743         (if (not (get-buffer-window gnus-group-buffer))
5744             ()
5745           ;; gotta use windows, because recenter does wierd stuff if
5746           ;; the current buffer ain't the displayed window.
5747           (let ((owin (selected-window))) 
5748             (select-window (get-buffer-window gnus-group-buffer))
5749             (and (gnus-group-goto-group group)
5750                  (recenter))
5751             (select-window owin))))
5752       t))))
5753
5754 (defun gnus-summary-prepare ()
5755   ;; Generate the summary buffer.
5756   (let ((buffer-read-only nil))
5757     (erase-buffer)
5758     (gnus-summary-prepare-threads 
5759      (if gnus-show-threads
5760          (gnus-gather-threads 
5761           (gnus-sort-threads 
5762            (if (and gnus-summary-expunge-below
5763                     (not gnus-fetch-old-headers))
5764                (gnus-make-threads-and-expunge)
5765              (gnus-make-threads))))
5766        gnus-newsgroup-headers)
5767      'cull)
5768     ;; Erase header retrieval message.
5769     (gnus-summary-update-lines)
5770     (message "")
5771     ;; Remove the final newline.
5772     ;;(goto-char (point-max))
5773     ;;(delete-char -1)
5774     ;; Call hooks for modifying summary buffer.
5775     ;; Suggested by sven@tde.LTH.Se (Sven Mattisson).
5776     (goto-char (point-min))
5777     (run-hooks 'gnus-summary-prepare-hook)))
5778
5779 (defun gnus-gather-threads (threads)
5780   "Gather threads that have lost their roots."
5781   (if (not gnus-summary-make-false-root)
5782       threads 
5783     (let ((hashtb (gnus-make-hashtable 1023))
5784           (prev threads)
5785           (result threads)
5786           subject hthread whole-subject)
5787       (while threads
5788         (setq whole-subject 
5789               (setq subject (header-subject (car (car threads)))))
5790         (if gnus-summary-gather-subject-limit
5791             (or (and (numberp gnus-summary-gather-subject-limit)
5792                      (> (length subject) gnus-summary-gather-subject-limit)
5793                      (setq subject
5794                            (substring subject 0 
5795                                       gnus-summary-gather-subject-limit)))
5796                 (and (eq 'fuzzy gnus-summary-gather-subject-limit)
5797                      (setq subject (gnus-simplify-subject-fuzzy subject))))
5798           (setq subject (gnus-simplify-subject-re subject)))
5799         (if (setq hthread 
5800                   (gnus-gethash subject hashtb))
5801             (progn
5802               (or (stringp (car (car hthread)))
5803                   (setcar hthread (list whole-subject (car hthread))))
5804               (setcdr (car hthread) (nconc (cdr (car hthread)) 
5805                                            (list (car threads))))
5806               (setcdr prev (cdr threads))
5807               (setq threads prev))
5808           (gnus-sethash subject threads hashtb))
5809         (setq prev threads)
5810         (setq threads (cdr threads)))
5811       result)))
5812
5813 (defun gnus-make-threads ()
5814   ;; This function takes the dependencies already made by 
5815   ;; `gnus-get-newsgroup-headers' and builds the trees. First we go
5816   ;; through the dependecies in the hash table and finds all the
5817   ;; roots. Roots do not refer back to any valid articles.
5818   (let (roots)
5819     (and gnus-fetch-old-headers (eq gnus-headers-retrieved-by 'nov)
5820          (gnus-build-old-threads))
5821     (mapatoms
5822      (lambda (refs)
5823        (if (not (car (symbol-value refs)))
5824            (setq roots (append (cdr (symbol-value refs)) roots))
5825          ;; Ok, these refer back to valid articles, but if
5826          ;; `gnus-thread-ignore-subject' is nil, we have to check that
5827          ;; the root has the same subject as its children. The children
5828          ;; that do not are made into roots and removed from the list
5829          ;; of children. 
5830          (or gnus-thread-ignore-subject
5831              (let* ((prev (symbol-value refs))
5832                     (subject (gnus-simplify-subject-re 
5833                               (header-subject (car prev))))
5834                     (headers (cdr prev)))
5835                (while headers
5836                  (if (not (string= subject
5837                                    (gnus-simplify-subject-re 
5838                                     (header-subject (car headers)))))
5839                      (progn
5840                        (setq roots (cons (car headers) roots))
5841                        (setcdr prev (cdr headers)))
5842                    (setq prev headers))
5843                  (setq headers (cdr headers)))))))
5844      gnus-newsgroup-dependencies)
5845     
5846     (mapcar 'gnus-trim-thread
5847             (apply 'append
5848                    (mapcar 'gnus-cut-thread
5849                            (mapcar 'gnus-make-sub-thread roots))))))
5850   
5851 (defun gnus-make-threads-and-expunge ()
5852   ;; This function takes the dependencies already made by 
5853   ;; `gnus-get-newsgroup-headers' and builds the trees. First we go
5854   ;; through the dependecies in the hash table and finds all the
5855   ;; roots. Roots do not refer back to any valid articles.
5856   (let ((default (or gnus-summary-default-score 0))
5857         (below gnus-summary-expunge-below)
5858         roots article)
5859     (and gnus-fetch-old-headers (eq gnus-headers-retrieved-by 'nov)
5860          (gnus-build-old-threads))
5861     (mapatoms
5862      (lambda (refs)
5863        (if (not (car (symbol-value refs)))
5864            ;; These articles do not refer back to any other articles -
5865            ;; they are roots.
5866            (let ((headers (cdr (symbol-value refs))))
5867              ;; We weed out the low-scored articles.
5868              (while headers
5869                (if (not (< (or (cdr (assq (header-number (car headers))
5870                                           gnus-newsgroup-scored)) default)
5871                            below))
5872                    ;; It is over.
5873                    (setq roots (cons (car headers) roots))
5874                  ;; It is below, so we mark it as read.
5875                  (setq gnus-newsgroup-unreads
5876                        (delq (header-number (car headers))
5877                              gnus-newsgroup-unreads)))
5878                (setq headers (cdr headers))))
5879          ;; Ok, these refer back to valid articles, but if
5880          ;; `gnus-thread-ignore-subject' is nil, we have to check that
5881          ;; the root has the same subject as its children. The children
5882          ;; that do not are made into roots and removed from the list
5883          ;; of children. 
5884          (or gnus-thread-ignore-subject
5885              (let* ((prev (symbol-value refs))
5886                     (subject (gnus-simplify-subject-re 
5887                               (header-subject (car prev))))
5888                     (headers (cdr prev)))
5889                (while headers
5890                  (if (not (string= subject
5891                                    (gnus-simplify-subject-re 
5892                                     (header-subject (car headers)))))
5893                      (progn
5894                        (if (not (< (or (cdr (assq (header-number (car headers))
5895                                                   gnus-newsgroup-scored))
5896                                        default) below))
5897                            (setq roots (cons (car headers) roots))
5898                          (setq gnus-newsgroup-unreads
5899                                (delq (header-number (car headers))
5900                                      gnus-newsgroup-unreads)))
5901                        (setcdr prev (cdr headers)))
5902                    (setq prev headers))
5903                  (setq headers (cdr headers)))))
5904          ;; If this article is expunged, some of the children might be
5905          ;; roots.  
5906          (if (< (or (cdr (assq (header-number (car (symbol-value refs)))
5907                                gnus-newsgroup-scored)) default)
5908                 below)
5909              (let* ((prev (symbol-value refs))
5910                     (headers (cdr prev)))
5911                (while headers
5912                  (setq article (header-number (car headers)))
5913                  (if (not (< (or (cdr (assq article gnus-newsgroup-scored))
5914                                  default) below))
5915                      (progn (setq roots (cons (car headers) roots))
5916                             (setq prev headers))
5917                    (setq gnus-newsgroup-unreads 
5918                          (delq article gnus-newsgroup-unreads))
5919                    (setcdr prev (cdr headers)))
5920                  (setq headers (cdr headers))))
5921            ;; It was not expunged, but we look at expunged children.
5922            (let* ((prev (symbol-value refs))
5923                   (headers (cdr prev))
5924                   article)
5925              (while headers
5926                (setq article (header-number (car headers)))
5927                (if (not (< (or (cdr (assq article gnus-newsgroup-scored))
5928                                default) below))
5929                    (setq prev headers)
5930                  (setq gnus-newsgroup-unreads 
5931                        (delq article gnus-newsgroup-unreads))
5932                  (setcdr prev (cdr headers)))
5933                (setq headers (cdr headers)))))))
5934      gnus-newsgroup-dependencies)
5935
5936     (mapcar 'gnus-trim-thread
5937             (apply 'append
5938                    (mapcar 'gnus-cut-thread
5939                            (mapcar 'gnus-make-sub-thread roots))))))
5940   
5941 (defun gnus-cut-thread (thread)
5942   ;; Remove leaf dormant or ancient articles from THREAD.
5943   (let ((head (car thread))
5944         (tail (apply 'append (mapcar 'gnus-cut-thread (cdr thread)))))
5945     (if (and (null tail)
5946              (let ((number (header-number head)))
5947                (or (memq number gnus-newsgroup-ancient)
5948                    (memq number gnus-newsgroup-dormant)
5949                    (and gnus-summary-expunge-below
5950                         (eq gnus-fetch-old-headers 'some)
5951                         (< (or (cdr (assq number gnus-newsgroup-scored))
5952                                gnus-summary-default-score 0)
5953                            gnus-summary-expunge-below)
5954                         (progn
5955                           (setq gnus-newsgroup-unreads
5956                                 (delq number gnus-newsgroup-unreads))
5957                           t)))))
5958         nil
5959       (list (cons head tail)))))
5960
5961 (defun gnus-trim-thread (thread)
5962   ;; Remove root ancient articles with only one child from THREAD.
5963   (if (and (eq gnus-fetch-old-headers 'some)
5964            (memq (header-number (car thread)) gnus-newsgroup-ancient)
5965            (= (length thread) 2))
5966       (gnus-trim-thread (nth 1 thread))
5967     thread))
5968
5969 (defun gnus-make-sub-thread (root)
5970   ;; This function makes a sub-tree for a node in the tree.
5971   (let ((children (reverse (cdr (gnus-gethash (downcase (header-id root))
5972                                               gnus-newsgroup-dependencies)))))
5973     (cons root (mapcar 'gnus-make-sub-thread children))))
5974
5975 (defun gnus-build-old-threads ()
5976   ;; Look at all the articles that refer back to old articles, and
5977   ;; fetch the headers for the articles that aren't there. This will
5978   ;; build complete threads - if the roots haven't been expired by the
5979   ;; server, that is.
5980   (let (id heads)
5981     (mapatoms
5982      (lambda (refs)
5983        (if (not (car (symbol-value refs)))
5984            (progn
5985              (setq heads (cdr (symbol-value refs)))
5986              (while heads
5987                (if (not (memq (header-number (car heads))
5988                               gnus-newsgroup-dormant))
5989                    (progn
5990                      (setq id (symbol-name refs))
5991                      (while (and (setq id (gnus-build-get-header id))
5992                                  (not (car (gnus-gethash 
5993                                             id gnus-newsgroup-dependencies)))))
5994                      (setq heads nil))
5995                  (setq heads (cdr heads)))))))
5996      gnus-newsgroup-dependencies)))
5997
5998 (defun gnus-build-get-header (id)
5999   ;; Look through the buffer of NOV lines and find the header to
6000   ;; ID. Enter this line into the dependencies hash table, and return
6001   ;; the id of the parent article (if any).
6002   (let ((deps gnus-newsgroup-dependencies)
6003         found header)
6004     (prog1
6005         (save-excursion
6006           (set-buffer nntp-server-buffer)
6007           (goto-char (point-min))
6008           (while (and (not found) (search-forward id nil t))
6009             (beginning-of-line)
6010             (setq found (looking-at 
6011                          (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
6012                                  (regexp-quote id))))
6013             (or found (beginning-of-line 2)))
6014           (if found
6015               (let (ref)
6016                 (beginning-of-line)
6017                 (and
6018                  (setq header (gnus-nov-parse-line 
6019                                (read (current-buffer)) deps))
6020                  (setq ref (header-references header))
6021                  (string-match "\\(<[^>]+>\\) *$" ref)
6022                  (substring ref (match-beginning 1) (match-end 1))))))
6023       (and header
6024            (setq gnus-newsgroup-headers (cons header gnus-newsgroup-headers)
6025                  gnus-newsgroup-ancient (cons (header-number header)
6026                                               gnus-newsgroup-ancient))))))
6027
6028 ;; Re-build the thread containing ID.
6029 (defun gnus-rebuild-thread (id)
6030   (let ((dep gnus-newsgroup-dependencies)
6031         (buffer-read-only nil)
6032         parent headers refs thread art)
6033     (while (and id (setq headers
6034                          (car (setq art (gnus-gethash (downcase id) dep)))))
6035       (setq parent art)
6036       (setq id (and (setq refs (header-references headers))
6037                     (string-match "\\(<[^>]+>\\) *$" refs)
6038                     (substring refs (match-beginning 1) (match-end 1)))))
6039     (setq thread (gnus-make-sub-thread (car parent)))
6040     (gnus-rebuild-remove-articles thread)
6041     (let ((beg (point)))
6042       (gnus-summary-prepare-threads (list thread))
6043       (gnus-summary-update-lines beg (point)))))
6044
6045 ;; Delete all lines in the summary buffer that correspond to articles
6046 ;; in this thread.
6047 (defun gnus-rebuild-remove-articles (thread)
6048   (and (gnus-summary-goto-subject (header-number (car thread)))
6049        (gnus-delete-line))
6050   (mapcar (lambda (th) (gnus-rebuild-remove-articles th)) (cdr thread)))
6051
6052 (defun gnus-sort-threads (threads)
6053   ;; Sort threads as specified in `gnus-thread-sort-functions'.
6054   (let ((fun gnus-thread-sort-functions))
6055     (while fun
6056       (setq threads (sort threads (car fun))
6057             fun (cdr fun))))
6058   threads)
6059
6060 (defmacro gnus-thread-header-old (thread)
6061   (or (symbolp thread) (error "Boo!"))
6062   (` (if (consp (, thread))
6063          (if (stringp (car (, thread)))
6064              (car (car (cdr (, thread))))
6065            (car (, thread)))
6066        (, thread))))
6067
6068 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
6069 (defmacro gnus-thread-header (thread)
6070   ;; Return header of first article in THREAD.
6071   ;; Note that THREAD must never, evr be anything else than a variable -
6072   ;; using some other form will lead to serious barfage.
6073   (or (symbolp thread) (error "Boo!"))
6074   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
6075         (vector thread) 1))
6076
6077 (defun gnus-thread-sort-by-number (h1 h2)
6078   "Sort threads by root article number."
6079   (< (header-number (gnus-thread-header h1))
6080      (header-number (gnus-thread-header h2))))
6081
6082 (defun gnus-thread-sort-by-author (h1 h2)
6083   "Sort threads by root author."
6084   (string-lessp
6085    (let ((extract (funcall 
6086                    gnus-extract-address-components
6087                    (header-from (gnus-thread-header h1)))))
6088      (or (car extract) (cdr extract)))
6089    (let ((extract (funcall
6090                    gnus-extract-address-components 
6091                    (header-from (gnus-thread-header h2)))))
6092      (or (car extract) (cdr extract)))))
6093
6094 (defun gnus-thread-sort-by-subject (h1 h2)
6095   "Sort threads by root subject."
6096   (string-lessp
6097    (downcase (gnus-simplify-subject 
6098               (header-subject (gnus-thread-header h1))))
6099    (downcase (gnus-simplify-subject 
6100               (header-subject (gnus-thread-header h2))))))
6101
6102 (defun gnus-thread-sort-by-date (h1 h2)
6103   "Sort threads by root article date."
6104   (string-lessp
6105    (gnus-sortable-date (header-date (gnus-thread-header h1)))
6106    (gnus-sortable-date (header-date (gnus-thread-header h2)))))
6107
6108 (defun gnus-thread-sort-by-score (h1 h2)
6109   "Sort threads by root article score.
6110 Unscored articles will be counted as having a score of zero."
6111   (let ((s1 (assq (header-number (gnus-thread-header h1))
6112                   gnus-newsgroup-scored))
6113         (s2 (assq (header-number (gnus-thread-header h2))
6114                   gnus-newsgroup-scored)))
6115     (> (or (cdr s1) gnus-summary-default-score 0)
6116        (or (cdr s2) gnus-summary-default-score 0))))
6117
6118 (defun gnus-thread-sort-by-total-score (h1 h2)
6119   "Sort threads by the sum of all scores in the thread.
6120 Unscored articles will be counted as having a score of zero."
6121   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
6122
6123 (defun gnus-thread-total-score (thread)
6124   ;;  This function find the total score of THREAD.
6125   (if (consp thread)
6126       (if (stringp (car thread))
6127           (apply gnus-thread-score-function 0
6128                  (mapcar 'gnus-thread-total-score-1 (cdr thread)))
6129         (gnus-thread-total-score-1 thread))
6130     (gnus-thread-total-score-1 (list thread))))
6131
6132 (defun gnus-thread-total-score-1 (root)
6133   ;; This function find the total score of the thread below ROOT.
6134   (setq root (car root))
6135   (apply gnus-thread-score-function
6136          (or (cdr (assq (header-number root) gnus-newsgroup-scored))
6137              gnus-summary-default-score 0)
6138          (mapcar 'gnus-thread-total-score
6139                  (cdr (gnus-gethash (downcase (header-id root))
6140                                     gnus-newsgroup-dependencies)))))
6141
6142 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
6143 (defvar gnus-tmp-prev-subject "")
6144
6145 (defun gnus-summary-prepare-threads (threads &optional cull)
6146   "Prepare summary buffer from THREADS and indentation LEVEL.  
6147 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'  
6148 or a straight list of headers."
6149   (let ((level 0)
6150         thread header number subject stack state gnus-tmp-gathered)
6151     (if (vectorp (car threads))
6152         ;; If this is a straight (sic) list of headers, then a
6153         ;; threaded summary display isn't required, so we just create
6154         ;; an unthreaded one.
6155         (gnus-summary-prepare-unthreaded threads)
6156
6157       ;; Do the threaded display.
6158
6159       (while (or threads stack)
6160         
6161         (if threads
6162             ;; If there are some threads, we do them before the
6163             ;; threads on the stack.
6164             (setq thread threads
6165                   header (car (car thread)))
6166           ;; There were no current threads, so we pop something off
6167           ;; the stack. 
6168           (setq state (car stack)
6169                 level (car state)
6170                 thread (cdr state)
6171                 stack (cdr stack)
6172                 header (car (car thread))))
6173
6174         (if (stringp header)
6175             (progn
6176               ;; The header is a dummy root.
6177               (cond 
6178                ((eq gnus-summary-make-false-root 'adopt)
6179                 ;; We let the first article adopt the rest.
6180                 (let ((th (car (cdr (car thread)))))
6181                   (while (cdr th)
6182                     (setq th (cdr th)))
6183                   (setcdr th (cdr (cdr (car thread))))
6184                   (setq gnus-tmp-gathered 
6185                         (nconc (mapcar
6186                                 (lambda (h) (header-number (car h)))
6187                                 (cdr (cdr (car thread))))
6188                                gnus-tmp-gathered))
6189                   (setcdr (cdr (car thread)) nil))
6190                 (setq level -1))
6191                ((eq gnus-summary-make-false-root 'empty)
6192                 ;; We print adopted articles with empty subject fields.
6193                 (setq gnus-tmp-gathered 
6194                       (nconc (mapcar
6195                               (lambda (h) (header-number (car h)))
6196                               (cdr (cdr (car thread))))
6197                              gnus-tmp-gathered))
6198                 (setq level -1))
6199                ((eq gnus-summary-make-false-root 'dummy)
6200                 ;; We output a dummy root.
6201                 (gnus-summary-insert-dummy-line 
6202                  nil header (header-number (car (car (cdr (car thread)))))))
6203                (t
6204                 ;; We do not make a root for the gathered
6205                 ;; sub-threads at all.  
6206                 (setq level -1))))
6207       
6208           (setq number (header-number header)
6209                 subject (header-subject header))
6210
6211           ;; Do the async thing.
6212           (and gnus-newsgroup-async
6213                (setq gnus-newsgroup-threads
6214                      (cons (cons number (header-lines header)) 
6215                            gnus-newsgroup-threads)))
6216
6217           ;; We may have to root out some bad articles...
6218           (and cull
6219                (= level 0)
6220                (cond ((memq (setq number (header-number header))
6221                             gnus-newsgroup-dormant)
6222                       (setq header nil))
6223                      ((and gnus-summary-expunge-below
6224                            (< (or (cdr (assq number gnus-newsgroup-scored))
6225                                   gnus-summary-default-score 0)
6226                               gnus-summary-expunge-below))
6227                       (setq header nil)
6228                       (setq gnus-newsgroup-unreads 
6229                             (delq number gnus-newsgroup-unreads)))))
6230
6231           (and
6232            header
6233            (progn
6234              (gnus-summary-insert-line
6235               nil header level nil 
6236               (cond 
6237                ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
6238                ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
6239                ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
6240                ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
6241                (t gnus-ancient-mark))
6242               (memq number gnus-newsgroup-replied)
6243               (memq number gnus-newsgroup-expirable)
6244               (if (and (eq gnus-summary-make-false-root 'empty)
6245                        (memq number gnus-tmp-gathered))
6246                   gnus-summary-same-subject
6247                 (if (or (zerop level)
6248                         (and gnus-thread-ignore-subject
6249                              (not (string= 
6250                                    (gnus-simplify-subject-re
6251                                     gnus-tmp-prev-subject)
6252                                    (gnus-simplify-subject-re
6253                                     subject)))))
6254                     subject
6255                   gnus-summary-same-subject))
6256               (and (eq gnus-summary-make-false-root 'adopt)
6257                    (memq number gnus-tmp-gathered))
6258               (cdr (assq number gnus-newsgroup-scored)))
6259
6260              (setq gnus-tmp-prev-subject subject))))
6261
6262         (if (nth 1 thread) 
6263             (setq stack (cons (cons (max 0 level) (nthcdr 1 thread)) stack)))
6264         (setq level (1+ level))
6265         (setq threads (cdr (car thread)))))))
6266
6267
6268 (defun gnus-summary-prepare-unthreaded (headers)
6269   (let (header number)
6270
6271     ;; Do the async thing, if that is required.
6272     (if gnus-newsgroup-async
6273         (setq gnus-newsgroup-threads
6274               (mapcar (lambda (h) (cons (header-number h) (header-lines h)))
6275                       headers)))
6276
6277     (while headers
6278       (setq header (car headers)
6279             headers (cdr headers)
6280             number (header-number header))
6281
6282       (gnus-summary-insert-line
6283        nil header 0 nil 
6284        (cond ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
6285              ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
6286              ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
6287              ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
6288              (t gnus-ancient-mark))
6289        (memq number gnus-newsgroup-replied)
6290        (memq number gnus-newsgroup-expirable)
6291        (header-subject header) nil
6292        (cdr (assq number gnus-newsgroup-scored))))))
6293
6294 (defun gnus-select-newsgroup (group &optional read-all)
6295   "Select newsgroup GROUP.
6296 If READ-ALL is non-nil, all articles in the group are selected."
6297   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
6298          (info (nth 2 entry))
6299          articles)
6300     (gnus-check-news-server
6301      (setq gnus-current-select-method (gnus-find-method-for-group group)))
6302
6303     (or (gnus-server-opened gnus-current-select-method)
6304         (gnus-open-server gnus-current-select-method)
6305         (error "Couldn't open server"))
6306     
6307     (or (and (eq (car entry) t)
6308              (gnus-activate-newsgroup (car info)))
6309         (gnus-request-group group t)
6310         (progn
6311           (kill-buffer (current-buffer))
6312           (error "Couldn't request group %s: %s" 
6313                  group (gnus-status-message group))))
6314
6315     (setq gnus-newsgroup-name group)
6316     (setq gnus-newsgroup-unselected nil)
6317     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
6318
6319     (and gnus-asynchronous
6320          (gnus-check-backend-function 
6321           'request-asynchronous gnus-newsgroup-name)
6322          (setq gnus-newsgroup-async
6323                (gnus-request-asynchronous gnus-newsgroup-name)))
6324
6325     (setq articles (gnus-articles-to-read group read-all))
6326
6327     (cond 
6328      ((null articles) 
6329       (gnus-message 3 "Couldn't select newsgroup")
6330       'quit)
6331      ((eq articles 0) nil)
6332      (t
6333       ;; Init the dependencies hash table.
6334       (setq gnus-newsgroup-dependencies 
6335             (gnus-make-hashtable (length articles)))
6336       ;; Retrieve the headers and read them in.
6337       (setq gnus-newsgroup-headers 
6338             (if (eq 'nov (setq gnus-headers-retrieved-by
6339                                (gnus-retrieve-headers 
6340                                 (if (and gnus-fetch-old-headers 
6341                                          (not (eq 1 (car articles))))
6342                                     (cons 1 articles)
6343                                   articles)
6344                                 gnus-newsgroup-name)))
6345                 (progn
6346                   (gnus-get-newsgroup-headers-xover articles))
6347               ;; If we were to fetch old headers, but the backend didn't
6348               ;; support XOVER, then it is possible we fetched one article
6349               ;; that we shouldn't have. If that's the case, we pop it off the
6350               ;; list of headers.
6351               (if (not gnus-fetch-old-headers)
6352                   ()
6353                 (save-excursion
6354                   (set-buffer nntp-server-buffer)
6355                   (goto-char (point-min))
6356                   (and (looking-at "[0-9]+[ \t]+1[ \t]")
6357                        (delete-region 
6358                         (point) 
6359                         (search-forward "\n.\n" nil t)))))
6360               (gnus-get-newsgroup-headers)))
6361       ;; Remove canceled articles from the list of unread articles.
6362       (setq gnus-newsgroup-unreads
6363             (gnus-set-sorted-intersection 
6364              gnus-newsgroup-unreads
6365              (mapcar (lambda (headers) (header-number headers))
6366                      gnus-newsgroup-headers)))
6367       ;; Adjust and set lists of article marks.
6368       (and info
6369            (let (marked)
6370              (gnus-adjust-marked-articles info)
6371              (setq gnus-newsgroup-marked 
6372                    (copy-sequence
6373                     (cdr (assq 'tick (setq marked (nth 3 info))))))
6374              (setq gnus-newsgroup-replied 
6375                    (copy-sequence (cdr (assq 'reply marked))))
6376              (setq gnus-newsgroup-expirable
6377                    (copy-sequence (cdr (assq 'expire marked))))
6378              (setq gnus-newsgroup-killed
6379                    (copy-sequence (cdr (assq 'killed marked))))
6380              (setq gnus-newsgroup-bookmarks 
6381                    (copy-sequence (cdr (assq 'bookmark marked))))
6382              (setq gnus-newsgroup-dormant 
6383                    (copy-sequence (cdr (assq 'dormant marked))))
6384              (setq gnus-newsgroup-scored 
6385                    (copy-sequence (cdr (assq 'score marked))))
6386              (setq gnus-newsgroup-processable nil)))
6387       ;; Check whether auto-expire is to be done in this group.
6388       (setq gnus-newsgroup-auto-expire
6389             (or (and (stringp gnus-auto-expirable-newsgroups)
6390                      (string-match gnus-auto-expirable-newsgroups group))
6391                 (memq 'auto-expire (nth 5 info))))
6392       ;; First and last article in this newsgroup.
6393       (and gnus-newsgroup-headers
6394            (setq gnus-newsgroup-begin 
6395                  (header-number (car gnus-newsgroup-headers)))
6396            (setq gnus-newsgroup-end
6397                  (header-number (gnus-last-element gnus-newsgroup-headers))))
6398       (setq gnus-reffed-article-number -1)
6399       ;; GROUP is successfully selected.
6400       (or gnus-newsgroup-headers t)))))
6401
6402 (defun gnus-articles-to-read (group read-all)
6403   ;; Find out what articles the user wants to read.
6404   (let* ((articles
6405           ;; Select all articles if `read-all' is non-nil, or if all the
6406           ;; unread articles are dormant articles.
6407           (if (or (and read-all (not (numberp read-all)))
6408                   (= (length gnus-newsgroup-unreads) 
6409                      (length gnus-newsgroup-dormant)))
6410               (gnus-uncompress-range 
6411                (gnus-gethash group gnus-active-hashtb))
6412             gnus-newsgroup-unreads))
6413          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
6414          (scored (length scored-list))
6415          (number (length articles))
6416          (marked (+ (length gnus-newsgroup-marked)
6417                     (length gnus-newsgroup-dormant)))
6418          (select
6419           (cond 
6420            ((numberp read-all)
6421             read-all)
6422            (t
6423             (condition-case ()
6424                 (cond ((and (or (<= scored marked)
6425                                 (= scored number))
6426                             (numberp gnus-large-newsgroup)
6427                             (> number gnus-large-newsgroup))
6428                        (let ((input
6429                               (read-string
6430                                (format
6431                                 "How many articles from %s (default %d): "
6432                                 gnus-newsgroup-name number))))
6433                          (if (string-match "^[ \t]*$" input)
6434                              number input)))
6435                       ((and (> scored marked) (< scored number))
6436                        (let ((input
6437                               (read-string
6438                                (format 
6439                                 "%s %s (%d scored, %d total): "
6440                                 "How many articles from"
6441                                 group scored number))))
6442                          (if (string-match "^[ \t]*$" input)
6443                              number input)))
6444                       (t number))
6445               (quit nil))))))
6446     (setq select (if (stringp select) (string-to-number select) select))
6447     (if (or (null select) (zerop select))
6448         select
6449       (if (and (not (zerop scored)) (<= (abs select) scored))
6450           (progn
6451             (setq articles (sort scored-list '<))
6452             (setq number (length articles)))
6453         (setq articles (copy-sequence articles)))
6454
6455       (if (< (abs select) number)
6456           (if (< select 0) 
6457               ;; Select the N oldest articles.
6458               (setcdr (nthcdr (1- (abs select)) articles) nil)
6459             ;; Select the N most recent articles.
6460             (setq articles (nthcdr (- number select) articles))))
6461       (setq gnus-newsgroup-unselected
6462             (gnus-sorted-intersection
6463              gnus-newsgroup-unreads
6464              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
6465       articles)))
6466
6467 (defun gnus-killed-articles (killed articles)
6468   (let (out)
6469     (while articles
6470       (if (inline (gnus-member-of-range (car articles) killed))
6471           (setq out (cons (car articles) out)))
6472       (setq articles (cdr articles)))
6473     out))
6474
6475 (defun gnus-adjust-marked-articles (info &optional active)
6476   "Remove all marked articles that are no longer legal."
6477   (let ((marked-lists (nth 3 info))
6478         (active (or active (gnus-gethash (car info) gnus-active-hashtb)))
6479         m prev)
6480     ;; There are many types of marked articles.
6481     (while marked-lists
6482       (setq m (cdr (setq prev (car marked-lists))))
6483       (cond ((or (eq 'tick (car prev)) (eq 'dormant (car prev)))
6484              ;; Make sure that all ticked articles are a subset of the
6485              ;; unread/unselected articles.
6486              (while m
6487                (if (or (memq (car m) gnus-newsgroup-unreads)
6488                        (memq (car m) gnus-newsgroup-unselected))
6489                    (setq prev m)
6490                  (setcdr prev (cdr m)))
6491                (setq m (cdr m))))
6492             ((eq 'score (car prev))
6493              ;; Scored articles should be a subset of
6494              ;; unread/unselected articles. 
6495              (while m
6496                (if (or (memq (car (car m)) gnus-newsgroup-unreads)
6497                        (memq (car (car m)) gnus-newsgroup-unreads))
6498                    (setq prev m)
6499                  (setcdr prev (cdr m)))
6500                (setq m (cdr m))))
6501             ((eq 'bookmark (car prev))
6502              ;; Bookmarks should be a subset of active articles.
6503              (while m
6504                (if (< (car (car m)) (car active))
6505                    (setcdr prev (cdr m))
6506                  (setq prev m))
6507                (setq m (cdr m))))
6508             ((eq 'killed (car prev))
6509              ;; Articles that have been through the kill process are
6510              ;; to be a subset of active articles.
6511              (while (and m (< (or (and (numberp (car m)) (car m))
6512                                   (cdr (car m)))
6513                               (car active)))
6514                (setcdr prev (cdr m))
6515                (setq m (cdr m)))
6516              (if (and m (< (or (and (numberp (car m)) (car m))
6517                                (car (car m)))
6518                            (car active))) 
6519                  (setcar (if (numberp (car m)) m (car m)) (car active))))
6520             ((or (eq 'reply (car prev)) (eq 'expire (car prev)))
6521              ;; The replied and expirable articles have to be articles
6522              ;; that are active. 
6523              (while m
6524                (if (< (car m) (car active))
6525                    (setcdr prev (cdr m))
6526                  (setq prev m))
6527                (setq m (cdr m)))))
6528       (setq marked-lists (cdr marked-lists)))
6529     ;; Remove all lists that are empty.
6530     (setq marked-lists (nth 3 info))
6531     (if marked-lists
6532         (progn
6533           (while (= 1 (length (car marked-lists)))
6534             (setq marked-lists (cdr marked-lists)))
6535           (setq m (cdr (setq prev marked-lists)))
6536           (while m
6537             (if (= 1 (length (car m)))
6538                 (setcdr prev (cdr m))
6539               (setq prev m))
6540             (setq m (cdr m)))
6541           (setcar (nthcdr 3 info) marked-lists)))
6542     ;; Finally, if there are no marked lists at all left, and if there
6543     ;; are no elements after the lists in the info list, we just chop
6544     ;; the info list off before the marked lists.
6545     (and (null marked-lists) 
6546          (not (nthcdr 4 info))
6547          (setcdr (nthcdr 2 info) nil)))
6548   info)
6549
6550 (defun gnus-set-marked-articles 
6551   (info ticked replied expirable killed dormant bookmark score) 
6552   "Enter the various lists of marked articles into the newsgroup info list."
6553   (let (newmarked)
6554     (and ticked (setq newmarked (cons (cons 'tick ticked) nil)))
6555     (and replied (setq newmarked (cons (cons 'reply replied) newmarked)))
6556     (and expirable (setq newmarked (cons (cons 'expire expirable) 
6557                                          newmarked)))
6558     (and killed (setq newmarked (cons (cons 'killed killed) newmarked)))
6559     (and dormant (setq newmarked (cons (cons 'dormant dormant) newmarked)))
6560     (and bookmark (setq newmarked (cons (cons 'bookmark bookmark) 
6561                                         newmarked)))
6562     (and score (setq newmarked (cons (cons 'score score) newmarked)))
6563     (if (nthcdr 3 info)
6564         (progn
6565           (setcar (nthcdr 3 info) newmarked)
6566           (and (not newmarked)
6567                (not (nthcdr 4 info))
6568                (setcdr (nthcdr 2 info) nil)))
6569       (if newmarked
6570           (setcdr (nthcdr 2 info) (list newmarked))))))
6571
6572 (defun gnus-add-marked-articles (group type articles &optional info force)
6573   ;; Add ARTICLES of TYPE to the info of GROUP.
6574   ;; If INFO is non-nil, use that info. If FORCE is non-nil, don't
6575   ;; add, but replace marked articles of TYPE with ARTICLES.
6576   (let ((info (or info (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
6577         marked m)
6578     (or (not info)
6579         (and (not (setq marked (nthcdr 3 info)))
6580              (setcdr (nthcdr 2 info) (list (list (cons type articles)))))
6581         (and (not (setq m (assq type (car marked))))
6582              (setcar marked (cons (cons type articles) (car marked))))
6583         (if force
6584             (setcdr m articles)
6585           (nconc m articles)))))
6586          
6587 (defun gnus-set-mode-line (where)
6588   "This function sets the mode line of the article or summary buffers.
6589 If WHERE is `summary', the summary mode line format will be used."
6590   (if (memq where gnus-updated-mode-lines)
6591       (let (mode-string)
6592         (save-excursion
6593           (set-buffer gnus-summary-buffer)
6594           (let* ((mformat (if (eq where 'article) 
6595                               gnus-article-mode-line-format-spec
6596                             gnus-summary-mode-line-format-spec))
6597                  (group-name gnus-newsgroup-name)
6598                  (article-number (or gnus-current-article 0))
6599                  (unread (- (length gnus-newsgroup-unreads)
6600                             (length gnus-newsgroup-dormant)))
6601                  (unread-and-unticked 
6602                   (- unread (length gnus-newsgroup-marked)))
6603                  (unselected (length gnus-newsgroup-unselected))
6604                  (unread-and-unselected
6605                   (cond ((and (zerop unread-and-unticked)
6606                               (zerop unselected)) "")
6607                         ((zerop unselected) 
6608                          (format "{%d more}" unread-and-unticked))
6609                         (t (format "{%d(+%d) more}"
6610                                    unread-and-unticked unselected))))
6611                  (subject
6612                   (if gnus-current-headers
6613                       (header-subject gnus-current-headers) ""))
6614                  (max-len (and gnus-mode-non-string-length
6615                                (- (frame-width) gnus-mode-non-string-length)))
6616                  header) ;; passed as argument to any user-format-funcs
6617             (setq mode-string (eval mformat))
6618             (or (numberp max-len)
6619                 (setq max-len (length mode-string)))
6620             (if (< max-len 4) (setq max-len 4))
6621             (if (> (length mode-string) max-len)
6622                 ;; modified by MORIOKA Tomohiko <morioka@jaist.ac.jp>
6623                 ;;  function `substring' might cut on a middle
6624                 ;;  of multi-octet character.
6625                 (setq mode-string 
6626                       (concat (gnus-truncate-string mode-string (- max-len 3))
6627                               "...")))
6628             (setq mode-string (format (format "%%-%ds" max-len)
6629                                       mode-string))))
6630         (setq mode-line-buffer-identification mode-string)
6631         (set-buffer-modified-p t))))
6632
6633 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
6634   "Go through the HEADERS list and add all Xrefs to a hash table.
6635 The resulting hash table is returned, or nil if no Xrefs were found."
6636   (let* ((from-method (gnus-find-method-for-group from-newsgroup))
6637          (prefix (if (and 
6638                       (gnus-group-foreign-p from-newsgroup)
6639                       (not (memq 'virtual 
6640                                  (assoc (symbol-name (car from-method))
6641                                         gnus-valid-select-methods))))
6642                      (gnus-group-real-prefix from-newsgroup)))
6643          (xref-hashtb (make-vector 63 0))
6644          start group entry number xrefs header)
6645     (while headers
6646       (setq header (car headers))
6647       (if (and (setq xrefs (header-xref header))
6648                (not (memq (header-number header) unreads)))
6649           (progn
6650             (setq start 0)
6651             (while (string-match "\\([^ ]+\\):\\([0-9]+\\)" xrefs start)
6652               (setq start (match-end 0))
6653               (setq group (concat prefix (substring xrefs (match-beginning 1) 
6654                                                     (match-end 1))))
6655               (setq number 
6656                     (string-to-int (substring xrefs (match-beginning 2) 
6657                                               (match-end 2))))
6658               (if (setq entry (gnus-gethash group xref-hashtb))
6659                   (setcdr entry (cons number (cdr entry)))
6660                 (gnus-sethash group (cons number nil) xref-hashtb)))))
6661       (setq headers (cdr headers)))
6662     (if start xref-hashtb nil)))
6663
6664 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads expirable)
6665   "Look through all the headers and mark the Xrefs as read."
6666   (let ((virtual (memq 'virtual 
6667                        (assoc (symbol-name (car (gnus-find-method-for-group 
6668                                                  from-newsgroup)))
6669                               gnus-valid-select-methods)))
6670         name entry info xref-hashtb idlist method
6671         nth4)
6672     (save-excursion
6673       (set-buffer gnus-group-buffer)
6674       (if (setq xref-hashtb 
6675                 (gnus-create-xref-hashtb from-newsgroup headers unreads))
6676           (mapatoms 
6677            (lambda (group)
6678              (if (string= from-newsgroup (setq name (symbol-name group)))
6679                  ()
6680                (setq idlist (symbol-value group))
6681                ;; Dead groups are not updated.
6682                (if (and (prog1 
6683                             (setq entry (gnus-gethash name gnus-newsrc-hashtb)
6684                                   info (nth 2 entry))
6685                           (if (stringp (setq nth4 (nth 4 info)))
6686                               (setq nth4 (gnus-server-to-method nth4))))
6687                         ;; Only do the xrefs if the group has the same
6688                         ;; select method as the group we have just read.
6689                         (or (gnus-methods-equal-p 
6690                              nth4 (gnus-find-method-for-group from-newsgroup))
6691                             virtual
6692                             (equal nth4 
6693                                    (setq method (gnus-find-method-for-group 
6694                                                  from-newsgroup)))
6695                             (and (equal (car nth4) (car method))
6696                                  (equal (nth 1 nth4) (nth 1 method))))
6697                         gnus-use-cross-reference
6698                         (or (not (eq gnus-use-cross-reference t))
6699                             virtual
6700                             ;; Only do cross-references on subscribed
6701                             ;; groups, if that is what is wanted.  
6702                             (<= (nth 1 info) gnus-level-subscribed)))
6703                    (gnus-group-make-articles-read name idlist expirable))))
6704            xref-hashtb)))))
6705
6706 (defun gnus-group-make-articles-read (group articles expirable)
6707   (let* ((num 0)
6708          (entry (gnus-gethash group gnus-newsrc-hashtb))
6709          (info (nth 2 entry))
6710          (active (gnus-gethash group gnus-active-hashtb))
6711          exps expirable range)
6712     ;; First peel off all illegal article numbers.
6713     (if active
6714         (let ((ids articles)
6715               (ticked (cdr (assq 'tick (nth 3 info))))
6716               (dormant (cdr (assq 'dormant (nth 3 info))))
6717               id)
6718           (setq exps nil)
6719           (while ids
6720             (setq id (car ids))
6721             (if (or (> id (cdr active))
6722                     (< id (car active))
6723                     (memq id ticked)
6724                     (memq id dormant))
6725                 (setq articles (delq id articles)))
6726             (and (memq id expirable)
6727                  (setq exps (cons id exps)))
6728             (setq ids (cdr ids)))))
6729     ;; Update expirable articles.
6730     (gnus-add-marked-articles nil 'expirable exps info)
6731     (and active
6732          (null (nth 2 info))
6733          (> (car active) 1)
6734          (setcar (nthcdr 2 info) (cons 1 (1- (car active)))))
6735     (setcar (nthcdr 2 info)
6736             (setq range
6737                   (gnus-add-to-range 
6738                    (nth 2 info) 
6739                    (setq articles (sort articles '<)))))
6740     ;; Then we have to re-compute how many unread
6741     ;; articles there are in this group.
6742     (if active
6743         (progn
6744           (cond 
6745            ((not range)
6746             (setq num (- (1+ (cdr active)) (car active))))
6747            ((not (listp (cdr range)))
6748             (setq num (- (cdr active) (- (1+ (cdr range)) 
6749                                          (car range)))))
6750            (t
6751             (while range
6752               (if (numberp (car range))
6753                   (setq num (1+ num))
6754                 (setq num (+ num (- (1+ (cdr (car range)))
6755                                     (car (car range))))))
6756               (setq range (cdr range)))
6757             (setq num (- (cdr active) num))))
6758           ;; Update the number of unread articles.
6759           (setcar 
6760            entry 
6761            (max 0 (- num 
6762                      (length (cdr (assq 'tick (nth 3 info))))
6763                      (length 
6764                       (cdr (assq 'dormant (nth 3 info)))))))
6765           ;; Update the group buffer.
6766           (gnus-group-update-group group t)))))
6767
6768 (defun gnus-methods-equal-p (m1 m2)
6769   (let ((m1 (or m1 gnus-select-method))
6770         (m2 (or m2 gnus-select-method)))
6771     (or (equal m1 m2)
6772         (and (eq (car m1) (car m2))
6773              (or (not (memq 'address (assoc (symbol-name (car m1))
6774                                             gnus-valid-select-methods)))
6775                  (equal (nth 1 m1) (nth 1 m2)))))))
6776
6777 (defsubst gnus-header-value ()
6778   (buffer-substring (match-end 0) (gnus-point-at-eol)))
6779
6780 (defvar gnus-newsgroup-none-id 0)
6781
6782 (defun gnus-get-newsgroup-headers ()
6783   (setq gnus-article-internal-prepare-hook nil)
6784   (let ((cur nntp-server-buffer)
6785         (dependencies gnus-newsgroup-dependencies)
6786         headers id dep end ref)
6787     (save-excursion
6788       (set-buffer nntp-server-buffer)
6789       (goto-char (point-min))
6790       ;; Search to the beginning of the next header. Error messages
6791       ;; do not begin with 2 or 3.
6792       (while (re-search-forward "^[23][0-9]+ " nil t)
6793         (let ((header (make-vector 9 nil))
6794               (case-fold-search t)
6795               (p (point))
6796               in-reply-to)
6797           (setq id nil
6798                 ref nil)
6799           (header-set-number header (read cur))
6800           ;; This implementation of this function, with nine
6801           ;; search-forwards instead of the one re-search-forward and
6802           ;; a case (which basically was the old function) is actually
6803           ;; about twice as fast, even though it looks messier. You
6804           ;; can't have everything, I guess. Speed and elegance
6805           ;; doesn't always come hand in hand.
6806           (save-restriction
6807             (narrow-to-region (point) (or (save-excursion 
6808                                             (search-forward "\n.\n" nil t))
6809                                           (point)))
6810             (if (search-forward "\nfrom: " nil t)
6811                 (header-set-from header (gnus-header-value))
6812               (header-set-from header "(nobody)"))
6813             (goto-char p)
6814             (if (search-forward "\nsubject: " nil t)
6815                 (header-set-subject header (gnus-header-value))
6816               (header-set-subject header "(none)"))
6817             (goto-char p)
6818             (and (search-forward "\nxref: " nil t)
6819                  (header-set-xref header (gnus-header-value)))
6820             (goto-char p)
6821             (or (numberp (and (search-forward "\nlines: " nil t)
6822                               (header-set-lines header (read cur))))
6823                 (header-set-lines header 0))
6824             (goto-char p)
6825             (and (search-forward "\ndate: " nil t)
6826                  (header-set-date header (gnus-header-value)))
6827             (goto-char p)
6828             (if (search-forward "\nmessage-id: " nil t)
6829                 (header-set-id header (setq id (gnus-header-value)))
6830               ;; If there was no message-id, we just fake one to make
6831               ;; subsequent routines simpler.
6832               (header-set-id 
6833                header 
6834                (setq id (concat "none+" 
6835                                 (int-to-string 
6836                                  (setq gnus-newsgroup-none-id 
6837                                        (1+ gnus-newsgroup-none-id)))))))
6838             (goto-char p)
6839             (if (search-forward "\nreferences: " nil t)
6840                 (progn
6841                   (header-set-references header (gnus-header-value))
6842                   (setq end (match-end 0))
6843                   (save-excursion
6844                     (setq ref 
6845                           (downcase
6846                            (buffer-substring
6847                             (progn 
6848                               (end-of-line)
6849                               (search-backward ">" end t)
6850                               (1+ (point)))
6851                             (progn
6852                               (search-backward "<" end t)
6853                               (point)))))))
6854               ;; Get the references from the in-reply-to header if there
6855               ;; ware no references and the in-reply-to header looks
6856               ;; promising. 
6857               (if (and (search-forward "\nin-reply-to: " nil t)
6858                        (setq in-reply-to (gnus-header-value))
6859                        (string-match "<[^>]+>" in-reply-to))
6860                   (progn
6861                     (header-set-references 
6862                      header 
6863                      (setq ref (substring in-reply-to (match-beginning 0)
6864                                           (match-end 0))))
6865                     (setq ref (downcase ref)))
6866                 (setq ref "none")))
6867             ;; We do some threading while we read the headers. The
6868             ;; message-id and the last reference are both entered into
6869             ;; the same hash table. Some tippy-toeing around has to be
6870             ;; done in case an article has arrived before the article
6871             ;; which it refers to.
6872             (if (boundp (setq dep (intern (downcase id) dependencies)))
6873                 (if (car (symbol-value dep))
6874                     ;; An article with this Message-ID has already
6875                     ;; been seen, so we ignore this one, except we add
6876                     ;; any additional Xrefs (in case the two articles
6877                     ;; came from different servers.
6878                     (progn
6879                       (header-set-xref 
6880                        (car (symbol-value dep))
6881                        (concat (or (header-xref (car (symbol-value dep))) "")
6882                                (or (header-xref header) "")))
6883                       (setq header nil))
6884                   (setcar (symbol-value dep) header))
6885               (set dep (list header)))
6886             (if header
6887                 (progn
6888                   (if (boundp (setq dep (intern ref dependencies)))
6889                       (setcdr (symbol-value dep) 
6890                               (cons header (cdr (symbol-value dep))))
6891                     (set dep (list nil header)))
6892                   (setq headers (cons header headers))))
6893             (goto-char (point-max))))))
6894     (nreverse headers)))
6895
6896 ;; The following macros and functions were written by Felix Lee
6897 ;; <flee@cse.psu.edu>. 
6898
6899 (defmacro gnus-nov-read-integer ()
6900   '(prog1
6901        (if (= (following-char) ?\t)
6902            0
6903          (let ((num (condition-case nil (read buffer) (error nil))))
6904            (if (numberp num) num 0)))
6905      (or (eobp) (forward-char 1))))
6906
6907 (defmacro gnus-nov-skip-field ()
6908   '(search-forward "\t" eol 'move))
6909
6910 (defmacro gnus-nov-field ()
6911   '(buffer-substring (point) (if (gnus-nov-skip-field) (1- (point)) eol)))
6912
6913 ;; Goes through the xover lines and returns a list of vectors
6914 (defun gnus-get-newsgroup-headers-xover (sequence)
6915   "Parse the news overview data in the server buffer, and return a
6916 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
6917   ;; Get the Xref when the users reads the articles since most/some
6918   ;; NNTP servers do not include Xrefs when using XOVER.
6919   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
6920   (let ((cur nntp-server-buffer)
6921         (dependencies gnus-newsgroup-dependencies)
6922         number headers header)
6923     (save-excursion
6924       (set-buffer nntp-server-buffer)
6925       (goto-char (point-min))
6926       (while (and sequence (not (eobp)))
6927         (setq number (read cur))
6928         (while (and sequence (< (car sequence) number))
6929           (setq sequence (cdr sequence)))
6930         (and sequence 
6931              (eq number (car sequence))
6932              (progn
6933                (setq sequence (cdr sequence))
6934                (if (setq header 
6935                          (inline (gnus-nov-parse-line number dependencies)))
6936                    (setq headers (cons header headers)))))
6937         (forward-line 1))
6938       (setq headers (nreverse headers)))
6939     headers))
6940
6941 ;; This function has to be called with point after the article number
6942 ;; on the beginning of the line.
6943 (defun gnus-nov-parse-line (number dependencies)
6944   (let ((none 0)
6945         (eol (gnus-point-at-eol)) 
6946         (buffer (current-buffer))
6947         header ref id dep)
6948
6949     ;; overview: [num subject from date id refs chars lines misc]
6950     (narrow-to-region (point) eol)
6951     (forward-char)
6952
6953     (condition-case nil
6954         (setq header
6955               (vector 
6956                number                   ; number
6957                (gnus-nov-field)         ; subject
6958                (gnus-nov-field)         ; from
6959                (gnus-nov-field)         ; date
6960                (setq id (or (gnus-nov-field)
6961                             (concat "none+"
6962                                     (int-to-string 
6963                                      (setq none (1+ none)))))) ; id
6964                (progn
6965                  (save-excursion
6966                    (let ((beg (point)))
6967                      (search-forward "\t" eol)
6968                      (if (search-backward ">" beg t)
6969                          (setq ref 
6970                                (downcase 
6971                                 (buffer-substring 
6972                                  (1+ (point))
6973                                  (progn
6974                                    (search-backward "<" beg t)
6975                                    (point)))))
6976                        (setq ref nil))))
6977                  (gnus-nov-field))      ; refs
6978                (gnus-nov-read-integer)  ; chars
6979                (gnus-nov-read-integer)  ; lines
6980                (if (= (following-char) ?\n)
6981                    nil
6982                  (gnus-nov-field))      ; misc
6983                ))
6984       (error (progn 
6985                (ding)
6986                (message "Strange nov line.")
6987                (setq header nil)
6988                (goto-char eol))))
6989
6990     (widen)
6991
6992     ;; We build the thread tree.
6993     (and header
6994          (if (boundp (setq dep (intern (downcase id) dependencies)))
6995              (if (car (symbol-value dep))
6996                  ;; An article with this Message-ID has already been seen,
6997                  ;; so we ignore this one, except we add any additional
6998                  ;; Xrefs (in case the two articles came from different
6999                  ;; servers.
7000                  (progn
7001                    (header-set-xref 
7002                     (car (symbol-value dep))
7003                     (concat (or (header-xref (car (symbol-value dep))) "")
7004                             (or (header-xref header) "")))
7005                    (setq header nil))
7006                (setcar (symbol-value dep) header))
7007            (set dep (list header))))
7008     (if header
7009         (progn
7010           (if (boundp (setq dep (intern (or ref "none") 
7011                                         dependencies)))
7012               (setcdr (symbol-value dep) 
7013                       (cons header (cdr (symbol-value dep))))
7014             (set dep (list nil header)))))
7015     header))
7016
7017 (defun gnus-article-get-xrefs ()
7018   "Fill in the Xref value in `gnus-current-headers', if necessary.
7019 This is meant to be called in `gnus-article-internal-prepare-hook'."
7020   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
7021                                  gnus-current-headers)))
7022     (or (not gnus-use-cross-reference)
7023         (not headers)
7024         (and (header-xref headers)
7025              (not (string= (header-xref headers) "")))
7026         (let ((case-fold-search t)
7027               xref)
7028           (save-restriction
7029             (gnus-narrow-to-headers)
7030             (goto-char (point-min))
7031             (if (or (and (eq (downcase (following-char)) ?x)
7032                          (looking-at "Xref:"))
7033                     (search-forward "\nXref:" nil t))
7034                 (progn
7035                   (goto-char (1+ (match-end 0)))
7036                   (setq xref (buffer-substring (point) 
7037                                                (progn (end-of-line) (point))))
7038                   (header-set-xref headers xref))))))))
7039
7040 (defalias 'gnus-find-header-by-number 'gnus-get-header-by-number)
7041 (make-obsolete 'gnus-find-header-by-number 'gnus-get-header-by-number)
7042
7043 ;; Return a header specified by a NUMBER.
7044 (defun gnus-get-header-by-number (number)
7045   (save-excursion
7046     (set-buffer gnus-summary-buffer)
7047     (or gnus-newsgroup-headers-hashtb-by-number
7048         (gnus-make-headers-hashtable-by-number))
7049     (gnus-gethash (int-to-string number)
7050                   gnus-newsgroup-headers-hashtb-by-number)))
7051
7052 (defun gnus-make-headers-hashtable-by-number ()
7053   "Make hashtable for the variable gnus-newsgroup-headers by number."
7054   (save-excursion
7055     (set-buffer gnus-summary-buffer)
7056     (let ((headers gnus-newsgroup-headers)
7057           header)
7058       (setq gnus-newsgroup-headers-hashtb-by-number
7059             (gnus-make-hashtable (length headers)))
7060       (while headers
7061         (setq header (car headers))
7062         (gnus-sethash (int-to-string (header-number header))
7063                       header gnus-newsgroup-headers-hashtb-by-number)
7064         (setq headers (cdr headers))))))
7065
7066 (defun gnus-more-header-backward ()
7067   "Find new header backward."
7068   (let ((first (car (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
7069         (artnum gnus-newsgroup-begin)
7070         (header nil))
7071     (while (and (not header)
7072                 (> artnum first))
7073       (setq artnum (1- artnum))
7074       (setq header (gnus-read-header artnum)))
7075     header))
7076
7077 (defun gnus-more-header-forward (&optional backward)
7078   "Find new header forward.
7079 If BACKWARD, find new header backward instead."
7080   (if backward
7081       (gnus-more-header-backward)
7082     (let ((last (cdr (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
7083           (artnum gnus-newsgroup-end)
7084           (header nil))
7085       (while (and (not header)
7086                   (< artnum last))
7087         (setq artnum (1+ artnum))
7088         (setq header (gnus-read-header artnum)))
7089       header)))
7090
7091 (defun gnus-extend-newsgroup (header &optional backward)
7092   "Extend newsgroup selection with HEADER.
7093 Optional argument BACKWARD means extend toward backward."
7094   (if header
7095       (let ((artnum (header-number header)))
7096         (setq gnus-newsgroup-headers
7097               (if backward
7098                   (cons header gnus-newsgroup-headers)
7099                 (nconc gnus-newsgroup-headers (list header))))
7100         (setq gnus-newsgroup-unselected
7101               (delq artnum gnus-newsgroup-unselected))
7102         (setq gnus-newsgroup-begin (min gnus-newsgroup-begin artnum))
7103         (setq gnus-newsgroup-end (max gnus-newsgroup-end artnum)))))
7104
7105 (defun gnus-summary-work-articles (n)
7106   "Return a list of articles to be worked upon. The prefix argument,
7107 the list of process marked articles, and the current article will be
7108 taken into consideration."
7109   (let (articles)
7110     (if (and n (numberp n))
7111         (let ((backward (< n 0))
7112               (n (abs n)))
7113           (save-excursion
7114             (while (and (> n 0)
7115                         (setq articles (cons (gnus-summary-article-number) 
7116                                              articles))
7117                         (gnus-summary-search-forward nil nil backward))
7118               (setq n (1- n))))
7119           (sort articles (function <)))
7120       (or (reverse gnus-newsgroup-processable)
7121           (list (gnus-summary-article-number))))))
7122
7123 (defun gnus-summary-search-group (&optional backward use-level)
7124   "Search for next unread newsgroup.
7125 If optional argument BACKWARD is non-nil, search backward instead."
7126   (save-excursion
7127     (set-buffer gnus-group-buffer)
7128     (if (gnus-group-search-forward 
7129          backward nil (if use-level (gnus-group-group-level) nil))
7130         (gnus-group-group-name))))
7131
7132 (defun gnus-summary-best-group (&optional exclude-group)
7133   "Find the name of the best unread group.
7134 If EXCLUDE-GROUP, do not go to this group."
7135   (save-excursion
7136     (set-buffer gnus-group-buffer)
7137     (save-excursion
7138       (gnus-group-best-unread-group exclude-group))))
7139
7140 (defun gnus-summary-search-subject (&optional backward unread subject)
7141   "Search for article forward.
7142 If BACKWARD is non-nil, search backward.
7143 If UNREAD is non-nil, only unread articles are selected.
7144 If SUBJECT is non-nil, the article which has the same subject will be
7145 searched for." 
7146   (let ((func (if backward 'previous-single-property-change
7147                 'next-single-property-change))
7148         (beg (point))
7149         (did t)
7150         pos psubject)
7151     (beginning-of-line)
7152     (and gnus-summary-check-current unread
7153          (eq (get-text-property (point) 'gnus-mark) gnus-unread-mark)
7154          (setq did nil))
7155     (if (not did)
7156         ()
7157       (forward-char (if backward (if (bobp) 0 -1) (if (eobp) 0 1)))
7158       (while
7159           (and 
7160            (setq pos (funcall func (point) 'gnus-number))
7161            (goto-char (if backward (1- pos) pos))
7162            (setq did
7163                  (not (and
7164                        (or (not unread)
7165                            (eq (get-text-property (point) 'gnus-mark)
7166                                gnus-unread-mark))
7167                        (or (not subject)
7168                            (and (setq psubject (gnus-summary-subject-string))
7169                                 (gnus-subject-equal subject psubject))))))
7170            (if backward (if (bobp) nil (forward-char -1) t)
7171              (if (eobp) nil (forward-char 1) t)))))
7172     (if did
7173         (progn (goto-char beg) nil)
7174       (prog1
7175           (get-text-property (point) 'gnus-number)
7176         (gnus-summary-position-cursor)))))
7177
7178 (defun gnus-subject-equal (s1 s2)
7179   (cond
7180    ((null gnus-summary-gather-subject-limit)
7181     (equal (gnus-simplify-subject-re s1)
7182            (gnus-simplify-subject-re s2)))
7183    ((eq gnus-summary-gather-subject-limit 'fuzzy)
7184     (equal (gnus-simplify-subject-fuzzy s1)
7185            (gnus-simplify-subject-fuzzy s2)))
7186    ((numberp gnus-summary-gather-subject-limit)
7187     (equal (gnus-limit-string s1 gnus-summary-gather-subject-limit)
7188            (gnus-limit-string s2 gnus-summary-gather-subject-limit)))
7189    (t
7190     (equal s1 s2))))
7191     
7192 (defun gnus-summary-search-forward (&optional unread subject backward)
7193   "Search for article forward.
7194 If UNREAD is non-nil, only unread articles are selected.
7195 If SUBJECT is non-nil, the article which has the same subject will be
7196 searched for. 
7197 If BACKWARD is non-nil, the search will be performed backwards instead."
7198   (gnus-summary-search-subject backward unread subject))
7199
7200 (defun gnus-summary-search-backward (&optional unread subject)
7201   "Search for article backward.
7202 If 1st optional argument UNREAD is non-nil, only unread article is selected.
7203 If 2nd optional argument SUBJECT is non-nil, the article which has
7204 the same subject will be searched for."
7205   (gnus-summary-search-forward unread subject t))
7206
7207 (defun gnus-summary-article-number (&optional number-or-nil)
7208   "The article number of the article on the current line.
7209 If there isn's an article number here, then we return the current
7210 article number."
7211   (let* ((number (get-text-property (gnus-point-at-bol) 'gnus-number)))
7212     (if number-or-nil number (or number gnus-current-article))))
7213
7214 (defun gnus-summary-thread-level ()
7215   "The thread level of the article on the current line."
7216   (or (get-text-property (gnus-point-at-bol) 'gnus-level)
7217       0))
7218
7219 (defun gnus-summary-pseudo-article ()
7220   "The thread level of the article on the current line."
7221   (get-text-property (gnus-point-at-bol) 'gnus-pseudo))
7222
7223 (defun gnus-summary-article-mark ()
7224   "The mark on the current line."
7225   (get-text-property (gnus-point-at-bol) 'gnus-mark))
7226
7227 (defun gnus-summary-subject-string ()
7228   "Return current subject string or nil if nothing."
7229   (let ((article (gnus-summary-article-number))
7230         header)
7231     (and article 
7232          (setq header (gnus-get-header-by-number article))
7233          (vectorp header)
7234          (header-subject header))))
7235
7236 (defalias 'gnus-summary-score 'gnus-summary-article-score)
7237 (make-obsolete 'gnus-summary-score 'gnus-summary-article-score)
7238 (defun gnus-summary-article-score ()
7239   "Return current article score."
7240   (or (cdr (assq (gnus-summary-article-number) gnus-newsgroup-scored))
7241       gnus-summary-default-score 0))
7242
7243 (defun gnus-summary-recenter ()
7244   "Center point in the summary window.
7245 If `gnus-auto-center-summary' is nil, or the article buffer isn't
7246 displayed, no centering will be performed." 
7247   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
7248   ;; Recenter only when requested. Suggested by popovich@park.cs.columbia.edu.
7249   (let* ((top (cond ((< (window-height) 4) 0)
7250                     ((< (window-height) 7) 1)
7251                     (t 2)))
7252          (height (1- (window-height)))
7253          (bottom (save-excursion (goto-char (point-max))
7254                                  (forward-line (- height))
7255                                  (point)))
7256          (window (get-buffer-window (current-buffer))))
7257     (and 
7258      ;; The user has to want it,
7259      gnus-auto-center-summary 
7260      ;; the article buffer must be displayed,
7261      (get-buffer-window gnus-article-buffer)
7262      ;; Set the window start to either `bottom', which is the biggest
7263      ;; possible valid number, or the second line from the top,
7264      ;; whichever is the least.
7265      (set-window-start
7266       window (min bottom (save-excursion (forward-line (- top)) (point)))))))
7267
7268 ;; Function written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
7269 (defun gnus-short-group-name (group &optional levels)
7270   "Collapse GROUP name LEVELS."
7271   (let* ((name "") (foreign "") (depth -1) (skip 1)
7272          (levels (or levels
7273                      (progn
7274                        (while (string-match "\\." group skip)
7275                          (setq skip (match-end 0)
7276                                depth (+ depth 1)))
7277                        depth))))
7278     (if (string-match ":" group)
7279         (setq foreign (substring group 0 (match-end 0))
7280               group (substring group (match-end 0))))
7281     (while group
7282       (if (and (string-match "\\." group) (> levels 0))
7283           (setq name (concat name (substring group 0 1))
7284                 group (substring group (match-end 0))
7285                 levels (- levels 1)
7286                 name (concat name "."))
7287         (setq name (concat foreign name group)
7288               group nil)))
7289     name))
7290
7291 (defun gnus-summary-jump-to-group (newsgroup)
7292   "Move point to NEWSGROUP in group mode buffer."
7293   ;; Keep update point of group mode buffer if visible.
7294   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
7295       (save-window-excursion
7296         ;; Take care of tree window mode.
7297         (if (get-buffer-window gnus-group-buffer)
7298             (pop-to-buffer gnus-group-buffer))
7299         (gnus-group-jump-to-group newsgroup))
7300     (save-excursion
7301       ;; Take care of tree window mode.
7302       (if (get-buffer-window gnus-group-buffer)
7303           (pop-to-buffer gnus-group-buffer)
7304         (set-buffer gnus-group-buffer))
7305       (gnus-group-jump-to-group newsgroup))))
7306
7307 ;; This function returns a list of article numbers based on the
7308 ;; difference between the ranges of read articles in this group and
7309 ;; the range of active articles.
7310 (defun gnus-list-of-unread-articles (group)
7311   (let* ((read (nth 2 (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
7312          (active (gnus-gethash group gnus-active-hashtb))
7313          (last (cdr active))
7314          first nlast unread)
7315     ;; If none are read, then all are unread. 
7316     (if (not read)
7317         (setq first (car active))
7318       ;; If the range of read articles is a single range, then the
7319       ;; first unread article is the article after the last read
7320       ;; article. Sounds logical, doesn't it?
7321       (if (not (listp (cdr read)))
7322           (setq first (1+ (cdr read)))
7323         ;; `read' is a list of ranges.
7324         (if (/= (setq nlast (or (and (numberp (car read)) (car read)) 
7325                                 (car (car read)))) 1)
7326             (setq first 1))
7327         (while read
7328           (if first 
7329               (while (< first nlast)
7330                 (setq unread (cons first unread))
7331                 (setq first (1+ first))))
7332           (setq first (1+ (if (atom (car read)) (car read) (cdr (car read)))))
7333           (setq nlast (if (atom (car (cdr read))) 
7334                           (car (cdr read))
7335                         (car (car (cdr read)))))
7336           (setq read (cdr read)))))
7337     ;; And add the last unread articles.
7338     (while (<= first last)
7339       (setq unread (cons first unread))
7340       (setq first (1+ first)))
7341     ;; Return the list of unread articles.
7342     (nreverse unread)))
7343
7344 (defun gnus-list-of-read-articles (group)
7345   (let ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
7346         (active (gnus-gethash group gnus-active-hashtb)))
7347     (and info active
7348          (gnus-sorted-complement 
7349           (gnus-uncompress-range active) 
7350           (gnus-list-of-unread-articles group)))))
7351
7352 ;; Various summary commands
7353
7354 (defun gnus-summary-universal-argument ()
7355   "Perform any operation on all articles marked with the process mark."
7356   (interactive)
7357   (gnus-set-global-variables)
7358   (let ((articles (reverse gnus-newsgroup-processable))
7359         func)
7360     (or articles (error "No articles marked"))
7361     (or (setq func (key-binding (read-key-sequence "C-c C-u")))
7362         (error "Undefined key"))
7363     (while articles
7364       (gnus-summary-goto-subject (car articles))
7365       (command-execute func)
7366       (gnus-summary-remove-process-mark (car articles))
7367       (setq articles (cdr articles)))))
7368
7369 (defun gnus-summary-toggle-truncation (arg)
7370   "Toggle truncation of summary lines.
7371 With arg, turn line truncation on iff arg is positive."
7372   (interactive "P")
7373   (setq truncate-lines
7374         (if (null arg) (not truncate-lines)
7375           (> (prefix-numeric-value arg) 0)))
7376   (redraw-display))
7377
7378 (defun gnus-summary-reselect-current-group (all)
7379   "Once exit and then reselect the current newsgroup.
7380 The prefix argument ALL means to select all articles."
7381   (interactive "P")
7382   (gnus-set-global-variables)
7383   (let ((current-subject (gnus-summary-article-number))
7384         (group gnus-newsgroup-name))
7385     (setq gnus-newsgroup-begin nil)
7386     (gnus-summary-exit t)
7387     ;; We have to adjust the point of group mode buffer because the
7388     ;; current point was moved to the next unread newsgroup by
7389     ;; exiting.
7390     (gnus-summary-jump-to-group group)
7391     (gnus-group-read-group all t)
7392     (gnus-summary-goto-subject current-subject)))
7393
7394 (defun gnus-summary-rescan-group (all)
7395   "Exit the newsgroup, ask for new articles, and select the newsgroup."
7396   (interactive "P")
7397   (gnus-set-global-variables)
7398   ;; Fix by Ilja Weis <kult@uni-paderborn.de>.
7399   (let ((group gnus-newsgroup-name))
7400     (gnus-summary-exit)
7401     (gnus-summary-jump-to-group group)
7402     (save-excursion
7403       (set-buffer gnus-group-buffer)
7404       (gnus-group-get-new-news-this-group 1))
7405     (gnus-summary-jump-to-group group)
7406     (gnus-group-read-group all)))
7407
7408 (defun gnus-summary-update-info ()
7409   (let* ((group gnus-newsgroup-name))
7410     (if gnus-newsgroup-kill-headers
7411         (setq gnus-newsgroup-killed
7412               (gnus-compress-sequence
7413                (nconc
7414                 (gnus-set-sorted-intersection
7415                  (gnus-uncompress-range gnus-newsgroup-killed)
7416                  (setq gnus-newsgroup-unselected
7417                        (sort gnus-newsgroup-unselected '<)))
7418                 (setq gnus-newsgroup-unreads
7419                       (sort gnus-newsgroup-unreads '<))) t)))
7420     (or (listp (cdr gnus-newsgroup-killed))
7421         (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
7422     (let ((headers gnus-newsgroup-headers))
7423       (gnus-close-group group)
7424       (run-hooks 'gnus-exit-group-hook)
7425       (gnus-update-read-articles 
7426        group gnus-newsgroup-unreads gnus-newsgroup-unselected 
7427        gnus-newsgroup-marked
7428        t gnus-newsgroup-replied gnus-newsgroup-expirable
7429        gnus-newsgroup-killed gnus-newsgroup-dormant
7430        gnus-newsgroup-bookmarks 
7431        (and gnus-save-score gnus-newsgroup-scored))
7432       (and gnus-use-cross-reference
7433            (gnus-mark-xrefs-as-read 
7434             group headers gnus-newsgroup-unreads gnus-newsgroup-expirable))
7435       ;; Do adaptive scoring, and possibly save score files.
7436       (and gnus-newsgroup-adaptive
7437            (gnus-score-adaptive))
7438       (and gnus-use-scoring 
7439            (fboundp 'gnus-score-save)
7440            (funcall 'gnus-score-save))
7441       ;; Do not switch windows but change the buffer to work.
7442       (set-buffer gnus-group-buffer)
7443       (or (assoc 'quit-config (gnus-find-method-for-group gnus-newsgroup-name))
7444           (gnus-group-update-group group)))))
7445   
7446 (defun gnus-summary-exit (&optional temporary)
7447   "Exit reading current newsgroup, and then return to group selection mode.
7448 gnus-exit-group-hook is called with no arguments if that value is non-nil."
7449   (interactive)
7450   (gnus-set-global-variables)
7451   (gnus-kill-save-kill-buffer)
7452   (let* ((group gnus-newsgroup-name)
7453          (quit-config (nth 1 (assoc 'quit-config (gnus-find-method-for-group
7454                                                   gnus-newsgroup-name))))
7455          (mode major-mode)
7456          (buf (current-buffer)))
7457     (run-hooks 'gnus-summary-exit-hook)
7458     (gnus-summary-update-info) ; Make all changes in this group permanent.
7459     (set-buffer buf)
7460     (and gnus-use-cache (gnus-cache-possibly-remove-articles))
7461     ;; Make sure where I was, and go to next newsgroup.
7462     (set-buffer gnus-group-buffer)
7463     (or quit-config
7464         (progn
7465           (gnus-group-jump-to-group group)
7466           (gnus-group-next-unread-group 1)))
7467     (if temporary
7468         nil                             ;Nothing to do.
7469       ;; We set all buffer-local variables to nil. It is unclear why
7470       ;; this is needed, but if we don't, buffer-local variables are
7471       ;; not garbage-collected, it seems. This would the lead to en
7472       ;; ever-growing Emacs.
7473       (set-buffer buf)
7474       (gnus-summary-clear-local-variables)
7475       ;; We clear the global counterparts of the buffer-local
7476       ;; variables as well, just to be on the safe side.
7477       (gnus-configure-windows 'group 'force)
7478       (gnus-summary-clear-local-variables)
7479       ;; Return to group mode buffer. 
7480       (if (eq mode 'gnus-summary-mode)
7481           (gnus-kill-buffer buf))
7482       (if (get-buffer gnus-article-buffer)
7483           (bury-buffer gnus-article-buffer))
7484       (setq gnus-current-select-method gnus-select-method)
7485       (pop-to-buffer gnus-group-buffer)
7486       (if (not quit-config)
7487           (progn
7488             (gnus-group-jump-to-group group)
7489             (gnus-group-next-unread-group 1))
7490         (if (not (buffer-name (car quit-config)))
7491             (gnus-configure-windows 'group 'force)
7492           (set-buffer (car quit-config))
7493           (and (eq major-mode 'gnus-summary-mode)
7494                (gnus-set-global-variables))
7495           (gnus-configure-windows (cdr quit-config)))))))
7496
7497 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
7498 (defun gnus-summary-exit-no-update (&optional no-questions)
7499   "Quit reading current newsgroup without updating read article info."
7500   (interactive)
7501   (gnus-set-global-variables)
7502   (let* ((group gnus-newsgroup-name)
7503          (quit-config (nth 1 (assoc 'quit-config 
7504                                     (gnus-find-method-for-group group)))))
7505     (if (or no-questions
7506             gnus-expert-user
7507             (gnus-y-or-n-p "Do you really wanna quit reading this group? "))
7508         (progn
7509           (gnus-close-group group)
7510           (gnus-summary-clear-local-variables)
7511           (set-buffer gnus-group-buffer)
7512           (gnus-summary-clear-local-variables)
7513           ;; Return to group selection mode.
7514           (gnus-configure-windows 'group 'force)
7515           (if (get-buffer gnus-summary-buffer)
7516               (kill-buffer gnus-summary-buffer))
7517           (if (get-buffer gnus-article-buffer)
7518               (bury-buffer gnus-article-buffer))
7519           (if (equal (gnus-group-group-name) group)
7520               (gnus-group-next-unread-group 1))
7521           (if quit-config
7522               (progn
7523                 (if (not (buffer-name (car quit-config)))
7524                     (gnus-configure-windows 'group 'force)
7525                   (set-buffer (car quit-config))
7526                   (and (eq major-mode 'gnus-summary-mode)
7527                        (gnus-set-global-variables))
7528                   (gnus-configure-windows (cdr quit-config)))))))))
7529
7530 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
7531 (defun gnus-summary-fetch-faq (group)
7532   "Fetch the FAQ for the current group."
7533   (interactive (list gnus-newsgroup-name))
7534   (let ((gnus-faq-buffer 
7535          (find-file (concat gnus-group-faq-directory 
7536                             (gnus-group-real-name group)))))
7537   (and gnus-faq-buffer (gnus-configure-windows 'summary-faq))))
7538
7539 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
7540 (defun gnus-summary-describe-group (force)
7541   "Describe the current newsgroup."
7542   (interactive "P")
7543   (gnus-group-describe-group force gnus-newsgroup-name))
7544
7545 (defun gnus-summary-describe-briefly ()
7546   "Describe summary mode commands briefly."
7547   (interactive)
7548   (gnus-message 6
7549     (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")))
7550
7551 ;; Walking around group mode buffer from summary mode.
7552
7553 (defun gnus-summary-next-group (&optional no-article target-group backward)
7554   "Exit current newsgroup and then select next unread newsgroup.
7555 If prefix argument NO-ARTICLE is non-nil, no article is selected
7556 initially. If NEXT-GROUP, go to this group. If BACKWARD, go to
7557 previous group instead."
7558   (interactive "P")
7559   (gnus-set-global-variables)
7560   (let ((current-group gnus-newsgroup-name)
7561         (current-buffer (current-buffer))
7562         entered)
7563     ;; First we semi-exit this group to update Xrefs and all variables.
7564     ;; We can't do a real exit, because the window conf must remain
7565     ;; the same in case the user is prompted for info, and we don't
7566     ;; want the window conf to change before that...
7567     (gnus-summary-exit t)
7568     (while (not entered)
7569       ;; Then we find what group we are supposed to enter.
7570       (set-buffer gnus-group-buffer)
7571       (gnus-group-jump-to-group current-group)
7572       (setq target-group 
7573             (or target-group        
7574                 (if (eq gnus-keep-same-level 'best) 
7575                     (gnus-summary-best-group gnus-newsgroup-name)
7576                   (gnus-summary-search-group backward gnus-keep-same-level))))
7577       (if (not target-group)
7578           ;; There are no further groups, so we return to the group
7579           ;; buffer.
7580           (progn
7581             (gnus-message 5 "Returning to the group buffer")
7582             (setq entered t)
7583             (set-buffer current-buffer)
7584             (gnus-summary-exit))
7585         ;; We try to enter the target group.
7586         (gnus-group-jump-to-group target-group)
7587         (let ((unreads (gnus-group-group-unread)))
7588           (if (and (or (eq t unreads)
7589                        (and unreads (not (zerop unreads))))
7590                    (gnus-summary-read-group
7591                     target-group nil no-article current-buffer))
7592               (setq entered t)
7593             (setq current-group target-group
7594                   target-group nil)))))))
7595
7596 (defun gnus-summary-next-group-old (&optional no-article group backward)
7597   "Exit current newsgroup and then select next unread newsgroup.
7598 If prefix argument NO-ARTICLE is non-nil, no article is selected initially.
7599 If BACKWARD, go to previous group instead."
7600   (interactive "P")
7601   (gnus-set-global-variables)
7602   (let ((ingroup gnus-newsgroup-name)
7603         (sumbuf (current-buffer))
7604         num)
7605     (set-buffer gnus-group-buffer)
7606     (if (and group
7607              (or (and (numberp (setq num (car (gnus-gethash
7608                                                group gnus-newsrc-hashtb))))
7609                       (< num 1))
7610                  (null num)))
7611         (progn
7612           (gnus-group-jump-to-group group)
7613           (setq group nil))
7614       (gnus-group-jump-to-group ingroup))
7615     (gnus-summary-search-group backward)
7616     (let ((group (or group (gnus-summary-search-group backward))))
7617       (set-buffer sumbuf)
7618       (gnus-summary-exit t)             ;Update all information.
7619       (if (null group)
7620           (gnus-summary-exit-no-update t)
7621         (gnus-group-jump-to-group ingroup)
7622         (setq group (gnus-summary-search-group backward))
7623         (gnus-message 5 "Selecting %s..." group)
7624         (set-buffer gnus-group-buffer)
7625         ;; We are now in group mode buffer.
7626         ;; Make sure group mode buffer point is on GROUP.
7627         (gnus-group-jump-to-group group)
7628         (if (not (eq gnus-auto-select-next 'quietly))
7629             (progn
7630               (gnus-summary-read-group group nil no-article sumbuf)
7631               (and (string= gnus-newsgroup-name ingroup)
7632                    (bufferp sumbuf) (buffer-name sumbuf)
7633                    (progn
7634                      (set-buffer (setq gnus-summary-buffer sumbuf))
7635                      (gnus-summary-exit-no-update t))))
7636           (let ((prevgroup group))
7637             (gnus-group-jump-to-group ingroup)
7638             (setq group (gnus-summary-search-group backward))
7639             (gnus-summary-read-group group nil no-article sumbuf)
7640             (while (and (string= gnus-newsgroup-name ingroup)
7641                         (bufferp sumbuf) 
7642                         (buffer-name sumbuf)
7643                         (not (string= prevgroup (gnus-group-group-name))))
7644               (set-buffer gnus-group-buffer)
7645               (gnus-summary-read-group 
7646                (setq prevgroup (gnus-group-group-name)) 
7647                nil no-article sumbuf))
7648             (and (string= prevgroup (gnus-group-group-name))
7649                  ;; We have reached the final group in the group
7650                  ;; buffer.
7651                  (progn
7652                    (if (buffer-name sumbuf)
7653                        (progn
7654                          (set-buffer sumbuf)
7655                          (gnus-summary-exit)))))))))))
7656
7657 (defun gnus-summary-prev-group (no-article)
7658   "Exit current newsgroup and then select previous unread newsgroup.
7659 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7660   (interactive "P")
7661   (gnus-summary-next-group no-article nil t))
7662
7663 ;; Walking around summary lines.
7664
7665 (defun gnus-summary-first-subject (unread)
7666   "Go to the first unread subject.
7667 If UNREAD is non-nil, go to the first unread article.
7668 Returns nil if there are no unread articles."
7669   (interactive "P")
7670   (prog1
7671       (cond ((not unread)
7672              (goto-char (point-min)))
7673             ((gnus-goto-char 
7674               (text-property-any 
7675                (point-min) (point-max) 'gnus-mark gnus-unread-mark))
7676              t)
7677             (t 
7678              ;; There are no unread articles.
7679              (gnus-message 3 "No more unread articles")
7680              nil))
7681     (gnus-summary-position-cursor)))
7682
7683 (defun gnus-summary-next-subject (n &optional unread dont-display)
7684   "Go to next N'th summary line.
7685 If N is negative, go to the previous N'th subject line.
7686 If UNREAD is non-nil, only unread articles are selected.
7687 The difference between N and the actual number of steps taken is
7688 returned."
7689   (interactive "p")
7690   (let ((backward (< n 0))
7691         (n (abs n)))
7692     (while (and (> n 0)
7693                 (gnus-summary-search-forward unread nil backward))
7694       (setq n (1- n)))
7695     (if (/= 0 n) (gnus-message 7 "No more%s articles"
7696                                (if unread " unread" "")))
7697     (or dont-display
7698         (progn
7699           (gnus-summary-recenter)
7700           (gnus-summary-position-cursor)))
7701   n))
7702
7703 (defun gnus-summary-next-unread-subject (n)
7704   "Go to next N'th unread summary line."
7705   (interactive "p")
7706   (gnus-summary-next-subject n t))
7707
7708 (defun gnus-summary-prev-subject (n &optional unread)
7709   "Go to previous N'th summary line.
7710 If optional argument UNREAD is non-nil, only unread article is selected."
7711   (interactive "p")
7712   (gnus-summary-next-subject (- n) unread))
7713
7714 (defun gnus-summary-prev-unread-subject (n)
7715   "Go to previous N'th unread summary line."
7716   (interactive "p")
7717   (gnus-summary-next-subject (- n) t))
7718
7719 (defun gnus-summary-goto-subject (article)
7720   "Go the subject line of ARTICLE."
7721   (interactive
7722    (list
7723     (string-to-int
7724      (completing-read "Article number: "
7725                       (mapcar
7726                        (lambda (headers)
7727                          (list
7728                           (int-to-string (header-number headers))))
7729                        gnus-newsgroup-headers)
7730                       nil 'require-match))))
7731   (or article (error "No article number"))
7732   (let ((b (point)))
7733     (if (not (gnus-goto-char (text-property-any (point-min) (point-max)
7734                                                 'gnus-number article)))
7735         ()
7736       (gnus-summary-show-thread)
7737       ;; Skip dummy articles. 
7738       (if (eq (gnus-summary-article-mark) gnus-dummy-mark)
7739           (forward-line 1))
7740       (prog1
7741           (if (not (eobp))
7742               article
7743             (goto-char b)
7744             nil)
7745         (gnus-summary-position-cursor)))))
7746
7747 ;; Walking around summary lines with displaying articles.
7748
7749 (defun gnus-summary-expand-window (arg)
7750   "Make the summary buffer take up the entire Emacs frame.
7751 Given a prefix, will force an `article' buffer configuration."
7752   (interactive "P")
7753   (gnus-set-global-variables)
7754   (if arg
7755       (gnus-configure-windows 'article 'force)
7756     (gnus-configure-windows 'summary 'force)))
7757
7758 (defun gnus-summary-display-article (article &optional all-header)
7759   "Display ARTICLE in article buffer."
7760   (gnus-set-global-variables)
7761   (if (null article)
7762       nil
7763     (prog1
7764         (gnus-article-prepare article all-header)
7765       (gnus-summary-show-thread)
7766       (if (eq (gnus-summary-article-mark) gnus-dummy-mark)
7767           (progn
7768             (forward-line 1)
7769             (gnus-summary-position-cursor)))
7770       (run-hooks 'gnus-select-article-hook)
7771       (gnus-summary-recenter)
7772       (gnus-summary-goto-subject article)
7773       ;; Successfully display article.
7774       (gnus-summary-update-line)
7775       (gnus-article-set-window-start 
7776        (cdr (assq article gnus-newsgroup-bookmarks)))
7777       t)))
7778
7779 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7780   "Select the current article.
7781 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7782 non-nil, the article will be re-fetched even if it already present in
7783 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7784 be displayed."
7785   (and (not pseudo) (gnus-summary-pseudo-article)
7786        (error "This is a pseudo-article."))
7787   (let ((article (or article (gnus-summary-article-number)))
7788         (all-headers (not (not all-headers))) ;Must be T or NIL.
7789         did) 
7790     (prog1
7791         (save-excursion
7792           (set-buffer gnus-summary-buffer)
7793           (if (or (null gnus-current-article)
7794                   (null gnus-article-current)
7795                   (null (get-buffer gnus-article-buffer))
7796                   (not (eq article (cdr gnus-article-current)))
7797                   (not (equal (car gnus-article-current) gnus-newsgroup-name))
7798                   force)
7799               ;; The requested article is different from the current article.
7800               (progn
7801                 (gnus-summary-display-article article all-headers)
7802                 (setq did article))
7803             (if (or all-headers gnus-show-all-headers) 
7804                 (gnus-article-show-all-headers))
7805             nil))
7806       (if did 
7807           (gnus-article-set-window-start 
7808            (cdr (assq article gnus-newsgroup-bookmarks)))))))
7809
7810 (defun gnus-summary-set-current-mark (&optional current-mark)
7811   "Obsolete function."
7812   nil)
7813
7814 (defun gnus-summary-next-article (unread &optional subject backward)
7815   "Select the next article.
7816 If UNREAD, only unread articles are selected.
7817 If SUBJECT, only articles with SUBJECT are selected.
7818 If BACKWARD, the previous article is selected instead of the next."
7819   (interactive "P")
7820   (gnus-set-global-variables)
7821   (let (header)
7822     (cond
7823      ;; Is there such an article?
7824      ((gnus-summary-display-article 
7825        (gnus-summary-search-forward unread subject backward))
7826       (gnus-summary-position-cursor))
7827      ;; If not, we try the first unread, if that is wanted.
7828      ((and subject
7829            gnus-auto-select-same
7830            (gnus-summary-first-unread-article))
7831       (gnus-message 6 "Wrapped"))
7832      ;; Try to get next/previous article not displayed in this group.
7833      ((and gnus-auto-extend-newsgroup
7834            (not unread) (not subject)
7835            (setq header (gnus-more-header-forward backward)))
7836       (gnus-extend-newsgroup header backward)
7837       (let ((buffer-read-only nil))
7838         (goto-char (if backward (point-min) (point-max)))
7839         (gnus-summary-prepare-threads (list header)))
7840       (gnus-summary-goto-article (if backward gnus-newsgroup-begin
7841                                    gnus-newsgroup-end)))
7842      ;; Go to next/previous group.
7843      (t
7844       (or (assoc 'quit-config (gnus-find-method-for-group gnus-newsgroup-name))
7845           (gnus-summary-jump-to-group gnus-newsgroup-name))
7846       (let ((cmd (aref (this-command-keys) 0))
7847             (group 
7848              (if (eq gnus-keep-same-level 'best) 
7849                  (gnus-summary-best-group gnus-newsgroup-name)
7850                (gnus-summary-search-group backward gnus-keep-same-level))))
7851         ;; For some reason, the group window gets selected. We change
7852         ;; it back.  
7853         (select-window (get-buffer-window (current-buffer)))
7854         ;; Keep just the event type of CMD.
7855         (and (listp cmd) (setq cmd (car cmd)))
7856         ;; Select next unread newsgroup automagically.
7857         (cond 
7858          ((not gnus-auto-select-next)
7859           (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7860          ((eq gnus-auto-select-next 'quietly)
7861           ;; Select quietly.
7862           (if (assoc 'quit-config (gnus-find-method-for-group 
7863                                    gnus-newsgroup-name))
7864               (gnus-summary-exit)
7865             (gnus-message 7 "No more%s articles (%s)..."
7866                           (if unread " unread" "") 
7867                           (if group (concat "selecting " group)
7868                             "exiting"))
7869             (gnus-summary-next-group nil group backward)))
7870          (t
7871           (let ((keystrokes '(?\C-n ?\C-p))
7872                 key)
7873             (while (or (null key) (memq key keystrokes))
7874               (gnus-message 
7875                7 "No more%s articles%s" (if unread " unread" "")
7876                (if (and group (not (assoc 'quit-config
7877                                           (gnus-find-method-for-group 
7878                                            gnus-newsgroup-name))))
7879                    (format " (Type %s for %s [%s])"
7880                            (single-key-description cmd) group
7881                            (car (gnus-gethash group gnus-newsrc-hashtb)))
7882                  (format " (Type %s to exit %s)"
7883                          (single-key-description cmd)
7884                          gnus-newsgroup-name)))
7885               ;; Confirm auto selection.
7886               (let* ((event (read-event)))
7887                 (setq key (if (listp event) (car event) event))
7888                 (if (memq key keystrokes)
7889                     (let ((obuf (current-buffer)))
7890                       (switch-to-buffer gnus-group-buffer)
7891                       (gnus-group-jump-to-group group)
7892                       (execute-kbd-macro (char-to-string key))
7893                       (setq group (gnus-group-group-name))
7894                       (switch-to-buffer obuf)))))
7895             (if (equal key cmd)
7896                 (if (or (not group) (assoc 'quit-config
7897                                            (gnus-find-method-for-group
7898                                             gnus-newsgroup-name)))
7899                     (gnus-summary-exit)
7900                   (gnus-summary-next-group nil group backward))
7901               (setq unread-command-events (list key)))))))))))
7902
7903 (defun gnus-summary-next-unread-article ()
7904   "Select unread article after current one."
7905   (interactive)
7906   (gnus-summary-next-article t (and gnus-auto-select-same
7907                                     (gnus-summary-subject-string))))
7908
7909 (defun gnus-summary-prev-article (unread &optional subject)
7910   "Select the article after the current one.
7911 If UNREAD is non-nil, only unread articles are selected."
7912   (interactive "P")
7913   (gnus-summary-next-article unread subject t))
7914
7915 (defun gnus-summary-prev-unread-article ()
7916   "Select unred article before current one."
7917   (interactive)
7918   (gnus-summary-prev-article t (and gnus-auto-select-same
7919                                     (gnus-summary-subject-string))))
7920
7921 (defun gnus-summary-next-page (lines &optional circular)
7922   "Show next page of selected article.
7923 If end of article, select next article.
7924 Argument LINES specifies lines to be scrolled up.
7925 If CIRCULAR is non-nil, go to the start of the article instead of 
7926 instead of selecting the next article when reaching the end of the
7927 current article." 
7928   (interactive "P")
7929   (setq gnus-summary-buffer (current-buffer))
7930   (gnus-set-global-variables)
7931   (let ((article (gnus-summary-article-number))
7932         (endp nil))
7933     (gnus-configure-windows 'article)
7934     (if (or (null gnus-current-article)
7935             (null gnus-article-current)
7936             (/= article (cdr gnus-article-current))
7937             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7938         ;; Selected subject is different from current article's.
7939         (gnus-summary-display-article article)
7940       (gnus-eval-in-buffer-window
7941        gnus-article-buffer
7942        (setq endp (gnus-article-next-page lines)))
7943       (if endp
7944           (cond (circular
7945                  (gnus-summary-beginning-of-article))
7946                 (lines
7947                  (gnus-message 3 "End of message"))
7948                 ((null lines)
7949                  (gnus-summary-next-unread-article)))))
7950     (gnus-summary-recenter)
7951     (gnus-summary-position-cursor)))
7952
7953 (defun gnus-summary-prev-page (lines)
7954   "Show previous page of selected article.
7955 Argument LINES specifies lines to be scrolled down."
7956   (interactive "P")
7957   (gnus-set-global-variables)
7958   (let ((article (gnus-summary-article-number)))
7959     (gnus-configure-windows 'article)
7960     (if (or (null gnus-current-article)
7961             (null gnus-article-current)
7962             (/= article (cdr gnus-article-current))
7963             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7964         ;; Selected subject is different from current article's.
7965         (gnus-summary-display-article article)
7966       (gnus-summary-recenter)
7967       (gnus-eval-in-buffer-window gnus-article-buffer
7968         (gnus-article-prev-page lines))))
7969   (gnus-summary-position-cursor))
7970
7971 (defun gnus-summary-scroll-up (lines)
7972   "Scroll up (or down) one line current article.
7973 Argument LINES specifies lines to be scrolled up (or down if negative)."
7974   (interactive "p")
7975   (gnus-set-global-variables)
7976   (gnus-configure-windows 'article)
7977   (or (gnus-summary-select-article nil nil 'pseudo)
7978       (gnus-eval-in-buffer-window 
7979        gnus-article-buffer
7980        (cond ((> lines 0)
7981               (if (gnus-article-next-page lines)
7982                   (gnus-message 3 "End of message")))
7983              ((< lines 0)
7984               (gnus-article-prev-page (- lines))))))
7985   (gnus-summary-recenter)
7986   (gnus-summary-position-cursor))
7987
7988 (defun gnus-summary-next-same-subject ()
7989   "Select next article which has the same subject as current one."
7990   (interactive)
7991   (gnus-set-global-variables)
7992   (gnus-summary-next-article nil (gnus-summary-subject-string)))
7993
7994 (defun gnus-summary-prev-same-subject ()
7995   "Select previous article which has the same subject as current one."
7996   (interactive)
7997   (gnus-set-global-variables)
7998   (gnus-summary-prev-article nil (gnus-summary-subject-string)))
7999
8000 (defun gnus-summary-next-unread-same-subject ()
8001   "Select next unread article which has the same subject as current one."
8002   (interactive)
8003   (gnus-set-global-variables)
8004   (gnus-summary-next-article t (gnus-summary-subject-string)))
8005
8006 (defun gnus-summary-prev-unread-same-subject ()
8007   "Select previous unread article which has the same subject as current one."
8008   (interactive)
8009   (gnus-set-global-variables)
8010   (gnus-summary-prev-article t (gnus-summary-subject-string)))
8011
8012 (defun gnus-summary-first-unread-article ()
8013   "Select the first unread article. 
8014 Return nil if there are no unread articles."
8015   (interactive)
8016   (gnus-set-global-variables)
8017   (prog1
8018       (if (gnus-summary-first-subject t)
8019           (gnus-summary-display-article (gnus-summary-article-number)))
8020     (gnus-summary-position-cursor)))
8021
8022 (defun gnus-summary-best-unread-article ()
8023   "Select the unread article with the highest score."
8024   (interactive)
8025   (gnus-set-global-variables)
8026   (let ((scored gnus-newsgroup-scored)
8027         (best -1000000)
8028         article art)
8029     (while scored
8030       (or (> best (cdr (car scored)))
8031           (and (memq (setq art (car (car scored))) gnus-newsgroup-unreads)
8032                (not (memq art gnus-newsgroup-marked))
8033                (not (memq art gnus-newsgroup-dormant))
8034                (if (= best (cdr (car scored)))
8035                    (setq article (min art article))
8036                  (setq article art)
8037                  (setq best (cdr (car scored))))))
8038       (setq scored (cdr scored)))
8039     (if article 
8040         (gnus-summary-goto-article article)
8041       (gnus-summary-first-unread-article))
8042     (gnus-summary-position-cursor)))
8043
8044 (defun gnus-summary-goto-article (article &optional all-headers)
8045   "Fetch ARTICLE and display it if it exists.
8046 If ALL-HEADERS is non-nil, no header lines are hidden."
8047   (interactive
8048    (list
8049     (string-to-int
8050      (completing-read 
8051       "Article number: "
8052       (mapcar (lambda (headers) (list (int-to-string (header-number headers))))
8053               gnus-newsgroup-headers) 
8054       nil 'require-match))))
8055   (prog1
8056       (and (gnus-summary-goto-subject article)
8057            (gnus-summary-display-article article all-headers))
8058     (gnus-summary-position-cursor)))
8059
8060 (defun gnus-summary-goto-last-article ()
8061   "Go to the previously read article."
8062   (interactive)
8063   (prog1
8064       (and gnus-last-article
8065            (gnus-summary-goto-article gnus-last-article))
8066     (gnus-summary-position-cursor)))
8067
8068 (defun gnus-summary-pop-article (number)
8069   "Pop one article off the history and go to the previous.
8070 NUMBER articles will be popped off."
8071   (interactive "p")
8072   (let (to)
8073     (setq gnus-newsgroup-history
8074           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
8075     (if to
8076         (gnus-summary-goto-article (car to))
8077       (error "Article history empty")))
8078   (gnus-summary-position-cursor))
8079
8080 ;; Summary article oriented commands
8081
8082 (defun gnus-summary-refer-parent-article (n)
8083   "Refer parent article N times.
8084 The difference between N and the number of articles fetched is returned."
8085   (interactive "p")
8086   (gnus-set-global-variables)
8087   (while 
8088       (and 
8089        (> n 0)
8090        (let ((ref (header-references (gnus-get-header-by-number
8091                                       (gnus-summary-article-number)))))
8092          (if (and ref (not (equal ref ""))
8093                   (string-match "<[^<>]*>[ \t]*$" ref))
8094              (gnus-summary-refer-article 
8095               (substring ref (match-beginning 0) (match-end 0)))
8096            (gnus-message 1 "No references in article %d"
8097                          (gnus-summary-article-number))
8098            nil)))
8099     (setq n (1- n)))
8100   (gnus-summary-position-cursor)
8101   n)
8102     
8103 (defun gnus-summary-refer-article (message-id)
8104   "Refer article specified by MESSAGE-ID.
8105 NOTE: This command only works with newsgroups that use real or simulated NNTP."
8106   (interactive "sMessage-ID: ")
8107   (if (or (not (stringp message-id))
8108           (zerop (length message-id)))
8109       ()
8110     ;; Construct the correct Message-ID if necessary.
8111     ;; Suggested by tale@pawl.rpi.edu.
8112     (or (string-match "^<" message-id)
8113         (setq message-id (concat "<" message-id)))
8114     (or (string-match ">$" message-id)
8115         (setq message-id (concat message-id ">")))
8116     (let ((header (car (gnus-gethash (downcase message-id)
8117                                      gnus-newsgroup-dependencies))))
8118       (if header
8119           (or (gnus-summary-goto-article (header-number header))
8120               ;; The header has been read, but the article had been
8121               ;; expunged, so we insert it again.
8122               (progn
8123                 (gnus-summary-insert-line
8124                  nil header 0 nil gnus-read-mark nil nil
8125                  (header-subject header))
8126                 (forward-line -1)
8127                 (header-number header)))
8128         (let ((gnus-override-method gnus-refer-article-method)
8129               (gnus-ancient-mark gnus-read-mark)
8130               (tmp-point (window-start
8131                           (get-buffer-window gnus-article-buffer)))
8132               number tmp-buf)
8133           (and gnus-refer-article-method
8134                (or (gnus-server-opened gnus-refer-article-method)
8135                    (gnus-open-server gnus-refer-article-method)))
8136           ;; Save the old article buffer.
8137           (save-excursion
8138             (set-buffer gnus-article-buffer)
8139             (gnus-kill-buffer " *temp Article*")
8140             (setq tmp-buf (rename-buffer " *temp Article*")))
8141           (prog1
8142               (if (gnus-article-prepare 
8143                    message-id nil (gnus-read-header message-id))
8144                   (progn
8145                     (setq number (header-number gnus-current-headers))
8146                     (gnus-rebuild-thread message-id)
8147                     (gnus-summary-goto-subject number)
8148                     (gnus-summary-recenter)
8149                     (gnus-article-set-window-start 
8150                      (cdr (assq number gnus-newsgroup-bookmarks)))
8151                     message-id)
8152                 ;; We restore the old article buffer.
8153                 (save-excursion
8154                   (kill-buffer gnus-article-buffer)
8155                   (set-buffer tmp-buf)
8156                   (rename-buffer gnus-article-buffer)
8157                   (let ((buffer-read-only nil))
8158                     (and tmp-point
8159                          (set-window-start (get-buffer-window (current-buffer))
8160                                            tmp-point)))))))))))
8161
8162 (defun gnus-summary-enter-digest-group ()
8163   "Enter a digest group based on the current article."
8164   (interactive)
8165   (gnus-set-global-variables)
8166   (gnus-summary-select-article)
8167   ;; We do not want a narrowed article.
8168   (gnus-summary-stop-page-breaking)
8169   (let ((name (format "%s-%d" 
8170                       (gnus-group-prefixed-name 
8171                        gnus-newsgroup-name (list 'nndoc "")) 
8172                       gnus-current-article))
8173         (ogroup gnus-newsgroup-name)
8174         (buf (current-buffer)))
8175     (if (gnus-group-read-ephemeral-group 
8176          name (list 'nndoc name
8177                     (list 'nndoc-address (get-buffer gnus-article-buffer))
8178                     '(nndoc-article-type digest))
8179          t)
8180         (setcdr (nthcdr 4 (nth 2 (gnus-gethash name gnus-newsrc-hashtb)))
8181                 (list (list (cons 'to-group ogroup))))
8182       (switch-to-buffer buf)
8183       (gnus-set-global-variables)
8184       (gnus-configure-windows 'summary)
8185       (gnus-message 3 "Article not a digest?"))))
8186
8187 (defun gnus-summary-isearch-article ()
8188   "Do incremental search forward on current article."
8189   (interactive)
8190   (gnus-set-global-variables)
8191   (gnus-summary-select-article)
8192   (gnus-eval-in-buffer-window 
8193    gnus-article-buffer (isearch-forward)))
8194
8195 (defun gnus-summary-search-article-forward (regexp &optional backward)
8196   "Search for an article containing REGEXP forward.
8197 If BACKWARD, search backward instead."
8198   (interactive
8199    (list (read-string
8200           (format "Search article %s (regexp%s): "
8201                   (if current-prefix-arg "backward" "forward")
8202                   (if gnus-last-search-regexp
8203                       (concat ", default " gnus-last-search-regexp)
8204                     "")))
8205          current-prefix-arg))
8206   (gnus-set-global-variables)
8207   (if (string-equal regexp "")
8208       (setq regexp (or gnus-last-search-regexp ""))
8209     (setq gnus-last-search-regexp regexp))
8210   (if (gnus-summary-search-article regexp backward)
8211       (gnus-article-set-window-start 
8212        (cdr (assq (gnus-summary-article-number) gnus-newsgroup-bookmarks)))
8213     (error "Search failed: \"%s\"" regexp)))
8214
8215 (defun gnus-summary-search-article-backward (regexp)
8216   "Search for an article containing REGEXP backward."
8217   (interactive
8218    (list (read-string
8219           (format "Search article backward (regexp%s): "
8220                   (if gnus-last-search-regexp
8221                       (concat ", default " gnus-last-search-regexp)
8222                     "")))))
8223   (gnus-summary-search-article-forward regexp 'backward))
8224
8225 (defun gnus-summary-search-article (regexp &optional backward)
8226   "Search for an article containing REGEXP.
8227 Optional argument BACKWARD means do search for backward.
8228 gnus-select-article-hook is not called during the search."
8229   (let ((gnus-select-article-hook nil)  ;Disable hook.
8230         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8231         (re-search
8232          (if backward
8233              (function re-search-backward) (function re-search-forward)))
8234         (found nil)
8235         (last nil))
8236     ;; Hidden thread subtrees must be searched for ,too.
8237     (gnus-summary-show-all-threads)
8238     (if (eobp) (forward-line -1))
8239     ;; First of all, search current article.
8240     ;; We don't want to read article again from NNTP server nor reset
8241     ;; current point.
8242     (gnus-summary-select-article)
8243     (gnus-message 9 "Searching article: %d..." gnus-current-article)
8244     (setq last gnus-current-article)
8245     (gnus-eval-in-buffer-window gnus-article-buffer
8246       (save-restriction
8247         (widen)
8248         ;; Begin search from current point.
8249         (setq found (funcall re-search regexp nil t))))
8250     ;; Then search next articles.
8251     (while (and (not found)
8252                 (gnus-summary-display-article 
8253                  (gnus-summary-search-subject backward nil nil)))
8254       (gnus-message 9 "Searching article: %d..." gnus-current-article)
8255       (gnus-eval-in-buffer-window gnus-article-buffer
8256         (save-restriction
8257           (widen)
8258           (goto-char (if backward (point-max) (point-min)))
8259           (setq found (funcall re-search regexp nil t)))))
8260     (message "")
8261     ;; Adjust article pointer.
8262     (or (eq last gnus-current-article)
8263         (setq gnus-last-article last))
8264     ;; Return T if found such article.
8265     found))
8266
8267 (defun gnus-summary-execute-command (header regexp command &optional backward)
8268   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8269 If HEADER is an empty string (or nil), the match is done on the entire
8270 article. If BACKWARD (the prefix) is non-nil, search backward instead."
8271   (interactive
8272    (list (let ((completion-ignore-case t))
8273            (completing-read 
8274             "Header name: "
8275             (mapcar (lambda (string) (list string))
8276                     '("Number" "Subject" "From" "Lines" "Date"
8277                       "Message-ID" "Xref" "References"))
8278             nil 'require-match))
8279          (read-string "Regexp: ")
8280          (read-key-sequence "Command: ")
8281          current-prefix-arg))
8282   (gnus-set-global-variables)
8283   ;; Hidden thread subtrees must be searched as well.
8284   (gnus-summary-show-all-threads)
8285   ;; We don't want to change current point nor window configuration.
8286   (save-excursion
8287     (save-window-excursion
8288       (gnus-message 6 "Executing %s..." (key-description command))
8289       ;; We'd like to execute COMMAND interactively so as to give arguments.
8290       (gnus-execute header regexp
8291                     (` (lambda ()
8292                          (call-interactively '(, (key-binding command)))))
8293                     backward)
8294       (gnus-message 6 "Executing %s...done" (key-description command)))))
8295
8296 (defun gnus-summary-beginning-of-article ()
8297   "Scroll the article back to the beginning."
8298   (interactive)
8299   (gnus-set-global-variables)
8300   (gnus-summary-select-article)
8301   (gnus-configure-windows 'article)
8302   (gnus-eval-in-buffer-window
8303    gnus-article-buffer
8304    (widen)
8305    (goto-char (point-min))
8306    (and gnus-break-pages (gnus-narrow-to-page))))
8307
8308 (defun gnus-summary-end-of-article ()
8309   "Scroll to the end of the article."
8310   (interactive)
8311   (gnus-set-global-variables)
8312   (gnus-summary-select-article)
8313   (gnus-configure-windows 'article)
8314   (gnus-eval-in-buffer-window 
8315    gnus-article-buffer
8316    (widen)
8317    (goto-char (point-max))
8318    (and gnus-break-pages (gnus-narrow-to-page))))
8319
8320 (defun gnus-summary-show-article (no-refetch)
8321   "Force re-fetching of the current article.
8322 If the prefix argument NO-REFETCH is non-nil, no actual refetch will
8323 be performed.  The current article will simply be redisplayed."
8324   (interactive "P")
8325   (gnus-set-global-variables)
8326   (if (not no-refetch)
8327       (gnus-summary-select-article gnus-have-all-headers t)
8328     (or gnus-current-article
8329         (error "There is no current article"))
8330     (gnus-summary-goto-subject gnus-current-article)
8331     (gnus-configure-windows 'article)
8332     (gnus-summary-position-cursor)))
8333
8334 (defun gnus-summary-verbose-headers (arg)
8335   "Toggle permanent full header display.
8336 If ARG is a positive number, turn header display on.
8337 If ARG is a negative number, turn header display off."
8338   (interactive "P")
8339   (gnus-set-global-variables)
8340   (gnus-summary-toggle-header arg)
8341   (setq gnus-have-all-headers
8342         (cond ((or (not (numberp arg))
8343                    (zerop arg))
8344                (not gnus-have-all-headers))
8345               ((natnump arg)
8346                t))))
8347
8348 (defun gnus-summary-toggle-header (arg)
8349   "Show the headers if they are hidden, or hide them if they are shown.
8350 If ARG is a positive number, show the entire header.
8351 If ARG is a negative number, hide the unwanted header lines."
8352   (interactive "P")
8353   (gnus-set-global-variables)
8354   (save-excursion
8355     (set-buffer gnus-article-buffer)
8356     (let ((buffer-read-only nil))
8357       (if (numberp arg) 
8358           (if (> arg 0) (remove-text-properties (point-min) (point-max) 
8359                                                 gnus-hidden-properties)
8360             (if (< arg 0) (run-hooks 'gnus-article-display-hook)))
8361         (if (text-property-any (point-min) (point-max) 'invisible t)
8362             (remove-text-properties (point-min) (point-max)
8363                                     gnus-hidden-properties)
8364           (let ((gnus-have-all-headers nil))
8365             (run-hooks 'gnus-article-display-hook))))
8366       (set-window-point (get-buffer-window (current-buffer)) (point-min)))))
8367
8368 (defun gnus-summary-show-all-headers ()
8369   "Make all header lines visible."
8370   (interactive)
8371   (gnus-set-global-variables)
8372   (gnus-article-show-all-headers))
8373
8374 (defun gnus-summary-toggle-mime (arg)
8375   "Toggle MIME processing.
8376 If ARG is a positive number, turn MIME processing on."
8377   (interactive "P")
8378   (gnus-set-global-variables)
8379   (setq gnus-show-mime
8380         (if (null arg) (not gnus-show-mime)
8381           (> (prefix-numeric-value arg) 0)))
8382   (gnus-summary-select-article t 'force))
8383
8384 (defun gnus-summary-caesar-message (arg)
8385   "Caesar rotate the current article by 13.
8386 The numerical prefix specifies how manu places to rotate each letter
8387 forward."
8388   (interactive "P")
8389   (gnus-set-global-variables)
8390   (gnus-summary-select-article)
8391   (let ((mail-header-separator ""))
8392     (gnus-eval-in-buffer-window 
8393      gnus-article-buffer
8394      (save-restriction
8395        (widen)
8396        (let ((last (point)))
8397          (news-caesar-buffer-body arg)
8398          (goto-char last)
8399          (recenter 0))))))
8400
8401 (defun gnus-summary-stop-page-breaking ()
8402   "Stop page breaking in the current article."
8403   (interactive)
8404   (gnus-set-global-variables)
8405   (gnus-summary-select-article)
8406   (gnus-eval-in-buffer-window gnus-article-buffer (widen)))
8407
8408 ;; Suggested by Brian Edmonds <bedmonds@prodigy.bc.ca>.
8409
8410 (defun gnus-summary-move-article (n &optional to-newsgroup select-method)
8411   "Move the current article to a different newsgroup.
8412 If N is a positive number, move the N next articles.
8413 If N is a negative number, move the N previous articles.
8414 If N is nil and any articles have been marked with the process mark,
8415 move those articles instead.
8416 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to. 
8417 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
8418 re-spool using this method.
8419 For this function to work, both the current newsgroup and the
8420 newsgroup that you want to move to have to support the `request-move'
8421 and `request-accept' functions. (Ie. mail newsgroups at present.)"
8422   (interactive "P")
8423   (gnus-set-global-variables)
8424   (or (gnus-check-backend-function 'request-move-article gnus-newsgroup-name)
8425       (error "The current newsgroup does not support article moving"))
8426   (let ((articles (gnus-summary-work-articles n))
8427         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
8428         art-group to-method sel-met)
8429     (if (and (not to-newsgroup) (not select-method))
8430         (setq to-newsgroup
8431               (completing-read 
8432                (format "Where do you want to move %s? %s"
8433                        (if (> (length articles) 1)
8434                            (format "these %d articles" (length articles))
8435                          "this article")
8436                        (if gnus-current-move-group
8437                            (format "(%s default) " gnus-current-move-group)
8438                          ""))
8439                gnus-active-hashtb nil nil prefix)))
8440     (if to-newsgroup
8441         (progn
8442           (if (or (string= to-newsgroup "") (string= to-newsgroup prefix))
8443               (setq to-newsgroup (or gnus-current-move-group "")))
8444           (or (gnus-gethash to-newsgroup gnus-active-hashtb)
8445               (gnus-activate-newsgroup to-newsgroup)
8446               (error "No such group: %s" to-newsgroup))
8447           (setq gnus-current-move-group to-newsgroup)))
8448     (setq to-method (if select-method (list select-method "")
8449                       (gnus-find-method-for-group to-newsgroup)))
8450     (or (gnus-check-backend-function 'request-accept-article (car to-method))
8451         (error "%s does not support article copying" (car to-method)))
8452     (or (gnus-server-opened to-method)
8453         (gnus-open-server to-method)
8454         (error "Can't open server %s" (car to-method)))
8455     (gnus-message 6 "Moving to %s: %s..." 
8456                   (or select-method to-newsgroup) articles)
8457     (while articles
8458       (if (setq art-group
8459                 (gnus-request-move-article 
8460                  (car articles)                   ; Article to move
8461                  gnus-newsgroup-name              ; From newsgrouo
8462                  (nth 1 (gnus-find-method-for-group 
8463                          gnus-newsgroup-name))    ; Server
8464                  (list 'gnus-request-accept-article 
8465                        (if select-method
8466                            (list 'quote select-method)
8467                          to-newsgroup)
8468                        (not (cdr articles)))     ; Accept form
8469                  (not (cdr articles))))          ; Only save nov last time
8470           (let* ((buffer-read-only nil)
8471                  (entry 
8472                   (or
8473                    (gnus-gethash (car art-group) gnus-newsrc-hashtb)
8474                    (gnus-gethash 
8475                     (gnus-group-prefixed-name 
8476                      (car art-group) 
8477                      (if select-method (list select-method "")
8478                        (gnus-find-method-for-group to-newsgroup)))
8479                     gnus-newsrc-hashtb)))
8480                  (info (nth 2 entry))
8481                  (article (car articles)))
8482             (gnus-summary-goto-subject article)
8483             (beginning-of-line)
8484             (delete-region (point)
8485                            (progn (forward-line 1) (point)))
8486             (if (not (memq article gnus-newsgroup-unreads))
8487                 (setcar (cdr (cdr info))
8488                         (gnus-add-to-range (nth 2 info) 
8489                                            (list (cdr art-group)))))
8490             ;; Copy any marks over to the new group.
8491             (let ((marks '((tick . gnus-newsgroup-marked)
8492                            (dormant . gnus-newsgroup-dormant)
8493                            (expire . gnus-newsgroup-expirable)
8494                            (bookmark . gnus-newsgroup-bookmarks)
8495                         ;   (score . gnus-newsgroup-scored)
8496                            (reply . gnus-newsgroup-replied)))
8497                   (to-article (cdr art-group)))
8498               (while marks
8499                 (if (memq article (symbol-value (cdr (car marks))))
8500                     (gnus-add-marked-articles 
8501                      (car info) (car (car marks)) (list to-article) info))
8502                 (setq marks (cdr marks))))
8503             (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8504             (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8505             (setq gnus-newsgroup-dormant
8506                   (delq article gnus-newsgroup-dormant)))
8507         (gnus-message 1 "Couldn't move article %s" (car articles)))
8508       (gnus-summary-remove-process-mark (car articles))
8509       (setq articles (cdr articles)))))
8510
8511 (defun gnus-summary-respool-article (n &optional respool-method)
8512   "Respool the current article.
8513 The article will be squeezed through the mail spooling process again,
8514 which means that it will be put in some mail newsgroup or other
8515 depending on `nnmail-split-methods'.
8516 If N is a positive number, respool the N next articles.
8517 If N is a negative number, respool the N previous articles.
8518 If N is nil and any articles have been marked with the process mark,
8519 respool those articles instead.
8520
8521 Respooling can be done both from mail groups and \"real\" newsgroups.
8522 In the former case, the articles in question will be moved from the
8523 current group into whatever groups they are destined to.  In the
8524 latter case, they will be copied into the relevant groups."
8525   (interactive "P")
8526   (gnus-set-global-variables)
8527   (let ((respool-methods (gnus-methods-using 'respool))
8528         (methname 
8529          (symbol-name (car (gnus-find-method-for-group gnus-newsgroup-name)))))
8530     (or respool-method
8531         (setq respool-method
8532               (completing-read
8533                "What method do you want to use when respooling? "
8534                respool-methods nil t methname)))
8535     (or (string= respool-method "")
8536         (if (assoc (symbol-name
8537                     (car (gnus-find-method-for-group gnus-newsgroup-name)))
8538                    respool-methods)
8539             (gnus-summary-move-article n nil (intern respool-method))
8540           (gnus-summary-copy-article n nil (intern respool-method))))))
8541
8542 ;; Suggested by gregj@unidata.com (Gregory J. Grubbs).
8543 (defun gnus-summary-copy-article (n &optional to-newsgroup select-method)
8544   "Move the current article to a different newsgroup.
8545 If N is a positive number, move the N next articles.
8546 If N is a negative number, move the N previous articles.
8547 If N is nil and any articles have been marked with the process mark,
8548 move those articles instead.
8549 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to. 
8550 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
8551 re-spool using this method.
8552 For this function to work, the newsgroup that you want to move to have
8553 to support the `request-move' and `request-accept'
8554 functions. (Ie. mail newsgroups at present.)"
8555   (interactive "P")
8556   (gnus-set-global-variables)
8557   (let ((articles (gnus-summary-work-articles n))
8558         (copy-buf (get-buffer-create "*copy work*"))
8559         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
8560         art-group to-method)
8561     (buffer-disable-undo copy-buf)
8562     (if (and (not to-newsgroup) (not select-method))
8563         (setq to-newsgroup
8564               (completing-read 
8565                (format "Where do you want to copy %s? %s"
8566                        (if (> (length articles) 1)
8567                            (format "these %d articles" (length articles))
8568                          "this article")
8569                        (if gnus-current-move-group
8570                            (format "(%s default) " gnus-current-move-group)
8571                          ""))
8572                gnus-active-hashtb nil nil prefix)))
8573     (if to-newsgroup
8574         (progn
8575           (if (or (string= to-newsgroup "") (string= to-newsgroup prefix))
8576               (setq to-newsgroup (or gnus-current-move-group "")))
8577           (or (gnus-gethash to-newsgroup gnus-active-hashtb)
8578               (gnus-activate-newsgroup to-newsgroup)
8579               (error "No such group: %s" to-newsgroup))
8580           (setq gnus-current-move-group to-newsgroup)))
8581     (setq to-method (if select-method (list select-method "")
8582                       (gnus-find-method-for-group to-newsgroup)))
8583     (or (gnus-check-backend-function 'request-accept-article (car to-method))
8584         (error "%s does not support article copying" (car to-method)))
8585     (or (gnus-server-opened to-method)
8586         (gnus-open-server to-method)
8587         (error "Can't open server %s" (car to-method)))
8588     (gnus-message 6 "Copying to %s: %s..." 
8589                   (or select-method to-newsgroup) articles)
8590     (while articles
8591       (if (setq art-group
8592                 (save-excursion
8593                   (set-buffer copy-buf)
8594                   (gnus-request-article-this-buffer
8595                    (car articles) gnus-newsgroup-name)
8596                   (gnus-request-accept-article
8597                    (if select-method (quote select-method) to-newsgroup)
8598                    (not (cdr articles)))))
8599           (let* ((entry 
8600                   (or
8601                    (gnus-gethash (car art-group) gnus-newsrc-hashtb)
8602                    (gnus-gethash 
8603                     (gnus-group-prefixed-name 
8604                      (car art-group) 
8605                      (if select-method (list select-method "")
8606                        (gnus-find-method-for-group to-newsgroup)))
8607                     gnus-newsrc-hashtb)))
8608                  (info (nth 2 entry))
8609                  (article (car articles)))
8610             (if (not (memq article gnus-newsgroup-unreads))
8611                 (setcar (cdr (cdr info))
8612                         (gnus-add-to-range (nth 2 info) 
8613                                            (list (cdr art-group)))))
8614             ;; Copy any marks over to the new group.
8615             (let ((marks '((tick . gnus-newsgroup-marked)
8616                            (dormant . gnus-newsgroup-dormant)
8617                            (expire . gnus-newsgroup-expirable)
8618                            (bookmark . gnus-newsgroup-bookmarks)
8619                         ;   (score . gnus-newsgroup-scored)
8620                            (reply . gnus-newsgroup-replied)))
8621                   (to-article (cdr art-group)))
8622               (while marks
8623                 (if (memq article (symbol-value (cdr (car marks))))
8624                     (gnus-add-marked-articles 
8625                      (car info) (car (car marks)) (list to-article) info))
8626                 (setq marks (cdr marks)))))
8627         (gnus-message 1 "Couldn't copy article %s" (car articles)))
8628       (gnus-summary-remove-process-mark (car articles))
8629       (setq articles (cdr articles)))
8630     (kill-buffer copy-buf)))
8631
8632 (defun gnus-summary-import-article (file)
8633   "Import a random file into a mail newsgroup."
8634   (interactive "fImport file: ")
8635   (let ((group gnus-newsgroup-name)
8636         atts)
8637     (or (gnus-check-backend-function 'request-accept-article group)
8638         (error "%s does not support article importing" group))
8639     (or (file-readable-p file)
8640         (not (file-regular-p file))
8641         (error "Can't read %s" file))
8642     (save-excursion
8643       (set-buffer (get-buffer-create " *import file*"))
8644       (buffer-disable-undo (current-buffer))
8645       (erase-buffer)
8646       (insert-file-contents file)
8647       (goto-char (point-min))
8648       (if (nnheader-article-p)
8649           ()
8650         (setq atts (file-attributes file))
8651         (insert "From: " (read-string "From: ") "\n"
8652                 "Subject: " (read-string "Subject: ") "\n"
8653                 "Date: " (current-time-string (nth 5 atts)) "\n"
8654                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8655       (gnus-request-accept-article group t)
8656       (kill-buffer (current-buffer)))))
8657
8658 (defun gnus-summary-expire-articles ()
8659   "Expire all articles that are marked as expirable in the current group."
8660   (interactive)
8661   (if (not (gnus-check-backend-function 
8662             'request-expire-articles gnus-newsgroup-name))
8663       ()
8664     (let* ((info (nth 2 (gnus-gethash gnus-newsgroup-name 
8665                                       gnus-newsrc-hashtb)))
8666            (total (memq 'total-expire (nth 5 info)))
8667            (expirable (if total
8668                           (gnus-list-of-read-articles gnus-newsgroup-name)
8669                         (setq gnus-newsgroup-expirable
8670                               (sort gnus-newsgroup-expirable '<))))
8671            es)
8672       (if (not expirable)
8673           ()
8674         (gnus-message 6 "Expiring articles...")
8675         ;; The list of articles that weren't expired is returned.
8676         (setq es (gnus-request-expire-articles expirable gnus-newsgroup-name))
8677         (or total (setq gnus-newsgroup-expirable es))
8678         ;; We go through the old list of expirable, and mark all
8679         ;; really expired articles as non-existant.
8680         (or (eq es expirable) ; If nothing was expired, we don't mark.
8681             (while expirable
8682               (or (memq (car expirable) es)
8683                   (gnus-summary-mark-article
8684                    (car expirable) gnus-canceled-mark))
8685               (setq expirable (cdr expirable))))
8686         (gnus-message 6 "Expiring articles...done")))))
8687
8688 (defun gnus-summary-expire-articles-now ()
8689   "Expunge all expirable articles in the current group.
8690 This means that *all* articles that are marked as expirable will be
8691 deleted forever, right now."
8692   (interactive)
8693   (or gnus-expert-user
8694       (gnus-y-or-n-p
8695        "Are you really, really, really sure you want to expunge? ")
8696       (error "Phew!"))
8697   (let ((nnmail-expiry-wait -1)
8698         (nnmail-expiry-wait-function nil))
8699     (gnus-summary-expire-articles)))
8700
8701 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
8702 (defun gnus-summary-delete-article (n)
8703   "Delete the N next (mail) articles.
8704 This command actually deletes articles. This is not a marking
8705 command. The article will disappear forever from you life, never to
8706 return. 
8707 If N is negative, delete backwards.
8708 If N is nil and articles have been marked with the process mark,
8709 delete these instead."
8710   (interactive "P")
8711   (or (gnus-check-backend-function 'request-expire-articles 
8712                                    gnus-newsgroup-name)
8713       (error "The current newsgroup does not support article deletion."))
8714   ;; Compute the list of articles to delete.
8715   (let ((articles (gnus-summary-work-articles n))
8716         not-deleted)
8717     (if (and gnus-novice-user
8718              (not (gnus-y-or-n-p 
8719                    (format "Do you really want to delete %s forever? "
8720                            (if (> (length articles) 1) "these articles"
8721                              "this article")))))
8722         ()
8723       ;; Delete the articles.
8724       (setq not-deleted (gnus-request-expire-articles 
8725                          articles gnus-newsgroup-name 'force))
8726       (while articles
8727         (gnus-summary-remove-process-mark (car articles))       
8728         ;; The backend might not have been able to delete the article
8729         ;; after all.  
8730         (or (memq (car articles) not-deleted)
8731             (gnus-summary-mark-article (car articles) gnus-canceled-mark))
8732         (setq articles (cdr articles))))
8733     (gnus-summary-position-cursor)
8734     not-deleted))
8735
8736 (defun gnus-summary-edit-article ()
8737   "Enter into a buffer and edit the current article.
8738 This will have permanent effect only in mail groups."
8739   (interactive)
8740   (or (gnus-check-backend-function 
8741        'request-replace-article gnus-newsgroup-name)
8742       (error "The current newsgroup does not support article editing."))
8743   (gnus-summary-select-article t)
8744   (gnus-configure-windows 'article)
8745   (select-window (get-buffer-window gnus-article-buffer))
8746   (gnus-message 6 "C-c C-c to end edits")
8747   (setq buffer-read-only nil)
8748   (text-mode)
8749   (use-local-map (copy-keymap (current-local-map)))
8750   (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
8751   (buffer-enable-undo)
8752   (goto-char (point-min))
8753   (search-forward "\n\n" nil t))
8754
8755 (defun gnus-summary-edit-article-done ()
8756   "Make edits to the current article permanent."
8757   (interactive)
8758   (if (not (gnus-request-replace-article 
8759             (cdr gnus-article-current) (car gnus-article-current) 
8760             (current-buffer)))
8761       (error "Couldn't replace article.")
8762     (gnus-article-mode)
8763     (use-local-map gnus-article-mode-map)
8764     (setq buffer-read-only t)
8765     (buffer-disable-undo (current-buffer))
8766     (pop-to-buffer gnus-summary-buffer)))      
8767
8768 (defun gnus-summary-fancy-query ()
8769   "Query where the fancy respool algorithm would put this article."
8770   (interactive)
8771   (gnus-summary-select-article)
8772   (save-excursion
8773     (set-buffer gnus-article-buffer)
8774     (save-restriction
8775       (goto-char (point-min))
8776       (search-forward "\n\n")
8777       (narrow-to-region (point-min) (point))
8778       (pp-eval-expression (list 'quote (nnmail-split-fancy))))))
8779
8780 ;; Summary score commands.
8781
8782 ;; Suggested by boubaker@cenatls.cena.dgac.fr.
8783
8784 (defun gnus-summary-raise-score (n)
8785   "Raise the score of the current article by N."
8786   (interactive "p")
8787   (gnus-summary-set-score (+ (gnus-summary-article-score) n)))
8788
8789 (defun gnus-summary-lower-score (n)
8790   "Lower the score of the current article by N."
8791   (interactive "p")
8792   (gnus-summary-raise-score (- n)))
8793
8794 (defun gnus-summary-set-score (n)
8795   "Set the score of the current article to N."
8796   (interactive "p")
8797   ;; Skip dummy header line.
8798   (save-excursion
8799     (gnus-summary-show-thread)
8800     (if (eq (gnus-summary-article-mark) gnus-dummy-mark)
8801         (forward-line 1))
8802     (let ((buffer-read-only nil))
8803       ;; Set score.
8804       (gnus-summary-update-mark
8805        (if (= n (or gnus-summary-default-score 0)) ? 
8806          (if (< n (or gnus-summary-default-score 0)) 
8807              gnus-score-below-mark gnus-score-over-mark)) 'score))
8808     (let* ((article (gnus-summary-article-number))
8809            (score (assq article gnus-newsgroup-scored)))
8810       (if score (setcdr score n)
8811         (setq gnus-newsgroup-scored 
8812               (cons (cons article n) gnus-newsgroup-scored))))
8813     (gnus-summary-update-line)))
8814
8815 (defun gnus-summary-current-score ()
8816   "Return the score of the current article."
8817   (interactive)
8818   (message "%s" (gnus-summary-article-score)))
8819
8820 ;; Summary marking commands.
8821
8822 (defun gnus-summary-raise-same-subject-and-select (score)
8823   "Raise articles which has the same subject with SCORE and select the next."
8824   (interactive "p")
8825   (let ((subject (gnus-summary-subject-string)))
8826     (gnus-summary-raise-score score)
8827     (while (gnus-summary-search-subject nil nil subject)
8828       (gnus-summary-raise-score score))
8829     (gnus-summary-next-article t)))
8830
8831 (defun gnus-summary-raise-same-subject (score)
8832   "Raise articles which has the same subject with SCORE."
8833   (interactive "p")
8834   (let ((subject (gnus-summary-subject-string)))
8835     (gnus-summary-raise-score score)
8836     (while (gnus-summary-search-subject nil nil subject)
8837       (gnus-summary-raise-score score))
8838     (gnus-summary-next-subject 1 t)))
8839
8840 (defun gnus-score-default (level)
8841   (if level (prefix-numeric-value level) 
8842     gnus-score-interactive-default-score))
8843
8844 (defun gnus-summary-raise-thread (score)
8845   "Raise the score of the articles in the current thread with SCORE."
8846   (interactive "P")
8847   (setq score (1- (gnus-score-default score)))
8848   (let (e)
8849     (save-excursion
8850       (let ((level (gnus-summary-thread-level)))
8851         (gnus-summary-raise-score score)
8852         (while (and (zerop (gnus-summary-next-subject 1 nil t))
8853                     (> (gnus-summary-thread-level) level))
8854           (gnus-summary-raise-score score))
8855         (setq e (point))))
8856     (let ((gnus-summary-check-current t))
8857       (or (zerop (gnus-summary-next-subject 1 t))
8858           (goto-char e))))
8859   (gnus-summary-recenter)
8860   (gnus-summary-position-cursor)
8861   (gnus-set-mode-line 'summary))
8862
8863 (defun gnus-summary-lower-same-subject-and-select (score)
8864   "Raise articles which has the same subject with SCORE and select the next."
8865   (interactive "p")
8866   (gnus-summary-raise-same-subject-and-select (- score)))
8867
8868 (defun gnus-summary-lower-same-subject (score)
8869   "Raise articles which has the same subject with SCORE."
8870   (interactive "p")
8871   (gnus-summary-raise-same-subject (- score)))
8872
8873 (defun gnus-summary-lower-thread (score)
8874   "Lower score of articles in the current thread with SCORE."
8875   (interactive "P")
8876   (gnus-summary-raise-thread (- (1- (gnus-score-default score)))))
8877
8878 (defun gnus-summary-kill-same-subject-and-select (unmark)
8879   "Mark articles which has the same subject as read, and then select the next.
8880 If UNMARK is positive, remove any kind of mark.
8881 If UNMARK is negative, tick articles."
8882   (interactive "P")
8883   (if unmark
8884       (setq unmark (prefix-numeric-value unmark)))
8885   (let ((count
8886          (gnus-summary-mark-same-subject
8887           (gnus-summary-subject-string) unmark)))
8888     ;; Select next unread article. If auto-select-same mode, should
8889     ;; select the first unread article.
8890     (gnus-summary-next-article t (and gnus-auto-select-same
8891                                       (gnus-summary-subject-string)))
8892     (gnus-message 7 "%d articles are marked as %s"
8893                   count (if unmark "unread" "read"))))
8894
8895 (defun gnus-summary-kill-same-subject (unmark)
8896   "Mark articles which has the same subject as read. 
8897 If UNMARK is positive, remove any kind of mark.
8898 If UNMARK is negative, tick articles."
8899   (interactive "P")
8900   (if unmark
8901       (setq unmark (prefix-numeric-value unmark)))
8902   (let ((count
8903          (gnus-summary-mark-same-subject
8904           (gnus-summary-subject-string) unmark)))
8905     ;; If marked as read, go to next unread subject.
8906     (if (null unmark)
8907         ;; Go to next unread subject.
8908         (gnus-summary-next-subject 1 t))
8909     (gnus-message 7 "%d articles are marked as %s"
8910                   count (if unmark "unread" "read"))))
8911
8912 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8913   "Mark articles with same SUBJECT as read, and return marked number.
8914 If optional argument UNMARK is positive, remove any kinds of marks.
8915 If optional argument UNMARK is negative, mark articles as unread instead."
8916   (let ((count 1))
8917     (save-excursion
8918       (cond ((null unmark)
8919              (gnus-summary-mark-article nil gnus-killed-mark))
8920             ((> unmark 0)
8921              (gnus-summary-tick-article nil t))
8922             (t
8923              (gnus-summary-tick-article)))
8924       (while (and subject
8925                   (gnus-summary-search-forward nil subject))
8926         (cond ((null unmark)
8927                (gnus-summary-mark-article nil gnus-killed-mark))
8928               ((> unmark 0)
8929                (gnus-summary-tick-article nil t))
8930               (t
8931                (gnus-summary-tick-article)))
8932         (setq count (1+ count))))
8933     ;; Hide killed thread subtrees.  Does not work properly always.
8934     ;;(and (null unmark)
8935     ;;     gnus-thread-hide-killed
8936     ;;     (gnus-summary-hide-thread))
8937     ;; Return number of articles marked as read.
8938     count))
8939
8940 (defun gnus-summary-mark-as-processable (n &optional unmark)
8941   "Set the process mark on the next N articles.
8942 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8943 the process mark instead.  The difference between N and the actual
8944 number of articles marked is returned."
8945   (interactive "p")
8946   (let ((backward (< n 0))
8947         (n (abs n)))
8948   (while (and 
8949           (> n 0)
8950           (if unmark
8951               (gnus-summary-remove-process-mark (gnus-summary-article-number))
8952             (gnus-summary-set-process-mark (gnus-summary-article-number)))
8953           (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8954     (setq n (1- n)))
8955   (if (/= 0 n) (gnus-message 7 "No more articles"))
8956   (gnus-summary-recenter)
8957   (gnus-summary-position-cursor)
8958   n))
8959
8960 (defun gnus-summary-unmark-as-processable (n)
8961   "Remove the process mark from the next N articles.
8962 If N is negative, mark backward instead.  The difference between N and
8963 the actual number of articles marked is returned."
8964   (interactive "p")
8965   (gnus-summary-mark-as-processable n t))
8966
8967 (defun gnus-summary-unmark-all-processable ()
8968   "Remove the process mark from all articles."
8969   (interactive)
8970   (save-excursion
8971     (while gnus-newsgroup-processable
8972       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8973   (gnus-summary-position-cursor))
8974
8975 (defun gnus-summary-mark-as-expirable (n)
8976   "Mark N articles forward as expirable.
8977 If N is negative, mark backward instead. The difference between N and
8978 the actual number of articles marked is returned."
8979   (interactive "p")
8980   (gnus-summary-mark-forward n gnus-expirable-mark))
8981
8982 (defun gnus-summary-mark-article-as-replied (article)
8983   "Mark ARTICLE replied and update the summary line."
8984   (setq gnus-newsgroup-replied (cons article gnus-newsgroup-replied))
8985   (let ((buffer-read-only nil))
8986     (if (gnus-summary-goto-subject article)
8987         (progn
8988           (gnus-summary-update-mark gnus-replied-mark 'replied)
8989           t))))
8990
8991 (defun gnus-summary-set-bookmark (article)
8992   "Set a bookmark in current article."
8993   (interactive (list (gnus-summary-article-number)))
8994   (if (or (not (get-buffer gnus-article-buffer))
8995           (not gnus-current-article)
8996           (not gnus-article-current)
8997           (not (equal gnus-newsgroup-name (car gnus-article-current))))
8998       (error "No current article selected"))
8999   ;; Remove old bookmark, if one exists.
9000   (let ((old (assq article gnus-newsgroup-bookmarks)))
9001     (if old (setq gnus-newsgroup-bookmarks 
9002                   (delq old gnus-newsgroup-bookmarks))))
9003   ;; Set the new bookmark, which is on the form 
9004   ;; (article-number . line-number-in-body).
9005   (setq gnus-newsgroup-bookmarks 
9006         (cons 
9007          (cons article 
9008                (save-excursion
9009                  (set-buffer gnus-article-buffer)
9010                  (count-lines
9011                   (min (point)
9012                        (save-excursion
9013                          (goto-char (point-min))
9014                          (search-forward "\n\n" nil t)
9015                          (point)))
9016                   (point))))
9017          gnus-newsgroup-bookmarks))
9018   (gnus-message 6 "A bookmark has been added to the current article."))
9019
9020 (defun gnus-summary-remove-bookmark (article)
9021   "Remove the bookmark from the current article."
9022   (interactive (list (gnus-summary-article-number)))
9023   ;; Remove old bookmark, if one exists.
9024   (let ((old (assq article gnus-newsgroup-bookmarks)))
9025     (if old 
9026         (progn
9027           (setq gnus-newsgroup-bookmarks 
9028                 (delq old gnus-newsgroup-bookmarks))
9029           (gnus-message 6 "Removed bookmark."))
9030       (gnus-message 6 "No bookmark in current article."))))
9031
9032 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9033 (defun gnus-summary-mark-as-dormant (n)
9034   "Mark N articles forward as dormant.
9035 If N is negative, mark backward instead.  The difference between N and
9036 the actual number of articles marked is returned."
9037   (interactive "p")
9038   (gnus-summary-mark-forward n gnus-dormant-mark))
9039
9040 (defun gnus-summary-set-process-mark (article)
9041   "Set the process mark on ARTICLE and update the summary line."
9042   (setq gnus-newsgroup-processable (cons article gnus-newsgroup-processable))
9043   (let ((buffer-read-only nil))
9044     (if (gnus-summary-goto-subject article)
9045         (progn
9046           (gnus-summary-show-thread)
9047           (and (eq (gnus-summary-article-mark) gnus-dummy-mark)
9048                (forward-line 1))
9049           (gnus-summary-update-mark gnus-process-mark 'replied)
9050           t))))
9051
9052 (defun gnus-summary-remove-process-mark (article)
9053   "Remove the process mark from ARTICLE and update the summary line."
9054   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9055   (let ((buffer-read-only nil))
9056     (if (gnus-summary-goto-subject article)
9057         (progn
9058           (gnus-summary-show-thread)
9059           (and (eq (gnus-summary-article-mark) gnus-dummy-mark)
9060                (forward-line 1))
9061           (gnus-summary-update-mark ?  'replied)
9062           (if (memq article gnus-newsgroup-replied) 
9063               (gnus-summary-update-mark gnus-replied-mark 'replied))
9064           t))))
9065
9066 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9067   "Mark N articles as read forwards.
9068 If N is negative, mark backwards instead.
9069 Mark with MARK. If MARK is ? , ?! or ??, articles will be
9070 marked as unread. 
9071 The difference between N and the actual number of articles marked is
9072 returned."
9073   (interactive "p")
9074   (gnus-set-global-variables)
9075   (let ((backward (< n 0))
9076         (gnus-summary-goto-unread
9077          (and gnus-summary-goto-unread
9078               (not (memq mark (list gnus-unread-mark
9079                                     gnus-ticked-mark gnus-dormant-mark)))))
9080         (n (abs n))
9081         (mark (or mark gnus-del-mark)))
9082   (while (and (> n 0)
9083               (gnus-summary-mark-article nil mark no-expire)
9084               (zerop (gnus-summary-next-subject 
9085                       (if backward -1 1) gnus-summary-goto-unread t)))
9086     (setq n (1- n)))
9087   (if (/= 0 n) (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9088   (gnus-summary-recenter)
9089   (gnus-summary-position-cursor)
9090   (gnus-set-mode-line 'summary)
9091   n))
9092
9093 (defun gnus-summary-mark-article (&optional article mark no-expire)
9094   "Mark ARTICLE with MARK.
9095 MARK can be any character.
9096 Five MARK strings are reserved: ?  (unread), 
9097 ?! (ticked), ?? (dormant), ?D (read), ?E (expirable).
9098 If MARK is nil, then the default character ?D is used.
9099 If ARTICLE is nil, then the article on the current line will be
9100 marked." 
9101   ;; If no mark is given, then we check auto-expiring.
9102   (and (not no-expire)
9103        gnus-newsgroup-auto-expire 
9104        (or (not mark)
9105            (and (numberp mark) 
9106                 (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
9107                     (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
9108                     (= mark gnus-read-mark))))
9109        (setq mark gnus-expirable-mark))
9110   (let* ((mark (or (and (stringp mark) (aref mark 0)) mark gnus-del-mark))
9111          (article (or article (gnus-summary-article-number))))
9112     (or article (error "No article on current line"))
9113     (if (or (= mark gnus-unread-mark) 
9114             (= mark gnus-ticked-mark) 
9115             (= mark gnus-dormant-mark))
9116         (gnus-mark-article-as-unread article mark)
9117       (gnus-mark-article-as-read article mark))
9118
9119     ;; See whether the article is to be put in the cache.
9120     (and gnus-use-cache
9121          (not (= mark gnus-canceled-mark))
9122          (save-excursion
9123            (gnus-cache-possibly-enter-article 
9124             gnus-newsgroup-name article 
9125             (gnus-get-header-by-number article)
9126             (= mark gnus-ticked-mark)
9127             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9128
9129     (if (gnus-summary-goto-subject article)
9130         (let ((buffer-read-only nil))
9131           (gnus-summary-show-thread)
9132           (and (eq (gnus-summary-article-mark) gnus-dummy-mark)
9133                (forward-line 1))
9134           ;; Fix the mark.
9135           (gnus-summary-update-mark mark 'unread)
9136           t))))
9137
9138 (defun gnus-summary-update-mark (mark type)
9139   (beginning-of-line)
9140   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
9141         plist)
9142     (if (not forward)
9143         ()
9144       (forward-char forward)
9145       (setq plist (text-properties-at (point)))
9146       (delete-char 1)
9147       (insert mark)
9148       (and plist (add-text-properties (1- (point)) (point) plist))
9149       (and (eq type 'unread)
9150            (add-text-properties (1- (point)) (point) (list 'gnus-mark mark)))
9151       (gnus-summary-update-line (eq mark gnus-unread-mark)))))
9152   
9153 (defun gnus-mark-article-as-read (article &optional mark)
9154   "Enter ARTICLE in the pertinent lists and remove it from others."
9155   ;; Make the article expirable.
9156   (let ((mark (or (and (stringp mark) (aref mark 0)) mark gnus-del-mark)))
9157     (if (= mark gnus-expirable-mark)
9158         (setq gnus-newsgroup-expirable (cons article gnus-newsgroup-expirable))
9159       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
9160     ;; Remove from unread and marked lists.
9161     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9162     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9163     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9164     ;; Possibly remove from cache, if that is used. 
9165     (and gnus-use-cache (gnus-cache-enter-remove-article article))))
9166
9167 (defun gnus-mark-article-as-unread (article &optional mark)
9168   "Enter ARTICLE in the pertinent lists and remove it from others."
9169   (let ((mark (or (and (stringp mark) (aref mark 0)) mark gnus-ticked-mark)))
9170     ;; Add to unread list.
9171     (or (memq article gnus-newsgroup-unreads)
9172         (setq gnus-newsgroup-unreads (cons article gnus-newsgroup-unreads)))
9173     ;; If CLEAR-MARK is non-nil, the article must be removed from mark
9174     ;; lists.  Otherwise, it must be added to the list.
9175     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9176     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9177     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9178     (if (= mark gnus-ticked-mark)
9179         (setq gnus-newsgroup-marked (cons article gnus-newsgroup-marked)))
9180     (if (= mark gnus-dormant-mark)
9181         (setq gnus-newsgroup-dormant (cons article gnus-newsgroup-dormant)))))
9182
9183 (defalias 'gnus-summary-mark-as-unread-forward 
9184   'gnus-summary-tick-article-forward)
9185 (make-obsolete 'gnus-summary-mark-as-unread-forward 
9186                'gnus-summary-tick-article-forward)
9187 (defun gnus-summary-tick-article-forward (n)
9188   "Tick N articles forwards.
9189 If N is negative, tick backwards instead.
9190 The difference between N and the number of articles ticked is returned."
9191   (interactive "p")
9192   (gnus-summary-mark-forward n gnus-ticked-mark))
9193
9194 (defalias 'gnus-summary-mark-as-unread-backward 
9195   'gnus-summary-tick-article-backward)
9196 (make-obsolete 'gnus-summary-mark-as-unread-backward 
9197                'gnus-summary-tick-article-backward)
9198 (defun gnus-summary-tick-article-backward (n)
9199   "Tick N articles backwards.
9200 The difference between N and the number of articles ticked is returned."
9201   (interactive "p")
9202   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
9203
9204 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9205 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9206 (defun gnus-summary-tick-article (&optional article clear-mark)
9207   "Mark current article as unread.
9208 Optional 1st argument ARTICLE specifies article number to be marked as unread.
9209 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
9210   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
9211                                        gnus-ticked-mark)))
9212
9213 (defun gnus-summary-mark-as-read-forward (n)
9214   "Mark N articles as read forwards.
9215 If N is negative, mark backwards instead.
9216 The difference between N and the actual number of articles marked is
9217 returned."
9218   (interactive "p")
9219   (gnus-summary-mark-forward n gnus-del-mark t))
9220
9221 (defun gnus-summary-mark-as-read-backward (n)
9222   "Mark the N articles as read backwards.
9223 The difference between N and the actual number of articles marked is
9224 returned."
9225   (interactive "p")
9226   (gnus-summary-mark-forward (- n) gnus-del-mark t))
9227
9228 (defun gnus-summary-mark-as-read (&optional article mark)
9229   "Mark current article as read.
9230 ARTICLE specifies the article to be marked as read.
9231 MARK specifies a string to be inserted at the beginning of the line."
9232   (gnus-summary-mark-article article mark))
9233
9234 (defun gnus-summary-clear-mark-forward (n)
9235   "Clear marks from N articles forward.
9236 If N is negative, clear backward instead.
9237 The difference between N and the number of marks cleared is returned."
9238   (interactive "p")
9239   (gnus-summary-mark-forward n gnus-unread-mark))
9240
9241 (defun gnus-summary-clear-mark-backward (n)
9242   "Clear marks from N articles backward.
9243 The difference between N and the number of marks cleared is returned."
9244   (interactive "p")
9245   (gnus-summary-mark-forward (- n) gnus-unread-mark))
9246
9247 (defun gnus-summary-mark-unread-as-read ()
9248   "Intended to be used by `gnus-summary-mark-article-hook'."
9249   (or (memq gnus-current-article gnus-newsgroup-marked)
9250       (memq gnus-current-article gnus-newsgroup-dormant)
9251       (memq gnus-current-article gnus-newsgroup-expirable)
9252       (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
9253
9254 (defun gnus-summary-mark-region-as-read (point mark all)
9255   "Mark all unread articles between point and mark as read.
9256 If given a prefix, mark all articles between point and mark as read,
9257 even ticked and dormant ones."
9258   (interactive "r\nP")
9259   (save-excursion
9260     (goto-char point)
9261     (beginning-of-line)
9262     (while (and 
9263             (< (point) mark)
9264             (progn
9265               (and
9266                (or all
9267                    (and
9268                     (not (memq (gnus-summary-article-number)
9269                                gnus-newsgroup-marked))
9270                     (not (memq (gnus-summary-article-number)
9271                                gnus-newsgroup-dormant))))
9272                (gnus-summary-mark-article
9273                 (gnus-summary-article-number) gnus-del-mark))
9274               t)
9275             (zerop (forward-line 1))))))
9276
9277 ;; Fix by Per Abrahamsen <amanda@iesd.auc.dk>.
9278 (defalias 'gnus-summary-delete-marked-as-read 
9279   'gnus-summary-remove-lines-marked-as-read)
9280 (make-obsolete 'gnus-summary-delete-marked-as-read 
9281                'gnus-summary-remove-lines-marked-as-read)
9282 (defun gnus-summary-remove-lines-marked-as-read ()
9283   "Remove lines that are marked as read."
9284   (interactive)
9285   (gnus-summary-remove-lines-marked-with 
9286    (concat (mapconcat
9287             (lambda (char) (char-to-string (symbol-value char)))
9288             '(gnus-del-mark gnus-read-mark gnus-ancient-mark
9289               gnus-killed-mark gnus-kill-file-mark
9290               gnus-low-score-mark gnus-expirable-mark
9291               gnus-canceled-mark)
9292             ""))))
9293
9294 (defalias 'gnus-summary-delete-marked-with 
9295   'gnus-summary-remove-lines-marked-with)
9296 (make-obsolete 'gnus-summary-delete-marked-with 
9297                'gnus-summary-remove-lines-marked-with)
9298 ;; Rewrite by Daniel Quinlan <quinlan@best.com>.
9299 (defun gnus-summary-remove-lines-marked-with (marks)
9300   "Remove lines that are marked with MARKS (e.g. \"DK\")."
9301   (interactive "sMarks: ")
9302   ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
9303   (gnus-set-global-variables)
9304   (let ((buffer-read-only nil)
9305         (marks (concat "^[" marks "]")))
9306     (goto-char (point-min))
9307     (if gnus-newsgroup-adaptive
9308         (gnus-score-remove-lines-adaptive marks)
9309       (while (re-search-forward marks nil t)
9310         (gnus-delete-line)))
9311     ;; If we use dummy roots, we have to do an additional sweep over
9312     ;; the buffer.
9313     (if (not (eq gnus-summary-make-false-root 'dummy))
9314         ()
9315       (goto-char (point-min))
9316       (setq marks (concat "^[" (char-to-string gnus-dummy-mark) "]"))
9317       (while (re-search-forward marks nil t)
9318         (if (gnus-subject-equal
9319              (gnus-summary-subject-string)
9320              (progn
9321                (forward-line 1)
9322                (gnus-summary-subject-string)))
9323             ()
9324           (forward-line -1)
9325           (gnus-delete-line)))))
9326   (or (zerop (buffer-size))
9327       (if (eobp)
9328           (gnus-summary-prev-subject 1)
9329         (gnus-summary-position-cursor))))
9330
9331 (defun gnus-summary-expunge-below (score)
9332   "Remove articles with score less than SCORE."
9333   (interactive "P")
9334   (gnus-set-global-variables)
9335   (setq score (if score
9336                   (prefix-numeric-value score)
9337                 (or gnus-summary-default-score 0)))
9338   (save-excursion
9339     (set-buffer gnus-summary-buffer)
9340     (goto-char (point-min))
9341     (let ((buffer-read-only nil)
9342           beg)
9343       (while (not (eobp))
9344         (if (< (gnus-summary-article-score) score)
9345             (progn
9346               (setq beg (point))
9347               (forward-line 1)
9348               (delete-region beg (point)))
9349           (forward-line 1)))
9350       ;; Adjust point.
9351       (or (zerop (buffer-size))
9352           (if (eobp)
9353               (gnus-summary-prev-subject 1)
9354             (gnus-summary-position-cursor))))))
9355
9356 (defun gnus-summary-mark-below (score mark)
9357   "Mark articles with score less than SCORE with MARK."
9358   (interactive "P\ncMark: ")
9359   (gnus-set-global-variables)
9360   (setq score (if score
9361                   (prefix-numeric-value score)
9362                 (or gnus-summary-default-score 0)))
9363   (save-excursion
9364     (set-buffer gnus-summary-buffer)
9365     (goto-char (point-min))
9366     (while (not (eobp))
9367       (and (< (gnus-summary-article-score) score)
9368            (gnus-summary-mark-article nil mark))
9369       (forward-line 1))))
9370
9371 (defun gnus-summary-kill-below (score)
9372   "Mark articles with score below SCORE as read."
9373   (interactive "P")
9374   (gnus-summary-mark-below score gnus-killed-mark))
9375
9376 (defun gnus-summary-clear-above (score)
9377   "Clear all marks from articles with score above SCORE."
9378   (interactive "P")
9379   (gnus-summary-mark-above score gnus-unread-mark))
9380
9381 (defun gnus-summary-tick-above (score)
9382   "Tick all articles with score above SCORE."
9383   (interactive "P")
9384   (gnus-summary-mark-above score gnus-ticked-mark))
9385
9386 (defun gnus-summary-mark-above (score mark)
9387   "Mark articles with score over SCORE with MARK."
9388   (interactive "P\ncMark: ")
9389   (setq score (if score
9390                   (prefix-numeric-value score)
9391                 (or gnus-summary-default-score 0)))
9392   (save-excursion
9393     (set-buffer gnus-summary-buffer)
9394     (goto-char (point-min))
9395     (while (not (eobp))
9396       (if (> (gnus-summary-article-score) score)
9397           (progn
9398             (gnus-summary-mark-article nil mark)
9399             (forward-line 1))
9400         (forward-line 1)))))
9401
9402 ;; Suggested by Daniel Quinlan <quinlan@best.com>.  
9403 (defun gnus-summary-show-all-expunged ()
9404   "Display all the hidden articles that were expunged for low scores."
9405   (interactive)
9406   (let ((buffer-read-only nil))
9407     (let ((scored gnus-newsgroup-scored)
9408           headers h)
9409       (while scored
9410         (or (gnus-summary-goto-subject (car (car scored)))
9411             (and (setq h (gnus-get-header-by-number (car (car scored))))
9412                  (< (cdr (car scored)) gnus-summary-expunge-below)
9413                  (setq headers (cons h headers))))
9414         (setq scored (cdr scored)))
9415       (or headers (error "No expunged articles hidden."))
9416       (goto-char (point-min))
9417       (save-excursion 
9418         (gnus-summary-update-lines 
9419          (point)
9420          (progn
9421            (gnus-summary-prepare-threads (nreverse headers))
9422            (point)))))
9423     (goto-char (point-min))
9424     (gnus-summary-position-cursor)))
9425
9426 (defun gnus-summary-show-all-dormant ()
9427   "Display all the hidden articles that are marked as dormant."
9428   (interactive)
9429   (let ((buffer-read-only nil))
9430     (let ((dormant gnus-newsgroup-dormant)
9431           headers h)
9432       (while dormant
9433         (or (gnus-summary-goto-subject (car dormant))
9434             (and (setq h (gnus-get-header-by-number (car dormant)))
9435                  (setq headers (cons h headers))))
9436         (setq dormant (cdr dormant)))
9437       (or headers (error "No dormant articles hidden."))
9438       (goto-char (point-min))
9439       (save-excursion 
9440         (gnus-summary-update-lines 
9441          (point)
9442          (progn
9443            (gnus-summary-prepare-threads (nreverse headers))
9444            (point)))))
9445     (goto-char (point-min))
9446     (gnus-summary-position-cursor)))
9447
9448 (defun gnus-summary-hide-all-dormant ()
9449   "Hide all dormant articles."
9450   (interactive)
9451   (gnus-summary-remove-lines-marked-with (char-to-string gnus-dormant-mark))
9452   (gnus-summary-position-cursor))
9453
9454 (defun gnus-summary-catchup (all &optional quietly to-here not-mark)
9455   "Mark all articles not marked as unread in this newsgroup as read.
9456 If prefix argument ALL is non-nil, all articles are marked as read.
9457 If QUIETLY is non-nil, no questions will be asked.
9458 If TO-HERE is non-nil, it should be a point in the buffer. All
9459 articles before this point will be marked as read.
9460 The number of articles marked as read is returned."
9461   (interactive "P")
9462   (prog1
9463       (if (or quietly
9464               (not gnus-interactive-catchup) ;Without confirmation?
9465               gnus-expert-user
9466               (gnus-y-or-n-p
9467                (if all
9468                    "Mark absolutely all articles as read? "
9469                  "Mark all unread articles as read? ")))
9470           (if (and not-mark 
9471                    (not gnus-newsgroup-adaptive)
9472                    (not gnus-newsgroup-auto-expire))
9473               (progn
9474                 (and all (setq gnus-newsgroup-marked nil
9475                                gnus-newsgroup-dormant nil))
9476                 (setq gnus-newsgroup-unreads 
9477                       (append gnus-newsgroup-marked gnus-newsgroup-dormant)))
9478             ;; We actually mark all articles as canceled, which we
9479             ;; have to do when using auto-expiry or adaptive scoring. 
9480             (let ((unreads (length gnus-newsgroup-unreads)))
9481               (if (gnus-summary-first-subject (not all))
9482                   (while (and (if to-here (< (point) to-here) t)
9483                               (gnus-summary-mark-article nil gnus-catchup-mark)
9484                               (gnus-summary-search-subject nil (not all)))))
9485               (- unreads (length gnus-newsgroup-unreads))
9486               (or to-here
9487                   (setq gnus-newsgroup-unreads gnus-newsgroup-marked)))))
9488     (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
9489       (if (and (not to-here) (eq 'nnvirtual (car method)))
9490           (nnvirtual-catchup-group
9491            (gnus-group-real-name gnus-newsgroup-name) (nth 1 method) all)))
9492     (gnus-summary-position-cursor)))
9493
9494 (defun gnus-summary-catchup-to-here (&optional all)
9495   "Mark all unticked articles before the current one as read.
9496 If ALL is non-nil, also mark ticked and dormant articles as read."
9497   (interactive)
9498   (save-excursion
9499     (and (zerop (forward-line -1))
9500          (progn
9501            (end-of-line)
9502            (gnus-summary-catchup all t (point))
9503            (gnus-set-mode-line 'summary))))
9504   (gnus-summary-position-cursor))
9505
9506 (defun gnus-summary-catchup-all (&optional quietly)
9507   "Mark all articles in this newsgroup as read."
9508   (interactive)
9509   (gnus-summary-catchup t quietly))
9510
9511 (defun gnus-summary-catchup-and-exit (all &optional quietly)
9512   "Mark all articles not marked as unread in this newsgroup as read, then exit.
9513 If prefix argument ALL is non-nil, all articles are marked as read."
9514   (interactive "P")
9515   (gnus-summary-catchup all quietly nil 'fast)
9516   ;; Select next newsgroup or exit.
9517   (if (eq gnus-auto-select-next 'quietly)
9518       (gnus-summary-next-group nil)
9519     (gnus-summary-exit)))
9520
9521 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
9522   "Mark all articles in this newsgroup as read, and then exit."
9523   (interactive)
9524   (gnus-summary-catchup-and-exit t quietly))
9525
9526 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
9527 (defun gnus-summary-catchup-and-goto-next-group (all)
9528   "Mark all articles in this group as read and select the next group.
9529 If given a prefix, mark all articles, unread as well as ticked, as
9530 read." 
9531   (interactive "P")
9532   (gnus-summary-catchup all)
9533   (gnus-summary-next-group))
9534
9535 ;; Thread-based commands.
9536
9537 (defun gnus-summary-toggle-threads (arg)
9538   "Toggle showing conversation threads.
9539 If ARG is positive number, turn showing conversation threads on."
9540   (interactive "P")
9541   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
9542     (setq gnus-show-threads
9543           (if (null arg) (not gnus-show-threads)
9544             (> (prefix-numeric-value arg) 0)))
9545     (gnus-summary-prepare)
9546     (gnus-summary-goto-subject current)
9547     (gnus-summary-position-cursor)))
9548
9549 (defun gnus-summary-show-all-threads ()
9550   "Show all threads."
9551   (interactive)
9552   (save-excursion
9553     (let ((buffer-read-only nil))
9554       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
9555   (gnus-summary-position-cursor))
9556
9557 (defun gnus-summary-show-thread ()
9558   "Show thread subtrees.
9559 Returns nil if no thread was there to be shown."
9560   (interactive)
9561   (prog1
9562       (save-excursion
9563         (let ((buffer-read-only nil)
9564               ;; first goto end then to beg, to have point at beg after let
9565               (end (progn (end-of-line) (point)))
9566               (beg (progn (beginning-of-line) (point))))
9567           (prog1
9568               ;; Any hidden lines here?
9569               (search-forward "\r" end t)
9570             (subst-char-in-region beg end ?\^M ?\n t))))
9571     (gnus-summary-position-cursor)))
9572
9573 (defun gnus-summary-hide-all-threads ()
9574   "Hide all thread subtrees."
9575   (interactive)
9576   (save-excursion
9577     (goto-char (point-min))
9578     (gnus-summary-hide-thread)
9579     (while (and (not (eobp)) (zerop (forward-line 1)))
9580       (gnus-summary-hide-thread)))
9581   (gnus-summary-position-cursor))
9582
9583 (defun gnus-summary-hide-thread ()
9584   "Hide thread subtrees.
9585 Returns nil if no threads were there to be hidden."
9586   (interactive)
9587   (let ((buffer-read-only nil)
9588         (start (point))
9589         (level (gnus-summary-thread-level))
9590         (end (point)))
9591     ;; Go forward until either the buffer ends or the subthread
9592     ;; ends. 
9593     (if (eobp)
9594         ()
9595       (while (and (zerop (forward-line 1))
9596                   (> (gnus-summary-thread-level) level))
9597         (setq end (point)))
9598       (prog1
9599           (save-excursion
9600             (goto-char end)
9601             (search-backward "\n" start t))
9602         (subst-char-in-region start end ?\n ?\^M t)
9603         (forward-line -1)))))
9604
9605 (defun gnus-summary-go-to-next-thread (&optional previous)
9606   "Go to the same level (or less) next thread.
9607 If PREVIOUS is non-nil, go to previous thread instead.
9608 Return the article number moved to, or nil if moving was impossible."
9609   (let ((level (gnus-summary-thread-level))
9610         (article (gnus-summary-article-number)))
9611     (if previous 
9612         (while (and (zerop (forward-line -1))
9613                     (> (gnus-summary-thread-level) level)))
9614       (while (and (save-excursion
9615                     (forward-line 1)
9616                     (not (eobp)))
9617                   (zerop (forward-line 1))
9618                   (> (gnus-summary-thread-level) level))))
9619     (gnus-summary-recenter)
9620     (gnus-summary-position-cursor)
9621     (let ((oart (gnus-summary-article-number)))
9622       (and (/= oart article) oart))))
9623
9624 (defun gnus-summary-next-thread (n)
9625   "Go to the same level next N'th thread.
9626 If N is negative, search backward instead.
9627 Returns the difference between N and the number of skips actually
9628 done."
9629   (interactive "p")
9630   (let ((backward (< n 0))
9631         (n (abs n)))
9632   (while (and (> n 0)
9633               (gnus-summary-go-to-next-thread backward))
9634     (setq n (1- n)))
9635   (gnus-summary-position-cursor)
9636   (if (/= 0 n) (gnus-message 7 "No more threads"))
9637   n))
9638
9639 (defun gnus-summary-prev-thread (n)
9640   "Go to the same level previous N'th thread.
9641 Returns the difference between N and the number of skips actually
9642 done."
9643   (interactive "p")
9644   (gnus-summary-next-thread (- n)))
9645
9646 (defun gnus-summary-go-down-thread (&optional same)
9647   "Go down one level in the current thread.
9648 If SAME is non-nil, also move to articles of the same level."
9649   (let ((level (gnus-summary-thread-level))
9650         (start (point)))
9651     (if (and (zerop (forward-line 1))
9652              (> (gnus-summary-thread-level) level))
9653         t
9654       (goto-char start)
9655       nil)))
9656
9657 (defun gnus-summary-go-up-thread ()
9658   "Go up one level in the current thread."
9659   (let ((level (gnus-summary-thread-level))
9660         (start (point)))
9661     (while (and (zerop (forward-line -1))
9662                 (>= (gnus-summary-thread-level) level)))
9663     (if (>= (gnus-summary-thread-level) level)
9664         (progn
9665           (goto-char start)
9666           nil)
9667       t)))
9668
9669 (defun gnus-summary-down-thread (n)
9670   "Go down thread N steps.
9671 If N is negative, go up instead.
9672 Returns the difference between N and how many steps down that were
9673 taken."
9674   (interactive "p")
9675   (let ((up (< n 0))
9676         (n (abs n)))
9677   (while (and (> n 0)
9678               (if up (gnus-summary-go-up-thread)
9679                 (gnus-summary-go-down-thread)))
9680     (setq n (1- n)))
9681   (gnus-summary-position-cursor)
9682   (if (/= 0 n) (gnus-message 7 "Can't go further"))
9683   n))
9684
9685 (defun gnus-summary-up-thread (n)
9686   "Go up thread N steps.
9687 If N is negative, go up instead.
9688 Returns the difference between N and how many steps down that were
9689 taken."
9690   (interactive "p")
9691   (gnus-summary-down-thread (- n)))
9692
9693 (defun gnus-summary-kill-thread (unmark)
9694   "Mark articles under current thread as read.
9695 If the prefix argument is positive, remove any kinds of marks.
9696 If the prefix argument is negative, tick articles instead."
9697   (interactive "P")
9698   (if unmark
9699       (setq unmark (prefix-numeric-value unmark)))
9700   (let ((killing t)
9701         (level (gnus-summary-thread-level)))
9702     (save-excursion
9703       (while killing
9704         ;; Mark the article...
9705         (cond ((null unmark) (gnus-summary-mark-article nil gnus-killed-mark))
9706               ((> unmark 0) (gnus-summary-tick-article nil t))
9707               (t (gnus-summary-tick-article)))
9708         ;; ...and go forward until either the buffer ends or the subtree
9709         ;; ends. 
9710         (if (not (and (zerop (forward-line 1))
9711                       (> (gnus-summary-thread-level) level)))
9712             (setq killing nil))))
9713     ;; Hide killed subtrees.
9714     (and (null unmark)
9715          gnus-thread-hide-killed
9716          (gnus-summary-hide-thread))
9717     ;; If marked as read, go to next unread subject.
9718     (if (null unmark)
9719         ;; Go to next unread subject.
9720         (gnus-summary-next-subject 1 t)))
9721   (gnus-set-mode-line 'summary))
9722
9723 ;; Summary sorting commands
9724
9725 (defun gnus-summary-sort-by-number (&optional reverse)
9726   "Sort summary buffer by article number.
9727 Argument REVERSE means reverse order."
9728   (interactive "P")
9729   (gnus-summary-sort 
9730    (cons 'gnus-summary-article-number 'gnus-thread-sort-by-number) reverse))
9731
9732 (defun gnus-summary-sort-by-author (&optional reverse)
9733   "Sort summary buffer by author name alphabetically.
9734 If case-fold-search is non-nil, case of letters is ignored.
9735 Argument REVERSE means reverse order."
9736   (interactive "P")
9737   (gnus-summary-sort
9738    (cons
9739     (lambda ()
9740       (let ((extract (funcall
9741                       gnus-extract-address-components
9742                       (header-from (gnus-get-header-by-number
9743                                     (gnus-summary-article-number))))))
9744         (or (car extract) (cdr extract))))
9745     'gnus-thread-sort-by-author)
9746    reverse))
9747
9748 (defun gnus-summary-sort-by-subject (&optional reverse)
9749   "Sort summary buffer by subject alphabetically. `Re:'s are ignored.
9750 If case-fold-search is non-nil, case of letters is ignored.
9751 Argument REVERSE means reverse order."
9752   (interactive "P")
9753   (gnus-summary-sort
9754    (cons
9755     (lambda ()
9756       (downcase (gnus-simplify-subject (gnus-summary-subject-string))))
9757     'gnus-thread-sort-by-subject)
9758    reverse))
9759
9760 (defun gnus-summary-sort-by-date (&optional reverse)
9761   "Sort summary buffer by date.
9762 Argument REVERSE means reverse order."
9763   (interactive "P")
9764   (gnus-summary-sort
9765    (cons
9766     (lambda ()
9767       (gnus-sortable-date
9768        (header-date 
9769         (gnus-get-header-by-number (gnus-summary-article-number)))))
9770     'gnus-thread-sort-by-date)
9771    reverse))
9772
9773 (defun gnus-summary-sort-by-score (&optional reverse)
9774   "Sort summary buffer by score.
9775 Argument REVERSE means reverse order."
9776   (interactive "P")
9777   (gnus-summary-sort 
9778    (cons 'gnus-summary-article-score 'gnus-thread-sort-by-score)
9779    (not reverse)))
9780
9781 (defvar gnus-summary-already-sorted nil)
9782 (defun gnus-summary-sort (predicate reverse)
9783   ;; Sort summary buffer by PREDICATE.  REVERSE means reverse order. 
9784   (if gnus-summary-already-sorted
9785       ()
9786     (let (buffer-read-only)
9787       (if (not gnus-show-threads)
9788           (progn
9789             (goto-char (point-min))
9790             (sort-subr reverse 'forward-line 'end-of-line (car predicate)))
9791         (let ((gnus-thread-sort-functions (list (cdr predicate)))
9792               (gnus-summary-prepare-hook nil)
9793               (gnus-summary-already-sorted nil))
9794           (gnus-summary-prepare)
9795           (and gnus-show-threads
9796                gnus-thread-hide-subtree
9797                (gnus-summary-hide-all-threads))
9798           ;; If in async mode, we send some info to the backend.
9799           (and gnus-newsgroup-async
9800                (setq gnus-newsgroup-threads (nreverse gnus-newsgroup-threads))
9801                (gnus-request-asynchronous 
9802                 gnus-newsgroup-name
9803                 (if (and gnus-asynchronous-article-function
9804                          (fboundp gnus-asynchronous-article-function))
9805                     (funcall gnus-asynchronous-article-function
9806                              gnus-newsgroup-threads)))))))))
9807
9808   
9809 (defun gnus-sortable-date (date)
9810   "Make sortable string by string-lessp from DATE.
9811 Timezone package is used."
9812   (let* ((date   (timezone-fix-time date nil nil)) ;[Y M D H M S]
9813          (year   (aref date 0))
9814          (month  (aref date 1))
9815          (day    (aref date 2)))
9816     (timezone-make-sortable-date 
9817      year month day 
9818      (timezone-make-time-string
9819       (aref date 3) (aref date 4) (aref date 5)))))
9820
9821
9822 ;; Summary saving commands.
9823
9824 (defun gnus-summary-save-article (n)
9825   "Save the current article using the default saver function.
9826 If N is a positive number, save the N next articles.
9827 If N is a negative number, save the N previous articles.
9828 If N is nil and any articles have been marked with the process mark,
9829 save those articles instead.
9830 The variable `gnus-default-article-saver' specifies the saver function."
9831   (interactive "P")
9832   (let ((articles (gnus-summary-work-articles n)))
9833     (while articles
9834       (let ((header (gnus-get-header-by-number (car articles))))
9835         (if (vectorp header)
9836             (progn
9837               (gnus-summary-select-article t nil nil (car articles))
9838               (or gnus-save-all-headers
9839                   (gnus-article-hide-headers t))
9840               ;; Remove any X-Gnus lines.
9841               (save-excursion
9842                 (save-restriction
9843                   (set-buffer gnus-article-buffer)
9844                   (let ((buffer-read-only nil))
9845                     (goto-char (point-min))
9846                     (narrow-to-region (point) (or (search-forward "\n\n" nil t)
9847                                                   (point-max)))
9848                     (while (re-search-forward "^X-Gnus" nil t)
9849                       (beginning-of-line)
9850                       (delete-region (point)
9851                                      (progn (forward-line 1) (point))))
9852                     (widen))))
9853               (save-excursion
9854                 (if gnus-default-article-saver
9855                     (funcall gnus-default-article-saver)
9856                   (error "No default saver is defined."))))
9857           (if (assq 'name header)
9858               (gnus-copy-file (cdr (assq 'name header)))
9859             (gnus-message 1 "Article %d is unsaveable" (car articles)))))
9860       (gnus-summary-remove-process-mark (car articles))
9861       (setq articles (cdr articles)))
9862     (gnus-summary-position-cursor)
9863     n))
9864
9865 (defun gnus-summary-pipe-output (arg)
9866   "Pipe the current article to a subprocess.
9867 If N is a positive number, pipe the N next articles.
9868 If N is a negative number, pipe the N previous articles.
9869 If N is nil and any articles have been marked with the process mark,
9870 pipe those articles instead."
9871   (interactive "P")
9872   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9873     (gnus-summary-save-article arg)))
9874
9875 (defun gnus-summary-save-article-mail (arg)
9876   "Append the current article to an mail file.
9877 If N is a positive number, save the N next articles.
9878 If N is a negative number, save the N previous articles.
9879 If N is nil and any articles have been marked with the process mark,
9880 save those articles instead."
9881   (interactive "P")
9882   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9883     (gnus-summary-save-article arg)))
9884
9885 (defun gnus-summary-save-article-rmail (arg)
9886   "Append the current article to an rmail file.
9887 If N is a positive number, save the N next articles.
9888 If N is a negative number, save the N previous articles.
9889 If N is nil and any articles have been marked with the process mark,
9890 save those articles instead."
9891   (interactive "P")
9892   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9893     (gnus-summary-save-article arg)))
9894
9895 (defun gnus-summary-save-article-file (arg)
9896   "Append the current article to a file.
9897 If N is a positive number, save the N next articles.
9898 If N is a negative number, save the N previous articles.
9899 If N is nil and any articles have been marked with the process mark,
9900 save those articles instead."
9901   (interactive "P")
9902   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9903     (gnus-summary-save-article arg)))
9904
9905 (defun gnus-read-save-file-name (prompt default-name)
9906   (let ((methods gnus-split-methods)
9907         split-name)
9908     (if (not gnus-split-methods)
9909         ()
9910       (save-excursion
9911         (set-buffer gnus-article-buffer)
9912         (gnus-narrow-to-headers)
9913         (while methods
9914           (goto-char (point-min))
9915           (and (condition-case () 
9916                    (re-search-forward (car (car methods)) nil t)
9917                  (error nil))
9918                (setq split-name (cons (nth 1 (car methods)) split-name)))
9919           (setq methods (cdr methods)))
9920         (widen)))
9921     (cond ((null split-name)
9922            (read-file-name
9923             (concat prompt " (default "
9924                     (file-name-nondirectory default-name) ") ")
9925             (file-name-directory default-name)
9926             default-name))
9927           ((= 1 (length split-name))
9928            (read-file-name
9929             (concat prompt " (default " (car split-name) ") ")
9930             gnus-article-save-directory
9931             (concat gnus-article-save-directory (car split-name))))
9932           (t
9933            (setq split-name (mapcar (lambda (el) (list el))
9934                                     (nreverse split-name)))
9935            (let ((result (completing-read 
9936                           (concat prompt " ")
9937                           split-name nil nil)))
9938              (concat gnus-article-save-directory
9939                      (if (string= result "")
9940                          (car (car split-name))
9941                        result)))))))
9942
9943 (defun gnus-summary-save-in-rmail (&optional filename)
9944   "Append this article to Rmail file.
9945 Optional argument FILENAME specifies file name.
9946 Directory to save to is default to `gnus-article-save-directory' which
9947 is initialized from the SAVEDIR environment variable."
9948   (interactive)
9949   (let ((default-name
9950           (funcall gnus-rmail-save-name gnus-newsgroup-name
9951                    gnus-current-headers gnus-newsgroup-last-rmail)))
9952     (or filename
9953         (setq filename (gnus-read-save-file-name 
9954                         "Save in rmail file:" default-name)))
9955     (gnus-make-directory (file-name-directory filename))
9956     (gnus-eval-in-buffer-window 
9957      gnus-article-buffer
9958      (save-excursion
9959        (save-restriction
9960          (widen)
9961          (gnus-output-to-rmail filename))))
9962     ;; Remember the directory name to save articles.
9963     (setq gnus-newsgroup-last-rmail filename)))
9964
9965 (defun gnus-summary-save-in-mail (&optional filename)
9966   "Append this article to Unix mail file.
9967 Optional argument FILENAME specifies file name.
9968 Directory to save to is default to `gnus-article-save-directory' which
9969 is initialized from the SAVEDIR environment variable."
9970   (interactive)
9971   (let ((default-name
9972           (funcall gnus-mail-save-name gnus-newsgroup-name
9973                    gnus-current-headers gnus-newsgroup-last-mail)))
9974     (or filename
9975         (setq filename (gnus-read-save-file-name 
9976                         "Save in Unix mail file:" default-name)))
9977     (setq filename
9978           (expand-file-name filename
9979                             (and default-name
9980                                  (file-name-directory default-name))))
9981     (gnus-make-directory (file-name-directory filename))
9982     (gnus-eval-in-buffer-window 
9983      gnus-article-buffer
9984      (save-excursion
9985        (save-restriction
9986          (widen)
9987          (if (and (file-readable-p filename) (rmail-file-p filename))
9988              (gnus-output-to-rmail filename)
9989            (rmail-output filename 1 t t)))))
9990     ;; Remember the directory name to save articles.
9991     (setq gnus-newsgroup-last-mail filename)))
9992
9993 (defun gnus-summary-save-in-file (&optional filename)
9994   "Append this article to file.
9995 Optional argument FILENAME specifies file name.
9996 Directory to save to is default to `gnus-article-save-directory' which
9997 is initialized from the SAVEDIR environment variable."
9998   (interactive)
9999   (let ((default-name
10000           (funcall gnus-file-save-name gnus-newsgroup-name
10001                    gnus-current-headers gnus-newsgroup-last-file)))
10002     (or filename
10003         (setq filename (gnus-read-save-file-name 
10004                         "Save in file:" default-name)))
10005     (gnus-make-directory (file-name-directory filename))
10006     (gnus-eval-in-buffer-window 
10007      gnus-article-buffer
10008      (save-excursion
10009        (save-restriction
10010          (widen)
10011          (gnus-output-to-file filename))))
10012     ;; Remember the directory name to save articles.
10013     (setq gnus-newsgroup-last-file filename)))
10014
10015 (defun gnus-summary-save-in-pipe (&optional command)
10016   "Pipe this article to subprocess."
10017   (interactive)
10018   (let ((command (read-string "Shell command on article: "
10019                               gnus-last-shell-command)))
10020     (if (string-equal command "")
10021         (setq command gnus-last-shell-command))
10022     (gnus-eval-in-buffer-window 
10023      gnus-article-buffer
10024      (save-restriction
10025        (widen)
10026        (shell-command-on-region (point-min) (point-max) command nil)))
10027     (setq gnus-last-shell-command command)))
10028
10029 ;; Summary extract commands
10030
10031 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
10032   (let ((buffer-read-only nil)
10033         (article (gnus-summary-article-number))
10034         b)
10035     (or (gnus-summary-goto-subject article)
10036         (error (format "No such article: %d" article)))
10037     (or gnus-newsgroup-headers-hashtb-by-number
10038         (gnus-make-headers-hashtable-by-number))
10039     (gnus-summary-position-cursor)
10040     ;; If all commands are to be bunched up on one line, we collect
10041     ;; them here.  
10042     (if gnus-view-pseudos-separately
10043         ()
10044       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
10045             files action)
10046         (while ps
10047           (setq action (cdr (assq 'action (car ps))))
10048           (setq files (list (cdr (assq 'name (car ps)))))
10049           (while (and ps (cdr ps)
10050                       (string= (or action "1")
10051                                (or (cdr (assq 'action (car (cdr ps)))) "2")))
10052             (setq files (cons (cdr (assq 'name (car (cdr ps)))) files))
10053             (setcdr ps (cdr (cdr ps))))
10054           (if (not files)
10055               ()
10056             (if (not (string-match "%s" action))
10057                 (setq files (cons " " files)))
10058             (setq files (cons " " files))
10059             (and (assq 'execute (car ps))
10060                  (setcdr (assq 'execute (car ps))
10061                          (funcall (if (string-match "%s" action)
10062                                       'format 'concat)
10063                                   action 
10064                                   (mapconcat (lambda (f) f) files " ")))))
10065           (setq ps (cdr ps)))))
10066     (if (and gnus-view-pseudos (not not-view))
10067         (while pslist
10068           (and (assq 'execute (car pslist))
10069                (gnus-execute-command (cdr (assq 'execute (car pslist)))
10070                                      (eq gnus-view-pseudos 'not-confirm)))
10071           (setq pslist (cdr pslist)))
10072       (save-excursion
10073         (while pslist
10074           (gnus-summary-goto-subject (or (cdr (assq 'article (car pslist)))
10075                                          (gnus-summary-article-number)))
10076           (forward-line 1)
10077           (setq b (point))
10078           (insert "          " (file-name-nondirectory 
10079                                 (cdr (assq 'name (car pslist))))
10080                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
10081           (add-text-properties 
10082            b (1+ b) (list 'gnus-number gnus-reffed-article-number
10083                           'gnus-mark gnus-unread-mark 
10084                           'gnus-level 0
10085                           'gnus-pseudo (car pslist)))
10086           (forward-line -1)
10087           (gnus-sethash (int-to-string gnus-reffed-article-number)
10088                         (car pslist) gnus-newsgroup-headers-hashtb-by-number)
10089           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
10090           (setq pslist (cdr pslist)))))))
10091
10092 (defun gnus-pseudos< (p1 p2)
10093   (let ((c1 (cdr (assq 'action p1)))
10094         (c2 (cdr (assq 'action p2))))
10095     (and c1 c2 (string< c1 c2))))
10096
10097 (defun gnus-request-pseudo-article (props)
10098   (cond ((assq 'execute props)
10099          (gnus-execute-command (cdr (assq 'execute props)))))
10100   (let ((gnus-current-article (gnus-summary-article-number)))
10101     (run-hooks 'gnus-mark-article-hook)))
10102
10103 (defun gnus-execute-command (command &optional automatic)
10104   (save-excursion
10105     (gnus-article-setup-buffer)
10106     (set-buffer gnus-article-buffer)
10107     (let ((command (if automatic command (read-string "Command: " command)))
10108           (buffer-read-only nil))
10109       (erase-buffer)
10110       (insert "$ " command "\n\n")
10111       (if gnus-view-pseudo-asynchronously
10112           (start-process "gnus-execute" nil "sh" "-c" command)
10113         (call-process "sh" nil t nil "-c" command)))))
10114
10115 (defun gnus-copy-file (file &optional to)
10116   "Copy FILE to TO."
10117   (interactive
10118    (list (read-file-name "Copy file: " default-directory)
10119          (read-file-name "Copy file to: " default-directory)))
10120   (or to (setq to (read-file-name "Copy file to: " default-directory)))
10121   (and (file-directory-p to) 
10122        (setq to (concat (file-name-as-directory to)
10123                         (file-name-nondirectory file))))
10124   (copy-file file to))
10125
10126 ;; Summary kill commands.
10127
10128 (defun gnus-summary-edit-global-kill (article)
10129   "Edit the \"global\" kill file."
10130   (interactive (list (gnus-summary-article-number)))
10131   (gnus-group-edit-global-kill article))
10132
10133 (defun gnus-summary-edit-local-kill ()
10134   "Edit a local kill file applied to the current newsgroup."
10135   (interactive)
10136   (setq gnus-current-headers 
10137         (gnus-gethash 
10138          (int-to-string (gnus-summary-article-number))
10139          gnus-newsgroup-headers-hashtb-by-number))
10140   (gnus-set-global-variables)
10141   (gnus-group-edit-local-kill 
10142    (gnus-summary-article-number) gnus-newsgroup-name))
10143
10144 \f
10145 ;;;
10146 ;;; Gnus article mode
10147 ;;;
10148
10149 (put 'gnus-article-mode 'mode-class 'special)
10150
10151 (if gnus-article-mode-map
10152     nil
10153   (setq gnus-article-mode-map (make-keymap))
10154   (suppress-keymap gnus-article-mode-map)
10155   (define-key gnus-article-mode-map " " 'gnus-article-next-page)
10156   (define-key gnus-article-mode-map "\177" 'gnus-article-prev-page)
10157   (define-key gnus-article-mode-map "\C-c^" 'gnus-article-refer-article)
10158   (define-key gnus-article-mode-map "h" 'gnus-article-show-summary)
10159   (define-key gnus-article-mode-map "s" 'gnus-article-show-summary)
10160   (define-key gnus-article-mode-map "\C-c\C-m" 'gnus-article-mail)
10161   (define-key gnus-article-mode-map "\C-c\C-M" 'gnus-article-mail-with-original)
10162   (define-key gnus-article-mode-map "?" 'gnus-article-describe-briefly)
10163   (define-key gnus-article-mode-map gnus-mouse-2 'gnus-article-push-button)
10164   (define-key gnus-article-mode-map "\r" 'gnus-article-press-button)
10165   (define-key gnus-article-mode-map "\t" 'gnus-article-next-button)
10166   (define-key gnus-article-mode-map "\C-c\C-b" 'gnus-bug)
10167   
10168   ;; Duplicate almost all summary keystrokes in the article mode map.
10169   (let ((commands 
10170          (list 
10171           " " "\177" "p" "N" "P" "\M-\C-n" "\M-\C-p"
10172           "\M-n" "\M-p" "." "," "\M-s" "\M-r" "<" ">" "j" "^" "\M-^"
10173           "u" "!" "U" "d" "D" "E" "\M-u" "\M-U" "k" "\C-k" "\M-\C-k"
10174           "\M-\C-l" "e" "#" "\M-#" "\M-\C-t" "\M-\C-s" "\M-\C-h"
10175           "\M-\C-f" "\M-\C-b" "\M-\C-u" "\M-\C-d" "&" "\C-w"
10176           "\C-t" "?" "\C-c\M-\C-s" "\C-c\C-s\C-n" "\C-c\C-s\C-a"
10177           "\C-c\C-s\C-s" "\C-c\C-s\C-d" "\C-c\C-s\C-i" "\C-x\C-s"
10178           "\M-g" "w" "\C-c\C-r" "\M-t" "C"
10179           "o" "\C-o" "|" "\M-k" "\M-K" "V" "\C-c\C-d"
10180           "\C-c\C-i" "x" "X" "s" "t" "g" "?" "l"
10181           "\C-c\C-v\C-v" "\C-d" "v" 
10182 ;;        "Mt" "M!" "Md" "Mr"
10183 ;;        "Mc" "M " "Me" "Mx" "M?" "Mb" "MB" "M#" "M\M-#" "M\M-r"
10184 ;;        "M\M-\C-r" "MD" "M\M-D" "MS" "MC" "MH" "M\C-c" "Mk" "MK"
10185 ;;        "Ms" "Mc" "Mu" "Mm" "Mk" "Gn" "Gp" "GN" "GP" "G\C-n" "G\C-p"
10186 ;;        "G\M-n" "G\M-p" "Gf" "Gb" "Gg" "Gl" "Gp" "Tk" "Tl" "Ti" "TT"
10187 ;;        "Ts" "TS" "Th" "TH" "Tn" "Tp" "Tu" "Td" "T#" "A " "An" "A\177" "Ap"
10188 ;;        "A\r" "A<" "A>" "Ab" "Ae" "A^" "Ar" "Aw" "Ac" "Ag" "At" "Am"
10189 ;;        "As" "Wh" "Ws" "Wc" "Wo" "Ww" "Wd" "Wq" "Wf" "Wt" "W\C-t"
10190 ;;        "WT" "WA" "Wa" "WH" "WC" "WS" "Wb" "Hv" "Hf" "Hd" "Hh" "Hi"
10191 ;;        "Be" "B\177" "Bm" "Br" "Bw" "Bc" "Bq" "Bi" "Oo" "Om" "Or"
10192 ;;        "Of" "Oh" "Ov" "Op" "Vu" "V\C-s" "V\C-r" "Vr" "V&" "VT" "Ve"
10193 ;;        "VD" "Vk" "VK" "Vsn" "Vsa" "Vss" "Vsd" "Vsi"
10194           )))
10195     (while commands
10196       (define-key gnus-article-mode-map (car commands) 
10197         'gnus-article-summary-command)
10198       (setq commands (cdr commands))))
10199
10200   (let ((commands (list "q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
10201 ;;                      "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP" 
10202                          "=" "n")))
10203     (while commands
10204       (define-key gnus-article-mode-map (car commands) 
10205         'gnus-article-summary-command-nosave)
10206       (setq commands (cdr commands)))))
10207
10208
10209 (defun gnus-article-mode ()
10210   "Major mode for displaying an article.
10211
10212 All normal editing commands are switched off.
10213
10214 The following commands are available:
10215
10216 \\<gnus-article-mode-map>
10217 \\[gnus-article-next-page]\t Scroll the article one page forwards
10218 \\[gnus-article-prev-page]\t Scroll the article one page backwards
10219 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
10220 \\[gnus-article-show-summary]\t Display the summary buffer
10221 \\[gnus-article-mail]\t Send a reply to the address near point
10222 \\[gnus-article-mail-with-original]\t Send a reply to the address near point; include the original article
10223 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
10224 \\[gnus-info-find-node]\t Go to the Gnus info node"
10225   (interactive)
10226   (if gnus-visual (gnus-article-make-menu-bar))
10227   (kill-all-local-variables)
10228   (setq mode-line-modified "-- ")
10229   (make-local-variable 'mode-line-format)
10230   (setq mode-line-format (copy-sequence mode-line-format))
10231   (and (equal (nth 3 mode-line-format) "   ")
10232        (setcar (nthcdr 3 mode-line-format) ""))
10233   (setq mode-name "Article")
10234   (setq major-mode 'gnus-article-mode)
10235   (make-local-variable 'minor-mode-alist)
10236   (or (assq 'gnus-show-mime minor-mode-alist)
10237       (setq minor-mode-alist
10238             (cons (list 'gnus-show-mime " MIME") minor-mode-alist)))
10239   (use-local-map gnus-article-mode-map)
10240   (make-local-variable 'page-delimiter)
10241   (setq page-delimiter gnus-page-delimiter)
10242   (buffer-disable-undo (current-buffer))
10243   (setq buffer-read-only t)             ;Disable modification
10244   (run-hooks 'gnus-article-mode-hook))
10245
10246 (defun gnus-article-setup-buffer ()
10247   "Initialize article mode buffer."
10248   (if (get-buffer gnus-article-buffer)
10249       (save-excursion
10250         (set-buffer gnus-article-buffer)
10251         (buffer-disable-undo (current-buffer))
10252         (setq buffer-read-only t)
10253         (gnus-add-current-to-buffer-list)
10254         (or (eq major-mode 'gnus-article-mode)
10255             (gnus-article-mode)))
10256     (save-excursion
10257       (set-buffer (get-buffer-create gnus-article-buffer))
10258       (gnus-add-current-to-buffer-list)
10259       (gnus-article-mode))))
10260
10261 ;; Set article window start at LINE, where LINE is the number of lines
10262 ;; from the head of the article.
10263 (defun gnus-article-set-window-start (&optional line)
10264   (set-window-start 
10265    (get-buffer-window gnus-article-buffer)
10266    (save-excursion
10267      (set-buffer gnus-article-buffer)
10268      (goto-char (point-min))
10269      (if (not line)
10270          (point-min)
10271        (gnus-message 6 "Moved to bookmark")
10272        (search-forward "\n\n" nil t)
10273        (forward-line line)
10274        (point)))))
10275
10276 (defun gnus-request-article-this-buffer (article group)
10277   "Get an article and insert it into this buffer."
10278   (setq group (or group gnus-newsgroup-name))
10279
10280   ;; Open server if it has closed.
10281   (gnus-check-news-server (gnus-find-method-for-group group))
10282
10283   ;; Using `gnus-request-article' directly will insert the article into
10284   ;; `nntp-server-buffer' - so we'll save some time by not having to
10285   ;; copy it from the server buffer into the article buffer.
10286
10287   ;; We only request an article by message-id when we do not have the
10288   ;; headers for it, so we'll have to get those.
10289   (and (stringp article) 
10290        (let ((gnus-override-method gnus-refer-article-method))
10291          (gnus-read-header article)))
10292
10293   ;; If the article number is negative, that means that this article
10294   ;; doesn't belong in this newsgroup (possibly), so we find its
10295   ;; message-id and request it by id instead of number.
10296   (if (not (numberp article))
10297       ()
10298     (save-excursion
10299       (set-buffer gnus-summary-buffer)
10300       (let ((header (gnus-get-header-by-number article)))
10301         (if (< article 0)
10302             (if (vectorp header)
10303                 ;; It's a real article.
10304                 (setq article (header-id header))
10305               ;; It is an extracted pseudo-article.
10306               (setq article 'pseudo)
10307               (gnus-request-pseudo-article header)))
10308
10309         (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
10310           (if (not (eq (car method) 'nneething))
10311               ()
10312             (let ((dir (concat (file-name-as-directory (nth 1 method))
10313                                (header-subject header))))
10314               (if (file-directory-p dir)
10315                   (progn
10316                     (setq article 'nneething)
10317                     (gnus-group-enter-directory dir)))))))))
10318
10319   ;; Check the cache.
10320   (if (and gnus-use-cache
10321            (numberp article)
10322            (gnus-cache-request-article article group))
10323       'article
10324     ;; Get the article and into the article buffer.
10325     (if (or (stringp article) (numberp article))
10326         (progn
10327           (erase-buffer)
10328           (let ((gnus-override-method 
10329                  (and (stringp article) gnus-refer-article-method)))
10330             (and (gnus-request-article article group (current-buffer))
10331                  'article)))
10332       article)))
10333
10334 (defun gnus-read-header (id)
10335   "Read the headers of article ID and enter them into the Gnus system."
10336   (or gnus-newsgroup-headers-hashtb-by-number
10337       (gnus-make-headers-hashtable-by-number))
10338   (let (header)
10339     (if (not (setq header 
10340                    (car (if (let ((gnus-nov-is-evil t))
10341                               (gnus-retrieve-headers 
10342                                (list id) gnus-newsgroup-name))
10343                             (gnus-get-newsgroup-headers)))))
10344         nil
10345       (if (stringp id)
10346           (header-set-number header gnus-reffed-article-number))
10347       (setq gnus-newsgroup-headers (cons header gnus-newsgroup-headers))
10348       (gnus-sethash (int-to-string (header-number header)) header
10349                     gnus-newsgroup-headers-hashtb-by-number)
10350       (if (stringp id)
10351           (setq gnus-reffed-article-number (1- gnus-reffed-article-number)))
10352       (setq gnus-current-headers header)
10353       header)))
10354
10355 (defun gnus-article-prepare (article &optional all-headers header)
10356   "Prepare ARTICLE in article mode buffer.
10357 ARTICLE should either be an article number or a Message-ID.
10358 If ARTICLE is an id, HEADER should be the article headers.
10359 If ALL-HEADERS is non-nil, no headers are hidden."
10360   (save-excursion
10361     ;; Make sure we start in a summary buffer.
10362     (or (eq major-mode 'gnus-summary-mode)
10363         (set-buffer gnus-summary-buffer))
10364     (setq gnus-summary-buffer (current-buffer))
10365     ;; Make sure the connection to the server is alive.
10366     (or (gnus-server-opened (gnus-find-method-for-group gnus-newsgroup-name))
10367         (progn
10368           (gnus-check-news-server 
10369            (gnus-find-method-for-group gnus-newsgroup-name))
10370           (gnus-request-group gnus-newsgroup-name t)))
10371     (or gnus-newsgroup-headers-hashtb-by-number
10372         (gnus-make-headers-hashtable-by-number))
10373     (let* ((article (if header (header-number header) article))
10374            (summary-buffer (current-buffer))
10375            (internal-hook gnus-article-internal-prepare-hook)
10376            (group gnus-newsgroup-name)
10377            result)
10378       (save-excursion
10379         (gnus-article-setup-buffer)
10380         (set-buffer gnus-article-buffer)
10381         (if (not (setq result (let ((buffer-read-only nil))
10382                                 (gnus-request-article-this-buffer 
10383                                  article group))))
10384             ;; There is no such article.
10385             (save-excursion
10386               (if (not (numberp article))
10387                   ()
10388                 (setq gnus-article-current 
10389                       (cons gnus-newsgroup-name article))
10390                 (set-buffer gnus-summary-buffer)
10391                 (setq gnus-current-article article)
10392                 (gnus-summary-mark-article article gnus-canceled-mark))
10393               (gnus-message 1 "No such article (may be canceled)")
10394               (ding)
10395               nil)
10396           (if (or (eq result 'pseudo) (eq result 'nneething))
10397               (progn
10398                 (save-excursion
10399                   (set-buffer summary-buffer)
10400                   (setq gnus-last-article gnus-current-article
10401                         gnus-newsgroup-history (cons gnus-current-article
10402                                                      gnus-newsgroup-history)
10403                         gnus-current-article 0
10404                         gnus-current-headers nil
10405                         gnus-article-current nil)
10406                   (if (eq result 'nneething)
10407                       (gnus-configure-windows 'summary)
10408                     (gnus-configure-windows 'article))
10409                   (gnus-set-global-variables))
10410                 (gnus-set-mode-line 'article))
10411             ;; The result from the `request' was an actual article -
10412             ;; or at least some text that is now displayed in the
10413             ;; article buffer.
10414             (if (and (numberp article)
10415                      (not (eq article gnus-current-article)))
10416                 ;; Seems like a new article has been selected.
10417                 ;; `gnus-current-article' must be an article number.
10418                 (save-excursion
10419                   (set-buffer summary-buffer)
10420                   (setq gnus-last-article gnus-current-article
10421                         gnus-newsgroup-history (cons gnus-current-article
10422                                                      gnus-newsgroup-history)
10423                         gnus-current-article article
10424                         gnus-current-headers 
10425                         (gnus-get-header-by-number gnus-current-article)
10426                         gnus-article-current 
10427                         (cons gnus-newsgroup-name gnus-current-article))
10428                   (gnus-summary-show-thread)
10429                   (run-hooks 'gnus-mark-article-hook)
10430                   (gnus-set-mode-line 'summary)
10431                   (and gnus-visual 
10432                        (run-hooks 'gnus-visual-mark-article-hook))
10433                   ;; Set the global newsgroup variables here.
10434                   ;; Suggested by Jim Sisolak
10435                   ;; <sisolak@trans4.neep.wisc.edu>.
10436                   (gnus-set-global-variables)
10437                   (setq gnus-have-all-headers 
10438                         (or all-headers gnus-show-all-headers))
10439                   (and gnus-use-cache 
10440                        (gnus-cache-possibly-enter-article
10441                         group article
10442                         (gnus-get-header-by-number article)
10443                         (memq article gnus-newsgroup-marked)
10444                         (memq article gnus-newsgroup-dormant)
10445                         (memq article gnus-newsgroup-unreads)))))
10446             ;; Hooks for getting information from the article.
10447             ;; This hook must be called before being narrowed.
10448             (let (buffer-read-only)
10449               (run-hooks 'internal-hook)
10450               (run-hooks 'gnus-article-prepare-hook)
10451               ;; Decode MIME message.
10452               (if (and gnus-show-mime
10453                        (or (not gnus-strict-mime)
10454                            (gnus-fetch-field "Mime-Version")))
10455                   (funcall gnus-show-mime-method))
10456               ;; Perform the article display hooks.
10457               (run-hooks 'gnus-article-display-hook))
10458             ;; Do page break.
10459             (goto-char (point-min))
10460             (and gnus-break-pages (gnus-narrow-to-page))
10461             (gnus-set-mode-line 'article)
10462             (gnus-configure-windows 'article)
10463             (goto-char (point-min))
10464             t))))))
10465
10466 (defun gnus-article-show-all-headers ()
10467   "Show all article headers in article mode buffer."
10468   (save-excursion 
10469     (gnus-article-setup-buffer)
10470     (set-buffer gnus-article-buffer)
10471     (let ((buffer-read-only nil))
10472       (remove-text-properties (point-min) (point-max) 
10473                               gnus-hidden-properties))))
10474
10475 (defun gnus-article-hide-headers-if-wanted ()
10476   "Hide unwanted headers if `gnus-have-all-headers' is nil.
10477 Provided for backwards compatability."
10478   (or (save-excursion (set-buffer gnus-summary-buffer) gnus-have-all-headers)
10479       (gnus-article-hide-headers)))
10480
10481 (defun gnus-article-hide-headers (&optional delete)
10482   "Hide unwanted headers and possibly sort them as well."
10483   (interactive "P")
10484   (save-excursion
10485     (set-buffer gnus-article-buffer)
10486     (save-restriction
10487       (let ((sorted gnus-sorted-header-list)
10488             (buffer-read-only nil)
10489             want-list beg want-l)
10490         ;; First we narrow to just the headers.
10491         (widen)
10492         (goto-char (point-min))
10493         ;; Hide any "From " lines at the beginning of (mail) articles. 
10494         (while (looking-at "From ")
10495           (forward-line 1))
10496         (if (bobp) 
10497             (add-text-properties (point-min) (point) gnus-hidden-properties))
10498         ;; Then treat the rest of the header lines.
10499         (narrow-to-region 
10500          (point) 
10501          (progn (search-forward "\n\n" nil t) (forward-line -1) (point)))
10502         ;; Then we use the two regular expressions
10503         ;; `gnus-ignored-headers' and `gnus-visible-headers' to
10504         ;; select which header lines is to remain visible in the
10505         ;; article buffer.
10506         (goto-char (point-min))
10507         (while (re-search-forward "^[^ \t]*:" nil t)
10508           (beginning-of-line)
10509           ;; We add the headers we want to keep to a list and delete
10510           ;; them from the buffer.
10511           (if (or (and (stringp gnus-visible-headers)
10512                        (looking-at gnus-visible-headers))
10513                   (and (not (stringp gnus-visible-headers))
10514                        (stringp gnus-ignored-headers)
10515                        (not (looking-at gnus-ignored-headers))))
10516               (progn
10517                 (setq beg (point))
10518                 (forward-line 1)
10519                 ;; Be sure to get multi-line headers...
10520                 (re-search-forward "^[^ \t]*:" nil t)
10521                 (beginning-of-line)
10522                 (setq want-list 
10523                       (cons (buffer-substring beg (point)) want-list))
10524                 (delete-region beg (point))
10525                 (goto-char beg))
10526             (forward-line 1)))
10527         ;; Next we perform the sorting by looking at
10528         ;; `gnus-sorted-header-list'. 
10529         (goto-char (point-min))
10530         (while (and sorted want-list)
10531           (setq want-l want-list)
10532           (while (and want-l
10533                       (not (string-match (car sorted) (car want-l))))
10534             (setq want-l (cdr want-l)))
10535           (if want-l 
10536               (progn
10537                 (insert (car want-l))
10538                 (setq want-list (delq (car want-l) want-list))))
10539           (setq sorted (cdr sorted)))
10540         ;; Any headers that were not matched by the sorted list we
10541         ;; just tack on the end of the visible header list.
10542         (while want-list
10543           (insert (car want-list))
10544           (setq want-list (cdr want-list)))
10545         ;; And finally we make the unwanted headers invisible.
10546         (if delete
10547             (delete-region (point) (point-max))
10548           ;; Suggested by Sudish Joseph <joseph@cis.ohio-state.edu>.
10549           (add-text-properties (point) (point-max) gnus-hidden-properties))))))
10550
10551 ;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.
10552 (defun gnus-article-treat-overstrike ()
10553   "Translate overstrikes into bold text."
10554   (interactive)
10555   (save-excursion
10556     (set-buffer gnus-article-buffer)
10557     (let ((buffer-read-only nil))
10558       (while (search-forward "\b" nil t)
10559         (let ((next (following-char))
10560               (previous (char-after (- (point) 2))))
10561           (cond ((eq next previous)
10562                  (delete-region (- (point) 2) (point))
10563                  (put-text-property (point) (1+ (point))
10564                                     'face 'bold))
10565                 ((eq next ?_)
10566                  (delete-region (1- (point)) (1+ (point)))
10567                  (put-text-property (1- (point)) (point)
10568                                     'face 'underline))
10569                 ((eq previous ?_)
10570                  (delete-region (- (point) 2) (point))
10571                  (put-text-property (point) (1+ (point))
10572                                     'face 'underline))))))))
10573
10574 (defun gnus-article-word-wrap ()
10575   "Format too long lines."
10576   (interactive)
10577   (save-excursion
10578     (set-buffer gnus-article-buffer)
10579     (let ((buffer-read-only nil))
10580       (goto-char (point-min))
10581       (search-forward "\n\n" nil t)
10582       (end-of-line 1)
10583       (let ((paragraph-start "^[>|#:<;* ]*[ \t]*$")
10584             (adaptive-fill-regexp "[ \t]*\\([|#:<;>*]+ *\\)?")
10585             (adaptive-fill-mode t))
10586         (while (not (eobp))
10587           (and (>= (current-column) (min fill-column (window-width)))
10588                (/= (preceding-char) ?:)
10589                (fill-paragraph nil))
10590           (end-of-line 2))))))
10591
10592 (defun gnus-article-remove-cr ()
10593   "Remove carriage returns from an article."
10594   (interactive)
10595   (save-excursion
10596     (set-buffer gnus-article-buffer)
10597     (let ((buffer-read-only nil))
10598       (goto-char (point-min))
10599       (while (search-forward "\r" nil t)
10600         (replace-match "" t t)))))
10601
10602 (defun gnus-article-display-x-face (&optional force)
10603   "Look for an X-Face header and display it if present."
10604   (interactive (list 'force))
10605   (save-excursion
10606     (set-buffer gnus-article-buffer)
10607     (let ((inhibit-point-motion-hooks t)
10608           (case-fold-search nil))
10609       (save-restriction
10610         (goto-char (point-min))
10611         (search-forward "\n\n")
10612         (narrow-to-region (point-min) (point))
10613         (goto-char (point-min))
10614         (if (or (not gnus-article-x-face-command)
10615                 (and (not force)
10616                      (or (not gnus-article-x-face-too-ugly)
10617                          (string-match gnus-article-x-face-too-ugly
10618                                        (mail-fetch-field "from"))))
10619                 (progn
10620                   (goto-char (point-min))
10621                   (not (re-search-forward "^X-Face: " nil t))))
10622             nil
10623           (let ((beg (point))
10624                 (end (1- (re-search-forward "^\\($\\|[^ \t]\\)" nil t))))
10625             (if (symbolp gnus-article-x-face-command)
10626                 (and (or (fboundp gnus-article-x-face-command)
10627                          (error "%s is not a function"
10628                                 gnus-article-x-face-command))
10629                      (funcall gnus-article-x-face-command beg end))
10630               (call-process-region beg end "sh" nil 0 nil
10631                                    "-c" gnus-article-x-face-command))))))))
10632
10633 (defun gnus-article-de-quoted-unreadable (&optional force)
10634   "Do a naïve translation of a quoted-printable-encoded article.
10635 This is in no way, shape or form meant as a replacement for real MIME
10636 processing, but is simply a stop-gap measure until MIME support is
10637 written.
10638 If FORCE, decode the article whether it is marked as quoted-printable
10639 or not." 
10640   (interactive (list 'force))
10641   (save-excursion
10642     (set-buffer gnus-article-buffer)
10643     (let ((case-fold-search t)
10644           (buffer-read-only nil)
10645           (type (gnus-fetch-field "content-transfer-encoding")))
10646       (if (or force (and type (string-match "quoted-printable" type)))
10647           (progn
10648             (goto-char (point-min))
10649             (search-forward "\n\n" nil 'move)
10650             (gnus-mime-decode-quoted-printable (point) (point-max)))))))
10651
10652 (defun gnus-mime-decode-quoted-printable (from to)
10653   ;; Decode quoted-printable from region between FROM and TO.
10654   (save-excursion
10655     (goto-char from)
10656     (while (search-forward "=" to t)
10657       (cond ((eq (following-char) ?\n)
10658              (delete-char -1)
10659              (delete-char 1))
10660             ((looking-at "[0-9A-F][0-9A-F]")
10661              (delete-char -1)
10662              (insert (hexl-hex-string-to-integer
10663                       (buffer-substring (point) (+ 2 (point)))))
10664              (delete-char 2))
10665             ((gnus-message 3 "Malformed MIME quoted-printable message"))))))
10666
10667 (defvar gnus-article-time-units
10668   (list (cons 'year (* 365.25 24 60 60))
10669         (cons 'week (* 7 24 60 60))
10670         (cons 'day (* 24 60 60))
10671         (cons 'hour (* 60 60))
10672         (cons 'minute 60)
10673         (cons 'second 1)))
10674
10675 (defun gnus-article-date-ut (&optional type)
10676   "Convert DATE date to universal time in the current article.
10677 If TYPE is `local', convert to local time; if it is `lapsed', output
10678 how much time has lapsed since DATE."
10679   (interactive (list 'ut))
10680   (let ((date (header-date (or gnus-current-headers 
10681                                (gnus-get-header-by-number
10682                                 (gnus-summary-article-number))"")))
10683         (date-regexp "^Date: \\|^X-Sent: "))
10684     (if (or (not date)
10685             (string= date ""))
10686         ()
10687       (save-excursion
10688         (set-buffer gnus-article-buffer)
10689         (let ((buffer-read-only nil))
10690           (goto-char (point-min))
10691           (if (and (re-search-forward date-regexp nil t)
10692                    (progn 
10693                      (beginning-of-line)
10694                      (looking-at date-regexp)))
10695               (delete-region (gnus-point-at-bol)
10696                              (progn (end-of-line) (1+ (point))))
10697             (goto-char (point-min))
10698             (goto-char (- (search-forward "\n\n") 2)))
10699           (insert
10700            (cond 
10701             ((eq type 'local)
10702              (concat "Date: " (condition-case ()
10703                                   (timezone-make-date-arpa-standard date)
10704                                 (error date))
10705                      "\n"))
10706             ((eq type 'ut)
10707              (concat "Date: "
10708                      (condition-case ()
10709                          (timezone-make-date-arpa-standard date nil "UT")
10710                        (error date))
10711                      "\n"))
10712             ((eq type 'lapsed)
10713              ;; If the date is seriously mangled, the timezone
10714              ;; functions are liable to bug out, so we condition-case
10715              ;; the entire thing.  
10716              (let* ((sec (condition-case ()
10717                              (max (- (gnus-seconds-since-epoch 
10718                                       (timezone-make-date-arpa-standard
10719                                        (current-time-string) 
10720                                        (current-time-zone) "UT"))
10721                                      (gnus-seconds-since-epoch 
10722                                       (timezone-make-date-arpa-standard 
10723                                        date nil "UT")))
10724                                   0)
10725                            (error 0)))
10726                     num prev)
10727                (concat
10728                 "X-Sent: "
10729                 (mapconcat 
10730                  (lambda (unit)
10731                    (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
10732                        ""
10733                      (setq sec (- sec (* num (cdr unit))))
10734                      (prog1
10735                          (concat (if prev ", " "") (int-to-string (floor num))
10736                                  " " (symbol-name (car unit))
10737                                  (if (> num 1) "s" ""))
10738                        (setq prev t))))
10739                  gnus-article-time-units "")
10740                 " ago\n")))
10741             (t
10742              (error "Unknown conversion type: %s" type)))))))))
10743
10744 (defun gnus-article-date-local ()
10745   "Convert the current article date to the local timezone."
10746   (interactive)
10747   (gnus-article-date-ut 'local))
10748
10749 (defun gnus-article-date-lapsed ()
10750   "Convert the current article date to time lapsed since it was sent."
10751   (interactive)
10752   (gnus-article-date-ut 'lapsed))
10753
10754 (defun gnus-article-maybe-highlight ()
10755   (if gnus-visual (gnus-article-highlight)))
10756
10757 ;; Article savers.
10758
10759 (defun gnus-output-to-rmail (file-name)
10760   "Append the current article to an Rmail file named FILE-NAME."
10761   (require 'rmail)
10762   ;; Most of these codes are borrowed from rmailout.el.
10763   (setq file-name (expand-file-name file-name))
10764   (setq rmail-default-rmail-file file-name)
10765   (let ((artbuf (current-buffer))
10766         (tmpbuf (get-buffer-create " *Gnus-output*")))
10767     (save-excursion
10768       (or (get-file-buffer file-name)
10769           (file-exists-p file-name)
10770           (if (gnus-yes-or-no-p
10771                (concat "\"" file-name "\" does not exist, create it? "))
10772               (let ((file-buffer (create-file-buffer file-name)))
10773                 (save-excursion
10774                   (set-buffer file-buffer)
10775                   (rmail-insert-rmail-file-header)
10776                   (let ((require-final-newline nil))
10777                     (write-region (point-min) (point-max) file-name t 1)))
10778                 (kill-buffer file-buffer))
10779             (error "Output file does not exist")))
10780       (set-buffer tmpbuf)
10781       (buffer-disable-undo (current-buffer))
10782       (erase-buffer)
10783       (insert-buffer-substring artbuf)
10784       (gnus-convert-article-to-rmail)
10785       ;; Decide whether to append to a file or to an Emacs buffer.
10786       (let ((outbuf (get-file-buffer file-name)))
10787         (if (not outbuf)
10788             (append-to-file (point-min) (point-max) file-name)
10789           ;; File has been visited, in buffer OUTBUF.
10790           (set-buffer outbuf)
10791           (let ((buffer-read-only nil)
10792                 (msg (and (boundp 'rmail-current-message)
10793                           (symbol-value 'rmail-current-message))))
10794             ;; If MSG is non-nil, buffer is in RMAIL mode.
10795             (if msg
10796                 (progn (widen)
10797                        (narrow-to-region (point-max) (point-max))))
10798             (insert-buffer-substring tmpbuf)
10799             (if msg
10800                 (progn
10801                   (goto-char (point-min))
10802                   (widen)
10803                   (search-backward "\^_")
10804                   (narrow-to-region (point) (point-max))
10805                   (goto-char (1+ (point-min)))
10806                   (rmail-count-new-messages t)
10807                   (rmail-show-message msg)))))))
10808     (kill-buffer tmpbuf)))
10809
10810 (defun gnus-output-to-file (file-name)
10811   "Append the current article to a file named FILE-NAME."
10812   (setq file-name (expand-file-name file-name))
10813   (let ((artbuf (current-buffer))
10814         (tmpbuf (get-buffer-create " *Gnus-output*")))
10815     (save-excursion
10816       (set-buffer tmpbuf)
10817       (buffer-disable-undo (current-buffer))
10818       (erase-buffer)
10819       (insert-buffer-substring artbuf)
10820       ;; Append newline at end of the buffer as separator, and then
10821       ;; save it to file.
10822       (goto-char (point-max))
10823       (insert "\n")
10824       (append-to-file (point-min) (point-max) file-name))
10825     (kill-buffer tmpbuf)))
10826
10827 (defun gnus-convert-article-to-rmail ()
10828   "Convert article in current buffer to Rmail message format."
10829   (let ((buffer-read-only nil))
10830     ;; Convert article directly into Babyl format.
10831     ;; Suggested by Rob Austein <sra@lcs.mit.edu>
10832     (goto-char (point-min))
10833     (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
10834     (while (search-forward "\n\^_" nil t) ;single char
10835       (replace-match "\n^_" t t))               ;2 chars: "^" and "_"
10836     (goto-char (point-max))
10837     (insert "\^_")))
10838
10839 (defun gnus-narrow-to-page (&optional arg)
10840   "Make text outside current page invisible except for page delimiter.
10841 A numeric arg specifies to move forward or backward by that many pages,
10842 thus showing a page other than the one point was originally in."
10843   (interactive "P")
10844   (setq arg (if arg (prefix-numeric-value arg) 0))
10845   (save-excursion
10846     (forward-page -1)                   ;Beginning of current page.
10847     (widen)
10848     (if (> arg 0)
10849         (forward-page arg)
10850       (if (< arg 0)
10851           (forward-page (1- arg))))
10852     ;; Find the end of the page.
10853     (forward-page)
10854     ;; If we stopped due to end of buffer, stay there.
10855     ;; If we stopped after a page delimiter, put end of restriction
10856     ;; at the beginning of that line.
10857     ;; These are commented out.
10858     ;;    (if (save-excursion (beginning-of-line)
10859     ;;                  (looking-at page-delimiter))
10860     ;;  (beginning-of-line))
10861     (narrow-to-region (point)
10862                       (progn
10863                         ;; Find the top of the page.
10864                         (forward-page -1)
10865                         ;; If we found beginning of buffer, stay there.
10866                         ;; If extra text follows page delimiter on same line,
10867                         ;; include it.
10868                         ;; Otherwise, show text starting with following line.
10869                         (if (and (eolp) (not (bobp)))
10870                             (forward-line 1))
10871                         (point)))))
10872
10873 (defun gnus-gmt-to-local ()
10874   "Rewrite Date header described in GMT to local in current buffer.
10875 Intended to be used with gnus-article-prepare-hook."
10876   (save-excursion
10877     (save-restriction
10878       (widen)
10879       (goto-char (point-min))
10880       (narrow-to-region (point-min)
10881                         (progn (search-forward "\n\n" nil 'move) (point)))
10882       (goto-char (point-min))
10883       (if (re-search-forward "^Date:[ \t]\\(.*\\)$" nil t)
10884           (let ((buffer-read-only nil)
10885                 (date (buffer-substring-no-properties
10886                        (match-beginning 1) (match-end 1))))
10887             (delete-region (match-beginning 1) (match-end 1))
10888             (insert
10889              (timezone-make-date-arpa-standard 
10890               date nil (current-time-zone))))))))
10891
10892
10893 ;; Article mode commands
10894
10895 (defun gnus-article-next-page (lines)
10896   "Show next page of current article.
10897 If end of article, return non-nil. Otherwise return nil.
10898 Argument LINES specifies lines to be scrolled up."
10899   (interactive "P")
10900   (move-to-window-line -1)
10901   ;; Fixed by enami@ptgd.sony.co.jp (enami tsugutomo)
10902   (if (save-excursion
10903         (end-of-line)
10904         (and (pos-visible-in-window-p)  ;Not continuation line.
10905              (eobp)))
10906       ;; Nothing in this page.
10907       (if (or (not gnus-break-pages)
10908               (save-excursion
10909                 (save-restriction
10910                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
10911           t                             ;Nothing more.
10912         (gnus-narrow-to-page 1)         ;Go to next page.
10913         nil)
10914     ;; More in this page.
10915     (condition-case ()
10916         (scroll-up lines)
10917       (end-of-buffer
10918        ;; Long lines may cause an end-of-buffer error.
10919        (goto-char (point-max))))
10920     nil))
10921
10922 (defun gnus-article-prev-page (lines)
10923   "Show previous page of current article.
10924 Argument LINES specifies lines to be scrolled down."
10925   (interactive "P")
10926   (move-to-window-line 0)
10927   (if (and gnus-break-pages
10928            (bobp)
10929            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
10930       (progn
10931         (gnus-narrow-to-page -1) ;Go to previous page.
10932         (goto-char (point-max))
10933         (recenter -1))
10934     (scroll-down lines)))
10935
10936 (defun gnus-article-refer-article ()
10937   "Read article specified by message-id around point."
10938   (interactive)
10939   (search-forward ">" nil t)    ;Move point to end of "<....>".
10940   (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
10941       (let ((message-id
10942              (buffer-substring (match-beginning 1) (match-end 1))))
10943         (set-buffer gnus-summary-buffer)
10944         (gnus-summary-refer-article message-id))
10945     (error "No references around point")))
10946
10947 (defun gnus-article-show-summary ()
10948   "Reconfigure windows to show summary buffer."
10949   (interactive)
10950   (gnus-configure-windows 'article)
10951   (gnus-summary-goto-subject gnus-current-article))
10952
10953 (defun gnus-article-describe-briefly ()
10954   "Describe article mode commands briefly."
10955   (interactive)
10956   (gnus-message 6
10957    (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")))
10958
10959 (defun gnus-article-summary-command ()
10960   "Execute the last keystroke in the summary buffer."
10961   (interactive)
10962   (let ((obuf (current-buffer))
10963         (owin (current-window-configuration))
10964         func)
10965     (switch-to-buffer gnus-summary-buffer 'norecord)
10966     (setq func (lookup-key (current-local-map) (this-command-keys)))
10967     (call-interactively func)
10968     (set-buffer obuf)
10969     (set-window-configuration owin)
10970     (set-window-start (get-buffer-window (current-buffer)) (point))))
10971
10972 (defun gnus-article-summary-command-nosave ()
10973   "Execute the last keystroke in the summary buffer."
10974   (interactive)
10975   (let (func)
10976     (pop-to-buffer gnus-summary-buffer 'norecord)
10977     (setq func (lookup-key (current-local-map) (this-command-keys)))
10978     (call-interactively func)))
10979
10980 \f
10981 ;; Basic ideas by emv@math.lsa.umich.edu (Edward Vielmetti)
10982
10983 ;;;###autoload
10984 (defalias 'gnus-batch-kill 'gnus-batch-score)
10985 ;;;###autoload
10986 (defun gnus-batch-score ()
10987   "Run batched scoring.
10988 Usage: emacs -batch -l gnus -f gnus-batch-score <newsgroups> ...
10989 Newsgroups is a list of strings in Bnews format.  If you want to score
10990 the comp hierarchy, you'd say \"comp.all\". If you would not like to
10991 score the alt hierarchy, you'd say \"!alt.all\"."
10992   (interactive)
10993   (let* ((yes-and-no
10994           (gnus-newsrc-parse-options
10995            (apply (function concat)
10996                   (mapcar (lambda (g) (concat g " "))
10997                           command-line-args-left))))
10998          (gnus-expert-user t)
10999          (nnmail-spool-file nil)
11000          (gnus-use-dribble-file nil)
11001          (yes (car yes-and-no))
11002          (no (cdr yes-and-no))
11003          group newsrc entry
11004          ;; Disable verbose message.
11005          gnus-novice-user gnus-large-newsgroup)
11006     ;; Eat all arguments.
11007     (setq command-line-args-left nil)
11008     ;; Start Gnus.
11009     (gnus)
11010     ;; Apply kills to specified newsgroups in command line arguments.
11011     (setq newsrc (cdr gnus-newsrc-alist))
11012     (while newsrc
11013       (setq group (car (car newsrc)))
11014       (setq entry (gnus-gethash group gnus-newsrc-hashtb))
11015       (if (and (<= (nth 1 (car newsrc)) gnus-level-subscribed)
11016                (and (car entry)
11017                     (or (eq (car entry) t)
11018                         (not (zerop (car entry)))))
11019                (if yes (string-match yes group) t)
11020                (or (null no) (not (string-match no group))))
11021           (progn
11022             (gnus-summary-read-group group nil t)
11023             (and (eq (current-buffer) (get-buffer gnus-summary-buffer))
11024                  (gnus-summary-exit))))
11025       (setq newsrc (cdr newsrc)))
11026     ;; Exit Emacs.
11027     (switch-to-buffer gnus-group-buffer)
11028     (gnus-group-save-newsrc)))
11029
11030 (defun gnus-apply-kill-file ()
11031   "Apply a kill file to the current newsgroup.
11032 Returns the number of articles marked as read."
11033   (if (or (file-exists-p (gnus-newsgroup-kill-file nil))
11034           (file-exists-p (gnus-newsgroup-kill-file gnus-newsgroup-name)))
11035       (gnus-apply-kill-file-internal)
11036     0))
11037
11038 (defun gnus-kill-save-kill-buffer ()
11039   (save-excursion
11040     (let ((file (gnus-newsgroup-kill-file gnus-newsgroup-name)))
11041       (if (get-file-buffer file)
11042           (progn
11043             (set-buffer (get-file-buffer file))
11044             (and (buffer-modified-p) (save-buffer))
11045             (kill-buffer (current-buffer)))))))
11046
11047 (defvar gnus-kill-file-name "KILL"
11048   "Suffix of the kill files.")
11049
11050 (defun gnus-newsgroup-kill-file (newsgroup)
11051   "Return the name of a kill file name for NEWSGROUP.
11052 If NEWSGROUP is nil, return the global kill file name instead."
11053   (cond ((or (null newsgroup)
11054              (string-equal newsgroup ""))
11055          ;; The global KILL file is placed at top of the directory.
11056          (expand-file-name gnus-kill-file-name
11057                            (or gnus-kill-files-directory "~/News")))
11058         ((gnus-use-long-file-name 'not-kill)
11059          ;; Append ".KILL" to newsgroup name.
11060          (expand-file-name (concat newsgroup "." gnus-kill-file-name)
11061                            (or gnus-kill-files-directory "~/News")))
11062         (t
11063          ;; Place "KILL" under the hierarchical directory.
11064          (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
11065                                    "/" gnus-kill-file-name)
11066                            (or gnus-kill-files-directory "~/News")))))
11067
11068 \f
11069 ;;;
11070 ;;; Dribble file
11071 ;;;
11072
11073 (defvar gnus-dribble-ignore nil)
11074
11075 (defun gnus-dribble-file-name ()
11076   (concat gnus-startup-file "-dribble"))
11077
11078 (defun gnus-dribble-open ()
11079   (save-excursion 
11080     (set-buffer 
11081      (setq gnus-dribble-buffer (find-file-noselect (gnus-dribble-file-name))))
11082     (buffer-disable-undo (current-buffer))
11083     (bury-buffer gnus-dribble-buffer)
11084     (auto-save-mode t)
11085     (goto-char (point-max))))
11086
11087 (defun gnus-dribble-enter (string)
11088   (if (and (not gnus-dribble-ignore)
11089            gnus-dribble-buffer
11090            (buffer-name gnus-dribble-buffer))
11091       (let ((obuf (current-buffer)))
11092         (set-buffer gnus-dribble-buffer)
11093         (insert string "\n")
11094         (set-window-point (get-buffer-window (current-buffer)) (point-max))
11095         (set-buffer obuf))))
11096
11097 (defun gnus-dribble-read-file ()
11098   (let ((dribble-file (gnus-dribble-file-name)))
11099     (save-excursion 
11100       (set-buffer (setq gnus-dribble-buffer 
11101                         (get-buffer-create 
11102                          (file-name-nondirectory dribble-file))))
11103       (gnus-add-current-to-buffer-list)
11104       (erase-buffer)
11105       (set-visited-file-name dribble-file)
11106       (buffer-disable-undo (current-buffer))
11107       (bury-buffer (current-buffer))
11108       (set-buffer-modified-p nil)
11109       (let ((auto (make-auto-save-file-name))
11110             (gnus-dribble-ignore t))
11111         (if (or (file-exists-p auto) (file-exists-p dribble-file))
11112             (progn
11113               (if (file-newer-than-file-p auto dribble-file)
11114                   (setq dribble-file auto))
11115               (insert-file-contents dribble-file)
11116               (if (not (zerop (buffer-size)))
11117                   (set-buffer-modified-p t))
11118               (if (gnus-y-or-n-p 
11119                    "Auto-save file exists. Do you want to read it? ")
11120                   (progn
11121                     (gnus-message 5 "Reading %s..." dribble-file) 
11122                     (eval-buffer (current-buffer))
11123                     (gnus-message 5 "Reading %s...done" dribble-file)))))))))
11124
11125 (defun gnus-dribble-delete-file ()
11126   (if (file-exists-p (gnus-dribble-file-name))
11127       (delete-file (gnus-dribble-file-name)))
11128   (if gnus-dribble-buffer
11129       (save-excursion
11130         (set-buffer gnus-dribble-buffer)
11131         (let ((auto (make-auto-save-file-name)))
11132           (if (file-exists-p auto)
11133               (delete-file auto))
11134           (erase-buffer)
11135           (set-buffer-modified-p nil)))))
11136
11137 (defun gnus-dribble-save ()
11138   (if (and gnus-dribble-buffer
11139            (buffer-name gnus-dribble-buffer))
11140       (save-excursion
11141         (set-buffer gnus-dribble-buffer)
11142         (save-buffer))))
11143
11144 (defun gnus-dribble-clear ()
11145   (save-excursion
11146     (if (gnus-buffer-exists-p gnus-dribble-buffer)
11147         (progn
11148           (set-buffer gnus-dribble-buffer)
11149           (erase-buffer)
11150           (set-buffer-modified-p nil)
11151           (setq buffer-saved-size (buffer-size))))))
11152
11153 ;;;
11154 ;;; Server Communication
11155 ;;;
11156
11157 ;; All the Gnus backends have the same interface, and should return
11158 ;; data in a similar format. Below is an overview of what functions
11159 ;; these packages must supply and what results they should return.
11160 ;;
11161 ;; Variables:
11162 ;;
11163 ;; `nntp-server-buffer' - All data should be returned to Gnus in this
11164 ;; buffer. 
11165 ;;
11166 ;; Functions for the imaginary backend `choke':
11167 ;;
11168 ;; `choke-retrieve-headers ARTICLES &optional GROUP SERVER'
11169 ;; Should return all headers for all ARTICLES, or return NOV lines for
11170 ;; the same.
11171 ;;
11172 ;; `choke-request-group GROUP &optional SERVER DISCARD'
11173 ;; Switch to GROUP. If DISCARD is nil, active information on the group
11174 ;; must be returned.
11175 ;;
11176 ;; `choke-close-group GROUP &optional SERVER'
11177 ;; Close group. Most backends won't have to do anything with this
11178 ;; call, but it is an opportunity to clean up, if that is needed. It
11179 ;; is called when Gnus exits a group.
11180 ;;
11181 ;; `choke-request-article ARTICLE &optional GROUP SERVER'
11182 ;; Return ARTICLE, which is either an article number or
11183 ;; message-id. Note that not all backends can return articles based on
11184 ;; message-id. 
11185 ;;
11186 ;; `choke-request-list SERVER'
11187 ;; Return a list of all newsgroups on SERVER.
11188 ;;
11189 ;; `choke-request-list-newsgroups SERVER'
11190 ;; Return a list of descriptions of all newsgroups on SERVER.
11191 ;;
11192 ;; `choke-request-newgroups DATE &optional SERVER'
11193 ;; Return a list of all groups that have arrived after DATE on
11194 ;; SERVER. Note that the date doesn't have to be respected - Gnus will
11195 ;; always check whether the groups are old or not. Backends that do
11196 ;; not store date information may just return the entire list of
11197 ;; groups, although this might not be a good idea in general.
11198 ;;
11199 ;; `choke-request-post-buffer METHOD HEADER ARTICLE-BUFFER GROUP INFO'
11200 ;; Should return a buffer that is suitable for "posting". nnspool and
11201 ;; nntp return a `*post-buffer*', and nnmail return a `*mail*'
11202 ;; buffer. This function should fill out the appropriate headers. 
11203 ;;
11204 ;; `choke-request-post &optional SERVER'
11205 ;; Function that will be called from a buffer to be posted. 
11206 ;;
11207 ;; `choke-open-server SERVER &optional ARGUMENT'
11208 ;; Open a connection to SERVER.
11209 ;;
11210 ;; `choke-close-server &optional SERVER'
11211 ;; Close the connection to SERVER.
11212 ;;
11213 ;; `choke-server-opened &optional SERVER'
11214 ;; Whether the conenction to SERVER is opened or not.
11215 ;;
11216 ;; `choke-server-status &optional SERVER'
11217 ;; Should return a status string (not in the nntp buffer, but as the
11218 ;; result of the function).
11219 ;;
11220 ;; `choke-retrieve-groups GROUPS &optional SERVER'
11221 ;; Optional function for retrieving active file info on all groups in
11222 ;; GROUPS.  Two return formats are supported: The normal active file
11223 ;; format, and a list of GROUP lines.  This function should return (as
11224 ;; a function value) either `active' or `group', depending on what
11225 ;; format it returns.
11226 ;;
11227 ;; The following functions are optional and apply only to backends
11228 ;; that are able to control the contents of their groups totally
11229 ;; (ie. mail backends.)  Backends that aren't able to do that
11230 ;; shouldn't define these functions at all. Gnus will check for their
11231 ;; presence before attempting to call them.
11232 ;;
11233 ;; `choke-request-expire-articles ARTICLES &optional NEWSGROUP SERVER'
11234 ;; Should expire (according to some aging scheme) all ARTICLES. Most
11235 ;; backends will not be able to expire articles. Should return a list
11236 ;; of all articles that were not expired.
11237 ;;
11238 ;; `choke-request-move-article ARTICLE GROUP SERVER ACCEPT-FORM &optional LAST'
11239 ;; Should move ARTICLE from GROUP on SERVER by using ACCEPT-FORM.
11240 ;; Removes any information it has added to the article (extra headers,
11241 ;; whatever - make it as clean as possible), and then passes the
11242 ;; article on by evaling ACCEPT-FORM, which is normally a call to the
11243 ;; function described below. If the ACCEPT-FORM returns a non-nil
11244 ;; value, the article should then be deleted. If LAST is nil, that
11245 ;; means that there will be further calls to this function. This might
11246 ;; be taken as an advice not to save buffers/internal variables just
11247 ;; yet, but wait until the last call to speed things up.
11248 ;;
11249 ;; `choke-request-accept-article GROUP &optional LAST' 
11250 ;; The contents of the current buffer will be put into GROUP.  There
11251 ;; should, of course, be an article in the current buffer.  This
11252 ;; function is normally only called by the function described above,
11253 ;; and LAST works the same way as in that function.
11254 ;;
11255 ;; `choke-request-replace-article ARTICLE GROUP BUFFER'
11256 ;; Replace ARTICLE in GROUP with the contents of BUFFER.
11257 ;; This provides an easy interface for allowing editing of
11258 ;; articles. Note that even headers may be edited, so the backend has
11259 ;; to update any tables (nov buffers, etc) that it maintains after
11260 ;; replacing the article.
11261 ;;
11262 ;; `choke-request-create-group GROUP &optional SERVER'
11263 ;; Create GROUP on SERVER.  This might be a new, empty group, or it
11264 ;; might be a group that already exists, but hasn't been registered
11265 ;; yet. 
11266 ;;
11267 ;; All these functions must return nil if they couldn't service the
11268 ;; request. If the optional arguments are not supplied, some "current"
11269 ;; or "default" values should be used. In short, one should emulate an
11270 ;; NNTP server, in a way.
11271 ;;
11272 ;; If you want to write a new backend, you just have to supply the
11273 ;; functions listed above. In addition, you must enter the new backend
11274 ;; into the list of valid select methods:
11275 ;; (setq gnus-valid-select-methods 
11276 ;;       (cons '("choke" mail) gnus-valid-select-methods))
11277 ;; The first element in this list is the name of the backend. Other
11278 ;; elemnets may be `mail' (for mail groups),  `post' (for news
11279 ;; groups), `none' (neither), `respool' (for groups that can control
11280 ;; their contents). 
11281
11282 (defun gnus-start-news-server (&optional confirm)
11283   "Open a method for getting news.
11284 If CONFIRM is non-nil, the user will be asked for an NNTP server."
11285   (let (how)
11286     (if gnus-current-select-method
11287         ;; Stream is already opened.
11288         nil
11289       ;; Open NNTP server.
11290       (if (null gnus-nntp-service) (setq gnus-nntp-server nil))
11291       (if confirm
11292           (progn
11293             ;; Read server name with completion.
11294             (setq gnus-nntp-server
11295                   (completing-read "NNTP server: "
11296                                    (mapcar (lambda (server) (list server))
11297                                            (cons (list gnus-nntp-server)
11298                                                  gnus-secondary-servers))
11299                                    nil nil gnus-nntp-server))))
11300
11301       (if (and gnus-nntp-server 
11302                (stringp gnus-nntp-server)
11303                (not (string= gnus-nntp-server "")))
11304           (setq gnus-select-method
11305                 (cond ((or (string= gnus-nntp-server "")
11306                            (string= gnus-nntp-server "::"))
11307                        (list 'nnspool (system-name)))
11308                       ((string-match "^:" gnus-nntp-server)
11309                        (list 'nnmh gnus-nntp-server 
11310                              (list 'nnmh-directory 
11311                                    (file-name-as-directory
11312                                     (expand-file-name
11313                                      (concat "~/" (substring
11314                                                    gnus-nntp-server 1)))))
11315                              (list 'nnmh-get-new-mail nil)))
11316                       (t
11317                        (list 'nntp gnus-nntp-server)))))
11318
11319       (setq how (car gnus-select-method))
11320       (cond ((eq how 'nnspool)
11321              (require 'nnspool)
11322              (gnus-message 5 "Looking up local news spool..."))
11323             ((eq how 'nnmh)
11324              (require 'nnmh)
11325              (gnus-message 5 "Looking up mh spool..."))
11326             (t
11327              (require 'nntp)))
11328       (setq gnus-current-select-method gnus-select-method)
11329       (run-hooks 'gnus-open-server-hook)
11330       (or 
11331        ;; gnus-open-server-hook might have opened it
11332        (gnus-server-opened gnus-select-method)  
11333        (gnus-open-server gnus-select-method)
11334        (gnus-y-or-n-p
11335         (format
11336          "%s server on %s can't be opened. Continue? "
11337          (car gnus-select-method) (nth 1 gnus-select-method)))
11338        (progn
11339          (gnus-message 1 "Couldn't open server on %s" 
11340                        (nth 1 gnus-select-method))
11341          (ding)
11342          nil)))))
11343
11344 (defun gnus-check-news-server (&optional method)
11345   "If the news server is down, start it up again."
11346   (let ((method (if method method gnus-select-method)))
11347     (and (stringp method)
11348          (setq method (gnus-server-to-method method)))
11349     (if (gnus-server-opened method)
11350         ;; Stream is already opened.
11351         t
11352       ;; Open server.
11353       (gnus-message 5 "Opening server %s on %s..." (car method) (nth 1 method))
11354       (run-hooks 'gnus-open-server-hook)
11355       (or (gnus-server-opened method)
11356           (gnus-open-server method))
11357       (message ""))))
11358
11359 (defun gnus-nntp-message (&optional message)
11360   "Check the status of the NNTP server.
11361 If the status of the server is clear and MESSAGE is non-nil, MESSAGE
11362 is returned insted of the status string."
11363   (let ((status (gnus-status-message (gnus-find-method-for-group 
11364                                       gnus-newsgroup-name)))
11365         (message (or message "")))
11366     (if (and (stringp status) (> (length status) 0))
11367         status message)))
11368
11369 (defun gnus-get-function (method function)
11370   (and (stringp method)
11371        (setq method (gnus-server-to-method method)))
11372   (let ((func (intern (format "%s-%s" (car method) function))))
11373     (if (not (fboundp func)) 
11374         (progn
11375           (require (car method))
11376           (if (not (fboundp func)) 
11377               (error "No such function: %s" func))))
11378     func))
11379
11380 ;;; Interface functions to the backends.
11381
11382 (defun gnus-open-server (method)
11383   (funcall (gnus-get-function method 'open-server)
11384            (nth 1 method) (nthcdr 2 method)))
11385
11386 (defun gnus-close-server (method)
11387   (funcall (gnus-get-function method 'close-server) (nth 1 method)))
11388
11389 (defun gnus-request-list (method)
11390   (funcall (gnus-get-function method 'request-list) (nth 1 method)))
11391
11392 (defun gnus-request-list-newsgroups (method)
11393   (funcall (gnus-get-function method 'request-list-newsgroups) (nth 1 method)))
11394
11395 (defun gnus-request-newgroups (date method)
11396   (funcall (gnus-get-function method 'request-newgroups) 
11397            date (nth 1 method)))
11398
11399 (defun gnus-server-opened (method)
11400   (funcall (gnus-get-function method 'server-opened) (nth 1 method)))
11401
11402 (defun gnus-status-message (method)
11403   (let ((method (if (stringp method) (gnus-find-method-for-group method)
11404                   method)))
11405     (funcall (gnus-get-function method 'status-message) (nth 1 method))))
11406
11407 (defun gnus-request-group (group &optional dont-check)
11408   (let ((method (gnus-find-method-for-group group)))
11409 ;    (and t (message "%s GROUP %s" (car method) group))
11410     (funcall (gnus-get-function method 'request-group) 
11411              (gnus-group-real-name group) (nth 1 method) dont-check)))
11412
11413 (defun gnus-request-asynchronous (group &optional articles)
11414   (let ((method (gnus-find-method-for-group group)))
11415     (funcall (gnus-get-function method 'request-asynchronous) 
11416              (gnus-group-real-name group) (nth 1 method) articles)))
11417
11418 (defun gnus-list-active-group (group)
11419   (let ((method (gnus-find-method-for-group group))
11420         (func 'list-active-group))
11421     (and (gnus-check-backend-function func group)
11422          (funcall (gnus-get-function method func) 
11423                   (gnus-group-real-name group) (nth 1 method)))))
11424
11425 (defun gnus-request-group-description (group)
11426   (let ((method (gnus-find-method-for-group group))
11427         (func 'request-group-description))
11428     (and (gnus-check-backend-function func group)
11429          (funcall (gnus-get-function method func) 
11430                   (gnus-group-real-name group) (nth 1 method)))))
11431
11432 (defun gnus-close-group (group)
11433   (let ((method (gnus-find-method-for-group group)))
11434     (funcall (gnus-get-function method 'close-group) 
11435              (gnus-group-real-name group) (nth 1 method))))
11436
11437 (defun gnus-retrieve-headers (articles group)
11438   (let ((method (gnus-find-method-for-group group)))
11439     (if (and gnus-use-cache (numberp (car articles)))
11440         (gnus-cache-retrieve-headers articles group)
11441       (funcall (gnus-get-function method 'retrieve-headers) 
11442                articles (gnus-group-real-name group) (nth 1 method)))))
11443
11444 (defun gnus-retrieve-groups (groups method)
11445   (funcall (gnus-get-function method 'retrieve-groups) groups (nth 1 method)))
11446
11447 (defun gnus-request-article (article group &optional buffer)
11448   (let ((method (gnus-find-method-for-group group)))
11449     (funcall (gnus-get-function method 'request-article) 
11450              article (gnus-group-real-name group) (nth 1 method) buffer)))
11451
11452 (defun gnus-request-head (article group)
11453   (let ((method (gnus-find-method-for-group group)))
11454     (funcall (gnus-get-function method 'request-head) 
11455              article (gnus-group-real-name group) (nth 1 method))))
11456
11457 ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
11458 (defun gnus-request-post-buffer (post group subject header artbuf
11459                                       info follow-to respect-poster)
11460    (let* ((info (or info (and group (nth 2 (gnus-gethash 
11461                                             group gnus-newsrc-hashtb)))))
11462           (method
11463            (if (and gnus-post-method
11464                     ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
11465                     (memq 'post (assoc
11466                                  (format "%s" (car (gnus-find-method-for-group
11467                                                     gnus-newsgroup-name)))
11468                                         gnus-valid-select-methods)))
11469                gnus-post-method
11470              (gnus-find-method-for-group gnus-newsgroup-name))))
11471      (or (gnus-server-opened method)
11472          (gnus-open-server method)
11473          (error "Can't open server %s:%s" (car method) (nth 1 method)))
11474      (let ((mail-self-blind nil)
11475            (mail-archive-file-name nil))
11476        (funcall (gnus-get-function method 'request-post-buffer) 
11477                 post group subject header artbuf info follow-to
11478                 respect-poster))))
11479
11480 (defun gnus-request-post (method &optional force)
11481   (and (stringp method)
11482        (setq method (gnus-server-to-method method)))
11483   (and (not force) gnus-post-method
11484        (memq 'post (assoc (format "%s" (car method))
11485                           gnus-valid-select-methods))
11486        (setq method gnus-post-method))
11487   (funcall (gnus-get-function method 'request-post) 
11488            (nth 1 method)))
11489
11490 (defun gnus-request-expire-articles (articles group &optional force)
11491   (let ((method (gnus-find-method-for-group group)))
11492     (funcall (gnus-get-function method 'request-expire-articles) 
11493              articles (gnus-group-real-name group) (nth 1 method)
11494              force)))
11495
11496 (defun gnus-request-move-article 
11497   (article group server accept-function &optional last)
11498   (let ((method (gnus-find-method-for-group group)))
11499     (funcall (gnus-get-function method 'request-move-article) 
11500              article (gnus-group-real-name group) 
11501              (nth 1 method) accept-function last)))
11502
11503 (defun gnus-request-accept-article (group &optional last)
11504   (let ((func (if (symbolp group) group
11505                 (car (gnus-find-method-for-group group)))))
11506     (funcall (intern (format "%s-request-accept-article" func))
11507              (if (stringp group) (gnus-group-real-name group) group)
11508              last)))
11509
11510 (defun gnus-request-replace-article (article group buffer)
11511   (let ((func (car (gnus-find-method-for-group group))))
11512     (funcall (intern (format "%s-request-replace-article" func))
11513              article (gnus-group-real-name group) buffer)))
11514
11515 (defun gnus-request-create-group (group)
11516   (let ((method (gnus-find-method-for-group group)))
11517     (funcall (gnus-get-function method 'request-create-group) 
11518              (gnus-group-real-name group) (nth 1 method))))
11519
11520 (defun gnus-member-of-valid (symbol group)
11521   (memq symbol (assoc
11522                 (format "%s" (car (gnus-find-method-for-group group)))
11523                 gnus-valid-select-methods)))
11524
11525 (defsubst gnus-secondary-method-p (method)
11526   (member method gnus-secondary-select-methods))
11527
11528 (defun gnus-find-method-for-group (group &optional info)
11529   (or gnus-override-method
11530       (and (not group)
11531            gnus-select-method)
11532       (let ((info (or info (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
11533             method)
11534         (if (or (not info)
11535                 (not (setq method (nth 4 info))))
11536             (setq method gnus-select-method)
11537           (setq method
11538                 (cond ((stringp method)
11539                        (gnus-server-to-method method))
11540                       ((stringp (car method))
11541                        (gnus-server-extend-method group method))
11542                       (t
11543                        method))))
11544         (gnus-server-add-address method))))
11545
11546 (defun gnus-check-backend-function (func group)
11547   (let ((method (if (stringp group) (car (gnus-find-method-for-group group))
11548                  group)))
11549     (fboundp (intern (format "%s-%s" method func)))))
11550
11551 (defun gnus-methods-using (method)
11552   (let ((valids gnus-valid-select-methods)
11553         outs)
11554     (while valids
11555       (if (memq method (car valids)) 
11556           (setq outs (cons (car valids) outs)))
11557       (setq valids (cdr valids)))
11558     outs))
11559
11560 ;;; 
11561 ;;; Active & Newsrc File Handling
11562 ;;;
11563
11564 ;; Newsrc related functions.
11565 ;; Gnus internal format of gnus-newsrc-alist:
11566 ;; (("alt.general" 3 (1 . 1))
11567 ;;  ("alt.misc"    3 ((1 . 10) (12 . 15)))
11568 ;;  ("alt.test"    7 (1 . 99) (45 57 93)) ...)
11569 ;; The first item is the group name; the second is the subscription
11570 ;; level; the third is either a range of a list of ranges of read
11571 ;; articles, the optional fourth element is a list of marked articles,
11572 ;; the optional fifth element is the select method.
11573 ;;
11574 ;; Gnus internal format of gnus-newsrc-hashtb:
11575 ;; (95 ("alt.general" 3 (1 . 1)) ("alt.misc" 3 ((1 . 10) (12 . 15))) ...)
11576 ;; This is the entry for "alt.misc". The first element is the number
11577 ;; of unread articles in "alt.misc". The cdr of this entry is the
11578 ;; element *before* "alt.misc" in gnus-newsrc-alist, which makes is
11579 ;; trivial to remove or add new elements into gnus-newsrc-alist
11580 ;; without scanning the entire list. So, to get the actual information
11581 ;; of "alt.misc", you'd say something like 
11582 ;; (nth 2 (gnus-gethash "alt.misc" gnus-newsrc-hashtb))
11583 ;;
11584 ;; Gnus internal format of gnus-active-hashtb:
11585 ;; ((1 . 1))
11586 ;;  (5 . 10))
11587 ;;  (67 . 99)) ...)
11588 ;; The only element in each entry in this hash table is a range of
11589 ;; (possibly) available articles. (Articles in this range may have
11590 ;; been expired or canceled.)
11591 ;;
11592 ;; Gnus internal format of gnus-killed-list and gnus-zombie-list:
11593 ;; ("alt.misc" "alt.test" "alt.general" ...)
11594
11595 (defun gnus-setup-news (&optional rawfile level)
11596   "Setup news information.
11597 If RAWFILE is non-nil, the .newsrc file will also be read.
11598 If LEVEL is non-nil, the news will be set up at level LEVEL."
11599   (let ((init (not (and gnus-newsrc-alist gnus-active-hashtb (not rawfile)))))
11600     ;; Clear some variables to re-initialize news information.
11601     (if init (setq gnus-newsrc-alist nil gnus-active-hashtb nil))
11602
11603     ;; Read the newsrc file and create `gnus-newsrc-hashtb'.
11604     (if init (gnus-read-newsrc-file rawfile))
11605
11606     ;; Read the active file and create `gnus-active-hashtb'.
11607     ;; If `gnus-read-active-file' is nil, then we just create an empty
11608     ;; hash table. The partial filling out of the hash table will be
11609     ;; done in `gnus-get-unread-articles'.
11610     (if (and gnus-read-active-file 
11611              (not level))
11612         (gnus-read-active-file)
11613       (setq gnus-active-hashtb (make-vector 4095 0)))
11614
11615     (and init gnus-use-dribble-file (gnus-dribble-read-file))
11616
11617     ;; Find the number of unread articles in each non-dead group.
11618     (gnus-get-unread-articles (or level (1+ gnus-level-subscribed)))
11619     ;; Find new newsgroups and treat them.
11620     (if (and init gnus-check-new-newsgroups gnus-read-active-file (not level)
11621              (gnus-server-opened gnus-select-method))
11622         (gnus-find-new-newsgroups))
11623     (if (and init gnus-check-bogus-newsgroups 
11624              gnus-read-active-file (not level)
11625              (gnus-server-opened gnus-select-method))
11626         (gnus-check-bogus-newsgroups))))
11627
11628 (defun gnus-find-new-newsgroups ()
11629   "Search for new newsgroups and add them.
11630 Each new newsgroup will be treated with `gnus-subscribe-newsgroup-method.'
11631 The `-n' option line from .newsrc is respected."
11632   (interactive)
11633   (or (gnus-check-first-time-used)
11634       (if (or (consp gnus-check-new-newsgroups)
11635               (eq gnus-check-new-newsgroups 'ask-server))
11636           (gnus-ask-server-for-new-groups)
11637         (let ((groups 0)
11638               group new-newsgroups)
11639           (or gnus-have-read-active-file (gnus-read-active-file))
11640           (setq gnus-newsrc-last-checked-date (current-time-string))
11641           (if (not gnus-killed-hashtb) (gnus-make-hashtable-from-killed))
11642           ;; Go though every newsgroup in `gnus-active-hashtb' and compare
11643           ;; with `gnus-newsrc-hashtb' and `gnus-killed-hashtb'.
11644           (mapatoms
11645            (lambda (sym)
11646              (setq group (symbol-name sym))
11647              (if (or (gnus-gethash group gnus-killed-hashtb)
11648                      (gnus-gethash group gnus-newsrc-hashtb))
11649                  ()
11650                (let ((do-sub (gnus-matches-options-n group)))
11651                  (cond ((eq do-sub 'subscribe)
11652                         (setq groups (1+ groups))
11653                         (gnus-sethash group group gnus-killed-hashtb)
11654                         (funcall 
11655                          gnus-subscribe-options-newsgroup-method group))
11656                        ((eq do-sub 'ignore)
11657                         nil)
11658                        (t
11659                         (setq groups (1+ groups))
11660                         (gnus-sethash group group gnus-killed-hashtb)
11661                         (if gnus-subscribe-hierarchical-interactive
11662                             (setq new-newsgroups (cons group new-newsgroups))
11663                           (funcall gnus-subscribe-newsgroup-method group)))))))
11664            gnus-active-hashtb)
11665           (if new-newsgroups 
11666               (gnus-subscribe-hierarchical-interactive new-newsgroups))
11667           ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
11668           (if (> groups 0)
11669               (gnus-message 6 "%d new newsgroup%s arrived." 
11670                             groups (if (> groups 1) "s have" " has")))))))
11671
11672 (defun gnus-matches-options-n (group)
11673   ;; Returns `subscribe' if the group is to be uncoditionally
11674   ;; subscribed, `ignore' if it is to be ignored, and nil if there is
11675   ;; no match for the group.
11676
11677   ;; First we check the two user variables.
11678   (cond
11679    ((and gnus-options-subscribe
11680          (string-match gnus-options-subscribe group))
11681     'subscribe)
11682    ((and gnus-options-not-subscribe
11683          (string-match gnus-options-not-subscribe group))
11684     'ignore)
11685    ;; Then we go through the list that was retrieved from the .newsrc
11686    ;; file.  This list has elements on the form 
11687    ;; `(REGEXP . {ignore,subscribe})'. The first match found (the list
11688    ;; is in the reverse order of the options line) is returned.
11689    (t
11690     (let ((regs gnus-newsrc-options-n))
11691       (while (and regs
11692                   (not (string-match (car (car regs)) group)))
11693         (setq regs (cdr regs)))
11694       (and regs (cdr (car regs)))))))
11695
11696 (defun gnus-ask-server-for-new-groups ()
11697   (let* ((date (or gnus-newsrc-last-checked-date (current-time-string)))
11698          (methods (cons gnus-select-method 
11699                         (append
11700                          (and (consp gnus-check-new-newsgroups)
11701                               gnus-check-new-newsgroups)
11702                          gnus-secondary-select-methods)))
11703          (groups 0)
11704          (new-date (current-time-string))
11705          hashtb group new-newsgroups got-new)
11706     ;; Go thorugh both primary and secondary select methods and
11707     ;; request new newsgroups.  
11708     (while methods
11709       (and (or (gnus-server-opened (car methods))
11710                (gnus-open-server (car methods)))
11711            (gnus-request-newgroups date (car methods))
11712            (save-excursion
11713              (setq got-new t)
11714              (set-buffer nntp-server-buffer)
11715              (or hashtb (setq hashtb (gnus-make-hashtable 
11716                                       (count-lines (point-min) (point-max)))))
11717              ;; Enter all the new groups in a hashtable.
11718              (gnus-active-to-gnus-format (car methods) hashtb 'ignore)))
11719       (setq methods (cdr methods)))
11720     (and got-new (setq gnus-newsrc-last-checked-date new-date))
11721     ;; Now all new groups from all select methods are in `hashtb'.
11722     (mapatoms
11723      (lambda (group-sym)
11724        (setq group (symbol-name group-sym))
11725        (if (or (gnus-gethash group gnus-newsrc-hashtb)
11726                (member group gnus-zombie-list)
11727                (member group gnus-killed-list))
11728            ;; The group is already known.
11729            ()
11730          (and (symbol-value group-sym)
11731               (gnus-sethash group (symbol-value group-sym) gnus-active-hashtb))
11732          (let ((do-sub (gnus-matches-options-n group)))
11733            (cond ((eq do-sub 'subscribe)
11734                   (setq groups (1+ groups))
11735                   (gnus-sethash group group gnus-killed-hashtb)
11736                   (funcall 
11737                    gnus-subscribe-options-newsgroup-method group))
11738                  ((eq do-sub 'ignore)
11739                   nil)
11740                  (t
11741                   (setq groups (1+ groups))
11742                   (gnus-sethash group group gnus-killed-hashtb)
11743                   (if gnus-subscribe-hierarchical-interactive
11744                       (setq new-newsgroups (cons group new-newsgroups))
11745                     (funcall gnus-subscribe-newsgroup-method group)))))))
11746      hashtb)
11747     (if new-newsgroups 
11748         (gnus-subscribe-hierarchical-interactive new-newsgroups))
11749     ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
11750     (if (> groups 0)
11751         (gnus-message 6 "%d new newsgroup%s arrived." 
11752                       groups (if (> groups 1) "s have" " has")))
11753     got-new))
11754
11755 (defun gnus-check-first-time-used ()
11756   (if (or (> (length gnus-newsrc-alist) 1)
11757           (file-exists-p gnus-startup-file)
11758           (file-exists-p (concat gnus-startup-file ".el"))
11759           (file-exists-p (concat gnus-startup-file ".eld")))
11760       nil
11761     (gnus-message 6 "First time user; subscribing you to default groups")
11762     (or gnus-have-read-active-file (gnus-read-active-file))
11763     (setq gnus-newsrc-last-checked-date (current-time-string))
11764     (let ((groups gnus-default-subscribed-newsgroups)
11765           group)
11766       (if (eq groups t)
11767           nil
11768         (setq groups (or groups gnus-backup-default-subscribed-newsgroups))
11769         (mapatoms
11770          (lambda (sym)
11771            (setq group (symbol-name sym))
11772            (let ((do-sub (gnus-matches-options-n group)))
11773              (cond ((eq do-sub 'subscribe)
11774                     (gnus-sethash group group gnus-killed-hashtb)
11775                     (funcall 
11776                      gnus-subscribe-options-newsgroup-method group))
11777                    ((eq do-sub 'ignore)
11778                     nil)
11779                    (t
11780                     (setq gnus-killed-list (cons group gnus-killed-list))))))
11781          gnus-active-hashtb)
11782         (while groups
11783           (if (gnus-gethash (car groups) gnus-active-hashtb)
11784               (gnus-group-change-level 
11785                (car groups) gnus-level-default-subscribed gnus-level-killed))
11786           (setq groups (cdr groups)))
11787         (gnus-group-make-help-group)
11788         (and gnus-novice-user
11789              (gnus-message 7 "`A k' to list killed groups"))))))
11790
11791 (defun gnus-subscribe-group (group previous &optional method)
11792   (gnus-group-change-level 
11793    (if method
11794        (list t group gnus-level-default-subscribed nil nil method)
11795      group) 
11796    gnus-level-default-subscribed gnus-level-killed previous t))
11797
11798 ;; `gnus-group-change-level' is the fundamental function for changing
11799 ;; subscription levels of newsgroups. This might mean just changing
11800 ;; from level 1 to 2, which is pretty trivial, from 2 to 6 or back
11801 ;; again, which subscribes/unsubscribes a group, which is equally
11802 ;; trivial. Changing from 1-7 to 8-9 means that you kill a group, and
11803 ;; from 8-9 to 1-7 means that you remove the group from the list of
11804 ;; killed (or zombie) groups and add them to the (kinda) subscribed
11805 ;; groups. And last but not least, moving from 8 to 9 and 9 to 8,
11806 ;; which is trivial.
11807 ;; ENTRY can either be a string (newsgroup name) or a list (if
11808 ;; FROMKILLED is t, it's a list on the format (NUM INFO-LIST),
11809 ;; otherwise it's a list in the format of the `gnus-newsrc-hashtb'
11810 ;; entries. 
11811 ;; LEVEL is the new level of the group, OLDLEVEL is the old level and
11812 ;; PREVIOUS is the group (in hashtb entry format) to insert this group
11813 ;; after. 
11814 (defun gnus-group-change-level (entry level &optional oldlevel
11815                                       previous fromkilled)
11816   (let (group info active num)
11817     ;; Glean what info we can from the arguments
11818     (if (consp entry)
11819         (if fromkilled (setq group (nth 1 entry))
11820           (setq group (car (nth 2 entry))))
11821       (setq group entry))
11822     (if (and (stringp entry)
11823              oldlevel 
11824              (< oldlevel gnus-level-zombie))
11825         (setq entry (gnus-gethash entry gnus-newsrc-hashtb)))
11826     (if (and (not oldlevel)
11827              (consp entry))
11828         (setq oldlevel (car (cdr (nth 2 entry)))))
11829     (if (stringp previous)
11830         (setq previous (gnus-gethash previous gnus-newsrc-hashtb)))
11831
11832     (gnus-dribble-enter
11833      (format "(gnus-group-change-level %S %S %S %S %S)" 
11834              group level oldlevel (car (nth 2 previous)) fromkilled))
11835     
11836     ;; Then we remove the newgroup from any old structures, if needed.
11837     ;; If the group was killed, we remove it from the killed or zombie
11838     ;; list. If not, and it is in fact going to be killed, we remove
11839     ;; it from the newsrc hash table and assoc.
11840     (cond ((>= oldlevel gnus-level-zombie)
11841            (if (= oldlevel gnus-level-zombie)
11842                (setq gnus-zombie-list (delete group gnus-zombie-list))
11843              (setq gnus-killed-list (delete group gnus-killed-list))))
11844           (t
11845            (if (and (>= level gnus-level-zombie)
11846                     entry)
11847                (progn
11848                  (gnus-sethash (car (nth 2 entry)) nil gnus-newsrc-hashtb)
11849                  (if (nth 3 entry)
11850                      (setcdr (gnus-gethash (car (nth 3 entry))
11851                                            gnus-newsrc-hashtb)
11852                              (cdr entry)))
11853                  (setcdr (cdr entry) (cdr (cdr (cdr entry))))))))
11854
11855     ;; Finally we enter (if needed) the list where it is supposed to
11856     ;; go, and change the subscription level. If it is to be killed,
11857     ;; we enter it into the killed or zombie list.
11858     (cond ((>= level gnus-level-zombie)
11859            ;; Remove from the hash table.
11860            (gnus-sethash group nil gnus-newsrc-hashtb)
11861            (or (gnus-group-foreign-p group)
11862                ;; We do not enter foreign groups into the list of dead
11863                ;; groups.  
11864                (if (= level gnus-level-zombie)
11865                    (setq gnus-zombie-list (cons group gnus-zombie-list))
11866                  (setq gnus-killed-list (cons group gnus-killed-list)))))
11867           (t
11868            ;; If the list is to be entered into the newsrc assoc, and
11869            ;; it was killed, we have to create an entry in the newsrc
11870            ;; hashtb format and fix the pointers in the newsrc assoc.
11871            (if (>= oldlevel gnus-level-zombie)
11872                (progn
11873                  (if (listp entry)
11874                      (progn
11875                        (setq info (cdr entry))
11876                        (setq num (car entry)))
11877                    (setq active (gnus-gethash group gnus-active-hashtb))
11878                    (setq num (if active (- (1+ (cdr active)) (car active)) t))
11879                    ;; Check whether the group is foreign. If so, the
11880                    ;; foreign select method has to be entered into the
11881                    ;; info. 
11882                    (let ((method (gnus-group-method-name group)))
11883                      (if (eq method gnus-select-method)
11884                          (setq info (list group level nil))
11885                        (setq info (list group level nil nil method)))))
11886                  (or previous 
11887                      (setq previous 
11888                            (let ((p gnus-newsrc-alist))
11889                              (while (cdr (cdr p))
11890                                (setq p (cdr p)))
11891                              p)))
11892                  (setq entry (cons info (cdr (cdr previous))))
11893                  (if (cdr previous)
11894                      (progn
11895                        (setcdr (cdr previous) entry)
11896                        (gnus-sethash group (cons num (cdr previous)) 
11897                                      gnus-newsrc-hashtb))
11898                    (setcdr previous entry)
11899                    (gnus-sethash group (cons num previous)
11900                                  gnus-newsrc-hashtb))
11901                  (if (cdr entry)
11902                      (setcdr (gnus-gethash (car (car (cdr entry)))
11903                                            gnus-newsrc-hashtb)
11904                              entry)))
11905              ;; It was alive, and it is going to stay alive, so we
11906              ;; just change the level and don't change any pointers or
11907              ;; hash table entries.
11908              (setcar (cdr (car (cdr (cdr entry)))) level))))))
11909
11910 (defun gnus-kill-newsgroup (newsgroup)
11911   "Obsolete function. Kills a newsgroup."
11912   (gnus-group-change-level
11913    (gnus-gethash newsgroup gnus-newsrc-hashtb) gnus-level-killed))
11914
11915 (defun gnus-check-bogus-newsgroups (&optional confirm)
11916   "Remove bogus newsgroups.
11917 If CONFIRM is non-nil, the user has to confirm the deletion of every
11918 newsgroup." 
11919   (let ((newsrc (cdr gnus-newsrc-alist))
11920         bogus group entry)
11921     (gnus-message 5 "Checking bogus newsgroups...")
11922     (or gnus-have-read-active-file (gnus-read-active-file))
11923     ;; Find all bogus newsgroup that are subscribed.
11924     (while newsrc
11925       (setq group (car (car newsrc)))
11926       (if (or (gnus-gethash group gnus-active-hashtb) ; Active
11927               (nth 4 (car newsrc))      ; Foreign
11928               (and confirm
11929                    (not (gnus-y-or-n-p
11930                          (format "Remove bogus newsgroup: %s " group)))))
11931           ;; Don't remove.
11932           ()
11933         ;; Found a bogus newsgroup.
11934         (setq bogus (cons group bogus)))
11935       (setq newsrc (cdr newsrc)))
11936     ;; Remove all bogus subscribed groups by first killing them, and
11937     ;; then removing them from the list of killed groups.
11938     (while bogus
11939       (and (setq entry (gnus-gethash (car bogus) gnus-newsrc-hashtb))
11940            (progn
11941              (gnus-group-change-level entry gnus-level-killed)
11942              (setq gnus-killed-list (delete (car bogus) gnus-killed-list))))
11943       (setq bogus (cdr bogus)))
11944     ;; Then we remove all bogus groups from the list of killed and
11945     ;; zombie groups. They are are removed without confirmation.
11946     (let ((dead-lists '(gnus-killed-list gnus-zombie-list))
11947           killed)
11948       (while dead-lists
11949         (setq killed (symbol-value (car dead-lists)))
11950         (while killed
11951           (setq group (car killed))
11952           (or (gnus-gethash group gnus-active-hashtb)
11953               ;; The group is bogus.
11954               (set (car dead-lists)
11955                    (delete group (symbol-value (car dead-lists)))))
11956           (setq killed (cdr killed)))
11957         (setq dead-lists (cdr dead-lists))))
11958     (gnus-message 5 "Checking bogus newsgroups...done")))
11959
11960 (defun gnus-check-duplicate-killed-groups ()
11961   "Remove duplicates from the list of killed groups."
11962   (interactive)
11963   (let ((killed gnus-killed-list))
11964     (while killed
11965       (gnus-message 9 "%d" (length killed))
11966       (setcdr killed (delete (car killed) (cdr killed)))
11967       (setq killed (cdr killed)))))
11968
11969 ;; Go though `gnus-newsrc-alist' and compare with `gnus-active-hashtb'
11970 ;; and compute how many unread articles there are in each group.
11971 (defun gnus-get-unread-articles (&optional level) 
11972   (let* ((newsrc (cdr gnus-newsrc-alist))
11973          (level (or level (1+ gnus-level-subscribed)))
11974          (foreign-level
11975           (min 
11976            (cond ((and gnus-activate-foreign-newsgroups 
11977                        (not (numberp gnus-activate-foreign-newsgroups)))
11978                   (1+ gnus-level-subscribed))
11979                  ((numberp gnus-activate-foreign-newsgroups)
11980                   gnus-activate-foreign-newsgroups)
11981                  (t 0))
11982            level))
11983          info group active virtuals method)
11984     (gnus-message 5 "Checking new news...")
11985
11986     (while newsrc
11987       (setq info (car newsrc)
11988             group (car info)
11989             active (gnus-gethash group gnus-active-hashtb))
11990
11991       ;; Check newsgroups. If the user doesn't want to check them, or
11992       ;; they can't be checked (for instance, if the news server can't
11993       ;; be reached) we just set the number of unread articles in this
11994       ;; newsgroup to t. This means that Gnus thinks that there are
11995       ;; unread articles, but it has no idea how many.
11996       (if (and (setq method (nth 4 info))
11997                (not (gnus-server-equal gnus-select-method
11998                                        (gnus-server-get-method nil method)))
11999                (not (gnus-secondary-method-p method)))
12000           ;; These groups are foreign. Check the level.
12001           (if (<= (nth 1 info) foreign-level)
12002               (if (eq (car (if (stringp method) 
12003                                (gnus-server-to-method method)
12004                              (nth 4 info))) 'nnvirtual)
12005                   ;; We have to activate the virtual groups after all
12006                   ;; the others, so we just pop them on a list for
12007                   ;; now. 
12008                   (setq virtuals (cons info virtuals))
12009                 (and (setq active (gnus-activate-newsgroup (car info)))
12010                      ;; Close the groups as we look at them!
12011                      (gnus-close-group group))))
12012                 
12013         ;; These groups are native or secondary. 
12014         (if (and (not gnus-have-read-active-file)
12015                  (<= (nth 1 info) level))
12016             (setq active (gnus-activate-newsgroup (car info)))))
12017       
12018       (if active
12019           (gnus-get-unread-articles-in-group info active)
12020         ;; The group couldn't be reached, so we nix out the number of
12021         ;; unread articles and stuff.
12022         (gnus-sethash group nil gnus-active-hashtb)
12023         (setcar (gnus-gethash group gnus-newsrc-hashtb) t))
12024
12025       (setq newsrc (cdr newsrc)))
12026
12027     ;; Activate the virtual groups. This has to be done after all the
12028     ;; other groups. 
12029     ;; !!! If one virtual group contains another virtual group, even
12030     ;; doing it this way might cause problems.
12031    (while virtuals
12032       (and (setq active (gnus-activate-newsgroup (car (car virtuals))))
12033            (gnus-get-unread-articles-in-group (car virtuals) active))
12034       (setq virtuals (cdr virtuals)))
12035
12036     (gnus-message 5 "Checking new news...done")))
12037
12038 ;; Create a hash table out of the newsrc alist. The `car's of the
12039 ;; alist elements are used as keys.
12040 (defun gnus-make-hashtable-from-newsrc-alist ()
12041   (let ((alist gnus-newsrc-alist)
12042         (ohashtb gnus-newsrc-hashtb)
12043         prev)
12044     (setq gnus-newsrc-hashtb (gnus-make-hashtable (length alist)))
12045     (setq alist 
12046           (setq prev (setq gnus-newsrc-alist 
12047                            (if (equal (car (car gnus-newsrc-alist))
12048                                       "dummy.group")
12049                                gnus-newsrc-alist
12050                              (cons (list "dummy.group" 0 nil) alist)))))
12051     (while alist
12052       (gnus-sethash (car (car alist)) 
12053                     (cons (and ohashtb (car (gnus-gethash 
12054                                              (car (car alist)) ohashtb))) 
12055                           prev) gnus-newsrc-hashtb)
12056       (setq prev alist
12057             alist (cdr alist)))))
12058
12059 (defun gnus-make-hashtable-from-killed ()
12060   "Create a hash table from the killed and zombie lists."
12061   (let ((lists '(gnus-killed-list gnus-zombie-list))
12062         list)
12063     (setq gnus-killed-hashtb 
12064           (gnus-make-hashtable 
12065            (+ (length gnus-killed-list) (length gnus-zombie-list))))
12066     (while lists
12067       (setq list (symbol-value (car lists)))
12068       (setq lists (cdr lists))
12069       (while list
12070         (gnus-sethash (car list) (car list) gnus-killed-hashtb)
12071         (setq list (cdr list))))))
12072
12073 (defun gnus-get-unread-articles-in-group (info active)
12074   (let* ((range (nth 2 info))
12075          (num 0)
12076          (marked (nth 3 info)))
12077     ;; If a cache is present, we may have to alter the active info.
12078     (and gnus-use-cache
12079          (gnus-cache-possibly-alter-active (car info) active))
12080     ;; Modify the list of read articles according to what articles 
12081     ;; are available; then tally the unread articles and add the
12082     ;; number to the group hash table entry.
12083     (cond ((zerop (cdr active))
12084            (setq num 0))
12085           ((not range)
12086            (setq num (- (1+ (cdr active)) (car active))))
12087           ((not (listp (cdr range)))
12088            ;; Fix a single (num . num) range according to the
12089            ;; active hash table.
12090            ;; Fix by Carsten Bormann <cabo@Informatik.Uni-Bremen.DE>.
12091            (and (< (cdr range) (car active)) (setcdr range (1- (car active))))
12092            (and (> (cdr range) (cdr active)) (setcdr range (cdr active)))
12093            ;; Compute number of unread articles.
12094            (setq num (max 0 (- (cdr active) 
12095                                (- (1+ (cdr range)) (car range))))))
12096           (t
12097            ;; The read list is a list of ranges. Fix them according to
12098            ;; the active hash table.
12099            ;; First peel off any elements that are below the lower
12100            ;; active limit. 
12101            (while (and (cdr range) 
12102                        (>= (car active) 
12103                            (or (and (atom (car (cdr range))) (car (cdr range)))
12104                                (car (car (cdr range))))))
12105              (if (numberp (car range))
12106                  (setcar range 
12107                          (cons (car range) 
12108                                (or (and (numberp (car (cdr range)))
12109                                         (car (cdr range))) 
12110                                    (cdr (car (cdr range))))))
12111                (setcdr (car range) 
12112                        (or (and (numberp (nth 1 range)) (nth 1 range))
12113                            (cdr (car (cdr range))))))
12114              (setcdr range (cdr (cdr range))))
12115            ;; Adjust the first element to be the same as the lower limit. 
12116            (if (and (not (atom (car range))) 
12117                     (< (cdr (car range)) (car active)))
12118                (setcdr (car range) (1- (car active))))
12119            ;; Then we want to peel off any elements that are higher
12120            ;; than the upper active limit.  
12121            (let ((srange range))
12122              ;; Go past all legal elements.
12123              (while (and (cdr srange) 
12124                          (<= (or (and (atom (car (cdr srange)))
12125                                       (car (cdr srange)))
12126                                  (car (car (cdr srange)))) (cdr active)))
12127                (setq srange (cdr srange)))
12128              (if (cdr srange)
12129                  ;; Nuke all remaining illegal elements.
12130                  (setcdr srange nil))
12131
12132              ;; Adjust the final element.
12133              (if (and (not (atom (car srange)))
12134                       (> (cdr (car srange)) (cdr active)))
12135                  (setcdr (car srange) (cdr active))))
12136            ;; Compute the number of unread articles.
12137            (while range
12138              (setq num (+ num (- (1+ (or (and (atom (car range)) (car range))
12139                                          (cdr (car range))))
12140                                  (or (and (atom (car range)) (car range))
12141                                      (car (car range))))))
12142              (setq range (cdr range)))
12143            (setq num (max 0 (- (cdr active) num)))))
12144     (and info
12145          (progn
12146            (and (assq 'tick marked)
12147                 (inline (gnus-remove-illegal-marked-articles
12148                          (assq 'tick marked) (nth 2 info))))
12149            (and (assq 'dormant marked)
12150                 (inline (gnus-remove-illegal-marked-articles
12151                          (assq 'dormant marked) (nth 2 info))))
12152            (setcar
12153             (gnus-gethash (car info) gnus-newsrc-hashtb) 
12154             (setq num (max 0 (- num (length (cdr (assq 'tick marked)))
12155                                 (length (cdr (assq 'dormant marked)))))))))
12156     num))
12157
12158 (defun gnus-remove-illegal-marked-articles (marked ranges)
12159   (let ((m (cdr marked)))
12160     ;; Make sure that all ticked articles are a subset of the unread
12161     ;; articles. 
12162     (while m
12163       (if (gnus-member-of-range (car m) ranges)
12164           (setcdr marked (cdr m))
12165         (setq marked m))
12166       (setq m (cdr m)))))
12167
12168 (defun gnus-activate-newsgroup (group)
12169   (let ((method (gnus-find-method-for-group group))
12170         active)
12171     (and (or (gnus-server-opened method) (gnus-open-server method))
12172          (gnus-request-group group)
12173          (save-excursion
12174            (set-buffer nntp-server-buffer)
12175            (goto-char (point-min))
12176            (and (looking-at "[0-9]+ [0-9]+ \\([0-9]+\\) [0-9]+")
12177                 (progn
12178                   (goto-char (match-beginning 1))
12179                   (gnus-sethash 
12180                    group (setq active (cons (read (current-buffer))
12181                                             (read (current-buffer))))
12182                    gnus-active-hashtb))
12183                 active)))))
12184
12185 (defun gnus-update-read-articles 
12186   (group unread unselected ticked &optional domarks replied expirable killed
12187          dormant bookmark score)
12188   "Update the list of read and ticked articles in GROUP using the
12189 UNREAD and TICKED lists.
12190 Note: UNSELECTED has to be sorted over `<'.
12191 Returns whether the updating was successful."
12192   (let* ((active (or gnus-newsgroup-active 
12193                      (gnus-gethash group gnus-active-hashtb)))
12194          (entry (gnus-gethash group gnus-newsrc-hashtb))
12195          (info (nth 2 entry))
12196          (marked (nth 3 info))
12197          (prev 1)
12198          (unread (sort (copy-sequence unread) (function <)))
12199          read)
12200     (if (or (not info) (not active))
12201         ;; There is no info on this group if it was, in fact,
12202         ;; killed. Gnus stores no information on killed groups, so
12203         ;; there's nothing to be done. 
12204         ;; One could store the information somewhere temporarily,
12205         ;; perhaps... Hmmm... 
12206         ()
12207       ;; Remove any negative articles numbers.
12208       (while (and unread (< (car unread) 0))
12209         (setq unread (cdr unread)))
12210       ;; Remove any expired article numbers
12211       (while (and unread (< (car unread) (car active)))
12212         (setq unread (cdr unread)))
12213       (while (and ticked (< (car ticked) (car active)))
12214         (setq ticked (cdr ticked)))
12215       (while (and dormant (< (car dormant) (car active)))
12216         (setq dormant (cdr dormant)))
12217       (setq unread (sort (append unselected unread) '<))
12218       ;; Set the number of unread articles in gnus-newsrc-hashtb.
12219       (setcar entry (max 0 (- (length unread) (length ticked) 
12220                               (length dormant))))
12221       ;; Compute the ranges of read articles by looking at the list of
12222       ;; unread articles.  
12223       (while unread
12224         (if (/= (car unread) prev)
12225             (setq read (cons (if (= prev (1- (car unread))) prev
12226                                (cons prev (1- (car unread)))) read)))
12227         (setq prev (1+ (car unread)))
12228         (setq unread (cdr unread)))
12229       (if (<= prev (cdr active))
12230           (setq read (cons (cons prev (cdr active)) read)))
12231       ;; Enter this list into the group info.
12232       (setcar (cdr (cdr info)) 
12233               (if (> (length read) 1) (nreverse read) read))
12234       ;; Enter the list of ticked articles.
12235       (gnus-set-marked-articles 
12236        info ticked
12237        (if domarks replied (cdr (assq 'reply marked)))
12238        (if domarks expirable (cdr (assq 'expire marked)))
12239        (if domarks killed (cdr (assq 'killed marked)))
12240        (if domarks dormant (cdr (assq 'dormant marked)))
12241        (if domarks bookmark (cdr (assq 'bookmark marked)))
12242        (if domarks score (cdr (assq 'score marked))))
12243       t)))
12244
12245 (defun gnus-make-articles-unread (group articles)
12246   "Mark ARTICLES in GROUP as unread."
12247   (let* ((info (nth 2 (or (gnus-gethash group gnus-newsrc-hashtb)
12248                           (gnus-gethash (gnus-group-real-name group)
12249                                         gnus-newsrc-hashtb))))
12250          (ranges (nth 2 info))
12251          news)
12252     (while articles
12253       (and (gnus-member-of-range (car articles) ranges)
12254            (setq news (cons (car articles) news)))
12255       (setq articles (cdr articles)))
12256     (if (not news)
12257         ()
12258       (setcar (nthcdr 2 info)
12259               (gnus-remove-from-range (nth 2 info) (nreverse news)))
12260       (gnus-group-update-group group t))))
12261
12262 ;; Get the active file(s) from the backend(s).
12263 (defun gnus-read-active-file ()
12264   (gnus-group-set-mode-line)
12265   (let ((methods (if (or (gnus-server-opened gnus-select-method)
12266                          (gnus-open-server gnus-select-method))
12267                      ;; The native server is available.
12268                      (cons gnus-select-method gnus-secondary-select-methods)
12269                    ;; The native server is down, so we just do the
12270                    ;; secondary ones.   
12271                    gnus-secondary-select-methods))
12272         list-type)
12273     (setq gnus-have-read-active-file nil)
12274     (save-excursion
12275       (set-buffer nntp-server-buffer)
12276       (while methods
12277         (let* ((where (nth 1 (car methods)))
12278                (mesg (format "Reading active file%s via %s..."
12279                              (if (and where (not (zerop (length where))))
12280                                  (concat " from " where) "")
12281                              (car (car methods)))))
12282           (gnus-message 5 mesg)
12283           (gnus-check-news-server (car methods))
12284           (cond 
12285            ((and (eq gnus-read-active-file 'some)
12286                  (gnus-check-backend-function
12287                   'retrieve-groups (car (car methods))))
12288             (let ((newsrc (cdr gnus-newsrc-alist))
12289                   groups)
12290               (while newsrc
12291                 (and (gnus-server-equal 
12292                       (gnus-find-method-for-group
12293                        (car (car newsrc)) (car newsrc))
12294                       (gnus-server-get-method nil (car methods)))
12295                      (setq groups (cons (car (car newsrc)) groups)))
12296                 (setq newsrc (cdr newsrc)))
12297               (setq list-type (gnus-retrieve-groups groups (car methods)))
12298               (cond ((not list-type)
12299                      (gnus-message 
12300                       1 "Cannot read partial active file from %s server." 
12301                       (car (car methods)))
12302                      (ding)
12303                      (sit-for 2))
12304                     ((eq list-type 'active)
12305                      (gnus-active-to-gnus-format (car methods)))
12306                     (t
12307                      (gnus-groups-to-gnus-format (car methods))))))
12308            (t
12309             (if (not (gnus-request-list (car methods)))
12310                 (progn
12311                   (gnus-message 1 "Cannot read active file from %s server." 
12312                                 (car (car methods)))
12313                   (ding))
12314               (gnus-active-to-gnus-format (car methods))
12315               (setq gnus-have-read-active-file t)
12316               (gnus-message 5 "%sdone" mesg)))))
12317         (setq methods (cdr methods))))))
12318
12319 ;; Read an active file and place the results in `gnus-active-hashtb'.
12320 (defun gnus-active-to-gnus-format (method &optional hashtb ignore-errors)
12321   (let ((cur (current-buffer))
12322         (hashtb (or hashtb 
12323                     (if (and gnus-active-hashtb 
12324                              (not (equal method gnus-select-method)))
12325                         gnus-active-hashtb
12326                       (setq gnus-active-hashtb
12327                             (if (equal method gnus-select-method)
12328                                 (gnus-make-hashtable 
12329                                  (count-lines (point-min) (point-max)))
12330                               (gnus-make-hashtable 4096)))))))
12331     ;; Delete unnecessary lines.
12332     (goto-char (point-min))
12333     (while (search-forward "\nto." nil t)
12334       (delete-region (1+ (match-beginning 0)) 
12335                      (progn (forward-line 1) (point))))
12336     (or (string= gnus-ignored-newsgroups "")
12337         (progn
12338           (goto-char (point-min))
12339           (delete-matching-lines gnus-ignored-newsgroups)))
12340     ;; If these are groups from a foreign select method, we insert the
12341     ;; group prefix in front of the group names. 
12342     (and method (not (eq method gnus-select-method))
12343          (let ((prefix (gnus-group-prefixed-name "" method)))
12344            (goto-char (point-min))
12345            (while (and (not (eobp))
12346                        (progn (insert prefix)
12347                               (zerop (forward-line 1)))))))
12348     (goto-char (point-min))
12349     ;; Store active file in hashtable.
12350     (goto-char (point-min))
12351     (if (string-match "%[oO]" gnus-group-line-format)
12352         ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
12353         ;; If we want information on moderated groups, we use this
12354         ;; loop...   
12355         (let* ((mod-hashtb (make-vector 7 0))
12356                (m (intern "m" mod-hashtb))
12357                group max min)
12358           (while (not (eobp))
12359             (condition-case nil
12360                 (progn
12361                   (narrow-to-region (point) (gnus-point-at-eol))
12362                   (setq group (let ((obarray hashtb)) (read cur)))
12363                   (if (and (numberp (setq max (read cur)))
12364                            (numberp (setq min (read cur))))
12365                       (set group (cons min max))
12366                     (set group nil))
12367                   ;; Enter moderated groups into a list.
12368                   (if (eq (let ((obarray mod-hashtb)) (read cur)) m)
12369                       (setq gnus-moderated-list 
12370                             (cons (symbol-name group) gnus-moderated-list))))
12371               (error nil))
12372             (widen)
12373             (forward-line 1)))
12374       ;; And if we do not care about moderation, we use this loop,
12375       ;; which is faster.
12376       (let (group max min)
12377         (while (not (eobp))
12378           (condition-case ()
12379               (progn
12380                 (narrow-to-region (point) (gnus-point-at-eol))
12381                 ;; group gets set to a symbol interned in the hash table
12382                 ;; (what a hack!!)
12383                 (setq group (let ((obarray hashtb)) (read cur)))
12384                 (if (and (numberp (setq max (read cur)))
12385                          (numberp (setq min (read cur))))
12386                     (set group (cons min max))
12387                   (set group nil)))
12388             (error 
12389              (progn 
12390                (if ignore-errors
12391                    (set group nil)
12392                  (ding) 
12393                  (gnus-message 3 "Warning - illegal active: %s"
12394                                (buffer-substring 
12395                                 (gnus-point-at-bol) (gnus-point-at-eol)))
12396                  nil))))
12397           (widen)
12398           (forward-line 1))))))
12399
12400 (defun gnus-groups-to-gnus-format (method &optional hashtb)
12401   ;; Parse a "groups" active file.
12402   (let ((cur (current-buffer))
12403         (hashtb (or hashtb 
12404                     (if method
12405                         gnus-active-hashtb
12406                       (setq gnus-active-hashtb
12407                             (gnus-make-hashtable 
12408                              (count-lines (point-min) (point-max)))))))
12409         (prefix (and method (not (eq method gnus-select-method))
12410                      (gnus-group-prefixed-name "" method))))
12411
12412     (goto-char (point-min))
12413     (condition-case ()
12414         ;; We split this into to separate loops, one with the prefix
12415         ;; and one without to speed the reading up somewhat.
12416         (if prefix
12417             (let (min max opoint)
12418               (while (not (eobp))
12419                 (read cur) (read cur)
12420                 (setq min (read cur)
12421                       max (read cur)
12422                       opoint (point))
12423                 (skip-chars-forward " \t")
12424                 (insert prefix)
12425                 (goto-char opoint)
12426                 (set (let ((obarray hashtb)) (read cur)) 
12427                      (cons min max))
12428                 (forward-line 1)))
12429           (let (min max)
12430             (while (not (eobp))
12431               (if (= (following-char) ?2)
12432                   (progn
12433                     (read cur) (read cur)
12434                     (setq min (read cur)
12435                           max (read cur))
12436                     (set (let ((obarray hashtb)) (read cur)) 
12437                          (cons min max))))
12438               (forward-line 1))))
12439       (error 
12440        (progn (ding) (gnus-message 3 "Possible error in active file."))))))
12441
12442 (defun gnus-read-newsrc-file (&optional force)
12443   "Read startup file.
12444 If FORCE is non-nil, the .newsrc file is read."
12445   (setq gnus-current-startup-file (gnus-make-newsrc-file gnus-startup-file))
12446   ;; Reset variables that might be defined in the .newsrc.eld file.
12447   (let ((variables gnus-variable-list))
12448     (while variables
12449       (set (car variables) nil)
12450       (setq variables (cdr variables))))
12451   (let* ((newsrc-file gnus-current-startup-file)
12452          (quick-file (concat newsrc-file ".el")))
12453     (save-excursion
12454       ;; We always load the .newsrc.eld file. If always contains
12455       ;; much information that can not be gotten from the .newsrc
12456       ;; file (ticked articles, killed groups, foreign methods, etc.)
12457       (gnus-read-newsrc-el-file quick-file)
12458  
12459       (if (or force
12460               (and (file-newer-than-file-p newsrc-file quick-file)
12461                    (file-newer-than-file-p newsrc-file 
12462                                            (concat quick-file "d")))
12463               (not gnus-newsrc-alist))
12464           ;; We read the .newsrc file. Note that if there if a
12465           ;; .newsrc.eld file exists, it has already been read, and
12466           ;; the `gnus-newsrc-hashtb' has been created. While reading
12467           ;; the .newsrc file, Gnus will only use the information it
12468           ;; can find there for changing the data already read -
12469           ;; ie. reading the .newsrc file will not trash the data
12470           ;; already read (except for read articles).
12471           (save-excursion
12472             (gnus-message 5 "Reading %s..." newsrc-file)
12473             (set-buffer (find-file-noselect newsrc-file))
12474             (buffer-disable-undo (current-buffer))
12475             (gnus-newsrc-to-gnus-format)
12476             (kill-buffer (current-buffer))
12477             (gnus-message 5 "Reading %s...done" newsrc-file))))))
12478
12479 (defun gnus-read-newsrc-el-file (file)
12480   (let ((ding-file (concat file "d")))
12481     ;; We always, always read the .eld file.
12482     (gnus-message 5 "Reading %s..." ding-file)
12483     (let (gnus-newsrc-assoc)
12484       (condition-case nil
12485           (load ding-file t t t)
12486         (error nil))
12487       (and gnus-newsrc-assoc (setq gnus-newsrc-alist gnus-newsrc-assoc)))
12488     (let ((inhibit-quit t))
12489       (gnus-uncompress-newsrc-alist))
12490     (gnus-make-hashtable-from-newsrc-alist)
12491     (if (not (file-newer-than-file-p file ding-file))
12492         ()
12493       ;; Old format quick file
12494       (gnus-message 5 "Reading %s..." file)
12495       ;; The .el file is newer than the .eld file, so we read that one
12496       ;; as well. 
12497       (gnus-read-old-newsrc-el-file file))))
12498
12499 ;; Parse the old-style quick startup file
12500 (defun gnus-read-old-newsrc-el-file (file)
12501   (let (newsrc killed marked group m)
12502     (prog1
12503         (let ((gnus-killed-assoc nil)
12504               gnus-marked-assoc gnus-newsrc-alist gnus-newsrc-assoc)
12505           (prog1
12506               (condition-case nil
12507                   (load file t t t)
12508                 (error nil))
12509             (setq newsrc gnus-newsrc-assoc
12510                   killed gnus-killed-assoc
12511                   marked gnus-marked-assoc)))
12512       (setq gnus-newsrc-alist nil)
12513       (while newsrc
12514         (setq group (car newsrc))
12515         (let ((info (nth 2 (gnus-gethash (car group) gnus-newsrc-hashtb))))
12516           (if info
12517               (progn
12518                 (setcar (nthcdr 2 info) (cdr (cdr group)))
12519                 (setcar (cdr info)
12520                         (if (nth 1 group) gnus-level-default-subscribed 
12521                           gnus-level-default-unsubscribed))
12522                 (setq gnus-newsrc-alist (cons info gnus-newsrc-alist)))
12523             (setq gnus-newsrc-alist
12524                   (cons 
12525                    (setq info
12526                          (list (car group)
12527                                (if (nth 1 group) gnus-level-default-subscribed
12528                                  gnus-level-default-unsubscribed) 
12529                                (cdr (cdr group))))
12530                    gnus-newsrc-alist)))
12531           (if (setq m (assoc (car group) marked))
12532             (setcdr (cdr (cdr info)) (cons (list (cons 'tick (cdr m))) nil))))
12533         (setq newsrc (cdr newsrc)))
12534       (setq newsrc killed)
12535       (while newsrc
12536         (setcar newsrc (car (car newsrc)))
12537         (setq newsrc (cdr newsrc)))
12538       (setq gnus-killed-list killed))
12539     ;; The .el file version of this variable does not begin with
12540     ;; "options", while the .eld version does, so we just add it if it
12541     ;; isn't there.
12542     (and
12543      gnus-newsrc-options 
12544      (progn
12545        (and (not (string-match "^ *options" gnus-newsrc-options))
12546             (setq gnus-newsrc-options (concat "options " gnus-newsrc-options)))
12547        (and (not (string-match "\n$" gnus-newsrc-options))
12548             (setq gnus-newsrc-options (concat gnus-newsrc-options "\n")))))
12549     (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist))
12550     (gnus-make-hashtable-from-newsrc-alist)))
12551       
12552 (defun gnus-make-newsrc-file (file)
12553   "Make server dependent file name by catenating FILE and server host name."
12554   (let* ((file (expand-file-name file nil))
12555          (real-file (concat file "-" (nth 1 gnus-select-method))))
12556     (if (or (file-exists-p real-file)
12557             (file-exists-p (concat real-file ".el"))
12558             (file-exists-p (concat real-file ".eld")))
12559         real-file file)))
12560
12561 (defun gnus-uncompress-newsrc-alist ()
12562   ;; Uncompress all lists of marked articles in the newsrc assoc.
12563   (let ((newsrc gnus-newsrc-alist)
12564         marked)
12565     (while newsrc
12566       (if (not (setq marked (nth 3 (car newsrc))))
12567           ()
12568         (while marked
12569           (or (eq 'score (car (car marked)))
12570               (eq 'bookmark (car (car marked)))
12571               (eq 'killed (car (car marked)))
12572               (setcdr (car marked) (gnus-uncompress-range (cdr (car marked)))))
12573           (setq marked (cdr marked))))
12574       (setq newsrc (cdr newsrc)))))
12575
12576 (defun gnus-compress-newsrc-alist ()
12577   ;; Compress all lists of marked articles in the newsrc assoc.
12578   (let ((newsrc gnus-newsrc-alist)
12579         marked)
12580     (while newsrc
12581       (if (not (setq marked (nth 3 (car newsrc))))
12582           ()
12583         (while marked
12584           (or (eq 'score (car (car marked)))
12585               (eq 'bookmark (car (car marked)))
12586               (eq 'killed (car (car marked)))
12587               (setcdr (car marked) 
12588                       (condition-case ()
12589                           (gnus-compress-sequence 
12590                            (sort (cdr (car marked)) '<) t)
12591                         (error (cdr (car marked))))))
12592           (setq marked (cdr marked))))
12593       (setq newsrc (cdr newsrc)))))
12594
12595 (defun gnus-newsrc-to-gnus-format ()
12596   (setq gnus-newsrc-options "")
12597   (setq gnus-newsrc-options-n nil)
12598
12599   (or gnus-active-hashtb
12600       (setq gnus-active-hashtb (make-vector 4095 0)))
12601   (let ((buf (current-buffer))
12602         (already-read (> (length gnus-newsrc-alist) 1))
12603         group subscribed options-symbol newsrc Options-symbol
12604         symbol reads num1)
12605     (goto-char (point-min))
12606  symbol `opt    ;; We intern theions' in the active hashtb so that we
12607     ;; can `eq' against it later.
12608     (setq options-symbol (intern "options" gnus-active-hashtb))
12609     (setq Options-symbol (intern "Options" gnus-active-hashtb))
12610   
12611     (while (not (eobp))
12612       ;; We first read the first word on the line by narrowing and
12613       ;; then reading into `gnus-active-hashtb'.  Most groups will
12614       ;; already exist in that hashtb, so this will save some string
12615       ;; space.
12616       (narrow-to-region
12617        (point)
12618        (progn (skip-chars-forward "^ \t!:\n") (point)))
12619       (goto-char (point-min))
12620       (setq symbol 
12621             (and (/= (point-min) (point-max))
12622                  (let ((obarray gnus-active-hashtb)) (read buf))))
12623       (widen)
12624       ;; Now, the symbol we have read is either `options' or a group
12625       ;; name.  If it is an options line, we just add it to a string. 
12626       (cond 
12627        ((or (eq symbol options-symbol)
12628             (eq symbol Options-symbol))
12629         (setq gnus-newsrc-options
12630               ;; This concatting is quite inefficient, but since our
12631               ;; thorough studies show that approx 99.37% of all
12632               ;; .newsrc files only contain a single options line, we
12633               ;; don't give a damn, frankly, my dear.
12634               (concat gnus-newsrc-options
12635                       (buffer-substring 
12636                        (gnus-point-at-bol)
12637                        ;; Options may continue on the next line.
12638                        (or (and (re-search-forward "^[^ \t]" nil 'move)
12639                                 (progn (beginning-of-line) (point)))
12640                            (point))))))
12641        (symbol
12642         ;; It was a group name.
12643         (setq subscribed (= (following-char) ?:)
12644               group (symbol-name symbol)
12645               reads nil)
12646         (if (eolp)
12647             ;; If the line ends here, this is clearly a buggy line, so
12648             ;; we put point a the beginning of line and let the cond
12649             ;; below do the error handling.
12650             (beginning-of-line)
12651           ;; We skip to the beginning of the ranges.
12652           (skip-chars-forward "!: \t"))
12653         ;; We are now at the beginning of the list of read articles.
12654         ;; We read them range by range.
12655         (while
12656             (cond 
12657              ((looking-at "[0-9]+")
12658               ;; We narrow and read a number instead of buffer-substring/
12659               ;; string-to-int because it's faster. narrow/widen is
12660               ;; faster than save-restriction/narrow, and save-restriction
12661               ;; produces a garbage object.
12662               (setq num1 (progn
12663                            (narrow-to-region (match-beginning 0) (match-end 0))
12664                            (read buf)))
12665               (widen)
12666               ;; If the next character is a dash, then this is a range.
12667               (if (= (following-char) ?-)
12668                   (progn
12669                     ;; We read the upper bound of the range.
12670                     (forward-char 1)
12671                     (if (not (looking-at "[0-9]+"))
12672                         ;; This is a buggy line, by we pretend that
12673                         ;; it's kinda OK. Perhaps the user should be
12674                         ;; dinged? 
12675                         (setq reads (cons num1 reads))
12676                       (setq reads 
12677                             (cons 
12678                              (cons num1 (progn
12679                                           (narrow-to-region (match-beginning 0) 
12680                                                             (match-end 0))
12681                                           (read buf)))
12682                              reads))
12683                       (widen)))
12684                 ;; It was just a simple number, so we add it to the
12685                 ;; list of ranges.
12686                 (setq reads (cons num1 reads)))
12687               ;; If the next char in ?\n, then we have reached the end
12688               ;; of the line and return nil.
12689               (/= (following-char) ?\n))
12690              ((= (following-char) ?\n)
12691               ;; End of line, so we end.
12692               nil)
12693              (t
12694               ;; Not numbers and not eol, so this might be a buggy
12695               ;; line... 
12696               (or (eobp) ; If it was eob instead of ?\n, we allow it.
12697                   (progn
12698                     ;; The line was buggy.
12699                     (setq group nil)
12700                     (gnus-message 3 "Mangled line: %s" 
12701                                   (buffer-substring (gnus-point-at-bol) 
12702                                                     (gnus-point-at-eol)))
12703                     (ding)
12704                     (sit-for 1)))
12705               nil))
12706           ;; Skip past ", ". Spaces are illegal in these ranges, but
12707           ;; we allow them, because it's a common mistake to put a
12708           ;; space after the comma.
12709           (skip-chars-forward ", "))
12710
12711         ;; We have already read .newsrc.eld, so we gently update the
12712         ;; data in the hash table with the information we have just
12713         ;; read. 
12714         (if (not group)
12715             ()
12716           (let ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
12717                 level)
12718             (if info
12719                 ;; There is an entry for this file in the alist.
12720                 (progn
12721                   (setcar (nthcdr 2 info) (nreverse reads))
12722                   ;; We update the level very gently.  In fact, we
12723                   ;; only change it if there's been a status change
12724                   ;; from subscribed to unsubscribed, or vice versa.
12725                   (setq level (nth 1 info))
12726                   (cond ((and (<= level gnus-level-subscribed)
12727                               (not subscribed))
12728                          (setq level (if reads
12729                                          gnus-level-default-unsubscribed 
12730                                        (1+ gnus-level-default-unsubscribed))))
12731                         ((and (> level gnus-level-subscribed) subscribed)
12732                          (setq level gnus-level-default-subscribed)))
12733                   (setcar (cdr info) level))
12734               ;; This is a new group.
12735               (setq info (list group 
12736                                (if subscribed
12737                                    gnus-level-default-subscribed 
12738                                  (if reads
12739                                      (1+ gnus-level-subscribed)
12740                                    gnus-level-default-unsubscribed))
12741                                (nreverse reads))))
12742             (setq newsrc (cons info newsrc))))))
12743       (forward-line 1))
12744     
12745     (setq newsrc (nreverse newsrc))
12746
12747     (if (not already-read)
12748         ()
12749       ;; We now have two newsrc lists - `newsrc', which is what we
12750       ;; have read from .newsrc, and `gnus-newsrc-alist', which is
12751       ;; what we've read from .newsrc.eld. We have to merge these
12752       ;; lists. We do this by "attaching" any (foreign) groups in the
12753       ;; gnus-newsrc-alist to the (native) group that precedes them. 
12754       (let ((rc (cdr gnus-newsrc-alist))
12755             (prev gnus-newsrc-alist)
12756             entry mentry)
12757         (while rc
12758           (or (null (nth 4 (car rc))) ; It's a native group.
12759               (assoc (car (car rc)) newsrc) ; It's already in the alist.
12760               (if (setq entry (assoc (car (car prev)) newsrc))
12761                   (setcdr (setq mentry (memq entry newsrc))
12762                           (cons (car rc) (cdr mentry)))
12763                 (setq newsrc (cons (car rc) newsrc))))
12764           (setq prev rc
12765                 rc (cdr rc)))))
12766
12767     (setq gnus-newsrc-alist newsrc)
12768     ;; We make the newsrc hashtb.
12769     (gnus-make-hashtable-from-newsrc-alist)
12770
12771     ;; Finally, if we read some options lines, we parse them.
12772     (or (string= gnus-newsrc-options "")
12773         (gnus-newsrc-parse-options gnus-newsrc-options))))
12774
12775 ;; Parse options lines to find "options -n !all rec.all" and stuff.
12776 ;; The return value will be a list on the form
12777 ;; ((regexp1 . ignore)
12778 ;;  (regexp2 . subscribe)...)
12779 ;; When handling new newsgroups, groups that match a `ignore' regexp
12780 ;; will be ignored, and groups that match a `subscribe' regexp will be
12781 ;; subscribed. A line like
12782 ;; options -n !all rec.all
12783 ;; will lead to a list that looks like
12784 ;; (("^rec\\..+" . subscribe) 
12785 ;;  ("^.+" . ignore))
12786 ;; So all "rec.*" groups will be subscribed, while all the other
12787 ;; groups will be ignored. Note that "options -n !all rec.all" is very
12788 ;; different from "options -n rec.all !all". 
12789 (defun gnus-newsrc-parse-options (options)
12790   (let (out eol)
12791     (save-excursion
12792       (gnus-set-work-buffer)
12793       (insert (regexp-quote options))
12794       ;; First we treat all continuation lines.
12795       (goto-char (point-min))
12796       (while (re-search-forward "\n[ \t]+" nil t)
12797         (replace-match " " t t))
12798       ;; Then we transform all "all"s into ".+"s.
12799       (goto-char (point-min))
12800       (while (re-search-forward "\\ball\\b" nil t)
12801         (replace-match ".+" t t))
12802       (goto-char (point-min))
12803       ;; We remove all other options than the "-n" ones.
12804       (while (re-search-forward "[ \t]-[^n][^-]*" nil t)
12805         (replace-match " ")
12806         (forward-char -1))
12807       (goto-char (point-min))
12808
12809       ;; We are only interested in "options -n" lines - we
12810       ;; ignore the other option lines.
12811       (while (re-search-forward "[ \t]-n" nil t)
12812         (setq eol 
12813               (or (save-excursion
12814                     (and (re-search-forward "[ \t]-n" (gnus-point-at-eol) t)
12815                          (- (point) 2)))
12816                   (gnus-point-at-eol)))
12817         ;; Search for all "words"...
12818         (while (re-search-forward "[^ \t,\n]+" eol t)
12819           (if (= (char-after (match-beginning 0)) ?!)
12820               ;; If the word begins with a bang (!), this is a "not"
12821               ;; spec. We put this spec (minus the bang) and the
12822               ;; symbol `ignore' into the list.
12823               (setq out (cons (cons (concat 
12824                                      "^" (buffer-substring 
12825                                           (1+ (match-beginning 0))
12826                                           (match-end 0)))
12827                                     'ignore) out))
12828             ;; There was no bang, so this is a "yes" spec.
12829             (setq out (cons (cons (concat 
12830                                    "^" (buffer-substring (match-beginning 0)
12831                                                          (match-end 0)))
12832                                   'subscribe) out)))))
12833     
12834       (setq gnus-newsrc-options-n out))))
12835                
12836
12837 (defun gnus-save-newsrc-file ()
12838   "Save .newsrc file."
12839   ;; Note: We cannot save .newsrc file if all newsgroups are removed
12840   ;; from the variable gnus-newsrc-alist.
12841   (and (or gnus-newsrc-alist gnus-killed-list)
12842        gnus-current-startup-file
12843        (progn
12844          (run-hooks 'gnus-save-newsrc-hook)
12845          (save-excursion
12846            (if (or (not gnus-dribble-buffer)
12847                    (not (buffer-name gnus-dribble-buffer))
12848                    (zerop (save-excursion
12849                             (set-buffer gnus-dribble-buffer)
12850                             (buffer-size))))
12851                (gnus-message 4 "(No changes need to be saved)")
12852              (if gnus-save-newsrc-file
12853                  (progn
12854                    (gnus-message 5 "Saving %s..." gnus-current-startup-file)
12855                    ;; Make backup file of master newsrc.
12856                    (gnus-gnus-to-newsrc-format)
12857                    (gnus-message 5 "Saving %s...done"
12858                                  gnus-current-startup-file)))
12859              ;; Quickly loadable .newsrc.
12860              (set-buffer (get-buffer-create " *Gnus-newsrc*"))
12861              (set-visited-file-name (concat gnus-current-startup-file ".eld"))
12862              (gnus-add-current-to-buffer-list)
12863              (buffer-disable-undo (current-buffer))
12864              (erase-buffer)
12865              (gnus-message 5 "Saving %s.eld..." gnus-current-startup-file)
12866              (gnus-gnus-to-quick-newsrc-format)
12867              (save-buffer)
12868              (kill-buffer (current-buffer))
12869              (gnus-message 5 "Saving %s.eld...done" gnus-current-startup-file)
12870              (gnus-dribble-delete-file))))))
12871
12872 (defun gnus-gnus-to-quick-newsrc-format ()
12873   "Insert Gnus variables such as gnus-newsrc-alist in lisp format."
12874   (insert ";; (ding) Gnus startup file.\n")
12875   (insert ";; Never delete this file - touch .newsrc instead to force Gnus\n")
12876   (insert ";; to read .newsrc.\n")
12877   (let ((variables gnus-variable-list)
12878         (inhibit-quit t)
12879         (gnus-newsrc-alist (cdr gnus-newsrc-alist))
12880         variable)
12881     ;; insert lisp expressions.
12882     (gnus-compress-newsrc-alist)
12883     (while variables
12884       (setq variable (car variables))
12885       (and (boundp variable)
12886            (symbol-value variable)
12887            (or gnus-save-killed-list (not (eq variable 'gnus-killed-list)))
12888            (insert "(setq " (symbol-name variable) " '"
12889                    (prin1-to-string (symbol-value variable))
12890                    ")\n"))
12891       (setq variables (cdr variables)))
12892     (gnus-uncompress-newsrc-alist)))
12893
12894
12895 (defun gnus-gnus-to-newsrc-format ()
12896   ;; Generate and save the .newsrc file.
12897   (let ((newsrc (cdr gnus-newsrc-alist))
12898         info ranges range)
12899     (save-excursion
12900       (set-buffer (create-file-buffer gnus-current-startup-file))
12901       (set-visited-file-name gnus-current-startup-file)
12902       (buffer-disable-undo (current-buffer))
12903       (erase-buffer)
12904       ;; Write options.
12905       (if gnus-newsrc-options (insert gnus-newsrc-options))
12906       ;; Write subscribed and unsubscribed.
12907       (while newsrc
12908         (setq info (car newsrc))
12909         (if (not (nth 4 info))          ;Don't write foreign groups to .newsrc.
12910             (progn
12911               (insert (car info) (if (> (nth 1 info) gnus-level-subscribed)
12912                                      "!" ":"))
12913               (if (setq ranges (nth 2 info))
12914                   (progn
12915                     (insert " ")
12916                     (if (not (listp (cdr ranges)))
12917                         (if (= (car ranges) (cdr ranges))
12918                             (insert (int-to-string (car ranges)))
12919                           (insert (int-to-string (car ranges)) "-" 
12920                                   (int-to-string (cdr ranges))))
12921                       (while ranges
12922                         (setq range (car ranges)
12923                               ranges (cdr ranges))
12924                         (if (or (atom range) (= (car range) (cdr range)))
12925                             (insert (int-to-string 
12926                                      (or (and (atom range) range) 
12927                                          (car range))))
12928                           (insert (int-to-string (car range)) "-"
12929                                   (int-to-string (cdr range))))
12930                         (if ranges (insert ","))))))
12931               (insert "\n")))
12932         (setq newsrc (cdr newsrc)))
12933       (save-buffer)
12934       (kill-buffer (current-buffer)))))
12935
12936 (defun gnus-read-all-descriptions-files ()
12937   (let ((methods (cons gnus-select-method gnus-secondary-select-methods)))
12938     (while methods
12939       (gnus-read-descriptions-file (car methods))
12940       (setq methods (cdr methods)))
12941     t))
12942
12943 (defun gnus-read-descriptions-file (&optional method)
12944   (let ((method (or method gnus-select-method)))
12945     ;; We create the hashtable whether we manage to read the desc file
12946     ;; to avoid trying to re-read after a failed read.
12947     (or gnus-description-hashtb
12948         (setq gnus-description-hashtb 
12949               (gnus-make-hashtable (length gnus-active-hashtb))))
12950     ;; Mark this method's desc file as read.
12951     (gnus-sethash (gnus-group-prefixed-name "" method) "Has read"
12952                   gnus-description-hashtb)
12953
12954     (gnus-message 5 "Reading descriptions file via %s..." (car method))
12955     (cond 
12956      ((not (or (gnus-server-opened method)
12957                (gnus-open-server method)))
12958       (gnus-message 1 "Couldn't open server")
12959       nil)
12960      ((not (gnus-request-list-newsgroups method))
12961       (gnus-message 1 "Couldn't read newsgroups descriptions")
12962       nil)
12963      (t
12964       (let (group)
12965         (save-excursion
12966           (save-restriction
12967             (set-buffer nntp-server-buffer)
12968             (goto-char (point-min))
12969             (if (or (search-forward "\n.\n" nil t)
12970                     (goto-char (point-max)))
12971                 (progn
12972                   (beginning-of-line)
12973                   (narrow-to-region (point-min) (point))))
12974             (goto-char (point-min))
12975             (while (not (eobp))
12976               ;; If we get an error, we set group to 0, which is not a
12977               ;; symbol... 
12978               (setq group 
12979                     (condition-case ()
12980                         (let ((obarray gnus-description-hashtb))
12981                           ;; Group is set to a symbol interned in this
12982                           ;; hash table.
12983                           (read nntp-server-buffer))
12984                       (error 0)))
12985               (skip-chars-forward " \t")
12986               ;; ... which leads to this line being effectively ignored.
12987               (and (symbolp group)
12988                    (set group (buffer-substring 
12989                                (point) (progn (end-of-line) (point)))))
12990               (forward-line 1))))
12991         (gnus-message 5 "Reading descriptions file...done")
12992         t)))))
12993
12994 (defun gnus-group-get-description (group)
12995   ;; Get the description of a group by sending XGTITLE to the server.
12996   (and (gnus-request-group-description group)
12997        (save-excursion
12998          (set-buffer nntp-server-buffer)
12999          (goto-char (point-min))
13000          (and (looking-at "[^ \t]+[ \t]+\\(.*\\)")
13001               (buffer-substring (match-beginning 1) (match-end 1))))))
13002
13003 ;;;
13004 ;;; Server
13005 ;;;
13006
13007 (defvar gnus-server-mode-hook nil
13008   "Hook run in `gnus-server-mode' buffers.")
13009
13010 (defconst gnus-server-line-format "     {%(%h:%w%)}\n"
13011   "Format of server lines.
13012 It works along the same lines as a normal formatting string,
13013 with some simple extensions.")
13014
13015 (defvar gnus-server-mode-line-format "(ding) List of servers"
13016   "The format specification for the server mode line.")
13017
13018 (defconst gnus-server-line-format-alist
13019   (list (list ?h 'how ?s)
13020         (list ?n 'name ?s)
13021         (list ?w 'where ?s)
13022         ))
13023
13024 (defconst gnus-server-mode-line-format-alist 
13025   (list (list ?S 'news-server ?s)
13026         (list ?M 'news-method ?s)
13027         (list ?u 'user-defined ?s)))
13028
13029 (defvar gnus-server-line-format-spec nil)
13030 (defvar gnus-server-mode-line-format-spec nil)
13031 (defvar gnus-server-killed-servers nil)
13032
13033 (defvar gnus-server-mode-map nil)
13034 (put 'gnus-server-mode 'mode-class 'special)
13035
13036 (if gnus-server-mode-map
13037     nil
13038   (setq gnus-server-mode-map (make-sparse-keymap))
13039   (suppress-keymap gnus-server-mode-map)
13040   (define-key gnus-server-mode-map " " 'gnus-server-read-server)
13041   (define-key gnus-server-mode-map "\r" 'gnus-server-read-server)
13042   (define-key gnus-server-mode-map gnus-mouse-2 'gnus-server-pick-server)
13043   (define-key gnus-server-mode-map "q" 'gnus-server-exit)
13044   (define-key gnus-server-mode-map "l" 'gnus-server-list-servers)
13045   (define-key gnus-server-mode-map "k" 'gnus-server-kill-server)
13046   (define-key gnus-server-mode-map "y" 'gnus-server-yank-server)
13047   (define-key gnus-server-mode-map "c" 'gnus-server-copy-server)
13048   (define-key gnus-server-mode-map "a" 'gnus-server-add-server)
13049   (define-key gnus-server-mode-map "e" 'gnus-server-edit-server))
13050
13051 (defun gnus-server-mode ()
13052   "Major mode for listing and editing servers.
13053
13054 All normal editing commands are switched off.
13055 \\<gnus-server-mode-map>
13056
13057 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]'). 
13058
13059 The following commands are available:
13060
13061 \\{gnus-server-mode-map}"
13062   (interactive)
13063   (if gnus-visual (gnus-server-make-menu-bar))
13064   (kill-all-local-variables)
13065   (setq mode-line-modified "-- ")
13066   (make-local-variable 'mode-line-format)
13067   (setq mode-line-format (copy-sequence mode-line-format))
13068   (and (equal (nth 3 mode-line-format) "   ")
13069        (setcar (nthcdr 3 mode-line-format) ""))
13070   (setq major-mode 'gnus-server-mode)
13071   (setq mode-name "Server")
13072 ;  (gnus-group-set-mode-line)
13073   (setq mode-line-process nil)
13074   (use-local-map gnus-server-mode-map)
13075   (buffer-disable-undo (current-buffer))
13076   (setq truncate-lines t)
13077   (setq buffer-read-only t)
13078   (run-hooks 'gnus-server-mode-hook))
13079
13080 (defun gnus-server-insert-server-line (sformat name method)
13081   (let* ((sformat (or sformat gnus-server-line-format-spec))
13082          (how (car method))
13083          (where (nth 1 method))
13084          b)
13085     (beginning-of-line)
13086     (setq b (point))
13087     ;; Insert the text.
13088     (insert (eval sformat))
13089     (add-text-properties b (1+ b) (list 'gnus-server (intern name)))))
13090
13091 (defun gnus-server-setup-buffer ()
13092   (if (get-buffer gnus-server-buffer)
13093       ()
13094     (save-excursion
13095       (set-buffer (get-buffer-create gnus-server-buffer))
13096       (gnus-server-mode)
13097       (and gnus-carpal (gnus-carpal-setup-buffer 'server)))))
13098
13099 (defun gnus-server-prepare ()
13100   (setq gnus-server-mode-line-format-spec 
13101         (gnus-parse-format gnus-server-mode-line-format 
13102                            gnus-server-mode-line-format-alist))
13103   (setq gnus-server-line-format-spec 
13104         (gnus-parse-format gnus-server-line-format 
13105                            gnus-server-line-format-alist))
13106   (let ((alist gnus-server-alist)
13107         (buffer-read-only nil))
13108     (erase-buffer)
13109     (while alist
13110       (gnus-server-insert-server-line nil (car (car alist)) (cdr (car alist)))
13111       (setq alist (cdr alist))))
13112   (goto-char (point-min))
13113   (gnus-server-position-cursor))
13114
13115 (defun gnus-server-server-name ()
13116   (let ((server (get-text-property (gnus-point-at-bol) 'gnus-server)))
13117     (and server (symbol-name server))))
13118
13119 (defalias 'gnus-server-position-cursor 'gnus-goto-colon)
13120
13121 (defconst gnus-server-edit-buffer "*Gnus edit server*")
13122
13123 (defun gnus-server-update-server (server)
13124   (save-excursion
13125     (set-buffer gnus-server-buffer)
13126     (let ((buffer-read-only nil)
13127           (info (cdr (assoc server gnus-server-alist))))
13128       (gnus-dribble-enter 
13129        (concat "(gnus-server-set-info \"" server "\" '"
13130                (prin1-to-string info) ")"))
13131       ;; Buffer may be narrowed.
13132       (save-restriction
13133         (widen)
13134         (if (gnus-server-goto-server server)
13135             (delete-region (progn (beginning-of-line) (point))
13136                            (progn (forward-line 1) (point))))
13137         (let ((entry (assoc server gnus-server-alist)))
13138           (gnus-server-insert-server-line nil (car entry) (cdr entry))
13139           (gnus-server-position-cursor))))))
13140
13141 (defun gnus-server-set-info (server info)
13142   ;; Enter a select method into the virtual server alist.
13143   (gnus-dribble-enter 
13144    (concat "(gnus-server-set-info \"" server "\" '"
13145            (prin1-to-string info) ")"))
13146   (let* ((server (nth 1 info))
13147          (entry (assoc server gnus-server-alist)))
13148     (if entry (setcdr entry info)
13149       (setq gnus-server-alist
13150             (nconc gnus-server-alist (list (cons server info)))))))
13151
13152 (defun gnus-server-to-method (server)
13153   ;; Map virtual server names to select methods.
13154   (or (and (equal server "native") gnus-select-method)
13155       (cdr (assoc server gnus-server-alist))))
13156
13157 (defun gnus-server-extend-method (group method)
13158   ;; This function "extends" a virtual server.  If the server is
13159   ;; "hello", and the select method is ("hello" (my-var "something")) 
13160   ;; in the group "alt.alt", this will result in a new virtual server
13161   ;; called "helly+alt.alt".
13162   (let ((entry
13163          (gnus-copy-sequence 
13164           (if (equal (car method) "native") gnus-select-method
13165               (cdr (assoc (car method) gnus-server-alist))))))
13166     (setcar (cdr entry) (concat (nth 1 entry) "+" group))
13167     (nconc entry (cdr method))))
13168
13169 (defun gnus-server-get-method (group method)
13170   ;; Input either a server name, and extended server name, or a
13171   ;; select method, and return a select method. 
13172   (cond ((stringp method)
13173          (gnus-server-to-method method))
13174         ((stringp (car method))
13175          (gnus-server-extend-method group method))
13176         (t
13177          (gnus-server-add-address method))))
13178
13179 (defun gnus-server-add-address (method)
13180   (let ((method-name (symbol-name (car method))))
13181     (if (and (memq 'address (assoc method-name gnus-valid-select-methods))
13182              (not (assq (intern (concat method-name "-address")) method)))
13183         (append method (list (list (intern (concat method-name "-address"))
13184                                    (nth 1 method))))
13185       method)))
13186
13187 (defun gnus-server-equal (s1 s2)
13188   (or (equal s1 s2)
13189       (and (= (length s1) (length s2))
13190            (progn
13191              (while (and s1 (member (car s1) s2))
13192                (setq s1 (cdr s1)))
13193              (null s1)))))
13194
13195 ;;; Interactive server functions.
13196
13197 (defun gnus-server-kill-server (server)
13198   "Kill the server on the current line."
13199   (interactive (list (gnus-server-server-name)))
13200   (or (gnus-server-goto-server server)
13201       (if server (error "No such server: %s" server)
13202         (error "No server on the current line")))
13203   (let ((buffer-read-only nil))
13204     (delete-region (progn (beginning-of-line) (point))
13205                    (progn (forward-line 1) (point))))
13206   (setq gnus-server-killed-servers 
13207         (cons (assoc server gnus-server-alist) gnus-server-killed-servers))
13208   (setq gnus-server-alist (delq (car gnus-server-killed-servers)
13209                                 gnus-server-alist))
13210   (gnus-server-position-cursor))
13211
13212 (defun gnus-server-yank-server ()
13213   "Yank the previously killed server."
13214   (interactive)
13215   (or gnus-server-killed-servers
13216       (error "No killed servers to be yanked"))
13217   (let ((alist gnus-server-alist)
13218         (server (gnus-server-server-name))
13219         (killed (car gnus-server-killed-servers)))
13220     (if (not server) 
13221         (setq gnus-server-alist (nconc gnus-server-alist (list killed)))
13222       (if (string= server (car (car gnus-server-alist)))
13223           (setq gnus-server-alist (cons killed gnus-server-alist))
13224         (while (and (cdr alist)
13225                     (not (string= server (car (car (cdr alist))))))
13226           (setq alist (cdr alist)))
13227         (setcdr alist (cons killed (cdr alist)))))
13228     (gnus-server-update-server (car killed))
13229     (setq gnus-server-killed-servers (cdr gnus-server-killed-servers))
13230     (gnus-server-position-cursor)))
13231
13232 (defun gnus-server-exit ()
13233   "Return to the group buffer."
13234   (interactive)
13235   (kill-buffer (current-buffer))
13236   (switch-to-buffer gnus-group-buffer))
13237
13238 (defun gnus-server-list-servers ()
13239   "List all available servers."
13240   (interactive)
13241   (let ((cur (gnus-server-server-name)))
13242     (gnus-server-prepare)
13243     (if cur (gnus-server-goto-server cur)
13244       (goto-char (point-max))
13245       (forward-line -1))
13246     (gnus-server-position-cursor)))
13247
13248 (defun gnus-server-copy-server (from to)
13249   (interactive
13250    (list
13251     (or (gnus-server-server-name)
13252         (error "No server on the current line"))
13253     (read-string "Copy to: ")))
13254   (or from (error "No server on current line"))
13255   (or (and to (not (string= to ""))) (error "No name to copy to"))
13256   (and (assoc to gnus-server-alist) (error "%s already exists" to))
13257   (or (assoc from gnus-server-alist) 
13258       (error "%s: no such server" from))
13259   (let ((to-entry (gnus-copy-sequence (assoc from gnus-server-alist))))
13260     (setcar to-entry to)
13261     (setcar (nthcdr 2 to-entry) to)
13262     (setq gnus-server-killed-servers 
13263           (cons to-entry gnus-server-killed-servers))
13264     (gnus-server-yank-server)))
13265
13266 (defun gnus-server-add-server (how where)
13267   (interactive 
13268    (list (intern (completing-read "Server method: "
13269                                   gnus-valid-select-methods nil t))
13270          (read-string "Server name: ")))
13271   (setq gnus-server-killed-servers 
13272         (cons (list where how where) gnus-server-killed-servers))
13273   (gnus-server-yank-server))
13274
13275 (defun gnus-server-goto-server (server)
13276   "Jump to a server line."
13277   (interactive
13278    (list (completing-read "Goto server: " gnus-server-alist nil t)))
13279   (let ((to (text-property-any (point-min) (point-max) 
13280                                'gnus-server (intern server))))
13281     (and to
13282          (progn
13283            (goto-char to) 
13284            (gnus-server-position-cursor)))))
13285
13286 (defun gnus-server-edit-server (server)
13287   "Edit the server on the current line."
13288   (interactive (list (gnus-server-server-name)))
13289   (or server
13290       (error "No server on current line"))
13291   (let ((winconf (current-window-configuration)))
13292     (get-buffer-create gnus-server-edit-buffer)
13293     (gnus-configure-windows 'edit-server)
13294     (gnus-add-current-to-buffer-list)
13295     (emacs-lisp-mode)
13296     (make-local-variable 'gnus-prev-winconf)
13297     (setq gnus-prev-winconf winconf)
13298     (use-local-map (copy-keymap (current-local-map)))
13299     (let ((done-func '(lambda () 
13300                         "Exit editing mode and update the information."
13301                         (interactive)
13302                         (gnus-server-edit-server-done 'group))))
13303       (setcar (cdr (nth 4 done-func)) server)
13304       (local-set-key "\C-c\C-c" done-func))
13305     (erase-buffer)
13306     (insert ";; Type `C-c C-c' after you have edited the server.\n\n")
13307     (insert (pp-to-string (cdr (assoc server gnus-server-alist))))))
13308
13309 (defun gnus-server-edit-server-done (server)
13310   (interactive)
13311   (set-buffer (get-buffer-create gnus-server-edit-buffer))
13312   (goto-char (point-min))
13313   (let ((form (read (current-buffer)))
13314         (winconf gnus-prev-winconf))
13315     (gnus-server-set-info server form)
13316     (kill-buffer (current-buffer))
13317     (and winconf (set-window-configuration winconf))
13318     (set-buffer gnus-server-buffer)
13319     (gnus-server-update-server (gnus-server-server-name))
13320     (gnus-server-position-cursor)))
13321
13322 (defun gnus-server-read-server (server)
13323   "Browse a server."
13324   (interactive (list (gnus-server-server-name)))
13325   (gnus-browse-foreign-server (gnus-server-to-method server) (current-buffer)))
13326
13327 (defun gnus-mouse-pick-server (e)
13328   (interactive "e")
13329   (mouse-set-point e)
13330   (gnus-server-read-server (gnus-server-server-name)))
13331
13332 ;;;
13333 ;;; entry points into gnus-score.el
13334 ;;;
13335
13336 ;;; Finding score files. 
13337
13338 (defvar gnus-global-score-files nil
13339   "*List of global score files and directories.
13340 Set this variable if you want to use people's score files.  One entry
13341 for each score file or each score file directory.  Gnus will decide
13342 by itself what score files are applicable to which group.
13343
13344 Say you want to use the single score file
13345 \"/ftp.ifi.uio.no@ftp:/pub/larsi/ding/score/soc.motss.SCORE\" and all
13346 score files in the \"/ftp.some-where:/pub/score\" directory.
13347
13348  (setq gnus-global-score-files
13349        '(\"/ftp.ifi.uio.no:/pub/larsi/ding/score/soc.motss.SCORE\"
13350          \"/ftp.some-where:/pub/score\"))")
13351
13352 (defun gnus-score-score-files (group)
13353   "Return a list of all possible score files."
13354   ;; Search and set any global score files.
13355   (and gnus-global-score-files 
13356        (or gnus-internal-global-score-files
13357            (gnus-score-search-global-directories gnus-global-score-files)))
13358   ;; Fix the kill-file dir variable.
13359   (setq gnus-kill-files-directory 
13360         (file-name-as-directory
13361          (or gnus-kill-files-directory "~/News/")))
13362   ;; If we can't read it, there are no score files.
13363   (if (not (file-exists-p (expand-file-name gnus-kill-files-directory)))
13364       (setq gnus-score-file-list nil)
13365     (if (gnus-use-long-file-name 'not-score)
13366         ;; We want long file names.
13367         (if (or (not gnus-score-file-list)
13368                 (not (car gnus-score-file-list))
13369                 (gnus-file-newer-than gnus-kill-files-directory
13370                                       (car gnus-score-file-list)))
13371               (setq gnus-score-file-list 
13372                     (cons (nth 5 (file-attributes gnus-kill-files-directory))
13373                           (nreverse 
13374                            (directory-files 
13375                             gnus-kill-files-directory t 
13376                             (gnus-score-file-regexp))))))
13377       ;; We do not use long file names, so we have to do some
13378       ;; directory traversing.  
13379       (let ((mdir (length (expand-file-name gnus-kill-files-directory)))
13380             (suffixes (list gnus-score-file-suffix gnus-adaptive-file-suffix))
13381             dir files suffix)
13382         (while suffixes
13383           (setq dir (expand-file-name
13384                      (concat gnus-kill-files-directory
13385                              (gnus-replace-chars-in-string group ?. ?/))))
13386           (setq dir (gnus-replace-chars-in-string dir ?: ?/))
13387           (setq suffix (car suffixes)
13388                 suffixes (cdr suffixes))
13389           (if (file-exists-p (concat dir "/" suffix))
13390               (setq files (cons (concat dir "/" suffix) files)))
13391           (while (>= (1+ (length dir)) mdir)
13392             (and (file-exists-p (concat dir "/all/" suffix))
13393                  (setq files (cons (concat dir "/all/" suffix) files)))
13394             (string-match "/[^/]*$" dir)
13395             (setq dir (substring dir 0 (match-beginning 0)))))
13396         (setq gnus-score-file-list 
13397               (cons nil (nreverse files)))))
13398     (cdr gnus-score-file-list)))
13399
13400 (defun gnus-score-file-regexp ()
13401   (concat "\\(" gnus-score-file-suffix 
13402           "\\|" gnus-adaptive-file-suffix "\\)$"))
13403         
13404 (defun gnus-score-find-bnews (group)
13405   "Return a list of score files for GROUP.
13406 The score files are those files in the ~/News directory which matches
13407 GROUP using BNews sys file syntax."
13408   (let* ((sfiles (append (gnus-score-score-files group)
13409                          gnus-internal-global-score-files))
13410          (kill-dir (file-name-as-directory 
13411                     (expand-file-name gnus-kill-files-directory)))
13412          (klen (length kill-dir))
13413          ofiles not-match regexp)
13414     (save-excursion
13415       (set-buffer (get-buffer-create "*gnus score files*"))
13416       (buffer-disable-undo (current-buffer))
13417       ;; Go through all score file names and create regexp with them
13418       ;; as the source.  
13419       (while sfiles
13420         (erase-buffer)
13421         (insert (car sfiles))
13422         (goto-char (point-min))
13423         ;; First remove the suffix itself.
13424         (re-search-forward (concat "." (gnus-score-file-regexp)))
13425         (replace-match "" t t) 
13426         (goto-char (point-min))
13427         (if (looking-at (regexp-quote kill-dir))
13428             ;; If the file name was just "SCORE", `klen' is one character
13429             ;; too much.
13430             (delete-char (min (1- (point-max)) klen))
13431           (goto-char (point-max))
13432           (search-backward "/")
13433           (delete-region (1+ (point)) (point-min)))
13434         ;; If short file names were used, we have to translate slashes.
13435         (goto-char (point-min))
13436         (while (re-search-forward "[/:]" nil t)
13437           (replace-match "." t t))
13438         ;; Translate "all" to ".*".
13439         (while (search-forward "all" nil t)
13440           (replace-match ".*" t t))
13441         (goto-char (point-min))
13442         ;; Deal with "not."s.
13443         (if (looking-at "not.")
13444             (progn
13445               (setq not-match t)
13446               (setq regexp (buffer-substring 5 (point-max))))
13447           (setq regexp (buffer-substring 1 (point-max)))
13448           (setq not-match nil))
13449         ;; Finally - if this resulting regexp matches the group name,
13450         ;; we add this score file to the list of score files
13451         ;; applicable to this group.
13452         (if (or (and not-match
13453                      (not (string-match regexp group)))
13454                 (and (not not-match)
13455                      (string-match regexp group)))
13456             (setq ofiles (cons (car sfiles) ofiles)))
13457         (setq sfiles (cdr sfiles)))
13458       (kill-buffer (current-buffer))
13459       ;; Slight kludge here - the last score file returned should be
13460       ;; the local score file, whether it exists or not. This is so
13461       ;; that any score commands the user enters will go to the right
13462       ;; file, and not end up in some global score file.
13463       (let ((localscore
13464              (expand-file-name
13465               (if (gnus-use-long-file-name 'not-score)
13466                   (concat gnus-kill-files-directory group "." 
13467                           gnus-score-file-suffix)
13468                 (concat gnus-kill-files-directory
13469                         (gnus-replace-chars-in-string group ?. ?/)
13470                         "/" gnus-score-file-suffix)))))
13471         (and (member localscore ofiles)
13472              (delete localscore ofiles))
13473         (setq ofiles (cons localscore ofiles)))
13474       (nreverse ofiles))))
13475
13476 (defun gnus-score-find-single (group)
13477   "Return list containing the score file for GROUP."
13478   (list (gnus-score-file-name group gnus-adaptive-file-suffix)
13479         (gnus-score-file-name group)))
13480
13481 (defun gnus-score-find-hierarchical (group)
13482   "Return list of score files for GROUP.
13483 This includes the score file for the group and all its parents."
13484   (let ((all (copy-sequence '(nil)))
13485         (start 0))
13486     (while (string-match "\\." group (1+ start))
13487       (setq start (match-beginning 0))
13488       (setq all (cons (substring group 0 start) all)))
13489     (setq all (cons group all))
13490     (nconc
13491      (mapcar (lambda (newsgroup)
13492                (gnus-score-file-name newsgroup gnus-adaptive-file-suffix))
13493              (setq all (nreverse all)))
13494      (mapcar 'gnus-score-file-name all))))
13495
13496 (defvar gnus-score-file-alist-cache nil)
13497
13498 (defun gnus-score-find-alist (group)
13499   "Return list of score files for GROUP.
13500 The list is determined from the variable gnus-score-file-alist."
13501   (let ((alist gnus-score-file-multiple-match-alist)
13502         score-files)
13503     ;; if this group has been seen before, return the cached entry
13504     (if (setq score-files (assoc group gnus-score-file-alist-cache))
13505         (cdr score-files)       ; ensures caching of groups with no matches
13506       ;; handle the multiple match alist
13507       (while alist
13508         (and (string-match (car (car alist)) group)
13509              (setq score-files
13510                    (nconc score-files (cdr (car alist)))))
13511         (setq alist (cdr alist)))
13512       (setq alist gnus-score-file-single-match-alist)
13513       ;; handle the single match alist
13514       (catch 'done
13515         (while alist
13516           (and (string-match (car (car alist)) group)
13517                ;; progn used just in case ("regexp") has no files
13518                ;; and score-files is still nil. -sj
13519                ;; this can be construed as a "stop searching here" feature :>
13520                ;; and used to simplify regexps in the single-alist 
13521                (progn
13522                  (setq score-files
13523                        (append score-files (cdr (car alist))))
13524                  (throw 'done nil)))
13525           (setq alist (cdr alist))))
13526       ;; cache the score files
13527       (setq gnus-score-file-alist-cache
13528             (cons (cons group score-files) gnus-score-file-alist-cache))
13529       score-files)))
13530
13531
13532 (defun gnus-possibly-score-headers (&optional trace)
13533   (let ((func gnus-score-find-score-files-function)
13534         score-files)
13535     (and func (not (listp func))
13536          (setq func (list func)))
13537     ;; Go through all the functions for finding score files (or actual
13538     ;; scores) and add them to a list.
13539     (setq score-files (copy-sequence
13540                        (gnus-score-find-alist gnus-newsgroup-name)))
13541     (while func
13542       (and (symbolp (car func))
13543            (fboundp (car func))
13544            (setq score-files 
13545                  (nconc score-files (funcall (car func) gnus-newsgroup-name))))
13546       (setq func (cdr func)))
13547     (if score-files (gnus-score-headers score-files trace))))
13548
13549 (defun gnus-score-file-name (newsgroup &optional suffix)
13550   "Return the name of a score file for NEWSGROUP."
13551   (let ((suffix (or suffix gnus-score-file-suffix)))
13552     (cond  ((or (null newsgroup)
13553                 (string-equal newsgroup ""))
13554             ;; The global score file is placed at top of the directory.
13555             (expand-file-name 
13556              suffix (or gnus-kill-files-directory "~/News")))
13557            ((gnus-use-long-file-name 'not-score)
13558             ;; Append ".SCORE" to newsgroup name.
13559             (expand-file-name (concat newsgroup "." suffix)
13560                               (or gnus-kill-files-directory "~/News")))
13561            (t
13562             ;; Place "SCORE" under the hierarchical directory.
13563             (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
13564                                       "/" suffix)
13565                               (or gnus-kill-files-directory "~/News"))))))
13566
13567 (defun gnus-score-search-global-directories (files)
13568   "Scan all global score directories for score files."
13569   ;; Set the variable `gnus-internal-global-score-files' to all
13570   ;; available global score files.
13571   (interactive (list gnus-global-score-files))
13572   (let (out)
13573     (while files
13574       (if (string-match "/$" (car files))
13575           (setq out (nconc (directory-files 
13576                             (car files) t
13577                             (concat (gnus-score-file-regexp) "$"))))
13578         (setq out (cons (car files) out)))
13579       (setq files (cdr files)))
13580     (setq gnus-internal-global-score-files out)))
13581
13582 ;; Allow redefinition of Gnus functions.
13583
13584 (gnus-ems-redefine)
13585
13586 (provide 'gnus)
13587
13588 ;;; gnus.el ends here