e39e9b2d08ae095ad87ba5a7fbc4143b6deeed39
[gnus] / lisp / gnus.el
1 ;;; gnus.el --- a newsreader for GNU Emacs
2 ;; Copyright (C) 1987,88,89,90,93,94,95 Free Software Foundation, Inc.
3
4 ;; Author: Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
5 ;;      Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23
24 ;;; Commentary:
25
26 ;; Although (ding) Gnus looks suspiciously like GNUS, it isn't quite
27 ;; the same beast. Most internal structures have been changed. If you
28 ;; have written packages that depend on any of the hash tables,
29 ;; `gnus-newsrc-alist', `gnus-killed-assoc', marked lists, the .newsrc
30 ;; buffer, or internal knowledge of the `nntp-header-' macros, or
31 ;; dependence on the buffers having a certain format, your code will
32 ;; fail.
33
34 ;;; Code:
35
36 (require 'mail-utils)
37 (require 'timezone)
38 (require 'nnheader)
39
40 ;; Site dependent variables. These variables should be defined in
41 ;; paths.el.
42
43 (defvar gnus-default-nntp-server nil
44   "Specify a default NNTP server.
45 This variable should be defined in paths.el, and should never be set
46 by the user.
47 If you want to change servers, you should use `gnus-select-method'.
48 See the documentation to that variable.")
49
50 (defconst gnus-backup-default-subscribed-newsgroups 
51   '("news.announce.newusers" "news.groups.questions" "gnu.emacs.gnus")
52   "Default default new newsgroups the first time Gnus is run.
53 Should be set in paths.el, and shouldn't be touched by the user.")
54
55 (defvar gnus-local-domain nil
56   "Local domain name without a host name.
57 The DOMAINNAME environment variable is used instead if it is defined.
58 If the `system-name' function returns the full Internet name, there is
59 no need to set this variable.")
60
61 (defvar gnus-local-organization nil
62   "String with a description of what organization (if any) the user belongs to.
63 The ORGANIZATION environment variable is used instead if it is defined.
64 If this variable contains a function, this function will be called
65 with the current newsgroup name as the argument. The function should
66 return a string.
67
68 In any case, if the string (either in the variable, in the environment
69 variable, or returned by the function) is a file name, the contents of
70 this file will be used as the organization.")
71
72 (defvar gnus-use-generic-from nil
73   "If nil, the full host name will be the system name prepended to the domain name.
74 If this is a string, the full host name will be this string.
75 If this is non-nil, non-string, the domain name will be used as the
76 full host name.")
77
78 (defvar gnus-use-generic-path nil
79   "If nil, use the NNTP server name in the Path header.
80 If stringp, use this; if non-nil, use no host name (user name only).")
81
82
83 ;; Customization variables
84
85 (defvar gnus-select-method 
86   (list 'nntp (or (getenv "NNTPSERVER") 
87                   (if (and gnus-default-nntp-server
88                            (not (string= gnus-default-nntp-server "")))
89                       gnus-default-nntp-server)
90                   (system-name)))
91   "*Default method for selecting a newsgroup.
92 This variable should be a list, where the first element is how the
93 news is to be fetched, the second is the address. 
94
95 For instance, if you want to get your news via NNTP from
96 \"flab.flab.edu\", you could say:
97
98 (setq gnus-select-method '(nntp \"flab.flab.edu\"))
99
100 If you want to use your local spool, say:
101
102 (setq gnus-select-method (list 'nnspool (system-name)))
103
104 If you use this variable, you must set `gnus-nntp-server' to nil.
105
106 There is a lot more to know about select methods and virtual servers -
107 see the manual for details.")
108
109 ;; Added by Sudish Joseph <joseph@cis.ohio-state.edu>.
110 (defvar gnus-post-method nil
111   "*Preferred method for posting USENET news.
112 If this variable is nil, Gnus will use the current method to decide
113 which method to use when posting.  If it is non-nil, it will override
114 the current method.  This method will not be used in mail groups and
115 the like, only in \"real\" newsgroups.
116
117 The value must be a valid method as discussed in the documentation of
118 `gnus-select-method'.")
119
120 (defvar gnus-refer-article-method nil
121   "*Preferred method for fetching an article by Message-ID.
122 If you are reading news from the local spool (with nnspool), fetching
123 articles by Message-ID is painfully slow. By setting this method to an
124 nntp method, you might get acceptable results.
125
126 The value of this variable must be a valid select method as discussed
127 in the documentation of `gnus-select-method'")
128
129 (defvar gnus-secondary-select-methods nil
130   "*A list of secondary methods that will be used for reading news.
131 This is a list where each element is a complete select method (see
132 `gnus-select-method').  
133
134 If, for instance, you want to read your mail with the nnml backend,
135 you could set this variable:
136
137 (setq gnus-secondary-select-methods '((nnml \"\"))")
138
139 (defvar gnus-secondary-servers nil
140   "*List of NNTP servers that the user can choose between interactively.
141 To make Gnus query you for a server, you have to give `gnus' a
142 non-numeric prefix - `C-u M-x gnus', in short.")
143
144 (defvar gnus-nntp-server nil
145   "*The name of the host running the NNTP server.
146 This variable is semi-obsolete. Use the `gnus-select-method'
147 variable instead.")
148
149 (defvar gnus-nntp-service "nntp"
150   "*NNTP service name (\"nntp\" or 119).
151 This is an obsolete variable, which is scarcely used. If you use an
152 nntp server for your newsgroup and want to change the port number
153 used to 899, you would say something along these lines:
154
155  (setq gnus-select-method '(nntp \"my.nntp.server\" (nntp-port-number 899)))")
156
157 (defvar gnus-startup-file "~/.newsrc"
158   "*Your `.newsrc' file.
159 `.newsrc-SERVER' will be used instead if that exists.")
160
161 (defvar gnus-init-file "~/.gnus"
162   "*Your Gnus elisp startup file.
163 If a file with the .el or .elc suffixes exist, it will be read
164 instead.") 
165
166 (defvar gnus-group-faq-directory
167   "/anonymous@rtfm.mit.edu:/pub/usenet-by-group/"
168   "*Directory where the group FAQs are stored.
169 This will most commonly be on a remote machine, and the file will be
170 fetched by ange-ftp.")
171
172 (defvar gnus-group-archive-directory
173   "/ftp@sina.tcamc.uh.edu:/pub/emacs/ding-list/" 
174   "*The address of the (ding) archives.")
175
176 (defvar gnus-default-subscribed-newsgroups nil
177   "*This variable lists what newsgroups should be subscribed the first time Gnus is used.
178 It should be a list of strings.
179 If it is `t', Gnus will not do anything special the first time it is
180 started; it'll just use the normal newsgroups subscription methods.")
181
182 (defvar gnus-use-cross-reference t
183   "*Non-nil means that cross referenced articles will be marked as read.
184 If nil, ignore cross references.  If t, mark articles as read in
185 subscribed newsgroups. If neither t nor nil, mark as read in all
186 newsgroups.") 
187
188 (defvar gnus-use-dribble-file t
189   "*Non-nil means that Gnus will use a dribble file to store user updates.
190 If Emacs should crash without saving the .newsrc files, complete
191 information can be restored from the dribble file.")
192
193 (defvar gnus-asynchronous nil
194   "*If non-nil, Gnus will supply backends with data needed for async article fetching.")
195
196 (defvar gnus-asynchronous-article-function nil
197   "*Function for picking articles to pre-fetch, possibly.")
198
199 (defvar gnus-score-file-single-match-alist nil
200   "*Alist mapping regexps to lists of score files.
201 Each element of this alist should be of the form
202         (\"REGEXP\" [ \"SCORE-FILE-1\" ] [ \"SCORE-FILE-2\" ] ... )
203
204 If the name of a group is matched by REGEXP, the corresponding scorefiles
205 will be used for that group.
206 The first match found is used, subsequent matching entries are ignored (to
207 use multiple matches, see gnus-score-file-multiple-match-alist).
208
209 These score files are loaded in addition to any files returned by
210 gnus-score-find-score-files-function (which see).")
211
212 (defvar gnus-score-file-multiple-match-alist nil
213   "*Alist mapping regexps to lists of score files.
214 Each element of this alist should be of the form
215         (\"REGEXP\" [ \"SCORE-FILE-1\" ] [ \"SCORE-FILE-2\" ] ... )
216
217 If the name of a group is matched by REGEXP, the corresponding scorefiles
218 will be used for that group.
219 If multiple REGEXPs match a group, the score files corresponding to each
220 match will be used (for only one match to be used, see
221 gnus-score-file-single-match-alist).
222
223 These score files are loaded in addition to any files returned by
224 gnus-score-find-score-files-function (which see).")
225
226
227 (defvar gnus-score-file-suffix "SCORE"
228   "*Suffix of the score files.")
229
230 (defvar gnus-adaptive-file-suffix "ADAPT"
231   "*Suffix of the adaptive score files.")
232
233 (defvar gnus-score-find-score-files-function 'gnus-score-find-bnews
234   "*Function used to find SCORE files.
235 The function will be called with the group name as the argument, and
236 should return a list of score files to apply to that group.  The score
237 files do not actually have to exist.
238
239 Predefined values are:
240
241 gnus-score-find-single: Only apply the group's own SCORE file.
242 gnus-score-find-hierarchical: Also apply SCORE files from parent groups.
243 gnus-score-find-bnews: Apply SCORE files whose names matches.
244
245 See the documentation to these functions for more information.
246
247 This variable can also be a list of functions to be called.  Each
248 function should either return a list of score files, or a list of
249 score alists.")
250
251 (defvar gnus-score-interactive-default-score 1000
252   "*Scoring commands will raise/lower the score with this number as the default.")
253
254 (defvar gnus-large-newsgroup 200
255   "*The number of articles which indicates a large newsgroup.
256 If the number of articles in a newsgroup is greater than this value,
257 confirmation is required for selecting the newsgroup.")
258
259 ;; Suggested by Andrew Eskilsson <pi92ae@lelle.pt.hk-r.se>.
260 (defvar gnus-no-groups-message "No news is horrible news"
261   "*Message displayed by Gnus when no groups are available.")
262
263 (defvar gnus-use-long-file-name (not (memq system-type '(usg-unix-v xenix)))
264   "*Non-nil means that the default name of a file to save articles in is the group name.
265 If it's nil, the directory form of the group name is used instead.
266
267 If this variable is a list, and the list contains the element
268 `not-score', long file names will not be used for score files; if it
269 contains the element `not-save', long file names will not be used for
270 saving; and if it contains the element `not-kill', long file names
271 will not be used for kill files.")
272
273 (defvar gnus-article-save-directory (or (getenv "SAVEDIR") "~/News/")
274   "*Name of the directory articles will be saved in (default \"~/News\").
275 Initialized from the SAVEDIR environment variable.")
276
277 (defvar gnus-kill-files-directory (or (getenv "SAVEDIR") "~/News/")
278   "*Name of the directory where kill files will be stored (default \"~/News\").
279 Initialized from the SAVEDIR environment variable.")
280
281 (defvar gnus-default-article-saver 'gnus-summary-save-in-rmail
282   "*A function to save articles in your favorite format.
283 The function must be interactively callable (in other words, it must
284 be an Emacs command).
285
286 Gnus provides the following functions:
287
288 * gnus-summary-save-in-rmail (Rmail format)
289 * gnus-summary-save-in-mail (Unix mail format)
290 * gnus-summary-save-in-folder (MH folder)
291 * gnus-summary-save-in-file (article format).
292 * gnus-summary-save-in-vm (use VM's folder format).")
293
294 (defvar gnus-rmail-save-name (function gnus-plain-save-name)
295   "*A function generating a file name to save articles in Rmail format.
296 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
297
298 (defvar gnus-mail-save-name (function gnus-plain-save-name)
299   "*A function generating a file name to save articles in Unix mail format.
300 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
301
302 (defvar gnus-folder-save-name (function gnus-folder-save-name)
303   "*A function generating a file name to save articles in MH folder.
304 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER.")
305
306 (defvar gnus-file-save-name (function gnus-numeric-save-name)
307   "*A function generating a file name to save articles in article format.
308 The function is called with NEWSGROUP, HEADERS, and optional
309 LAST-FILE.")
310
311 (defvar gnus-split-methods nil
312   "*Variable used to suggest where articles are to be saved.
313 The syntax of this variable is the same as `nnmail-split-methods'.  
314
315 For instance, if you would like to save articles related to Gnus in
316 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
317 you could set this variable to something like:
318
319  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
320    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))")
321
322 (defvar gnus-save-score nil
323   "*If non-nil, save group scoring info.")
324
325 (defvar gnus-use-adaptive-scoring nil
326   "*If non-nil, use some adaptive scoring scheme.")
327
328 (defvar gnus-use-cache nil
329   "*If non-nil, Gnus will cache (some) articles locally.")
330
331 (defvar gnus-use-scoring t
332   "*If non-nil, enable scoring.")
333
334 (defvar gnus-fetch-old-headers nil
335   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
336 If an unread article in the group refers to an older, already read (or
337 just marked as read) article, the old article will not normally be
338 displayed in the Summary buffer.  If this variable is non-nil, Gnus
339 will attempt to grab the headers to the old articles, and thereby
340 build complete threads.  If it has the value `some', only enough
341 headers to connect otherwise loose threads will be displayed.
342
343 The server has to support XOVER for any of this to work.")
344
345 (defvar gnus-visual t
346   "*If non-nil, will do various highlighting.
347 If nil, no mouse highlights (or any other highlights) will be
348 performed.  This might speed up Gnus some when generating large group
349 and summary buffers.")
350
351 (defvar gnus-novice-user t
352   "*Non-nil means that you are a usenet novice.
353 If non-nil, verbose messages may be displayed and confirmations may be
354 required.")
355
356 (defvar gnus-expert-user nil
357   "*Non-nil means that you will never be asked for confirmation about anything.
358 And that means *anything*.")
359
360 (defvar gnus-verbose 7
361   "*Integer that says how verbose Gnus should be.
362 The higher the number, the more messages Gnus will flash to say what
363 it's doing.  At zero, Gnus will be totally mute; at five, Gnus will
364 display most important messages; and at ten, Gnus will keep on
365 jabbering all the time.")
366
367 (defvar gnus-keep-same-level nil
368   "*Non-nil means that the next newsgroup after the current will be on the same level.
369 When you type, for instance, `n' after reading the last article in the
370 current newsgroup, you will go to the next newsgroup. If this variable
371 is nil, the next newsgroup will be the next from the group
372 buffer. 
373 If this variable is non-nil, Gnus will either put you in the
374 next newsgroup with the same level, or, if no such newsgroup is
375 available, the next newsgroup with the lowest possible level higher
376 than the current level.
377 If this variable is `best', Gnus will make the next newsgroup the one
378 with the best level.")
379
380 (defvar gnus-summary-make-false-root 'adopt
381   "*nil means that Gnus won't gather loose threads.
382 If the root of a thread has expired or been read in a previous
383 session, the information necessary to build a complete thread has been
384 lost. Instead of having many small sub-threads from this original thread
385 scattered all over the summary buffer, Gnus can gather them. 
386
387 If non-nil, Gnus will try to gather all loose sub-threads from an
388 original thread into one large thread.
389
390 If this variable is non-nil, it should be one of `none', `adopt',
391 `dummy' or `empty'.
392
393 If this variable is `none', Gnus will not make a false root, but just
394 present the sub-threads after another.
395 If this variable is `dummy', Gnus will create a dummy root that will
396 have all the sub-threads as children.
397 If this variable is `adopt', Gnus will make one of the \"children\"
398 the parent and mark all the step-children as such.
399 If this variable is `empty', the \"children\" are printed with empty
400 subject fields.  (Or rather, they will be printed with a string
401 given by the `gnus-summary-same-subject' variable.)")
402
403 (defvar gnus-summary-gather-subject-limit nil
404   "*Maximum length of subject comparisons when gathering loose threads.
405 Use nil to compare full subjects.  Setting this variable to a low
406 number will help gather threads that have been corrupted by
407 newsreaders chopping off subject lines, but it might also mean that
408 unrelated articles that have subject that happen to begin with the
409 same few characters will be incorrectly gathered.
410
411 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
412 comparing subjects.")
413
414 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
415 (defvar gnus-summary-same-subject ""
416   "*String indicating that the current article has the same subject as the previous.
417 This variable will only be used if the value of
418 `gnus-summary-make-false-root' is `empty'.")
419
420 (defvar gnus-summary-goto-unread nil
421   "*If non-nil, marking commands will go to the next unread article.")
422
423 (defvar gnus-group-goto-unread t
424   "*If non-nil, movement commands will go to the next unread and subscribed group.")
425
426 (defvar gnus-check-new-newsgroups t
427   "*Non-nil means that Gnus will add new newsgroups at startup.
428 If this variable is `ask-server', Gnus will ask the server for new
429 groups since the last time it checked. This means that the killed list
430 is no longer necessary, so you could set `gnus-save-killed-list' to
431 nil. 
432
433 A variant is to have this variable be a list of select methods. Gnus
434 will then use the `ask-server' method on all these select methods to
435 query for new groups from all those servers.
436
437 Eg.
438   (setq gnus-check-new-newsgroups 
439         '((nntp \"some.server\") (nntp \"other.server\")))
440
441 If this variable is nil, then you have to tell Gnus explicitly to
442 check for new newsgroups with \\<gnus-group-mode-map>\\[gnus-find-new-newsgroups].")
443
444 (defvar gnus-check-bogus-newsgroups nil
445   "*Non-nil means that Gnus will check and remove bogus newsgroup at startup.
446 If this variable is nil, then you have to tell Gnus explicitly to
447 check for bogus newsgroups with \\<gnus-group-mode-map>\\[gnus-group-check-bogus-groups].")
448
449 (defvar gnus-read-active-file t
450   "*Non-nil means that Gnus will read the entire active file at startup.
451 If this variable is nil, Gnus will only know about the groups in your
452 `.newsrc' file.
453
454 If this variable is `some', Gnus will try to only read the relevant
455 parts of the active file from the server.  Not all servers support
456 this, and it might be quite slow with other servers, but this should
457 generally be faster than both the t and nil value.
458
459 If you set this variable to nil or `some', you probably still want to
460 be told about new newsgroups that arrive.  To do that, set
461 `gnus-check-new-newsgroups' to `ask-server'.  This may not work
462 properly with all servers.")
463
464 (defvar gnus-level-subscribed 5
465   "*Groups with levels less than or equal to this variable are subscribed.")
466
467 (defvar gnus-level-unsubscribed 7
468   "*Groups with levels less than or equal to this variable are unsubscribed.
469 Groups with levels less than `gnus-level-subscribed', which should be
470 less than this variable, are subscribed.")
471
472 (defvar gnus-level-zombie 8
473   "*Groups with this level are zombie groups.")
474
475 (defvar gnus-level-killed 9
476   "*Groups with this level are killed.")
477
478 (defvar gnus-level-default-subscribed 3
479   "*New subscribed groups will be subscribed at this level.")
480
481 (defvar gnus-level-default-unsubscribed 6
482   "*New unsubscribed groups will be unsubscribed at this level.")
483
484 (defvar gnus-activate-foreign-newsgroups nil
485   "*If nil, Gnus will not check foreign newsgroups at startup.
486 If it is non-nil, it should be a number between one and nine. Foreign
487 newsgroups that have a level lower or equal to this number will be
488 activated on startup. For instance, if you want to active all
489 subscribed newsgroups, but not the rest, you'd set this variable to 
490 `gnus-level-subscribed'.
491
492 If you subscribe to lots of newsgroups from different servers, startup
493 might take a while. By setting this variable to nil, you'll save time,
494 but you won't be told how many unread articles there are in the
495 groups.")
496
497 (defvar gnus-save-newsrc-file t
498   "*Non-nil means that Gnus will save the `.newsrc' file.
499 Gnus always saves its own startup file, which is called
500 \".newsrc.eld\".  The file called \".newsrc\" is in a format that can
501 be readily understood by other newsreaders.  If you don't plan on
502 using other newsreaders, set this variable to nil to save some time on
503 exit.")
504
505 (defvar gnus-save-killed-list t
506   "*If non-nil, save the list of killed groups to the startup file.
507 This will save both time (when starting and quitting) and space (both
508 memory and disk), but it will also mean that Gnus has no record of
509 which groups are new and which are old, so the automatic new
510 newsgroups subscription methods become meaningless. You should always
511 set `gnus-check-new-newsgroups' to `ask-server' or nil if you set this
512 variable to nil.")
513
514 (defvar gnus-interactive-catchup t
515   "*If non-nil, require your confirmation when catching up a group.")
516
517 (defvar gnus-interactive-post t
518   "*If non-nil, group name will be asked for when posting.")
519
520 (defvar gnus-interactive-exit t
521   "*If non-nil, require your confirmation when exiting Gnus.")
522
523 (defvar gnus-kill-killed nil
524   "*If non-nil, Gnus will apply kill files to already killed articles.
525 If it is nil, Gnus will never apply kill files to articles that have
526 already been through the scoring process, which might very well save lots
527 of time.")
528
529 (defvar gnus-extract-address-components 'gnus-extract-address-components
530   "*Function for extracting address components from a From header.
531 Two pre-defined function exist: `gnus-extract-address-components',
532 which is the default, quite fast, and too simplistic solution, and
533 `mail-extract-address-components', which works much better, but is
534 slower.")
535
536 (defvar gnus-summary-default-score 0
537   "*Default article score level.
538 If this variable is nil, scoring will be disabled.")
539
540 (defvar gnus-summary-zcore-fuzz 0
541   "*Fuzziness factor for the zcore in the summary buffer.
542 Articles with scores closer than this to `gnus-summary-default-score'
543 will not be marked.")
544
545 (defvar gnus-group-default-list-level gnus-level-subscribed
546   "*Default listing level. 
547 Ignored if `gnus-group-use-permanent-levels' is nil.")
548
549 (defvar gnus-group-use-permanent-levels nil
550   "*If non-nil, once you set a level, Gnus will use this level.")
551
552 (defvar gnus-show-mime nil
553   "*If non-ni, do mime processing of articles.
554 The articles will simply be fed to the function given by
555 `gnus-show-mime-method'.")
556
557 (defvar gnus-strict-mime t
558   "*If nil, decode MIME header even if there is not Mime-Version field.")
559  
560 (defvar gnus-show-mime-method (function metamail-buffer)
561   "*Function to process a MIME message.
562 The function is called from the article buffer.")
563
564 (defvar gnus-show-threads t
565   "*If non-nil, display threads in summary mode.")
566
567 (defvar gnus-thread-hide-subtree nil
568   "*If non-nil, hide all threads initially.
569 If threads are hidden, you have to run the command
570 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
571 to expose hidden threads.")
572
573 (defvar gnus-thread-hide-killed t
574   "*If non-nil, hide killed threads automatically.")
575
576 (defvar gnus-thread-ignore-subject nil
577   "*If non-nil, ignore subjects and do all threading based on the Reference header.
578 If nil, which is the default, articles that have different subjects
579 from their parents will start separate threads.")
580
581 (defvar gnus-thread-indent-level 4
582   "*Number that says how much each sub-thread should be indented.")
583
584 (defvar gnus-ignored-newsgroups ""
585   "*A regexp to match uninteresting newsgroups in the active file.
586 Any lines in the active file matching this regular expression are
587 removed from the newsgroup list before anything else is done to it,
588 thus making them effectively non-existent.")
589
590 (defvar gnus-ignored-headers
591   "^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:"
592   "*All headers that match this regexp will be hidden.
593 Also see `gnus-visible-headers'.")
594
595 (defvar gnus-visible-headers "^From:\\|^Newsgroups:\\|^Subject:\\|^Date:\\|^Followup-To:\\|^Reply-To:\\|^Organization:\\|^Summary:\\|^Keywords:\\|^To:\\|^Cc:"
596   "*All headers that do not match this regexp will be hidden.
597 Also see `gnus-ignored-headers'.")
598
599 (defvar gnus-sorted-header-list
600   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:" "^To:" 
601     "^Cc:" "^Date:" "^Organization:")
602   "*This variable is a list of regular expressions.
603 If it is non-nil, headers that match the regular expressions will
604 be placed first in the article buffer in the sequence specified by
605 this list.")
606
607 (defvar gnus-show-all-headers nil
608   "*If non-nil, don't hide any headers.")
609
610 (defvar gnus-save-all-headers t
611   "*If non-nil, don't remove any headers before saving.")
612
613 (defvar gnus-inhibit-startup-message nil
614   "*If non-nil, the startup message will not be displayed.")
615
616 (defvar gnus-signature-separator "^-- *$"
617   "Regexp matching signature separator.")
618
619 (defvar gnus-auto-extend-newsgroup t
620   "*If non-nil, extend newsgroup forward and backward when requested.")
621
622 (defvar gnus-auto-select-first t
623   "*If non-nil, select the first unread article when entering a group.
624 If you want to prevent automatic selection of the first unread article
625 in some newsgroups, set the variable to nil in
626 `gnus-select-group-hook'.") 
627
628 (defvar gnus-auto-select-next t
629   "*If non-nil, offer to go to the next group from the end of the previous.
630 If the value is t and the next newsgroup is empty, Gnus will exit
631 summary mode and go back to group mode.  If the value is neither nil
632 nor t, Gnus will select the following unread newsgroup.  In
633 particular, if the value is the symbol `quietly', the next unread
634 newsgroup will be selected without any confirmations.")
635
636 (defvar gnus-auto-select-same nil
637   "*If non-nil, select the next article with the same subject.")
638
639 (defvar gnus-summary-check-current nil
640   "*If non-nil, consider the current article when moving.
641 The \"unread\" movement commands will stay on the same line if the
642 current article is unread.")
643
644 (defvar gnus-auto-center-summary t
645   "*If non-nil, always center the current summary buffer.")
646
647 (defvar gnus-break-pages t
648   "*If non-nil, do page breaking on articles.
649 The page delimiter is specified by the `gnus-page-delimiter'
650 variable.")
651
652 (defvar gnus-page-delimiter "^\^L"
653   "*Regexp describing what to use as article page delimiters.
654 The default value is \"^\^L\", which is a form linefeed at the
655 beginning of a line.")
656
657 (defvar gnus-use-full-window t
658   "*If non-nil, use the entire Emacs screen.")
659
660 (defvar gnus-window-configuration nil
661   "Obsolete variable.  See `gnus-buffer-configuration'.")
662
663 (defvar gnus-buffer-configuration
664   '((group ([group 1.0 point] 
665             (if gnus-carpal [group-carpal 4])))
666     (summary ([summary 1.0 point]
667               (if gnus-carpal [summary-carpal 4])))
668     (article ([summary 0.25 point] 
669               (if gnus-carpal [summary-carpal 4]) 
670               [article 1.0]))
671     (server ([server 1.0 point]
672              (if gnus-carpal [server-carpal 2])))
673     (browse ([browse 1.0 point]
674              (if gnus-carpal [browse-carpal 2])))
675     (group-mail ([mail 1.0 point]))
676     (summary-mail ([mail 1.0 point]))
677     (summary-reply ([article 0.5]
678                     [mail 1.0 point]))
679     (info ([nil 1.0 point]))
680     (summary-faq ([summary 0.25]
681                   [article 1.0 point]))
682     (edit-group ([group 0.5]
683                  [edit-group 1.0 point]))
684     (edit-server ([server 0.5]
685                   [edit-server 1.0 point]))
686     (post ([post 1.0 point]))
687     (reply ([article 0.5]
688             [mail 1.0 point]))
689     (mail-forward ([mail 1.0 point]))
690     (post-forward ([post 1.0 point]))
691     (reply-yank ([mail 1.0 point]))
692     (followup ([article 0.5]
693                [post 1.0 point]))
694     (followup-yank ([post 1.0 point])))
695   "Window configuration for all possible Gnus buffers.
696 This variable is a list of lists.  Each of these lists has a NAME and
697 a RULE.  The NAMEs are commonsense names like `group', which names a
698 rule used when displaying the group buffer; `summary', which names a
699 rule for what happens when you enter a group and do not display an
700 article buffer; and so on.  See the value of this variable for a
701 complete list of NAMEs.
702
703 Each RULE is a list of vectors.  The first element in this vector is
704 the name of the buffer to be displayed; the second element is the
705 percentage of the screen this buffer is to occupy (a number in the
706 0.0-0.99 range); the optional third element is `point', which should
707 be present to denote which buffer point is to go to after making this
708 buffer configuration.")
709
710 (defvar gnus-window-to-buffer
711   '((group . gnus-group-buffer)
712     (summary . gnus-summary-buffer)
713     (article . gnus-article-buffer)
714     (server . gnus-server-buffer)
715     (browse . "*Gnus Browse Server*")
716     (edit-group . gnus-group-edit-buffer)
717     (edit-server . gnus-server-edit-buffer)
718     (group-carpal . gnus-carpal-group-buffer)
719     (summary-carpal . gnus-carpal-summary-buffer)
720     (server-carpal . gnus-carpal-server-buffer)
721     (browse-carpal . gnus-carpal-browse-buffer)
722     (mail . "*mail*")
723     (post . gnus-post-news-buffer))
724   "Mapping from short symbols to buffer names or buffer variables.")
725
726 (defvar gnus-carpal nil
727   "*If non-nil, display clickable icons.")
728
729 (defvar gnus-subscribe-newsgroup-method 'gnus-subscribe-zombies
730   "*Function called with a group name when new group is detected.
731 A few pre-made functions are supplied: `gnus-subscribe-randomly'
732 inserts new groups at the beginning of the list of groups;
733 `gnus-subscribe-alphabetically' inserts new groups in strict
734 alphabetic order; `gnus-subscribe-hierarchically' inserts new groups
735 in hierarchical newsgroup order; `gnus-subscribe-interactively' asks
736 for your decision.")
737
738 ;; Suggested by a bug report by Hallvard B Furuseth.
739 ;; <h.b.furuseth@usit.uio.no>. 
740 (defvar gnus-subscribe-options-newsgroup-method
741   (function gnus-subscribe-alphabetically)
742   "*This function is called to subscribe newsgroups mentioned on \"options -n\" lines.
743 If, for instance, you want to subscribe to all newsgroups in the
744 \"no\" and \"alt\" hierarchies, you'd put the following in your
745 .newsrc file:
746
747 options -n no.all alt.all
748
749 Gnus will the subscribe all new newsgroups in these hierarchies with
750 the subscription method in this variable.")
751
752 (defvar gnus-subscribe-hierarchical-interactive nil
753   "*If non-nil, Gnus will offer to subscribe hierarchically.
754 When a new hierarchy appears, Gnus will ask the user:
755
756 'alt.binaries': Do you want to subscribe to this hierarchy? ([d]ys):
757
758 If the user pressed `d', Gnus will descend the hierarchy, `y' will
759 subscribe to all newsgroups in the hierarchy and `s' will skip this
760 hierarchy in its entirety.")
761
762 (defvar gnus-group-sort-function 'gnus-group-sort-by-alphabet
763   "*Function used for sorting the group buffer.
764 This function will be called with group info entries as the arguments
765 for the groups to be sorted.  Pre-made functions include
766 `gnus-sort-by-alphabet', `gnus-sort-by-unread' and
767 `gnus-sort-by-level'")
768
769 ;; Mark variables suggested by Thomas Michanek
770 ;; <Thomas.Michanek@telelogic.se>. 
771 (defvar gnus-unread-mark ? 
772   "*Mark used for unread articles.")
773 (defvar gnus-ticked-mark ?!
774   "*Mark used for ticked articles.")
775 (defvar gnus-dormant-mark ??
776   "*Mark used for dormant articles.")
777 (defvar gnus-del-mark ?D
778   "*Mark used for del'd articles.")
779 (defvar gnus-read-mark ?d
780   "*Mark used for read articles.")
781 (defvar gnus-expirable-mark ?E
782   "*Mark used for expirable articles.")
783 (defvar gnus-killed-mark ?K
784   "*Mark used for killed articles.")
785 (defvar gnus-kill-file-mark ?X
786   "*Mark used for articles killed by kill files.")
787 (defvar gnus-low-score-mark ?Y
788   "*Mark used for articles with a low score.")
789 (defvar gnus-catchup-mark ?C
790   "*Mark used for articles that are caught up.")
791 (defvar gnus-replied-mark ?R
792   "*Mark used for articles that have been replied to.")
793 (defvar gnus-process-mark ?# 
794   "*Process mark.")
795 (defvar gnus-ancient-mark ?A
796   "*Mark used for ancient articles.")
797 (defvar gnus-canceled-mark ?G
798   "*Mark used for cancelled articles.")
799 (defvar gnus-score-over-mark ?+
800   "*Score mark used for articles with high scores.")
801 (defvar gnus-score-below-mark ?-
802   "*Score mark used for articles with low scores.")
803 (defvar gnus-empty-thread-mark ? 
804   "*There is no thread under the article.")
805 (defvar gnus-not-empty-thread-mark ?=
806   "*There is a thread under the article.")
807 (defvar gnus-dummy-mark ?Z
808   "*This is a dummy article.")
809
810 (defvar gnus-view-pseudo-asynchronously nil
811   "*If non-nil, Gnus will view pseudo-articles asynchronously.")
812
813 (defvar gnus-view-pseudos nil
814   "*If `automatic', pseudo-articles will be viewed automatically.
815 If `not-confirm', pseudos will be viewed automatically, and the user
816 will not be asked to confirm the command.")
817
818 (defvar gnus-view-pseudos-separately t
819   "*If non-nil, one pseudo-article will be created for each file to be viewed.
820 If nil, all files that use the same viewing command will be given as a
821 list of parameters to that command.")
822
823 (defvar gnus-group-line-format "%M%S%p%5y: %(%g%)\n"
824   "*Format of group lines.
825 It works along the same lines as a normal formatting string,
826 with some simple extensions.
827
828 %M    Only marked articles (character, \"*\" or \" \")
829 %S    Whether the group is subscribed (character, \"U\", \"K\", \"Z\" or \" \")
830 %L    Level of subscribedness (integer)
831 %N    Number of unread articles (integer)
832 %I    Number of dormant articles (integer)
833 %i    Number of ticked and dormant (integer)
834 %T    Number of ticked articles (integer)
835 %R    Number of read articles (integer)
836 %t    Total number of articles (integer)
837 %y    Number of unread, unticked articles (integer)
838 %G    Group name (string)
839 %g    Qualified group name (string)
840 %D    Group description (string)
841 %s    Select method (string)
842 %o    Moderated group (char, \"m\")
843 %p    Process mark (char)
844 %O    Moderated group (string, \"(m)\" or \"\")
845 %n    Select from where (string)
846 %z    A string that look like `<%s:%n>' if a foreign select method is used
847 %u    User defined specifier. The next character in the format string should
848       be a letter.  Gnus will call the function gnus-user-format-function-X,
849       where X is the letter following %u. The function will be passed the
850       current header as argument. The function should return a string, which
851       will be inserted into the buffer just like information from any other
852       group specifier.
853
854 Text between %( and %) will be highlighted with `gnus-mouse-face' when
855 the mouse point move inside the area.  There can only be one such area.
856
857 Note that this format specification is not always respected. For
858 reasons of efficiency, when listing killed groups, this specification
859 is ignored altogether. If the spec is changed considerably, your
860 output may end up looking strange when listing both alive and killed
861 groups.
862
863 If you use %o or %O, reading the active file will be slower and quite
864 a bit of extra memory will be used. %D will also worsen performance.
865 Also note that if you change the format specification to include any
866 of these specs, you must probably re-start Gnus to see them go into
867 effect.") 
868
869 (defvar gnus-summary-line-format "%U%R%z%I%(%[%4L: %-20,20n%]%) %s\n"
870   "*The format specification of the lines in the summary buffer.
871
872 It works along the same lines as a normal formatting string,
873 with some simple extensions.
874
875 %N   Article number, left padded with spaces (string)
876 %S   Subject (string)
877 %s   Subject if it is at the root of a thread, and \"\" otherwise (string)
878 %n   Name of the poster (string)
879 %A   Address of the poster (string)
880 %F   Contents of the From: header (string)
881 %x   Contents of the Xref: header (string)
882 %D   Date of the article (string)
883 %d   Date of the article (string) in DD-MMM format
884 %M   Message-id of the article (string)
885 %r   References of the article (string)
886 %c   Number of characters in the article (integer)
887 %L   Number of lines in the article (integer)
888 %I   Indentation based on thread level (a string of spaces)
889 %T   A string with two possible values: 80 spaces if the article
890      is on thread level two or larger and 0 spaces on level one
891 %R   \"R\" if this article has been replied to, \" \" otherwise (character)
892 %U   Status of this article (character, \"D\", \"K\", \"-\" or \" \")
893 %[   Opening bracket (character, \"[\" or \"<\")
894 %]   Closing bracket (character, \"]\" or \">\")
895 %>   Spaces of length thread-level (string)
896 %<   Spaces of length (- 20 thread-level) (string)
897 %i   Article score (number)
898 %z   Article zcore (character)
899 %t   Number of articles under the current thread (number).
900 %e   Whether the thread is empty or not (character).
901 %u   User defined specifier. The next character in the format string should
902      be a letter.  Gnus will call the function gnus-user-format-function-X,
903      where X is the letter following %u. The function will be passed the
904      current header as argument. The function should return a string, which
905      will be inserted into the summary just like information from any other
906      summary specifier.
907
908 Text between %( and %) will be highlighted with `gnus-mouse-face'
909 when the mouse point is placed inside the area.  There can only be one
910 such area.
911
912 The %U (status), %R (replied) and %z (zcore) specs have to be handled
913 with care. For reasons of efficiency, Gnus will compute what column
914 these characters will end up in, and \"hard-code\" that. This means that
915 it is illegal to have these specs after a variable-length spec. Well,
916 you might not be arrested, but your summary buffer will look strange,
917 which is bad enough.
918
919 The smart choice is to have these specs as for to the left as
920 possible. 
921
922 This restriction may disappear in later versions of Gnus.")
923
924 (defvar gnus-summary-dummy-line-format "*  :                          : %S\n"
925   "*The format specification for the dummy roots in the summary buffer.
926 It works along the same lines as a normal formatting string,
927 with some simple extensions.
928
929 %S  The subject")
930
931 (defvar gnus-summary-mode-line-format "(ding) %G/%A %Z"
932   "*The format specification for the summary mode line.")
933
934 (defvar gnus-article-mode-line-format "(ding) %G/%A %S"
935   "*The format specification for the article mode line.")
936
937 (defvar gnus-group-mode-line-format "(ding) List of groups   {%M:%S}  "
938   "*The format specification for the group mode line.")
939
940 (defvar gnus-valid-select-methods
941   '(("nntp" post address prompt-address)
942     ("nnspool" post)
943     ("nnvirtual" none virtual prompt-address) 
944     ("nnmbox" mail respool) 
945     ("nnml" mail respool)
946     ("nnmh" mail respool) 
947     ("nndir" none prompt-address address)
948     ("nneething" none prompt-address)
949     ("nndigest" none) 
950     ("nndoc" none prompt-address) 
951     ("nnbabyl" mail respool) 
952     ("nnkiboze" post virtual) 
953     ("nnsoup" post)
954     ("nnfolder" mail respool))
955   "An alist of valid select methods.
956 The first element of each list lists should be a string with the name
957 of the select method. The other elements may be be the category of
958 this method (ie. `post', `mail', `none' or whatever) or other
959 properties that this method has (like being respoolable).
960 If you implement a new select method, all you should have to change is
961 this variable. I think.")
962
963 (defvar gnus-updated-mode-lines '(group article summary)
964   "*List of buffers that should update their mode lines.
965 The list may contain the symbols `group', `article' and `summary'. If
966 the corresponding symbol is present, Gnus will keep that mode line
967 updated with information that may be pertinent. 
968 If this variable is nil, screen refresh may be quicker.")
969
970 ;; Added by Keinonen Kari <kk85613@cs.tut.fi>.
971 (defvar gnus-mode-non-string-length 21
972   "*Max length of mode-line non-string contents.
973 If this is nil, Gnus will take space as is needed, leaving the rest
974 of the modeline intact.")
975
976 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
977 (defvar gnus-mouse-face 'highlight
978   "*Face used for mouse highlighting in Gnus.
979 No mouse highlights will be done if `gnus-visual' is nil.")
980
981 (defvar gnus-summary-mark-below nil
982   "*Mark all articles with a score below this variable as read.
983 This variable is local to each summary buffer and usually set by the
984 score file.")  
985
986 (defvar gnus-thread-sort-functions '(gnus-thread-sort-by-number)
987   "*List of functions used for sorting threads in the summary buffer.
988 By default, threads are sorted by article number.
989
990 Each function takes two threads and return non-nil if the first thread
991 should be sorted before the other.  If you use more than one function,
992 the primary sort function should be the last.
993
994 Ready-mady functions include `gnus-thread-sort-by-number',
995 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
996 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
997 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').")
998
999 (defvar gnus-thread-score-function '+
1000   "*Function used for calculating the total score of a thread.
1001
1002 The function is called with the scores of the article and each
1003 subthread and should then return the score of the thread.
1004
1005 Some functions you can use are `+', `max', or `min'.")
1006
1007 (defvar gnus-options-subscribe nil
1008   "*All new groups matching this regexp will be subscribed unconditionally.
1009 Note that this variable deals only with new newsgroups.  This variable
1010 does not affect old newsgroups.")
1011
1012 (defvar gnus-options-not-subscribe nil
1013   "*All new groups matching this regexp will be ignored.
1014 Note that this variable deals only with new newsgroups.  This variable
1015 does not affect old (already subscribed) newsgroups.")
1016
1017 (defvar gnus-auto-expirable-newsgroups nil
1018   "*Groups in which to automatically mark read articles as expirable.
1019 If non-nil, this should be a regexp that should match all groups in
1020 which to perform auto-expiry.  This only makes sense for mail groups.")
1021
1022 (defvar gnus-hidden-properties '(invisible t intangible t)
1023   "Property list to use for hiding text.")
1024
1025 ;; Hooks.
1026
1027 (defvar gnus-group-mode-hook nil
1028   "*A hook for Gnus group mode.")
1029
1030 (defvar gnus-summary-mode-hook nil
1031   "*A hook for Gnus summary mode.")
1032
1033 (defvar gnus-article-mode-hook nil
1034   "*A hook for Gnus article mode.")
1035
1036 (defvar gnus-open-server-hook nil
1037   "*A hook called just before opening connection to the news server.")
1038
1039 (defvar gnus-startup-hook nil
1040   "*A hook called at startup.
1041 This hook is called after Gnus is connected to the NNTP server.")
1042
1043 (defvar gnus-get-new-news-hook nil
1044   "*A hook run just before Gnus checks for new news.")
1045
1046 (defvar gnus-group-prepare-function 'gnus-group-prepare-flat
1047   "*A function that is called to generate the group buffer.
1048 The function is called with three arguments: The first is a number;
1049 all group with a level less or equal to that number should be listed,
1050 if the second is non-nil, empty groups should also be displayed. If
1051 the third is non-nil, it is a number. No groups with a level lower
1052 than this number should be displayed.
1053
1054 The only current function implemented is `gnus-group-prepare-flat'.")
1055
1056 (defvar gnus-group-prepare-hook nil
1057   "*A hook called after the group buffer has been generated.
1058 If you want to modify the group buffer, you can use this hook.")
1059
1060 (defvar gnus-summary-prepare-hook nil
1061   "*A hook called after the summary buffer has been generated.
1062 If you want to modify the summary buffer, you can use this hook.")
1063
1064 (defvar gnus-article-prepare-hook nil
1065   "*A hook called after an article has been prepared in the article buffer.
1066 If you want to run a special decoding program like nkf, use this hook.")
1067
1068 (defvar gnus-article-display-hook nil
1069   "*A hook called after the article is displayed in the article buffer.
1070 The hook is designed to change the contents of the article
1071 buffer. Typical functions that this hook may contain are
1072 `gnus-article-hide-headers' (hide selected headers),
1073 `gnus-article-maybe-highlight' (perform fancy article highlighting), 
1074 `gnus-article-hide-signature' (hide signature) and
1075 `gnus-article-treat-overstrike' (turn \"^H_\" into bold characters).")
1076 (add-hook 'gnus-article-display-hook 'gnus-article-hide-headers-if-wanted)
1077 (add-hook 'gnus-article-display-hook 'gnus-article-treat-overstrike)
1078
1079 (defvar gnus-article-x-face-command
1080   "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | xv -quit -"
1081   "String or function to be executed to display an X-Face header.
1082 If it is a string, the command will be executed in a sub-shell
1083 asynchronously. The compressed face will be piped to this command.") 
1084
1085 (defvar gnus-article-x-face-too-ugly nil
1086   "Regexp matching posters whose face shouldn't be shown automatically.")
1087
1088 (defvar gnus-select-group-hook nil
1089   "*A hook called when a newsgroup is selected.
1090
1091 If you'd like to simplify subjects like the
1092 `gnus-summary-next-same-subject' command does, you can use the
1093 following hook:
1094
1095  (setq gnus-select-group-hook
1096       (list
1097         (lambda ()
1098           (mapcar (lambda (header)
1099                      (header-set-subject
1100                       header
1101                       (gnus-simplify-subject
1102                        (header-subject header) 're-only)))
1103                   gnus-newsgroup-headers))))")
1104
1105 (defvar gnus-select-article-hook
1106   '(gnus-summary-show-thread)
1107   "*A hook called when an article is selected.
1108 The default hook shows conversation thread subtrees of the selected
1109 article automatically using `gnus-summary-show-thread'.")
1110
1111 (defvar gnus-apply-kill-hook '(gnus-apply-kill-file)
1112   "*A hook called to apply kill files to a group.
1113 This hook is intended to apply a kill file to the selected newsgroup.
1114 The function `gnus-apply-kill-file' is called by default.
1115
1116 Since a general kill file is too heavy to use only for a few
1117 newsgroups, I recommend you to use a lighter hook function. For
1118 example, if you'd like to apply a kill file to articles which contains
1119 a string `rmgroup' in subject in newsgroup `control', you can use the
1120 following hook:
1121
1122 \(setq gnus-apply-kill-hook
1123       (list
1124         (lambda ()
1125           (cond ((string-match \"control\" gnus-newsgroup-name)
1126                  (gnus-kill \"Subject\" \"rmgroup\")
1127                  (gnus-expunge \"X\"))))))")
1128
1129 (defvar gnus-visual-mark-article-hook 
1130   (list 'gnus-highlight-selected-summary)
1131   "*Hook run after selecting an article in the summary buffer.
1132 It is meant to be used for highlighting the article in some way.  It
1133 is not run if `gnus-visual' is nil.")
1134
1135 (defvar gnus-prepare-article-hook (list 'gnus-inews-insert-signature)
1136   "*A hook called after preparing body, but before preparing header headers.
1137 The default hook (`gnus-inews-insert-signature') inserts a signature
1138 file specified by the variable `gnus-signature-file'.")
1139
1140 (defvar gnus-exit-group-hook nil
1141   "*A hook called when exiting (not quitting) summary mode.")
1142
1143 (defvar gnus-suspend-gnus-hook nil
1144   "*A hook called when suspending (not exiting) Gnus.")
1145
1146 (defvar gnus-exit-gnus-hook nil
1147   "*A hook called when exiting Gnus.")
1148
1149 (defvar gnus-save-newsrc-hook nil
1150   "*A hook called when saving the newsrc file.")
1151
1152 (defvar gnus-summary-update-hook 
1153   (list 'gnus-summary-highlight-line)
1154   "*A hook called when a summary line is changed.
1155 The hook will not be called if `gnus-visual' is nil.
1156
1157 The default function `gnus-summary-highlight-line' will
1158 highlight the line according to the `gnus-summary-highlight'
1159 variable.")
1160
1161 (defvar gnus-mark-article-hook (list 'gnus-summary-mark-unread-as-read)
1162   "*A hook called when an article is selected for the first time.
1163 The hook is intended to mark an article as read (or unread)
1164 automatically when it is selected.")
1165
1166 \f
1167 ;; Internal variables
1168
1169 ;; Avoid highlighting in kill files.
1170 (defvar gnus-summary-inhibit-highlight nil)
1171 (defvar gnus-newsgroup-selected-overlay nil)
1172
1173 (defvar gnus-article-mode-map nil)
1174 (defvar caesar-translate-table nil)
1175 (defvar gnus-dribble-buffer nil)
1176 (defvar gnus-headers-retrieved-by nil)
1177 (defvar gnus-article-reply nil)
1178 (defvar gnus-override-method nil)
1179 (defvar gnus-article-check-size nil)
1180
1181 (defvar gnus-current-score-file nil)
1182 (defvar gnus-internal-global-score-files nil)
1183 (defvar gnus-score-file-list nil)
1184
1185
1186 (defvar gnus-current-move-group nil)
1187
1188 (defvar gnus-newsgroup-dependencies nil)
1189 (defvar gnus-newsgroup-threads nil)
1190 (defvar gnus-newsgroup-async nil)
1191 (defconst gnus-group-edit-buffer "*Gnus edit newsgroup*")
1192
1193 (defvar gnus-newsgroup-adaptive nil)
1194
1195 (defvar gnus-summary-display-table nil)
1196
1197 (defconst gnus-group-line-format-alist
1198   (list (list ?M 'marked ?c)
1199         (list ?S 'subscribed ?c)
1200         (list ?L 'level ?d)
1201         (list ?N 'number ?s)
1202         (list ?I 'number-of-dormant ?d)
1203         (list ?T 'number-of-ticked ?d)
1204         (list ?R 'number-of-read ?s)
1205         (list ?t 'number-total ?d)
1206         (list ?y 'number-of-unread-unticked ?s)
1207         (list ?i 'number-of-ticked-and-dormant ?d)
1208         (list ?g 'group ?s)
1209         (list ?G 'qualified-group ?s)
1210         (list ?D 'newsgroup-description ?s)
1211         (list ?o 'moderated ?c)
1212         (list ?O 'moderated-string ?s)
1213         (list ?p 'process-marked ?c)
1214         (list ?s 'news-server ?s)
1215         (list ?n 'news-method ?s)
1216         (list ?z 'news-method-string ?s)
1217         (list ?u 'user-defined ?s)))
1218
1219 (defconst gnus-summary-line-format-alist 
1220   (list (list ?N 'number ?d)
1221         (list ?S 'subject ?s)
1222         (list ?s 'subject-or-nil ?s)
1223         (list ?n 'name ?s)
1224         (list ?A 'address ?s)
1225         (list ?F 'from ?s)
1226         (list ?x (macroexpand '(header-xref header)) ?s)
1227         (list ?D (macroexpand '(header-date header)) ?s)
1228         (list ?d '(gnus-dd-mmm (header-date header)) ?s)
1229         (list ?M (macroexpand '(header-id header)) ?s)
1230         (list ?r (macroexpand '(header-references header)) ?s)
1231         (list ?c '(or (header-chars header) 0) ?d)
1232         (list ?L 'lines ?d)
1233         (list ?I 'indentation ?s)
1234         (list ?T '(if (= level 0) "" (make-string (frame-width) ? )) ?s)
1235         (list ?R 'replied ?c)
1236         (list ?\[ 'opening-bracket ?c)
1237         (list ?\] 'closing-bracket ?c)
1238         (list ?\> '(make-string level ? ) ?s)
1239         (list ?\< '(make-string (max 0 (- 20 level)) ? ) ?s)
1240         (list ?i 'score ?d)
1241         (list ?z 'score-char ?c)
1242         (list ?U 'unread ?c)
1243         (list ?t '(gnus-summary-number-of-articles-in-thread 
1244                    (or (prog1 gnus-tmp-adopt-thread 
1245                          (setq gnus-tmp-adopt-thread nil))
1246                        (if (boundp 'thread) (symbol-value 'thread)
1247                          thread nil)))
1248                    ?d)
1249         (list ?e '(gnus-summary-number-of-articles-in-thread 
1250                    (or gnus-tmp-adopt-thread 
1251                        (if (boundp 'thread) (symbol-value 'thread)
1252                          thread nil)) t)
1253                    ?d)
1254         (list ?u 'user-defined ?s))
1255   "An alist of format specifications that can appear in summary lines,
1256 and what variables they correspond with, along with the type of the
1257 variable (string, integer, character, etc).")
1258
1259 (defconst gnus-summary-dummy-line-format-alist
1260   (list (list ?S 'subject ?s)
1261         (list ?N 'number ?d)
1262         (list ?u 'user-defined ?s)))
1263
1264 (defconst gnus-summary-mode-line-format-alist 
1265   (list (list ?G 'group-name ?s)
1266         (list ?g '(gnus-short-group-name group-name) ?s)
1267         (list ?A 'article-number ?d)
1268         (list ?Z 'unread-and-unselected ?s)
1269         (list ?V 'gnus-version ?s)
1270         (list ?U 'unread ?d)
1271         (list ?S 'subject ?s)
1272         (list ?e 'unselected ?d)
1273         (list ?u 'user-defined ?s)
1274         (list ?s '(gnus-current-score-file-nondirectory) ?s)))
1275
1276 (defconst gnus-group-mode-line-format-alist 
1277   (list (list ?S 'news-server ?s)
1278         (list ?M 'news-method ?s)
1279         (list ?u 'user-defined ?s)))
1280
1281 (defvar gnus-have-read-active-file nil)
1282
1283 (defconst gnus-maintainer "gnus-bug@ifi.uio.no (The Gnus Bugfixing Girls & Boys)"
1284   "The mail address of the Gnus maintainers.")
1285
1286 (defconst gnus-version "(ding) Gnus v0.84"
1287   "Version number for this version of Gnus.")
1288
1289 (defvar gnus-info-nodes
1290   '((gnus-group-mode            "(gnus)The Group Buffer")
1291     (gnus-summary-mode          "(gnus)The Summary Buffer")
1292     (gnus-article-mode          "(gnus)The Article Buffer"))
1293   "Assoc list of major modes and related Info nodes.")
1294
1295 (defvar gnus-documentation-group-file "~/dgnus/lisp/doc.txt"
1296   "The location of the (ding) Gnus documentation group.")
1297
1298 (defvar gnus-group-buffer "*Group*")
1299 (defvar gnus-summary-buffer "*Summary*")
1300 (defvar gnus-article-buffer "*Article*")
1301 (defvar gnus-server-buffer "*Server*")
1302
1303 (defvar gnus-work-buffer " *gnus work*")
1304
1305 (defvar gnus-buffer-list nil
1306   "Gnus buffers that should be killed on exit.")
1307
1308 (defvar gnus-server-alist nil
1309   "List of available servers.")
1310
1311 (defvar gnus-variable-list
1312   '(gnus-newsrc-options gnus-newsrc-options-n
1313     gnus-newsrc-last-checked-date 
1314     gnus-newsrc-alist gnus-server-alist
1315     gnus-killed-list gnus-zombie-list)
1316   "Gnus variables saved in the quick startup file.")
1317
1318 (defvar gnus-overload-functions
1319   '((news-inews gnus-inews-news "rnewspost")
1320     (caesar-region gnus-caesar-region "rnews"))
1321   "Functions overloaded by gnus.
1322 It is a list of `(original overload &optional file)'.")
1323
1324 (defvar gnus-newsrc-options nil
1325   "Options line in the .newsrc file.")
1326
1327 (defvar gnus-newsrc-options-n nil
1328   "List of regexps representing groups to be subscribed/ignored unconditionally.") 
1329
1330 (defvar gnus-newsrc-last-checked-date nil
1331   "Date Gnus last asked server for new newsgroups.")
1332
1333 (defvar gnus-newsrc-alist nil
1334   "Assoc list of read articles.
1335 gnus-newsrc-hashtb should be kept so that both hold the same information.")
1336
1337 (defvar gnus-newsrc-hashtb nil
1338   "Hashtable of gnus-newsrc-alist.")
1339
1340 (defvar gnus-killed-list nil
1341   "List of killed newsgroups.")
1342
1343 (defvar gnus-killed-hashtb nil
1344   "Hash table equivalent of gnus-killed-list.")
1345
1346 (defvar gnus-zombie-list nil
1347   "List of almost dead newsgroups.")
1348
1349 (defvar gnus-description-hashtb nil
1350   "Descriptions of newsgroups.")
1351
1352 (defvar gnus-list-of-killed-groups nil
1353   "List of newsgroups that have recently been killed by the user.")
1354
1355 (defvar gnus-active-hashtb nil
1356   "Hashtable of active articles.")
1357
1358 (defvar gnus-moderated-list nil
1359   "List of moderated newsgroups.")
1360
1361 (defvar gnus-group-marked nil)
1362
1363 (defvar gnus-current-startup-file nil
1364   "Startup file for the current host.")
1365
1366 (defvar gnus-last-search-regexp nil
1367   "Default regexp for article search command.")
1368
1369 (defvar gnus-last-shell-command nil
1370   "Default shell command on article.")
1371
1372 (defvar gnus-current-select-method nil
1373   "The current method for selecting a newsgroup.")
1374
1375 (defvar gnus-have-all-newsgroups nil)
1376
1377 (defvar gnus-article-internal-prepare-hook nil)
1378
1379 (defvar gnus-newsgroup-name nil)
1380 (defvar gnus-newsgroup-begin nil)
1381 (defvar gnus-newsgroup-end nil)
1382 (defvar gnus-newsgroup-last-rmail nil)
1383 (defvar gnus-newsgroup-last-mail nil)
1384 (defvar gnus-newsgroup-last-folder nil)
1385 (defvar gnus-newsgroup-last-file nil)
1386 (defvar gnus-newsgroup-auto-expire nil)
1387 (defvar gnus-newsgroup-active nil)
1388
1389 (defvar gnus-newsgroup-unreads nil
1390   "List of unread articles in the current newsgroup.")
1391
1392 (defvar gnus-newsgroup-unselected nil
1393   "List of unselected unread articles in the current newsgroup.")
1394
1395 (defvar gnus-newsgroup-marked nil
1396   "List of ticked articles in the current newsgroup (a subset of unread art).")
1397
1398 (defvar gnus-newsgroup-killed nil
1399   "List of ranges of articles that have been through the scoring process.")
1400
1401 (defvar gnus-newsgroup-kill-headers nil)
1402
1403 (defvar gnus-newsgroup-replied nil
1404   "List of articles that have been replied to in the current newsgroup.")
1405
1406 (defvar gnus-newsgroup-expirable nil
1407   "List of articles in the current newsgroup that can be expired.")
1408
1409 (defvar gnus-newsgroup-processable nil
1410   "List of articles in the current newsgroup that can be processed.")
1411
1412 (defvar gnus-newsgroup-bookmarks nil
1413   "List of articles in the current newsgroup that have bookmarks.")
1414
1415 (defvar gnus-newsgroup-dormant nil
1416   "List of dormant articles in the current newsgroup.")
1417
1418 (defvar gnus-newsgroup-scored nil
1419   "List of scored articles in the current newsgroup.")
1420
1421 (defvar gnus-newsgroup-headers nil
1422   "List of article headers in the current newsgroup.")
1423 (defvar gnus-newsgroup-headers-hashtb-by-number nil)
1424
1425 (defvar gnus-newsgroup-ancient nil
1426   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1427
1428 (defvar gnus-current-article nil)
1429 (defvar gnus-article-current nil)
1430 (defvar gnus-current-headers nil)
1431 (defvar gnus-have-all-headers nil)
1432 (defvar gnus-last-article nil)
1433 (defvar gnus-newsgroup-history nil)
1434 (defvar gnus-current-kill-article nil)
1435
1436 ;; Save window configuration.
1437 (defvar gnus-prev-winconf nil)
1438
1439 ;; Format specs
1440 (defvar gnus-summary-line-format-spec nil)
1441 (defvar gnus-summary-dummy-line-format-spec nil)
1442 (defvar gnus-group-line-format-spec nil)
1443 (defvar gnus-summary-mode-line-format-spec nil)
1444 (defvar gnus-article-mode-line-format-spec nil)
1445 (defvar gnus-group-mode-line-format-spec nil)
1446 (defvar gnus-summary-mark-positions nil)
1447
1448 (defvar gnus-summary-expunge-below nil)
1449 (defvar gnus-reffed-article-number nil)
1450
1451 (defvar rmail-default-file (expand-file-name "~/XMBOX"))
1452 (defvar rmail-default-rmail-file (expand-file-name "~/XNEWS"))
1453
1454 (defconst gnus-summary-local-variables 
1455   '(gnus-newsgroup-name 
1456     gnus-newsgroup-begin gnus-newsgroup-end 
1457     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail 
1458     gnus-newsgroup-last-folder gnus-newsgroup-last-file 
1459     gnus-newsgroup-auto-expire gnus-newsgroup-unreads 
1460     gnus-newsgroup-unselected gnus-newsgroup-marked
1461     gnus-newsgroup-replied gnus-newsgroup-expirable
1462     gnus-newsgroup-processable gnus-newsgroup-killed
1463     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1464     gnus-newsgroup-headers gnus-newsgroup-headers-hashtb-by-number
1465     gnus-current-article gnus-current-headers gnus-have-all-headers
1466     gnus-last-article gnus-article-internal-prepare-hook
1467     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1468     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1469     gnus-newsgroup-threads gnus-newsgroup-async
1470     gnus-score-alist gnus-current-score-file gnus-summary-expunge-below 
1471     gnus-summary-mark-below gnus-newsgroup-active gnus-scores-exclude-files
1472     gnus-newsgroup-history gnus-newsgroup-ancient
1473     gnus-newsgroup-adaptive)
1474   "Variables that are buffer-local to the summary buffers.")
1475
1476 (defconst gnus-bug-message
1477   "Sending a bug report to the Gnus Towers.
1478 ========================================
1479
1480 The buffer below is a mail buffer.  When you press `C-c C-c', it will
1481 be sent to the Gnus Bug Exterminators. 
1482
1483 At the bottom of the buffer you'll see lots of variable settings.
1484 Please do not delete those.  They will tell the Bug People what your
1485 environment is, so that it will be easier to locate the bugs.
1486
1487 If you have found a bug that makes Emacs go \"beep\", set
1488 debug-on-error to t (`M-x set-variable RET debug-on-error RET t RET') 
1489 and include the backtrace in your bug report.
1490
1491 Please describe the bug in annoying, painstaking detail.
1492
1493 Thank you for your help in stamping out bugs.
1494 ")
1495
1496 ;;; End of variables.
1497
1498 ;; Define some autoload functions Gnus might use.
1499 (eval-and-compile
1500
1501   ;; Various 
1502   (autoload 'metamail-buffer "metamail")
1503   (autoload 'Info-goto-node "info")
1504   (autoload 'hexl-hex-string-to-integer "hexl")
1505   (autoload 'pp "pp")
1506   (autoload 'pp-to-string "pp")
1507   (autoload 'pp-eval-expression "pp")
1508   (autoload 'mail-extract-address-components "mail-extr")
1509
1510   (autoload 'nnmail-split-fancy "nnmail")
1511   (autoload 'nnvirtual-catchup-group "nnvirtual")
1512
1513   ;; timezone
1514   (autoload 'timezone-make-date-arpa-standard "timezone")
1515   (autoload 'timezone-fix-time "timezone")
1516   (autoload 'timezone-make-sortable-date "timezone")
1517   (autoload 'timezone-make-time-string "timezone")
1518
1519   ;; rmail & friends
1520   (autoload 'mail-position-on-field "sendmail")
1521   (autoload 'mail-setup "sendmail")
1522   (autoload 'rmail-output "rmailout")
1523   (autoload 'news-mail-other-window "rnewspost")
1524   (autoload 'news-reply-yank-original "rnewspost")
1525   (autoload 'news-caesar-buffer-body "rnewspost")
1526   (autoload 'rmail-insert-rmail-file-header "rmail")
1527   (autoload 'rmail-count-new-messages "rmail")
1528   (autoload 'rmail-show-message "rmail")
1529
1530   ;; gnus-soup
1531   (autoload 'gnus-group-brew-soup "gnus-soup" nil t)
1532   (autoload 'gnus-brew-soup "gnus-soup" nil t)
1533   (autoload 'gnus-soup-add-article "gnus-soup" nil t)
1534   (autoload 'gnus-soup-send-replies "gnus-soup" nil t)
1535   (autoload 'gnus-soup-save-areas "gnus-soup" nil t)
1536   (autoload 'gnus-soup-pack-packet "gnus-soup" nil t)
1537   (autoload 'nnsoup-pack-replies "nnsoup" nil t)
1538
1539   ;; gnus-mh
1540   (autoload 'gnus-mail-reply-using-mhe "gnus-mh")
1541   (autoload 'gnus-mail-forward-using-mhe "gnus-mh")
1542   (autoload 'gnus-mail-other-window-using-mhe "gnus-mh")
1543   (autoload 'gnus-summary-save-in-folder "gnus-mh")
1544   (autoload 'gnus-summary-save-article-folder "gnus-mh")
1545   (autoload 'gnus-Folder-save-name "gnus-mh")
1546   (autoload 'gnus-folder-save-name "gnus-mh")
1547
1548   ;; gnus-vis misc
1549   (autoload 'gnus-group-make-menu-bar "gnus-vis")
1550   (autoload 'gnus-summary-make-menu-bar "gnus-vis")
1551   (autoload 'gnus-server-make-menu-bar "gnus-vis")
1552   (autoload 'gnus-article-make-menu-bar "gnus-vis")
1553   (autoload 'gnus-browse-make-menu-bar "gnus-vis")
1554   (autoload 'gnus-highlight-selected-summary "gnus-vis")
1555   (autoload 'gnus-summary-highlight-line "gnus-vis")
1556   (autoload 'gnus-carpal-setup-buffer "gnus-vis")
1557
1558   ;; gnus-vis article
1559   (autoload 'gnus-article-push-button "gnus-vis" nil t)
1560   (autoload 'gnus-article-press-button "gnus-vis" nil t)
1561   (autoload 'gnus-article-highlight "gnus-vis" nil t)
1562   (autoload 'gnus-article-hide "gnus-vis" nil t)
1563   (autoload 'gnus-article-hide-signature "gnus-vis" nil t)
1564   (autoload 'gnus-article-highlight-headers "gnus-vis" nil t)
1565   (autoload 'gnus-article-highlight-signature "gnus-vis" nil t)
1566   (autoload 'gnus-article-add-buttons "gnus-vis" nil t)
1567   (autoload 'gnus-article-next-button "gnus-vis" nil t)
1568   (autoload 'gnus-article-add-button "gnus-vis")
1569
1570   ;; gnus-cite
1571   (autoload 'gnus-article-highlight-citation "gnus-cite" nil t)
1572   (autoload 'gnus-article-hide-citation-maybe "gnus-cite" nil t)
1573   (autoload 'gnus-article-hide-citation "gnus-cite" nil t)
1574
1575   ;; gnus-kill
1576   (autoload 'gnus-kill "gnus-kill")
1577   (autoload 'gnus-apply-kill-file-internal "gnus-kill")
1578   (autoload 'gnus-kill-file-edit-file "gnus-kill")
1579   (autoload 'gnus-kill-file-raise-followups-to-author "gnus-kill")
1580   (autoload 'gnus-execute "gnus-kill")
1581   (autoload 'gnus-expunge "gnus-kill")
1582
1583   ;; gnus-cache
1584   (autoload 'gnus-cache-possibly-enter-article "gnus-cache")
1585   (autoload 'gnus-cache-save-buffers "gnus-cache")
1586   (autoload 'gnus-cache-possibly-remove-article "gnus-cache")
1587   (autoload 'gnus-cache-request-article "gnus-cache")
1588   (autoload 'gnus-cache-retrieve-headers "gnus-cache")
1589   (autoload 'gnus-cache-possibly-alter-active "gnus-cache")
1590   (autoload 'gnus-jog-cache "gnus-cache" nil t)
1591
1592   ;; gnus-score
1593   (autoload 'gnus-summary-increase-score "gnus-score" nil t)
1594   (autoload 'gnus-summary-lower-score "gnus-score" nil t)
1595   (autoload 'gnus-summary-score-map "gnus-score" nil nil 'keymap)
1596   (autoload 'gnus-score-save "gnus-score")
1597   (autoload 'gnus-score-headers "gnus-score")
1598   (autoload 'gnus-current-score-file-nondirectory "gnus-score")
1599   (autoload 'gnus-score-adaptive "gnus-score")
1600   (autoload 'gnus-score-remove-lines-adaptive "gnus-score")
1601   (autoload 'gnus-score-find-trace "gnus-score")
1602
1603   ;; gnus-edit
1604   (autoload 'gnus-score-customize "gnus-edit" nil t)
1605
1606   ;; gnus-uu
1607   (autoload 'gnus-uu-extract-map "gnus-uu" nil nil 'keymap)
1608   (autoload 'gnus-uu-mark-map "gnus-uu" nil nil 'keymap)
1609   (autoload 'gnus-uu-digest-mail-forward "gnus-uu" nil t)
1610   (autoload 'gnus-uu-digest-post-forward "gnus-uu" nil t)
1611   (autoload 'gnus-uu-mark-series "gnus-uu" nil t)
1612   (autoload 'gnus-uu-mark-region "gnus-uu" nil t)
1613   (autoload 'gnus-uu-mark-by-regexp "gnus-uu" nil t)
1614   (autoload 'gnus-uu-mark-all "gnus-uu" nil t)
1615   (autoload 'gnus-uu-mark-sparse "gnus-uu" nil t)
1616   (autoload 'gnus-uu-mark-thread "gnus-uu" nil t)
1617   (autoload 'gnus-uu-decode-uu "gnus-uu" nil t)
1618   (autoload 'gnus-uu-decode-uu-and-save "gnus-uu" nil t)
1619   (autoload 'gnus-uu-decode-unshar "gnus-uu" nil t)
1620   (autoload 'gnus-uu-decode-unshar-and-save "gnus-uu" nil t)
1621   (autoload 'gnus-uu-decode-save "gnus-uu" nil t)
1622   (autoload 'gnus-uu-decode-binhex "gnus-uu" nil t)
1623   (autoload 'gnus-uu-decode-uu-view "gnus-uu" nil t)
1624   (autoload 'gnus-uu-decode-uu-and-save-view "gnus-uu" nil t)
1625   (autoload 'gnus-uu-decode-unshar-view "gnus-uu" nil t)
1626   (autoload 'gnus-uu-decode-unshar-and-save-view "gnus-uu" nil t)
1627   (autoload 'gnus-uu-decode-save-view "gnus-uu" nil t)
1628   (autoload 'gnus-uu-decode-binhex-view "gnus-uu" nil t)
1629
1630   ;; gnus-msg
1631   (autoload 'gnus-summary-send-map "gnus-msg" nil nil 'keymap)
1632   (autoload 'gnus-group-post-news "gnus-msg" nil t)
1633   (autoload 'gnus-group-mail "gnus-msg" nil t)
1634   (autoload 'gnus-summary-post-news "gnus-msg" nil t)
1635   (autoload 'gnus-summary-followup "gnus-msg" nil t)
1636   (autoload 'gnus-summary-followup-with-original "gnus-msg" nil t)
1637   (autoload 'gnus-summary-followup-and-reply "gnus-msg" nil t)
1638   (autoload 'gnus-summary-followup-and-reply-with-original "gnus-msg" nil t)
1639   (autoload 'gnus-summary-cancel-article "gnus-msg" nil t)
1640   (autoload 'gnus-summary-supersede-article "gnus-msg" nil t)
1641   (autoload 'gnus-post-news "gnus-msg" nil t)
1642   (autoload 'gnus-inews-news "gnus-msg" nil t)
1643   (autoload 'gnus-cancel-news "gnus-msg" nil t)
1644   (autoload 'gnus-summary-reply "gnus-msg" nil t)
1645   (autoload 'gnus-summary-reply-with-original "gnus-msg" nil t)
1646   (autoload 'gnus-summary-mail-forward "gnus-msg" nil t)
1647   (autoload 'gnus-summary-mail-other-window "gnus-msg" nil t)
1648   (autoload 'gnus-mail-reply-using-mail "gnus-msg")
1649   (autoload 'gnus-mail-yank-original "gnus-msg")
1650   (autoload 'gnus-mail-send-and-exit "gnus-msg")
1651   (autoload 'gnus-mail-forward-using-mail "gnus-msg")
1652   (autoload 'gnus-mail-other-window-using-mail "gnus-msg")
1653   (autoload 'gnus-article-mail-with-original "gnus-msg")
1654   (autoload 'gnus-article-mail "gnus-msg")
1655
1656   ;; gnus-vm
1657   (autoload 'gnus-summary-save-in-vm "gnus-vm" nil t)
1658   (autoload 'gnus-summary-save-article-vm "gnus-vm" nil t)
1659   (autoload 'gnus-mail-forward-using-vm "gnus-vm")
1660   (autoload 'gnus-mail-reply-using-vm "gnus-vm")
1661   (autoload 'gnus-mail-other-window-using-vm "gnus-vm" nil t)
1662   (autoload 'gnus-yank-article "gnus-vm" nil t)
1663
1664   )
1665
1666 \f
1667
1668 ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
1669 ;; If you want the cursor to go somewhere else, set these two
1670 ;; functions in some startup hook to whatever you want.
1671 (defalias 'gnus-summary-position-cursor 'gnus-goto-colon)
1672 (defalias 'gnus-group-position-cursor 'gnus-goto-colon)
1673
1674 ;;; Various macros and substs.
1675
1676 (defmacro gnus-eval-in-buffer-window (buffer &rest forms)
1677   "Pop to BUFFER, evaluate FORMS, and then returns to original window."
1678   (` (let ((GnusStartBufferWindow (selected-window)))
1679        (unwind-protect
1680            (progn
1681              (pop-to-buffer (, buffer))
1682              (,@ forms))
1683          (select-window GnusStartBufferWindow)))))
1684
1685 (defmacro gnus-gethash (string hashtable)
1686   "Get hash value of STRING in HASHTABLE."
1687   ;;(` (symbol-value (abbrev-symbol (, string) (, hashtable))))
1688   ;;(` (abbrev-expansion (, string) (, hashtable)))
1689   (` (symbol-value (intern-soft (, string) (, hashtable)))))
1690
1691 (defmacro gnus-sethash (string value hashtable)
1692   "Set hash value. Arguments are STRING, VALUE, and HASHTABLE."
1693   ;; We cannot use define-abbrev since it only accepts string as value.
1694   ;; (set (intern string hashtable) value))
1695   (` (set (intern (, string) (, hashtable)) (, value))))
1696
1697 (defsubst gnus-buffer-substring (beg end)
1698   (buffer-substring (match-beginning beg) (match-end end)))
1699
1700 (defsubst gnus-simplify-subject-re (subject)
1701   "Remove \"Re:\" from subject lines."
1702   (let ((case-fold-search t))
1703     (if (string-match "^re: *" subject)
1704         (substring subject (match-end 0))
1705       subject)))
1706
1707 (defsubst gnus-goto-char (point)
1708   (and point (goto-char point)))
1709
1710 (defmacro gnus-buffer-exists-p (buffer)
1711   (` (and (, buffer)
1712           (funcall (if (stringp (, buffer)) 'get-buffer 'buffer-name)
1713                    (, buffer)))))
1714
1715 (defmacro gnus-kill-buffer (buffer)
1716   (` (if (gnus-buffer-exists-p (, buffer))
1717          (kill-buffer (, buffer)))))
1718
1719 (defsubst gnus-point-at-bol ()
1720   "Return point at the beginning of line."
1721   (let ((p (point)))
1722     (beginning-of-line)
1723     (prog1
1724         (point)
1725       (goto-char p))))
1726
1727 (defsubst gnus-point-at-eol ()
1728   "Return point at the beginning of line."
1729   (let ((p (point)))
1730     (end-of-line)
1731     (prog1
1732         (point)
1733       (goto-char p))))
1734
1735 ;; Delete the current line (and the next N lines.);
1736 (defmacro gnus-delete-line (&optional n)
1737   (` (delete-region (progn (beginning-of-line) (point))
1738                     (progn (forward-line (, (or n 1))) (point)))))
1739
1740 ;;; Load the compatability functions. 
1741
1742 (require 'gnus-ems)
1743
1744 \f
1745 ;;;
1746 ;;; Gnus Utility Functions
1747 ;;;
1748
1749 (defun gnus-extract-address-components (from)
1750   (let (name address)
1751     ;; First find the address - the thing with the @ in it.  This may
1752     ;; not be accurate in mail addresses, but does the trick most of
1753     ;; the time in news messages.
1754     (if (string-match "\\b[^@ \t<>]+[!@][^@ \t<>]+\\b" from)
1755         (setq address (substring from (match-beginning 0) (match-end 0))))
1756     ;; Then we check whether the "name <address>" format is used.
1757     (and address
1758          (string-match (concat "<" (regexp-quote address) ">") from)
1759          (and (setq name (substring from 0 (1- (match-beginning 0))))
1760               ;; Strip any quotes from the name.
1761               (string-match "\".*\"" name)
1762               (setq name (substring name 1 (1- (match-end 0))))))
1763     ;; If not, then "address (name)" is used.
1764     (or name
1765         (and (string-match "(.+)" from)
1766              (setq name (substring from (1+ (match-beginning 0)) 
1767                                    (1- (match-end 0)))))
1768         ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>.
1769         ;; XOVER might not support folded From headers.
1770         (and (string-match "(.*" from)
1771              (setq name (substring from (1+ (match-beginning 0)) 
1772                                    (match-end 0)))))
1773     ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
1774     (list (or name from) (or address from))))
1775
1776 (defun gnus-fetch-field (field)
1777   "Return the value of the header FIELD of current article."
1778   (save-excursion
1779     (save-restriction
1780       (let ((case-fold-search t))
1781         (gnus-narrow-to-headers)
1782         (mail-fetch-field field)))))
1783
1784 (defun gnus-goto-colon ()
1785   (beginning-of-line)
1786   (search-forward ":" (gnus-point-at-eol) t))
1787
1788 (defun gnus-narrow-to-headers ()
1789   (widen)
1790   (save-excursion
1791     (narrow-to-region
1792      (goto-char (point-min))
1793      (if (search-forward "\n\n" nil t)
1794          (1- (point))
1795        (point-max)))))
1796
1797 (defun gnus-update-format-specifications ()
1798   (gnus-make-thread-indent-array)
1799   (setq gnus-summary-line-format-spec 
1800         (gnus-parse-format
1801          gnus-summary-line-format gnus-summary-line-format-alist))
1802   (gnus-update-summary-mark-positions)
1803   (setq gnus-summary-dummy-line-format-spec 
1804         (gnus-parse-format gnus-summary-dummy-line-format 
1805                            gnus-summary-dummy-line-format-alist))
1806   (setq gnus-group-line-format-spec
1807         (gnus-parse-format 
1808          gnus-group-line-format 
1809          gnus-group-line-format-alist))
1810   (if (and (string-match "%D" gnus-group-line-format)
1811            (not gnus-description-hashtb)
1812            gnus-read-active-file)
1813       (gnus-read-all-descriptions-files))
1814   (setq gnus-summary-mode-line-format-spec 
1815         (gnus-parse-format gnus-summary-mode-line-format 
1816                            gnus-summary-mode-line-format-alist))
1817   (setq gnus-article-mode-line-format-spec 
1818         (gnus-parse-format gnus-article-mode-line-format 
1819                            gnus-summary-mode-line-format-alist))
1820   (setq gnus-group-mode-line-format-spec 
1821         (gnus-parse-format gnus-group-mode-line-format 
1822                            gnus-group-mode-line-format-alist)))
1823
1824 (defun gnus-update-summary-mark-positions ()
1825   (save-excursion
1826     (let ((gnus-replied-mark 129)
1827           (gnus-score-below-mark 130)
1828           (gnus-score-over-mark 130)
1829           (thread nil)
1830           pos)
1831       (gnus-set-work-buffer)
1832       (gnus-summary-insert-line 
1833        nil [0 "" "" "" "" "" 0 0 ""]  0 nil 128 t nil "" nil 1)
1834       (goto-char (point-min))
1835       (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
1836                                          (- (point) 2)))))
1837       (goto-char (point-min))
1838       (setq pos (cons (cons 'replied (and (search-forward "\201" nil t)
1839                                           (- (point) 2))) pos))
1840       (goto-char (point-min))
1841       (setq pos (cons (cons 'score (and (search-forward "\202" nil t)
1842                                         (- (point) 2))) pos))
1843       (setq gnus-summary-mark-positions pos))))
1844
1845 (defun gnus-format-max-width (form length)
1846   (let* ((val (eval form))
1847          (valstr (if (numberp val) (int-to-string val) val)))
1848     (if (> (length valstr) length)
1849         (substring valstr 0 length)
1850       valstr)))
1851
1852 (defun gnus-set-mouse-face (string)
1853   ;; Set mouse face property on STRING.
1854   (put-text-property 0 (length string) 'mouse-face gnus-mouse-face string)
1855   string)
1856
1857 (defun gnus-parse-format (format spec-alist)
1858   ;; This function parses the FORMAT string with the help of the
1859   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
1860   ;; string.  If the FORMAT string contains the specifiers %( and %)
1861   ;; the text between them will have the mouse-face text property.
1862   (if (string-match "\\`\\(.*\\)%(\\(.*\\)%)\\(.*\n?\\)\\'" format)
1863       (if (and gnus-visual gnus-mouse-face)
1864           (let ((pre (substring format (match-beginning 1) (match-end 1)))
1865                 (button (substring format (match-beginning 2) (match-end 2)))
1866                 (post (substring format (match-beginning 3) (match-end 3))))
1867             (list 'concat
1868                   (gnus-parse-simple-format pre spec-alist)
1869                   (list 'gnus-set-mouse-face
1870                         (gnus-parse-simple-format button spec-alist))
1871                   (gnus-parse-simple-format post spec-alist)))
1872         (gnus-parse-simple-format
1873          (concat (substring format (match-beginning 1) (match-end 1))
1874                  (substring format (match-beginning 2) (match-end 2))
1875                  (substring format (match-beginning 3) (match-end 3)))
1876          spec-alist))
1877     (gnus-parse-simple-format format spec-alist)))
1878
1879 (defun gnus-parse-simple-format (format spec-alist)
1880   ;; This function parses the FORMAT string with the help of the
1881   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
1882   ;; string. The list will consist of the symbol `format', a format
1883   ;; specification string, and a list of forms depending on the
1884   ;; SPEC-ALIST.
1885   (let ((max-width 0)
1886         spec flist fstring b newspec max-width elem beg)
1887     (save-excursion
1888       (gnus-set-work-buffer)
1889       (insert format)
1890       (goto-char (point-min))
1891       (while (re-search-forward "%[-0-9]*\\(,[0-9]*\\)*\\(.\\)\\(.\\)?" nil t)
1892         (setq spec (string-to-char (buffer-substring (match-beginning 2)
1893                                                      (match-end 2))))
1894         ;; First check if there are any specs that look anything like
1895         ;; "%12,12A", ie. with a "max width specification". These have
1896         ;; to be treated specially.
1897         (if (setq beg (match-beginning 1))
1898             (setq max-width 
1899                   (string-to-int 
1900                    (buffer-substring (1+ (match-beginning 1)) (match-end 1))))
1901           (setq max-width 0)
1902           (setq beg (match-beginning 2)))
1903         ;; Find the specification from `spec-alist'.
1904         (if (not (setq elem (cdr (assq spec spec-alist))))
1905             (setq elem '("*" ?s)))
1906         ;; Treat user defined format specifiers specially
1907         (and (eq (car elem) 'user-defined)
1908              (setq elem
1909                    (list 
1910                     (list (intern (concat "gnus-user-format-function-"
1911                                           (buffer-substring
1912                                            (match-beginning 3)
1913                                            (match-end 3))))
1914                           'header)
1915                     ?s))
1916              (delete-region (match-beginning 3) (match-end 3)))
1917         (if (not (zerop max-width))
1918             (let ((el (car elem)))
1919               (cond ((= (car (cdr elem)) ?c) 
1920                      (setq el (list 'char-to-string el)))
1921                     ((= (car (cdr elem)) ?d)
1922                      (numberp el) (setq el (list 'int-to-string el))))
1923               (setq flist (cons (list 'gnus-format-max-width el max-width) 
1924                                 flist))
1925               (setq newspec ?s))
1926           (setq flist (cons (car elem) flist))
1927           (setq newspec (car (cdr elem))))
1928         ;; Remove the old specification (and possibly a ",12" string).
1929         (delete-region beg (match-end 2))
1930         ;; Insert the new specification.
1931         (goto-char beg)
1932         (insert newspec))
1933       (setq fstring (buffer-substring 1 (point-max))))
1934     (cons 'format (cons fstring (nreverse flist)))))
1935
1936 ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
1937 (defun gnus-read-init-file ()
1938   (and gnus-init-file
1939        (or (and (file-exists-p gnus-init-file) 
1940                 ;; Don't try to load a directory.
1941                 (not (file-directory-p gnus-init-file)))
1942            (file-exists-p (concat gnus-init-file ".el"))
1943            (file-exists-p (concat gnus-init-file ".elc")))
1944        (load gnus-init-file nil t)))
1945
1946 (defun gnus-set-work-buffer ()
1947   (if (get-buffer gnus-work-buffer)
1948       (progn
1949         (set-buffer gnus-work-buffer)
1950         (erase-buffer))
1951     (set-buffer (get-buffer-create gnus-work-buffer))
1952     (kill-all-local-variables)
1953     (buffer-disable-undo (current-buffer))
1954     (gnus-add-current-to-buffer-list)))
1955
1956 ;; Article file names when saving.
1957
1958 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
1959   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1960 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group/num.
1961 Otherwise, it is like ~/News/news/group/num."
1962   (let ((default
1963           (expand-file-name
1964            (concat (if (gnus-use-long-file-name 'not-save)
1965                        (gnus-capitalize-newsgroup newsgroup)
1966                      (gnus-newsgroup-directory-form newsgroup))
1967                    "/" (int-to-string (header-number headers)))
1968            (or gnus-article-save-directory "~/News"))))
1969     (if (and last-file
1970              (string-equal (file-name-directory default)
1971                            (file-name-directory last-file))
1972              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
1973         default
1974       (or last-file default))))
1975
1976 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
1977   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1978 If variable `gnus-use-long-file-name' is nil, it is ~/News/news.group/num.
1979 Otherwise, it is like ~/News/news/group/num."
1980   (let ((default
1981           (expand-file-name
1982            (concat (if (gnus-use-long-file-name 'not-save)
1983                        newsgroup
1984                      (gnus-newsgroup-directory-form newsgroup))
1985                    "/" (int-to-string (header-number headers)))
1986            (or gnus-article-save-directory "~/News"))))
1987     (if (and last-file
1988              (string-equal (file-name-directory default)
1989                            (file-name-directory last-file))
1990              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
1991         default
1992       (or last-file default))))
1993
1994 (defun gnus-Plain-save-name (newsgroup headers &optional last-file)
1995   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1996 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group.
1997 Otherwise, it is like ~/News/news/group/news."
1998   (or last-file
1999       (expand-file-name
2000        (if (gnus-use-long-file-name 'not-save)
2001            (gnus-capitalize-newsgroup newsgroup)
2002          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2003        (or gnus-article-save-directory "~/News"))))
2004
2005 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
2006   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2007 If variable `gnus-use-long-file-name' is nil, it is ~/News/news.group.
2008 Otherwise, it is like ~/News/news/group/news."
2009   (or last-file
2010       (expand-file-name
2011        (if (gnus-use-long-file-name 'not-save)
2012            newsgroup
2013          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2014        (or gnus-article-save-directory "~/News"))))
2015
2016 ;; For subscribing new newsgroup
2017
2018 (defun gnus-subscribe-hierarchical-interactive (groups)
2019   (let ((groups (sort groups 'string<))
2020         prefixes prefix start rest ans group starts)
2021     (while groups
2022       (setq prefixes (list "^"))
2023       (while (and groups prefixes)
2024         (while (not (string-match (car prefixes) (car groups)))
2025           (setq prefixes (cdr prefixes)))
2026         (setq prefix (car prefixes))
2027         (setq start (1- (length prefix)))
2028         (if (and (string-match "[^\\.]\\." (car groups) start)
2029                  (cdr groups)
2030                  (setq prefix 
2031                        (concat "^" (substring (car groups) 0 (match-end 0))))
2032                  (string-match prefix (car (cdr groups))))
2033             (progn
2034               (setq prefixes (cons prefix prefixes))
2035               (message "Descend hierarchy %s? ([y]nsq): " 
2036                        (substring prefix 1 (1- (length prefix))))
2037               (setq ans (read-char))
2038               (cond ((= ans ?n)
2039                      (while (and groups 
2040                                  (string-match prefix 
2041                                                (setq group (car groups))))
2042                        (setq gnus-killed-list 
2043                              (cons group gnus-killed-list))
2044                        (gnus-sethash group group gnus-killed-hashtb)
2045                        (setq groups (cdr groups)))
2046                      (setq starts (cdr starts)))
2047                     ((= ans ?s)
2048                      (while (and groups 
2049                                  (string-match prefix 
2050                                                (setq group (car groups))))
2051                        (gnus-sethash group group gnus-killed-hashtb)
2052                        (gnus-subscribe-alphabetically (car groups))
2053                        (setq groups (cdr groups)))
2054                      (setq starts (cdr starts)))
2055                     ((= ans ?q)
2056                      (while groups
2057                        (setq group (car groups))
2058                        (setq gnus-killed-list (cons group gnus-killed-list))
2059                        (gnus-sethash group group gnus-killed-hashtb)
2060                        (setq groups (cdr groups))))
2061                     (t nil)))
2062           (message "Subscribe %s? ([n]yq)" (car groups))
2063           (setq ans (read-char))
2064           (setq group (car groups))
2065           (cond ((= ans ?y)
2066                  (gnus-subscribe-alphabetically (car groups))
2067                  (gnus-sethash group group gnus-killed-hashtb))
2068                 ((= ans ?q)
2069                  (while groups
2070                    (setq group (car groups))
2071                    (setq gnus-killed-list (cons group gnus-killed-list))
2072                    (gnus-sethash group group gnus-killed-hashtb)
2073                    (setq groups (cdr groups))))
2074                 (t 
2075                  (setq gnus-killed-list (cons group gnus-killed-list))
2076                  (gnus-sethash group group gnus-killed-hashtb)))
2077           (setq groups (cdr groups)))))))
2078
2079 (defun gnus-subscribe-randomly (newsgroup)
2080   "Subscribe new NEWSGROUP by making it the first newsgroup."
2081   (gnus-subscribe-newsgroup newsgroup))
2082
2083 (defun gnus-subscribe-alphabetically (newgroup)
2084   "Subscribe new NEWSGROUP and insert it in alphabetical order."
2085   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2086   (let ((groups (cdr gnus-newsrc-alist))
2087         before)
2088     (while (and (not before) groups)
2089       (if (string< newgroup (car (car groups)))
2090           (setq before (car (car groups)))
2091         (setq groups (cdr groups))))
2092     (gnus-subscribe-newsgroup newgroup before)))
2093
2094 (defun gnus-subscribe-hierarchically (newgroup)
2095   "Subscribe new NEWSGROUP and insert it in hierarchical newsgroup order."
2096   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2097   (save-excursion
2098     (set-buffer (find-file-noselect gnus-current-startup-file))
2099     (let ((groupkey newgroup)
2100           before)
2101       (while (and (not before) groupkey)
2102         (goto-char (point-min))
2103         (let ((groupkey-re
2104                (concat "^\\(" (regexp-quote groupkey) ".*\\)[!:]")))
2105           (while (and (re-search-forward groupkey-re nil t)
2106                       (progn
2107                         (setq before (buffer-substring
2108                                       (match-beginning 1) (match-end 1)))
2109                         (string< before newgroup)))))
2110         ;; Remove tail of newsgroup name (eg. a.b.c -> a.b)
2111         (setq groupkey
2112               (if (string-match "^\\(.*\\)\\.[^.]+$" groupkey)
2113                   (substring groupkey (match-beginning 1) (match-end 1)))))
2114       (gnus-subscribe-newsgroup newgroup before))))
2115
2116 (defun gnus-subscribe-interactively (newsgroup)
2117   "Subscribe new NEWSGROUP interactively.
2118 It is inserted in hierarchical newsgroup order if subscribed. If not,
2119 it is killed."
2120   (if (gnus-y-or-n-p (format "Subscribe new newsgroup: %s " newsgroup))
2121       (gnus-subscribe-hierarchically newsgroup)
2122     (setq gnus-killed-list (cons newsgroup gnus-killed-list))))
2123
2124 (defun gnus-subscribe-zombies (newsgroup)
2125   "Make new NEWSGROUP a zombie group."
2126   (setq gnus-zombie-list (cons newsgroup gnus-zombie-list)))
2127
2128 (defun gnus-subscribe-newsgroup (newsgroup &optional next)
2129   "Subscribe new NEWSGROUP.
2130 If NEXT is non-nil, it is inserted before NEXT. Otherwise it is made
2131 the first newsgroup."
2132   ;; We subscribe the group by changing its level to `subscribed'.
2133   (gnus-group-change-level 
2134    newsgroup gnus-level-default-subscribed
2135    gnus-level-killed (gnus-gethash (or next "dummy.group") gnus-newsrc-hashtb))
2136   (gnus-message 5 "Subscribe newsgroup: %s" newsgroup))
2137
2138 ;; For directories
2139
2140 (defun gnus-newsgroup-directory-form (newsgroup)
2141   "Make hierarchical directory name from NEWSGROUP name."
2142   (let ((newsgroup (substring newsgroup 0)) ;Copy string.
2143         (len (length newsgroup))
2144         idx)
2145     ;; If this is a foreign group, we don't want to translate the
2146     ;; entire name.  
2147     (if (setq idx (string-match ":" newsgroup))
2148         (aset newsgroup idx ?/)
2149       (setq idx 0))
2150     ;; Replace all occurrences of `.' with `/'.
2151     (while (< idx len)
2152       (if (= (aref newsgroup idx) ?.)
2153           (aset newsgroup idx ?/))
2154       (setq idx (1+ idx)))
2155     newsgroup))
2156
2157 (defun gnus-make-directory (dir)
2158   "Make DIRECTORY recursively."
2159   (let* ((dir (expand-file-name dir default-directory))
2160          dirs)
2161     (if (string-match "/$" dir)
2162         (setq dir (substring dir 0 (match-beginning 0))))
2163     (while (not (file-exists-p dir))
2164       (setq dirs (cons dir dirs))
2165       (string-match "/[^/]+$" dir)
2166       (setq dir (substring dir 0 (match-beginning 0))))
2167     (while dirs
2168       (make-directory (car dirs))
2169       (setq dirs (cdr dirs)))))
2170
2171 (defun gnus-capitalize-newsgroup (newsgroup)
2172   "Capitalize NEWSGROUP name."
2173   (and (not (zerop (length newsgroup)))
2174        (concat (char-to-string (upcase (aref newsgroup 0)))
2175                (substring newsgroup 1))))
2176
2177 ;; Var
2178
2179 (defun gnus-simplify-subject (subject &optional re-only)
2180   "Remove `Re:' and words in parentheses.
2181 If optional argument RE-ONLY is non-nil, strip `Re:' only."
2182   (let ((case-fold-search t))           ;Ignore case.
2183     ;; Remove `Re:' and `Re^N:'.
2184     (if (string-match "^re:[ \t]*" subject)
2185         (setq subject (substring subject (match-end 0))))
2186     ;; Remove words in parentheses from end.
2187     (or re-only
2188         (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
2189           (setq subject (substring subject 0 (match-beginning 0)))))
2190     ;; Return subject string.
2191     subject))
2192
2193 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
2194 ;; all whitespace.
2195 (defun gnus-simplify-subject-fuzzy (subject)
2196   (let ((case-fold-search t))
2197     (save-excursion
2198       (gnus-set-work-buffer)
2199       (insert subject)
2200       (inline gnus-simplify-buffer-fuzzy)
2201       (buffer-string))))
2202
2203 (defun gnus-simplify-buffer-fuzzy ()
2204   (goto-char (point-min))
2205   ;; Fix by Stainless Steel Rat <ratinox@ccs.neu.edu>.
2206   (while (re-search-forward "^[ \t]*\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;][ \t]*"
2207                             nil t)
2208     (replace-match "" t t))
2209   (goto-char (point-min))
2210   (while (re-search-forward "[ \t\n]*([^()]*)[ \t\n]*$" nil t)
2211     (replace-match "" t t))
2212   (goto-char (point-min))
2213   (while (re-search-forward "[ \t]+" nil t)
2214     (replace-match " " t t))
2215   (goto-char (point-min))
2216   (while (re-search-forward "[ \t]+$" nil t)
2217     (replace-match "" t t))
2218   (goto-char (point-min))
2219   (while (re-search-forward "^[ \t]+" nil t)
2220     (replace-match "" t t)))
2221
2222 ;; Add the current buffer to the list of buffers to be killed on exit. 
2223 (defun gnus-add-current-to-buffer-list ()
2224   (or (memq (current-buffer) gnus-buffer-list)
2225       (setq gnus-buffer-list (cons (current-buffer) gnus-buffer-list))))
2226
2227 (defun gnus-string> (s1 s2)
2228   (not (or (string< s1 s2)
2229            (string= s1 s2))))
2230
2231 ;; Functions accessing headers.
2232 ;; Functions are more convenient than macros in some cases.
2233
2234 (defun gnus-header-number (header)
2235   (header-number header))
2236
2237 (defun gnus-header-subject (header)
2238   (header-subject header))
2239
2240 (defun gnus-header-from (header)
2241   (header-from header))
2242
2243 (defun gnus-header-xref (header)
2244   (header-xref header))
2245
2246 (defun gnus-header-lines (header)
2247   (header-lines header))
2248
2249 (defun gnus-header-date (header)
2250   (header-date header))
2251
2252 (defun gnus-header-id (header)
2253   (header-id header))
2254
2255 (defun gnus-header-references (header)
2256   (header-references header))
2257
2258 ;;; General various misc type functions.
2259
2260 (defun gnus-clear-system ()
2261   "Clear all variables and buffers."
2262   ;; Clear Gnus variables.
2263   (let ((variables gnus-variable-list))
2264     (while variables
2265       (set (car variables) nil)
2266       (setq variables (cdr variables))))
2267   ;; Clear other internal variables.
2268   (setq gnus-list-of-killed-groups nil
2269         gnus-have-read-active-file nil
2270         gnus-newsrc-alist nil
2271         gnus-newsrc-hashtb nil
2272         gnus-killed-list nil
2273         gnus-zombie-list nil
2274         gnus-killed-hashtb nil
2275         gnus-active-hashtb nil
2276         gnus-moderated-list nil
2277         gnus-description-hashtb nil
2278         gnus-newsgroup-headers nil
2279         gnus-newsgroup-headers-hashtb-by-number nil
2280         gnus-newsgroup-name nil
2281         gnus-server-alist nil
2282         gnus-current-select-method nil)
2283   ;; Reset any score variables.
2284   (and (boundp 'gnus-score-cache)
2285        (set 'gnus-score-cache nil))
2286   (and (boundp 'gnus-internal-global-score-files)
2287        (set 'gnus-internal-global-score-files nil))
2288   ;; Kill the startup file.
2289   (and gnus-current-startup-file
2290        (get-file-buffer gnus-current-startup-file)
2291        (kill-buffer (get-file-buffer gnus-current-startup-file)))
2292   ;; Save any cache buffers.
2293   (and gnus-use-cache (gnus-cache-save-buffers))
2294   ;; Clear the dribble buffer.
2295   (gnus-dribble-clear)
2296   ;; Kill global KILL file buffer.
2297   (if (get-file-buffer (gnus-newsgroup-kill-file nil))
2298       (kill-buffer (get-file-buffer (gnus-newsgroup-kill-file nil))))
2299   (gnus-kill-buffer nntp-server-buffer)
2300   ;; Kill Gnus buffers.
2301   (while gnus-buffer-list
2302     (gnus-kill-buffer (car gnus-buffer-list))
2303     (setq gnus-buffer-list (cdr gnus-buffer-list))))
2304
2305 (defun gnus-windows-old-to-new (setting)
2306   (if (symbolp setting)
2307       (setq setting 
2308             (cond ((eq setting 'SelectArticle)
2309                    'article)
2310                   ((eq setting 'SelectSubject)
2311                    'summary)
2312                   ((eq setting 'SelectNewsgroup)
2313                    'group)
2314                   (t setting))))
2315   (if (or (listp setting)
2316           (not (and gnus-window-configuration
2317                     (memq setting '(group summary article)))))
2318       setting
2319     (let* ((setting (if (eq setting 'group) 
2320                         (if (assq 'newsgroup gnus-window-configuration)
2321                             'newsgroup
2322                           'newsgroups) setting))
2323            (elem (car (cdr (assq setting gnus-window-configuration))))
2324            (total (apply '+ elem))
2325            (types '(group summary article))
2326            (pbuf (if (eq setting 'newsgroups) 'group 'summary))
2327            (i 0)
2328            perc
2329            out)
2330       (while (< i 3)
2331         (or (zerop (nth i elem))
2332             (progn
2333               (setq perc  (/ (* 1.0 (nth 0 elem)) total))
2334               (setq out (cons (if (eq pbuf (nth i types))
2335                                   (vector (nth i types) perc 'point)
2336                                 (vector (nth i types) perc))
2337                               out))))
2338         (setq i (1+ i)))
2339       (list (nreverse out)))))
2340            
2341 (defun gnus-add-configuration (conf)
2342   (setq gnus-buffer-configuration 
2343         (cons conf (delq (assq (car conf) gnus-buffer-configuration)
2344                          gnus-buffer-configuration))))
2345
2346 (defun gnus-configure-windows (setting)
2347   (setq setting (gnus-windows-old-to-new setting))
2348   (let ((r (if (symbolp setting)
2349                   (cdr (assq setting gnus-buffer-configuration))
2350                 setting))
2351         (in-buf (current-buffer))
2352         rule val window w height hor ohor heights sub jump-buffer
2353         rel total to-buf)
2354     (or r (error "No such setting: %s" setting))
2355
2356     ;; Either remove all windows or just remove all Gnus windows.
2357     (if gnus-use-full-window
2358         (delete-other-windows)
2359       (gnus-remove-some-windows)
2360       (switch-to-buffer nntp-server-buffer))
2361
2362     (while r
2363       (setq hor (car r)
2364             ohor nil)
2365
2366       ;; We have to do the (possible) horizontal splitting before the
2367       ;; vertical. 
2368       (if (and (listp (car hor)) 
2369                (eq (car (car hor)) 'horizontal))
2370           (progn
2371             (split-window nil (- (frame-width) 
2372                                  (floor (* (frame-width) (nth 1 (car hor)))))
2373                           t)
2374             (setq hor (cdr hor))))
2375
2376       ;; Go through the rules and eval the elements that are to be
2377       ;; evaled.  
2378       (while hor
2379         (if (setq val (if (vectorp (car hor)) (car hor) (eval (car hor))))
2380             (progn
2381               ;; Expand short buffer name.
2382               (setq w (aref val 0))
2383               (and (setq w (cdr (assq w gnus-window-to-buffer)))
2384                    (progn
2385                      (setq val (apply 'vector (mapcar (lambda (v) v) val)))
2386                      (aset val 0 w)))
2387               (setq ohor (cons val ohor))))
2388         (setq hor (cdr hor)))
2389       (setq rule (cons (nreverse ohor) rule))
2390       (setq r (cdr r)))
2391     (setq rule (nreverse rule))
2392
2393     ;; We tally the window sizes.
2394     (setq total (window-height))
2395     (while rule
2396       (setq hor (car rule))
2397       (if (and (listp (car hor)) (eq (car (car hor)) 'horizontal))
2398           (setq hor (cdr hor)))
2399       (setq sub 0)
2400       (while hor
2401         (setq rel (aref (car hor) 1)
2402               heights (cons
2403                        (cond ((and (floatp rel) (= 1.0 rel))
2404                               'x)
2405                              ((integerp rel)
2406                               rel)
2407                              (t
2408                               (max (floor (* total rel)) 4)))
2409                        heights)
2410               sub (+ sub (if (numberp (car heights)) (car heights) 0))
2411               hor (cdr hor)))
2412       (setq heights (nreverse heights)
2413             hor (car rule))
2414
2415       ;; We then go through these heighs and create windows for them.
2416       (while heights
2417         (setq height (car heights)
2418               heights (cdr heights))
2419         (and (eq height 'x)
2420              (setq height (- total sub)))
2421         (and heights
2422              (split-window nil height))
2423         (setq to-buf (aref (car hor) 0))
2424         (switch-to-buffer 
2425          (cond ((not to-buf)
2426                 in-buf)
2427                ((symbolp to-buf)
2428                 (symbol-value (aref (car hor) 0)))
2429                (t
2430                 (aref (car hor) 0))))
2431         (and (> (length (car hor)) 2)
2432              (eq (aref (car hor) 2) 'point)
2433              (setq jump-buffer (current-buffer)))
2434         (other-window 1)
2435         (setq hor (cdr hor)))
2436       
2437       (setq rule (cdr rule)))
2438
2439     ;; Finally, we pop to the buffer that's supposed to have point. 
2440     (or jump-buffer (error "Missing `point' in spec for %s" setting))
2441
2442     (select-window (get-buffer-window jump-buffer))
2443     (set-buffer jump-buffer)))
2444       
2445 (defun gnus-remove-some-windows ()
2446   (let ((buffers gnus-window-to-buffer)
2447         (first t)
2448         buf)
2449     (while buffers
2450       (setq buf (cdr (car buffers)))
2451       (if (symbolp buf)
2452           (setq buf (and (boundp buf) (symbol-value buf))))
2453       (and buf 
2454            (get-buffer-window buf)
2455            (progn
2456              (set-buffer buf)
2457              (if first
2458                  (progn
2459                    (switch-to-buffer nntp-server-buffer)
2460                    (setq first nil))
2461                (delete-window (get-buffer-window buf)))))
2462       (setq buffers (cdr buffers)))
2463     (set-buffer nntp-server-buffer)))
2464
2465 (defun gnus-version ()
2466   "Version numbers of this version of Gnus."
2467   (interactive)
2468   (let ((methods gnus-valid-select-methods)
2469         (mess gnus-version)
2470         meth)
2471     ;; Go through all the legal select methods and add their version
2472     ;; numbers to the total version string. Only the backends that are
2473     ;; currently in use will have their message numbers taken into
2474     ;; consideration. 
2475     (while methods
2476       (setq meth (intern (concat (car (car methods)) "-version")))
2477       (and (boundp meth)
2478            (stringp (symbol-value meth))
2479            (setq mess (concat mess "; " (symbol-value meth))))
2480       (setq methods (cdr methods)))
2481     (gnus-message 2 mess)))
2482
2483 (defun gnus-info-find-node ()
2484   "Find Info documentation of Gnus."
2485   (interactive)
2486   ;; Enlarge info window if needed.
2487   (let ((mode major-mode))
2488     (gnus-configure-windows 'info)
2489     (Info-goto-node (car (cdr (assq mode gnus-info-nodes))))))
2490
2491 (defun gnus-bug ()
2492   "Send a bug report to the Gnus maintainers."
2493   (interactive)
2494   (let ((winconf (current-window-configuration)))
2495     (delete-other-windows)
2496     (switch-to-buffer "*Gnus Bug Help*")
2497     (erase-buffer)
2498     (insert gnus-bug-message)
2499     (goto-char (point-min))
2500     (pop-to-buffer "*Gnus Bug*")
2501     (erase-buffer)
2502     (mail-mode)
2503     (mail-setup gnus-maintainer nil nil nil nil nil)
2504     (make-local-variable 'gnus-prev-winconf)
2505     (setq gnus-prev-winconf winconf)
2506     (goto-char (point-min))
2507     (re-search-forward (concat "^" (regexp-quote mail-header-separator) "$"))
2508     (forward-line 1)
2509     (insert (format "%s\n%s\n\n\n\n\n" (gnus-version) (emacs-version)))
2510     (let ((b (point)))
2511       (gnus-debug)
2512       (goto-char (- b 3)))
2513     (message "")))
2514
2515 (defun gnus-debug ()
2516   "Attemps to go through the Gnus source file and report what variables have been changed.
2517 The source file has to be in the Emacs load path."
2518   (interactive)
2519   (let ((files '("gnus.el" "gnus-msg.el" "gnus-score.el"))
2520         file dirs expr olist)
2521     (save-excursion
2522       (set-buffer (get-buffer-create " *gnus bug info*"))
2523       (buffer-disable-undo (current-buffer))
2524       (message "Please wait while we snoop your variables...")
2525       (sit-for 0)
2526       (while files
2527         (erase-buffer)
2528         (setq dirs load-path)
2529         (while dirs
2530           (if (or (not (car dirs))
2531                   (not (stringp (car dirs)))
2532                   (not (file-exists-p 
2533                         (setq file (concat (file-name-as-directory 
2534                                             (car dirs)) (car files))))))
2535               (setq dirs (cdr dirs))
2536             (setq dirs nil)
2537             (insert-file-contents file)
2538             (goto-char (point-min))
2539             (or (re-search-forward "^;;* Internal variables" nil t)
2540                 (error "Malformed sources in file %s" file))
2541             (narrow-to-region (point-min) (point))
2542             (goto-char (point-min))
2543             (while (setq expr (condition-case () 
2544                                   (read (current-buffer)) (error nil)))
2545               (and (eq (car expr) 'defvar)
2546                    (stringp (nth 3 expr))
2547                    (or (not (boundp (nth 1 expr)))
2548                        (not (equal (eval (nth 2 expr))
2549                                    (symbol-value (nth 1 expr)))))
2550                    (setq olist (cons (nth 1 expr) olist))))))
2551         (setq files (cdr files)))
2552       (kill-buffer (current-buffer)))
2553     (insert "------------------- Environment follows -------------------\n\n")
2554     (while olist
2555       (if (boundp (car olist))
2556           (insert "(setq " (symbol-name (car olist)) " '" 
2557                   (prin1-to-string (symbol-value (car olist))) ")\n")
2558         (insert ";; (makeunbound '" (symbol-name (car olist)) ")\n"))
2559       (setq olist (cdr olist)))
2560     (insert "\n\n")))
2561
2562 (defun gnus-overload-functions (&optional overloads)
2563   "Overload functions specified by optional argument OVERLOADS.
2564 If nothing is specified, use the variable gnus-overload-functions."
2565   (let ((defs nil)
2566         (overloads (or overloads gnus-overload-functions)))
2567     (while overloads
2568       (setq defs (car overloads))
2569       (setq overloads (cdr overloads))
2570       ;; Load file before overloading function if necessary.  Make
2571       ;; sure we cannot use `require' always.
2572       (and (not (fboundp (car defs)))
2573            (car (cdr (cdr defs)))
2574            (load (car (cdr (cdr defs))) nil 'nomessage))
2575       (fset (car defs) (car (cdr defs))))))
2576
2577 (defun gnus-replace-chars-in-string (string from to)
2578   "Replace characters in STRING from FROM to TO."
2579   (let ((string (substring string 0))   ;Copy string.
2580         (len (length string))
2581         (idx 0))
2582     ;; Replace all occurrences of FROM with TO.
2583     (while (< idx len)
2584       (if (= (aref string idx) from)
2585           (aset string idx to))
2586       (setq idx (1+ idx)))
2587     string))
2588
2589 (defun gnus-days-between (date1 date2)
2590   ;; Return the number of days between date1 and date2.
2591   (- (gnus-day-number date1) (gnus-day-number date2)))
2592
2593 (defun gnus-day-number (date)
2594   (let ((dat (mapcar (lambda (s) (and s (string-to-int s)) )
2595                      (timezone-parse-date date))))
2596     (timezone-absolute-from-gregorian 
2597      (nth 1 dat) (nth 2 dat) (car dat))))
2598
2599 ;; Returns a floating point number that says how many seconds have
2600 ;; lapsed between Jan 1 12:00:00 1970 and DATE.
2601 (defun gnus-seconds-since-epoch (date)
2602   (let* ((tdate (mapcar (lambda (ti) (and ti (string-to-int ti)))
2603                         (timezone-parse-date date)))
2604          (ttime (mapcar (lambda (ti) (and ti (string-to-int ti)))
2605                         (timezone-parse-time
2606                          (aref (timezone-parse-date date) 3))))
2607          (edate (mapcar (lambda (ti) (and ti (string-to-int ti)))
2608                         (timezone-parse-date "Jan 1 12:00:00 1970")))
2609          (tday (- (timezone-absolute-from-gregorian 
2610                    (nth 1 tdate) (nth 2 tdate) (nth 0 tdate))
2611                   (timezone-absolute-from-gregorian 
2612                    (nth 1 edate) (nth 2 edate) (nth 0 edate)))))
2613     (+ (nth 2 ttime)
2614        (* (nth 1 ttime) 60)
2615        (* 1.0 (nth 0 ttime) 60 60)
2616        (* 1.0 tday 60 60 24))))
2617
2618 (defun gnus-file-newer-than (file date)
2619   (let ((fdate (nth 5 (file-attributes file))))
2620     (or (> (car fdate) (car date))
2621         (and (= (car fdate) (car date))
2622              (> (nth 1 fdate) (nth 1 date))))))
2623
2624 ;; Two silly functions to ensure that all `y-or-n-p' questions clear
2625 ;; the echo area.
2626 (defun gnus-y-or-n-p (prompt)
2627   (prog1
2628       (y-or-n-p prompt)
2629     (message "")))
2630
2631 (defun gnus-yes-or-no-p (prompt)
2632   (prog1
2633       (yes-or-no-p prompt)
2634     (message "")))
2635
2636 ;; Check whether to use long file names.
2637 (defun gnus-use-long-file-name (symbol)
2638   ;; The variable has to be set...
2639   (and gnus-use-long-file-name
2640        ;; If it isn't a list, then we return t.
2641        (or (not (listp gnus-use-long-file-name))
2642            ;; If it is a list, and the list contains `symbol', we
2643            ;; return nil.  
2644            (not (memq symbol gnus-use-long-file-name)))))
2645
2646 ;; I suspect there's a better way, but I haven't taken the time to do
2647 ;; it yet. -erik selberg@cs.washington.edu
2648 (defun gnus-dd-mmm (messy-date)
2649   "Return a string like DD-MMM from a big messy string"
2650   (let ((datevec (timezone-parse-date messy-date)))
2651     (format "%2s-%s"
2652             (or (aref datevec 2) "??")
2653             (capitalize
2654              (or (car 
2655                   (nth (1- (string-to-number (aref datevec 1)))
2656                        timezone-months-assoc))
2657                  "???")))))
2658
2659 ;; Make a hash table (default and minimum size is 255).
2660 ;; Optional argument HASHSIZE specifies the table size.
2661 (defun gnus-make-hashtable (&optional hashsize)
2662   (make-vector (if hashsize (max (gnus-create-hash-size hashsize) 255) 255) 0))
2663
2664 ;; Make a number that is suitable for hashing; bigger than MIN and one
2665 ;; less than 2^x.
2666 (defun gnus-create-hash-size (min)
2667   (let ((i 1))
2668     (while (< i min)
2669       (setq i (* 2 i)))
2670     (1- i)))
2671
2672 ;; Show message if message has a lower level than `gnus-verbose'. 
2673 ;; Guide-line for numbers:
2674 ;; 1 - error messages, 3 - non-serious error messages, 5 - messages
2675 ;; for things that take a long time, 7 - not very important messages
2676 ;; on stuff, 9 - messages inside loops.
2677 (defun gnus-message (level &rest args)
2678   (if (<= level gnus-verbose)
2679       (apply 'message args)
2680     ;; We have to do this format thingie here even if the result isn't
2681     ;; shown - the return value has to be the same as the return value
2682     ;; from `message'.
2683     (apply 'format args)))
2684
2685 ;; Generate a unique new group name.
2686 (defun gnus-generate-new-group-name (leaf)
2687   (let ((name leaf)
2688         (num 0))
2689     (while (gnus-gethash name gnus-newsrc-hashtb)
2690       (setq name (concat leaf "<" (int-to-string (setq num (1+ num))) ">")))
2691     name))
2692
2693 (defun gnus-find-file-noselect (file &optional force)
2694   "Does vaguely the same as find-file-noselect. No hooks are run."
2695   (let (buf insert)
2696     (if (setq buf (get-file-buffer file))
2697         (setq insert force)
2698       (setq buf (create-file-buffer file))
2699       (setq insert t))
2700     (if (not insert)
2701         buf
2702       (save-excursion
2703         (set-buffer buf)
2704         (erase-buffer)
2705         (and (file-readable-p file)
2706              (insert-file-contents file))
2707         (set-visited-file-name file)
2708         (set-buffer-modified-p nil)
2709         (current-buffer)))))
2710
2711 ;;; List and range functions
2712
2713 (defun gnus-last-element (list)
2714   "Return last element of LIST."
2715   (while (cdr list)
2716     (setq list (cdr list)))
2717   (car list))
2718
2719 (defun gnus-copy-sequence (list)
2720   "Do a complete, total copy of a list."
2721   (if (and (consp list) (not (consp (cdr list))))
2722       (cons (car list) (cdr list))
2723     (mapcar (lambda (elem) (if (consp elem) 
2724                                (if (consp (cdr elem))
2725                                    (gnus-copy-sequence elem)
2726                                  (cons (car elem) (cdr elem)))
2727                              elem))
2728             list)))
2729
2730 (defun gnus-set-difference (list1 list2)
2731   "Return a list of elements of LIST1 that do not appear in LIST2."
2732   (let ((list1 (copy-sequence list1)))
2733     (while list2
2734       (setq list1 (delq (car list2) list1))
2735       (setq list2 (cdr list2)))
2736     list1))
2737
2738 (defun gnus-sorted-complement (list1 list2)
2739   "Return a list of elements of LIST1 that do not appear in LIST2.
2740 Both lists have to be sorted over <."
2741   (let (out)
2742     (if (or (null list1) (null list2))
2743         (or list1 list2)
2744       (while (and list1 list2)
2745         (cond ((= (car list1) (car list2))
2746                (setq list1 (cdr list1)
2747                      list2 (cdr list2)))
2748               ((< (car list1) (car list2))
2749                (setq out (cons (car list1) out))
2750                (setq list1 (cdr list1)))
2751               (t
2752                (setq out (cons (car list2) out))
2753                (setq list2 (cdr list2)))))
2754       (nconc (nreverse out) (or list1 list2)))))
2755
2756 (defun gnus-intersection (list1 list2)      
2757   (let ((result nil))
2758     (while list2
2759       (if (memq (car list2) list1)
2760           (setq result (cons (car list2) result)))
2761       (setq list2 (cdr list2)))
2762     result))
2763
2764 (defun gnus-sorted-intersection (list1 list2)
2765   ;; LIST1 and LIST2 have to be sorted over <.
2766   (let (out)
2767     (while (and list1 list2)
2768       (cond ((= (car list1) (car list2))
2769              (setq out (cons (car list1) out)
2770                    list1 (cdr list1)
2771                    list2 (cdr list2)))
2772             ((< (car list1) (car list2))
2773              (setq list1 (cdr list1)))
2774             (t
2775              (setq list2 (cdr list2)))))
2776     (nreverse out)))
2777
2778 (defun gnus-set-sorted-intersection (list1 list2)
2779   ;; LIST1 and LIST2 have to be sorted over <.
2780   ;; This function modifies LIST1.
2781   (let* ((top (cons nil list1))
2782          (prev top))
2783   (while (and list1 list2)
2784     (cond ((= (car list1) (car list2))
2785            (setq prev list1
2786                  list1 (cdr list1)
2787                  list2 (cdr list2)))
2788           ((< (car list1) (car list2))
2789            (setcdr prev (cdr list1))
2790            (setq list1 (cdr list1)))
2791           (t
2792            (setq list2 (cdr list2)))))
2793   (setcdr prev nil)
2794   (cdr top)))
2795
2796 (defun gnus-compress-sequence (numbers &optional always-list)
2797   "Convert list of numbers to a list of ranges or a single range.
2798 If ALWAYS-LIST is non-nil, this function will always release a list of
2799 ranges."
2800   (let* ((first (car numbers))
2801          (last (car numbers))
2802          result)
2803     (if (null numbers)
2804         nil
2805       (if (not (listp (cdr numbers)))
2806           numbers
2807         (while numbers
2808           (cond ((= last (car numbers)) nil) ;Omit duplicated number
2809                 ((= (1+ last) (car numbers)) ;Still in sequence
2810                  (setq last (car numbers)))
2811                 (t                      ;End of one sequence
2812                  (setq result 
2813                        (cons (if (= first last) first
2814                                (cons first last)) result))
2815                  (setq first (car numbers))
2816                  (setq last  (car numbers))))
2817           (setq numbers (cdr numbers)))
2818         (if (and (not always-list) (null result))
2819             (if (= first last) (list first) (cons first last))
2820           (nreverse (cons (if (= first last) first (cons first last))
2821                           result)))))))
2822
2823 (defalias 'gnus-uncompress-sequence 'gnus-uncompress-range)
2824 (defun gnus-uncompress-range (ranges)
2825   "Expand a list of ranges into a list of numbers.
2826 RANGES is either a single range on the form `(num . num)' or a list of
2827 these ranges."
2828   (let (first last result)
2829     (cond 
2830      ((null ranges)
2831       nil)
2832      ((not (listp (cdr ranges)))
2833       (setq first (car ranges))
2834       (setq last (cdr ranges))
2835       (while (<= first last)
2836         (setq result (cons first result))
2837         (setq first (1+ first)))
2838       (nreverse result))
2839      (t
2840       (while ranges
2841         (if (atom (car ranges))
2842             (if (numberp (car ranges))
2843                 (setq result (cons (car ranges) result)))
2844           (setq first (car (car ranges)))
2845           (setq last  (cdr (car ranges)))
2846           (while (<= first last)
2847             (setq result (cons first result))
2848             (setq first (1+ first))))
2849         (setq ranges (cdr ranges)))
2850       (nreverse result)))))
2851
2852 (defun gnus-add-to-range (ranges list)
2853   "Return a list of ranges that has all articles from both RANGES and LIST.
2854 Note: LIST has to be sorted over `<'."
2855   (if (not ranges)
2856       (gnus-compress-sequence list t)
2857     (setq list (copy-sequence list))
2858     (or (listp (cdr ranges))
2859         (setq ranges (list ranges)))
2860     (let ((out ranges)
2861           ilist lowest highest temp)
2862       (while (and ranges list)
2863         (setq ilist list)
2864         (setq lowest (or (and (atom (car ranges)) (car ranges))
2865                          (car (car ranges))))
2866         (while (and list (cdr list) (< (car (cdr list)) lowest))
2867           (setq list (cdr list)))
2868         (if (< (car ilist) lowest)
2869             (progn
2870               (setq temp list)
2871               (setq list (cdr list))
2872               (setcdr temp nil)
2873               (setq out (nconc (gnus-compress-sequence ilist t) out))))
2874         (setq highest (or (and (atom (car ranges)) (car ranges))
2875                           (cdr (car ranges))))
2876         (while (and list (<= (car list) highest))
2877           (setq list (cdr list)))
2878         (setq ranges (cdr ranges)))
2879       (if list
2880           (setq out (nconc (gnus-compress-sequence list t) out)))
2881       (setq out (sort out (lambda (r1 r2) 
2882                             (< (or (and (atom r1) r1) (car r1))
2883                                (or (and (atom r2) r2) (car r2))))))
2884       (setq ranges out)
2885       (while ranges
2886         (if (atom (car ranges))
2887             (if (cdr ranges)
2888                 (if (atom (car (cdr ranges)))
2889                     (if (= (1+ (car ranges)) (car (cdr ranges)))
2890                         (progn
2891                           (setcar ranges (cons (car ranges) 
2892                                                (car (cdr ranges))))
2893                           (setcdr ranges (cdr (cdr ranges)))))
2894                   (if (= (1+ (car ranges)) (car (car (cdr ranges))))
2895                       (progn
2896                         (setcar (car (cdr ranges)) (car ranges))
2897                         (setcar ranges (car (cdr ranges)))
2898                         (setcdr ranges (cdr (cdr ranges)))))))
2899           (if (cdr ranges)
2900               (if (atom (car (cdr ranges)))
2901                   (if (= (1+ (cdr (car ranges))) (car (cdr ranges)))
2902                       (progn
2903                         (setcdr (car ranges) (car (cdr ranges)))
2904                         (setcdr ranges (cdr (cdr ranges)))))
2905                 (if (= (1+ (cdr (car ranges))) (car (car (cdr ranges))))
2906                     (progn
2907                       (setcdr (car ranges) (cdr (car (cdr ranges))))
2908                       (setcdr ranges (cdr (cdr ranges))))))))
2909         (setq ranges (cdr ranges)))
2910       out)))
2911
2912 (defun gnus-remove-from-range (ranges list)
2913   "Return a list of ranges that has all articles from LIST removed from RANGES.
2914 Note: LIST has to be sorted over `<'."
2915   ;; !!! This function shouldn't look like this, but I've got a headache.
2916   (gnus-compress-sequence 
2917    (gnus-sorted-complement
2918     (gnus-uncompress-range ranges) list)))
2919
2920 (defun gnus-member-of-range (number ranges)
2921   (if (not (listp (cdr ranges)))
2922       (and (>= number (car ranges)) 
2923            (<= number (cdr ranges)))
2924     (let ((not-stop t))
2925       (while (and ranges 
2926                   (if (numberp (car ranges))
2927                       (>= number (car ranges))
2928                     (>= number (car (car ranges))))
2929                   not-stop)
2930         (if (if (numberp (car ranges))
2931                 (= number (car ranges))
2932               (and (>= number (car (car ranges)))
2933                    (<= number (cdr (car ranges)))))
2934             (setq not-stop nil))
2935         (setq ranges (cdr ranges)))
2936       (not not-stop))))
2937
2938 \f
2939 ;;;
2940 ;;; Gnus group mode
2941 ;;;
2942
2943 (defvar gnus-group-mode-map nil)
2944 (defvar gnus-group-group-map nil)
2945 (defvar gnus-group-mark-map nil)
2946 (defvar gnus-group-list-map nil)
2947 (defvar gnus-group-sub-map nil)
2948 (put 'gnus-group-mode 'mode-class 'special)
2949
2950 (if gnus-group-mode-map
2951     nil
2952   (setq gnus-group-mode-map (make-keymap))
2953   (suppress-keymap gnus-group-mode-map)
2954   (define-key gnus-group-mode-map " " 'gnus-group-read-group)
2955   (define-key gnus-group-mode-map "=" 'gnus-group-select-group)
2956   (define-key gnus-group-mode-map "\r" 'gnus-group-select-group)
2957   (define-key gnus-group-mode-map "j" 'gnus-group-jump-to-group)
2958   (define-key gnus-group-mode-map "n" 'gnus-group-next-unread-group)
2959   (define-key gnus-group-mode-map "p" 'gnus-group-prev-unread-group)
2960   (define-key gnus-group-mode-map "\177" 'gnus-group-prev-unread-group)
2961   (define-key gnus-group-mode-map "N" 'gnus-group-next-group)
2962   (define-key gnus-group-mode-map "P" 'gnus-group-prev-group)
2963   (define-key gnus-group-mode-map "\M-n" 'gnus-group-next-unread-group-same-level)
2964   (define-key gnus-group-mode-map "\M-p" 'gnus-group-prev-unread-group-same-level)
2965   (define-key gnus-group-mode-map "," 'gnus-group-best-unread-group)
2966   (define-key gnus-group-mode-map "." 'gnus-group-first-unread-group)
2967   (define-key gnus-group-mode-map "u" 'gnus-group-unsubscribe-current-group)
2968   (define-key gnus-group-mode-map "U" 'gnus-group-unsubscribe-group)
2969   (define-key gnus-group-mode-map "c" 'gnus-group-catchup-current)
2970   (define-key gnus-group-mode-map "C" 'gnus-group-catchup-current-all)
2971   (define-key gnus-group-mode-map "l" 'gnus-group-list-groups)
2972   (define-key gnus-group-mode-map "L" 'gnus-group-list-all-groups)
2973   (define-key gnus-group-mode-map "m" 'gnus-group-mail)
2974   (define-key gnus-group-mode-map "g" 'gnus-group-get-new-news)
2975   (define-key gnus-group-mode-map "\M-g" 'gnus-group-get-new-news-this-group)
2976   (define-key gnus-group-mode-map "R" 'gnus-group-restart)
2977   (define-key gnus-group-mode-map "r" 'gnus-group-read-init-file)
2978   (define-key gnus-group-mode-map "B" 'gnus-group-browse-foreign-server)
2979   (define-key gnus-group-mode-map "b" 'gnus-group-check-bogus-groups)
2980   (define-key gnus-group-mode-map "F" 'gnus-find-new-newsgroups)
2981   (define-key gnus-group-mode-map "\C-c\C-d" 'gnus-group-describe-group)
2982   (define-key gnus-group-mode-map "\M-d" 'gnus-group-describe-all-groups)
2983   (define-key gnus-group-mode-map "\C-c\C-a" 'gnus-group-apropos)
2984   (define-key gnus-group-mode-map "\C-c\M-C-a" 'gnus-group-description-apropos)
2985   (define-key gnus-group-mode-map "a" 'gnus-group-post-news)
2986   (define-key gnus-group-mode-map "\ek" 'gnus-group-edit-local-kill)
2987   (define-key gnus-group-mode-map "\eK" 'gnus-group-edit-global-kill)
2988   (define-key gnus-group-mode-map "\C-k" 'gnus-group-kill-group)
2989   (define-key gnus-group-mode-map "\C-y" 'gnus-group-yank-group)
2990   (define-key gnus-group-mode-map "\C-w" 'gnus-group-kill-region)
2991   (define-key gnus-group-mode-map "\C-x\C-t" 'gnus-group-transpose-groups)
2992   (define-key gnus-group-mode-map "\C-c\C-l" 'gnus-group-list-killed)
2993   (define-key gnus-group-mode-map "\C-c\C-x" 'gnus-group-expire-articles)
2994   (define-key gnus-group-mode-map "\C-c\M-\C-x" 'gnus-group-expire-all-groups)
2995   (define-key gnus-group-mode-map "V" 'gnus-version)
2996   (define-key gnus-group-mode-map "s" 'gnus-group-save-newsrc)
2997   (define-key gnus-group-mode-map "z" 'gnus-group-suspend)
2998   (define-key gnus-group-mode-map "Z" 'gnus-group-clear-dribble)
2999   (define-key gnus-group-mode-map "q" 'gnus-group-exit)
3000   (define-key gnus-group-mode-map "Q" 'gnus-group-quit)
3001   (define-key gnus-group-mode-map "\M-f" 'gnus-group-fetch-faq)
3002   (define-key gnus-group-mode-map "?" 'gnus-group-describe-briefly)
3003   (define-key gnus-group-mode-map "\C-c\C-i" 'gnus-info-find-node)
3004   (define-key gnus-group-mode-map "\M-e" 'gnus-group-edit-group-method)
3005   (define-key gnus-group-mode-map "^" 'gnus-group-enter-server-mode)
3006   (define-key gnus-group-mode-map gnus-mouse-2 'gnus-mouse-pick-group)
3007   (define-key gnus-group-mode-map "<" 'beginning-of-buffer)
3008   (define-key gnus-group-mode-map ">" 'end-of-buffer)
3009   (define-key gnus-group-mode-map "\C-c\C-b" 'gnus-bug)
3010   (define-key gnus-group-mode-map "\C-c\C-s" 'gnus-group-sort-groups)
3011
3012   (define-key gnus-group-mode-map "#" 'gnus-group-mark-group)
3013   (define-key gnus-group-mode-map "\M-#" 'gnus-group-unmark-group)
3014   (define-prefix-command 'gnus-group-mark-map)
3015   (define-key gnus-group-mode-map "M" 'gnus-group-mark-map)
3016   (define-key gnus-group-mark-map "m" 'gnus-group-mark-group)
3017   (define-key gnus-group-mark-map "u" 'gnus-group-unmark-group)
3018   (define-key gnus-group-mark-map "w" 'gnus-group-mark-region)
3019
3020   (define-prefix-command 'gnus-group-group-map)
3021   (define-key gnus-group-mode-map "G" 'gnus-group-group-map)
3022   (define-key gnus-group-group-map "d" 'gnus-group-make-directory-group)
3023   (define-key gnus-group-group-map "h" 'gnus-group-make-help-group)
3024   (define-key gnus-group-group-map "a" 'gnus-group-make-archive-group)
3025   (define-key gnus-group-group-map "k" 'gnus-group-make-kiboze-group)
3026   (define-key gnus-group-group-map "m" 'gnus-group-make-group)
3027   (define-key gnus-group-group-map "E" 'gnus-group-edit-group)
3028   (define-key gnus-group-group-map "e" 'gnus-group-edit-group-method)
3029   (define-key gnus-group-group-map "p" 'gnus-group-edit-group-parameters)
3030   (define-key gnus-group-group-map "v" 'gnus-group-add-to-virtual)
3031   (define-key gnus-group-group-map "V" 'gnus-group-make-empty-virtual)
3032   (define-key gnus-group-group-map "D" 'gnus-group-enter-directory)
3033   (define-key gnus-group-group-map "f" 'gnus-group-make-doc-group)
3034   (define-key gnus-group-group-map "sb" 'gnus-group-brew-soup)
3035   (define-key gnus-group-group-map "sw" 'gnus-soup-save-areas)
3036   (define-key gnus-group-group-map "ss" 'gnus-soup-send-replies)
3037   (define-key gnus-group-group-map "sp" 'gnus-soup-pack-packet)
3038   (define-key gnus-group-group-map "sr" 'nnsoup-pack-replies)
3039
3040   (define-prefix-command 'gnus-group-list-map)
3041   (define-key gnus-group-mode-map "A" 'gnus-group-list-map)
3042   (define-key gnus-group-list-map "k" 'gnus-group-list-killed)
3043   (define-key gnus-group-list-map "z" 'gnus-group-list-zombies)
3044   (define-key gnus-group-list-map "s" 'gnus-group-list-groups)
3045   (define-key gnus-group-list-map "u" 'gnus-group-list-all-groups)
3046   (define-key gnus-group-list-map "a" 'gnus-group-apropos)
3047   (define-key gnus-group-list-map "d" 'gnus-group-description-apropos)
3048   (define-key gnus-group-list-map "m" 'gnus-group-list-matching)
3049   (define-key gnus-group-list-map "M" 'gnus-group-list-all-matching)
3050
3051   (define-prefix-command 'gnus-group-sub-map)
3052   (define-key gnus-group-mode-map "S" 'gnus-group-sub-map)
3053   (define-key gnus-group-sub-map "l" 'gnus-group-set-current-level)
3054   (define-key gnus-group-sub-map "t" 'gnus-group-unsubscribe-current-group)
3055   (define-key gnus-group-sub-map "s" 'gnus-group-unsubscribe-group)
3056   (define-key gnus-group-sub-map "k" 'gnus-group-kill-group)
3057   (define-key gnus-group-sub-map "y" 'gnus-group-yank-group)
3058   (define-key gnus-group-sub-map "w" 'gnus-group-kill-region)
3059   (define-key gnus-group-sub-map "z" 'gnus-group-kill-all-zombies))
3060
3061 (defun gnus-group-mode ()
3062   "Major mode for reading news.
3063
3064 All normal editing commands are switched off.
3065 \\<gnus-group-mode-map>
3066 The group buffer lists (some of) the groups available.  For instance,
3067 `\\[gnus-group-list-groups]' will list all subscribed groups with unread articles, while `\\[gnus-group-list-zombies]'
3068 lists all zombie groups. 
3069
3070 Groups that are displayed can be entered with `\\[gnus-group-read-group]'.  To subscribe 
3071 to a group not displayed, type `\\[gnus-group-unsubscribe-group]'. 
3072
3073 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]'). 
3074
3075 The following commands are available:
3076
3077 \\{gnus-group-mode-map}"
3078   (interactive)
3079   (if gnus-visual (gnus-group-make-menu-bar))
3080   (kill-all-local-variables)
3081   (setq mode-line-modified "-- ")
3082   (make-local-variable 'mode-line-format)
3083   (setq mode-line-format (copy-sequence mode-line-format))
3084   (and (equal (nth 3 mode-line-format) "   ")
3085        (setcar (nthcdr 3 mode-line-format) ""))
3086   (setq major-mode 'gnus-group-mode)
3087   (setq mode-name "Group")
3088   (gnus-group-set-mode-line)
3089   (setq mode-line-process nil)
3090   (use-local-map gnus-group-mode-map)
3091   (buffer-disable-undo (current-buffer))
3092   (setq truncate-lines t)
3093   (setq buffer-read-only t)
3094   (run-hooks 'gnus-group-mode-hook))
3095
3096 (defun gnus-mouse-pick-group (e)
3097   (interactive "e")
3098   (mouse-set-point e)
3099   (gnus-group-read-group nil))
3100
3101 ;;;###autoload
3102 (defun gnus-no-server (&optional arg)
3103   "Read network news.
3104 If ARG is a positive number, Gnus will use that as the
3105 startup level. If ARG is nil, Gnus will be started at level 2. 
3106 If ARG is non-nil and not a positive number, Gnus will
3107 prompt the user for the name of an NNTP server to use.
3108 As opposed to `gnus', this command will not connect to the local server."
3109   (interactive "P")
3110   (gnus (or arg (1- gnus-level-default-subscribed)) t))
3111
3112 (defalias '\(ding\) 'gnus)
3113
3114 ;;;###autoload
3115 (defun gnus (&optional arg dont-connect)
3116   "Read network news.
3117 If ARG is non-nil and a positive number, Gnus will use that as the
3118 startup level. If ARG is non-nil and not a positive number, Gnus will
3119 prompt the user for the name of an NNTP server to use."
3120   (interactive "P")
3121   (if (get-buffer gnus-group-buffer)
3122       (progn
3123         (switch-to-buffer gnus-group-buffer)
3124         (gnus-group-get-new-news))
3125     (gnus-clear-system)
3126     (nnheader-init-server-buffer)
3127     (gnus-read-init-file)
3128     (let ((level (and arg (numberp arg) (> arg 0) arg))
3129           did-connect)
3130       (unwind-protect
3131           (progn
3132             (gnus-group-setup-buffer)
3133             (or dont-connect 
3134                 (setq did-connect
3135                       (gnus-start-news-server (and arg (not level))))))
3136         (if (and (not dont-connect) 
3137                  (not did-connect))
3138             (gnus-group-quit)
3139           (run-hooks 'gnus-startup-hook)
3140           ;; NNTP server is successfully open. 
3141           (gnus-update-format-specifications)
3142           (gnus-summary-make-display-table)
3143           (let ((buffer-read-only nil))
3144             (erase-buffer)
3145             (if (not gnus-inhibit-startup-message)
3146                 (progn
3147                   (gnus-group-startup-message)
3148                   (sit-for 0))))
3149           (gnus-setup-news nil level)
3150           (and gnus-use-dribble-file (gnus-dribble-open))
3151           (gnus-group-list-groups level)
3152           (gnus-configure-windows 'group))))))
3153
3154 (defun gnus-group-startup-message (&optional x y)
3155   "Insert startup message in current buffer."
3156   ;; Insert the message.
3157   (erase-buffer)
3158   (insert
3159    (format "
3160     %s
3161            A newsreader 
3162       for GNU Emacs
3163
3164         Based on GNUS 
3165              written by 
3166      Masanobu UMEDA
3167
3168     Lars Magne 
3169          Ingebrigtsen 
3170       larsi@ifi.uio.no
3171
3172            gnus-version))
3173   ;; And then hack it.
3174   ;; 18 is the longest line.
3175   (indent-rigidly (point-min) (point-max) 
3176                   (/ (max (- (window-width) (or x 28)) 0) 2))
3177   (goto-char (point-min))
3178   ;; +4 is fuzzy factor.
3179   (insert-char ?\n (/ (max (- (window-height) (or y 12)) 0) 2)))
3180
3181 (defun gnus-group-setup-buffer ()
3182   (or (get-buffer gnus-group-buffer)
3183       (progn
3184         (switch-to-buffer gnus-group-buffer)
3185         (gnus-add-current-to-buffer-list)
3186         (gnus-group-mode)
3187         (and gnus-carpal (gnus-carpal-setup-buffer 'group)))))
3188
3189 (defun gnus-group-list-groups (level &optional unread)
3190   "List newsgroups with level LEVEL or lower that have unread articles.
3191 Default is all subscribed groups.
3192 If argument UNREAD is non-nil, groups with no unread articles are also listed."
3193   (interactive (list (and current-prefix-arg
3194                           (prefix-numeric-value current-prefix-arg))))
3195   (if gnus-group-use-permanent-levels
3196       (progn
3197         (setq gnus-group-default-list-level 
3198               (or level gnus-group-default-list-level))
3199         (setq level (or gnus-group-default-list-level gnus-level-subscribed)))
3200     (setq level (or level gnus-group-default-list-level 
3201                     gnus-level-subscribed)))
3202   (gnus-group-setup-buffer)     ;May call from out of group buffer
3203   (let ((case-fold-search nil)
3204         (group (gnus-group-group-name)))
3205     (funcall gnus-group-prepare-function level unread nil)
3206     (if (zerop (buffer-size))
3207         (gnus-message 5 gnus-no-groups-message)
3208       (goto-char (point-min))
3209       (if (not group)
3210           ;; Go to the first group with unread articles.
3211           (gnus-group-search-forward nil nil nil t)
3212         ;; Find the right group to put point on. If the current group
3213         ;; has disapeared in the new listing, try to find the next
3214         ;; one. If no next one can be found, just leave point at the
3215         ;; first newsgroup in the buffer.
3216         (if (not (gnus-goto-char
3217                   (text-property-any (point-min) (point-max) 
3218                                      'gnus-group (intern group))))
3219             (let ((newsrc (nthcdr 3 (gnus-gethash group gnus-newsrc-hashtb))))
3220               (while (and newsrc
3221                           (not (gnus-goto-char 
3222                                 (text-property-any 
3223                                  (point-min) (point-max) 'gnus-group 
3224                                  (intern (car (car newsrc)))))))
3225                 (setq newsrc (cdr newsrc)))
3226               (or newsrc (progn (goto-char (point-max))
3227                                 (forward-line -1))))))
3228       ;; Adjust cursor point.
3229       (gnus-group-position-cursor))))
3230
3231 (defun gnus-group-prepare-flat (level &optional all lowest regexp) 
3232   "List all newsgroups with unread articles of level LEVEL or lower.
3233 If ALL is non-nil, list groups that have no unread articles.
3234 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher.
3235 If REGEXP, only list groups matching REGEXP."
3236   (set-buffer gnus-group-buffer)
3237   (let ((buffer-read-only nil)
3238         (newsrc (cdr gnus-newsrc-alist))
3239         (lowest (or lowest 1))
3240         info clevel unread group)
3241     (erase-buffer)
3242     (if (< lowest gnus-level-zombie)
3243         ;; List living groups.
3244         (while newsrc
3245           (setq info (car newsrc)
3246                 group (car info)
3247                 newsrc (cdr newsrc)
3248                 unread (car (gnus-gethash group gnus-newsrc-hashtb)))
3249           (and unread ; This group might be bogus
3250                (or (not regexp)
3251                    (string-match regexp group))
3252                (<= (setq clevel (car (cdr info))) level) 
3253                (>= clevel lowest)
3254                (or all            ; We list all groups?
3255                    (eq unread t)  ; We list unactivated groups
3256                    (> unread 0)   ; We list groups with unread articles
3257                    (cdr (assq 'tick (nth 3 info)))) ; And groups with tickeds
3258                (gnus-group-insert-group-line 
3259                 nil group (car (cdr info)) (nth 3 info) unread (nth 4 info)))))
3260
3261     ;; List dead groups.
3262     (and (>= level gnus-level-zombie) (<= lowest gnus-level-zombie)
3263          (gnus-group-prepare-flat-list-dead 
3264           (setq gnus-zombie-list (sort gnus-zombie-list 'string<)) 
3265           gnus-level-zombie ?Z
3266           regexp))
3267     (and (>= level gnus-level-killed) (<= lowest gnus-level-killed)
3268          (gnus-group-prepare-flat-list-dead 
3269           (setq gnus-killed-list (sort gnus-killed-list 'string<)) 
3270           gnus-level-killed ?K regexp))
3271
3272     (gnus-group-set-mode-line)
3273     (setq gnus-have-all-newsgroups all)
3274     (run-hooks 'gnus-group-prepare-hook)))
3275
3276 (defun gnus-group-prepare-flat-list-dead (groups level mark regexp)
3277   ;; List zombies and killed lists somehwat faster, which was
3278   ;; suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>. It does
3279   ;; this by ignoring the group format specification altogether.
3280   (let (group beg)
3281     (while groups
3282       (setq group (car groups)
3283             groups (cdr groups))
3284       (if (or (not regexp)
3285               (string-match regexp group))
3286           (progn
3287             (setq beg (point))
3288             (insert (format " %c     *: %s\n" mark group))
3289             (add-text-properties 
3290              beg (1+ beg) 
3291              (list 'gnus-group (intern group)
3292                    'gnus-unread t
3293                    'gnus-level level)))))))
3294
3295 (defun gnus-group-real-name (group)
3296   "Find the real name of a foreign newsgroup."
3297   (if (string-match ":[^:]+$" group)
3298       (substring group (1+ (match-beginning 0)))
3299     group))
3300
3301 (defun gnus-group-prefixed-name (group method)
3302   "Return the whole name from GROUP and METHOD."
3303   (and (stringp method) (setq method (gnus-server-to-method method)))
3304   (concat (format "%s" (car method))
3305           (if (and 
3306                (assoc (format "%s" (car method)) (gnus-methods-using 'address))
3307                (not (string= (nth 1 method) "")))
3308               (concat "+" (nth 1 method)))
3309           ":" group))
3310
3311 (defun gnus-group-real-prefix (group)
3312   "Return the prefix of the current group name."
3313   (if (string-match "^[^:]+:" group)
3314       (substring group 0 (match-end 0))
3315     ""))
3316
3317 (defun gnus-group-method-name (group)
3318   "Return the method used for selecting GROUP."
3319   (let ((prefix (gnus-group-real-prefix group)))
3320     (if (equal prefix "")
3321         gnus-select-method
3322       (if (string-match "^[^\\+]+\\+" prefix)
3323           (list (intern (substring prefix 0 (1- (match-end 0))))
3324                 (substring prefix (match-end 0) (1- (length prefix))))
3325         (list (intern (substring prefix 0 (1- (length prefix)))) "")))))
3326
3327 (defun gnus-group-foreign-p (group)
3328   "Return nil if GROUP is native, non-nil if it is foreign."
3329   (string-match ":" group))
3330
3331 (defun gnus-group-set-info (info &optional method-only-group part)
3332   (let* ((entry (gnus-gethash
3333                  (or method-only-group (car info)) gnus-newsrc-hashtb))
3334          (part-info info)
3335          (info (if method-only-group (nth 2 entry) info)))
3336     (if (not method-only-group)
3337         ()
3338       (or entry
3339           (error "Trying to change non-existent group %s" method-only-group))
3340       ;; We have recevied parts of the actual group info - either the
3341       ;; select method or the group parameters.  We first check
3342       ;; whether we have to extend the info, and if so, do that.
3343       (let ((len (length info))
3344             (total (if (eq part 'method) 5 6)))
3345         (and (< len total)
3346              (setcdr (nthcdr (1- len) info)
3347                      (make-list (- total len) nil)))
3348         ;; Then we enter the new info.
3349         (setcar (nthcdr (1- total) info) part-info)))
3350     ;; We uncompress some lists of marked articles.
3351     (let (marked)
3352       (if (not (setq marked (nth 3 info)))
3353           ()
3354         (while marked
3355           (or (eq 'score (car (car marked)))
3356               (eq 'bookmark (car (car marked)))
3357               (eq 'killed (car (car marked)))
3358               (setcdr (car marked) 
3359                       (gnus-uncompress-range (cdr (car marked)))))
3360           (setq marked (cdr marked)))))
3361     (if entry
3362         ()
3363       ;; This is a new group, so we just create it.
3364       (save-excursion
3365         (set-buffer gnus-group-buffer)
3366         (if (nth 4 info)
3367             ;; It's a foreign group...
3368             (gnus-group-make-group 
3369              (gnus-group-real-name (car info))
3370              (prin1-to-string (car (nth 4 info)))
3371              (nth 1 (nth 4 info)))
3372           ;; It's a native group.
3373           (gnus-group-make-group
3374            (car info)
3375            (prin1-to-string (car gnus-select-method))
3376            (nth 1 gnus-select-method)))
3377         (gnus-message 6 "Note: New group created")
3378         (setq entry 
3379               (gnus-gethash (gnus-group-prefixed-name 
3380                              (gnus-group-real-name (car info))
3381                              (or (nth 4 info) gnus-select-method))
3382                             gnus-newsrc-hashtb))))
3383     ;; Whether it was a new group or not, we now have the entry, so we
3384     ;; can do the update.
3385     (if entry
3386         (progn
3387           (setcar (nthcdr 2 entry) info)
3388           (if (and (not (eq (car entry) t)) 
3389                    (gnus-gethash (car info) gnus-active-hashtb))
3390               (let ((marked (nth 3 info)))
3391                 (setcar entry 
3392                         (max 0 (- (length (gnus-list-of-unread-articles 
3393                                            (car info)))
3394                                   (length (cdr (assq 'tick marked)))
3395                                   (length (cdr (assq 'dormant marked)))))))))
3396       (error "No such group: %s" (car info)))))
3397
3398 (defun gnus-group-set-method-info (group select-method)
3399   (gnus-group-set-info select-method group 'method))
3400
3401 (defun gnus-group-set-params-info (group params)
3402   (gnus-group-set-info params group 'params))
3403
3404 (defun gnus-group-update-group-line ()
3405   "This function updates the current line in the newsgroup buffer and
3406 moves the point to the colon."
3407   (let* ((buffer-read-only nil)
3408          (group (gnus-group-group-name))
3409          (entry (and group (gnus-gethash group gnus-newsrc-hashtb))))
3410     (if entry
3411         (gnus-dribble-enter 
3412          (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
3413                  ")")))
3414     (beginning-of-line)
3415     (delete-region (point) (progn (forward-line 1) (point)))
3416     (gnus-group-insert-group-line-info group)
3417     (forward-line -1)
3418     (gnus-group-position-cursor)))
3419
3420 (defun gnus-group-insert-group-line-info (group)
3421   (let ((entry (gnus-gethash group gnus-newsrc-hashtb)) 
3422         active info)
3423     (if entry
3424         (progn
3425           (setq info (nth 2 entry))
3426           (gnus-group-insert-group-line 
3427            nil group (nth 1 info) (nth 3 info) (car entry) (nth 4 info)))
3428       (setq active (gnus-gethash group gnus-active-hashtb))
3429       (gnus-group-insert-group-line 
3430        nil group (if (member group gnus-zombie-list) gnus-level-zombie
3431                    gnus-level-killed)
3432        nil (if active (- (1+ (cdr active)) (car active)) 0) nil))))
3433
3434 (defun gnus-group-insert-group-line (gformat group level marked number method)
3435   (let* ((gformat (or gformat gnus-group-line-format-spec))
3436          (active (gnus-gethash group gnus-active-hashtb))
3437          (number-total (if active (1+ (- (cdr active) (car active))) 0))
3438          (number-of-dormant (length (cdr (assq 'dormant marked))))
3439          (number-of-ticked (length (cdr (assq 'tick marked))))
3440          (number-of-ticked-and-dormant
3441           (+ number-of-ticked number-of-dormant))
3442          (number-of-unread-unticked 
3443           (if (numberp number) (int-to-string (max 0 number))
3444             "*"))
3445          (number-of-read
3446           (if (numberp number)
3447               (max 0 (- number-total number))
3448             "*"))
3449          (subscribed (cond ((<= level gnus-level-subscribed) ? )
3450                            ((<= level gnus-level-unsubscribed) ?U)
3451                            ((= level gnus-level-zombie) ?Z)
3452                            (t ?K)))
3453          (qualified-group (gnus-group-real-name group))
3454          (newsgroup-description 
3455           (if gnus-description-hashtb
3456               (or (gnus-gethash group gnus-description-hashtb) "")
3457             ""))
3458          (moderated (if (member group gnus-moderated-list) ?m ? ))
3459          (moderated-string (if (eq moderated ?m) "(m)" ""))
3460          (method (gnus-server-get-method group method))
3461          (news-server (or (car (cdr method)) ""))
3462          (news-method (or (car method) ""))
3463          (news-method-string 
3464           (if method (format "(%s:%s)" (car method) (car (cdr method))) ""))
3465          (marked (if (and 
3466                       (numberp number) 
3467                       (zerop number)
3468                       (> number-of-ticked 0))
3469                      ?* ? ))
3470          (number (if (eq number t) "*" (+ number number-of-dormant 
3471                                           number-of-ticked)))
3472          (process-marked (if (member qualified-group gnus-group-marked)
3473                              gnus-process-mark ? ))
3474          (buffer-read-only nil)
3475          b)
3476     (beginning-of-line)
3477     (setq b (point))
3478     ;; Insert the text.
3479     (insert (eval gformat))
3480
3481     (add-text-properties 
3482      b (1+ b) (list 'gnus-group (intern group)
3483                     'gnus-unread (if (numberp number)
3484                                      (string-to-int number-of-unread-unticked)
3485                                    t)
3486                     'gnus-marked marked
3487                     'gnus-level level))))
3488
3489 (defun gnus-group-update-group (group &optional visible-only)
3490   "Update newsgroup info of GROUP.
3491 If VISIBLE-ONLY is non-nil, the group won't be displayed if it isn't already."
3492   (save-excursion
3493     (set-buffer gnus-group-buffer)
3494     (let ((buffer-read-only nil)
3495           visible)
3496       (let ((entry (gnus-gethash group gnus-newsrc-hashtb)))
3497         (if entry
3498             (gnus-dribble-enter 
3499              (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
3500                      ")"))))
3501       ;; Buffer may be narrowed.
3502       (save-restriction
3503         (widen)
3504         ;; Search a line to modify.  If the buffer is large, the search
3505         ;; takes long time.  In most cases, current point is on the line
3506         ;; we are looking for.  So, first of all, check current line. 
3507         (if (or (progn
3508                   (beginning-of-line)
3509                   (eq (get-text-property (point) 'gnus-group)
3510                       (intern group)))
3511                 (progn
3512                   (gnus-goto-char 
3513                    (text-property-any 
3514                     (point-min) (point-max) 'gnus-group (intern group)))))
3515             ;; GROUP is listed in current buffer. So, delete old line.
3516             (progn
3517               (setq visible t)
3518               (beginning-of-line)
3519               (delete-region (point) (progn (forward-line 1) (point))))
3520           ;; No such line in the buffer, find out where it's supposed to
3521           ;; go, and insert it there (or at the end of the buffer).
3522           ;; Fix by Per Abrahamsen <amanda@iesd.auc.dk>.
3523           (or visible-only
3524               (let ((entry 
3525                      (cdr (cdr (gnus-gethash group gnus-newsrc-hashtb)))))
3526                 (while (and entry
3527                             (car entry)
3528                             (not
3529                              (gnus-goto-char
3530                               (text-property-any
3531                                (point-min) (point-max) 
3532                                'gnus-group (intern (car (car entry)))))))
3533                   (setq entry (cdr entry)))
3534                 (or entry (goto-char (point-max)))))))
3535       (if (or visible (not visible-only))
3536           (gnus-group-insert-group-line-info group))
3537       (gnus-group-set-mode-line))))
3538
3539 (defun gnus-group-set-mode-line ()
3540   (if (memq 'group gnus-updated-mode-lines)
3541       (let* ((gformat (or gnus-group-mode-line-format-spec
3542                           (setq gnus-group-mode-line-format-spec
3543                                 (gnus-parse-format 
3544                                  gnus-group-mode-line-format 
3545                                  gnus-group-mode-line-format-alist))))
3546              (news-server (car (cdr gnus-select-method)))
3547              (news-method (car gnus-select-method))
3548              (max-len 60)
3549              (mode-string (eval gformat)))
3550         (setq mode-string (eval gformat))
3551         (if (> (length mode-string) max-len) 
3552             (setq mode-string (substring mode-string 0 (- max-len 4))))
3553         (setq mode-line-buffer-identification mode-string)
3554         (set-buffer-modified-p t))))
3555
3556 (defun gnus-group-group-name ()
3557   "Get the name of the newsgroup on the current line."
3558   (let ((group (get-text-property (gnus-point-at-bol) 'gnus-group)))
3559     (and group (symbol-name group))))
3560
3561 (defun gnus-group-group-level ()
3562   "Get the level of the newsgroup on the current line."
3563   (get-text-property (gnus-point-at-bol) 'gnus-level))
3564
3565 (defun gnus-group-search-forward (&optional backward all level first-too)
3566   "Find the next newsgroup with unread articles.
3567 If BACKWARD is non-nil, find the previous newsgroup instead.
3568 If ALL is non-nil, just find any newsgroup.
3569 If LEVEL is non-nil, find group with level LEVEL, or higher if no such
3570 group exists.
3571 If FIRST-TOO, the current line is also eligible as a target."
3572   (let ((way (if backward -1 1))
3573         (low 10)
3574         (beg (point))
3575         pos found)
3576     (if (and backward (progn (beginning-of-line)) (bobp))
3577         nil
3578       (or first-too (forward-line way))
3579       (while (and 
3580               (not (eobp))
3581               (not (setq 
3582                     found 
3583                     (and (or all
3584                              (and
3585                               (let ((unread 
3586                                      (get-text-property (point) 'gnus-unread)))
3587                                 (or (eq unread t) (and unread (> unread 0))))
3588                               (let ((lev (get-text-property
3589                                           (point) 'gnus-level)))
3590                                 (and lev (<= (get-text-property 
3591                                               (point) 'gnus-level)
3592                                              gnus-level-subscribed)))))
3593                          (or (not level)
3594                              (let ((lev (get-text-property (point) 'gnus-level)))
3595                                (if (and lev (<= lev level))
3596                                    t
3597                                  (if (< lev low)
3598                                      (progn
3599                                        (setq low lev)
3600                                        (setq pos (point))))
3601                                  nil))))))
3602               (zerop (forward-line way)))))
3603     (if found 
3604         (progn (gnus-group-position-cursor) t)
3605       (if pos (goto-char pos) (goto-char beg))
3606       nil)))
3607
3608 ;;; Gnus group mode commands
3609
3610 ;; Group marking.
3611
3612 (defun gnus-group-mark-group (n &optional unmark)
3613   "Mark the current group."
3614   (interactive "p")
3615   (let ((buffer-read-only nil)
3616         group)
3617     (while 
3618         (and (> n 0) 
3619              (setq group (gnus-group-group-name))
3620              (progn
3621                (beginning-of-line)
3622                (forward-char 2)
3623                (delete-char 1)
3624                (if unmark
3625                    (progn
3626                      (insert " ")
3627                      (setq gnus-group-marked (delete group gnus-group-marked)))
3628                  (insert "#")
3629                  (setq gnus-group-marked
3630                        (cons group (delete group gnus-group-marked))))
3631                t)
3632              (zerop (gnus-group-next-group 1)))
3633       (setq n (1- n)))
3634     (gnus-summary-position-cursor)
3635     n))
3636
3637 (defun gnus-group-unmark-group (n)
3638   "Remove the mark from the current group."
3639   (interactive "p")
3640   (gnus-group-mark-group n 'unmark))
3641
3642 (defun gnus-group-mark-region (unmark beg end)
3643   "Mark all groups between point and mark.
3644 If UNMARK, remove the mark instead."
3645   (interactive "P\nr")
3646   (let ((num (count-lines beg end)))
3647     (save-excursion
3648       (goto-char beg)
3649       (- num (gnus-group-mark-group num unmark)))))
3650
3651 (defun gnus-group-remove-mark (group)
3652   (and (gnus-group-goto-group group)
3653        (save-excursion
3654          (gnus-group-mark-group 1 'unmark))))
3655
3656 ;; Return a list of groups to work on.  Take into consideration N (the
3657 ;; prefix) and the list of marked groups.
3658 (defun gnus-group-process-prefix (n)
3659   (cond (n
3660          (setq n (prefix-numeric-value n))
3661          ;; There is a prefix, so we return a list of the N next
3662          ;; groups. 
3663          (let ((way (if (< n 0) -1 1))
3664                (n (abs n))
3665                group groups)
3666            (save-excursion
3667              (while (and (> n 0)
3668                          (setq group (gnus-group-group-name)))
3669                (setq groups (cons group groups))
3670                (setq n (1- n))
3671                (forward-line way)))
3672            (nreverse groups)))
3673         (gnus-group-marked
3674          ;; No prefix, but a list of marked articles.
3675          (reverse gnus-group-marked))
3676         (t
3677          ;; Neither marked articles or a prefix, so we return the
3678          ;; current group.
3679          (let ((group (gnus-group-group-name)))
3680            (and group (list group))))))
3681
3682 ;; Selecting groups.
3683
3684 (defun gnus-group-read-group (all &optional no-article group)
3685   "Read news in this newsgroup.
3686 If argument ALL is non-nil, already read articles become readable.
3687 If optional argument NO-ARTICLE is non-nil, no article body is displayed."
3688   (interactive "P")
3689   (let ((group (or group (gnus-group-group-name)))
3690         number active marked entry)
3691     (or group (error "No group on current line"))
3692     (setq marked 
3693           (nth 3 (nth 2 (setq entry (gnus-gethash group gnus-newsrc-hashtb)))))
3694     ;; This group might be a dead group. In that case we have to get
3695     ;; the number of unread articles from `gnus-active-hashtb'.
3696     (if entry
3697         (setq number (car entry))
3698       (if (setq active (gnus-gethash group gnus-active-hashtb))
3699           (setq number (- (1+ (cdr active)) (car active)))))
3700     (gnus-summary-read-group 
3701      group (or all (and (numberp number) 
3702                         (zerop (+ number (length (cdr (assq 'tick marked)))
3703                                   (length (cdr (assq 'dormant marked)))))))
3704      no-article)))
3705
3706 (defun gnus-group-select-group (all)
3707   "Select this newsgroup.
3708 No article is selected automatically.
3709 If argument ALL is non-nil, already read articles become readable."
3710   (interactive "P")
3711   (gnus-group-read-group all t))
3712
3713 ;; Enter a group that is not in the group buffer. Non-nil is returned
3714 ;; if selection was successful.
3715 (defun gnus-group-read-ephemeral-group 
3716   (group method &optional activate quit-config)
3717   (let ((group (if (gnus-group-foreign-p group) group
3718                  (gnus-group-prefixed-name group method))))
3719     (gnus-sethash 
3720      group
3721      (list t nil (list group gnus-level-default-subscribed nil nil 
3722                        (append method
3723                                (list
3724                                 (list 'quit-config 
3725                                       (if quit-config quit-config
3726                                         (cons (current-buffer) 'summary)))))))
3727      gnus-newsrc-hashtb)
3728     (set-buffer gnus-group-buffer)
3729     (or (gnus-server-opened method)
3730         (gnus-open-server method)
3731         (error "Unable to contact server: %s" (gnus-status-message method)))
3732     (if activate (gnus-request-group group))
3733     (condition-case ()
3734         (gnus-group-read-group t t group)
3735       (error nil)
3736       (quit nil))
3737     (not (equal major-mode 'gnus-group-mode))))
3738   
3739 (defun gnus-group-jump-to-group (group)
3740   "Jump to newsgroup GROUP."
3741   (interactive 
3742    (list (completing-read 
3743           "Group: " gnus-active-hashtb nil (not (not gnus-read-active-file)))))
3744
3745   (if (equal group "")
3746       (error "Empty group name"))
3747
3748   (let ((b (text-property-any 
3749             (point-min) (point-max) 'gnus-group (intern group))))
3750     (if b
3751         ;; Either go to the line in the group buffer...
3752         (goto-char b)
3753       ;; ... or insert the line.
3754       (or
3755        (gnus-gethash group gnus-active-hashtb)
3756        (gnus-activate-newsgroup group)
3757        (error "%s error: %s" group (gnus-status-message group)))
3758
3759       (gnus-group-update-group group)
3760       (goto-char (text-property-any 
3761                   (point-min) (point-max) 'gnus-group (intern group)))))
3762   ;; Adjust cursor point.
3763   (gnus-group-position-cursor))
3764
3765 (defun gnus-group-goto-group (group)
3766   "Goto to newsgroup GROUP."
3767   (let ((b (text-property-any (point-min) (point-max) 
3768                               'gnus-group (intern group))))
3769     (and b (goto-char b))))
3770
3771 (defun gnus-group-next-group (n)
3772   "Go to next N'th newsgroup.
3773 If N is negative, search backward instead.
3774 Returns the difference between N and the number of skips actually
3775 done."
3776   (interactive "p")
3777   (gnus-group-next-unread-group n t))
3778
3779 (defun gnus-group-next-unread-group (n &optional all level)
3780   "Go to next N'th unread newsgroup.
3781 If N is negative, search backward instead.
3782 If ALL is non-nil, choose any newsgroup, unread or not.
3783 If LEVEL is non-nil, choose the next group with level LEVEL, or, if no
3784 such group can be found, the next group with a level higher than
3785 LEVEL.
3786 Returns the difference between N and the number of skips actually
3787 made."
3788   (interactive "p")
3789   (let ((backward (< n 0))
3790         (n (abs n)))
3791     (while (and (> n 0)
3792                 (gnus-group-search-forward 
3793                  backward (or (not gnus-group-goto-unread) all) level))
3794       (setq n (1- n)))
3795     (if (/= 0 n) (gnus-message 7 "No more%s newsgroups%s" (if all "" " unread")
3796                                (if level " on this level or higher" "")))
3797     n))
3798
3799 (defun gnus-group-prev-group (n)
3800   "Go to previous N'th newsgroup.
3801 Returns the difference between N and the number of skips actually
3802 done."
3803   (interactive "p")
3804   (gnus-group-next-unread-group (- n) t))
3805
3806 (defun gnus-group-prev-unread-group (n)
3807   "Go to previous N'th unread newsgroup.
3808 Returns the difference between N and the number of skips actually
3809 done."  
3810   (interactive "p")
3811   (gnus-group-next-unread-group (- n)))
3812
3813 (defun gnus-group-next-unread-group-same-level (n)
3814   "Go to next N'th unread newsgroup on the same level.
3815 If N is negative, search backward instead.
3816 Returns the difference between N and the number of skips actually
3817 done."
3818   (interactive "p")
3819   (gnus-group-next-unread-group n t (gnus-group-group-level))
3820   (gnus-group-position-cursor))
3821
3822 (defun gnus-group-prev-unread-group-same-level (n)
3823   "Go to next N'th unread newsgroup on the same level.
3824 Returns the difference between N and the number of skips actually
3825 done."
3826   (interactive "p")
3827   (gnus-group-next-unread-group (- n) t (gnus-group-group-level))
3828   (gnus-group-position-cursor))
3829
3830 (defun gnus-group-best-unread-group (&optional exclude-group)
3831   "Go to the group with the highest level.
3832 If EXCLUDE-GROUP, do not go to that group."
3833   (interactive)
3834   (goto-char (point-min))
3835   (let ((best 100000)
3836         unread best-point)
3837     (while (setq unread (get-text-property (point) 'gnus-unread))
3838       (if (and (numberp unread) (> unread 0))
3839           (progn
3840             (or best-point (setq best-point (point)))
3841             (if (and (< (get-text-property (point) 'gnus-level) best)
3842                      (or (not exclude-group)
3843                          (not (equal exclude-group (gnus-group-group-name)))))
3844                 (progn 
3845                   (setq best (get-text-property (point) 'gnus-level))
3846                   (setq best-point (point))))))
3847       (forward-line 1))
3848     (if best-point (goto-char best-point))
3849     (gnus-summary-position-cursor)
3850     (and best-point (gnus-group-group-name))))
3851
3852 (defun gnus-group-first-unread-group ()
3853   "Go to the first group with unread articles."
3854   (interactive)
3855   (goto-char (point-min))
3856   (or (not (zerop (or (get-text-property (point) 'gnus-unread) 0)))
3857       (gnus-group-next-unread-group 1))
3858   (gnus-group-position-cursor))
3859
3860 (defun gnus-group-enter-server-mode ()
3861   "Jump to the server buffer."
3862   (interactive)
3863   (gnus-server-setup-buffer)
3864   (gnus-configure-windows 'server)
3865   (gnus-server-prepare))
3866
3867 (defun gnus-group-make-group (name method &optional address)
3868   "Add a new newsgroup.
3869 The user will be prompted for a NAME, for a select METHOD, and an
3870 ADDRESS."
3871   (interactive
3872    (cons 
3873     (read-string "Group name: ")
3874     (let ((method
3875            (completing-read 
3876             "Method: " (append gnus-valid-select-methods gnus-server-alist)
3877             nil t)))
3878       (if (assoc method gnus-valid-select-methods)
3879           (list method
3880                 (if (memq 'prompt-address
3881                           (assoc method gnus-valid-select-methods))
3882                     (read-string "Address: ")
3883                   ""))
3884         (list method nil)))))
3885   
3886   (let* ((meth (if address (list (intern method) address) method))
3887          (nname (gnus-group-prefixed-name name meth))
3888          info)
3889     (and (gnus-gethash nname gnus-newsrc-hashtb)
3890          (error "Group %s already exists" nname))
3891     (gnus-group-change-level 
3892      (setq info (list t nname gnus-level-default-subscribed nil nil meth))
3893      gnus-level-default-subscribed gnus-level-killed 
3894      (and (gnus-group-group-name)
3895           (gnus-gethash (gnus-group-group-name)
3896                         gnus-newsrc-hashtb))
3897      t)
3898     (gnus-sethash nname '(0 . 0) gnus-active-hashtb)
3899     (gnus-dribble-enter 
3900      (concat "(gnus-group-set-info '" (prin1-to-string (cdr info)) ")"))
3901     (gnus-group-insert-group-line-info nname)
3902
3903     (and (gnus-check-backend-function 'request-create-group nname)
3904          (gnus-request-create-group nname))))
3905
3906 (defun gnus-group-edit-group (group &optional part)
3907   "Edit the group on the current line."
3908   (interactive (list (gnus-group-group-name)))
3909   (let ((done-func '(lambda () 
3910                       "Exit editing mode and update the information."
3911                       (interactive)
3912                       (gnus-group-edit-group-done 'part 'group)))
3913         (part (or part 'info))
3914         (winconf (current-window-configuration))
3915         info)
3916     (if group (setq info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
3917       (error "No group on current line"))
3918     (set-buffer (get-buffer-create gnus-group-edit-buffer))
3919     (gnus-configure-windows 'edit-group)
3920     (gnus-add-current-to-buffer-list)
3921     (emacs-lisp-mode)
3922     ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3923     (use-local-map (copy-keymap emacs-lisp-mode-map))
3924     (local-set-key "\C-c\C-c" done-func)
3925     (make-local-variable 'gnus-prev-winconf)
3926     (setq gnus-prev-winconf winconf)
3927     ;; We modify the func to let it know what part it is editing.
3928     (setcar (cdr (nth 4 done-func)) (list 'quote part))
3929     (setcar (cdr (cdr (nth 4 done-func))) group)
3930     (erase-buffer)
3931     (insert
3932      (cond 
3933       ((eq part 'method)
3934        ";; Type `C-c C-c' after editing the select method.\n\n")
3935       ((eq part 'params)
3936        ";; Type `C-c C-c' after editing the group parameters.\n\n")
3937       ((eq part 'info)
3938        ";; Type `C-c C-c' after editing the group info.\n\n")))
3939     (let ((cinfo (gnus-copy-sequence info))
3940           marked)
3941       (if (not (setq marked (nth 3 cinfo)))
3942           ()
3943         (while marked
3944           (or (eq 'score (car (car marked)))
3945               (eq 'bookmark (car (car marked)))
3946               (eq 'killed (car (car marked)))
3947               (setcdr (car marked) 
3948                       (gnus-compress-sequence (sort (cdr (car marked)) '<) t)))
3949           (setq marked (cdr marked))))
3950       (insert 
3951        (pp-to-string
3952         (cond ((eq part 'method)
3953                (or (nth 4 info) "native"))
3954               ((eq part 'params)
3955                (nth 5 info))
3956               (t
3957                cinfo)))
3958        "\n"))))
3959
3960 (defun gnus-group-edit-group-method (group)
3961   "Edit the select method of GROUP."
3962   (interactive (list (gnus-group-group-name)))
3963   (gnus-group-edit-group group 'method))
3964
3965 (defun gnus-group-edit-group-parameters (group)
3966   "Edit the group parameters of GROUP."
3967   (interactive (list (gnus-group-group-name)))
3968   (gnus-group-edit-group group 'params))
3969
3970 (defun gnus-group-edit-group-done (part group)
3971   "Get info from buffer, update variables and jump to the group buffer."
3972   (set-buffer (get-buffer-create gnus-group-edit-buffer))
3973   (goto-char (point-min))
3974   (let ((form (read (current-buffer)))
3975         (winconf gnus-prev-winconf))
3976     (if (eq part 'info) 
3977         (gnus-group-set-info form)
3978       (gnus-group-set-info form group part))
3979     (kill-buffer (current-buffer))
3980     (and winconf (set-window-configuration winconf))
3981     (set-buffer gnus-group-buffer)
3982     (gnus-group-update-group (gnus-group-group-name))
3983     (gnus-group-position-cursor)))
3984
3985 (defun gnus-group-make-help-group ()
3986   "Create the (ding) Gnus documentation group."
3987   (interactive)
3988   (let ((path load-path))
3989     (and (gnus-gethash (setq name (gnus-group-prefixed-name
3990                                    "gnus-help" '(nndoc "gnus-help")))
3991                        gnus-newsrc-hashtb)
3992          (error "Documentation group already exists"))
3993     (while (and path
3994                 (not (file-exists-p (concat (file-name-as-directory (car path))
3995                                             "doc.txt"))))
3996       (setq path (cdr path)))
3997     (or path (error "Couldn't find doc group"))
3998     (gnus-group-make-group 
3999      (gnus-group-real-name name)
4000      (list 'nndoc name
4001            (list 'nndoc-address (concat (file-name-as-directory (car path)) "doc.txt"))
4002            (list 'nndoc-article-type 'mbox))))
4003   (gnus-group-position-cursor))
4004
4005 (defun gnus-group-make-doc-group (file type)
4006   "Create a group that uses a single file as the source."
4007   (interactive 
4008    (list (read-file-name "File name: ") 
4009          (let ((err "")
4010                found char)
4011            (while (not found)
4012              (message "%sFile type (mbox, babyl, digest) [mbd]: " err)
4013              (setq found (cond ((= (setq char (read-char)) ?m) 'mbox)
4014                                ((= char ?b) 'babyl)
4015                                ((= char ?d) 'digest)
4016                                (t (setq mess "%c unknown. " char)
4017                                   nil))))
4018            found)))
4019   (let* ((file (expand-file-name file))
4020          (name (gnus-generate-new-group-name
4021                 (gnus-group-prefixed-name
4022                  (file-name-nondirectory file) '(nndoc "")))))
4023     (gnus-group-make-group 
4024      (gnus-group-real-name name)
4025      (list 'nndoc name
4026            (list 'nndoc-address file)
4027            (list 'nndoc-article-type type)))))
4028
4029 (defun gnus-group-make-archive-group ()
4030   "Create the (ding) Gnus archive group."
4031   (interactive)
4032   (and (gnus-gethash (gnus-group-prefixed-name "ding.archives" '(nndir ""))
4033                      gnus-newsrc-hashtb)
4034        (error "Archive group already exists"))
4035   (gnus-group-make-group "ding.archives" "nndir" gnus-group-archive-directory)
4036   (gnus-group-position-cursor))
4037
4038 (defun gnus-group-make-directory-group (dir)
4039   "Create an nndir group.
4040 The user will be prompted for a directory. The contents of this
4041 directory will be used as a newsgroup. The directory should contain
4042 mail messages or news articles in files that have numeric names."
4043   (interactive
4044    (list (read-file-name "Create group from directory: ")))
4045   (or (file-exists-p dir) (error "No such directory"))
4046   (or (file-directory-p dir) (error "Not a directory"))
4047   (gnus-group-make-group dir "nndir" dir)
4048   (gnus-group-position-cursor))
4049
4050 (defun gnus-group-make-kiboze-group (group address scores)
4051   "Create an nnkiboze group.
4052 The user will be prompted for a name, a regexp to match groups, and
4053 score file entries for articles to include in the group."
4054   (interactive
4055    (list
4056     (read-string "nnkiboze group name: ")
4057     (read-string "Source groups (regexp): ")
4058     (let ((headers (mapcar (lambda (group) (list group))
4059                            '("subject" "from" "number" "date" "message-id"
4060                              "references" "chars" "lines" "xref")))
4061           scores header regexp regexps)
4062       (while (not (equal "" (setq header (completing-read 
4063                                           "Match on header: " headers nil t))))
4064         (setq regexps nil)
4065         (while (not (equal "" (setq regexp (read-string 
4066                                             (format "Match on %s (string): "
4067                                                     header)))))
4068           (setq regexps (cons (list regexp nil nil 'r) regexps)))
4069         (setq scores (cons (cons header regexps) scores)))
4070       scores)))
4071   (gnus-group-make-group group "nnkiboze" address)
4072   (save-excursion
4073     (gnus-set-work-buffer)
4074     (let (emacs-lisp-mode-hook)
4075       (pp scores (current-buffer)))
4076     (write-region (point-min) (point-max) 
4077                   (concat (or gnus-kill-files-directory "~/News")
4078                           "nnkiboze:" group "." gnus-score-file-suffix)))
4079   (gnus-group-position-cursor))
4080
4081 (defun gnus-group-add-to-virtual (n vgroup)
4082   "Add the current group to a virtual group."
4083   (interactive
4084    (list current-prefix-arg
4085          (completing-read "Add to virtual group: " gnus-newsrc-hashtb nil t
4086                           "nnvirtual:")))
4087   (or (eq (car (gnus-find-method-for-group vgroup)) 'nnvirtual)
4088       (error "%s is not an nnvirtual group" vgroup))
4089   (let* ((groups (gnus-group-process-prefix n))
4090          (method (nth 4 (nth 2 (gnus-gethash vgroup gnus-newsrc-hashtb)))))
4091     (setcar (cdr method)
4092             (concat 
4093              (nth 1 method) "\\|"
4094              (mapconcat 
4095               (lambda (s) 
4096                 (gnus-group-remove-mark s)
4097                 (concat "\\(^" (regexp-quote s) "$\\)"))
4098               groups "\\|"))))
4099   (gnus-group-position-cursor))
4100
4101 (defun gnus-group-make-empty-virtual (group)
4102   "Create a new, fresh, empty virtual group."
4103   (interactive "sCreate new, empty virtual group: ")
4104   (let* ((method (list 'nnvirtual "^$"))
4105          (pgroup (gnus-group-prefixed-name group method)))
4106     ;; Check whether it exists already.
4107     (and (gnus-gethash pgroup gnus-newsrc-hashtb)
4108          (error "Group %s already exists." pgroup))
4109     ;; Subscribe the new group after the group on the current line.
4110     (gnus-subscribe-group pgroup (gnus-group-group-name) method)
4111     (gnus-group-update-group pgroup)
4112     (forward-line -1)
4113     (gnus-group-position-cursor)))
4114
4115 (defun gnus-group-enter-directory (dir)
4116   "Enter an ephemeral nneething group."
4117   (interactive "DDirectory to read: ")
4118   (let* ((method (list 'nneething dir))
4119          (leaf (gnus-group-prefixed-name
4120                 (file-name-nondirectory (directory-file-name dir))
4121                 method))
4122          (name (gnus-generate-new-group-name leaf))
4123          (num 0))
4124     (let ((nneething-read-only t))
4125       (or (gnus-group-read-ephemeral-group 
4126            name method t
4127            (cons (current-buffer) (if (eq major-mode 'gnus-summary-mode)
4128                                       'summary 'group)))
4129           (error "Couldn't enter %s" dir)))))
4130
4131 ;; Group sorting commands
4132 ;; Suggested by Joe Hildebrand <hildjj@idaho.fuentez.com>.
4133
4134 (defun gnus-group-sort-groups ()
4135   "Sort the group buffer using `gnus-group-sort-function'."
4136   (interactive)
4137   (setq gnus-newsrc-alist 
4138         (sort (cdr gnus-newsrc-alist) gnus-group-sort-function))
4139   (gnus-make-hashtable-from-newsrc-alist)
4140   (gnus-group-list-groups nil gnus-have-all-newsgroups))
4141
4142 (defun gnus-group-sort-by-alphabet (info1 info2)
4143   (string< (car info1) (car info2)))
4144
4145 (defun gnus-group-sort-by-unread (info1 info2)
4146   (let ((n1 (car (gnus-gethash (car info1) gnus-newsrc-hashtb)))
4147         (n2 (car (gnus-gethash (car info2) gnus-newsrc-hashtb))))
4148     (< (or (and (numberp n1) n1) 0)
4149        (or (and (numberp n2) n2) 0))))
4150
4151 (defun gnus-group-sort-by-level (info1 info2)
4152   (< (nth 1 info1) (nth 1 info2)))
4153
4154 ;; Group catching up.
4155
4156 (defun gnus-group-catchup-current (n &optional all)
4157   "Mark all articles not marked as unread in current newsgroup as read.
4158 If prefix argument N is numeric, the ARG next newsgroups will be
4159 caught up. If ALL is non-nil, marked articles will also be marked as
4160 read. Cross references (Xref: header) of articles are ignored.
4161 The difference between N and actual number of newsgroups that were
4162 caught up is returned."
4163   (interactive "P")
4164   (if (not (or (not gnus-interactive-catchup) ;Without confirmation?
4165                gnus-expert-user
4166                (gnus-y-or-n-p
4167                 (if all
4168                     "Do you really want to mark all articles as read? "
4169                   "Mark all unread articles as read? "))))
4170       n
4171     (let ((groups (gnus-group-process-prefix n))
4172           (ret 0))
4173       (while groups
4174         ;; Virtual groups have to be given special treatment. 
4175         (let ((method (gnus-find-method-for-group (car groups))))
4176           (if (eq 'nnvirtual (car method))
4177               (nnvirtual-catchup-group
4178                (gnus-group-real-name (car groups)) (nth 1 method) all)))
4179         (gnus-group-remove-mark (car groups))
4180         (if (prog1
4181                 (gnus-group-goto-group (car groups))
4182               (gnus-group-catchup (car groups) all))
4183             (gnus-group-update-group-line)
4184           (setq ret (1+ ret)))
4185         (setq groups (cdr groups)))
4186       (gnus-group-next-unread-group 1)
4187       ret)))
4188
4189 (defun gnus-group-catchup-current-all (n)
4190   "Mark all articles in current newsgroup as read.
4191 Cross references (Xref: header) of articles are ignored."
4192   (interactive "P")
4193   (gnus-group-catchup-current n 'all))
4194
4195 (defun gnus-group-catchup (group &optional all)
4196   "Mark all articles in GROUP as read.
4197 If ALL is non-nil, all articles are marked as read.
4198 The return value is the number of articles that were marked as read,
4199 or nil if no action could be taken."
4200   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4201          (num (car entry))
4202          (marked (nth 3 (nth 2 entry)))
4203          ticked)
4204     (if (not (numberp (car entry)))
4205         (gnus-message 1 "Can't catch up; non-active group")
4206       ;; Do the updating only if the newsgroup isn't killed.
4207       (if (not entry)
4208           ()
4209         (gnus-update-read-articles 
4210          group (and (not all) (append (cdr (assq 'tick marked))
4211                                       (cdr (assq 'dormant marked))))
4212          nil (and (not all) (cdr (assq 'tick marked))))
4213         (and all marked
4214              (setcar (nthcdr 3 (nth 2 entry)) 
4215                      (delq (assq 'dormant marked) 
4216                            (nth 3 (nth 2 entry)))))))
4217     num))
4218
4219 (defun gnus-group-expire-articles (n)
4220   "Expire all expirable articles in the current newsgroup."
4221   (interactive "P")
4222   (let ((groups (gnus-group-process-prefix n))
4223         group)
4224     (or groups (error "No groups to expire"))
4225     (while groups
4226       (setq group (car groups)
4227             groups (cdr groups))
4228       (gnus-group-remove-mark group)
4229       (if (not (gnus-check-backend-function 'request-expire-articles group))
4230           ()
4231         (let* ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
4232                (expirable (if (memq 'total-expire (nth 5 info))
4233                               (cons nil (gnus-list-of-read-articles group))
4234                             (assq 'expire (nth 3 info)))))
4235           (and expirable 
4236                (setcdr expirable
4237                        (gnus-request-expire-articles 
4238                         (cdr expirable) group))))))))
4239
4240 (defun gnus-group-expire-all-groups ()
4241   "Expire all expirable articles in all newsgroups."
4242   (interactive)
4243   (gnus-message 5 "Expiring...")
4244   (let ((gnus-group-marked (mapcar (lambda (info) (car info))
4245                                    (cdr gnus-newsrc-alist))))
4246     (gnus-group-expire-articles nil))
4247   (gnus-message 5 "Expiring...done"))
4248
4249 (defun gnus-group-set-current-level (n level)
4250   "Set the level of the next N groups to LEVEL."
4251   (interactive "P\nnLevel: ")
4252   (or (and (>= level 1) (<= level gnus-level-killed))
4253       (error "Illegal level: %d" level))
4254   (let ((groups (gnus-group-process-prefix n))
4255         group)
4256     (while groups
4257       (setq group (car groups)
4258             groups (cdr groups))
4259       (gnus-group-remove-mark group)
4260       (gnus-message 6 "Changed level of %s from %d to %d" 
4261                     group (gnus-group-group-level) level)
4262       (gnus-group-change-level group level
4263                                (gnus-group-group-level))
4264       (gnus-group-update-group-line)))
4265   (gnus-group-position-cursor))
4266
4267 (defun gnus-group-unsubscribe-current-group (arg)
4268   "Toggle subscribe from/to unsubscribe current group."
4269   (interactive "P")
4270   (let ((group (gnus-group-group-name)))
4271     (or group (error "No newsgroup on current line"))
4272     (or arg (setq arg (if (<= (gnus-group-group-level) gnus-level-subscribed)
4273                           gnus-level-default-unsubscribed
4274                         gnus-level-default-subscribed)))
4275     (gnus-group-unsubscribe-group group arg)
4276     (gnus-group-next-group 1)))
4277
4278 (defun gnus-group-unsubscribe-group (group &optional level)
4279   "Toggle subscribe from/to unsubscribe GROUP.
4280 New newsgroup is added to .newsrc automatically."
4281   (interactive
4282    (list (completing-read "Group: " gnus-active-hashtb nil 
4283                           gnus-have-read-active-file)))
4284   (let ((newsrc (gnus-gethash group gnus-newsrc-hashtb)))
4285     (cond (newsrc
4286            ;; Toggle subscription flag.
4287            (gnus-group-change-level 
4288             newsrc (if level level (if (<= (nth 1 (nth 2 newsrc)) 
4289                                            gnus-level-subscribed) 
4290                                        (1+ gnus-level-subscribed)
4291                                      gnus-level-default-subscribed)))
4292            (gnus-group-update-group group))
4293           ((and (stringp group)
4294                 (or (not gnus-have-read-active-file)
4295                     (gnus-gethash group gnus-active-hashtb)))
4296            ;; Add new newsgroup.
4297            (gnus-group-change-level 
4298             group 
4299             (if level level gnus-level-default-subscribed) 
4300             (or (and (member group gnus-zombie-list) 
4301                      gnus-level-zombie) 
4302                 gnus-level-killed)
4303             (and (gnus-group-group-name)
4304                  (gnus-gethash (gnus-group-group-name) gnus-newsrc-hashtb)))
4305            (gnus-group-update-group group))
4306           (t (error "No such newsgroup: %s" group)))
4307     (gnus-group-position-cursor)))
4308
4309 (defun gnus-group-transpose-groups (n)
4310   "Move the current newsgroup up N places.
4311 If given a negative prefix, move down instead. The difference between
4312 N and the number of steps taken is returned." 
4313   (interactive "p")
4314   (or (gnus-group-group-name)
4315       (error "No group on current line"))
4316   (gnus-group-kill-group 1)
4317   (prog1
4318       (forward-line (- n))
4319     (gnus-group-yank-group)
4320     (gnus-group-position-cursor)))
4321
4322 (defun gnus-group-kill-all-zombies ()
4323   "Kill all zombie newsgroups."
4324   (interactive)
4325   (setq gnus-killed-list (nconc gnus-zombie-list gnus-killed-list))
4326   (setq gnus-zombie-list nil)
4327   (funcall gnus-group-prepare-function gnus-level-subscribed nil nil)
4328   (goto-char (point-min))
4329   (gnus-group-position-cursor))
4330
4331 (defun gnus-group-kill-region (begin end)
4332   "Kill newsgroups in current region (excluding current point).
4333 The killed newsgroups can be yanked by using \\[gnus-group-yank-group]."
4334   (interactive "r")
4335   (let ((lines
4336          ;; Exclude a line where current point is on.
4337          (1-
4338           ;; Count lines.
4339           (save-excursion
4340             (count-lines
4341              (progn
4342                (goto-char begin)
4343                (beginning-of-line)
4344                (point))
4345              (progn
4346                (goto-char end)
4347                (end-of-line)
4348                (point)))))))
4349     (goto-char begin)
4350     (beginning-of-line)                 ;Important when LINES < 1
4351     (gnus-group-kill-group lines)))
4352
4353 (defun gnus-group-kill-group (n)
4354   "The the next N groups.
4355 The killed newsgroups can be yanked by using \\[gnus-group-yank-group].
4356 However, only groups that were alive can be yanked; already killed 
4357 groups or zombie groups can't be yanked.
4358 The return value is the name of the (last) group that was killed."
4359   (interactive "P")
4360   (let ((buffer-read-only nil)
4361         (groups (gnus-group-process-prefix n))
4362         group entry level)
4363     (while groups
4364       (setq group (car groups)
4365             groups (cdr groups))
4366       (gnus-group-remove-mark group)
4367       (setq level (gnus-group-group-level))
4368       (gnus-delete-line)
4369       (if (setq entry (gnus-gethash group gnus-newsrc-hashtb))
4370           (setq gnus-list-of-killed-groups 
4371                 (cons (cons (car entry) (nth 2 entry)) 
4372                       gnus-list-of-killed-groups)))
4373       (gnus-group-change-level 
4374        (if entry entry group) gnus-level-killed (if entry nil level)))
4375     (gnus-group-position-cursor)
4376     group))
4377
4378 (defun gnus-group-yank-group (&optional arg)
4379   "Yank the last newsgroups killed with \\[gnus-group-kill-group],
4380 inserting it before the current newsgroup.  The numeric ARG specifies
4381 how many newsgroups are to be yanked.  The name of the (last)
4382 newsgroup yanked is returned."
4383   (interactive "p")
4384   (if (not arg) (setq arg 1))
4385   (let (info group prev)
4386     (while (>= (setq arg (1- arg)) 0)
4387       (if (not (setq info (car gnus-list-of-killed-groups)))
4388           (error "No more newsgroups to yank"))
4389       (setq group (nth 2 info))
4390       ;; Find which newsgroup to insert this one before - search
4391       ;; backward until something suitable is found. If there are no
4392       ;; other newsgroups in this buffer, just make this newsgroup the
4393       ;; first newsgroup.
4394       (setq prev (gnus-group-group-name))
4395       (gnus-group-change-level 
4396        info (nth 2 info) gnus-level-killed 
4397        (and prev (gnus-gethash prev gnus-newsrc-hashtb))
4398        t)
4399       (gnus-group-insert-group-line-info (nth 1 info))
4400       (setq gnus-list-of-killed-groups 
4401             (cdr gnus-list-of-killed-groups)))
4402     (forward-line -1)
4403     (gnus-group-position-cursor)
4404     group))
4405       
4406 (defun gnus-group-list-all-groups (arg)
4407   "List all newsgroups with level ARG or lower.
4408 Default is gnus-level-unsubscribed, which lists all subscribed and most
4409 unsubscribed groups."
4410   (interactive "P")
4411   (setq arg (or arg gnus-level-unsubscribed))
4412   (gnus-group-list-groups arg t))
4413
4414 (defun gnus-group-list-killed ()
4415   "List all killed newsgroups in the group buffer."
4416   (interactive)
4417   (if (not gnus-killed-list)
4418       (gnus-message 6 "No killed groups")
4419     (funcall gnus-group-prepare-function gnus-level-killed t gnus-level-killed)
4420     (goto-char (point-min)))
4421   (gnus-group-position-cursor))
4422
4423 (defun gnus-group-list-zombies ()
4424   "List all zombie newsgroups in the group buffer."
4425   (interactive)
4426   (if (not gnus-zombie-list)
4427       (gnus-message 6 "No zombie groups")
4428     (funcall gnus-group-prepare-function gnus-level-zombie t gnus-level-zombie)
4429     (goto-char (point-min)))
4430   (gnus-group-position-cursor))
4431
4432 (defun gnus-group-get-new-news (&optional arg)
4433   "Get newly arrived articles.
4434 If ARG is non-nil, it should be a number between one and nine to
4435 specify which levels you are interested in re-scanning."
4436   (interactive "P")
4437   (run-hooks 'gnus-get-new-news-hook)
4438   (let ((level arg))
4439     (if gnus-group-use-permanent-levels
4440         (if level
4441             (setq gnus-group-default-list-level level)
4442           (setq level (or gnus-group-default-list-level 
4443                           gnus-level-subscribed))))
4444     (if (and gnus-read-active-file (not level))
4445         (progn
4446           (gnus-read-active-file)
4447           (gnus-get-unread-articles (or level (1+ gnus-level-subscribed))))
4448       (let ((gnus-read-active-file nil))
4449         (gnus-get-unread-articles (or level (1+ gnus-level-subscribed)))))
4450     (gnus-group-list-groups (or (and gnus-group-use-permanent-levels level)
4451                                 gnus-group-default-list-level
4452                                 gnus-level-subscribed)
4453                             gnus-have-all-newsgroups)))
4454
4455 (defun gnus-group-get-new-news-this-group (n)
4456   "Check for newly arrived news in the current group (and the N-1 next groups).
4457 The difference between N and the number of newsgroup checked is returned.
4458 If N is negative, this group and the N-1 previous groups will be checked."
4459   (interactive "P")
4460   (let* ((groups (gnus-group-process-prefix n))
4461          (ret (if (numberp n) (- n (length groups)) 0))
4462          (w-p (window-start))
4463          group)
4464     (while groups
4465       (setq group (car groups)
4466             groups (cdr groups))
4467       (gnus-group-remove-mark group)
4468       (or (gnus-get-new-news-in-group group)
4469           (progn 
4470             (ding) 
4471             (message "%s error: %s" group (gnus-status-message group))
4472             (sit-for 2))))
4473     ;; !!! I don't know why the buffer scrolls forward when updating
4474     ;; the first line in the group buffer, but it does. So we set the
4475     ;; window start forcibly.
4476 ;    (set-window-start (get-buffer-window (current-buffer)) w-p)
4477     (gnus-group-next-unread-group 1 t)
4478     (gnus-summary-position-cursor)
4479     ret))
4480
4481 (defun gnus-get-new-news-in-group (group)
4482   (and group 
4483        (gnus-activate-newsgroup group)
4484        (progn
4485          (gnus-get-unread-articles-in-group 
4486           (nth 2 (gnus-gethash group gnus-newsrc-hashtb))
4487           (gnus-gethash group gnus-active-hashtb))
4488          (gnus-group-update-group-line)
4489          t)))
4490
4491 (defun gnus-group-fetch-faq (group)
4492   "Fetch the FAQ for the current group."
4493   (interactive (list (gnus-group-real-name (gnus-group-group-name))))
4494   (or group (error "No group name given"))
4495   (let ((file (concat gnus-group-faq-directory group))) 
4496     (if (not (file-exists-p file))
4497         (error "No such file: %s" file)
4498       (find-file file))))
4499   
4500 (defun gnus-group-describe-group (force &optional group)
4501   "Display a description of the current newsgroup."
4502   (interactive (list current-prefix-arg (gnus-group-group-name)))
4503   (and force (setq gnus-description-hashtb nil))
4504   (let ((method (gnus-find-method-for-group group))
4505         desc)
4506     (or group (error "No group name given"))
4507     (and (or (and gnus-description-hashtb
4508                   ;; We check whether this group's method has been
4509                   ;; queried for a description file.  
4510                   (gnus-gethash 
4511                    (gnus-group-prefixed-name "" method) 
4512                    gnus-description-hashtb))
4513              (setq desc (gnus-group-get-description group))
4514              (gnus-read-descriptions-file method))
4515          (message
4516           (or desc (gnus-gethash group gnus-description-hashtb)
4517               "No description available")))))
4518
4519 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
4520 (defun gnus-group-describe-all-groups (force)
4521   "Pop up a buffer with descriptions of all newsgroups."
4522   (interactive "P")
4523   (and force (setq gnus-description-hashtb nil))
4524   (if (not (or gnus-description-hashtb
4525                (gnus-read-all-descriptions-files)))
4526       (error "Couldn't request descriptions file"))
4527   (let ((buffer-read-only nil)
4528         b)
4529     (erase-buffer)
4530     (mapatoms
4531      (lambda (group)
4532        (setq b (point))
4533        (insert (format "      *: %-20s %s\n" (symbol-name group)
4534                        (symbol-value group)))
4535        (add-text-properties 
4536         b (1+ b) (list 'gnus-group group
4537                        'gnus-unread t 'gnus-marked nil
4538                        'gnus-level (1+ gnus-level-subscribed))))
4539      gnus-description-hashtb)
4540     (goto-char (point-min))
4541     (gnus-group-position-cursor)))
4542
4543 ;; Suggested by by Daniel Quinlan <quinlan@best.com>.
4544 (defun gnus-group-apropos (regexp &optional search-description)
4545   "List all newsgroups that have names that match a regexp."
4546   (interactive "sGnus apropos (regexp): ")
4547   (let ((prev "")
4548         (obuf (current-buffer))
4549         groups des prev)
4550     ;; Go through all newsgroups that are known to Gnus.
4551     (mapatoms 
4552      (lambda (group)
4553        (and (string-match regexp (symbol-name group))
4554             (setq groups (cons (symbol-name group) groups))))
4555      gnus-active-hashtb)
4556     ;; Go through all descriptions that are known to Gnus. 
4557     (if search-description
4558         (mapatoms 
4559          (lambda (group)
4560            (and (string-match regexp (symbol-value group))
4561                 (gnus-gethash (symbol-name group) gnus-active-hashtb)
4562                 (setq groups (cons (symbol-name group) groups))))
4563          gnus-description-hashtb))
4564     (if (not groups)
4565         (gnus-message 3 "No groups matched \"%s\"." regexp)
4566       ;; Print out all the groups.
4567       (save-excursion
4568         (pop-to-buffer "*Gnus Help*")
4569         (buffer-disable-undo (current-buffer))
4570         (erase-buffer)
4571         (setq groups (sort groups 'string<))
4572         (while groups
4573           ;; Groups may be entered twice into the list of groups.
4574           (if (not (string= (car groups) prev))
4575               (progn
4576                 (insert (setq prev (car groups)) "\n")
4577                 (if (and gnus-description-hashtb
4578                          (setq des (gnus-gethash (car groups) 
4579                                                  gnus-description-hashtb)))
4580                     (insert "  " des "\n"))))
4581           (setq groups (cdr groups)))
4582         (goto-char (point-min))))
4583     (pop-to-buffer obuf)))
4584
4585 (defun gnus-group-description-apropos (regexp)
4586   "List all newsgroups that have names or descriptions that match a regexp."
4587   (interactive "sGnus description apropos (regexp): ")
4588   (if (not (or gnus-description-hashtb
4589                (gnus-read-all-descriptions-files)))
4590       (error "Couldn't request descriptions file"))
4591   (gnus-group-apropos regexp t))
4592
4593 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
4594 (defun gnus-group-list-matching (level regexp &optional all lowest) 
4595   "List all groups with unread articles that match REGEXP.
4596 If the prefix LEVEL is non-nil, it should be a number that says which
4597 level to cut off listing groups. 
4598 If ALL, also list groups with no unread articles.
4599 If LOWEST, don't list groups with level lower than LOWEST."
4600   (interactive "P\nsList newsgroups matching: ")
4601   (gnus-group-prepare-flat (or level gnus-level-subscribed)
4602                            all (or lowest 1) regexp)
4603   (goto-char (point-min))
4604   (gnus-group-position-cursor))
4605
4606 (defun gnus-group-list-all-matching (level regexp &optional lowest) 
4607   "List all groups that match REGEXP.
4608 If the prefix LEVEL is non-nil, it should be a number that says which
4609 level to cut off listing groups. 
4610 If LOWEST, don't list groups with level lower than LOWEST."
4611   (interactive "P\nsList newsgroups matching: ")
4612   (gnus-group-list-matching (or level gnus-level-killed) regexp t lowest))
4613
4614 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
4615 (defun gnus-group-save-newsrc ()
4616   "Save the Gnus startup files."
4617   (interactive)
4618   (gnus-save-newsrc-file))
4619
4620 (defun gnus-group-restart (&optional arg)
4621   "Force Gnus to read the .newsrc file."
4622   (interactive "P")
4623   (gnus-save-newsrc-file)
4624   (gnus-setup-news 'force)
4625   (gnus-group-list-groups arg gnus-have-all-newsgroups))
4626
4627 (defun gnus-group-read-init-file ()
4628   "Read the Gnus elisp init file."
4629   (interactive)
4630   (gnus-read-init-file))
4631
4632 (defun gnus-group-check-bogus-groups (silent)
4633   "Check bogus newsgroups.
4634 If given a prefix, don't ask for confirmation before removing a bogus
4635 group."
4636   (interactive "P")
4637   (gnus-check-bogus-newsgroups (and (not silent) (not gnus-expert-user)))
4638   (gnus-group-list-groups nil gnus-have-all-newsgroups))
4639
4640 (defun gnus-group-edit-global-kill (article &optional group)
4641   "Edit the global kill file.
4642 If GROUP, edit that local kill file instead."
4643   (interactive "P")
4644   (setq gnus-current-kill-article article)
4645   (gnus-kill-file-edit-file group)
4646   (gnus-message 6
4647    (substitute-command-keys
4648     "Editing a global kill file (Type \\[gnus-kill-file-exit] to exit)")))
4649
4650 (defun gnus-group-edit-local-kill (article group)
4651   "Edit a local kill file."
4652   (interactive (list nil (gnus-group-group-name)))
4653   (gnus-group-edit-global-kill article group))
4654
4655 (defun gnus-group-force-update ()
4656   "Update `.newsrc' file."
4657   (interactive)
4658   (gnus-save-newsrc-file))
4659
4660 (defun gnus-group-suspend ()
4661   "Suspend the current Gnus session.
4662 In fact, cleanup buffers except for group mode buffer.
4663 The hook gnus-suspend-gnus-hook is called before actually suspending."
4664   (interactive)
4665   (run-hooks 'gnus-suspend-gnus-hook)
4666   ;; Kill Gnus buffers except for group mode buffer.
4667   (let ((group-buf (get-buffer gnus-group-buffer)))
4668     ;; Do this on a separate list in case the user does a ^G before we finish
4669     (let ((gnus-buffer-list
4670            (delq group-buf (delq gnus-dribble-buffer
4671                                  (append gnus-buffer-list nil)))))
4672       (while gnus-buffer-list
4673         (gnus-kill-buffer (car gnus-buffer-list))
4674         (setq gnus-buffer-list (cdr gnus-buffer-list))))
4675     (if group-buf
4676         (progn
4677           (setq gnus-buffer-list (list group-buf))
4678           (bury-buffer group-buf)
4679           (delete-windows-on group-buf t)))))
4680
4681 (defun gnus-group-clear-dribble ()
4682   "Clear all information from the dribble buffer."
4683   (interactive)
4684   (gnus-dribble-clear))
4685
4686 (defun gnus-group-exit ()
4687   "Quit reading news after updating .newsrc.eld and .newsrc.
4688 The hook `gnus-exit-gnus-hook' is called before actually exiting."
4689   (interactive)
4690   (if (or noninteractive                ;For gnus-batch-kill
4691           (zerop (buffer-size))         ;No news is good news.
4692           (not (gnus-server-opened gnus-select-method)) ;NNTP connection closed
4693           (not gnus-interactive-exit)   ;Without confirmation
4694           gnus-expert-user
4695           (gnus-y-or-n-p "Are you sure you want to quit reading news? "))
4696       (progn
4697         (run-hooks 'gnus-exit-gnus-hook)
4698         (gnus-offer-save-summaries)
4699         (gnus-save-newsrc-file)
4700         (gnus-close-backends)
4701         (gnus-clear-system))))
4702
4703 (defun gnus-close-backends ()
4704   ;; Send a close request to all backends that support such a request. 
4705   (let ((methods gnus-valid-select-methods)
4706         func)
4707     (while methods
4708       (if (fboundp (setq func (intern (concat (car (car methods))
4709                                               "-request-close"))))
4710           (funcall func))
4711       (setq methods (cdr methods)))))
4712
4713 (defun gnus-group-quit ()
4714   "Quit reading news without updating .newsrc.eld or .newsrc.
4715 The hook `gnus-exit-gnus-hook' is called before actually exiting."
4716   (interactive)
4717   (if (or noninteractive                ;For gnus-batch-kill
4718           (zerop (buffer-size))
4719           (not (gnus-server-opened gnus-select-method))
4720           gnus-expert-user
4721           (not gnus-current-startup-file)
4722           (gnus-yes-or-no-p
4723            (format "Quit reading news without saving %s? "
4724                    (file-name-nondirectory gnus-current-startup-file))))
4725       (progn
4726         (run-hooks 'gnus-exit-gnus-hook)
4727         (gnus-dribble-save)
4728         (gnus-close-backends)
4729         (gnus-clear-system))))
4730
4731 (defun gnus-offer-save-summaries ()
4732   (let ((buffers (buffer-list)))
4733     (save-excursion
4734       (while buffers
4735         (and 
4736          ;; We look for buffers with "Summary" in the name.
4737          (string-match "Summary" (or (buffer-name (car buffers)) ""))
4738          (progn
4739            (set-buffer (car buffers))
4740            ;; We check that this is, indeed, a summary buffer.
4741            (eq major-mode 'gnus-summary-mode)) 
4742          ;; We ask the user whether she wants to save the info.
4743          (gnus-y-or-n-p
4744                (format "Update summary buffer %s? " (buffer-name)))
4745          ;; We do it by simply exiting.
4746          (gnus-summary-exit))
4747         (setq buffers (cdr buffers))))))
4748
4749 (defun gnus-group-describe-briefly ()
4750   "Give a one line description of the group mode commands."
4751   (interactive)
4752   (gnus-message 6
4753    (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")))
4754
4755 (defun gnus-group-browse-foreign-server (method)
4756   "Browse a foreign news server.
4757 If called interactively, this function will ask for a select method
4758  (nntp, nnspool, etc.) and a server address (eg. nntp.some.where). 
4759 If not, METHOD should be a list where the first element is the method
4760 and the second element is the address."
4761   (interactive
4762    (list (let ((how (completing-read 
4763                      "Which backend: "
4764                      (append gnus-valid-select-methods gnus-server-alist)
4765                      nil t "nntp")))
4766            ;; We either got a backend name or a virtual server name.
4767            ;; If the first, we also need an address.
4768            (if (assoc how gnus-valid-select-methods)
4769                (list (intern how)
4770                      ;; Suggested by mapjph@bath.ac.uk.
4771                      (completing-read 
4772                       "Address: " 
4773                       (mapcar (lambda (server) (list server))
4774                               gnus-secondary-servers)))
4775              ;; We got a server name, so we find the method.
4776              (gnus-server-to-method how)))))
4777   (gnus-browse-foreign-server method))
4778
4779 \f
4780 ;;;
4781 ;;; Browse Server Mode
4782 ;;;
4783
4784 (defvar gnus-browse-mode-hook nil)
4785 (defvar gnus-browse-mode-map nil)
4786 (put 'gnus-browse-mode 'mode-class 'special)
4787
4788 (if gnus-browse-mode-map
4789     nil
4790   (setq gnus-browse-mode-map (make-keymap))
4791   (suppress-keymap gnus-browse-mode-map)
4792   (define-key gnus-browse-mode-map " " 'gnus-browse-read-group)
4793   (define-key gnus-browse-mode-map "=" 'gnus-browse-select-group)
4794   (define-key gnus-browse-mode-map "n" 'gnus-browse-next-group)
4795   (define-key gnus-browse-mode-map "p" 'gnus-browse-prev-group)
4796   (define-key gnus-browse-mode-map "\177" 'gnus-browse-prev-group)
4797   (define-key gnus-browse-mode-map "N" 'gnus-browse-next-group)
4798   (define-key gnus-browse-mode-map "P" 'gnus-browse-prev-group)
4799   (define-key gnus-browse-mode-map "\M-n" 'gnus-browse-next-group)
4800   (define-key gnus-browse-mode-map "\M-p" 'gnus-browse-prev-group)
4801   (define-key gnus-browse-mode-map "\r" 'gnus-browse-select-group)
4802   (define-key gnus-browse-mode-map "u" 'gnus-browse-unsubscribe-current-group)
4803   (define-key gnus-browse-mode-map "l" 'gnus-browse-exit)
4804   (define-key gnus-browse-mode-map "L" 'gnus-browse-exit)
4805   (define-key gnus-browse-mode-map "q" 'gnus-browse-exit)
4806   (define-key gnus-browse-mode-map "Q" 'gnus-browse-exit)
4807   (define-key gnus-browse-mode-map "\C-c\C-c" 'gnus-browse-exit)
4808   (define-key gnus-browse-mode-map "?" 'gnus-browse-describe-briefly)
4809   (define-key gnus-browse-mode-map "\C-c\C-i" 'gnus-info-find-node)
4810   )
4811
4812 (defvar gnus-browse-current-method nil)
4813 (defvar gnus-browse-return-buffer nil)
4814
4815 (defvar gnus-browse-buffer "*Gnus Browse Server*")
4816
4817 (defun gnus-browse-foreign-server (method &optional return-buffer)
4818   (setq gnus-browse-current-method method)
4819   (setq gnus-browse-return-buffer return-buffer)
4820   (let ((gnus-select-method method)
4821         groups group)
4822     (gnus-message 5 "Connecting to %s..." (nth 1 method))
4823     (or (gnus-server-opened method)
4824         (gnus-open-server method)
4825         (error "Unable to contact server: %s" (gnus-status-message method)))
4826     (or (gnus-request-list method)
4827         (error "Couldn't request list: %s" (gnus-status-message method)))
4828     (get-buffer-create gnus-browse-buffer)
4829     (gnus-add-current-to-buffer-list)
4830     (and gnus-carpal (gnus-carpal-setup-buffer 'browse))
4831     (gnus-configure-windows 'browse)
4832     (buffer-disable-undo (current-buffer))
4833     (let ((buffer-read-only nil))
4834       (erase-buffer))
4835     (gnus-browse-mode)
4836     (setq mode-line-buffer-identification
4837           (format
4838            "(ding) Browse Server {%s:%s}" (car method) (car (cdr method))))
4839     (save-excursion
4840       (set-buffer nntp-server-buffer)
4841       (let ((cur (current-buffer)))
4842         (goto-char (point-min))
4843         (or (string= gnus-ignored-newsgroups "")
4844             (delete-matching-lines gnus-ignored-newsgroups))
4845         (while (re-search-forward 
4846                 "\\(^[^ \t]+\\)[ \t]+[0-9]+[ \t]+[0-9]+" nil t)
4847           (goto-char (match-end 1))
4848           (setq groups (cons (cons (buffer-substring (match-beginning 1)
4849                                                      (match-end 1))
4850                                    (max 0 (- (1+ (read cur)) (read cur))))
4851                              groups)))))
4852     (setq groups (sort groups 
4853                        (lambda (l1 l2)
4854                          (string< (car l1) (car l2)))))
4855     (let ((buffer-read-only nil))
4856       (while groups
4857         (setq group (car groups))
4858         (insert 
4859          (format "K%7d: %s\n" (cdr group) (car group)))
4860         (setq groups (cdr groups))))
4861     (switch-to-buffer (current-buffer))
4862     (goto-char (point-min))
4863     (gnus-group-position-cursor)))
4864
4865 (defun gnus-browse-mode ()
4866   "Major mode for browsing a foreign server.
4867
4868 All normal editing commands are switched off.
4869
4870 \\<gnus-browse-mode-map>
4871 The only things you can do in this buffer is
4872
4873 1) `\\[gnus-browse-unsubscribe-current-group]' to subscribe to a group.
4874 The group will be inserted into the group buffer upon exit from this
4875 buffer.  
4876
4877 2) `\\[gnus-browse-read-group]' to read a group ephemerally.
4878
4879 3) `\\[gnus-browse-exit]' to return to the group buffer."
4880   (interactive)
4881   (kill-all-local-variables)
4882   (if gnus-visual (gnus-browse-make-menu-bar))
4883   (setq mode-line-modified "-- ")
4884   (make-local-variable 'mode-line-format)
4885   (setq mode-line-format (copy-sequence mode-line-format))
4886   (and (equal (nth 3 mode-line-format) "   ")
4887        (setcar (nthcdr 3 mode-line-format) ""))
4888   (setq major-mode 'gnus-browse-mode)
4889   (setq mode-name "Browse Server")
4890   (setq mode-line-process nil)
4891   (use-local-map gnus-browse-mode-map)
4892   (buffer-disable-undo (current-buffer))
4893   (setq truncate-lines t)
4894   (setq buffer-read-only t)
4895   (run-hooks 'gnus-browse-mode-hook))
4896
4897 (defun gnus-browse-read-group (&optional no-article)
4898   "Enter the group at the current line."
4899   (interactive)
4900   (let ((group (gnus-browse-group-name)))
4901     (or (gnus-group-read-ephemeral-group 
4902          group gnus-browse-current-method nil
4903          (cons (current-buffer) 'browse))
4904         (error "Couldn't enter %s" group))))
4905
4906 (defun gnus-browse-select-group ()
4907   "Select the current group."
4908   (interactive)
4909   (gnus-browse-read-group 'no))
4910
4911 (defun gnus-browse-next-group (n)
4912   "Go to the next group."
4913   (interactive "p")
4914   (prog1
4915       (forward-line n)
4916     (gnus-group-position-cursor)))
4917
4918 (defun gnus-browse-prev-group (n)
4919   "Go to the next group."
4920   (interactive "p")
4921   (gnus-browse-next-group (- n)))
4922
4923 (defun gnus-browse-unsubscribe-current-group (arg)
4924   "(Un)subscribe to the next ARG groups."
4925   (interactive "p")
4926   (and (eobp)
4927        (error "No group at current line."))
4928   (let ((ward (if (< arg 0) -1 1))
4929         (arg (abs arg)))
4930     (while (and (> arg 0)
4931                 (not (eobp))
4932                 (gnus-browse-unsubscribe-group)
4933                 (zerop (gnus-browse-next-group ward)))
4934       (setq arg (1- arg)))
4935     (gnus-group-position-cursor)
4936     (if (/= 0 arg) (gnus-message 7 "No more newsgroups"))
4937     arg))
4938
4939 (defun gnus-browse-group-name ()
4940   (save-excursion
4941     (beginning-of-line)
4942     (if (not (re-search-forward ": \\(.*\\)$" (gnus-point-at-eol) t))
4943         ()
4944       (gnus-group-prefixed-name 
4945        (buffer-substring (match-beginning 1) (match-end 1))
4946        gnus-browse-current-method))))
4947   
4948 (defun gnus-browse-unsubscribe-group ()
4949   (let ((sub nil)
4950         (buffer-read-only nil)
4951         group)
4952     (save-excursion
4953       (beginning-of-line)
4954       (if (= (following-char) ?K) (setq sub t))
4955       (setq group (gnus-browse-group-name))
4956       (beginning-of-line)
4957       (delete-char 1)
4958       (if sub
4959           (progn
4960             (gnus-group-change-level 
4961              (list t group gnus-level-default-subscribed
4962                    nil nil gnus-browse-current-method) 
4963              gnus-level-default-subscribed gnus-level-killed
4964              (gnus-gethash (car (nth 1 gnus-newsrc-alist)) gnus-newsrc-hashtb)
4965              t)
4966             (insert ? ))
4967         (gnus-group-change-level 
4968          group gnus-level-killed gnus-level-default-subscribed)
4969         (insert ?K)))
4970     t))
4971
4972 (defun gnus-browse-exit ()
4973   "Quit browsing and return to the group buffer."
4974   (interactive)
4975   (if (eq major-mode 'gnus-browse-mode)
4976       (kill-buffer (current-buffer)))
4977   (if gnus-browse-return-buffer
4978       (gnus-configure-windows 'server)
4979     (gnus-configure-windows 'group)))
4980
4981 (defun gnus-browse-describe-briefly ()
4982   "Give a one line description of the group mode commands."
4983   (interactive)
4984   (gnus-message 6
4985    (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")))
4986       
4987 \f
4988 ;;;
4989 ;;; Gnus summary mode
4990 ;;;
4991
4992 (defvar gnus-summary-mode-map nil)
4993 (defvar gnus-summary-mark-map nil)
4994 (defvar gnus-summary-mscore-map nil)
4995 (defvar gnus-summary-article-map nil)
4996 (defvar gnus-summary-thread-map nil)
4997 (defvar gnus-summary-goto-map nil)
4998 (defvar gnus-summary-exit-map nil)
4999 (defvar gnus-summary-various-map nil)
5000 (defvar gnus-summary-interest-map nil)
5001 (defvar gnus-summary-sort-map nil)
5002 (defvar gnus-summary-backend-map nil)
5003 (defvar gnus-summary-save-map nil)
5004 (defvar gnus-summary-wash-map nil)
5005 (defvar gnus-summary-help-map nil)
5006
5007 (put 'gnus-summary-mode 'mode-class 'special)
5008
5009 (if gnus-summary-mode-map
5010     nil
5011   (setq gnus-summary-mode-map (make-keymap))
5012   (suppress-keymap gnus-summary-mode-map)
5013
5014   ;; Non-orthogonal keys
5015
5016   (define-key gnus-summary-mode-map " " 'gnus-summary-next-page)
5017   (define-key gnus-summary-mode-map "\177" 'gnus-summary-prev-page)
5018   (define-key gnus-summary-mode-map "\r" 'gnus-summary-scroll-up)
5019   (define-key gnus-summary-mode-map "n" 'gnus-summary-next-unread-article)
5020   (define-key gnus-summary-mode-map "p" 'gnus-summary-prev-unread-article)
5021   (define-key gnus-summary-mode-map "N" 'gnus-summary-next-article)
5022   (define-key gnus-summary-mode-map "P" 'gnus-summary-prev-article)
5023   (define-key gnus-summary-mode-map "\M-\C-n" 'gnus-summary-next-same-subject)
5024   (define-key gnus-summary-mode-map "\M-\C-p" 'gnus-summary-prev-same-subject)
5025   (define-key gnus-summary-mode-map "\M-n" 'gnus-summary-next-unread-subject)
5026   (define-key gnus-summary-mode-map "\M-p" 'gnus-summary-prev-unread-subject)
5027   (define-key gnus-summary-mode-map "." 'gnus-summary-first-unread-article)
5028   (define-key gnus-summary-mode-map "," 'gnus-summary-best-unread-article)
5029   (define-key gnus-summary-mode-map "\M-s" 'gnus-summary-search-article-forward)
5030   (define-key gnus-summary-mode-map "\M-r" 'gnus-summary-search-article-backward)
5031   (define-key gnus-summary-mode-map "<" 'gnus-summary-beginning-of-article)
5032   (define-key gnus-summary-mode-map ">" 'gnus-summary-end-of-article)
5033   (define-key gnus-summary-mode-map "j" 'gnus-summary-goto-subject)
5034   (define-key gnus-summary-mode-map "^" 'gnus-summary-refer-parent-article)
5035   (define-key gnus-summary-mode-map "\M-^" 'gnus-summary-refer-article)
5036   (define-key gnus-summary-mode-map "u" 'gnus-summary-tick-article-forward)
5037   (define-key gnus-summary-mode-map "!" 'gnus-summary-tick-article-forward)
5038   (define-key gnus-summary-mode-map "U" 'gnus-summary-tick-article-backward)
5039   (define-key gnus-summary-mode-map "d" 'gnus-summary-mark-as-read-forward)
5040   (define-key gnus-summary-mode-map "D" 'gnus-summary-mark-as-read-backward)
5041   (define-key gnus-summary-mode-map "E" 'gnus-summary-mark-as-expirable)
5042   (define-key gnus-summary-mode-map "\M-u" 'gnus-summary-clear-mark-forward)
5043   (define-key gnus-summary-mode-map "\M-U" 'gnus-summary-clear-mark-backward)
5044   (define-key gnus-summary-mode-map "k" 'gnus-summary-kill-same-subject-and-select)
5045   (define-key gnus-summary-mode-map "\C-k" 'gnus-summary-kill-same-subject)
5046   (define-key gnus-summary-mode-map "\M-\C-k" 'gnus-summary-kill-thread)
5047   (define-key gnus-summary-mode-map "\M-\C-l" 'gnus-summary-lower-thread)
5048   (define-key gnus-summary-mode-map "e" 'gnus-summary-edit-article)
5049   (define-key gnus-summary-mode-map "#" 'gnus-summary-mark-as-processable)
5050   (define-key gnus-summary-mode-map "\M-#" 'gnus-summary-unmark-as-processable)
5051   (define-key gnus-summary-mode-map "\M-\C-t" 'gnus-summary-toggle-threads)
5052   (define-key gnus-summary-mode-map "\M-\C-s" 'gnus-summary-show-thread)
5053   (define-key gnus-summary-mode-map "\M-\C-h" 'gnus-summary-hide-thread)
5054   (define-key gnus-summary-mode-map "\M-\C-f" 'gnus-summary-next-thread)
5055   (define-key gnus-summary-mode-map "\M-\C-b" 'gnus-summary-prev-thread)
5056   (define-key gnus-summary-mode-map "\M-\C-u" 'gnus-summary-up-thread)
5057   (define-key gnus-summary-mode-map "\M-\C-d" 'gnus-summary-down-thread)
5058   (define-key gnus-summary-mode-map "&" 'gnus-summary-execute-command)
5059   (define-key gnus-summary-mode-map "c" 'gnus-summary-catchup-and-exit)
5060   (define-key gnus-summary-mode-map "\C-w" 'gnus-summary-mark-region-as-read)
5061   (define-key gnus-summary-mode-map "\C-t" 'gnus-summary-toggle-truncation)
5062   (define-key gnus-summary-mode-map "?" 'gnus-summary-mark-as-dormant)
5063   (define-key gnus-summary-mode-map "\C-c\M-\C-s" 'gnus-summary-show-all-expunged)
5064   (define-key gnus-summary-mode-map "\C-c\C-s\C-n" 'gnus-summary-sort-by-number)
5065   (define-key gnus-summary-mode-map "\C-c\C-s\C-a" 'gnus-summary-sort-by-author)
5066   (define-key gnus-summary-mode-map "\C-c\C-s\C-s" 'gnus-summary-sort-by-subject)
5067   (define-key gnus-summary-mode-map "\C-c\C-s\C-d" 'gnus-summary-sort-by-date)
5068   (define-key gnus-summary-mode-map "\C-c\C-s\C-i" 'gnus-summary-sort-by-score)
5069   (define-key gnus-summary-mode-map "=" 'gnus-summary-expand-window)
5070   (define-key gnus-summary-mode-map "\C-x\C-s" 'gnus-summary-reselect-current-group)
5071   (define-key gnus-summary-mode-map "\M-g" 'gnus-summary-rescan-group)
5072   (define-key gnus-summary-mode-map "w" 'gnus-summary-stop-page-breaking)
5073   (define-key gnus-summary-mode-map "\C-c\C-r" 'gnus-summary-caesar-message)
5074   (define-key gnus-summary-mode-map "\M-t" 'gnus-summary-toggle-mime)
5075   (define-key gnus-summary-mode-map "f" 'gnus-summary-followup)
5076   (define-key gnus-summary-mode-map "F" 'gnus-summary-followup-with-original)
5077   (define-key gnus-summary-mode-map "C" 'gnus-summary-cancel-article)
5078   (define-key gnus-summary-mode-map "r" 'gnus-summary-reply)
5079   (define-key gnus-summary-mode-map "R" 'gnus-summary-reply-with-original)
5080   (define-key gnus-summary-mode-map "\C-c\C-f" 'gnus-summary-mail-forward)
5081   (define-key gnus-summary-mode-map "o" 'gnus-summary-save-article)
5082   (define-key gnus-summary-mode-map "\C-o" 'gnus-summary-save-article-mail)
5083   (define-key gnus-summary-mode-map "|" 'gnus-summary-pipe-output)
5084   (define-key gnus-summary-mode-map "\M-k" 'gnus-summary-edit-local-kill)
5085   (define-key gnus-summary-mode-map "\M-K" 'gnus-summary-edit-global-kill)
5086   (define-key gnus-summary-mode-map "V" 'gnus-version)
5087   (define-key gnus-summary-mode-map "\C-c\C-d" 'gnus-summary-describe-group)
5088   (define-key gnus-summary-mode-map "q" 'gnus-summary-exit)
5089   (define-key gnus-summary-mode-map "Q" 'gnus-summary-exit-no-update)
5090   (define-key gnus-summary-mode-map "\C-c\C-i" 'gnus-info-find-node)
5091   (define-key gnus-summary-mode-map gnus-mouse-2 'gnus-mouse-pick-article)
5092   (define-key gnus-summary-mode-map "m" 'gnus-summary-mail-other-window)
5093   (define-key gnus-summary-mode-map "a" 'gnus-summary-post-news)
5094   (define-key gnus-summary-mode-map "x" 'gnus-summary-remove-lines-marked-as-read)
5095 ; (define-key gnus-summary-mode-map "X" 'gnus-summary-remove-lines-marked-with)
5096   (define-key gnus-summary-mode-map "s" 'gnus-summary-isearch-article)
5097   (define-key gnus-summary-mode-map "t" 'gnus-summary-toggle-header)
5098   (define-key gnus-summary-mode-map "g" 'gnus-summary-show-article)
5099 ;  (define-key gnus-summary-mode-map "?" 'gnus-summary-describe-briefly)
5100   (define-key gnus-summary-mode-map "l" 'gnus-summary-goto-last-article)
5101   (define-key gnus-summary-mode-map "\C-c\C-v\C-v" 'gnus-uu-decode-uu-view)
5102   (define-key gnus-summary-mode-map "\C-d" 'gnus-summary-enter-digest-group)
5103
5104
5105   ;; Sort of orthogonal keymap
5106   (define-prefix-command 'gnus-summary-mark-map)
5107   (define-key gnus-summary-mode-map "M" 'gnus-summary-mark-map)
5108   (define-key gnus-summary-mark-map "t" 'gnus-summary-tick-article-forward)
5109   (define-key gnus-summary-mark-map "!" 'gnus-summary-tick-article-forward)
5110   (define-key gnus-summary-mark-map "d" 'gnus-summary-mark-as-read-forward)
5111   (define-key gnus-summary-mark-map "r" 'gnus-summary-mark-as-read-forward)
5112   (define-key gnus-summary-mark-map "c" 'gnus-summary-clear-mark-forward)
5113   (define-key gnus-summary-mark-map " " 'gnus-summary-clear-mark-forward)
5114   (define-key gnus-summary-mark-map "e" 'gnus-summary-mark-as-expirable)
5115   (define-key gnus-summary-mark-map "x" 'gnus-summary-mark-as-expirable)
5116   (define-key gnus-summary-mark-map "?" 'gnus-summary-mark-as-dormant)
5117   (define-key gnus-summary-mark-map "b" 'gnus-summary-set-bookmark)
5118   (define-key gnus-summary-mark-map "B" 'gnus-summary-remove-bookmark)
5119   (define-key gnus-summary-mark-map "#" 'gnus-summary-mark-as-processable)
5120   (define-key gnus-summary-mark-map "\M-#" 'gnus-summary-unmark-as-processable)
5121   (define-key gnus-summary-mark-map "\M-r" 'gnus-summary-remove-lines-marked-as-read)
5122   (define-key gnus-summary-mark-map "\M-\C-r" 'gnus-summary-remove-lines-marked-with)
5123   (define-key gnus-summary-mark-map "D" 'gnus-summary-show-all-dormant)
5124   (define-key gnus-summary-mark-map "\M-D" 'gnus-summary-hide-all-dormant)
5125   (define-key gnus-summary-mark-map "S" 'gnus-summary-show-all-expunged)
5126   (define-key gnus-summary-mark-map "C" 'gnus-summary-catchup)
5127   (define-key gnus-summary-mark-map "H" 'gnus-summary-catchup-to-here)
5128   (define-key gnus-summary-mark-map "\C-c" 'gnus-summary-catchup-all)
5129   (define-key gnus-summary-mark-map "k" 'gnus-summary-kill-same-subject-and-select)
5130   (define-key gnus-summary-mark-map "K" 'gnus-summary-kill-same-subject)
5131
5132   (define-prefix-command 'gnus-summary-mscore-map)
5133   (define-key gnus-summary-mark-map "s" 'gnus-summary-mscore-map)
5134   (define-key gnus-summary-mscore-map "c" 'gnus-summary-clear-above)
5135   (define-key gnus-summary-mscore-map "u" 'gnus-summary-tick-above)
5136   (define-key gnus-summary-mscore-map "m" 'gnus-summary-mark-above)
5137   (define-key gnus-summary-mscore-map "k" 'gnus-summary-kill-below)
5138
5139   (define-key gnus-summary-mark-map "p" 'gnus-uu-mark-map)
5140   
5141   (define-key gnus-summary-mode-map "S" 'gnus-summary-send-map)
5142   
5143   (define-prefix-command 'gnus-summary-goto-map)
5144   (define-key gnus-summary-mode-map "G" 'gnus-summary-goto-map)
5145   (define-key gnus-summary-goto-map "n" 'gnus-summary-next-unread-article)
5146   (define-key gnus-summary-goto-map "p" 'gnus-summary-prev-unread-article)
5147   (define-key gnus-summary-goto-map "N" 'gnus-summary-next-article)
5148   (define-key gnus-summary-goto-map "P" 'gnus-summary-prev-article)
5149   (define-key gnus-summary-goto-map "\C-n" 'gnus-summary-next-same-subject)
5150   (define-key gnus-summary-goto-map "\C-p" 'gnus-summary-prev-same-subject)
5151   (define-key gnus-summary-goto-map "\M-n" 'gnus-summary-next-unread-subject)
5152   (define-key gnus-summary-goto-map "\M-p" 'gnus-summary-prev-unread-subject)
5153   (define-key gnus-summary-goto-map "f" 'gnus-summary-first-unread-article)
5154   (define-key gnus-summary-goto-map "b" 'gnus-summary-best-unread-article)
5155   (define-key gnus-summary-goto-map "g" 'gnus-summary-goto-subject)
5156   (define-key gnus-summary-goto-map "l" 'gnus-summary-goto-last-article)
5157   (define-key gnus-summary-goto-map "p" 'gnus-summary-pop-article)
5158
5159
5160   (define-prefix-command 'gnus-summary-thread-map)
5161   (define-key gnus-summary-mode-map "T" 'gnus-summary-thread-map)
5162   (define-key gnus-summary-thread-map "k" 'gnus-summary-kill-thread)
5163   (define-key gnus-summary-thread-map "l" 'gnus-summary-lower-thread)
5164   (define-key gnus-summary-thread-map "i" 'gnus-summary-raise-thread)
5165   (define-key gnus-summary-thread-map "T" 'gnus-summary-toggle-threads)
5166   (define-key gnus-summary-thread-map "s" 'gnus-summary-show-thread)
5167   (define-key gnus-summary-thread-map "S" 'gnus-summary-show-all-threads)
5168   (define-key gnus-summary-thread-map "h" 'gnus-summary-hide-thread)
5169   (define-key gnus-summary-thread-map "H" 'gnus-summary-hide-all-threads)
5170   (define-key gnus-summary-thread-map "n" 'gnus-summary-next-thread)
5171   (define-key gnus-summary-thread-map "p" 'gnus-summary-prev-thread)
5172   (define-key gnus-summary-thread-map "u" 'gnus-summary-up-thread)
5173   (define-key gnus-summary-thread-map "d" 'gnus-summary-down-thread)
5174   (define-key gnus-summary-thread-map "#" 'gnus-uu-mark-thread)
5175
5176   
5177   (define-prefix-command 'gnus-summary-exit-map)
5178   (define-key gnus-summary-mode-map "Z" 'gnus-summary-exit-map)
5179   (define-key gnus-summary-exit-map "c" 'gnus-summary-catchup-and-exit)
5180   (define-key gnus-summary-exit-map "C" 'gnus-summary-catchup-all-and-exit)
5181   (define-key gnus-summary-exit-map "E" 'gnus-summary-exit-no-update)
5182   (define-key gnus-summary-exit-map "Q" 'gnus-summary-exit)
5183   (define-key gnus-summary-exit-map "Z" 'gnus-summary-exit)
5184   (define-key gnus-summary-exit-map "n" 'gnus-summary-catchup-and-goto-next-group)
5185   (define-key gnus-summary-exit-map "R" 'gnus-summary-reselect-current-group)
5186   (define-key gnus-summary-exit-map "G" 'gnus-summary-rescan-group)
5187   (define-key gnus-summary-exit-map "N" 'gnus-summary-next-group)
5188   (define-key gnus-summary-exit-map "P" 'gnus-summary-prev-group)
5189
5190
5191   (define-prefix-command 'gnus-summary-article-map)
5192   (define-key gnus-summary-mode-map "A" 'gnus-summary-article-map)
5193   (define-key gnus-summary-article-map " " 'gnus-summary-next-page)
5194   (define-key gnus-summary-article-map "n" 'gnus-summary-next-page)
5195   (define-key gnus-summary-article-map "\177" 'gnus-summary-prev-page)
5196   (define-key gnus-summary-article-map "p" 'gnus-summary-prev-page)
5197   (define-key gnus-summary-article-map "\r" 'gnus-summary-scroll-up)
5198   (define-key gnus-summary-article-map "<" 'gnus-summary-beginning-of-article)
5199   (define-key gnus-summary-article-map ">" 'gnus-summary-end-of-article)
5200   (define-key gnus-summary-article-map "b" 'gnus-summary-beginning-of-article)
5201   (define-key gnus-summary-article-map "e" 'gnus-summary-end-of-article)
5202   (define-key gnus-summary-article-map "^" 'gnus-summary-refer-parent-article)
5203   (define-key gnus-summary-article-map "r" 'gnus-summary-refer-parent-article)
5204   (define-key gnus-summary-article-map "w" 'gnus-summary-stop-page-breaking)
5205   (define-key gnus-summary-article-map "c" 'gnus-summary-caesar-message)
5206   (define-key gnus-summary-article-map "g" 'gnus-summary-show-article)
5207   (define-key gnus-summary-article-map "t" 'gnus-summary-toggle-header)
5208   (define-key gnus-summary-article-map "m" 'gnus-summary-toggle-mime)
5209   (define-key gnus-summary-article-map "s" 'gnus-summary-isearch-article)
5210
5211
5212   (define-prefix-command 'gnus-summary-wash-map)
5213   (define-key gnus-summary-mode-map "W" 'gnus-summary-wash-map)
5214   (define-key gnus-summary-wash-map "h" 'gnus-article-hide-headers)
5215   (define-key gnus-summary-wash-map "s" 'gnus-article-hide-signature)
5216   (define-key gnus-summary-wash-map "c" 'gnus-article-hide-citation)
5217   (define-key gnus-summary-wash-map "o" 'gnus-article-treat-overstrike)
5218   (define-key gnus-summary-wash-map "w" 'gnus-article-word-wrap)
5219   (define-key gnus-summary-wash-map "d" 'gnus-article-remove-cr)
5220   (define-key gnus-summary-wash-map "q" 'gnus-article-de-quoted-unreadable)
5221   (define-key gnus-summary-wash-map "f" 'gnus-article-display-x-face)
5222   (define-key gnus-summary-wash-map "t" 'gnus-article-date-ut)
5223   (define-key gnus-summary-wash-map "\C-t" 'gnus-article-date-local)
5224   (define-key gnus-summary-wash-map "T" 'gnus-article-date-lapsed)
5225
5226   (define-key gnus-summary-wash-map "A" 'gnus-article-highlight)
5227   (define-key gnus-summary-wash-map "a" 'gnus-article-hide)
5228   (define-key gnus-summary-wash-map "H" 'gnus-article-highlight-headers)
5229   (define-key gnus-summary-wash-map "C" 'gnus-article-highlight-citation)
5230   (define-key gnus-summary-wash-map "S" 'gnus-article-highlight-signature)
5231   (define-key gnus-summary-wash-map "b" 'gnus-article-add-buttons)
5232
5233
5234   (define-prefix-command 'gnus-summary-help-map)
5235   (define-key gnus-summary-mode-map "H" 'gnus-summary-help-map)
5236   (define-key gnus-summary-help-map "v" 'gnus-version)
5237   (define-key gnus-summary-help-map "f" 'gnus-summary-fetch-faq)
5238   (define-key gnus-summary-help-map "d" 'gnus-summary-describe-group)
5239   (define-key gnus-summary-help-map "h" 'gnus-summary-describe-briefly)
5240   (define-key gnus-summary-help-map "i" 'gnus-info-find-node)
5241
5242
5243   (define-prefix-command 'gnus-summary-backend-map)
5244   (define-key gnus-summary-mode-map "B" 'gnus-summary-backend-map)
5245   (define-key gnus-summary-backend-map "e" 'gnus-summary-expire-articles)
5246   (define-key gnus-summary-backend-map "\177" 'gnus-summary-delete-article)
5247   (define-key gnus-summary-backend-map "m" 'gnus-summary-move-article)
5248   (define-key gnus-summary-backend-map "r" 'gnus-summary-respool-article)
5249   (define-key gnus-summary-backend-map "w" 'gnus-summary-edit-article)
5250   (define-key gnus-summary-backend-map "c" 'gnus-summary-copy-article)
5251   (define-key gnus-summary-backend-map "q" 'gnus-summary-fancy-query)
5252   (define-key gnus-summary-backend-map "i" 'gnus-summary-import-article)
5253
5254
5255   (define-prefix-command 'gnus-summary-save-map)
5256   (define-key gnus-summary-mode-map "O" 'gnus-summary-save-map)
5257   (define-key gnus-summary-save-map "o" 'gnus-summary-save-article)
5258   (define-key gnus-summary-save-map "m" 'gnus-summary-save-article-mail)
5259   (define-key gnus-summary-save-map "r" 'gnus-summary-save-article-rmail)
5260   (define-key gnus-summary-save-map "f" 'gnus-summary-save-article-file)
5261   (define-key gnus-summary-save-map "h" 'gnus-summary-save-article-folder)
5262   (define-key gnus-summary-save-map "v" 'gnus-summary-save-article-vm)
5263   (define-key gnus-summary-save-map "p" 'gnus-summary-pipe-output)
5264 ;  (define-key gnus-summary-save-map "s" 'gnus-soup-add-article)
5265
5266   (define-key gnus-summary-mode-map "X" 'gnus-uu-extract-map)
5267   
5268   (define-prefix-command 'gnus-summary-various-map)
5269   (define-key gnus-summary-mode-map "V" 'gnus-summary-various-map)
5270   (define-key gnus-summary-various-map "u" 'gnus-summary-universal-argument)
5271   (define-key gnus-summary-various-map "\C-s" 'gnus-summary-search-article-forward)
5272   (define-key gnus-summary-various-map "\C-r" 'gnus-summary-search-article-backward)
5273   (define-key gnus-summary-various-map "r" 'gnus-summary-refer-article)
5274   (define-key gnus-summary-various-map "&" 'gnus-summary-execute-command)
5275   (define-key gnus-summary-various-map "T" 'gnus-summary-toggle-truncation)
5276   (define-key gnus-summary-various-map "e" 'gnus-summary-expand-window)
5277   (define-key gnus-summary-various-map "D" 'gnus-summary-enter-digest-group)
5278   (define-key gnus-summary-various-map "k" 'gnus-summary-edit-local-kill)
5279   (define-key gnus-summary-various-map "K" 'gnus-summary-edit-global-kill)
5280
5281   (define-key gnus-summary-various-map "S" 'gnus-summary-score-map)
5282
5283   (define-prefix-command 'gnus-summary-sort-map)
5284   (define-key gnus-summary-various-map "s" 'gnus-summary-sort-map)
5285   (define-key gnus-summary-sort-map "n" 'gnus-summary-sort-by-number)
5286   (define-key gnus-summary-sort-map "a" 'gnus-summary-sort-by-author)
5287   (define-key gnus-summary-sort-map "s" 'gnus-summary-sort-by-subject)
5288   (define-key gnus-summary-sort-map "d" 'gnus-summary-sort-by-date)
5289   (define-key gnus-summary-sort-map "i" 'gnus-summary-sort-by-score)
5290
5291   (define-key gnus-summary-mode-map "I" 'gnus-summary-increase-score)
5292   (define-key gnus-summary-mode-map "L" 'gnus-summary-lower-score)
5293   )
5294
5295
5296 \f
5297
5298 (defun gnus-summary-mode ()
5299   "Major mode for reading articles.
5300
5301 All normal editing commands are switched off.
5302 \\<gnus-summary-mode-map>
5303 Each line in this buffer represents one article.  To read an
5304 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
5305 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]', 
5306 respectively.
5307
5308 You can also post articles and send mail from this buffer.  To 
5309 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author 
5310 of an article, type `\\[gnus-summary-reply]'.
5311
5312 There are approx. one gazillion commands you can execute in this 
5313 buffer; read the info pages for more information (`\\[gnus-info-find-node]'). 
5314
5315 The following commands are available:
5316
5317 \\{gnus-summary-mode-map}"
5318   (interactive)
5319   (if gnus-visual (gnus-summary-make-menu-bar))
5320   (kill-all-local-variables)
5321   (let ((locals gnus-summary-local-variables))
5322     (while locals
5323       (if (consp (car locals))
5324           (progn
5325             (make-local-variable (car (car locals)))
5326             (set (car (car locals)) (eval (cdr (car locals)))))
5327         (make-local-variable (car locals))
5328         (set (car locals) nil))
5329       (setq locals (cdr locals))))
5330   (gnus-make-thread-indent-array)
5331   (gnus-update-format-specifications)
5332   (setq mode-line-modified "-- ")
5333   (make-local-variable 'mode-line-format)
5334   (setq mode-line-format (copy-sequence mode-line-format))
5335   (and (equal (nth 3 mode-line-format) "   ")
5336        (setcar (nthcdr 3 mode-line-format) ""))
5337   (setq major-mode 'gnus-summary-mode)
5338   (setq mode-name "Summary")
5339   (make-local-variable 'minor-mode-alist)
5340   (use-local-map gnus-summary-mode-map)
5341   (buffer-disable-undo (current-buffer))
5342   (setq buffer-read-only t)             ;Disable modification
5343   (setq truncate-lines t)
5344   (setq selective-display t)
5345   (setq selective-display-ellipses t)   ;Display `...'
5346   (setq buffer-display-table gnus-summary-display-table)
5347   (run-hooks 'gnus-summary-mode-hook))
5348
5349 (defun gnus-summary-make-display-table ()
5350   ;; Change the display table.  Odd characters have a tendency to mess
5351   ;; up nicely formatted displays - we make all possible glyphs
5352   ;; display only a single character.
5353
5354   ;; We start from the standard display table, if any.
5355   (setq gnus-summary-display-table 
5356         (or (copy-sequence standard-display-table)
5357             (make-display-table)))
5358   ;; Nix out all the control chars...
5359   (let ((i 32))
5360     (while (>= (setq i (1- i)) 0)
5361       (aset gnus-summary-display-table i [??])))
5362   ;; ... but not newline and cr, of course. (cr is necessary for the
5363   ;; selective display).  
5364   (aset gnus-summary-display-table ?\n nil)
5365   (aset gnus-summary-display-table ?\r nil)
5366   ;; We nix out any glyphs over 126 that are not set already.  
5367   (let ((i 256))
5368     (while (>= (setq i (1- i)) 127)
5369       ;; Only modify if the entry is nil.
5370       (or (aref gnus-summary-display-table i) 
5371           (aset gnus-summary-display-table i [??])))))
5372
5373 (defun gnus-summary-clear-local-variables ()
5374   (let ((locals gnus-summary-local-variables))
5375     (while locals
5376       (if (consp (car locals))
5377           (and (vectorp (car (car locals)))
5378                (set (car (car locals)) nil))
5379         (and (vectorp (car locals))
5380              (set (car locals) nil)))
5381       (setq locals (cdr locals)))))
5382
5383 (defun gnus-mouse-pick-article (e)
5384   (interactive "e")
5385   (mouse-set-point e)
5386   (gnus-summary-next-page nil t))
5387
5388 (defun gnus-summary-setup-buffer (group)
5389   "Initialize summary buffer."
5390   (let ((buffer (concat "*Summary " group "*")))
5391     (if (get-buffer buffer)
5392         (progn
5393           (set-buffer buffer)
5394           (not gnus-newsgroup-begin))
5395       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
5396       (setq gnus-summary-buffer (set-buffer (get-buffer-create buffer)))
5397       (gnus-add-current-to-buffer-list)
5398       (gnus-summary-mode)
5399       (and gnus-carpal (gnus-carpal-setup-buffer 'summary))
5400       (setq gnus-newsgroup-name group)
5401       t)))
5402
5403 (defun gnus-set-global-variables ()
5404   ;; Set the global equivalents of the summary buffer-local variables
5405   ;; to the latest values they had. These reflect the summary buffer
5406   ;; that was in action when the last article was fetched.
5407   (if (eq major-mode 'gnus-summary-mode) 
5408       (progn
5409         (setq gnus-summary-buffer (current-buffer))
5410         (let ((name gnus-newsgroup-name)
5411               (marked gnus-newsgroup-marked)
5412               (unread gnus-newsgroup-unreads)
5413               (headers gnus-current-headers)
5414               (score-file gnus-current-score-file))
5415           (save-excursion
5416             (set-buffer gnus-group-buffer)
5417             (setq gnus-newsgroup-name name)
5418             (setq gnus-newsgroup-marked marked)
5419             (setq gnus-newsgroup-unreads unread)
5420             (setq gnus-current-headers headers)
5421             (setq gnus-current-score-file score-file))))))
5422
5423 (defun gnus-summary-insert-dummy-line (sformat subject number)
5424   (if (not sformat) 
5425       (setq sformat gnus-summary-dummy-line-format-spec))
5426   (let (b)
5427     (beginning-of-line)
5428     (setq b (point))
5429     (insert (eval sformat))
5430     (add-text-properties
5431      b (1+ b)
5432      (list 'gnus-number number 
5433            'gnus-mark gnus-dummy-mark
5434            'gnus-level 0))))
5435
5436 (defvar gnus-thread-indent-array nil)
5437 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
5438 (defun gnus-make-thread-indent-array ()
5439   (let ((n 200))
5440     (if (and gnus-thread-indent-array
5441              (= gnus-thread-indent-level gnus-thread-indent-array-level))
5442         nil
5443       (setq gnus-thread-indent-array (make-vector 201 "")
5444             gnus-thread-indent-array-level gnus-thread-indent-level)
5445       (while (>= n 0)
5446         (aset gnus-thread-indent-array n
5447               (make-string (* n gnus-thread-indent-level) ? ))
5448         (setq n (1- n))))))
5449
5450 (defun gnus-summary-insert-line 
5451   (sformat header level current unread replied expirable subject-or-nil
5452            &optional dummy score)
5453   (or sformat (setq sformat gnus-summary-line-format-spec))
5454   (let* ((indentation (aref gnus-thread-indent-array level))
5455          (lines (header-lines header))
5456          (score (or score gnus-summary-default-score 0))
5457          (score-char
5458           (if (or (null gnus-summary-default-score)
5459                   (<= (abs (- score gnus-summary-default-score))
5460                       gnus-summary-zcore-fuzz)) ? 
5461             (if (< score gnus-summary-default-score)
5462                 gnus-score-below-mark gnus-score-over-mark)))
5463          (replied (if replied gnus-replied-mark ? ))
5464          (from (header-from header))
5465          (name-address (funcall gnus-extract-address-components from))
5466          (address (car (cdr name-address)))
5467          (name (or (car name-address) (car (cdr name-address))))
5468          (subject (header-subject header))
5469          (number (header-number header))
5470          (opening-bracket (if dummy ?\< ?\[))
5471          (closing-bracket (if dummy ?\> ?\]))
5472          (buffer-read-only nil)
5473          (b (progn (beginning-of-line) (point))))
5474     (or (numberp lines) (setq lines 0))
5475     (insert (eval sformat))
5476     (add-text-properties
5477      b (1+ b) (list 'gnus-number number 
5478                     'gnus-mark (or unread gnus-unread-mark)
5479                     'gnus-level level))))
5480
5481 (defun gnus-summary-update-line (&optional dont-update)
5482   ;; Update summary line after change.
5483   (or (not gnus-summary-default-score)
5484       gnus-summary-inhibit-highlight
5485       (let ((gnus-summary-inhibit-highlight t)
5486             (article (gnus-summary-article-number)))
5487         (progn
5488           (or dont-update
5489               (if (and gnus-summary-mark-below
5490                        (< (gnus-summary-article-score)
5491                           gnus-summary-mark-below))
5492                   (and (not (memq article gnus-newsgroup-marked))
5493                        (not (memq article gnus-newsgroup-dormant))
5494                        (memq article gnus-newsgroup-unreads)
5495                        (gnus-summary-mark-article nil gnus-low-score-mark))
5496                 (and (eq (gnus-summary-article-mark) gnus-low-score-mark)
5497                      (gnus-summary-mark-article nil gnus-unread-mark))))
5498           (and gnus-visual
5499                (run-hooks 'gnus-summary-update-hook))))))
5500
5501 (defun gnus-summary-update-lines (&optional beg end)
5502   ;; Rehighlight summary buffer according to `gnus-summary-highlight'.
5503   (let ((beg (or beg (point-min)))
5504         (end (or end (point-max))))
5505     (save-excursion
5506       (set-buffer gnus-summary-buffer)
5507       (goto-char beg)
5508       (while (and (not (eobp)) (< (point) end))
5509         (gnus-summary-update-line)
5510         (forward-line 1)))))
5511
5512 (defun gnus-summary-number-of-articles-in-thread (thread &optional char)
5513   ;; Sum up all elements (and sub-elements) in a list.
5514   (let ((number 
5515          (if (listp thread) 
5516              (apply 
5517               '+ (mapcar 'gnus-summary-number-of-articles-in-thread thread))
5518            1)))
5519     (if char 
5520         (if (> number 1) gnus-not-empty-thread-mark
5521           gnus-empty-thread-mark)
5522       number)))
5523
5524 (defun gnus-summary-read-group (group &optional show-all no-article kill-buffer)
5525   "Start reading news in newsgroup GROUP.
5526 If SHOW-ALL is non-nil, already read articles are also listed.
5527 If NO-ARTICLE is non-nil, no article is selected initially."
5528   (gnus-message 5 "Retrieving newsgroup: %s..." group)
5529   (let* ((new-group (gnus-summary-setup-buffer group))
5530          (did-select (and new-group (gnus-select-newsgroup group show-all)))
5531          (method (car (gnus-find-method-for-group group))))
5532     (cond 
5533      ((not new-group)
5534       (gnus-set-global-variables)
5535       (gnus-kill-buffer kill-buffer)
5536       (gnus-configure-windows 'summary)
5537       (gnus-set-mode-line 'summary)
5538       (gnus-summary-position-cursor)
5539       (message "")
5540       t)
5541      ((null did-select) 
5542       (and (eq major-mode 'gnus-summary-mode)
5543            (not (equal (current-buffer) kill-buffer))
5544            (progn
5545              (kill-buffer (current-buffer))
5546              (set-buffer gnus-group-buffer)
5547              (gnus-group-next-unread-group 1)))
5548       (message "Can't select group")
5549       nil)
5550      ((eq did-select 'quit)
5551       (and (eq major-mode 'gnus-summary-mode)
5552            (not (equal (current-buffer) kill-buffer))
5553            (kill-buffer (current-buffer)))
5554       (gnus-kill-buffer kill-buffer)
5555       (gnus-configure-windows 'group)
5556       (gnus-group-next-unread-group 1)
5557       (signal 'quit nil))
5558      (t
5559       (gnus-set-global-variables)
5560       ;; Save the active value in effect when the group was entered.
5561       (setq gnus-newsgroup-active 
5562             (gnus-copy-sequence
5563              (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
5564       ;; You can change the subjects in this hook.
5565       (run-hooks 'gnus-select-group-hook)
5566       ;; Do score processing.
5567       (and gnus-use-scoring (gnus-possibly-score-headers))
5568       ;; Update the format specifiers.
5569       (gnus-update-format-specifications)
5570       ;; Generate the summary buffer.
5571       (gnus-summary-prepare)
5572       (if (zerop (buffer-size))
5573           (cond (gnus-newsgroup-dormant
5574                  (gnus-summary-show-all-dormant))
5575                 ((and gnus-newsgroup-scored show-all)
5576                  (gnus-summary-show-all-expunged))))
5577       ;; Function `gnus-apply-kill-file' must be called in this hook.
5578       (run-hooks 'gnus-apply-kill-hook)
5579       (if (zerop (buffer-size))
5580           (progn
5581             ;; This newsgroup is empty.
5582             (gnus-summary-catchup-and-exit nil t) ;Without confirmations.
5583             (gnus-message 6 "No unread news")
5584             (gnus-kill-buffer kill-buffer)
5585             nil)
5586         ;;(save-excursion
5587         ;;  (if kill-buffer
5588         ;;      (let ((gnus-summary-buffer kill-buffer))
5589         ;;      (gnus-configure-windows 'group))))
5590         ;; Hide conversation thread subtrees.  We cannot do this in
5591         ;; gnus-summary-prepare-hook since kill processing may not
5592         ;; work with hidden articles.
5593         (and gnus-show-threads
5594              gnus-thread-hide-subtree
5595              (gnus-summary-hide-all-threads))
5596         ;; Show first unread article if requested.
5597         (goto-char (point-min))
5598         (if (and (not no-article)
5599                  gnus-auto-select-first
5600                  (gnus-summary-first-unread-article))
5601             ()
5602           (gnus-configure-windows 'summary))
5603         (gnus-set-mode-line 'summary)
5604         (gnus-summary-position-cursor)
5605         ;; If in async mode, we send some info to the backend.
5606         (and gnus-newsgroup-async
5607              (setq gnus-newsgroup-threads (nreverse gnus-newsgroup-threads))
5608              (gnus-request-asynchronous 
5609               gnus-newsgroup-name
5610               (if (and gnus-asynchronous-article-function
5611                        (fboundp gnus-asynchronous-article-function))
5612                   (funcall gnus-asynchronous-article-function
5613                            gnus-newsgroup-threads)
5614                 gnus-newsgroup-threads)))
5615         (gnus-kill-buffer kill-buffer)
5616         (if (not (get-buffer-window gnus-group-buffer))
5617             ()
5618           ;; gotta use windows, because recenter does wierd stuff if
5619           ;; the current buffer ain't the displayed window.
5620           (let ((owin (selected-window))) 
5621             (select-window (get-buffer-window gnus-group-buffer))
5622             (and (gnus-group-goto-group group)
5623                  (recenter))
5624             (select-window owin))))
5625       t))))
5626
5627 (defun gnus-summary-prepare ()
5628   ;; Generate the summary buffer.
5629   (let ((buffer-read-only nil))
5630     (erase-buffer)
5631     (gnus-summary-prepare-threads 
5632      (if gnus-show-threads
5633          (gnus-gather-threads 
5634           (gnus-sort-threads 
5635            (if (and gnus-summary-expunge-below
5636                     (not gnus-fetch-old-headers))
5637                (gnus-make-threads-and-expunge)
5638              (gnus-make-threads))))
5639        gnus-newsgroup-headers)
5640      0 nil nil t)
5641     ;; Erase header retrieval message.
5642     (gnus-summary-update-lines)
5643     (message "")
5644     ;; Remove the final newline.
5645     ;;(goto-char (point-max))
5646     ;;(delete-char -1)
5647     ;; Call hooks for modifying summary buffer.
5648     ;; Suggested by sven@tde.LTH.Se (Sven Mattisson).
5649     (goto-char (point-min))
5650     (run-hooks 'gnus-summary-prepare-hook)))
5651
5652 (defun gnus-subject-equal (s1 s2)
5653   (cond 
5654    ((numberp gnus-summary-gather-subject-limit)
5655     (string= (if (> (length s1) gnus-summary-gather-subject-limit)
5656                  (substring s1 0 gnus-summary-gather-subject-limit)
5657                s1)
5658              (if (> (length s2) gnus-summary-gather-subject-limit)
5659                  (substring s2 0 gnus-summary-gather-subject-limit)
5660                s2)))
5661    ((eq 'fuzzy gnus-summary-gather-subject-limit)
5662     (string= (gnus-simplify-subject-fuzzy s1)
5663              (gnus-simplify-subject-fuzzy s2)))
5664    (t
5665     (string= s1 s2))))
5666
5667 (defun gnus-gather-threads (threads)
5668   "Gather threads that have lost their roots."
5669   (if (not gnus-summary-make-false-root)
5670       threads 
5671     (let ((hashtb (gnus-make-hashtable 1023))
5672           (prev threads)
5673           (result threads)
5674           thread subject hthread whole-subject)
5675       (while threads
5676         (setq whole-subject 
5677               (setq subject (header-subject (car (car threads)))))
5678         (if gnus-summary-gather-subject-limit
5679             (or (and (numberp gnus-summary-gather-subject-limit)
5680                      (> (length subject) gnus-summary-gather-subject-limit)
5681                      (setq subject
5682                            (substring subject 0 
5683                                       gnus-summary-gather-subject-limit)))
5684                 (and (eq 'fuzzy gnus-summary-gather-subject-limit)
5685                      (setq subject (gnus-simplify-subject-fuzzy subject))))
5686           (setq subject (gnus-simplify-subject-re subject)))
5687         (if (setq hthread 
5688                   (gnus-gethash subject hashtb))
5689             (progn
5690               (or (stringp (car (car hthread)))
5691                   (setcar hthread (list whole-subject (car hthread))))
5692               (setcdr (car hthread) (nconc (cdr (car hthread)) 
5693                                            (list (car threads))))
5694               (setcdr prev (cdr threads))
5695               (setq threads prev))
5696           (gnus-sethash subject threads hashtb))
5697         (setq prev threads)
5698         (setq threads (cdr threads)))
5699       result)))
5700
5701 (defun gnus-make-threads ()
5702   ;; This function takes the dependencies already made by 
5703   ;; `gnus-get-newsgroup-headers' and builds the trees. First we go
5704   ;; through the dependecies in the hash table and finds all the
5705   ;; roots. Roots do not refer back to any valid articles.
5706   (let (roots)
5707     (and gnus-fetch-old-headers (eq gnus-headers-retrieved-by 'nov)
5708          (gnus-build-old-threads))
5709     (mapatoms
5710      (lambda (refs)
5711        (if (not (car (symbol-value refs)))
5712            (setq roots (append (cdr (symbol-value refs)) roots))
5713          ;; Ok, these refer back to valid articles, but if
5714          ;; `gnus-thread-ignore-subject' is nil, we have to check that
5715          ;; the root has the same subject as its children. The children
5716          ;; that do not are made into roots and removed from the list
5717          ;; of children. 
5718          (or gnus-thread-ignore-subject
5719              (let* ((prev (symbol-value refs))
5720                     (subject (gnus-simplify-subject-re 
5721                               (header-subject (car prev))))
5722                     (headers (cdr prev)))
5723                (while headers
5724                  (if (not (string= subject
5725                                    (gnus-simplify-subject-re 
5726                                     (header-subject (car headers)))))
5727                      (progn
5728                        (setq roots (cons (car headers) roots))
5729                        (setcdr prev (cdr headers)))
5730                    (setq prev headers))
5731                  (setq headers (cdr headers)))))))
5732      gnus-newsgroup-dependencies)
5733     
5734     (mapcar 'gnus-trim-thread
5735             (apply 'append
5736                    (mapcar 'gnus-cut-thread
5737                            (mapcar 'gnus-make-sub-thread roots))))))
5738   
5739 (defun gnus-make-threads-and-expunge ()
5740   ;; This function takes the dependencies already made by 
5741   ;; `gnus-get-newsgroup-headers' and builds the trees. First we go
5742   ;; through the dependecies in the hash table and finds all the
5743   ;; roots. Roots do not refer back to any valid articles.
5744   (let ((default (or gnus-summary-default-score 0))
5745         (below gnus-summary-expunge-below)
5746         roots article)
5747     (and gnus-fetch-old-headers (eq gnus-headers-retrieved-by 'nov)
5748          (gnus-build-old-threads))
5749     (mapatoms
5750      (lambda (refs)
5751        (if (not (car (symbol-value refs)))
5752            ;; These articles do not refer back to any other articles -
5753            ;; they are roots.
5754            (let ((headers (cdr (symbol-value refs))))
5755              ;; We weed out the low-scored articles.
5756              (while headers
5757                (if (not (< (or (cdr (assq (header-number (car headers))
5758                                           gnus-newsgroup-scored)) default)
5759                            below))
5760                    ;; It is over.
5761                    (setq roots (cons (car headers) roots))
5762                  ;; It is below, so we mark it as read.
5763                  (setq gnus-newsgroup-unreads
5764                        (delq (header-number (car headers))
5765                              gnus-newsgroup-unreads)))
5766                (setq headers (cdr headers))))
5767          ;; Ok, these refer back to valid articles, but if
5768          ;; `gnus-thread-ignore-subject' is nil, we have to check that
5769          ;; the root has the same subject as its children. The children
5770          ;; that do not are made into roots and removed from the list
5771          ;; of children. 
5772          (or gnus-thread-ignore-subject
5773              (let* ((prev (symbol-value refs))
5774                     (subject (gnus-simplify-subject-re 
5775                               (header-subject (car prev))))
5776                     (headers (cdr prev)))
5777                (while headers
5778                  (if (not (string= subject
5779                                    (gnus-simplify-subject-re 
5780                                     (header-subject (car headers)))))
5781                      (progn
5782                        (if (not (< (or (cdr (assq (header-number (car headers))
5783                                                   gnus-newsgroup-scored))
5784                                        default) below))
5785                            (setq roots (cons (car headers) roots))
5786                          (setq gnus-newsgroup-unreads
5787                                (delq (header-number (car headers))
5788                                      gnus-newsgroup-unreads)))
5789                        (setcdr prev (cdr headers)))
5790                    (setq prev headers))
5791                  (setq headers (cdr headers)))))
5792          ;; If this article is expunged, some of the children might be
5793          ;; roots.  
5794          (if (< (or (cdr (assq (header-number (car (symbol-value refs)))
5795                                gnus-newsgroup-scored)) default)
5796                 below)
5797              (let* ((prev (symbol-value refs))
5798                     (headers (cdr prev)))
5799                (while headers
5800                  (setq article (header-number (car headers)))
5801                  (if (not (< (or (cdr (assq article gnus-newsgroup-scored))
5802                                  default) below))
5803                      (progn (setq roots (cons (car headers) roots))
5804                             (setq prev headers))
5805                    (setq gnus-newsgroup-unreads 
5806                          (delq article gnus-newsgroup-unreads))
5807                    (setcdr prev (cdr headers)))
5808                  (setq headers (cdr headers))))
5809            ;; It was not expunged, but we look at expunged children.
5810            (let* ((prev (symbol-value refs))
5811                   (headers (cdr prev))
5812                   article id)
5813              (while headers
5814                (setq article (header-number (car headers)))
5815                (if (not (< (or (cdr (assq article gnus-newsgroup-scored))
5816                                default) below))
5817                    (setq prev headers)
5818                  (setq gnus-newsgroup-unreads 
5819                        (delq article gnus-newsgroup-unreads))
5820                  (setcdr prev (cdr headers)))
5821                (setq headers (cdr headers)))))))
5822      gnus-newsgroup-dependencies)
5823
5824     (mapcar 'gnus-trim-thread
5825             (apply 'append
5826                    (mapcar 'gnus-cut-thread
5827                            (mapcar 'gnus-make-sub-thread roots))))))
5828   
5829 (defun gnus-cut-thread (thread)
5830   ;; Remove leaf dormant or ancient articles from THREAD.
5831   (let ((head (car thread))
5832         (tail (apply 'append (mapcar 'gnus-cut-thread (cdr thread)))))
5833     (if (and (null tail)
5834              (let ((number (header-number head)))
5835                (or (memq number gnus-newsgroup-ancient)
5836                    (memq number gnus-newsgroup-dormant)
5837                    (and gnus-summary-expunge-below
5838                         (eq gnus-fetch-old-headers 'some)
5839                         (< (or (cdr (assq number gnus-newsgroup-scored))
5840                                gnus-summary-default-score 0)
5841                            gnus-summary-expunge-below)
5842                         (progn
5843                           (setq gnus-newsgroup-unreads
5844                                 (delq number gnus-newsgroup-unreads))
5845                           t)))))
5846         nil
5847       (list (cons head tail)))))
5848
5849 (defun gnus-trim-thread (thread)
5850   ;; Remove root ancient articles with only one child from THREAD.
5851   (if (and (eq gnus-fetch-old-headers 'some)
5852            (memq (header-number (car thread)) gnus-newsgroup-ancient)
5853            (= (length thread) 2))
5854       (gnus-trim-thread (nth 1 thread))
5855     thread))
5856
5857 (defun gnus-make-sub-thread (root)
5858   ;; This function makes a sub-tree for a node in the tree.
5859   (let ((children (reverse (cdr (gnus-gethash (downcase (header-id root))
5860                                               gnus-newsgroup-dependencies)))))
5861     (cons root (mapcar 'gnus-make-sub-thread children))))
5862
5863 (defun gnus-build-old-threads ()
5864   ;; Look at all the articles that refer back to old articles, and
5865   ;; fetch the headers for the articles that aren't there. This will
5866   ;; build complete threads - if the roots haven't been expired by the
5867   ;; server, that is.
5868   (let (id heads)
5869     (mapatoms
5870      (lambda (refs)
5871        (if (not (car (symbol-value refs)))
5872            (progn
5873              (setq heads (cdr (symbol-value refs)))
5874              (while heads
5875                (if (not (memq (header-number (car heads))
5876                               gnus-newsgroup-dormant))
5877                    (progn
5878                      (setq id (symbol-name refs))
5879                      (while (and (setq id (gnus-build-get-header id))
5880                                  (not (car (gnus-gethash 
5881                                             id gnus-newsgroup-dependencies)))))
5882                      (setq heads nil))
5883                  (setq heads (cdr heads)))))))
5884      gnus-newsgroup-dependencies)))
5885
5886 (defun gnus-build-get-header (id)
5887   ;; Look through the buffer of NOV lines and find the header to
5888   ;; ID. Enter this line into the dependencies hash table, and return
5889   ;; the id of the parent article (if any).
5890   (let ((deps gnus-newsgroup-dependencies)
5891         found header)
5892     (prog1
5893         (save-excursion
5894           (set-buffer nntp-server-buffer)
5895           (goto-char (point-min))
5896           (while (and (not found) (search-forward id nil t))
5897             (beginning-of-line)
5898             (setq found (looking-at 
5899                          (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
5900                                  (regexp-quote id))))
5901             (or found (beginning-of-line 2)))
5902           (if found
5903               (let (ref)
5904                 (beginning-of-line)
5905                 (and
5906                  (setq header (gnus-nov-parse-line 
5907                                (read (current-buffer)) deps))
5908                  (setq ref (header-references header))
5909                  (string-match "\\(<[^>]+>\\) *$" ref)
5910                  (substring ref (match-beginning 1) (match-end 1))))))
5911       (and header
5912            (setq gnus-newsgroup-headers (cons header gnus-newsgroup-headers)
5913                  gnus-newsgroup-ancient (cons (header-number header)
5914                                               gnus-newsgroup-ancient))))))
5915
5916 ;; Re-build the thread containing ID.
5917 (defun gnus-rebuild-thread (id)
5918   (let ((dep gnus-newsgroup-dependencies)
5919         (buffer-read-only nil)
5920         parent headers refs thread art)
5921     (while (and id (setq headers
5922                          (car (setq art (gnus-gethash (downcase id) dep)))))
5923       (setq parent art)
5924       (setq id (and (setq refs (header-references headers))
5925                     (string-match "\\(<[^>]+>\\) *$" refs)
5926                     (substring refs (match-beginning 1) (match-end 1)))))
5927     (setq thread (gnus-make-sub-thread (car parent)))
5928     (gnus-rebuild-remove-articles thread)
5929     (let ((beg (point)))
5930       (gnus-summary-prepare-threads (list thread) 0)
5931       (gnus-summary-update-lines beg (point)))))
5932
5933 ;; Delete all lines in the summary buffer that correspond to articles
5934 ;; in this thread.
5935 (defun gnus-rebuild-remove-articles (thread)
5936   (and (gnus-summary-goto-subject (header-number (car thread)))
5937        (gnus-delete-line))
5938   (mapcar (lambda (th) (gnus-rebuild-remove-articles th)) (cdr thread)))
5939
5940 (defun gnus-sort-threads (threads)
5941   ;; Sort threads as specified in `gnus-thread-sort-functions'.
5942   (let ((fun gnus-thread-sort-functions))
5943     (while fun
5944       (setq threads (sort threads (car fun))
5945             fun (cdr fun))))
5946   threads)
5947
5948 (defun gnus-thread-header (thread)
5949   ;; Return header of first article in THREAD.
5950   (if (consp thread)
5951       (if (stringp (car thread))
5952           (car (car (cdr thread)))
5953         (car thread))
5954     thread))
5955
5956 (defun gnus-thread-sort-by-number (h1 h2)
5957   "Sort threads by root article number."
5958   (let ((h1 (gnus-thread-header h1))
5959         (h2 (gnus-thread-header h2)))
5960     (< (header-number h1) (header-number h2))))
5961
5962 (defun gnus-thread-sort-by-author (h1 h2)
5963   "Sort threads by root author."
5964   (let ((h1 (gnus-thread-header h1))
5965         (h2 (gnus-thread-header h2)))
5966     (string-lessp
5967      (let ((extract (funcall 
5968                      gnus-extract-address-components (header-from h1))))
5969        (or (car extract) (cdr extract)))
5970      (let ((extract (funcall
5971                      gnus-extract-address-components (header-from h2))))
5972        (or (car extract) (cdr extract))))))
5973
5974 (defun gnus-thread-sort-by-subject (h1 h2)
5975   "Sort threads by root subject."
5976   (let ((h1 (gnus-thread-header h1))
5977         (h2 (gnus-thread-header h2)))
5978     (string-lessp
5979      (downcase (gnus-simplify-subject (header-subject h1)))
5980      (downcase (gnus-simplify-subject (header-subject h2))))))
5981
5982 (defun gnus-thread-sort-by-date (h1 h2)
5983   "Sort threads by root article date."
5984   (let ((h1 (gnus-thread-header h1))
5985         (h2 (gnus-thread-header h2)))
5986     (string-lessp
5987      (gnus-sortable-date (header-date h1))
5988      (gnus-sortable-date (header-date h2)))))
5989
5990 (defun gnus-thread-sort-by-score (h1 h2)
5991   "Sort threads by root article score.
5992 Unscored articles will be counted as having a score of zero."
5993   (let ((h1 (gnus-thread-header h1))
5994         (h2 (gnus-thread-header h2)))
5995     (let ((s1 (assq (header-number h1) gnus-newsgroup-scored))
5996           (s2 (assq (header-number h2) gnus-newsgroup-scored)))
5997       (> (or (cdr s1) gnus-summary-default-score 0)
5998          (or (cdr s2) gnus-summary-default-score 0)))))
5999
6000 (defun gnus-thread-sort-by-total-score (h1 h2)
6001   "Sort threads by the sum of all scores in the thread.
6002 Unscored articles will be counted as having a score of zero."
6003   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
6004
6005 (defun gnus-thread-total-score (thread)
6006   ;;  This function find the total score of THREAD.
6007   (if (consp thread)
6008       (if (stringp (car thread))
6009           (apply gnus-thread-score-function 0
6010                  (mapcar 'gnus-thread-total-score-1 (cdr thread)))
6011         (gnus-thread-total-score-1 thread))
6012     (gnus-thread-total-score-1 (list thread))))
6013
6014 (defun gnus-thread-total-score-1 (root)
6015   ;; This function find the total score of the thread below ROOT.
6016   (setq root (car root))
6017   (apply gnus-thread-score-function
6018          (or (cdr (assq (header-number root) gnus-newsgroup-scored))
6019              gnus-summary-default-score 0)
6020          (mapcar 'gnus-thread-total-score
6021                  (cdr (gnus-gethash (downcase (header-id root))
6022                                     gnus-newsgroup-dependencies)))))
6023
6024 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
6025 (defvar gnus-tmp-prev-subject "")
6026 (defvar gnus-tmp-adopt-thread nil)
6027
6028 ;; Basic ideas by Paul Dworkin <paul@media-lab.media.mit.edu>.
6029 (defun gnus-summary-prepare-threads 
6030   (threads level &optional not-child no-subject cull)
6031   "Prepare summary buffer from THREADS and indentation LEVEL.  
6032 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'  
6033 or a straight list of headers."
6034   (let (thread header number subject clevel)
6035     (while threads
6036       (setq thread (car threads)
6037             threads (cdr threads))
6038       ;; If `thread' is a cons, hierarchical threads are used.  If not,
6039       ;; `thread' is the header.
6040       (if (consp thread)
6041           (setq header (car thread))
6042         (setq header thread)
6043         (and cull
6044              (or (memq (setq number (header-number header))
6045                        gnus-newsgroup-dormant)
6046                  (and gnus-summary-expunge-below
6047                       (< (or (cdr (assq number gnus-newsgroup-scored))
6048                              gnus-summary-default-score 0)
6049                          gnus-summary-expunge-below)))
6050              (progn
6051                (setq header nil)
6052                (setq gnus-newsgroup-unreads 
6053                      (delq number gnus-newsgroup-unreads)))))
6054       (cond 
6055        ((stringp header)
6056         ;; The header is a dummy root.
6057         (cond ((eq gnus-summary-make-false-root 'adopt)
6058                ;; We let the first article adopt the rest.
6059                (let ((gnus-tmp-adopt-thread (list (cdr thread))))
6060                  (gnus-summary-prepare-threads (list (car (cdr thread))) 0))
6061                (setq thread (cdr (cdr thread)))
6062                (while thread
6063                  (gnus-summary-prepare-threads (list (car thread)) 1 t)
6064                  (setq thread (cdr thread))))
6065               ((eq gnus-summary-make-false-root 'dummy)
6066                ;; We output a dummy root.
6067                (gnus-summary-insert-dummy-line 
6068                 nil header (header-number (car (car (cdr thread)))))
6069                (setq clevel 1))
6070               ((eq gnus-summary-make-false-root 'empty)
6071                ;; We print the articles with empty subject fields. 
6072                (let ((gnus-tmp-adopt-thread (list (cdr thread))))
6073                  (gnus-summary-prepare-threads (list (car (cdr thread))) 0))
6074                (setq thread (cdr (cdr thread)))
6075                (while thread
6076                  (gnus-summary-prepare-threads 
6077                   (list (car thread)) 0 nil
6078                   (not (and (eq gnus-summary-gather-subject-limit 'fuzzy)
6079                             (not (string=  
6080                                   (gnus-simplify-subject-re 
6081                                    (header-subject (car (car thread))))
6082                                   (gnus-simplify-subject-re header))))))
6083                  (setq thread (cdr thread))))
6084               (t
6085                ;; We do not make a root for the gathered
6086                ;; sub-threads at all.  
6087                (setq clevel 0)))
6088         ;; Print the sub-threads.
6089         (and (consp thread) (cdr thread)
6090              (gnus-summary-prepare-threads (cdr thread) clevel)))
6091        ;; The header is a real article.
6092        (header
6093         (setq number (header-number header)
6094               subject (header-subject header))
6095         (and gnus-newsgroup-async
6096              (setq gnus-newsgroup-threads
6097                    (cons (cons (header-number header)
6098                                (header-lines header)) gnus-newsgroup-threads)))
6099         (gnus-summary-insert-line
6100          nil header level nil 
6101          (cond ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
6102                ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
6103                ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
6104                ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
6105                (t gnus-ancient-mark))
6106          (memq number gnus-newsgroup-replied)
6107          (memq number gnus-newsgroup-expirable)
6108          (if no-subject 
6109              gnus-summary-same-subject
6110            (if (or (zerop level)
6111                    (and gnus-thread-ignore-subject
6112                         (not (string= 
6113                               (gnus-simplify-subject-re gnus-tmp-prev-subject)
6114                               (gnus-simplify-subject-re subject)))))
6115                subject
6116              gnus-summary-same-subject))
6117          not-child
6118          (cdr (assq number gnus-newsgroup-scored)))
6119         (setq gnus-tmp-prev-subject subject)
6120         ;; Recursively print subthreads.
6121         (and (consp thread) (cdr thread)
6122              (gnus-summary-prepare-threads (cdr thread) (1+ level))))))))
6123
6124 (defun gnus-select-newsgroup (group &optional read-all)
6125   "Select newsgroup GROUP.
6126 If READ-ALL is non-nil, all articles in the group are selected."
6127   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
6128          (info (nth 2 entry))
6129          articles header-marks)
6130     (gnus-check-news-server
6131      (setq gnus-current-select-method (gnus-find-method-for-group group)))
6132
6133     (or (gnus-server-opened gnus-current-select-method)
6134         (gnus-open-server gnus-current-select-method)
6135         (error "Couldn't open server"))
6136     
6137     (or (and (eq (car entry) t)
6138              (gnus-activate-newsgroup (car info)))
6139         (gnus-request-group group t)
6140         (progn
6141           (kill-buffer (current-buffer))
6142           (error "Couldn't request group %s: %s" 
6143                  group (gnus-status-message group))))
6144
6145     (setq gnus-newsgroup-name group)
6146     (setq gnus-newsgroup-unselected nil)
6147     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
6148
6149     (and gnus-asynchronous
6150          (gnus-check-backend-function 
6151           'request-asynchronous gnus-newsgroup-name)
6152          (setq gnus-newsgroup-async
6153                (gnus-request-asynchronous gnus-newsgroup-name)))
6154
6155     (setq articles (gnus-articles-to-read group read-all))
6156
6157     (cond 
6158      ((null articles) 
6159       (gnus-message 3 "Couldn't select newsgroup")
6160       'quit)
6161      ((eq articles 0) nil)
6162      (t
6163       ;; Init the dependencies hash table.
6164       (setq gnus-newsgroup-dependencies 
6165             (gnus-make-hashtable (length articles)))
6166       ;; Retrieve the headers and read them in.
6167       (setq gnus-newsgroup-headers 
6168             (if (eq 'nov (setq gnus-headers-retrieved-by
6169                                (gnus-retrieve-headers 
6170                                 (if (and gnus-fetch-old-headers 
6171                                          (not (eq 1 (car articles))))
6172                                     (cons 1 articles)
6173                                   articles)
6174                                 gnus-newsgroup-name)))
6175                 (progn
6176                   (gnus-get-newsgroup-headers-xover articles))
6177               (gnus-get-newsgroup-headers)))
6178       ;; If we were to fetch old headers, but the backend didn't
6179       ;; support XOVER, then it is possible we fetched one article
6180       ;; that we shouldn't have. If that's the case, we pop it off the
6181       ;; list of headers.
6182       (and (not (eq gnus-headers-retrieved-by 'nov))
6183            gnus-fetch-old-headers
6184            gnus-newsgroup-headers
6185            (/= (header-number (car gnus-newsgroup-headers)) (car articles))
6186            (let ((val (gnus-gethash 
6187                        (downcase (header-id (car gnus-newsgroup-headers)))
6188                        gnus-newsgroup-dependencies)))
6189              (and val (setcar val nil))
6190              (setq gnus-newsgroup-headers (cdr gnus-newsgroup-headers))))
6191       ;; Remove cancelled articles from the list of unread articles.
6192       (setq gnus-newsgroup-unreads
6193             (gnus-set-sorted-intersection 
6194              gnus-newsgroup-unreads
6195              (mapcar (lambda (headers) (header-number headers))
6196                      gnus-newsgroup-headers)))
6197       ;; Adjust and set lists of article marks.
6198       (and info
6199            (let (marked)
6200              (gnus-adjust-marked-articles info)
6201              (setq gnus-newsgroup-marked 
6202                    (cdr (assq 'tick (setq marked (nth 3 info)))))
6203              (setq gnus-newsgroup-replied (cdr (assq 'reply marked)))
6204              (setq gnus-newsgroup-expirable (cdr (assq 'expire marked)))
6205              (setq gnus-newsgroup-killed (cdr (assq 'killed marked)))
6206              (setq gnus-newsgroup-bookmarks (cdr (assq 'bookmark marked)))
6207              (setq gnus-newsgroup-dormant (cdr (assq 'dormant marked)))
6208              (setq gnus-newsgroup-scored (cdr (assq 'score marked)))
6209              (setq gnus-newsgroup-processable nil)))
6210       ;; Check whether auto-expire is to be done in this group.
6211       (setq gnus-newsgroup-auto-expire
6212             (or (and (stringp gnus-auto-expirable-newsgroups)
6213                      (string-match gnus-auto-expirable-newsgroups group))
6214                 (memq 'auto-expire (nth 5 info))))
6215       ;; First and last article in this newsgroup.
6216       (and gnus-newsgroup-headers
6217            (setq gnus-newsgroup-begin 
6218                  (header-number (car gnus-newsgroup-headers)))
6219            (setq gnus-newsgroup-end
6220                  (header-number (gnus-last-element gnus-newsgroup-headers))))
6221       (setq gnus-reffed-article-number -1)
6222       ;; GROUP is successfully selected.
6223       (or gnus-newsgroup-headers t)))))
6224
6225 (defun gnus-articles-to-read (group read-all)
6226   ;; Find out what articles the user wants to read.
6227   (let* ((articles
6228           ;; Select all articles if `read-all' is non-nil, or if all the
6229           ;; unread articles are dormant articles.
6230           (if (or (and read-all (not (numberp read-all)))
6231                   (= (length gnus-newsgroup-unreads) 
6232                      (length gnus-newsgroup-dormant)))
6233               (gnus-uncompress-range 
6234                (gnus-gethash group gnus-active-hashtb))
6235             gnus-newsgroup-unreads))
6236          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
6237          (scored (length scored-list))
6238          (number (length articles))
6239          (marked (+ (length gnus-newsgroup-marked)
6240                     (length gnus-newsgroup-dormant)))
6241          (select
6242           (cond 
6243            ((numberp read-all)
6244             read-all)
6245            (t
6246             (condition-case ()
6247                 (cond ((and (or (<= scored marked)
6248                                 (= scored number))
6249                             (numberp gnus-large-newsgroup)
6250                             (> number gnus-large-newsgroup))
6251                        (let ((input
6252                               (read-string
6253                                (format
6254                                 "How many articles from %s (default %d): "
6255                                 gnus-newsgroup-name number))))
6256                          (if (string-equal input "")
6257                              number input)))
6258                       ((and (> scored marked) (< scored number))
6259                        (let ((input
6260                               (read-string
6261                                (format 
6262                                 "%s %s (%d scored, %d total): "
6263                                 "How many articles from"
6264                                 group scored number))))
6265                          (if (string-equal input "")
6266                              number input)))
6267                       (t number))
6268               (quit nil)))))
6269          total-articles)
6270     (setq select (if (stringp select) (string-to-number select) select))
6271     (if (or (null select) (zerop select))
6272         select
6273       (if (and (not (zerop scored)) (<= (abs select) scored))
6274           (progn
6275             (setq articles (sort scored-list '<))
6276             (setq number (length articles)))
6277         (setq articles (copy-sequence articles)))
6278
6279       (setq total-articles articles)
6280       
6281       (if (< (abs select) number)
6282           (if (< select 0) 
6283               ;; Select the N oldest articles.
6284               (setcdr (nthcdr (1- (abs select)) articles) nil)
6285             ;; Select the N most recent articles.
6286             (setq articles (nthcdr (- number select) articles))))
6287       (setq gnus-newsgroup-unselected
6288             (gnus-sorted-intersection
6289              gnus-newsgroup-unreads
6290              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
6291       articles)))
6292
6293 (defun gnus-killed-articles (killed articles)
6294   (let (out)
6295     (while articles
6296       (if (inline (gnus-member-of-range (car articles) killed))
6297           (setq out (cons (car articles) out)))
6298       (setq articles (cdr articles)))
6299     out))
6300
6301 (defun gnus-adjust-marked-articles (info &optional active)
6302   "Remove all marked articles that are no longer legal."
6303   (let ((marked-lists (nth 3 info))
6304         (active (or active (gnus-gethash (car info) gnus-active-hashtb)))
6305         marked m prev)
6306     ;; There are many types of marked articles.
6307     (while marked-lists
6308       (setq m (cdr (setq prev (car marked-lists))))
6309       (cond ((or (eq 'tick (car prev)) (eq 'dormant (car prev)))
6310              ;; Make sure that all ticked articles are a subset of the
6311              ;; unread/unselected articles.
6312              (while m
6313                (if (or (memq (car m) gnus-newsgroup-unreads)
6314                        (memq (car m) gnus-newsgroup-unselected))
6315                    (setq prev m)
6316                  (setcdr prev (cdr m)))
6317                (setq m (cdr m))))
6318             ((eq 'score (car prev))
6319              ;; Scored articles should be a subset of
6320              ;; unread/unselected articles. 
6321              (while m
6322                (if (or (memq (car (car m)) gnus-newsgroup-unreads)
6323                        (memq (car (car m)) gnus-newsgroup-unreads))
6324                    (setq prev m)
6325                  (setcdr prev (cdr m)))
6326                (setq m (cdr m))))
6327             ((eq 'bookmark (car prev))
6328              ;; Bookmarks should be a subset of active articles.
6329              (while m
6330                (if (< (car (car m)) (car active))
6331                    (setcdr prev (cdr m))
6332                  (setq prev m))
6333                (setq m (cdr m))))
6334             ((eq 'killed (car prev))
6335              ;; Articles that have been through the kill process are
6336              ;; to be a subset of active articles.
6337              (while (and m (< (or (and (numberp (car m)) (car m))
6338                                   (cdr (car m)))
6339                               (car active)))
6340                (setcdr prev (cdr m))
6341                (setq m (cdr m)))
6342              (if (and m (< (or (and (numberp (car m)) (car m))
6343                                (car (car m)))
6344                            (car active))) 
6345                  (setcar (if (numberp (car m)) m (car m)) (car active))))
6346             ((or (eq 'reply (car prev)) (eq 'expire (car prev)))
6347              ;; The replied and expirable articles have to be articles
6348              ;; that are active. 
6349              (while m
6350                (if (< (car m) (car active))
6351                    (setcdr prev (cdr m))
6352                  (setq prev m))
6353                (setq m (cdr m)))))
6354       (setq marked-lists (cdr marked-lists)))
6355     ;; Remove all lists that are empty.
6356     (setq marked-lists (nth 3 info))
6357     (if marked-lists
6358         (progn
6359           (while (= 1 (length (car marked-lists)))
6360             (setq marked-lists (cdr marked-lists)))
6361           (setq m (cdr (setq prev marked-lists)))
6362           (while m
6363             (if (= 1 (length (car m)))
6364                 (setcdr prev (cdr m))
6365               (setq prev m))
6366             (setq m (cdr m)))
6367           (setcar (nthcdr 3 info) marked-lists)))
6368     ;; Finally, if there are no marked lists at all left, and if there
6369     ;; are no elements after the lists in the info list, we just chop
6370     ;; the info list off before the marked lists.
6371     (and (null marked-lists) 
6372          (not (nthcdr 4 info))
6373          (setcdr (nthcdr 2 info) nil)))
6374   info)
6375
6376 (defun gnus-set-marked-articles 
6377   (info ticked replied expirable killed dormant bookmark score) 
6378   "Enter the various lists of marked articles into the newsgroup info list."
6379   (let (newmarked)
6380     (and ticked (setq newmarked (cons (cons 'tick ticked) nil)))
6381     (and replied (setq newmarked (cons (cons 'reply replied) newmarked)))
6382     (and expirable (setq newmarked (cons (cons 'expire expirable) 
6383                                          newmarked)))
6384     (and killed (setq newmarked (cons (cons 'killed killed) newmarked)))
6385     (and dormant (setq newmarked (cons (cons 'dormant dormant) newmarked)))
6386     (and bookmark (setq newmarked (cons (cons 'bookmark bookmark) 
6387                                         newmarked)))
6388     (and score (setq newmarked (cons (cons 'score score) newmarked)))
6389     (if (nthcdr 3 info)
6390         (progn
6391           (setcar (nthcdr 3 info) newmarked)
6392           (and (not newmarked)
6393                (not (nthcdr 4 info))
6394                (setcdr (nthcdr 2 info) nil)))
6395       (if newmarked
6396           (setcdr (nthcdr 2 info) (list newmarked))))))
6397
6398 (defun gnus-add-marked-articles (group type articles &optional info force)
6399   ;; Add ARTICLES of TYPE to the info of GROUP.
6400   ;; If INFO is non-nil, use that info. If FORCE is non-nil, don't
6401   ;; add, but replace marked articles of TYPE with ARTICLES.
6402   (let ((info (or info (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
6403         marked m)
6404     (or (not info)
6405         (and (not (setq marked (nthcdr 3 info)))
6406              (setcdr (nthcdr 2 info) (list (list (cons type articles)))))
6407         (and (not (setq m (assq type (car marked))))
6408              (setcar marked (cons (cons type articles) (car marked))))
6409         (if force
6410             (setcdr m articles)
6411           (nconc m articles)))))
6412          
6413 (defun gnus-set-mode-line (where)
6414   "This function sets the mode line of the article or summary buffers.
6415 If WHERE is `summary', the summary mode line format will be used."
6416   (if (memq where gnus-updated-mode-lines)
6417       (let (mode-string)
6418         (save-excursion
6419           (set-buffer gnus-summary-buffer)
6420           (let* ((mformat (if (eq where 'article) 
6421                               gnus-article-mode-line-format-spec
6422                             gnus-summary-mode-line-format-spec))
6423                  (group-name gnus-newsgroup-name)
6424                  (article-number (or gnus-current-article 0))
6425                  (unread (- (length gnus-newsgroup-unreads)
6426                             (length gnus-newsgroup-dormant)))
6427                  (unread-and-unticked 
6428                   (- unread (length gnus-newsgroup-marked)))
6429                  (unselected (length gnus-newsgroup-unselected))
6430                  (unread-and-unselected
6431                   (cond ((and (zerop unread-and-unticked)
6432                               (zerop unselected)) "")
6433                         ((zerop unselected) 
6434                          (format "{%d more}" unread-and-unticked))
6435                         (t (format "{%d(+%d) more}"
6436                                    unread-and-unticked unselected))))
6437                  (subject
6438                   (if gnus-current-headers
6439                       (header-subject gnus-current-headers) ""))
6440                  (max-len (and gnus-mode-non-string-length
6441                                (- (frame-width) gnus-mode-non-string-length)))
6442                  header) ;; passed as argument to any user-format-funcs
6443             (setq mode-string (eval mformat))
6444             (or (numberp max-len)
6445                 (setq max-len (length mode-string)))
6446             (if (< max-len 4) (setq max-len 4))
6447             (if (> (length mode-string) max-len)
6448                 (setq mode-string 
6449                       (concat (substring mode-string 0 (- max-len 3))
6450                               "...")))
6451             (setq mode-string (format (format "%%-%ds" max-len)
6452                                       mode-string))))
6453         (setq mode-line-buffer-identification mode-string)
6454         (set-buffer-modified-p t))))
6455
6456 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
6457   "Go through the HEADERS list and add all Xrefs to a hash table.
6458 The resulting hash table is returned, or nil if no Xrefs were found."
6459   (let* ((from-method (gnus-find-method-for-group from-newsgroup))
6460          (prefix (if (and 
6461                       (gnus-group-foreign-p from-newsgroup)
6462                       (not (memq 'virtual 
6463                                  (assoc (symbol-name (car from-method))
6464                                         gnus-valid-select-methods))))
6465                      (gnus-group-real-prefix from-newsgroup)))
6466          (xref-hashtb (make-vector 63 0))
6467          start group entry number xrefs header)
6468     (while headers
6469       (setq header (car headers))
6470       (if (and (setq xrefs (header-xref header))
6471                (not (memq (header-number header) unreads)))
6472           (progn
6473             (setq start 0)
6474             (while (string-match "\\([^ ]+\\):\\([0-9]+\\)" xrefs start)
6475               (setq start (match-end 0))
6476               (setq group (concat prefix (substring xrefs (match-beginning 1) 
6477                                                     (match-end 1))))
6478               (setq number 
6479                     (string-to-int (substring xrefs (match-beginning 2) 
6480                                               (match-end 2))))
6481               (if (setq entry (gnus-gethash group xref-hashtb))
6482                   (setcdr entry (cons number (cdr entry)))
6483                 (gnus-sethash group (cons number nil) xref-hashtb)))))
6484       (setq headers (cdr headers)))
6485     (if start xref-hashtb nil)))
6486
6487 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads expirable)
6488   "Look through all the headers and mark the Xrefs as read."
6489   (let ((virtual (memq 'virtual 
6490                        (assoc (symbol-name (car (gnus-find-method-for-group 
6491                                                  from-newsgroup)))
6492                               gnus-valid-select-methods)))
6493         name entry read info xref-hashtb idlist active num range exps method
6494         nth4)
6495     (save-excursion
6496       (set-buffer gnus-group-buffer)
6497       (if (setq xref-hashtb 
6498                 (gnus-create-xref-hashtb from-newsgroup headers unreads))
6499           (mapatoms 
6500            (lambda (group)
6501              (if (string= from-newsgroup (setq name (symbol-name group)))
6502                  ()
6503                (setq idlist (symbol-value group))
6504                ;; Dead groups are not updated.
6505                (if (and (prog1 
6506                             (setq entry (gnus-gethash name gnus-newsrc-hashtb)
6507                                   info (nth 2 entry))
6508                           (if (stringp (setq nth4 (nth 4 info)))
6509                               (setq nth4 (gnus-server-to-method nth4))))
6510                         ;; Only do the xrefs if the group has the same
6511                         ;; select method as the group we have just read.
6512                         (or (gnus-methods-equal-p 
6513                              nth4 (gnus-find-method-for-group from-newsgroup))
6514                             virtual
6515                             (equal nth4 
6516                                    (setq method (gnus-find-method-for-group 
6517                                                  from-newsgroup)))
6518                             (and (equal (car nth4) (car method))
6519                                  (equal (nth 1 nth4) (nth 1 method))))
6520                         gnus-use-cross-reference
6521                         (or (not (eq gnus-use-cross-reference t))
6522                             virtual
6523                             ;; Only do cross-references on subscribed
6524                             ;; groups, if that is what is wanted.  
6525                             (<= (nth 1 info) gnus-level-subscribed)))
6526                    (progn
6527                      (setq num 0)
6528                      ;; Set the new list of read articles in this group.
6529                      (setq active (gnus-gethash name gnus-active-hashtb))
6530                      ;; First peel off all illegal article numbers.
6531                      (if active
6532                          (let ((ids idlist)
6533                                (ticked (cdr (assq 'tick (nth 3 info))))
6534                                (dormant (cdr (assq 'dormant (nth 3 info))))
6535                                id)
6536                            (setq exps nil)
6537                            (while ids
6538                              (setq id (car ids))
6539                              (if (or (> id (cdr active))
6540                                      (< id (car active))
6541                                      (memq id ticked)
6542                                      (memq id dormant))
6543                                  (setq idlist (delq id idlist)))
6544                              (and (memq id expirable)
6545                                   (setq exps (cons id exps)))
6546                              (setq ids (cdr ids)))))
6547                      ;; Update expirable articles.
6548                      (gnus-add-marked-articles nil 'expirable exps info)
6549                      (and active
6550                           (null (nth 2 info))
6551                           (> (car active) 1)
6552                           (setcar (nthcdr 2 info) (cons 1 (1- (car active)))))
6553                      (setcar (nthcdr 2 info)
6554                              (setq range
6555                                    (gnus-add-to-range 
6556                                     (nth 2 info) 
6557                                     (setq idlist (sort idlist '<)))))
6558                      ;; Then we have to re-compute how many unread
6559                      ;; articles there are in this group.
6560                      (if active
6561                          (progn
6562                            (cond 
6563                             ((not range)
6564                              (setq num (- (1+ (cdr active)) (car active))))
6565                             ((not (listp (cdr range)))
6566                              (setq num (- (cdr active) (- (1+ (cdr range)) 
6567                                                           (car range)))))
6568                             (t
6569                              (while range
6570                                (if (numberp (car range))
6571                                    (setq num (1+ num))
6572                                  (setq num (+ num (- (1+ (cdr (car range)))
6573                                                      (car (car range))))))
6574                                (setq range (cdr range)))
6575                              (setq num (- (cdr active) num))))
6576                            ;; Update the number of unread articles.
6577                            (setcar 
6578                             entry 
6579                             (max 0 (- num 
6580                                       (length (cdr (assq 'tick (nth 3 info))))
6581                                       (length 
6582                                        (cdr (assq 'dormant (nth 3 info)))))))
6583                            ;; Update the group buffer.
6584                            (gnus-group-update-group name t)))))))
6585            xref-hashtb)))))
6586
6587 (defun gnus-methods-equal-p (m1 m2)
6588   (let ((m1 (or m1 gnus-select-method))
6589         (m2 (or m2 gnus-select-method)))
6590     (or (equal m1 m2)
6591         (and (eq (car m1) (car m2))
6592              (or (not (memq 'address (assoc (symbol-name (car m1))
6593                                             gnus-valid-select-methods)))
6594                  (equal (nth 1 m1) (nth 1 m2)))))))
6595
6596 (defsubst gnus-header-value ()
6597   (buffer-substring (match-end 0) (gnus-point-at-eol)))
6598
6599 (defvar gnus-newsgroup-none-id 0)
6600
6601 (defun gnus-get-newsgroup-headers ()
6602   (setq gnus-article-internal-prepare-hook nil)
6603   (let ((cur nntp-server-buffer)
6604         (dependencies gnus-newsgroup-dependencies)
6605         headers char article id dep end)
6606     (save-excursion
6607       (set-buffer nntp-server-buffer)
6608       (goto-char (point-min))
6609       ;; Search to the beginning of the next header. Error messages
6610       ;; do not begin with 2 or 3.
6611       (while (re-search-forward "^[23][0-9]+ " nil t)
6612         (let ((header (make-vector 9 nil))
6613               (c (following-char))
6614               (case-fold-search t)
6615               (p (point))
6616               from subject in-reply-to references ref)
6617           (setq id nil
6618                 ref nil
6619                 references nil
6620                 subject nil
6621                 from nil)
6622           (header-set-number header (setq article (read cur)))
6623           ;; This implementation of this function, with nine
6624           ;; search-forwards instead of the one re-search-forward and
6625           ;; a case (which basically was the old function) is actually
6626           ;; about twice as fast, even though it looks messier. You
6627           ;; can't have everything, I guess. Speed and elegance
6628           ;; doesn't always come hand in hand.
6629           (save-restriction
6630             (narrow-to-region (point) (or (save-excursion 
6631                                             (search-forward "\n.\n" nil t))
6632                                           (point)))
6633             (if (search-forward "\nfrom: " nil t)
6634                 (header-set-from header (gnus-header-value))
6635               (header-set-from header "(nobody)"))
6636             (goto-char p)
6637             (if (search-forward "\nsubject: " nil t)
6638                 (header-set-subject header (gnus-header-value))
6639               (header-set-subject header "(none)"))
6640             (goto-char p)
6641             (and (search-forward "\nxref: " nil t)
6642                  (header-set-xref header (gnus-header-value)))
6643             (goto-char p)
6644             (or (numberp (and (search-forward "\nlines: " nil t)
6645                               (header-set-lines header (read cur))))
6646                 (header-set-lines header 0))
6647             (goto-char p)
6648             (and (search-forward "\ndate: " nil t)
6649                  (header-set-date header (gnus-header-value)))
6650             (goto-char p)
6651             (if (search-forward "\nmessage-id: " nil t)
6652                 (header-set-id header (setq id (gnus-header-value)))
6653               ;; If there was no message-id, we just fake one to make
6654               ;; subsequent routines simpler.
6655               (header-set-id 
6656                header 
6657                (setq id (concat "none+" 
6658                                 (int-to-string 
6659                                  (setq gnus-newsgroup-none-id 
6660                                        (1+ gnus-newsgroup-none-id)))))))
6661             (goto-char p)
6662             (if (search-forward "\nreferences: " nil t)
6663                 (progn
6664                   (header-set-references header (gnus-header-value))
6665                   (setq end (match-end 0))
6666                   (save-excursion
6667                     (setq ref 
6668                           (downcase
6669                            (buffer-substring
6670                             (progn 
6671                               (end-of-line)
6672                               (search-backward ">" end t)
6673                               (1+ (point)))
6674                             (progn
6675                               (search-backward "<" end t)
6676                               (point)))))))
6677               ;; Get the references from the in-reply-to header if there
6678               ;; ware no references and the in-reply-to header looks
6679               ;; promising. 
6680               (if (and (search-forward "\nin-reply-to: " nil t)
6681                        (setq in-reply-to (gnus-header-value))
6682                        (string-match "<[^>]+>" in-reply-to))
6683                   (progn
6684                     (header-set-references 
6685                      header 
6686                      (setq ref (substring in-reply-to (match-beginning 0)
6687                                           (match-end 0))))
6688                     (setq ref (downcase ref)))
6689                 (setq ref "none")))
6690             ;; We do some threading while we read the headers. The
6691             ;; message-id and the last reference are both entered into
6692             ;; the same hash table. Some tippy-toeing around has to be
6693             ;; done in case an article has arrived before the article
6694             ;; which it refers to.
6695             (if (boundp (setq dep (intern (downcase id) dependencies)))
6696                 (if (car (symbol-value dep))
6697                     ;; An article with this Message-ID has already
6698                     ;; been seen, so we ignore this one, except we add
6699                     ;; any additional Xrefs (in case the two articles
6700                     ;; came from different servers.
6701                     (progn
6702                       (header-set-xref 
6703                        (car (symbol-value dep))
6704                        (concat (or (header-xref (car (symbol-value dep))) "")
6705                                (or (header-xref header) "")))
6706                       (setq header nil))
6707                   (setcar (symbol-value dep) header))
6708               (set dep (list header)))
6709             (if header
6710                 (progn
6711                   (if (boundp (setq dep (intern ref dependencies)))
6712                       (setcdr (symbol-value dep) 
6713                               (cons header (cdr (symbol-value dep))))
6714                     (set dep (list nil header)))
6715                   (setq headers (cons header headers))))
6716             (goto-char (point-max))))))
6717     (nreverse headers)))
6718
6719 ;; The following macros and functions were written by Felix Lee
6720 ;; <flee@cse.psu.edu>. 
6721
6722 (defmacro gnus-nov-read-integer ()
6723   '(prog1
6724        (if (= (following-char) ?\t)
6725            0
6726          (let ((num (condition-case nil (read buffer) (error nil))))
6727            (if (numberp num) num 0)))
6728      (or (eobp) (forward-char 1))))
6729
6730 (defmacro gnus-nov-skip-field ()
6731   '(search-forward "\t" eol 'move))
6732
6733 (defmacro gnus-nov-field ()
6734   '(buffer-substring (point) (if (gnus-nov-skip-field) (1- (point)) eol)))
6735
6736 ;; Goes through the xover lines and returns a list of vectors
6737 (defun gnus-get-newsgroup-headers-xover (sequence)
6738   "Parse the news overview data in the server buffer, and return a
6739 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
6740   ;; Get the Xref when the users reads the articles since most/some
6741   ;; NNTP servers do not include Xrefs when using XOVER.
6742   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
6743   (let ((cur nntp-server-buffer)
6744         (dependencies gnus-newsgroup-dependencies)
6745         (none 0)
6746         number headers header)
6747     (save-excursion
6748       (set-buffer nntp-server-buffer)
6749       (goto-char (point-min))
6750       (while (and sequence (not (eobp)))
6751         (setq number (read cur))
6752         (while (and sequence (< (car sequence) number))
6753           (setq sequence (cdr sequence)))
6754         (and sequence 
6755              (eq number (car sequence))
6756              (progn
6757                (setq sequence (cdr sequence))
6758                (if (setq header 
6759                          (inline (gnus-nov-parse-line number dependencies)))
6760                    (setq headers (cons header headers)))))
6761         (forward-line 1))
6762       (setq headers (nreverse headers)))
6763     headers))
6764
6765 ;; This function has to be called with point after the article number
6766 ;; on the beginning of the line.
6767 (defun gnus-nov-parse-line (number dependencies)
6768   (let ((none 0)
6769         (eol (gnus-point-at-eol)) 
6770         (buffer (current-buffer))
6771         header ref id dep)
6772
6773     ;; overview: [num subject from date id refs chars lines misc]
6774     (narrow-to-region (point) eol)
6775     (forward-char)
6776
6777     (condition-case nil
6778         (setq header
6779               (vector 
6780                number                   ; number
6781                (gnus-nov-field)         ; subject
6782                (gnus-nov-field)         ; from
6783                (gnus-nov-field)         ; date
6784                (setq id (or (gnus-nov-field)
6785                             (concat "none+"
6786                                     (int-to-string 
6787                                      (setq none (1+ none)))))) ; id
6788                (progn
6789                  (save-excursion
6790                    (let ((beg (point)))
6791                      (search-forward "\t" eol)
6792                      (if (search-backward ">" beg t)
6793                          (setq ref 
6794                                (downcase 
6795                                 (buffer-substring 
6796                                  (1+ (point))
6797                                  (progn
6798                                    (search-backward "<" beg t)
6799                                    (point)))))
6800                        (setq ref nil))))
6801                  (gnus-nov-field))      ; refs
6802                (gnus-nov-read-integer)  ; chars
6803                (gnus-nov-read-integer)  ; lines
6804                (if (= (following-char) ?\n)
6805                    nil
6806                  (gnus-nov-field))      ; misc
6807                ))
6808       (error (progn 
6809                (ding)
6810                (message "Strange nov line.")
6811                (setq header nil)
6812                (goto-char eol))))
6813
6814     (widen)
6815
6816     ;; We build the thread tree.
6817     (and header
6818          (if (boundp (setq dep (intern (downcase id) dependencies)))
6819              (if (car (symbol-value dep))
6820                  ;; An article with this Message-ID has already been seen,
6821                  ;; so we ignore this one, except we add any additional
6822                  ;; Xrefs (in case the two articles came from different
6823                  ;; servers.
6824                  (progn
6825                    (header-set-xref 
6826                     (car (symbol-value dep))
6827                     (concat (or (header-xref (car (symbol-value dep))) "")
6828                             (or (header-xref header) "")))
6829                    (setq header nil))
6830                (setcar (symbol-value dep) header))
6831            (set dep (list header))))
6832     (if header
6833         (progn
6834           (if (boundp (setq dep (intern (or ref "none") 
6835                                         dependencies)))
6836               (setcdr (symbol-value dep) 
6837                       (cons header (cdr (symbol-value dep))))
6838             (set dep (list nil header)))))
6839     header))
6840
6841 (defun gnus-article-get-xrefs ()
6842   "Fill in the Xref value in `gnus-current-headers', if necessary.
6843 This is meant to be called in `gnus-article-internal-prepare-hook'."
6844   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
6845                                  gnus-current-headers)))
6846     (or (not gnus-use-cross-reference)
6847         (not headers)
6848         (and (header-xref headers)
6849              (not (string= (header-xref headers) "")))
6850         (let ((case-fold-search t)
6851               xref)
6852           (save-restriction
6853             (gnus-narrow-to-headers)
6854             (goto-char (point-min))
6855             (if (or (and (eq (downcase (following-char)) ?x)
6856                          (looking-at "Xref:"))
6857                     (search-forward "\nXref:" nil t))
6858                 (progn
6859                   (goto-char (1+ (match-end 0)))
6860                   (setq xref (buffer-substring (point) 
6861                                                (progn (end-of-line) (point))))
6862                   (header-set-xref headers xref))))))))
6863
6864 (defalias 'gnus-find-header-by-number 'gnus-get-header-by-number)
6865 (make-obsolete 'gnus-find-header-by-number 'gnus-get-header-by-number)
6866
6867 ;; Return a header specified by a NUMBER.
6868 (defun gnus-get-header-by-number (number)
6869   (save-excursion
6870     (set-buffer gnus-summary-buffer)
6871     (or gnus-newsgroup-headers-hashtb-by-number
6872         (gnus-make-headers-hashtable-by-number))
6873     (gnus-gethash (int-to-string number)
6874                   gnus-newsgroup-headers-hashtb-by-number)))
6875
6876 (defun gnus-make-headers-hashtable-by-number ()
6877   "Make hashtable for the variable gnus-newsgroup-headers by number."
6878   (save-excursion
6879     (set-buffer gnus-summary-buffer)
6880     (let ((headers gnus-newsgroup-headers)
6881           header)
6882       (setq gnus-newsgroup-headers-hashtb-by-number
6883             (gnus-make-hashtable (length headers)))
6884       (while headers
6885         (setq header (car headers))
6886         (gnus-sethash (int-to-string (header-number header))
6887                       header gnus-newsgroup-headers-hashtb-by-number)
6888         (setq headers (cdr headers))))))
6889
6890 (defun gnus-more-header-backward ()
6891   "Find new header backward."
6892   (let ((first (car (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
6893         (artnum gnus-newsgroup-begin)
6894         (header nil))
6895     (while (and (not header)
6896                 (> artnum first))
6897       (setq artnum (1- artnum))
6898       (setq header (gnus-read-header artnum)))
6899     header))
6900
6901 (defun gnus-more-header-forward (&optional backward)
6902   "Find new header forward.
6903 If BACKWARD, find new header backward instead."
6904   (if backward
6905       (gnus-more-header-backward)
6906     (let ((last (cdr (gnus-gethash gnus-newsgroup-name gnus-active-hashtb)))
6907           (artnum gnus-newsgroup-end)
6908           (header nil))
6909       (while (and (not header)
6910                   (< artnum last))
6911         (setq artnum (1+ artnum))
6912         (setq header (gnus-read-header artnum)))
6913       header)))
6914
6915 (defun gnus-extend-newsgroup (header &optional backward)
6916   "Extend newsgroup selection with HEADER.
6917 Optional argument BACKWARD means extend toward backward."
6918   (if header
6919       (let ((artnum (header-number header)))
6920         (setq gnus-newsgroup-headers
6921               (if backward
6922                   (cons header gnus-newsgroup-headers)
6923                 (nconc gnus-newsgroup-headers (list header))))
6924         (setq gnus-newsgroup-unselected
6925               (delq artnum gnus-newsgroup-unselected))
6926         (setq gnus-newsgroup-begin (min gnus-newsgroup-begin artnum))
6927         (setq gnus-newsgroup-end (max gnus-newsgroup-end artnum)))))
6928
6929 (defun gnus-summary-work-articles (n)
6930   "Return a list of articles to be worked upon. The prefix argument,
6931 the list of process marked articles, and the current article will be
6932 taken into consideration."
6933   (let (articles)
6934     (if (and n (numberp n))
6935         (let ((backward (< n 0))
6936               (n (abs n)))
6937           (save-excursion
6938             (while (and (> n 0)
6939                         (setq articles (cons (gnus-summary-article-number) 
6940                                              articles))
6941                         (gnus-summary-search-forward nil nil backward))
6942               (setq n (1- n))))
6943           (sort articles (function <)))
6944       (or (reverse gnus-newsgroup-processable)
6945           (list (gnus-summary-article-number))))))
6946
6947 (defun gnus-summary-search-group (&optional backward use-level)
6948   "Search for next unread newsgroup.
6949 If optional argument BACKWARD is non-nil, search backward instead."
6950   (save-excursion
6951     (set-buffer gnus-group-buffer)
6952     (if (gnus-group-search-forward 
6953          backward nil (if use-level (gnus-group-group-level) nil))
6954         (gnus-group-group-name))))
6955
6956 (defun gnus-summary-best-group (&optional exclude-group)
6957   "Find the name of the best unread group.
6958 If EXCLUDE-GROUP, do not go to this group."
6959   (save-excursion
6960     (set-buffer gnus-group-buffer)
6961     (save-excursion
6962       (gnus-group-best-unread-group exclude-group))))
6963
6964 (defun gnus-summary-search-subject (&optional backward unread subject)
6965   "Search for article forward.
6966 If BACKWARD is non-nil, search backward.
6967 If UNREAD is non-nil, only unread articles are selected.
6968 If SUBJECT is non-nil, the article which has the same subject will be
6969 searched for." 
6970   (let ((func (if backward 'previous-single-property-change
6971                 'next-single-property-change))
6972         (beg (point))
6973         (did t)
6974         pos psubject)
6975     (beginning-of-line)
6976     (and gnus-summary-check-current unread
6977          (eq (get-text-property (point) 'gnus-mark) gnus-unread-mark)
6978          (setq did nil))
6979     (if (not did)
6980         ()
6981       (forward-char (if backward (if (bobp) 0 -1) (if (eobp) 0 1)))
6982       (while
6983           (and 
6984            (setq pos (funcall func (point) 'gnus-number))
6985            (goto-char (if backward (1- pos) pos))
6986            (setq did
6987                  (not (and
6988                        (or (not unread)
6989                            (eq (get-text-property (point) 'gnus-mark)
6990                                gnus-unread-mark))
6991                        (or (not subject)
6992                            (and (setq psubject (gnus-summary-subject-string))
6993                                 (equal (gnus-simplify-subject-re subject)
6994                                        (gnus-simplify-subject-re
6995                                         psubject)))))))
6996            (if backward (if (bobp) nil (forward-char -1) t)
6997              (if (eobp) nil (forward-char 1) t)))))
6998     (if did
6999         (progn (goto-char beg) nil)
7000       (prog1
7001           (get-text-property (point) 'gnus-number)
7002         (gnus-summary-position-cursor)))))
7003
7004 (defun gnus-summary-search-forward (&optional unread subject backward)
7005   "Search for article forward.
7006 If UNREAD is non-nil, only unread articles are selected.
7007 If SUBJECT is non-nil, the article which has the same subject will be
7008 searched for. 
7009 If BACKWARD is non-nil, the search will be performed backwards instead."
7010   (gnus-summary-search-subject backward unread subject))
7011
7012 (defun gnus-summary-search-backward (&optional unread subject)
7013   "Search for article backward.
7014 If 1st optional argument UNREAD is non-nil, only unread article is selected.
7015 If 2nd optional argument SUBJECT is non-nil, the article which has
7016 the same subject will be searched for."
7017   (gnus-summary-search-forward unread subject t))
7018
7019 (defun gnus-summary-article-number (&optional number-or-nil)
7020   "The article number of the article on the current line.
7021 If there isn's an article number here, then we return the current
7022 article number."
7023   (let* ((number (get-text-property (gnus-point-at-bol) 'gnus-number)))
7024     (if number-or-nil number (or number gnus-current-article))))
7025
7026 (defun gnus-summary-thread-level ()
7027   "The thread level of the article on the current line."
7028   (or (get-text-property (gnus-point-at-bol) 'gnus-level)
7029       0))
7030
7031 (defun gnus-summary-pseudo-article ()
7032   "The thread level of the article on the current line."
7033   (get-text-property (gnus-point-at-bol) 'gnus-pseudo))
7034
7035 (defun gnus-summary-article-mark ()
7036   "The mark on the current line."
7037   (get-text-property (gnus-point-at-bol) 'gnus-mark))
7038
7039 (defun gnus-summary-subject-string ()
7040   "Return current subject string or nil if nothing."
7041   (let ((article (gnus-summary-article-number))
7042         header)
7043     (and article 
7044          (setq header (gnus-get-header-by-number article))
7045          (vectorp header)
7046          (header-subject header))))
7047
7048 (defalias 'gnus-summary-score 'gnus-summary-article-score)
7049 (make-obsolete 'gnus-summary-score 'gnus-summary-article-score)
7050 (defun gnus-summary-article-score ()
7051   "Return current article score."
7052   (or (cdr (assq (gnus-summary-article-number) gnus-newsgroup-scored))
7053       gnus-summary-default-score 0))
7054
7055 ;; Written by Sudish Joseph <joseph@cis.ohio-state.edu>.
7056
7057 (defun gnus-summary-recenter ()
7058   "Center point in the summary window.
7059 If `gnus-auto-center-summary' is nil, or the article buffer isn't
7060 displayed, no centering will be performed." 
7061   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
7062   ;; Recenter only when requested. Suggested by popovich@park.cs.columbia.edu.
7063   (let* ((top (cond ((< (window-height) 4) 0)
7064                     ((< (window-height) 6) 1)
7065                     (t 2)))
7066          (height (1- (window-height)))
7067          (bottom (save-excursion (goto-char (point-max))
7068                                  (forward-line (- height))
7069                                  (point)))
7070          (window (get-buffer-window (current-buffer))))
7071     (and 
7072      ;; The user has to want it,
7073      gnus-auto-center-summary 
7074      ;; the article buffer must be displayed,
7075      (get-buffer-window gnus-article-buffer)
7076      ;; Set the window start to either `bottom', which is the biggest
7077      ;; possible valid number, or the second line from the top,
7078      ;; whichever is the least.
7079      (set-window-start
7080       window (min bottom (save-excursion (forward-line (- top)) (point)))))))
7081
7082 ;; Function written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
7083 (defun gnus-short-group-name (group &optional levels)
7084   "Collapse GROUP name LEVELS."
7085   (let* ((name "") (foreign "") (depth -1) (skip 1)
7086          (levels (or levels
7087                      (progn
7088                        (while (string-match "\\." group skip)
7089                          (setq skip (match-end 0)
7090                                depth (+ depth 1)))
7091                        depth))))
7092     (if (string-match ":" group)
7093         (setq foreign (substring group 0 (match-end 0))
7094               group (substring group (match-end 0))))
7095     (while group
7096       (if (and (string-match "\\." group) (> levels 0))
7097           (setq name (concat name (substring group 0 1))
7098                 group (substring group (match-end 0))
7099                 levels (- levels 1)
7100                 name (concat name "."))
7101         (setq name (concat foreign name group)
7102               group nil)))
7103     name))
7104
7105 (defun gnus-summary-jump-to-group (newsgroup)
7106   "Move point to NEWSGROUP in group mode buffer."
7107   ;; Keep update point of group mode buffer if visible.
7108   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
7109       (save-window-excursion
7110         ;; Take care of tree window mode.
7111         (if (get-buffer-window gnus-group-buffer)
7112             (pop-to-buffer gnus-group-buffer))
7113         (gnus-group-jump-to-group newsgroup))
7114     (save-excursion
7115       ;; Take care of tree window mode.
7116       (if (get-buffer-window gnus-group-buffer)
7117           (pop-to-buffer gnus-group-buffer)
7118         (set-buffer gnus-group-buffer))
7119       (gnus-group-jump-to-group newsgroup))))
7120
7121 ;; This function returns a list of article numbers based on the
7122 ;; difference between the ranges of read articles in this group and
7123 ;; the range of active articles.
7124 (defun gnus-list-of-unread-articles (group)
7125   (let* ((read (nth 2 (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
7126          (active (gnus-gethash group gnus-active-hashtb))
7127          (last (cdr active))
7128          unread first nlast unread)
7129     ;; If none are read, then all are unread. 
7130     (if (not read)
7131         (setq first (car active))
7132       ;; If the range of read articles is a single range, then the
7133       ;; first unread article is the article after the last read
7134       ;; article. Sounds logical, doesn't it?
7135       (if (not (listp (cdr read)))
7136           (setq first (1+ (cdr read)))
7137         ;; `read' is a list of ranges.
7138         (if (/= (setq nlast (or (and (numberp (car read)) (car read)) 
7139                                 (car (car read)))) 1)
7140             (setq first 1))
7141         (while read
7142           (if first 
7143               (while (< first nlast)
7144                 (setq unread (cons first unread))
7145                 (setq first (1+ first))))
7146           (setq first (1+ (if (atom (car read)) (car read) (cdr (car read)))))
7147           (setq nlast (if (atom (car (cdr read))) 
7148                           (car (cdr read))
7149                         (car (car (cdr read)))))
7150           (setq read (cdr read)))))
7151     ;; And add the last unread articles.
7152     (while (<= first last)
7153       (setq unread (cons first unread))
7154       (setq first (1+ first)))
7155     ;; Return the list of unread articles.
7156     (nreverse unread)))
7157
7158 (defun gnus-list-of-read-articles (group)
7159   (let ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
7160         (active (gnus-gethash group gnus-active-hashtb)))
7161     (and info active
7162          (gnus-sorted-complement 
7163           (gnus-uncompress-range active) 
7164           (gnus-list-of-unread-articles group)))))
7165
7166 ;; Various summary commands
7167
7168 (defun gnus-summary-universal-argument ()
7169   "Perform any operation on all articles marked with the process mark."
7170   (interactive)
7171   (gnus-set-global-variables)
7172   (let ((articles (reverse gnus-newsgroup-processable))
7173         key func)
7174     (or articles (error "No articles marked"))
7175     (or (setq func (key-binding (read-key-sequence "C-c C-u")))
7176         (error "Undefined key"))
7177     (while articles
7178       (gnus-summary-goto-subject (car articles))
7179       (command-execute func)
7180       (gnus-summary-remove-process-mark (car articles))
7181       (setq articles (cdr articles)))))
7182
7183 (defun gnus-summary-toggle-truncation (arg)
7184   "Toggle truncation of summary lines.
7185 With arg, turn line truncation on iff arg is positive."
7186   (interactive "P")
7187   (setq truncate-lines
7188         (if (null arg) (not truncate-lines)
7189           (> (prefix-numeric-value arg) 0)))
7190   (redraw-display))
7191
7192 (defun gnus-summary-reselect-current-group (all)
7193   "Once exit and then reselect the current newsgroup.
7194 The prefix argument ALL means to select all articles."
7195   (interactive "P")
7196   (gnus-set-global-variables)
7197   (let ((current-subject (gnus-summary-article-number))
7198         (group gnus-newsgroup-name))
7199     (setq gnus-newsgroup-begin nil)
7200     (gnus-summary-exit t)
7201     ;; We have to adjust the point of group mode buffer because the
7202     ;; current point was moved to the next unread newsgroup by
7203     ;; exiting.
7204     (gnus-summary-jump-to-group group)
7205     (gnus-group-read-group all t)
7206     (gnus-summary-goto-subject current-subject)))
7207
7208 (defun gnus-summary-rescan-group (all)
7209   "Exit the newsgroup, ask for new articles, and select the newsgroup."
7210   (interactive "P")
7211   (gnus-set-global-variables)
7212   ;; Fix by Ilja Weis <kult@uni-paderborn.de>.
7213   (let ((group gnus-newsgroup-name))
7214     (gnus-summary-exit)
7215     (gnus-summary-jump-to-group group)
7216     (save-excursion
7217       (set-buffer gnus-group-buffer)
7218       (gnus-group-get-new-news-this-group 1))
7219     (gnus-summary-jump-to-group group)
7220     (gnus-group-read-group all)))
7221
7222 (defun gnus-summary-update-info ()
7223   (let* ((group gnus-newsgroup-name)
7224          (method (car (gnus-find-method-for-group group))))
7225     (if gnus-newsgroup-kill-headers
7226         (setq gnus-newsgroup-killed
7227               (gnus-compress-sequence
7228                (nconc
7229                 (gnus-set-sorted-intersection
7230                  (gnus-uncompress-range gnus-newsgroup-killed)
7231                  (setq gnus-newsgroup-unselected
7232                        (sort gnus-newsgroup-unselected '<)))
7233                 (setq gnus-newsgroup-unreads
7234                       (sort gnus-newsgroup-unreads '<))) t)))
7235     (or (listp (cdr gnus-newsgroup-killed))
7236         (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
7237     (let ((updated nil)
7238           (headers gnus-newsgroup-headers))
7239       (gnus-close-group group)
7240       (run-hooks 'gnus-exit-group-hook)
7241       (gnus-update-read-articles 
7242        group gnus-newsgroup-unreads gnus-newsgroup-unselected 
7243        gnus-newsgroup-marked
7244        t gnus-newsgroup-replied gnus-newsgroup-expirable
7245        gnus-newsgroup-killed gnus-newsgroup-dormant
7246        gnus-newsgroup-bookmarks 
7247        (and gnus-save-score gnus-newsgroup-scored))
7248       (and gnus-use-cross-reference
7249            (gnus-mark-xrefs-as-read 
7250             group headers gnus-newsgroup-unreads gnus-newsgroup-expirable))
7251       ;; Do adaptive scoring, and possibly save score files.
7252       (and gnus-newsgroup-adaptive
7253            (gnus-score-adaptive))
7254       (and gnus-use-scoring 
7255            (fboundp 'gnus-score-save)
7256            (funcall 'gnus-score-save))
7257       ;; Do not switch windows but change the buffer to work.
7258       (set-buffer gnus-group-buffer)
7259       (or (assoc 'quit-config (gnus-find-method-for-group gnus-newsgroup-name))
7260           (gnus-group-update-group group)))))
7261   
7262 (defun gnus-summary-exit (&optional temporary)
7263   "Exit reading current newsgroup, and then return to group selection mode.
7264 gnus-exit-group-hook is called with no arguments if that value is non-nil."
7265   (interactive)
7266   (gnus-set-global-variables)
7267   (gnus-kill-save-kill-buffer)
7268   (let* ((group gnus-newsgroup-name)
7269          (quit-config (nth 1 (assoc 'quit-config (gnus-find-method-for-group
7270                                                   gnus-newsgroup-name))))
7271          (mode major-mode)
7272          (method (car (gnus-find-method-for-group group)))
7273          (buf (current-buffer)))
7274     (gnus-summary-update-info) ; Make all changes in this group permanent.
7275     ;; Make sure where I was, and go to next newsgroup.
7276     (or quit-config
7277         (progn
7278           (gnus-group-jump-to-group group)
7279           (gnus-group-next-unread-group 1)))
7280     (if temporary
7281         nil                             ;Nothing to do.
7282       ;; We set all buffer-local variables to nil. It is unclear why
7283       ;; this is needed, but if we don't, buffer-local variables are
7284       ;; not garbage-collected, it seems. This would the lead to en
7285       ;; ever-growing Emacs.
7286       (set-buffer buf)
7287       (gnus-summary-clear-local-variables)
7288       ;; We clear the global counterparts of the buffer-local
7289       ;; variables as well, just to be on the safe side.
7290       (gnus-configure-windows 'group)
7291       (gnus-summary-clear-local-variables)
7292       ;; Return to group mode buffer. 
7293       (if (eq mode 'gnus-summary-mode)
7294           (gnus-kill-buffer buf))
7295       (if (get-buffer gnus-article-buffer)
7296           (bury-buffer gnus-article-buffer))
7297       (setq gnus-current-select-method gnus-select-method)
7298       (pop-to-buffer gnus-group-buffer)
7299       (if (not quit-config)
7300           (progn
7301             (gnus-group-jump-to-group group)
7302             (gnus-group-next-unread-group 1))
7303         (if (not (buffer-name (car quit-config)))
7304             (gnus-configure-windows 'group)
7305           (set-buffer (car quit-config))
7306           (and (eq major-mode 'gnus-summary-mode)
7307                (gnus-set-global-variables))
7308           (gnus-configure-windows (cdr quit-config)))))))
7309
7310 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
7311 (defun gnus-summary-exit-no-update (&optional no-questions)
7312   "Quit reading current newsgroup without updating read article info."
7313   (interactive)
7314   (let* ((group gnus-newsgroup-name)
7315          (quit-config (nth 1 (assoc 'quit-config 
7316                                     (gnus-find-method-for-group group)))))
7317     (if (or no-questions
7318             gnus-expert-user
7319             (gnus-y-or-n-p "Do you really wanna quit reading this group? "))
7320         (progn
7321           (gnus-close-group group)
7322           (gnus-summary-clear-local-variables)
7323           (set-buffer gnus-group-buffer)
7324           (gnus-summary-clear-local-variables)
7325           ;; Return to group selection mode.
7326           (gnus-configure-windows 'group)
7327           (if (get-buffer gnus-summary-buffer)
7328               (kill-buffer gnus-summary-buffer))
7329           (if (get-buffer gnus-article-buffer)
7330               (bury-buffer gnus-article-buffer))
7331           (if (equal (gnus-group-group-name) group)
7332               (gnus-group-next-unread-group 1))
7333           (if quit-config
7334               (progn
7335                 (if (not (buffer-name (car quit-config)))
7336                     (gnus-configure-windows 'group)
7337                   (set-buffer (car quit-config))
7338                   (and (eq major-mode 'gnus-summary-mode)
7339                        (gnus-set-global-variables))
7340                   (gnus-configure-windows (cdr quit-config)))))))))
7341
7342 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
7343 (defun gnus-summary-fetch-faq (group)
7344   "Fetch the FAQ for the current group."
7345   (interactive (list gnus-newsgroup-name))
7346   (gnus-configure-windows 'summary-faq)
7347   (find-file (concat gnus-group-faq-directory group)))
7348
7349 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
7350 (defun gnus-summary-describe-group (force)
7351   "Describe the current newsgroup."
7352   (interactive "P")
7353   (gnus-group-describe-group force gnus-newsgroup-name))
7354
7355 (defun gnus-summary-describe-briefly ()
7356   "Describe summary mode commands briefly."
7357   (interactive)
7358   (gnus-message 6
7359     (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")))
7360
7361 ;; Walking around group mode buffer from summary mode.
7362
7363 (defun gnus-summary-next-group (&optional no-article group backward)
7364   "Exit current newsgroup and then select next unread newsgroup.
7365 If prefix argument NO-ARTICLE is non-nil, no article is selected initially.
7366 If BACKWARD, go to previous group instead."
7367   (interactive "P")
7368   (gnus-set-global-variables)
7369   (let ((ingroup gnus-newsgroup-name)
7370         (sumbuf (current-buffer))
7371         num)
7372     (gnus-summary-exit t)               ;Update all information.
7373     (if (and group
7374              (or (and (numberp (setq num (car (gnus-gethash
7375                                                group gnus-newsrc-hashtb))))
7376                       (< num 1))
7377                  (null num)))
7378         (progn
7379           (gnus-group-jump-to-group group)
7380           (setq group nil))
7381       (gnus-group-jump-to-group ingroup))
7382     (gnus-summary-search-group backward)
7383     (let ((group (or group (gnus-summary-search-group backward)))
7384           (buf gnus-summary-buffer))
7385       (if (null group)
7386           (gnus-summary-exit-no-update t)
7387         (gnus-message 5 "Selecting %s..." group)
7388         ;; We are now in group mode buffer.
7389         ;; Make sure group mode buffer point is on GROUP.
7390         (gnus-group-jump-to-group group)
7391         (if (not (eq gnus-auto-select-next 'quietly))
7392             (progn
7393               (gnus-summary-read-group group nil no-article buf)
7394               (and (string= gnus-newsgroup-name ingroup)
7395                    (bufferp sumbuf) (buffer-name sumbuf)
7396                    (progn
7397                      (set-buffer (setq gnus-summary-buffer sumbuf))
7398                      (gnus-summary-exit-no-update t))))
7399           (let ((prevgroup group))
7400             (gnus-summary-read-group group nil no-article buf)
7401             (while (and (string= gnus-newsgroup-name ingroup)
7402                         (bufferp sumbuf) 
7403                         (buffer-name sumbuf)
7404                         (not (string= prevgroup (gnus-group-group-name))))
7405               (set-buffer gnus-group-buffer)
7406               (gnus-summary-read-group 
7407                (setq prevgroup (gnus-group-group-name)) 
7408                nil no-article buf))
7409             (and (string= prevgroup (gnus-group-group-name))
7410                  ;; We have reached the final group in the group
7411                  ;; buffer.
7412                  (progn
7413                    (set-buffer sumbuf)
7414                    (gnus-summary-exit)))))))))
7415
7416 (defun gnus-summary-prev-group (no-article)
7417   "Exit current newsgroup and then select previous unread newsgroup.
7418 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7419   (interactive "P")
7420   (gnus-summary-next-group no-article nil t))
7421
7422 ;; Walking around summary lines.
7423
7424 (defun gnus-summary-first-subject (unread)
7425   "Go to the first unread subject.
7426 If UNREAD is non-nil, go to the first unread article.
7427 Returns nil if there are no unread articles."
7428   (interactive "P")
7429   (prog1
7430       (if (or (not unread)
7431               (gnus-goto-char 
7432                (text-property-any 
7433                 (point-min) (point-max) 'gnus-mark gnus-unread-mark)))
7434           t 
7435         ;; If there are no unread articles.
7436         (gnus-message 3 "No more unread articles")
7437         nil)
7438     (gnus-summary-position-cursor)))
7439
7440 (defun gnus-summary-next-subject (n &optional unread dont-display)
7441   "Go to next N'th summary line.
7442 If N is negative, go to the previous N'th subject line.
7443 If UNREAD is non-nil, only unread articles are selected.
7444 The difference between N and the actual number of steps taken is
7445 returned."
7446   (interactive "p")
7447   (let ((backward (< n 0))
7448         (n (abs n)))
7449     (while (and (> n 0)
7450                 (gnus-summary-search-forward unread nil backward))
7451       (setq n (1- n)))
7452     (if (/= 0 n) (gnus-message 7 "No more%s articles"
7453                                (if unread " unread" "")))
7454     (or dont-display
7455         (progn
7456           (gnus-summary-recenter)
7457           (gnus-summary-position-cursor)))
7458   n))
7459
7460 (defun gnus-summary-next-unread-subject (n)
7461   "Go to next N'th unread summary line."
7462   (interactive "p")
7463   (gnus-summary-next-subject n t))
7464
7465 (defun gnus-summary-prev-subject (n &optional unread)
7466   "Go to previous N'th summary line.
7467 If optional argument UNREAD is non-nil, only unread article is selected."
7468   (interactive "p")
7469   (gnus-summary-next-subject (- n) unread))
7470
7471 (defun gnus-summary-prev-unread-subject (n)
7472   "Go to previous N'th unread summary line."
7473   (interactive "p")
7474   (gnus-summary-next-subject (- n) t))
7475
7476 (defun gnus-summary-goto-subject (article)
7477   "Go the subject line of ARTICLE."
7478   (interactive
7479    (list
7480     (string-to-int
7481      (completing-read "Article number: "
7482                       (mapcar
7483                        (lambda (headers)
7484                          (list
7485                           (int-to-string (header-number headers))))
7486                        gnus-newsgroup-headers)
7487                       nil 'require-match))))
7488   (or article (error "No article number"))
7489   (let ((b (point)))
7490     (if (not (gnus-goto-char (text-property-any (point-min) (point-max)
7491                                                 'gnus-number article)))
7492         ()
7493       (gnus-summary-show-thread)
7494       ;; Skip dummy articles. 
7495       (if (eq (gnus-summary-article-mark) gnus-dummy-mark)
7496           (forward-line 1))
7497       (prog1
7498           (if (not (eobp))
7499               article
7500             (goto-char b)
7501             nil)
7502         (gnus-summary-position-cursor)))))
7503
7504 ;; Walking around summary lines with displaying articles.
7505
7506 (defun gnus-summary-expand-window ()
7507   "Make the summary buffer take up the entire Emacs frame."
7508   (interactive)
7509   (gnus-set-global-variables)
7510   (gnus-configure-windows 'summary))
7511
7512 (defun gnus-summary-display-article (article &optional all-header)
7513   "Display ARTICLE in article buffer."
7514   (gnus-set-global-variables)
7515   (if (null article)
7516       nil
7517     (gnus-article-prepare article all-header)
7518     (gnus-summary-show-thread)
7519     (if (eq (gnus-summary-article-mark) gnus-dummy-mark)
7520         (progn
7521           (forward-line 1)
7522           (gnus-summary-position-cursor)))
7523     (run-hooks 'gnus-select-article-hook)
7524     (gnus-summary-recenter)
7525 ;    (set-window-point (get-buffer-window (current-buffer)) (point-max))
7526 ;    (sit-for 0)
7527     (gnus-summary-goto-subject article)
7528     ;; Successfully display article.
7529     (gnus-summary-update-line)
7530     (gnus-article-set-window-start 
7531      (cdr (assq article gnus-newsgroup-bookmarks)))
7532     t))
7533
7534 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7535   "Select the current article.
7536 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7537 non-nil, the article will be re-fetched even if it already present in
7538 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7539 be displayed."
7540   (and (not pseudo) (gnus-summary-pseudo-article)
7541        (error "This is a pseudo-article."))
7542   (let ((article (or article (gnus-summary-article-number)))
7543         (all-headers (not (not all-headers))) ;Must be T or NIL.
7544         did) 
7545     (prog1
7546         (save-excursion
7547           (set-buffer gnus-summary-buffer)
7548           (if (or (null gnus-current-article)
7549                   (null gnus-article-current)
7550                   (not (eq article (cdr gnus-article-current)))
7551                   (not (equal (car gnus-article-current) gnus-newsgroup-name))
7552                   force)
7553               ;; The requested article is different from the current article.
7554               (progn
7555                 (gnus-summary-display-article article all-headers)
7556                 (setq did article))
7557             (if all-headers (gnus-article-show-all-headers))
7558             nil))
7559       (if did 
7560           (gnus-article-set-window-start 
7561            (cdr (assq article gnus-newsgroup-bookmarks)))))))
7562
7563 (defun gnus-summary-set-current-mark (&optional current-mark)
7564   "Obsolete function."
7565   nil)
7566
7567 (defun gnus-summary-next-article (unread &optional subject backward)
7568   "Select the next article.
7569 If UNREAD, only unread articles are selected.
7570 If SUBJECT, only articles with SUBJECT are selected.
7571 If BACKWARD, the previous article is selected instead of the next."
7572   (interactive "P")
7573   (gnus-set-global-variables)
7574   (let ((opoint (point))
7575         (method (car (gnus-find-method-for-group gnus-newsgroup-name)))
7576         header)
7577     (cond
7578      ;; Is there such an article?
7579      ((gnus-summary-display-article 
7580        (gnus-summary-search-forward unread subject backward))
7581       (gnus-summary-position-cursor))
7582      ;; If not, we try the first unread, if that is wanted.
7583      ((and subject
7584            gnus-auto-select-same
7585            (gnus-summary-first-unread-article))
7586       (gnus-message 6 "Wrapped"))
7587      ;; Try to get next/previous article not displayed in this group.
7588      ((and gnus-auto-extend-newsgroup
7589            (not unread) (not subject)
7590            (setq header (gnus-more-header-forward backward)))
7591       (gnus-extend-newsgroup header backward)
7592       (let ((buffer-read-only nil))
7593         (goto-char (if backward (point-min) (point-max)))
7594         (gnus-summary-prepare-threads (list header) 0))
7595       (gnus-summary-goto-article (if backward gnus-newsgroup-begin
7596                                    gnus-newsgroup-end)))
7597      ;; Go to next/previous group.
7598      (t
7599       (or (assoc 'quit-config (gnus-find-method-for-group gnus-newsgroup-name))
7600           (gnus-summary-jump-to-group gnus-newsgroup-name))
7601       (let ((cmd (aref (this-command-keys) 0))
7602             (group 
7603              (if (eq gnus-keep-same-level 'best) 
7604                  (gnus-summary-best-group gnus-newsgroup-name)
7605                (gnus-summary-search-group backward gnus-keep-same-level))))
7606         ;; For some reason, the group window gets selected. We change
7607         ;; it back.  
7608         (select-window (get-buffer-window (current-buffer)))
7609         ;; Keep just the event type of CMD.
7610         (and (listp cmd) (setq cmd (car cmd)))
7611         ;; Select next unread newsgroup automagically.
7612         (cond 
7613          ((not gnus-auto-select-next)
7614           (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7615          ((eq gnus-auto-select-next 'quietly)
7616           ;; Select quietly.
7617           (if (assoc 'quit-config (gnus-find-method-for-group 
7618                                    gnus-newsgroup-name))
7619               (gnus-summary-exit)
7620             (gnus-message 7 "No more%s articles (%s)..."
7621                           (if unread " unread" "") 
7622                           (if group (concat "selecting " group)
7623                             "exiting"))
7624             (gnus-summary-next-group nil group backward)))
7625          (t
7626           (let ((keystrokes '(?\C-n ?\C-p))
7627                 key)
7628             (while (or (null key) (memq key keystrokes))
7629               (gnus-message 
7630                7 "No more%s articles%s" (if unread " unread" "")
7631                (if (and group (not (assoc 'quit-config
7632                                           (gnus-find-method-for-group 
7633                                            gnus-newsgroup-name))))
7634                    (format " (Type %s for %s [%s])"
7635                            (single-key-description cmd) group
7636                            (car (gnus-gethash group gnus-newsrc-hashtb)))
7637                  (format " (Type %s to exit %s)"
7638                          (single-key-description cmd)
7639                          gnus-newsgroup-name)))
7640               ;; Confirm auto selection.
7641               (let* ((event (read-event)))
7642                 (setq key (if (listp event) (car event) event))
7643                 (if (memq key keystrokes)
7644                     (let ((obuf (current-buffer)))
7645                       (switch-to-buffer gnus-group-buffer)
7646                       (gnus-group-jump-to-group group)
7647                       (execute-kbd-macro (char-to-string key))
7648                       (setq group (gnus-group-group-name))
7649                       (switch-to-buffer obuf)))))
7650             (if (equal key cmd)
7651                 (if (or (not group) (assoc 'quit-config
7652                                            (gnus-find-method-for-group
7653                                             gnus-newsgroup-name)))
7654                     (gnus-summary-exit)
7655                   (gnus-summary-next-group nil group backward))
7656               (setq unread-command-events (list key)))))))))))
7657
7658 (defun gnus-summary-next-unread-article ()
7659   "Select unread article after current one."
7660   (interactive)
7661   (gnus-summary-next-article t (and gnus-auto-select-same
7662                                     (gnus-summary-subject-string))))
7663
7664 (defun gnus-summary-prev-article (unread &optional subject)
7665   "Select the article after the current one.
7666 If UNREAD is non-nil, only unread articles are selected."
7667   (interactive "P")
7668   (gnus-summary-next-article unread subject t))
7669
7670 (defun gnus-summary-prev-unread-article ()
7671   "Select unred article before current one."
7672   (interactive)
7673   (gnus-summary-prev-article t (and gnus-auto-select-same
7674                                     (gnus-summary-subject-string))))
7675
7676 (defun gnus-summary-next-page (lines &optional circular)
7677   "Show next page of selected article.
7678 If end of article, select next article.
7679 Argument LINES specifies lines to be scrolled up.
7680 If CIRCULAR is non-nil, go to the start of the article instead of 
7681 instead of selecting the next article when reaching the end of the
7682 current article." 
7683   (interactive "P")
7684   (setq gnus-summary-buffer (current-buffer))
7685   (gnus-set-global-variables)
7686   (let ((article (gnus-summary-article-number))
7687         (endp nil))
7688     (gnus-configure-windows 'article)
7689     (if (or (null gnus-current-article)
7690             (null gnus-article-current)
7691             (/= article (cdr gnus-article-current))
7692             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7693         ;; Selected subject is different from current article's.
7694         (gnus-summary-display-article article)
7695       (gnus-eval-in-buffer-window
7696        gnus-article-buffer
7697        (setq endp (gnus-article-next-page lines)))
7698       (if endp
7699           (cond (circular
7700                  (gnus-summary-beginning-of-article))
7701                 (lines
7702                  (gnus-message 3 "End of message"))
7703                 ((null lines)
7704                  (gnus-summary-next-unread-article)))))
7705     (gnus-summary-recenter)
7706     (gnus-summary-position-cursor)))
7707
7708 (defun gnus-summary-prev-page (lines)
7709   "Show previous page of selected article.
7710 Argument LINES specifies lines to be scrolled down."
7711   (interactive "P")
7712   (gnus-set-global-variables)
7713   (let ((article (gnus-summary-article-number)))
7714     (gnus-configure-windows 'article)
7715     (if (or (null gnus-current-article)
7716             (null gnus-article-current)
7717             (/= article (cdr gnus-article-current))
7718             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7719         ;; Selected subject is different from current article's.
7720         (gnus-summary-display-article article)
7721       (gnus-summary-recenter)
7722       (gnus-eval-in-buffer-window gnus-article-buffer
7723         (gnus-article-prev-page lines))))
7724   (gnus-summary-position-cursor))
7725
7726 (defun gnus-summary-scroll-up (lines)
7727   "Scroll up (or down) one line current article.
7728 Argument LINES specifies lines to be scrolled up (or down if negative)."
7729   (interactive "p")
7730   (gnus-set-global-variables)
7731   (gnus-configure-windows 'article)
7732   (or (gnus-summary-select-article nil nil 'pseudo)
7733       (gnus-eval-in-buffer-window 
7734        gnus-article-buffer
7735        (cond ((> lines 0)
7736               (if (gnus-article-next-page lines)
7737                   (gnus-message 3 "End of message")))
7738              ((< lines 0)
7739               (gnus-article-prev-page (- lines))))))
7740   (gnus-summary-recenter)
7741   (gnus-summary-position-cursor))
7742
7743 (defun gnus-summary-next-same-subject ()
7744   "Select next article which has the same subject as current one."
7745   (interactive)
7746   (gnus-set-global-variables)
7747   (gnus-summary-next-article nil (gnus-summary-subject-string)))
7748
7749 (defun gnus-summary-prev-same-subject ()
7750   "Select previous article which has the same subject as current one."
7751   (interactive)
7752   (gnus-set-global-variables)
7753   (gnus-summary-prev-article nil (gnus-summary-subject-string)))
7754
7755 (defun gnus-summary-next-unread-same-subject ()
7756   "Select next unread article which has the same subject as current one."
7757   (interactive)
7758   (gnus-set-global-variables)
7759   (gnus-summary-next-article t (gnus-summary-subject-string)))
7760
7761 (defun gnus-summary-prev-unread-same-subject ()
7762   "Select previous unread article which has the same subject as current one."
7763   (interactive)
7764   (gnus-set-global-variables)
7765   (gnus-summary-prev-article t (gnus-summary-subject-string)))
7766
7767 (defun gnus-summary-first-unread-article ()
7768   "Select the first unread article. 
7769 Return nil if there are no unread articles."
7770   (interactive)
7771   (gnus-set-global-variables)
7772   (prog1
7773       (if (gnus-summary-first-subject t)
7774           (gnus-summary-display-article (gnus-summary-article-number)))
7775     (gnus-summary-position-cursor)))
7776
7777 (defun gnus-summary-best-unread-article ()
7778   "Select the unread article with the highest score."
7779   (interactive)
7780   (gnus-set-global-variables)
7781   (let ((scored gnus-newsgroup-scored)
7782         (best -1000000)
7783         article art)
7784     (while scored
7785       (or (> best (cdr (car scored)))
7786           (and (memq (setq art (car (car scored))) gnus-newsgroup-unreads)
7787                (not (memq art gnus-newsgroup-marked))
7788                (not (memq art gnus-newsgroup-dormant))
7789                (if (= best (cdr (car scored)))
7790                    (setq article (min art article))
7791                  (setq article art)
7792                  (setq best (cdr (car scored))))))
7793       (setq scored (cdr scored)))
7794     (if article 
7795         (gnus-summary-goto-article article)
7796       (gnus-summary-first-unread-article))
7797     (gnus-summary-position-cursor)))
7798
7799 (defun gnus-summary-goto-article (article &optional all-headers)
7800   "Fetch ARTICLE and display it if it exists.
7801 If ALL-HEADERS is non-nil, no header lines are hidden."
7802   (interactive
7803    (list
7804     (string-to-int
7805      (completing-read 
7806       "Article number: "
7807       (mapcar (lambda (headers) (list (int-to-string (header-number headers))))
7808               gnus-newsgroup-headers) 
7809       nil 'require-match))))
7810   (prog1
7811       (and (gnus-summary-goto-subject article)
7812            (gnus-summary-display-article article all-headers))
7813     (gnus-summary-position-cursor)))
7814
7815 (defun gnus-summary-goto-last-article ()
7816   "Go to the last article."
7817   (interactive)
7818   (prog1
7819       (and gnus-last-article
7820            (gnus-summary-goto-article gnus-last-article))
7821     (gnus-summary-position-cursor)))
7822
7823 (defun gnus-summary-pop-article (number)
7824   "Pop one article off the history and go to the previous.
7825 NUMBER articles will be popped off."
7826   (interactive "p")
7827   (let (to)
7828     (setq gnus-newsgroup-history
7829           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7830     (if to
7831         (gnus-summary-goto-article (car to))
7832       (error "Article history empty")))
7833   (gnus-summary-position-cursor))
7834
7835 ;; Summary article oriented commands
7836
7837 (defun gnus-summary-refer-parent-article (n)
7838   "Refer parent article N times.
7839 The difference between N and the number of articles fetched is returned."
7840   (interactive "p")
7841   (gnus-set-global-variables)
7842   (while 
7843       (and 
7844        (> n 0)
7845        (let ((ref (header-references (gnus-get-header-by-number
7846                                       (gnus-summary-article-number)))))
7847          (if (and ref (not (equal ref ""))
7848                   (string-match "<[^<>]*>[ \t]*$" ref))
7849              (gnus-summary-refer-article 
7850               (substring ref (match-beginning 0) (match-end 0))))))
7851     (setq n (1- n)))
7852   (or (zerop n) 
7853       (gnus-message 1 "No references in article or expired article."))
7854   (gnus-summary-position-cursor)
7855   n)
7856     
7857 (defun gnus-summary-refer-article (message-id)
7858   "Refer article specified by MESSAGE-ID.
7859 NOTE: This command only works with newsgroups that use real or simulated NNTP."
7860   (interactive "sMessage-ID: ")
7861   (if (or (not (stringp message-id))
7862           (zerop (length message-id)))
7863       ()
7864     ;; Construct the correct Message-ID if necessary.
7865     ;; Suggested by tale@pawl.rpi.edu.
7866     (or (string-match "^<" message-id)
7867         (setq message-id (concat "<" message-id)))
7868     (or (string-match ">$" message-id)
7869         (setq message-id (concat message-id ">")))
7870     (let ((header (car (gnus-gethash (downcase message-id)
7871                                      gnus-newsgroup-dependencies))))
7872       (if header
7873           (or (gnus-summary-goto-article (header-number header))
7874               ;; The header has been read, but the article had been
7875               ;; expunged, so we insert it again.
7876               (progn
7877                 (gnus-summary-insert-line
7878                  nil header 0 nil gnus-read-mark nil nil
7879                  (header-subject header))
7880                 (forward-line -1)
7881                 (header-number header)))
7882         (let ((gnus-override-method gnus-refer-article-method)
7883               (gnus-ancient-mark gnus-read-mark)
7884               number)
7885           (and gnus-refer-article-method
7886                (or (gnus-server-opened gnus-refer-article-method)
7887                    (gnus-open-server gnus-refer-article-method)))
7888           (if (gnus-article-prepare 
7889                message-id nil (gnus-read-header message-id))
7890               (progn
7891                 (setq number (header-number gnus-current-headers))
7892                 (gnus-rebuild-thread message-id)
7893                 (gnus-summary-goto-subject number)
7894                 (gnus-article-set-window-start 
7895                  (cdr (assq number gnus-newsgroup-bookmarks)))
7896                 message-id)
7897             (gnus-message 1 "No such references")
7898             nil))))))
7899
7900 (defun gnus-summary-enter-digest-group ()
7901   "Enter a digest group based on the current article."
7902   (interactive)
7903   (gnus-set-global-variables)
7904   (gnus-summary-select-article)
7905   ;; We do not want a narrowed article.
7906   (gnus-summary-stop-page-breaking)
7907   (let ((name (format "%s-%d" 
7908                       (gnus-group-prefixed-name 
7909                        gnus-newsgroup-name (list 'nndoc "")) 
7910                       gnus-current-article))
7911         (buf (current-buffer)))
7912     (if (gnus-group-read-ephemeral-group 
7913          name (list 'nndoc name
7914                     (list 'nndoc-address (get-buffer gnus-article-buffer))
7915                     '(nndoc-article-type digest))
7916          t)
7917         ()
7918       (switch-to-buffer buf)
7919       (gnus-set-global-variables)
7920       (gnus-configure-windows 'summary)
7921       (gnus-message 3 "Article not a digest?"))))
7922
7923 (defun gnus-summary-isearch-article ()
7924   "Do incremental search forward on current article."
7925   (interactive)
7926   (gnus-set-global-variables)
7927   (gnus-summary-select-article)
7928   (gnus-eval-in-buffer-window 
7929    gnus-article-buffer (isearch-forward)))
7930
7931 (defun gnus-summary-search-article-forward (regexp)
7932   "Search for an article containing REGEXP forward.
7933 gnus-select-article-hook is not called during the search."
7934   (interactive
7935    (list (read-string
7936           (concat "Search forward (regexp): "
7937                   (if gnus-last-search-regexp
7938                       (concat "(default " gnus-last-search-regexp ") "))))))
7939   (gnus-set-global-variables)
7940   (if (string-equal regexp "")
7941       (setq regexp (or gnus-last-search-regexp ""))
7942     (setq gnus-last-search-regexp regexp))
7943   (if (gnus-summary-search-article regexp nil)
7944       (gnus-eval-in-buffer-window 
7945        gnus-article-buffer
7946        (recenter 0))
7947     (error "Search failed: \"%s\"" regexp)))
7948
7949 (defun gnus-summary-search-article-backward (regexp)
7950   "Search for an article containing REGEXP backward.
7951 gnus-select-article-hook is not called during the search."
7952   (interactive
7953    (list (read-string
7954           (concat "Search backward (regexp): "
7955                   (if gnus-last-search-regexp
7956                       (concat "(default " gnus-last-search-regexp ") "))))))
7957   (gnus-set-global-variables)
7958   (if (string-equal regexp "")
7959       (setq regexp (or gnus-last-search-regexp ""))
7960     (setq gnus-last-search-regexp regexp))
7961   (if (gnus-summary-search-article regexp t)
7962       (gnus-eval-in-buffer-window
7963        gnus-article-buffer
7964        (recenter 0))
7965     (error "Search failed: \"%s\"" regexp)))
7966
7967 (defun gnus-summary-search-article (regexp &optional backward)
7968   "Search for an article containing REGEXP.
7969 Optional argument BACKWARD means do search for backward.
7970 gnus-select-article-hook is not called during the search."
7971   (let ((gnus-select-article-hook nil)  ;Disable hook.
7972         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7973         (re-search
7974          (if backward
7975              (function re-search-backward) (function re-search-forward)))
7976         (found nil)
7977         (last nil))
7978     ;; Hidden thread subtrees must be searched for ,too.
7979     (gnus-summary-show-all-threads)
7980     (if (eobp) (forward-line -1))
7981     ;; First of all, search current article.
7982     ;; We don't want to read article again from NNTP server nor reset
7983     ;; current point.
7984     (gnus-summary-select-article)
7985     (gnus-message 9 "Searching article: %d..." gnus-current-article)
7986     (setq last gnus-current-article)
7987     (gnus-eval-in-buffer-window gnus-article-buffer
7988       (save-restriction
7989         (widen)
7990         ;; Begin search from current point.
7991         (setq found (funcall re-search regexp nil t))))
7992     ;; Then search next articles.
7993     (while (and (not found)
7994                 (gnus-summary-display-article 
7995                  (gnus-summary-search-subject backward nil nil)))
7996       (gnus-message 9 "Searching article: %d..." gnus-current-article)
7997       (gnus-eval-in-buffer-window gnus-article-buffer
7998         (save-restriction
7999           (widen)
8000           (goto-char (if backward (point-max) (point-min)))
8001           (setq found (funcall re-search regexp nil t)))))
8002     (message "")
8003     ;; Adjust article pointer.
8004     (or (eq last gnus-current-article)
8005         (setq gnus-last-article last))
8006     ;; Return T if found such article.
8007     found))
8008
8009 (defun gnus-summary-execute-command (header regexp command &optional backward)
8010   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8011 If HEADER is an empty string (or nil), the match is done on the entire
8012 article. If BACKWARD (the prefix) is non-nil, search backward instead."
8013   (interactive
8014    (list (let ((completion-ignore-case t))
8015            (completing-read 
8016             "Header name: "
8017             (mapcar (lambda (string) (list string))
8018                     '("Number" "Subject" "From" "Lines" "Date"
8019                       "Message-ID" "Xref" "References"))
8020             nil 'require-match))
8021          (read-string "Regexp: ")
8022          (read-key-sequence "Command: ")
8023          current-prefix-arg))
8024   (gnus-set-global-variables)
8025   ;; Hidden thread subtrees must be searched as well.
8026   (gnus-summary-show-all-threads)
8027   ;; We don't want to change current point nor window configuration.
8028   (save-excursion
8029     (save-window-excursion
8030       (gnus-message 6 "Executing %s..." (key-description command))
8031       ;; We'd like to execute COMMAND interactively so as to give arguments.
8032       (gnus-execute header regexp
8033                     (` (lambda ()
8034                          (call-interactively '(, (key-binding command)))))
8035                     backward)
8036       (gnus-message 6 "Executing %s...done" (key-description command)))))
8037
8038 (defun gnus-summary-beginning-of-article ()
8039   "Scroll the article back to the beginning."
8040   (interactive)
8041   (gnus-set-global-variables)
8042   (gnus-summary-select-article)
8043   (gnus-eval-in-buffer-window
8044    gnus-article-buffer
8045    (widen)
8046    (goto-char (point-min))
8047    (and gnus-break-pages (gnus-narrow-to-page))))
8048
8049 (defun gnus-summary-end-of-article ()
8050   "Scroll to the end of the article."
8051   (interactive)
8052   (gnus-set-global-variables)
8053   (gnus-summary-select-article)
8054   (gnus-eval-in-buffer-window 
8055    gnus-article-buffer
8056    (widen)
8057    (goto-char (point-max))
8058    (and gnus-break-pages (gnus-narrow-to-page))))
8059
8060 (defun gnus-summary-show-article ()
8061   "Force re-fetching of the current article."
8062   (interactive)
8063   (gnus-set-global-variables)
8064   (gnus-summary-select-article gnus-have-all-headers t))
8065
8066 (defun gnus-summary-toggle-header (arg)
8067   "Show the headers if they are hidden, or hide them if they are shown.
8068 If ARG is a positive number, show the entire header.
8069 If ARG is a negative number, hide the unwanted header lines."
8070   (interactive "P")
8071   (gnus-set-global-variables)
8072   (save-excursion
8073     (set-buffer gnus-article-buffer)
8074     (let ((buffer-read-only nil))
8075       (if (numberp arg) 
8076           (if (> arg 0) (remove-text-properties (point-min) (point-max) 
8077                                                 gnus-hidden-properties)
8078             (if (< arg 0) (run-hooks 'gnus-article-display-hook)))
8079         (if (text-property-any (point-min) (point-max) 'invisible t)
8080             (remove-text-properties (point-min) (point-max)
8081                                     gnus-hidden-properties)
8082           (let ((gnus-have-all-headers nil))
8083             (run-hooks 'gnus-article-display-hook))))
8084       (set-window-point (get-buffer-window (current-buffer)) (point-min)))))
8085
8086 (defun gnus-summary-show-all-headers ()
8087   "Make all header lines visible."
8088   (interactive)
8089   (gnus-set-global-variables)
8090   (gnus-article-show-all-headers))
8091
8092 (defun gnus-summary-toggle-mime (arg)
8093   "Toggle MIME processing.
8094 If ARG is a positive number, turn MIME processing on."
8095   (interactive "P")
8096   (gnus-set-global-variables)
8097   (setq gnus-show-mime
8098         (if (null arg) (not gnus-show-mime)
8099           (> (prefix-numeric-value arg) 0)))
8100   (gnus-summary-select-article t 'force))
8101
8102 (defun gnus-summary-caesar-message (rotnum)
8103   "Caesar rotates all letters of current message by 13/47 places.
8104 With prefix arg, specifies the number of places to rotate each letter forward.
8105 Caesar rotates Japanese letters by 47 places in any case."
8106   (interactive "P")
8107   (gnus-set-global-variables)
8108   (gnus-summary-select-article)
8109   (let ((mail-header-separator "")) ; !!! Is this necessary?
8110     (gnus-overload-functions)
8111     (gnus-eval-in-buffer-window 
8112      gnus-article-buffer
8113      (save-restriction
8114        (widen)
8115        ;; We don't want to jump to the beginning of the message.
8116        ;; `save-excursion' does not do its job.
8117        (move-to-window-line 0)
8118        (let ((last (point)))
8119          (news-caesar-buffer-body rotnum)
8120          (goto-char last)
8121          (recenter 0))))))
8122
8123 (defun gnus-summary-stop-page-breaking ()
8124   "Stop page breaking in the current article."
8125   (interactive)
8126   (gnus-set-global-variables)
8127   (gnus-summary-select-article)
8128   (gnus-eval-in-buffer-window gnus-article-buffer (widen)))
8129
8130 ;; Suggested by Brian Edmonds <bedmonds@prodigy.bc.ca>.
8131
8132 (defun gnus-summary-move-article (n &optional to-newsgroup select-method)
8133   "Move the current article to a different newsgroup.
8134 If N is a positive number, move the N next articles.
8135 If N is a negative number, move the N previous articles.
8136 If N is nil and any articles have been marked with the process mark,
8137 move those articles instead.
8138 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to. 
8139 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
8140 re-spool using this method.
8141 For this function to work, both the current newsgroup and the
8142 newsgroup that you want to move to have to support the `request-move'
8143 and `request-accept' functions. (Ie. mail newsgroups at present.)"
8144   (interactive "P")
8145   (gnus-set-global-variables)
8146   (or (gnus-check-backend-function 'request-move-article gnus-newsgroup-name)
8147       (error "The current newsgroup does not support article moving"))
8148   (let ((articles (gnus-summary-work-articles n))
8149         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
8150         art-group)
8151     (if (and (not to-newsgroup) (not select-method))
8152         (setq to-newsgroup
8153               (completing-read 
8154                (format "Where do you want to move %s? %s"
8155                        (if (> (length articles) 1)
8156                            (format "these %d articles" (length articles))
8157                          "this article")
8158                        (if gnus-current-move-group
8159                            (format "(%s default) " gnus-current-move-group)
8160                          ""))
8161                gnus-active-hashtb nil nil prefix)))
8162     (if to-newsgroup
8163         (progn
8164           (if (or (string= to-newsgroup "") (string= to-newsgroup prefix))
8165               (setq to-newsgroup (or gnus-current-move-group "")))
8166           (or (gnus-gethash to-newsgroup gnus-active-hashtb)
8167               (gnus-activate-newsgroup to-newsgroup)
8168               (error "No such group: %s" to-newsgroup))
8169           (setq gnus-current-move-group to-newsgroup)))
8170     (or (gnus-check-backend-function 'request-accept-article 
8171                                      (or select-method to-newsgroup))
8172         (error "%s does not support article moving" to-newsgroup))
8173     (gnus-message 6 "Moving to %s: %s..." 
8174                   (or select-method to-newsgroup) articles)
8175     (while articles
8176       (if (setq art-group
8177                 (gnus-request-move-article 
8178                  (car articles)                   ; Article to move
8179                  gnus-newsgroup-name              ; From newsgrouo
8180                  (nth 1 (gnus-find-method-for-group 
8181                          gnus-newsgroup-name))    ; Server
8182                  (list 'gnus-request-accept-article 
8183                        (if select-method
8184                            (list 'quote select-method)
8185                          to-newsgroup)
8186                        (not (cdr articles)))     ; Accept form
8187                  (not (cdr articles))))          ; Only save nov last time
8188           (let* ((buffer-read-only nil)
8189                  (entry 
8190                   (or
8191                    (gnus-gethash (car art-group) gnus-newsrc-hashtb)
8192                    (gnus-gethash 
8193                     (gnus-group-prefixed-name 
8194                      (car art-group) 
8195                      (if select-method (list select-method "")
8196                        (gnus-find-method-for-group to-newsgroup)))
8197                     gnus-newsrc-hashtb)))
8198                  (info (nth 2 entry))
8199                  (article (car articles))
8200                  (marked (nth 3 info)))
8201             (gnus-summary-goto-subject article)
8202             (beginning-of-line)
8203             (delete-region (point)
8204                            (progn (forward-line 1) (point)))
8205             (if (not (memq article gnus-newsgroup-unreads))
8206                 (setcar (cdr (cdr info))
8207                         (gnus-add-to-range (nth 2 info) 
8208                                            (list (cdr art-group)))))
8209             ;; Copy any marks over to the new group.
8210             (let ((marks '((tick . gnus-newsgroup-marked)
8211                            (dormant . gnus-newsgroup-dormant)
8212                            (expire . gnus-newsgroup-expirable)
8213                            (bookmark . gnus-newsgroup-bookmarks)
8214                         ;   (score . gnus-newsgroup-scored)
8215                            (reply . gnus-newsgroup-replied)))
8216                   (to-article (cdr art-group)))
8217               (while marks
8218                 (if (memq article (symbol-value (cdr (car marks))))
8219                     (gnus-add-marked-articles 
8220                      (car info) (car (car marks)) (list to-article) info))
8221                 (setq marks (cdr marks))))
8222             (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8223             (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8224             (setq gnus-newsgroup-dormant
8225                   (delq article gnus-newsgroup-dormant)))
8226         (gnus-message 1 "Couldn't move article %s" (car articles)))
8227       (gnus-summary-remove-process-mark (car articles))
8228       (setq articles (cdr articles)))))
8229
8230 (defun gnus-summary-respool-article (n &optional respool-method)
8231   "Respool the current article.
8232 The article will be squeezed through the mail spooling process again,
8233 which means that it will be put in some mail newsgroup or other
8234 depending on `nnmail-split-methods'.
8235 If N is a positive number, respool the N next articles.
8236 If N is a negative number, respool the N previous articles.
8237 If N is nil and any articles have been marked with the process mark,
8238 respool those articles instead.
8239
8240 Respooling can be done both from mail groups and \"real\" newsgroups.
8241 In the former case, the articles in question will be moved from the
8242 current group into whatever groups they are destined to.  In the
8243 latter case, they will be copied into the relevant groups."
8244   (interactive "P")
8245   (gnus-set-global-variables)
8246   (let ((respool-methods (gnus-methods-using 'respool))
8247         (methname 
8248          (symbol-name (car (gnus-find-method-for-group gnus-newsgroup-name)))))
8249     (or respool-method
8250         (setq respool-method
8251               (completing-read
8252                "What method do you want to use when respooling? "
8253                respool-methods nil t methname)))
8254     (or (string= respool-method "")
8255         (if (assoc (symbol-name
8256                     (car (gnus-find-method-for-group gnus-newsgroup-name)))
8257                    respool-methods)
8258             (gnus-summary-move-article n nil (intern respool-method))
8259           (gnus-summary-copy-article n nil (intern respool-method))))))
8260
8261 ;; Suggested by gregj@unidata.com (Gregory J. Grubbs).
8262 (defun gnus-summary-copy-article (n &optional to-newsgroup select-method)
8263   "Move the current article to a different newsgroup.
8264 If N is a positive number, move the N next articles.
8265 If N is a negative number, move the N previous articles.
8266 If N is nil and any articles have been marked with the process mark,
8267 move those articles instead.
8268 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to. 
8269 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
8270 re-spool using this method.
8271 For this function to work, the newsgroup that you want to move to have
8272 to support the `request-move' and `request-accept'
8273 functions. (Ie. mail newsgroups at present.)"
8274   (interactive "P")
8275   (gnus-set-global-variables)
8276   (let ((articles (gnus-summary-work-articles n))
8277         (copy-buf (get-buffer-create "*copy work*"))
8278         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
8279         art-group)
8280     (buffer-disable-undo copy-buf)
8281     (if (and (not to-newsgroup) (not select-method))
8282         (setq to-newsgroup
8283               (completing-read 
8284                (format "Where do you want to copy %s? %s"
8285                        (if (> (length articles) 1)
8286                            (format "these %d articles" (length articles))
8287                          "this article")
8288                        (if gnus-current-move-group
8289                            (format "(%s default) " gnus-current-move-group)
8290                          ""))
8291                gnus-active-hashtb nil nil prefix)))
8292     (if to-newsgroup
8293         (progn
8294           (if (or (string= to-newsgroup "") (string= to-newsgroup prefix))
8295               (setq to-newsgroup (or gnus-current-move-group "")))
8296           (or (gnus-gethash to-newsgroup gnus-active-hashtb)
8297               (gnus-activate-newsgroup to-newsgroup)
8298               (error "No such group: %s" to-newsgroup))
8299           (setq gnus-current-move-group to-newsgroup)))
8300     (or (gnus-check-backend-function 'request-accept-article 
8301                                      (or select-method to-newsgroup))
8302         (error "%s does not support article copying" to-newsgroup))
8303     (gnus-message 6 "Copying to %s: %s..." 
8304                   (or select-method to-newsgroup) articles)
8305     (while articles
8306       (if (setq art-group
8307                 (save-excursion
8308                   (set-buffer copy-buf)
8309                   (gnus-request-article-this-buffer
8310                    (car articles) gnus-newsgroup-name)
8311                   (gnus-request-accept-article
8312                    (if select-method (quote select-method) to-newsgroup)
8313                    (not (cdr articles)))))
8314           (let* ((entry 
8315                   (or
8316                    (gnus-gethash (car art-group) gnus-newsrc-hashtb)
8317                    (gnus-gethash 
8318                     (gnus-group-prefixed-name 
8319                      (car art-group) 
8320                      (if select-method (list select-method "")
8321                        (gnus-find-method-for-group to-newsgroup)))
8322                     gnus-newsrc-hashtb)))
8323                  (info (nth 2 entry))
8324                  (article (car articles))
8325                  (marked (nth 3 info)))
8326             (if (not (memq article gnus-newsgroup-unreads))
8327                 (setcar (cdr (cdr info))
8328                         (gnus-add-to-range (nth 2 info) 
8329                                            (list (cdr art-group)))))
8330             ;; Copy any marks over to the new group.
8331             (let ((marks '((tick . gnus-newsgroup-marked)
8332                            (dormant . gnus-newsgroup-dormant)
8333                            (expire . gnus-newsgroup-expirable)
8334                            (bookmark . gnus-newsgroup-bookmarks)
8335                         ;   (score . gnus-newsgroup-scored)
8336                            (reply . gnus-newsgroup-replied)))
8337                   (to-article (cdr art-group)))
8338               (while marks
8339                 (if (memq article (symbol-value (cdr (car marks))))
8340                     (gnus-add-marked-articles 
8341                      (car info) (car (car marks)) (list to-article) info))
8342                 (setq marks (cdr marks)))))
8343         (gnus-message 1 "Couldn't copy article %s" (car articles)))
8344       (gnus-summary-remove-process-mark (car articles))
8345       (setq articles (cdr articles)))
8346     (kill-buffer copy-buf)))
8347
8348 (defun gnus-summary-import-article (file)
8349   "Import a random file into a mail newsgroup."
8350   (interactive "fImport file: ")
8351   (let ((group gnus-newsgroup-name)
8352         atts)
8353     (or (gnus-check-backend-function 'request-accept-article group)
8354         (error "%s does not support article importing" group))
8355     (or (file-readable-p file)
8356         (not (file-regular-p file))
8357         (error "Can't read %s" file))
8358     (save-excursion
8359       (set-buffer (get-buffer-create " *import file*"))
8360       (buffer-disable-undo (current-buffer))
8361       (erase-buffer)
8362       (insert-file-contents file)
8363       (goto-char (point-min))
8364       (if (nnheader-article-p)
8365           ()
8366         (setq atts (file-attributes file))
8367         (insert "From: " (read-string "From: ") "\n"
8368                 "Subject: " (read-string "Subject: ") "\n"
8369                 "Date: " (current-time-string (nth 5 atts)) "\n"
8370                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8371       (gnus-request-accept-article group t)
8372       (kill-buffer (current-buffer)))))
8373
8374 (defun gnus-summary-expire-articles ()
8375   "Expire all articles that are marked as expirable in the current group."
8376   (interactive)
8377   (if (and gnus-newsgroup-expirable
8378            (gnus-check-backend-function 
8379             'request-expire-articles gnus-newsgroup-name))
8380       (let ((expirable gnus-newsgroup-expirable))
8381         ;; The list of articles that weren't expired is returned.
8382         (setq gnus-newsgroup-expirable 
8383               (gnus-request-expire-articles gnus-newsgroup-expirable
8384                                             gnus-newsgroup-name))
8385         ;; We go through the old list of expirable, and mark all
8386         ;; really expired articles as non-existent.
8387         (while expirable
8388           (or (memq (car expirable) gnus-newsgroup-expirable)
8389               (gnus-summary-mark-as-read (car expirable) gnus-canceled-mark))
8390           (setq expirable (cdr expirable))))))
8391
8392 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
8393 (defun gnus-summary-delete-article (n)
8394   "Delete the N next (mail) articles.
8395 This command actually deletes articles. This is not a marking
8396 command. The article will disappear forever from you life, never to
8397 return. 
8398 If N is negative, delete backwards.
8399 If N is nil and articles have been marked with the process mark,
8400 delete these instead."
8401   (interactive "P")
8402   (or (gnus-check-backend-function 'request-expire-articles 
8403                                    gnus-newsgroup-name)
8404       (error "The current newsgroup does not support article deletion."))
8405   ;; Compute the list of articles to delete.
8406   (let ((articles (gnus-summary-work-articles n))
8407         not-deleted)
8408     (if (and gnus-novice-user
8409              (not (gnus-y-or-n-p 
8410                    (format "Do you really want to delete %s forever? "
8411                            (if (> (length articles) 1) "these articles"
8412                              "this article")))))
8413         ()
8414       ;; Delete the articles.
8415       (setq not-deleted (gnus-request-expire-articles 
8416                          articles gnus-newsgroup-name 'force))
8417       (while articles
8418         (gnus-summary-remove-process-mark (car articles))       
8419         ;; The backend might not have been able to delete the article
8420         ;; after all.  
8421         (or (memq (car articles) not-deleted)
8422             (gnus-summary-mark-as-read (car articles) gnus-canceled-mark))
8423         (setq articles (cdr articles))))
8424     (gnus-summary-position-cursor)
8425     not-deleted))
8426
8427 (defun gnus-summary-edit-article ()
8428   "Enter into a buffer and edit the current article.
8429 This will have permanent effect only in mail groups."
8430   (interactive)
8431   (or (gnus-check-backend-function 
8432        'request-replace-article gnus-newsgroup-name)
8433       (error "The current newsgroup does not support article editing."))
8434   (gnus-summary-select-article t)
8435   (other-window 1)
8436   (gnus-message 6 "C-c C-c to end edits")
8437   (setq buffer-read-only nil)
8438   (text-mode)
8439   (use-local-map (copy-keymap (current-local-map)))
8440   (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
8441   (goto-char (point-min))
8442   (search-forward "\n\n" nil t))
8443
8444 (defun gnus-summary-edit-article-done ()
8445   "Make edits to the current article permanent."
8446   (interactive)
8447   (if (not (gnus-request-replace-article 
8448             (cdr gnus-article-current) (car gnus-article-current) 
8449             (current-buffer)))
8450       (error "Couldn't replace article.")
8451     (gnus-article-mode)
8452     (use-local-map gnus-article-mode-map)
8453     (setq buffer-read-only t)
8454     (pop-to-buffer gnus-summary-buffer)))      
8455
8456 (defun gnus-summary-fancy-query ()
8457   "Query where the fancy respool algorithm would put this article."
8458   (interactive)
8459   (gnus-summary-select-article)
8460   (save-excursion
8461     (set-buffer gnus-article-buffer)
8462     (save-restriction
8463       (goto-char (point-min))
8464       (search-forward "\n\n")
8465       (narrow-to-region (point-min) (point))
8466       (pp-eval-expression (list 'quote (nnmail-split-fancy))))))
8467
8468 ;; Summary score commands.
8469
8470 ;; Suggested by boubaker@cenatls.cena.dgac.fr.
8471
8472 (defun gnus-summary-raise-score (n)
8473   "Raise the score of the current article by N."
8474   (interactive "p")
8475   (gnus-summary-set-score (+ (gnus-summary-article-score) n)))
8476
8477 (defun gnus-summary-lower-score (n)
8478   "Lower the score of the current article by N."
8479   (interactive "p")
8480   (gnus-summary-raise-score (- n)))
8481
8482 (defun gnus-summary-set-score (n)
8483   "Set the score of the current article to N."
8484   (interactive "p")
8485   ;; Skip dummy header line.
8486   (save-excursion
8487     (gnus-summary-show-thread)
8488     (if (eq (gnus-summary-article-mark) gnus-dummy-mark)
8489         (forward-line 1))
8490     (let ((buffer-read-only nil))
8491       ;; Set score.
8492       (gnus-summary-update-mark
8493        (if (= n (or gnus-summary-default-score 0)) ? 
8494          (if (< n (or gnus-summary-default-score 0)) 
8495              gnus-score-below-mark gnus-score-over-mark)) 'score))
8496     (let* ((article (gnus-summary-article-number))
8497            (score (assq article gnus-newsgroup-scored)))
8498       (if score (setcdr score n)
8499         (setq gnus-newsgroup-scored 
8500               (cons (cons article n) gnus-newsgroup-scored))))
8501     (gnus-summary-update-line)))
8502
8503 (defun gnus-summary-current-score ()
8504   "Return the score of the current article."
8505   (interactive)
8506   (message "%s" (gnus-summary-article-score)))
8507
8508 ;; Summary marking commands.
8509
8510 (defun gnus-summary-raise-same-subject-and-select (score)
8511   "Raise articles which has the same subject with SCORE and select the next."
8512   (interactive "p")
8513   (let ((subject (gnus-summary-subject-string)))
8514     (gnus-summary-raise-score score)
8515     (while (gnus-summary-search-subject nil nil subject)
8516       (gnus-summary-raise-score score))
8517     (gnus-summary-next-article t)))
8518
8519 (defun gnus-summary-raise-same-subject (score)
8520   "Raise articles which has the same subject with SCORE."
8521   (interactive "p")
8522   (let ((subject (gnus-summary-subject-string)))
8523     (gnus-summary-raise-score score)
8524     (while (gnus-summary-search-subject nil nil subject)
8525       (gnus-summary-raise-score score))
8526     (gnus-summary-next-subject 1 t)))
8527
8528 (defun gnus-score-default (level)
8529   (if level (prefix-numeric-value level) 
8530     gnus-score-interactive-default-score))
8531
8532 (defun gnus-summary-raise-thread (score)
8533   "Raise articles under current thread with SCORE."
8534   (interactive "P")
8535   (setq score (1- (gnus-score-default score)))
8536   (let (e)
8537     (save-excursion
8538       (let ((level (gnus-summary-thread-level)))
8539         (gnus-summary-raise-score score)
8540         (while (and (zerop (gnus-summary-next-subject 1 nil t))
8541                     (> (gnus-summary-thread-level) level))
8542           (gnus-summary-raise-score score))
8543         (setq e (point))))
8544     (let ((gnus-summary-check-current t))
8545       (or (zerop (gnus-summary-next-subject 1 t))
8546           (goto-char e))))
8547   (gnus-summary-recenter)
8548   (gnus-summary-position-cursor)
8549   (gnus-set-mode-line 'summary))
8550
8551 (defun gnus-summary-lower-same-subject-and-select (score)
8552   "Raise articles which has the same subject with SCORE and select the next."
8553   (interactive "p")
8554   (gnus-summary-raise-same-subject-and-select (- score)))
8555
8556 (defun gnus-summary-lower-same-subject (score)
8557   "Raise articles which has the same subject with SCORE."
8558   (interactive "p")
8559   (gnus-summary-raise-same-subject (- score)))
8560
8561 (defun gnus-summary-lower-thread (score)
8562   "Raise articles under current thread with SCORE."
8563   (interactive "P")
8564   (gnus-summary-raise-thread (- (1- (gnus-score-default score)))))
8565
8566 (defun gnus-summary-kill-same-subject-and-select (unmark)
8567   "Mark articles which has the same subject as read, and then select the next.
8568 If UNMARK is positive, remove any kind of mark.
8569 If UNMARK is negative, tick articles."
8570   (interactive "P")
8571   (if unmark
8572       (setq unmark (prefix-numeric-value unmark)))
8573   (let ((count
8574          (gnus-summary-mark-same-subject
8575           (gnus-summary-subject-string) unmark)))
8576     ;; Select next unread article. If auto-select-same mode, should
8577     ;; select the first unread article.
8578     (gnus-summary-next-article t (and gnus-auto-select-same
8579                                       (gnus-summary-subject-string)))
8580     (gnus-message 7 "%d articles are marked as %s"
8581                   count (if unmark "unread" "read"))))
8582
8583 (defun gnus-summary-kill-same-subject (unmark)
8584   "Mark articles which has the same subject as read. 
8585 If UNMARK is positive, remove any kind of mark.
8586 If UNMARK is negative, tick articles."
8587   (interactive "P")
8588   (if unmark
8589       (setq unmark (prefix-numeric-value unmark)))
8590   (let ((count
8591          (gnus-summary-mark-same-subject
8592           (gnus-summary-subject-string) unmark)))
8593     ;; If marked as read, go to next unread subject.
8594     (if (null unmark)
8595         ;; Go to next unread subject.
8596         (gnus-summary-next-subject 1 t))
8597     (gnus-message 7 "%d articles are marked as %s"
8598                   count (if unmark "unread" "read"))))
8599
8600 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8601   "Mark articles with same SUBJECT as read, and return marked number.
8602 If optional argument UNMARK is positive, remove any kinds of marks.
8603 If optional argument UNMARK is negative, mark articles as unread instead."
8604   (let ((count 1))
8605     (save-excursion
8606       (cond ((null unmark)
8607              (gnus-summary-mark-as-read nil gnus-killed-mark))
8608             ((> unmark 0)
8609              (gnus-summary-tick-article nil t))
8610             (t
8611              (gnus-summary-tick-article)))
8612       (while (and subject
8613                   (gnus-summary-search-forward nil subject))
8614         (cond ((null unmark)
8615                (gnus-summary-mark-as-read nil gnus-killed-mark))
8616               ((> unmark 0)
8617                (gnus-summary-tick-article nil t))
8618               (t
8619                (gnus-summary-tick-article)))
8620         (setq count (1+ count))))
8621     ;; Hide killed thread subtrees.  Does not work properly always.
8622     ;;(and (null unmark)
8623     ;;     gnus-thread-hide-killed
8624     ;;     (gnus-summary-hide-thread))
8625     ;; Return number of articles marked as read.
8626     count))
8627
8628 (defun gnus-summary-mark-as-processable (n &optional unmark)
8629   "Set the process mark on the next N articles.
8630 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8631 the process mark instead.  The difference between N and the actual
8632 number of articles marked is returned."
8633   (interactive "p")
8634   (let ((backward (< n 0))
8635         (n (abs n)))
8636   (while (and 
8637           (> n 0)
8638           (if unmark
8639               (gnus-summary-remove-process-mark (gnus-summary-article-number))
8640             (gnus-summary-set-process-mark (gnus-summary-article-number)))
8641           (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8642     (setq n (1- n)))
8643   (if (/= 0 n) (gnus-message 7 "No more articles"))
8644   (gnus-summary-recenter)
8645   (gnus-summary-position-cursor)
8646   n))
8647
8648 (defun gnus-summary-unmark-as-processable (n)
8649   "Remove the process mark from the next N articles.
8650 If N is negative, mark backward instead.  The difference between N and
8651 the actual number of articles marked is returned."
8652   (interactive "p")
8653   (gnus-summary-mark-as-processable n t))
8654
8655 (defun gnus-summary-unmark-all-processable ()
8656   "Remove the process mark from all articles."
8657   (interactive)
8658   (save-excursion
8659     (while gnus-newsgroup-processable
8660       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8661   (gnus-summary-position-cursor))
8662
8663 (defun gnus-summary-mark-as-expirable (n)
8664   "Mark N articles forward as expirable.
8665 If N is negative, mark backward instead. The difference between N and
8666 the actual number of articles marked is returned."
8667   (interactive "p")
8668   (gnus-summary-mark-forward n gnus-expirable-mark))
8669
8670 (defun gnus-summary-mark-article-as-replied (article)
8671   "Mark ARTICLE replied and update the summary line."
8672   (setq gnus-newsgroup-replied (cons article gnus-newsgroup-replied))
8673   (let ((buffer-read-only nil))
8674     (if (gnus-summary-goto-subject article)
8675         (progn
8676           (gnus-summary-update-mark gnus-replied-mark 'replied)
8677           t))))
8678
8679 (defun gnus-summary-set-bookmark (article)
8680   "Set a bookmark in current article."
8681   (interactive (list (gnus-summary-article-number)))
8682   (if (or (not (get-buffer gnus-article-buffer))
8683           (not gnus-current-article)
8684           (not gnus-article-current)
8685           (not (equal gnus-newsgroup-name (car gnus-article-current))))
8686       (error "No current article selected"))
8687   ;; Remove old bookmark, if one exists.
8688   (let ((old (assq article gnus-newsgroup-bookmarks)))
8689     (if old (setq gnus-newsgroup-bookmarks 
8690                   (delq old gnus-newsgroup-bookmarks))))
8691   ;; Set the new bookmark, which is on the form 
8692   ;; (article-number . line-number-in-body).
8693   (setq gnus-newsgroup-bookmarks 
8694         (cons 
8695          (cons article 
8696                (save-excursion
8697                  (set-buffer gnus-article-buffer)
8698                  (count-lines
8699                   (min (point)
8700                        (save-excursion
8701                          (goto-char (point-min))
8702                          (search-forward "\n\n" nil t)
8703                          (point)))
8704                   (point))))
8705          gnus-newsgroup-bookmarks))
8706   (gnus-message 6 "A bookmark has been added to the current article."))
8707
8708 (defun gnus-summary-remove-bookmark (article)
8709   "Remove the bookmark from the current article."
8710   (interactive (list (gnus-summary-article-number)))
8711   ;; Remove old bookmark, if one exists.
8712   (let ((old (assq article gnus-newsgroup-bookmarks)))
8713     (if old 
8714         (progn
8715           (setq gnus-newsgroup-bookmarks 
8716                 (delq old gnus-newsgroup-bookmarks))
8717           (gnus-message 6 "Removed bookmark."))
8718       (gnus-message 6 "No bookmark in current article."))))
8719
8720 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8721 (defun gnus-summary-mark-as-dormant (n)
8722   "Mark N articles forward as dormant.
8723 If N is negative, mark backward instead.  The difference between N and
8724 the actual number of articles marked is returned."
8725   (interactive "p")
8726   (gnus-summary-mark-forward n gnus-dormant-mark))
8727
8728 (defun gnus-summary-set-process-mark (article)
8729   "Set the process mark on ARTICLE and update the summary line."
8730   (setq gnus-newsgroup-processable (cons article gnus-newsgroup-processable))
8731   (let ((buffer-read-only nil))
8732     (if (gnus-summary-goto-subject article)
8733         (progn
8734           (gnus-summary-show-thread)
8735           (and (eq (gnus-summary-article-mark) gnus-dummy-mark)
8736                (forward-line 1))
8737           (gnus-summary-update-mark gnus-process-mark 'replied)
8738           t))))
8739
8740 (defun gnus-summary-remove-process-mark (article)
8741   "Remove the process mark from ARTICLE and update the summary line."
8742   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8743   (let ((buffer-read-only nil))
8744     (if (gnus-summary-goto-subject article)
8745         (progn
8746           (gnus-summary-show-thread)
8747           (and (eq (gnus-summary-article-mark) gnus-dummy-mark)
8748                (forward-line 1))
8749           (gnus-summary-update-mark ?  'replied)
8750           (if (memq article gnus-newsgroup-replied) 
8751               (gnus-summary-update-mark gnus-replied-mark 'replied))
8752           t))))
8753
8754 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8755   "Mark N articles as read forwards.
8756 If N is negative, mark backwards instead.
8757 Mark with MARK. If MARK is ? , ?! or ??, articles will be
8758 marked as unread. 
8759 The difference between N and the actual number of articles marked is
8760 returned."
8761   (interactive "p")
8762   (gnus-set-global-variables)
8763   (let ((backward (< n 0))
8764         (n (abs n))
8765         (mark (or mark gnus-del-mark)))
8766   (while (and (> n 0)
8767               (gnus-summary-mark-article nil mark no-expire)
8768               (zerop (gnus-summary-next-subject 
8769                       (if backward -1 1) gnus-summary-goto-unread t)))
8770     (setq n (1- n)))
8771   (if (/= 0 n) (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8772   (gnus-summary-recenter)
8773   (gnus-summary-position-cursor)
8774   (gnus-set-mode-line 'summary)
8775   n))
8776
8777 (defun gnus-summary-mark-article (&optional article mark no-expire)
8778   "Mark ARTICLE with MARK.
8779 MARK can be any character.
8780 Five MARK strings are reserved: ?  (unread), 
8781 ?! (ticked), ?? (dormant), ?D (read), ?E (expirable).
8782 If MARK is nil, then the default character ?D is used.
8783 If ARTICLE is nil, then the article on the current line will be
8784 marked." 
8785   ;; If no mark is given, then we check auto-expiring.
8786   (and (not no-expire)
8787        gnus-newsgroup-auto-expire 
8788        (or (not mark)
8789            (and (numberp mark) (or (= mark gnus-killed-mark)
8790                                    (= mark gnus-del-mark)
8791                                    (= mark gnus-catchup-mark)
8792                                    (= mark gnus-low-score-mark)
8793                                    (= mark gnus-read-mark))))
8794        (setq mark gnus-expirable-mark))
8795   (let* ((mark (or (and (stringp mark) (aref mark 0)) mark gnus-del-mark))
8796          (article (or article (gnus-summary-article-number))))
8797     (if (or (= mark gnus-unread-mark) 
8798             (= mark gnus-ticked-mark) 
8799             (= mark gnus-dormant-mark))
8800         (gnus-mark-article-as-unread article mark)
8801       (gnus-mark-article-as-read article mark))
8802
8803     ;; See whether the article is to be put in the cache.
8804     (and gnus-use-cache
8805          (save-excursion
8806            (gnus-cache-possibly-enter-article 
8807             gnus-newsgroup-name article 
8808             (gnus-get-header-by-number article)
8809             (= mark gnus-ticked-mark)
8810             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8811
8812     (if (gnus-summary-goto-subject article)
8813         (let ((buffer-read-only nil))
8814           (gnus-summary-show-thread)
8815           (and (eq (gnus-summary-article-mark) gnus-dummy-mark)
8816                (forward-line 1))
8817           ;; Fix the mark.
8818           (gnus-summary-update-mark mark 'unread)
8819           t))))
8820
8821 (defun gnus-summary-update-mark (mark type)
8822   (beginning-of-line)
8823   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8824         plist)
8825     (if (not forward)
8826         ()
8827       (forward-char forward)
8828       (setq plist (text-properties-at (point)))
8829       (delete-char 1)
8830       (insert mark)
8831       (and plist (add-text-properties (1- (point)) (point) plist))
8832       (and (eq type 'unread)
8833            (add-text-properties (1- (point)) (point) (list 'gnus-mark mark)))
8834       (gnus-summary-update-line (eq mark gnus-unread-mark)))))
8835   
8836 (defun gnus-mark-article-as-read (article &optional mark)
8837   "Enter ARTICLE in the pertinent lists and remove it from others."
8838   ;; Make the article expirable.
8839   (let ((mark (or (and (stringp mark) (aref mark 0)) mark gnus-del-mark)))
8840     (if (= mark gnus-expirable-mark)
8841         (setq gnus-newsgroup-expirable (cons article gnus-newsgroup-expirable))
8842       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8843     ;; Remove from unread and marked lists.
8844     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8845     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8846     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8847     ;; Possibly remove from cache, if that is used. 
8848     (and gnus-use-cache 
8849          (gnus-cache-possibly-remove-article
8850           gnus-newsgroup-name article 
8851           (memq article gnus-newsgroup-marked)
8852           (memq article gnus-newsgroup-dormant)
8853           (or (memq article gnus-newsgroup-unreads)
8854               (memq article gnus-newsgroup-unselected))))))
8855
8856 (defun gnus-mark-article-as-unread (article &optional mark)
8857   "Enter ARTICLE in the pertinent lists and remove it from others."
8858   (let ((mark (or (and (stringp mark) (aref mark 0)) mark gnus-ticked-mark)))
8859     ;; Add to unread list.
8860     (or (memq article gnus-newsgroup-unreads)
8861         (setq gnus-newsgroup-unreads (cons article gnus-newsgroup-unreads)))
8862     ;; If CLEAR-MARK is non-nil, the article must be removed from mark
8863     ;; lists.  Otherwise, it must be added to the list.
8864     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8865     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8866     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8867     (if (= mark gnus-ticked-mark)
8868         (setq gnus-newsgroup-marked (cons article gnus-newsgroup-marked)))
8869     (if (= mark gnus-dormant-mark)
8870         (setq gnus-newsgroup-dormant (cons article gnus-newsgroup-dormant)))))
8871
8872 (defalias 'gnus-summary-mark-as-unread-forward 
8873   'gnus-summary-tick-article-forward)
8874 (make-obsolete 'gnus-summary-mark-as-unread-forward 
8875                'gnus-summary-tick-article-forward)
8876 (defun gnus-summary-tick-article-forward (n)
8877   "Tick N articles forwards.
8878 If N is negative, tick backwards instead.
8879 The difference between N and the number of articles ticked is returned."
8880   (interactive "p")
8881   (gnus-summary-mark-forward n gnus-ticked-mark))
8882
8883 (defalias 'gnus-summary-mark-as-unread-backward 
8884   'gnus-summary-tick-article-backward)
8885 (make-obsolete 'gnus-summary-mark-as-unread-backward 
8886                'gnus-summary-tick-article-backward)
8887 (defun gnus-summary-tick-article-backward (n)
8888   "Tick N articles backwards.
8889 The difference between N and the number of articles ticked is returned."
8890   (interactive "p")
8891   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8892
8893 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8894 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8895 (defun gnus-summary-tick-article (&optional article clear-mark)
8896   "Mark current article as unread.
8897 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8898 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8899   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8900                                        gnus-ticked-mark)))
8901
8902 (defun gnus-summary-mark-as-read-forward (n)
8903   "Mark N articles as read forwards.
8904 If N is negative, mark backwards instead.
8905 The difference between N and the actual number of articles marked is
8906 returned."
8907   (interactive "p")
8908   (gnus-summary-mark-forward n gnus-del-mark t))
8909
8910 (defun gnus-summary-mark-as-read-backward (n)
8911   "Mark the N articles as read backwards.
8912 The difference between N and the actual number of articles marked is
8913 returned."
8914   (interactive "p")
8915   (gnus-summary-mark-forward (- n) gnus-del-mark t))
8916
8917 (defun gnus-summary-mark-as-read (&optional article mark)
8918   "Mark current article as read.
8919 ARTICLE specifies the article to be marked as read.
8920 MARK specifies a string to be inserted at the beginning of the line."
8921   (gnus-summary-mark-article article mark))
8922
8923 (defun gnus-summary-clear-mark-forward (n)
8924   "Clear marks from N articles forward.
8925 If N is negative, clear backward instead.
8926 The difference between N and the number of marks cleared is returned."
8927   (interactive "p")
8928   (gnus-summary-mark-forward n gnus-unread-mark))
8929
8930 (defun gnus-summary-clear-mark-backward (n)
8931   "Clear marks from N articles backward.
8932 The difference between N and the number of marks cleared is returned."
8933   (interactive "p")
8934   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8935
8936 (defun gnus-summary-mark-unread-as-read ()
8937   "Intended to be used by `gnus-summary-mark-article-hook'."
8938   (or (memq gnus-current-article gnus-newsgroup-marked)
8939       (memq gnus-current-article gnus-newsgroup-dormant)
8940       (memq gnus-current-article gnus-newsgroup-expirable)
8941       (gnus-summary-mark-as-read gnus-current-article gnus-read-mark)))
8942
8943 (defun gnus-summary-mark-region-as-read (point mark all)
8944   "Mark all unread articles between point and mark as read.
8945 If given a prefix, mark all articles between point and mark as read,
8946 even ticked and dormant ones."
8947   (interactive "r\nP")
8948   (save-excursion
8949     (goto-char point)
8950     (beginning-of-line)
8951     (while (and 
8952             (< (point) mark)
8953             (progn
8954               (and
8955                (or all
8956                    (and
8957                     (not (memq (gnus-summary-article-number)
8958                                gnus-newsgroup-marked))
8959                     (not (memq (gnus-summary-article-number)
8960                                gnus-newsgroup-dormant))))
8961                (gnus-summary-mark-article
8962                 (gnus-summary-article-number) gnus-del-mark))
8963               t)
8964             (zerop (forward-line 1))))))
8965
8966 ;; Fix by Per Abrahamsen <amanda@iesd.auc.dk>.
8967 (defalias 'gnus-summary-delete-marked-as-read 
8968   'gnus-summary-remove-lines-marked-as-read)
8969 (make-obsolete 'gnus-summary-delete-marked-as-read 
8970                'gnus-summary-remove-lines-marked-as-read)
8971 (defun gnus-summary-remove-lines-marked-as-read ()
8972   "Remove lines that are marked as read."
8973   (interactive)
8974   (gnus-summary-remove-lines-marked-with 
8975    (concat (mapconcat
8976             (lambda (char) (char-to-string (symbol-value char)))
8977             '(gnus-del-mark gnus-read-mark gnus-ancient-mark
8978               gnus-killed-mark gnus-kill-file-mark
8979               gnus-low-score-mark gnus-expirable-mark)
8980             ""))))
8981
8982 (defalias 'gnus-summary-delete-marked-with 
8983   'gnus-summary-remove-lines-marked-with)
8984 (make-obsolete 'gnus-summary-delete-marked-with 
8985                'gnus-summary-remove-lines-marked-with)
8986 ;; Rewrite by Daniel Quinlan <quinlan@best.com>.
8987 (defun gnus-summary-remove-lines-marked-with (marks)
8988   "Remove lines that are marked with MARKS (e.g. \"DK\")."
8989   (interactive "sMarks: ")
8990   ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
8991   (gnus-set-global-variables)
8992   (let ((buffer-read-only nil)
8993         (marks (concat "^[" marks "]")))
8994     (goto-char (point-min))
8995     (if gnus-newsgroup-adaptive
8996         (gnus-score-remove-lines-adaptive marks)
8997       (while (re-search-forward marks nil t)
8998         (gnus-delete-line)))
8999     ;; If we use dummy roots, we have to do an additional sweep over
9000     ;; the buffer.
9001     (if (not (eq gnus-summary-make-false-root 'dummy))
9002         ()
9003       (goto-char (point-min))
9004       (setq marks (concat "^[" (char-to-string gnus-dummy-mark) "]"))
9005       (while (re-search-forward marks nil t)
9006         (if (gnus-subject-equal
9007              (gnus-summary-subject-string)
9008              (progn
9009                (forward-line 1)
9010                (gnus-summary-subject-string)))
9011             ()
9012           (forward-line -1)
9013           (gnus-delete-line)))))
9014   (or (zerop (buffer-size))
9015       (if (eobp)
9016           (gnus-summary-prev-subject 1)
9017         (gnus-summary-position-cursor))))
9018
9019 (defun gnus-summary-expunge-below (score)
9020   "Remove articles with score less than SCORE."
9021   (interactive "P")
9022   (gnus-set-global-variables)
9023   (setq score (if score
9024                   (prefix-numeric-value score)
9025                 (or gnus-summary-default-score 0)))
9026   (save-excursion
9027     (set-buffer gnus-summary-buffer)
9028     (goto-char (point-min))
9029     (let ((buffer-read-only nil)
9030           beg)
9031       (while (not (eobp))
9032         (if (< (gnus-summary-article-score) score)
9033             (progn
9034               (setq beg (point))
9035               (forward-line 1)
9036               (delete-region beg (point)))
9037           (forward-line 1)))
9038       ;; Adjust point.
9039       (or (zerop (buffer-size))
9040           (if (eobp)
9041               (gnus-summary-prev-subject 1)
9042             (gnus-summary-position-cursor))))))
9043
9044 (defun gnus-summary-mark-below (score mark)
9045   "Mark articles with score less than SCORE with MARK."
9046   (interactive "P\ncMark: ")
9047   (gnus-set-global-variables)
9048   (setq score (if score
9049                   (prefix-numeric-value score)
9050                 (or gnus-summary-default-score 0)))
9051   (save-excursion
9052     (set-buffer gnus-summary-buffer)
9053     (goto-char (point-min))
9054     (while (not (eobp))
9055       (and (< (gnus-summary-article-score) score)
9056            (gnus-summary-mark-article nil mark))
9057       (forward-line 1))))
9058
9059 (defun gnus-summary-kill-below (score)
9060   "Mark articles with score below SCORE as read."
9061   (interactive "P")
9062   (gnus-summary-mark-below score gnus-killed-mark))
9063
9064 (defun gnus-summary-clear-above (score)
9065   "Clear all marks from articles with score above SCORE."
9066   (interactive "P")
9067   (gnus-summary-mark-above score gnus-unread-mark))
9068
9069 (defun gnus-summary-tick-above (score)
9070   "Tick all articles with score above SCORE."
9071   (interactive "P")
9072   (gnus-summary-mark-above score gnus-ticked-mark))
9073
9074 (defun gnus-summary-mark-above (score mark)
9075   "Mark articles with score over SCORE with MARK."
9076   (interactive "P\ncMark: ")
9077   (setq score (if score
9078                   (prefix-numeric-value score)
9079                 (or gnus-summary-default-score 0)))
9080   (save-excursion
9081     (set-buffer gnus-summary-buffer)
9082     (goto-char (point-min))
9083     (while (not (eobp))
9084       (if (> (gnus-summary-article-score) score)
9085           (progn
9086             (gnus-summary-mark-article nil mark)
9087             (forward-line 1))
9088         (forward-line 1)))))
9089
9090 ;; Suggested by Daniel Quinlan <quinlan@best.com>.  
9091 (defun gnus-summary-show-all-expunged ()
9092   "Display all the hidden articles that were expunged for low scores."
9093   (interactive)
9094   (let ((buffer-read-only nil))
9095     (let ((scored gnus-newsgroup-scored)
9096           headers h)
9097       (while scored
9098         (or (gnus-summary-goto-subject (car (car scored)))
9099             (and (setq h (gnus-get-header-by-number (car (car scored))))
9100                  (< (cdr (car scored)) gnus-summary-expunge-below)
9101                  (setq headers (cons h headers))))
9102         (setq scored (cdr scored)))
9103       (or headers (error "No expunged articles hidden."))
9104       (goto-char (point-min))
9105       (save-excursion 
9106         (gnus-summary-update-lines 
9107          (point)
9108          (progn
9109            (gnus-summary-prepare-threads (nreverse headers) 0)
9110            (point)))))
9111     (goto-char (point-min))
9112     (gnus-summary-position-cursor)))
9113
9114 (defun gnus-summary-show-all-dormant ()
9115   "Display all the hidden articles that are marked as dormant."
9116   (interactive)
9117   (let ((buffer-read-only nil))
9118     (goto-char (point-min))
9119     (let ((dormant gnus-newsgroup-dormant)
9120           headers h)
9121       (while dormant
9122         (or (gnus-summary-goto-subject (car dormant))
9123             (and (setq h (gnus-get-header-by-number (car dormant)))
9124                  (setq headers (cons h headers))))
9125         (setq dormant (cdr dormant)))
9126       (or headers (error "No dormant articles hidden."))
9127       (save-excursion 
9128         (gnus-summary-update-lines 
9129          (point)
9130          (progn
9131            (gnus-summary-prepare-threads (nreverse headers) 0)
9132            (point)))))
9133     (goto-char (point-min))
9134     (gnus-summary-position-cursor)))
9135
9136 (defun gnus-summary-hide-all-dormant ()
9137   "Hide all dormant articles."
9138   (interactive)
9139   (gnus-summary-remove-lines-marked-with (char-to-string gnus-dormant-mark))
9140   (gnus-summary-position-cursor))
9141
9142 (defun gnus-summary-catchup (all &optional quietly to-here not-mark)
9143   "Mark all articles not marked as unread in this newsgroup as read.
9144 If prefix argument ALL is non-nil, all articles are marked as read.
9145 If QUIETLY is non-nil, no questions will be asked.
9146 If TO-HERE is non-nil, it should be a point in the buffer. All
9147 articles before this point will be marked as read.
9148 The number of articles marked as read is returned."
9149   (interactive "P")
9150   (prog1
9151       (if (or quietly
9152               (not gnus-interactive-catchup) ;Without confirmation?
9153               gnus-expert-user
9154               (gnus-y-or-n-p
9155                (if all
9156                    "Mark absolutely all articles as read? "
9157                  "Mark all unread articles as read? ")))
9158           (if (and not-mark 
9159                    (not gnus-newsgroup-adaptive)
9160                    (not gnus-newsgroup-auto-expire))
9161               (progn
9162                 (and all (setq gnus-newsgroup-marked nil
9163                                gnus-newsgroup-dormant nil))
9164                 (setq gnus-newsgroup-unreads 
9165                       (append gnus-newsgroup-marked gnus-newsgroup-dormant)))
9166             ;; We actually mark all articles as cancelled, which we
9167             ;; have to do when using auto-expiry or adaptive scoring. 
9168             (let ((unreads (length gnus-newsgroup-unreads)))
9169               (if (gnus-summary-first-subject (not all))
9170                   (while (and (gnus-summary-mark-as-read nil gnus-catchup-mark)
9171                               (if to-here (< (point) to-here) t)
9172                               (gnus-summary-search-subject nil (not all)))))
9173               (- unreads (length gnus-newsgroup-unreads))
9174               (or to-here
9175                   (setq gnus-newsgroup-unreads gnus-newsgroup-marked)))))
9176     (gnus-summary-position-cursor)))
9177
9178 (defun gnus-summary-catchup-to-here (&optional all)
9179   "Mark all unticked articles before the current one as read.
9180 If ALL is non-nil, also mark ticked and dormant articles as read."
9181   (interactive)
9182   (beginning-of-line)
9183   (gnus-summary-catchup all t (point))
9184   (gnus-set-mode-line 'summary)
9185   (gnus-summary-position-cursor))
9186
9187 (defun gnus-summary-catchup-all (&optional quietly)
9188   "Mark all articles in this newsgroup as read."
9189   (interactive)
9190   (gnus-summary-catchup t quietly))
9191
9192 (defun gnus-summary-catchup-and-exit (all &optional quietly)
9193   "Mark all articles not marked as unread in this newsgroup as read, then exit.
9194 If prefix argument ALL is non-nil, all articles are marked as read."
9195   (interactive "P")
9196   (gnus-summary-catchup all quietly nil 'fast)
9197   ;; Select next newsgroup or exit.
9198   (if (eq gnus-auto-select-next 'quietly)
9199       (gnus-summary-next-group nil)
9200     (gnus-summary-exit)))
9201
9202 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
9203   "Mark all articles in this newsgroup as read, and then exit."
9204   (interactive)
9205   (gnus-summary-catchup-and-exit t quietly))
9206
9207 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
9208 (defun gnus-summary-catchup-and-goto-next-group (all)
9209   "Mark all articles in this group as read and select the next group.
9210 If given a prefix, mark all articles, unread as well as ticked, as
9211 read." 
9212   (interactive "P")
9213   (gnus-summary-catchup all)
9214   (gnus-summary-next-group))
9215
9216 ;; Thread-based commands.
9217
9218 (defun gnus-summary-toggle-threads (arg)
9219   "Toggle showing conversation threads.
9220 If ARG is positive number, turn showing conversation threads on."
9221   (interactive "P")
9222   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
9223     (setq gnus-show-threads
9224           (if (null arg) (not gnus-show-threads)
9225             (> (prefix-numeric-value arg) 0)))
9226     (gnus-summary-prepare)
9227     (gnus-summary-goto-subject current)
9228     (gnus-summary-position-cursor)))
9229
9230 (defun gnus-summary-show-all-threads ()
9231   "Show all threads."
9232   (interactive)
9233   (save-excursion
9234     (let ((buffer-read-only nil))
9235       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
9236   (gnus-summary-position-cursor))
9237
9238 (defun gnus-summary-show-thread ()
9239   "Show thread subtrees.
9240 Returns nil if no thread was there to be shown."
9241   (interactive)
9242   (prog1
9243       (save-excursion
9244         (let ((buffer-read-only nil)
9245               ;; first goto end then to beg, to have point at beg after let
9246               (end (progn (end-of-line) (point)))
9247               (beg (progn (beginning-of-line) (point))))
9248           (prog1
9249               ;; Any hidden lines here?
9250               (search-forward "\r" end t)
9251             (subst-char-in-region beg end ?\^M ?\n t))))
9252     (gnus-summary-position-cursor)))
9253
9254 (defun gnus-summary-hide-all-threads ()
9255   "Hide all thread subtrees."
9256   (interactive)
9257   (save-excursion
9258     (goto-char (point-min))
9259     (gnus-summary-hide-thread)
9260     (while (and (not (eobp)) (zerop (forward-line 1)))
9261       (gnus-summary-hide-thread)))
9262   (gnus-summary-position-cursor))
9263
9264 (defun gnus-summary-hide-thread ()
9265   "Hide thread subtrees.
9266 Returns nil if no threads were there to be hidden."
9267   (interactive)
9268   (let ((buffer-read-only nil)
9269         (start (point))
9270         (level (gnus-summary-thread-level))
9271         (end (point)))
9272     ;; Go forward until either the buffer ends or the subthread
9273     ;; ends. 
9274     (if (eobp)
9275         ()
9276       (while (and (zerop (forward-line 1))
9277                   (> (gnus-summary-thread-level) level))
9278         (setq end (point)))
9279       (prog1
9280           (save-excursion
9281             (goto-char end)
9282             (search-backward "\n" start t))
9283         (subst-char-in-region start end ?\n ?\^M t)
9284         (forward-line -1)))))
9285
9286 (defun gnus-summary-go-to-next-thread (&optional previous)
9287   "Go to the same level (or less) next thread.
9288 If PREVIOUS is non-nil, go to previous thread instead.
9289 Return the article number moved to, or nil if moving was impossible."
9290   (let ((level (gnus-summary-thread-level))
9291         (article (gnus-summary-article-number)))
9292     (if previous 
9293         (while (and (zerop (gnus-summary-prev-subject 1))
9294                     (> (gnus-summary-thread-level) level)))
9295       (while (and (zerop (gnus-summary-next-subject 1))
9296                   (> (gnus-summary-thread-level) level))))
9297     (gnus-summary-recenter)
9298     (gnus-summary-position-cursor)
9299     (let ((oart (gnus-summary-article-number)))
9300       (and (/= oart article) oart))))
9301
9302 (defun gnus-summary-next-thread (n)
9303   "Go to the same level next N'th thread.
9304 If N is negative, search backward instead.
9305 Returns the difference between N and the number of skips actually
9306 done."
9307   (interactive "p")
9308   (let ((backward (< n 0))
9309         (n (abs n)))
9310   (while (and (> n 0)
9311               (gnus-summary-go-to-next-thread backward))
9312     (setq n (1- n)))
9313   (gnus-summary-position-cursor)
9314   (if (/= 0 n) (gnus-message 7 "No more threads"))
9315   n))
9316
9317 (defun gnus-summary-prev-thread (n)
9318   "Go to the same level previous N'th thread.
9319 Returns the difference between N and the number of skips actually
9320 done."
9321   (interactive "p")
9322   (gnus-summary-next-thread (- n)))
9323
9324 (defun gnus-summary-go-down-thread (&optional same)
9325   "Go down one level in the current thread.
9326 If SAME is non-nil, also move to articles of the same level."
9327   (let ((level (gnus-summary-thread-level))
9328         (start (point)))
9329     (if (and (zerop (forward-line 1))
9330              (> (gnus-summary-thread-level) level))
9331         t
9332       (goto-char start)
9333       nil)))
9334
9335 (defun gnus-summary-go-up-thread ()
9336   "Go up one level in the current thread."
9337   (let ((level (gnus-summary-thread-level))
9338         (start (point)))
9339     (while (and (zerop (forward-line -1))
9340                 (>= (gnus-summary-thread-level) level)))
9341     (if (>= (gnus-summary-thread-level) level)
9342         (progn
9343           (goto-char start)
9344           nil)
9345       t)))
9346
9347 (defun gnus-summary-down-thread (n)
9348   "Go down thread N steps.
9349 If N is negative, go up instead.
9350 Returns the difference between N and how many steps down that were
9351 taken."
9352   (interactive "p")
9353   (let ((up (< n 0))
9354         (n (abs n)))
9355   (while (and (> n 0)
9356               (if up (gnus-summary-go-up-thread)
9357                 (gnus-summary-go-down-thread)))
9358     (setq n (1- n)))
9359   (gnus-summary-position-cursor)
9360   (if (/= 0 n) (gnus-message 7 "Can't go further"))
9361   n))
9362
9363 (defun gnus-summary-up-thread (n)
9364   "Go up thread N steps.
9365 If N is negative, go up instead.
9366 Returns the difference between N and how many steps down that were
9367 taken."
9368   (interactive "p")
9369   (gnus-summary-down-thread (- n)))
9370
9371 (defun gnus-summary-kill-thread (unmark)
9372   "Mark articles under current thread as read.
9373 If the prefix argument is positive, remove any kinds of marks.
9374 If the prefix argument is negative, tick articles instead."
9375   (interactive "P")
9376   (if unmark
9377       (setq unmark (prefix-numeric-value unmark)))
9378   (let ((killing t)
9379         (level (gnus-summary-thread-level)))
9380     (save-excursion
9381       (while killing
9382         ;; Mark the article...
9383         (cond ((null unmark) (gnus-summary-mark-as-read nil gnus-killed-mark))
9384               ((> unmark 0) (gnus-summary-tick-article nil t))
9385               (t (gnus-summary-tick-article)))
9386         ;; ...and go forward until either the buffer ends or the subtree
9387         ;; ends. 
9388         (if (not (and (zerop (forward-line 1))
9389                       (> (gnus-summary-thread-level) level)))
9390             (setq killing nil))))
9391     ;; Hide killed subtrees.
9392     (and (null unmark)
9393          gnus-thread-hide-killed
9394          (gnus-summary-hide-thread))
9395     ;; If marked as read, go to next unread subject.
9396     (if (null unmark)
9397         ;; Go to next unread subject.
9398         (gnus-summary-next-subject 1 t)))
9399   (gnus-set-mode-line 'summary))
9400
9401 ;; Summary sorting commands
9402
9403 (defun gnus-summary-sort-by-number (&optional reverse)
9404   "Sort summary buffer by article number.
9405 Argument REVERSE means reverse order."
9406   (interactive "P")
9407   (gnus-summary-sort 
9408    (cons 'gnus-summary-article-number 'gnus-thread-sort-by-number) reverse))
9409
9410 (defun gnus-summary-sort-by-author (&optional reverse)
9411   "Sort summary buffer by author name alphabetically.
9412 If case-fold-search is non-nil, case of letters is ignored.
9413 Argument REVERSE means reverse order."
9414   (interactive "P")
9415   (gnus-summary-sort
9416    (cons
9417     (lambda ()
9418       (let ((extract (funcall
9419                       gnus-extract-address-components
9420                       (header-from (gnus-get-header-by-number
9421                                     (gnus-summary-article-number))))))
9422         (or (car extract) (cdr extract))))
9423     'gnus-thread-sort-by-author)
9424    reverse))
9425
9426 (defun gnus-summary-sort-by-subject (&optional reverse)
9427   "Sort summary buffer by subject alphabetically. `Re:'s are ignored.
9428 If case-fold-search is non-nil, case of letters is ignored.
9429 Argument REVERSE means reverse order."
9430   (interactive "P")
9431   (gnus-summary-sort
9432    (cons
9433     (lambda ()
9434       (downcase (gnus-simplify-subject (gnus-summary-subject-string))))
9435     'gnus-thread-sort-by-subject)
9436    reverse))
9437
9438 (defun gnus-summary-sort-by-date (&optional reverse)
9439   "Sort summary buffer by date.
9440 Argument REVERSE means reverse order."
9441   (interactive "P")
9442   (gnus-summary-sort
9443    (cons
9444     (lambda ()
9445       (gnus-sortable-date
9446        (header-date 
9447         (gnus-get-header-by-number (gnus-summary-article-number)))))
9448     'gnus-thread-sort-by-date)
9449    reverse))
9450
9451 (defun gnus-summary-sort-by-score (&optional reverse)
9452   "Sort summary buffer by score.
9453 Argument REVERSE means reverse order."
9454   (interactive "P")
9455   (gnus-summary-sort 
9456    (cons 'gnus-summary-article-score 'gnus-thread-sort-by-score)
9457    (not reverse)))
9458
9459 (defvar gnus-summary-already-sorted nil)
9460 (defun gnus-summary-sort (predicate reverse)
9461   ;; Sort summary buffer by PREDICATE.  REVERSE means reverse order. 
9462   (if gnus-summary-already-sorted
9463       ()
9464     (let (buffer-read-only)
9465       (if (not gnus-show-threads)
9466           (progn
9467             (goto-char (point-min))
9468             (sort-subr reverse 'forward-line 'end-of-line (car predicate)))
9469         (let ((gnus-thread-sort-functions (list (cdr predicate)))
9470               (gnus-summary-prepare-hook nil)
9471               (gnus-summary-already-sorted nil))
9472           (gnus-summary-prepare)
9473           (and gnus-show-threads
9474                gnus-thread-hide-subtree
9475                (gnus-summary-hide-all-threads))
9476           ;; If in async mode, we send some info to the backend.
9477           (and gnus-newsgroup-async
9478                (setq gnus-newsgroup-threads (nreverse gnus-newsgroup-threads))
9479                (gnus-request-asynchronous 
9480                 gnus-newsgroup-name
9481                 (if (and gnus-asynchronous-article-function
9482                          (fboundp gnus-asynchronous-article-function))
9483                     (funcall gnus-asynchronous-article-function
9484                              gnus-newsgroup-threads)))))))))
9485
9486   
9487 (defun gnus-sortable-date (date)
9488   "Make sortable string by string-lessp from DATE.
9489 Timezone package is used."
9490   (let* ((date   (timezone-fix-time date nil nil)) ;[Y M D H M S]
9491          (year   (aref date 0))
9492          (month  (aref date 1))
9493          (day    (aref date 2)))
9494     (timezone-make-sortable-date 
9495      year month day 
9496      (timezone-make-time-string
9497       (aref date 3) (aref date 4) (aref date 5)))))
9498
9499
9500 ;; Summary saving commands.
9501
9502 (defun gnus-summary-save-article (n)
9503   "Save the current article using the default saver function.
9504 If N is a positive number, save the N next articles.
9505 If N is a negative number, save the N previous articles.
9506 If N is nil and any articles have been marked with the process mark,
9507 save those articles instead.
9508 The variable `gnus-default-article-saver' specifies the saver function."
9509   (interactive "P")
9510   (let ((articles (gnus-summary-work-articles n)))
9511     (while articles
9512       (let ((header (gnus-get-header-by-number (car articles))))
9513         (if (vectorp header)
9514             (progn
9515               (gnus-summary-select-article t nil nil (car articles))
9516               (or gnus-save-all-headers
9517                   (gnus-article-hide-headers t))
9518               ;; Remove any X-Gnus lines.
9519               (save-excursion
9520                 (save-restriction
9521                   (set-buffer gnus-article-buffer)
9522                   (let ((buffer-read-only nil))
9523                     (goto-char (point-min))
9524                     (narrow-to-region (point) (or (search-forward "\n\n" nil t)
9525                                                   (point-max)))
9526                     (while (re-search-forward "^X-Gnus" nil t)
9527                       (beginning-of-line)
9528                       (delete-region (point)
9529                                      (progn (forward-line 1) (point))))
9530                     (widen))))
9531               (save-excursion
9532                 (if gnus-default-article-saver
9533                     (funcall gnus-default-article-saver)
9534                   (error "No default saver is defined."))))
9535           (if (assq 'name header)
9536               (gnus-copy-file (cdr (assq 'name header)))
9537             (gnus-message 1 "Article %d is unsaveable" (car articles)))))
9538       (gnus-summary-remove-process-mark (car articles))
9539       (setq articles (cdr articles)))
9540     (gnus-summary-position-cursor)
9541     n))
9542
9543 (defun gnus-summary-pipe-output (arg)
9544   "Pipe the current article to a subprocess.
9545 If N is a positive number, pipe the N next articles.
9546 If N is a negative number, pipe the N previous articles.
9547 If N is nil and any articles have been marked with the process mark,
9548 pipe those articles instead."
9549   (interactive "P")
9550   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9551     (gnus-summary-save-article arg)))
9552
9553 (defun gnus-summary-save-article-mail (arg)
9554   "Append the current article to an mail file.
9555 If N is a positive number, save the N next articles.
9556 If N is a negative number, save the N previous articles.
9557 If N is nil and any articles have been marked with the process mark,
9558 save those articles instead."
9559   (interactive "P")
9560   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9561     (gnus-summary-save-article arg)))
9562
9563 (defun gnus-summary-save-article-rmail (arg)
9564   "Append the current article to an rmail file.
9565 If N is a positive number, save the N next articles.
9566 If N is a negative number, save the N previous articles.
9567 If N is nil and any articles have been marked with the process mark,
9568 save those articles instead."
9569   (interactive "P")
9570   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9571     (gnus-summary-save-article arg)))
9572
9573 (defun gnus-summary-save-article-file (arg)
9574   "Append the current article to a file.
9575 If N is a positive number, save the N next articles.
9576 If N is a negative number, save the N previous articles.
9577 If N is nil and any articles have been marked with the process mark,
9578 save those articles instead."
9579   (interactive "P")
9580   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9581     (gnus-summary-save-article arg)))
9582
9583 (defun gnus-read-save-file-name (prompt default-name)
9584   (let ((methods gnus-split-methods)
9585         split-name)
9586     (if (not gnus-split-methods)
9587         ()
9588       (save-excursion
9589         (set-buffer gnus-article-buffer)
9590         (gnus-narrow-to-headers)
9591         (while methods
9592           (goto-char (point-min))
9593           (and (condition-case () 
9594                    (re-search-forward (car (car methods)) nil t)
9595                  (error nil))
9596                (setq split-name (cons (nth 1 (car methods)) split-name)))
9597           (setq methods (cdr methods)))
9598         (widen)))
9599     (cond ((null split-name)
9600            (read-file-name
9601             (concat prompt " (default "
9602                     (file-name-nondirectory default-name) ") ")
9603             (file-name-directory default-name)
9604             default-name))
9605           ((= 1 (length split-name))
9606            (read-file-name
9607             (concat prompt " (default " (car split-name) ") ")
9608             gnus-article-save-directory
9609             (concat gnus-article-save-directory (car split-name))))
9610           (t
9611            (setq split-name (mapcar (lambda (el) (list el))
9612                                     (nreverse split-name)))
9613            (let ((result (completing-read 
9614                           (concat prompt " ")
9615                           split-name nil nil)))
9616              (concat gnus-article-save-directory
9617                      (if (string= result "")
9618                          (car (car split-name))
9619                        result)))))))
9620
9621 (defun gnus-summary-save-in-rmail (&optional filename)
9622   "Append this article to Rmail file.
9623 Optional argument FILENAME specifies file name.
9624 Directory to save to is default to `gnus-article-save-directory' which
9625 is initialized from the SAVEDIR environment variable."
9626   (interactive)
9627   (let ((default-name
9628           (funcall gnus-rmail-save-name gnus-newsgroup-name
9629                    gnus-current-headers gnus-newsgroup-last-rmail)))
9630     (or filename
9631         (setq filename (gnus-read-save-file-name 
9632                         "Save in rmail file:" default-name)))
9633     (gnus-make-directory (file-name-directory filename))
9634     (gnus-eval-in-buffer-window 
9635      gnus-article-buffer
9636      (save-excursion
9637        (save-restriction
9638          (widen)
9639          (gnus-output-to-rmail filename))))
9640     ;; Remember the directory name to save articles.
9641     (setq gnus-newsgroup-last-rmail filename)))
9642
9643 (defun gnus-summary-save-in-mail (&optional filename)
9644   "Append this article to Unix mail file.
9645 Optional argument FILENAME specifies file name.
9646 Directory to save to is default to `gnus-article-save-directory' which
9647 is initialized from the SAVEDIR environment variable."
9648   (interactive)
9649   (let ((default-name
9650           (funcall gnus-mail-save-name gnus-newsgroup-name
9651                    gnus-current-headers gnus-newsgroup-last-mail)))
9652     (or filename
9653         (setq filename (gnus-read-save-file-name 
9654                         "Save in Unix mail file:" default-name)))
9655     (setq filename
9656           (expand-file-name filename
9657                             (and default-name
9658                                  (file-name-directory default-name))))
9659     (gnus-make-directory (file-name-directory filename))
9660     (gnus-eval-in-buffer-window 
9661      gnus-article-buffer
9662      (save-excursion
9663        (save-restriction
9664          (widen)
9665          (if (and (file-readable-p filename) (rmail-file-p filename))
9666              (gnus-output-to-rmail filename)
9667            (rmail-output filename 1 t t)))))
9668     ;; Remember the directory name to save articles.
9669     (setq gnus-newsgroup-last-mail filename)))
9670
9671 (defun gnus-summary-save-in-file (&optional filename)
9672   "Append this article to file.
9673 Optional argument FILENAME specifies file name.
9674 Directory to save to is default to `gnus-article-save-directory' which
9675 is initialized from the SAVEDIR environment variable."
9676   (interactive)
9677   (let ((default-name
9678           (funcall gnus-file-save-name gnus-newsgroup-name
9679                    gnus-current-headers gnus-newsgroup-last-file)))
9680     (or filename
9681         (setq filename (gnus-read-save-file-name 
9682                         "Save in file:" default-name)))
9683     (gnus-make-directory (file-name-directory filename))
9684     (gnus-eval-in-buffer-window 
9685      gnus-article-buffer
9686      (save-excursion
9687        (save-restriction
9688          (widen)
9689          (gnus-output-to-file filename))))
9690     ;; Remember the directory name to save articles.
9691     (setq gnus-newsgroup-last-file filename)))
9692
9693 (defun gnus-summary-save-in-pipe (&optional command)
9694   "Pipe this article to subprocess."
9695   (interactive)
9696   (let ((command (read-string "Shell command on article: "
9697                               gnus-last-shell-command)))
9698     (if (string-equal command "")
9699         (setq command gnus-last-shell-command))
9700     (gnus-eval-in-buffer-window 
9701      gnus-article-buffer
9702      (save-restriction
9703        (widen)
9704        (shell-command-on-region (point-min) (point-max) command nil)))
9705     (setq gnus-last-shell-command command)))
9706
9707 ;; Summary extract commands
9708
9709 (defun gnus-summary-insert-pseudos (pslist)
9710   (let ((buffer-read-only nil)
9711         (article (gnus-summary-article-number))
9712         b)
9713     (or (gnus-summary-goto-subject article)
9714         (error (format "No such article: %d" article)))
9715     (or gnus-newsgroup-headers-hashtb-by-number
9716         (gnus-make-headers-hashtable-by-number))
9717     (gnus-summary-position-cursor)
9718     ;; If all commands are to be bunched up on one line, we collect
9719     ;; them here.  
9720     (if gnus-view-pseudos-separately
9721         ()
9722       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9723             files action)
9724         (while ps
9725           (setq action (cdr (assq 'action (car ps))))
9726           (setq files (list (cdr (assq 'name (car ps)))))
9727           (while (and ps (cdr ps)
9728                       (string= (or action "1")
9729                                (or (cdr (assq 'action (car (cdr ps)))) "2")))
9730             (setq files (cons (cdr (assq 'name (car (cdr ps)))) files))
9731             (setcdr ps (cdr (cdr ps))))
9732           (if (not files)
9733               ()
9734             (if (not (string-match "%s" action))
9735                 (setq files (cons " " files)))
9736             (setq files (cons " " files))
9737             (and (assq 'execute (car ps))
9738                  (setcdr (assq 'execute (car ps))
9739                          (funcall (if (string-match "%s" action)
9740                                       'format 'concat)
9741                                   action 
9742                                   (mapconcat (lambda (f) f) files " ")))))
9743           (setq ps (cdr ps)))))
9744     (if gnus-view-pseudos
9745         (while pslist
9746           (and (assq 'execute (car pslist))
9747                (gnus-execute-command (cdr (assq 'execute (car pslist)))
9748                                      (eq gnus-view-pseudos 'not-confirm)))
9749           (setq pslist (cdr pslist)))
9750       (save-excursion
9751         (while pslist
9752           (gnus-summary-goto-subject (or (cdr (assq 'article (car pslist)))
9753                                          (gnus-summary-article-number)))
9754           (forward-line 1)
9755           (setq b (point))
9756           (insert "          " (file-name-nondirectory 
9757                                 (cdr (assq 'name (car pslist))))
9758                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9759           (add-text-properties 
9760            b (1+ b) (list 'gnus-number gnus-reffed-article-number
9761                           'gnus-mark gnus-unread-mark 
9762                           'gnus-level 0
9763                           'gnus-pseudo (car pslist)))
9764           (forward-line -1)
9765           (gnus-sethash (int-to-string gnus-reffed-article-number)
9766                         (car pslist) gnus-newsgroup-headers-hashtb-by-number)
9767           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9768           (setq pslist (cdr pslist)))))))
9769
9770 (defun gnus-pseudos< (p1 p2)
9771   (let ((c1 (cdr (assq 'action p1)))
9772         (c2 (cdr (assq 'action p2))))
9773     (and c1 c2 (string< c1 c2))))
9774
9775 (defun gnus-request-pseudo-article (props)
9776   (cond ((assq 'execute props)
9777          (gnus-execute-command (cdr (assq 'execute props)))))
9778   (let ((gnus-current-article (gnus-summary-article-number)))
9779     (run-hooks 'gnus-mark-article-hook)))
9780
9781 (defun gnus-execute-command (command &optional automatic)
9782   (save-excursion
9783     (gnus-article-setup-buffer)
9784     (set-buffer gnus-article-buffer)
9785     (let ((command (if automatic command (read-string "Command: " command)))
9786           (buffer-read-only nil))
9787       (erase-buffer)
9788       (insert "$ " command "\n\n")
9789       (if gnus-view-pseudo-asynchronously
9790           (start-process "gnus-execute" nil "sh" "-c" command)
9791         (call-process "sh" nil t nil "-c" command)))))
9792
9793 (defun gnus-copy-file (file &optional to)
9794   "Copy FILE to TO."
9795   (interactive
9796    (list (read-file-name "Copy file: " default-directory)
9797          (read-file-name "Copy file to: " default-directory)))
9798   (or to (setq to (read-file-name "Copy file to: " default-directory)))
9799   (and (file-directory-p to) 
9800        (setq to (concat (file-name-as-directory to)
9801                         (file-name-nondirectory file))))
9802   (copy-file file to))
9803
9804 ;; Summary kill commands.
9805
9806 (defun gnus-summary-edit-global-kill (article)
9807   "Edit the \"global\" kill file."
9808   (interactive (list (gnus-summary-article-number)))
9809   (gnus-group-edit-global-kill article))
9810
9811 (defun gnus-summary-edit-local-kill ()
9812   "Edit a local kill file applied to the current newsgroup."
9813   (interactive)
9814   (setq gnus-current-headers 
9815         (gnus-gethash 
9816          (int-to-string (gnus-summary-article-number))
9817          gnus-newsgroup-headers-hashtb-by-number))
9818   (gnus-set-global-variables)
9819   (gnus-group-edit-local-kill 
9820    (gnus-summary-article-number) gnus-newsgroup-name))
9821
9822 \f
9823 ;;;
9824 ;;; Gnus article mode
9825 ;;;
9826
9827 (put 'gnus-article-mode 'mode-class 'special)
9828
9829 (if gnus-article-mode-map
9830     nil
9831   (setq gnus-article-mode-map (make-keymap))
9832   (suppress-keymap gnus-article-mode-map)
9833   (define-key gnus-article-mode-map " " 'gnus-article-next-page)
9834   (define-key gnus-article-mode-map "\177" 'gnus-article-prev-page)
9835   (define-key gnus-article-mode-map "\C-c^" 'gnus-article-refer-article)
9836   (define-key gnus-article-mode-map "h" 'gnus-article-show-summary)
9837   (define-key gnus-article-mode-map "s" 'gnus-article-show-summary)
9838   (define-key gnus-article-mode-map "\C-c\C-m" 'gnus-article-mail)
9839   (define-key gnus-article-mode-map "\C-c\C-M" 'gnus-article-mail-with-original)
9840   (define-key gnus-article-mode-map "?" 'gnus-article-describe-briefly)
9841   (define-key gnus-article-mode-map gnus-mouse-2 'gnus-article-push-button)
9842   (define-key gnus-article-mode-map "\r" 'gnus-article-press-button)
9843   (define-key gnus-article-mode-map "\t" 'gnus-article-next-button)
9844   
9845   ;; Duplicate almost all summary keystrokes in the article mode map.
9846   (let ((commands 
9847          (list "#" "\M-#" "\C-c\M-#" "n" "p"
9848                "N" "P" "\M-\C-n" "\M-\C-p" "." "\M-s" "\M-r"
9849                "<" ">" "l" "j" "^" "\M-^" "-" "u" "U" "d" "D"
9850                "\M-u" "\M-U" "k" "\C-k" "\M-\C-k""x" "X" 
9851                "\M-\C-x" "\M-\177" "b" "B" "$" "w" "\C-c\C-r"
9852                "t" "\M-t" "C" "S"
9853                "m" "o" "\C-o" "|" "\M-m" "\M-\C-m" "\M-k" "M"
9854                "V" "\C-c\C-d")))
9855     (while commands
9856       (define-key gnus-article-mode-map (car commands) 
9857         'gnus-article-summary-command)
9858       (setq commands (cdr commands))))
9859
9860   (let ((commands (list "q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F")))
9861     (while commands
9862       (define-key gnus-article-mode-map (car commands) 
9863         'gnus-article-summary-command-nosave)
9864       (setq commands (cdr commands)))))
9865
9866
9867 (defun gnus-article-mode ()
9868   "Major mode for displaying an article.
9869
9870 All normal editing commands are switched off.
9871
9872 The following commands are available:
9873
9874 \\<gnus-article-mode-map>
9875 \\[gnus-article-next-page]\t Scroll the article one page forwards
9876 \\[gnus-article-prev-page]\t Scroll the article one page backwards
9877 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
9878 \\[gnus-article-show-summary]\t Display the summary buffer
9879 \\[gnus-article-mail]\t Send a reply to the address near point
9880 \\[gnus-article-mail-with-original]\t Send a reply to the address near point; include the original article
9881 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
9882 \\[gnus-info-find-node]\t Go to the Gnus info node"
9883   (interactive)
9884   (if gnus-visual (gnus-article-make-menu-bar))
9885   (kill-all-local-variables)
9886   (setq mode-line-modified "-- ")
9887   (make-local-variable 'mode-line-format)
9888   (setq mode-line-format (copy-sequence mode-line-format))
9889   (and (equal (nth 3 mode-line-format) "   ")
9890        (setcar (nthcdr 3 mode-line-format) ""))
9891   (setq mode-name "Article")
9892   (setq major-mode 'gnus-article-mode)
9893   (make-local-variable 'minor-mode-alist)
9894   (or (assq 'gnus-show-mime minor-mode-alist)
9895       (setq minor-mode-alist
9896             (cons (list 'gnus-show-mime " MIME") minor-mode-alist)))
9897   (use-local-map gnus-article-mode-map)
9898   (make-local-variable 'page-delimiter)
9899   (setq page-delimiter gnus-page-delimiter)
9900   (buffer-disable-undo (current-buffer))
9901   (setq buffer-read-only t)             ;Disable modification
9902   (run-hooks 'gnus-article-mode-hook))
9903
9904 (defun gnus-article-setup-buffer ()
9905   "Initialize article mode buffer."
9906   (if (get-buffer gnus-article-buffer)
9907       (save-excursion
9908         (set-buffer gnus-article-buffer)
9909         (gnus-add-current-to-buffer-list)
9910         (or (eq major-mode 'gnus-article-mode)
9911             (gnus-article-mode)))
9912     (save-excursion
9913       (set-buffer (get-buffer-create gnus-article-buffer))
9914       (gnus-add-current-to-buffer-list)
9915       (gnus-article-mode))))
9916
9917 ;; Set article window start at LINE, where LINE is the number of lines
9918 ;; from the head of the article.
9919 (defun gnus-article-set-window-start (&optional line)
9920   (set-window-start 
9921    (get-buffer-window gnus-article-buffer)
9922    (save-excursion
9923      (set-buffer gnus-article-buffer)
9924      (goto-char (point-min))
9925      (if (not line)
9926          (point-min)
9927        (gnus-message 6 "Moved to bookmark")
9928        (search-forward "\n\n" nil t)
9929        (forward-line line)
9930        (point)))))
9931
9932 (defun gnus-request-article-this-buffer (article group)
9933   "Get an article and insert it into this buffer."
9934   (setq group (or group gnus-newsgroup-name))
9935
9936   ;; Open server if it has closed.
9937   (gnus-check-news-server (gnus-find-method-for-group group))
9938
9939   ;; Using `gnus-request-article' directly will insert the article into
9940   ;; `nntp-server-buffer' - so we'll save some time by not having to
9941   ;; copy it from the server buffer into the article buffer.
9942
9943   ;; We only request an article by message-id when we do not have the
9944   ;; headers for it, so we'll have to get those.
9945   (and (stringp article) 
9946        (let ((gnus-override-method gnus-refer-article-method))
9947          (gnus-read-header article)))
9948
9949   ;; If the article number is negative, that means that this article
9950   ;; doesn't belong in this newsgroup (possibly), so we find its
9951   ;; message-id and request it by id instead of number.
9952   (if (not (numberp article))
9953       ()
9954     (save-excursion
9955       (set-buffer gnus-summary-buffer)
9956       (let ((header (gnus-get-header-by-number article)))
9957         (if (< article 0)
9958             (if (vectorp header)
9959                 ;; It's a real article.
9960                 (setq article (header-id header))
9961               ;; It is an extracted pseudo-article.
9962               (setq article 'pseudo)
9963               (gnus-request-pseudo-article header)))
9964
9965         (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
9966           (if (not (eq (car method) 'nneething))
9967               ()
9968             (let ((dir (concat (file-name-as-directory (nth 1 method))
9969                                (header-subject header))))
9970               (if (file-directory-p dir)
9971                   (progn
9972                     (setq article 'nneething)
9973                     (gnus-group-enter-directory dir)))))))))
9974
9975   ;; Check the cache.
9976   (if (and gnus-use-cache
9977            (numberp article)
9978            (gnus-cache-request-article article group))
9979       'article
9980     ;; Get the article and into the article buffer.
9981     (if (or (stringp article) (numberp article))
9982         (progn
9983           (erase-buffer)
9984           (let ((gnus-override-method 
9985                  (and (stringp article) gnus-refer-article-method)))
9986             (and (gnus-request-article article group (current-buffer))
9987                  'article)))
9988       article)))
9989
9990 (defun gnus-read-header (id)
9991   "Read the headers of article ID and enter them into the Gnus system."
9992   (or gnus-newsgroup-headers-hashtb-by-number
9993       (gnus-make-headers-hashtable-by-number))
9994   (let (header)
9995     (if (not (setq header 
9996                    (car (if (let ((gnus-nov-is-evil t))
9997                               (gnus-retrieve-headers 
9998                                (list id) gnus-newsgroup-name))
9999                             (gnus-get-newsgroup-headers)))))
10000         nil
10001       (if (stringp id)
10002           (header-set-number header gnus-reffed-article-number))
10003       (setq gnus-newsgroup-headers (cons header gnus-newsgroup-headers))
10004       (gnus-sethash (int-to-string (header-number header)) header
10005                     gnus-newsgroup-headers-hashtb-by-number)
10006       (if (stringp id)
10007           (setq gnus-reffed-article-number (1- gnus-reffed-article-number)))
10008       (setq gnus-current-headers header)
10009       header)))
10010
10011 (defun gnus-article-prepare (article &optional all-headers header)
10012   "Prepare ARTICLE in article mode buffer.
10013 ARTICLE should either be an article number or a Message-ID.
10014 If ARTICLE is an id, HEADER should be the article headers.
10015 If ALL-HEADERS is non-nil, no headers are hidden."
10016   (save-excursion
10017     ;; Make sure we start in a summary buffer.
10018     (or (eq major-mode 'gnus-summary-mode)
10019         (set-buffer gnus-summary-buffer))
10020     (setq gnus-summary-buffer (current-buffer))
10021     ;; Make sure the connection to the server is alive.
10022     (or (gnus-server-opened (gnus-find-method-for-group gnus-newsgroup-name))
10023         (progn
10024           (gnus-check-news-server 
10025            (gnus-find-method-for-group gnus-newsgroup-name))
10026           (gnus-request-group gnus-newsgroup-name t)))
10027     (or gnus-newsgroup-headers-hashtb-by-number
10028         (gnus-make-headers-hashtable-by-number))
10029     (let* ((article (if header (header-number header) article))
10030            (summary-buffer (current-buffer))
10031            (internal-hook gnus-article-internal-prepare-hook)
10032            (group gnus-newsgroup-name)
10033            result)
10034       (save-excursion
10035         (gnus-article-setup-buffer)
10036         (set-buffer gnus-article-buffer)
10037         (if (not (setq result (let ((buffer-read-only nil))
10038                                 (gnus-request-article-this-buffer 
10039                                  article group))))
10040             ;; There is no such article.
10041             (progn
10042               (save-excursion
10043                 (set-buffer gnus-summary-buffer)
10044                 (setq gnus-current-article 0)
10045                 (and (numberp article) 
10046                      (gnus-summary-mark-as-read article gnus-canceled-mark))
10047                 (gnus-message 1 "No such article (may be canceled)")
10048                 (and (numberp article)
10049                      (setq gnus-current-article article))
10050                 (ding))
10051               (and (numberp article)
10052                    (setq gnus-article-current 
10053                          (cons gnus-newsgroup-name article)))
10054               nil)
10055           (if (or (eq result 'pseudo) (eq result 'nneething))
10056               (progn
10057                 (save-excursion
10058                   (set-buffer summary-buffer)
10059                   (setq gnus-last-article gnus-current-article
10060                         gnus-newsgroup-history (cons gnus-current-article
10061                                                      gnus-newsgroup-history)
10062                         gnus-current-article 0
10063                         gnus-current-headers nil
10064                         gnus-article-current nil)
10065                   (if (eq result 'nneething)
10066                       (gnus-configure-windows 'summary)
10067                     (gnus-configure-windows 'article))
10068                   (gnus-set-global-variables))
10069                 (gnus-set-mode-line 'article))
10070             ;; The result from the `request' was an actual article -
10071             ;; or at least some text that is now displayed in the
10072             ;; article buffer.
10073             (if (and (numberp article)
10074                      (not (eq article gnus-current-article)))
10075                 ;; Seems like a new article has been selected.
10076                 ;; `gnus-current-article' must be an article number.
10077                 (save-excursion
10078                   (set-buffer summary-buffer)
10079                   (setq gnus-last-article gnus-current-article
10080                         gnus-newsgroup-history (cons gnus-current-article
10081                                                      gnus-newsgroup-history)
10082                         gnus-current-article article
10083                         gnus-current-headers 
10084                         (gnus-get-header-by-number gnus-current-article)
10085                         gnus-article-current 
10086                         (cons gnus-newsgroup-name gnus-current-article))
10087                   (gnus-summary-show-thread)
10088                   (run-hooks 'gnus-mark-article-hook)
10089                   (gnus-set-mode-line 'summary)
10090                   (and gnus-visual 
10091                        (run-hooks 'gnus-visual-mark-article-hook))
10092                   ;; Set the global newsgroup variables here.
10093                   ;; Suggested by Jim Sisolak
10094                   ;; <sisolak@trans4.neep.wisc.edu>.
10095                   (gnus-set-global-variables)
10096                   (and gnus-use-cache 
10097                        (gnus-cache-possibly-enter-article
10098                         group article
10099                         (gnus-get-header-by-number article)
10100                         (memq article gnus-newsgroup-marked)
10101                         (memq article gnus-newsgroup-dormant)
10102                         (memq article gnus-newsgroup-unreads)))))
10103             ;; gnus-have-all-headers must be either T or NIL.
10104             (setq gnus-have-all-headers
10105                   (not (not (or all-headers gnus-show-all-headers))))
10106             ;; Hooks for getting information from the article.
10107             ;; This hook must be called before being narrowed.
10108             (let (buffer-read-only)
10109               (run-hooks 'internal-hook)
10110               (run-hooks 'gnus-article-prepare-hook)
10111               ;; Decode MIME message.
10112               (if (and gnus-show-mime
10113                        (or (not gnus-strict-mime)
10114                            (gnus-fetch-field "Mime-Version")))
10115                   (funcall gnus-show-mime-method))
10116               ;; Perform the article display hooks.
10117               (run-hooks 'gnus-article-display-hook))
10118             ;; Do page break.
10119             (goto-char (point-min))
10120             (and gnus-break-pages (gnus-narrow-to-page))
10121             (gnus-set-mode-line 'article)
10122             (gnus-configure-windows 'article)
10123             (goto-char (point-min))
10124             t))))))
10125
10126 (defun gnus-article-show-all-headers ()
10127   "Show all article headers in article mode buffer."
10128   (save-excursion 
10129     (gnus-article-setup-buffer)
10130     (set-buffer gnus-article-buffer)
10131     (let ((buffer-read-only nil))
10132       (remove-text-properties (point-min) (point-max) 
10133                               gnus-hidden-properties))))
10134
10135 (defun gnus-article-hide-headers-if-wanted ()
10136   "Hide unwanted headers if `gnus-have-all-headers' is nil.
10137 Provided for backwards compatability."
10138   (or gnus-have-all-headers
10139       (gnus-article-hide-headers)))
10140
10141 (defun gnus-article-hide-headers (&optional delete)
10142   "Hide unwanted headers and possibly sort them as well."
10143   (interactive "P")
10144   (save-excursion
10145     (set-buffer gnus-article-buffer)
10146     (save-restriction
10147       (let ((sorted gnus-sorted-header-list)
10148             (buffer-read-only nil)
10149             want want-list beg want-l)
10150         ;; First we narrow to just the headers.
10151         (widen)
10152         (goto-char (point-min))
10153         ;; Hide any "From " lines at the beginning of (mail) articles. 
10154         (while (looking-at "From ")
10155           (forward-line 1))
10156         (if (bobp) 
10157             (add-text-properties (point-min) (point) gnus-hidden-properties))
10158         ;; Then treat the rest of the header lines.
10159         (narrow-to-region 
10160          (point) 
10161          (progn (search-forward "\n\n" nil t) (forward-line -1) (point)))
10162         ;; Then we use the two regular expressions
10163         ;; `gnus-ignored-headers' and `gnus-visible-headers' to
10164         ;; select which header lines is to remain visible in the
10165         ;; article buffer.
10166         (goto-char (point-min))
10167         (while (re-search-forward "^[^ \t]*:" nil t)
10168           (beginning-of-line)
10169           ;; We add the headers we want to keep to a list and delete
10170           ;; them from the buffer.
10171           (if (or (and (stringp gnus-visible-headers)
10172                        (looking-at gnus-visible-headers))
10173                   (and (not (stringp gnus-visible-headers))
10174                        (stringp gnus-ignored-headers)
10175                        (not (looking-at gnus-ignored-headers))))
10176               (progn
10177                 (setq beg (point))
10178                 (forward-line 1)
10179                 ;; Be sure to get multi-line headers...
10180                 (re-search-forward "^[^ \t]*:" nil t)
10181                 (beginning-of-line)
10182                 (setq want-list 
10183                       (cons (buffer-substring beg (point)) want-list))
10184                 (delete-region beg (point))
10185                 (goto-char beg))
10186             (forward-line 1)))
10187         ;; Next we perform the sorting by looking at
10188         ;; `gnus-sorted-header-list'. 
10189         (goto-char (point-min))
10190         (while (and sorted want-list)
10191           (setq want-l want-list)
10192           (while (and want-l
10193                       (not (string-match (car sorted) (car want-l))))
10194             (setq want-l (cdr want-l)))
10195           (if want-l 
10196               (progn
10197                 (insert (car want-l))
10198                 (setq want-list (delq (car want-l) want-list))))
10199           (setq sorted (cdr sorted)))
10200         ;; Any headers that were not matched by the sorted list we
10201         ;; just tack on the end of the visible header list.
10202         (while want-list
10203           (insert (car want-list))
10204           (setq want-list (cdr want-list)))
10205         ;; And finally we make the unwanted headers invisible.
10206         (if delete
10207             (delete-region (point) (point-max))
10208           ;; Suggested by Sudish Joseph <joseph@cis.ohio-state.edu>.
10209           (add-text-properties (point) (point-max) gnus-hidden-properties))))))
10210
10211 ;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.
10212 (defun gnus-article-treat-overstrike ()
10213   "Translate overstrikes into bold text."
10214   (interactive)
10215   (save-excursion
10216     (set-buffer gnus-article-buffer)
10217     (let ((buffer-read-only nil))
10218       (while (search-forward "\b" nil t)
10219         (let ((next (following-char))
10220               (previous (char-after (- (point) 2))))
10221           (cond ((eq next previous)
10222                  (delete-region (- (point) 2) (point))
10223                  (put-text-property (point) (1+ (point))
10224                                     'face 'bold))
10225                 ((eq next ?_)
10226                  (delete-region (1- (point)) (1+ (point)))
10227                  (put-text-property (1- (point)) (point)
10228                                     'face 'underline))
10229                 ((eq previous ?_)
10230                  (delete-region (- (point) 2) (point))
10231                  (put-text-property (point) (1+ (point))
10232                                     'face 'underline))))))))
10233
10234 (defun gnus-article-word-wrap ()
10235   "Format too long lines."
10236   (interactive)
10237   (save-excursion
10238     (set-buffer gnus-article-buffer)
10239     (let ((buffer-read-only nil))
10240       (goto-char (point-min))
10241       (search-forward "\n\n" nil t)
10242       (end-of-line 1)
10243       (let ((paragraph-start "^\\W"))
10244         (while (not (eobp))
10245           (and (>= (current-column) (min fill-column (window-width)))
10246                (/= (preceding-char) ?:)
10247                (fill-paragraph nil))
10248           (end-of-line 2))))))
10249
10250 (defun gnus-article-remove-cr ()
10251   "Remove carriage returns from an article."
10252   (interactive)
10253   (save-excursion
10254     (set-buffer gnus-article-buffer)
10255     (let ((buffer-read-only nil))
10256       (goto-char (point-min))
10257       (while (search-forward "\r" nil t)
10258         (replace-match "" t t)))))
10259
10260 (defun gnus-article-display-x-face (&optional force)
10261   "Look for an X-Face header and display it if present."
10262   (interactive (list 'force))
10263   (save-excursion
10264     (set-buffer gnus-article-buffer)
10265     (let ((inhibit-point-motion-hooks t)
10266           (case-fold-search nil))
10267       (save-restriction
10268         (goto-char (point-min))
10269         (search-forward "\n\n")
10270         (narrow-to-region (point-min) (point))
10271         (goto-char (point-min))
10272         (if (or (not gnus-article-x-face-command)
10273                 (and (not force)
10274                      (or (not gnus-article-x-face-too-ugly)
10275                          (string-match gnus-article-x-face-too-ugly
10276                                        (mail-fetch-field "from"))))
10277                 (progn
10278                   (goto-char (point-min))
10279                   (not (re-search-forward "^X-Face: " nil t))))
10280             nil
10281           (let ((beg (point))
10282                 (end (1- (re-search-forward "^\\($\\|[^ \t]\\)" nil t))))
10283             (if (symbolp gnus-article-x-face-command)
10284                 (and (or (fboundp gnus-article-x-face-command)
10285                          (error "%s is not a function"
10286                                 gnus-article-x-face-command))
10287                      (funcall gnus-article-x-face-command beg end))
10288               (call-process-region beg end "sh" nil 0 nil
10289                                    "-c" gnus-article-x-face-command))))))))
10290
10291 (defun gnus-article-de-quoted-unreadable (&optional force)
10292   "Do a naïve translation of a quoted-printable-encoded article.
10293 This is in no way, shape or form meant as a replacement for real MIME
10294 processing, but is simply a stop-gap measure until MIME support is
10295 written.
10296 If FORCE, decode the article whether it is marked as quoted-printable
10297 or not." 
10298   (interactive (list 'force))
10299   (save-excursion
10300     (set-buffer gnus-article-buffer)
10301     (let ((case-fold-search t)
10302           (buffer-read-only nil)
10303           (type (gnus-fetch-field "content-transfer-encoding")))
10304       (if (or force (and type (string-match "quoted-printable" type)))
10305           (progn
10306             (goto-char (point-min))
10307             (search-forward "\n\n" nil 'move)
10308             (gnus-mime-decode-quoted-printable (point) (point-max)))))))
10309
10310 (defun gnus-mime-decode-quoted-printable (from to)
10311   ;; Decode quoted-printable from region between FROM and TO.
10312   (save-excursion
10313     (goto-char from)
10314     (while (search-forward "=" to t)
10315       (cond ((eq (following-char) ?\n)
10316              (delete-char -1)
10317              (delete-char 1))
10318             ((looking-at "[0-9A-F][0-9A-F]")
10319              (delete-char -1)
10320              (insert (hexl-hex-string-to-integer
10321                       (buffer-substring (point) (+ 2 (point)))))
10322              (delete-char 2))
10323             ((gnus-message 3 "Malformed MIME quoted-printable message"))))))
10324
10325 (defvar gnus-article-time-units
10326   (list (cons 'year (* 365.25 24 60 60))
10327         (cons 'week (* 7 24 60 60))
10328         (cons 'day (* 24 60 60))
10329         (cons 'hour (* 60 60))
10330         (cons 'minute 60)
10331         (cons 'second 1)))
10332
10333 (defun gnus-article-date-ut (&optional type)
10334   "Convert DATE date to universal time in the current article.
10335 If TYPE is `local', convert to local time; if it is `lapsed', output
10336 how much time has lapsed since DATE."
10337   (interactive (list 'ut))
10338   (let ((date (header-date (or gnus-current-headers 
10339                                (gnus-get-header-by-number
10340                                 (gnus-summary-article-number))"")))
10341         (date-regexp "^Date: \\|^X-Sent: "))
10342     (if (or (not date)
10343             (string= date ""))
10344         ()
10345       (save-excursion
10346         (set-buffer gnus-article-buffer)
10347         (let ((buffer-read-only nil))
10348           (goto-char (point-min))
10349           (if (and (re-search-forward date-regexp nil t)
10350                    (progn 
10351                      (beginning-of-line)
10352                      (looking-at date-regexp)))
10353               (delete-region (gnus-point-at-bol)
10354                              (progn (end-of-line) (1+ (point))))
10355             (goto-char (point-min))
10356             (goto-char (- (search-forward "\n\n") 2)))
10357           (insert
10358            (cond 
10359             ((eq type 'local)
10360              (concat "Date: " (timezone-make-date-arpa-standard date) "\n"))
10361             ((eq type 'ut)
10362              (concat "Date: " (timezone-make-date-arpa-standard date nil "UT")
10363                      "\n"))
10364             ((eq type 'lapsed)
10365              (let* ((sec (max (- (gnus-seconds-since-epoch 
10366                                   (timezone-make-date-arpa-standard
10367                                    (current-time-string) 
10368                                    (current-time-zone) "UT"))
10369                                  (gnus-seconds-since-epoch 
10370                                   (timezone-make-date-arpa-standard 
10371                                    date nil "UT")))
10372                               0))
10373                     num prev)
10374                (concat
10375                 "X-Sent: "
10376                 (mapconcat 
10377                  (lambda (unit)
10378                    (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
10379                        ""
10380                      (setq sec (- sec (* num (cdr unit))))
10381                      (prog1
10382                          (concat (if prev ", " "") (int-to-string (floor num))
10383                                  " " (symbol-name (car unit))
10384                                  (if (> num 1) "s" ""))
10385                        (setq prev t))))
10386                  gnus-article-time-units "")
10387                 " ago\n")))
10388             (t
10389              (error "Unknown conversion type: %s" type)))))))))
10390
10391 (defun gnus-article-date-local ()
10392   "Convert the current article date to the local timezone."
10393   (interactive)
10394   (gnus-article-date-ut 'local))
10395
10396 (defun gnus-article-date-lapsed ()
10397   "Convert the current article date to time lapsed since it was sent."
10398   (interactive)
10399   (gnus-article-date-ut 'lapsed))
10400
10401 (defun gnus-article-maybe-highlight ()
10402   (if gnus-visual (gnus-article-highlight)))
10403
10404 ;; Article savers.
10405
10406 (defun gnus-output-to-rmail (file-name)
10407   "Append the current article to an Rmail file named FILE-NAME."
10408   (require 'rmail)
10409   ;; Most of these codes are borrowed from rmailout.el.
10410   (setq file-name (expand-file-name file-name))
10411   (setq rmail-default-rmail-file file-name)
10412   (let ((artbuf (current-buffer))
10413         (tmpbuf (get-buffer-create " *Gnus-output*")))
10414     (save-excursion
10415       (or (get-file-buffer file-name)
10416           (file-exists-p file-name)
10417           (if (gnus-yes-or-no-p
10418                (concat "\"" file-name "\" does not exist, create it? "))
10419               (let ((file-buffer (create-file-buffer file-name)))
10420                 (save-excursion
10421                   (set-buffer file-buffer)
10422                   (rmail-insert-rmail-file-header)
10423                   (let ((require-final-newline nil))
10424                     (write-region (point-min) (point-max) file-name t 1)))
10425                 (kill-buffer file-buffer))
10426             (error "Output file does not exist")))
10427       (set-buffer tmpbuf)
10428       (buffer-disable-undo (current-buffer))
10429       (erase-buffer)
10430       (insert-buffer-substring artbuf)
10431       (gnus-convert-article-to-rmail)
10432       ;; Decide whether to append to a file or to an Emacs buffer.
10433       (let ((outbuf (get-file-buffer file-name)))
10434         (if (not outbuf)
10435             (append-to-file (point-min) (point-max) file-name)
10436           ;; File has been visited, in buffer OUTBUF.
10437           (set-buffer outbuf)
10438           (let ((buffer-read-only nil)
10439                 (msg (and (boundp 'rmail-current-message)
10440                           (symbol-value 'rmail-current-message))))
10441             ;; If MSG is non-nil, buffer is in RMAIL mode.
10442             (if msg
10443                 (progn (widen)
10444                        (narrow-to-region (point-max) (point-max))))
10445             (insert-buffer-substring tmpbuf)
10446             (if msg
10447                 (progn
10448                   (goto-char (point-min))
10449                   (widen)
10450                   (search-backward "\^_")
10451                   (narrow-to-region (point) (point-max))
10452                   (goto-char (1+ (point-min)))
10453                   (rmail-count-new-messages t)
10454                   (rmail-show-message msg)))))))
10455     (kill-buffer tmpbuf)))
10456
10457 (defun gnus-output-to-file (file-name)
10458   "Append the current article to a file named FILE-NAME."
10459   (setq file-name (expand-file-name file-name))
10460   (let ((artbuf (current-buffer))
10461         (tmpbuf (get-buffer-create " *Gnus-output*")))
10462     (save-excursion
10463       (set-buffer tmpbuf)
10464       (buffer-disable-undo (current-buffer))
10465       (erase-buffer)
10466       (insert-buffer-substring artbuf)
10467       ;; Append newline at end of the buffer as separator, and then
10468       ;; save it to file.
10469       (goto-char (point-max))
10470       (insert "\n")
10471       (append-to-file (point-min) (point-max) file-name))
10472     (kill-buffer tmpbuf)))
10473
10474 (defun gnus-convert-article-to-rmail ()
10475   "Convert article in current buffer to Rmail message format."
10476   (let ((buffer-read-only nil))
10477     ;; Convert article directly into Babyl format.
10478     ;; Suggested by Rob Austein <sra@lcs.mit.edu>
10479     (goto-char (point-min))
10480     (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
10481     (while (search-forward "\n\^_" nil t) ;single char
10482       (replace-match "\n^_" t t))               ;2 chars: "^" and "_"
10483     (goto-char (point-max))
10484     (insert "\^_")))
10485
10486 (defun gnus-narrow-to-page (&optional arg)
10487   "Make text outside current page invisible except for page delimiter.
10488 A numeric arg specifies to move forward or backward by that many pages,
10489 thus showing a page other than the one point was originally in."
10490   (interactive "P")
10491   (setq arg (if arg (prefix-numeric-value arg) 0))
10492   (save-excursion
10493     (forward-page -1)                   ;Beginning of current page.
10494     (widen)
10495     (if (> arg 0)
10496         (forward-page arg)
10497       (if (< arg 0)
10498           (forward-page (1- arg))))
10499     ;; Find the end of the page.
10500     (forward-page)
10501     ;; If we stopped due to end of buffer, stay there.
10502     ;; If we stopped after a page delimiter, put end of restriction
10503     ;; at the beginning of that line.
10504     ;; These are commented out.
10505     ;;    (if (save-excursion (beginning-of-line)
10506     ;;                  (looking-at page-delimiter))
10507     ;;  (beginning-of-line))
10508     (narrow-to-region (point)
10509                       (progn
10510                         ;; Find the top of the page.
10511                         (forward-page -1)
10512                         ;; If we found beginning of buffer, stay there.
10513                         ;; If extra text follows page delimiter on same line,
10514                         ;; include it.
10515                         ;; Otherwise, show text starting with following line.
10516                         (if (and (eolp) (not (bobp)))
10517                             (forward-line 1))
10518                         (point)))))
10519
10520 (defun gnus-gmt-to-local ()
10521   "Rewrite Date header described in GMT to local in current buffer.
10522 Intended to be used with gnus-article-prepare-hook."
10523   (save-excursion
10524     (save-restriction
10525       (widen)
10526       (goto-char (point-min))
10527       (narrow-to-region (point-min)
10528                         (progn (search-forward "\n\n" nil 'move) (point)))
10529       (goto-char (point-min))
10530       (if (re-search-forward "^Date:[ \t]\\(.*\\)$" nil t)
10531           (let ((buffer-read-only nil)
10532                 (date (buffer-substring-no-properties
10533                        (match-beginning 1) (match-end 1))))
10534             (delete-region (match-beginning 1) (match-end 1))
10535             (insert
10536              (timezone-make-date-arpa-standard 
10537               date nil (current-time-zone))))))))
10538
10539
10540 ;; Article mode commands
10541
10542 (defun gnus-article-next-page (lines)
10543   "Show next page of current article.
10544 If end of article, return non-nil. Otherwise return nil.
10545 Argument LINES specifies lines to be scrolled up."
10546   (interactive "P")
10547   (move-to-window-line -1)
10548   ;; Fixed by enami@ptgd.sony.co.jp (enami tsugutomo)
10549   (if (save-excursion
10550         (end-of-line)
10551         (and (pos-visible-in-window-p)  ;Not continuation line.
10552              (eobp)))
10553       ;; Nothing in this page.
10554       (if (or (not gnus-break-pages)
10555               (save-excursion
10556                 (save-restriction
10557                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
10558           t                             ;Nothing more.
10559         (gnus-narrow-to-page 1)         ;Go to next page.
10560         nil)
10561     ;; More in this page.
10562     (condition-case ()
10563         (scroll-up lines)
10564       (end-of-buffer
10565        ;; Long lines may cause an end-of-buffer error.
10566        (goto-char (point-max))))
10567     nil))
10568
10569 (defun gnus-article-prev-page (lines)
10570   "Show previous page of current article.
10571 Argument LINES specifies lines to be scrolled down."
10572   (interactive "P")
10573   (move-to-window-line 0)
10574   (if (and gnus-break-pages
10575            (bobp)
10576            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
10577       (progn
10578         (gnus-narrow-to-page -1) ;Go to previous page.
10579         (goto-char (point-max))
10580         (recenter -1))
10581     (scroll-down lines)))
10582
10583 (defun gnus-article-refer-article ()
10584   "Read article specified by message-id around point."
10585   (interactive)
10586   (search-forward ">" nil t)    ;Move point to end of "<....>".
10587   (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
10588       (let ((message-id
10589              (buffer-substring (match-beginning 1) (match-end 1))))
10590         (set-buffer gnus-summary-buffer)
10591         (gnus-summary-refer-article message-id))
10592     (error "No references around point")))
10593
10594 (defun gnus-article-show-summary ()
10595   "Reconfigure windows to show summary buffer."
10596   (interactive)
10597   (gnus-configure-windows 'article)
10598   (gnus-summary-goto-subject gnus-current-article))
10599
10600 (defun gnus-article-describe-briefly ()
10601   "Describe article mode commands briefly."
10602   (interactive)
10603   (gnus-message 6
10604    (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")))
10605
10606 (defun gnus-article-summary-command ()
10607   "Execute the last keystroke in the summary buffer."
10608   (interactive)
10609   (let ((obuf (current-buffer))
10610         (owin (current-window-configuration))
10611         func)
10612     (switch-to-buffer gnus-summary-buffer 'norecord)
10613     (setq func (lookup-key (current-local-map) (this-command-keys)))
10614     (call-interactively func)
10615     (set-buffer obuf)
10616     (let ((npoint (point)))
10617       (set-window-configuration owin)
10618       (set-window-start (get-buffer-window (current-buffer)) (point)))))
10619
10620 (defun gnus-article-summary-command-nosave ()
10621   "Execute the last keystroke in the summary buffer."
10622   (interactive)
10623   (let ((obuf (current-buffer))
10624         (owin (current-window-configuration))
10625         func)
10626     (switch-to-buffer gnus-summary-buffer 'norecord)
10627     (setq func (lookup-key (current-local-map) (this-command-keys)))
10628     (call-interactively func)))
10629
10630 ;; caesar-region written by phr@prep.ai.mit.edu  Nov 86
10631 ;; Modified by tower@prep Nov 86
10632 ;; Modified by umerin@flab.flab.Fujitsu.JUNET for ROT47.
10633
10634 (defun gnus-caesar-region (&optional n)
10635   "Caesar rotation of region by N, default 13, for decrypting netnews.
10636 ROT47 will be performed for Japanese text in any case."
10637   (interactive (if current-prefix-arg   ; Was there a prefix arg?
10638                    (list (prefix-numeric-value current-prefix-arg))
10639                  (list nil)))
10640   (cond ((not (numberp n)) (setq n 13))
10641         (t (setq n (mod n 26))))        ;canonicalize N
10642   (if (not (zerop n))           ; no action needed for a rot of 0
10643       (progn
10644         (if (or (not (boundp 'caesar-translate-table))
10645                 (not caesar-translate-table)
10646                 (/= (aref caesar-translate-table ?a) (+ ?a n)))
10647             (let ((i 0) 
10648                   (lower "abcdefghijklmnopqrstuvwxyz")
10649                   upper)
10650               (gnus-message 9 "Building caesar-translate-table...")
10651               (setq caesar-translate-table (make-vector 256 0))
10652               (while (< i 256)
10653                 (aset caesar-translate-table i i)
10654                 (setq i (1+ i)))
10655               (setq lower (concat lower lower)
10656                     upper (upcase lower)
10657                     i 0)
10658               (while (< i 26)
10659                 (aset caesar-translate-table (+ ?a i) (aref lower (+ i n)))
10660                 (aset caesar-translate-table (+ ?A i) (aref upper (+ i n)))
10661                 (setq i (1+ i)))
10662               ;; ROT47 for Japanese text.
10663               ;; Thanks to ichikawa@flab.fujitsu.junet.
10664               (setq i 161)
10665               (let ((t1 (logior ?O 128))
10666                     (t2 (logior ?! 128))
10667                     (t3 (logior ?~ 128)))
10668                 (while (< i 256)
10669                   (aset caesar-translate-table i
10670                         (let ((v (aref caesar-translate-table i)))
10671                           (if (<= v t1) (if (< v t2) v (+ v 47))
10672                             (if (<= v t3) (- v 47) v))))
10673                   (setq i (1+ i))))
10674               (gnus-message 9 "Building caesar-translate-table...done")))
10675         (let ((from (region-beginning))
10676               (to (region-end))
10677               (i 0) str len)
10678           (setq str (buffer-substring from to))
10679           (setq len (length str))
10680           (while (< i len)
10681             (aset str i (aref caesar-translate-table (aref str i)))
10682             (setq i (1+ i)))
10683           (goto-char from)
10684           (delete-region from to)
10685           (insert str)))))
10686
10687 \f
10688 ;; Basic ideas by emv@math.lsa.umich.edu (Edward Vielmetti)
10689
10690 ;;;###autoload
10691 (defalias 'gnus-batch-kill 'gnus-batch-score)
10692 ;;;###autoload
10693 (defun gnus-batch-score ()
10694   "Run batched scoring.
10695 Usage: emacs -batch -l gnus -f gnus-batch-score <newsgroups> ...
10696 Newsgroups is a list of strings in Bnews format.  If you want to score
10697 the comp hierarchy, you'd say \"comp.all\". If you would not like to
10698 score the alt hierarchy, you'd say \"!alt.all\"."
10699   (interactive)
10700   (let* ((yes-and-no
10701           (gnus-newsrc-parse-options
10702            (apply (function concat)
10703                   (mapcar (lambda (g) (concat g " "))
10704                           command-line-args-left))))
10705          (gnus-expert-user t)
10706          (nnmail-spool-file nil)
10707          (gnus-use-dribble-file nil)
10708          (yes (car yes-and-no))
10709          (no (cdr yes-and-no))
10710          group subscribed newsrc entry
10711          ;; Disable verbose message.
10712          gnus-novice-user gnus-large-newsgroup)
10713     ;; Eat all arguments.
10714     (setq command-line-args-left nil)
10715     ;; Start Gnus.
10716     (gnus)
10717     ;; Apply kills to specified newsgroups in command line arguments.
10718     (setq newsrc (cdr gnus-newsrc-alist))
10719     (while newsrc
10720       (setq group (car (car newsrc)))
10721       (setq entry (gnus-gethash group gnus-newsrc-hashtb))
10722       (if (and (<= (nth 1 (car newsrc)) gnus-level-subscribed)
10723                (and (car entry)
10724                     (or (eq (car entry) t)
10725                         (not (zerop (car entry)))))
10726                (if yes (string-match yes group) t)
10727                (or (null no) (not (string-match no group))))
10728           (progn
10729             (gnus-summary-read-group group nil t)
10730             (and (eq (current-buffer) (get-buffer gnus-summary-buffer))
10731                  (gnus-summary-exit))))
10732       (setq newsrc (cdr newsrc)))
10733     ;; Exit Emacs.
10734     (switch-to-buffer gnus-group-buffer)
10735     (gnus-group-save-newsrc)))
10736
10737 (defun gnus-apply-kill-file ()
10738   "Apply a kill file to the current newsgroup.
10739 Returns the number of articles marked as read."
10740   (if (or (file-exists-p (gnus-newsgroup-kill-file nil))
10741           (file-exists-p (gnus-newsgroup-kill-file gnus-newsgroup-name)))
10742       (gnus-apply-kill-file-internal)
10743     0))
10744
10745 (defun gnus-kill-save-kill-buffer ()
10746   (save-excursion
10747     (let ((file (gnus-newsgroup-kill-file gnus-newsgroup-name)))
10748       (if (get-file-buffer file)
10749           (progn
10750             (set-buffer (get-file-buffer file))
10751             (and (buffer-modified-p) (save-buffer))
10752             (kill-buffer (current-buffer)))))))
10753
10754 (defvar gnus-kill-file-name "KILL"
10755   "Suffix of the kill files.")
10756
10757 (defun gnus-newsgroup-kill-file (newsgroup)
10758   "Return the name of a kill file name for NEWSGROUP.
10759 If NEWSGROUP is nil, return the global kill file name instead."
10760   (cond ((or (null newsgroup)
10761              (string-equal newsgroup ""))
10762          ;; The global KILL file is placed at top of the directory.
10763          (expand-file-name gnus-kill-file-name
10764                            (or gnus-kill-files-directory "~/News")))
10765         ((gnus-use-long-file-name 'not-kill)
10766          ;; Append ".KILL" to newsgroup name.
10767          (expand-file-name (concat newsgroup "." gnus-kill-file-name)
10768                            (or gnus-kill-files-directory "~/News")))
10769         (t
10770          ;; Place "KILL" under the hierarchical directory.
10771          (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
10772                                    "/" gnus-kill-file-name)
10773                            (or gnus-kill-files-directory "~/News")))))
10774
10775 \f
10776 ;;;
10777 ;;; Dribble file
10778 ;;;
10779
10780 (defvar gnus-dribble-ignore nil)
10781
10782 (defun gnus-dribble-file-name ()
10783   (concat gnus-startup-file "-dribble"))
10784
10785 (defun gnus-dribble-open ()
10786   (save-excursion 
10787     (set-buffer 
10788      (setq gnus-dribble-buffer (find-file-noselect (gnus-dribble-file-name))))
10789     (buffer-disable-undo (current-buffer))
10790     (bury-buffer gnus-dribble-buffer)
10791     (auto-save-mode t)
10792     (goto-char (point-max))))
10793
10794 (defun gnus-dribble-enter (string)
10795   (if (and (not gnus-dribble-ignore)
10796            gnus-dribble-buffer
10797            (buffer-name gnus-dribble-buffer))
10798       (let ((obuf (current-buffer)))
10799         (set-buffer gnus-dribble-buffer)
10800         (insert string "\n")
10801         (set-window-point (get-buffer-window (current-buffer)) (point-max))
10802         (set-buffer obuf))))
10803
10804 (defun gnus-dribble-read-file ()
10805   (let ((dribble-file (gnus-dribble-file-name)))
10806     (save-excursion 
10807       (set-buffer (setq gnus-dribble-buffer 
10808                         (get-buffer-create 
10809                          (file-name-nondirectory dribble-file))))
10810       (gnus-add-current-to-buffer-list)
10811       (erase-buffer)
10812       (set-visited-file-name dribble-file)
10813       (buffer-disable-undo (current-buffer))
10814       (bury-buffer (current-buffer))
10815       (set-buffer-modified-p nil)
10816       (let ((auto (make-auto-save-file-name))
10817             (gnus-dribble-ignore t))
10818         (if (or (file-exists-p auto) (file-exists-p dribble-file))
10819             (progn
10820               (if (file-newer-than-file-p auto dribble-file)
10821                   (setq dribble-file auto))
10822               (insert-file-contents dribble-file)
10823               (if (not (zerop (buffer-size)))
10824                   (set-buffer-modified-p t))
10825               (if (gnus-y-or-n-p 
10826                    "Auto-save file exists. Do you want to read it? ")
10827                   (progn
10828                     (gnus-message 5 "Reading %s..." dribble-file) 
10829                     (eval-current-buffer)
10830                     (gnus-message 5 "Reading %s...done" dribble-file)))))))))
10831
10832 (defun gnus-dribble-delete-file ()
10833   (if (file-exists-p (gnus-dribble-file-name))
10834       (delete-file (gnus-dribble-file-name)))
10835   (if gnus-dribble-buffer
10836       (save-excursion
10837         (set-buffer gnus-dribble-buffer)
10838         (let ((auto (make-auto-save-file-name)))
10839           (if (file-exists-p auto)
10840               (delete-file auto))
10841           (erase-buffer)
10842           (set-buffer-modified-p nil)))))
10843
10844 (defun gnus-dribble-save ()
10845   (if (and gnus-dribble-buffer
10846            (buffer-name gnus-dribble-buffer))
10847       (save-excursion
10848         (set-buffer gnus-dribble-buffer)
10849         (save-buffer))))
10850
10851 (defun gnus-dribble-clear ()
10852   (save-excursion
10853     (if (gnus-buffer-exists-p gnus-dribble-buffer)
10854         (progn
10855           (set-buffer gnus-dribble-buffer)
10856           (erase-buffer)
10857           (set-buffer-modified-p nil)
10858           (setq buffer-saved-size (buffer-size))))))
10859
10860 ;;;
10861 ;;; Server Communication
10862 ;;;
10863
10864 ;; All the Gnus backends have the same interface, and should return
10865 ;; data in a similar format. Below is an overview of what functions
10866 ;; these packages must supply and what results they should return.
10867 ;;
10868 ;; Variables:
10869 ;;
10870 ;; `nntp-server-buffer' - All data should be returned to Gnus in this
10871 ;; buffer. 
10872 ;;
10873 ;; Functions for the imaginary backend `choke':
10874 ;;
10875 ;; `choke-retrieve-headers ARTICLES &optional GROUP SERVER'
10876 ;; Should return all headers for all ARTICLES, or return NOV lines for
10877 ;; the same.
10878 ;;
10879 ;; `choke-request-group GROUP &optional SERVER DISCARD'
10880 ;; Switch to GROUP. If DISCARD is nil, active information on the group
10881 ;; must be returned.
10882 ;;
10883 ;; `choke-close-group GROUP &optional SERVER'
10884 ;; Close group. Most backends won't have to do anything with this
10885 ;; call, but it is an opportunity to clean up, if that is needed. It
10886 ;; is called when Gnus exits a group.
10887 ;;
10888 ;; `choke-request-article ARTICLE &optional GROUP SERVER'
10889 ;; Return ARTICLE, which is either an article number or
10890 ;; message-id. Note that not all backends can return articles based on
10891 ;; message-id. 
10892 ;;
10893 ;; `choke-request-list SERVER'
10894 ;; Return a list of all newsgroups on SERVER.
10895 ;;
10896 ;; `choke-request-list-newsgroups SERVER'
10897 ;; Return a list of descriptions of all newsgroups on SERVER.
10898 ;;
10899 ;; `choke-request-newgroups DATE &optional SERVER'
10900 ;; Return a list of all groups that have arrived after DATE on
10901 ;; SERVER. Note that the date doesn't have to be respected - Gnus will
10902 ;; always check whether the groups are old or not. Backends that do
10903 ;; not store date information may just return the entire list of
10904 ;; groups, although this might not be a good idea in general.
10905 ;;
10906 ;; `choke-request-post-buffer METHOD HEADER ARTICLE-BUFFER GROUP INFO'
10907 ;; Should return a buffer that is suitable for "posting". nnspool and
10908 ;; nntp return a `*post-buffer*', and nnmail return a `*mail*'
10909 ;; buffer. This function should fill out the appropriate headers. 
10910 ;;
10911 ;; `choke-request-post &optional SERVER'
10912 ;; Function that will be called from a buffer to be posted. 
10913 ;;
10914 ;; `choke-open-server SERVER &optional ARGUMENT'
10915 ;; Open a connection to SERVER.
10916 ;;
10917 ;; `choke-close-server &optional SERVER'
10918 ;; Close the connection to SERVER.
10919 ;;
10920 ;; `choke-server-opened &optional SERVER'
10921 ;; Whether the conenction to SERVER is opened or not.
10922 ;;
10923 ;; `choke-server-status &optional SERVER'
10924 ;; Should return a status string (not in the nntp buffer, but as the
10925 ;; result of the function).
10926 ;;
10927 ;; `choke-retrieve-groups GROUPS &optional SERVER'
10928 ;; Optional function for retrieving active file info on all groups in
10929 ;; GROUPS.  Two return formats are supported: The normal active file
10930 ;; format, and a list of GROUP lines.  This function should return (as
10931 ;; a function value) either `active' or `group', depending on what
10932 ;; format it returns.
10933 ;;
10934 ;; The following functions are optional and apply only to backends
10935 ;; that are able to control the contents of their groups totally
10936 ;; (ie. mail backends.)  Backends that aren't able to do that
10937 ;; shouldn't define these functions at all. Gnus will check for their
10938 ;; presence before attempting to call them.
10939 ;;
10940 ;; `choke-request-expire-articles ARTICLES &optional NEWSGROUP SERVER'
10941 ;; Should expire (according to some aging scheme) all ARTICLES. Most
10942 ;; backends will not be able to expire articles. Should return a list
10943 ;; of all articles that were not expired.
10944 ;;
10945 ;; `choke-request-move-article ARTICLE GROUP SERVER ACCEPT-FORM &optional LAST'
10946 ;; Should move ARTICLE from GROUP on SERVER by using ACCEPT-FORM.
10947 ;; Removes any information it has added to the article (extra headers,
10948 ;; whatever - make it as clean as possible), and then passes the
10949 ;; article on by evaling ACCEPT-FORM, which is normally a call to the
10950 ;; function described below. If the ACCEPT-FORM returns a non-nil
10951 ;; value, the article should then be deleted. If LAST is nil, that
10952 ;; means that there will be further calls to this function. This might
10953 ;; be taken as an advice not to save buffers/internal variables just
10954 ;; yet, but wait until the last call to speed things up.
10955 ;;
10956 ;; `choke-request-accept-article GROUP &optional LAST' 
10957 ;; The contents of the current buffer will be put into GROUP.  There
10958 ;; should, of course, be an article in the current buffer.  This
10959 ;; function is normally only called by the function described above,
10960 ;; and LAST works the same way as in that function.
10961 ;;
10962 ;; `choke-request-replace-article ARTICLE GROUP BUFFER'
10963 ;; Replace ARTICLE in GROUP with the contents of BUFFER.
10964 ;; This provides an easy interface for allowing editing of
10965 ;; articles. Note that even headers may be edited, so the backend has
10966 ;; to update any tables (nov buffers, etc) that it maintains after
10967 ;; replacing the article.
10968 ;;
10969 ;; `choke-request-create-group GROUP &optional SERVER'
10970 ;; Create GROUP on SERVER.  This might be a new, empty group, or it
10971 ;; might be a group that already exists, but hasn't been registered
10972 ;; yet. 
10973 ;;
10974 ;; All these functions must return nil if they couldn't service the
10975 ;; request. If the optional arguments are not supplied, some "current"
10976 ;; or "default" values should be used. In short, one should emulate an
10977 ;; NNTP server, in a way.
10978 ;;
10979 ;; If you want to write a new backend, you just have to supply the
10980 ;; functions listed above. In addition, you must enter the new backend
10981 ;; into the list of valid select methods:
10982 ;; (setq gnus-valid-select-methods 
10983 ;;       (cons '("choke" mail) gnus-valid-select-methods))
10984 ;; The first element in this list is the name of the backend. Other
10985 ;; elemnets may be `mail' (for mail groups),  `post' (for news
10986 ;; groups), `none' (neither), `respool' (for groups that can control
10987 ;; their contents). 
10988
10989 (defun gnus-start-news-server (&optional confirm)
10990   "Open a method for getting news.
10991 If CONFIRM is non-nil, the user will be asked for an NNTP server."
10992   (let (how where)
10993     (if gnus-current-select-method
10994         ;; Stream is already opened.
10995         nil
10996       ;; Open NNTP server.
10997       (if (null gnus-nntp-service) (setq gnus-nntp-server nil))
10998       (if confirm
10999           (progn
11000             ;; Read server name with completion.
11001             (setq gnus-nntp-server
11002                   (completing-read "NNTP server: "
11003                                    (mapcar (lambda (server) (list server))
11004                                            (cons (list gnus-nntp-server)
11005                                                  gnus-secondary-servers))
11006                                    nil nil gnus-nntp-server))))
11007
11008       (if (and gnus-nntp-server 
11009                (stringp gnus-nntp-server)
11010                (not (string= gnus-nntp-server "")))
11011           (setq gnus-select-method
11012                 (cond ((or (string= gnus-nntp-server "")
11013                            (string= gnus-nntp-server "::"))
11014                        (list 'nnspool (system-name)))
11015                       ((string-match "^:" gnus-nntp-server)
11016                        (list 'nnmh gnus-nntp-server 
11017                              (list 'nnmh-directory 
11018                                    (file-name-as-directory
11019                                     (expand-file-name
11020                                      (concat "~/" (substring
11021                                                    gnus-nntp-server 1)))))
11022                              (list 'nnmh-get-new-mail nil)))
11023                       (t
11024                        (list 'nntp gnus-nntp-server)))))
11025
11026       (setq how (car gnus-select-method))
11027       (setq where (car (cdr gnus-select-method)))
11028       (cond ((eq how 'nnspool)
11029              (require 'nnspool)
11030              (gnus-message 5 "Looking up local news spool..."))
11031             ((eq how 'nnmh)
11032              (require 'nnmh)
11033              (gnus-message 5 "Looking up mh spool..."))
11034             (t
11035              (require 'nntp)))
11036       (setq gnus-current-select-method gnus-select-method)
11037       (run-hooks 'gnus-open-server-hook)
11038       (or 
11039        ;; gnus-open-server-hook might have opened it
11040        (gnus-server-opened gnus-select-method)  
11041        (gnus-open-server gnus-select-method)
11042        (gnus-y-or-n-p
11043         (format
11044          "%s server on %s can't be opened. Continue? "
11045          (car gnus-select-method) (nth 1 gnus-select-method)))
11046        (progn
11047          (gnus-message 1 "Couldn't open server on %s" 
11048                        (nth 1 gnus-select-method))
11049          (ding)
11050          nil)))))
11051
11052 (defun gnus-check-news-server (&optional method)
11053   "If the news server is down, start it up again."
11054   (let ((method (if method method gnus-select-method)))
11055     (and (stringp method)
11056          (setq method (gnus-server-to-method method)))
11057     (if (gnus-server-opened method)
11058         ;; Stream is already opened.
11059         t
11060       ;; Open server.
11061       (gnus-message 5 "Opening server %s on %s..." (car method) (nth 1 method))
11062       (run-hooks 'gnus-open-server-hook)
11063       (or (gnus-server-opened method)
11064           (gnus-open-server method))
11065       (message ""))))
11066
11067 (defun gnus-nntp-message (&optional message)
11068   "Check the status of the NNTP server.
11069 If the status of the server is clear and MESSAGE is non-nil, MESSAGE
11070 is returned insted of the status string."
11071   (let ((status (gnus-status-message (gnus-find-method-for-group 
11072                                       gnus-newsgroup-name)))
11073         (message (or message "")))
11074     (if (and (stringp status) (> (length status) 0))
11075         status message)))
11076
11077 (defun gnus-get-function (method function)
11078   (and (stringp method)
11079        (setq method (gnus-server-to-method method)))
11080   (let ((func (intern (format "%s-%s" (car method) function))))
11081     (if (not (fboundp func)) 
11082         (progn
11083           (require (car method))
11084           (if (not (fboundp func)) 
11085               (error "No such function: %s" func))))
11086     func))
11087
11088 ;;; Interface functions to the backends.
11089
11090 (defun gnus-open-server (method)
11091   (funcall (gnus-get-function method 'open-server)
11092            (nth 1 method) (nthcdr 2 method)))
11093
11094 (defun gnus-close-server (method)
11095   (funcall (gnus-get-function method 'close-server) (nth 1 method)))
11096
11097 (defun gnus-request-list (method)
11098   (funcall (gnus-get-function method 'request-list) (nth 1 method)))
11099
11100 (defun gnus-request-list-newsgroups (method)
11101   (funcall (gnus-get-function method 'request-list-newsgroups) (nth 1 method)))
11102
11103 (defun gnus-request-newgroups (date method)
11104   (funcall (gnus-get-function method 'request-newgroups) 
11105            date (nth 1 method)))
11106
11107 (defun gnus-server-opened (method)
11108   (funcall (gnus-get-function method 'server-opened) (nth 1 method)))
11109
11110 (defun gnus-status-message (method)
11111   (let ((method (if (stringp method) (gnus-find-method-for-group method)
11112                   method)))
11113     (funcall (gnus-get-function method 'status-message) (nth 1 method))))
11114
11115 (defun gnus-request-group (group &optional dont-check)
11116   (let ((method (gnus-find-method-for-group group)))
11117 ;    (and t (message "%s GROUP %s" (car method) group))
11118     (funcall (gnus-get-function method 'request-group) 
11119              (gnus-group-real-name group) (nth 1 method) dont-check)))
11120
11121 (defun gnus-request-asynchronous (group &optional articles)
11122   (let ((method (gnus-find-method-for-group group)))
11123     (funcall (gnus-get-function method 'request-asynchronous) 
11124              (gnus-group-real-name group) (nth 1 method) articles)))
11125
11126 (defun gnus-list-active-group (group)
11127   (let ((method (gnus-find-method-for-group group))
11128         (func 'list-active-group))
11129     (and (gnus-check-backend-function func group)
11130          (funcall (gnus-get-function method func) 
11131                   (gnus-group-real-name group) (nth 1 method)))))
11132
11133 (defun gnus-request-group-description (group)
11134   (let ((method (gnus-find-method-for-group group))
11135         (func 'request-group-description))
11136     (and (gnus-check-backend-function func group)
11137          (funcall (gnus-get-function method func) 
11138                   (gnus-group-real-name group) (nth 1 method)))))
11139
11140 (defun gnus-close-group (group)
11141   (let ((method (gnus-find-method-for-group group)))
11142     (funcall (gnus-get-function method 'close-group) 
11143              (gnus-group-real-name group) (nth 1 method))))
11144
11145 (defun gnus-retrieve-headers (articles group)
11146   (let ((method (gnus-find-method-for-group group)))
11147     (if (and gnus-use-cache (numberp (car articles)))
11148         (gnus-cache-retrieve-headers articles group)
11149       (funcall (gnus-get-function method 'retrieve-headers) 
11150                articles (gnus-group-real-name group) (nth 1 method)))))
11151
11152 (defun gnus-retrieve-groups (groups method)
11153   (funcall (gnus-get-function method 'retrieve-groups) groups (nth 1 method)))
11154
11155 (defun gnus-request-article (article group &optional buffer)
11156   (let ((method (gnus-find-method-for-group group)))
11157     (funcall (gnus-get-function method 'request-article) 
11158              article (gnus-group-real-name group) (nth 1 method) buffer)))
11159
11160 (defun gnus-request-head (article group)
11161   (let ((method (gnus-find-method-for-group group)))
11162     (funcall (gnus-get-function method 'request-head) 
11163              article (gnus-group-real-name group) (nth 1 method))))
11164
11165 ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
11166 (defun gnus-request-post-buffer (post group subject header artbuf
11167                                       info follow-to respect-poster)
11168    (let* ((info (or info (and group (nth 2 (gnus-gethash 
11169                                             group gnus-newsrc-hashtb)))))
11170           (method
11171            (if (and gnus-post-method
11172                     ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
11173                     (memq 'post (assoc
11174                                  (format "%s" (car (gnus-find-method-for-group
11175                                                     gnus-newsgroup-name)))
11176                                         gnus-valid-select-methods)))
11177                gnus-post-method
11178              (gnus-find-method-for-group gnus-newsgroup-name))))
11179      (or (gnus-server-opened method)
11180          (gnus-open-server method)
11181          (error "Can't open server %s:%s" (car method) (nth 1 method)))
11182      (let ((mail-self-blind nil)
11183            (mail-archive-file-name nil))
11184        (funcall (gnus-get-function method 'request-post-buffer) 
11185                 post group subject header artbuf info follow-to
11186                 respect-poster))))
11187
11188 (defun gnus-request-post (method &optional force)
11189   (and (stringp method)
11190        (setq method (gnus-server-to-method method)))
11191   (and (not force) gnus-post-method
11192        (memq 'post (assoc (format "%s" (car method))
11193                           gnus-valid-select-methods))
11194        (setq method gnus-post-method))
11195   (funcall (gnus-get-function method 'request-post) 
11196            (nth 1 method)))
11197
11198 (defun gnus-request-expire-articles (articles group &optional force)
11199   (let ((method (gnus-find-method-for-group group)))
11200     (funcall (gnus-get-function method 'request-expire-articles) 
11201              articles (gnus-group-real-name group) (nth 1 method)
11202              force)))
11203
11204 (defun gnus-request-move-article 
11205   (article group server accept-function &optional last)
11206   (let ((method (gnus-find-method-for-group group)))
11207     (funcall (gnus-get-function method 'request-move-article) 
11208              article (gnus-group-real-name group) 
11209              (nth 1 method) accept-function last)))
11210
11211 (defun gnus-request-accept-article (group &optional last)
11212   (let ((func (if (symbolp group) group
11213                 (car (gnus-find-method-for-group group)))))
11214     (funcall (intern (format "%s-request-accept-article" func))
11215              (if (stringp group) (gnus-group-real-name group) group)
11216              last)))
11217
11218 (defun gnus-request-replace-article (article group buffer)
11219   (let ((func (car (gnus-find-method-for-group group))))
11220     (funcall (intern (format "%s-request-replace-article" func))
11221              article (gnus-group-real-name group) buffer)))
11222
11223 (defun gnus-request-create-group (group)
11224   (let ((method (gnus-find-method-for-group group)))
11225     (funcall (gnus-get-function method 'request-create-group) 
11226              (gnus-group-real-name group) (nth 1 method))))
11227
11228 (defun gnus-member-of-valid (symbol group)
11229   (memq symbol (assoc
11230                 (format "%s" (car (gnus-find-method-for-group group)))
11231                 gnus-valid-select-methods)))
11232
11233 (defsubst gnus-secondary-method-p (method)
11234   (member method gnus-secondary-select-methods))
11235
11236 (defun gnus-find-method-for-group (group &optional info)
11237   (or gnus-override-method
11238       (and (not group)
11239            gnus-select-method)
11240       (let ((info (or info (nth 2 (gnus-gethash group gnus-newsrc-hashtb))))
11241             method)
11242         (if (or (not info)
11243                 (not (setq method (nth 4 info))))
11244             (setq method gnus-select-method)
11245           (setq method
11246                 (cond ((stringp method)
11247                        (gnus-server-to-method method))
11248                       ((stringp (car method))
11249                        (gnus-server-extend-method group method))
11250                       (t
11251                        method))))
11252         (gnus-server-add-address method))))
11253
11254 (defun gnus-check-backend-function (func group)
11255   (let ((method (if (stringp group) (car (gnus-find-method-for-group group))
11256                  group)))
11257     (fboundp (intern (format "%s-%s" method func)))))
11258
11259 (defun gnus-methods-using (method)
11260   (let ((valids gnus-valid-select-methods)
11261         outs)
11262     (while valids
11263       (if (memq method (car valids)) 
11264           (setq outs (cons (car valids) outs)))
11265       (setq valids (cdr valids)))
11266     outs))
11267
11268 ;;; 
11269 ;;; Active & Newsrc File Handling
11270 ;;;
11271
11272 ;; Newsrc related functions.
11273 ;; Gnus internal format of gnus-newsrc-alist:
11274 ;; (("alt.general" 3 (1 . 1))
11275 ;;  ("alt.misc"    3 ((1 . 10) (12 . 15)))
11276 ;;  ("alt.test"    7 (1 . 99) (45 57 93)) ...)
11277 ;; The first item is the group name; the second is the subscription
11278 ;; level; the third is either a range of a list of ranges of read
11279 ;; articles, the optional fourth element is a list of marked articles,
11280 ;; the optional fifth element is the select method.
11281 ;;
11282 ;; Gnus internal format of gnus-newsrc-hashtb:
11283 ;; (95 ("alt.general" 3 (1 . 1)) ("alt.misc" 3 ((1 . 10) (12 . 15))) ...)
11284 ;; This is the entry for "alt.misc". The first element is the number
11285 ;; of unread articles in "alt.misc". The cdr of this entry is the
11286 ;; element *before* "alt.misc" in gnus-newsrc-alist, which makes is
11287 ;; trivial to remove or add new elements into gnus-newsrc-alist
11288 ;; without scanning the entire list. So, to get the actual information
11289 ;; of "alt.misc", you'd say something like 
11290 ;; (nth 2 (gnus-gethash "alt.misc" gnus-newsrc-hashtb))
11291 ;;
11292 ;; Gnus internal format of gnus-active-hashtb:
11293 ;; ((1 . 1))
11294 ;;  (5 . 10))
11295 ;;  (67 . 99)) ...)
11296 ;; The only element in each entry in this hash table is a range of
11297 ;; (possibly) available articles. (Articles in this range may have
11298 ;; been expired or cancelled.)
11299 ;;
11300 ;; Gnus internal format of gnus-killed-list and gnus-zombie-list:
11301 ;; ("alt.misc" "alt.test" "alt.general" ...)
11302
11303 (defun gnus-setup-news (&optional rawfile level)
11304   "Setup news information.
11305 If RAWFILE is non-nil, the .newsrc file will also be read.
11306 If LEVEL is non-nil, the news will be set up at level LEVEL."
11307   (let ((init (not (and gnus-newsrc-alist gnus-active-hashtb (not rawfile)))))
11308     ;; Clear some variables to re-initialize news information.
11309     (if init (setq gnus-newsrc-alist nil gnus-active-hashtb nil))
11310
11311     ;; Read the newsrc file and create `gnus-newsrc-hashtb'.
11312     (if init (gnus-read-newsrc-file rawfile))
11313
11314     ;; Read the active file and create `gnus-active-hashtb'.
11315     ;; If `gnus-read-active-file' is nil, then we just create an empty
11316     ;; hash table. The partial filling out of the hash table will be
11317     ;; done in `gnus-get-unread-articles'.
11318     (if (and gnus-read-active-file 
11319              (not level)
11320              (gnus-server-opened gnus-select-method))
11321         (gnus-read-active-file)
11322       (setq gnus-active-hashtb (make-vector 4095 0)))
11323
11324     (and init gnus-use-dribble-file (gnus-dribble-read-file))
11325
11326     ;; Find the number of unread articles in each non-dead group.
11327     (gnus-get-unread-articles (or level (1+ gnus-level-subscribed)))
11328     ;; Find new newsgroups and treat them.
11329     (if (and init gnus-check-new-newsgroups gnus-read-active-file (not level)
11330              (gnus-server-opened gnus-select-method))
11331         (gnus-find-new-newsgroups))
11332     (if (and init gnus-check-bogus-newsgroups 
11333              gnus-read-active-file (not level)
11334              (gnus-server-opened gnus-select-method))
11335         (gnus-check-bogus-newsgroups))))
11336
11337 (defun gnus-find-new-newsgroups ()
11338   "Search for new newsgroups and add them.
11339 Each new newsgroup will be treated with `gnus-subscribe-newsgroup-method.'
11340 The `-n' option line from .newsrc is respected."
11341   (interactive)
11342   (or (gnus-check-first-time-used)
11343       (if (or (consp gnus-check-new-newsgroups)
11344               (eq gnus-check-new-newsgroups 'ask-server))
11345           (gnus-ask-server-for-new-groups)
11346         (let ((groups 0)
11347               group new-newsgroups)
11348           (or gnus-have-read-active-file (gnus-read-active-file))
11349           (setq gnus-newsrc-last-checked-date (current-time-string))
11350           (if (not gnus-killed-hashtb) (gnus-make-hashtable-from-killed))
11351           ;; Go though every newsgroup in `gnus-active-hashtb' and compare
11352           ;; with `gnus-newsrc-hashtb' and `gnus-killed-hashtb'.
11353           (mapatoms
11354            (lambda (sym)
11355              (setq group (symbol-name sym))
11356              (if (or (gnus-gethash group gnus-killed-hashtb)
11357                      (gnus-gethash group gnus-newsrc-hashtb))
11358                  ()
11359                (let ((do-sub (gnus-matches-options-n group)))
11360                  (cond ((eq do-sub 'subscribe)
11361                         (setq groups (1+ groups))
11362                         (gnus-sethash group group gnus-killed-hashtb)
11363                         (funcall 
11364                          gnus-subscribe-options-newsgroup-method group))
11365                        ((eq do-sub 'ignore)
11366                         nil)
11367                        (t
11368                         (setq groups (1+ groups))
11369                         (gnus-sethash group group gnus-killed-hashtb)
11370                         (if gnus-subscribe-hierarchical-interactive
11371                             (setq new-newsgroups (cons group new-newsgroups))
11372                           (funcall gnus-subscribe-newsgroup-method group)))))))
11373            gnus-active-hashtb)
11374           (if new-newsgroups 
11375               (gnus-subscribe-hierarchical-interactive new-newsgroups))
11376           ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
11377           (if (> groups 0)
11378               (gnus-message 6 "%d new newsgroup%s arrived." 
11379                             groups (if (> groups 1) "s have" " has")))))))
11380
11381 (defun gnus-matches-options-n (group)
11382   ;; Returns `subscribe' if the group is to be uncoditionally
11383   ;; subscribed, `ignore' if it is to be ignored, and nil if there is
11384   ;; no match for the group.
11385
11386   ;; First we check the two user variables.
11387   (cond
11388    ((and gnus-options-subscribe
11389          (string-match gnus-options-subscribe group))
11390     'subscribe)
11391    ((and gnus-options-not-subscribe
11392          (string-match gnus-options-not-subscribe group))
11393     'ignore)
11394    ;; Then we go through the list that was retrieved from the .newsrc
11395    ;; file.  This list has elements on the form 
11396    ;; `(REGEXP . {ignore,subscribe})'. The first match found (the list
11397    ;; is in the reverse order of the options line) is returned.
11398    (t
11399     (let ((regs gnus-newsrc-options-n))
11400       (while (and regs
11401                   (not (string-match (car (car regs)) group)))
11402         (setq regs (cdr regs)))
11403       (and regs (cdr (car regs)))))))
11404
11405 (defun gnus-ask-server-for-new-groups ()
11406   (let* ((date (or gnus-newsrc-last-checked-date (current-time-string)))
11407          (methods (cons gnus-select-method 
11408                         (append
11409                          (and (consp gnus-check-new-newsgroups)
11410                               gnus-check-new-newsgroups)
11411                          gnus-secondary-select-methods)))
11412          (groups 0)
11413          (new-date (current-time-string))
11414          hashtb group new-newsgroups got-new)
11415     ;; Go thorugh both primary and secondary select methods and
11416     ;; request new newsgroups.  
11417     (while methods
11418       (and (or (gnus-server-opened (car methods))
11419                (gnus-open-server (car methods)))
11420            (gnus-request-newgroups date (car methods))
11421            (save-excursion
11422              (setq got-new t)
11423              (set-buffer nntp-server-buffer)
11424              (or hashtb (setq hashtb (gnus-make-hashtable 
11425                                       (count-lines (point-min) (point-max)))))
11426              ;; Enter all the new groups in a hashtable.
11427              (gnus-active-to-gnus-format (car methods) hashtb)))
11428       (setq methods (cdr methods)))
11429     (and got-new (setq gnus-newsrc-last-checked-date new-date))
11430     ;; Now all new groups from all select methods are in `hashtb'.
11431     (mapatoms
11432      (lambda (group-sym)
11433        (setq group (symbol-name group-sym))
11434        (if (or (gnus-gethash group gnus-newsrc-hashtb)
11435                (member group gnus-zombie-list)
11436                (member group gnus-killed-list))
11437            ;; The group is already known.
11438            ()
11439          (gnus-sethash group (symbol-value group-sym) gnus-active-hashtb)
11440          (let ((do-sub (gnus-matches-options-n group)))
11441            (cond ((eq do-sub 'subscribe)
11442                   (setq groups (1+ groups))
11443                   (gnus-sethash group group gnus-killed-hashtb)
11444                   (funcall 
11445                    gnus-subscribe-options-newsgroup-method group))
11446                  ((eq do-sub 'ignore)
11447                   nil)
11448                  (t
11449                   (setq groups (1+ groups))
11450                   (gnus-sethash group group gnus-killed-hashtb)
11451                   (if gnus-subscribe-hierarchical-interactive
11452                       (setq new-newsgroups (cons group new-newsgroups))
11453                     (funcall gnus-subscribe-newsgroup-method group)))))))
11454      hashtb)
11455     (if new-newsgroups 
11456         (gnus-subscribe-hierarchical-interactive new-newsgroups))
11457     ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
11458     (if (> groups 0)
11459         (gnus-message 6 "%d new newsgroup%s arrived." 
11460                       groups (if (> groups 1) "s have" " has")))
11461     got-new))
11462
11463 (defun gnus-check-first-time-used ()
11464   (if (or (> (length gnus-newsrc-alist) 1)
11465           (file-exists-p gnus-startup-file)
11466           (file-exists-p (concat gnus-startup-file ".el"))
11467           (file-exists-p (concat gnus-startup-file ".eld")))
11468       nil
11469     (gnus-message 6 "First time user; subscribing you to default groups")
11470     (or gnus-have-read-active-file (gnus-read-active-file))
11471     (setq gnus-newsrc-last-checked-date (current-time-string))
11472     (let ((groups gnus-default-subscribed-newsgroups)
11473           group)
11474       (if (eq groups t)
11475           nil
11476         (setq groups (or groups gnus-backup-default-subscribed-newsgroups))
11477         (mapatoms
11478          (lambda (sym)
11479            (setq group (symbol-name sym))
11480            (let ((do-sub (gnus-matches-options-n group)))
11481              (cond ((eq do-sub 'subscribe)
11482                     (gnus-sethash group group gnus-killed-hashtb)
11483                     (funcall 
11484                      gnus-subscribe-options-newsgroup-method group))
11485                    ((eq do-sub 'ignore)
11486                     nil)
11487                    (t
11488                     (setq gnus-killed-list (cons group gnus-killed-list))))))
11489          gnus-active-hashtb)
11490         (while groups
11491           (if (gnus-gethash (car groups) gnus-active-hashtb)
11492               (gnus-group-change-level 
11493                (car groups) gnus-level-default-subscribed gnus-level-killed))
11494           (setq groups (cdr groups)))
11495         (gnus-group-make-help-group)
11496         (and gnus-novice-user
11497              (gnus-message 7 "`A k' to list killed groups"))))))
11498
11499 (defun gnus-subscribe-group (group previous &optional method)
11500   (gnus-group-change-level 
11501    (if method
11502        (list t group gnus-level-default-subscribed nil nil method)
11503      group) 
11504    gnus-level-default-subscribed gnus-level-killed previous t))
11505
11506 ;; `gnus-group-change-level' is the fundamental function for changing
11507 ;; subscription levels of newsgroups. This might mean just changing
11508 ;; from level 1 to 2, which is pretty trivial, from 2 to 6 or back
11509 ;; again, which subscribes/unsubscribes a group, which is equally
11510 ;; trivial. Changing from 1-7 to 8-9 means that you kill a group, and
11511 ;; from 8-9 to 1-7 means that you remove the group from the list of
11512 ;; killed (or zombie) groups and add them to the (kinda) subscribed
11513 ;; groups. And last but not least, moving from 8 to 9 and 9 to 8,
11514 ;; which is trivial.
11515 ;; ENTRY can either be a string (newsgroup name) or a list (if
11516 ;; FROMKILLED is t, it's a list on the format (NUM INFO-LIST),
11517 ;; otherwise it's a list in the format of the `gnus-newsrc-hashtb'
11518 ;; entries. 
11519 ;; LEVEL is the new level of the group, OLDLEVEL is the old level and
11520 ;; PREVIOUS is the group (in hashtb entry format) to insert this group
11521 ;; after. 
11522 (defun gnus-group-change-level (entry level &optional oldlevel
11523                                       previous fromkilled)
11524   (let ((pinfo entry)
11525         group info active num)
11526     ;; Glean what info we can from the arguments
11527     (if (consp entry)
11528         (if fromkilled (setq group (nth 1 entry))
11529           (setq group (car (nth 2 entry))))
11530       (setq group entry))
11531     (if (and (stringp entry)
11532              oldlevel 
11533              (< oldlevel gnus-level-zombie))
11534         (setq entry (gnus-gethash entry gnus-newsrc-hashtb)))
11535     (if (and (not oldlevel)
11536              (consp entry))
11537         (setq oldlevel (car (cdr (nth 2 entry)))))
11538     (if (stringp previous)
11539         (setq previous (gnus-gethash previous gnus-newsrc-hashtb)))
11540
11541     (gnus-dribble-enter
11542      (format "(gnus-group-change-level %S %S %S %S %S)" 
11543              group level oldlevel (car (nth 2 previous)) fromkilled))
11544     
11545     ;; Then we remove the newgroup from any old structures, if needed.
11546     ;; If the group was killed, we remove it from the killed or zombie
11547     ;; list. If not, and it is in fact going to be killed, we remove
11548     ;; it from the newsrc hash table and assoc.
11549     (cond ((>= oldlevel gnus-level-zombie)
11550            (if (= oldlevel gnus-level-zombie)
11551                (setq gnus-zombie-list (delete group gnus-zombie-list))
11552              (setq gnus-killed-list (delete group gnus-killed-list))))
11553           (t
11554            (if (>= level gnus-level-zombie)
11555                (progn
11556                  (gnus-sethash (car (nth 2 entry))
11557                                nil gnus-newsrc-hashtb)
11558                  (if (nth 3 entry)
11559                      (setcdr (gnus-gethash (car (nth 3 entry))
11560                                            gnus-newsrc-hashtb)
11561                              (cdr entry)))
11562                  (setcdr (cdr entry) (cdr (cdr (cdr entry))))))))
11563
11564     ;; Finally we enter (if needed) the list where it is supposed to
11565     ;; go, and change the subscription level. If it is to be killed,
11566     ;; we enter it into the killed or zombie list.
11567     (cond ((>= level gnus-level-zombie)
11568            ;; Remove from the hash table.
11569            (gnus-sethash group nil gnus-newsrc-hashtb)
11570            (or (gnus-group-foreign-p group)
11571                ;; We do not enter foreign groups into the list of dead
11572                ;; groups.  
11573                (if (= level gnus-level-zombie)
11574                    (setq gnus-zombie-list (cons group gnus-zombie-list))
11575                  (setq gnus-killed-list (cons group gnus-killed-list)))))
11576           (t
11577            ;; If the list is to be entered into the newsrc assoc, and
11578            ;; it was killed, we have to create an entry in the newsrc
11579            ;; hashtb format and fix the pointers in the newsrc assoc.
11580            (if (>= oldlevel gnus-level-zombie)
11581                (progn
11582                  (if (listp entry)
11583                      (progn
11584                        (setq info (cdr entry))
11585                        (setq num (car entry)))
11586                    (setq active (gnus-gethash group gnus-active-hashtb))
11587                    (setq num (if active (- (1+ (cdr active)) (car active)) t))
11588                    ;; Check whether the group is foreign. If so, the
11589                    ;; foreign select method has to be entered into the
11590                    ;; info. 
11591                    (let ((method (gnus-group-method-name group)))
11592                      (if (eq method gnus-select-method)
11593                          (setq info (list group level nil))
11594                        (setq info (list group level nil nil method)))))
11595                  (or previous 
11596                      (setq previous 
11597                            (let ((p gnus-newsrc-alist))
11598                              (while (cdr (cdr p))
11599                                (setq p (cdr p)))
11600                              p)))
11601                  (setq entry (cons info (cdr (cdr previous))))
11602                  (if (cdr previous)
11603                      (progn
11604                        (setcdr (cdr previous) entry)
11605                        (gnus-sethash group (cons num (cdr previous)) 
11606                                      gnus-newsrc-hashtb))
11607                    (setcdr previous entry)
11608                    (gnus-sethash group (cons num previous)
11609                                  gnus-newsrc-hashtb))
11610                  (if (cdr entry)
11611                      (setcdr (gnus-gethash (car (car (cdr entry)))
11612                                            gnus-newsrc-hashtb)
11613                              entry)))
11614              ;; It was alive, and it is going to stay alive, so we
11615              ;; just change the level and don't change any pointers or
11616              ;; hash table entries.
11617              (setcar (cdr (car (cdr (cdr entry)))) level))))))
11618
11619 (defun gnus-kill-newsgroup (newsgroup)
11620   "Obsolete function. Kills a newsgroup."
11621   (gnus-group-change-level
11622    (gnus-gethash newsgroup gnus-newsrc-hashtb) gnus-level-killed))
11623
11624 (defun gnus-check-bogus-newsgroups (&optional confirm)
11625   "Remove bogus newsgroups.
11626 If CONFIRM is non-nil, the user has to confirm the deletion of every
11627 newsgroup." 
11628   (let ((newsrc (cdr gnus-newsrc-alist))
11629         bogus group entry)
11630     (gnus-message 5 "Checking bogus newsgroups...")
11631     (or gnus-have-read-active-file (gnus-read-active-file))
11632     ;; Find all bogus newsgroup that are subscribed.
11633     (while newsrc
11634       (setq group (car (car newsrc)))
11635       (if (or (gnus-gethash group gnus-active-hashtb) ; Active
11636               (nth 4 (car newsrc))      ; Foreign
11637               (and confirm
11638                    (not (gnus-y-or-n-p
11639                          (format "Remove bogus newsgroup: %s " group)))))
11640           ;; Don't remove.
11641           ()
11642         ;; Found a bogus newsgroup.
11643         (setq bogus (cons group bogus)))
11644       (setq newsrc (cdr newsrc)))
11645     ;; Remove all bogus subscribed groups by first killing them, and
11646     ;; then removing them from the list of killed groups.
11647     (while bogus
11648       (and (setq entry (gnus-gethash (car bogus) gnus-newsrc-hashtb))
11649            (progn
11650              (gnus-group-change-level entry gnus-level-killed)
11651              (setq gnus-killed-list (delete (car bogus) gnus-killed-list))))
11652       (setq bogus (cdr bogus)))
11653     ;; Then we remove all bogus groups from the list of killed and
11654     ;; zombie groups. They are are removed without confirmation.
11655     (let ((dead-lists '(gnus-killed-list gnus-zombie-list))
11656           killed)
11657       (while dead-lists
11658         (setq killed (symbol-value (car dead-lists)))
11659         (while killed
11660           (setq group (car killed))
11661           (or (gnus-gethash group gnus-active-hashtb)
11662               ;; The group is bogus.
11663               (set (car dead-lists)
11664                    (delete group (symbol-value (car dead-lists)))))
11665           (setq killed (cdr killed)))
11666         (setq dead-lists (cdr dead-lists))))
11667     (gnus-message 5 "Checking bogus newsgroups...done")))
11668
11669 (defun gnus-check-duplicate-killed-groups ()
11670   "Remove duplicates from the list of killed groups."
11671   (interactive)
11672   (let ((killed gnus-killed-list))
11673     (while killed
11674       (gnus-message 9 "%d" (length killed))
11675       (setcdr killed (delete (car killed) (cdr killed)))
11676       (setq killed (cdr killed)))))
11677
11678 ;; Go though `gnus-newsrc-alist' and compare with `gnus-active-hashtb'
11679 ;; and compute how many unread articles there are in each group.
11680 (defun gnus-get-unread-articles (&optional level) 
11681   (let* ((newsrc (cdr gnus-newsrc-alist))
11682          (conditional level)
11683          (level (or level (1+ gnus-level-subscribed)))
11684          info group active virtuals method)
11685     (gnus-message 5 "Checking new news...")
11686     (while newsrc
11687       (setq info (car newsrc))
11688       (setq group (car info))
11689       (setq active (gnus-gethash group gnus-active-hashtb))
11690
11691       ;; Check newsgroups. If the user doesn't want to check them, or
11692       ;; they can't be checked (for instance, if the news server can't
11693       ;; be reached) we just set the number of unread articles in this
11694       ;; newsgroup to t. This means that Gnus thinks that there are
11695       ;; unread articles, but it has no idea how many.
11696       (if (and (setq method (nth 4 info))
11697                (not (gnus-server-equal gnus-select-method
11698                                        (gnus-server-get-method nil method))))
11699           ;; These groups are foreign.
11700           (if (or (and gnus-activate-foreign-newsgroups 
11701                        (not (numberp gnus-activate-foreign-newsgroups)))
11702                   (and (numberp gnus-activate-foreign-newsgroups)
11703                        (<= (nth 1 info) gnus-activate-foreign-newsgroups)
11704                        (<= (nth 1 info) level))
11705                   (gnus-secondary-method-p method))
11706               (if (eq (car (if (stringp method) 
11707                                (gnus-server-to-method method)
11708                              (nth 4 info))) 'nnvirtual)
11709                   (setq virtuals (cons info virtuals))
11710                 (setq active (gnus-activate-newsgroup (car info)))))
11711         ;; These groups are native.
11712         (if (and (not gnus-read-active-file)
11713                  (<= (nth 1 info) level))
11714             (progn
11715               (setq active (gnus-activate-newsgroup (car info))))))
11716       
11717       (or active (progn (gnus-sethash group nil gnus-active-hashtb)
11718                         (setcar (gnus-gethash group gnus-newsrc-hashtb) t)))
11719       (and active 
11720            (gnus-get-unread-articles-in-group info active)
11721            ;; Close the groups as we look at them!
11722            (gnus-close-group group))
11723       (setq newsrc (cdr newsrc)))
11724
11725     ;; Activate the virtual groups. This has to be done after all the
11726     ;; other groups. 
11727     ;; !!! If one virtual group contains another virtual group, even
11728     ;; doing it this way might cause problems.
11729    (while virtuals
11730       (and (setq active (gnus-activate-newsgroup (car (car virtuals))))
11731            (gnus-get-unread-articles-in-group (car virtuals) active))
11732       (setq virtuals (cdr virtuals)))
11733
11734     (gnus-message 5 "Checking new news...done")))
11735
11736 ;; Create a hash table out of the newsrc alist. The `car's of the
11737 ;; alist elements are used as keys.
11738 (defun gnus-make-hashtable-from-newsrc-alist ()
11739   (let ((alist gnus-newsrc-alist)
11740         (ohashtb gnus-newsrc-hashtb)
11741         prev)
11742     (setq gnus-newsrc-hashtb (gnus-make-hashtable (length alist)))
11743     (setq alist 
11744           (setq prev (setq gnus-newsrc-alist 
11745                            (if (equal (car (car gnus-newsrc-alist))
11746                                       "dummy.group")
11747                                gnus-newsrc-alist
11748                              (cons (list "dummy.group" 0 nil) alist)))))
11749     (while alist
11750       (gnus-sethash (car (car alist)) 
11751                     (cons (and ohashtb (car (gnus-gethash 
11752                                              (car (car alist)) ohashtb))) 
11753                           prev) gnus-newsrc-hashtb)
11754       (setq prev alist
11755             alist (cdr alist)))))
11756
11757 (defun gnus-make-hashtable-from-killed ()
11758   "Create a hash table from the killed and zombie lists."
11759   (let ((lists '(gnus-killed-list gnus-zombie-list))
11760         list)
11761     (setq gnus-killed-hashtb 
11762           (gnus-make-hashtable 
11763            (+ (length gnus-killed-list) (length gnus-zombie-list))))
11764     (while lists
11765       (setq list (symbol-value (car lists)))
11766       (setq lists (cdr lists))
11767       (while list
11768         (gnus-sethash (car list) (car list) gnus-killed-hashtb)
11769         (setq list (cdr list))))))
11770
11771 (defun gnus-get-unread-articles-in-group (info active)
11772   (let* ((range (nth 2 info))
11773          (num 0)
11774          (marked (nth 3 info))
11775          srange lowest group highest)
11776     ;; If a cache is present, we may have to alter the active info.
11777     (and gnus-use-cache
11778          (gnus-cache-possibly-alter-active (car info) active))
11779     ;; Modify the list of read articles according to what articles 
11780     ;; are available; then tally the unread articles and add the
11781     ;; number to the group hash table entry.
11782     (cond ((zerop (cdr active))
11783            (setq num 0))
11784           ((not range)
11785            (setq num (- (1+ (cdr active)) (car active))))
11786           ((not (listp (cdr range)))
11787            ;; Fix a single (num . num) range according to the
11788            ;; active hash table.
11789            ;; Fix by Carsten Bormann <cabo@Informatik.Uni-Bremen.DE>.
11790            (and (< (cdr range) (car active)) (setcdr range (1- (car active))))
11791            (and (> (cdr range) (cdr active)) (setcdr range (cdr active)))
11792            ;; Compute number of unread articles.
11793            (setq num (max 0 (- (cdr active) 
11794                                (- (1+ (cdr range)) (car range))))))
11795           (t
11796            ;; The read list is a list of ranges. Fix them according to
11797            ;; the active hash table.
11798            ;; First peel off any elements that are below the lower
11799            ;; active limit. 
11800            (while (and (cdr range) 
11801                        (>= (car active) 
11802                            (or (and (atom (car (cdr range))) (car (cdr range)))
11803                                (car (car (cdr range))))))
11804              (if (numberp (car range))
11805                  (setcar range 
11806                          (cons (car range) 
11807                                (or (and (numberp (car (cdr range)))
11808                                         (car (cdr range))) 
11809                                    (cdr (car (cdr range))))))
11810                (setcdr (car range) 
11811                        (or (and (numberp (nth 1 range)) (nth 1 range))
11812                            (cdr (car (cdr range))))))
11813              (setcdr range (cdr (cdr range))))
11814            ;; Adjust the first element to be the same as the lower limit. 
11815            (if (and (not (atom (car range))) 
11816                     (< (cdr (car range)) (car active)))
11817                (setcdr (car range) (1- (car active))))
11818            ;; Then we want to peel off any elements that are higher
11819            ;; than the upper active limit.  
11820            (let ((srange range))
11821              ;; Go past all legal elements.
11822              (while (and (cdr srange) 
11823                          (<= (or (and (atom (car (cdr srange)))
11824                                       (car (cdr srange)))
11825                                  (car (car (cdr srange)))) (cdr active)))
11826                (setq srange (cdr srange)))
11827              (if (cdr srange)
11828                  ;; Nuke all remaining illegal elements.
11829                  (setcdr srange nil))
11830
11831              ;; Adjust the final element.
11832              (if (and (not (atom (car srange)))
11833                       (> (cdr (car srange)) (cdr active)))
11834                  (setcdr (car srange) (cdr active))))
11835            ;; Compute the number of unread articles.
11836            (while range
11837              (setq num (+ num (- (1+ (or (and (atom (car range)) (car range))
11838                                          (cdr (car range))))
11839                                  (or (and (atom (car range)) (car range))
11840                                      (car (car range))))))
11841              (setq range (cdr range)))
11842            (setq num (max 0 (- (cdr active) num)))))
11843     (and info
11844          (progn
11845            (and (assq 'tick marked)
11846                 (inline (gnus-remove-illegal-marked-articles
11847                          (assq 'tick marked) (nth 2 info))))
11848            (and (assq 'dormant marked)
11849                 (inline (gnus-remove-illegal-marked-articles
11850                          (assq 'dormant marked) (nth 2 info))))
11851            (setcar
11852             (gnus-gethash (car info) gnus-newsrc-hashtb) 
11853             (setq num (max 0 (- num (length (cdr (assq 'tick marked)))
11854                                 (length (cdr (assq 'dormant marked)))))))))
11855     num))
11856
11857 (defun gnus-remove-illegal-marked-articles (marked ranges)
11858   (let ((m (cdr marked)))
11859     ;; Make sure that all ticked articles are a subset of the unread
11860     ;; articles. 
11861     (while m
11862       (if (gnus-member-of-range (car m) ranges)
11863           (setcdr marked (cdr m))
11864         (setq marked m))
11865       (setq m (cdr m)))))
11866
11867 (defun gnus-activate-newsgroup (group)
11868   (let ((method (gnus-find-method-for-group group))
11869         active)
11870     (and (or (gnus-server-opened method) (gnus-open-server method))
11871          (gnus-request-group group)
11872          (save-excursion
11873            (set-buffer nntp-server-buffer)
11874            (goto-char (point-min))
11875            (and (looking-at "[0-9]+ [0-9]+ \\([0-9]+\\) [0-9]+")
11876                 (progn
11877                   (goto-char (match-beginning 1))
11878                   (gnus-sethash 
11879                    group (setq active (cons (read (current-buffer))
11880                                             (read (current-buffer))))
11881                    gnus-active-hashtb))
11882                 active)))))
11883
11884 (defun gnus-update-read-articles 
11885   (group unread unselected ticked &optional domarks replied expirable killed
11886          dormant bookmark score)
11887   "Update the list of read and ticked articles in GROUP using the
11888 UNREAD and TICKED lists.
11889 Note: UNSELECTED has to be sorted over `<'.
11890 Returns whether the updating was successful."
11891   (let* ((active (or gnus-newsgroup-active 
11892                      (gnus-gethash group gnus-active-hashtb)))
11893          (entry (gnus-gethash group gnus-newsrc-hashtb))
11894          (number (car entry))
11895          (info (nth 2 entry))
11896          (marked (nth 3 info))
11897          (prev 1)
11898          (unread (sort (copy-sequence unread) (function <)))
11899          last read)
11900     (if (or (not info) (not active))
11901         ;; There is no info on this group if it was, in fact,
11902         ;; killed. Gnus stores no information on killed groups, so
11903         ;; there's nothing to be done. 
11904         ;; One could store the information somewhere temporarily,
11905         ;; perhaps... Hmmm... 
11906         ()
11907       ;; Remove any negative articles numbers.
11908       (while (and unread (< (car unread) 0))
11909         (setq unread (cdr unread)))
11910       ;; Remove any expired article numbers
11911       (while (and unread (< (car unread) (car active)))
11912         (setq unread (cdr unread)))
11913       (while (and ticked (< (car ticked) (car active)))
11914         (setq ticked (cdr ticked)))
11915       (while (and dormant (< (car dormant) (car active)))
11916         (setq dormant (cdr dormant)))
11917       (setq unread (sort (append unselected unread) '<))
11918       ;; Set the number of unread articles in gnus-newsrc-hashtb.
11919       (setcar entry (max 0 (- (length unread) (length ticked) 
11920                               (length dormant))))
11921       ;; Compute the ranges of read articles by looking at the list of
11922       ;; unread articles.  
11923       (while unread
11924         (if (/= (car unread) prev)
11925             (setq read (cons (if (= prev (1- (car unread))) prev
11926                                (cons prev (1- (car unread)))) read)))
11927         (setq prev (1+ (car unread)))
11928         (setq unread (cdr unread)))
11929       (if (<= prev (cdr active))
11930           (setq read (cons (cons prev (cdr active)) read)))
11931       ;; Enter this list into the group info.
11932       (setcar (cdr (cdr info)) 
11933               (if (> (length read) 1) (nreverse read) read))
11934       ;; Enter the list of ticked articles.
11935       (gnus-set-marked-articles 
11936        info ticked
11937        (if domarks replied (cdr (assq 'reply marked)))
11938        (if domarks expirable (cdr (assq 'expire marked)))
11939        (if domarks killed (cdr (assq 'killed marked)))
11940        (if domarks dormant (cdr (assq 'dormant marked)))
11941        (if domarks bookmark (cdr (assq 'bookmark marked)))
11942        (if domarks score (cdr (assq 'score marked))))
11943       t)))
11944
11945 (defun gnus-make-articles-unread (group articles)
11946   "Mark ARTICLES in GROUP as unread."
11947   (let* ((info (nth 2 (or (gnus-gethash group gnus-newsrc-hashtb)
11948                           (gnus-gethash (gnus-group-real-name group)
11949                                         gnus-newsrc-hashtb))))
11950          (ranges (nth 2 info))
11951          news)
11952     (while articles
11953       (and (gnus-member-of-range (car articles) ranges)
11954            (setq news (cons (car articles) news)))
11955       (setq articles (cdr articles)))
11956     (if (not news)
11957         ()
11958       (setcar (nthcdr 2 info)
11959               (gnus-remove-from-range (nth 2 info) (nreverse news)))
11960       (gnus-group-update-group group t))))
11961
11962 (defun gnus-read-active-file ()
11963   "Get active file from NNTP server."
11964   (gnus-group-set-mode-line)
11965   (let ((methods (cons gnus-select-method gnus-secondary-select-methods))
11966         (not-first nil)
11967         list-type)
11968     (setq gnus-have-read-active-file nil)
11969     (save-excursion
11970       (set-buffer nntp-server-buffer)
11971       (while methods
11972         (let* ((where (nth 1 (car methods)))
11973                (mesg (format "Reading active file%s via %s..."
11974                              (if (and where (not (zerop (length where))))
11975                                  (concat " from " where) "")
11976                              (car (car methods)))))
11977           (gnus-message 5 mesg)
11978           (gnus-check-news-server (car methods))
11979           (cond 
11980            ((and (eq gnus-read-active-file 'some)
11981                  (gnus-check-backend-function
11982                   'retrieve-groups (car (car methods))))
11983             (let ((newsrc (cdr gnus-newsrc-alist))
11984                   groups)
11985               (while newsrc
11986                 (and (gnus-server-equal 
11987                       (gnus-find-method-for-group
11988                        (car (car newsrc)) (car newsrc))
11989                       (gnus-server-get-method nil (car methods)))
11990                      (setq groups (cons (car (car newsrc)) groups)))
11991                 (setq newsrc (cdr newsrc)))
11992               (setq list-type (gnus-retrieve-groups groups (car methods)))
11993               (cond ((not list-type)
11994                      (gnus-message 
11995                       1 "Cannot read partial active file from %s server." 
11996                       (car (car methods)))
11997                      (ding)
11998                      (sit-for 2))
11999                     ((eq list-type 'active)
12000                      (gnus-active-to-gnus-format (and not-first (car methods)))
12001                      (setq not-first t))
12002                     (t
12003                      (gnus-groups-to-gnus-format (and not-first (car methods)))
12004                      (setq not-first t)))))
12005            (t
12006             (if (not (gnus-request-list (car methods)))
12007                 (progn
12008                   (gnus-message 1 "Cannot read active file from %s server." 
12009                                 (car (car methods)))
12010                   (ding))
12011               (gnus-active-to-gnus-format 
12012                (and gnus-have-read-active-file (car methods)))
12013               (setq gnus-have-read-active-file t)
12014               (gnus-message 5 "%sdone" mesg)))))
12015         (setq methods (cdr methods))))))
12016
12017 ;; Read an active file and place the results in `gnus-active-hashtb'.
12018 (defun gnus-active-to-gnus-format (method &optional hashtb)
12019   (let ((cur (current-buffer))
12020         (hashtb (or hashtb 
12021                     (if method
12022                         gnus-active-hashtb
12023                       (setq gnus-active-hashtb
12024                             (gnus-make-hashtable 
12025                              (count-lines (point-min) (point-max))))))))
12026     ;; Delete unnecessary lines.
12027     (goto-char (point-min))
12028     (while (search-forward "\nto." nil t)
12029       (delete-region (match-beginning 0) (progn (forward-line 1) (point))))
12030     (or (string= gnus-ignored-newsgroups "")
12031         (progn
12032           (goto-char (point-min))
12033           (delete-matching-lines gnus-ignored-newsgroups)))
12034     ;; If these are groups from a foreign select method, we insert the
12035     ;; group prefix in front of the group names. 
12036     (and method (not (eq method gnus-select-method))
12037          (let ((prefix (gnus-group-prefixed-name "" method)))
12038            (goto-char (point-min))
12039            (while (and (not (eobp))
12040                        (progn (insert prefix)
12041                               (zerop (forward-line 1)))))))
12042     (goto-char (point-min))
12043     ;; Store active file in hashtable.
12044     (goto-char (point-min))
12045     (if (string-match "%[oO]" gnus-group-line-format)
12046         ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
12047         ;; If we want information on moderated groups, we use this
12048         ;; loop...   
12049         (let* ((mod-hashtb (make-vector 7 0))
12050                (m (intern "m" mod-hashtb))
12051                group max mod min)
12052           (while (not (eobp))
12053             (condition-case nil
12054                 (progn
12055                   (narrow-to-region (point) (gnus-point-at-eol))
12056                   (setq group (let ((obarray hashtb)) (read cur)))
12057                   (if (and (numberp (setq max (read cur)))
12058                            (numberp (setq min (read cur))))
12059                       (set group (cons min max))
12060                     (set group nil))
12061                   ;; Enter moderated groups into a list.
12062                   (if (eq (let ((obarray mod-hashtb)) (read cur)) m)
12063                       (setq gnus-moderated-list 
12064                             (cons (symbol-name group) gnus-moderated-list))))
12065               (error nil))
12066             (widen)
12067             (forward-line 1)))
12068       ;; And if we do not care about moderation, we use this loop,
12069       ;; which is faster.
12070       (let (group max min)
12071         (while (not (eobp))
12072           (condition-case ()
12073               (progn
12074                 (narrow-to-region (point) (gnus-point-at-eol))
12075                 ;; group gets set to a symbol interned in the hash table
12076                 ;; (what a hack!!)
12077                 (setq group (let ((obarray hashtb)) (read cur)))
12078                 (if (and (numberp (setq max (read cur)))
12079                          (numberp (setq min (read cur))))
12080                     (set group (cons min max))
12081                   (set group nil)))
12082             (error 
12083              (progn 
12084                (ding) 
12085                (gnus-message 3 "Warning - illegal active: %s"
12086                              (buffer-substring 
12087                               (gnus-point-at-bol) (gnus-point-at-eol)))
12088                nil)))
12089           (widen)
12090           (forward-line 1))))))
12091
12092 (defun gnus-groups-to-gnus-format (method &optional hashtb)
12093   ;; Parse a "groups" active file.
12094   (let ((cur (current-buffer))
12095         (hashtb (or hashtb 
12096                     (if method
12097                         gnus-active-hashtb
12098                       (setq gnus-active-hashtb
12099                             (gnus-make-hashtable 
12100                              (count-lines (point-min) (point-max)))))))
12101         (prefix (and method (not (eq method gnus-select-method))
12102                      (gnus-group-prefixed-name "" method))))
12103
12104     (goto-char (point-min))
12105     (condition-case ()
12106         ;; We split this into to separate loops, one with the prefix
12107         ;; and one without to speed the reading up somewhat.
12108         (if prefix
12109             (let (min max opoint)
12110               (while (not (eobp))
12111                 (read cur) (read cur)
12112                 (setq min (read cur)
12113                       max (read cur)
12114                       opoint (point))
12115                 (skip-chars-forward " \t")
12116                 (insert prefix)
12117                 (goto-char opoint)
12118                 (set (let ((obarray hashtb)) (read cur)) 
12119                      (cons min max))
12120                 (forward-line 1)))
12121           (let (min max opoint)
12122             (while (not (eobp))
12123               (if (= (following-char) ?2)
12124                   (progn
12125                     (read cur) (read cur)
12126                     (setq min (read cur)
12127                           max (read cur))
12128                     (set (let ((obarray hashtb)) (read cur)) 
12129                          (cons min max))))
12130               (forward-line 1))))
12131       (error 
12132        (progn (ding) (gnus-message 3 "Possible error in active file."))))))
12133
12134 (defun gnus-read-newsrc-file (&optional force)
12135   "Read startup file.
12136 If FORCE is non-nil, the .newsrc file is read."
12137   (setq gnus-current-startup-file (gnus-make-newsrc-file gnus-startup-file))
12138   ;; Reset variables that might be defined in the .newsrc.eld file.
12139   (let ((variables gnus-variable-list))
12140     (while variables
12141       (set (car variables) nil)
12142       (setq variables (cdr variables))))
12143   (let* ((newsrc-file gnus-current-startup-file)
12144          (quick-file (concat newsrc-file ".el")))
12145     (save-excursion
12146       ;; We always load the .newsrc.eld file. If always contains
12147       ;; much information that can not be gotten from the .newsrc
12148       ;; file (ticked articles, killed groups, foreign methods, etc.)
12149       (gnus-read-newsrc-el-file quick-file)
12150  
12151       (if (or force
12152               (and (file-newer-than-file-p newsrc-file quick-file)
12153                    (file-newer-than-file-p newsrc-file 
12154                                            (concat quick-file "d")))
12155               (not gnus-newsrc-alist))
12156           ;; We read the .newsrc file. Note that if there if a
12157           ;; .newsrc.eld file exists, it has already been read, and
12158           ;; the `gnus-newsrc-hashtb' has been created. While reading
12159           ;; the .newsrc file, Gnus will only use the information it
12160           ;; can find there for changing the data already read -
12161           ;; ie. reading the .newsrc file will not trash the data
12162           ;; already read (except for read articles).
12163           (save-excursion
12164             (gnus-message 5 "Reading %s..." newsrc-file)
12165             (set-buffer (find-file-noselect newsrc-file))
12166             (buffer-disable-undo (current-buffer))
12167             (gnus-newsrc-to-gnus-format)
12168             (kill-buffer (current-buffer))
12169             (gnus-message 5 "Reading %s...done" newsrc-file))))))
12170
12171 (defun gnus-read-newsrc-el-file (file)
12172   (let ((ding-file (concat file "d")))
12173     ;; We always, always read the .eld file.
12174     (gnus-message 5 "Reading %s..." ding-file)
12175     (let (gnus-newsrc-assoc)
12176       (condition-case nil
12177           (load ding-file t t t)
12178         (error nil))
12179       (and gnus-newsrc-assoc (setq gnus-newsrc-alist gnus-newsrc-assoc)))
12180     (let ((inhibit-quit t))
12181       (gnus-uncompress-newsrc-assoc))
12182     (gnus-make-hashtable-from-newsrc-alist)
12183     (if (not (file-newer-than-file-p file ding-file))
12184         ()
12185       ;; Old format quick file
12186       (gnus-message 5 "Reading %s..." file)
12187       ;; The .el file is newer than the .eld file, so we read that one
12188       ;; as well. 
12189       (gnus-read-old-newsrc-el-file file))))
12190
12191 ;; Parse the old-style quick startup file
12192 (defun gnus-read-old-newsrc-el-file (file)
12193   (let (newsrc killed marked group g m len info)
12194     (prog1
12195         (let ((gnus-killed-assoc nil)
12196               gnus-marked-assoc gnus-newsrc-alist gnus-newsrc-assoc)
12197           (prog1
12198               (condition-case nil
12199                   (load file t t t)
12200                 (error nil))
12201             (setq newsrc gnus-newsrc-assoc
12202                   killed gnus-killed-assoc
12203                   marked gnus-marked-assoc)))
12204       (setq gnus-newsrc-alist nil)
12205       (while newsrc
12206         (setq group (car newsrc))
12207         (let ((info (nth 2 (gnus-gethash (car group) gnus-newsrc-hashtb))))
12208           (if info
12209               (progn
12210                 (setcar (nthcdr 2 info) (cdr (cdr group)))
12211                 (setcar (cdr info)
12212                         (if (nth 1 group) gnus-level-default-subscribed 
12213                           gnus-level-default-unsubscribed))
12214                 (setq gnus-newsrc-alist (cons info gnus-newsrc-alist)))
12215             (setq gnus-newsrc-alist
12216                   (cons 
12217                    (setq info
12218                          (list (car group)
12219                                (if (nth 1 group) gnus-level-default-subscribed
12220                                  gnus-level-default-unsubscribed) 
12221                                (cdr (cdr group))))
12222                    gnus-newsrc-alist)))
12223           (if (setq m (assoc (car group) marked))
12224             (setcdr (cdr (cdr info)) (cons (list (cons 'tick (cdr m))) nil))))
12225         (setq newsrc (cdr newsrc)))
12226       (setq newsrc killed)
12227       (while newsrc
12228         (setcar newsrc (car (car newsrc)))
12229         (setq newsrc (cdr newsrc)))
12230       (setq gnus-killed-list killed))
12231     (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist))
12232     (gnus-make-hashtable-from-newsrc-alist)))
12233       
12234 (defun gnus-make-newsrc-file (file)
12235   "Make server dependent file name by catenating FILE and server host name."
12236   (let* ((file (expand-file-name file nil))
12237          (real-file (concat file "-" (nth 1 gnus-select-method))))
12238     (if (file-exists-p real-file)
12239         real-file file)))
12240
12241 (defun gnus-uncompress-newsrc-assoc ()
12242   ;; Uncompress all lists of marked articles in the newsrc assoc.
12243   (let ((newsrc gnus-newsrc-alist)
12244         marked)
12245     (while newsrc
12246       (if (not (setq marked (nth 3 (car newsrc))))
12247           ()
12248         (while marked
12249           (or (eq 'score (car (car marked)))
12250               (eq 'bookmark (car (car marked)))
12251               (eq 'killed (car (car marked)))
12252               (setcdr (car marked) (gnus-uncompress-range (cdr (car marked)))))
12253           (setq marked (cdr marked))))
12254       (setq newsrc (cdr newsrc)))))
12255
12256 (defun gnus-compress-newsrc-assoc ()
12257   ;; Compress all lists of marked articles in the newsrc assoc.
12258   (let ((newsrc gnus-newsrc-alist)
12259         marked)
12260     (while newsrc
12261       (if (not (setq marked (nth 3 (car newsrc))))
12262           ()
12263         (while marked
12264           (or (eq 'score (car (car marked)))
12265               (eq 'bookmark (car (car marked)))
12266               (eq 'killed (car (car marked)))
12267               (setcdr (car marked) 
12268                       (gnus-compress-sequence (sort (cdr (car marked)) '<) t)))
12269           (setq marked (cdr marked))))
12270       (setq newsrc (cdr newsrc)))))
12271
12272 (defun gnus-newsrc-to-gnus-format ()
12273   (setq gnus-newsrc-options "")
12274   (or gnus-active-hashtb
12275       (setq gnus-active-hashtb (make-vector 4095 0)))
12276   (let ((buf (current-buffer))
12277         (already-read (> (length gnus-newsrc-alist) 1))
12278         group level subscribed info options-symbol newsrc
12279         symbol reads num1)
12280     (goto-char (point-min))
12281     ;; We intern the symbol `options' in the active hashtb so that we
12282     ;; can `eq' against it later.
12283     (setq options-symbol (intern "options" gnus-active-hashtb))
12284   
12285     (while (not (eobp))
12286       ;; We first read the first word on the line by narrowing and
12287       ;; then reading into `gnus-active-hashtb'.  Most groups will
12288       ;; already exist in that hashtb, so this will save some string
12289       ;; space.
12290       (narrow-to-region
12291        (point)
12292        (progn (skip-chars-forward "^ \t!:\n") (point)))
12293       (goto-char (point-min))
12294       (setq symbol 
12295             (and (/= (point-min) (point-max))
12296                  (let ((obarray gnus-active-hashtb)) (read buf))))
12297       (widen)
12298       ;; Now, the symbol we have read is either `options' or a group
12299       ;; name.  If it is an options line, we just add it to a string. 
12300       (cond 
12301        ((eq symbol options-symbol)
12302         (setq gnus-newsrc-options
12303               ;; This concatting is quite inefficient, but since our
12304               ;; thorough studies show that approx 99.37% of all
12305               ;; .newsrc files only contain a single options line, we
12306               ;; don't give a damn, frankly, my dear.
12307               (concat gnus-newsrc-options
12308                       (buffer-substring 
12309                        (gnus-point-at-bol)
12310                        ;; Options may continue on the next line.
12311                        (or (and (re-search-forward "^[^ \t]" nil 'move)
12312                                 (progn (beginning-of-line) (point)))
12313                            (point))))))
12314        (symbol
12315         ;; It was a group name.
12316         (setq subscribed (= (following-char) ?:)
12317               group (symbol-name symbol)
12318               reads nil)
12319         (if (eolp)
12320             ;; If the line ends here, this is clearly a buggy line, so
12321             ;; we put point a the beginning of line and let the cond
12322             ;; below do the error handling.
12323             (beginning-of-line)
12324           ;; We skip to the beginning of the ranges.
12325           (skip-chars-forward "!: \t"))
12326         ;; We are now at the beginning of the list of read articles.
12327         ;; We read them range by range.
12328         (while
12329             (cond 
12330              ((looking-at "[0-9]+")
12331               ;; We narrow and read a number instead of buffer-substring/
12332               ;; string-to-int because it's faster. narrow/widen is
12333               ;; faster than save-restriction/narrow, and save-restriction
12334               ;; produces a garbage object.
12335               (setq num1 (progn
12336                            (narrow-to-region (match-beginning 0) (match-end 0))
12337                            (read buf)))
12338               (widen)
12339               ;; If the next character is a dash, then this is a range.
12340               (if (= (following-char) ?-)
12341                   (progn
12342                     ;; We read the upper bound of the range.
12343                     (forward-char 1)
12344                     (if (not (looking-at "[0-9]+"))
12345                         ;; This is a buggy line, by we pretend that
12346                         ;; it's kinda OK. Perhaps the user should be
12347                         ;; dinged? 
12348                         (setq reads (cons num1 reads))
12349                       (setq reads 
12350                             (cons 
12351                              (cons num1 (progn
12352                                           (narrow-to-region (match-beginning 0) 
12353                                                             (match-end 0))
12354                                           (read buf)))
12355                              reads))
12356                       (widen)))
12357                 ;; It was just a simple number, so we add it to the
12358                 ;; list of ranges.
12359                 (setq reads (cons num1 reads)))
12360               ;; If the next char in ?\n, then we have reached the end
12361               ;; of the line and return nil.
12362               (/= (following-char) ?\n))
12363              ((= (following-char) ?\n)
12364               ;; End of line, so we end.
12365               nil)
12366              (t
12367               ;; Not numbers and not eol, so this might be a buggy
12368               ;; line... 
12369               (or (eobp) ; If it was eob instead of ?\n, we allow it.
12370                   (progn
12371                     ;; The line was buggy.
12372                     (setq group nil)
12373                     (gnus-message 3 "Mangled line: %s" 
12374                                   (buffer-substring (gnus-point-at-bol) 
12375                                                     (gnus-point-at-eol)))
12376                     (ding)
12377                     (sit-for 1)))
12378               nil))
12379           ;; Skip past ", ". Spaces are illegal in these ranges, but
12380           ;; we allow them, because it's a common mistake to put a
12381           ;; space after the comma.
12382           (skip-chars-forward ", "))
12383
12384         ;; We have already read .newsrc.eld, so we gently update the
12385         ;; data in the hash table with the information we have just
12386         ;; read. 
12387         (if (not group)
12388             ()
12389           (let ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
12390                 level)
12391             (if info
12392                 ;; There is an entry for this file in the alist.
12393                 (progn
12394                   (setcar (nthcdr 2 info) (nreverse reads))
12395                   ;; We update the level very gently.  In fact, we
12396                   ;; only change it if there's been a status change
12397                   ;; from subscribed to unsubscribed, or vice versa.
12398                   (setq level (nth 1 info))
12399                   (cond ((and (<= level gnus-level-subscribed)
12400                               (not subscribed))
12401                          (setq level (if reads
12402                                          gnus-level-default-unsubscribed 
12403                                        (1+ gnus-level-default-unsubscribed))))
12404                         ((and (> level gnus-level-subscribed) subscribed)
12405                          (setq level gnus-level-default-subscribed)))
12406                   (setcar (cdr info) level))
12407               ;; This is a new group.
12408               (setq info (list group 
12409                                (if subscribed
12410                                    gnus-level-default-subscribed 
12411                                  (if reads
12412                                      (1+ gnus-level-subscribed)
12413                                    gnus-level-default-unsubscribed))
12414                                (nreverse reads))))
12415             (setq newsrc (cons info newsrc))))
12416         (forward-line 1))))
12417     
12418     (setq newsrc (nreverse newsrc))
12419
12420     (if (not already-read)
12421         ()
12422       ;; We now have two newsrc lists - `newsrc', which is what we
12423       ;; have read from .newsrc, and `gnus-newsrc-alist', which is
12424       ;; what we've read from .newsrc.eld. We have to merge these
12425       ;; lists. We do this by "attaching" any (foreign) groups in the
12426       ;; gnus-newsrc-alist to the (native) group that precedes them. 
12427       (let ((rc (cdr gnus-newsrc-alist))
12428             (prev gnus-newsrc-alist)
12429             entry mentry)
12430         (while rc
12431           (or (assoc (car (car rc)) newsrc) ; It's already in the alist.
12432               (null (nth 4 (car rc))) ; It's a native group.
12433               (if (setq entry (assoc (car (car prev)) newsrc))
12434                   (setcdr (setq mentry (memq entry newsrc))
12435                           (cons (car rc) (cdr mentry)))
12436                 (setq newsrc (cons (car rc) newsrc))))
12437           (setq prev rc
12438                 rc (cdr rc)))))
12439
12440     (setq gnus-newsrc-alist newsrc)
12441     ;; We make the newsrc hashtb.
12442     (gnus-make-hashtable-from-newsrc-alist)
12443
12444     ;; Finally, if we read some options lines, we parse them.
12445     (or (string= gnus-newsrc-options "")
12446         (gnus-newsrc-parse-options gnus-newsrc-options))))
12447
12448 ;; Parse options lines to find "options -n !all rec.all" and stuff.
12449 ;; The return value will be a list on the form
12450 ;; ((regexp1 . ignore)
12451 ;;  (regexp2 . subscribe)...)
12452 ;; When handling new newsgroups, groups that match a `ignore' regexp
12453 ;; will be ignored, and groups that match a `subscribe' regexp will be
12454 ;; subscribed. A line like
12455 ;; options -n !all rec.all
12456 ;; will lead to a list that looks like
12457 ;; (("^rec\\..+" . subscribe) 
12458 ;;  ("^.+" . ignore))
12459 ;; So all "rec.*" groups will be subscribed, while all the other
12460 ;; groups will be ignored. Note that "options -n !all rec.all" is very
12461 ;; different from "options -n rec.all !all". 
12462 (defun gnus-newsrc-parse-options (options)
12463   (let (out eol)
12464     (save-excursion
12465       (gnus-set-work-buffer)
12466       (insert (regexp-quote options))
12467       ;; First we treat all continuation lines.
12468       (goto-char (point-min))
12469       (while (re-search-forward "\n[ \t]+" nil t)
12470         (replace-match " " t t))
12471       ;; Then we transform all "all"s into ".+"s.
12472       (goto-char (point-min))
12473       (while (re-search-forward "\\ball\\b" nil t)
12474         (replace-match ".+" t t))
12475       (goto-char (point-min))
12476       ;; We remove all other options than the "-n" ones.
12477       (while (re-search-forward "[ \t]-[^n][^-]*" nil t)
12478         (replace-match " ")
12479         (forward-char -1))
12480       (goto-char (point-min))
12481
12482       ;; We are only interested in "options -n" lines - we
12483       ;; ignore the other option lines.
12484       (while (re-search-forward "[ \t]-n" nil t)
12485         (setq eol 
12486               (or (save-excursion
12487                     (and (re-search-forward "[ \t]-n" (gnus-point-at-eol) t)
12488                          (- (point) 2)))
12489                   (gnus-point-at-eol)))
12490         ;; Search for all "words"...
12491         (while (re-search-forward "[^ \t,\n-]+" eol t)
12492           (if (= (char-after (match-beginning 0)) ?!)
12493               ;; If the word begins with a bang (!), this is a "not"
12494               ;; spec. We put this spec (minus the bang) and the
12495               ;; symbol `ignore' into the list.
12496               (setq out (cons (cons (concat 
12497                                      "^" (buffer-substring 
12498                                           (1+ (match-beginning 0))
12499                                           (match-end 0)))
12500                                     'ignore) out))
12501             ;; There was no bang, so this is a "yes" spec.
12502             (setq out (cons (cons (concat 
12503                                    "^" (buffer-substring (match-beginning 0)
12504                                                          (match-end 0)))
12505                                   'subscribe) out)))))
12506     
12507       (setq gnus-newsrc-options-n out))))
12508                
12509
12510 (defun gnus-save-newsrc-file ()
12511   "Save .newsrc file."
12512   ;; Note: We cannot save .newsrc file if all newsgroups are removed
12513   ;; from the variable gnus-newsrc-alist.
12514   (and (or gnus-newsrc-alist gnus-killed-list)
12515        gnus-current-startup-file
12516        (let ((make-backup-files t)
12517              (version-control nil)
12518              (require-final-newline t)) ;Don't ask even if requested.
12519          ;; You can stop or change version control of backup file.
12520          ;; Suggested by jason@violet.berkeley.edu.
12521          (run-hooks 'gnus-save-newsrc-hook)
12522          (save-excursion
12523            (if (or (not gnus-dribble-buffer)
12524                    (not (buffer-name gnus-dribble-buffer))
12525                    (zerop (save-excursion
12526                             (set-buffer gnus-dribble-buffer)
12527                             (buffer-size))))
12528                (gnus-message 4 "(No changes need to be saved)")
12529              (if gnus-save-newsrc-file
12530                  (progn
12531                    (gnus-message 5 "Saving %s..." gnus-current-startup-file)
12532                    ;; Make backup file of master newsrc.
12533                    (gnus-gnus-to-newsrc-format)
12534                    (gnus-message 5 "Saving %s...done"
12535                                  gnus-current-startup-file)))
12536              ;; Quickly loadable .newsrc.
12537              (set-buffer (get-buffer-create " *Gnus-newsrc*"))
12538              (gnus-add-current-to-buffer-list)
12539              (buffer-disable-undo (current-buffer))
12540              (erase-buffer)
12541              (gnus-message 5 "Saving %s.eld..." gnus-current-startup-file)
12542              (gnus-gnus-to-quick-newsrc-format)
12543              (write-region 1 (point-max) 
12544                            (concat gnus-current-startup-file ".eld") 
12545                            nil 'nomesg)
12546              (kill-buffer (current-buffer))
12547              (gnus-message 5 "Saving %s.eld...done" gnus-current-startup-file)
12548              (gnus-dribble-delete-file))))))
12549
12550 (defun gnus-gnus-to-quick-newsrc-format ()
12551   "Insert Gnus variables such as gnus-newsrc-alist in lisp format."
12552   (insert ";; (ding) Gnus startup file.\n")
12553   (insert ";; Never delete this file - touch .newsrc instead to force Gnus\n")
12554   (insert ";; to read .newsrc.\n")
12555   (let ((variables gnus-variable-list)
12556         (inhibit-quit t)
12557         (gnus-newsrc-alist (cdr gnus-newsrc-alist))
12558         variable)
12559     ;; insert lisp expressions.
12560     (gnus-compress-newsrc-assoc)
12561     (while variables
12562       (setq variable (car variables))
12563       (and (boundp variable)
12564            (symbol-value variable)
12565            (or gnus-save-killed-list (not (eq variable 'gnus-killed-list)))
12566            (insert "(setq " (symbol-name variable) " '"
12567                    (prin1-to-string (symbol-value variable))
12568                    ")\n"))
12569       (setq variables (cdr variables)))
12570     (gnus-uncompress-newsrc-assoc)))
12571
12572
12573 (defun gnus-gnus-to-newsrc-format ()
12574   ;; Generate and save the .newsrc file.
12575   (let ((newsrc (cdr gnus-newsrc-alist))
12576         info ranges range)
12577     (save-excursion
12578       (set-buffer (create-file-buffer gnus-startup-file))
12579       (buffer-disable-undo (current-buffer))
12580       (erase-buffer)
12581       ;; Write options.
12582       (if gnus-newsrc-options (insert gnus-newsrc-options))
12583       ;; Write subscribed and unsubscribed.
12584       (while newsrc
12585         (setq info (car newsrc))
12586         (if (not (nth 4 info))          ;Don't write foreign groups to .newsrc.
12587             (progn
12588               (insert (car info) (if (> (nth 1 info) gnus-level-subscribed)
12589                                      "!" ":"))
12590               (if (setq ranges (nth 2 info))
12591                   (progn
12592                     (insert " ")
12593                     (if (not (listp (cdr ranges)))
12594                         (if (= (car ranges) (cdr ranges))
12595                             (insert (int-to-string (car ranges)))
12596                           (insert (int-to-string (car ranges)) "-" 
12597                                   (int-to-string (cdr ranges))))
12598                       (while ranges
12599                         (setq range (car ranges)
12600                               ranges (cdr ranges))
12601                         (if (or (atom range) (= (car range) (cdr range)))
12602                             (insert (int-to-string 
12603                                      (or (and (atom range) range) 
12604                                          (car range))))
12605                           (insert (int-to-string (car range)) "-"
12606                                   (int-to-string (cdr range))))
12607                         (if ranges (insert ","))))))
12608               (insert "\n")))
12609         (setq newsrc (cdr newsrc)))
12610       (write-region 1 (point-max) gnus-current-startup-file nil 'nomesg)
12611       (kill-buffer (current-buffer)))))
12612
12613 (defun gnus-read-all-descriptions-files ()
12614   (let ((methods (nconc (list gnus-select-method) 
12615                         gnus-secondary-select-methods)))
12616     (while methods
12617       (gnus-read-descriptions-file (car methods))
12618       (setq methods (cdr methods)))
12619     t))
12620
12621 (defun gnus-read-descriptions-file (&optional method)
12622   (let ((method (or method gnus-select-method)))
12623     ;; We create the hashtable whether we manage to read the desc file
12624     ;; to avoid trying to re-read after a failed read.
12625     (or gnus-description-hashtb
12626         (setq gnus-description-hashtb 
12627               (gnus-make-hashtable (length gnus-active-hashtb))))
12628     ;; Mark this method's desc file as read.
12629     (gnus-sethash (gnus-group-prefixed-name "" method) "Has read"
12630                   gnus-description-hashtb)
12631
12632     (gnus-message 5 "Reading descriptions file via %s..." (car method))
12633     (cond 
12634      ((not (or (gnus-server-opened method)
12635                (gnus-open-server method)))
12636       (gnus-message 1 "Couldn't open server")
12637       nil)
12638      ((not (gnus-request-list-newsgroups method))
12639       (gnus-message 1 "Couldn't read newsgroups descriptions")
12640       nil)
12641      (t
12642       (let (group)
12643         (save-excursion
12644           (save-restriction
12645             (set-buffer nntp-server-buffer)
12646             (goto-char (point-min))
12647             (if (or (search-forward "\n.\n" nil t)
12648                     (goto-char (point-max)))
12649                 (progn
12650                   (beginning-of-line)
12651                   (narrow-to-region (point-min) (point))))
12652             (goto-char (point-min))
12653             (while (not (eobp))
12654               ;; If we get an error, we set group to 0, which is not a
12655               ;; symbol... 
12656               (setq group 
12657                     (condition-case ()
12658                         (let ((obarray gnus-description-hashtb))
12659                           ;; Group is set to a symbol interned in this
12660                           ;; hash table.
12661                           (read nntp-server-buffer))
12662                       (error 0)))
12663               (skip-chars-forward " \t")
12664               ;; ... which leads to this line being effectively ignored.
12665               (and (symbolp group)
12666                    (set group (buffer-substring 
12667                                (point) (progn (end-of-line) (point)))))
12668               (forward-line 1))))
12669         (gnus-message 5 "Reading descriptions file...done")
12670         t)))))
12671
12672 (defun gnus-group-get-description (group)
12673   ;; Get the description of a group by sending XGTITLE to the server.
12674   (and (gnus-request-group-description group)
12675        (save-excursion
12676          (set-buffer nntp-server-buffer)
12677          (goto-char (point-min))
12678          (and (looking-at "[^ \t]+[ \t]+\\(.*\\)")
12679               (buffer-substring (match-beginning 1) (match-end 1))))))
12680
12681 ;;;
12682 ;;; Server
12683 ;;;
12684
12685 (defvar gnus-server-mode-hook nil
12686   "Hook run in `gnus-server-mode' buffers.")
12687
12688 (defconst gnus-server-line-format "     {%(%h:%w%)}\n"
12689   "Format of server lines.
12690 It works along the same lines as a normal formatting string,
12691 with some simple extensions.")
12692
12693 (defvar gnus-server-mode-line-format "(ding) List of servers"
12694   "The format specification for the server mode line.")
12695
12696 (defconst gnus-server-line-format-alist
12697   (list (list ?h 'how ?s)
12698         (list ?n 'name ?s)
12699         (list ?w 'where ?s)
12700         ))
12701
12702 (defconst gnus-server-mode-line-format-alist 
12703   (list (list ?S 'news-server ?s)
12704         (list ?M 'news-method ?s)
12705         (list ?u 'user-defined ?s)))
12706
12707 (defvar gnus-server-line-format-spec nil)
12708 (defvar gnus-server-mode-line-format-spec nil)
12709 (defvar gnus-server-killed-servers nil)
12710
12711 (defvar gnus-server-mode-map nil)
12712 (put 'gnus-server-mode 'mode-class 'special)
12713
12714 (if gnus-server-mode-map
12715     nil
12716   (setq gnus-server-mode-map (make-sparse-keymap))
12717   (suppress-keymap gnus-server-mode-map)
12718   (define-key gnus-server-mode-map " " 'gnus-server-read-server)
12719   (define-key gnus-server-mode-map "\r" 'gnus-server-read-server)
12720   (define-key gnus-server-mode-map gnus-mouse-2 'gnus-server-pick-server)
12721   (define-key gnus-server-mode-map "q" 'gnus-server-exit)
12722   (define-key gnus-server-mode-map "l" 'gnus-server-list-servers)
12723   (define-key gnus-server-mode-map "k" 'gnus-server-kill-server)
12724   (define-key gnus-server-mode-map "y" 'gnus-server-yank-server)
12725   (define-key gnus-server-mode-map "c" 'gnus-server-copy-server)
12726   (define-key gnus-server-mode-map "a" 'gnus-server-add-server)
12727   (define-key gnus-server-mode-map "e" 'gnus-server-edit-server))
12728
12729 (defun gnus-server-mode ()
12730   "Major mode for listing and editing servers.
12731
12732 All normal editing commands are switched off.
12733 \\<gnus-server-mode-map>
12734
12735 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]'). 
12736
12737 The following commands are available:
12738
12739 \\{gnus-server-mode-map}"
12740   (interactive)
12741   (if gnus-visual (gnus-server-make-menu-bar))
12742   (kill-all-local-variables)
12743   (setq mode-line-modified "-- ")
12744   (make-local-variable 'mode-line-format)
12745   (setq mode-line-format (copy-sequence mode-line-format))
12746   (and (equal (nth 3 mode-line-format) "   ")
12747        (setcar (nthcdr 3 mode-line-format) ""))
12748   (setq major-mode 'gnus-server-mode)
12749   (setq mode-name "Server")
12750 ;  (gnus-group-set-mode-line)
12751   (setq mode-line-process nil)
12752   (use-local-map gnus-server-mode-map)
12753   (buffer-disable-undo (current-buffer))
12754   (setq truncate-lines t)
12755   (setq buffer-read-only t)
12756   (run-hooks 'gnus-server-mode-hook))
12757
12758 (defun gnus-server-insert-server-line (sformat name method)
12759   (let* ((sformat (or sformat gnus-server-line-format-spec))
12760          (how (car method))
12761          (where (nth 1 method))
12762          b)
12763     (beginning-of-line)
12764     (setq b (point))
12765     ;; Insert the text.
12766     (insert (eval sformat))
12767     (add-text-properties 
12768      b (1+ b) (list 'gnus-server (intern name)))))
12769
12770 (defun gnus-server-setup-buffer ()
12771   (if (get-buffer gnus-server-buffer)
12772       ()
12773     (save-excursion
12774       (set-buffer (get-buffer-create gnus-server-buffer))
12775       (gnus-server-mode)
12776       (and gnus-carpal (gnus-carpal-setup-buffer 'server)))))
12777
12778 (defun gnus-server-prepare ()
12779   (setq gnus-server-mode-line-format-spec 
12780         (gnus-parse-format gnus-server-mode-line-format 
12781                            gnus-server-mode-line-format-alist))
12782   (setq gnus-server-line-format-spec 
12783         (gnus-parse-format gnus-server-line-format 
12784                            gnus-server-line-format-alist))
12785   (let ((alist gnus-server-alist)
12786         (buffer-read-only nil))
12787     (erase-buffer)
12788     (while alist
12789       (gnus-server-insert-server-line nil (car (car alist)) (cdr (car alist)))
12790       (setq alist (cdr alist))))
12791   (goto-char (point-min))
12792   (gnus-server-position-cursor))
12793
12794 (defun gnus-server-server-name ()
12795   (let ((server (get-text-property (gnus-point-at-bol) 'gnus-server)))
12796     (and server (symbol-name server))))
12797
12798 (defalias 'gnus-server-position-cursor 'gnus-goto-colon)
12799
12800 (defconst gnus-server-edit-buffer "*Gnus edit server*")
12801
12802 (defun gnus-server-update-server (server)
12803   (save-excursion
12804     (set-buffer gnus-server-buffer)
12805     (let ((buffer-read-only nil)
12806           (info (cdr (assoc server gnus-server-alist))))
12807       (gnus-dribble-enter 
12808        (concat "(gnus-server-set-info \"" server "\" '"
12809                (prin1-to-string info) ")"))
12810       ;; Buffer may be narrowed.
12811       (save-restriction
12812         (widen)
12813         (if (gnus-server-goto-server server)
12814             (delete-region (progn (beginning-of-line) (point))
12815                            (progn (forward-line 1) (point))))
12816         (let ((entry (assoc server gnus-server-alist)))
12817           (gnus-server-insert-server-line nil (car entry) (cdr entry))
12818           (gnus-server-position-cursor))))))
12819
12820 (defun gnus-server-set-info (server info)
12821   ;; Enter a select method into the virtual server alist.
12822   (gnus-dribble-enter 
12823    (concat "(gnus-server-set-info \"" server "\" '"
12824            (prin1-to-string info) ")"))
12825   (let* ((server (nth 1 info))
12826          (entry (assoc server gnus-server-alist)))
12827     (if entry (setcdr entry info)
12828       (setq gnus-server-alist
12829             (nconc gnus-server-alist (list (cons server info)))))))
12830
12831 (defun gnus-server-to-method (server)
12832   ;; Map virtual server names to select methods.
12833   (or (and (equal server "native") gnus-select-method)
12834       (cdr (assoc server gnus-server-alist))))
12835
12836 (defun gnus-server-extend-method (group method)
12837   ;; This function "extends" a virtual server.  If the server is
12838   ;; "hello", and the select method is ("hello" (my-var "something")) 
12839   ;; in the group "alt.alt", this will result in a new virtual server
12840   ;; called "helly+alt.alt".
12841   (let ((entry
12842          (gnus-copy-sequence 
12843           (if (equal (car method) "native") gnus-select-method
12844               (cdr (assoc (car method) gnus-server-alist))))))
12845     (setcar (cdr entry) (concat (nth 1 entry) "+" group))
12846     (nconc entry (cdr method))))
12847
12848 (defun gnus-server-get-method (group method)
12849   ;; Input either a server name, and extended server name, or a
12850   ;; select method, and return a select method. 
12851   (cond ((stringp method)
12852          (gnus-server-to-method method))
12853         ((stringp (car method))
12854          (gnus-server-extend-method group method))
12855         (t
12856          (gnus-server-add-address method))))
12857
12858 (defun gnus-server-add-address (method)
12859   (let ((method-name (symbol-name (car method))))
12860     (if (and (memq 'address (assoc method-name gnus-valid-select-methods))
12861              (not (assq (intern (concat method-name "-address")) method)))
12862         (append method (list (list (intern (concat method-name "-address"))
12863                                    (nth 1 method))))
12864       method)))
12865
12866 (defun gnus-server-equal (s1 s2)
12867   (or (equal s1 s2)
12868       (and (= (length s1) (length s2))
12869            (progn
12870              (while (and s1 (member (car s1) s2))
12871                (setq s1 (cdr s1)))
12872              (null s1)))))
12873
12874 ;;; Interactive server functions.
12875
12876 (defun gnus-server-kill-server (server)
12877   "Kill the server on the current line."
12878   (interactive (list (gnus-server-server-name)))
12879   (or (gnus-server-goto-server server)
12880       (if server (error "No such server: %s" server)
12881         (error "No server on the current line")))
12882   (let ((buffer-read-only nil))
12883     (delete-region (progn (beginning-of-line) (point))
12884                    (progn (forward-line 1) (point))))
12885   (setq gnus-server-killed-servers 
12886         (cons (assoc server gnus-server-alist) gnus-server-killed-servers))
12887   (setq gnus-server-alist (delq (car gnus-server-killed-servers)
12888                                 gnus-server-alist))
12889   (gnus-server-position-cursor))
12890
12891 (defun gnus-server-yank-server ()
12892   "Yank the previously killed server."
12893   (interactive)
12894   (or gnus-server-killed-servers
12895       (error "No killed servers to be yanked"))
12896   (let ((alist gnus-server-alist)
12897         (server (gnus-server-server-name))
12898         (killed (car gnus-server-killed-servers)))
12899     (if (not server) 
12900         (setq gnus-server-alist (nconc gnus-server-alist (list killed)))
12901       (if (string= server (car (car gnus-server-alist)))
12902           (setq gnus-server-alist (cons killed gnus-server-alist))
12903         (while (and (cdr alist)
12904                     (not (string= server (car (car (cdr alist))))))
12905           (setq alist (cdr alist)))
12906         (setcdr alist (cons killed (cdr alist)))))
12907     (gnus-server-update-server (car killed))
12908     (setq gnus-server-killed-servers (cdr gnus-server-killed-servers))
12909     (gnus-server-position-cursor)))
12910
12911 (defun gnus-server-exit ()
12912   "Return to the group buffer."
12913   (interactive)
12914   (kill-buffer (current-buffer))
12915   (switch-to-buffer gnus-group-buffer))
12916
12917 (defun gnus-server-list-servers ()
12918   "List all available servers."
12919   (interactive)
12920   (let ((cur (gnus-server-server-name)))
12921     (gnus-server-prepare)
12922     (if cur (gnus-server-goto-server cur)
12923       (goto-char (point-max))
12924       (forward-line -1))
12925     (gnus-server-position-cursor)))
12926
12927 (defun gnus-server-copy-server (from to)
12928   (interactive
12929    (list
12930     (or (gnus-server-server-name)
12931         (error "No server on the current line"))
12932     (read-string "Copy to: ")))
12933   (or from (error "No server on current line"))
12934   (or (and to (not (string= to ""))) (error "No name to copy to"))
12935   (and (assoc to gnus-server-alist) (error "%s already exists" to))
12936   (or (assoc from gnus-server-alist) 
12937       (error "%s: no such server" from))
12938   (let ((to-entry (gnus-copy-sequence (assoc from gnus-server-alist))))
12939     (setcar to-entry to)
12940     (setcar (nthcdr 2 to-entry) to)
12941     (setq gnus-server-killed-servers 
12942           (cons to-entry gnus-server-killed-servers))
12943     (gnus-server-yank-server)))
12944
12945 (defun gnus-server-add-server (how where)
12946   (interactive 
12947    (list (intern (completing-read "Server method: "
12948                                   gnus-valid-select-methods nil t))
12949          (read-string "Server name: ")))
12950   (setq gnus-server-killed-servers 
12951         (cons (list where how where) gnus-server-killed-servers))
12952   (gnus-server-yank-server))
12953
12954 (defun gnus-server-goto-server (server)
12955   "Jump to a server line."
12956   (interactive
12957    (list (completing-read "Goto server: " gnus-server-alist nil t)))
12958   (let ((to (text-property-any (point-min) (point-max) 
12959                                'gnus-server (intern server))))
12960     (and to
12961          (progn
12962            (goto-char to) 
12963            (gnus-server-position-cursor)))))
12964
12965 (defun gnus-server-edit-server (server)
12966   "Edit the server on the current line."
12967   (interactive (list (gnus-server-server-name)))
12968   (or server
12969       (error "No server on current line"))
12970   (let ((winconf (current-window-configuration)))
12971     (get-buffer-create gnus-server-edit-buffer)
12972     (gnus-configure-windows 'edit-server)
12973     (gnus-add-current-to-buffer-list)
12974     (emacs-lisp-mode)
12975     (make-local-variable 'gnus-prev-winconf)
12976     (setq gnus-prev-winconf winconf)
12977     (use-local-map (copy-keymap (current-local-map)))
12978     (let ((done-func '(lambda () 
12979                         "Exit editing mode and update the information."
12980                         (interactive)
12981                         (gnus-server-edit-server-done 'group))))
12982       (setcar (cdr (nth 4 done-func)) server)
12983       (local-set-key "\C-c\C-c" done-func))
12984     (erase-buffer)
12985     (insert ";; Type `C-c C-c' after you have edited the server.\n\n")
12986     (insert (pp-to-string (cdr (assoc server gnus-server-alist))))))
12987
12988 (defun gnus-server-edit-server-done (server)
12989   (interactive)
12990   (set-buffer (get-buffer-create gnus-server-edit-buffer))
12991   (goto-char (point-min))
12992   (let ((form (read (current-buffer)))
12993         (winconf gnus-prev-winconf))
12994     (gnus-server-set-info server form)
12995     (kill-buffer (current-buffer))
12996     (and winconf (set-window-configuration winconf))
12997     (set-buffer gnus-server-buffer)
12998     (gnus-server-update-server (gnus-server-server-name))
12999     (gnus-server-position-cursor)))
13000
13001 (defun gnus-server-read-server (server)
13002   "Browse a server."
13003   (interactive (list (gnus-server-server-name)))
13004   (gnus-browse-foreign-server (gnus-server-to-method server) (current-buffer)))
13005
13006 (defun gnus-mouse-pick-server (e)
13007   (interactive "e")
13008   (mouse-set-point e)
13009   (gnus-server-read-server (gnus-server-server-name)))
13010
13011 ;;;
13012 ;;; entry points into gnus-score.el
13013 ;;;
13014
13015 ;;; Finding score files. 
13016
13017 (defvar gnus-global-score-files nil
13018   "*List of global score files and directories.
13019 Set this variable if you want to use people's score files.  One entry
13020 for each score file or each score file directory.  Gnus will decide
13021 by itself what score files are applicable to which group.
13022
13023 Say you want to use the single score file
13024 \"/ftp.ifi.uio.no@ftp:/pub/larsi/ding/score/soc.motss.SCORE\" and all
13025 score files in the \"/ftp.some-where:/pub/score\" directory.
13026
13027  (setq gnus-global-score-files
13028        '(\"/ftp.ifi.uio.no:/pub/larsi/ding/score/soc.motss.SCORE\"
13029          \"/ftp.some-where:/pub/score\"))")
13030
13031 (defun gnus-score-score-files (group)
13032   "Return a list of all possible score files."
13033   ;; Search and set any global score files.
13034   (and gnus-global-score-files 
13035        (or gnus-internal-global-score-files
13036            (gnus-score-search-global-directories gnus-global-score-files)))
13037   ;; Fix the kill-file dir variable.
13038   (setq gnus-kill-files-directory 
13039         (file-name-as-directory
13040          (or gnus-kill-files-directory "~/News/")))
13041   ;; If we can't read it, there are no score files.
13042   (if (not (file-exists-p (expand-file-name gnus-kill-files-directory)))
13043       (setq gnus-score-file-list nil)
13044     (if (gnus-use-long-file-name 'not-score)
13045         ;; We want long file names.
13046         (if (or (not gnus-score-file-list)
13047                 (not (car gnus-score-file-list))
13048                 (gnus-file-newer-than gnus-kill-files-directory
13049                                       (car gnus-score-file-list)))
13050               (setq gnus-score-file-list 
13051                     (cons (nth 5 (file-attributes gnus-kill-files-directory))
13052                           (nreverse 
13053                            (directory-files 
13054                             gnus-kill-files-directory t 
13055                             (gnus-score-file-regexp))))))
13056       ;; We do not use long file names, so we have to do some
13057       ;; directory traversing.  
13058       (let ((mdir (length (expand-file-name gnus-kill-files-directory)))
13059             (suffixes (list gnus-score-file-suffix gnus-adaptive-file-suffix))
13060             dir files suffix)
13061         (while suffixes
13062           (setq dir (expand-file-name
13063                      (concat gnus-kill-files-directory
13064                              (gnus-replace-chars-in-string group ?. ?/))))
13065           (setq suffix (car suffixes)
13066                 suffixes (cdr suffixes))
13067           (if (file-exists-p (concat dir "/" suffix))
13068               (setq files (cons (concat dir "/" suffix) files)))
13069           (while (>= (1+ (length dir)) mdir)
13070             (and (file-exists-p (concat dir "/all/" suffix))
13071                  (setq files (cons (concat dir "/all/" suffix) files)))
13072             (string-match "/[^/]*$" dir)
13073             (setq dir (substring dir 0 (match-beginning 0)))))
13074         (setq gnus-score-file-list 
13075               (cons nil (nreverse files)))))
13076     (cdr gnus-score-file-list)))
13077
13078 (defun gnus-score-file-regexp ()
13079   (concat "\\(" gnus-score-file-suffix 
13080           "\\|" gnus-adaptive-file-suffix "\\)$"))
13081         
13082 (defun gnus-score-find-bnews (group)
13083   "Return a list of score files for GROUP.
13084 The score files are those files in the ~/News directory which matches
13085 GROUP using BNews sys file syntax."
13086   (let* ((sfiles (append (gnus-score-score-files group)
13087                          gnus-internal-global-score-files))
13088          (kill-dir (file-name-as-directory 
13089                     (expand-file-name gnus-kill-files-directory)))
13090          (klen (length kill-dir))
13091          ofiles not-match regexp)
13092     (save-excursion
13093       (set-buffer (get-buffer-create "*gnus score files*"))
13094       (buffer-disable-undo (current-buffer))
13095       ;; Go through all score file names and create regexp with them
13096       ;; as the source.  
13097       (while sfiles
13098         (erase-buffer)
13099         (insert (car sfiles))
13100         (goto-char (point-min))
13101         ;; First remove the suffix itself.
13102         (re-search-forward (concat "." (gnus-score-file-regexp)))
13103         (replace-match "" t t) 
13104         (goto-char (point-min))
13105         (if (looking-at (regexp-quote kill-dir))
13106             ;; If the file name was just "SCORE", `klen' is one character
13107             ;; too much.
13108             (delete-char (min (1- (point-max)) klen))
13109           (goto-char (point-max))
13110           (search-backward "/")
13111           (delete-region (1+ (point)) (point-min)))
13112         ;; If short file names were used, we have to translate slashes.
13113         (goto-char (point-min))
13114         (while (search-forward "/" nil t)
13115           (replace-match "." t t))
13116         ;; Translate "all" to ".*".
13117         (while (search-forward "all" nil t)
13118           (replace-match ".*" t t))
13119         (goto-char (point-min))
13120         ;; Deal with "not."s.
13121         (if (looking-at "not.")
13122             (progn
13123               (setq not-match t)
13124               (setq regexp (buffer-substring 5 (point-max))))
13125           (setq regexp (buffer-substring 1 (point-max)))
13126           (setq not-match nil))
13127         ;; Finally - if this resulting regexp matches the group name,
13128         ;; we add this score file to the list of score files
13129         ;; applicable to this group.
13130         (if (or (and not-match
13131                      (not (string-match regexp group)))
13132                 (and (not not-match)
13133                      (string-match regexp group)))
13134             (setq ofiles (cons (car sfiles) ofiles)))
13135         (setq sfiles (cdr sfiles)))
13136       (kill-buffer (current-buffer))
13137       ;; Slight kludge here - the last score file returned should be
13138       ;; the local score file, whether it exists or not. This is so
13139       ;; that any score commands the user enters will go to the right
13140       ;; file, and not end up in some global score file.
13141       (let ((localscore
13142              (expand-file-name
13143               (if (gnus-use-long-file-name 'not-score)
13144                   (concat gnus-kill-files-directory group "." 
13145                           gnus-score-file-suffix)
13146                 (concat gnus-kill-files-directory
13147                         (gnus-replace-chars-in-string group ?. ?/)
13148                         "/" gnus-score-file-suffix)))))
13149         (and (member localscore ofiles)
13150              (delete localscore ofiles))
13151         (setq ofiles (cons localscore ofiles)))
13152       (nreverse ofiles))))
13153
13154 (defun gnus-score-find-single (group)
13155   "Return list containing the score file for GROUP."
13156   (list (gnus-score-file-name group gnus-adaptive-file-suffix)
13157         (gnus-score-file-name group)))
13158
13159 (defun gnus-score-find-hierarchical (group)
13160   "Return list of score files for GROUP.
13161 This includes the score file for the group and all its parents."
13162   (let ((all (copy-sequence '(nil)))
13163         (start 0))
13164     (while (string-match "\\." group (1+ start))
13165       (setq start (match-beginning 0))
13166       (setq all (cons (substring group 0 start) all)))
13167     (setq all (cons group all))
13168     (nconc
13169      (mapcar 'gnus-score-file-name (setq all (nreverse all))
13170              gnus-adaptive-file-suffix)
13171      (mapcar 'gnus-score-file-name all))))
13172
13173 (defvar gnus-score-file-alist-cache nil)
13174
13175 (defun gnus-score-find-alist (group)
13176   "Return list of score files for GROUP.
13177 The list is determined from the variable gnus-score-file-alist."
13178   (let ((alist gnus-score-file-multiple-match-alist)
13179         score-files)
13180     ;; if this group has been seen before, return the cached entry
13181     (if (setq score-files (assoc group gnus-score-file-alist-cache))
13182         (cdr score-files)       ; ensures caching of groups with no matches
13183       ;; handle the multiple match alist
13184       (while alist
13185         (and (string-match (car (car alist)) group)
13186              (setq score-files
13187                    (nconc score-files (cdr (car alist)))))
13188         (setq alist (cdr alist)))
13189       (setq alist gnus-score-file-single-match-alist)
13190       ;; handle the single match alist
13191       (catch 'done
13192         (while alist
13193           (and (string-match (car (car alist)) group)
13194                ;; progn used just in case ("regexp") has no files
13195                ;; and score-files is still nil. -sj
13196                ;; this can be construed as a "stop searching here" feature :>
13197                ;; and used to simplify regexps in the single-alist 
13198                (progn
13199                  (setq score-files
13200                        (nconc score-files (cdr (car alist))))
13201                  (throw 'done nil)))
13202           (setq alist (cdr alist))))
13203       ;; cache the score files
13204       (setq gnus-score-file-alist-cache
13205             (cons (cons group score-files) gnus-score-file-alist-cache))
13206       score-files)))
13207
13208
13209 (defun gnus-possibly-score-headers (&optional trace)
13210   (let ((func gnus-score-find-score-files-function)
13211         score-files scores)
13212     (and func (not (listp func))
13213          (setq func (list func)))
13214     ;; Go through all the functions for finding score files (or actual
13215     ;; scores) and add them to a list.
13216     (setq score-files (gnus-score-find-alist gnus-newsgroup-name))
13217     (while func
13218       (and (symbolp (car func))
13219            (fboundp (car func))
13220            (setq score-files 
13221                  (nconc score-files (funcall (car func) gnus-newsgroup-name))))
13222       (setq func (cdr func)))
13223     (if score-files (gnus-score-headers score-files trace))))
13224
13225 (defun gnus-score-file-name (newsgroup &optional suffix)
13226   "Return the name of a score file for NEWSGROUP."
13227   (let ((suffix (or suffix gnus-score-file-suffix)))
13228     (cond  ((or (null newsgroup)
13229                 (string-equal newsgroup ""))
13230             ;; The global score file is placed at top of the directory.
13231             (expand-file-name 
13232              suffix (or gnus-kill-files-directory "~/News")))
13233            ((gnus-use-long-file-name 'not-score)
13234             ;; Append ".SCORE" to newsgroup name.
13235             (expand-file-name (concat newsgroup "." suffix)
13236                               (or gnus-kill-files-directory "~/News")))
13237            (t
13238             ;; Place "SCORE" under the hierarchical directory.
13239             (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
13240                                       "/" suffix)
13241                               (or gnus-kill-files-directory "~/News"))))))
13242
13243 (defun gnus-score-search-global-directories (files)
13244   "Scan all global score directories for score files."
13245   ;; Set the variable `gnus-internal-global-score-files' to all
13246   ;; available global score files.
13247   (interactive (list gnus-global-score-files))
13248   (let (out)
13249     (while files
13250       (if (string-match "/$" (car files))
13251           (setq out (nconc (directory-files 
13252                             (car files) t
13253                             (concat (gnus-score-file-regexp) "$"))))
13254         (setq out (cons (car files) out)))
13255       (setq files (cdr files)))
13256     (setq gnus-internal-global-score-files out)))
13257
13258 ;; Allow redefinition of Gnus functions.
13259
13260 (gnus-ems-redefine)
13261
13262 (provide 'gnus)
13263
13264 ;;; gnus.el ends here